@clagentic/console 1.0.0-beta.1

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 (221) hide show
  1. package/CREDITS.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +250 -0
  4. package/bin/cli.js +2812 -0
  5. package/lib/agents-favorites.js +164 -0
  6. package/lib/agents.js +282 -0
  7. package/lib/ask-user-mcp-server.js +120 -0
  8. package/lib/browser-mcp-server.js +484 -0
  9. package/lib/build-user-env.js +63 -0
  10. package/lib/certs/fullchain.pem +47 -0
  11. package/lib/certs/privkey.pem +5 -0
  12. package/lib/cli-sessions.js +265 -0
  13. package/lib/codex-defaults.js +18 -0
  14. package/lib/config.js +332 -0
  15. package/lib/crisis-safety.js +60 -0
  16. package/lib/daemon-projects.js +164 -0
  17. package/lib/daemon.js +1602 -0
  18. package/lib/dm.js +128 -0
  19. package/lib/ipc.js +125 -0
  20. package/lib/ipv4-only.js +39 -0
  21. package/lib/mcp-local.js +385 -0
  22. package/lib/notes.js +141 -0
  23. package/lib/os-users.js +529 -0
  24. package/lib/pages.js +1325 -0
  25. package/lib/project-connection.js +302 -0
  26. package/lib/project-external-trigger.js +236 -0
  27. package/lib/project-file-watch.js +120 -0
  28. package/lib/project-filesystem.js +528 -0
  29. package/lib/project-http.js +753 -0
  30. package/lib/project-image.js +95 -0
  31. package/lib/project-knowledge.js +88 -0
  32. package/lib/project-loop.js +1304 -0
  33. package/lib/project-mcp.js +374 -0
  34. package/lib/project-memory.js +39 -0
  35. package/lib/project-notifications.js +262 -0
  36. package/lib/project-sessions.js +1581 -0
  37. package/lib/project-user-mention.js +193 -0
  38. package/lib/project-user-message.js +628 -0
  39. package/lib/project.js +1303 -0
  40. package/lib/public/app.js +1031 -0
  41. package/lib/public/apple-touch-icon.png +0 -0
  42. package/lib/public/claude-code-avatar.png +0 -0
  43. package/lib/public/codex-avatar.png +0 -0
  44. package/lib/public/console-icon.png +0 -0
  45. package/lib/public/css/admin.css +736 -0
  46. package/lib/public/css/agent-picker.css +119 -0
  47. package/lib/public/css/base.css +235 -0
  48. package/lib/public/css/channel-layout.css +441 -0
  49. package/lib/public/css/command-palette.css +559 -0
  50. package/lib/public/css/debate.css +1609 -0
  51. package/lib/public/css/diff.css +139 -0
  52. package/lib/public/css/filebrowser.css +1976 -0
  53. package/lib/public/css/highlight.css +144 -0
  54. package/lib/public/css/home-hub.css +545 -0
  55. package/lib/public/css/icon-strip.css +1856 -0
  56. package/lib/public/css/input.css +1349 -0
  57. package/lib/public/css/loop.css +1248 -0
  58. package/lib/public/css/mention.css +317 -0
  59. package/lib/public/css/menus.css +857 -0
  60. package/lib/public/css/messages.css +1833 -0
  61. package/lib/public/css/mobile-nav.css +1332 -0
  62. package/lib/public/css/notifications-center.css +296 -0
  63. package/lib/public/css/overlays.css +1273 -0
  64. package/lib/public/css/playbook.css +264 -0
  65. package/lib/public/css/profile.css +397 -0
  66. package/lib/public/css/rewind.css +671 -0
  67. package/lib/public/css/scheduler-modal.css +1495 -0
  68. package/lib/public/css/scheduler.css +1420 -0
  69. package/lib/public/css/server-settings.css +909 -0
  70. package/lib/public/css/session-search.css +159 -0
  71. package/lib/public/css/sidebar.css +2012 -0
  72. package/lib/public/css/skills.css +789 -0
  73. package/lib/public/css/sticky-notes.css +854 -0
  74. package/lib/public/css/stt.css +155 -0
  75. package/lib/public/css/title-bar.css +674 -0
  76. package/lib/public/css/tooltip.css +67 -0
  77. package/lib/public/css/user-settings.css +702 -0
  78. package/lib/public/emoji-data.json +1 -0
  79. package/lib/public/favicon-banded-32.png +0 -0
  80. package/lib/public/favicon-banded.png +0 -0
  81. package/lib/public/favicon-dark.svg +32 -0
  82. package/lib/public/favicon.svg +32 -0
  83. package/lib/public/icon-192.png +0 -0
  84. package/lib/public/icon-512.png +0 -0
  85. package/lib/public/icon-banded-76.png +0 -0
  86. package/lib/public/icon-banded-96.png +0 -0
  87. package/lib/public/icon-mono.svg +25 -0
  88. package/lib/public/icons/email/gmail.svg +7 -0
  89. package/lib/public/icons/email/outlook.svg +35 -0
  90. package/lib/public/icons/email/yahoo.svg +1 -0
  91. package/lib/public/index.html +1946 -0
  92. package/lib/public/manifest.json +27 -0
  93. package/lib/public/markdown-worker.js +43 -0
  94. package/lib/public/marked.min.js +6 -0
  95. package/lib/public/modules/admin.js +954 -0
  96. package/lib/public/modules/agent-picker.js +429 -0
  97. package/lib/public/modules/app-connection.js +216 -0
  98. package/lib/public/modules/app-cursors.js +492 -0
  99. package/lib/public/modules/app-dm.js +265 -0
  100. package/lib/public/modules/app-favicon.js +231 -0
  101. package/lib/public/modules/app-header.js +242 -0
  102. package/lib/public/modules/app-home-hub.js +432 -0
  103. package/lib/public/modules/app-loop-ui.js +691 -0
  104. package/lib/public/modules/app-loop-wizard.js +439 -0
  105. package/lib/public/modules/app-messages.js +1415 -0
  106. package/lib/public/modules/app-misc.js +463 -0
  107. package/lib/public/modules/app-notifications.js +651 -0
  108. package/lib/public/modules/app-panels.js +1061 -0
  109. package/lib/public/modules/app-projects.js +875 -0
  110. package/lib/public/modules/app-rate-limit.js +529 -0
  111. package/lib/public/modules/app-rendering.js +739 -0
  112. package/lib/public/modules/app-skills-install.js +285 -0
  113. package/lib/public/modules/avatar.js +36 -0
  114. package/lib/public/modules/command-palette.js +491 -0
  115. package/lib/public/modules/console-logo.js +162 -0
  116. package/lib/public/modules/context-sources.js +365 -0
  117. package/lib/public/modules/diff.js +412 -0
  118. package/lib/public/modules/dom-refs.js +21 -0
  119. package/lib/public/modules/emoji-picker.js +222 -0
  120. package/lib/public/modules/events.js +21 -0
  121. package/lib/public/modules/filebrowser.js +1966 -0
  122. package/lib/public/modules/fileicons.js +172 -0
  123. package/lib/public/modules/icons.js +80 -0
  124. package/lib/public/modules/input.js +1102 -0
  125. package/lib/public/modules/longpress.js +74 -0
  126. package/lib/public/modules/markdown.js +486 -0
  127. package/lib/public/modules/mcp-ui.js +295 -0
  128. package/lib/public/modules/mention.js +1093 -0
  129. package/lib/public/modules/notifications.js +613 -0
  130. package/lib/public/modules/playbook.js +578 -0
  131. package/lib/public/modules/profile.js +594 -0
  132. package/lib/public/modules/project-settings.js +849 -0
  133. package/lib/public/modules/project-switcher.js +375 -0
  134. package/lib/public/modules/qrcode.js +87 -0
  135. package/lib/public/modules/rewind.js +381 -0
  136. package/lib/public/modules/scheduler-config.js +1611 -0
  137. package/lib/public/modules/scheduler-history.js +131 -0
  138. package/lib/public/modules/scheduler.js +1689 -0
  139. package/lib/public/modules/server-settings.js +842 -0
  140. package/lib/public/modules/session-search.js +456 -0
  141. package/lib/public/modules/settings-defaults.js +253 -0
  142. package/lib/public/modules/sidebar-mobile.js +1425 -0
  143. package/lib/public/modules/sidebar-projects.js +2022 -0
  144. package/lib/public/modules/sidebar-sessions.js +1548 -0
  145. package/lib/public/modules/sidebar-users.js +449 -0
  146. package/lib/public/modules/sidebar.js +348 -0
  147. package/lib/public/modules/skills.js +793 -0
  148. package/lib/public/modules/state.js +3 -0
  149. package/lib/public/modules/sticky-notes.js +1221 -0
  150. package/lib/public/modules/store.js +34 -0
  151. package/lib/public/modules/stt.js +266 -0
  152. package/lib/public/modules/terminal.js +987 -0
  153. package/lib/public/modules/theme.js +812 -0
  154. package/lib/public/modules/tool-palette.js +562 -0
  155. package/lib/public/modules/tools.js +2285 -0
  156. package/lib/public/modules/tooltip.js +150 -0
  157. package/lib/public/modules/user-settings.js +544 -0
  158. package/lib/public/modules/utils.js +56 -0
  159. package/lib/public/modules/ws-ref.js +26 -0
  160. package/lib/public/style.css +34 -0
  161. package/lib/public/sw.js +159 -0
  162. package/lib/push.js +149 -0
  163. package/lib/scheduler.js +471 -0
  164. package/lib/sdk-bridge.js +1792 -0
  165. package/lib/sdk-message-processor.js +740 -0
  166. package/lib/sdk-message-queue.js +42 -0
  167. package/lib/sdk-skill-discovery.js +131 -0
  168. package/lib/sdk-worker.js +677 -0
  169. package/lib/server-admin.js +712 -0
  170. package/lib/server-auth.js +737 -0
  171. package/lib/server-dm.js +170 -0
  172. package/lib/server-palette.js +104 -0
  173. package/lib/server-settings.js +553 -0
  174. package/lib/server-skills.js +280 -0
  175. package/lib/server.js +1390 -0
  176. package/lib/session-search.js +693 -0
  177. package/lib/sessions.js +946 -0
  178. package/lib/smtp.js +221 -0
  179. package/lib/terminal-manager.js +219 -0
  180. package/lib/terminal.js +39 -0
  181. package/lib/themes/ayu-light.json +9 -0
  182. package/lib/themes/catppuccin-latte.json +9 -0
  183. package/lib/themes/catppuccin-mocha.json +9 -0
  184. package/lib/themes/clagentic-dark.json +10 -0
  185. package/lib/themes/clagentic-light.json +10 -0
  186. package/lib/themes/dracula.json +9 -0
  187. package/lib/themes/everforest-light.json +9 -0
  188. package/lib/themes/everforest.json +9 -0
  189. package/lib/themes/github-light.json +9 -0
  190. package/lib/themes/gruvbox-dark.json +9 -0
  191. package/lib/themes/gruvbox-light.json +9 -0
  192. package/lib/themes/monokai.json +9 -0
  193. package/lib/themes/nord-light.json +9 -0
  194. package/lib/themes/nord.json +9 -0
  195. package/lib/themes/one-dark.json +9 -0
  196. package/lib/themes/one-light.json +9 -0
  197. package/lib/themes/rose-pine-dawn.json +9 -0
  198. package/lib/themes/rose-pine.json +9 -0
  199. package/lib/themes/solarized-dark.json +9 -0
  200. package/lib/themes/solarized-light.json +9 -0
  201. package/lib/themes/tokyo-night-light.json +9 -0
  202. package/lib/themes/tokyo-night.json +9 -0
  203. package/lib/updater.js +124 -0
  204. package/lib/user-presence.js +92 -0
  205. package/lib/users-auth.js +146 -0
  206. package/lib/users-permissions.js +118 -0
  207. package/lib/users-preferences.js +254 -0
  208. package/lib/users.js +465 -0
  209. package/lib/utils.js +64 -0
  210. package/lib/worktree.js +134 -0
  211. package/lib/ws-schema.js +464 -0
  212. package/lib/yoke/adapters/claude-worker.js +759 -0
  213. package/lib/yoke/adapters/claude.js +1599 -0
  214. package/lib/yoke/adapters/codex.js +1559 -0
  215. package/lib/yoke/codex-app-server.js +352 -0
  216. package/lib/yoke/index.js +319 -0
  217. package/lib/yoke/instructions.js +61 -0
  218. package/lib/yoke/interface.js +98 -0
  219. package/lib/yoke/mcp-bridge-server.js +302 -0
  220. package/lib/yoke/package.json +7 -0
  221. package/package.json +72 -0
