@leitwerk-dev/protocol 0.1.2 → 0.1.4
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/dist/config-snapshot.d.ts +33 -34
- package/dist/config-snapshot.d.ts.map +1 -0
- package/dist/config-snapshot.js +1 -1
- package/dist/config-snapshot.js.map +1 -1
- package/dist/external-review.d.ts +8 -0
- package/dist/external-review.d.ts.map +1 -0
- package/dist/external-review.js +2 -0
- package/dist/external-review.js.map +1 -0
- package/dist/form-contract.d.ts +10 -11
- package/dist/form-contract.d.ts.map +1 -0
- package/dist/form-contract.js +1 -1
- package/dist/form-contract.js.map +1 -1
- package/dist/http-contracts.d.ts +959 -7
- package/dist/http-contracts.d.ts.map +1 -0
- package/dist/http-contracts.js +432 -31
- package/dist/http-contracts.js.map +1 -1
- package/dist/index.d.ts +21 -177
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +20 -1087
- package/dist/index.js.map +1 -1
- package/dist/initial-prompt.d.ts +8 -0
- package/dist/initial-prompt.d.ts.map +1 -0
- package/dist/initial-prompt.js +103 -0
- package/dist/initial-prompt.js.map +1 -0
- package/dist/launcher-contract.d.ts +10 -12
- package/dist/launcher-contract.d.ts.map +1 -0
- package/dist/launcher-contract.js +1 -1
- package/dist/launcher-contract.js.map +1 -1
- package/dist/live-turn-projection.d.ts +39 -0
- package/dist/live-turn-projection.d.ts.map +1 -0
- package/dist/live-turn-projection.js +395 -0
- package/dist/live-turn-projection.js.map +1 -0
- package/dist/pi-session-message.d.ts +25 -0
- package/dist/pi-session-message.d.ts.map +1 -0
- package/dist/pi-session-message.js +27 -0
- package/dist/pi-session-message.js.map +1 -0
- package/dist/primary-path-snapshot.d.ts +77 -0
- package/dist/primary-path-snapshot.d.ts.map +1 -0
- package/dist/primary-path-snapshot.js +2 -0
- package/dist/primary-path-snapshot.js.map +1 -0
- package/dist/protocol.d.ts +428 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +261 -0
- package/dist/protocol.js.map +1 -0
- package/dist/session-entry-tree.d.ts +15 -0
- package/dist/session-entry-tree.d.ts.map +1 -0
- package/dist/session-entry-tree.js +42 -0
- package/dist/session-entry-tree.js.map +1 -0
- package/dist/streamable-events.d.ts +4 -0
- package/dist/streamable-events.d.ts.map +1 -0
- package/dist/streamable-events.js +17 -0
- package/dist/streamable-events.js.map +1 -0
- package/dist/text-preview.d.ts +7 -0
- package/dist/text-preview.d.ts.map +1 -0
- package/dist/text-preview.js +23 -0
- package/dist/text-preview.js.map +1 -0
- package/dist/timeline-projection.d.ts +9 -0
- package/dist/timeline-projection.d.ts.map +1 -0
- package/dist/timeline-projection.js +30 -0
- package/dist/timeline-projection.js.map +1 -0
- package/dist/timestamp-ordering.d.ts +3 -0
- package/dist/timestamp-ordering.d.ts.map +1 -0
- package/dist/timestamp-ordering.js +15 -0
- package/dist/timestamp-ordering.js.map +1 -0
- package/dist/tool-renderer-contract.d.ts +8 -9
- package/dist/tool-renderer-contract.d.ts.map +1 -0
- package/dist/tool-renderer-contract.js +21 -10
- package/dist/tool-renderer-contract.js.map +1 -1
- package/dist/turn-continuation.d.ts +36 -0
- package/dist/turn-continuation.d.ts.map +1 -0
- package/dist/turn-continuation.js +179 -0
- package/dist/turn-continuation.js.map +1 -0
- package/dist/usage-by-turn-record.d.ts +5 -0
- package/dist/usage-by-turn-record.d.ts.map +1 -0
- package/dist/usage-by-turn-record.js +25 -0
- package/dist/usage-by-turn-record.js.map +1 -0
- package/dist/usage-snapshot.d.ts +25 -0
- package/dist/usage-snapshot.d.ts.map +1 -0
- package/dist/usage-snapshot.js +120 -0
- package/dist/usage-snapshot.js.map +1 -0
- package/dist/ws-event-payloads.d.ts +18 -0
- package/dist/ws-event-payloads.d.ts.map +1 -0
- package/dist/ws-event-payloads.js +47 -0
- package/dist/ws-event-payloads.js.map +1 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,1088 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
parseLauncherInputJson,
|
|
22
|
-
parseLauncherModelConfigInput,
|
|
23
|
-
parseLauncherRequestBody,
|
|
24
|
-
parseLauncherTurnConfigsJson,
|
|
25
|
-
parseLiteral,
|
|
26
|
-
parsePrimaryPathWsFrameInput,
|
|
27
|
-
parseScheduleRequestInput,
|
|
28
|
-
parseSchema,
|
|
29
|
-
parseWsFrame,
|
|
30
|
-
resolvePromptCacheSwitch,
|
|
31
|
-
serializeFutureActionPayload,
|
|
32
|
-
serializeFutureLaunchPayload,
|
|
33
|
-
unknownRecordSchema,
|
|
34
|
-
validateScheduleRequestInput
|
|
35
|
-
} from "./chunk-7PCFBAV4.js";
|
|
36
|
-
import "./chunk-IEMT3JG4.js";
|
|
37
|
-
import {
|
|
38
|
-
TOOL_CALL_RENDERER_VALUE_KINDS,
|
|
39
|
-
TOOL_CALL_RENDERER_VALUE_SOURCES,
|
|
40
|
-
readValueAtPath
|
|
41
|
-
} from "./chunk-IWVU76M5.js";
|
|
42
|
-
|
|
43
|
-
// src/initial-prompt.ts
|
|
44
|
-
var INITIAL_PROMPT_FIELD_PRIORITY = [
|
|
45
|
-
"initialPrompt",
|
|
46
|
-
"prompt",
|
|
47
|
-
"taskPrompt",
|
|
48
|
-
"userPrompt",
|
|
49
|
-
"instructions",
|
|
50
|
-
"instruction",
|
|
51
|
-
"initialCommand",
|
|
52
|
-
"command",
|
|
53
|
-
"query",
|
|
54
|
-
"message",
|
|
55
|
-
"requestedChange",
|
|
56
|
-
"request",
|
|
57
|
-
"description"
|
|
58
|
-
];
|
|
59
|
-
function isRecord(value) {
|
|
60
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
61
|
-
}
|
|
62
|
-
function normalizePromptValue(value) {
|
|
63
|
-
const normalized = value.replace(/\s+/g, " ").trim();
|
|
64
|
-
return normalized.length > 0 ? normalized : null;
|
|
65
|
-
}
|
|
66
|
-
function truncatePromptPreview(value, maxLength = 180) {
|
|
67
|
-
const normalized = typeof value === "string" ? normalizePromptValue(value) : null;
|
|
68
|
-
return normalized && normalized.length > maxLength ? `${normalized.slice(0, maxLength).trimEnd()}\u2026` : normalized;
|
|
69
|
-
}
|
|
70
|
-
function readStringField(record2, fieldName) {
|
|
71
|
-
const exact = record2[fieldName];
|
|
72
|
-
if (typeof exact === "string") {
|
|
73
|
-
return normalizePromptValue(exact);
|
|
74
|
-
}
|
|
75
|
-
const normalizedFieldName = fieldName.toLowerCase();
|
|
76
|
-
for (const [key, value] of Object.entries(record2)) {
|
|
77
|
-
if (key.toLowerCase() !== normalizedFieldName || typeof value !== "string") {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
return normalizePromptValue(value);
|
|
81
|
-
}
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
function extractInitialPromptFromValue(value) {
|
|
85
|
-
if (!isRecord(value)) {
|
|
86
|
-
return null;
|
|
87
|
-
}
|
|
88
|
-
for (const fieldName of INITIAL_PROMPT_FIELD_PRIORITY) {
|
|
89
|
-
const prompt = readStringField(value, fieldName);
|
|
90
|
-
if (prompt) {
|
|
91
|
-
return prompt;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
function extractInitialPromptFromParamsJson(paramsJson) {
|
|
97
|
-
if (!paramsJson) {
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
return extractInitialPromptFromValue(JSON.parse(paramsJson));
|
|
102
|
-
} catch {
|
|
103
|
-
return null;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
function extractInitialPromptPreviewFromParamsJson(paramsJson, maxLength = 180) {
|
|
107
|
-
return truncatePromptPreview(extractInitialPromptFromParamsJson(paramsJson), maxLength);
|
|
108
|
-
}
|
|
109
|
-
function projectFutureExecutionOverview(summary) {
|
|
110
|
-
const common = {
|
|
111
|
-
id: summary.id,
|
|
112
|
-
scheduleKind: summary.scheduleKind,
|
|
113
|
-
processId: summary.processId,
|
|
114
|
-
nextRunAt: summary.nextRunAt,
|
|
115
|
-
cronExpression: summary.cronExpression,
|
|
116
|
-
title: summary.title,
|
|
117
|
-
status: summary.status ?? "scheduled",
|
|
118
|
-
blockedReason: summary.blockedReason ?? null,
|
|
119
|
-
initialPromptPreview: summary.kind === "launch" ? truncatePromptPreview(extractInitialPromptFromValue(summary.launcherInput)) : null
|
|
120
|
-
};
|
|
121
|
-
return summary.kind === "action" ? {
|
|
122
|
-
...common,
|
|
123
|
-
kind: "action",
|
|
124
|
-
instanceId: summary.instanceId,
|
|
125
|
-
actionId: summary.actionId,
|
|
126
|
-
actionLabel: summary.actionLabel
|
|
127
|
-
} : {
|
|
128
|
-
...common,
|
|
129
|
-
kind: "launch",
|
|
130
|
-
instanceId: null,
|
|
131
|
-
launcherId: summary.launcherId,
|
|
132
|
-
launcherLabel: summary.launcherLabel
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// src/timestamp-ordering.ts
|
|
137
|
-
function compareTimestampStrings(left, right) {
|
|
138
|
-
const leftMs = Date.parse(left);
|
|
139
|
-
const rightMs = Date.parse(right);
|
|
140
|
-
if (Number.isFinite(leftMs) && Number.isFinite(rightMs)) {
|
|
141
|
-
return leftMs - rightMs;
|
|
142
|
-
}
|
|
143
|
-
return left.localeCompare(right);
|
|
144
|
-
}
|
|
145
|
-
function happenedOnOrAfterStart(entryTimestamp, startedAt) {
|
|
146
|
-
if (!startedAt) {
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
return compareTimestampStrings(entryTimestamp, startedAt) >= 0;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
// src/usage-snapshot.ts
|
|
153
|
-
import * as v from "valibot";
|
|
154
|
-
var unknownRecordSchema2 = v.pipe(
|
|
155
|
-
v.unknown(),
|
|
156
|
-
v.check(
|
|
157
|
-
(value) => typeof value === "object" && value !== null && !Array.isArray(value),
|
|
158
|
-
"Expected object"
|
|
159
|
-
),
|
|
160
|
-
v.record(v.string(), v.unknown())
|
|
161
|
-
);
|
|
162
|
-
function readNumber(value) {
|
|
163
|
-
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
164
|
-
}
|
|
165
|
-
function normalizeUsageCostSnapshot(value) {
|
|
166
|
-
const parsedValue = v.safeParse(unknownRecordSchema2, value);
|
|
167
|
-
if (!parsedValue.success) {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
const record2 = parsedValue.output;
|
|
171
|
-
const input = readNumber(record2.input);
|
|
172
|
-
const output = readNumber(record2.output);
|
|
173
|
-
const cacheRead = readNumber(record2.cacheRead);
|
|
174
|
-
const cacheWrite = readNumber(record2.cacheWrite);
|
|
175
|
-
const total = typeof record2.total === "number" && Number.isFinite(record2.total) ? record2.total : input + output + cacheRead + cacheWrite;
|
|
176
|
-
return {
|
|
177
|
-
input,
|
|
178
|
-
output,
|
|
179
|
-
cacheRead,
|
|
180
|
-
cacheWrite,
|
|
181
|
-
total
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
function cloneUsageCostSnapshot(cost) {
|
|
185
|
-
return { ...cost };
|
|
186
|
-
}
|
|
187
|
-
function cloneUsageSnapshot(usage) {
|
|
188
|
-
if (!usage) {
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
return {
|
|
192
|
-
...usage,
|
|
193
|
-
cost: usage.cost ? cloneUsageCostSnapshot(usage.cost) : null
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
function normalizeUsageSnapshot(value) {
|
|
197
|
-
const parsedValue = v.safeParse(unknownRecordSchema2, value);
|
|
198
|
-
if (!parsedValue.success) {
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
const record2 = parsedValue.output;
|
|
202
|
-
const input = readNumber(record2.input);
|
|
203
|
-
const output = readNumber(record2.output);
|
|
204
|
-
const reasoning = typeof record2.reasoning === "number" && Number.isFinite(record2.reasoning) ? record2.reasoning : void 0;
|
|
205
|
-
const cacheRead = readNumber(record2.cacheRead);
|
|
206
|
-
const cacheWrite = readNumber(record2.cacheWrite);
|
|
207
|
-
const totalTokens = typeof record2.totalTokens === "number" && Number.isFinite(record2.totalTokens) ? record2.totalTokens : input + output + cacheRead + cacheWrite;
|
|
208
|
-
const cost = normalizeUsageCostSnapshot(record2.cost);
|
|
209
|
-
const requestCount = typeof record2.requestCount === "number" && Number.isFinite(record2.requestCount) ? Math.max(0, Math.floor(record2.requestCount)) : 1;
|
|
210
|
-
const maxInputTokens = typeof record2.maxInputTokens === "number" && Number.isFinite(record2.maxInputTokens) ? Math.max(0, record2.maxInputTokens) : input;
|
|
211
|
-
const hasExplicitUsageTelemetry = ["input", "output", "reasoning", "cacheRead", "cacheWrite", "totalTokens"].some(
|
|
212
|
-
(key) => typeof record2[key] === "number" && Number.isFinite(record2[key])
|
|
213
|
-
) || cost !== null;
|
|
214
|
-
if (!hasExplicitUsageTelemetry && input === 0 && output === 0 && cacheRead === 0 && cacheWrite === 0 && totalTokens === 0) {
|
|
215
|
-
return null;
|
|
216
|
-
}
|
|
217
|
-
return {
|
|
218
|
-
input,
|
|
219
|
-
output,
|
|
220
|
-
...reasoning !== void 0 ? { reasoning } : {},
|
|
221
|
-
cacheRead,
|
|
222
|
-
cacheWrite,
|
|
223
|
-
totalTokens,
|
|
224
|
-
cost,
|
|
225
|
-
requestCount,
|
|
226
|
-
maxInputTokens
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
function mergeUsageSnapshots(total, usage) {
|
|
230
|
-
if (!usage) {
|
|
231
|
-
return total ? cloneUsageSnapshot(total) : null;
|
|
232
|
-
}
|
|
233
|
-
if (!total) {
|
|
234
|
-
return cloneUsageSnapshot(usage);
|
|
235
|
-
}
|
|
236
|
-
const hasRequestMetrics = total.requestCount !== void 0 || usage.requestCount !== void 0 || total.maxInputTokens !== void 0 || usage.maxInputTokens !== void 0;
|
|
237
|
-
return {
|
|
238
|
-
input: total.input + usage.input,
|
|
239
|
-
output: total.output + usage.output,
|
|
240
|
-
...total.reasoning !== void 0 || usage.reasoning !== void 0 ? { reasoning: (total.reasoning ?? 0) + (usage.reasoning ?? 0) } : {},
|
|
241
|
-
cacheRead: total.cacheRead + usage.cacheRead,
|
|
242
|
-
cacheWrite: total.cacheWrite + usage.cacheWrite,
|
|
243
|
-
totalTokens: total.totalTokens + usage.totalTokens,
|
|
244
|
-
cost: total.cost && usage.cost ? {
|
|
245
|
-
input: total.cost.input + usage.cost.input,
|
|
246
|
-
output: total.cost.output + usage.cost.output,
|
|
247
|
-
cacheRead: total.cost.cacheRead + usage.cost.cacheRead,
|
|
248
|
-
cacheWrite: total.cost.cacheWrite + usage.cost.cacheWrite,
|
|
249
|
-
total: total.cost.total + usage.cost.total
|
|
250
|
-
} : null,
|
|
251
|
-
...hasRequestMetrics ? {
|
|
252
|
-
requestCount: (total.requestCount ?? 1) + (usage.requestCount ?? 1),
|
|
253
|
-
maxInputTokens: Math.max(
|
|
254
|
-
total.maxInputTokens ?? total.input,
|
|
255
|
-
usage.maxInputTokens ?? usage.input
|
|
256
|
-
)
|
|
257
|
-
} : {}
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// src/ws-event-payloads.ts
|
|
262
|
-
function asWsEventPayloadRecord(value) {
|
|
263
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
|
|
264
|
-
}
|
|
265
|
-
function readWsEventNonEmptyString(value) {
|
|
266
|
-
if (typeof value !== "string") {
|
|
267
|
-
return null;
|
|
268
|
-
}
|
|
269
|
-
const trimmed = value.trim();
|
|
270
|
-
return trimmed.length > 0 ? trimmed : null;
|
|
271
|
-
}
|
|
272
|
-
function readWsEventTimestamp(data, fallbackTimestamp) {
|
|
273
|
-
return readWsEventNonEmptyString(data.timestamp) ?? fallbackTimestamp;
|
|
274
|
-
}
|
|
275
|
-
function readWsEventTurnRecordId(data) {
|
|
276
|
-
return readWsEventNonEmptyString(data.turnRecordId);
|
|
277
|
-
}
|
|
278
|
-
function readWsEventPiTurnId(data) {
|
|
279
|
-
return readWsEventNonEmptyString(data.turnId);
|
|
280
|
-
}
|
|
281
|
-
function readWsEventStreamText(data) {
|
|
282
|
-
return typeof data.text === "string" ? data.text : typeof data.delta === "string" ? data.delta : typeof data.chunk === "string" ? data.chunk : null;
|
|
283
|
-
}
|
|
284
|
-
function readWsEventToolName(data) {
|
|
285
|
-
return readWsEventNonEmptyString(data.name) ?? readWsEventNonEmptyString(data.toolName) ?? "tool";
|
|
286
|
-
}
|
|
287
|
-
function readWsEventToolArguments(data) {
|
|
288
|
-
const value = data.arguments ?? data.args;
|
|
289
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
290
|
-
}
|
|
291
|
-
function readWsEventToolCallId(data) {
|
|
292
|
-
return readWsEventNonEmptyString(data.toolCallId);
|
|
293
|
-
}
|
|
294
|
-
function resolveWsEventToolCallId(data, options) {
|
|
295
|
-
return readWsEventToolCallId(data) ?? `${options.toolName ?? readWsEventToolName(data)}:${options.timestamp}${options.ordinal ? `:${options.ordinal}` : ""}`;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// src/live-turn-projection.ts
|
|
299
|
-
var PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES = [
|
|
300
|
-
"pi.error",
|
|
301
|
-
"pi.retry.start",
|
|
302
|
-
"pi.retry.end",
|
|
303
|
-
"pi.compaction.start",
|
|
304
|
-
"pi.compaction.end"
|
|
305
|
-
];
|
|
306
|
-
var PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET = new Set(
|
|
307
|
-
PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES
|
|
308
|
-
);
|
|
309
|
-
function cloneAssistant(assistant) {
|
|
310
|
-
return { ...assistant };
|
|
311
|
-
}
|
|
312
|
-
function cloneToolCall(toolCall) {
|
|
313
|
-
return {
|
|
314
|
-
...toolCall,
|
|
315
|
-
...toolCall.arguments ? { arguments: { ...toolCall.arguments } } : {}
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
function cloneTraceItem(traceItem) {
|
|
319
|
-
return { ...traceItem };
|
|
320
|
-
}
|
|
321
|
-
function readString(value) {
|
|
322
|
-
return typeof value === "string" && value.trim() !== "" ? value : null;
|
|
323
|
-
}
|
|
324
|
-
function readNumber2(value) {
|
|
325
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
326
|
-
return value;
|
|
327
|
-
}
|
|
328
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
329
|
-
const parsed = Number(value);
|
|
330
|
-
return Number.isFinite(parsed) ? parsed : null;
|
|
331
|
-
}
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
function formatReason(value) {
|
|
335
|
-
const reason = readString(value);
|
|
336
|
-
return reason ? reason.replace(/[_-]+/g, " ") : null;
|
|
337
|
-
}
|
|
338
|
-
function firstMessage(data, keys) {
|
|
339
|
-
for (const key of keys) {
|
|
340
|
-
const value = readString(data[key]);
|
|
341
|
-
if (value) {
|
|
342
|
-
return value;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
return null;
|
|
346
|
-
}
|
|
347
|
-
function retryStartFallbackMessage(data) {
|
|
348
|
-
const attempt = readNumber2(data.attempt);
|
|
349
|
-
const maxAttempts = readNumber2(data.maxAttempts);
|
|
350
|
-
const delayMs = readNumber2(data.delayMs);
|
|
351
|
-
const attemptLabel = attempt !== null && maxAttempts !== null ? ` ${attempt}/${maxAttempts}` : "";
|
|
352
|
-
const delayLabel = delayMs !== null && delayMs > 0 ? ` in ${delayMs}ms` : "";
|
|
353
|
-
return `Retry${attemptLabel} scheduled${delayLabel}.`;
|
|
354
|
-
}
|
|
355
|
-
function retryEndFallbackMessage(data) {
|
|
356
|
-
const attempt = readNumber2(data.attempt);
|
|
357
|
-
if (data.success === true) {
|
|
358
|
-
return attempt !== null ? `Retry sequence succeeded after ${attempt} ${attempt === 1 ? "retry" : "retries"}.` : "Retry sequence succeeded.";
|
|
359
|
-
}
|
|
360
|
-
return attempt !== null ? `Retry sequence ended after ${attempt} ${attempt === 1 ? "retry" : "retries"}.` : "Retry sequence ended.";
|
|
361
|
-
}
|
|
362
|
-
function buildPrimaryPathOperationalTraceItem(input) {
|
|
363
|
-
if (!PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET.has(input.eventType)) {
|
|
364
|
-
return null;
|
|
365
|
-
}
|
|
366
|
-
const timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
|
|
367
|
-
if (input.eventType === "pi.error") {
|
|
368
|
-
return {
|
|
369
|
-
kind: "operational_event",
|
|
370
|
-
eventType: input.eventType,
|
|
371
|
-
severity: "error",
|
|
372
|
-
title: "Pi request failed",
|
|
373
|
-
message: firstMessage(input.data, ["message", "errorMessage", "finalError"]) ?? "Pi reported an error.",
|
|
374
|
-
timestamp
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
if (input.eventType === "pi.retry.start") {
|
|
378
|
-
return {
|
|
379
|
-
kind: "operational_event",
|
|
380
|
-
eventType: input.eventType,
|
|
381
|
-
severity: "warning",
|
|
382
|
-
title: "Retry scheduled",
|
|
383
|
-
message: firstMessage(input.data, ["message", "errorMessage"]) ?? retryStartFallbackMessage(input.data),
|
|
384
|
-
timestamp
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
if (input.eventType === "pi.retry.end") {
|
|
388
|
-
const success = input.data.success === true;
|
|
389
|
-
return {
|
|
390
|
-
kind: "operational_event",
|
|
391
|
-
eventType: input.eventType,
|
|
392
|
-
severity: success ? "success" : "error",
|
|
393
|
-
title: success ? "Retry succeeded" : "Retry exhausted",
|
|
394
|
-
message: firstMessage(input.data, ["message", "finalError", "errorMessage"]) ?? retryEndFallbackMessage(input.data),
|
|
395
|
-
timestamp
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
if (input.eventType === "pi.compaction.start") {
|
|
399
|
-
const reason = formatReason(input.data.reason);
|
|
400
|
-
return {
|
|
401
|
-
kind: "operational_event",
|
|
402
|
-
eventType: input.eventType,
|
|
403
|
-
severity: "info",
|
|
404
|
-
title: "Context compaction started",
|
|
405
|
-
message: reason ? `Pi started compacting context (${reason}).` : "Pi started compacting context.",
|
|
406
|
-
timestamp
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
if (input.eventType === "pi.compaction.end") {
|
|
410
|
-
const reason = formatReason(input.data.reason);
|
|
411
|
-
const errorMessage = firstMessage(input.data, ["errorMessage", "message"]);
|
|
412
|
-
const aborted = input.data.aborted === true;
|
|
413
|
-
const willRetry = input.data.willRetry === true;
|
|
414
|
-
const statusMessage = errorMessage ? errorMessage : aborted ? "Context compaction was cancelled." : `Pi compacted context${reason ? ` (${reason})` : ""}.${willRetry ? " Pi will continue automatically." : ""}`;
|
|
415
|
-
return {
|
|
416
|
-
kind: "operational_event",
|
|
417
|
-
eventType: input.eventType,
|
|
418
|
-
severity: errorMessage ? "error" : aborted ? "warning" : "success",
|
|
419
|
-
title: errorMessage ? "Context compaction failed" : aborted ? "Context compaction cancelled" : "Context compacted",
|
|
420
|
-
message: statusMessage,
|
|
421
|
-
timestamp
|
|
422
|
-
};
|
|
423
|
-
}
|
|
424
|
-
return null;
|
|
425
|
-
}
|
|
426
|
-
function addOpenToolCallId(projection, toolName, toolCallId) {
|
|
427
|
-
const ids = projection.openToolCallIdsByName.get(toolName) ?? [];
|
|
428
|
-
ids.push(toolCallId);
|
|
429
|
-
projection.openToolCallIdsByName.set(toolName, ids);
|
|
430
|
-
}
|
|
431
|
-
function consumeLatestOpenToolCallId(projection, toolName) {
|
|
432
|
-
const ids = projection.openToolCallIdsByName.get(toolName);
|
|
433
|
-
if (!ids || ids.length === 0) {
|
|
434
|
-
return null;
|
|
435
|
-
}
|
|
436
|
-
const toolCallId = ids.pop() ?? null;
|
|
437
|
-
if (ids.length === 0) {
|
|
438
|
-
projection.openToolCallIdsByName.delete(toolName);
|
|
439
|
-
} else {
|
|
440
|
-
projection.openToolCallIdsByName.set(toolName, ids);
|
|
441
|
-
}
|
|
442
|
-
return toolCallId;
|
|
443
|
-
}
|
|
444
|
-
function removeOpenToolCallId(projection, toolCallId) {
|
|
445
|
-
for (const [toolName, ids] of projection.openToolCallIdsByName.entries()) {
|
|
446
|
-
const nextIds = ids.filter((candidate) => candidate !== toolCallId);
|
|
447
|
-
if (nextIds.length === ids.length) {
|
|
448
|
-
continue;
|
|
449
|
-
}
|
|
450
|
-
if (nextIds.length === 0) {
|
|
451
|
-
projection.openToolCallIdsByName.delete(toolName);
|
|
452
|
-
} else {
|
|
453
|
-
projection.openToolCallIdsByName.set(toolName, nextIds);
|
|
454
|
-
}
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
function canonicalizeToolCallId(input) {
|
|
459
|
-
const explicitToolCallId = readWsEventToolCallId(input.data);
|
|
460
|
-
if (explicitToolCallId) {
|
|
461
|
-
if (input.preferOpenCall) {
|
|
462
|
-
removeOpenToolCallId(input.projection, explicitToolCallId);
|
|
463
|
-
}
|
|
464
|
-
return explicitToolCallId;
|
|
465
|
-
}
|
|
466
|
-
if (input.preferOpenCall) {
|
|
467
|
-
const openToolCallId = consumeLatestOpenToolCallId(input.projection, input.toolName);
|
|
468
|
-
if (openToolCallId) {
|
|
469
|
-
return openToolCallId;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
return resolveWsEventToolCallId(input.data, {
|
|
473
|
-
timestamp: input.timestamp,
|
|
474
|
-
toolName: input.toolName,
|
|
475
|
-
ordinal: input.projection.nextFallbackOrdinal++
|
|
476
|
-
});
|
|
477
|
-
}
|
|
478
|
-
function appendThinkingTraceText(projection, text) {
|
|
479
|
-
if (text.length === 0) {
|
|
480
|
-
return;
|
|
481
|
-
}
|
|
482
|
-
const lastTraceItem = projection.traceItems.at(-1);
|
|
483
|
-
if (lastTraceItem?.kind === "thinking") {
|
|
484
|
-
lastTraceItem.text += text;
|
|
485
|
-
return;
|
|
486
|
-
}
|
|
487
|
-
projection.traceItems.push({
|
|
488
|
-
kind: "thinking",
|
|
489
|
-
text
|
|
490
|
-
});
|
|
491
|
-
}
|
|
492
|
-
function ensureToolTraceItem(projection, toolCallId) {
|
|
493
|
-
if (projection.traceItems.some(
|
|
494
|
-
(traceItem) => traceItem.kind === "tool_call" && traceItem.toolCallId === toolCallId
|
|
495
|
-
)) {
|
|
496
|
-
return;
|
|
497
|
-
}
|
|
498
|
-
projection.traceItems.push({
|
|
499
|
-
kind: "tool_call",
|
|
500
|
-
toolCallId
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
function createMutableLiveTurnProjection() {
|
|
504
|
-
return {
|
|
505
|
-
assistant: {
|
|
506
|
-
text: "",
|
|
507
|
-
thinking: "",
|
|
508
|
-
lastUpdatedAt: null
|
|
509
|
-
},
|
|
510
|
-
toolCalls: [],
|
|
511
|
-
traceItems: [],
|
|
512
|
-
usage: null,
|
|
513
|
-
toolCallsById: /* @__PURE__ */ new Map(),
|
|
514
|
-
openToolCallIdsByName: /* @__PURE__ */ new Map(),
|
|
515
|
-
nextFallbackOrdinal: 1
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
function snapshotLiveTurnProjection(projection) {
|
|
519
|
-
return {
|
|
520
|
-
assistant: cloneAssistant(projection.assistant),
|
|
521
|
-
toolCalls: projection.toolCalls.map(cloneToolCall),
|
|
522
|
-
traceItems: projection.traceItems.map(cloneTraceItem),
|
|
523
|
-
usage: cloneUsageSnapshot(projection.usage)
|
|
524
|
-
};
|
|
525
|
-
}
|
|
526
|
-
function applyPiEventToLiveTurnProjection(projection, input) {
|
|
527
|
-
const timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
|
|
528
|
-
if (input.eventType === "pi.stream.delta") {
|
|
529
|
-
const text = readWsEventStreamText(input.data);
|
|
530
|
-
if (text) {
|
|
531
|
-
if (readWsEventNonEmptyString(input.data.streamType) === "thinking") {
|
|
532
|
-
projection.assistant.thinking += text;
|
|
533
|
-
appendThinkingTraceText(projection, text);
|
|
534
|
-
} else {
|
|
535
|
-
projection.assistant.text += text;
|
|
536
|
-
}
|
|
537
|
-
projection.assistant.lastUpdatedAt = timestamp;
|
|
538
|
-
}
|
|
539
|
-
return {
|
|
540
|
-
canonicalData: input.data,
|
|
541
|
-
timestamp,
|
|
542
|
-
toolCallId: null,
|
|
543
|
-
toolName: null
|
|
544
|
-
};
|
|
545
|
-
}
|
|
546
|
-
if (input.eventType === "pi.tool.call") {
|
|
547
|
-
const toolName = readWsEventToolName(input.data);
|
|
548
|
-
const toolCallId = canonicalizeToolCallId({
|
|
549
|
-
projection,
|
|
550
|
-
data: input.data,
|
|
551
|
-
timestamp,
|
|
552
|
-
toolName,
|
|
553
|
-
preferOpenCall: false
|
|
554
|
-
});
|
|
555
|
-
const canonicalData = readWsEventToolCallId(input.data) === toolCallId ? input.data : { ...input.data, toolCallId };
|
|
556
|
-
const existingToolCall = projection.toolCallsById.get(toolCallId);
|
|
557
|
-
if (!existingToolCall) {
|
|
558
|
-
const toolCall = {
|
|
559
|
-
toolCallId,
|
|
560
|
-
toolName,
|
|
561
|
-
status: "running",
|
|
562
|
-
startedAt: timestamp,
|
|
563
|
-
completedAt: null,
|
|
564
|
-
arguments: readWsEventToolArguments(canonicalData),
|
|
565
|
-
result: null,
|
|
566
|
-
isError: false
|
|
567
|
-
};
|
|
568
|
-
projection.toolCalls.push(toolCall);
|
|
569
|
-
projection.toolCallsById.set(toolCallId, toolCall);
|
|
570
|
-
}
|
|
571
|
-
ensureToolTraceItem(projection, toolCallId);
|
|
572
|
-
addOpenToolCallId(projection, toolName, toolCallId);
|
|
573
|
-
return {
|
|
574
|
-
canonicalData,
|
|
575
|
-
timestamp,
|
|
576
|
-
toolCallId,
|
|
577
|
-
toolName
|
|
578
|
-
};
|
|
579
|
-
}
|
|
580
|
-
if (input.eventType === "pi.tool.result") {
|
|
581
|
-
const toolName = readWsEventToolName(input.data);
|
|
582
|
-
const toolCallId = canonicalizeToolCallId({
|
|
583
|
-
projection,
|
|
584
|
-
data: input.data,
|
|
585
|
-
timestamp,
|
|
586
|
-
toolName,
|
|
587
|
-
preferOpenCall: true
|
|
588
|
-
});
|
|
589
|
-
const canonicalData = readWsEventToolCallId(input.data) === toolCallId ? input.data : { ...input.data, toolCallId };
|
|
590
|
-
const existingToolCall = projection.toolCallsById.get(toolCallId);
|
|
591
|
-
if (existingToolCall) {
|
|
592
|
-
existingToolCall.status = "completed";
|
|
593
|
-
existingToolCall.completedAt = timestamp;
|
|
594
|
-
existingToolCall.result = canonicalData.result ?? null;
|
|
595
|
-
existingToolCall.isError = canonicalData.isError === true;
|
|
596
|
-
removeOpenToolCallId(projection, toolCallId);
|
|
597
|
-
} else {
|
|
598
|
-
const completedToolCall = {
|
|
599
|
-
toolCallId,
|
|
600
|
-
toolName,
|
|
601
|
-
status: "completed",
|
|
602
|
-
startedAt: timestamp,
|
|
603
|
-
completedAt: timestamp,
|
|
604
|
-
arguments: null,
|
|
605
|
-
result: canonicalData.result ?? null,
|
|
606
|
-
isError: canonicalData.isError === true
|
|
607
|
-
};
|
|
608
|
-
projection.toolCalls.push(completedToolCall);
|
|
609
|
-
projection.toolCallsById.set(toolCallId, completedToolCall);
|
|
610
|
-
}
|
|
611
|
-
ensureToolTraceItem(projection, toolCallId);
|
|
612
|
-
return {
|
|
613
|
-
canonicalData,
|
|
614
|
-
timestamp,
|
|
615
|
-
toolCallId,
|
|
616
|
-
toolName
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
if (input.eventType === "pi.usage") {
|
|
620
|
-
projection.usage = mergeUsageSnapshots(projection.usage, normalizeUsageSnapshot(input.data));
|
|
621
|
-
return {
|
|
622
|
-
canonicalData: input.data,
|
|
623
|
-
timestamp,
|
|
624
|
-
toolCallId: null,
|
|
625
|
-
toolName: null
|
|
626
|
-
};
|
|
627
|
-
}
|
|
628
|
-
const operationalTraceItem = buildPrimaryPathOperationalTraceItem({
|
|
629
|
-
eventType: input.eventType,
|
|
630
|
-
data: input.data,
|
|
631
|
-
fallbackTimestamp: input.fallbackTimestamp
|
|
632
|
-
});
|
|
633
|
-
if (operationalTraceItem) {
|
|
634
|
-
projection.traceItems.push(operationalTraceItem);
|
|
635
|
-
return {
|
|
636
|
-
canonicalData: input.data,
|
|
637
|
-
timestamp: operationalTraceItem.timestamp,
|
|
638
|
-
toolCallId: null,
|
|
639
|
-
toolName: null
|
|
640
|
-
};
|
|
641
|
-
}
|
|
642
|
-
return {
|
|
643
|
-
canonicalData: input.data,
|
|
644
|
-
timestamp,
|
|
645
|
-
toolCallId: null,
|
|
646
|
-
toolName: null
|
|
647
|
-
};
|
|
648
|
-
}
|
|
649
|
-
function compareProcessEventsChronologically(left, right) {
|
|
650
|
-
const leftTimestamp = readWsEventTimestamp(asWsEventPayloadRecord(left.data), left.createdAt);
|
|
651
|
-
const rightTimestamp = readWsEventTimestamp(asWsEventPayloadRecord(right.data), right.createdAt);
|
|
652
|
-
const timestampComparison = compareTimestampStrings(leftTimestamp, rightTimestamp);
|
|
653
|
-
if (timestampComparison !== 0) {
|
|
654
|
-
return timestampComparison;
|
|
655
|
-
}
|
|
656
|
-
const createdAtComparison = compareTimestampStrings(left.createdAt, right.createdAt);
|
|
657
|
-
if (createdAtComparison !== 0) {
|
|
658
|
-
return createdAtComparison;
|
|
659
|
-
}
|
|
660
|
-
return left.id.localeCompare(right.id);
|
|
661
|
-
}
|
|
662
|
-
function buildLiveTurnProjectionFromEvents(events) {
|
|
663
|
-
const projection = createMutableLiveTurnProjection();
|
|
664
|
-
for (const event of [...events].sort(compareProcessEventsChronologically)) {
|
|
665
|
-
applyPiEventToLiveTurnProjection(projection, {
|
|
666
|
-
eventType: event.eventType,
|
|
667
|
-
data: asWsEventPayloadRecord(event.data),
|
|
668
|
-
fallbackTimestamp: event.createdAt
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
return projection;
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
// src/pi-session-message.ts
|
|
675
|
-
function isPiSessionMessageEntryType(entry) {
|
|
676
|
-
return entry?.type === "message";
|
|
677
|
-
}
|
|
678
|
-
function isPiSessionMessageRecord(value) {
|
|
679
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
680
|
-
}
|
|
681
|
-
function isPiSessionMessageEntryWithRecord(entry) {
|
|
682
|
-
return isPiSessionMessageEntryType(entry) && isPiSessionMessageRecord(entry.message);
|
|
683
|
-
}
|
|
684
|
-
function extractPiSessionMessageText(content) {
|
|
685
|
-
if (typeof content === "string") {
|
|
686
|
-
return content;
|
|
687
|
-
}
|
|
688
|
-
if (!Array.isArray(content)) {
|
|
689
|
-
return "";
|
|
690
|
-
}
|
|
691
|
-
return content.map((block) => {
|
|
692
|
-
if (typeof block !== "object" || block === null || Array.isArray(block)) {
|
|
693
|
-
return "";
|
|
694
|
-
}
|
|
695
|
-
const record2 = block;
|
|
696
|
-
return record2.type === "text" && typeof record2.text === "string" ? record2.text : "";
|
|
697
|
-
}).join("");
|
|
698
|
-
}
|
|
699
|
-
|
|
700
|
-
// src/session-entry-tree.ts
|
|
701
|
-
function createReadonlyEntryTree(entries) {
|
|
702
|
-
const orderedEntries = [...entries];
|
|
703
|
-
const entriesById = new Map(orderedEntries.map((entry) => [entry.id, entry]));
|
|
704
|
-
const childrenByParentId = /* @__PURE__ */ new Map();
|
|
705
|
-
for (const entry of orderedEntries) {
|
|
706
|
-
if (typeof entry.parentId !== "string" || entry.parentId.trim() === "") {
|
|
707
|
-
continue;
|
|
708
|
-
}
|
|
709
|
-
const siblings = childrenByParentId.get(entry.parentId) ?? [];
|
|
710
|
-
siblings.push(entry);
|
|
711
|
-
childrenByParentId.set(entry.parentId, siblings);
|
|
712
|
-
}
|
|
713
|
-
const leafId = orderedEntries.at(-1)?.id ?? null;
|
|
714
|
-
const getEntry = (id) => entriesById.get(id);
|
|
715
|
-
const getChildren = (parentId) => (childrenByParentId.get(parentId) ?? []).slice();
|
|
716
|
-
const getBranch = (fromId) => {
|
|
717
|
-
const resolvedFromId = fromId ?? leafId ?? void 0;
|
|
718
|
-
if (!resolvedFromId) {
|
|
719
|
-
return [];
|
|
720
|
-
}
|
|
721
|
-
const branch = [];
|
|
722
|
-
const visited = /* @__PURE__ */ new Set();
|
|
723
|
-
let currentEntry = entriesById.get(resolvedFromId);
|
|
724
|
-
while (currentEntry) {
|
|
725
|
-
if (visited.has(currentEntry.id)) {
|
|
726
|
-
break;
|
|
727
|
-
}
|
|
728
|
-
visited.add(currentEntry.id);
|
|
729
|
-
branch.unshift(currentEntry);
|
|
730
|
-
currentEntry = currentEntry.parentId ? entriesById.get(currentEntry.parentId) : void 0;
|
|
731
|
-
}
|
|
732
|
-
return branch;
|
|
733
|
-
};
|
|
734
|
-
return {
|
|
735
|
-
entries: orderedEntries,
|
|
736
|
-
leafId,
|
|
737
|
-
getEntry,
|
|
738
|
-
getChildren,
|
|
739
|
-
getBranch
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// src/streamable-events.ts
|
|
744
|
-
var STREAMABLE_WORKER_EVENT_TYPES = [
|
|
745
|
-
"pi.stream.delta",
|
|
746
|
-
"pi.turn.start",
|
|
747
|
-
"pi.turn.end",
|
|
748
|
-
"pi.tool.call",
|
|
749
|
-
"pi.tool.result",
|
|
750
|
-
"pi.label.changed",
|
|
751
|
-
"pi.error",
|
|
752
|
-
"pi.retry.start",
|
|
753
|
-
"pi.retry.end",
|
|
754
|
-
"pi.usage"
|
|
755
|
-
];
|
|
756
|
-
var STREAMABLE_EVENT_TYPES = new Set(STREAMABLE_WORKER_EVENT_TYPES);
|
|
757
|
-
function isStreamableEvent(eventType) {
|
|
758
|
-
return STREAMABLE_EVENT_TYPES.has(eventType);
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// src/text-preview.ts
|
|
762
|
-
function truncateTextPreview(value, maxLength) {
|
|
763
|
-
return value.length <= maxLength ? { text: value, truncated: false } : { text: value.slice(0, maxLength).trimEnd(), truncated: true };
|
|
764
|
-
}
|
|
765
|
-
function buildTrailingLinePreview(value, lineCount, maxLength) {
|
|
766
|
-
const lines = value.replace(/\r\n?/g, "\n").split("\n");
|
|
767
|
-
if (lines.length === 1 && lines[0] === "") return { text: "", truncated: false };
|
|
768
|
-
let text = lines.slice(-lineCount).join("\n");
|
|
769
|
-
let truncated = lines.length > lineCount;
|
|
770
|
-
if (text.length > maxLength) {
|
|
771
|
-
text = text.slice(-maxLength);
|
|
772
|
-
truncated = true;
|
|
773
|
-
}
|
|
774
|
-
if (truncated) {
|
|
775
|
-
const previewLines = text.split("\n");
|
|
776
|
-
previewLines[0] = previewLines[0] ? `\u2026 ${previewLines[0]}` : "\u2026";
|
|
777
|
-
text = previewLines.join("\n");
|
|
778
|
-
}
|
|
779
|
-
return { text, truncated };
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
// src/timeline-projection.ts
|
|
783
|
-
function timelinePresentationForTurnType(turnType) {
|
|
784
|
-
if (turnType === "external") return "external_trigger";
|
|
785
|
-
if (turnType === "human") return "operator_decision";
|
|
786
|
-
return turnType === "automatic" ? "automatic_turn" : "llm_turn";
|
|
787
|
-
}
|
|
788
|
-
function buildActiveTimelineTurnSummary(activeTurn, input = { summary: `Current step: ${activeTurn.turnId}`, output: activeTurn.assistant.text.trim() }) {
|
|
789
|
-
return {
|
|
790
|
-
id: activeTurn.turnRecordId,
|
|
791
|
-
turnId: activeTurn.turnId,
|
|
792
|
-
turnType: activeTurn.turnType,
|
|
793
|
-
displayTurn: activeTurn.turnId,
|
|
794
|
-
outcome: "in_progress",
|
|
795
|
-
summary: input.summary,
|
|
796
|
-
output: input.output,
|
|
797
|
-
turnResultMarkdown: "",
|
|
798
|
-
pathType: activeTurn.pathType,
|
|
799
|
-
createdAt: activeTurn.startedAt,
|
|
800
|
-
presentation: timelinePresentationForTurnType(activeTurn.turnType),
|
|
801
|
-
status: "in_progress",
|
|
802
|
-
modelProfileId: null,
|
|
803
|
-
attemptNumber: 1,
|
|
804
|
-
parentTurnRecordId: null,
|
|
805
|
-
startedAt: activeTurn.startedAt,
|
|
806
|
-
endedAt: null,
|
|
807
|
-
actionSource: input.actionSource ?? null
|
|
808
|
-
};
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
// src/turn-continuation.ts
|
|
812
|
-
import { trimToNull } from "@leitwerk-dev/domain";
|
|
813
|
-
function createEntriesById(entries) {
|
|
814
|
-
return new Map(entries.map((entry) => [entry.id, entry]));
|
|
815
|
-
}
|
|
816
|
-
function branchContainsAncestor(entriesById, entryId, ancestorId) {
|
|
817
|
-
const visited = /* @__PURE__ */ new Set();
|
|
818
|
-
let current = entriesById.get(entryId);
|
|
819
|
-
while (current) {
|
|
820
|
-
if (visited.has(current.id)) {
|
|
821
|
-
return false;
|
|
822
|
-
}
|
|
823
|
-
visited.add(current.id);
|
|
824
|
-
if (current.id === ancestorId) {
|
|
825
|
-
return true;
|
|
826
|
-
}
|
|
827
|
-
current = current.parentId ? entriesById.get(current.parentId) : void 0;
|
|
828
|
-
}
|
|
829
|
-
return false;
|
|
830
|
-
}
|
|
831
|
-
function happenedOnOrBeforeEnd(entryTimestamp, endedAt) {
|
|
832
|
-
if (!endedAt) {
|
|
833
|
-
return true;
|
|
834
|
-
}
|
|
835
|
-
return compareTimestampStrings(entryTimestamp, endedAt) <= 0;
|
|
836
|
-
}
|
|
837
|
-
function isContinuableEntryType(entry) {
|
|
838
|
-
return isPiSessionMessageEntryType(entry) || entry?.type === "compaction";
|
|
839
|
-
}
|
|
840
|
-
function isExplicitLeafUsable(entriesById, leafId, forkPiEntryId, startedAt, endedAt) {
|
|
841
|
-
const leafEntry = entriesById.get(leafId);
|
|
842
|
-
if (!isContinuableEntryType(leafEntry)) {
|
|
843
|
-
return false;
|
|
844
|
-
}
|
|
845
|
-
if (!happenedOnOrAfterStart(leafEntry.timestamp, startedAt)) {
|
|
846
|
-
return false;
|
|
847
|
-
}
|
|
848
|
-
if (!happenedOnOrBeforeEnd(leafEntry.timestamp, endedAt)) {
|
|
849
|
-
return false;
|
|
850
|
-
}
|
|
851
|
-
if (!forkPiEntryId) {
|
|
852
|
-
return true;
|
|
853
|
-
}
|
|
854
|
-
return branchContainsAncestor(entriesById, leafId, forkPiEntryId);
|
|
855
|
-
}
|
|
856
|
-
function findLatestContinuableEntryIdOnBranch(entries, entriesById, options) {
|
|
857
|
-
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
858
|
-
const entry = entries[index];
|
|
859
|
-
if (!entry) {
|
|
860
|
-
continue;
|
|
861
|
-
}
|
|
862
|
-
if (!isContinuableEntryType(entry)) {
|
|
863
|
-
continue;
|
|
864
|
-
}
|
|
865
|
-
if (!happenedOnOrAfterStart(entry.timestamp, options.startedAt)) {
|
|
866
|
-
continue;
|
|
867
|
-
}
|
|
868
|
-
if (!happenedOnOrBeforeEnd(entry.timestamp, options.endedAt)) {
|
|
869
|
-
continue;
|
|
870
|
-
}
|
|
871
|
-
if (options.ancestorId && !branchContainsAncestor(entriesById, entry.id, options.ancestorId)) {
|
|
872
|
-
continue;
|
|
873
|
-
}
|
|
874
|
-
return entry.id;
|
|
875
|
-
}
|
|
876
|
-
return null;
|
|
877
|
-
}
|
|
878
|
-
function readMessageRole(entry) {
|
|
879
|
-
const role = entry?.message?.role;
|
|
880
|
-
return typeof role === "string" && role.trim() !== "" ? role : null;
|
|
881
|
-
}
|
|
882
|
-
function resolveTurnContinuationLeafEntryIdFromIndex(entries, entriesById, turnRecord, bounds = {}) {
|
|
883
|
-
const startedAt = trimToNull(turnRecord.startedAt);
|
|
884
|
-
const endedAt = trimToNull(bounds.endedAt);
|
|
885
|
-
const forkPiEntryId = trimToNull(turnRecord.forkPiEntryId);
|
|
886
|
-
const explicitLeafId = trimToNull(turnRecord.resultPiEntryId);
|
|
887
|
-
if (explicitLeafId && isExplicitLeafUsable(entriesById, explicitLeafId, forkPiEntryId, startedAt, endedAt)) {
|
|
888
|
-
if (turnRecord.status !== "failed") {
|
|
889
|
-
return explicitLeafId;
|
|
890
|
-
}
|
|
891
|
-
return findLatestContinuableEntryIdOnBranch(entries, entriesById, {
|
|
892
|
-
startedAt,
|
|
893
|
-
endedAt,
|
|
894
|
-
ancestorId: explicitLeafId
|
|
895
|
-
});
|
|
896
|
-
}
|
|
897
|
-
return findLatestContinuableEntryIdOnBranch(entries, entriesById, {
|
|
898
|
-
startedAt,
|
|
899
|
-
endedAt,
|
|
900
|
-
ancestorId: forkPiEntryId
|
|
901
|
-
});
|
|
902
|
-
}
|
|
903
|
-
function createTurnContinuationIndex(entries) {
|
|
904
|
-
const orderedEntries = entries;
|
|
905
|
-
const entriesById = createEntriesById(orderedEntries);
|
|
906
|
-
const resolveLeafEntryId = (turnRecord, bounds = {}) => resolveTurnContinuationLeafEntryIdFromIndex(orderedEntries, entriesById, turnRecord, bounds);
|
|
907
|
-
const buildSlice = (turnRecord, bounds = {}) => {
|
|
908
|
-
const continuationLeafId = resolveLeafEntryId(turnRecord, bounds);
|
|
909
|
-
if (!continuationLeafId) {
|
|
910
|
-
return [];
|
|
911
|
-
}
|
|
912
|
-
const forkPiEntryId = trimToNull(turnRecord.forkPiEntryId);
|
|
913
|
-
const reversedSlice = [];
|
|
914
|
-
const visited = /* @__PURE__ */ new Set();
|
|
915
|
-
let current = entriesById.get(continuationLeafId);
|
|
916
|
-
let foundFork = forkPiEntryId === null;
|
|
917
|
-
while (current && !visited.has(current.id)) {
|
|
918
|
-
visited.add(current.id);
|
|
919
|
-
if (current.id === forkPiEntryId) {
|
|
920
|
-
foundFork = true;
|
|
921
|
-
break;
|
|
922
|
-
}
|
|
923
|
-
reversedSlice.push(current);
|
|
924
|
-
current = current.parentId ? entriesById.get(current.parentId) : void 0;
|
|
925
|
-
}
|
|
926
|
-
if (!foundFork) {
|
|
927
|
-
return [];
|
|
928
|
-
}
|
|
929
|
-
const startedAt = trimToNull(turnRecord.startedAt);
|
|
930
|
-
const endedAt = trimToNull(bounds.endedAt);
|
|
931
|
-
return reversedSlice.reverse().filter(
|
|
932
|
-
(entry) => happenedOnOrAfterStart(entry.timestamp, startedAt) && happenedOnOrBeforeEnd(entry.timestamp, endedAt)
|
|
933
|
-
);
|
|
934
|
-
};
|
|
935
|
-
return { resolveLeafEntryId, buildSlice };
|
|
936
|
-
}
|
|
937
|
-
function resolveTurnContinuationLeafEntryId(entries, turnRecord, bounds = {}) {
|
|
938
|
-
return createTurnContinuationIndex(entries).resolveLeafEntryId(turnRecord, bounds);
|
|
939
|
-
}
|
|
940
|
-
function buildTurnContinuationSliceFromTree(tree, turnRecord, bounds = {}) {
|
|
941
|
-
return createTurnContinuationIndex(tree.entries).buildSlice(turnRecord, bounds);
|
|
942
|
-
}
|
|
943
|
-
function buildTurnContinuationSlice(entries, turnRecord, bounds = {}) {
|
|
944
|
-
return buildTurnContinuationSliceFromTree(createReadonlyEntryTree(entries), turnRecord, bounds);
|
|
945
|
-
}
|
|
946
|
-
function extractFirstUserPromptOnBranch(tree, leafId) {
|
|
947
|
-
const resolvedLeafId = trimToNull(leafId);
|
|
948
|
-
if (!resolvedLeafId) {
|
|
949
|
-
return { text: null, createdAt: null };
|
|
950
|
-
}
|
|
951
|
-
const branch = tree.getBranch(resolvedLeafId);
|
|
952
|
-
for (const entry of branch) {
|
|
953
|
-
if (!isPiSessionMessageEntryWithRecord(entry) || readMessageRole(entry) !== "user") {
|
|
954
|
-
continue;
|
|
955
|
-
}
|
|
956
|
-
const text = extractPiSessionMessageText(entry.message.content).trim();
|
|
957
|
-
if (text === "") {
|
|
958
|
-
continue;
|
|
959
|
-
}
|
|
960
|
-
return {
|
|
961
|
-
text,
|
|
962
|
-
createdAt: entry.timestamp
|
|
963
|
-
};
|
|
964
|
-
}
|
|
965
|
-
return { text: null, createdAt: null };
|
|
966
|
-
}
|
|
967
|
-
function resolveTurnContinuationUserPrompt(entries, turnRecord, bounds = {}) {
|
|
968
|
-
const continuationLeafId = resolveTurnContinuationLeafEntryId(entries, turnRecord, bounds);
|
|
969
|
-
if (!continuationLeafId) {
|
|
970
|
-
return null;
|
|
971
|
-
}
|
|
972
|
-
const continuationLeaf = createEntriesById(entries).get(continuationLeafId);
|
|
973
|
-
if (!isPiSessionMessageEntryWithRecord(continuationLeaf) || readMessageRole(continuationLeaf) !== "user") {
|
|
974
|
-
return null;
|
|
975
|
-
}
|
|
976
|
-
const promptText = extractPiSessionMessageText(continuationLeaf.message.content).trim();
|
|
977
|
-
return promptText !== "" ? promptText : null;
|
|
978
|
-
}
|
|
979
|
-
function hasTurnContinuationProgress(entries, turnRecord, bounds = {}) {
|
|
980
|
-
return resolveTurnContinuationLeafEntryId(entries, turnRecord, bounds) !== null;
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
// src/usage-by-turn-record.ts
|
|
984
|
-
function buildUsageSnapshotsByTurnRecordId(events) {
|
|
985
|
-
const snapshots = {};
|
|
986
|
-
for (const event of events) {
|
|
987
|
-
if (event.eventType !== "pi.usage") {
|
|
988
|
-
continue;
|
|
989
|
-
}
|
|
990
|
-
const payload = asWsEventPayloadRecord(event.data);
|
|
991
|
-
const turnRecordId = readWsEventTurnRecordId(payload);
|
|
992
|
-
if (!turnRecordId) {
|
|
993
|
-
continue;
|
|
994
|
-
}
|
|
995
|
-
const usage = normalizeUsageSnapshot(payload);
|
|
996
|
-
if (!usage) {
|
|
997
|
-
continue;
|
|
998
|
-
}
|
|
999
|
-
const merged = mergeUsageSnapshots(snapshots[turnRecordId] ?? null, usage);
|
|
1000
|
-
if (merged) {
|
|
1001
|
-
snapshots[turnRecordId] = merged;
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
return snapshots;
|
|
1005
|
-
}
|
|
1006
|
-
export {
|
|
1007
|
-
INITIAL_PROMPT_FIELD_PRIORITY,
|
|
1008
|
-
PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES,
|
|
1009
|
-
STREAMABLE_WORKER_EVENT_TYPES,
|
|
1010
|
-
TOOL_CALL_RENDERER_VALUE_KINDS,
|
|
1011
|
-
TOOL_CALL_RENDERER_VALUE_SOURCES,
|
|
1012
|
-
WS_FRAME_DURABILITY,
|
|
1013
|
-
WS_PI_STREAM_TYPES,
|
|
1014
|
-
WS_PRIMARY_PATH_TYPES,
|
|
1015
|
-
WS_PROCESS_TYPES,
|
|
1016
|
-
WS_PROTOCOL_VERSION,
|
|
1017
|
-
applyPiEventToLiveTurnProjection,
|
|
1018
|
-
asWsEventPayloadRecord,
|
|
1019
|
-
buildActiveTimelineTurnSummary,
|
|
1020
|
-
buildLiveTurnProjectionFromEvents,
|
|
1021
|
-
buildPrimaryPathOperationalTraceItem,
|
|
1022
|
-
buildTrailingLinePreview,
|
|
1023
|
-
buildTurnContinuationSlice,
|
|
1024
|
-
buildTurnContinuationSliceFromTree,
|
|
1025
|
-
buildUsageSnapshotsByTurnRecordId,
|
|
1026
|
-
cloneUsageSnapshot,
|
|
1027
|
-
compareTimestampStrings,
|
|
1028
|
-
createDurableWsFrame,
|
|
1029
|
-
createEphemeralWsFrame,
|
|
1030
|
-
createMutableLiveTurnProjection,
|
|
1031
|
-
createReadonlyEntryTree,
|
|
1032
|
-
createTurnContinuationIndex,
|
|
1033
|
-
err,
|
|
1034
|
-
extractFirstUserPromptOnBranch,
|
|
1035
|
-
extractInitialPromptFromParamsJson,
|
|
1036
|
-
extractInitialPromptFromValue,
|
|
1037
|
-
extractInitialPromptPreviewFromParamsJson,
|
|
1038
|
-
extractPiSessionMessageText,
|
|
1039
|
-
happenedOnOrAfterStart,
|
|
1040
|
-
hasTurnContinuationProgress,
|
|
1041
|
-
isPiSessionMessageEntryType,
|
|
1042
|
-
isPiSessionMessageEntryWithRecord,
|
|
1043
|
-
isPiSessionMessageRecord,
|
|
1044
|
-
isStreamableEvent,
|
|
1045
|
-
mapWorkerEventToWsType,
|
|
1046
|
-
mergeUsageSnapshots,
|
|
1047
|
-
normalizeUsageSnapshot,
|
|
1048
|
-
nullableStringSchema,
|
|
1049
|
-
ok,
|
|
1050
|
-
optionalNullableStringSchema,
|
|
1051
|
-
parseActionRequestBody,
|
|
1052
|
-
parseFutureActionPayloadJson,
|
|
1053
|
-
parseFutureActionPayloadObject,
|
|
1054
|
-
parseFutureLaunchPayloadJson,
|
|
1055
|
-
parseFutureLaunchPayloadObject,
|
|
1056
|
-
parseLauncherInputJson,
|
|
1057
|
-
parseLauncherModelConfigInput,
|
|
1058
|
-
parseLauncherRequestBody,
|
|
1059
|
-
parseLauncherTurnConfigsJson,
|
|
1060
|
-
parseLiteral,
|
|
1061
|
-
parsePrimaryPathWsFrameInput,
|
|
1062
|
-
parseScheduleRequestInput,
|
|
1063
|
-
parseSchema,
|
|
1064
|
-
parseWsFrame,
|
|
1065
|
-
projectFutureExecutionOverview,
|
|
1066
|
-
readValueAtPath,
|
|
1067
|
-
readWsEventNonEmptyString,
|
|
1068
|
-
readWsEventPiTurnId,
|
|
1069
|
-
readWsEventStreamText,
|
|
1070
|
-
readWsEventTimestamp,
|
|
1071
|
-
readWsEventToolArguments,
|
|
1072
|
-
readWsEventToolCallId,
|
|
1073
|
-
readWsEventToolName,
|
|
1074
|
-
readWsEventTurnRecordId,
|
|
1075
|
-
resolvePromptCacheSwitch,
|
|
1076
|
-
resolveTurnContinuationLeafEntryId,
|
|
1077
|
-
resolveTurnContinuationUserPrompt,
|
|
1078
|
-
resolveWsEventToolCallId,
|
|
1079
|
-
serializeFutureActionPayload,
|
|
1080
|
-
serializeFutureLaunchPayload,
|
|
1081
|
-
snapshotLiveTurnProjection,
|
|
1082
|
-
timelinePresentationForTurnType,
|
|
1083
|
-
truncatePromptPreview,
|
|
1084
|
-
truncateTextPreview,
|
|
1085
|
-
unknownRecordSchema,
|
|
1086
|
-
validateScheduleRequestInput
|
|
1087
|
-
};
|
|
1
|
+
export * from "./config-snapshot.js";
|
|
2
|
+
export * from "./external-review.js";
|
|
3
|
+
export * from "./form-contract.js";
|
|
4
|
+
export * from "./http-contracts.js";
|
|
5
|
+
export * from "./initial-prompt.js";
|
|
6
|
+
export * from "./launcher-contract.js";
|
|
7
|
+
export * from "./live-turn-projection.js";
|
|
8
|
+
export * from "./pi-session-message.js";
|
|
9
|
+
export * from "./primary-path-snapshot.js";
|
|
10
|
+
export * from "./protocol.js";
|
|
11
|
+
export * from "./session-entry-tree.js";
|
|
12
|
+
export * from "./streamable-events.js";
|
|
13
|
+
export * from "./text-preview.js";
|
|
14
|
+
export * from "./timeline-projection.js";
|
|
15
|
+
export * from "./timestamp-ordering.js";
|
|
16
|
+
export * from "./tool-renderer-contract.js";
|
|
17
|
+
export * from "./turn-continuation.js";
|
|
18
|
+
export * from "./usage-by-turn-record.js";
|
|
19
|
+
export * from "./usage-snapshot.js";
|
|
20
|
+
export * from "./ws-event-payloads.js";
|
|
1088
21
|
//# sourceMappingURL=index.js.map
|