chatccc 0.2.121 → 0.2.123
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 +22 -14
- package/package.json +63 -63
- package/src/__tests__/orchestrator.test.ts +43 -1
- package/src/adapters/adapter-interface.ts +7 -0
- package/src/adapters/claude-adapter.ts +7 -3
- package/src/adapters/cursor-adapter.ts +11 -1
- package/src/cards.ts +2 -0
- package/src/config.ts +18 -0
- package/src/orchestrator.ts +214 -15
- package/src/session.ts +4 -1
package/README.md
CHANGED
|
@@ -296,20 +296,28 @@ Codex 的默认模型和推理强度可继续由 `~/.codex/config.toml` 管理
|
|
|
296
296
|
|
|
297
297
|
## 可用指令
|
|
298
298
|
|
|
299
|
-
| 指令 | 作用 |
|
|
300
|
-
| --- | --- |
|
|
301
|
-
| `/new` | 使用默认 Agent 创建新会话 |
|
|
302
|
-
| `/new claude` | 创建 Claude Code 会话 |
|
|
303
|
-
| `/new cursor` | 创建 Cursor 会话 |
|
|
304
|
-
| `/new codex` | 创建 Codex 会话 |
|
|
305
|
-
| `/newh` | 重置当前会话,保留工作目录 |
|
|
306
|
-
| `/
|
|
307
|
-
| `/
|
|
308
|
-
| `/state` | 查看当前会话状态 |
|
|
309
|
-
| `/
|
|
310
|
-
| `/
|
|
311
|
-
| `/
|
|
312
|
-
| `/
|
|
299
|
+
| 指令 | 作用 | 适用 Agent |
|
|
300
|
+
| --- | --- | --- |
|
|
301
|
+
| `/new` | 使用默认 Agent 创建新会话 | 全部 |
|
|
302
|
+
| `/new claude` | 创建 Claude Code 会话 | 全部 |
|
|
303
|
+
| `/new cursor` | 创建 Cursor 会话 | 全部 |
|
|
304
|
+
| `/new codex` | 创建 Codex 会话 | 全部 |
|
|
305
|
+
| `/newh` | 重置当前会话,保留工作目录 | 全部 |
|
|
306
|
+
| `/stop` | 停止当前回复 | 全部 |
|
|
307
|
+
| `/cancel` | 取消缓存队列中的消息 | 全部 |
|
|
308
|
+
| `/state` | 查看当前会话状态 | 全部 |
|
|
309
|
+
| `/sessions` | 查看所有会话状态 | 全部 |
|
|
310
|
+
| `/session <编号>` | 将当前群聊切换到指定编号的会话 | 全部 |
|
|
311
|
+
| `/model` | 查看当前会话的可用模型 | 全部 |
|
|
312
|
+
| `/model <名称>` | 模糊匹配切换当前会话模型 | 全部 |
|
|
313
|
+
| `/model clear` | 清除模型覆盖,恢复默认 | 全部 |
|
|
314
|
+
| `/cd` | 查看当前工作目录(含最近目录快捷按钮) | 全部 |
|
|
315
|
+
| `/cd <路径>` | 设置新建会话的默认工作目录 | 全部 |
|
|
316
|
+
| `/plan <消息>` | Claude Code 使用 CLI 只读/规划模式;其他 Agent 会收到包含 `/plan` 前缀的原始消息,可能通过消息内容自行支持 | Claude Code(CLI 模式)/ 全部(消息透传) |
|
|
317
|
+
| `/ask <消息>` | Cursor 使用 CLI 只读/问答模式;其他 Agent 会收到包含 `/ask` 前缀的原始消息,可能通过消息内容自行支持 | Cursor(CLI 模式)/ 全部(消息透传) |
|
|
318
|
+
| `/git <子命令>` | 在当前会话工作目录执行 git 命令并回传输出 | 全部 |
|
|
319
|
+
| `/deleteg` | 解散当前群聊,保留 Agent 会话 | 全部(仅群聊) |
|
|
320
|
+
| `/restart` | 重启 ChatCCC 服务进程 | 全部 |
|
|
313
321
|
|
|
314
322
|
> **模型切换**:`/model` 查看可选模型清单,`/model <名称>` 模糊匹配切换,`/model clear` 恢复默认。当前仅 Claude Code 支持模型切换(需同时填写 `claude.model` 和 `claude.subagentModel`),Cursor / Codex 切换正在开发中。
|
|
315
323
|
|
package/package.json
CHANGED
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "chatccc",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "Feishu bot bridge for Claude Code",
|
|
5
|
-
"license": "Apache-2.0",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"main": "./src/index.ts",
|
|
8
|
-
"bin": {
|
|
9
|
-
"chatccc": "bin/chatccc.mjs"
|
|
10
|
-
},
|
|
11
|
-
"files": [
|
|
12
|
-
"src/",
|
|
13
|
-
"bin/",
|
|
14
|
-
"scripts/postinstall-sharp-check.mjs",
|
|
15
|
-
"demo/ilink_echo_probe.ts",
|
|
16
|
-
"im-skills/",
|
|
17
|
-
"images/img_readme_*.jpg",
|
|
18
|
-
"images/img_readme_*.png",
|
|
19
|
-
"images/avatars/status_*.png",
|
|
20
|
-
"images/avatars/badges/",
|
|
21
|
-
"package.json",
|
|
22
|
-
"README.md",
|
|
23
|
-
"config.sample.json"
|
|
24
|
-
],
|
|
25
|
-
"scripts": {
|
|
26
|
-
"dev": "tsx src/index.ts",
|
|
27
|
-
"chatccc": "tsx src/index.ts",
|
|
28
|
-
"start": "tsx src/index.ts",
|
|
29
|
-
"demo:bot-test": "tsx demo/bot_test.ts",
|
|
30
|
-
"demo:bot-test:local": "tsx demo/bot_test.ts --local",
|
|
31
|
-
"demo:create-group": "tsx src/index.ts",
|
|
32
|
-
"demo:create-group:local": "tsx src/index.ts --local",
|
|
33
|
-
"demo:permission-check": "tsx demo/permission_check.ts",
|
|
34
|
-
"demo:claude-hi": "tsx demo/claude_say_hi.ts",
|
|
35
|
-
"demo:codex-hi": "tsx demo/codex_say_hi.ts",
|
|
36
|
-
"demo:ilink-echo": "tsx demo/ilink_echo_probe.ts",
|
|
37
|
-
"test": "vitest run",
|
|
38
|
-
"test:watch": "vitest",
|
|
39
|
-
"postinstall": "node scripts/postinstall-sharp-check.mjs"
|
|
40
|
-
},
|
|
41
|
-
"dependencies": {
|
|
42
|
-
"@ai-sdk/openai-compatible": "^2.0.47",
|
|
43
|
-
"@anthropic-ai/claude-agent-sdk": "0.2.133",
|
|
44
|
-
"@larksuiteoapi/node-sdk": "^1.59.0",
|
|
45
|
-
"@openilink/openilink-sdk-node": "^0.6.0",
|
|
46
|
-
"ai": "^6.0.184",
|
|
47
|
-
"nodemailer": "^8.0.7",
|
|
48
|
-
"qrcode-terminal": "^0.12.0",
|
|
49
|
-
"sharp": "^0.34.5",
|
|
50
|
-
"tsx": "^4.0.0",
|
|
51
|
-
"ws": "^8.18.0"
|
|
52
|
-
},
|
|
53
|
-
"devDependencies": {
|
|
54
|
-
"@types/node": "^20.0.0",
|
|
55
|
-
"@types/qrcode-terminal": "^0.12.2",
|
|
56
|
-
"@types/ws": "^8.18.1",
|
|
57
|
-
"typescript": "^5.0.0",
|
|
58
|
-
"vitest": "^3.2.4"
|
|
59
|
-
},
|
|
60
|
-
"engines": {
|
|
61
|
-
"node": ">=20"
|
|
62
|
-
}
|
|
63
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "chatccc",
|
|
3
|
+
"version": "0.2.123",
|
|
4
|
+
"description": "Feishu bot bridge for Claude Code",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"chatccc": "bin/chatccc.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src/",
|
|
13
|
+
"bin/",
|
|
14
|
+
"scripts/postinstall-sharp-check.mjs",
|
|
15
|
+
"demo/ilink_echo_probe.ts",
|
|
16
|
+
"im-skills/",
|
|
17
|
+
"images/img_readme_*.jpg",
|
|
18
|
+
"images/img_readme_*.png",
|
|
19
|
+
"images/avatars/status_*.png",
|
|
20
|
+
"images/avatars/badges/",
|
|
21
|
+
"package.json",
|
|
22
|
+
"README.md",
|
|
23
|
+
"config.sample.json"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "tsx src/index.ts",
|
|
27
|
+
"chatccc": "tsx src/index.ts",
|
|
28
|
+
"start": "tsx src/index.ts",
|
|
29
|
+
"demo:bot-test": "tsx demo/bot_test.ts",
|
|
30
|
+
"demo:bot-test:local": "tsx demo/bot_test.ts --local",
|
|
31
|
+
"demo:create-group": "tsx src/index.ts",
|
|
32
|
+
"demo:create-group:local": "tsx src/index.ts --local",
|
|
33
|
+
"demo:permission-check": "tsx demo/permission_check.ts",
|
|
34
|
+
"demo:claude-hi": "tsx demo/claude_say_hi.ts",
|
|
35
|
+
"demo:codex-hi": "tsx demo/codex_say_hi.ts",
|
|
36
|
+
"demo:ilink-echo": "tsx demo/ilink_echo_probe.ts",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"test:watch": "vitest",
|
|
39
|
+
"postinstall": "node scripts/postinstall-sharp-check.mjs"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@ai-sdk/openai-compatible": "^2.0.47",
|
|
43
|
+
"@anthropic-ai/claude-agent-sdk": "0.2.133",
|
|
44
|
+
"@larksuiteoapi/node-sdk": "^1.59.0",
|
|
45
|
+
"@openilink/openilink-sdk-node": "^0.6.0",
|
|
46
|
+
"ai": "^6.0.184",
|
|
47
|
+
"nodemailer": "^8.0.7",
|
|
48
|
+
"qrcode-terminal": "^0.12.0",
|
|
49
|
+
"sharp": "^0.34.5",
|
|
50
|
+
"tsx": "^4.0.0",
|
|
51
|
+
"ws": "^8.18.0"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@types/node": "^20.0.0",
|
|
55
|
+
"@types/qrcode-terminal": "^0.12.2",
|
|
56
|
+
"@types/ws": "^8.18.1",
|
|
57
|
+
"typescript": "^5.0.0",
|
|
58
|
+
"vitest": "^3.2.4"
|
|
59
|
+
},
|
|
60
|
+
"engines": {
|
|
61
|
+
"node": ">=20"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -169,6 +169,48 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
169
169
|
expect(platform.sendText).toHaveBeenCalledWith("wx-chat", "生成中...");
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
+
it.each([
|
|
173
|
+
{ tool: "claude" as const, sessionId: "sid-claude", text: "/ask 只回答不要改文件" },
|
|
174
|
+
{ tool: "cursor" as const, sessionId: "sid-cursor", text: "/plan 先给我计划" },
|
|
175
|
+
])("passes unsupported mode commands through as raw prompts for $tool", async ({ tool, sessionId, text }) => {
|
|
176
|
+
const platform = mockPlatform();
|
|
177
|
+
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
178
|
+
yield {
|
|
179
|
+
type: "assistant" as const,
|
|
180
|
+
blocks: [{ type: "text" as const, text: `收到: ${userText}` }],
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
_setAdapterForToolForTest(tool, {
|
|
184
|
+
displayName: tool,
|
|
185
|
+
sessionDescPrefix: `${tool} Session:`,
|
|
186
|
+
createSession: vi.fn(async () => ({ sessionId })),
|
|
187
|
+
prompt,
|
|
188
|
+
getSessionInfo: async (sessionId: string): Promise<SessionInfo> => ({
|
|
189
|
+
sessionId,
|
|
190
|
+
cwd: "F:\\repo",
|
|
191
|
+
}),
|
|
192
|
+
closeSession: async () => {},
|
|
193
|
+
});
|
|
194
|
+
await recordSessionRegistry({
|
|
195
|
+
chatId: "wx-chat",
|
|
196
|
+
sessionId,
|
|
197
|
+
tool,
|
|
198
|
+
chatName: "ready-session",
|
|
199
|
+
running: false,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
await handleCommand(platform, text, "wx-chat", "wx-user", Date.now(), "p2p");
|
|
203
|
+
|
|
204
|
+
expect(platform.sendText).not.toHaveBeenCalledWith("wx-chat", expect.stringContaining("仅在以下 Agent 会话中可用"));
|
|
205
|
+
expect(prompt).toHaveBeenCalledWith(
|
|
206
|
+
sessionId,
|
|
207
|
+
expect.stringContaining(text),
|
|
208
|
+
"F:\\repo",
|
|
209
|
+
expect.any(AbortSignal),
|
|
210
|
+
expect.objectContaining({ permissionMode: undefined }),
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
|
|
172
214
|
it("cleans stale Feishu p2p binding, creates a group, and sends the private message as first prompt", async () => {
|
|
173
215
|
const platform = mockPlatform("feishu");
|
|
174
216
|
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
@@ -177,7 +219,7 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
177
219
|
blocks: [{ type: "text" as const, text: `收到: ${userText}` }],
|
|
178
220
|
};
|
|
179
221
|
});
|
|
180
|
-
_setAdapterForToolForTest("
|
|
222
|
+
_setAdapterForToolForTest("claude", {
|
|
181
223
|
displayName: "Claude",
|
|
182
224
|
sessionDescPrefix: "Claude Session:",
|
|
183
225
|
createSession: vi.fn(async () => ({ sessionId: "sid-feishu-new" })),
|
|
@@ -128,6 +128,13 @@ export interface ToolPromptOptions {
|
|
|
128
128
|
onProcessStart?: (info: ToolProcessInfo) => void;
|
|
129
129
|
/** Called when the adapter leaves the prompt process scope normally or by abort. */
|
|
130
130
|
onProcessExit?: (info: ToolProcessInfo) => void;
|
|
131
|
+
/**
|
|
132
|
+
* 权限模式覆盖:
|
|
133
|
+
* - "plan": Claude Code --permission-mode plan(只读/规划)
|
|
134
|
+
* - "ask": Cursor Agent --mode ask(只读/问答)
|
|
135
|
+
* 不传或为 undefined 时使用适配器默认行为(正常编辑模式)。
|
|
136
|
+
*/
|
|
137
|
+
permissionMode?: "plan" | "ask";
|
|
131
138
|
}
|
|
132
139
|
|
|
133
140
|
// ---------------------------------------------------------------------------
|
|
@@ -274,15 +274,18 @@ function buildCliArgs(
|
|
|
274
274
|
isEmpty: (value: string) => boolean,
|
|
275
275
|
mcpConfigJson: string | null,
|
|
276
276
|
extraArgs: string[],
|
|
277
|
+
permissionMode?: "plan" | "ask",
|
|
277
278
|
): string[] {
|
|
279
|
+
const permMode = permissionMode === "plan" ? "plan" : permissionMode === "ask" ? "default" : "bypassPermissions";
|
|
280
|
+
const skipPermissions = permissionMode !== "plan" && permissionMode !== "ask";
|
|
278
281
|
const args = [
|
|
279
282
|
"-p",
|
|
280
283
|
"--output-format", "stream-json",
|
|
281
284
|
"--verbose",
|
|
282
285
|
"--setting-sources", "user,project,local",
|
|
283
|
-
"--permission-mode",
|
|
284
|
-
"--dangerously-skip-permissions",
|
|
285
|
-
"--settings", "{\"maxTurns\":0}",
|
|
286
|
+
"--permission-mode", permMode,
|
|
287
|
+
...(skipPermissions ? ["--dangerously-skip-permissions"] : []),
|
|
288
|
+
"--settings", "{\"maxTurns\":0,\"autoCompact\":{\"enabled\":true}}",
|
|
286
289
|
];
|
|
287
290
|
|
|
288
291
|
if (!isEmpty(model)) args.push("--model", model);
|
|
@@ -432,6 +435,7 @@ class ClaudeAdapter implements ToolAdapter {
|
|
|
432
435
|
const args = buildCliArgs(
|
|
433
436
|
this.model, this.effort, this.isEmpty, mcpConfigJson,
|
|
434
437
|
["--resume", sessionId, "--input-format", "stream-json", "--replay-user-messages"],
|
|
438
|
+
options?.permissionMode,
|
|
435
439
|
);
|
|
436
440
|
|
|
437
441
|
const proc = spawnCli(args, cwd, env, true);
|
|
@@ -261,8 +261,18 @@ function spawnAgent(
|
|
|
261
261
|
cwd?: string,
|
|
262
262
|
stdinText?: string,
|
|
263
263
|
modelOverride?: string,
|
|
264
|
+
permissionMode?: "plan" | "ask",
|
|
264
265
|
): ChildProcess {
|
|
265
266
|
let allArgs = [...CURSOR_AGENT_ARGS, ...extraArgs];
|
|
267
|
+
if (permissionMode === "ask") {
|
|
268
|
+
// 替换已有的 --mode 或追加
|
|
269
|
+
const modeIdx = allArgs.findIndex((a, i) => a === "--mode" && i + 1 < allArgs.length);
|
|
270
|
+
if (modeIdx >= 0) {
|
|
271
|
+
allArgs[modeIdx + 1] = "ask";
|
|
272
|
+
} else {
|
|
273
|
+
allArgs.push("--mode", "ask");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
266
276
|
if (modelOverride) {
|
|
267
277
|
// 替换全局 --model 为 per-session override
|
|
268
278
|
const modelIdx = allArgs.findIndex((a, i) => a === "--model" && i + 1 < allArgs.length);
|
|
@@ -370,7 +380,7 @@ class CursorAdapter implements ToolAdapter {
|
|
|
370
380
|
options?: ToolPromptOptions,
|
|
371
381
|
): AsyncIterable<UnifiedStreamMessage> {
|
|
372
382
|
console.log(`[Cursor debug] prompt start: sessionId=${sessionId}, cwd=${cwd}, userTextLen=${userText.length}`);
|
|
373
|
-
const proc = spawnAgent(["--resume", sessionId], cwd, userText, this.modelOverride);
|
|
383
|
+
const proc = spawnAgent(["--resume", sessionId], cwd, userText, this.modelOverride, options?.permissionMode);
|
|
374
384
|
this.activeProcs.add(proc);
|
|
375
385
|
if (proc.pid !== undefined) options?.onProcessStart?.({ pid: proc.pid });
|
|
376
386
|
|
package/src/cards.ts
CHANGED
|
@@ -131,6 +131,8 @@ export function buildHelpCard(
|
|
|
131
131
|
"发送 **/new cursor** 创建新 Cursor 会话",
|
|
132
132
|
"发送 **/new codex** 创建新 Codex 会话",
|
|
133
133
|
"发送 **/newh** 重置当前会话(沿用当前工作目录,不切换)",
|
|
134
|
+
"发送 **/plan <消息>** 在 Claude 会话中以只读规划模式提问",
|
|
135
|
+
"发送 **/ask <消息>** 在 Cursor 会话中以只读问答模式提问",
|
|
134
136
|
].join("\n");
|
|
135
137
|
return JSON.stringify({
|
|
136
138
|
config: { wide_screen_mode: true },
|
package/src/config.ts
CHANGED
|
@@ -122,6 +122,24 @@ export interface AppConfig {
|
|
|
122
122
|
export type AgentTool = "claude" | "cursor" | "codex";
|
|
123
123
|
export const AGENT_TOOLS: AgentTool[] = ["claude", "cursor", "codex"];
|
|
124
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Agent 专属模式指令(内部配置,非用户可配)。
|
|
127
|
+
* 映射每个 Agent 支持的特殊指令及其对应的 CLI 权限模式。
|
|
128
|
+
*
|
|
129
|
+
* - claude: /plan → --permission-mode plan(只读/规划)
|
|
130
|
+
* - cursor: /ask → --mode ask(只读/问答)
|
|
131
|
+
* - codex: 暂无原生只读模式,不注册任何模式指令
|
|
132
|
+
*/
|
|
133
|
+
export const AGENT_MODE_COMMANDS: Record<AgentTool, { command: string; mode: "plan" | "ask"; description: string }[]> = {
|
|
134
|
+
claude: [
|
|
135
|
+
{ command: "/plan", mode: "plan", description: "以只读规划模式提问(Claude 专属)" },
|
|
136
|
+
],
|
|
137
|
+
cursor: [
|
|
138
|
+
{ command: "/ask", mode: "ask", description: "以只读问答模式提问(Cursor 专属)" },
|
|
139
|
+
],
|
|
140
|
+
codex: [],
|
|
141
|
+
};
|
|
142
|
+
|
|
125
143
|
/** 获取指定 agent 配置中所有模型相关的值(最多 100 个,去重) */
|
|
126
144
|
export function getAllModelsForTool(tool: AgentTool, cfg: AppConfig = config): string[] {
|
|
127
145
|
const seen = new Set<string>();
|
package/src/orchestrator.ts
CHANGED
|
@@ -19,6 +19,8 @@ import {
|
|
|
19
19
|
CONFIG_FILE,
|
|
20
20
|
GIT_TIMEOUT_MS,
|
|
21
21
|
PROJECT_ROOT,
|
|
22
|
+
AGENT_MODE_COMMANDS,
|
|
23
|
+
AGENT_TOOLS,
|
|
22
24
|
anthropicConfigDisplay,
|
|
23
25
|
config,
|
|
24
26
|
fileLog,
|
|
@@ -125,6 +127,85 @@ function isNonWechatP2p(platform: PlatformAdapter, chatType: string): boolean {
|
|
|
125
127
|
return chatType === "p2p" && platform.kind !== "wechat";
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
/** 根据 Agent 类型构建欢迎卡片中的指令说明 */
|
|
131
|
+
function buildWelcomeCommands(tool: string): string {
|
|
132
|
+
const lines = [
|
|
133
|
+
"发送 **/cd** 切换新建会话的默认目录。",
|
|
134
|
+
"发送 **/model** 查看或切换当前会话的模型。",
|
|
135
|
+
"发送 **/new** 创建新会话,**/newh** 重置当前会话(沿用工作目录)。",
|
|
136
|
+
"发送 **/sessions** 查看所有会话状态。",
|
|
137
|
+
];
|
|
138
|
+
const modeCmds = AGENT_MODE_COMMANDS[tool as AgentTool] ?? [];
|
|
139
|
+
for (const mc of modeCmds) {
|
|
140
|
+
lines.push(`发送 **${mc.command} <消息>** ${mc.description}。`);
|
|
141
|
+
}
|
|
142
|
+
return lines.join("\n");
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** 根据指令名从 AGENT_MODE_COMMANDS 查找对应的 mode 和支持的 agent 列表 */
|
|
146
|
+
function resolveModeCommand(cmd: string): { mode: "plan" | "ask"; supportedTools: string[] } | null {
|
|
147
|
+
const supportedTools: string[] = [];
|
|
148
|
+
let mode: "plan" | "ask" | null = null;
|
|
149
|
+
for (const tool of AGENT_TOOLS) {
|
|
150
|
+
for (const mc of AGENT_MODE_COMMANDS[tool]) {
|
|
151
|
+
if (mc.command === cmd) {
|
|
152
|
+
supportedTools.push(tool);
|
|
153
|
+
mode = mc.mode;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return mode ? { mode, supportedTools } : null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 如果当前群名是未命名会话("新会话" 或 "新会话-xxx"),用消息内容前缀重命名 */
|
|
162
|
+
async function renameUntitledSession(
|
|
163
|
+
platform: PlatformAdapter,
|
|
164
|
+
chatId: string,
|
|
165
|
+
chatType: string,
|
|
166
|
+
sessionId: string,
|
|
167
|
+
tool: string,
|
|
168
|
+
namePrefix: string,
|
|
169
|
+
description: string | undefined,
|
|
170
|
+
chatInfo: { name: string } | undefined,
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
const MAX_PREFIX = 10;
|
|
173
|
+
const prefix = namePrefix.slice(0, MAX_PREFIX);
|
|
174
|
+
|
|
175
|
+
if (chatType !== "p2p" && chatInfo && isUntitledSessionChatName(chatInfo.name)) {
|
|
176
|
+
const adapter = getAdapterForTool(tool, sessionId);
|
|
177
|
+
const info = await adapter.getSessionInfo(sessionId).catch(() => undefined);
|
|
178
|
+
const sessionCwd = info?.cwd ?? (await getDefaultCwd(chatId));
|
|
179
|
+
const newName = sessionChatName(prefix, sessionCwd);
|
|
180
|
+
try {
|
|
181
|
+
await platform.updateChatInfo(chatId, newName, description!);
|
|
182
|
+
console.log(`[${ts()}] [RENAME] First message → group renamed to "${newName}"`);
|
|
183
|
+
await recordSessionRegistry({ chatId, sessionId, tool, chatName: newName }).catch(() => {});
|
|
184
|
+
await saveSessionTool(sessionId, tool, newName).catch(() => {});
|
|
185
|
+
} catch (err) {
|
|
186
|
+
console.error(`[${ts()}] [RENAME] Failed: ${(err as Error).message}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (chatType === "p2p" && platform.kind === "wechat") {
|
|
191
|
+
try {
|
|
192
|
+
const reg = await loadSessionRegistryForBinding();
|
|
193
|
+
const rec = reg[chatId];
|
|
194
|
+
if (rec && rec.sessionId === sessionId && isUntitledSessionChatName(rec.chatName ?? "")) {
|
|
195
|
+
const adapter = getAdapterForTool(tool, sessionId);
|
|
196
|
+
const info = await adapter.getSessionInfo(sessionId).catch(() => undefined);
|
|
197
|
+
const sessionCwd = info?.cwd ?? (await getDefaultCwd(chatId));
|
|
198
|
+
const newName2 = sessionChatName(prefix, sessionCwd);
|
|
199
|
+
await recordSessionRegistry({ chatId, sessionId, tool, chatName: newName2 }).catch(() => {});
|
|
200
|
+
await saveSessionTool(sessionId, tool, newName2).catch(() => {});
|
|
201
|
+
console.log(`[${ts()}] [RENAME] WeChat P2P → "${newName2}"`);
|
|
202
|
+
}
|
|
203
|
+
} catch (err) {
|
|
204
|
+
console.error(`[${ts()}] [RENAME] WeChat P2P failed: ${(err as Error).message}`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
128
209
|
async function cleanupNonWechatP2pBinding(
|
|
129
210
|
platform: PlatformAdapter,
|
|
130
211
|
chatId: string,
|
|
@@ -429,11 +510,7 @@ export async function handleCommand(
|
|
|
429
510
|
`**Session ID:** ${sessionId}\n` +
|
|
430
511
|
`**工作目录:** \`${cwd}\`\n\n` +
|
|
431
512
|
`直接在这里发消息即可与 ${toolLabel} 对话。\n\n` +
|
|
432
|
-
|
|
433
|
-
`发送 **/model** 查看或切换当前会话的模型。\n` +
|
|
434
|
-
`发送 **/new** 创建新会话,**/newh** 重置当前会话(沿用工作目录)。\n` +
|
|
435
|
-
`发送 **/sessions** 查看所有会话状态。\n` +
|
|
436
|
-
`发送 \`/git <子命令>\` 在本会话工作目录执行 git,例如 \`/git status\`、\`/git log --oneline -n 5\`。`,
|
|
513
|
+
buildWelcomeCommands(tool),
|
|
437
514
|
"green",
|
|
438
515
|
);
|
|
439
516
|
console.log(
|
|
@@ -515,11 +592,7 @@ export async function handleCommand(
|
|
|
515
592
|
`**Session ID:** ${sessionId}\n` +
|
|
516
593
|
`**工作目录:** \`${cwd}\`\n\n` +
|
|
517
594
|
`直接在这里发消息即可与 ${toolLabel} 对话。\n\n` +
|
|
518
|
-
|
|
519
|
-
`发送 **/model** 查看或切换当前会话的模型。\n` +
|
|
520
|
-
`发送 **/new** 创建新会话,**/newh** 重置当前会话(沿用工作目录)。\n` +
|
|
521
|
-
`发送 **/sessions** 查看所有会话状态。\n` +
|
|
522
|
-
`发送 \`/git <子命令>\` 在本会话工作目录执行 git,例如 \`/git status\`、\`/git log --oneline -n 5\`。`,
|
|
595
|
+
buildWelcomeCommands(tool),
|
|
523
596
|
"green",
|
|
524
597
|
);
|
|
525
598
|
|
|
@@ -877,9 +950,8 @@ export async function handleCommand(
|
|
|
877
950
|
`会话已重置为新的 **${toolLabel}** 会话。\n\n` +
|
|
878
951
|
`**Session ID:** ${newSessionId}\n` +
|
|
879
952
|
`**工作目录:** \`${cwd}\`(沿用当前会话目录)\n\n` +
|
|
880
|
-
`直接在这里发消息即可继续对话。\n` +
|
|
881
|
-
|
|
882
|
-
`发送 **/model** 查看或切换当前会话的模型。`,
|
|
953
|
+
`直接在这里发消息即可继续对话。\n\n` +
|
|
954
|
+
buildWelcomeCommands(descriptionTool),
|
|
883
955
|
"green",
|
|
884
956
|
);
|
|
885
957
|
|
|
@@ -1181,6 +1253,134 @@ export async function handleCommand(
|
|
|
1181
1253
|
return;
|
|
1182
1254
|
}
|
|
1183
1255
|
|
|
1256
|
+
// /plan <message> — 只读/规划模式
|
|
1257
|
+
if (textLower.startsWith("/plan ")) {
|
|
1258
|
+
const planText = text.slice(6).trim();
|
|
1259
|
+
const resolved = resolveModeCommand("/plan");
|
|
1260
|
+
if (!planText) {
|
|
1261
|
+
await platform.sendText(chatId, "用法:`/plan <消息>` — 在只读/规划模式下提问。").catch(() => {});
|
|
1262
|
+
logTrace(tid, "DONE", { outcome: "plan_no_message" });
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
if (resolved?.supportedTools.includes(descriptionTool) === true) {
|
|
1266
|
+
logTrace(tid, "BRANCH", { cmd: "/plan", sessionId, tool: descriptionTool });
|
|
1267
|
+
|
|
1268
|
+
await renameUntitledSession(
|
|
1269
|
+
platform, chatId, chatType, sessionId, descriptionTool,
|
|
1270
|
+
planText, description, chatInfo,
|
|
1271
|
+
);
|
|
1272
|
+
|
|
1273
|
+
if (isSessionRunning(sessionId)) {
|
|
1274
|
+
const queued = enqueueMessage(sessionId, {
|
|
1275
|
+
text, chatId, openId, msgTimestamp, chatType, traceId: tid,
|
|
1276
|
+
});
|
|
1277
|
+
if (queued) {
|
|
1278
|
+
logTrace(tid, "QUEUED", { sessionId, mode: "plan" });
|
|
1279
|
+
if (platform.kind === "wechat") {
|
|
1280
|
+
await platform.sendText(chatId, "当前会话正在生成中,你的消息已进入缓存队列,生成完成后会立即处理。发送 /cancel 可取消缓存。").catch(() => {});
|
|
1281
|
+
} else {
|
|
1282
|
+
await platform.sendRawCard(chatId, buildQueuedCard(planText)).catch(() => {});
|
|
1283
|
+
}
|
|
1284
|
+
} else {
|
|
1285
|
+
logTrace(tid, "QUEUE_FULL", { sessionId });
|
|
1286
|
+
if (platform.kind === "wechat") {
|
|
1287
|
+
await platform.sendText(chatId, "当前缓存队列中已有消息等待处理,请等待或发送 /stop(停止生成)或 /cancel(取消缓存)。").catch(() => {});
|
|
1288
|
+
} else {
|
|
1289
|
+
await platform.sendRawCard(chatId, buildQueueFullCard()).catch(() => {});
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
if (shouldSendWechatProcessingAck(platform, planText.toLowerCase(), chatType)) {
|
|
1296
|
+
await platform.sendText(chatId, "生成中...").catch(() => {});
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
try {
|
|
1300
|
+
logTrace(tid, "RESUME", { sessionId, tool: descriptionTool, mode: "plan" });
|
|
1301
|
+
await resumeAndPrompt(
|
|
1302
|
+
sessionId, planText, platform, chatId, msgTimestamp,
|
|
1303
|
+
descriptionTool, tid, "plan",
|
|
1304
|
+
);
|
|
1305
|
+
logTrace(tid, "DONE", { outcome: "plan_done", sessionId });
|
|
1306
|
+
} catch (err) {
|
|
1307
|
+
logTrace(tid, "DONE", { outcome: "plan_fail", error: (err as Error).message });
|
|
1308
|
+
await platform.sendCard(
|
|
1309
|
+
chatId, "Error",
|
|
1310
|
+
`Failed to run plan mode:\n${(err as Error).message}`,
|
|
1311
|
+
"red",
|
|
1312
|
+
);
|
|
1313
|
+
}
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
logTrace(tid, "BRANCH", { cmd: "/plan", sessionId, tool: descriptionTool, fallback: "raw_message" });
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
// /ask <message> — 只读/问答模式
|
|
1321
|
+
if (textLower.startsWith("/ask ")) {
|
|
1322
|
+
const askText = text.slice(5).trim();
|
|
1323
|
+
const resolved = resolveModeCommand("/ask");
|
|
1324
|
+
if (!askText) {
|
|
1325
|
+
await platform.sendText(chatId, "用法:`/ask <消息>` — 在只读/问答模式下提问。").catch(() => {});
|
|
1326
|
+
logTrace(tid, "DONE", { outcome: "ask_no_message" });
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
if (resolved?.supportedTools.includes(descriptionTool) === true) {
|
|
1330
|
+
logTrace(tid, "BRANCH", { cmd: "/ask", sessionId, tool: descriptionTool });
|
|
1331
|
+
|
|
1332
|
+
await renameUntitledSession(
|
|
1333
|
+
platform, chatId, chatType, sessionId, descriptionTool,
|
|
1334
|
+
askText, description, chatInfo,
|
|
1335
|
+
);
|
|
1336
|
+
|
|
1337
|
+
if (isSessionRunning(sessionId)) {
|
|
1338
|
+
const queued = enqueueMessage(sessionId, {
|
|
1339
|
+
text, chatId, openId, msgTimestamp, chatType, traceId: tid,
|
|
1340
|
+
});
|
|
1341
|
+
if (queued) {
|
|
1342
|
+
logTrace(tid, "QUEUED", { sessionId, mode: "ask" });
|
|
1343
|
+
if (platform.kind === "wechat") {
|
|
1344
|
+
await platform.sendText(chatId, "当前会话正在生成中,你的消息已进入缓存队列,生成完成后会立即处理。发送 /cancel 可取消缓存。").catch(() => {});
|
|
1345
|
+
} else {
|
|
1346
|
+
await platform.sendRawCard(chatId, buildQueuedCard(askText)).catch(() => {});
|
|
1347
|
+
}
|
|
1348
|
+
} else {
|
|
1349
|
+
logTrace(tid, "QUEUE_FULL", { sessionId });
|
|
1350
|
+
if (platform.kind === "wechat") {
|
|
1351
|
+
await platform.sendText(chatId, "当前缓存队列中已有消息等待处理,请等待或发送 /stop(停止生成)或 /cancel(取消缓存)。").catch(() => {});
|
|
1352
|
+
} else {
|
|
1353
|
+
await platform.sendRawCard(chatId, buildQueueFullCard()).catch(() => {});
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
return;
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
if (shouldSendWechatProcessingAck(platform, askText.toLowerCase(), chatType)) {
|
|
1360
|
+
await platform.sendText(chatId, "生成中...").catch(() => {});
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
try {
|
|
1364
|
+
logTrace(tid, "RESUME", { sessionId, tool: descriptionTool, mode: "ask" });
|
|
1365
|
+
await resumeAndPrompt(
|
|
1366
|
+
sessionId, askText, platform, chatId, msgTimestamp,
|
|
1367
|
+
descriptionTool, tid, "ask",
|
|
1368
|
+
);
|
|
1369
|
+
logTrace(tid, "DONE", { outcome: "ask_done", sessionId });
|
|
1370
|
+
} catch (err) {
|
|
1371
|
+
logTrace(tid, "DONE", { outcome: "ask_fail", error: (err as Error).message });
|
|
1372
|
+
await platform.sendCard(
|
|
1373
|
+
chatId, "Error",
|
|
1374
|
+
`Failed to run ask mode:\n${(err as Error).message}`,
|
|
1375
|
+
"red",
|
|
1376
|
+
);
|
|
1377
|
+
}
|
|
1378
|
+
return;
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
logTrace(tid, "BRANCH", { cmd: "/ask", sessionId, tool: descriptionTool, fallback: "raw_message" });
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1184
1384
|
const lastTs = lastMsgTimestamps.get(chatId);
|
|
1185
1385
|
if (lastTs !== undefined && msgTimestamp <= lastTs) {
|
|
1186
1386
|
logTrace(tid, "DONE", {
|
|
@@ -1419,8 +1619,7 @@ export async function handleCommand(
|
|
|
1419
1619
|
`**Session ID:** ${sessionId}\n` +
|
|
1420
1620
|
`**工作目录:** \`${cwd}\`\n\n` +
|
|
1421
1621
|
`下面会自动把你的私聊消息作为第一句话发送给 ${toolLabel}。\n\n` +
|
|
1422
|
-
|
|
1423
|
-
`发送 **/new** 创建新会话,**/newh** 重置当前会话(沿用工作目录)。`,
|
|
1622
|
+
buildWelcomeCommands(tool),
|
|
1424
1623
|
"green",
|
|
1425
1624
|
);
|
|
1426
1625
|
platform.setChatAvatar(newChatId, tool, "new").catch(() => {});
|
package/src/session.ts
CHANGED
|
@@ -832,8 +832,9 @@ export async function resumeAndPrompt(
|
|
|
832
832
|
msgTimestamp: number,
|
|
833
833
|
tool: string,
|
|
834
834
|
traceId?: string,
|
|
835
|
+
permissionMode?: "plan" | "ask",
|
|
835
836
|
): Promise<void> {
|
|
836
|
-
return runAgentSession(sessionId, userText, platform, chatId, msgTimestamp, tool, traceId);
|
|
837
|
+
return runAgentSession(sessionId, userText, platform, chatId, msgTimestamp, tool, traceId, permissionMode);
|
|
837
838
|
}
|
|
838
839
|
|
|
839
840
|
// ---------------------------------------------------------------------------
|
|
@@ -848,6 +849,7 @@ export async function runAgentSession(
|
|
|
848
849
|
msgTimestamp: number,
|
|
849
850
|
tool: string,
|
|
850
851
|
traceId?: string,
|
|
852
|
+
permissionMode?: "plan" | "ask",
|
|
851
853
|
): Promise<void> {
|
|
852
854
|
const tid = traceId ?? "";
|
|
853
855
|
|
|
@@ -1106,6 +1108,7 @@ export async function runAgentSession(
|
|
|
1106
1108
|
clearPromptProcessMonitor(sessionId);
|
|
1107
1109
|
if (exitInfo.pid !== undefined) unregisterProcess(exitInfo.pid);
|
|
1108
1110
|
},
|
|
1111
|
+
permissionMode,
|
|
1109
1112
|
})) {
|
|
1110
1113
|
for (const block of unifiedMsg.blocks) {
|
|
1111
1114
|
accumulateBlockContent(block, state, toolCallMap);
|