@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.
- package/CREDITS.md +3 -0
- package/LICENSE +21 -0
- package/README.md +250 -0
- package/bin/cli.js +2812 -0
- package/lib/agents-favorites.js +164 -0
- package/lib/agents.js +282 -0
- package/lib/ask-user-mcp-server.js +120 -0
- package/lib/browser-mcp-server.js +484 -0
- package/lib/build-user-env.js +63 -0
- package/lib/certs/fullchain.pem +47 -0
- package/lib/certs/privkey.pem +5 -0
- package/lib/cli-sessions.js +265 -0
- package/lib/codex-defaults.js +18 -0
- package/lib/config.js +332 -0
- package/lib/crisis-safety.js +60 -0
- package/lib/daemon-projects.js +164 -0
- package/lib/daemon.js +1602 -0
- package/lib/dm.js +128 -0
- package/lib/ipc.js +125 -0
- package/lib/ipv4-only.js +39 -0
- package/lib/mcp-local.js +385 -0
- package/lib/notes.js +141 -0
- package/lib/os-users.js +529 -0
- package/lib/pages.js +1325 -0
- package/lib/project-connection.js +302 -0
- package/lib/project-external-trigger.js +236 -0
- package/lib/project-file-watch.js +120 -0
- package/lib/project-filesystem.js +528 -0
- package/lib/project-http.js +753 -0
- package/lib/project-image.js +95 -0
- package/lib/project-knowledge.js +88 -0
- package/lib/project-loop.js +1304 -0
- package/lib/project-mcp.js +374 -0
- package/lib/project-memory.js +39 -0
- package/lib/project-notifications.js +262 -0
- package/lib/project-sessions.js +1581 -0
- package/lib/project-user-mention.js +193 -0
- package/lib/project-user-message.js +628 -0
- package/lib/project.js +1303 -0
- package/lib/public/app.js +1031 -0
- package/lib/public/apple-touch-icon.png +0 -0
- package/lib/public/claude-code-avatar.png +0 -0
- package/lib/public/codex-avatar.png +0 -0
- package/lib/public/console-icon.png +0 -0
- package/lib/public/css/admin.css +736 -0
- package/lib/public/css/agent-picker.css +119 -0
- package/lib/public/css/base.css +235 -0
- package/lib/public/css/channel-layout.css +441 -0
- package/lib/public/css/command-palette.css +559 -0
- package/lib/public/css/debate.css +1609 -0
- package/lib/public/css/diff.css +139 -0
- package/lib/public/css/filebrowser.css +1976 -0
- package/lib/public/css/highlight.css +144 -0
- package/lib/public/css/home-hub.css +545 -0
- package/lib/public/css/icon-strip.css +1856 -0
- package/lib/public/css/input.css +1349 -0
- package/lib/public/css/loop.css +1248 -0
- package/lib/public/css/mention.css +317 -0
- package/lib/public/css/menus.css +857 -0
- package/lib/public/css/messages.css +1833 -0
- package/lib/public/css/mobile-nav.css +1332 -0
- package/lib/public/css/notifications-center.css +296 -0
- package/lib/public/css/overlays.css +1273 -0
- package/lib/public/css/playbook.css +264 -0
- package/lib/public/css/profile.css +397 -0
- package/lib/public/css/rewind.css +671 -0
- package/lib/public/css/scheduler-modal.css +1495 -0
- package/lib/public/css/scheduler.css +1420 -0
- package/lib/public/css/server-settings.css +909 -0
- package/lib/public/css/session-search.css +159 -0
- package/lib/public/css/sidebar.css +2012 -0
- package/lib/public/css/skills.css +789 -0
- package/lib/public/css/sticky-notes.css +854 -0
- package/lib/public/css/stt.css +155 -0
- package/lib/public/css/title-bar.css +674 -0
- package/lib/public/css/tooltip.css +67 -0
- package/lib/public/css/user-settings.css +702 -0
- package/lib/public/emoji-data.json +1 -0
- package/lib/public/favicon-banded-32.png +0 -0
- package/lib/public/favicon-banded.png +0 -0
- package/lib/public/favicon-dark.svg +32 -0
- package/lib/public/favicon.svg +32 -0
- package/lib/public/icon-192.png +0 -0
- package/lib/public/icon-512.png +0 -0
- package/lib/public/icon-banded-76.png +0 -0
- package/lib/public/icon-banded-96.png +0 -0
- package/lib/public/icon-mono.svg +25 -0
- package/lib/public/icons/email/gmail.svg +7 -0
- package/lib/public/icons/email/outlook.svg +35 -0
- package/lib/public/icons/email/yahoo.svg +1 -0
- package/lib/public/index.html +1946 -0
- package/lib/public/manifest.json +27 -0
- package/lib/public/markdown-worker.js +43 -0
- package/lib/public/marked.min.js +6 -0
- package/lib/public/modules/admin.js +954 -0
- package/lib/public/modules/agent-picker.js +429 -0
- package/lib/public/modules/app-connection.js +216 -0
- package/lib/public/modules/app-cursors.js +492 -0
- package/lib/public/modules/app-dm.js +265 -0
- package/lib/public/modules/app-favicon.js +231 -0
- package/lib/public/modules/app-header.js +242 -0
- package/lib/public/modules/app-home-hub.js +432 -0
- package/lib/public/modules/app-loop-ui.js +691 -0
- package/lib/public/modules/app-loop-wizard.js +439 -0
- package/lib/public/modules/app-messages.js +1415 -0
- package/lib/public/modules/app-misc.js +463 -0
- package/lib/public/modules/app-notifications.js +651 -0
- package/lib/public/modules/app-panels.js +1061 -0
- package/lib/public/modules/app-projects.js +875 -0
- package/lib/public/modules/app-rate-limit.js +529 -0
- package/lib/public/modules/app-rendering.js +739 -0
- package/lib/public/modules/app-skills-install.js +285 -0
- package/lib/public/modules/avatar.js +36 -0
- package/lib/public/modules/command-palette.js +491 -0
- package/lib/public/modules/console-logo.js +162 -0
- package/lib/public/modules/context-sources.js +365 -0
- package/lib/public/modules/diff.js +412 -0
- package/lib/public/modules/dom-refs.js +21 -0
- package/lib/public/modules/emoji-picker.js +222 -0
- package/lib/public/modules/events.js +21 -0
- package/lib/public/modules/filebrowser.js +1966 -0
- package/lib/public/modules/fileicons.js +172 -0
- package/lib/public/modules/icons.js +80 -0
- package/lib/public/modules/input.js +1102 -0
- package/lib/public/modules/longpress.js +74 -0
- package/lib/public/modules/markdown.js +486 -0
- package/lib/public/modules/mcp-ui.js +295 -0
- package/lib/public/modules/mention.js +1093 -0
- package/lib/public/modules/notifications.js +613 -0
- package/lib/public/modules/playbook.js +578 -0
- package/lib/public/modules/profile.js +594 -0
- package/lib/public/modules/project-settings.js +849 -0
- package/lib/public/modules/project-switcher.js +375 -0
- package/lib/public/modules/qrcode.js +87 -0
- package/lib/public/modules/rewind.js +381 -0
- package/lib/public/modules/scheduler-config.js +1611 -0
- package/lib/public/modules/scheduler-history.js +131 -0
- package/lib/public/modules/scheduler.js +1689 -0
- package/lib/public/modules/server-settings.js +842 -0
- package/lib/public/modules/session-search.js +456 -0
- package/lib/public/modules/settings-defaults.js +253 -0
- package/lib/public/modules/sidebar-mobile.js +1425 -0
- package/lib/public/modules/sidebar-projects.js +2022 -0
- package/lib/public/modules/sidebar-sessions.js +1548 -0
- package/lib/public/modules/sidebar-users.js +449 -0
- package/lib/public/modules/sidebar.js +348 -0
- package/lib/public/modules/skills.js +793 -0
- package/lib/public/modules/state.js +3 -0
- package/lib/public/modules/sticky-notes.js +1221 -0
- package/lib/public/modules/store.js +34 -0
- package/lib/public/modules/stt.js +266 -0
- package/lib/public/modules/terminal.js +987 -0
- package/lib/public/modules/theme.js +812 -0
- package/lib/public/modules/tool-palette.js +562 -0
- package/lib/public/modules/tools.js +2285 -0
- package/lib/public/modules/tooltip.js +150 -0
- package/lib/public/modules/user-settings.js +544 -0
- package/lib/public/modules/utils.js +56 -0
- package/lib/public/modules/ws-ref.js +26 -0
- package/lib/public/style.css +34 -0
- package/lib/public/sw.js +159 -0
- package/lib/push.js +149 -0
- package/lib/scheduler.js +471 -0
- package/lib/sdk-bridge.js +1792 -0
- package/lib/sdk-message-processor.js +740 -0
- package/lib/sdk-message-queue.js +42 -0
- package/lib/sdk-skill-discovery.js +131 -0
- package/lib/sdk-worker.js +677 -0
- package/lib/server-admin.js +712 -0
- package/lib/server-auth.js +737 -0
- package/lib/server-dm.js +170 -0
- package/lib/server-palette.js +104 -0
- package/lib/server-settings.js +553 -0
- package/lib/server-skills.js +280 -0
- package/lib/server.js +1390 -0
- package/lib/session-search.js +693 -0
- package/lib/sessions.js +946 -0
- package/lib/smtp.js +221 -0
- package/lib/terminal-manager.js +219 -0
- package/lib/terminal.js +39 -0
- package/lib/themes/ayu-light.json +9 -0
- package/lib/themes/catppuccin-latte.json +9 -0
- package/lib/themes/catppuccin-mocha.json +9 -0
- package/lib/themes/clagentic-dark.json +10 -0
- package/lib/themes/clagentic-light.json +10 -0
- package/lib/themes/dracula.json +9 -0
- package/lib/themes/everforest-light.json +9 -0
- package/lib/themes/everforest.json +9 -0
- package/lib/themes/github-light.json +9 -0
- package/lib/themes/gruvbox-dark.json +9 -0
- package/lib/themes/gruvbox-light.json +9 -0
- package/lib/themes/monokai.json +9 -0
- package/lib/themes/nord-light.json +9 -0
- package/lib/themes/nord.json +9 -0
- package/lib/themes/one-dark.json +9 -0
- package/lib/themes/one-light.json +9 -0
- package/lib/themes/rose-pine-dawn.json +9 -0
- package/lib/themes/rose-pine.json +9 -0
- package/lib/themes/solarized-dark.json +9 -0
- package/lib/themes/solarized-light.json +9 -0
- package/lib/themes/tokyo-night-light.json +9 -0
- package/lib/themes/tokyo-night.json +9 -0
- package/lib/updater.js +124 -0
- package/lib/user-presence.js +92 -0
- package/lib/users-auth.js +146 -0
- package/lib/users-permissions.js +118 -0
- package/lib/users-preferences.js +254 -0
- package/lib/users.js +465 -0
- package/lib/utils.js +64 -0
- package/lib/worktree.js +134 -0
- package/lib/ws-schema.js +464 -0
- package/lib/yoke/adapters/claude-worker.js +759 -0
- package/lib/yoke/adapters/claude.js +1599 -0
- package/lib/yoke/adapters/codex.js +1559 -0
- package/lib/yoke/codex-app-server.js +352 -0
- package/lib/yoke/index.js +319 -0
- package/lib/yoke/instructions.js +61 -0
- package/lib/yoke/interface.js +98 -0
- package/lib/yoke/mcp-bridge-server.js +302 -0
- package/lib/yoke/package.json +7 -0
- package/package.json +72 -0
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
var usersModule = require("./users");
|
|
2
|
+
|
|
3
|
+
function attachMessageProcessor(ctx) {
|
|
4
|
+
var sm = ctx.sm;
|
|
5
|
+
var send = ctx.send;
|
|
6
|
+
var slug = ctx.slug;
|
|
7
|
+
var pushModule = ctx.pushModule;
|
|
8
|
+
var getNotificationsModule = ctx.getNotificationsModule || function () { return null; };
|
|
9
|
+
var getSDK = ctx.getSDK;
|
|
10
|
+
var adapter = ctx.adapter;
|
|
11
|
+
var cwd = ctx.cwd;
|
|
12
|
+
var onProcessingChanged = ctx.onProcessingChanged;
|
|
13
|
+
var onTurnDone = ctx.onTurnDone;
|
|
14
|
+
var onAutoTitle = ctx.onAutoTitle;
|
|
15
|
+
var opts = ctx.opts;
|
|
16
|
+
var discoverSkillDirs = ctx.discoverSkillDirs;
|
|
17
|
+
var mergeSkills = ctx.mergeSkills;
|
|
18
|
+
|
|
19
|
+
var AUTO_TITLE_TURN_THRESHOLD = 2;
|
|
20
|
+
|
|
21
|
+
function sendAndRecord(session, obj) {
|
|
22
|
+
sm.sendAndRecord(session, obj);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sendToSession(session, obj) {
|
|
26
|
+
sm.sendToSession(session, obj);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getModelsForVendor(vendor) {
|
|
30
|
+
if (vendor && sm.modelsByVendor && sm.modelsByVendor[vendor]) return sm.modelsByVendor[vendor];
|
|
31
|
+
return sm.availableModels || [];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function toolActivityTextForSubagent(name, input) {
|
|
35
|
+
if (name === "Bash" && input && input.description) return input.description;
|
|
36
|
+
if (name === "Read" && input && input.file_path) return "Reading " + input.file_path.split("/").pop();
|
|
37
|
+
if (name === "Edit" && input && input.file_path) return "Editing " + input.file_path.split("/").pop();
|
|
38
|
+
if (name === "Write" && input && input.file_path) return "Writing " + input.file_path.split("/").pop();
|
|
39
|
+
if (name === "Grep" && input && input.pattern) return "Searching for " + input.pattern;
|
|
40
|
+
if (name === "Glob" && input && input.pattern) return "Finding " + input.pattern;
|
|
41
|
+
if (name === "WebSearch" && input && input.query) return "Searching: " + input.query;
|
|
42
|
+
if (name === "WebFetch") return "Fetching URL...";
|
|
43
|
+
if (name === "Task" && input && input.description) return input.description;
|
|
44
|
+
return "Running " + name + "...";
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
// Maps a rate-limit bucket id to a predicate on the model string.
|
|
49
|
+
// null means "applies to all models in the vendor" (e.g. five_hour, seven_day).
|
|
50
|
+
var BUCKET_MODEL_PREDICATES = {
|
|
51
|
+
"five_hour": null,
|
|
52
|
+
"seven_day": null,
|
|
53
|
+
"seven_day_sonnet": function (m) { return /sonnet/i.test(m || ""); },
|
|
54
|
+
"seven_day_opus": function (m) { return /opus/i.test(m || ""); },
|
|
55
|
+
// Future: "seven_day_haiku": function (m) { return /haiku/i.test(m || ""); }
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function bucketBlocksModel(bucketType, model) {
|
|
59
|
+
if (!bucketType) return false;
|
|
60
|
+
if (!(bucketType in BUCKET_MODEL_PREDICATES)) {
|
|
61
|
+
// Unknown bucket — be conservative and treat as blocking.
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
var pred = BUCKET_MODEL_PREDICATES[bucketType];
|
|
65
|
+
if (pred === null) return true; // vendor-wide
|
|
66
|
+
return pred(model);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function processSubagentMessage(session, parsed) {
|
|
70
|
+
var parentId = parsed.parentToolUseId;
|
|
71
|
+
var content = parsed.content;
|
|
72
|
+
if (!Array.isArray(content)) return;
|
|
73
|
+
|
|
74
|
+
if (parsed.messageRole === "assistant") {
|
|
75
|
+
// Extract tool_use blocks from sub-agent assistant messages
|
|
76
|
+
for (var i = 0; i < content.length; i++) {
|
|
77
|
+
var block = content[i];
|
|
78
|
+
if (block.type === "tool_use") {
|
|
79
|
+
var activityText = toolActivityTextForSubagent(block.name, block.input);
|
|
80
|
+
sendAndRecord(session, {
|
|
81
|
+
type: "subagent_tool",
|
|
82
|
+
parentToolId: parentId,
|
|
83
|
+
toolName: block.name,
|
|
84
|
+
toolId: block.id,
|
|
85
|
+
text: activityText,
|
|
86
|
+
});
|
|
87
|
+
} else if (block.type === "thinking") {
|
|
88
|
+
sendAndRecord(session, {
|
|
89
|
+
type: "subagent_activity",
|
|
90
|
+
parentToolId: parentId,
|
|
91
|
+
text: "Thinking...",
|
|
92
|
+
});
|
|
93
|
+
} else if (block.type === "text" && block.text) {
|
|
94
|
+
sendAndRecord(session, {
|
|
95
|
+
type: "subagent_activity",
|
|
96
|
+
parentToolId: parentId,
|
|
97
|
+
text: "Writing response...",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// user messages with parentToolUseId contain tool_results -- skip silently
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function processSDKMessage(session, parsed) {
|
|
106
|
+
// Timing: log key SDK milestones relative to query start
|
|
107
|
+
if (session._queryStartTs) {
|
|
108
|
+
var _elapsed = Date.now() - session._queryStartTs;
|
|
109
|
+
if (parsed.yokeType === "init") {
|
|
110
|
+
console.log("[PERF] processSDKMessage: system/init +" + _elapsed + "ms");
|
|
111
|
+
}
|
|
112
|
+
if (parsed.yokeType === "turn_start") {
|
|
113
|
+
console.log("[PERF] processSDKMessage: message_start (API response begun) +" + _elapsed + "ms");
|
|
114
|
+
}
|
|
115
|
+
if ((parsed.yokeType === "text_delta" || parsed.yokeType === "tool_input_delta" || parsed.yokeType === "thinking_delta") && !session._firstTextLogged) {
|
|
116
|
+
session._firstTextLogged = true;
|
|
117
|
+
console.log("[PERF] processSDKMessage: FIRST content_block_delta (visible text) +" + _elapsed + "ms");
|
|
118
|
+
}
|
|
119
|
+
if (parsed.yokeType === "result") {
|
|
120
|
+
console.log("[PERF] processSDKMessage: result +" + _elapsed + "ms");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Extract session_id from any message that carries it
|
|
125
|
+
if (parsed.sessionId && !session.cliSessionId) {
|
|
126
|
+
session.cliSessionId = parsed.sessionId;
|
|
127
|
+
sm.saveSessionFile(session);
|
|
128
|
+
sendAndRecord(session, { type: "session_id", cliSessionId: session.cliSessionId });
|
|
129
|
+
} else if (parsed.sessionId) {
|
|
130
|
+
session.cliSessionId = parsed.sessionId;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Capture message UUIDs for rewind support
|
|
134
|
+
if (parsed.uuid) {
|
|
135
|
+
if (parsed.messageType === "user" && !parsed.parentToolUseId) {
|
|
136
|
+
session.messageUUIDs.push({ uuid: parsed.uuid, type: "user", historyIndex: session.history.length });
|
|
137
|
+
sendAndRecord(session, { type: "message_uuid", uuid: parsed.uuid, messageType: "user" });
|
|
138
|
+
} else if (parsed.messageType === "assistant") {
|
|
139
|
+
session.messageUUIDs.push({ uuid: parsed.uuid, type: "assistant", historyIndex: session.history.length });
|
|
140
|
+
sendAndRecord(session, { type: "message_uuid", uuid: parsed.uuid, messageType: "assistant" });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Cache slash_commands and model from CLI init message
|
|
145
|
+
if (parsed.yokeType === "init") {
|
|
146
|
+
var fsSkills = discoverSkillDirs();
|
|
147
|
+
sm.skillNames = mergeSkills(parsed.skills, fsSkills);
|
|
148
|
+
if (parsed.slashCommands) {
|
|
149
|
+
// Union: SDK slash_commands + merged skills (deduplicated)
|
|
150
|
+
var seen = new Set();
|
|
151
|
+
var combined = [];
|
|
152
|
+
var all = parsed.slashCommands.concat(Array.from(sm.skillNames));
|
|
153
|
+
for (var k = 0; k < all.length; k++) {
|
|
154
|
+
if (!seen.has(all[k])) {
|
|
155
|
+
seen.add(all[k]);
|
|
156
|
+
combined.push(all[k]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
sm.slashCommands = combined;
|
|
160
|
+
send({ type: "slash_commands", commands: sm.slashCommands });
|
|
161
|
+
}
|
|
162
|
+
if (parsed.model) {
|
|
163
|
+
sm.currentModel = sm.currentModel || sm._savedDefaultModel || parsed.model;
|
|
164
|
+
var initVendor = session.vendor || (adapter && adapter.vendor) || "claude";
|
|
165
|
+
send({
|
|
166
|
+
type: "model_info",
|
|
167
|
+
model: sm.currentModel,
|
|
168
|
+
models: getModelsForVendor(initVendor),
|
|
169
|
+
vendor: initVendor,
|
|
170
|
+
availableVendors: sm.availableVendors || [],
|
|
171
|
+
installedVendors: sm.installedVendors || [],
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (parsed.fastModeState) {
|
|
175
|
+
sendAndRecord(session, { type: "fast_mode_state", state: parsed.fastModeState });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (parsed.yokeType === "turn_start") {
|
|
180
|
+
if (parsed.inputTokens) {
|
|
181
|
+
session.lastStreamInputTokens = parsed.inputTokens;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
} else if (parsed.yokeType === "tool_start" || parsed.yokeType === "thinking_start" || parsed.yokeType === "text_start") {
|
|
185
|
+
var idx = parsed.blockId;
|
|
186
|
+
|
|
187
|
+
if (parsed.yokeType === "tool_start") {
|
|
188
|
+
session.blocks[idx] = { type: "tool_use", id: parsed.toolId, name: parsed.toolName, inputJson: "" };
|
|
189
|
+
sendAndRecord(session, { type: "tool_start", id: parsed.toolId, name: parsed.toolName });
|
|
190
|
+
} else if (parsed.yokeType === "thinking_start") {
|
|
191
|
+
session.blocks[idx] = { type: "thinking", thinkingText: "", startTime: Date.now() };
|
|
192
|
+
sendAndRecord(session, { type: "thinking_start" });
|
|
193
|
+
} else if (parsed.yokeType === "text_start") {
|
|
194
|
+
session.blocks[idx] = { type: "text" };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
} else if (parsed.yokeType === "text_delta" || parsed.yokeType === "tool_input_delta" || parsed.yokeType === "thinking_delta") {
|
|
198
|
+
var idx = parsed.blockId;
|
|
199
|
+
|
|
200
|
+
if (parsed.yokeType === "text_delta" && typeof parsed.text === "string") {
|
|
201
|
+
session.streamedText = true;
|
|
202
|
+
if (session.responsePreview.length < 200) {
|
|
203
|
+
session.responsePreview += parsed.text;
|
|
204
|
+
}
|
|
205
|
+
// Accumulate text for mate DM response
|
|
206
|
+
if (typeof session._mateDmResponseText === "string") {
|
|
207
|
+
session._mateDmResponseText += parsed.text;
|
|
208
|
+
}
|
|
209
|
+
sendAndRecord(session, { type: "delta", text: parsed.text });
|
|
210
|
+
} else if (parsed.yokeType === "tool_input_delta" && session.blocks[idx]) {
|
|
211
|
+
session.blocks[idx].inputJson += parsed.partialJson;
|
|
212
|
+
} else if (parsed.yokeType === "thinking_delta" && session.blocks[idx]) {
|
|
213
|
+
session.blocks[idx].thinkingText += parsed.text;
|
|
214
|
+
sendAndRecord(session, { type: "thinking_delta", text: parsed.text });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
} else if (parsed.yokeType === "tool_executing") {
|
|
218
|
+
sendAndRecord(session, {
|
|
219
|
+
type: "tool_executing",
|
|
220
|
+
id: parsed.toolId,
|
|
221
|
+
name: parsed.toolName,
|
|
222
|
+
input: parsed.input || {},
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
} else if (parsed.yokeType === "tool_result") {
|
|
226
|
+
sendAndRecord(session, {
|
|
227
|
+
type: "tool_result",
|
|
228
|
+
id: parsed.toolId,
|
|
229
|
+
content: parsed.content || "",
|
|
230
|
+
is_error: !!parsed.isError,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
} else if (parsed.yokeType === "plan_updated") {
|
|
234
|
+
var todos = Array.isArray(parsed.plan) ? parsed.plan.map(function(step, idx) {
|
|
235
|
+
var todo = {
|
|
236
|
+
id: String(idx + 1),
|
|
237
|
+
content: step.step || "",
|
|
238
|
+
status: step.status || "pending",
|
|
239
|
+
};
|
|
240
|
+
if (todo.status === "in_progress" && parsed.explanation) {
|
|
241
|
+
todo.activeForm = parsed.explanation;
|
|
242
|
+
}
|
|
243
|
+
return todo;
|
|
244
|
+
}) : [];
|
|
245
|
+
sendAndRecord(session, {
|
|
246
|
+
type: "tool_executing",
|
|
247
|
+
id: parsed.turnId || "codex-plan",
|
|
248
|
+
name: "TodoWrite",
|
|
249
|
+
input: {
|
|
250
|
+
todos: todos,
|
|
251
|
+
meta: {
|
|
252
|
+
variant: "plan",
|
|
253
|
+
title: parsed.title || "Plan",
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
} else if (parsed.yokeType === "plan_content") {
|
|
259
|
+
sendAndRecord(session, {
|
|
260
|
+
type: "plan_content",
|
|
261
|
+
content: parsed.content || "",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
} else if (parsed.yokeType === "block_stop") {
|
|
265
|
+
var idx = parsed.blockId;
|
|
266
|
+
var block = session.blocks[idx];
|
|
267
|
+
|
|
268
|
+
if (block && block.type === "tool_use") {
|
|
269
|
+
var input = {};
|
|
270
|
+
try { input = JSON.parse(block.inputJson); } catch (e) {}
|
|
271
|
+
sendAndRecord(session, { type: "tool_executing", id: block.id, name: block.name, input: input });
|
|
272
|
+
|
|
273
|
+
// Track active Task tools for sub-agent done detection
|
|
274
|
+
if (block.name === "Task") {
|
|
275
|
+
if (!session.activeTaskToolIds) session.activeTaskToolIds = {};
|
|
276
|
+
session.activeTaskToolIds[block.id] = true;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (pushModule && block.name === "AskUserQuestion" && input.questions) {
|
|
280
|
+
var q = input.questions[0];
|
|
281
|
+
pushModule.sendPush({
|
|
282
|
+
type: "ask_user",
|
|
283
|
+
slug: slug,
|
|
284
|
+
title: "Claude has a question",
|
|
285
|
+
body: q ? q.question : "Waiting for your response",
|
|
286
|
+
tag: "claude-ask",
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
} else if (block && block.type === "thinking") {
|
|
290
|
+
var duration = block.startTime ? (Date.now() - block.startTime) / 1000 : 0;
|
|
291
|
+
sendAndRecord(session, { type: "thinking_stop", duration: duration });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
delete session.blocks[idx];
|
|
295
|
+
|
|
296
|
+
} else if (parsed.yokeType === "subagent_message") {
|
|
297
|
+
// Sub-agent messages: extract tool_use blocks for activity display
|
|
298
|
+
processSubagentMessage(session, parsed);
|
|
299
|
+
|
|
300
|
+
} else if (parsed.yokeType === "message") {
|
|
301
|
+
var content = parsed.content;
|
|
302
|
+
|
|
303
|
+
// Fallback: if assistant text wasn't streamed via deltas, send it now
|
|
304
|
+
if (parsed.messageRole === "assistant" && !session.streamedText && Array.isArray(content)) {
|
|
305
|
+
var assistantText = content
|
|
306
|
+
.filter(function(c) { return c.type === "text"; })
|
|
307
|
+
.map(function(c) { return c.text; })
|
|
308
|
+
.join("");
|
|
309
|
+
if (assistantText) {
|
|
310
|
+
if (session.responsePreview.length < 200) {
|
|
311
|
+
session.responsePreview += assistantText;
|
|
312
|
+
}
|
|
313
|
+
sendAndRecord(session, { type: "delta", text: assistantText });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Check for local slash command output in user messages
|
|
318
|
+
if (parsed.messageRole === "user") {
|
|
319
|
+
var fullText = "";
|
|
320
|
+
if (typeof content === "string") {
|
|
321
|
+
fullText = content;
|
|
322
|
+
} else if (Array.isArray(content)) {
|
|
323
|
+
fullText = content
|
|
324
|
+
.filter(function(c) { return c.type === "text"; })
|
|
325
|
+
.map(function(c) { return c.text; })
|
|
326
|
+
.join("\n");
|
|
327
|
+
}
|
|
328
|
+
if (fullText.indexOf("local-command-stdout") !== -1) {
|
|
329
|
+
var m = fullText.match(/<local-command-stdout>([\s\S]*?)<\/local-command-stdout>/);
|
|
330
|
+
if (m) {
|
|
331
|
+
sendAndRecord(session, { type: "slash_command_result", text: m[1].trim() });
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (Array.isArray(content)) {
|
|
337
|
+
for (var i = 0; i < content.length; i++) {
|
|
338
|
+
var block = content[i];
|
|
339
|
+
if (block.type === "tool_result" && !session.sentToolResults[block.tool_use_id]) {
|
|
340
|
+
// Clear active Task tool when its result arrives
|
|
341
|
+
if (session.activeTaskToolIds && session.activeTaskToolIds[block.tool_use_id]) {
|
|
342
|
+
sendAndRecord(session, {
|
|
343
|
+
type: "subagent_done",
|
|
344
|
+
parentToolId: block.tool_use_id,
|
|
345
|
+
});
|
|
346
|
+
delete session.activeTaskToolIds[block.tool_use_id];
|
|
347
|
+
}
|
|
348
|
+
var resultText = "";
|
|
349
|
+
var resultImages = [];
|
|
350
|
+
if (typeof block.content === "string") {
|
|
351
|
+
resultText = block.content;
|
|
352
|
+
} else if (Array.isArray(block.content)) {
|
|
353
|
+
resultText = block.content
|
|
354
|
+
.filter(function(c) { return c.type === "text"; })
|
|
355
|
+
.map(function(c) { return c.text; })
|
|
356
|
+
.join("\n");
|
|
357
|
+
for (var ri = 0; ri < block.content.length; ri++) {
|
|
358
|
+
var rc = block.content[ri];
|
|
359
|
+
if (rc.type === "image" && rc.source) {
|
|
360
|
+
resultImages.push({
|
|
361
|
+
mediaType: rc.source.media_type,
|
|
362
|
+
data: rc.source.data,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
session.sentToolResults[block.tool_use_id] = true;
|
|
368
|
+
var toolResultMsg = {
|
|
369
|
+
type: "tool_result",
|
|
370
|
+
id: block.tool_use_id,
|
|
371
|
+
content: resultText,
|
|
372
|
+
is_error: block.is_error || false,
|
|
373
|
+
};
|
|
374
|
+
if (resultImages.length > 0) toolResultMsg.images = resultImages;
|
|
375
|
+
sendAndRecord(session, toolResultMsg);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
} else if (parsed.yokeType === "result") {
|
|
381
|
+
session.blocks = {};
|
|
382
|
+
session.sentToolResults = {};
|
|
383
|
+
session.pendingPermissions = {};
|
|
384
|
+
session.pendingElicitations = {};
|
|
385
|
+
// Record ask_user_answered for any leftover pending questions so replay pairs correctly.
|
|
386
|
+
// EXCEPTION: "mcp" mode entries are stateless — the tool returned immediately and the
|
|
387
|
+
// turn is expected to end while the card is still awaiting the user's answer. Those
|
|
388
|
+
// entries must survive across turns so the eventual ask_user_response can inject the
|
|
389
|
+
// answer as the next user message. Only blocking modes (Claude canUseTool) get closed.
|
|
390
|
+
var leftoverAskIds = Object.keys(session.pendingAskUser);
|
|
391
|
+
var keptAskUser = {};
|
|
392
|
+
for (var lai = 0; lai < leftoverAskIds.length; lai++) {
|
|
393
|
+
var lid = leftoverAskIds[lai];
|
|
394
|
+
var lentry = session.pendingAskUser[lid];
|
|
395
|
+
if (lentry && lentry.mode === "mcp") {
|
|
396
|
+
keptAskUser[lid] = lentry;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
sendAndRecord(session, { type: "ask_user_answered", toolId: lid });
|
|
400
|
+
}
|
|
401
|
+
session.pendingAskUser = keptAskUser;
|
|
402
|
+
session.activeTaskToolIds = {};
|
|
403
|
+
session.taskIdMap = {};
|
|
404
|
+
// Only clear rateLimitResetsAt on genuine success (non-zero cost).
|
|
405
|
+
// When rate-limited, the SDK sends result with zero cost right after
|
|
406
|
+
// rate_limit_event; clearing here would prevent auto-continue scheduling.
|
|
407
|
+
if (parsed.cost && parsed.cost > 0) {
|
|
408
|
+
session.rateLimitResetsAt = null;
|
|
409
|
+
}
|
|
410
|
+
console.log("[sdk-bridge] result handler: session " + session.localId + " cost=" + parsed.cost + " rateLimitResetsAt=" + session.rateLimitResetsAt);
|
|
411
|
+
|
|
412
|
+
// Handle SDK execution errors: show the error to the user instead of
|
|
413
|
+
// silently swallowing it. These have subtype "error_during_execution".
|
|
414
|
+
if (parsed.subtype === "error_during_execution") {
|
|
415
|
+
var execErrors = parsed.errors || [];
|
|
416
|
+
var execError = execErrors.length > 0
|
|
417
|
+
? execErrors.join("; ")
|
|
418
|
+
: "Unknown SDK error";
|
|
419
|
+
if (parsed.terminalReason) execError += " (reason: " + parsed.terminalReason + ")";
|
|
420
|
+
console.error("[sdk-bridge] Execution error for session " + session.localId + ": " + execError);
|
|
421
|
+
session.isProcessing = false;
|
|
422
|
+
onProcessingChanged();
|
|
423
|
+
sendAndRecord(session, { type: "error", text: "Claude error: " + execError });
|
|
424
|
+
sendAndRecord(session, { type: "done", code: 1 });
|
|
425
|
+
sm.broadcastSessionList();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
session.isProcessing = false;
|
|
430
|
+
onProcessingChanged();
|
|
431
|
+
// Detect "Not logged in" scenario early for the check below
|
|
432
|
+
var previewTrimmed = (session.responsePreview || "").trim();
|
|
433
|
+
var isZeroCost = !parsed.cost || parsed.cost === 0;
|
|
434
|
+
var isLoginPrompt = isZeroCost && previewTrimmed.length < 100
|
|
435
|
+
&& /not logged in/i.test(previewTrimmed) && /\/login/i.test(previewTrimmed);
|
|
436
|
+
// Fetch rich context usage breakdown (fire-and-forget, non-blocking)
|
|
437
|
+
if (session.queryInstance && typeof session.queryInstance.getContextUsage === "function") {
|
|
438
|
+
session.queryInstance.getContextUsage().then(function(ctxUsage) {
|
|
439
|
+
session.lastContextUsage = ctxUsage;
|
|
440
|
+
sendToSession(session, { type: "context_usage", data: ctxUsage });
|
|
441
|
+
}).catch(function(e) {
|
|
442
|
+
console.error("[sdk-bridge] getContextUsage failed (non-fatal):", e.message || e);
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
var lastStreamInput = session.lastStreamInputTokens || parsed.lastStreamInputTokens || null;
|
|
446
|
+
session.lastStreamInputTokens = null;
|
|
447
|
+
sendAndRecord(session, {
|
|
448
|
+
type: "result",
|
|
449
|
+
cost: parsed.cost,
|
|
450
|
+
duration: parsed.duration,
|
|
451
|
+
usage: parsed.usage || null,
|
|
452
|
+
modelUsage: parsed.modelUsage || null,
|
|
453
|
+
sessionId: parsed.sessionId,
|
|
454
|
+
lastStreamInputTokens: lastStreamInput,
|
|
455
|
+
});
|
|
456
|
+
if (parsed.fastModeState) {
|
|
457
|
+
sendAndRecord(session, { type: "fast_mode_state", state: parsed.fastModeState });
|
|
458
|
+
}
|
|
459
|
+
// Detect "Not logged in / Please run /login" from SDK.
|
|
460
|
+
// This is a short canned response with zero cost, not actual AI output.
|
|
461
|
+
if (isLoginPrompt) {
|
|
462
|
+
var authUser = session.ownerId ? usersModule.findUserById(session.ownerId) : null;
|
|
463
|
+
var authLinuxUser = authUser && authUser.linuxUser ? authUser.linuxUser : null;
|
|
464
|
+
var canAutoLogin = !usersModule.isMultiUser()
|
|
465
|
+
|| !!authLinuxUser
|
|
466
|
+
|| (authUser && authUser.role === "admin");
|
|
467
|
+
var authTitle = session.vendor === "codex" ? "Codex is not logged in." : "Claude Code is not logged in.";
|
|
468
|
+
var loginCommand = session.vendor === "codex"
|
|
469
|
+
? "codex login --device-auth"
|
|
470
|
+
: "claude login";
|
|
471
|
+
var _nmLogin = getNotificationsModule();
|
|
472
|
+
var authMsg = {
|
|
473
|
+
type: "auth_required",
|
|
474
|
+
text: authTitle,
|
|
475
|
+
vendor: session.vendor || "claude",
|
|
476
|
+
loginCommand: loginCommand,
|
|
477
|
+
linuxUser: authLinuxUser,
|
|
478
|
+
canAutoLogin: canAutoLogin,
|
|
479
|
+
};
|
|
480
|
+
sendAndRecord(session, authMsg);
|
|
481
|
+
if (_nmLogin) {
|
|
482
|
+
_nmLogin.notify("auth_required", {
|
|
483
|
+
title: authTitle,
|
|
484
|
+
body: "Open a terminal, then click the URL and follow the instructions.",
|
|
485
|
+
slug: slug,
|
|
486
|
+
sessionId: session.localId,
|
|
487
|
+
ownerId: session.ownerId || null,
|
|
488
|
+
vendor: session.vendor || "claude",
|
|
489
|
+
loginCommand: loginCommand,
|
|
490
|
+
linuxUser: authLinuxUser,
|
|
491
|
+
canAutoLogin: canAutoLogin,
|
|
492
|
+
});
|
|
493
|
+
}
|
|
494
|
+
// Reset CLI session so next query starts fresh with new auth
|
|
495
|
+
session.cliSessionId = null;
|
|
496
|
+
}
|
|
497
|
+
sendAndRecord(session, { type: "done", code: 0 });
|
|
498
|
+
var _donePreviewText = (session.responsePreview || "").replace(/\s+/g, " ").trim();
|
|
499
|
+
if (_donePreviewText.length > 140) _donePreviewText = _donePreviewText.substring(0, 140) + "...";
|
|
500
|
+
var _doneTitle = session.title || "Claude";
|
|
501
|
+
|
|
502
|
+
if (pushModule) {
|
|
503
|
+
pushModule.sendPush({
|
|
504
|
+
type: "done",
|
|
505
|
+
slug: slug,
|
|
506
|
+
title: _doneTitle,
|
|
507
|
+
body: _donePreviewText || "Response ready",
|
|
508
|
+
tag: "claude-done",
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
var _nm = getNotificationsModule();
|
|
513
|
+
if (_nm && !session.loop) {
|
|
514
|
+
_nm.notify("response_done", {
|
|
515
|
+
title: _doneTitle,
|
|
516
|
+
preview: _donePreviewText,
|
|
517
|
+
slug: slug,
|
|
518
|
+
sessionId: session.localId,
|
|
519
|
+
mateId: null,
|
|
520
|
+
ownerId: session.ownerId || null,
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
// Reset for next turn in the same query
|
|
524
|
+
session.lastActivityAt = Date.now();
|
|
525
|
+
session.turnCount = (session.turnCount || 0) + 1;
|
|
526
|
+
var donePreview = session.responsePreview || "";
|
|
527
|
+
session.responsePreview = "";
|
|
528
|
+
session.streamedText = false;
|
|
529
|
+
sm.broadcastSessionList();
|
|
530
|
+
|
|
531
|
+
// Auto-generate title after N turns (skip if loop or already auto-generated)
|
|
532
|
+
if (session.turnCount === AUTO_TITLE_TURN_THRESHOLD
|
|
533
|
+
&& !session.titleAutoGenerated
|
|
534
|
+
&& !session.titleManuallySet
|
|
535
|
+
&& !session.loop
|
|
536
|
+
&& onAutoTitle) {
|
|
537
|
+
try { onAutoTitle(session); } catch (e) {
|
|
538
|
+
console.error("[auto-title] onAutoTitle threw:", e.message || e);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (onTurnDone) {
|
|
543
|
+
try { onTurnDone(session, donePreview); } catch (e) {}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
} else if (parsed.yokeType === "status") {
|
|
547
|
+
if (parsed.status === "compacting") {
|
|
548
|
+
sendAndRecord(session, { type: "compacting", active: true });
|
|
549
|
+
} else if (session.compacting) {
|
|
550
|
+
sendAndRecord(session, { type: "compacting", active: false });
|
|
551
|
+
}
|
|
552
|
+
session.compacting = parsed.status === "compacting";
|
|
553
|
+
|
|
554
|
+
} else if (parsed.yokeType === "task_started") {
|
|
555
|
+
var parentId = parsed.parentToolId;
|
|
556
|
+
if (parentId) {
|
|
557
|
+
if (!session.taskIdMap) session.taskIdMap = {};
|
|
558
|
+
session.taskIdMap[parentId] = parsed.taskId;
|
|
559
|
+
sendAndRecord(session, {
|
|
560
|
+
type: "task_started",
|
|
561
|
+
parentToolId: parentId,
|
|
562
|
+
taskId: parsed.taskId,
|
|
563
|
+
description: parsed.description || "",
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
} else if (parsed.yokeType === "task_progress") {
|
|
568
|
+
var parentId = parsed.parentToolId;
|
|
569
|
+
if (parentId) {
|
|
570
|
+
sendAndRecord(session, {
|
|
571
|
+
type: "task_progress",
|
|
572
|
+
parentToolId: parentId,
|
|
573
|
+
taskId: parsed.taskId,
|
|
574
|
+
usage: parsed.usage || null,
|
|
575
|
+
lastToolName: parsed.lastToolName || null,
|
|
576
|
+
description: parsed.description || "",
|
|
577
|
+
summary: parsed.summary || null,
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
} else if (parsed.yokeType === "task_updated") {
|
|
582
|
+
// Live task state patches (status, description, error, backgrounded)
|
|
583
|
+
var taskId = parsed.task_id;
|
|
584
|
+
var patch = parsed.patch || {};
|
|
585
|
+
var parentId = null;
|
|
586
|
+
if (session.taskIdMap) {
|
|
587
|
+
for (var k in session.taskIdMap) {
|
|
588
|
+
if (session.taskIdMap[k] === taskId) { parentId = k; break; }
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (parentId) {
|
|
592
|
+
sendAndRecord(session, {
|
|
593
|
+
type: "task_updated",
|
|
594
|
+
parentToolId: parentId,
|
|
595
|
+
taskId: taskId,
|
|
596
|
+
patch: patch,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
} else if (parsed.yokeType === "tool_progress") {
|
|
601
|
+
// Sub-agent tool_progress: forward as activity update
|
|
602
|
+
var parentId = parsed.parentToolId;
|
|
603
|
+
if (parentId) {
|
|
604
|
+
sendAndRecord(session, {
|
|
605
|
+
type: "subagent_activity",
|
|
606
|
+
parentToolId: parentId,
|
|
607
|
+
text: parsed.text || "",
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
} else if (parsed.yokeType === "task_notification") {
|
|
612
|
+
var parentId = parsed.parentToolId;
|
|
613
|
+
if (parentId) {
|
|
614
|
+
sendAndRecord(session, {
|
|
615
|
+
type: "subagent_done",
|
|
616
|
+
parentToolId: parentId,
|
|
617
|
+
status: parsed.status || "completed",
|
|
618
|
+
summary: parsed.summary || "",
|
|
619
|
+
usage: parsed.usage || null,
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
if (session.taskIdMap) {
|
|
623
|
+
for (var k in session.taskIdMap) {
|
|
624
|
+
if (session.taskIdMap[k] === parsed.taskId) {
|
|
625
|
+
delete session.taskIdMap[k];
|
|
626
|
+
break;
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
} else if (parsed.yokeType === "rate_limit") {
|
|
632
|
+
var info = parsed.rateLimitInfo;
|
|
633
|
+
console.log("[sdk-bridge] rate_limit_event for session " + session.localId + ": status=" + info.status + " resetsAt=" + info.resetsAt + " isUsingOverage=" + info.isUsingOverage + " isProcessing=" + session.isProcessing);
|
|
634
|
+
|
|
635
|
+
// Broadcast reset time for top-bar usage link
|
|
636
|
+
if (info.rateLimitType && info.resetsAt) {
|
|
637
|
+
send({
|
|
638
|
+
type: "rate_limit_usage",
|
|
639
|
+
rateLimitType: info.rateLimitType,
|
|
640
|
+
resetsAt: info.resetsAt * 1000,
|
|
641
|
+
status: info.status,
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Warning/rejection handling (existing behavior)
|
|
646
|
+
if (info.status === "allowed_warning" || info.status === "rejected") {
|
|
647
|
+
sendAndRecord(session, {
|
|
648
|
+
type: "rate_limit",
|
|
649
|
+
status: info.status,
|
|
650
|
+
resetsAt: info.resetsAt ? info.resetsAt * 1000 : null,
|
|
651
|
+
rateLimitType: info.rateLimitType || null,
|
|
652
|
+
utilization: info.utilization || null,
|
|
653
|
+
isUsingOverage: info.isUsingOverage || false,
|
|
654
|
+
blocksCurrentModel: bucketBlocksModel(info.rateLimitType, sm && sm.currentModel),
|
|
655
|
+
});
|
|
656
|
+
// Track rejection for auto-continue / scheduled message support
|
|
657
|
+
if (info.status === "rejected" && info.resetsAt) {
|
|
658
|
+
session.rateLimitResetsAt = info.resetsAt * 1000;
|
|
659
|
+
|
|
660
|
+
// Schedule auto-continue immediately on rejection (don't wait for
|
|
661
|
+
// query completion which has timing issues with worker/non-worker paths).
|
|
662
|
+
if (!session.scheduledMessage && !session.destroying) {
|
|
663
|
+
var acEnabled = session.onQueryComplete ||
|
|
664
|
+
(typeof opts.getAutoContinueSetting === "function" && opts.getAutoContinueSetting(session));
|
|
665
|
+
console.log("[sdk-bridge] rate_limit rejected: acEnabled=" + acEnabled + " overage=" + !!info.isUsingOverage + " session=" + session.localId);
|
|
666
|
+
if (acEnabled) {
|
|
667
|
+
session.rateLimitAutoContinuePending = true;
|
|
668
|
+
if (info.isUsingOverage) {
|
|
669
|
+
// Extra usage available: send continue immediately (5s delay for query to finish)
|
|
670
|
+
console.log("[sdk-bridge] Overage available, sending immediate continue for session " + session.localId);
|
|
671
|
+
session.rateLimitResetsAt = null;
|
|
672
|
+
if (typeof opts.scheduleMessage === "function") {
|
|
673
|
+
opts.scheduleMessage(session, "continue", Date.now());
|
|
674
|
+
}
|
|
675
|
+
} else {
|
|
676
|
+
// No overage: schedule after rate limit resets
|
|
677
|
+
var acResetsAt = session.rateLimitResetsAt;
|
|
678
|
+
session.rateLimitResetsAt = null;
|
|
679
|
+
console.log("[sdk-bridge] Scheduling auto-continue on rate limit rejection for session " + session.localId);
|
|
680
|
+
if (typeof opts.scheduleMessage === "function") {
|
|
681
|
+
opts.scheduleMessage(session, "continue", acResetsAt);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
} else if (parsed.yokeType === "prompt_suggestion") {
|
|
690
|
+
sendAndRecord(session, {
|
|
691
|
+
type: "prompt_suggestion",
|
|
692
|
+
suggestion: parsed.suggestion || "",
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
} else if (parsed.yokeType === "notification") {
|
|
696
|
+
var notifText = parsed.text || "";
|
|
697
|
+
var notifPriority = parsed.priority || "low";
|
|
698
|
+
if (notifText) {
|
|
699
|
+
sendAndRecord(session, {
|
|
700
|
+
type: "sdk_notification",
|
|
701
|
+
key: parsed.key || "",
|
|
702
|
+
text: notifText,
|
|
703
|
+
priority: notifPriority,
|
|
704
|
+
color: parsed.color || null,
|
|
705
|
+
timeoutMs: parsed.timeout_ms || null,
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
} else if (parsed.yokeType === "api_retry") {
|
|
710
|
+
// Transient retry notification, show in UI but don't persist in history
|
|
711
|
+
var retryText = parsed.message || parsed.error || "Retrying API request...";
|
|
712
|
+
sendToSession(session, { type: "system_info", text: retryText });
|
|
713
|
+
|
|
714
|
+
} else if (parsed.yokeType === "system") {
|
|
715
|
+
// Catch-all for unhandled system subtypes (e.g. hook-block errors).
|
|
716
|
+
// Extract any error text and surface it in the UI.
|
|
717
|
+
var sysText = parsed.error || parsed.message || parsed.text || "";
|
|
718
|
+
if (!sysText && Array.isArray(parsed.content)) {
|
|
719
|
+
sysText = parsed.content
|
|
720
|
+
.filter(function(c) { return c.type === "text"; })
|
|
721
|
+
.map(function(c) { return c.text; })
|
|
722
|
+
.join("\n");
|
|
723
|
+
}
|
|
724
|
+
if (sysText) {
|
|
725
|
+
console.log("[sdk-bridge] Unhandled system message (subtype=" + (parsed.subtype || "none") + "): " + sysText.substring(0, 200));
|
|
726
|
+
sendAndRecord(session, { type: "error", text: sysText });
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
return {
|
|
732
|
+
processSDKMessage: processSDKMessage,
|
|
733
|
+
sendAndRecord: sendAndRecord,
|
|
734
|
+
sendToSession: sendToSession,
|
|
735
|
+
processSubagentMessage: processSubagentMessage,
|
|
736
|
+
toolActivityTextForSubagent: toolActivityTextForSubagent,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
module.exports = { attachMessageProcessor: attachMessageProcessor };
|