From ae86d56a7dc8b6391653eeb4c2e0ec4703129e7e Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Sat, 15 Aug 2026 11:00:30 +0800 Subject: [PATCH] feat(webui): improve AGENTS.md editor placeholder and read-only notice (#310) * feat(webui): improve AGENTS.md editor placeholder and read-only notice * docs: add AGENTS.md documentation under Shumai Agent Settings --- docs/docs.json | 1 + docs/using-shumai/agents-md.mdx | 53 +++++++ .../agents-md/agents-md-editor.test.tsx | 147 ++++++++++++++++++ .../components/agents-md/agents-md-editor.tsx | 34 ++-- .../markdown-editor/markdown-editor.css | 3 + .../markdown-editor/markdown-editor.tsx | 2 +- packages/webui/messages/en.json | 4 +- packages/webui/messages/zh.json | 4 +- .../messages/agents_md_placeholder.d.ts | 2 +- .../messages/agents_md_placeholder.js | 22 ++- .../messages/agents_md_readonly_hint.d.ts | 2 +- .../messages/agents_md_readonly_hint.js | 6 +- 12 files changed, 251 insertions(+), 29 deletions(-) create mode 100644 docs/using-shumai/agents-md.mdx create mode 100644 packages/webui/components/agents-md/agents-md-editor.test.tsx diff --git a/docs/docs.json b/docs/docs.json index 1c1cb102..5bbb88a2 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -43,6 +43,7 @@ "pages": [ "using-shumai/agent-providers", "using-shumai/agents", + "using-shumai/agents-md", "using-shumai/agent-skills", "using-shumai/agent-tools", "using-shumai/mcp-servers", diff --git a/docs/using-shumai/agents-md.mdx b/docs/using-shumai/agents-md.mdx new file mode 100644 index 00000000..1ef51f79 --- /dev/null +++ b/docs/using-shumai/agents-md.mdx @@ -0,0 +1,53 @@ +--- +title: AGENTS.md Context & Instructions +description: Provide hierarchical, folder-specific instructions and context to AI agents. +--- + +In addition to global agent system prompts and skills, Shumai allows you to define project-level and folder-level instructions using **`AGENTS.md`**. + +When an AI agent (such as a Chat Agent or Autofill Agent) operates within a project or folder, Shumai automatically loads the corresponding `AGENTS.md` files to provide context, operational rules, and project-specific knowledge. + +--- + +## Hierarchical Traversal & Inheritance + +Shumai uses a hierarchical context model for `AGENTS.md`: + +- **Upward Traversal**: When an agent works on a file or folder, Shumai traverses the folder tree upwards from the current folder all the way to the **project root level**. +- **Combined Context**: All `AGENTS.md` files found along this path are loaded into the agent's context in order (from the project root down to the current folder). +- **Layered Rules**: This lets you define broad, project-wide rules and guidelines at the project root `AGENTS.md`, while adding specific rules, vocabulary, or autofill hints in nested subfolder `AGENTS.md` files. + +--- + +## What to Include in AGENTS.md + +You can use `AGENTS.md` to define any instructions you want the agent to follow when working in that folder or project: + +- **Project & Folder Overview**: Describe what assets are stored in the folder and how they relate to the broader project. +- **Agent Behavioral Rules**: Specify guardrails, tone, required formats, or things the agent should and shouldn't do when answering queries or leaving comments. +- **Terminology & Domain Knowledge**: Define specialized vocabulary, acronyms, brand naming conventions, or project-specific jargon. +- **Metadata & Autofill Instructions**: Guide the **Autofill Agent** on how to summarize assets, generate tags, or populate specific custom metadata fields for files in this folder. + +--- + +## Accessing and Editing AGENTS.md + +### Opening the Editor + +To view or edit `AGENTS.md`: + +1. Open any project or folder in the Shumai web application. +2. Click the **`AGENTS.md`** button in the top navigation toolbar, or right-click any folder in the file browser and choose **AGENTS.md**. +3. A dedicated WYSIWYG Markdown editor opens with full formatting support (headings, lists, code blocks, bold/italics, and links). + +### Auto-Save + +As you make changes in the editor, Shumai automatically saves your edits in the background with visual save status indicators (`Saving...`, `Saved`). + +--- + +## Permissions & Read-Only Mode + +- **Viewing**: All team members who have read access to the project can view `AGENTS.md`. +- **Editing**: **Only Team Owners** are permitted to create, edit, or delete `AGENTS.md` files. +- **Read-Only State**: Non-owner members will see a clear read-only indicator in the toolbar explaining that only team owners can edit instructions for the file. diff --git a/packages/webui/components/agents-md/agents-md-editor.test.tsx b/packages/webui/components/agents-md/agents-md-editor.test.tsx new file mode 100644 index 00000000..6373766c --- /dev/null +++ b/packages/webui/components/agents-md/agents-md-editor.test.tsx @@ -0,0 +1,147 @@ +// @vitest-environment happy-dom +import { cleanup, render, screen, waitFor } from '@testing-library/react' +import React from 'react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { AgentsMdEditor } from './agents-md-editor' +import { m } from '@/ui/paraglide/messages.js' + +// Mock API client +vi.mock('@/ui/api/client', () => ({ + client: { + api: { + folders: { + ':folderId': { + agentsmd: { + $get: vi.fn(), + $patch: vi.fn(), + }, + }, + }, + }, + }, +})) + +// Mock usePermissions +const mockUsePermissions = vi.fn() +vi.mock('@/ui/hooks/use-permissions', () => ({ + usePermissions: () => mockUsePermissions(), +})) + +// Mock useNavigate +const mockNavigate = vi.fn() +vi.mock('@tanstack/react-router', () => ({ + useNavigate: () => mockNavigate, +})) + +describe('AgentsMdEditor Component', () => { + let queryClient: QueryClient + + beforeEach(() => { + queryClient = new QueryClient({ + defaultOptions: { + queries: { + retry: false, + }, + }, + }) + mockUsePermissions.mockReturnValue({ + role: 'owner', + canEdit: true, + canAdmin: true, + }) + }) + + afterEach(() => { + cleanup() + vi.clearAllMocks() + }) + + it('renders loading state initially', async () => { + const { client } = await import('@/ui/api/client') + const getMock = client.api.folders[':folderId'].agentsmd.$get as unknown as ReturnType< + typeof vi.fn + > + getMock.mockImplementation(() => new Promise(() => {})) + + render( + + + , + ) + + expect(screen.getByText(m.agents_md_loading())).toBeDefined() + }) + + it('renders editable editor for team owner', async () => { + const { client } = await import('@/ui/api/client') + const getMock = client.api.folders[':folderId'].agentsmd.$get as unknown as ReturnType< + typeof vi.fn + > + getMock.mockResolvedValue({ + ok: true, + json: async () => ({ content: '# Instructions' }), + }) + + render( + + + , + ) + + await waitFor(() => { + expect(screen.getByRole('textbox')).toBeDefined() + }) + expect(screen.queryByText(m.agents_md_readonly_hint())).toBeNull() + }) + + it('renders read-only notice and restricts editing for non-owner', async () => { + mockUsePermissions.mockReturnValue({ + role: 'editor', + canEdit: true, + canAdmin: false, + }) + + const { client } = await import('@/ui/api/client') + const getMock = client.api.folders[':folderId'].agentsmd.$get as unknown as ReturnType< + typeof vi.fn + > + getMock.mockResolvedValue({ + ok: true, + json: async () => ({ content: '# Instructions' }), + }) + + const { container } = render( + + + , + ) + + await waitFor(() => { + expect(screen.getByText(m.agents_md_readonly_hint())).toBeDefined() + }) + + expect(container.querySelector('.read-only')).toBeDefined() + }) + + it('displays the friendly multiline placeholder when content is empty', async () => { + const { client } = await import('@/ui/api/client') + const getMock = client.api.folders[':folderId'].agentsmd.$get as unknown as ReturnType< + typeof vi.fn + > + getMock.mockResolvedValue({ + ok: true, + json: async () => ({ content: null }), + }) + + render( + + + , + ) + + await waitFor(() => { + expect(screen.getByText(/parent folders up to the project level/)).toBeDefined() + }) + }) +}) diff --git a/packages/webui/components/agents-md/agents-md-editor.tsx b/packages/webui/components/agents-md/agents-md-editor.tsx index 2b500e64..826be377 100644 --- a/packages/webui/components/agents-md/agents-md-editor.tsx +++ b/packages/webui/components/agents-md/agents-md-editor.tsx @@ -168,7 +168,7 @@ export function AgentsMdEditor({ projectId, assetId, rootFolderId, isRoot }: Age } const rightToolbarContent = ( -
+
{canAdmin ? (
{(saveStatus === 'saving' || isClosing) && ( @@ -192,27 +192,29 @@ export function AgentsMdEditor({ projectId, assetId, rootFolderId, isRoot }: Age
) : (
- - {m.read_only()} + + {m.agents_md_readonly_hint()}
)} - +
+ {canAdmin && } - + +
) diff --git a/packages/webui/components/markdown-editor/markdown-editor.css b/packages/webui/components/markdown-editor/markdown-editor.css index a1360229..3f893921 100644 --- a/packages/webui/components/markdown-editor/markdown-editor.css +++ b/packages/webui/components/markdown-editor/markdown-editor.css @@ -97,10 +97,13 @@ position: absolute; top: 16px; left: 16px; + right: 16px; color: var(--muted-foreground, #94a3b8); pointer-events: none; user-select: none; font-size: 15px; + line-height: 1.7; + white-space: pre-wrap; } /* Typography styles in WYSIWYG mode */ diff --git a/packages/webui/components/markdown-editor/markdown-editor.tsx b/packages/webui/components/markdown-editor/markdown-editor.tsx index 959831b1..1f25c667 100644 --- a/packages/webui/components/markdown-editor/markdown-editor.tsx +++ b/packages/webui/components/markdown-editor/markdown-editor.tsx @@ -132,7 +132,7 @@ export function MarkdownEditor({ {!readOnly && !hideToolbar ? ( ) : rightToolbarContent ? ( -
+
{rightToolbarContent}
) : null} diff --git a/packages/webui/messages/en.json b/packages/webui/messages/en.json index 78b4706b..23b5696c 100644 --- a/packages/webui/messages/en.json +++ b/packages/webui/messages/en.json @@ -1039,8 +1039,8 @@ "saved": "Saved", "read_only": "Read-only", "agents_md": "AGENTS.md", - "agents_md_placeholder": "Write guidelines and instructions for AI agents here...", - "agents_md_readonly_hint": "Only project owners can edit AGENTS.md instructions.", + "agents_md_placeholder": "Give AI agents instructions and context for this project or folder.\n\nShumai automatically loads this file, along with AGENTS.md files from parent folders up to the project level, whenever an agent works here.\n\nUse it to define things like:\n- What this project or folder contains\n- Rules the agent should follow\n- Terminology, style, or workflow\n- How metadata and autofill should be generated", + "agents_md_readonly_hint": "Only team owners can edit this file.", "agents_md_save_failed": "Failed to save", "agents_md_loading": "Loading AGENTS.md..." } diff --git a/packages/webui/messages/zh.json b/packages/webui/messages/zh.json index 4921b050..67b04ef8 100644 --- a/packages/webui/messages/zh.json +++ b/packages/webui/messages/zh.json @@ -1039,8 +1039,8 @@ "saved": "已保存", "read_only": "只读", "agents_md": "AGENTS.md", - "agents_md_placeholder": "在此处为 AI Agent 编写项目或文件夹指引...", - "agents_md_readonly_hint": "只有项目所有者可以编辑 AGENTS.md 指引。", + "agents_md_placeholder": "为在此项目或文件夹中工作的 AI Agent 提供指引和上下文。\n\n当 Agent 在此处工作时,Shumai 会自动加载此文件以及向上追溯至项目根目录各级父文件夹中的 AGENTS.md 文件。\n\n您可以用它来定义:\n- 此项目或文件夹包含的内容\n- Agent 应当遵守的规则\n- 术语、风格或工作流规范\n- 元数据和自动填充的生成方式", + "agents_md_readonly_hint": "只有团队所有者可以修改此文件。", "agents_md_save_failed": "保存失败", "agents_md_loading": "正在加载 AGENTS.md..." } diff --git a/packages/webui/paraglide/messages/agents_md_placeholder.d.ts b/packages/webui/paraglide/messages/agents_md_placeholder.d.ts index ffcd6425..6a5672b9 100644 --- a/packages/webui/paraglide/messages/agents_md_placeholder.d.ts +++ b/packages/webui/paraglide/messages/agents_md_placeholder.d.ts @@ -1,7 +1,7 @@ /** * | output | * | --- | -* | "Write guidelines and instructions for AI agents here..." | +* | "Give AI agents instructions and context for this project or folder. Shumai automatically loads this file, along with AGENTS.md files from parent folders up t..." | * * @param {Agents_Md_PlaceholderInputs} inputs * @param {{ locale?: "en" | "zh" }} options diff --git a/packages/webui/paraglide/messages/agents_md_placeholder.js b/packages/webui/paraglide/messages/agents_md_placeholder.js index 66bc704f..dcffd644 100644 --- a/packages/webui/paraglide/messages/agents_md_placeholder.js +++ b/packages/webui/paraglide/messages/agents_md_placeholder.js @@ -6,17 +6,33 @@ import { getLocale, experimentalStaticLocale } from '../runtime.js'; /** @typedef {{}} Agents_Md_PlaceholderInputs */ const en_agents_md_placeholder = /** @type {(inputs: Agents_Md_PlaceholderInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`Write guidelines and instructions for AI agents here...`) + return /** @type {LocalizedString} */ (`Give AI agents instructions and context for this project or folder. + +Shumai automatically loads this file, along with AGENTS.md files from parent folders up to the project level, whenever an agent works here. + +Use it to define things like: +- What this project or folder contains +- Rules the agent should follow +- Terminology, style, or workflow +- How metadata and autofill should be generated`) }; const zh_agents_md_placeholder = /** @type {(inputs: Agents_Md_PlaceholderInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`在此处为 AI Agent 编写项目或文件夹指引...`) + return /** @type {LocalizedString} */ (`为在此项目或文件夹中工作的 AI Agent 提供指引和上下文。 + +当 Agent 在此处工作时,Shumai 会自动加载此文件以及向上追溯至项目根目录各级父文件夹中的 AGENTS.md 文件。 + +您可以用它来定义: +- 此项目或文件夹包含的内容 +- Agent 应当遵守的规则 +- 术语、风格或工作流规范 +- 元数据和自动填充的生成方式`) }; /** * | output | * | --- | -* | "Write guidelines and instructions for AI agents here..." | +* | "Give AI agents instructions and context for this project or folder. Shumai automatically loads this file, along with AGENTS.md files from parent folders up t..." | * * @param {Agents_Md_PlaceholderInputs} inputs * @param {{ locale?: "en" | "zh" }} options diff --git a/packages/webui/paraglide/messages/agents_md_readonly_hint.d.ts b/packages/webui/paraglide/messages/agents_md_readonly_hint.d.ts index bd162931..809cac25 100644 --- a/packages/webui/paraglide/messages/agents_md_readonly_hint.d.ts +++ b/packages/webui/paraglide/messages/agents_md_readonly_hint.d.ts @@ -1,7 +1,7 @@ /** * | output | * | --- | -* | "Only project owners can edit AGENTS.md instructions." | +* | "Only team owners can edit this file." | * * @param {Agents_Md_Readonly_HintInputs} inputs * @param {{ locale?: "en" | "zh" }} options diff --git a/packages/webui/paraglide/messages/agents_md_readonly_hint.js b/packages/webui/paraglide/messages/agents_md_readonly_hint.js index 9a7ef978..424a7fa7 100644 --- a/packages/webui/paraglide/messages/agents_md_readonly_hint.js +++ b/packages/webui/paraglide/messages/agents_md_readonly_hint.js @@ -6,17 +6,17 @@ import { getLocale, experimentalStaticLocale } from '../runtime.js'; /** @typedef {{}} Agents_Md_Readonly_HintInputs */ const en_agents_md_readonly_hint = /** @type {(inputs: Agents_Md_Readonly_HintInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`Only project owners can edit AGENTS.md instructions.`) + return /** @type {LocalizedString} */ (`Only team owners can edit this file.`) }; const zh_agents_md_readonly_hint = /** @type {(inputs: Agents_Md_Readonly_HintInputs) => LocalizedString} */ () => { - return /** @type {LocalizedString} */ (`只有项目所有者可以编辑 AGENTS.md 指引。`) + return /** @type {LocalizedString} */ (`只有团队所有者可以修改此文件。`) }; /** * | output | * | --- | -* | "Only project owners can edit AGENTS.md instructions." | +* | "Only team owners can edit this file." | * * @param {Agents_Md_Readonly_HintInputs} inputs * @param {{ locale?: "en" | "zh" }} options