chatccc 0.2.198 → 0.2.200

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 (49) hide show
  1. package/README.md +1 -1
  2. package/agent-prompts/claude_specific.md +45 -45
  3. package/agent-prompts/codex_specific.md +2 -2
  4. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  5. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  6. package/im-skills/feishu-skill/skill.md +3 -3
  7. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  8. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  9. package/im-skills/wechat-file-skill/skill.md +10 -10
  10. package/im-skills/wechat-image-skill/skill.md +10 -10
  11. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  12. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  13. package/im-skills/wechat-video-skill/skill.md +10 -10
  14. package/package.json +1 -1
  15. package/scripts/postinstall-sharp-check.mjs +58 -58
  16. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  17. package/src/__tests__/card-plain-text.test.ts +5 -5
  18. package/src/__tests__/cardkit.test.ts +60 -60
  19. package/src/__tests__/cards.test.ts +1 -1
  20. package/src/__tests__/claude-adapter.test.ts +592 -592
  21. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  22. package/src/__tests__/cursor-adapter.test.ts +66 -7
  23. package/src/__tests__/feishu-api.test.ts +60 -60
  24. package/src/__tests__/feishu-avatar.test.ts +47 -5
  25. package/src/__tests__/feishu-platform.test.ts +22 -22
  26. package/src/__tests__/format-message.test.ts +47 -47
  27. package/src/__tests__/orchestrator.test.ts +27 -2
  28. package/src/__tests__/privacy.test.ts +198 -198
  29. package/src/__tests__/shared-prefix.test.ts +36 -36
  30. package/src/__tests__/stream-state.test.ts +42 -42
  31. package/src/adapters/claude-session-meta-store.ts +120 -120
  32. package/src/adapters/cursor-adapter.ts +39 -6
  33. package/src/adapters/resource-monitor.ts +140 -140
  34. package/src/agent-delegate-task-rpc.ts +153 -153
  35. package/src/agent-delegate-task.ts +81 -81
  36. package/src/agent-stop-stuck.ts +129 -129
  37. package/src/cards.ts +1 -1
  38. package/src/codex-reset-actions.ts +184 -184
  39. package/src/feishu-api.ts +41 -18
  40. package/src/feishu-platform.ts +20 -20
  41. package/src/format-message.ts +293 -293
  42. package/src/litellm-proxy.ts +374 -374
  43. package/src/orchestrator.ts +4 -4
  44. package/src/privacy.ts +118 -118
  45. package/src/session-chat-binding.ts +6 -6
  46. package/src/session-name.ts +8 -8
  47. package/src/shared-prefix.ts +29 -29
  48. package/src/sim-platform.ts +20 -20
  49. package/src/turn-cards.ts +117 -117
@@ -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/feishu-api.ts CHANGED
@@ -323,7 +323,7 @@ const AVATAR_SIZE = 256;
323
323
  const AVATAR_BADGE_SIZE = 92;
324
324
  const AVATAR_BADGE_MARGIN = 10;
325
325
  const PLAIN_AVATAR_TOOL = "plain";
326
- const CODEX_AVATAR_USAGE_STYLE_VERSION = "usage-ring-gray-consumed-v13";
326
+ const CODEX_AVATAR_USAGE_STYLE_VERSION = "usage-window-aware-v14";
327
327
  const CURSOR_AVATAR_USAGE_STYLE_VERSION = "usage-battery-v1";
328
328
 
