chatccc 0.2.242 → 0.2.243
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/bin/cccagent.mjs +17 -17
- package/deepccc-agent/bin/deepccc.mjs +26 -26
- package/deepccc-agent/package.json +62 -62
- package/deepccc-agent/src/__tests__/chat-session.test.ts +578 -522
- package/deepccc-agent/src/__tests__/cli-json.test.ts +49 -49
- package/deepccc-agent/src/__tests__/config.test.ts +26 -26
- package/deepccc-agent/src/__tests__/context.test.ts +319 -319
- package/deepccc-agent/src/__tests__/file-tools.test.ts +240 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +195 -195
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +121 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +262 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +116 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +56 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +284 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +247 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +220 -220
- package/deepccc-agent/src/config.ts +84 -84
- package/deepccc-agent/src/context.ts +465 -465
- package/deepccc-agent/src/file-log.ts +38 -38
- package/deepccc-agent/src/index.ts +22 -0
- package/deepccc-agent/src/proc-tree-kill.ts +61 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +76 -76
- package/deepccc-agent/src/progress/reducer.ts +113 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +294 -294
- package/deepccc-agent/src/progress/view.ts +77 -77
- package/deepccc-agent/src/raw-stream-log.ts +124 -124
- package/deepccc-agent/src/session-search.ts +370 -370
- package/deepccc-agent/src/session-select.ts +48 -48
- package/deepccc-agent/src/sigint.ts +50 -50
- package/deepccc-agent/src/skills.ts +205 -205
- package/deepccc-agent/src/web-tools.ts +313 -313
- package/deepccc-agent/tsconfig.build.json +13 -13
- package/deepccc-agent/tsconfig.json +13 -13
- package/deepccc-agent/vitest.config.ts +7 -7
- package/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +522 -522
- package/src/__tests__/builtin-config.test.ts +26 -26
- package/src/__tests__/builtin-context.test.ts +319 -319
- package/src/__tests__/builtin-file-tools.test.ts +240 -240
- package/src/__tests__/builtin-permissions.test.ts +211 -211
- package/src/__tests__/builtin-session-search.test.ts +262 -262
- package/src/__tests__/builtin-session-select.test.ts +116 -116
- package/src/__tests__/builtin-sigint.test.ts +56 -56
- package/src/__tests__/builtin-skills.test.ts +284 -284
- package/src/__tests__/builtin-web-tools.test.ts +220 -220
- package/src/__tests__/config.test.ts +17 -17
- package/src/__tests__/progress-reducer.test.ts +121 -121
- package/src/__tests__/session-ccc-config.test.ts +45 -45
- package/src/__tests__/session.test.ts +298 -298
- package/src/adapters/ccc-adapter.ts +145 -145
- package/src/config-utils.ts +13 -13
- package/src/config.ts +13 -13
- package/src/progress/reducer.ts +113 -113
- package/src/session-chat-binding.ts +83 -83
- package/src/session.ts +311 -311
|
@@ -1,195 +1,195 @@
|
|
|
1
|
-
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs";
|
|
2
|
-
import { tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
|
|
5
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
-
|
|
7
|
-
const testHome = vi.hoisted(() => ({ dir: "" }));
|
|
8
|
-
|
|
9
|
-
const aiMocks = vi.hoisted(() => ({
|
|
10
|
-
streamText: vi.fn(),
|
|
11
|
-
generateText: vi.fn(),
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
vi.mock("node:os", async () => {
|
|
15
|
-
const actual = await vi.importActual<typeof import("node:os")>("node:os");
|
|
16
|
-
const [{ mkdtempSync }, { join }] = await Promise.all([import("node:fs"), import("node:path")]);
|
|
17
|
-
testHome.dir = mkdtempSync(join(actual.tmpdir(), "chatccc-builtin-perm-"));
|
|
18
|
-
return { ...actual, homedir: () => testHome.dir };
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
vi.mock("@ai-sdk/openai-compatible", () => ({
|
|
22
|
-
createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
vi.mock("ai", () => ({
|
|
26
|
-
streamText: aiMocks.streamText,
|
|
27
|
-
generateText: aiMocks.generateText,
|
|
28
|
-
isLoopFinished: vi.fn(() => ({ loopFinished: true })),
|
|
29
|
-
stepCountIs: vi.fn((count: number) => ({ count })),
|
|
30
|
-
jsonSchema: vi.fn((schema: unknown) => schema),
|
|
31
|
-
tool: vi.fn((definition: unknown) => definition),
|
|
32
|
-
}));
|
|
33
|
-
|
|
34
|
-
vi.mock("../raw-stream-log.js", () => ({
|
|
35
|
-
createRawStreamLog: vi.fn().mockResolvedValue(null),
|
|
36
|
-
}));
|
|
37
|
-
|
|
38
|
-
import {
|
|
39
|
-
PermissionGate,
|
|
40
|
-
getAllowRules,
|
|
41
|
-
isDangerousCommand,
|
|
42
|
-
matchRule,
|
|
43
|
-
reloadAllowRules,
|
|
44
|
-
} from "../permissions.js";
|
|
45
|
-
import { createBuiltinFileTools } from "../file-tools.js";
|
|
46
|
-
import { ChatSession } from "../index.js";
|
|
47
|
-
|
|
48
|
-
const streamTextMock = aiMocks.streamText;
|
|
49
|
-
const generateTextMock = aiMocks.generateText;
|
|
50
|
-
|
|
51
|
-
const ALLOW_FILE = () => join(testHome.dir, ".deepccc", "allow.json");
|
|
52
|
-
|
|
53
|
-
async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
|
|
54
|
-
const events: unknown[] = [];
|
|
55
|
-
for await (const event of iterable) events.push(event);
|
|
56
|
-
return events;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function* textStream(...chunks: string[]): AsyncIterable<string> {
|
|
60
|
-
for (const chunk of chunks) yield chunk;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function lastRunCommandExecute(): (input: { command: string }, opts?: unknown) => Promise<unknown> {
|
|
64
|
-
const call = streamTextMock.mock.calls.at(-1)?.[0] as { tools?: Record<string, { execute?: unknown }> };
|
|
65
|
-
const execute = call?.tools?.run_command?.execute as ((input: { command: string }, opts?: unknown) => Promise<unknown>) | undefined;
|
|
66
|
-
if (!execute) throw new Error("streamText was not called with tools.run_command.execute");
|
|
67
|
-
return execute;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
afterEach(() => {
|
|
71
|
-
try {
|
|
72
|
-
rmSync(ALLOW_FILE(), { force: true });
|
|
73
|
-
} catch {}
|
|
74
|
-
reloadAllowRules();
|
|
75
|
-
streamTextMock.mockReset();
|
|
76
|
-
generateTextMock.mockReset();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
beforeEach(() => {
|
|
80
|
-
try {
|
|
81
|
-
rmSync(join(testHome.dir, ".deepccc"), { recursive: true, force: true });
|
|
82
|
-
} catch {}
|
|
83
|
-
mkdirSync(join(testHome.dir, ".deepccc"), { recursive: true });
|
|
84
|
-
reloadAllowRules();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
describe("builtin permissions module", () => {
|
|
88
|
-
it("flags destructive commands and ignores normal ones", () => {
|
|
89
|
-
expect(isDangerousCommand("rm -rf node_modules")).toBe(true);
|
|
90
|
-
expect(isDangerousCommand("git push --force origin main")).toBe(true);
|
|
91
|
-
expect(isDangerousCommand("git status")).toBe(false);
|
|
92
|
-
expect(isDangerousCommand("npm test")).toBe(false);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("matches allow/deny rules with wildcards and relative paths", () => {
|
|
96
|
-
expect(matchRule("run_command:git status*", "run_command:git status --short")).toBe(true);
|
|
97
|
-
expect(matchRule("edit_file:node_modules/**", "edit_file:node_modules/a/b.js")).toBe(true);
|
|
98
|
-
expect(matchRule("run_command:npm test", "run_command:npm test extra")).toBe(false);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("returns empty rules when allow.json is missing", () => {
|
|
102
|
-
expect(getAllowRules()).toEqual({ allow: [], deny: [] });
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("loads rules from ~/.deepccc/allow.json", () => {
|
|
106
|
-
writeFileSync(ALLOW_FILE(), JSON.stringify({ deny: ["run_command:npm publish*"] }), "utf-8");
|
|
107
|
-
reloadAllowRules();
|
|
108
|
-
expect(getAllowRules().deny).toEqual(["run_command:npm publish*"]);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
describe("builtin PermissionGate", () => {
|
|
113
|
-
it("bypass mode allows everything", async () => {
|
|
114
|
-
const gate = new PermissionGate("bypass");
|
|
115
|
-
expect(await gate.check({ tool: "run_command", action: "rm -rf /", reason: "high-risk", detail: "x" })).toBe("allow");
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("ask mode denies high-risk without resolver", async () => {
|
|
119
|
-
const gate = new PermissionGate("ask");
|
|
120
|
-
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("ask mode allows non-high-risk operations without asking", async () => {
|
|
124
|
-
const resolver = vi.fn();
|
|
125
|
-
const gate = new PermissionGate("ask", resolver as never);
|
|
126
|
-
expect(await gate.check({ tool: "run_command", action: "npm test", reason: "rule", detail: "x" })).toBe("allow");
|
|
127
|
-
expect(await gate.check({ tool: "edit_file", action: "src/a.ts", reason: "rule", detail: "x" })).toBe("allow");
|
|
128
|
-
expect(resolver).not.toHaveBeenCalled();
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
it("follows resolver answer", async () => {
|
|
132
|
-
let answer: "allow" | "deny" = "allow";
|
|
133
|
-
const gate = new PermissionGate("ask", async () => answer);
|
|
134
|
-
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("allow");
|
|
135
|
-
answer = "deny";
|
|
136
|
-
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("deny rule wins over allow rule", async () => {
|
|
140
|
-
writeFileSync(
|
|
141
|
-
ALLOW_FILE(),
|
|
142
|
-
JSON.stringify({ allow: ["run_command:rm -rf /tmp*"], deny: ["run_command:rm -rf /tmp/forbidden*"] }),
|
|
143
|
-
"utf-8",
|
|
144
|
-
);
|
|
145
|
-
reloadAllowRules();
|
|
146
|
-
const gate = new PermissionGate("ask");
|
|
147
|
-
expect(await gate.check({ tool: "run_command", action: "rm -rf /tmp/forbidden/x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
describe("builtin file-tools permission integration", () => {
|
|
152
|
-
it("run_command high-risk is denied by default gate", async () => {
|
|
153
|
-
const tools = createBuiltinFileTools(testHome.dir, { permissionGate: new PermissionGate("ask") });
|
|
154
|
-
const tool = tools.run_command as unknown as {
|
|
155
|
-
execute: (input: { command: string }, opts?: unknown) => Promise<unknown>;
|
|
156
|
-
};
|
|
157
|
-
await expect(tool.execute({ command: "rm -rf node_modules" }, { abortSignal: undefined })).rejects.toThrow(/权限拒绝/);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("no gate keeps original behavior", async () => {
|
|
161
|
-
const tools = createBuiltinFileTools(testHome.dir);
|
|
162
|
-
const tool = tools.run_command as unknown as {
|
|
163
|
-
execute: (input: { command: string }, opts?: unknown) => Promise<{ exitCode: number | null }>;
|
|
164
|
-
};
|
|
165
|
-
const result = await tool.execute({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined });
|
|
166
|
-
expect(result.exitCode).toBe(0);
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
describe("builtin ChatSession permission integration", () => {
|
|
171
|
-
it("permissionMode bypass lets high-risk run_command execute", async () => {
|
|
172
|
-
const session = new ChatSession(
|
|
173
|
-
{ apiKey: "sk-test" },
|
|
174
|
-
{ sessionId: "perm-bypass", permissionMode: "bypass" },
|
|
175
|
-
);
|
|
176
|
-
streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
|
|
177
|
-
await collect(session.chat("hi"));
|
|
178
|
-
|
|
179
|
-
const result = (await lastRunCommandExecute()({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined })) as { exitCode: number };
|
|
180
|
-
expect(result.exitCode).toBe(0);
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it("default ask mode without resolver denies high-risk run_command", async () => {
|
|
184
|
-
const session = new ChatSession(
|
|
185
|
-
{ apiKey: "sk-test" },
|
|
186
|
-
{ sessionId: "perm-ask" },
|
|
187
|
-
);
|
|
188
|
-
streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
|
|
189
|
-
await collect(session.chat("hi"));
|
|
190
|
-
|
|
191
|
-
await expect(
|
|
192
|
-
lastRunCommandExecute()({ command: "rm -rf node_modules" }, { abortSignal: undefined }),
|
|
193
|
-
).rejects.toThrow(/权限拒绝/);
|
|
194
|
-
});
|
|
195
|
-
});
|
|
1
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
6
|
+
|
|
7
|
+
const testHome = vi.hoisted(() => ({ dir: "" }));
|
|
8
|
+
|
|
9
|
+
const aiMocks = vi.hoisted(() => ({
|
|
10
|
+
streamText: vi.fn(),
|
|
11
|
+
generateText: vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
vi.mock("node:os", async () => {
|
|
15
|
+
const actual = await vi.importActual<typeof import("node:os")>("node:os");
|
|
16
|
+
const [{ mkdtempSync }, { join }] = await Promise.all([import("node:fs"), import("node:path")]);
|
|
17
|
+
testHome.dir = mkdtempSync(join(actual.tmpdir(), "chatccc-builtin-perm-"));
|
|
18
|
+
return { ...actual, homedir: () => testHome.dir };
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
vi.mock("@ai-sdk/openai-compatible", () => ({
|
|
22
|
+
createOpenAICompatible: vi.fn(() => (modelId: string) => ({ modelId })),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock("ai", () => ({
|
|
26
|
+
streamText: aiMocks.streamText,
|
|
27
|
+
generateText: aiMocks.generateText,
|
|
28
|
+
isLoopFinished: vi.fn(() => ({ loopFinished: true })),
|
|
29
|
+
stepCountIs: vi.fn((count: number) => ({ count })),
|
|
30
|
+
jsonSchema: vi.fn((schema: unknown) => schema),
|
|
31
|
+
tool: vi.fn((definition: unknown) => definition),
|
|
32
|
+
}));
|
|
33
|
+
|
|
34
|
+
vi.mock("../raw-stream-log.js", () => ({
|
|
35
|
+
createRawStreamLog: vi.fn().mockResolvedValue(null),
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
import {
|
|
39
|
+
PermissionGate,
|
|
40
|
+
getAllowRules,
|
|
41
|
+
isDangerousCommand,
|
|
42
|
+
matchRule,
|
|
43
|
+
reloadAllowRules,
|
|
44
|
+
} from "../permissions.js";
|
|
45
|
+
import { createBuiltinFileTools } from "../file-tools.js";
|
|
46
|
+
import { ChatSession } from "../index.js";
|
|
47
|
+
|
|
48
|
+
const streamTextMock = aiMocks.streamText;
|
|
49
|
+
const generateTextMock = aiMocks.generateText;
|
|
50
|
+
|
|
51
|
+
const ALLOW_FILE = () => join(testHome.dir, ".deepccc", "allow.json");
|
|
52
|
+
|
|
53
|
+
async function collect(iterable: AsyncIterable<unknown>): Promise<unknown[]> {
|
|
54
|
+
const events: unknown[] = [];
|
|
55
|
+
for await (const event of iterable) events.push(event);
|
|
56
|
+
return events;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function* textStream(...chunks: string[]): AsyncIterable<string> {
|
|
60
|
+
for (const chunk of chunks) yield chunk;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function lastRunCommandExecute(): (input: { command: string }, opts?: unknown) => Promise<unknown> {
|
|
64
|
+
const call = streamTextMock.mock.calls.at(-1)?.[0] as { tools?: Record<string, { execute?: unknown }> };
|
|
65
|
+
const execute = call?.tools?.run_command?.execute as ((input: { command: string }, opts?: unknown) => Promise<unknown>) | undefined;
|
|
66
|
+
if (!execute) throw new Error("streamText was not called with tools.run_command.execute");
|
|
67
|
+
return execute;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
afterEach(() => {
|
|
71
|
+
try {
|
|
72
|
+
rmSync(ALLOW_FILE(), { force: true });
|
|
73
|
+
} catch {}
|
|
74
|
+
reloadAllowRules();
|
|
75
|
+
streamTextMock.mockReset();
|
|
76
|
+
generateTextMock.mockReset();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
try {
|
|
81
|
+
rmSync(join(testHome.dir, ".deepccc"), { recursive: true, force: true });
|
|
82
|
+
} catch {}
|
|
83
|
+
mkdirSync(join(testHome.dir, ".deepccc"), { recursive: true });
|
|
84
|
+
reloadAllowRules();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe("builtin permissions module", () => {
|
|
88
|
+
it("flags destructive commands and ignores normal ones", () => {
|
|
89
|
+
expect(isDangerousCommand("rm -rf node_modules")).toBe(true);
|
|
90
|
+
expect(isDangerousCommand("git push --force origin main")).toBe(true);
|
|
91
|
+
expect(isDangerousCommand("git status")).toBe(false);
|
|
92
|
+
expect(isDangerousCommand("npm test")).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("matches allow/deny rules with wildcards and relative paths", () => {
|
|
96
|
+
expect(matchRule("run_command:git status*", "run_command:git status --short")).toBe(true);
|
|
97
|
+
expect(matchRule("edit_file:node_modules/**", "edit_file:node_modules/a/b.js")).toBe(true);
|
|
98
|
+
expect(matchRule("run_command:npm test", "run_command:npm test extra")).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("returns empty rules when allow.json is missing", () => {
|
|
102
|
+
expect(getAllowRules()).toEqual({ allow: [], deny: [] });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("loads rules from ~/.deepccc/allow.json", () => {
|
|
106
|
+
writeFileSync(ALLOW_FILE(), JSON.stringify({ deny: ["run_command:npm publish*"] }), "utf-8");
|
|
107
|
+
reloadAllowRules();
|
|
108
|
+
expect(getAllowRules().deny).toEqual(["run_command:npm publish*"]);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe("builtin PermissionGate", () => {
|
|
113
|
+
it("bypass mode allows everything", async () => {
|
|
114
|
+
const gate = new PermissionGate("bypass");
|
|
115
|
+
expect(await gate.check({ tool: "run_command", action: "rm -rf /", reason: "high-risk", detail: "x" })).toBe("allow");
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("ask mode denies high-risk without resolver", async () => {
|
|
119
|
+
const gate = new PermissionGate("ask");
|
|
120
|
+
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("ask mode allows non-high-risk operations without asking", async () => {
|
|
124
|
+
const resolver = vi.fn();
|
|
125
|
+
const gate = new PermissionGate("ask", resolver as never);
|
|
126
|
+
expect(await gate.check({ tool: "run_command", action: "npm test", reason: "rule", detail: "x" })).toBe("allow");
|
|
127
|
+
expect(await gate.check({ tool: "edit_file", action: "src/a.ts", reason: "rule", detail: "x" })).toBe("allow");
|
|
128
|
+
expect(resolver).not.toHaveBeenCalled();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("follows resolver answer", async () => {
|
|
132
|
+
let answer: "allow" | "deny" = "allow";
|
|
133
|
+
const gate = new PermissionGate("ask", async () => answer);
|
|
134
|
+
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("allow");
|
|
135
|
+
answer = "deny";
|
|
136
|
+
expect(await gate.check({ tool: "run_command", action: "rm -rf x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("deny rule wins over allow rule", async () => {
|
|
140
|
+
writeFileSync(
|
|
141
|
+
ALLOW_FILE(),
|
|
142
|
+
JSON.stringify({ allow: ["run_command:rm -rf /tmp*"], deny: ["run_command:rm -rf /tmp/forbidden*"] }),
|
|
143
|
+
"utf-8",
|
|
144
|
+
);
|
|
145
|
+
reloadAllowRules();
|
|
146
|
+
const gate = new PermissionGate("ask");
|
|
147
|
+
expect(await gate.check({ tool: "run_command", action: "rm -rf /tmp/forbidden/x", reason: "high-risk", detail: "x" })).toBe("deny");
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe("builtin file-tools permission integration", () => {
|
|
152
|
+
it("run_command high-risk is denied by default gate", async () => {
|
|
153
|
+
const tools = createBuiltinFileTools(testHome.dir, { permissionGate: new PermissionGate("ask") });
|
|
154
|
+
const tool = tools.run_command as unknown as {
|
|
155
|
+
execute: (input: { command: string }, opts?: unknown) => Promise<unknown>;
|
|
156
|
+
};
|
|
157
|
+
await expect(tool.execute({ command: "rm -rf node_modules" }, { abortSignal: undefined })).rejects.toThrow(/权限拒绝/);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("no gate keeps original behavior", async () => {
|
|
161
|
+
const tools = createBuiltinFileTools(testHome.dir);
|
|
162
|
+
const tool = tools.run_command as unknown as {
|
|
163
|
+
execute: (input: { command: string }, opts?: unknown) => Promise<{ exitCode: number | null }>;
|
|
164
|
+
};
|
|
165
|
+
const result = await tool.execute({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined });
|
|
166
|
+
expect(result.exitCode).toBe(0);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe("builtin ChatSession permission integration", () => {
|
|
171
|
+
it("permissionMode bypass lets high-risk run_command execute", async () => {
|
|
172
|
+
const session = new ChatSession(
|
|
173
|
+
{ apiKey: "sk-test" },
|
|
174
|
+
{ sessionId: "perm-bypass", permissionMode: "bypass" },
|
|
175
|
+
);
|
|
176
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
|
|
177
|
+
await collect(session.chat("hi"));
|
|
178
|
+
|
|
179
|
+
const result = (await lastRunCommandExecute()({ command: "node -e \"console.log('ok')\"" }, { abortSignal: undefined })) as { exitCode: number };
|
|
180
|
+
expect(result.exitCode).toBe(0);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("default ask mode without resolver denies high-risk run_command", async () => {
|
|
184
|
+
const session = new ChatSession(
|
|
185
|
+
{ apiKey: "sk-test" },
|
|
186
|
+
{ sessionId: "perm-ask" },
|
|
187
|
+
);
|
|
188
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
|
|
189
|
+
await collect(session.chat("hi"));
|
|
190
|
+
|
|
191
|
+
await expect(
|
|
192
|
+
lastRunCommandExecute()({ command: "rm -rf node_modules" }, { abortSignal: undefined }),
|
|
193
|
+
).rejects.toThrow(/权限拒绝/);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import type { ChatEvent } from "../index.js";
|
|
4
|
-
import { reduceProgress, summarizeToolInput, summarizeToolResult } from "../progress/reducer.js";
|
|
5
|
-
import { progressView } from "../progress/view.js";
|
|
6
|
-
|
|
7
|
-
function feed(events: ChatEvent[]) {
|
|
8
|
-
return events.reduce(reduceProgress, progressView({ headerTitle: "生成中..." }));
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
describe("reduceProgress", () => {
|
|
12
|
-
it("renders explicit compaction and generation phases", () => {
|
|
13
|
-
const compacting = reduceProgress(
|
|
14
|
-
progressView({ headerTitle: "Generating..." }),
|
|
15
|
-
{ type: "status", phase: "compacting" },
|
|
16
|
-
);
|
|
17
|
-
expect(compacting.headerTitle).toBe("压缩上下文中...");
|
|
18
|
-
|
|
19
|
-
const generating = reduceProgress(compacting, { type: "status", phase: "generating" });
|
|
20
|
-
expect(generating.headerTitle).toBe("生成回复中...");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("accumulates text via accumulated field", () => {
|
|
24
|
-
const view = feed([
|
|
25
|
-
{ type: "text", text: "Hello", accumulated: "Hello" },
|
|
26
|
-
{ type: "text", text: " world", accumulated: "Hello world" },
|
|
27
|
-
]);
|
|
28
|
-
expect(view.text).toBe("Hello world");
|
|
29
|
-
expect(view.status).toBe("generating");
|
|
30
|
-
expect(view.showStop).toBe(true);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("appends tool_use as running and resolves status on tool_result", () => {
|
|
34
|
-
const view = feed([
|
|
35
|
-
{ type: "tool_use", id: "t1", name: "edit_file", input: { path: "a.ts" } },
|
|
36
|
-
{ type: "tool_result", tool_use_id: "t1", name: "edit_file", content: "ok", is_error: false },
|
|
37
|
-
]);
|
|
38
|
-
expect(view.tools).toHaveLength(1);
|
|
39
|
-
expect(view.tools[0]).toMatchObject({
|
|
40
|
-
id: "t1",
|
|
41
|
-
name: "edit_file",
|
|
42
|
-
status: "ok",
|
|
43
|
-
summary: "ok",
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("marks tool as error when is_error is true", () => {
|
|
48
|
-
const view = feed([
|
|
49
|
-
{ type: "tool_use", id: "t2", name: "run_command", input: { command: "npm test" } },
|
|
50
|
-
{ type: "tool_result", tool_use_id: "t2", name: "run_command", content: "boom", is_error: true },
|
|
51
|
-
]);
|
|
52
|
-
expect(view.tools[0].status).toBe("error");
|
|
53
|
-
expect(view.tools[0].summary).toBe("boom");
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it("falls back to last running tool when tool_use_id mismatches", () => {
|
|
57
|
-
const view = feed([
|
|
58
|
-
{ type: "tool_use", id: undefined, name: "list_dir", input: {} },
|
|
59
|
-
{ type: "tool_result", tool_use_id: "unknown-id", name: "list_dir", content: "ok", is_error: false },
|
|
60
|
-
]);
|
|
61
|
-
expect(view.tools[0].status).toBe("ok");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("keeps multiple tool calls independently", () => {
|
|
65
|
-
const view = feed([
|
|
66
|
-
{ type: "tool_use", id: "a", name: "read_file", input: { path: "a" } },
|
|
67
|
-
{ type: "tool_use", id: "b", name: "read_file", input: { path: "b" } },
|
|
68
|
-
{ type: "tool_result", tool_use_id: "b", name: "read_file", content: "bb", is_error: false },
|
|
69
|
-
]);
|
|
70
|
-
expect(view.tools[0].status).toBe("running");
|
|
71
|
-
expect(view.tools[1].status).toBe("ok");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("marks done: status done, showStop false, text finalized", () => {
|
|
75
|
-
const view = feed([
|
|
76
|
-
{ type: "text", text: "part", accumulated: "part" },
|
|
77
|
-
{ type: "done", text: "final answer" },
|
|
78
|
-
]);
|
|
79
|
-
expect(view.status).toBe("done");
|
|
80
|
-
expect(view.showStop).toBe(false);
|
|
81
|
-
expect(view.text).toBe("final answer");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("marks error status and keeps text", () => {
|
|
85
|
-
const view = feed([
|
|
86
|
-
{ type: "text", text: "x", accumulated: "x" },
|
|
87
|
-
{ type: "error", message: "boom" },
|
|
88
|
-
]);
|
|
89
|
-
expect(view.status).toBe("error");
|
|
90
|
-
expect(view.showStop).toBe(false);
|
|
91
|
-
expect(view.text).toBe("x");
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("ignores compact events without changing the view identity", () => {
|
|
95
|
-
const base = progressView({ headerTitle: "生成中..." });
|
|
96
|
-
const result = reduceProgress(base, { type: "compact", compactedMessages: 5 });
|
|
97
|
-
expect(result).toBe(base);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("does not mutate the previous view (immutable updates)", () => {
|
|
101
|
-
const base = progressView({ headerTitle: "生成中..." });
|
|
102
|
-
const next = reduceProgress(base, { type: "text", text: "hi", accumulated: "hi" });
|
|
103
|
-
expect(base.text).toBe("");
|
|
104
|
-
expect(next.text).toBe("hi");
|
|
105
|
-
expect(next).not.toBe(base);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
describe("summarizeToolInput / summarizeToolResult", () => {
|
|
110
|
-
it("flattens multiline input to one line", () => {
|
|
111
|
-
const s = summarizeToolInput({ path: "a\nb", big: "x".repeat(200) });
|
|
112
|
-
expect(s).not.toContain("\n");
|
|
113
|
-
expect(s.length).toBeLessThanOrEqual(121);
|
|
114
|
-
expect(s.endsWith("…")).toBe(true);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("takes first line of result content", () => {
|
|
118
|
-
expect(summarizeToolResult("line1\nline2\n")).toBe("line1");
|
|
119
|
-
expect(summarizeToolResult({ ok: true })).toBe('{"ok":true}');
|
|
120
|
-
});
|
|
121
|
-
});
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import type { ChatEvent } from "../index.js";
|
|
4
|
+
import { reduceProgress, summarizeToolInput, summarizeToolResult } from "../progress/reducer.js";
|
|
5
|
+
import { progressView } from "../progress/view.js";
|
|
6
|
+
|
|
7
|
+
function feed(events: ChatEvent[]) {
|
|
8
|
+
return events.reduce(reduceProgress, progressView({ headerTitle: "生成中..." }));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("reduceProgress", () => {
|
|
12
|
+
it("renders explicit compaction and generation phases", () => {
|
|
13
|
+
const compacting = reduceProgress(
|
|
14
|
+
progressView({ headerTitle: "Generating..." }),
|
|
15
|
+
{ type: "status", phase: "compacting" },
|
|
16
|
+
);
|
|
17
|
+
expect(compacting.headerTitle).toBe("压缩上下文中...");
|
|
18
|
+
|
|
19
|
+
const generating = reduceProgress(compacting, { type: "status", phase: "generating" });
|
|
20
|
+
expect(generating.headerTitle).toBe("生成回复中...");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("accumulates text via accumulated field", () => {
|
|
24
|
+
const view = feed([
|
|
25
|
+
{ type: "text", text: "Hello", accumulated: "Hello" },
|
|
26
|
+
{ type: "text", text: " world", accumulated: "Hello world" },
|
|
27
|
+
]);
|
|
28
|
+
expect(view.text).toBe("Hello world");
|
|
29
|
+
expect(view.status).toBe("generating");
|
|
30
|
+
expect(view.showStop).toBe(true);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("appends tool_use as running and resolves status on tool_result", () => {
|
|
34
|
+
const view = feed([
|
|
35
|
+
{ type: "tool_use", id: "t1", name: "edit_file", input: { path: "a.ts" } },
|
|
36
|
+
{ type: "tool_result", tool_use_id: "t1", name: "edit_file", content: "ok", is_error: false },
|
|
37
|
+
]);
|
|
38
|
+
expect(view.tools).toHaveLength(1);
|
|
39
|
+
expect(view.tools[0]).toMatchObject({
|
|
40
|
+
id: "t1",
|
|
41
|
+
name: "edit_file",
|
|
42
|
+
status: "ok",
|
|
43
|
+
summary: "ok",
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("marks tool as error when is_error is true", () => {
|
|
48
|
+
const view = feed([
|
|
49
|
+
{ type: "tool_use", id: "t2", name: "run_command", input: { command: "npm test" } },
|
|
50
|
+
{ type: "tool_result", tool_use_id: "t2", name: "run_command", content: "boom", is_error: true },
|
|
51
|
+
]);
|
|
52
|
+
expect(view.tools[0].status).toBe("error");
|
|
53
|
+
expect(view.tools[0].summary).toBe("boom");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("falls back to last running tool when tool_use_id mismatches", () => {
|
|
57
|
+
const view = feed([
|
|
58
|
+
{ type: "tool_use", id: undefined, name: "list_dir", input: {} },
|
|
59
|
+
{ type: "tool_result", tool_use_id: "unknown-id", name: "list_dir", content: "ok", is_error: false },
|
|
60
|
+
]);
|
|
61
|
+
expect(view.tools[0].status).toBe("ok");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("keeps multiple tool calls independently", () => {
|
|
65
|
+
const view = feed([
|
|
66
|
+
{ type: "tool_use", id: "a", name: "read_file", input: { path: "a" } },
|
|
67
|
+
{ type: "tool_use", id: "b", name: "read_file", input: { path: "b" } },
|
|
68
|
+
{ type: "tool_result", tool_use_id: "b", name: "read_file", content: "bb", is_error: false },
|
|
69
|
+
]);
|
|
70
|
+
expect(view.tools[0].status).toBe("running");
|
|
71
|
+
expect(view.tools[1].status).toBe("ok");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("marks done: status done, showStop false, text finalized", () => {
|
|
75
|
+
const view = feed([
|
|
76
|
+
{ type: "text", text: "part", accumulated: "part" },
|
|
77
|
+
{ type: "done", text: "final answer" },
|
|
78
|
+
]);
|
|
79
|
+
expect(view.status).toBe("done");
|
|
80
|
+
expect(view.showStop).toBe(false);
|
|
81
|
+
expect(view.text).toBe("final answer");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("marks error status and keeps text", () => {
|
|
85
|
+
const view = feed([
|
|
86
|
+
{ type: "text", text: "x", accumulated: "x" },
|
|
87
|
+
{ type: "error", message: "boom" },
|
|
88
|
+
]);
|
|
89
|
+
expect(view.status).toBe("error");
|
|
90
|
+
expect(view.showStop).toBe(false);
|
|
91
|
+
expect(view.text).toBe("x");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("ignores compact events without changing the view identity", () => {
|
|
95
|
+
const base = progressView({ headerTitle: "生成中..." });
|
|
96
|
+
const result = reduceProgress(base, { type: "compact", compactedMessages: 5 });
|
|
97
|
+
expect(result).toBe(base);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("does not mutate the previous view (immutable updates)", () => {
|
|
101
|
+
const base = progressView({ headerTitle: "生成中..." });
|
|
102
|
+
const next = reduceProgress(base, { type: "text", text: "hi", accumulated: "hi" });
|
|
103
|
+
expect(base.text).toBe("");
|
|
104
|
+
expect(next.text).toBe("hi");
|
|
105
|
+
expect(next).not.toBe(base);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("summarizeToolInput / summarizeToolResult", () => {
|
|
110
|
+
it("flattens multiline input to one line", () => {
|
|
111
|
+
const s = summarizeToolInput({ path: "a\nb", big: "x".repeat(200) });
|
|
112
|
+
expect(s).not.toContain("\n");
|
|
113
|
+
expect(s.length).toBeLessThanOrEqual(121);
|
|
114
|
+
expect(s.endsWith("…")).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("takes first line of result content", () => {
|
|
118
|
+
expect(summarizeToolResult("line1\nline2\n")).toBe("line1");
|
|
119
|
+
expect(summarizeToolResult({ ok: true })).toBe('{"ok":true}');
|
|
120
|
+
});
|
|
121
|
+
});
|