Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(pi): 权限档 i18n 四语 + 权限弹窗正文 harness 无关 + 能力契约

zqchrisb93fe78e2c4d
diff --git a/apps/desktop/src/renderer/components/new-chat/PermissionPrompt.tsx b/apps/desktop/src/renderer/components/new-chat/PermissionPrompt.tsx
index 6dd73eb74f..22cb9247af 100644
--- a/apps/desktop/src/renderer/components/new-chat/PermissionPrompt.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/PermissionPrompt.tsx
@@ -34,23 +34,32 @@ interface PermissionPromptProps {
// Tool input → display text
// ---------------------------------------------------------------------------
-function formatToolInput(toolName: string, input: Record<string, unknown>): string {
- switch (toolName) {
- case 'Bash':
- return typeof input.command === 'string' ? input.command : JSON.stringify(input, null, 2);
- case 'Read':
- case 'Edit':
- case 'Write':
- return typeof input.file_path === 'string' ? input.file_path : JSON.stringify(input, null, 2);
- case 'Glob':
- return typeof input.pattern === 'string' ? input.pattern : JSON.stringify(input, null, 2);
- case 'Grep':
- return typeof input.pattern === 'string' ? input.pattern : JSON.stringify(input, null, 2);
- default: {
- const text = JSON.stringify(input, null, 2);
- return text.length > 500 ? text.slice(0, 500) + '...' : text;
- }
+function firstString(input: Record<string, unknown>, keys: string[]): string | undefined {
+ for (const k of keys) {
+ const v = input[k];
+ if (typeof v === 'string' && v.length > 0) return v;
+ }
+ return undefined;
+}
+
+// harness 无关:CC 工具名首字母大写 + file_path;pi 内置工具名小写 + path/command。
+// 按语义分组(命令 / 文件 / 模式)归一化,任一命名命中就抽出清爽正文,否则回退 JSON。
+export function formatToolInput(toolName: string, input: Record<string, unknown>): string {
+ const name = toolName.toLowerCase();
+ const fallback = () => {
+ const text = JSON.stringify(input, null, 2);
+ return text.length > 500 ? text.slice(0, 500) + '...' : text;
+ };
+ if (name === 'bash') {
+ return firstString(input, ['command']) ?? fallback();
+ }
+ if (name === 'read' || name === 'edit' || name === 'write') {
+ return firstString(input, ['file_path', 'path', 'notebook_path']) ?? fallback();
+ }
+ if (name === 'glob' || name === 'grep' || name === 'find' || name === 'ls') {
+ return firstString(input, ['pattern', 'path', 'query']) ?? fallback();
}
+ return fallback();
}
function filterSessionScopedSuggestions(suggestions?: unknown[]): unknown[] {
diff --git a/apps/desktop/src/renderer/components/new-chat/__tests__/formatToolInput.test.ts b/apps/desktop/src/renderer/components/new-chat/__tests__/formatToolInput.test.ts
new file mode 100644
index 0000000000..fb6d13a0a8
--- /dev/null
+++ b/apps/desktop/src/renderer/components/new-chat/__tests__/formatToolInput.test.ts
@@ -0,0 +1,37 @@
+/**
+ * formatToolInput 归一化 —— 权限弹窗正文抽取必须 harness 无关:
+ * CC 工具名首字母大写 + file_path;pi 内置工具名小写 + path/command。
+ * 两套命名都要抽出清爽正文(命令/路径),而非回退成 JSON blob。
+ */
+import { describe, expect, it } from 'vitest';
+
+import { formatToolInput } from '../PermissionPrompt';
+
+describe('formatToolInput (harness-agnostic)', () => {
+ it('extracts the command for both Bash and bash', () => {
+ expect(formatToolInput('Bash', { command: 'ls -la' })).toBe('ls -la');
+ expect(formatToolInput('bash', { command: 'git status' })).toBe('git status');
+ });
+
+ it('extracts the file path across file_path / path (CC and pi)', () => {
+ expect(formatToolInput('Write', { file_path: '/a/b.ts' })).toBe('/a/b.ts');
+ expect(formatToolInput('write', { path: '/a/c.ts', content: 'x' })).toBe('/a/c.ts');
+ expect(formatToolInput('edit', { path: '/a/d.ts' })).toBe('/a/d.ts');
+ expect(formatToolInput('read', { path: '/a/e.ts' })).toBe('/a/e.ts');
+ });
+
+ it('extracts pattern/path for search tools including pi grep/find/ls', () => {
+ expect(formatToolInput('Grep', { pattern: 'TODO' })).toBe('TODO');
+ expect(formatToolInput('grep', { pattern: 'TODO', path: '/src' })).toBe('TODO');
+ expect(formatToolInput('find', { path: '/src' })).toBe('/src');
+ expect(formatToolInput('ls', { path: '/src' })).toBe('/src');
+ });
+
+ it('falls back to truncated JSON for unknown tools / missing fields', () => {
+ expect(formatToolInput('mcp__x__y', { a: 1 })).toBe('{\n "a": 1\n}');
+ expect(formatToolInput('bash', {})).toBe('{}');
+ const long = formatToolInput('weird', { s: 'x'.repeat(600) });
+ expect(long.endsWith('...')).toBe(true);
+ expect(long.length).toBeLessThanOrEqual(503);
+ });
+});
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index 57afff7623..a01f3012e1 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -4706,6 +4706,20 @@
"label": "Full access",
"description": "Allows editing any file and running networked commands without another confirmation. Highest risk; use only for trusted tasks."
}
+ },
+ "pi": {
+ "ask": {
+ "label": "Default permissions",
+ "description": "Read-only tools run directly; writing files, running commands, and MCP tools ask each time."
+ },
+ "auto": {
+ "label": "Auto-review",
+ "description": "In-workspace writes and safe commands run automatically; out-of-workspace writes, risky commands, and MCP tools still ask."
+ },
+ "bypassPermissions": {
+ "label": "Full access",
+ "description": "Every tool runs without asking. Highest risk; use only for trusted tasks."
+ }
}
}
},
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 503c1e1f3a..3f5e1ae75e 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -4704,6 +4704,20 @@
"label": "フルアクセス",
"description": "追加確認なしで任意のファイル編集とネットワークコマンド実行を許可します。最もリスクが高いため、信頼できるタスクでのみ使用してください。"
}
+ },
+ "pi": {
+ "ask": {
+ "label": "デフォルト権限",
+ "description": "読み取り専用ツールはそのまま実行し、ファイル書き込み・コマンド実行・MCP ツールは毎回確認します。"
+ },
+ "auto": {
+ "label": "自動レビュー",
+ "description": "ワークスペース内の書き込みと安全なコマンドは自動実行し、境界外への書き込み・危険なコマンド・MCP ツールは確認します。"
+ },
+ "bypassPermissions": {
+ "label": "フルアクセス",
+ "description": "すべてのツールを確認なしで実行します。最もリスクが高いため、信頼できるタスクでのみ使用してください。"
+ }
}
}
},
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index 7b6b445045..b63b6e32ed 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -4704,6 +4704,20 @@
"label": "전체 접근",
"description": "추가 확인 없이 모든 파일 편집과 네트워크 명령 실행을 허용합니다. 가장 위험하므로 신뢰할 수 있는 작업에서만 사용하세요."
}
+ },
+ "pi": {
+ "ask": {
+ "label": "기본 권한",
+ "description": "읽기 전용 도구는 바로 실행하고, 파일 쓰기·명령 실행·MCP 도구는 매번 확인합니다."
+ },
+ "auto": {
+ "label": "자동 리뷰",
+ "description": "워크스페이스 안의 쓰기와 안전한 명령은 자동 실행하고, 경계 밖 쓰기·위험한 명령·MCP 도구는 확인합니다."
+ },
+ "bypassPermissions": {
+ "label": "전체 접근",
+ "description": "모든 도구를 확인 없이 실행합니다. 가장 위험하므로 신뢰할 수 있는 작업에서만 사용하세요."
+ }
}
}
},
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index d3aa21fc54..aef00c90ef 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -4704,6 +4704,20 @@
"label": "完全访问",
"description": "允许修改任意文件并运行联网命令,无需再次确认。风险最高,只适合可信任务。"
}
+ },
+ "pi": {
+ "ask": {
+ "label": "默认权限",
+ "description": "只读工具直接执行;写文件、跑命令和 MCP 工具每次都询问。"
+ },
+ "auto": {
+ "label": "自动审批",
+ "description": "工作区内写文件与安全命令自动执行;越界写、危险命令和 MCP 工具仍会询问。"
+ },
+ "bypassPermissions": {
+ "label": "完全访问",
+ "description": "所有工具无需询问直接执行。风险最高,只适合可信任务。"
+ }
}
}
},
diff --git a/packages/maker-core/src/agents/pi/__tests__/pi-capabilities.test.ts b/packages/maker-core/src/agents/pi/__tests__/pi-capabilities.test.ts
new file mode 100644
index 0000000000..d7d1b870c9
--- /dev/null
+++ b/packages/maker-core/src/agents/pi/__tests__/pi-capabilities.test.ts
@@ -0,0 +1,51 @@
+/**
+ * PiAgent capabilities 契约 —— 锁住无人值守链路依赖的安全不变量:
+ * permissionModes 必须**从严到宽**声明,`[0]` 是最严档(hook-control/defaults.ts
+ * 在「显式档不被支持」时回落 `permissionModes[0]`,顺序错了会把更严的选择静默放宽)。
+ */
+import { describe, expect, it } from 'vitest';
+
+import { PiAgent } from '../index.js';
+import type { AgentDeps } from '../../base-agent.js';
+import type { Logger } from '../../../interfaces/logger.js';
+
+const noopLogger: Logger = {
+ trace: () => {}, debug: () => {}, info: () => {}, warn: () => {}, error: () => {}, fatal: () => {},
+ child: () => noopLogger,
+};
+
+function buildAgent(): PiAgent {
+ const deps: AgentDeps = {
+ auth: {
+ getState: async () => ({ authenticated: true, identity: 't', authSource: 'api-key' as const }),
+ triggerLogin: async () => ({ authenticated: true }),
+ logout: async () => {},
+ getAuthEnv: async () => ({}),
+ },
+ runtimeConfig: { endpoint: 'http://127.0.0.1:9' },
+ binaryPath: '/nonexistent/pi',
+ logger: noopLogger,
+ capabilityAdditions: { availableModels: [] },
+ };
+ return new PiAgent(deps);
+}
+
+describe('PiAgent capabilities contract', () => {
+ it('declares permission modes strict→wide with ask first (unattended clamp safety)', () => {
+ const ids = buildAgent().capabilities.permissionModes.map((m) => m.id);
+ expect(ids).toEqual(['ask', 'auto', 'bypassPermissions']);
+ // [0] 必须是最严档 —— hook-control 回落取它
+ expect(ids[0]).toBe('ask');
+ // 最宽档必须在末位
+ expect(ids[ids.length - 1]).toBe('bypassPermissions');
+ });
+
+ it('every permission mode ships an English fallback label + description', () => {
+ for (const m of buildAgent().capabilities.permissionModes) {
+ expect(m.displayName && m.displayName.length > 0).toBe(true);
+ expect(m.description && m.description.length > 0).toBe(true);
+ // fallback 必须是英文(真实文案走 i18n);粗判:不含 CJK
+ expect(/[一-鿿]/.test(`${m.displayName}${m.description}`)).toBe(false);
+ }
+ });
+});
diff --git a/packages/maker-core/src/agents/pi/index.ts b/packages/maker-core/src/agents/pi/index.ts
index 61bb793df4..cb76fcf94d 100644
--- a/packages/maker-core/src/agents/pi/index.ts
+++ b/packages/maker-core/src/agents/pi/index.ts
@@ -187,10 +187,12 @@ export class PiAgent extends BaseAgent {
// auto 档:bridge 行为同 ask(非只读全部冒泡),Cindy 侧 dispatcher 先过
// Auto-Review Core(shared/auto-review.ts)—— 区内写/安全命令静默放行,
// 越界写/危险命令/MCP 工具仍弹窗(见 handleExtensionUiRequest)。
+ // displayName/description 为英文 fallback,真实文案走 i18n
+ // newChat.permissionSelector.modes.pi.*(与 cc/codex 同结构)。
permissionModes: [
- { id: 'ask', displayName: 'Default permissions', description: '只读工具直通;写文件、跑命令与 MCP 工具逐次询问' },
- { id: 'auto', displayName: 'Auto-review', description: '工作区内写与安全命令自动放行;越界写、危险命令与 MCP 工具仍询问' },
- { id: 'bypassPermissions', displayName: 'Full access', description: '全部工具免询问执行;风险高' },
+ { id: 'ask', displayName: 'Default permissions', description: 'Read-only tools run directly; writing files, running commands, and MCP tools ask each time.' },
+ { id: 'auto', displayName: 'Auto-review', description: 'In-workspace writes and safe commands run automatically; out-of-workspace writes, risky commands, and MCP tools still ask.' },
+ { id: 'bypassPermissions', displayName: 'Full access', description: 'Every tool runs without asking. Highest risk; use only for trusted tasks.' },
],
setPermissionModeMidSession: { supported: true },
// plan 模式经 pi 自带 plan-mode 扩展(--extension 加载):开启后禁用 edit/write、