chatccc 0.2.226 → 0.2.228

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 (67) hide show
  1. package/.agents/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  2. package/.claude/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  3. package/.cursor/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  4. package/README.md +90 -90
  5. package/package.json +1 -1
  6. package/src/__tests__/agent-activity.test.ts +76 -76
  7. package/src/__tests__/builtin-chat-session.test.ts +350 -350
  8. package/src/__tests__/builtin-config.test.ts +26 -26
  9. package/src/__tests__/builtin-context.test.ts +163 -163
  10. package/src/__tests__/builtin-file-tools.test.ts +275 -275
  11. package/src/__tests__/builtin-permissions.test.ts +211 -211
  12. package/src/__tests__/builtin-session-select.test.ts +116 -116
  13. package/src/__tests__/builtin-skills.test.ts +252 -141
  14. package/src/__tests__/builtin-web-tools.test.ts +220 -0
  15. package/src/__tests__/card-action-routing.test.ts +18 -18
  16. package/src/__tests__/ccc-adapter.test.ts +136 -136
  17. package/src/__tests__/claude-adapter.test.ts +614 -614
  18. package/src/__tests__/codex-adapter.test.ts +58 -58
  19. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  20. package/src/__tests__/cursor-adapter.test.ts +268 -268
  21. package/src/__tests__/feishu-avatar.test.ts +164 -164
  22. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  23. package/src/__tests__/package-files.test.ts +24 -24
  24. package/src/__tests__/progress-reducer.test.ts +110 -110
  25. package/src/__tests__/response-stall.test.ts +49 -49
  26. package/src/__tests__/sim-platform.test.ts +16 -16
  27. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  28. package/src/__tests__/stop-session.test.ts +34 -34
  29. package/src/__tests__/terminal-renderer.test.ts +247 -247
  30. package/src/__tests__/update-command-guard.test.ts +144 -144
  31. package/src/__tests__/web-ui.test.ts +326 -326
  32. package/src/adapters/adapter-interface.ts +18 -18
  33. package/src/adapters/ccc-adapter.ts +131 -131
  34. package/src/adapters/claude-adapter.ts +620 -620
  35. package/src/adapters/codex-adapter.ts +426 -426
  36. package/src/adapters/cursor-adapter.ts +681 -681
  37. package/src/agent-activity.ts +170 -170
  38. package/src/agent-delegate-task.ts +91 -91
  39. package/src/builtin/cli.ts +61 -2
  40. package/src/builtin/config.ts +84 -84
  41. package/src/builtin/context.ts +323 -323
  42. package/src/builtin/file-log.ts +38 -38
  43. package/src/builtin/file-tools.ts +37 -0
  44. package/src/builtin/index.ts +44 -24
  45. package/src/builtin/proc-tree-kill.ts +61 -61
  46. package/src/builtin/progress/cards-helpers.ts +76 -76
  47. package/src/builtin/progress/reducer.ts +108 -108
  48. package/src/builtin/progress/terminal-renderer.ts +294 -294
  49. package/src/builtin/progress/view.ts +77 -77
  50. package/src/builtin/raw-stream-log.ts +124 -124
  51. package/src/builtin/session-select.ts +48 -48
  52. package/src/builtin/skills.ts +190 -108
  53. package/src/builtin/web-tools.ts +313 -0
  54. package/src/card-action-routing.ts +14 -14
  55. package/src/feishu-api.ts +193 -193
  56. package/src/feishu-message-ingress.ts +195 -195
  57. package/src/index.ts +306 -306
  58. package/src/orchestrator.ts +2388 -2388
  59. package/src/platform-adapter.ts +6 -6
  60. package/src/progress/reducer.ts +108 -108
  61. package/src/progress/terminal-renderer.ts +294 -294
  62. package/src/progress/view.ts +77 -77
  63. package/src/response-stall.ts +28 -28
  64. package/src/session-chat-binding.ts +82 -82
  65. package/src/startup-lifecycle.ts +250 -250
  66. package/src/stream-state.ts +18 -18
  67. package/src/update-command-guard.ts +165 -165
