@parallelworks/code 0.0.0-stage → 0.2.0
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/LICENSE +202 -0
- package/README.md +205 -2
- package/dist/api.d.ts +3469 -0
- package/dist/apiClient.d.ts +26 -0
- package/dist/apiClient.js +5 -0
- package/dist/claudeApi.d.ts +27 -0
- package/dist/claudeApi.js +250 -0
- package/dist/command.d.ts +47 -0
- package/dist/command.js +64 -0
- package/dist/commandsApi.d.ts +64 -0
- package/dist/commandsApi.js +267 -0
- package/dist/createPwCodeApi.d.ts +688 -0
- package/dist/createPwCodeApi.js +31 -0
- package/dist/images.d.ts +7 -0
- package/dist/images.js +22 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +18 -0
- package/dist/locales/en/agentTranscript.js +170 -0
- package/dist/locales/en/agents.js +380 -0
- package/dist/locales/en.d.ts +531 -0
- package/dist/locales/en.js +8 -0
- package/dist/locales/es/agentTranscript.js +170 -0
- package/dist/locales/es/agents.js +380 -0
- package/dist/locales/es.d.ts +3 -0
- package/dist/locales/es.js +8 -0
- package/dist/locales/ja/agentTranscript.js +170 -0
- package/dist/locales/ja/agents.js +380 -0
- package/dist/locales/ja.d.ts +3 -0
- package/dist/locales/ja.js +8 -0
- package/dist/locales/ko/agentTranscript.js +170 -0
- package/dist/locales/ko/agents.js +380 -0
- package/dist/locales/ko.d.ts +3 -0
- package/dist/locales/ko.js +8 -0
- package/dist/locales/zh/agentTranscript.js +170 -0
- package/dist/locales/zh/agents.js +380 -0
- package/dist/locales/zh.d.ts +3 -0
- package/dist/locales/zh.js +8 -0
- package/dist/machine.d.ts +49 -0
- package/dist/machine.js +60 -0
- package/dist/messages.d.ts +17 -0
- package/dist/messages.js +31 -0
- package/dist/notices.d.ts +11 -0
- package/dist/notices.js +94 -0
- package/dist/pullRequest.d.ts +8 -0
- package/dist/pullRequest.js +29 -0
- package/dist/query.d.ts +28 -0
- package/dist/query.js +25 -0
- package/dist/session.d.ts +72 -0
- package/dist/session.js +111 -0
- package/dist/sessionEvents.d.ts +27 -0
- package/dist/sessionEvents.js +212 -0
- package/dist/sessionsApi.d.ts +105 -0
- package/dist/sessionsApi.js +513 -0
- package/dist/toolStrings.d.ts +4 -0
- package/dist/toolStrings.js +95 -0
- package/dist/transcript.d.ts +151 -0
- package/dist/transcript.js +1315 -0
- package/dist/types.d.ts +234 -0
- package/dist/useTranscript.d.ts +63 -0
- package/dist/useTranscript.js +243 -0
- package/dist/wording.d.ts +43 -0
- package/dist/wording.js +101 -0
- package/locales/en/agentTranscript.json +162 -0
- package/locales/en/agents.json +366 -0
- package/locales/es/agentTranscript.json +162 -0
- package/locales/es/agents.json +366 -0
- package/locales/ja/agentTranscript.json +162 -0
- package/locales/ja/agents.json +366 -0
- package/locales/ko/agentTranscript.json +162 -0
- package/locales/ko/agents.json +366 -0
- package/locales/zh/agentTranscript.json +162 -0
- package/locales/zh/agents.json +366 -0
- package/package.json +68 -3
|
@@ -0,0 +1,1315 @@
|
|
|
1
|
+
import { DateTime } from "luxon";
|
|
2
|
+
import { INTERRUPTED_NOTICE, applyPartDelta, askQuestionsFromArgs, classifyPseudoUserMessage, finalizeParts, firstNonEmptyLine, hiddenTranscriptTool, interruptionNotice, parseSubagentNotice, planFromArgs, resultFailed, subagentDeliveryLine, taskCallFromArgs } from "@parallelworks/ui/ai/headless";
|
|
3
|
+
// src/transcript.ts
|
|
4
|
+
var MAIN_SESSION_AUTHOR = {
|
|
5
|
+
id: "main",
|
|
6
|
+
username: "main session"
|
|
7
|
+
};
|
|
8
|
+
var synthCounter = 0;
|
|
9
|
+
function synthId(prefix) {
|
|
10
|
+
synthCounter += 1;
|
|
11
|
+
return `${prefix}-${synthCounter}`;
|
|
12
|
+
}
|
|
13
|
+
function userMessage(text, fromMain, id, pastes, images, imageUrl) {
|
|
14
|
+
const pseudo = classifyPseudoUserMessage("user", text);
|
|
15
|
+
const attachments = imageAttachments(images, imageUrl);
|
|
16
|
+
return {
|
|
17
|
+
id: id ?? synthId("u"),
|
|
18
|
+
role: "user",
|
|
19
|
+
content: text,
|
|
20
|
+
...pseudo ? { pseudo } : {},
|
|
21
|
+
...fromMain ? { author: MAIN_SESSION_AUTHOR } : {},
|
|
22
|
+
...pastes?.length ? { pastes } : {},
|
|
23
|
+
...attachments.length ? { attachments } : {}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** The daemon sends a preview of each image inline; one whose original it
|
|
27
|
+
* names opens at full size from the host, any other from its preview. */
|
|
28
|
+
function imageAttachments(images, imageUrl) {
|
|
29
|
+
return (images ?? []).filter((image) => image.mime.startsWith("image/")).map((image) => {
|
|
30
|
+
const preview = `data:${image.mime};base64,${image.data}`;
|
|
31
|
+
const previewSize = Math.floor(image.data.length * 3 / 4) - (image.data.match(/=*$/)?.[0].length ?? 0);
|
|
32
|
+
const original = image.ref && imageUrl ? imageUrl(image.ref) : void 0;
|
|
33
|
+
return {
|
|
34
|
+
id: original ?? preview,
|
|
35
|
+
previewUrl: preview,
|
|
36
|
+
filename: "",
|
|
37
|
+
contentType: original && image.originalMime || image.mime,
|
|
38
|
+
size: original ? image.originalSize ?? 0 : previewSize,
|
|
39
|
+
uploadedAt: ""
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function assistantMessage(parts = [], id) {
|
|
44
|
+
return {
|
|
45
|
+
id: id ?? synthId("a"),
|
|
46
|
+
role: "assistant",
|
|
47
|
+
parts
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
var SHELL_RECORD = /^<shell-input>([\s\S]*?)<\/shell-input>\s*<shell-stdout>([\s\S]*)<\/shell-stdout>\s*$/;
|
|
51
|
+
/** A `!` command and what it printed, from the user message the daemon records them as. */
|
|
52
|
+
function parseShellRecord(text) {
|
|
53
|
+
const match = SHELL_RECORD.exec(text.trim());
|
|
54
|
+
if (!match) return null;
|
|
55
|
+
return {
|
|
56
|
+
command: (match[1] ?? "").trim(),
|
|
57
|
+
output: match[2] ?? ""
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function fenced(text) {
|
|
61
|
+
const longest = (text.match(/`+/g) ?? []).reduce((max, run) => Math.max(max, run.length), 0);
|
|
62
|
+
const fence = "`".repeat(Math.max(3, longest + 1));
|
|
63
|
+
return `${fence}\n${text}\n${fence}`;
|
|
64
|
+
}
|
|
65
|
+
/** A `!` run as the terminal shows it: the command, then its output. */
|
|
66
|
+
function shellRunPart(command, output) {
|
|
67
|
+
const shown = output.replace(/^\s*\n/, "").trimEnd();
|
|
68
|
+
return {
|
|
69
|
+
kind: "notice",
|
|
70
|
+
text: `! ${command}`,
|
|
71
|
+
...shown ? {
|
|
72
|
+
detail: fenced(shown),
|
|
73
|
+
open: true
|
|
74
|
+
} : {}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The daemon emits nothing for a `!` run, so the page that ran it shows it. */
|
|
78
|
+
function appendShellRun(state, command, output) {
|
|
79
|
+
return {
|
|
80
|
+
...state,
|
|
81
|
+
messages: [...state.messages, assistantMessage([shellRunPart(command, output)])]
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** The daemon pairs results to calls positionally; the wire carries no ids. */
|
|
85
|
+
function completeOldestRunning(parts, result) {
|
|
86
|
+
const idx = parts.findIndex((p) => p.kind === "tool_call" && p.status === "running");
|
|
87
|
+
if (idx === -1) return parts;
|
|
88
|
+
return parts.map((p, i) => i === idx && p.kind === "tool_call" ? {
|
|
89
|
+
...p,
|
|
90
|
+
status: resultFailed(p.name, result) ? "error" : "ok",
|
|
91
|
+
result
|
|
92
|
+
} : p);
|
|
93
|
+
}
|
|
94
|
+
function approvalSubject(req) {
|
|
95
|
+
if (req.reason === "file is outside the workspace") return "path";
|
|
96
|
+
if (req.tool === "NotebookRun" || req.reason === "code in the notebook kernel") return "code";
|
|
97
|
+
if (req.tool !== void 0 && req.tool !== "Bash") return "call";
|
|
98
|
+
return "command";
|
|
99
|
+
}
|
|
100
|
+
function approvalToPart(req, words) {
|
|
101
|
+
const subject = req.kind === "confirm" ? approvalSubject(req) : void 0;
|
|
102
|
+
const canAllowDirectory = subject === "path";
|
|
103
|
+
return {
|
|
104
|
+
kind: "approval",
|
|
105
|
+
id: req.id,
|
|
106
|
+
approvalKind: req.kind,
|
|
107
|
+
...subject !== void 0 ? { subject } : {},
|
|
108
|
+
...req.tool !== void 0 ? { toolName: req.tool } : {},
|
|
109
|
+
...req.command !== void 0 ? { command: req.command } : {},
|
|
110
|
+
...req.reason !== void 0 ? { reason: req.reason } : {},
|
|
111
|
+
...req.agent !== void 0 ? { agent: req.agent } : {},
|
|
112
|
+
...req.mode !== void 0 ? { mode: req.mode } : {},
|
|
113
|
+
...req.kind === "ask" ? { questions: askQuestions(req.questions) } : {},
|
|
114
|
+
...req.callId !== void 0 ? { toolCallId: req.callId } : {},
|
|
115
|
+
...req.plan !== void 0 ? { plan: req.plan } : {},
|
|
116
|
+
canAllowDirectory,
|
|
117
|
+
...canAllowDirectory && req.allowDir && words ? { allowDirectoryLabel: words.allowDirectory(req.allowDir) } : {}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function askQuestions(questions) {
|
|
121
|
+
return (questions ?? []).map((q) => ({
|
|
122
|
+
question: q.question,
|
|
123
|
+
...q.header ? { header: q.header } : {},
|
|
124
|
+
...q.multiSelect ? { multiSelect: true } : {},
|
|
125
|
+
...q.options?.length ? { options: q.options.map((o) => ({
|
|
126
|
+
label: o.label,
|
|
127
|
+
...o.description !== void 0 ? { description: o.description } : {},
|
|
128
|
+
...o.preview !== void 0 ? { preview: o.preview } : {}
|
|
129
|
+
})) } : {}
|
|
130
|
+
}));
|
|
131
|
+
}
|
|
132
|
+
function approvalAnswerBody(answer) {
|
|
133
|
+
if (answer.kind === "confirm") return {
|
|
134
|
+
allowed: answer.allowed,
|
|
135
|
+
...answer.allowed && answer.allowDir ? { allowDir: true } : {},
|
|
136
|
+
...answer.denyMessage !== void 0 ? { denyMessage: answer.denyMessage } : {}
|
|
137
|
+
};
|
|
138
|
+
return {
|
|
139
|
+
answers: answer.answers,
|
|
140
|
+
...answer.chat !== void 0 ? { chat: answer.chat } : {}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** What a recorded ask gave each question; denied is the reader choosing to chat about them instead. */
|
|
144
|
+
function askOutcome(d) {
|
|
145
|
+
const answers = (d.answers ?? []).map((a) => ({
|
|
146
|
+
...a.picked?.length ? { picked: a.picked } : {},
|
|
147
|
+
...a.text ? { text: a.text } : {},
|
|
148
|
+
...a.notes ? { notes: a.notes } : {}
|
|
149
|
+
}));
|
|
150
|
+
return d.status === "denied" ? {
|
|
151
|
+
answers,
|
|
152
|
+
chat: true
|
|
153
|
+
} : { answers };
|
|
154
|
+
}
|
|
155
|
+
/** A review still under way, or one the turn cut short, settled nothing. */
|
|
156
|
+
function toolDecision(d) {
|
|
157
|
+
if (d.status === "reviewing" || d.status === "aborted") return null;
|
|
158
|
+
return {
|
|
159
|
+
status: d.status,
|
|
160
|
+
by: d.by,
|
|
161
|
+
at: d.at,
|
|
162
|
+
...d.username ? { name: d.username } : {},
|
|
163
|
+
...d.note ? { note: d.note } : {},
|
|
164
|
+
...d.rationale ? { reason: d.rationale } : {},
|
|
165
|
+
...d.action ? { action: d.action } : {}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/** Ids come from the item's position, so a refetch keeps message identity and
|
|
169
|
+
* React does not remount (and collapse) the whole transcript. */
|
|
170
|
+
function historyToMessages(history, options = {}) {
|
|
171
|
+
const finalizeOpen = options.finalizeOpen ?? true;
|
|
172
|
+
const { words } = options;
|
|
173
|
+
const lastSettledTurnEnd = history.findLastIndex((item) => item.kind === "turn_end" && !item.turnEnd?.pendingAgents);
|
|
174
|
+
const messages = [];
|
|
175
|
+
let current = null;
|
|
176
|
+
const flush = (finalize) => {
|
|
177
|
+
if (current) {
|
|
178
|
+
if (finalize) current = {
|
|
179
|
+
...current,
|
|
180
|
+
parts: finalizeParts(current.parts ?? [])
|
|
181
|
+
};
|
|
182
|
+
messages.push(current);
|
|
183
|
+
current = null;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const open = (index) => {
|
|
187
|
+
if (!current) current = assistantMessage([], `h-a-${index}`);
|
|
188
|
+
return current;
|
|
189
|
+
};
|
|
190
|
+
const append = (part, index) => {
|
|
191
|
+
const message = open(index);
|
|
192
|
+
current = {
|
|
193
|
+
...message,
|
|
194
|
+
parts: [...message.parts ?? [], part]
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
const ranCommand = (command, index, answer) => {
|
|
198
|
+
if (!command) {
|
|
199
|
+
append(answer.document ? {
|
|
200
|
+
kind: "text",
|
|
201
|
+
text: answer.document
|
|
202
|
+
} : {
|
|
203
|
+
kind: "notice",
|
|
204
|
+
text: answer.text,
|
|
205
|
+
...answer.detail ? { detail: answer.detail } : {}
|
|
206
|
+
}, index);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
flush(true);
|
|
210
|
+
messages.push({
|
|
211
|
+
...userMessage(command, false, `h-u-${index}`),
|
|
212
|
+
commandAnswer: answer
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
const pending = [];
|
|
216
|
+
const settle = (call, result) => {
|
|
217
|
+
const message = open(-1);
|
|
218
|
+
current = {
|
|
219
|
+
...message,
|
|
220
|
+
parts: settleCall(message.parts ?? [], call, result)
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
const dropWaitingAsks = () => {
|
|
224
|
+
const waiting = new Set(pending.flatMap((call) => call.kind === "ask" ? [call.id] : []));
|
|
225
|
+
if (current && waiting.size > 0) current = {
|
|
226
|
+
...current,
|
|
227
|
+
parts: (current.parts ?? []).filter((p) => !(p.kind === "approval" && waiting.has(p.id)))
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
history.forEach((item, index) => {
|
|
231
|
+
switch (item.kind) {
|
|
232
|
+
case "user": {
|
|
233
|
+
flush(true);
|
|
234
|
+
const shell = parseShellRecord(item.text ?? "");
|
|
235
|
+
if (shell) {
|
|
236
|
+
append(shellRunPart(shell.command, shell.output), index);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
messages.push(userMessage(item.text ?? "", item.fromMain ?? false, `h-u-${index}`, item.pastes, item.images, options.imageUrl));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
case "assistant": {
|
|
243
|
+
const interrupted = interruptionNotice(item.text ?? "");
|
|
244
|
+
append(interrupted ? {
|
|
245
|
+
kind: "notice",
|
|
246
|
+
text: interrupted
|
|
247
|
+
} : {
|
|
248
|
+
kind: "text",
|
|
249
|
+
text: item.text ?? ""
|
|
250
|
+
}, index);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
case "thinking":
|
|
254
|
+
append({
|
|
255
|
+
kind: "reasoning",
|
|
256
|
+
text: item.text ?? ""
|
|
257
|
+
}, index);
|
|
258
|
+
return;
|
|
259
|
+
case "tool_call": {
|
|
260
|
+
const name = item.toolName ?? "";
|
|
261
|
+
const args = item.toolArgs ?? "";
|
|
262
|
+
const id = `h-${index}`;
|
|
263
|
+
if (name === "Task") {
|
|
264
|
+
const { description, agentType } = taskCallFromArgs(args);
|
|
265
|
+
append({
|
|
266
|
+
kind: "subagent",
|
|
267
|
+
id,
|
|
268
|
+
status: "running",
|
|
269
|
+
description,
|
|
270
|
+
...agentType ? { agentType } : {}
|
|
271
|
+
}, index);
|
|
272
|
+
pending.push({
|
|
273
|
+
kind: "subagent",
|
|
274
|
+
id
|
|
275
|
+
});
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (name === "AskUserQuestion") {
|
|
279
|
+
const [card, call] = askCard(id, args, item.decision);
|
|
280
|
+
append(card, index);
|
|
281
|
+
pending.push(call);
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (name === "ExitPlanMode") {
|
|
285
|
+
append({
|
|
286
|
+
kind: "text",
|
|
287
|
+
text: planFromArgs(args)
|
|
288
|
+
}, index);
|
|
289
|
+
pending.push({ kind: "hidden" });
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const todos = name === "TodoWrite" ? todosFromArgs(args) : null;
|
|
293
|
+
if (todos?.length) {
|
|
294
|
+
const message = open(index);
|
|
295
|
+
current = {
|
|
296
|
+
...message,
|
|
297
|
+
parts: applyPartDelta(message.parts ?? [], {
|
|
298
|
+
type: "todo_snapshot",
|
|
299
|
+
todos
|
|
300
|
+
})
|
|
301
|
+
};
|
|
302
|
+
pending.push({ kind: "hidden" });
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (hiddenTranscriptTool(name)) {
|
|
306
|
+
pending.push({ kind: "hidden" });
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const decision = item.decision && toolDecision(item.decision);
|
|
310
|
+
append({
|
|
311
|
+
kind: "tool_call",
|
|
312
|
+
id: item.callId || id,
|
|
313
|
+
name,
|
|
314
|
+
args,
|
|
315
|
+
status: "running",
|
|
316
|
+
...item.startLine ? { startLine: item.startLine } : {},
|
|
317
|
+
...planFileEdit(name, args) ? { foldDiff: true } : {},
|
|
318
|
+
...decision ? { decision } : {}
|
|
319
|
+
}, index);
|
|
320
|
+
pending.push({ kind: "tool" });
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
case "tool_result": {
|
|
324
|
+
const call = pending.shift();
|
|
325
|
+
if (call) settle(call, item.text ?? "");
|
|
326
|
+
const images = imageAttachments(item.images, options.imageUrl);
|
|
327
|
+
if (images.length) append({
|
|
328
|
+
kind: "image",
|
|
329
|
+
images
|
|
330
|
+
}, index);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
case "plan_view": {
|
|
334
|
+
const path = item.text ?? "";
|
|
335
|
+
ranCommand(item.toolName, index, {
|
|
336
|
+
text: words ? words.planAnswer(path, item.toolName === "/plan open") : path,
|
|
337
|
+
...item.planBody ? { document: item.planBody } : {}
|
|
338
|
+
});
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
case "notice": {
|
|
342
|
+
const text = item.text ?? "";
|
|
343
|
+
const delivery = parseSubagentNotice(text);
|
|
344
|
+
if (delivery?.silent) return;
|
|
345
|
+
append(delivery ? {
|
|
346
|
+
kind: "notice",
|
|
347
|
+
text: deliveryLine(delivery, words),
|
|
348
|
+
...delivery.body ? { detail: delivery.body } : {}
|
|
349
|
+
} : {
|
|
350
|
+
kind: "notice",
|
|
351
|
+
text: text.trim()
|
|
352
|
+
}, index);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
case "warning":
|
|
356
|
+
append({
|
|
357
|
+
kind: "warning",
|
|
358
|
+
text: item.text ?? ""
|
|
359
|
+
}, index);
|
|
360
|
+
return;
|
|
361
|
+
case "error":
|
|
362
|
+
append({
|
|
363
|
+
kind: "error",
|
|
364
|
+
text: item.text ?? ""
|
|
365
|
+
}, index);
|
|
366
|
+
return;
|
|
367
|
+
case "goal_view":
|
|
368
|
+
ranCommand(item.toolName, index, { text: item.text ?? "" });
|
|
369
|
+
return;
|
|
370
|
+
case "compaction": {
|
|
371
|
+
const notice = options.compactionNotice;
|
|
372
|
+
if (notice) ranCommand(item.toolName, index, {
|
|
373
|
+
text: item.toolName ? notice.command : notice.automatic,
|
|
374
|
+
...item.text ? { detail: item.text } : {}
|
|
375
|
+
});
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
case "goal_progress": {
|
|
379
|
+
const progress = parseGoalProgress(item.text);
|
|
380
|
+
const text = goalText(progress, options.goalNotice);
|
|
381
|
+
const detail = goalDetail(progress, words);
|
|
382
|
+
if (text) append({
|
|
383
|
+
kind: "notice",
|
|
384
|
+
text,
|
|
385
|
+
...detail ? { detail } : {}
|
|
386
|
+
}, index);
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
case "turn_end": {
|
|
390
|
+
const te = item.turnEnd;
|
|
391
|
+
if (te && words) {
|
|
392
|
+
const waiting = index > lastSettledTurnEnd && (te.pendingAgents ?? 0) > 0;
|
|
393
|
+
append({
|
|
394
|
+
kind: "notice",
|
|
395
|
+
text: words.turnEnd(te, turnVerb(te.endedAt), waiting)
|
|
396
|
+
}, index);
|
|
397
|
+
}
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
case "recap": {
|
|
401
|
+
const text = recapText(item.text, options.recapNotice);
|
|
402
|
+
if (text) append({
|
|
403
|
+
kind: "notice",
|
|
404
|
+
text
|
|
405
|
+
}, index);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
default: return;
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
dropWaitingAsks();
|
|
412
|
+
flush(finalizeOpen);
|
|
413
|
+
return messages;
|
|
414
|
+
}
|
|
415
|
+
/** An ask's card, settled from its record when it has one, or else by the result that follows. */
|
|
416
|
+
function askCard(id, args, d) {
|
|
417
|
+
const card = {
|
|
418
|
+
kind: "approval",
|
|
419
|
+
id,
|
|
420
|
+
approvalKind: "ask",
|
|
421
|
+
questions: askQuestionsFromArgs(args),
|
|
422
|
+
canAllowDirectory: false
|
|
423
|
+
};
|
|
424
|
+
if (!d?.answers?.length) return [card, {
|
|
425
|
+
kind: "ask",
|
|
426
|
+
id
|
|
427
|
+
}];
|
|
428
|
+
return [{
|
|
429
|
+
...card,
|
|
430
|
+
resolved: true,
|
|
431
|
+
...askOutcome(d)
|
|
432
|
+
}, { kind: "hidden" }];
|
|
433
|
+
}
|
|
434
|
+
var BACKGROUND_STARTED_PREFIX = "Started background subagent";
|
|
435
|
+
function settleCall(parts, call, result) {
|
|
436
|
+
switch (call.kind) {
|
|
437
|
+
case "tool": return completeOldestRunning(parts, result);
|
|
438
|
+
case "subagent": return parts.map((p) => {
|
|
439
|
+
if (p.kind !== "subagent" || p.id !== call.id) return p;
|
|
440
|
+
if (result.startsWith(BACKGROUND_STARTED_PREFIX)) return {
|
|
441
|
+
...p,
|
|
442
|
+
background: true
|
|
443
|
+
};
|
|
444
|
+
return {
|
|
445
|
+
...p,
|
|
446
|
+
status: resultFailed("Task", result) ? "failed" : "done",
|
|
447
|
+
summary: firstNonEmptyLine(result, 60)
|
|
448
|
+
};
|
|
449
|
+
});
|
|
450
|
+
case "ask": return parts.map((p) => p.kind === "approval" && p.id === call.id ? {
|
|
451
|
+
...p,
|
|
452
|
+
resolved: true,
|
|
453
|
+
answer: result.trim()
|
|
454
|
+
} : p);
|
|
455
|
+
default: return parts;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
/** A session with no history resumes from the start: the daemon stamps a seq
|
|
459
|
+
* before the agent records it, so a prompt sent with `create` falls in that
|
|
460
|
+
* window, and replaying a session that seeded nothing can duplicate nothing. */
|
|
461
|
+
function resumeCursor(detail) {
|
|
462
|
+
return detail.history?.length ? detail.lastSeq ?? 0 : 0;
|
|
463
|
+
}
|
|
464
|
+
function initialTranscript(detail, options = {}) {
|
|
465
|
+
const running = detail.status === "awaiting_approval" || !!detail.turnId;
|
|
466
|
+
const messages = historyToMessages(detail.history ?? [], {
|
|
467
|
+
...options,
|
|
468
|
+
finalizeOpen: !running
|
|
469
|
+
});
|
|
470
|
+
let streaming = null;
|
|
471
|
+
const last = messages[messages.length - 1];
|
|
472
|
+
if (running && last && last.role === "assistant") {
|
|
473
|
+
streaming = last;
|
|
474
|
+
messages.pop();
|
|
475
|
+
}
|
|
476
|
+
if (detail.approvals && detail.approvals.length > 0) {
|
|
477
|
+
const approvals = detail.approvals.map((req) => approvalToPart(req, options.words));
|
|
478
|
+
streaming = streaming ? {
|
|
479
|
+
...streaming,
|
|
480
|
+
parts: [...streaming.parts ?? [], ...approvals]
|
|
481
|
+
} : assistantMessage(approvals);
|
|
482
|
+
}
|
|
483
|
+
const terminal = (streaming ?? messages.at(-1))?.parts?.at(-1);
|
|
484
|
+
const seededTerminalTurnId = detail.history?.findLast((item) => item.kind !== "turn_end")?.turnId;
|
|
485
|
+
return {
|
|
486
|
+
messages,
|
|
487
|
+
streaming,
|
|
488
|
+
seededTerminal: seededTerminalTurnId && (terminal?.kind === "error" || terminal?.kind === "notice") ? {
|
|
489
|
+
part: terminal,
|
|
490
|
+
turnId: seededTerminalTurnId
|
|
491
|
+
} : null,
|
|
492
|
+
cursor: resumeCursor(detail),
|
|
493
|
+
turnEnded: !running,
|
|
494
|
+
queued: [],
|
|
495
|
+
steered: [],
|
|
496
|
+
notes: [],
|
|
497
|
+
turnStartedAt: running ? detail.turnStartedAt ?? null : null,
|
|
498
|
+
waitingForUsageLimit: running && !!detail.waitingForUsageLimit,
|
|
499
|
+
usageLimitResumeAt: running && detail.waitingForUsageLimit ? detail.usageLimitResumeAt ?? null : null,
|
|
500
|
+
seededSeq: detail.lastSeq ?? 0
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/** A subagent's detail as the session detail its transcript seeds from. Its run
|
|
504
|
+
* has no turn id on the wire, so its own id names the one in flight. */
|
|
505
|
+
function subagentSessionDetail(sub) {
|
|
506
|
+
const running = sub.status === "running";
|
|
507
|
+
return {
|
|
508
|
+
status: sub.approvals?.length ? "awaiting_approval" : running ? "running" : "idle",
|
|
509
|
+
history: sub.history ?? [],
|
|
510
|
+
approvals: sub.approvals ?? [],
|
|
511
|
+
lastSeq: sub.lastSeq ?? 0,
|
|
512
|
+
...running ? { turnId: sub.id } : {},
|
|
513
|
+
...running && sub.startedAt ? { turnStartedAt: sub.startedAt } : {}
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
/** A session frame as one subagent's transcript reads it. A turn someone
|
|
517
|
+
* prompted on the subagent streams under its id, and a note it was handed
|
|
518
|
+
* arrives as its own event; the task it was given streams nothing, so a frame
|
|
519
|
+
* that only says it moved on is `refetch`. Null is a frame for someone else. */
|
|
520
|
+
function subagentFrame(ev, id) {
|
|
521
|
+
if (ev.type === "replay_truncated" || ev.type === "approval_resolved") return ev;
|
|
522
|
+
if (ev.type === "approval_request") return ev.approval?.subagentId === id ? ev : null;
|
|
523
|
+
if (ev.id !== id || ev.seq === 0) return null;
|
|
524
|
+
const { id: _, ...frame } = ev;
|
|
525
|
+
if (ev.type === "subagent_user_note") return {
|
|
526
|
+
...frame,
|
|
527
|
+
type: "user_note"
|
|
528
|
+
};
|
|
529
|
+
return ev.type.startsWith("subagent_") ? "refetch" : frame;
|
|
530
|
+
}
|
|
531
|
+
/** The approvals the live feed is still waiting on. */
|
|
532
|
+
function pendingApprovals(transcript) {
|
|
533
|
+
return (transcript.streaming?.parts ?? []).filter((p) => p.kind === "approval" && !p.resolved);
|
|
534
|
+
}
|
|
535
|
+
/** Status as the live feed shows it; `detail.status` is only the fetch moment. */
|
|
536
|
+
function liveSessionStatus(status, transcript, goal) {
|
|
537
|
+
if (status === "live_elsewhere" || !transcript) return status;
|
|
538
|
+
if (pendingApprovals(transcript).length > 0) return "awaiting_approval";
|
|
539
|
+
if (turnRunning(transcript) || goal?.checking) return "running";
|
|
540
|
+
return transcript.turnEnded ? "idle" : status;
|
|
541
|
+
}
|
|
542
|
+
/** A recorded ask settles its card with each question's answer, whether they were given here or in the terminal. */
|
|
543
|
+
function answeredAsk(parts, d) {
|
|
544
|
+
const card = parts.find((p) => p.kind === "approval" && p.toolCallId === d.callId);
|
|
545
|
+
return card ? applyPartDelta(parts, {
|
|
546
|
+
type: "approval_resolved",
|
|
547
|
+
id: card.id,
|
|
548
|
+
...askOutcome(d)
|
|
549
|
+
}) : parts;
|
|
550
|
+
}
|
|
551
|
+
/** The running call an event names; a daemon older than tool_end's callId names none, so the newest call of its name stands in. */
|
|
552
|
+
function runningToolId(parts, callId, name) {
|
|
553
|
+
for (let i = parts.length - 1; i >= 0; i -= 1) {
|
|
554
|
+
const p = parts[i];
|
|
555
|
+
if (p !== void 0 && p.kind === "tool_call" && p.status === "running" && (callId ? p.id === callId : name === void 0 || p.name === name)) return p.id;
|
|
556
|
+
}
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
function resolveApprovalEverywhere(state, id) {
|
|
560
|
+
const resolve = (m) => m.parts?.some((p) => p.kind === "approval" && p.id === id) ? {
|
|
561
|
+
...m,
|
|
562
|
+
parts: applyPartDelta(m.parts ?? [], {
|
|
563
|
+
type: "approval_resolved",
|
|
564
|
+
id
|
|
565
|
+
})
|
|
566
|
+
} : m;
|
|
567
|
+
return {
|
|
568
|
+
...state,
|
|
569
|
+
messages: state.messages.map(resolve),
|
|
570
|
+
streaming: state.streaming ? resolve(state.streaming) : null
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
/** The daemon queues a message sent mid-turn rather than cutting the turn
|
|
574
|
+
* short, so the page must not close the turn either: its open tool calls
|
|
575
|
+
* would read as interrupted while they are still running. */
|
|
576
|
+
function appendOwnMessage(state, text, id, pastes, previews) {
|
|
577
|
+
const message = userMessage(text, false, id, pastes, previews);
|
|
578
|
+
if (turnRunning(state)) return {
|
|
579
|
+
...state,
|
|
580
|
+
queued: [...state.queued, message]
|
|
581
|
+
};
|
|
582
|
+
return {
|
|
583
|
+
...state,
|
|
584
|
+
messages: [...state.messages, message],
|
|
585
|
+
seededTerminal: null,
|
|
586
|
+
turnEnded: false,
|
|
587
|
+
turnStartedAt: nowIso(),
|
|
588
|
+
opening: message.id
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
/** A subagent runs one turn at a time, so a message sent to it starts its next:
|
|
592
|
+
* a turn the transcript still shows running has ended. */
|
|
593
|
+
function appendTurnMessage(state, text, id, pastes, previews) {
|
|
594
|
+
return appendOwnMessage(finishTurn(state), text, id, pastes, previews);
|
|
595
|
+
}
|
|
596
|
+
/** An own message takes the pastes and images its echo says the model received. */
|
|
597
|
+
function withSentPastesAndImages(messages, ev, id, imageUrl) {
|
|
598
|
+
const pastes = ev.pastes?.length ? ev.pastes : null;
|
|
599
|
+
const attachments = imageAttachments(ev.images, imageUrl);
|
|
600
|
+
const idx = messages.findLastIndex((m) => id ? m.id === id : m.role === "user" && m.content === ev.content);
|
|
601
|
+
const message = messages[idx];
|
|
602
|
+
if (!message || !pastes && !message.pastes?.length && !attachments.length) return messages;
|
|
603
|
+
const sent = {
|
|
604
|
+
...message,
|
|
605
|
+
pastes,
|
|
606
|
+
...attachments.length ? { attachments } : {}
|
|
607
|
+
};
|
|
608
|
+
return messages.map((m, i) => i === idx ? sent : m);
|
|
609
|
+
}
|
|
610
|
+
/** Held apart until the daemon reports the model was handed it. */
|
|
611
|
+
function appendSteeredMessage(state, text, id, pastes, previews) {
|
|
612
|
+
return {
|
|
613
|
+
...state,
|
|
614
|
+
steered: [...state.steered, userMessage(text, false, id, pastes, previews)]
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
/** The daemon queued a message this page had shown as sent, because the turn
|
|
618
|
+
* ahead of it had not reached the page yet. */
|
|
619
|
+
function markQueued(state, id) {
|
|
620
|
+
const message = state.messages.find((m) => m.id === id);
|
|
621
|
+
if (!message) return state;
|
|
622
|
+
return {
|
|
623
|
+
...state,
|
|
624
|
+
messages: state.messages.filter((m) => m.id !== id),
|
|
625
|
+
queued: [...state.queued, message],
|
|
626
|
+
...state.opening === id ? { opening: null } : {}
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
/** Left in place, a message the machine refused reads as one the session received. */
|
|
630
|
+
function dropMessage(state, id) {
|
|
631
|
+
const dropped = {
|
|
632
|
+
...state,
|
|
633
|
+
messages: state.messages.filter((m) => m.id !== id),
|
|
634
|
+
queued: state.queued.filter((m) => m.id !== id),
|
|
635
|
+
steered: state.steered.filter((m) => m.id !== id)
|
|
636
|
+
};
|
|
637
|
+
return state.opening === id ? {
|
|
638
|
+
...dropped,
|
|
639
|
+
opening: null,
|
|
640
|
+
turnEnded: true,
|
|
641
|
+
turnStartedAt: null
|
|
642
|
+
} : dropped;
|
|
643
|
+
}
|
|
644
|
+
/** Everything sent that the transcript has not placed yet, in the order it
|
|
645
|
+
* will be read: handed to the model, then steered, then turns of their own. */
|
|
646
|
+
function pendingMessages(state) {
|
|
647
|
+
return [
|
|
648
|
+
...state.notes,
|
|
649
|
+
...state.steered,
|
|
650
|
+
...state.queued
|
|
651
|
+
];
|
|
652
|
+
}
|
|
653
|
+
/** A turn runs before it streams anything, and after it is handed a note. */
|
|
654
|
+
function turnRunning(state) {
|
|
655
|
+
return !!state && (!!state.streaming || !state.turnEnded && (state.turnStartedAt !== null || state.notes.length > 0));
|
|
656
|
+
}
|
|
657
|
+
/** Whether the turn waits on a Bash call, which the machine can move to the background. */
|
|
658
|
+
function bashRunning(state) {
|
|
659
|
+
return (state?.streaming?.parts ?? []).some((p) => p.kind === "tool_call" && p.name === "Bash" && p.status === "running");
|
|
660
|
+
}
|
|
661
|
+
/** Daemons before the interrupted flag close a stopped turn with the
|
|
662
|
+
* cancelled context's error. */
|
|
663
|
+
function stoppedTurn(ev) {
|
|
664
|
+
return !!ev.interrupted || /context canceled$/.test(ev.content ?? "");
|
|
665
|
+
}
|
|
666
|
+
/** What the agent records for a call a stop cut off (serverapi.UserInterruptedTool), so a stopped call reads live as it replays. */
|
|
667
|
+
function userInterruptedTool(name) {
|
|
668
|
+
return `The user interrupted ${name} before it finished.`;
|
|
669
|
+
}
|
|
670
|
+
function interruptRunningCalls(parts) {
|
|
671
|
+
return parts.reduce((settled, p) => p.kind === "tool_call" && p.status === "running" ? applyPartDelta(settled, {
|
|
672
|
+
type: "tool_end",
|
|
673
|
+
id: p.id,
|
|
674
|
+
result: userInterruptedTool(p.name),
|
|
675
|
+
isError: false
|
|
676
|
+
}) : settled, parts);
|
|
677
|
+
}
|
|
678
|
+
var TODO_STATUSES = /* @__PURE__ */ new Set([
|
|
679
|
+
"pending",
|
|
680
|
+
"in_progress",
|
|
681
|
+
"completed"
|
|
682
|
+
]);
|
|
683
|
+
/** TodoWrite's whole list, which its arguments carry; null when they hold none. */
|
|
684
|
+
function todosFromArgs(argsJSON) {
|
|
685
|
+
let args;
|
|
686
|
+
try {
|
|
687
|
+
args = JSON.parse(argsJSON);
|
|
688
|
+
} catch {
|
|
689
|
+
return null;
|
|
690
|
+
}
|
|
691
|
+
const raw = args && typeof args === "object" ? args["todos"] : void 0;
|
|
692
|
+
if (!Array.isArray(raw)) return null;
|
|
693
|
+
return raw.flatMap((t) => {
|
|
694
|
+
const { content, status, activeForm } = t && typeof t === "object" ? t : {};
|
|
695
|
+
if (typeof content !== "string" || typeof status !== "string" || !TODO_STATUSES.has(status)) return [];
|
|
696
|
+
return [{
|
|
697
|
+
content,
|
|
698
|
+
status,
|
|
699
|
+
...typeof activeForm === "string" ? { activeForm } : {}
|
|
700
|
+
}];
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
function applyPartEvent(parts, ev, imageUrl, words) {
|
|
704
|
+
switch (ev.type) {
|
|
705
|
+
case "chunk": return applyPartDelta(parts, {
|
|
706
|
+
type: "text",
|
|
707
|
+
text: ev.content ?? ""
|
|
708
|
+
});
|
|
709
|
+
case "reasoning": return applyPartDelta(parts, {
|
|
710
|
+
type: "reasoning",
|
|
711
|
+
text: ev.content ?? ""
|
|
712
|
+
});
|
|
713
|
+
case "retract": return applyPartDelta(parts, { type: "retract" });
|
|
714
|
+
case "tool_start": {
|
|
715
|
+
const todos = ev.name === "TodoWrite" ? todosFromArgs(ev.args ?? "") : null;
|
|
716
|
+
if (todos?.length) return applyPartDelta(parts, {
|
|
717
|
+
type: "todo_snapshot",
|
|
718
|
+
todos
|
|
719
|
+
});
|
|
720
|
+
if (hiddenTranscriptTool(ev.name ?? "")) return parts;
|
|
721
|
+
const id = ev.callId || String(ev.seq);
|
|
722
|
+
const started = applyPartDelta(parts, {
|
|
723
|
+
type: "tool_start",
|
|
724
|
+
id,
|
|
725
|
+
name: ev.name ?? "",
|
|
726
|
+
args: ev.args ?? "",
|
|
727
|
+
...ev.startLine !== void 0 ? { startLine: ev.startLine } : {}
|
|
728
|
+
});
|
|
729
|
+
return planFileEdit(ev.name ?? "", ev.args ?? "") ? started.map((p) => p.kind === "tool_call" && p.id === id ? {
|
|
730
|
+
...p,
|
|
731
|
+
foldDiff: true
|
|
732
|
+
} : p) : started;
|
|
733
|
+
}
|
|
734
|
+
case "tool_update": {
|
|
735
|
+
const id = runningToolId(parts, ev.callId, ev.name);
|
|
736
|
+
return id === null ? parts : parts.map((p) => p.kind === "tool_call" && p.id === id ? {
|
|
737
|
+
...p,
|
|
738
|
+
args: ev.args ?? p.args,
|
|
739
|
+
argsRewritten: true
|
|
740
|
+
} : p);
|
|
741
|
+
}
|
|
742
|
+
case "tool_end": {
|
|
743
|
+
const id = runningToolId(parts, ev.callId, ev.name);
|
|
744
|
+
const call = parts.find((p) => p.kind === "tool_call" && p.id === id);
|
|
745
|
+
if (id === null || call?.kind !== "tool_call") return parts;
|
|
746
|
+
const result = ev.result ?? "";
|
|
747
|
+
return applyPartDelta(parts, {
|
|
748
|
+
type: "tool_end",
|
|
749
|
+
id,
|
|
750
|
+
result,
|
|
751
|
+
isError: !!ev.isError || resultFailed(call.name, result)
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
case "images": {
|
|
755
|
+
const images = imageAttachments(ev.images, imageUrl);
|
|
756
|
+
return images.length ? [...parts, {
|
|
757
|
+
kind: "image",
|
|
758
|
+
images
|
|
759
|
+
}] : parts;
|
|
760
|
+
}
|
|
761
|
+
case "subagent_start": return applyPartDelta(parts, {
|
|
762
|
+
type: "subagent_start",
|
|
763
|
+
id: ev.id ?? String(ev.seq),
|
|
764
|
+
...ev.agentType !== void 0 ? { agentType: ev.agentType } : {},
|
|
765
|
+
...ev.color !== void 0 ? { color: ev.color } : {},
|
|
766
|
+
...ev.background !== void 0 ? { background: ev.background } : {},
|
|
767
|
+
...ev.name !== void 0 ? { description: ev.name } : {}
|
|
768
|
+
});
|
|
769
|
+
case "subagent_action": return applyPartDelta(parts, {
|
|
770
|
+
type: "subagent_action",
|
|
771
|
+
id: ev.id ?? "",
|
|
772
|
+
action: ev.content ?? ""
|
|
773
|
+
});
|
|
774
|
+
case "subagent_end": return applyPartDelta(parts, {
|
|
775
|
+
type: "subagent_end",
|
|
776
|
+
id: ev.id ?? "",
|
|
777
|
+
ok: !ev.isError,
|
|
778
|
+
...ev.result !== void 0 ? { summary: ev.result } : {},
|
|
779
|
+
...ev.interrupted ? { interrupted: true } : {}
|
|
780
|
+
});
|
|
781
|
+
case "notice":
|
|
782
|
+
case "warning": return ev.recorded && !ev.turnId ? null : applyPartDelta(parts, {
|
|
783
|
+
type: ev.type,
|
|
784
|
+
text: (ev.content ?? "").trim()
|
|
785
|
+
});
|
|
786
|
+
case "error": return stoppedTurn(ev) ? [...interruptRunningCalls(parts), {
|
|
787
|
+
kind: "notice",
|
|
788
|
+
text: INTERRUPTED_NOTICE
|
|
789
|
+
}] : [...parts, {
|
|
790
|
+
kind: "error",
|
|
791
|
+
text: ev.content ?? ""
|
|
792
|
+
}];
|
|
793
|
+
case "decision": {
|
|
794
|
+
if (ev.decision?.answers?.length) return answeredAsk(parts, ev.decision);
|
|
795
|
+
const decision = ev.decision && toolDecision(ev.decision);
|
|
796
|
+
return decision && ev.decision ? applyPartDelta(parts, {
|
|
797
|
+
type: "tool_decision",
|
|
798
|
+
id: ev.decision.callId,
|
|
799
|
+
decision
|
|
800
|
+
}) : parts;
|
|
801
|
+
}
|
|
802
|
+
case "approval_request": return ev.approval ? applyPartDelta(parts, {
|
|
803
|
+
type: "approval_request",
|
|
804
|
+
approval: approvalToPart(ev.approval, words)
|
|
805
|
+
}) : parts;
|
|
806
|
+
default: return null;
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
function controlEffects(ev) {
|
|
810
|
+
switch (ev.type) {
|
|
811
|
+
case "state": return ev.state ? [{
|
|
812
|
+
type: "state",
|
|
813
|
+
state: ev.state
|
|
814
|
+
}] : [];
|
|
815
|
+
case "cleared": return ev.newId ? [{
|
|
816
|
+
type: "cleared",
|
|
817
|
+
newId: ev.newId
|
|
818
|
+
}] : [];
|
|
819
|
+
case "history_changed": return [{ type: "refetch" }];
|
|
820
|
+
default: return [];
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function parseGoalProgress(text) {
|
|
824
|
+
try {
|
|
825
|
+
return JSON.parse(text ?? "");
|
|
826
|
+
} catch {
|
|
827
|
+
return null;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
var TURN_VERBS = [
|
|
831
|
+
"baked",
|
|
832
|
+
"brewed",
|
|
833
|
+
"churned",
|
|
834
|
+
"cogitated",
|
|
835
|
+
"cooked",
|
|
836
|
+
"crunched",
|
|
837
|
+
"sauteed",
|
|
838
|
+
"worked"
|
|
839
|
+
];
|
|
840
|
+
function deliveryLine(n, words) {
|
|
841
|
+
return words ? words.deliveryLine(n) : subagentDeliveryLine(n);
|
|
842
|
+
}
|
|
843
|
+
function isPlanFile(path) {
|
|
844
|
+
const slashed = path.replaceAll("\\", "/");
|
|
845
|
+
const base = slashed.slice(slashed.lastIndexOf("/") + 1).toLowerCase();
|
|
846
|
+
if (base === "plan.md") return true;
|
|
847
|
+
return base.endsWith(".md") && (slashed.startsWith("plans/") || slashed.includes("/plans/"));
|
|
848
|
+
}
|
|
849
|
+
/** An edit to the plan file, whose diff the plan shows anyway, so it stays behind its row as in the terminal. */
|
|
850
|
+
function planFileEdit(name, argsJSON) {
|
|
851
|
+
if (name !== "EditFile" && name !== "WriteFile") return false;
|
|
852
|
+
try {
|
|
853
|
+
const args = JSON.parse(argsJSON);
|
|
854
|
+
const path = args && typeof args === "object" ? args["path"] : void 0;
|
|
855
|
+
return typeof path === "string" && isPlanFile(path);
|
|
856
|
+
} catch {
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
/** The verb the terminal picks for a turn, so the same turn reads the same in both. */
|
|
861
|
+
function turnVerb(endedAt) {
|
|
862
|
+
return TURN_VERBS[fnv32a(goUtcTime(endedAt)) % TURN_VERBS.length] ?? "worked";
|
|
863
|
+
}
|
|
864
|
+
function goUtcTime(wire) {
|
|
865
|
+
const m = /^(.*?:\d{2})(\.\d+)?(Z|[+-]\d{2}:\d{2})$/.exec(wire);
|
|
866
|
+
if (!m) return wire;
|
|
867
|
+
const utc = DateTime.fromISO(`${m[1]}${m[3]}`, { setZone: true }).toUTC();
|
|
868
|
+
const fraction = (m[2] ?? "").replace(/0+$/, "").replace(/\.$/, "");
|
|
869
|
+
return `${utc.toFormat("yyyy-MM-dd'T'HH:mm:ss")}${fraction}Z`;
|
|
870
|
+
}
|
|
871
|
+
function fnv32a(text) {
|
|
872
|
+
let hash = 2166136261;
|
|
873
|
+
for (const byte of new TextEncoder().encode(text)) {
|
|
874
|
+
hash ^= byte;
|
|
875
|
+
hash = Math.imul(hash, 16777619) >>> 0;
|
|
876
|
+
}
|
|
877
|
+
return hash;
|
|
878
|
+
}
|
|
879
|
+
/** A loop stopped with nothing to report files nothing in the terminal, and
|
|
880
|
+
* nothing here. */
|
|
881
|
+
function goalText(p, goalNotice) {
|
|
882
|
+
if (!p || !goalNotice || !p.err && p.outcome === "stopped") return null;
|
|
883
|
+
return goalNotice(p);
|
|
884
|
+
}
|
|
885
|
+
function recapText(recap, recapNotice) {
|
|
886
|
+
return recap && recapNotice ? recapNotice(recap) : null;
|
|
887
|
+
}
|
|
888
|
+
/** Between turns a notice stands alone rather than opening a turn. */
|
|
889
|
+
function appendNotice(state, text, detail, kind = "notice") {
|
|
890
|
+
const notice = {
|
|
891
|
+
kind,
|
|
892
|
+
text,
|
|
893
|
+
...detail ? { detail } : {}
|
|
894
|
+
};
|
|
895
|
+
return state.streaming ? {
|
|
896
|
+
...state,
|
|
897
|
+
streaming: {
|
|
898
|
+
...state.streaming,
|
|
899
|
+
parts: [...state.streaming.parts ?? [], notice]
|
|
900
|
+
}
|
|
901
|
+
} : {
|
|
902
|
+
...state,
|
|
903
|
+
messages: [...state.messages, assistantMessage([notice])]
|
|
904
|
+
};
|
|
905
|
+
}
|
|
906
|
+
/** The session files its notice above the reply still streaming, which reaches the history when it ends. */
|
|
907
|
+
function placeSessionNotice(state, text, kind) {
|
|
908
|
+
if (!state.streaming) return appendNotice(state, text, void 0, kind);
|
|
909
|
+
const parts = state.streaming.parts ?? [];
|
|
910
|
+
let at = parts.length;
|
|
911
|
+
while (at > 0 && ["text", "reasoning"].includes(parts[at - 1]?.kind ?? "")) at--;
|
|
912
|
+
return {
|
|
913
|
+
...state,
|
|
914
|
+
streaming: {
|
|
915
|
+
...state.streaming,
|
|
916
|
+
parts: [
|
|
917
|
+
...parts.slice(0, at),
|
|
918
|
+
{
|
|
919
|
+
kind,
|
|
920
|
+
text
|
|
921
|
+
},
|
|
922
|
+
...parts.slice(at)
|
|
923
|
+
]
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
/** A continuing step opens to its goal and why it isn't met, as the terminal's does; other steps say all there is. */
|
|
928
|
+
function goalDetail(p, words) {
|
|
929
|
+
if (!p || !words || p.outcome === "achieved" || p.err || p.resumeAt || !p.reason?.trim()) return null;
|
|
930
|
+
return words.goalDetail(p);
|
|
931
|
+
}
|
|
932
|
+
var SUBAGENT_EVENTS = /* @__PURE__ */ new Set([
|
|
933
|
+
"subagent_start",
|
|
934
|
+
"subagent_action",
|
|
935
|
+
"subagent_end"
|
|
936
|
+
]);
|
|
937
|
+
/** Keeps the auto-reviewer's reviews under way for whoever asked: a review
|
|
938
|
+
* start adds its call, and a verdict, or a review the turn cut short, takes
|
|
939
|
+
* it away. */
|
|
940
|
+
function trackReview(state, ev) {
|
|
941
|
+
const d = ev.decision;
|
|
942
|
+
if (!d?.callId || ev.type !== "decision" && ev.type !== "subagent_decision") return state;
|
|
943
|
+
const scope = ev.type === "subagent_decision" ? ev.id ?? "" : "";
|
|
944
|
+
const reviews = state.reviews ?? {};
|
|
945
|
+
const under = reviews[scope] ?? [];
|
|
946
|
+
const decided = state.decided ?? [];
|
|
947
|
+
if (d.status === "reviewing") {
|
|
948
|
+
if (decided.includes(d.callId) || under.includes(d.callId)) return state;
|
|
949
|
+
return {
|
|
950
|
+
...state,
|
|
951
|
+
reviews: {
|
|
952
|
+
...reviews,
|
|
953
|
+
[scope]: [...under, d.callId]
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
return {
|
|
958
|
+
...state,
|
|
959
|
+
reviews: {
|
|
960
|
+
...reviews,
|
|
961
|
+
[scope]: under.filter((id) => id !== d.callId)
|
|
962
|
+
},
|
|
963
|
+
decided: [...decided, d.callId].slice(-200)
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
/** How many of the main conversation's calls the auto-reviewer is deciding on. */
|
|
967
|
+
function reviewingCount(state) {
|
|
968
|
+
return state?.reviews?.[""]?.length ?? 0;
|
|
969
|
+
}
|
|
970
|
+
/** Shows a subagent's reviews on its card, wherever its turn left it. */
|
|
971
|
+
function showReviews(state, id) {
|
|
972
|
+
const count = state.reviews?.[id]?.length ?? 0;
|
|
973
|
+
const update = (m) => m.parts?.some((p) => p.kind === "subagent" && p.id === id) ? {
|
|
974
|
+
...m,
|
|
975
|
+
parts: applyPartDelta(m.parts ?? [], {
|
|
976
|
+
type: "subagent_reviewing",
|
|
977
|
+
id,
|
|
978
|
+
count
|
|
979
|
+
})
|
|
980
|
+
} : m;
|
|
981
|
+
return {
|
|
982
|
+
...state,
|
|
983
|
+
messages: state.messages.map(update),
|
|
984
|
+
streaming: state.streaming ? update(state.streaming) : null
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
/** A subagent can outlive the turn that started it, and its later frames
|
|
988
|
+
* update its card where that turn left it rather than open a new turn. */
|
|
989
|
+
function applyToSettledSubagent(state, ev) {
|
|
990
|
+
if (!SUBAGENT_EVENTS.has(ev.type) || !ev.id) return null;
|
|
991
|
+
const holds = (m) => !!m.parts?.some((p) => p.kind === "subagent" && p.id === ev.id);
|
|
992
|
+
if (state.streaming && holds(state.streaming)) return ev.type === "subagent_start" ? state : null;
|
|
993
|
+
const idx = state.messages.findLastIndex(holds);
|
|
994
|
+
const message = state.messages[idx];
|
|
995
|
+
if (!message) return null;
|
|
996
|
+
if (ev.type === "subagent_start") return state;
|
|
997
|
+
const parts = applyPartEvent(message.parts ?? [], ev);
|
|
998
|
+
return parts ? {
|
|
999
|
+
...state,
|
|
1000
|
+
messages: state.messages.map((m, i) => i === idx ? {
|
|
1001
|
+
...m,
|
|
1002
|
+
parts
|
|
1003
|
+
} : m)
|
|
1004
|
+
} : null;
|
|
1005
|
+
}
|
|
1006
|
+
var ENDS_THOUGHT = /* @__PURE__ */ new Set([
|
|
1007
|
+
"chunk",
|
|
1008
|
+
"tool_start",
|
|
1009
|
+
"subagent_start",
|
|
1010
|
+
"approval_request",
|
|
1011
|
+
"notice",
|
|
1012
|
+
"warning",
|
|
1013
|
+
"error",
|
|
1014
|
+
"done",
|
|
1015
|
+
"user_message",
|
|
1016
|
+
"turn_start"
|
|
1017
|
+
]);
|
|
1018
|
+
/** Times a thought from when its model step began until something else arrives, as the terminal does. */
|
|
1019
|
+
function timeThought(state, ev, now) {
|
|
1020
|
+
const last = state.streaming?.parts?.at(-1);
|
|
1021
|
+
if (ev.type === "reasoning") return last?.kind === "reasoning" ? state : {
|
|
1022
|
+
...state,
|
|
1023
|
+
thoughtStartedAt: state.stepStartedAt ?? now()
|
|
1024
|
+
};
|
|
1025
|
+
if (ev.type === "tool_end") return {
|
|
1026
|
+
...state,
|
|
1027
|
+
stepStartedAt: now()
|
|
1028
|
+
};
|
|
1029
|
+
if (!ENDS_THOUGHT.has(ev.type)) return state;
|
|
1030
|
+
const at = now();
|
|
1031
|
+
const opensStep = ev.type === "user_message" || ev.type === "turn_start";
|
|
1032
|
+
if (!state.streaming || last?.kind !== "reasoning" || last.durationMs !== void 0 || !state.thoughtStartedAt) return opensStep ? {
|
|
1033
|
+
...state,
|
|
1034
|
+
stepStartedAt: at,
|
|
1035
|
+
thoughtStartedAt: null
|
|
1036
|
+
} : state;
|
|
1037
|
+
const durationMs = Math.max(0, DateTime.fromISO(at).diff(DateTime.fromISO(state.thoughtStartedAt)).as("milliseconds"));
|
|
1038
|
+
return {
|
|
1039
|
+
...state,
|
|
1040
|
+
streaming: {
|
|
1041
|
+
...state.streaming,
|
|
1042
|
+
parts: [...(state.streaming.parts ?? []).slice(0, -1), {
|
|
1043
|
+
...last,
|
|
1044
|
+
durationMs
|
|
1045
|
+
}]
|
|
1046
|
+
},
|
|
1047
|
+
thoughtStartedAt: null,
|
|
1048
|
+
stepStartedAt: at
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
function finishTurn(state) {
|
|
1052
|
+
const done = state.streaming ? {
|
|
1053
|
+
...state,
|
|
1054
|
+
streaming: {
|
|
1055
|
+
...state.streaming,
|
|
1056
|
+
parts: finalizeParts(state.streaming.parts ?? [])
|
|
1057
|
+
}
|
|
1058
|
+
} : state;
|
|
1059
|
+
const { "": _ended, ...children } = state.reviews ?? {};
|
|
1060
|
+
return {
|
|
1061
|
+
...placeNotes(done),
|
|
1062
|
+
turnEnded: true,
|
|
1063
|
+
turnStartedAt: null,
|
|
1064
|
+
opening: null,
|
|
1065
|
+
waitingForUsageLimit: false,
|
|
1066
|
+
usageLimitResumeAt: null,
|
|
1067
|
+
reviews: children
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
/** Where the model speaks after being handed a note, the note joins the
|
|
1071
|
+
* transcript ahead of what it says. */
|
|
1072
|
+
function placeNotes(state) {
|
|
1073
|
+
return {
|
|
1074
|
+
...state,
|
|
1075
|
+
messages: [
|
|
1076
|
+
...state.messages,
|
|
1077
|
+
...state.streaming ? [state.streaming] : [],
|
|
1078
|
+
...state.notes
|
|
1079
|
+
],
|
|
1080
|
+
notes: [],
|
|
1081
|
+
streaming: null
|
|
1082
|
+
};
|
|
1083
|
+
}
|
|
1084
|
+
function onlyAnsweredApprovals(message) {
|
|
1085
|
+
const parts = message?.parts ?? [];
|
|
1086
|
+
return parts.length > 0 && parts.every((p) => p.kind === "approval" && p.resolved);
|
|
1087
|
+
}
|
|
1088
|
+
var OUTPUT_EVENTS = /* @__PURE__ */ new Set([
|
|
1089
|
+
"chunk",
|
|
1090
|
+
"reasoning",
|
|
1091
|
+
"approval_request"
|
|
1092
|
+
]);
|
|
1093
|
+
/** A hidden tool draws nothing, so a note placed there leaves the turn idle. */
|
|
1094
|
+
function speaks(ev) {
|
|
1095
|
+
if (ev.type === "tool_start") return !hiddenTranscriptTool(ev.name ?? "");
|
|
1096
|
+
return OUTPUT_EVENTS.has(ev.type);
|
|
1097
|
+
}
|
|
1098
|
+
var DISCARDED_QUEUE = /^interrupted: discarded \d+ queued message/;
|
|
1099
|
+
/** By id when the send is known: the daemon echoes an MCP prompt expanded. */
|
|
1100
|
+
function takeOwn(list, id, text) {
|
|
1101
|
+
const idx = list.findIndex((m) => id ? m.id === id : m.content === text);
|
|
1102
|
+
return idx === -1 ? [void 0, list] : [list[idx], list.filter((_, i) => i !== idx)];
|
|
1103
|
+
}
|
|
1104
|
+
/** An own message's echo stands in for the copy held until its turn began. */
|
|
1105
|
+
function takeQueued(state, id, text) {
|
|
1106
|
+
const [message, queued] = takeOwn(state.queued, id, text);
|
|
1107
|
+
return message ? {
|
|
1108
|
+
...state,
|
|
1109
|
+
messages: [...state.messages, message],
|
|
1110
|
+
queued
|
|
1111
|
+
} : state;
|
|
1112
|
+
}
|
|
1113
|
+
/** The echo also says how each of the message's pastes was sent. */
|
|
1114
|
+
function takeEcho(state, id, ev, imageUrl) {
|
|
1115
|
+
const placed = takeQueued(state, id, ev.content ?? "");
|
|
1116
|
+
return {
|
|
1117
|
+
...placed,
|
|
1118
|
+
messages: withSentPastesAndImages(placed.messages, ev, id, imageUrl)
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
/** A handed-over note leaves the steered list, or arrives from another window. */
|
|
1122
|
+
function noteHandedOver(state, id, ev, imageUrl) {
|
|
1123
|
+
const text = ev.content ?? "";
|
|
1124
|
+
const [note, steered] = takeOwn(state.steered, id, text);
|
|
1125
|
+
return {
|
|
1126
|
+
...state,
|
|
1127
|
+
steered,
|
|
1128
|
+
notes: note ? withSentPastesAndImages([...state.notes, note], ev, note.id, imageUrl) : [...state.notes, userMessage(text, false, void 0, ev.pastes, ev.images, imageUrl)]
|
|
1129
|
+
};
|
|
1130
|
+
}
|
|
1131
|
+
function nowIso() {
|
|
1132
|
+
return DateTime.now().toISO();
|
|
1133
|
+
}
|
|
1134
|
+
/** Out-of-band frames are classified before seq dedupe, as the TUI does. */
|
|
1135
|
+
function applyStreamEvent(state, ev, options = {}) {
|
|
1136
|
+
const now = options.now ?? nowIso;
|
|
1137
|
+
if (ev.type === "cleared" && options.sessionId && ev.newId === options.sessionId && ev.seq > state.cursor) return {
|
|
1138
|
+
state: {
|
|
1139
|
+
...state,
|
|
1140
|
+
messages: [],
|
|
1141
|
+
streaming: null,
|
|
1142
|
+
seededTerminal: null,
|
|
1143
|
+
turnEnded: true,
|
|
1144
|
+
turnStartedAt: null,
|
|
1145
|
+
waitingForUsageLimit: false,
|
|
1146
|
+
usageLimitResumeAt: null,
|
|
1147
|
+
cursor: ev.seq
|
|
1148
|
+
},
|
|
1149
|
+
effects: []
|
|
1150
|
+
};
|
|
1151
|
+
if (ev.type === "replay_truncated") return {
|
|
1152
|
+
state: {
|
|
1153
|
+
...state,
|
|
1154
|
+
cursor: Math.max(state.cursor, ev.seq)
|
|
1155
|
+
},
|
|
1156
|
+
effects: [{ type: "refetch" }]
|
|
1157
|
+
};
|
|
1158
|
+
if (ev.type === "cleared" && ev.seq <= (state.seededSeq ?? 0) && ev.newId !== options.sessionId) return {
|
|
1159
|
+
state: {
|
|
1160
|
+
...state,
|
|
1161
|
+
cursor: Math.max(state.cursor, ev.seq)
|
|
1162
|
+
},
|
|
1163
|
+
effects: []
|
|
1164
|
+
};
|
|
1165
|
+
if (ev.seq === 0) return {
|
|
1166
|
+
state,
|
|
1167
|
+
effects: ev.type === "notice" || ev.type === "warning" ? [{
|
|
1168
|
+
type: "notice",
|
|
1169
|
+
text: ev.content ?? "",
|
|
1170
|
+
warn: ev.type === "warning"
|
|
1171
|
+
}] : []
|
|
1172
|
+
};
|
|
1173
|
+
if (ev.seq <= state.cursor) return {
|
|
1174
|
+
state,
|
|
1175
|
+
effects: []
|
|
1176
|
+
};
|
|
1177
|
+
if (ev.id && !ev.type.startsWith("subagent_")) return {
|
|
1178
|
+
state: {
|
|
1179
|
+
...state,
|
|
1180
|
+
cursor: ev.seq
|
|
1181
|
+
},
|
|
1182
|
+
effects: []
|
|
1183
|
+
};
|
|
1184
|
+
let next = {
|
|
1185
|
+
...state,
|
|
1186
|
+
cursor: ev.seq,
|
|
1187
|
+
...ev.turnEnd ? { turnEndSeq: ev.seq } : {}
|
|
1188
|
+
};
|
|
1189
|
+
if (next.notes.length > 0 && speaks(ev)) next = placeNotes(next);
|
|
1190
|
+
if (ev.type === "notice" && DISCARDED_QUEUE.test(ev.content ?? "")) next = {
|
|
1191
|
+
...next,
|
|
1192
|
+
queued: []
|
|
1193
|
+
};
|
|
1194
|
+
const lastMessage = next.streaming ?? next.messages.at(-1);
|
|
1195
|
+
const lastPart = lastMessage?.parts?.at(-1);
|
|
1196
|
+
if (ev.type === "error" && next.seededTerminal && ev.turnId === next.seededTerminal.turnId && lastPart === next.seededTerminal.part && next.notes.length === 0 && lastMessage?.role === "assistant" && (stoppedTurn(ev) ? lastPart?.kind === "notice" && lastPart.text === INTERRUPTED_NOTICE : lastPart?.kind === "error" && lastPart.text === (ev.content ?? ""))) return {
|
|
1197
|
+
state: {
|
|
1198
|
+
...finishTurn(next),
|
|
1199
|
+
seededTerminal: null,
|
|
1200
|
+
turnEnded: true,
|
|
1201
|
+
turnStartedAt: null
|
|
1202
|
+
},
|
|
1203
|
+
effects: []
|
|
1204
|
+
};
|
|
1205
|
+
if (ev.type === "error" || ev.type === "done" || ev.type === "user_message" || ev.type === "user_note" || ev.type === "tool_start" || ev.type === "subagent_start" || speaks(ev)) next = {
|
|
1206
|
+
...next,
|
|
1207
|
+
seededTerminal: null
|
|
1208
|
+
};
|
|
1209
|
+
next = trackReview(next, ev);
|
|
1210
|
+
if (ev.type === "subagent_end" && ev.id && next.reviews?.[ev.id]) {
|
|
1211
|
+
const { [ev.id]: _ended, ...rest } = next.reviews;
|
|
1212
|
+
next = {
|
|
1213
|
+
...next,
|
|
1214
|
+
reviews: rest
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
if (ev.type === "subagent_decision") return {
|
|
1218
|
+
state: showReviews(next, ev.id ?? ""),
|
|
1219
|
+
effects: []
|
|
1220
|
+
};
|
|
1221
|
+
const settled = applyToSettledSubagent(next, ev);
|
|
1222
|
+
if (settled) return {
|
|
1223
|
+
state: settled,
|
|
1224
|
+
effects: []
|
|
1225
|
+
};
|
|
1226
|
+
next = timeThought(next, ev, now);
|
|
1227
|
+
const parts = applyPartEvent(next.streaming?.parts ?? [], ev, options.imageUrl, options.words);
|
|
1228
|
+
if (parts !== null && (next.streaming !== null || parts.length > 0)) {
|
|
1229
|
+
const streaming = next.streaming ?? assistantMessage();
|
|
1230
|
+
const opened = next.streaming === null && next.turnEnded;
|
|
1231
|
+
next = {
|
|
1232
|
+
...next,
|
|
1233
|
+
streaming: {
|
|
1234
|
+
...streaming,
|
|
1235
|
+
parts
|
|
1236
|
+
},
|
|
1237
|
+
turnStartedAt: next.turnStartedAt ?? (opened ? now() : null)
|
|
1238
|
+
};
|
|
1239
|
+
if (ev.type === "error") next = finishTurn(next);
|
|
1240
|
+
return {
|
|
1241
|
+
state: next,
|
|
1242
|
+
effects: []
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
switch (ev.type) {
|
|
1246
|
+
case "user_message": {
|
|
1247
|
+
const ownEcho = !!options.echoOf || !!ev.turnId && !!options.ownTurnIds?.has(ev.turnId) || !!ev.content && !!options.ownTexts?.has(ev.content);
|
|
1248
|
+
next = finishTurn(next);
|
|
1249
|
+
next = ownEcho ? takeEcho(next, options.echoOf, ev, options.imageUrl) : {
|
|
1250
|
+
...next,
|
|
1251
|
+
messages: [...next.messages, userMessage(ev.content ?? "", false, void 0, ev.pastes, ev.images, options.imageUrl)]
|
|
1252
|
+
};
|
|
1253
|
+
next = {
|
|
1254
|
+
...next,
|
|
1255
|
+
turnEnded: false,
|
|
1256
|
+
turnStartedAt: now()
|
|
1257
|
+
};
|
|
1258
|
+
break;
|
|
1259
|
+
}
|
|
1260
|
+
case "turn_start":
|
|
1261
|
+
next = {
|
|
1262
|
+
...finishTurn(next),
|
|
1263
|
+
turnEnded: false,
|
|
1264
|
+
turnStartedAt: now()
|
|
1265
|
+
};
|
|
1266
|
+
break;
|
|
1267
|
+
case "user_note":
|
|
1268
|
+
next = noteHandedOver(next, options.echoOf, ev, options.imageUrl);
|
|
1269
|
+
break;
|
|
1270
|
+
case "approval_resolved":
|
|
1271
|
+
if (ev.approvalId) next = resolveApprovalEverywhere(next, ev.approvalId);
|
|
1272
|
+
if (next.turnEnded && onlyAnsweredApprovals(next.streaming)) next = finishTurn(next);
|
|
1273
|
+
break;
|
|
1274
|
+
case "usage_limit_wait":
|
|
1275
|
+
next = {
|
|
1276
|
+
...next,
|
|
1277
|
+
waitingForUsageLimit: !!ev.content,
|
|
1278
|
+
usageLimitResumeAt: ev.content && ev.resumeAt || null
|
|
1279
|
+
};
|
|
1280
|
+
break;
|
|
1281
|
+
case "subagent_delivered": {
|
|
1282
|
+
const delivery = parseSubagentNotice(ev.notice ?? "");
|
|
1283
|
+
if (delivery && !delivery.silent) next = appendNotice(next, deliveryLine(delivery, options.words), delivery.body || void 0);
|
|
1284
|
+
break;
|
|
1285
|
+
}
|
|
1286
|
+
case "done": {
|
|
1287
|
+
const turnEnd = ev.turnEnd && options.words ? options.words.turnEnd(ev.turnEnd, turnVerb(ev.turnEnd.endedAt), (ev.turnEnd.pendingAgents ?? 0) > 0) : null;
|
|
1288
|
+
if (turnEnd) next = appendNotice(next, turnEnd);
|
|
1289
|
+
next = finishTurn(next);
|
|
1290
|
+
if (next.steered.length > 0) next = {
|
|
1291
|
+
...next,
|
|
1292
|
+
messages: [...next.messages, ...next.steered],
|
|
1293
|
+
steered: []
|
|
1294
|
+
};
|
|
1295
|
+
break;
|
|
1296
|
+
}
|
|
1297
|
+
case "goal": {
|
|
1298
|
+
const text = goalText(ev.goalProgress, options.goalNotice);
|
|
1299
|
+
if (text) next = appendNotice(next, text, goalDetail(ev.goalProgress, options.words) ?? void 0);
|
|
1300
|
+
break;
|
|
1301
|
+
}
|
|
1302
|
+
case "recap": {
|
|
1303
|
+
const text = recapText(ev.content, options.recapNotice);
|
|
1304
|
+
if (text) next = appendNotice(next, text);
|
|
1305
|
+
break;
|
|
1306
|
+
}
|
|
1307
|
+
case "notice":
|
|
1308
|
+
case "warning": if (ev.recorded) next = placeSessionNotice(next, ev.content ?? "", ev.type);
|
|
1309
|
+
}
|
|
1310
|
+
return {
|
|
1311
|
+
state: next,
|
|
1312
|
+
effects: controlEffects(ev)
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
export { TURN_VERBS, appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, approvalAnswerBody, approvalToPart, bashRunning, dropMessage, historyToMessages, initialTranscript, liveSessionStatus, markQueued, pendingApprovals, pendingMessages, resolveApprovalEverywhere, resumeCursor, reviewingCount, subagentFrame, subagentSessionDetail, todosFromArgs, toolDecision, turnRunning, turnVerb, userInterruptedTool };
|