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。

fix(wecom): 展示工具审批参数

david4a98821373d1
diff --git a/apps/desktop/src/main/im/wecom/__tests__/textInteractions.test.ts b/apps/desktop/src/main/im/wecom/__tests__/textInteractions.test.ts
new file mode 100644
index 0000000000..92b52fd205
--- /dev/null
+++ b/apps/desktop/src/main/im/wecom/__tests__/textInteractions.test.ts
@@ -0,0 +1,47 @@
+import type { InteractionRequest } from '@cindy/maker-core';
+import { describe, expect, it } from 'vitest';
+
+import { formatWecomInteractionPrompt } from '../textInteractions';
+
+function permissionRequest(input: Record<string, unknown>): InteractionRequest {
+ return {
+ kind: 'permission',
+ requestId: 'permission-1',
+ toolName: 'Bash',
+ displayName: '运行命令',
+ input,
+ };
+}
+
+describe('formatWecomInteractionPrompt', () => {
+ it('在允许用户审批前展示工具参数', () => {
+ const prompt = formatWecomInteractionPrompt(
+ permissionRequest({
+ command: 'pnpm test',
+ path: 'D:\\workspace\\cindy',
+ }),
+ );
+
+ expect(prompt).toContain('需要确认工具“运行命令”');
+ expect(prompt).toContain('"command": "pnpm test"');
+ expect(prompt).toContain('"path": "D:\\\\workspace\\\\cindy"');
+ expect(prompt).toContain('回复“允许”执行一次');
+ });
+
+ it('截断过长参数,避免审批提示无限增长', () => {
+ const prompt = formatWecomInteractionPrompt(
+ permissionRequest({ payload: 'x'.repeat(2_000) }),
+ );
+
+ expect(prompt).toContain('…(已截断)');
+ expect(prompt).not.toContain('x'.repeat(1_000));
+ expect(prompt.length).toBeLessThan(1_100);
+ });
+
+ it('参数无法序列化时显示明确占位', () => {
+ const input: Record<string, unknown> = {};
+ input.self = input;
+
+ expect(formatWecomInteractionPrompt(permissionRequest(input))).toContain('<无法序列化>');
+ });
+});
diff --git a/apps/desktop/src/main/im/wecom/textInteractions.ts b/apps/desktop/src/main/im/wecom/textInteractions.ts
index 822cefdcfb..f5ca2e432f 100644
--- a/apps/desktop/src/main/im/wecom/textInteractions.ts
+++ b/apps/desktop/src/main/im/wecom/textInteractions.ts
@@ -8,6 +8,7 @@ interface PendingInteraction {
}
const INTERACTION_TIMEOUT_MS = 10 * 60_000;
+const MAX_PERMISSION_INPUT_PREVIEW = 800;
export class WecomTextInteractions {
private readonly pending = new Map<string, PendingInteraction>();
@@ -38,7 +39,7 @@ export class WecomTextInteractions {
this.pending.set(userId, { request, resolve: resolvePending, timer });
try {
- await this.im.sendText(userId, formatPrompt(request));
+ await this.im.sendText(userId, formatWecomInteractionPrompt(request));
} catch (error) {
const current = this.pending.get(userId);
if (current?.request.requestId === request.requestId) {
@@ -80,9 +81,27 @@ export class WecomTextInteractions {
}
}
-function formatPrompt(request: InteractionRequest): string {
+function previewPermissionInput(input: Record<string, unknown>): string {
+ try {
+ const json = JSON.stringify(input, null, 2);
+ if (!json) return '<无法序列化>';
+ return json.length > MAX_PERMISSION_INPUT_PREVIEW
+ ? `${json.slice(0, MAX_PERMISSION_INPUT_PREVIEW)}\n…(已截断)`
+ : json;
+ } catch {
+ return '<无法序列化>';
+ }
+}
+
+export function formatWecomInteractionPrompt(request: InteractionRequest): string {
if (request.kind === 'permission') {
return `需要确认工具“${request.displayName ?? request.toolName}”。
+
+参数:
+\`\`\`json
+${previewPermissionInput(request.input)}
+\`\`\`
+
回复“允许”执行一次,或回复“拒绝”取消本次操作。企业微信内不支持永久授权。`;
}
if (request.kind === 'plan_review') {