@yaklabs-ai/plugin-sdk 0.13.6 → 0.13.8

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.
@@ -0,0 +1,680 @@
1
+ // packages/time/dist/temporal.js
2
+ import { Temporal as Polyfill } from "@js-temporal/polyfill";
3
+ var engineTemporal = Reflect.get(globalThis, "Temporal");
4
+ var Temporal = engineTemporal ?? Polyfill;
5
+
6
+ // packages/plugin-protocol/dist/transcript.js
7
+ import { z } from "zod";
8
+ var WorkBlockCoverageSchema = z.strictObject({
9
+ firstToolResultEntryId: z.string().min(1),
10
+ lastToolResultEntryId: z.string().min(1),
11
+ toolResultEntryIds: z.array(z.string().min(1)).min(1).max(4096)
12
+ }).refine((value) => value.firstToolResultEntryId === value.toolResultEntryIds[0] && value.lastToolResultEntryId === value.toolResultEntryIds.at(-1) && new Set(value.toolResultEntryIds).size === value.toolResultEntryIds.length);
13
+ var RUN_STARTED_CUSTOM_TYPE = "session_run_started";
14
+ var RUN_TERMINAL_CUSTOM_TYPE = "session_run_state";
15
+
16
+ // packages/plugin-protocol/dist/transcript-review.js
17
+ var RUN_INPUT_CUSTOM_TYPE = "session_run_input";
18
+ var HUMAN_INPUT_CAUSES = /* @__PURE__ */ new Set([
19
+ "human_send",
20
+ "human_resume",
21
+ "queued_human"
22
+ ]);
23
+ var TASK_CONTEXT_TYPES = /* @__PURE__ */ new Set([
24
+ "subagent_result",
25
+ "collaboration_message",
26
+ "background_task_result",
27
+ "ui_action",
28
+ "user_pasted_content",
29
+ "message_draft_sent",
30
+ "daemon_service_turn"
31
+ ]);
32
+ var SYSTEM_PROMPT_CUSTOM_TYPES = /* @__PURE__ */ new Set([
33
+ "resolved_prompt",
34
+ "composition_snapshot",
35
+ "system_prompt_delta"
36
+ ]);
37
+ var ATTEMPT_SUBJECT_MAX_CHARS = 2e3;
38
+ var SHELL_TOOL_NAMES = /* @__PURE__ */ new Set(["bash", "shell"]);
39
+ var RUN_MARKER_TYPES = /* @__PURE__ */ new Set([
40
+ RUN_STARTED_CUSTOM_TYPE,
41
+ RUN_INPUT_CUSTOM_TYPE,
42
+ RUN_TERMINAL_CUSTOM_TYPE
43
+ ]);
44
+ function isObj(value) {
45
+ return typeof value === "object" && value !== null && !Array.isArray(value);
46
+ }
47
+ function str(value) {
48
+ return typeof value === "string" ? value : void 0;
49
+ }
50
+ function num(value) {
51
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
52
+ }
53
+ function reviewInstantMs(value) {
54
+ const asNumber = num(value);
55
+ if (asNumber !== void 0)
56
+ return asNumber;
57
+ const text = str(value);
58
+ if (text === void 0)
59
+ return void 0;
60
+ try {
61
+ return Temporal.Instant.from(text).epochMilliseconds;
62
+ } catch {
63
+ return void 0;
64
+ }
65
+ }
66
+ function formatReviewInstant(ms) {
67
+ return Temporal.Instant.fromEpochMilliseconds(ms).toString({
68
+ fractionalSecondDigits: 3
69
+ });
70
+ }
71
+ var instantMs = reviewInstantMs;
72
+ function messageOf(entry) {
73
+ return entry.type === "message" && isObj(entry.message) ? entry.message : void 0;
74
+ }
75
+ function reviewContentBlocks(content) {
76
+ if (typeof content === "string")
77
+ return [{ type: "text", text: content }];
78
+ return Array.isArray(content) ? content.filter(isObj) : [];
79
+ }
80
+ function reviewContentText(content) {
81
+ return reviewContentBlocks(content).filter((block) => block.type === "text").map((block) => str(block.text) ?? "").join("\n");
82
+ }
83
+ function isHumanVerifiedMessage(message) {
84
+ return isObj(message) && message.role === "user" && typeof message.clientIntentId === "string" && isObj(message.details) && typeof message.details.deliveryId === "string";
85
+ }
86
+ function reviewContextOf(entry) {
87
+ if (!isObj(entry))
88
+ return void 0;
89
+ const message = messageOf(entry);
90
+ const source = message?.role === "custom" ? message : entry.type === "custom_message" ? entry : void 0;
91
+ if (source === void 0)
92
+ return void 0;
93
+ const customType = str(source.customType);
94
+ return {
95
+ ...customType === void 0 ? {} : { customType },
96
+ ...isObj(source.details) ? { details: source.details } : {},
97
+ content: source.content
98
+ };
99
+ }
100
+ var ROLE_CLASS = {
101
+ assistant: "assistant",
102
+ toolResult: "tool_result",
103
+ system: "system"
104
+ };
105
+ var ROLE_KIND = {
106
+ user: "user",
107
+ assistant: "assistant",
108
+ toolResult: "tool_result"
109
+ };
110
+ function describeReviewEntry(entry) {
111
+ if (!isObj(entry))
112
+ return { kind: "unreadable", recordClass: "unreadable" };
113
+ const entryId = str(entry.id);
114
+ const base = entryId === void 0 ? {} : { entryId };
115
+ const entryMs = instantMs(entry.timestamp);
116
+ const message = messageOf(entry);
117
+ if (message !== void 0) {
118
+ const role = str(message.role);
119
+ const timestampMs = instantMs(message.timestamp) ?? entryMs;
120
+ const time2 = timestampMs === void 0 ? {} : { timestampMs };
121
+ if (role === "custom") {
122
+ const context = reviewContextOf(entry);
123
+ const customType = context?.customType;
124
+ const clientIntentId = str(context?.details?.clientIntentId);
125
+ return {
126
+ ...base,
127
+ kind: `custom_message:${customType ?? ""}`,
128
+ recordClass: "context",
129
+ ...customType === void 0 ? {} : { customType },
130
+ ...time2,
131
+ ...clientIntentId === void 0 ? {} : { clientIntentId }
132
+ };
133
+ }
134
+ if (role === "user") {
135
+ const clientIntentId = isHumanVerifiedMessage(message) ? str(message.clientIntentId) : void 0;
136
+ return {
137
+ ...base,
138
+ kind: "user",
139
+ recordClass: clientIntentId === void 0 ? "user" : "human",
140
+ ...time2,
141
+ ...clientIntentId === void 0 ? {} : { clientIntentId }
142
+ };
143
+ }
144
+ return {
145
+ ...base,
146
+ kind: (role === void 0 ? void 0 : ROLE_KIND[role]) ?? "other",
147
+ recordClass: (role === void 0 ? void 0 : ROLE_CLASS[role]) ?? "other",
148
+ ...time2
149
+ };
150
+ }
151
+ const time = entryMs === void 0 ? {} : { timestampMs: entryMs };
152
+ switch (entry.type) {
153
+ case "custom": {
154
+ const customType = str(entry.customType) ?? "";
155
+ const data = isObj(entry.data) ? entry.data : {};
156
+ const markerMs = entryMs ?? num(data.acceptedAtMs) ?? num(data.runStartedAt) ?? num(data.at) ?? num(data.observedAt);
157
+ return {
158
+ ...base,
159
+ kind: `custom:${customType}`,
160
+ recordClass: RUN_MARKER_TYPES.has(customType) ? "run_marker" : "custom",
161
+ customType,
162
+ ...markerMs === void 0 ? {} : { timestampMs: markerMs }
163
+ };
164
+ }
165
+ case "custom_message": {
166
+ const context = reviewContextOf(entry);
167
+ const clientIntentId = str(context?.details?.clientIntentId);
168
+ return {
169
+ ...base,
170
+ kind: `custom_message:${context?.customType ?? ""}`,
171
+ recordClass: "context",
172
+ ...context?.customType === void 0 ? {} : { customType: context.customType },
173
+ ...time,
174
+ ...clientIntentId === void 0 ? {} : { clientIntentId }
175
+ };
176
+ }
177
+ case "compaction":
178
+ return {
179
+ ...base,
180
+ kind: "compaction",
181
+ recordClass: "compaction",
182
+ ...time
183
+ };
184
+ case "branch_summary":
185
+ return {
186
+ ...base,
187
+ kind: "branch_summary",
188
+ recordClass: "branch_summary",
189
+ ...time
190
+ };
191
+ default:
192
+ return { ...base, kind: "other", recordClass: "other", ...time };
193
+ }
194
+ }
195
+ var SHELL_HEADER_RE = /^(exit_code|duration|stdout|stderr):/;
196
+ var ANSI_RE = new RegExp([
197
+ "\\u001b\\[[0-?]*[ -/]*[@-~]",
198
+ "\\u001b\\][^\\u0007\\u001b]*(?:\\u0007|\\u001b\\\\)",
199
+ "\\u001b[@-Z\\\\-_]",
200
+ "\\[[0-9;]*m"
201
+ ].join("|"), "g");
202
+ var WRAPPER_LINE_RE = /^<\/?[A-Za-z][\w.-]*>$/;
203
+ var ERROR_PREFIX = "[error] ";
204
+ function errorLineText(line) {
205
+ const text = line.replace(ANSI_RE, "").trim();
206
+ if (text === "" || WRAPPER_LINE_RE.test(text))
207
+ return void 0;
208
+ const bare = text.startsWith(ERROR_PREFIX) ? text.slice(ERROR_PREFIX.length).trim() : text;
209
+ return bare === "" ? void 0 : bare;
210
+ }
211
+ function firstErrorLine(text) {
212
+ const lines = text.split(/\r?\n/);
213
+ const stderrAt = lines.findIndex((line) => line.replace(ANSI_RE, "").trim().startsWith("stderr:"));
214
+ if (stderrAt >= 0) {
215
+ const sameLine = errorLineText(lines[stderrAt].replace(ANSI_RE, "").trim().slice("stderr:".length));
216
+ if (sameLine !== void 0)
217
+ return sameLine;
218
+ for (const line of lines.slice(stderrAt + 1)) {
219
+ const found = errorLineText(line);
220
+ if (found !== void 0)
221
+ return found;
222
+ }
223
+ }
224
+ for (const line of lines) {
225
+ const found = errorLineText(line);
226
+ if (found !== void 0 && !SHELL_HEADER_RE.test(found))
227
+ return found;
228
+ }
229
+ return void 0;
230
+ }
231
+ var URI_VALUE_RE = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//;
232
+ function collectUris(value, into, depth = 0) {
233
+ if (depth > 8)
234
+ return;
235
+ if (typeof value === "string") {
236
+ if (URI_VALUE_RE.test(value))
237
+ into.push(value);
238
+ } else if (Array.isArray(value)) {
239
+ for (const item of value)
240
+ collectUris(item, into, depth + 1);
241
+ } else if (isObj(value)) {
242
+ for (const item of Object.values(value))
243
+ collectUris(item, into, depth + 1);
244
+ }
245
+ }
246
+ function subjectOf(tool, args) {
247
+ if (!isObj(args))
248
+ return void 0;
249
+ if (SHELL_TOOL_NAMES.has(tool) && typeof args.command === "string") {
250
+ return args.command;
251
+ }
252
+ return str(args.path) ?? str(args.uri) ?? str(args.url) ?? str(args.command);
253
+ }
254
+ function createTranscriptReview(options = {}) {
255
+ const untilMs = options.untilMs;
256
+ const records = [];
257
+ const byPosition = /* @__PURE__ */ new Map();
258
+ const byEntryId = /* @__PURE__ */ new Map();
259
+ const rawInputs = [];
260
+ const runs = /* @__PURE__ */ new Map();
261
+ const runOrder = [];
262
+ const humanByIntent = /* @__PURE__ */ new Map();
263
+ const contextByIntent = /* @__PURE__ */ new Map();
264
+ const attempts = [];
265
+ const attemptByBlock = /* @__PURE__ */ new Map();
266
+ const attemptsByPosition = /* @__PURE__ */ new Map();
267
+ const results = [];
268
+ const resultByPosition = /* @__PURE__ */ new Map();
269
+ const open = /* @__PURE__ */ new Map();
270
+ const answered = /* @__PURE__ */ new Map();
271
+ let cutAt;
272
+ let finished;
273
+ const runFor = (runId) => {
274
+ let run = runs.get(runId);
275
+ if (run === void 0) {
276
+ run = { runId };
277
+ runs.set(runId, run);
278
+ runOrder.push(runId);
279
+ }
280
+ return run;
281
+ };
282
+ const addResult = (position, entryId, message) => {
283
+ const toolCallId = str(message.toolCallId);
284
+ const details = isObj(message.details) ? message.details : {};
285
+ const isError = message.isError === true;
286
+ const exitCode = num(details.exitCode);
287
+ const timedOut = details.timedOut === true;
288
+ const failed = isError || exitCode !== void 0 && exitCode !== 0 || timedOut;
289
+ let pairing;
290
+ let attempt;
291
+ let candidates = [];
292
+ if (toolCallId === void 0) {
293
+ pairing = "no-id";
294
+ } else {
295
+ const stillOpen = open.get(toolCallId) ?? [];
296
+ if (stillOpen.length === 1) {
297
+ attempt = stillOpen.pop();
298
+ attempts[attempt].result = position;
299
+ const done = answered.get(toolCallId) ?? [];
300
+ done.push(attempt);
301
+ answered.set(toolCallId, done);
302
+ pairing = "paired";
303
+ } else if (stillOpen.length > 1) {
304
+ pairing = "ambiguous";
305
+ candidates = [...stillOpen];
306
+ for (const index of stillOpen) {
307
+ attempts[index].ambiguousResults.push(position);
308
+ }
309
+ } else if ((answered.get(toolCallId) ?? []).length > 0) {
310
+ attempt = answered.get(toolCallId).at(-1);
311
+ attempts[attempt].extraResults.push(position);
312
+ pairing = "extra";
313
+ } else {
314
+ pairing = "orphan";
315
+ }
316
+ }
317
+ const errorLine = failed ? firstErrorLine(reviewContentText(message.content)) : void 0;
318
+ const tool = str(message.toolName);
319
+ resultByPosition.set(position, results.length);
320
+ results.push({
321
+ position,
322
+ ...entryId === void 0 ? {} : { entryId },
323
+ ...toolCallId === void 0 ? {} : { toolCallId },
324
+ ...tool === void 0 ? {} : { tool },
325
+ pairing,
326
+ ...attempt === void 0 ? {} : { attempt },
327
+ candidates,
328
+ isError,
329
+ ...exitCode === void 0 ? {} : { exitCode },
330
+ timedOut,
331
+ failed,
332
+ ...errorLine === void 0 ? {} : { firstErrorLine: errorLine }
333
+ });
334
+ };
335
+ const add = (input) => {
336
+ if (finished !== void 0) {
337
+ throw new Error("createTranscriptReview: add() after finish()");
338
+ }
339
+ if (cutAt !== void 0)
340
+ return "stop";
341
+ const description = describeReviewEntry(input.entry);
342
+ if (untilMs !== void 0 && description.timestampMs !== void 0 && description.timestampMs >= untilMs) {
343
+ cutAt = {
344
+ position: input.position,
345
+ ...description.entryId === void 0 ? {} : { entryId: description.entryId },
346
+ timestampMs: description.timestampMs
347
+ };
348
+ return "stop";
349
+ }
350
+ const position = input.position;
351
+ const entry = isObj(input.entry) ? input.entry : void 0;
352
+ const message = entry === void 0 ? void 0 : messageOf(entry);
353
+ const hasProse = description.recordClass === "assistant" && reviewContentText(message?.content).trim() !== "";
354
+ byPosition.set(position, records.length);
355
+ if (description.entryId !== void 0) {
356
+ byEntryId.set(description.entryId, records.length);
357
+ }
358
+ records.push({
359
+ ...description,
360
+ position,
361
+ ...input.messageLine === void 0 ? {} : { messageLine: input.messageLine },
362
+ hasProse
363
+ });
364
+ switch (description.recordClass) {
365
+ case "run_marker": {
366
+ const data = isObj(entry?.data) ? entry.data : {};
367
+ const runId = str(data.runId);
368
+ if (description.customType === RUN_INPUT_CUSTOM_TYPE) {
369
+ const cause = str(data.inputCause);
370
+ const channel = str(data.channel);
371
+ const inputId = str(data.inputId);
372
+ const acceptedAtMs = num(data.acceptedAtMs);
373
+ const runInput = {
374
+ position,
375
+ ...description.entryId === void 0 ? {} : { entryId: description.entryId },
376
+ ...runId === void 0 ? {} : { runId },
377
+ ...channel === void 0 ? {} : { channel },
378
+ ...cause === void 0 ? {} : { cause },
379
+ ...inputId === void 0 ? {} : { inputId },
380
+ ...acceptedAtMs === void 0 ? {} : { acceptedAtMs },
381
+ human: cause !== void 0 && HUMAN_INPUT_CAUSES.has(cause)
382
+ };
383
+ rawInputs.push(runInput);
384
+ if (runId !== void 0) {
385
+ const run = runFor(runId);
386
+ run.firstInput ??= { ...runInput, opensRun: true };
387
+ }
388
+ } else if (runId !== void 0) {
389
+ const run = runFor(runId);
390
+ if (description.customType === RUN_STARTED_CUSTOM_TYPE) {
391
+ run.startedPosition ??= position;
392
+ } else {
393
+ const state = str(data.runState);
394
+ run.firstStatePosition ??= position;
395
+ run.lastState = {
396
+ position,
397
+ ...state === void 0 ? {} : { state }
398
+ };
399
+ }
400
+ }
401
+ break;
402
+ }
403
+ case "human":
404
+ if (description.clientIntentId !== void 0 && !humanByIntent.has(description.clientIntentId)) {
405
+ humanByIntent.set(description.clientIntentId, position);
406
+ }
407
+ break;
408
+ case "context":
409
+ if (description.clientIntentId !== void 0 && description.customType !== void 0 && TASK_CONTEXT_TYPES.has(description.customType) && !contextByIntent.has(description.clientIntentId)) {
410
+ contextByIntent.set(description.clientIntentId, {
411
+ position,
412
+ customType: description.customType
413
+ });
414
+ }
415
+ break;
416
+ case "assistant":
417
+ reviewContentBlocks(message?.content).forEach((block, blockIndex) => {
418
+ if (block.type !== "toolCall")
419
+ return;
420
+ const toolCallId = str(block.id);
421
+ const tool = str(block.name) ?? "?";
422
+ const uris = [];
423
+ collectUris(block.arguments, uris);
424
+ const fullSubject = subjectOf(tool, block.arguments);
425
+ const subject = fullSubject === void 0 ? void 0 : fullSubject.slice(0, ATTEMPT_SUBJECT_MAX_CHARS);
426
+ const index = attempts.length;
427
+ attempts.push({
428
+ index,
429
+ position,
430
+ ...description.entryId === void 0 ? {} : { entryId: description.entryId },
431
+ block: blockIndex,
432
+ ...toolCallId === void 0 ? {} : { toolCallId },
433
+ tool,
434
+ ...subject === void 0 ? {} : { subject },
435
+ ...fullSubject !== void 0 && fullSubject.length > ATTEMPT_SUBJECT_MAX_CHARS ? { subjectLength: fullSubject.length } : {},
436
+ uris,
437
+ extraResults: [],
438
+ ambiguousResults: []
439
+ });
440
+ attemptByBlock.set(`${position}#${blockIndex}`, index);
441
+ const atPosition = attemptsByPosition.get(position) ?? [];
442
+ atPosition.push(index);
443
+ attemptsByPosition.set(position, atPosition);
444
+ if (toolCallId !== void 0) {
445
+ const list = open.get(toolCallId) ?? [];
446
+ list.push(index);
447
+ open.set(toolCallId, list);
448
+ }
449
+ });
450
+ break;
451
+ case "tool_result":
452
+ if (message !== void 0)
453
+ addResult(position, description.entryId, message);
454
+ break;
455
+ default:
456
+ break;
457
+ }
458
+ return "continue";
459
+ };
460
+ const finish = () => {
461
+ if (finished !== void 0)
462
+ return finished;
463
+ const endPosition = records.length === 0 ? cutAt?.position ?? 1 : records.at(-1).position + 1;
464
+ const contextByIntentPosition = contextByIntent;
465
+ const inputs = rawInputs.map((raw) => {
466
+ const run = raw.runId === void 0 ? void 0 : runs.get(raw.runId);
467
+ const opensRun = run?.firstInput?.position === raw.position;
468
+ const linkedMessage = raw.inputId === void 0 ? void 0 : humanByIntent.get(raw.inputId);
469
+ const linkedContext = raw.inputId === void 0 ? void 0 : contextByIntentPosition.get(raw.inputId)?.position;
470
+ return {
471
+ ...raw,
472
+ opensRun,
473
+ ...linkedMessage === void 0 ? {} : { linkedMessage },
474
+ ...linkedContext === void 0 ? {} : { linkedContext }
475
+ };
476
+ });
477
+ const inputByPosition = new Map(inputs.map((input) => [input.position, input]));
478
+ const inputById = /* @__PURE__ */ new Map();
479
+ for (const input of inputs) {
480
+ if (input.inputId !== void 0 && !inputById.has(input.inputId)) {
481
+ inputById.set(input.inputId, input);
482
+ }
483
+ }
484
+ const finalRuns = /* @__PURE__ */ new Map();
485
+ for (const runId of runOrder) {
486
+ const run = runs.get(runId);
487
+ const firstInput = run.firstInput === void 0 ? void 0 : inputByPosition.get(run.firstInput.position);
488
+ const boundary = Math.min(run.startedPosition ?? Number.POSITIVE_INFINITY, firstInput?.position ?? Number.POSITIVE_INFINITY, run.firstStatePosition ?? Number.POSITIVE_INFINITY);
489
+ finalRuns.set(runId, {
490
+ runId,
491
+ ...run.startedPosition === void 0 ? {} : { startedPosition: run.startedPosition },
492
+ ...firstInput === void 0 ? {} : { firstInput },
493
+ ...run.lastState === void 0 ? {} : { lastState: run.lastState },
494
+ boundary: Number.isFinite(boundary) ? boundary : endPosition
495
+ });
496
+ }
497
+ const hasRuns = inputs.length > 0;
498
+ const starts = [];
499
+ const openerOf = (position) => {
500
+ const record = records[byPosition.get(position)];
501
+ return {
502
+ position,
503
+ ...record.entryId === void 0 ? {} : { entryId: record.entryId },
504
+ ...record.recordClass === "context" && record.customType !== void 0 ? { customType: record.customType } : {}
505
+ };
506
+ };
507
+ if (hasRuns) {
508
+ for (const run of finalRuns.values()) {
509
+ const first = run.firstInput;
510
+ if (first === void 0 || !first.human)
511
+ continue;
512
+ const via = first.linkedMessage !== void 0 ? "message" : first.linkedContext !== void 0 ? "context" : "unresolved";
513
+ const openerPosition = first.linkedMessage ?? first.linkedContext;
514
+ starts.push({
515
+ // An opener stored before its run's markers still belongs to its span.
516
+ boundary: Math.min(run.boundary, openerPosition ?? Number.POSITIVE_INFINITY),
517
+ runId: run.runId,
518
+ ...openerPosition === void 0 ? {} : { opener: openerOf(openerPosition) },
519
+ openerVia: via,
520
+ firstInput: first
521
+ });
522
+ }
523
+ } else {
524
+ for (const record of records) {
525
+ if (record.recordClass !== "human")
526
+ continue;
527
+ starts.push({
528
+ boundary: record.position,
529
+ opener: openerOf(record.position),
530
+ openerVia: "fallback"
531
+ });
532
+ }
533
+ }
534
+ starts.sort((left, right) => left.boundary - right.boundary);
535
+ const runStarts = [...finalRuns.values()].map((run) => ({ runId: run.runId, boundary: run.boundary })).sort((left, right) => left.boundary - right.boundary);
536
+ const previousProse = (before) => {
537
+ let index = byPosition.get(before);
538
+ if (index === void 0)
539
+ return void 0;
540
+ for (index -= 1; index >= 0; index -= 1) {
541
+ if (records[index].hasProse)
542
+ return records[index].position;
543
+ }
544
+ return void 0;
545
+ };
546
+ const spans = starts.map((start, index) => {
547
+ const next = starts[index + 1];
548
+ const end = next?.boundary ?? endPosition;
549
+ const closingEnd = next?.opener !== void 0 ? Math.max(end, next.opener.position + 1) : end;
550
+ const ownRuns = [];
551
+ const closingRuns = [];
552
+ for (const run of runStarts) {
553
+ const started = finalRuns.get(run.runId).startedPosition;
554
+ if (started !== void 0 && started >= start.boundary && started < end) {
555
+ if (!ownRuns.includes(run.runId))
556
+ ownRuns.push(run.runId);
557
+ }
558
+ }
559
+ const joined = [];
560
+ for (const input of inputs) {
561
+ if (input.runId === void 0)
562
+ continue;
563
+ if (input.position >= start.boundary && input.position < end) {
564
+ if (!ownRuns.includes(input.runId))
565
+ ownRuns.push(input.runId);
566
+ if (input.human && !input.opensRun)
567
+ joined.push(input);
568
+ } else if (input.position >= end && input.position < closingEnd) {
569
+ if (!closingRuns.includes(input.runId))
570
+ closingRuns.push(input.runId);
571
+ }
572
+ }
573
+ ownRuns.sort((left, right) => finalRuns.get(left).boundary - finalRuns.get(right).boundary);
574
+ const close = next !== void 0 ? next.opener !== void 0 ? "next-opener" : "next-opener-unresolved" : cutAt !== void 0 ? "time-bound" : "end-of-store";
575
+ const previousAnswer = previousProse(start.boundary);
576
+ return {
577
+ index,
578
+ boundary: start.boundary,
579
+ end,
580
+ closingEnd,
581
+ ...start.runId === void 0 ? {} : { runId: start.runId },
582
+ ...start.opener === void 0 ? {} : { opener: start.opener },
583
+ openerVia: start.openerVia,
584
+ ...start.firstInput === void 0 ? {} : { firstInput: start.firstInput },
585
+ ownRuns,
586
+ closingRuns,
587
+ joined,
588
+ close,
589
+ ...previousAnswer === void 0 ? {} : { previousAnswer }
590
+ };
591
+ });
592
+ const frozenAttempts = attempts;
593
+ const lastAtOrBefore = (list, position) => {
594
+ let low = 0;
595
+ let high = list.length - 1;
596
+ let found;
597
+ while (low <= high) {
598
+ const middle = low + high >> 1;
599
+ if (list[middle].boundary <= position) {
600
+ found = list[middle];
601
+ low = middle + 1;
602
+ } else {
603
+ high = middle - 1;
604
+ }
605
+ }
606
+ return found;
607
+ };
608
+ finished = {
609
+ records,
610
+ ...cutAt === void 0 ? {} : { cutAt },
611
+ ...untilMs === void 0 ? {} : { untilMs },
612
+ inputs,
613
+ runs: finalRuns,
614
+ hasRuns,
615
+ spans,
616
+ attempts: frozenAttempts,
617
+ results,
618
+ endPosition,
619
+ recordAt: (position) => {
620
+ const index = byPosition.get(position);
621
+ return index === void 0 ? void 0 : records[index];
622
+ },
623
+ recordOfEntry: (entryId) => {
624
+ const index = byEntryId.get(entryId);
625
+ return index === void 0 ? void 0 : records[index];
626
+ },
627
+ spanAt: (position) => position < endPosition ? lastAtOrBefore(spans, position) : void 0,
628
+ runExtent: (runId) => {
629
+ const run = finalRuns.get(runId);
630
+ if (run === void 0)
631
+ return void 0;
632
+ const at = runStarts.findIndex((candidate) => candidate.runId === runId);
633
+ const next = runStarts.slice(at + 1).find((candidate) => candidate.boundary > run.boundary);
634
+ return { start: run.boundary, end: next?.boundary ?? endPosition };
635
+ },
636
+ inputById: (inputId) => inputById.get(inputId),
637
+ attemptAt: (position, block) => {
638
+ const index = attemptByBlock.get(`${position}#${block}`);
639
+ return index === void 0 ? void 0 : attempts[index];
640
+ },
641
+ attemptsAt: (position) => (attemptsByPosition.get(position) ?? []).map((index) => attempts[index]),
642
+ resultAt: (position) => {
643
+ const index = resultByPosition.get(position);
644
+ return index === void 0 ? void 0 : results[index];
645
+ },
646
+ outcome: (attempt, uptoPosition) => {
647
+ const resultPosition = attempt.result !== void 0 && (uptoPosition === void 0 || attempt.result <= uptoPosition) ? attempt.result : void 0;
648
+ if (resultPosition === void 0) {
649
+ return {
650
+ status: attempt.ambiguousResults.length > 0 ? "ambiguous" : "no-result",
651
+ isError: false,
652
+ timedOut: false
653
+ };
654
+ }
655
+ const result = results[resultByPosition.get(resultPosition)];
656
+ return {
657
+ status: result.failed ? "failed" : "ok",
658
+ isError: result.isError,
659
+ ...result.exitCode === void 0 ? {} : { exitCode: result.exitCode },
660
+ timedOut: result.timedOut,
661
+ resultPosition,
662
+ ...result.entryId === void 0 ? {} : { resultEntryId: result.entryId },
663
+ ...result.firstErrorLine === void 0 ? {} : { firstErrorLine: result.firstErrorLine }
664
+ };
665
+ }
666
+ };
667
+ return finished;
668
+ };
669
+ return { add, finish };
670
+ }
671
+ export {
672
+ SYSTEM_PROMPT_CUSTOM_TYPES,
673
+ TASK_CONTEXT_TYPES,
674
+ createTranscriptReview,
675
+ formatReviewInstant,
676
+ reviewContentBlocks,
677
+ reviewContentText,
678
+ reviewContextOf,
679
+ reviewInstantMs
680
+ };