chatccc 0.2.225 → 0.2.227
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/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +4 -4
- package/src/__tests__/builtin-config.test.ts +5 -16
- package/src/__tests__/builtin-context.test.ts +1 -1
- package/src/__tests__/builtin-permissions.test.ts +211 -0
- package/src/__tests__/builtin-session-select.test.ts +2 -2
- package/src/__tests__/builtin-skills.test.ts +252 -141
- package/src/adapters/ccc-adapter.ts +3 -0
- package/src/builtin/cli.ts +657 -556
- package/src/builtin/config.ts +84 -0
- package/src/builtin/context.ts +11 -11
- package/src/builtin/file-log.ts +38 -0
- package/src/builtin/file-tools.ts +1407 -1320
- package/src/builtin/index.ts +457 -426
- package/src/builtin/permissions.ts +226 -0
- package/src/builtin/privacy.ts +141 -0
- package/src/builtin/proc-tree-kill.ts +61 -0
- package/src/builtin/progress/cards-helpers.ts +76 -0
- package/src/builtin/progress/reducer.ts +108 -0
- package/src/builtin/progress/terminal-renderer.ts +294 -0
- package/src/builtin/progress/view.ts +77 -0
- package/src/builtin/raw-stream-log.ts +124 -0
- package/src/builtin/session-select.ts +2 -2
- package/src/builtin/skills.ts +190 -108
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@ import { join } from "node:path";
|
|
|
4
4
|
|
|
5
5
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
6
6
|
|
|
7
|
-
import { config } from "../config.ts";
|
|
7
|
+
import { config } from "../builtin/config.ts";
|
|
8
8
|
|
|
9
9
|
const streamTextMock = vi.fn();
|
|
10
10
|
const generateTextMock = vi.fn();
|
|
@@ -26,7 +26,7 @@ vi.mock("ai", () => ({
|
|
|
26
26
|
tool: vi.fn((definition: unknown) => definition),
|
|
27
27
|
}));
|
|
28
28
|
|
|
29
|
-
vi.mock("../
|
|
29
|
+
vi.mock("../builtin/raw-stream-log.ts", () => ({
|
|
30
30
|
createRawStreamLog: createRawStreamLogMock,
|
|
31
31
|
}));
|
|
32
32
|
|
|
@@ -271,7 +271,7 @@ describe("ChatSession context management", () => {
|
|
|
271
271
|
|
|
272
272
|
it("writes raw CCC fullStream parts when raw stream logs are enabled", async () => {
|
|
273
273
|
const { ChatSession } = await import("../builtin/index.ts");
|
|
274
|
-
config.rawStreamLogs
|
|
274
|
+
config.rawStreamLogs = {
|
|
275
275
|
enabled: true,
|
|
276
276
|
maxBytesPerTurn: 4096,
|
|
277
277
|
retentionDays: 3,
|
|
@@ -304,7 +304,7 @@ describe("ChatSession context management", () => {
|
|
|
304
304
|
|
|
305
305
|
expect(createRawStreamLogMock).toHaveBeenCalledWith(expect.objectContaining({
|
|
306
306
|
enabled: true,
|
|
307
|
-
tool: "
|
|
307
|
+
tool: "deepccc",
|
|
308
308
|
sessionId: "raw-log-session",
|
|
309
309
|
label: "prompt",
|
|
310
310
|
maxBytesPerTurn: 4096,
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { afterEach, describe, expect, it } from "vitest";
|
|
2
2
|
|
|
3
3
|
import { ChatSession } from "../builtin/index.ts";
|
|
4
|
-
import { config } from "../config.ts";
|
|
4
|
+
import { config } from "../builtin/config.ts";
|
|
5
5
|
|
|
6
6
|
const originalDeepSeekApiKey = process.env.DEEPSEEK_API_KEY;
|
|
7
|
-
const
|
|
7
|
+
const originalDeepCccApiKey = config.apiKey;
|
|
8
8
|
|
|
9
9
|
afterEach(() => {
|
|
10
10
|
if (originalDeepSeekApiKey === undefined) {
|
|
@@ -12,23 +12,12 @@ afterEach(() => {
|
|
|
12
12
|
} else {
|
|
13
13
|
process.env.DEEPSEEK_API_KEY = originalDeepSeekApiKey;
|
|
14
14
|
}
|
|
15
|
-
config.
|
|
15
|
+
config.apiKey = originalDeepCccApiKey;
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
describe("builtin ChatSession config", () => {
|
|
19
|
-
it("
|
|
20
|
-
|
|
21
|
-
enabled: false,
|
|
22
|
-
defaultAgent: false,
|
|
23
|
-
DEEPSEEK_API_KEY: "",
|
|
24
|
-
DEEPSEEK_BASE_URL: "https://api.deepseek.com/v1",
|
|
25
|
-
model: "deepseek-v4-pro",
|
|
26
|
-
alternativeModel: "",
|
|
27
|
-
effort: "",
|
|
28
|
-
};
|
|
29
|
-
process.env.DEEPSEEK_API_KEY = "sk-env-should-not-be-used";
|
|
30
|
-
|
|
31
|
-
expect(() => new ChatSession()).toThrow("ccc.DEEPSEEK_API_KEY 未设置");
|
|
19
|
+
it("uses the builtin ~/.deepccc config when no apiKey is passed", () => {
|
|
20
|
+
expect(() => new ChatSession()).not.toThrow();
|
|
32
21
|
});
|
|
33
22
|
|
|
34
23
|
it("allows constructor parameters to override config defaults", () => {
|
|
@@ -82,7 +82,7 @@ describe("BuiltinContextManager", () => {
|
|
|
82
82
|
expect(context.buildModelMessages()).toEqual([
|
|
83
83
|
{
|
|
84
84
|
role: "user",
|
|
85
|
-
content: expect.stringContaining("
|
|
85
|
+
content: expect.stringContaining("The following is an earlier conversation summary"),
|
|
86
86
|
},
|
|
87
87
|
{ role: "assistant", content: "recent" },
|
|
88
88
|
]);
|
|
@@ -0,0 +1,211 @@
|
|
|
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("../builtin/raw-stream-log.ts", () => ({
|
|
35
|
+
createRawStreamLog: vi.fn().mockResolvedValue(null),
|
|
36
|
+
}));
|
|
37
|
+
|
|
38
|
+
import {
|
|
39
|
+
PermissionGate,
|
|
40
|
+
getAllowRules,
|
|
41
|
+
isDangerousCommand,
|
|
42
|
+
matchRule,
|
|
43
|
+
reloadAllowRules,
|
|
44
|
+
} from "../builtin/permissions.ts";
|
|
45
|
+
import { createBuiltinFileTools } from "../builtin/file-tools.ts";
|
|
46
|
+
import { ChatSession } from "../builtin/index.ts";
|
|
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
|
+
});
|
|
196
|
+
|
|
197
|
+
describe("ccc-adapter uses bypass mode (aligns with claude/codex)", () => {
|
|
198
|
+
it("tools created via ccc adapter run high-risk commands without asking", async () => {
|
|
199
|
+
const { createCccAdapter } = await import("../adapters/ccc-adapter.ts");
|
|
200
|
+
const adapter = createCccAdapter({ apiKey: "sk-test", contextDir: testHome.dir });
|
|
201
|
+
const { sessionId } = await adapter.createSession(testHome.dir);
|
|
202
|
+
streamTextMock.mockReturnValueOnce({ textStream: textStream("hi") });
|
|
203
|
+
|
|
204
|
+
for await (const _m of adapter.prompt(sessionId, "hi", testHome.dir)) {
|
|
205
|
+
// drain
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const result = (await lastRunCommandExecute()({ command: "node -e \"console.log('bypass')\"" }, { abortSignal: undefined })) as { exitCode: number };
|
|
209
|
+
expect(result.exitCode).toBe(0);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -69,7 +69,7 @@ describe("resolveBuiltinSession", () => {
|
|
|
69
69
|
cwd: "C:\\repo",
|
|
70
70
|
contextDir,
|
|
71
71
|
resume: "missing",
|
|
72
|
-
})).toThrow("
|
|
72
|
+
})).toThrow("DeepCCC session not found: missing");
|
|
73
73
|
});
|
|
74
74
|
|
|
75
75
|
it("resumes the newest session for cwd when resume has no id", async () => {
|
|
@@ -111,6 +111,6 @@ describe("resolveBuiltinSession", () => {
|
|
|
111
111
|
cwd: "C:\\repo",
|
|
112
112
|
contextDir,
|
|
113
113
|
resume: true,
|
|
114
|
-
})).toThrow("
|
|
114
|
+
})).toThrow("No resumable DeepCCC session found for cwd: C:\\repo");
|
|
115
115
|
});
|
|
116
116
|
});
|