chatccc 0.2.190 → 0.2.192

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.
Files changed (72) hide show
  1. package/agent-prompts/claude_specific.md +45 -45
  2. package/agent-prompts/codex_specific.md +2 -2
  3. package/agent-prompts/cursor_specific.md +13 -13
  4. package/config.sample.json +14 -14
  5. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  6. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  7. package/im-skills/feishu-skill/skill.md +3 -3
  8. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  9. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  10. package/im-skills/wechat-file-skill/skill.md +10 -10
  11. package/im-skills/wechat-image-skill/skill.md +10 -10
  12. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  13. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  14. package/im-skills/wechat-video-skill/skill.md +10 -10
  15. package/package.json +1 -1
  16. package/scripts/postinstall-sharp-check.mjs +58 -58
  17. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  18. package/src/__tests__/builtin-chat-session.test.ts +76 -0
  19. package/src/__tests__/builtin-config.test.ts +33 -33
  20. package/src/__tests__/builtin-context.test.ts +126 -0
  21. package/src/__tests__/builtin-sigint.test.ts +56 -0
  22. package/src/__tests__/card-plain-text.test.ts +5 -5
  23. package/src/__tests__/cardkit.test.ts +60 -60
  24. package/src/__tests__/cards.test.ts +77 -77
  25. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  26. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  27. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  28. package/src/__tests__/claude-adapter.test.ts +592 -592
  29. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  30. package/src/__tests__/config-reload.test.ts +4 -4
  31. package/src/__tests__/config-sample.test.ts +17 -17
  32. package/src/__tests__/feishu-api.test.ts +60 -60
  33. package/src/__tests__/feishu-platform.test.ts +22 -22
  34. package/src/__tests__/format-message.test.ts +47 -47
  35. package/src/__tests__/orchestrator.test.ts +120 -120
  36. package/src/__tests__/privacy.test.ts +198 -198
  37. package/src/__tests__/raw-stream-log.test.ts +106 -106
  38. package/src/__tests__/session.test.ts +17 -17
  39. package/src/__tests__/shared-prefix.test.ts +36 -36
  40. package/src/__tests__/stream-state.test.ts +42 -42
  41. package/src/__tests__/web-ui.test.ts +209 -209
  42. package/src/adapters/claude-adapter.ts +566 -566
  43. package/src/adapters/claude-session-meta-store.ts +120 -120
  44. package/src/adapters/codex-adapter.ts +30 -30
  45. package/src/adapters/cursor-adapter.ts +46 -46
  46. package/src/adapters/raw-stream-log.ts +124 -124
  47. package/src/adapters/resource-monitor.ts +140 -140
  48. package/src/agent-delegate-task-rpc.ts +153 -153
  49. package/src/agent-delegate-task.ts +81 -81
  50. package/src/agent-stop-stuck.ts +129 -129
  51. package/src/builtin/cli.ts +211 -189
  52. package/src/builtin/context.ts +225 -0
  53. package/src/builtin/index.ts +232 -170
  54. package/src/builtin/sigint.ts +50 -0
  55. package/src/cards.ts +137 -137
  56. package/src/chatgpt-subscription-rpc.ts +27 -27
  57. package/src/chatgpt-subscription.ts +299 -299
  58. package/src/chrome-devtools-guard.ts +318 -318
  59. package/src/codex-reset-actions.ts +184 -184
  60. package/src/config.ts +86 -86
  61. package/src/feishu-platform.ts +20 -20
  62. package/src/format-message.ts +293 -293
  63. package/src/litellm-proxy.ts +374 -374
  64. package/src/orchestrator.ts +143 -143
  65. package/src/privacy.ts +118 -118
  66. package/src/session-chat-binding.ts +6 -6
  67. package/src/session-name.ts +8 -8
  68. package/src/session.ts +98 -98
  69. package/src/shared-prefix.ts +29 -29
  70. package/src/sim-platform.ts +20 -20
  71. package/src/turn-cards.ts +117 -117
  72. package/src/web-ui.ts +205 -205
