chatccc 0.2.225 → 0.2.226
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/.agents/skills/create-chatccc-feishu-app/SKILL.md +85 -85
- package/.claude/skills/create-chatccc-feishu-app/SKILL.md +85 -85
- package/.cursor/skills/create-chatccc-feishu-app/SKILL.md +85 -85
- package/README.md +90 -90
- package/package.json +1 -1
- package/src/__tests__/agent-activity.test.ts +76 -76
- package/src/__tests__/builtin-chat-session.test.ts +350 -350
- package/src/__tests__/builtin-config.test.ts +26 -37
- package/src/__tests__/builtin-context.test.ts +163 -163
- package/src/__tests__/builtin-file-tools.test.ts +275 -275
- package/src/__tests__/builtin-permissions.test.ts +211 -0
- package/src/__tests__/builtin-session-select.test.ts +116 -116
- package/src/__tests__/builtin-skills.test.ts +141 -141
- package/src/__tests__/card-action-routing.test.ts +18 -18
- package/src/__tests__/ccc-adapter.test.ts +136 -136
- package/src/__tests__/claude-adapter.test.ts +614 -614
- package/src/__tests__/codex-adapter.test.ts +58 -58
- package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
- package/src/__tests__/cursor-adapter.test.ts +268 -268
- package/src/__tests__/feishu-avatar.test.ts +164 -164
- package/src/__tests__/feishu-message-ingress.test.ts +138 -138
- package/src/__tests__/package-files.test.ts +24 -24
- package/src/__tests__/progress-reducer.test.ts +110 -110
- package/src/__tests__/response-stall.test.ts +49 -49
- package/src/__tests__/sim-platform.test.ts +16 -16
- package/src/__tests__/startup-lifecycle.test.ts +231 -231
- package/src/__tests__/stop-session.test.ts +34 -34
- package/src/__tests__/terminal-renderer.test.ts +247 -247
- package/src/__tests__/update-command-guard.test.ts +144 -144
- package/src/__tests__/web-ui.test.ts +326 -326
- package/src/adapters/adapter-interface.ts +18 -18
- package/src/adapters/ccc-adapter.ts +131 -128
- package/src/adapters/claude-adapter.ts +620 -620
- package/src/adapters/codex-adapter.ts +426 -426
- package/src/adapters/cursor-adapter.ts +681 -681
- package/src/agent-activity.ts +170 -170
- package/src/agent-delegate-task.ts +91 -91
- package/src/builtin/cli.ts +598 -556
- package/src/builtin/config.ts +84 -0
- package/src/builtin/context.ts +323 -323
- package/src/builtin/file-log.ts +38 -0
- package/src/builtin/file-tools.ts +1407 -1320
- package/src/builtin/index.ts +437 -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 +48 -48
- package/src/builtin/skills.ts +108 -108
- package/src/card-action-routing.ts +14 -14
- package/src/feishu-api.ts +193 -193
- package/src/feishu-message-ingress.ts +195 -195
- package/src/index.ts +306 -306
- package/src/orchestrator.ts +2388 -2388
- package/src/platform-adapter.ts +6 -6
- package/src/progress/reducer.ts +108 -108
- package/src/progress/terminal-renderer.ts +294 -294
- package/src/progress/view.ts +77 -77
- package/src/response-stall.ts +28 -28
- package/src/session-chat-binding.ts +82 -82
- package/src/startup-lifecycle.ts +250 -250
- package/src/stream-state.ts +18 -18
- package/src/update-command-guard.ts +165 -165
|
@@ -1,195 +1,195 @@
|
|
|
1
|
-
import { homedir } from "node:os";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
|
-
|
|
5
|
-
export const MAX_PROCESSED = 5000;
|
|
6
|
-
|
|
7
|
-
export type FeishuMessageDisposition = "accepted" | "duplicate" | "stale";
|
|
8
|
-
|
|
9
|
-
export interface FeishuMessageIdentity {
|
|
10
|
-
messageId?: string;
|
|
11
|
-
chatId: string;
|
|
12
|
-
createTime: number;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
interface PersistedMessageEntry {
|
|
16
|
-
messageId: string;
|
|
17
|
-
chatId: string;
|
|
18
|
-
createTime: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
interface PersistedMessageLedger {
|
|
22
|
-
version: 1;
|
|
23
|
-
entries: PersistedMessageEntry[];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
type ScheduleTask = (task: () => void) => void;
|
|
27
|
-
|
|
28
|
-
const defaultSchedule: ScheduleTask = (task) => {
|
|
29
|
-
setImmediate(task);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The Feishu SDK waits for an event handler's return value before sending its
|
|
34
|
-
* WebSocket response. Schedule the real work for the next event-loop turn so
|
|
35
|
-
* the SDK callback can return and acknowledge the event immediately.
|
|
36
|
-
*/
|
|
37
|
-
export function createAckFirstEventHandler<T>(
|
|
38
|
-
worker: (data: T) => Promise<void>,
|
|
39
|
-
onError: (error: unknown) => void,
|
|
40
|
-
schedule: ScheduleTask = defaultSchedule,
|
|
41
|
-
): (data: T) => Promise<void> {
|
|
42
|
-
return async (data) => {
|
|
43
|
-
schedule(() => {
|
|
44
|
-
void worker(data).catch(onError);
|
|
45
|
-
});
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function isPersistedEntry(value: unknown): value is PersistedMessageEntry {
|
|
50
|
-
if (!value || typeof value !== "object") return false;
|
|
51
|
-
const entry = value as Record<string, unknown>;
|
|
52
|
-
return typeof entry.messageId === "string"
|
|
53
|
-
&& entry.messageId.length > 0
|
|
54
|
-
&& typeof entry.chatId === "string"
|
|
55
|
-
&& typeof entry.createTime === "number"
|
|
56
|
-
&& Number.isFinite(entry.createTime);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export class FeishuMessageLedger {
|
|
60
|
-
private readonly messageIds: Set<string>;
|
|
61
|
-
private entries: PersistedMessageEntry[] = [];
|
|
62
|
-
private latestCreateTimeByChat = new Map<string, number>();
|
|
63
|
-
private persistTail: Promise<void> = Promise.resolve();
|
|
64
|
-
|
|
65
|
-
constructor(
|
|
66
|
-
public readonly filePath: string,
|
|
67
|
-
private readonly maxEntries = MAX_PROCESSED,
|
|
68
|
-
messageIds?: Set<string>,
|
|
69
|
-
) {
|
|
70
|
-
this.messageIds = messageIds ?? new Set<string>();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async load(): Promise<void> {
|
|
74
|
-
this.clearMemory();
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
const raw = await readFile(this.filePath, "utf-8");
|
|
78
|
-
const parsed = JSON.parse(raw) as Partial<PersistedMessageLedger>;
|
|
79
|
-
const entries = Array.isArray(parsed.entries)
|
|
80
|
-
? parsed.entries.filter(isPersistedEntry)
|
|
81
|
-
: [];
|
|
82
|
-
this.entries = entries.slice(-this.maxEntries);
|
|
83
|
-
this.rebuildIndexes();
|
|
84
|
-
|
|
85
|
-
if (entries.length > this.maxEntries) {
|
|
86
|
-
await this.persist();
|
|
87
|
-
}
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
90
|
-
console.error(
|
|
91
|
-
`[FEISHU-DEDUP] Failed to load ${this.filePath}: ${(error as Error).message}`,
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
async accept(identity: FeishuMessageIdentity): Promise<FeishuMessageDisposition> {
|
|
98
|
-
const { messageId, chatId, createTime } = identity;
|
|
99
|
-
|
|
100
|
-
if (messageId && this.messageIds.has(messageId)) {
|
|
101
|
-
return "duplicate";
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const latestCreateTime = this.latestCreateTimeByChat.get(chatId);
|
|
105
|
-
if (latestCreateTime !== undefined && createTime < latestCreateTime) {
|
|
106
|
-
return "stale";
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (!messageId) {
|
|
110
|
-
this.recordLatestCreateTime(chatId, createTime);
|
|
111
|
-
return "accepted";
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this.entries.push({ messageId, chatId, createTime });
|
|
115
|
-
this.messageIds.add(messageId);
|
|
116
|
-
this.recordLatestCreateTime(chatId, createTime);
|
|
117
|
-
|
|
118
|
-
if (this.entries.length > this.maxEntries) {
|
|
119
|
-
this.entries = this.entries.slice(-this.maxEntries);
|
|
120
|
-
this.rebuildIndexes();
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
try {
|
|
124
|
-
await this.persist();
|
|
125
|
-
} catch (error) {
|
|
126
|
-
console.error(
|
|
127
|
-
`[FEISHU-DEDUP] Failed to persist ${this.filePath}: ${(error as Error).message}`,
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
return "accepted";
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
clearMemory(): void {
|
|
134
|
-
this.entries = [];
|
|
135
|
-
this.messageIds.clear();
|
|
136
|
-
this.latestCreateTimeByChat.clear();
|
|
137
|
-
this.persistTail = Promise.resolve();
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
private recordLatestCreateTime(chatId: string, createTime: number): void {
|
|
141
|
-
const current = this.latestCreateTimeByChat.get(chatId);
|
|
142
|
-
if (current === undefined || createTime > current) {
|
|
143
|
-
this.latestCreateTimeByChat.set(chatId, createTime);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
private rebuildIndexes(): void {
|
|
148
|
-
this.messageIds.clear();
|
|
149
|
-
this.latestCreateTimeByChat.clear();
|
|
150
|
-
for (const entry of this.entries) {
|
|
151
|
-
this.messageIds.add(entry.messageId);
|
|
152
|
-
this.recordLatestCreateTime(entry.chatId, entry.createTime);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private persist(): Promise<void> {
|
|
157
|
-
const snapshot: PersistedMessageLedger = {
|
|
158
|
-
version: 1,
|
|
159
|
-
entries: this.entries.map((entry) => ({ ...entry })),
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
const nextPersist = this.persistTail
|
|
163
|
-
.catch(() => {})
|
|
164
|
-
.then(async () => {
|
|
165
|
-
await mkdir(dirname(this.filePath), { recursive: true });
|
|
166
|
-
const tempPath = `${this.filePath}.${process.pid}.tmp`;
|
|
167
|
-
try {
|
|
168
|
-
await writeFile(tempPath, JSON.stringify(snapshot), "utf-8");
|
|
169
|
-
await rename(tempPath, this.filePath);
|
|
170
|
-
} finally {
|
|
171
|
-
await rm(tempPath, { force: true }).catch(() => {});
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
this.persistTail = nextPersist;
|
|
175
|
-
return nextPersist;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const defaultLedgerPath = join(
|
|
180
|
-
homedir(),
|
|
181
|
-
".chatccc",
|
|
182
|
-
"state",
|
|
183
|
-
"feishu-message-ledger.json",
|
|
184
|
-
);
|
|
185
|
-
|
|
186
|
-
export const processedMessages = new Set<string>();
|
|
187
|
-
export const feishuMessageLedger = new FeishuMessageLedger(
|
|
188
|
-
defaultLedgerPath,
|
|
189
|
-
MAX_PROCESSED,
|
|
190
|
-
processedMessages,
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
export function clearFeishuMessageLedgerMemory(): void {
|
|
194
|
-
feishuMessageLedger.clearMemory();
|
|
195
|
-
}
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
export const MAX_PROCESSED = 5000;
|
|
6
|
+
|
|
7
|
+
export type FeishuMessageDisposition = "accepted" | "duplicate" | "stale";
|
|
8
|
+
|
|
9
|
+
export interface FeishuMessageIdentity {
|
|
10
|
+
messageId?: string;
|
|
11
|
+
chatId: string;
|
|
12
|
+
createTime: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface PersistedMessageEntry {
|
|
16
|
+
messageId: string;
|
|
17
|
+
chatId: string;
|
|
18
|
+
createTime: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface PersistedMessageLedger {
|
|
22
|
+
version: 1;
|
|
23
|
+
entries: PersistedMessageEntry[];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type ScheduleTask = (task: () => void) => void;
|
|
27
|
+
|
|
28
|
+
const defaultSchedule: ScheduleTask = (task) => {
|
|
29
|
+
setImmediate(task);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The Feishu SDK waits for an event handler's return value before sending its
|
|
34
|
+
* WebSocket response. Schedule the real work for the next event-loop turn so
|
|
35
|
+
* the SDK callback can return and acknowledge the event immediately.
|
|
36
|
+
*/
|
|
37
|
+
export function createAckFirstEventHandler<T>(
|
|
38
|
+
worker: (data: T) => Promise<void>,
|
|
39
|
+
onError: (error: unknown) => void,
|
|
40
|
+
schedule: ScheduleTask = defaultSchedule,
|
|
41
|
+
): (data: T) => Promise<void> {
|
|
42
|
+
return async (data) => {
|
|
43
|
+
schedule(() => {
|
|
44
|
+
void worker(data).catch(onError);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isPersistedEntry(value: unknown): value is PersistedMessageEntry {
|
|
50
|
+
if (!value || typeof value !== "object") return false;
|
|
51
|
+
const entry = value as Record<string, unknown>;
|
|
52
|
+
return typeof entry.messageId === "string"
|
|
53
|
+
&& entry.messageId.length > 0
|
|
54
|
+
&& typeof entry.chatId === "string"
|
|
55
|
+
&& typeof entry.createTime === "number"
|
|
56
|
+
&& Number.isFinite(entry.createTime);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class FeishuMessageLedger {
|
|
60
|
+
private readonly messageIds: Set<string>;
|
|
61
|
+
private entries: PersistedMessageEntry[] = [];
|
|
62
|
+
private latestCreateTimeByChat = new Map<string, number>();
|
|
63
|
+
private persistTail: Promise<void> = Promise.resolve();
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
public readonly filePath: string,
|
|
67
|
+
private readonly maxEntries = MAX_PROCESSED,
|
|
68
|
+
messageIds?: Set<string>,
|
|
69
|
+
) {
|
|
70
|
+
this.messageIds = messageIds ?? new Set<string>();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async load(): Promise<void> {
|
|
74
|
+
this.clearMemory();
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const raw = await readFile(this.filePath, "utf-8");
|
|
78
|
+
const parsed = JSON.parse(raw) as Partial<PersistedMessageLedger>;
|
|
79
|
+
const entries = Array.isArray(parsed.entries)
|
|
80
|
+
? parsed.entries.filter(isPersistedEntry)
|
|
81
|
+
: [];
|
|
82
|
+
this.entries = entries.slice(-this.maxEntries);
|
|
83
|
+
this.rebuildIndexes();
|
|
84
|
+
|
|
85
|
+
if (entries.length > this.maxEntries) {
|
|
86
|
+
await this.persist();
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
|
90
|
+
console.error(
|
|
91
|
+
`[FEISHU-DEDUP] Failed to load ${this.filePath}: ${(error as Error).message}`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async accept(identity: FeishuMessageIdentity): Promise<FeishuMessageDisposition> {
|
|
98
|
+
const { messageId, chatId, createTime } = identity;
|
|
99
|
+
|
|
100
|
+
if (messageId && this.messageIds.has(messageId)) {
|
|
101
|
+
return "duplicate";
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const latestCreateTime = this.latestCreateTimeByChat.get(chatId);
|
|
105
|
+
if (latestCreateTime !== undefined && createTime < latestCreateTime) {
|
|
106
|
+
return "stale";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!messageId) {
|
|
110
|
+
this.recordLatestCreateTime(chatId, createTime);
|
|
111
|
+
return "accepted";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.entries.push({ messageId, chatId, createTime });
|
|
115
|
+
this.messageIds.add(messageId);
|
|
116
|
+
this.recordLatestCreateTime(chatId, createTime);
|
|
117
|
+
|
|
118
|
+
if (this.entries.length > this.maxEntries) {
|
|
119
|
+
this.entries = this.entries.slice(-this.maxEntries);
|
|
120
|
+
this.rebuildIndexes();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
await this.persist();
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error(
|
|
127
|
+
`[FEISHU-DEDUP] Failed to persist ${this.filePath}: ${(error as Error).message}`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return "accepted";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
clearMemory(): void {
|
|
134
|
+
this.entries = [];
|
|
135
|
+
this.messageIds.clear();
|
|
136
|
+
this.latestCreateTimeByChat.clear();
|
|
137
|
+
this.persistTail = Promise.resolve();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private recordLatestCreateTime(chatId: string, createTime: number): void {
|
|
141
|
+
const current = this.latestCreateTimeByChat.get(chatId);
|
|
142
|
+
if (current === undefined || createTime > current) {
|
|
143
|
+
this.latestCreateTimeByChat.set(chatId, createTime);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private rebuildIndexes(): void {
|
|
148
|
+
this.messageIds.clear();
|
|
149
|
+
this.latestCreateTimeByChat.clear();
|
|
150
|
+
for (const entry of this.entries) {
|
|
151
|
+
this.messageIds.add(entry.messageId);
|
|
152
|
+
this.recordLatestCreateTime(entry.chatId, entry.createTime);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private persist(): Promise<void> {
|
|
157
|
+
const snapshot: PersistedMessageLedger = {
|
|
158
|
+
version: 1,
|
|
159
|
+
entries: this.entries.map((entry) => ({ ...entry })),
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const nextPersist = this.persistTail
|
|
163
|
+
.catch(() => {})
|
|
164
|
+
.then(async () => {
|
|
165
|
+
await mkdir(dirname(this.filePath), { recursive: true });
|
|
166
|
+
const tempPath = `${this.filePath}.${process.pid}.tmp`;
|
|
167
|
+
try {
|
|
168
|
+
await writeFile(tempPath, JSON.stringify(snapshot), "utf-8");
|
|
169
|
+
await rename(tempPath, this.filePath);
|
|
170
|
+
} finally {
|
|
171
|
+
await rm(tempPath, { force: true }).catch(() => {});
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
this.persistTail = nextPersist;
|
|
175
|
+
return nextPersist;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const defaultLedgerPath = join(
|
|
180
|
+
homedir(),
|
|
181
|
+
".chatccc",
|
|
182
|
+
"state",
|
|
183
|
+
"feishu-message-ledger.json",
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
export const processedMessages = new Set<string>();
|
|
187
|
+
export const feishuMessageLedger = new FeishuMessageLedger(
|
|
188
|
+
defaultLedgerPath,
|
|
189
|
+
MAX_PROCESSED,
|
|
190
|
+
processedMessages,
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
export function clearFeishuMessageLedgerMemory(): void {
|
|
194
|
+
feishuMessageLedger.clearMemory();
|
|
195
|
+
}
|