@@ -0,0 +1,529 @@
1
+ // app-rate-limit.js - Rate limit UI, scheduled messages, fast mode indicator
2
+ // Extracted from app.js (PR-26)
3
+
4
+ import { iconHtml, refreshIcons } from './icons.js';
5
+ import { store } from './store.js';
6
+ import { getWs } from './ws-ref.js';
7
+ import { addToMessages, scrollToBottom } from './app-rendering.js';
8
+ import { userAvatarUrl } from './avatar.js';
9
+ import { setScheduleDelayMs, clearScheduleDelay } from './input.js';
10
+ import { isSonnetModel, isOpusModel } from './settings-defaults.js';
11
+
12
+ // --- Module-owned state ---
13
+ var rateLimitCountdownTimer = null;
14
+ var rateLimitIndicatorEl = null;
15
+ var rateLimitResetsAt = null;
16
+ var rateLimitResetTimer = null;
17
+ var rateLimitUsageEl = null;
18
+ var rateLimitResetState = {};
19
+ var rateLimitTickTimer = null;
20
+ var _rateLimitUsageLastHtml = null; // guard against redundant innerHTML + refreshIcons()
21
+ var scheduledMsgEl = null;
22
+ var scheduledCountdownTimer = null;
23
+ var fastModeIndicatorEl = null;
24
+
25
+ // --- Internal helpers ---
26
+
27
+ function getVendorUsageMeta(vendor) {
28
+ if (vendor === "codex") {
29
+ return {
30
+ icon: "/codex-avatar.png",
31
+ alt: "Codex",
32
+ href: "https://chatgpt.com/admin/usage",
33
+ title: "Check usage on ChatGPT",
34
+ };
35
+ }
36
+ return {
37
+ icon: "/claude-code-avatar.png",
38
+ alt: "Claude Code",
39
+ href: "https://claude.ai/settings/usage",
40
+ title: "Check usage on claude.ai",
41
+ };
42
+ }
43
+
44
+ function rateLimitTypeLabel(type) {
45
+ if (!type) return "Usage";
46
+ var labels = {
47
+ "five_hour": "5-hour",
48
+ "seven_day": "7-day",
49
+ "seven_day_opus": "7-day Opus",
50
+ "seven_day_sonnet": "7-day Sonnet",
51
+ "overage": "Overage",
52
+ };
53
+ return labels[type] || type;
54
+ }
55
+
56
+ function startRateLimitCountdown(el, resetsAt, cardEl) {
57
+ if (rateLimitCountdownTimer) clearInterval(rateLimitCountdownTimer);
58
+
59
+ function tick() {
60
+ var remaining = resetsAt - Date.now();
61
+ if (remaining <= 0) {
62
+ clearInterval(rateLimitCountdownTimer);
63
+ rateLimitCountdownTimer = null;
64
+ clearRateLimitIndicator();
65
+ return;
66
+ }
67
+ // Update pill text with countdown
68
+ if (rateLimitIndicatorEl) {
69
+ var pillText = rateLimitIndicatorEl.querySelector(".header-pill-text");
70
+ if (pillText) {
71
+ var mins = Math.floor(remaining / 60000);
72
+ var secs = Math.floor((remaining % 60000) / 1000);
73
+ if (mins >= 60) {
74
+ var hrs = Math.floor(mins / 60);
75
+ mins = mins % 60;
76
+ pillText.textContent = hrs + "h " + mins + "m";
77
+ } else {
78
+ pillText.textContent = mins + "m " + secs + "s";
79
+ }
80
+ }
81
+ }
82
+ }
83
+
84
+ tick();
85
+ rateLimitCountdownTimer = setInterval(tick, 1000);
86
+ }
87
+
88
+ function updateRateLimitIndicator(msg) {
89
+ var statusArea = document.querySelector(".title-bar-content .status");
90
+ if (!statusArea) return;
91
+
92
+ if (!rateLimitIndicatorEl) {
93
+ rateLimitIndicatorEl = document.createElement("span");
94
+ rateLimitIndicatorEl.className = "header-rate-limit-wrap";
95
+ statusArea.insertBefore(rateLimitIndicatorEl, statusArea.firstChild);
96
+ }
97
+
98
+ var isRejected = msg.status === "rejected";
99
+ var pillClass = "header-rate-limit" + (isRejected ? " rejected" : " warning");
100
+ var label = isRejected ? "Rate limited" : "Rate warning";
101
+ rateLimitIndicatorEl.innerHTML =
102
+ '<span class="' + pillClass + '">' +
103
+ iconHtml("alert-triangle") +
104
+ '<span class="header-pill-text">' + label + "</span>" +
105
+ '<a href="https://claude.ai/settings/usage" target="_blank" rel="noopener" class="rate-limit-link">' +
106
+ iconHtml("external-link") +
107
+ "</a>" +
108
+ "</span>";
109
+ refreshIcons();
110
+ }
111
+
112
+ function showRateLimitPopover(text, isRejected) {
113
+ if (!rateLimitIndicatorEl) return;
114
+ // Remove existing popover
115
+ var old = rateLimitIndicatorEl.querySelector(".rate-limit-popover");
116
+ if (old) old.remove();
117
+
118
+ var pop = document.createElement("div");
119
+ pop.className = "rate-limit-popover" + (isRejected ? " rejected" : "");
120
+ pop.textContent = text;
121
+ rateLimitIndicatorEl.appendChild(pop);
122
+
123
+ // Auto-dismiss after 5s
124
+ setTimeout(function () {
125
+ pop.classList.add("fade-out");
126
+ setTimeout(function () { if (pop.parentNode) pop.remove(); }, 300);
127
+ }, 5000);
128
+ }
129
+
130
+ function clearRateLimitIndicator() {
131
+ if (rateLimitIndicatorEl) {
132
+ rateLimitIndicatorEl.remove();
133
+ rateLimitIndicatorEl = null;
134
+ }
135
+ }
136
+
137
+ function formatResetTime(resetsAt) {
138
+ if (!resetsAt) return "";
139
+ var d = new Date(resetsAt);
140
+ var now = new Date();
141
+ var diff = resetsAt - now.getTime();
142
+ if (diff <= 0) return "";
143
+ var hrs = Math.floor(diff / 3600000);
144
+ var mins = Math.floor((diff % 3600000) / 60000);
145
+ if (hrs > 0) return hrs + "h " + mins + "m";
146
+ return mins + "m";
147
+ }
148
+
149
+ function rateLimitTypeShortLabel(type) {
150
+ if (type === "five_hour") return "5h";
151
+ if (type === "seven_day") return "7d";
152
+ if (type === "seven_day_opus") return "7d opus";
153
+ if (type === "seven_day_sonnet") return "7d sonnet";
154
+ return type || "";
155
+ }
156
+
157
+ function tickRateLimitUsage() {
158
+ if (!rateLimitUsageEl) return;
159
+ var parts = [];
160
+ var types = ["five_hour", "seven_day", "seven_day_opus", "seven_day_sonnet"];
161
+ for (var i = 0; i < types.length; i++) {
162
+ var entry = rateLimitResetState[types[i]];
163
+ if (!entry || !entry.resetsAt) continue;
164
+ var timeStr = formatResetTime(entry.resetsAt);
165
+ if (!timeStr) { delete rateLimitResetState[types[i]]; continue; }
166
+ parts.push(rateLimitTypeShortLabel(types[i]) + " resets " + timeStr);
167
+ }
168
+ var newHtml;
169
+ if (parts.length === 0) {
170
+ if (rateLimitTickTimer) { clearInterval(rateLimitTickTimer); rateLimitTickTimer = null; }
171
+ newHtml = iconHtml("activity") + '<span>Check usage</span>' + iconHtml("external-link");
172
+ } else {
173
+ newHtml = iconHtml("activity") + '<span>' + parts.join(" · ") + '</span>' + iconHtml("external-link");
174
+ }
175
+ if (newHtml !== _rateLimitUsageLastHtml) {
176
+ _rateLimitUsageLastHtml = newHtml;
177
+ rateLimitUsageEl.innerHTML = newHtml;
178
+ refreshIcons();
179
+ }
180
+ }
181
+
182
+ // --- Exported functions ---
183
+
184
+ export function initRateLimit() {
185
+ store.subscribe(function(state, prev) {
186
+ if (state.currentVendor !== prev.currentVendor) {
187
+ if (state.currentVendor && state.currentVendor !== "claude") clearScheduleDelay();
188
+ // Re-stamp the usage link href whenever vendor changes so the link always
189
+ // points to the correct provider's usage page after a project switch.
190
+ if (rateLimitUsageEl) {
191
+ var meta = getVendorUsageMeta(state.currentVendor || "claude");
192
+ rateLimitUsageEl.href = meta.href;
193
+ rateLimitUsageEl.title = meta.title;
194
+ }
195
+ }
196
+ });
197
+ }
198
+
199
+ export function handleRateLimitEvent(msg) {
200
+ var isRejected = msg.status === "rejected";
201
+ var typeLabel = rateLimitTypeLabel(msg.rateLimitType);
202
+ var popoverText = "";
203
+
204
+ if (isRejected && msg.resetsAt) {
205
+ // Check if already expired (history replay) — skip popover
206
+ if (msg.resetsAt < Date.now()) {
207
+ updateRateLimitIndicator(msg);
208
+ return;
209
+ }
210
+ popoverText = typeLabel + " limit exceeded";
211
+ updateRateLimitIndicator(msg);
212
+ startRateLimitCountdown(null, msg.resetsAt, null);
213
+ // Track rate limit reset time
214
+ rateLimitResetsAt = msg.resetsAt;
215
+ if (rateLimitResetTimer) clearTimeout(rateLimitResetTimer);
216
+ // Auto-switch input to schedule mode: any message typed will be queued for after reset.
217
+ // Model-aware: a seven_day_sonnet rejection should not schedule when the user is on Opus
218
+ // (and vice-versa), since those messages would send fine against the other model's quota.
219
+ var delayUntilReset = msg.resetsAt - Date.now();
220
+ var currentVendor = store.get('currentVendor') || "claude";
221
+ var shouldSchedule = false;
222
+ if (delayUntilReset > 0 && currentVendor === "claude") {
223
+ if (typeof msg.blocksCurrentModel === "boolean") {
224
+ shouldSchedule = msg.blocksCurrentModel;
225
+ } else {
226
+ // Legacy fallback: server didn't ship the resolved boolean.
227
+ var currentModel = store.get('currentModel') || "";
228
+ if (msg.rateLimitType === "seven_day_sonnet") {
229
+ shouldSchedule = isSonnetModel(currentModel);
230
+ } else if (msg.rateLimitType === "seven_day_opus") {
231
+ shouldSchedule = isOpusModel(currentModel);
232
+ } else {
233
+ // five_hour, seven_day, or unknown vendor-wide types — always schedule
234
+ shouldSchedule = true;
235
+ }
236
+ }
237
+ if (shouldSchedule) {
238
+ setScheduleDelayMs(delayUntilReset + 60000); // +1min buffer after reset
239
+ }
240
+ }
241
+ // Only arm the reset timer when we actually entered schedule mode; otherwise we'd
242
+ // clear unrelated scheduled state on reset.
243
+ if (shouldSchedule) {
244
+ rateLimitResetTimer = setTimeout(function () {
245
+ rateLimitResetsAt = null;
246
+ rateLimitResetTimer = null;
247
+ // Clear schedule mode when rate limit resets
248
+ clearScheduleDelay();
249
+ }, msg.resetsAt - Date.now() + 1000);
250
+ }
251
+ } else {
252
+ var pct = msg.utilization ? Math.round(msg.utilization * 100) : null;
253
+ popoverText = typeLabel + " warning" + (pct ? " (" + pct + "% used)" : "");
254
+ updateRateLimitIndicator(msg);
255
+ }
256
+
257
+ showRateLimitPopover(popoverText, isRejected);
258
+ }
259
+
260
+ export function updateRateLimitUsage(msg) {
261
+ if (msg.rateLimitType && msg.resetsAt) {
262
+ rateLimitResetState[msg.rateLimitType] = { resetsAt: msg.resetsAt, status: msg.status };
263
+ }
264
+
265
+ var topBarActions = document.querySelector("#top-bar .top-bar-actions");
266
+ if (!topBarActions) return;
267
+
268
+ if (!rateLimitUsageEl) {
269
+ rateLimitUsageEl = document.createElement("a");
270
+ rateLimitUsageEl.id = "rate-limit-usage-link";
271
+ rateLimitUsageEl.className = "top-bar-pill pill-dim usage-check-link";
272
+ rateLimitUsageEl.target = "_blank";
273
+ rateLimitUsageEl.rel = "noopener";
274
+ var ref = document.getElementById("skip-perms-pill");
275
+ topBarActions.insertBefore(rateLimitUsageEl, ref);
276
+ }
277
+
278
+ // Build label from available reset times
279
+ var parts = [];
280
+ var types = ["five_hour", "seven_day", "seven_day_opus", "seven_day_sonnet"];
281
+ for (var i = 0; i < types.length; i++) {
282
+ var entry = rateLimitResetState[types[i]];
283
+ if (!entry || !entry.resetsAt) continue;
284
+ var timeStr = formatResetTime(entry.resetsAt);
285
+ if (!timeStr) continue;
286
+ parts.push(rateLimitTypeShortLabel(types[i]) + " resets " + timeStr);
287
+ }
288
+
289
+ var label = parts.length > 0 ? parts.join(" · ") : "Check usage";
290
+ var vendor = store.get('currentVendor') || "claude";
291
+ var meta = getVendorUsageMeta(vendor);
292
+ rateLimitUsageEl.href = meta.href;
293
+ rateLimitUsageEl.title = meta.title;
294
+ var newHtml =
295
+ '<img src="' + meta.icon + '" class="usage-check-vendor-icon" alt="' + meta.alt + '">' +
296
+ '<span>' + label + '</span>' +
297
+ iconHtml("external-link");
298
+ // Skip DOM write + full lucide scan when label hasn't changed.
299
+ // rate_limit_usage fires on every Claude rate-limit event; the label only
300
+ // changes when the reset time crosses a display boundary (~minutes).
301
+ if (newHtml !== _rateLimitUsageLastHtml) {
302
+ _rateLimitUsageLastHtml = newHtml;
303
+ rateLimitUsageEl.innerHTML = newHtml;
304
+ refreshIcons();
305
+ }
306
+
307
+ // Start or stop live countdown tick
308
+ if (parts.length > 0 && !rateLimitTickTimer) {
309
+ rateLimitTickTimer = setInterval(tickRateLimitUsage, 30000);
310
+ } else if (parts.length === 0 && rateLimitTickTimer) {
311
+ clearInterval(rateLimitTickTimer);
312
+ rateLimitTickTimer = null;
313
+ }
314
+ }
315
+
316
+ export function addScheduledMessageBubble(text, resetsAt) {
317
+ removeScheduledMessageBubble();
318
+ var isChannel = document.body.classList.contains("wide-view");
319
+ var wrap = document.createElement("div");
320
+ wrap.className = "msg-user scheduled-msg-wrap";
321
+ wrap.id = "scheduled-msg-bubble";
322
+
323
+ var countdownEl;
324
+ var cancelBtn;
325
+
326
+ if (isChannel) {
327
+ // Channel mode: avatar + header with scheduled badge + message
328
+ var _me = store.get('cachedAllUsers').find(function (u) { return u.id === store.get('myUserId'); });
329
+ if (!_me) { try { _me = JSON.parse(localStorage.getItem("clagentic_my_user") || localStorage.getItem("clay_my_user") || "null"); } catch(e) {} }
330
+ var _myName = document.body.dataset.myDisplayName || (_me && (_me.displayName || _me.username)) || "Me";
331
+
332
+ var avi = document.createElement("img");
333
+ avi.className = "dm-bubble-avatar dm-bubble-avatar-me";
334
+ avi.src = document.body.dataset.myAvatarUrl || userAvatarUrl(_me || { id: store.get('myUserId') }, 36);
335
+ wrap.appendChild(avi);
336
+
337
+ var content = document.createElement("div");
338
+ content.className = "dm-bubble-content";
339
+
340
+ var header = document.createElement("div");
341
+ header.className = "dm-bubble-header";
342
+
343
+ var nameSpan = document.createElement("span");
344
+ nameSpan.className = "dm-bubble-name";
345
+ nameSpan.textContent = _myName;
346
+ header.appendChild(nameSpan);
347
+
348
+ var badge = document.createElement("span");
349
+ badge.className = "scheduled-msg-badge";
350
+ badge.innerHTML = iconHtml("clock");
351
+ countdownEl = document.createElement("span");
352
+ countdownEl.className = "scheduled-msg-countdown";
353
+ badge.appendChild(countdownEl);
354
+ header.appendChild(badge);
355
+
356
+ var actions = document.createElement("span");
357
+ actions.className = "scheduled-msg-actions";
358
+
359
+ var sendNowBtn = document.createElement("button");
360
+ sendNowBtn.className = "scheduled-msg-send-now";
361
+ sendNowBtn.textContent = "Send now";
362
+ sendNowBtn.addEventListener("click", function () {
363
+ var ws = getWs();
364
+ if (ws && ws.readyState === 1) {
365
+ ws.send(JSON.stringify({ type: "send_scheduled_now" }));
366
+ }
367
+ });
368
+ actions.appendChild(sendNowBtn);
369
+
370
+ var sep = document.createElement("span");
371
+ sep.className = "scheduled-msg-sep";
372
+ sep.textContent = "\u00b7";
373
+ actions.appendChild(sep);
374
+
375
+ cancelBtn = document.createElement("button");
376
+ cancelBtn.className = "scheduled-msg-cancel";
377
+ cancelBtn.textContent = "Cancel";
378
+ cancelBtn.addEventListener("click", function () {
379
+ var ws = getWs();
380
+ if (ws && ws.readyState === 1) {
381
+ ws.send(JSON.stringify({ type: "cancel_scheduled_message" }));
382
+ }
383
+ });
384
+ actions.appendChild(cancelBtn);
385
+
386
+ header.appendChild(actions);
387
+
388
+ content.appendChild(header);
389
+
390
+ var bubble = document.createElement("div");
391
+ bubble.className = "bubble scheduled-msg-bubble";
392
+ var textEl = document.createElement("span");
393
+ textEl.textContent = text;
394
+ bubble.appendChild(textEl);
395
+ content.appendChild(bubble);
396
+
397
+ wrap.appendChild(content);
398
+ } else {
399
+ // Bubble mode: original layout
400
+ var bubble = document.createElement("div");
401
+ bubble.className = "bubble scheduled-msg-bubble";
402
+
403
+ var textEl = document.createElement("span");
404
+ textEl.textContent = text;
405
+ bubble.appendChild(textEl);
406
+
407
+ var metaEl = document.createElement("div");
408
+ metaEl.className = "scheduled-msg-meta";
409
+
410
+ var clockIcon = document.createElement("span");
411
+ clockIcon.className = "scheduled-msg-icon";
412
+ clockIcon.innerHTML = iconHtml("clock");
413
+ metaEl.appendChild(clockIcon);
414
+
415
+ countdownEl = document.createElement("span");
416
+ countdownEl.className = "scheduled-msg-countdown";
417
+ metaEl.appendChild(countdownEl);
418
+
419
+ var sendNowBtn2 = document.createElement("button");
420
+ sendNowBtn2.className = "scheduled-msg-send-now";
421
+ sendNowBtn2.textContent = "Send now";
422
+ sendNowBtn2.addEventListener("click", function () {
423
+ var ws = getWs();
424
+ if (ws && ws.readyState === 1) {
425
+ ws.send(JSON.stringify({ type: "send_scheduled_now" }));
426
+ }
427
+ });
428
+ metaEl.appendChild(sendNowBtn2);
429
+
430
+ var sep2 = document.createElement("span");
431
+ sep2.className = "scheduled-msg-sep";
432
+ sep2.textContent = "\u00b7";
433
+ metaEl.appendChild(sep2);
434
+
435
+ cancelBtn = document.createElement("button");
436
+ cancelBtn.className = "scheduled-msg-cancel";
437
+ cancelBtn.textContent = "Cancel";
438
+ cancelBtn.addEventListener("click", function () {
439
+ var ws = getWs();
440
+ if (ws && ws.readyState === 1) {
441
+ ws.send(JSON.stringify({ type: "cancel_scheduled_message" }));
442
+ }
443
+ });
444
+ metaEl.appendChild(cancelBtn);
445
+
446
+ wrap.appendChild(bubble);
447
+ wrap.appendChild(metaEl);
448
+ }
449
+
450
+ addToMessages(wrap);
451
+ scheduledMsgEl = wrap;
452
+ scrollToBottom();
453
+
454
+ // Start countdown
455
+ function updateCountdown() {
456
+ var remaining = resetsAt - Date.now();
457
+ if (remaining <= 0) {
458
+ countdownEl.textContent = "Sending...";
459
+ if (scheduledCountdownTimer) { clearInterval(scheduledCountdownTimer); scheduledCountdownTimer = null; }
460
+ return;
461
+ }
462
+ var hrs = Math.floor(remaining / 3600000);
463
+ var mins = Math.floor((remaining % 3600000) / 60000);
464
+ var secs = Math.floor((remaining % 60000) / 1000);
465
+ var timeStr = "";
466
+ if (hrs > 0) timeStr += hrs + "h ";
467
+ if (mins > 0 || hrs > 0) timeStr += mins + "m ";
468
+ timeStr += secs + "s";
469
+
470
+ var sendDate = new Date(resetsAt);
471
+ var absTime = sendDate.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
472
+ countdownEl.textContent = "Sends at " + absTime + " (" + timeStr + ")";
473
+ }
474
+ updateCountdown();
475
+ scheduledCountdownTimer = setInterval(updateCountdown, 1000);
476
+ }
477
+
478
+ export function removeScheduledMessageBubble() {
479
+ if (scheduledMsgEl) {
480
+ scheduledMsgEl.remove();
481
+ scheduledMsgEl = null;
482
+ }
483
+ if (scheduledCountdownTimer) {
484
+ clearInterval(scheduledCountdownTimer);
485
+ scheduledCountdownTimer = null;
486
+ }
487
+ }
488
+
489
+ export function handleFastModeState(state) {
490
+ var statusArea = document.querySelector(".title-bar-content .status");
491
+ if (!statusArea) return;
492
+
493
+ if (state === "off") {
494
+ if (fastModeIndicatorEl) {
495
+ fastModeIndicatorEl.remove();
496
+ fastModeIndicatorEl = null;
497
+ }
498
+ return;
499
+ }
500
+
501
+ if (!fastModeIndicatorEl) {
502
+ fastModeIndicatorEl = document.createElement("span");
503
+ statusArea.insertBefore(fastModeIndicatorEl, statusArea.firstChild);
504
+ }
505
+
506
+ if (state === "cooldown") {
507
+ fastModeIndicatorEl.className = "header-fast-mode cooldown";
508
+ fastModeIndicatorEl.innerHTML = iconHtml("timer") + '<span class="header-pill-text">Cooldown</span>';
509
+ } else if (state === "on") {
510
+ fastModeIndicatorEl.className = "header-fast-mode active";
511
+ fastModeIndicatorEl.innerHTML = iconHtml("zap") + '<span class="header-pill-text">Fast mode</span>';
512
+ }
513
+ refreshIcons();
514
+ }
515
+
516
+ export function getScheduledMsgEl() { return scheduledMsgEl; }
517
+
518
+ export function resetRateLimitState() {
519
+ clearRateLimitIndicator();
520
+ if (rateLimitCountdownTimer) { clearInterval(rateLimitCountdownTimer); rateLimitCountdownTimer = null; }
521
+ if (rateLimitTickTimer) { clearInterval(rateLimitTickTimer); rateLimitTickTimer = null; }
522
+ if (rateLimitResetTimer) { clearTimeout(rateLimitResetTimer); rateLimitResetTimer = null; }
523
+ rateLimitResetsAt = null;
524
+ rateLimitResetState = {};
525
+ // Remove the usage link element entirely so it is rebuilt fresh for the new
526
+ // project (correct vendor href, correct reset times).
527
+ if (rateLimitUsageEl) { rateLimitUsageEl.remove(); rateLimitUsageEl = null; }
528
+ if (fastModeIndicatorEl) { fastModeIndicatorEl.remove(); fastModeIndicatorEl = null; }
529
+ }