@@ -1,165 +1,165 @@
1
- import {
2
- existsSync,
3
- mkdirSync,
4
- readFileSync,
5
- renameSync,
6
- rmSync,
7
- writeFileSync,
8
- } from "node:fs";
9
- import { homedir } from "node:os";
10
- import { dirname, join } from "node:path";
11
-
12
- export const UPDATE_COMMAND_GUARD_FILE = join(
13
- homedir(),
14
- ".chatccc",
15
- "state",
16
- "update-command-guard.json",
17
- );
18
-
19
- const UPDATE_COMMAND_GUARD_VERSION = 1;
20
- const DEFAULT_MAX_PROCESSED_IDS = 100;
21
-
22
- interface ProcessedUpdateCommand {
23
- id: string;
24
- recordedAt: number;
25
- }
26
-
27
- interface UpdateCommandGuardState {
28
- version: 1;
29
- processed: ProcessedUpdateCommand[];
30
- }
31
-
32
- export type UpdateCommandGuardResult =
33
- | { allowed: true; reason: "accepted" | "missing_id" }
34
- | { allowed: false; reason: "duplicate_id" | "state_write_failed" };
35
-
36
- export interface AcquireUpdateCommandGuardOptions {
37
- filePath?: string;
38
- commandId?: string;
39
- now?: number;
40
- maxEntries?: number;
41
- warn?: (message: string) => void;
42
- }
43
-
44
- function asRecord(value: unknown): Record<string, unknown> | undefined {
45
- return typeof value === "object" && value !== null
46
- ? value as Record<string, unknown>
47
- : undefined;
48
- }
49
-
50
- /** 从飞书事件信封中读取重投时保持不变的 event_id。 */
51
- export function extractFeishuEventId(data: unknown): string | undefined {
52
- const envelope = asRecord(data);
53
- const event = asRecord(envelope?.event);
54
- const header = asRecord(envelope?.header) ?? asRecord(event?.header);
55
- const context = asRecord(event?.context);
56
- const candidates = [header?.event_id, envelope?.event_id, event?.event_id, context?.event_id];
57
- for (const candidate of candidates) {
58
- if (typeof candidate === "string" && candidate.trim()) return candidate.trim();
59
- }
60
- return undefined;
61
- }
62
-
63
- /** 分隔文字消息和卡片回调 ID 的命名空间。 */
64
- export function buildUpdateCommandId(
65
- source: "message" | "card",
66
- id: string | undefined,
67
- ): string | undefined {
68
- const normalized = id?.trim();
69
- return normalized ? `${source}:${normalized}` : undefined;
70
- }
71
-
72
- function emptyState(): UpdateCommandGuardState {
73
- return { version: UPDATE_COMMAND_GUARD_VERSION, processed: [] };
74
- }
75
-
76
- function parseState(raw: string, maxEntries: number): UpdateCommandGuardState {
77
- const parsed = JSON.parse(raw) as Partial<UpdateCommandGuardState>;
78
- if (parsed.version !== UPDATE_COMMAND_GUARD_VERSION || !Array.isArray(parsed.processed)) {
79
- throw new Error("invalid update command guard schema");
80
- }
81
-
82
- const processed = parsed.processed.map((entry) => {
83
- if (
84
- typeof entry !== "object"
85
- || entry === null
86
- || typeof entry.id !== "string"
87
- || entry.id.length === 0
88
- || typeof entry.recordedAt !== "number"
89
- || !Number.isFinite(entry.recordedAt)
90
- || entry.recordedAt < 0
91
- ) {
92
- throw new Error("invalid processed update command entry");
93
- }
94
- return { id: entry.id, recordedAt: entry.recordedAt };
95
- });
96
-
97
- return {
98
- version: UPDATE_COMMAND_GUARD_VERSION,
99
- processed: processed.slice(-maxEntries),
100
- };
101
- }
102
-
103
- function loadState(
104
- filePath: string,
105
- maxEntries: number,
106
- warn: (message: string) => void,
107
- ): UpdateCommandGuardState {
108
- if (!existsSync(filePath)) return emptyState();
109
- try {
110
- return parseState(readFileSync(filePath, "utf8"), maxEntries);
111
- } catch (err) {
112
- warn(`[UPDATE-GUARD] 状态文件损坏,将重建 ${filePath}: ${(err as Error).message}`);
113
- return emptyState();
114
- }
115
- }
116
-
117
- /**
118
- * 原子写入更新命令 ID。写失败时调用方必须拒绝更新:只有先落盘,
119
- * 新进程才能识别飞书在旧进程退出后重投的同一条 `/update`。
120
- */
121
- function persistState(filePath: string, state: UpdateCommandGuardState): void {
122
- mkdirSync(dirname(filePath), { recursive: true });
123
- const tempPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
124
- try {
125
- writeFileSync(tempPath, `${JSON.stringify(state, null, 2)}\n`, "utf8");
126
- renameSync(tempPath, filePath);
127
- } catch (err) {
128
- try { rmSync(tempPath, { force: true }); } catch {}
129
- throw err;
130
- }
131
- }
132
-
133
- /**
134
- * 获取 `/update` 执行资格。只比较稳定消息/事件 ID,因此用户主动发送的
135
- * 不同 `/update` 消息仍可立即执行。
136
- */
137
- export function acquireUpdateCommandGuard(
138
- options: AcquireUpdateCommandGuardOptions = {},
139
- ): UpdateCommandGuardResult {
140
- const filePath = options.filePath ?? UPDATE_COMMAND_GUARD_FILE;
141
- const commandId = options.commandId?.trim() || undefined;
142
- const now = options.now ?? Date.now();
143
- const maxEntries = Number.isInteger(options.maxEntries) && (options.maxEntries ?? 0) > 0
144
- ? options.maxEntries!
145
- : DEFAULT_MAX_PROCESSED_IDS;
146
- const warn = options.warn ?? ((message: string) => console.warn(message));
147
-
148
- // 模拟注入等没有稳定事件 ID 的入口无法做跨重启判断,保持原有行为。
149
- if (!commandId) return { allowed: true, reason: "missing_id" };
150
-
151
- const state = loadState(filePath, maxEntries, warn);
152
- if (state.processed.some((entry) => entry.id === commandId)) {
153
- return { allowed: false, reason: "duplicate_id" };
154
- }
155
-
156
- state.processed.push({ id: commandId, recordedAt: now });
157
- state.processed = state.processed.slice(-maxEntries);
158
- try {
159
- persistState(filePath, state);
160
- } catch (err) {
161
- warn(`[UPDATE-GUARD] 无法写入状态文件 ${filePath}: ${(err as Error).message}`);
162
- return { allowed: false, reason: "state_write_failed" };
163
- }
164
- return { allowed: true, reason: "accepted" };
165
- }
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readFileSync,
5
+ renameSync,
6
+ rmSync,
7
+ writeFileSync,
8
+ } from "node:fs";
9
+ import { homedir } from "node:os";
10
+ import { dirname, join } from "node:path";
11
+
12
+ export const UPDATE_COMMAND_GUARD_FILE = join(
13
+ homedir(),
14
+ ".chatccc",
15
+ "state",
16
+ "update-command-guard.json",
17
+ );
18
+
19
+ const UPDATE_COMMAND_GUARD_VERSION = 1;
20
+ const DEFAULT_MAX_PROCESSED_IDS = 100;
21
+
22
+ interface ProcessedUpdateCommand {
23
+ id: string;
24
+ recordedAt: number;
25
+ }
26
+
27
+ interface UpdateCommandGuardState {
28
+ version: 1;
29
+ processed: ProcessedUpdateCommand[];
30
+ }
31
+
32
+ export type UpdateCommandGuardResult =
33
+ | { allowed: true; reason: "accepted" | "missing_id" }
34
+ | { allowed: false; reason: "duplicate_id" | "state_write_failed" };
35
+
36
+ export interface AcquireUpdateCommandGuardOptions {
37
+ filePath?: string;
38
+ commandId?: string;
39
+ now?: number;
40
+ maxEntries?: number;
41
+ warn?: (message: string) => void;
42
+ }
43
+
44
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
45
+ return typeof value === "object" && value !== null
46
+ ? value as Record<string, unknown>
47
+ : undefined;
48
+ }
49
+
50
+ /** 从飞书事件信封中读取重投时保持不变的 event_id。 */
51
+ export function extractFeishuEventId(data: unknown): string | undefined {
52
+ const envelope = asRecord(data);
53
+ const event = asRecord(envelope?.event);
54
+ const header = asRecord(envelope?.header) ?? asRecord(event?.header);
55
+ const context = asRecord(event?.context);
56
+ const candidates = [header?.event_id, envelope?.event_id, event?.event_id, context?.event_id];
57
+ for (const candidate of candidates) {
58
+ if (typeof candidate === "string" && candidate.trim()) return candidate.trim();
59
+ }
60
+ return undefined;
61
+ }
62
+
63
+ /** 分隔文字消息和卡片回调 ID 的命名空间。 */
64
+ export function buildUpdateCommandId(
65
+ source: "message" | "card",
66
+ id: string | undefined,
67
+ ): string | undefined {
68
+ const normalized = id?.trim();
69
+ return normalized ? `${source}:${normalized}` : undefined;
70
+ }
71
+
72
+ function emptyState(): UpdateCommandGuardState {
73
+ return { version: UPDATE_COMMAND_GUARD_VERSION, processed: [] };
74
+ }
75
+
76
+ function parseState(raw: string, maxEntries: number): UpdateCommandGuardState {
77
+ const parsed = JSON.parse(raw) as Partial<UpdateCommandGuardState>;
78
+ if (parsed.version !== UPDATE_COMMAND_GUARD_VERSION || !Array.isArray(parsed.processed)) {
79
+ throw new Error("invalid update command guard schema");
80
+ }
81
+
82
+ const processed = parsed.processed.map((entry) => {
83
+ if (
84
+ typeof entry !== "object"
85
+ || entry === null
86
+ || typeof entry.id !== "string"
87
+ || entry.id.length === 0
88
+ || typeof entry.recordedAt !== "number"
89
+ || !Number.isFinite(entry.recordedAt)
90
+ || entry.recordedAt < 0
91
+ ) {
92
+ throw new Error("invalid processed update command entry");
93
+ }
94
+ return { id: entry.id, recordedAt: entry.recordedAt };
95
+ });
96
+
97
+ return {
98
+ version: UPDATE_COMMAND_GUARD_VERSION,
99
+ processed: processed.slice(-maxEntries),
100
+ };
101
+ }
102
+
103
+ function loadState(
104
+ filePath: string,
105
+ maxEntries: number,
106
+ warn: (message: string) => void,
107
+ ): UpdateCommandGuardState {
108
+ if (!existsSync(filePath)) return emptyState();
109
+ try {
110
+ return parseState(readFileSync(filePath, "utf8"), maxEntries);
111
+ } catch (err) {
112
+ warn(`[UPDATE-GUARD] 状态文件损坏,将重建 ${filePath}: ${(err as Error).message}`);
113
+ return emptyState();
114
+ }
115
+ }
116
+
117
+ /**
118
+ * 原子写入更新命令 ID。写失败时调用方必须拒绝更新:只有先落盘,
119
+ * 新进程才能识别飞书在旧进程退出后重投的同一条 `/update`。
120
+ */
121
+ function persistState(filePath: string, state: UpdateCommandGuardState): void {
122
+ mkdirSync(dirname(filePath), { recursive: true });
123
+ const tempPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
124
+ try {
125
+ writeFileSync(tempPath, `${JSON.stringify(state, null, 2)}\n`, "utf8");
126
+ renameSync(tempPath, filePath);
127
+ } catch (err) {
128
+ try { rmSync(tempPath, { force: true }); } catch {}
129
+ throw err;
130
+ }
131
+ }
132
+
133
+ /**
134
+ * 获取 `/update` 执行资格。只比较稳定消息/事件 ID,因此用户主动发送的
135
+ * 不同 `/update` 消息仍可立即执行。
136
+ */
137
+ export function acquireUpdateCommandGuard(
138
+ options: AcquireUpdateCommandGuardOptions = {},
139
+ ): UpdateCommandGuardResult {
140
+ const filePath = options.filePath ?? UPDATE_COMMAND_GUARD_FILE;
141
+ const commandId = options.commandId?.trim() || undefined;
142
+ const now = options.now ?? Date.now();
143
+ const maxEntries = Number.isInteger(options.maxEntries) && (options.maxEntries ?? 0) > 0
144
+ ? options.maxEntries!
145
+ : DEFAULT_MAX_PROCESSED_IDS;
146
+ const warn = options.warn ?? ((message: string) => console.warn(message));
147
+
148
+ // 模拟注入等没有稳定事件 ID 的入口无法做跨重启判断,保持原有行为。
149
+ if (!commandId) return { allowed: true, reason: "missing_id" };
150
+
151
+ const state = loadState(filePath, maxEntries, warn);
152
+ if (state.processed.some((entry) => entry.id === commandId)) {
153
+ return { allowed: false, reason: "duplicate_id" };
154
+ }
155
+
156
+ state.processed.push({ id: commandId, recordedAt: now });
157
+ state.processed = state.processed.slice(-maxEntries);
158
+ try {
159
+ persistState(filePath, state);
160
+ } catch (err) {
161
+ warn(`[UPDATE-GUARD] 无法写入状态文件 ${filePath}: ${(err as Error).message}`);
162
+ return { allowed: false, reason: "state_write_failed" };
163
+ }
164
+ return { allowed: true, reason: "accepted" };
165
+ }