From 9e6ca4a01d2462491323dbe04cd14ec34303e503 Mon Sep 17 00:00:00 2001 From: Fen Date: Thu, 19 Nov 2026 15:26:21 +0800 Subject: [PATCH 1/2] docs: add repository and design guidelines (#1678) ## Proposed Changes - Add `AGENTS.md` with repository structure, development commands, coding conventions, testing guidance, and commit/PR expectations. - Add an English `DESIGN.md` following the design.md alpha specification, documenting React Bootstrap as the design foundation and the default primary color `#0033FF`. - Document existing theme overrides, dark mode, layout conventions, and component usage, with YAML design tokens and source references. ## Validation - Checked commands and conventions against repository configuration and source files. - Confirmed `DESIGN.md` contains no Chinese text. - `git diff --cached --check` passed before committing. - Documentation-only changes; no application tests run. --- AGENTS.md | 31 ++++++++++++++++++ DESIGN.md | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 AGENTS.md create mode 100644 DESIGN.md diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 000000000..c2f36934b --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,31 @@ +# Repository Guidelines + +## Project Structure & Module Organization + +Apache Answer combines a Go backend with a React/TypeScript frontend. `cmd/answer` is the executable entry point; `internal/` contains controllers, services, repositories, schemas, and migrations. Shared Go utilities live in `pkg/`, and extension interfaces live in `plugin/`. Frontend pages, components, stores, and API clients are under `ui/src/`; static assets are in `ui/public/`, and production output is embedded from `ui/build/`. Translations live in `i18n/`, configuration examples in `configs/`, and deployment resources in `charts/`. Go tests generally sit beside their implementation; repository integration tests live in `internal/repo/repo_test/`. + +## Build, Test, and Development Commands + +Use Go 1.25+, Node.js matching `ui/package.json` (`^20.19.0 || >=22.12.0`), and pnpm 9.7.0. `make lint` also requires Docker or Podman installed and running for the ASF header formatter. + +- `make generate`: install generators, regenerate Go code/API documentation, and tidy modules. +- `make ui`: install frontend dependencies and build the UI. +- `make build`: regenerate code and build the `answer` executable. +- `./answer init -C ./data` then `./answer run -C ./data`: initialize and run locally. +- Before `cd ui && pnpm start` (Vite, port 3000), set `REACT_APP_API_URL=http://localhost:8080/` in `ui/.env.development.local`, matching your backend address. This overrides the checked-in private proxy target; restart Vite after changes. +- `make test`: run repository integration tests. +- `go test ./...`: run the full Go test suite. +- `make lint`: regenerate code, check ASF headers, and run golangci-lint. +- `cd ui && pnpm lint`: run ESLint with automatic fixes; `pnpm prettier` formats frontend sources. + +## Coding Style & Naming Conventions + +Format Go with `gofmt`; use tabs and conventional exported `PascalCase` names. Follow existing snake_case Go filenames and package organization. Frontend code uses two spaces, single quotes, trailing commas, and an 80-column Prettier target. Use PascalCase component directories and follow neighboring files. Respect `.editorconfig`, ESLint, and ASF license headers. Regenerate generated files through tooling rather than editing them manually. + +## Testing Guidelines + +Use Go's `testing` package and existing Testify assertions. Name files `*_test.go` and functions `TestXxx`. Add regression coverage for changed behavior. Repository tests default to SQLite; MySQL/PostgreSQL variants use `TEST_DB_DRIVER` and Docker. No fixed coverage threshold or frontend test script is configured; verify UI changes with `pnpm build` and browser checks. + +## Commit & Pull Request Guidelines + +Follow history's conventional prefixes: `fix:`, `feat:`, `build:`, `test:`, or `chore:`, with optional scope, such as `fix(security): enforce visibility`. Keep commits focused. Follow the PR template: link the issue with `Fixes #...` and describe proposed changes. Include validation results and screenshots for visible UI changes. Review generated changes before committing; CI checks linting and unexpected generated diffs. diff --git a/DESIGN.md b/DESIGN.md new file mode 100644 index 000000000..919236989 --- /dev/null +++ b/DESIGN.md @@ -0,0 +1,98 @@ +--- +version: alpha +name: Apache Answer +description: A React Bootstrap interface for Q&A communities, branded through the primary color. +omitted: + - section: typography + reason: Inherits Bootstrap typography without separate brand typography tokens. + - section: spacing + reason: Uses Bootstrap spacing utilities and existing page layouts without a separate spacing scale. + - section: rounded + reason: Inherits default Bootstrap component radii. + - section: motion + reason: Inherits Bootstrap and React Bootstrap component transitions. +--- + +# Apache Answer Design System + +## Overview + +Apache Answer supports Q&A communities and knowledge sharing. Keep the interface clear and practical, prioritizing readable questions, answers, and actions. + +The foundation is **React Bootstrap 2.10.6 + Bootstrap 5.3.3** (currently locked versions). Brand customization changes only the primary color. Component structure, sizing, typography, radii, and interactions follow Bootstrap defaults. Reuse existing domain styles and layouts without expanding them into a separate component design system. + +Dependency sources: `@ui/package.json` and `@ui/pnpm-lock.yaml`. Global stylesheet entry point: `@ui/src/index.scss`. + +## Colors + +The default brand primary is **Answer Blue / `#0033FF`**, used for primary actions, links, selected states, and branded navigation. Success, warning, danger, secondary actions, and neutrals retain Bootstrap semantic colors. Do not turn every state blue. + +```yaml +colors: + primary: "#0033FF" +``` + +This token represents the default theme. The site setting `theme_config[theme].primary_color` can override the active primary color. Design previews should reflect the target site's configuration. + +At build time, `@ui/src/common/_variable.scss` sets the default primary through `$blue` before Bootstrap is imported. At runtime, `@ui/src/components/CustomizeTheme/index.tsx` synchronizes primary colors, links, buttons, pagination, selected items, and form focus states. Reuse this mechanism to derive hover, active, focus, and disabled states instead of introducing isolated color values. + +Light and dark modes use `data-bs-theme`. Bootstrap semantic variables and `@ui/src/common/color.scss` adapt surfaces, text, and borders. Dark mode links use lighter primary shades derived by the existing theme logic. Avoid hardcoded light backgrounds in components. + +## Typography + +Use Bootstrap's default system font stack, sizes, weights, and line heights. Establish hierarchy with semantic headings, body text, and existing utilities such as `small`. Keep the existing typography scale and system fonts. Project utilities such as `.small` and `.fs-12` follow their global stylesheet definitions. + +Long questions, answers, and code should wrap or scroll locally without overflowing the page. Preserve semantic headings, form labels, and body structure. + +## Layout + +Use Bootstrap responsive containers, grids, breakpoints, and spacing utilities. Reuse neighboring page layouts first. Pages typically combine navigation, main content, and an optional sidebar. Stack content naturally on narrow screens and keep primary actions accessible. + +The existing sidebar layout is defined in `@ui/src/common/sideNavLayout.scss`: `.main-mx-with` has a maximum width of `1072px`; `.page-right-side` has a desktop width of `300px` and becomes full width below `1199.9px`. Inherit these values when using that layout, rather than imposing them on every page. + +## Elevation & Depth + +Use Bootstrap borders, surface layers, and default overlay styles to distinguish regions. Preserve the existing navigation shadow. Keep ordinary content areas free of additional decorative shadows. Dropdowns, tooltips, and modals follow component defaults for stacking and positioning. + +## Shapes + +Buttons, inputs, cards, and modals retain Bootstrap default radii and borders. Domain elements such as avatars and tags reuse their existing component shapes. Do not apply a universal pill or square treatment. + +## Motion & Micro-interaction + +Use React Bootstrap's built-in collapse, fade, and component state feedback. Preserve default transition timing and honor `prefers-reduced-motion`. Avoid decorative looping animation. Asynchronous actions should communicate loading and prevent duplicate submission. + +## Voice & Content Strategy + +Write direct, friendly copy with clear action names and consistent Q&A terminology. Reuse existing translation keys in `i18n/`. Add new copy through the internationalization workflow rather than hardcoding a single language in components. + +Action labels describe outcomes, such as “Post question” and “Save changes.” Empty states explain what is missing and suggest a next step. Errors explain the failure and an actionable remedy. Dates, numbers, and capitalization follow the active locale and neighboring interfaces. + +## Components + +Prefer components exported by `react-bootstrap` and reuse existing domain wrappers in `ui/src/components/`. + +- **Buttons:** Use `Button variant="primary"` for primary actions. Use existing secondary, outline, or link variants for secondary actions. Preserve danger semantics for destructive actions. +- **Forms:** Use standard `Form`, `Form.Control`, and `Form.Select` components. Keep labels, help text, validation messages, and visible focus. Checked states and focus colors follow the theme. +- **Navigation and pagination:** Reuse Nav, Navbar, Dropdown, and Pagination. Selected states follow primary; sizing and keyboard behavior follow component defaults. +- **Content and feedback:** Reuse Card, ListGroup, Alert, Modal, and Tooltip. Question tags retain existing tag components and `.badge-tag` styling. +- **Icons:** Use the project's existing Bootstrap Icons. Give icon buttons accessible names. + +These tokens describe only the brand color mapping for default primary buttons. Bootstrap supplies other properties, and the existing theme implementation derives interaction states. + +```yaml +components: + button-primary: + backgroundColor: "{colors.primary}" + button-outline-primary: + textColor: "{colors.primary}" +``` + +## Do's and Don'ts + +- Prefer component props, Bootstrap utilities, and semantic CSS variables. +- Maintain primary at the theme entry points; avoid repeating hardcoded `#0033FF` in domain components. +- Preserve hover, focus, active, disabled, loading, and validation states. +- Check light mode, dark mode, narrow screens, and keyboard interaction. Keep text, icons, and focus indicators clearly visible. +- Keep the existing font, radius, shadow, spacing, and button systems. +- Preserve danger, success, and warning semantics. Communicate status through more than color alone. From 23e1f8b22ad5a105636b1b40e144aaed5bb71f89 Mon Sep 17 00:00:00 2001 From: lhk Date: Sun, 22 Nov 2026 09:45:07 +0800 Subject: [PATCH 2/2] fix(mcp): honor page and page_size in search tools --- internal/controller/mcp_controller.go | 6 +-- internal/schema/mcp_schema.go | 30 +++++++++++++ internal/schema/mcp_schema_test.go | 60 ++++++++++++++++++++++++++ internal/schema/mcp_tools/mcp_tools.go | 12 ++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 internal/schema/mcp_schema_test.go diff --git a/internal/controller/mcp_controller.go b/internal/controller/mcp_controller.go index eebb8baf0..81e23e859 100644 --- a/internal/controller/mcp_controller.go +++ b/internal/controller/mcp_controller.go @@ -101,8 +101,8 @@ func (c *MCPController) MCPQuestionsHandler() func(ctx context.Context, request searchResp, err := c.searchService.Search(ctx, &schema.SearchDTO{ Query: cond.ToQueryString() + " is:question", - Page: 1, - Size: 5, + Page: cond.Page, + Size: cond.PageSize, Order: "newest", }) if err != nil { @@ -256,7 +256,7 @@ func (c *MCPController) MCPCommentsHandler() func(ctx context.Context, request m } dto := &comment.CommentQuery{ - PageCond: pager.PageCond{Page: 1, PageSize: 5}, + PageCond: pager.PageCond{Page: cond.Page, PageSize: cond.PageSize}, QueryCond: "newest", ObjectID: cond.ObjectID, } diff --git a/internal/schema/mcp_schema.go b/internal/schema/mcp_schema.go index 9afee72ec..a8902bc4e 100644 --- a/internal/schema/mcp_schema.go +++ b/internal/schema/mcp_schema.go @@ -27,6 +27,13 @@ import ( ) const ( + // MCPDefaultPage is the default page number for MCP list tools. + MCPDefaultPage = 1 + // MCPDefaultPageSize is the default number of results per page for MCP list tools. + MCPDefaultPageSize = 5 + // MCPMaxPageSize is the upper bound for page_size so clients cannot request unbounded results. + MCPMaxPageSize = 50 + MCPSearchCondKeyword = "keyword" MCPSearchCondUsername = "username" MCPSearchCondScore = "score" @@ -46,6 +53,8 @@ type MCPSearchCond struct { Score int `json:"score"` Tags []string `json:"tags"` QuestionID string `json:"question_id"` + Page int `json:"page"` + PageSize int `json:"page_size"` } type MCPSearchQuestionDetail struct { @@ -54,6 +63,8 @@ type MCPSearchQuestionDetail struct { type MCPSearchCommentCond struct { ObjectID string `json:"object_id"` + Page int `json:"page"` + PageSize int `json:"page_size"` } type MCPSearchTagCond struct { @@ -159,6 +170,7 @@ func NewMCPSearchCond(request mcp.CallToolRequest) *MCPSearchCond { if questionID, ok := getRequestValue(request, MCPSearchCondQuestionID); ok { cond.QuestionID = questionID } + cond.Page, cond.PageSize = newMCPPagination(request) return cond } @@ -183,9 +195,27 @@ func NewMCPSearchCommentCond(request mcp.CallToolRequest) *MCPSearchCommentCond if keyword, ok := getRequestValue(request, MCPSearchCondObjectID); ok { cond.ObjectID = keyword } + cond.Page, cond.PageSize = newMCPPagination(request) return cond } +// newMCPPagination reads the page and page_size arguments, applying defaults +// and an upper bound on page_size. +func newMCPPagination(request mcp.CallToolRequest) (page, pageSize int) { + page = request.GetInt(MCPSearchCondPage, MCPDefaultPage) + if page < 1 { + page = MCPDefaultPage + } + pageSize = request.GetInt(MCPSearchCondPageSize, MCPDefaultPageSize) + if pageSize < 1 { + pageSize = MCPDefaultPageSize + } + if pageSize > MCPMaxPageSize { + pageSize = MCPMaxPageSize + } + return page, pageSize +} + func NewMCPSearchTagCond(request mcp.CallToolRequest) *MCPSearchTagCond { cond := &MCPSearchTagCond{} if tagName, ok := getRequestValue(request, MCPSearchCondTagName); ok { diff --git a/internal/schema/mcp_schema_test.go b/internal/schema/mcp_schema_test.go new file mode 100644 index 000000000..6874132bd --- /dev/null +++ b/internal/schema/mcp_schema_test.go @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package schema + +import ( + "testing" + + "github.com/mark3labs/mcp-go/mcp" +) + +func TestNewMCPSearchCondPagination(t *testing.T) { + testCases := []struct { + name string + arguments map[string]any + wantPage int + wantPageSize int + }{ + {"defaults", nil, MCPDefaultPage, MCPDefaultPageSize}, + {"explicit page and page size", map[string]any{"page": float64(3), "page_size": float64(20)}, 3, 20}, + {"string arguments", map[string]any{"page": "2", "page_size": "10"}, 2, 10}, + {"non-positive values fall back to defaults", map[string]any{"page": float64(0), "page_size": float64(-1)}, MCPDefaultPage, MCPDefaultPageSize}, + {"page size is capped", map[string]any{"page_size": float64(1000)}, MCPDefaultPage, MCPMaxPageSize}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + request := mcp.CallToolRequest{} + request.Params.Arguments = testCase.arguments + + cond := NewMCPSearchCond(request) + if cond.Page != testCase.wantPage || cond.PageSize != testCase.wantPageSize { + t.Fatalf("NewMCPSearchCond() pagination = (%d, %d), want (%d, %d)", + cond.Page, cond.PageSize, testCase.wantPage, testCase.wantPageSize) + } + + commentCond := NewMCPSearchCommentCond(request) + if commentCond.Page != testCase.wantPage || commentCond.PageSize != testCase.wantPageSize { + t.Fatalf("NewMCPSearchCommentCond() pagination = (%d, %d), want (%d, %d)", + commentCond.Page, commentCond.PageSize, testCase.wantPage, testCase.wantPageSize) + } + }) + } +} diff --git a/internal/schema/mcp_tools/mcp_tools.go b/internal/schema/mcp_tools/mcp_tools.go index 3ae6b3bea..c7110c40d 100644 --- a/internal/schema/mcp_tools/mcp_tools.go +++ b/internal/schema/mcp_tools/mcp_tools.go @@ -51,6 +51,12 @@ func NewQuestionsTool() mcp.Tool { mcp.WithString(schema.MCPSearchCondScore, mcp.Description("Minimum score that the question must have"), ), + mcp.WithNumber(schema.MCPSearchCondPage, + mcp.Description("Page number to return (default 1)"), + ), + mcp.WithNumber(schema.MCPSearchCondPageSize, + mcp.Description("Number of results per page (default 5, max 50)"), + ), ) return listFilesTool } @@ -71,6 +77,12 @@ func NewCommentsTool() mcp.Tool { mcp.WithString(schema.MCPSearchCondObjectID, mcp.Description("Queries comments on an object, either a question or an answer. object_id is the id of the object."), ), + mcp.WithNumber(schema.MCPSearchCondPage, + mcp.Description("Page number to return (default 1)"), + ), + mcp.WithNumber(schema.MCPSearchCondPageSize, + mcp.Description("Number of results per page (default 5, max 50)"), + ), ) return listFilesTool }