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
This commit is contained in:
Yiling-J
2026-08-15 11:00:30 +08:00
committed by GitHub
parent 1cee103b1c
commit ae86d56a7d
12 changed files with 251 additions and 29 deletions
@@ -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(
<QueryClientProvider client={queryClient}>
<AgentsMdEditor projectId="proj-1" assetId="folder-1" rootFolderId="folder-1" isRoot />
</QueryClientProvider>,
)
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(
<QueryClientProvider client={queryClient}>
<AgentsMdEditor projectId="proj-1" assetId="folder-1" rootFolderId="folder-1" isRoot />
</QueryClientProvider>,
)
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(
<QueryClientProvider client={queryClient}>
<AgentsMdEditor projectId="proj-1" assetId="folder-1" rootFolderId="folder-1" isRoot />
</QueryClientProvider>,
)
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(
<QueryClientProvider client={queryClient}>
<AgentsMdEditor projectId="proj-1" assetId="folder-1" rootFolderId="folder-1" isRoot />
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText(/parent folders up to the project level/)).toBeDefined()
})
})
})
@@ -168,7 +168,7 @@ export function AgentsMdEditor({ projectId, assetId, rootFolderId, isRoot }: Age
}
const rightToolbarContent = (
<div className="flex items-center gap-2 shrink-0">
<div className="flex items-center justify-between w-full gap-2 shrink-0">
{canAdmin ? (
<div className="flex items-center gap-1.5 text-xs shrink-0">
{(saveStatus === 'saving' || isClosing) && (
@@ -192,27 +192,29 @@ export function AgentsMdEditor({ projectId, assetId, rootFolderId, isRoot }: Age
</div>
) : (
<div
className="inline-flex items-center gap-1 text-xs text-muted-foreground bg-muted px-2 py-0.5 rounded-md shrink-0"
className="inline-flex items-center gap-1.5 text-xs text-muted-foreground bg-muted/60 px-2.5 py-1 rounded-md shrink-0"
title={m.agents_md_readonly_hint()}
>
<Lock className="h-3 w-3 shrink-0" />
<span>{m.read_only()}</span>
<Lock className="h-3.5 w-3.5 shrink-0" />
<span>{m.agents_md_readonly_hint()}</span>
</div>
)}
<Separator orientation="vertical" className="h-4 shrink-0" />
<div className="flex items-center gap-2 shrink-0">
{canAdmin && <Separator orientation="vertical" className="h-4 shrink-0" />}
<Button
variant="ghost"
size="sm"
onClick={handleClose}
disabled={isClosing}
className="h-7 px-2.5 hover:bg-muted text-muted-foreground hover:text-foreground shrink-0"
title={m.close()}
>
<X className="h-4 w-4 mr-1 shrink-0" />
<span>{m.close()}</span>
</Button>
<Button
variant="ghost"
size="sm"
onClick={handleClose}
disabled={isClosing}
className="h-7 px-2.5 hover:bg-muted text-muted-foreground hover:text-foreground shrink-0"
title={m.close()}
>
<X className="h-4 w-4 mr-1 shrink-0" />
<span>{m.close()}</span>
</Button>
</div>
</div>
)
@@ -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 */
@@ -132,7 +132,7 @@ export function MarkdownEditor({
{!readOnly && !hideToolbar ? (
<ToolbarPlugin rightContent={rightToolbarContent} />
) : rightToolbarContent ? (
<div className="shumai-editor-toolbar flex items-center justify-end px-3 py-1.5 border-b">
<div className="shumai-editor-toolbar flex items-center justify-between px-3 py-1.5 border-b">
{rightToolbarContent}
</div>
) : null}
+2 -2
View File
@@ -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..."
}
+2 -2
View File
@@ -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..."
}
+1 -1
View File
@@ -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
+19 -3
View File
@@ -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
+1 -1
View File
@@ -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
@@ -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