329
329
  export interface CodexUsageBalance {
@@ -331,6 +331,7 @@ export interface CodexUsageBalance {
331
331
  remainingPercent: number;
332
332
  resetAtEpochSeconds: number | null;
333
333
  resetAfterSeconds: number | null;
334
+ limitWindowSeconds?: number | null;
334
335
  }
335
336
 
336
337
  export interface CodexRateLimitResetCredit {
@@ -339,7 +340,7 @@ export interface CodexRateLimitResetCredit {
339
340
  }
340
341
 
341
342
  export interface CodexUsageSummary {
342
- fiveHour: CodexUsageBalance;
343
+ fiveHour: CodexUsageBalance | null;
343
344
  weekly: CodexUsageBalance | null;
344
345
  rateLimitResetCreditsAvailable: number | null;
345
346
  rateLimitResetCredits: CodexRateLimitResetCredit[] | null;
@@ -359,6 +360,9 @@ function normalizeAvatarTool(tool: string): string {
359
360
  return AVATAR_BADGES[tool] ? tool : PLAIN_AVATAR_TOOL;
360
361
  }
361
362
 
363
+ const FIVE_HOUR_WINDOW_SECONDS = 5 * 60 * 60;
364
+ const SEVEN_DAY_WINDOW_SECONDS = 7 * 24 * 60 * 60;
365
+
362
366
  function normalizeAvatarStatus(status: string): string {
363
367
  return AVATAR_SOURCES[status] ? status : "idle";
364
368
  }
@@ -376,9 +380,9 @@ function avatarCacheKey(
376
380
  const normalizedTool = normalizeAvatarTool(tool);
377
381
  const normalizedStatus = normalizeAvatarStatus(status);
378
382
  if (normalizedTool === "codex") {
379
- return codexUsage
380
- ? `${normalizedTool}:${normalizedStatus}:${CODEX_AVATAR_USAGE_STYLE_VERSION}:week-battery:${codexUsage.weekly?.remainingPercent}:5h-ring:${codexUsage.fiveHour.remainingPercent}`
381
- : `${normalizedTool}:${normalizedStatus}:plain`;
383
+ if (!codexUsage?.weekly) return `${normalizedTool}:${normalizedStatus}:plain`;
384
+ const ringKey = codexUsage.fiveHour ? `:5h-ring:${codexUsage.fiveHour.remainingPercent}` : "";
385
+ return `${normalizedTool}:${normalizedStatus}:${CODEX_AVATAR_USAGE_STYLE_VERSION}:7d-battery:${codexUsage.weekly.remainingPercent}${ringKey}`;
382
386
  }
383
387
  if (normalizedTool === "cursor") {
384
388
  return cursorBatteryPercent !== null
@@ -423,12 +427,22 @@ function usageBalanceFromWindow(raw: Record<string, unknown>, fieldName: string)
423
427
  const used = clampPercent(usedPercent);
424
428
  const resetAt = Number(raw.reset_at);
425
429
  const resetAfter = Number(raw.reset_after_seconds);
426
- return {
430
+ const rawLimitWindow = raw.limit_window_seconds;
431
+ const limitWindow = rawLimitWindow === null || rawLimitWindow === undefined || rawLimitWindow === ""
432
+ ? Number.NaN
433
+ : Number(rawLimitWindow);
434
+ const balance: CodexUsageBalance = {
427
435
  usedPercent: used,
428
436
  remainingPercent: clampPercent(100 - used),
429
437
  resetAtEpochSeconds: Number.isFinite(resetAt) ? resetAt : null,
430
438
  resetAfterSeconds: Number.isFinite(resetAfter) ? resetAfter : null,
431
439
  };
440
+ if (Number.isFinite(limitWindow)) balance.limitWindowSeconds = limitWindow;
441
+ return balance;
442
+ }
443
+
444
+ function isUsageWindowDuration(balance: CodexUsageBalance | null, seconds: number): boolean {
445
+ return balance?.limitWindowSeconds === seconds;
432
446
  }
433
447
 
434
448
  function parseOptionalUsageWindow(rateLimit: Record<string, unknown>, keys: string[]): CodexUsageBalance | null {
@@ -541,18 +555,25 @@ export async function getCodexUsageSummary(): Promise<CodexUsageSummary> {
541
555
  const rateLimit = data.rate_limit;
542
556
  if (!rateLimit || typeof rateLimit !== "object") throw new Error("missing rate_limit");
543
557
 
544
- const fiveHour = parseOptionalUsageWindow(rateLimit, ["primary_window"]);
545
- if (!fiveHour) throw new Error("missing rate_limit.primary_window.used_percent");
558
+ const primaryWindow = parseOptionalUsageWindow(rateLimit, ["primary_window"]);
559
+ const secondaryWindow = parseOptionalUsageWindow(rateLimit, [
560
+ "secondary_window",
561
+ "weekly_window",
562
+ "week_window",
563
+ "long_window",
564
+ ]);
565
+ if (!primaryWindow && !secondaryWindow) throw new Error("missing supported rate_limit usage window");
566
+
567
+ const windows = [primaryWindow, secondaryWindow];
568
+ const fiveHour = windows.find((window) => isUsageWindowDuration(window, FIVE_HOUR_WINDOW_SECONDS))
569
+ ?? (primaryWindow?.limitWindowSeconds === undefined ? primaryWindow : null);
570
+ const weekly = windows.find((window) => isUsageWindowDuration(window, SEVEN_DAY_WINDOW_SECONDS))
571
+ ?? (secondaryWindow?.limitWindowSeconds === undefined ? secondaryWindow : null);
546
572
  const resetCredits = await resetCreditsPromise;
547
573
 
548
574
  return {
549
575
  fiveHour,
550
- weekly: parseOptionalUsageWindow(rateLimit, [
551
- "secondary_window",
552
- "weekly_window",
553
- "week_window",
554
- "long_window",
555
- ]),
576
+ weekly,
556
577
  rateLimitResetCreditsAvailable: resetCredits?.availableCount ?? parseRateLimitResetCredits(data),
557
578
  rateLimitResetCredits: resetCredits?.availableCredits ?? null,
558
579
  };
@@ -763,8 +784,8 @@ async function renderAvatar(
763
784
  const composites: sharp.OverlayOptions[] = [];
764
785
  const hasAgentBadge = normalizedTool !== PLAIN_AVATAR_TOOL;
765
786
 
766
- const codexWeeklyUsage = normalizedTool === "codex" ? codexUsage?.weekly : null;
767
- const useDynamicCodexAvatar = codexUsage && codexWeeklyUsage;
787
+ const codexWeeklyUsage = normalizedTool === "codex" ? codexUsage?.weekly ?? null : null;
788
+ const useDynamicCodexAvatar = normalizedTool === "codex" && codexUsage !== null && codexWeeklyUsage !== null;
768
789
  const useDynamicCursorAvatar = normalizedTool === "cursor" && cursorBatteryPercent !== null;
769
790
  const basePath = useDynamicCodexAvatar || useDynamicCursorAvatar
770
791
  ? AVATAR_SOURCES[normalizedStatus]
@@ -773,8 +794,10 @@ async function renderAvatar(
773
794
  : AVATAR_SOURCES[normalizedStatus];
774
795
 
775
796
  if (useDynamicCodexAvatar) {
797
+ if (codexUsage.fiveHour) {
798
+ composites.push({ input: buildCodexUsageRingSvg(codexUsage.fiveHour.remainingPercent), left: 0, top: 0 });
799
+ }
776
800
  composites.push(
777
- { input: buildCodexUsageRingSvg(codexUsage.fiveHour.remainingPercent), left: 0, top: 0 },
778
801
  { input: buildCodexUsageBatterySvg(codexWeeklyUsage.remainingPercent), left: 0, top: 0 },
779
802
  await buildAgentBadgeOverlay(normalizedTool),
780
803
  );
@@ -801,7 +824,7 @@ async function renderAvatar(
801
824
  buffer: jpeg,
802
825
  contentType: "image/jpeg",
803
826
  filename: normalizedTool === "codex" && codexUsage?.weekly
804
- ? `avatar_${normalizedTool}_${normalizedStatus}_week_${codexUsage.weekly.remainingPercent}_5h_${codexUsage.fiveHour.remainingPercent}.jpg`
827
+ ? `avatar_${normalizedTool}_${normalizedStatus}_7d_${codexUsage.weekly.remainingPercent}${codexUsage.fiveHour ? `_5h_${codexUsage.fiveHour.remainingPercent}` : ""}.jpg`
805
828
  : normalizedTool === "cursor" && cursorBatteryPercent !== null
806
829
  ? `avatar_${normalizedTool}_${normalizedStatus}_battery_${cursorBatteryPercent}.jpg`
807
830
  : `avatar_${normalizedTool}_${normalizedStatus}.jpg`,
@@ -29,10 +29,10 @@ export interface FeishuPlatform {
29
29
  updateChatInfo: typeof realApi.updateChatInfo;
30
30
  getChatInfo: typeof realApi.getChatInfo;
31
31
  disbandChat: typeof realApi.disbandChat;
32
- setChatAvatar: typeof realApi.setChatAvatar;
33
- getCodexUsageSummary: typeof realApi.getCodexUsageSummary;
34
- consumeCodexRateLimitResetCredit: typeof realApi.consumeCodexRateLimitResetCredit;
35
- getOrDownloadImage: typeof realApi.getOrDownloadImage;
32
+ setChatAvatar: typeof realApi.setChatAvatar;
33
+ getCodexUsageSummary: typeof realApi.getCodexUsageSummary;
34
+ consumeCodexRateLimitResetCredit: typeof realApi.consumeCodexRateLimitResetCredit;
35
+ getOrDownloadImage: typeof realApi.getOrDownloadImage;
36
36
  verifyAllPermissions: typeof realApi.verifyAllPermissions;
37
37
  reportPermissionResults: typeof realApi.reportPermissionResults;
38
38
  extractSessionInfo: typeof realApi.extractSessionInfo;
@@ -110,21 +110,21 @@ export function disbandChat(...args: Parameters<typeof realApi.disbandChat>): Re
110
110
  return _impl.disbandChat(...args);
111
111
  }
112
112
 
113
- export function setChatAvatar(...args: Parameters<typeof realApi.setChatAvatar>): ReturnType<typeof realApi.setChatAvatar> {
114
- return _impl.setChatAvatar(...args);
115
- }
116
-
117
- export function getCodexUsageSummary(...args: Parameters<typeof realApi.getCodexUsageSummary>): ReturnType<typeof realApi.getCodexUsageSummary> {
118
- return _impl.getCodexUsageSummary(...args);
119
- }
120
-
121
- export function consumeCodexRateLimitResetCredit(...args: Parameters<typeof realApi.consumeCodexRateLimitResetCredit>): ReturnType<typeof realApi.consumeCodexRateLimitResetCredit> {
122
- return _impl.consumeCodexRateLimitResetCredit(...args);
123
- }
124
-
125
- export function getOrDownloadImage(...args: Parameters<typeof realApi.getOrDownloadImage>): ReturnType<typeof realApi.getOrDownloadImage> {
126
- return _impl.getOrDownloadImage(...args);
127
- }
113
+ export function setChatAvatar(...args: Parameters<typeof realApi.setChatAvatar>): ReturnType<typeof realApi.setChatAvatar> {
114
+ return _impl.setChatAvatar(...args);
115
+ }
116
+
117
+ export function getCodexUsageSummary(...args: Parameters<typeof realApi.getCodexUsageSummary>): ReturnType<typeof realApi.getCodexUsageSummary> {
118
+ return _impl.getCodexUsageSummary(...args);
119
+ }
120
+
121
+ export function consumeCodexRateLimitResetCredit(...args: Parameters<typeof realApi.consumeCodexRateLimitResetCredit>): ReturnType<typeof realApi.consumeCodexRateLimitResetCredit> {
122
+ return _impl.consumeCodexRateLimitResetCredit(...args);
123
+ }
124
+
125
+ export function getOrDownloadImage(...args: Parameters<typeof realApi.getOrDownloadImage>): ReturnType<typeof realApi.getOrDownloadImage> {
126
+ return _impl.getOrDownloadImage(...args);
127
+ }
128
128
 
129
129
  export function verifyAllPermissions(...args: Parameters<typeof realApi.verifyAllPermissions>): ReturnType<typeof realApi.verifyAllPermissions> {
130
130
  return _impl.verifyAllPermissions(...args);
@@ -156,4 +156,4 @@ export function sendRestartCard(...args: Parameters<typeof realApi.sendRestartCa
156
156
 
157
157
  export function getMergeForwardMessages(...args: Parameters<typeof realApi.getMergeForwardMessages>): ReturnType<typeof realApi.getMergeForwardMessages> {
158
158
  return _impl.getMergeForwardMessages(...args);
159
- }
159
+ }