chatccc 0.2.184 → 0.2.186
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 +88 -69
- package/agent-prompts/cursor_specific.md +12 -1
- package/config.sample.json +29 -4
- package/im-skills/feishu-skill/skill.md +3 -3
- package/package.json +1 -1
- package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -0
- package/src/__tests__/chatgpt-subscription.test.ts +135 -0
- package/src/__tests__/chrome-devtools-guard.test.ts +125 -0
- package/src/__tests__/config-reload.test.ts +22 -4
- package/src/__tests__/config-sample.test.ts +31 -1
- package/src/__tests__/orchestrator.test.ts +162 -117
- package/src/__tests__/raw-stream-log.test.ts +106 -0
- package/src/__tests__/web-ui.test.ts +16 -0
- package/src/adapters/cursor-adapter.ts +46 -21
- package/src/adapters/raw-stream-log.ts +124 -0
- package/src/agent-delegate-task-rpc.ts +153 -153
- package/src/agent-delegate-task.ts +81 -81
- package/src/chatgpt-subscription-rpc.ts +27 -0
- package/src/chatgpt-subscription.ts +299 -0
- package/src/chrome-devtools-guard.ts +242 -0
- package/src/config.ts +99 -32
- package/src/index.ts +12 -4
- package/src/orchestrator.ts +41 -4
- package/src/session-name.ts +8 -8
- package/src/session.ts +6 -6
- package/src/web-ui.ts +136 -6
|
@@ -9,6 +9,7 @@ import type { SessionInfo, ToolAdapter } from "../adapters/adapter-interface.ts"
|
|
|
9
9
|
const mockStreamStates = new Map<string, { status: "running" | "done" | "stopped"; finalReply: string }>();
|
|
10
10
|
const mockGetCodexUsageSummary = vi.hoisted(() => vi.fn());
|
|
11
11
|
const mockGetCursorUsageSummary = vi.hoisted(() => vi.fn());
|
|
12
|
+
const mockGetChatGptSubscriptionStatus = vi.hoisted(() => vi.fn());
|
|
12
13
|
|
|
13
14
|
vi.mock("../im-skills.ts", () => ({
|
|
14
15
|
buildImSkillsPrompt: async () => "",
|
|
@@ -64,9 +65,13 @@ vi.mock("../cursor-usage.ts", () => ({
|
|
|
64
65
|
getCursorUsageSummary: mockGetCursorUsageSummary,
|
|
65
66
|
}));
|
|
66
67
|
|
|
68
|
+
vi.mock("../chatgpt-subscription.ts", () => ({
|
|
69
|
+
getChatGptSubscriptionStatus: mockGetChatGptSubscriptionStatus,
|
|
70
|
+
}));
|
|
71
|
+
|
|
67
72
|
import { handleCommand } from "../orchestrator.ts";
|
|
68
|
-
import {
|
|
69
|
-
_clearAdapterCacheForTest,
|
|
73
|
+
import {
|
|
74
|
+
_clearAdapterCacheForTest,
|
|
70
75
|
_resetSessionRegistryFileForTest,
|
|
71
76
|
_resetSessionToolsFileForTest,
|
|
72
77
|
_setAdapterForToolForTest,
|
|
@@ -74,10 +79,10 @@ import {
|
|
|
74
79
|
_setSessionToolsFileForTest,
|
|
75
80
|
loadSessionRegistryForBinding,
|
|
76
81
|
recordSessionRegistry,
|
|
77
|
-
resetState,
|
|
78
|
-
} from "../session.ts";
|
|
79
|
-
import { activePrompts, resetBindingState } from "../session-chat-binding.ts";
|
|
80
|
-
import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
|
|
82
|
+
resetState,
|
|
83
|
+
} from "../session.ts";
|
|
84
|
+
import { activePrompts, resetBindingState } from "../session-chat-binding.ts";
|
|
85
|
+
import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
|
|
81
86
|
|
|
82
87
|
function mockPlatform(kind: "wechat" | "feishu" = "wechat"): PlatformAdapter {
|
|
83
88
|
return {
|
|
@@ -129,12 +134,13 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
129
134
|
mockStreamStates.clear();
|
|
130
135
|
mockGetCodexUsageSummary.mockReset();
|
|
131
136
|
mockGetCursorUsageSummary.mockReset();
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
137
|
+
mockGetChatGptSubscriptionStatus.mockReset();
|
|
138
|
+
mockGetCodexUsageSummary.mockResolvedValue({
|
|
139
|
+
fiveHour: { usedPercent: 0, remainingPercent: 100, resetAtEpochSeconds: null, resetAfterSeconds: null },
|
|
140
|
+
weekly: { usedPercent: 0, remainingPercent: 100, resetAtEpochSeconds: null, resetAfterSeconds: null },
|
|
141
|
+
rateLimitResetCreditsAvailable: null,
|
|
142
|
+
rateLimitResetCredits: null,
|
|
143
|
+
});
|
|
138
144
|
mockGetCursorUsageSummary.mockResolvedValue({
|
|
139
145
|
billingCycleStart: "1779357999000",
|
|
140
146
|
billingCycleEnd: "1782036399000",
|
|
@@ -160,6 +166,12 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
160
166
|
displayMessage: "You've hit your usage limit",
|
|
161
167
|
autoBucketModels: ["default"],
|
|
162
168
|
});
|
|
169
|
+
mockGetChatGptSubscriptionStatus.mockResolvedValue({
|
|
170
|
+
ok: false,
|
|
171
|
+
code: "chrome_cdp_disabled",
|
|
172
|
+
reason: "Chrome CDP guard is disabled in ChatCCC config.",
|
|
173
|
+
chromeCdp: { enabled: false, port: 15166, status: "skipped" },
|
|
174
|
+
});
|
|
163
175
|
_setAdapterForToolForTest("claude", mockAdapter());
|
|
164
176
|
});
|
|
165
177
|
|
|
@@ -200,8 +212,8 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
200
212
|
expect(platform.sendCard).not.toHaveBeenCalled();
|
|
201
213
|
});
|
|
202
214
|
|
|
203
|
-
it("sends the WeChat processing ack after the busy check for normal prompts", async () => {
|
|
204
|
-
const platform = mockPlatform();
|
|
215
|
+
it("sends the WeChat processing ack after the busy check for normal prompts", async () => {
|
|
216
|
+
const platform = mockPlatform();
|
|
205
217
|
await recordSessionRegistry({
|
|
206
218
|
chatId: "wx-chat",
|
|
207
219
|
sessionId: "sid-wechat",
|
|
@@ -211,44 +223,44 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
211
223
|
});
|
|
212
224
|
|
|
213
225
|
await handleCommand(platform, "继续说明", "wx-chat", "wx-user", Date.now(), "p2p");
|
|
214
|
-
|
|
215
|
-
expect(platform.sendText).toHaveBeenCalledWith("wx-chat", "生成中...");
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it("treats /abd as a shared prompt prefix in an existing session", async () => {
|
|
219
|
-
const platform = mockPlatform();
|
|
220
|
-
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
221
|
-
yield {
|
|
222
|
-
type: "assistant" as const,
|
|
223
|
-
blocks: [{ type: "text" as const, text: "done" }],
|
|
224
|
-
};
|
|
225
|
-
});
|
|
226
|
-
_setAdapterForToolForTest("claude", {
|
|
227
|
-
displayName: "Claude",
|
|
228
|
-
sessionDescPrefix: "Claude Session:",
|
|
229
|
-
createSession: vi.fn(async () => ({ sessionId: "sid-wechat" })),
|
|
230
|
-
prompt,
|
|
231
|
-
getSessionInfo: async (sessionId: string): Promise<SessionInfo> => ({
|
|
232
|
-
sessionId,
|
|
233
|
-
cwd: "F:\\repo",
|
|
234
|
-
}),
|
|
235
|
-
closeSession: async () => {},
|
|
236
|
-
});
|
|
237
|
-
await recordSessionRegistry({
|
|
238
|
-
chatId: "wx-chat",
|
|
239
|
-
sessionId: "sid-wechat",
|
|
240
|
-
tool: "claude",
|
|
241
|
-
chatName: "ready-session",
|
|
242
|
-
running: false,
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
await handleCommand(platform, "/abd帮我分析", "wx-chat", "wx-user", Date.now(), "p2p");
|
|
246
|
-
|
|
247
|
-
expect(platform.sendText).toHaveBeenCalledWith("wx-chat", "生成中...");
|
|
248
|
-
const userText = prompt.mock.calls[0][1];
|
|
249
|
-
expect(userText).toContain(`[User message]\n帮我分析\n\n---\n${ABD_APPEND_PROMPT}\n[/User message]`);
|
|
250
|
-
expect(userText).not.toContain("/abd");
|
|
251
|
-
});
|
|
226
|
+
|
|
227
|
+
expect(platform.sendText).toHaveBeenCalledWith("wx-chat", "生成中...");
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("treats /abd as a shared prompt prefix in an existing session", async () => {
|
|
231
|
+
const platform = mockPlatform();
|
|
232
|
+
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
233
|
+
yield {
|
|
234
|
+
type: "assistant" as const,
|
|
235
|
+
blocks: [{ type: "text" as const, text: "done" }],
|
|
236
|
+
};
|
|
237
|
+
});
|
|
238
|
+
_setAdapterForToolForTest("claude", {
|
|
239
|
+
displayName: "Claude",
|
|
240
|
+
sessionDescPrefix: "Claude Session:",
|
|
241
|
+
createSession: vi.fn(async () => ({ sessionId: "sid-wechat" })),
|
|
242
|
+
prompt,
|
|
243
|
+
getSessionInfo: async (sessionId: string): Promise<SessionInfo> => ({
|
|
244
|
+
sessionId,
|
|
245
|
+
cwd: "F:\\repo",
|
|
246
|
+
}),
|
|
247
|
+
closeSession: async () => {},
|
|
248
|
+
});
|
|
249
|
+
await recordSessionRegistry({
|
|
250
|
+
chatId: "wx-chat",
|
|
251
|
+
sessionId: "sid-wechat",
|
|
252
|
+
tool: "claude",
|
|
253
|
+
chatName: "ready-session",
|
|
254
|
+
running: false,
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
await handleCommand(platform, "/abd帮我分析", "wx-chat", "wx-user", Date.now(), "p2p");
|
|
258
|
+
|
|
259
|
+
expect(platform.sendText).toHaveBeenCalledWith("wx-chat", "生成中...");
|
|
260
|
+
const userText = prompt.mock.calls[0][1];
|
|
261
|
+
expect(userText).toContain(`[User message]\n帮我分析\n\n---\n${ABD_APPEND_PROMPT}\n[/User message]`);
|
|
262
|
+
expect(userText).not.toContain("/abd");
|
|
263
|
+
});
|
|
252
264
|
|
|
253
265
|
it("does not send the stopped success text until the running prompt really exits", async () => {
|
|
254
266
|
const platform = mockPlatform();
|
|
@@ -270,7 +282,7 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
270
282
|
expect(platform.sendText).not.toHaveBeenCalledWith("wx-chat", "会话已停止。");
|
|
271
283
|
});
|
|
272
284
|
|
|
273
|
-
it("cleans stale Feishu p2p binding, creates a group, and sends the private message as first prompt", async () => {
|
|
285
|
+
it("cleans stale Feishu p2p binding, creates a group, and sends the private message as first prompt", async () => {
|
|
274
286
|
const platform = mockPlatform("feishu");
|
|
275
287
|
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
276
288
|
yield {
|
|
@@ -315,40 +327,40 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
315
327
|
|
|
316
328
|
const registry = await loadSessionRegistryForBinding();
|
|
317
329
|
expect(registry["feishu-p2p"]).toBeUndefined();
|
|
318
|
-
expect(registry["feishu-group"]?.sessionId).toBe("sid-feishu-new");
|
|
319
|
-
});
|
|
320
|
-
|
|
321
|
-
it("auto-creates a Feishu group for /abd private messages and sends the transformed prompt", async () => {
|
|
322
|
-
const platform = mockPlatform("feishu");
|
|
323
|
-
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
324
|
-
yield {
|
|
325
|
-
type: "assistant" as const,
|
|
326
|
-
blocks: [{ type: "text" as const, text: "done" }],
|
|
327
|
-
};
|
|
328
|
-
});
|
|
329
|
-
_setAdapterForToolForTest("claude", {
|
|
330
|
-
displayName: "Claude",
|
|
331
|
-
sessionDescPrefix: "Claude Session:",
|
|
332
|
-
createSession: vi.fn(async () => ({ sessionId: "sid-feishu-abd" })),
|
|
333
|
-
prompt,
|
|
334
|
-
getSessionInfo: async (sessionId: string): Promise<SessionInfo> => ({
|
|
335
|
-
sessionId,
|
|
336
|
-
cwd: "F:\\repo",
|
|
337
|
-
}),
|
|
338
|
-
closeSession: async () => {},
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
await handleCommand(platform, "/abd帮我看一下日志", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
342
|
-
|
|
343
|
-
expect(platform.createGroup).toHaveBeenCalledWith(expect.stringContaining("帮我看一下日志"), ["ou-user"]);
|
|
344
|
-
expect(platform.createGroup).not.toHaveBeenCalledWith(expect.stringContaining("---"), expect.anything());
|
|
345
|
-
const updateCall = vi.mocked(platform.updateChatInfo).mock.calls[0];
|
|
346
|
-
expect(updateCall[1]).not.toContain("---");
|
|
347
|
-
expect(updateCall[1]).not.toContain(ABD_APPEND_PROMPT);
|
|
348
|
-
const userText = prompt.mock.calls[0][1];
|
|
349
|
-
expect(userText).toContain(`[User message]\n帮我看一下日志\n\n---\n${ABD_APPEND_PROMPT}\n[/User message]`);
|
|
350
|
-
expect(userText).not.toContain("/abd");
|
|
351
|
-
});
|
|
330
|
+
expect(registry["feishu-group"]?.sessionId).toBe("sid-feishu-new");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("auto-creates a Feishu group for /abd private messages and sends the transformed prompt", async () => {
|
|
334
|
+
const platform = mockPlatform("feishu");
|
|
335
|
+
const prompt = vi.fn(async function* (_sessionId: string, userText: string) {
|
|
336
|
+
yield {
|
|
337
|
+
type: "assistant" as const,
|
|
338
|
+
blocks: [{ type: "text" as const, text: "done" }],
|
|
339
|
+
};
|
|
340
|
+
});
|
|
341
|
+
_setAdapterForToolForTest("claude", {
|
|
342
|
+
displayName: "Claude",
|
|
343
|
+
sessionDescPrefix: "Claude Session:",
|
|
344
|
+
createSession: vi.fn(async () => ({ sessionId: "sid-feishu-abd" })),
|
|
345
|
+
prompt,
|
|
346
|
+
getSessionInfo: async (sessionId: string): Promise<SessionInfo> => ({
|
|
347
|
+
sessionId,
|
|
348
|
+
cwd: "F:\\repo",
|
|
349
|
+
}),
|
|
350
|
+
closeSession: async () => {},
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
await handleCommand(platform, "/abd帮我看一下日志", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
354
|
+
|
|
355
|
+
expect(platform.createGroup).toHaveBeenCalledWith(expect.stringContaining("帮我看一下日志"), ["ou-user"]);
|
|
356
|
+
expect(platform.createGroup).not.toHaveBeenCalledWith(expect.stringContaining("---"), expect.anything());
|
|
357
|
+
const updateCall = vi.mocked(platform.updateChatInfo).mock.calls[0];
|
|
358
|
+
expect(updateCall[1]).not.toContain("---");
|
|
359
|
+
expect(updateCall[1]).not.toContain(ABD_APPEND_PROMPT);
|
|
360
|
+
const userText = prompt.mock.calls[0][1];
|
|
361
|
+
expect(userText).toContain(`[User message]\n帮我看一下日志\n\n---\n${ABD_APPEND_PROMPT}\n[/User message]`);
|
|
362
|
+
expect(userText).not.toContain("/abd");
|
|
363
|
+
});
|
|
352
364
|
|
|
353
365
|
it("cleans stale Feishu p2p binding but keeps valid commands from auto-creating a group", async () => {
|
|
354
366
|
const platform = mockPlatform("feishu");
|
|
@@ -370,35 +382,68 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
370
382
|
|
|
371
383
|
it("handles /usage without creating a new Feishu group", async () => {
|
|
372
384
|
const platform = mockPlatform("feishu");
|
|
373
|
-
mockGetCodexUsageSummary.mockResolvedValue({
|
|
374
|
-
fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
|
|
375
|
-
weekly: { usedPercent: 12, remainingPercent: 88, resetAtEpochSeconds: 1781842926, resetAfterSeconds: 325063 },
|
|
376
|
-
rateLimitResetCreditsAvailable: 2,
|
|
377
|
-
rateLimitResetCredits: [
|
|
378
|
-
{ grantedAt: "2026-06-12T04:01:47.770016Z", expiresAt: "2026-07-12T04:01:47.770016Z" },
|
|
379
|
-
{ grantedAt: "2026-06-18T00:44:23.904386Z", expiresAt: "2026-07-18T00:44:23.904386Z" },
|
|
380
|
-
],
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
await handleCommand(platform, "/usage", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
384
|
-
|
|
385
|
-
expect(platform.createGroup).not.toHaveBeenCalled();
|
|
386
|
-
expect(platform.sendCard).not.toHaveBeenCalled();
|
|
387
|
-
expect(platform.sendRawCard).toHaveBeenCalledTimes(1);
|
|
388
|
-
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
389
|
-
expect(card.header.title.content).toBe("Codex Usage");
|
|
390
|
-
expect(card.elements[0].text.content).toContain("2026-07-12");
|
|
391
|
-
expect(card.elements[0].text.content).toContain("2026-07-18");
|
|
392
|
-
expect(card.elements[0].text.content).toContain("**主动重置:** 剩余 2 次");
|
|
393
|
-
expect(card.elements[0].text.content).toContain("
|
|
394
|
-
expect(card.elements[0].text.content).toContain("
|
|
395
|
-
expect(card.elements[0].text.content).toContain("
|
|
396
|
-
expect(card.elements[0].text.content).toContain("
|
|
397
|
-
expect(card.elements[0].text.content).toContain("
|
|
398
|
-
expect(card.elements[0].text.content).toContain("
|
|
399
|
-
expect(card.elements[
|
|
400
|
-
expect(card.elements[2].actions[0].
|
|
401
|
-
|
|
385
|
+
mockGetCodexUsageSummary.mockResolvedValue({
|
|
386
|
+
fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
|
|
387
|
+
weekly: { usedPercent: 12, remainingPercent: 88, resetAtEpochSeconds: 1781842926, resetAfterSeconds: 325063 },
|
|
388
|
+
rateLimitResetCreditsAvailable: 2,
|
|
389
|
+
rateLimitResetCredits: [
|
|
390
|
+
{ grantedAt: "2026-06-12T04:01:47.770016Z", expiresAt: "2026-07-12T04:01:47.770016Z" },
|
|
391
|
+
{ grantedAt: "2026-06-18T00:44:23.904386Z", expiresAt: "2026-07-18T00:44:23.904386Z" },
|
|
392
|
+
],
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
await handleCommand(platform, "/usage", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
396
|
+
|
|
397
|
+
expect(platform.createGroup).not.toHaveBeenCalled();
|
|
398
|
+
expect(platform.sendCard).not.toHaveBeenCalled();
|
|
399
|
+
expect(platform.sendRawCard).toHaveBeenCalledTimes(1);
|
|
400
|
+
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
401
|
+
expect(card.header.title.content).toBe("Codex Usage");
|
|
402
|
+
expect(card.elements[0].text.content).toContain("2026-07-12");
|
|
403
|
+
expect(card.elements[0].text.content).toContain("2026-07-18");
|
|
404
|
+
expect(card.elements[0].text.content).toContain("**主动重置:** 剩余 2 次");
|
|
405
|
+
expect(card.elements[0].text.content).not.toContain("ChatGPT 订阅");
|
|
406
|
+
expect(card.elements[0].text.content).toContain("**5h:** 已用 37%,剩余 63%,重置:");
|
|
407
|
+
expect(card.elements[0].text.content).toContain("约 2小时52分钟后");
|
|
408
|
+
expect(card.elements[0].text.content).toContain("[███████░░░░░░░░░░░░░]");
|
|
409
|
+
expect(card.elements[0].text.content).toContain("**周:** 已用 12%,剩余 88%,重置:");
|
|
410
|
+
expect(card.elements[0].text.content).toContain("约 3天18小时17分钟后");
|
|
411
|
+
expect(card.elements[0].text.content).toContain("[██░░░░░░░░░░░░░░░░░░]");
|
|
412
|
+
expect(card.elements[2].actions[0].text.content).toBe("发起重置");
|
|
413
|
+
expect(card.elements[2].actions[0].value).toEqual({ action: "codex_reset_request", availableCount: 2 });
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("adds ChatGPT subscription expiry to Codex /usage when CDP lookup succeeds", async () => {
|
|
417
|
+
const platform = mockPlatform("feishu");
|
|
418
|
+
mockGetCodexUsageSummary.mockResolvedValue({
|
|
419
|
+
fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
|
|
420
|
+
weekly: { usedPercent: 12, remainingPercent: 88, resetAtEpochSeconds: 1781842926, resetAfterSeconds: 325063 },
|
|
421
|
+
rateLimitResetCreditsAvailable: 0,
|
|
422
|
+
rateLimitResetCredits: [],
|
|
423
|
+
});
|
|
424
|
+
mockGetChatGptSubscriptionStatus.mockResolvedValue({
|
|
425
|
+
ok: true,
|
|
426
|
+
code: "ok",
|
|
427
|
+
chromeCdp: { enabled: true, port: 15166, status: "healthy" },
|
|
428
|
+
chatgpt: { sessionOk: true, maskedEmail: "gg***@gmail.com", sessionExpiresAt: "2026-09-20T09:30:07.340Z" },
|
|
429
|
+
subscription: {
|
|
430
|
+
active: true,
|
|
431
|
+
plan: "chatgptprolite",
|
|
432
|
+
expiresAt: "2026-07-12T10:20:11+00:00",
|
|
433
|
+
willRenew: false,
|
|
434
|
+
purchaseOriginPlatform: "chatgpt_web",
|
|
435
|
+
remainingDays: 20,
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
await handleCommand(platform, "/usage", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
440
|
+
|
|
441
|
+
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
442
|
+
expect(card.elements[0].text.content).toContain("**ChatGPT 订阅:**");
|
|
443
|
+
expect(card.elements[0].text.content).toContain("- 套餐: chatgptprolite");
|
|
444
|
+
expect(card.elements[0].text.content).toContain("剩余 20 天");
|
|
445
|
+
expect(card.elements[0].text.content).toContain("- 自动续费: 否");
|
|
446
|
+
});
|
|
402
447
|
|
|
403
448
|
it("handles /usage as Cursor usage in Cursor chats", async () => {
|
|
404
449
|
const platform = mockPlatform("feishu");
|
|
@@ -438,7 +483,7 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
438
483
|
expect(codexPlatform.sendCard).toHaveBeenCalledWith(
|
|
439
484
|
"feishu-group",
|
|
440
485
|
"Codex Session Ready",
|
|
441
|
-
expect.stringContaining("发送 **/usage** 查看 Codex 5h/周用量,以及查询/使用主动重置卡。"),
|
|
486
|
+
expect.stringContaining("发送 **/usage** 查看 Codex 5h/周用量,以及查询/使用主动重置卡。"),
|
|
442
487
|
"green",
|
|
443
488
|
);
|
|
444
489
|
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { gunzipSync } from "node:zlib";
|
|
2
|
+
import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
|
|
6
|
+
import { describe, expect, it } from "vitest";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
createRawStreamLog,
|
|
10
|
+
sanitizeLogPathSegment,
|
|
11
|
+
} from "../adapters/raw-stream-log.ts";
|
|
12
|
+
|
|
13
|
+
describe("raw stream log", () => {
|
|
14
|
+
it("does nothing when disabled", async () => {
|
|
15
|
+
const root = await mkdtemp(join(tmpdir(), "chatccc-raw-log-"));
|
|
16
|
+
try {
|
|
17
|
+
const log = await createRawStreamLog({
|
|
18
|
+
enabled: false,
|
|
19
|
+
rootDir: root,
|
|
20
|
+
tool: "cursor",
|
|
21
|
+
sessionId: "sid",
|
|
22
|
+
label: "turn",
|
|
23
|
+
maxBytesPerTurn: 1024,
|
|
24
|
+
retentionDays: 7,
|
|
25
|
+
});
|
|
26
|
+
expect(log).toBeNull();
|
|
27
|
+
} finally {
|
|
28
|
+
await rm(root, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("writes gzipped JSONL and can keep the file", async () => {
|
|
33
|
+
const root = await mkdtemp(join(tmpdir(), "chatccc-raw-log-"));
|
|
34
|
+
try {
|
|
35
|
+
const log = await createRawStreamLog({
|
|
36
|
+
enabled: true,
|
|
37
|
+
rootDir: root,
|
|
38
|
+
tool: "cursor",
|
|
39
|
+
sessionId: "sid/unsafe",
|
|
40
|
+
label: "turn:1",
|
|
41
|
+
maxBytesPerTurn: 1024,
|
|
42
|
+
retentionDays: 7,
|
|
43
|
+
});
|
|
44
|
+
expect(log).not.toBeNull();
|
|
45
|
+
log!.writeLine('{"type":"assistant","text":"hello"}');
|
|
46
|
+
await log!.close({ keep: true });
|
|
47
|
+
|
|
48
|
+
const raw = gunzipSync(await readFile(log!.filePath)).toString("utf-8");
|
|
49
|
+
expect(raw).toBe('{"type":"assistant","text":"hello"}\n');
|
|
50
|
+
expect(await stat(log!.filePath)).toBeTruthy();
|
|
51
|
+
} finally {
|
|
52
|
+
await rm(root, { recursive: true, force: true });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("writes a truncation marker once maxBytesPerTurn is exceeded", async () => {
|
|
57
|
+
const root = await mkdtemp(join(tmpdir(), "chatccc-raw-log-"));
|
|
58
|
+
try {
|
|
59
|
+
const log = await createRawStreamLog({
|
|
60
|
+
enabled: true,
|
|
61
|
+
rootDir: root,
|
|
62
|
+
tool: "cursor",
|
|
63
|
+
sessionId: "sid",
|
|
64
|
+
label: "turn",
|
|
65
|
+
maxBytesPerTurn: 12,
|
|
66
|
+
retentionDays: 7,
|
|
67
|
+
});
|
|
68
|
+
log!.writeLine('{"a":1}');
|
|
69
|
+
log!.writeLine('{"too":"large"}');
|
|
70
|
+
log!.writeLine('{"ignored":true}');
|
|
71
|
+
await log!.close({ keep: true });
|
|
72
|
+
|
|
73
|
+
const raw = gunzipSync(await readFile(log!.filePath)).toString("utf-8");
|
|
74
|
+
expect(raw).toContain('{"a":1}');
|
|
75
|
+
expect(raw).toContain("chatccc_raw_stream_log_truncated");
|
|
76
|
+
expect(raw).not.toContain("ignored");
|
|
77
|
+
} finally {
|
|
78
|
+
await rm(root, { recursive: true, force: true });
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("removes the file when keep is false", async () => {
|
|
83
|
+
const root = await mkdtemp(join(tmpdir(), "chatccc-raw-log-"));
|
|
84
|
+
try {
|
|
85
|
+
const log = await createRawStreamLog({
|
|
86
|
+
enabled: true,
|
|
87
|
+
rootDir: root,
|
|
88
|
+
tool: "cursor",
|
|
89
|
+
sessionId: "sid",
|
|
90
|
+
label: "turn",
|
|
91
|
+
maxBytesPerTurn: 1024,
|
|
92
|
+
retentionDays: 7,
|
|
93
|
+
});
|
|
94
|
+
log!.writeLine('{"type":"result"}');
|
|
95
|
+
await log!.close({ keep: false });
|
|
96
|
+
await expect(stat(log!.filePath)).rejects.toThrow();
|
|
97
|
+
} finally {
|
|
98
|
+
await rm(root, { recursive: true, force: true });
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("sanitizes path segments", () => {
|
|
103
|
+
expect(sanitizeLogPathSegment("../sid:1/2")).toBe("sid_1_2");
|
|
104
|
+
expect(sanitizeLogPathSegment("")).toBe("unknown");
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -33,6 +33,22 @@ describe("unflattenConfig", () => {
|
|
|
33
33
|
},
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
|
+
|
|
37
|
+
it("maps Chrome CDP guard fields into chromeDevtools config", () => {
|
|
38
|
+
expect(
|
|
39
|
+
unflattenConfig({
|
|
40
|
+
CHATCCC_CHROME_DEVTOOLS_ENABLED: true,
|
|
41
|
+
CHATCCC_CHROME_DEVTOOLS_PORT: "15166",
|
|
42
|
+
CHATCCC_CHROME_DEVTOOLS_PATH: "C:/Program Files/Google/Chrome/Application/chrome.exe",
|
|
43
|
+
}),
|
|
44
|
+
).toEqual({
|
|
45
|
+
chromeDevtools: {
|
|
46
|
+
enabled: true,
|
|
47
|
+
port: 15166,
|
|
48
|
+
chromePath: "C:/Program Files/Google/Chrome/Application/chrome.exe",
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
});
|
|
36
52
|
});
|
|
37
53
|
|
|
38
54
|
describe("dashboard edit modal", () => {
|
|
@@ -20,12 +20,16 @@ import type {
|
|
|
20
20
|
SessionInfo,
|
|
21
21
|
} from "./adapter-interface.ts";
|
|
22
22
|
import { parseUserCommand } from "./adapter-interface.ts";
|
|
23
|
-
import { CURSOR_AGENT_COMMAND, CURSOR_AGENT_ARGS } from "../config.ts";
|
|
23
|
+
import { config, CURSOR_AGENT_COMMAND, CURSOR_AGENT_ARGS, RAW_STREAM_LOGS_DIR } from "../config.ts";
|
|
24
24
|
import {
|
|
25
25
|
defaultCursorSessionMetaStore,
|
|
26
26
|
type CursorSessionMetaStore,
|
|
27
27
|
} from "./cursor-session-meta-store.ts";
|
|
28
|
-
import { killProcessTree } from "./proc-tree-kill.ts";
|
|
28
|
+
import { killProcessTree } from "./proc-tree-kill.ts";
|
|
29
|
+
import {
|
|
30
|
+
createRawStreamLog,
|
|
31
|
+
type RawStreamLogHandle,
|
|
32
|
+
} from "./raw-stream-log.ts";
|
|
29
33
|
|
|
30
34
|
// ---------------------------------------------------------------------------
|
|
31
35
|
// 特殊注入提示
|
|
@@ -344,11 +348,12 @@ function spawnAgent(
|
|
|
344
348
|
return proc;
|
|
345
349
|
}
|
|
346
350
|
|
|
347
|
-
async function* readJsonLines(
|
|
348
|
-
proc: ChildProcess,
|
|
349
|
-
signal?: AbortSignal,
|
|
350
|
-
debugTag?: string,
|
|
351
|
-
|
|
351
|
+
async function* readJsonLines(
|
|
352
|
+
proc: ChildProcess,
|
|
353
|
+
signal?: AbortSignal,
|
|
354
|
+
debugTag?: string,
|
|
355
|
+
rawLog?: RawStreamLogHandle | null,
|
|
356
|
+
): AsyncGenerator<CursorMessageLine> {
|
|
352
357
|
const tag = debugTag ?? "cursor";
|
|
353
358
|
const rl = createInterface({ input: proc.stdout!, crlfDelay: Infinity });
|
|
354
359
|
// abort 时主动 close readline,避免等待 Windows 管道自然关闭(可能延迟数分钟)
|
|
@@ -359,9 +364,10 @@ async function* readJsonLines(
|
|
|
359
364
|
for await (const line of rl) {
|
|
360
365
|
if (signal?.aborted) break;
|
|
361
366
|
lineCount++;
|
|
362
|
-
const trimmed = line.trim();
|
|
363
|
-
if (!trimmed) continue;
|
|
364
|
-
|
|
367
|
+
const trimmed = line.trim();
|
|
368
|
+
if (!trimmed) continue;
|
|
369
|
+
rawLog?.writeLine(trimmed);
|
|
370
|
+
try {
|
|
365
371
|
yield JSON.parse(trimmed) as CursorMessageLine;
|
|
366
372
|
} catch { /* 非 JSON 行静默跳过 */ }
|
|
367
373
|
}
|
|
@@ -426,15 +432,32 @@ class CursorAdapter implements ToolAdapter {
|
|
|
426
432
|
cmd.mode ?? undefined,
|
|
427
433
|
);
|
|
428
434
|
this.activeProcs.add(proc);
|
|
429
|
-
if (proc.pid !== undefined) options?.onProcessStart?.({ pid: proc.pid });
|
|
435
|
+
if (proc.pid !== undefined) options?.onProcessStart?.({ pid: proc.pid });
|
|
436
|
+
|
|
437
|
+
const rawLogConfig = config.rawStreamLogs.cursor;
|
|
438
|
+
let rawLog: RawStreamLogHandle | null = null;
|
|
439
|
+
try {
|
|
440
|
+
rawLog = await createRawStreamLog({
|
|
441
|
+
enabled: rawLogConfig.enabled,
|
|
442
|
+
rootDir: RAW_STREAM_LOGS_DIR,
|
|
443
|
+
tool: "cursor",
|
|
444
|
+
sessionId,
|
|
445
|
+
label: "prompt",
|
|
446
|
+
maxBytesPerTurn: rawLogConfig.maxBytesPerTurn,
|
|
447
|
+
retentionDays: rawLogConfig.retentionDays,
|
|
448
|
+
});
|
|
449
|
+
} catch (err) {
|
|
450
|
+
console.error(`[Cursor raw stream log] create failed: ${(err as Error).message}`);
|
|
451
|
+
}
|
|
430
452
|
|
|
431
453
|
// 见 codex-adapter.ts 同位置注释:spawn 用了 shell:true,必须杀整棵树,
|
|
432
454
|
// 否则 abort 后真正在跑的孙进程 cursor-agent 还会继续输出 & 占用资源。
|
|
433
|
-
const onAbort = () => { void killProcessTree(proc.pid); };
|
|
434
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
455
|
+
const onAbort = () => { void killProcessTree(proc.pid); };
|
|
456
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
457
|
+
let sawResult = false;
|
|
458
|
+
|
|
459
|
+
try {
|
|
460
|
+
for await (const raw of readJsonLines(proc, signal, sessionId, rawLog)) {
|
|
438
461
|
if (signal?.aborted) break;
|
|
439
462
|
if (
|
|
440
463
|
raw.type === "system" &&
|
|
@@ -450,15 +473,17 @@ class CursorAdapter implements ToolAdapter {
|
|
|
450
473
|
if (normalized) yield normalized;
|
|
451
474
|
|
|
452
475
|
// result 是流末事件,收到后立即结束进程,防止 CLI 僵死导致 readline 挂起。
|
|
453
|
-
if (raw.type === "result") {
|
|
454
|
-
|
|
476
|
+
if (raw.type === "result") {
|
|
477
|
+
sawResult = true;
|
|
478
|
+
void killProcessTree(proc.pid);
|
|
455
479
|
break;
|
|
456
480
|
}
|
|
457
481
|
}
|
|
458
482
|
} finally {
|
|
459
483
|
signal?.removeEventListener("abort", onAbort);
|
|
460
|
-
await killProcessTree(proc.pid);
|
|
461
|
-
|
|
484
|
+
await killProcessTree(proc.pid);
|
|
485
|
+
await rawLog?.close({ keep: rawLogConfig.keepCompleted || signal?.aborted === true || !sawResult });
|
|
486
|
+
this.activeProcs.delete(proc);
|
|
462
487
|
if (proc.pid !== undefined) options?.onProcessExit?.({ pid: proc.pid });
|
|
463
488
|
console.log(`[Cursor debug] prompt end: sessionId=${sessionId}, signalAborted=${signal?.aborted ?? false}`);
|
|
464
489
|
}
|
|
@@ -494,4 +519,4 @@ export function createCursorAdapter(
|
|
|
494
519
|
options: CreateCursorAdapterOptions = {},
|
|
495
520
|
): ToolAdapter {
|
|
496
521
|
return new CursorAdapter(options.metaStore ?? defaultCursorSessionMetaStore, options.model);
|
|
497
|
-
}
|
|
522
|
+
}
|