chatccc 0.2.183 → 0.2.185

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.
@@ -1,184 +1,184 @@
1
- import { randomUUID } from "node:crypto";
2
-
3
- import { buildCodexResetConfirmCard } from "./cards.ts";
4
- import type { CodexResetConsumeResult } from "./feishu-api.ts";
5
-
6
- type CodexResetDecision = "yes" | "no";
7
-
8
- interface PendingCodexResetRequest {
9
- chatId: string;
10
- parentMessageId: string;
11
- status: "pending" | "handled";
12
- }
13
-
14
- export interface CodexResetActionDeps {
15
- getTenantAccessToken(): Promise<string>;
16
- sendRawCard(token: string, chatId: string, cardJson: string): Promise<boolean>;
17
- sendTextReply(token: string, chatId: string, text: string): Promise<boolean>;
18
- sendCardReply(token: string, chatId: string, title: string, content: string, template?: string): Promise<boolean>;
19
- updateCardMessage(token: string, messageId: string, content: string): Promise<boolean>;
20
- recallMessage(token: string, messageId: string): Promise<boolean>;
21
- consumeCodexRateLimitResetCredit(redeemRequestId: string): Promise<CodexResetConsumeResult>;
22
- createRequestId?: () => string;
23
- }
24
-
25
- const pendingCodexResetRequests = new Map<string, PendingCodexResetRequest>();
26
-
27
- function rawEvent(data: unknown): Record<string, unknown> {
28
- return ((data as Record<string, unknown>)?.event ?? data) as Record<string, unknown>;
29
- }
30
-
31
- function actionValue(raw: Record<string, unknown>): Record<string, unknown> | null {
32
- const action = raw.action as { value?: unknown } | undefined;
33
- const value = action?.value;
34
- if (!value || typeof value !== "object") return null;
35
- return value as Record<string, unknown>;
36
- }
37
-
38
- function eventMessageId(raw: Record<string, unknown>): string {
39
- return typeof raw.open_message_id === "string"
40
- ? raw.open_message_id
41
- : typeof (raw.context as Record<string, unknown> | undefined)?.open_message_id === "string"
42
- ? (raw.context as Record<string, string>).open_message_id
43
- : typeof raw.message_id === "string"
44
- ? raw.message_id
45
- : typeof (raw.context as Record<string, unknown> | undefined)?.message_id === "string"
46
- ? (raw.context as Record<string, string>).message_id
47
- : typeof (raw.message as Record<string, unknown> | undefined)?.message_id === "string"
48
- ? (raw.message as Record<string, string>).message_id
49
- : "";
50
- }
51
-
52
- function eventChatId(raw: Record<string, unknown>): string {
53
- return typeof raw.open_chat_id === "string"
54
- ? raw.open_chat_id
55
- : typeof (raw.context as Record<string, unknown> | undefined)?.open_chat_id === "string"
56
- ? (raw.context as Record<string, string>).open_chat_id
57
- : typeof (raw.message as Record<string, unknown> | undefined)?.chat_id === "string"
58
- ? (raw.message as Record<string, string>).chat_id
59
- : "";
60
- }
61
-
62
- function collapsedCard(): string {
63
- return JSON.stringify({
64
- config: { wide_screen_mode: true },
65
- elements: [{ tag: "markdown", content: " " }],
66
- });
67
- }
68
-
69
- function resetResultMessage(result: CodexResetConsumeResult): { content: string; template: string } {
70
- if (result.code === "reset") {
71
- return {
72
- content: `重置成功。已重置 ${result.windowsReset} 个 Codex 用量窗口。`,
73
- template: "green",
74
- };
75
- }
76
- if (result.code === "nothing_to_reset") {
77
- return {
78
- content: "没有需要重置的 Codex 用量窗口。",
79
- template: "yellow",
80
- };
81
- }
82
- if (result.code === "no_credit") {
83
- return {
84
- content: "没有可用的 Codex 主动重置次数。",
85
- template: "red",
86
- };
87
- }
88
- return {
89
- content: "这次 Codex 主动重置请求已经处理过。",
90
- template: "yellow",
91
- };
92
- }
93
-
94
- async function sendResetRequestConfirmation(
95
- raw: Record<string, unknown>,
96
- value: Record<string, unknown>,
97
- deps: CodexResetActionDeps,
98
- ): Promise<boolean> {
99
- const parentMessageId = eventMessageId(raw);
100
- const chatId = eventChatId(raw);
101
- if (!parentMessageId || !chatId) return true;
102
- const availableCount = Number(value.availableCount);
103
-
104
- const requestId = (deps.createRequestId ?? randomUUID)();
105
- pendingCodexResetRequests.set(requestId, {
106
- chatId,
107
- parentMessageId,
108
- status: "pending",
109
- });
110
-
111
- const token = await deps.getTenantAccessToken();
112
- await deps.sendRawCard(token, chatId, buildCodexResetConfirmCard({
113
- availableCount: Number.isFinite(availableCount) ? Math.max(1, Math.trunc(availableCount)) : 1,
114
- parentMessageId,
115
- requestId,
116
- }));
117
- return true;
118
- }
119
-
120
- async function handleResetConfirmation(
121
- raw: Record<string, unknown>,
122
- value: Record<string, unknown>,
123
- deps: CodexResetActionDeps,
124
- ): Promise<boolean> {
125
- const decision = value.decision;
126
- const requestId = value.requestId;
127
- const parentMessageId = value.parentMessageId;
128
- if ((decision !== "yes" && decision !== "no") || typeof requestId !== "string" || typeof parentMessageId !== "string") {
129
- return true;
130
- }
131
-
132
- const token = await deps.getTenantAccessToken();
133
- const confirmMessageId = eventMessageId(raw);
134
- if (confirmMessageId) {
135
- const collapsed = await deps.updateCardMessage(token, confirmMessageId, collapsedCard());
136
- console.log(`[Codex Reset] collapse confirmation card ${collapsed ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
137
- const recalled = await deps.recallMessage(token, confirmMessageId);
138
- console.log(`[Codex Reset] recall confirmation card ${recalled ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
139
- } else {
140
- console.error("[Codex Reset] missing confirmation card message id; cannot collapse");
141
- }
142
-
143
- const pending = pendingCodexResetRequests.get(requestId);
144
- const chatId = pending?.chatId ?? eventChatId(raw);
145
- if (!chatId) return true;
146
-
147
- if (!pending || pending.status !== "pending" || pending.parentMessageId !== parentMessageId) {
148
- await deps.sendTextReply(token, chatId, "Codex 主动重置:这张确认卡片已经失效或已处理。");
149
- return true;
150
- }
151
- pending.status = "handled";
152
-
153
- if (decision === "no") {
154
- await deps.sendTextReply(token, chatId, "Codex 主动重置:用户取消了重置。");
155
- return true;
156
- }
157
-
158
- try {
159
- const result = await deps.consumeCodexRateLimitResetCredit(requestId);
160
- const message = resetResultMessage(result);
161
- await deps.sendTextReply(token, chatId, `Codex 主动重置:${message.content}`);
162
- } catch (err) {
163
- await deps.sendTextReply(token, chatId, `Codex 主动重置失败:${(err as Error).message}`);
164
- }
165
- return true;
166
- }
167
-
168
- export async function handleCodexResetCardAction(data: unknown, deps: CodexResetActionDeps): Promise<boolean> {
169
- const raw = rawEvent(data);
170
- const value = actionValue(raw);
171
- if (!value) return false;
172
- const action = value?.action;
173
- if (action === "codex_reset_request") {
174
- return sendResetRequestConfirmation(raw, value, deps);
175
- }
176
- if (action === "codex_reset_confirm") {
177
- return handleResetConfirmation(raw, value, deps);
178
- }
179
- return false;
180
- }
181
-
182
- export function _resetCodexResetRequestsForTest(): void {
183
- pendingCodexResetRequests.clear();
184
- }
1
+ import { randomUUID } from "node:crypto";
2
+
3
+ import { buildCodexResetConfirmCard } from "./cards.ts";
4
+ import type { CodexResetConsumeResult } from "./feishu-api.ts";
5
+
6
+ type CodexResetDecision = "yes" | "no";
7
+
8
+ interface PendingCodexResetRequest {
9
+ chatId: string;
10
+ parentMessageId: string;
11
+ status: "pending" | "handled";
12
+ }
13
+
14
+ export interface CodexResetActionDeps {
15
+ getTenantAccessToken(): Promise<string>;
16
+ sendRawCard(token: string, chatId: string, cardJson: string): Promise<boolean>;
17
+ sendTextReply(token: string, chatId: string, text: string): Promise<boolean>;
18
+ sendCardReply(token: string, chatId: string, title: string, content: string, template?: string): Promise<boolean>;
19
+ updateCardMessage(token: string, messageId: string, content: string): Promise<boolean>;
20
+ recallMessage(token: string, messageId: string): Promise<boolean>;
21
+ consumeCodexRateLimitResetCredit(redeemRequestId: string): Promise<CodexResetConsumeResult>;
22
+ createRequestId?: () => string;
23
+ }
24
+
25
+ const pendingCodexResetRequests = new Map<string, PendingCodexResetRequest>();
26
+
27
+ function rawEvent(data: unknown): Record<string, unknown> {
28
+ return ((data as Record<string, unknown>)?.event ?? data) as Record<string, unknown>;
29
+ }
30
+
31
+ function actionValue(raw: Record<string, unknown>): Record<string, unknown> | null {
32
+ const action = raw.action as { value?: unknown } | undefined;
33
+ const value = action?.value;
34
+ if (!value || typeof value !== "object") return null;
35
+ return value as Record<string, unknown>;
36
+ }
37
+
38
+ function eventMessageId(raw: Record<string, unknown>): string {
39
+ return typeof raw.open_message_id === "string"
40
+ ? raw.open_message_id
41
+ : typeof (raw.context as Record<string, unknown> | undefined)?.open_message_id === "string"
42
+ ? (raw.context as Record<string, string>).open_message_id
43
+ : typeof raw.message_id === "string"
44
+ ? raw.message_id
45
+ : typeof (raw.context as Record<string, unknown> | undefined)?.message_id === "string"
46
+ ? (raw.context as Record<string, string>).message_id
47
+ : typeof (raw.message as Record<string, unknown> | undefined)?.message_id === "string"
48
+ ? (raw.message as Record<string, string>).message_id
49
+ : "";
50
+ }
51
+
52
+ function eventChatId(raw: Record<string, unknown>): string {
53
+ return typeof raw.open_chat_id === "string"
54
+ ? raw.open_chat_id
55
+ : typeof (raw.context as Record<string, unknown> | undefined)?.open_chat_id === "string"
56
+ ? (raw.context as Record<string, string>).open_chat_id
57
+ : typeof (raw.message as Record<string, unknown> | undefined)?.chat_id === "string"
58
+ ? (raw.message as Record<string, string>).chat_id
59
+ : "";
60
+ }
61
+
62
+ function collapsedCard(): string {
63
+ return JSON.stringify({
64
+ config: { wide_screen_mode: true },
65
+ elements: [{ tag: "markdown", content: " " }],
66
+ });
67
+ }
68
+
69
+ function resetResultMessage(result: CodexResetConsumeResult): { content: string; template: string } {
70
+ if (result.code === "reset") {
71
+ return {
72
+ content: `重置成功。已重置 ${result.windowsReset} 个 Codex 用量窗口。`,
73
+ template: "green",
74
+ };
75
+ }
76
+ if (result.code === "nothing_to_reset") {
77
+ return {
78
+ content: "没有需要重置的 Codex 用量窗口。",
79
+ template: "yellow",
80
+ };
81
+ }
82
+ if (result.code === "no_credit") {
83
+ return {
84
+ content: "没有可用的 Codex 主动重置次数。",
85
+ template: "red",
86
+ };
87
+ }
88
+ return {
89
+ content: "这次 Codex 主动重置请求已经处理过。",
90
+ template: "yellow",
91
+ };
92
+ }
93
+
94
+ async function sendResetRequestConfirmation(
95
+ raw: Record<string, unknown>,
96
+ value: Record<string, unknown>,
97
+ deps: CodexResetActionDeps,
98
+ ): Promise<boolean> {
99
+ const parentMessageId = eventMessageId(raw);
100
+ const chatId = eventChatId(raw);
101
+ if (!parentMessageId || !chatId) return true;
102
+ const availableCount = Number(value.availableCount);
103
+
104
+ const requestId = (deps.createRequestId ?? randomUUID)();
105
+ pendingCodexResetRequests.set(requestId, {
106
+ chatId,
107
+ parentMessageId,
108
+ status: "pending",
109
+ });
110
+
111
+ const token = await deps.getTenantAccessToken();
112
+ await deps.sendRawCard(token, chatId, buildCodexResetConfirmCard({
113
+ availableCount: Number.isFinite(availableCount) ? Math.max(1, Math.trunc(availableCount)) : 1,
114
+ parentMessageId,
115
+ requestId,
116
+ }));
117
+ return true;
118
+ }
119
+
120
+ async function handleResetConfirmation(
121
+ raw: Record<string, unknown>,
122
+ value: Record<string, unknown>,
123
+ deps: CodexResetActionDeps,
124
+ ): Promise<boolean> {
125
+ const decision = value.decision;
126
+ const requestId = value.requestId;
127
+ const parentMessageId = value.parentMessageId;
128
+ if ((decision !== "yes" && decision !== "no") || typeof requestId !== "string" || typeof parentMessageId !== "string") {
129
+ return true;
130
+ }
131
+
132
+ const token = await deps.getTenantAccessToken();
133
+ const confirmMessageId = eventMessageId(raw);
134
+ if (confirmMessageId) {
135
+ const collapsed = await deps.updateCardMessage(token, confirmMessageId, collapsedCard());
136
+ console.log(`[Codex Reset] collapse confirmation card ${collapsed ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
137
+ const recalled = await deps.recallMessage(token, confirmMessageId);
138
+ console.log(`[Codex Reset] recall confirmation card ${recalled ? "OK" : "FAILED"}: messageId=${confirmMessageId}`);
139
+ } else {
140
+ console.error("[Codex Reset] missing confirmation card message id; cannot collapse");
141
+ }
142
+
143
+ const pending = pendingCodexResetRequests.get(requestId);
144
+ const chatId = pending?.chatId ?? eventChatId(raw);
145
+ if (!chatId) return true;
146
+
147
+ if (!pending || pending.status !== "pending" || pending.parentMessageId !== parentMessageId) {
148
+ await deps.sendTextReply(token, chatId, "Codex 主动重置:这张确认卡片已经失效或已处理。");
149
+ return true;
150
+ }
151
+ pending.status = "handled";
152
+
153
+ if (decision === "no") {
154
+ await deps.sendTextReply(token, chatId, "Codex 主动重置:用户取消了重置。");
155
+ return true;
156
+ }
157
+
158
+ try {
159
+ const result = await deps.consumeCodexRateLimitResetCredit(requestId);
160
+ const message = resetResultMessage(result);
161
+ await deps.sendTextReply(token, chatId, `Codex 主动重置:${message.content}`);
162
+ } catch (err) {
163
+ await deps.sendTextReply(token, chatId, `Codex 主动重置失败:${(err as Error).message}`);
164
+ }
165
+ return true;
166
+ }
167
+
168
+ export async function handleCodexResetCardAction(data: unknown, deps: CodexResetActionDeps): Promise<boolean> {
169
+ const raw = rawEvent(data);
170
+ const value = actionValue(raw);
171
+ if (!value) return false;
172
+ const action = value?.action;
173
+ if (action === "codex_reset_request") {
174
+ return sendResetRequestConfirmation(raw, value, deps);
175
+ }
176
+ if (action === "codex_reset_confirm") {
177
+ return handleResetConfirmation(raw, value, deps);
178
+ }
179
+ return false;
180
+ }
181
+
182
+ export function _resetCodexResetRequestsForTest(): void {
183
+ pendingCodexResetRequests.clear();
184
+ }
package/src/config.ts CHANGED
@@ -113,9 +113,19 @@ export interface PlatformsConfig {
113
113
  ilink: PlatformConfig;
114
114
  }
115
115
 
116
+ export interface ChromeDevtoolsConfig {
117
+ /** 是否由 ChatCCC 守护一个常驻 Chrome CDP 实例 */
118
+ enabled: boolean;
119
+ /** Chrome remote debugging 端口,默认 15166 */
120
+ port: number;
121
+ /** Chrome 可执行文件路径;留空时按常见安装位置自动探测 */
122
+ chromePath: string;
123
+ }
124
+
116
125
  export interface AppConfig {
117
126
  feishu: FeishuConfig;
118
127
  platforms: PlatformsConfig;
128
+ chromeDevtools: ChromeDevtoolsConfig;
119
129
  port: number;
120
130
  gitTimeoutSeconds: number;
121
131
  /** 若为 false,AI 生成过程中用户发送消息不会打断,须先点「停止」再发送新消息 */
@@ -342,6 +352,7 @@ function loadConfig(): AppConfig {
342
352
  const defaults: AppConfig = {
343
353
  feishu: { appId: "", appSecret: "" },
344
354
  platforms: { feishu: { enabled: true }, ilink: { enabled: true } },
355
+ chromeDevtools: { enabled: false, port: 15166, chromePath: "" },
345
356
  port: 18080,
346
357
  gitTimeoutSeconds: 180,
347
358
  allowInterrupt: false,
@@ -406,6 +417,7 @@ function loadConfig(): AppConfig {
406
417
  onDemandMonthlyBudget?: unknown;
407
418
  };
408
419
  codex?: { enabled?: unknown; defaultAgent?: unknown; path?: unknown; command?: unknown; model?: unknown; effort?: unknown };
420
+ chromeDevtools?: { enabled?: unknown; port?: unknown; chromePath?: unknown };
409
421
  };
410
422
  try {
411
423
  parsed = JSON.parse(raw);
@@ -418,6 +430,7 @@ function loadConfig(): AppConfig {
418
430
  const claude = parsed.claude ?? {} as Partial<ClaudeConfig>;
419
431
  const cursorRaw = (parsed.cursor ?? {}) as NonNullable<typeof parsed.cursor>;
420
432
  const codexRaw = (parsed.codex ?? {}) as NonNullable<typeof parsed.codex>;
433
+ const chromeDevtoolsRaw = (parsed.chromeDevtools ?? {}) as NonNullable<typeof parsed.chromeDevtools>;
421
434
 
422
435
  // 兼容旧字段 `command`:命中时打印一次性 warning 提示用户改名
423
436
  const onLegacyField = (label: string, value: string): void => {
@@ -461,6 +474,7 @@ function loadConfig(): AppConfig {
461
474
  const claudeEnabled = resolveEnabled(claude.enabled, claudeNonEmpty);
462
475
  const cursorEnabled = resolveEnabled(cursorRaw.enabled, cursorNonEmpty);
463
476
  const codexEnabled = resolveEnabled(codexRaw.enabled, codexNonEmpty);
477
+ const chromeDevtoolsPort = Number(chromeDevtoolsRaw.port);
464
478
  const explicitDefaultTool: AgentTool | null =
465
479
  typeof claude.defaultAgent === "boolean" && claude.defaultAgent && claudeEnabled ? "claude" :
466
480
  typeof cursorRaw.defaultAgent === "boolean" && cursorRaw.defaultAgent && cursorEnabled ? "cursor" :
@@ -498,6 +512,13 @@ function loadConfig(): AppConfig {
498
512
  : true,
499
513
  },
500
514
  },
515
+ chromeDevtools: {
516
+ enabled: typeof chromeDevtoolsRaw.enabled === "boolean" ? chromeDevtoolsRaw.enabled : false,
517
+ port: Number.isInteger(chromeDevtoolsPort) && chromeDevtoolsPort >= 1 && chromeDevtoolsPort <= 65535
518
+ ? chromeDevtoolsPort
519
+ : 15166,
520
+ chromePath: normalizeOptionalConfigField(chromeDevtoolsRaw.chromePath, { label: "chromeDevtools.chromePath" }),
521
+ },
501
522
  port: typeof parsed.port === "number" ? parsed.port : 18080,
502
523
  gitTimeoutSeconds: typeof parsed.gitTimeoutSeconds === "number" ? parsed.gitTimeoutSeconds : 180,
503
524
  allowInterrupt: typeof parsed.allowInterrupt === "boolean" ? parsed.allowInterrupt : false,