@@ -1,129 +1,129 @@
1
- import type { IncomingMessage, ServerResponse } from "node:http";
2
-
3
- import { readUtf8JsonBody } from "./agent-rpc-body.ts";
4
- import { activePrompts, getChatsForSession } from "./session-chat-binding.ts";
5
- import { getPlatformForChat } from "./session.ts";
6
- import { readStreamState, writeStreamState } from "./stream-state.ts";
7
- import { ts } from "./config.ts";
8
-
9
- export const AGENT_STOP_STUCK_PATH = "/api/agent/stop-stuck-loop";
10
-
11
- const MAX_REQUEST_BYTES = 8 * 1024;
12
-
13
- function jsonReply(res: ServerResponse, status: number, data: unknown): void {
14
- res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
15
- res.end(JSON.stringify(data));
16
- }
17
-
18
- /**
19
- * 从原始 body 字节中用正则尽力提取 session_id。
20
- * 兼容 JSON 编码异常(如 GBK 中文导致 JSON.parse 失败)的场景。
21
- */
22
- function extractSessionIdFromRaw(buf: Buffer): string | null {
23
- // 匹配 "session_id":"<uuid>" 或 "session_id": "<uuid>"
24
- const match = buf.toString("latin1").match(/"session_id"\s*:\s*"([^"]+)"/);
25
- return match?.[1] ?? null;
26
- }
27
-
28
- export async function handleAgentStopStuckRequest(
29
- req: IncomingMessage,
30
- res: ServerResponse,
31
- ): Promise<boolean> {
32
- const url = new URL(req.url ?? "/", "http://127.0.0.1");
33
- if (url.pathname !== AGENT_STOP_STUCK_PATH) return false;
34
-
35
- if (req.method !== "POST") {
36
- jsonReply(res, 405, { error: "Method not allowed, use POST" });
37
- return true;
38
- }
39
-
40
- let body: { session_id?: string; final_reply?: string };
41
- let rawBody: Buffer | null = null;
42
- try {
43
- body = await readUtf8JsonBody<{ session_id?: string; final_reply?: string }>(req, MAX_REQUEST_BYTES);
44
- } catch (err) {
45
- // JSON 解析失败时仍尝试从原始字节中提取 session_id 并强行停止
46
- // 宁可输出乱码,也不能让 agent 持续循环
47
- rawBody = (err as { rawBody?: Buffer }).rawBody as Buffer | undefined ?? null;
48
- if (!rawBody) {
49
- jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
50
- return true;
51
- }
52
- const fallbackId = extractSessionIdFromRaw(rawBody);
53
- if (!fallbackId) {
54
- jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
55
- return true;
56
- }
57
- body = { session_id: fallbackId };
58
- }
59
-
60
- const sessionId = typeof body?.session_id === "string" ? body.session_id.trim() : "";
61
- if (!sessionId) {
62
- jsonReply(res, 400, { error: "session_id is required" });
63
- return true;
64
- }
65
-
66
- const prompt = activePrompts.get(sessionId);
67
- if (!prompt) {
68
- jsonReply(res, 404, { error: "Session not found or not running" });
69
- return true;
70
- }
71
-
72
- // 先发"卡住"提示消息给所有绑定的群聊
73
- const chats = getChatsForSession(sessionId);
74
- for (const chatId of chats) {
75
- const platform = getPlatformForChat(chatId);
76
- if (platform) {
77
- await platform.sendText(chatId, "⚠️ Agent 检测到自己陷入了循环,正在结束生成…").catch(() => {});
78
- }
79
- }
80
-
81
- // 不丢弃缓存队列中的消息,让 runAgentSession 的 finally 块正常消费,
82
- // 确保 stop-stuck-loop 结束后排队的消息仍能被正常处理。
83
-
84
- const finalReply = typeof body?.final_reply === "string" ? body.final_reply.trim() : "";
85
-
86
- // fire-and-forget:立即把 stream-state 标为 done(而非 stopped),
87
- // 让 display loop 以"正常完成"而非"已停止"来渲染卡片和最终回复。
88
- // 不设 prompt.stopped = true,这样 runAgentSession 的 finally 也会写 "done"。
89
- void (async () => {
90
- try {
91
- const current = await readStreamState(sessionId);
92
- if (!current) return;
93
- if (current.status !== "running") return;
94
- await writeStreamState({
95
- ...current,
96
- status: "done",
97
- stuckAt: Date.now(),
98
- finalReply: finalReply ? current.finalReply + "\n\n" + finalReply : current.finalReply,
99
- updatedAt: Date.now(),
100
- });
101
- } catch (err) {
102
- console.warn(
103
- `[${ts()}] [STUCK-LOOP] writeStreamState(done) failed for ${sessionId}: ${(err as Error).message}`,
104
- );
105
- }
106
- })();
107
-
108
- // 先关闭底层 SDK session,终止 CLI 子进程,然后再 abort 清理 adapter 层
109
- prompt.closeSession?.();
110
-
111
- // 强制杀死 CLI 子进程。controller.abort() 只在 for-await 收到下一条
112
- // stream 消息时才被检测到——如果 agent 陷入无输出的计算循环,abort 信号
113
- // 永远不会生效;SDK 的 session.close() 也不保证立即终止子进程。
114
- if (prompt.processPid !== undefined) {
115
- try {
116
- process.kill(prompt.processPid);
117
- } catch {
118
- // 进程可能已退出,忽略错误
119
- }
120
- }
121
-
122
- // 不设 stopped 标记 → finally block 写 "done" → 卡片正常结束
123
- prompt.controller.abort();
124
-
125
- console.log(`[${ts()}] [STUCK-LOOP] Session ${sessionId} aborted as done (agent detected stuck loop, final_reply=${finalReply ? "yes" : "no"})`);
126
-
127
- jsonReply(res, 200, { ok: true });
128
- return true;
129
- }
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+
3
+ import { readUtf8JsonBody } from "./agent-rpc-body.ts";
4
+ import { activePrompts, getChatsForSession } from "./session-chat-binding.ts";
5
+ import { getPlatformForChat } from "./session.ts";
6
+ import { readStreamState, writeStreamState } from "./stream-state.ts";
7
+ import { ts } from "./config.ts";
8
+
9
+ export const AGENT_STOP_STUCK_PATH = "/api/agent/stop-stuck-loop";
10
+
11
+ const MAX_REQUEST_BYTES = 8 * 1024;
12
+
13
+ function jsonReply(res: ServerResponse, status: number, data: unknown): void {
14
+ res.writeHead(status, { "Content-Type": "application/json; charset=utf-8" });
15
+ res.end(JSON.stringify(data));
16
+ }
17
+
18
+ /**
19
+ * 从原始 body 字节中用正则尽力提取 session_id。
20
+ * 兼容 JSON 编码异常(如 GBK 中文导致 JSON.parse 失败)的场景。
21
+ */
22
+ function extractSessionIdFromRaw(buf: Buffer): string | null {
23
+ // 匹配 "session_id":"<uuid>" 或 "session_id": "<uuid>"
24
+ const match = buf.toString("latin1").match(/"session_id"\s*:\s*"([^"]+)"/);
25
+ return match?.[1] ?? null;
26
+ }
27
+
28
+ export async function handleAgentStopStuckRequest(
29
+ req: IncomingMessage,
30
+ res: ServerResponse,
31
+ ): Promise<boolean> {
32
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
33
+ if (url.pathname !== AGENT_STOP_STUCK_PATH) return false;
34
+
35
+ if (req.method !== "POST") {
36
+ jsonReply(res, 405, { error: "Method not allowed, use POST" });
37
+ return true;
38
+ }
39
+
40
+ let body: { session_id?: string; final_reply?: string };
41
+ let rawBody: Buffer | null = null;
42
+ try {
43
+ body = await readUtf8JsonBody<{ session_id?: string; final_reply?: string }>(req, MAX_REQUEST_BYTES);
44
+ } catch (err) {
45
+ // JSON 解析失败时仍尝试从原始字节中提取 session_id 并强行停止
46
+ // 宁可输出乱码,也不能让 agent 持续循环
47
+ rawBody = (err as { rawBody?: Buffer }).rawBody as Buffer | undefined ?? null;
48
+ if (!rawBody) {
49
+ jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
50
+ return true;
51
+ }
52
+ const fallbackId = extractSessionIdFromRaw(rawBody);
53
+ if (!fallbackId) {
54
+ jsonReply(res, 400, { error: `Invalid request body: ${(err as Error).message}` });
55
+ return true;
56
+ }
57
+ body = { session_id: fallbackId };
58
+ }
59
+
60
+ const sessionId = typeof body?.session_id === "string" ? body.session_id.trim() : "";
61
+ if (!sessionId) {
62
+ jsonReply(res, 400, { error: "session_id is required" });
63
+ return true;
64
+ }
65
+
66
+ const prompt = activePrompts.get(sessionId);
67
+ if (!prompt) {
68
+ jsonReply(res, 404, { error: "Session not found or not running" });
69
+ return true;
70
+ }
71
+
72
+ // 先发"卡住"提示消息给所有绑定的群聊
73
+ const chats = getChatsForSession(sessionId);
74
+ for (const chatId of chats) {
75
+ const platform = getPlatformForChat(chatId);
76
+ if (platform) {
77
+ await platform.sendText(chatId, "⚠️ Agent 检测到自己陷入了循环,正在结束生成…").catch(() => {});
78
+ }
79
+ }
80
+
81
+ // 不丢弃缓存队列中的消息,让 runAgentSession 的 finally 块正常消费,
82
+ // 确保 stop-stuck-loop 结束后排队的消息仍能被正常处理。
83
+
84
+ const finalReply = typeof body?.final_reply === "string" ? body.final_reply.trim() : "";
85
+
86
+ // fire-and-forget:立即把 stream-state 标为 done(而非 stopped),
87
+ // 让 display loop 以"正常完成"而非"已停止"来渲染卡片和最终回复。
88
+ // 不设 prompt.stopped = true,这样 runAgentSession 的 finally 也会写 "done"。
89
+ void (async () => {
90
+ try {
91
+ const current = await readStreamState(sessionId);
92
+ if (!current) return;
93
+ if (current.status !== "running") return;
94
+ await writeStreamState({
95
+ ...current,
96
+ status: "done",
97
+ stuckAt: Date.now(),
98
+ finalReply: finalReply ? current.finalReply + "\n\n" + finalReply : current.finalReply,
99
+ updatedAt: Date.now(),
100
+ });
101
+ } catch (err) {
102
+ console.warn(
103
+ `[${ts()}] [STUCK-LOOP] writeStreamState(done) failed for ${sessionId}: ${(err as Error).message}`,
104
+ );
105
+ }
106
+ })();
107
+
108
+ // 先关闭底层 SDK session,终止 CLI 子进程,然后再 abort 清理 adapter 层
109
+ prompt.closeSession?.();
110
+
111
+ // 强制杀死 CLI 子进程。controller.abort() 只在 for-await 收到下一条
112
+ // stream 消息时才被检测到——如果 agent 陷入无输出的计算循环,abort 信号
113
+ // 永远不会生效;SDK 的 session.close() 也不保证立即终止子进程。
114
+ if (prompt.processPid !== undefined) {
115
+ try {
116
+ process.kill(prompt.processPid);
117
+ } catch {
118
+ // 进程可能已退出,忽略错误
119
+ }
120
+ }
121
+
122
+ // 不设 stopped 标记 → finally block 写 "done" → 卡片正常结束
123
+ prompt.controller.abort();
124
+
125
+ console.log(`[${ts()}] [STUCK-LOOP] Session ${sessionId} aborted as done (agent detected stuck loop, final_reply=${finalReply ? "yes" : "no"})`);
126
+
127
+ jsonReply(res, 200, { ok: true });
128
+ return true;
129
+ }