@agentknight/sdk 0.9.56 → 0.9.57
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/chunks/{acp-adapter-HUNUQGUF.js → acp-adapter-WYDK3PLS.js} +4 -3
- package/dist/chunks/chunk-3LEPYV7Y.js +72 -0
- package/dist/chunks/{chunk-L3A736SO.js → chunk-7ZYBES47.js} +2 -66
- package/dist/chunks/{chunk-NTFNQYQ3.js → chunk-DAKDVZ7X.js} +2 -2
- package/dist/chunks/{chunk-HFVA7ZOD.js → chunk-EDZPP6SJ.js} +2 -2
- package/dist/chunks/{chunk-5J7BK2Y7.js → chunk-IPJWNGNK.js} +1 -1
- package/dist/chunks/{chunk-K5CMKBW6.js → chunk-KBI5IWPR.js} +183 -51
- package/dist/chunks/chunk-LT5ULDVT.js +1217 -0
- package/dist/chunks/{chunk-6ZPE7VIE.js → chunk-OOCAIULD.js} +28 -1223
- package/dist/chunks/{chunk-Z5STULMQ.js → chunk-USI66MEF.js} +1 -1
- package/dist/chunks/{chunk-E7QL4CBT.js → chunk-WPVH7HKC.js} +1 -1
- package/dist/chunks/chunk-ZLKFFKAX.js +571 -0
- package/dist/chunks/{claude-adapter-G6YTPXRP.js → claude-adapter-GCH36EZ6.js} +4 -3
- package/dist/chunks/{codex-adapter-ELYJE2A2.js → codex-adapter-CXD4SVCE.js} +4 -3
- package/dist/chunks/{copilot-adapter-2QAIOSEK.js → copilot-adapter-SYFGOAGE.js} +456 -632
- package/dist/chunks/{managed-agent-adapter-KEXZFLGA.js → managed-agent-adapter-ODCNYYO6.js} +14 -6
- package/dist/chunks/{pi-adapter-5BFIJTPD.js → pi-adapter-XLY46UDL.js} +4 -3
- package/dist/configuration.js +4 -3
- package/dist/index.d.ts +51 -0
- package/dist/index.js +62 -34
- package/dist/runtime.js +2 -2
- package/dist/session-analytics-worker.js +474 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CONVERSATION_SOURCE_ID_LIMIT
|
|
3
|
+
} from "./chunk-KBI5IWPR.js";
|
|
4
|
+
|
|
5
|
+
// src/conversation-normalizer.ts
|
|
6
|
+
import { createHash } from "node:crypto";
|
|
7
|
+
var SUMMARY_LIMIT = 4096;
|
|
8
|
+
var IDENTIFIER_LIMIT = 256;
|
|
9
|
+
var TASK_COMPLETE_TOOL_NAME = "task_complete";
|
|
10
|
+
var MAX_INLINE_IMAGE_BYTES = 3 * 1024 * 1024;
|
|
11
|
+
var MAX_INLINE_IMAGE_MESSAGE_BYTES = 3 * 1024 * 1024;
|
|
12
|
+
var MAX_INLINE_IMAGES_PER_MESSAGE = 4;
|
|
13
|
+
var SUPPORTED_IMAGE_MIME_TYPES = /* @__PURE__ */ new Set([
|
|
14
|
+
"image/gif",
|
|
15
|
+
"image/jpeg",
|
|
16
|
+
"image/png",
|
|
17
|
+
"image/webp"
|
|
18
|
+
]);
|
|
19
|
+
function splitToolImages(images) {
|
|
20
|
+
const groups = [];
|
|
21
|
+
let group = [];
|
|
22
|
+
let groupBytes = 0;
|
|
23
|
+
for (const image of images) {
|
|
24
|
+
const imageBytes = image.attachment.byteLength ?? 0;
|
|
25
|
+
if (group.length > 0 && (group.length >= MAX_INLINE_IMAGES_PER_MESSAGE || groupBytes + imageBytes > MAX_INLINE_IMAGE_MESSAGE_BYTES)) {
|
|
26
|
+
groups.push(group);
|
|
27
|
+
group = [];
|
|
28
|
+
groupBytes = 0;
|
|
29
|
+
}
|
|
30
|
+
group.push(image);
|
|
31
|
+
groupBytes += imageBytes;
|
|
32
|
+
}
|
|
33
|
+
if (group.length > 0) {
|
|
34
|
+
groups.push(group);
|
|
35
|
+
}
|
|
36
|
+
return groups;
|
|
37
|
+
}
|
|
38
|
+
function protocolId(value) {
|
|
39
|
+
if (value.length <= IDENTIFIER_LIMIT) {
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
const digest = createHash("sha256").update(value).digest("hex");
|
|
43
|
+
return `sha256:${digest}`;
|
|
44
|
+
}
|
|
45
|
+
function scopedId(scope, value) {
|
|
46
|
+
const id = `${scope}:${value}`;
|
|
47
|
+
if (id.length <= IDENTIFIER_LIMIT) {
|
|
48
|
+
return id;
|
|
49
|
+
}
|
|
50
|
+
const digest = createHash("sha256").update(value).digest("hex");
|
|
51
|
+
return `${scope}:sha256:${digest}`;
|
|
52
|
+
}
|
|
53
|
+
function assistantMessageId(data) {
|
|
54
|
+
return scopedId("assistant", data.apiCallId ?? data.messageId);
|
|
55
|
+
}
|
|
56
|
+
function eventRevision(timestamp, previousRevision) {
|
|
57
|
+
const timestampRevision = Date.parse(timestamp);
|
|
58
|
+
if (!Number.isSafeInteger(timestampRevision) || timestampRevision < 0) {
|
|
59
|
+
throw new Error(`Invalid SDK event timestamp: ${timestamp}`);
|
|
60
|
+
}
|
|
61
|
+
const revision = previousRevision === void 0 || timestampRevision > previousRevision ? timestampRevision : previousRevision + 1;
|
|
62
|
+
if (!Number.isSafeInteger(revision)) {
|
|
63
|
+
throw new Error(`SDK event revision exceeds the safe integer range: ${timestamp}`);
|
|
64
|
+
}
|
|
65
|
+
return revision;
|
|
66
|
+
}
|
|
67
|
+
function removeInternalMarkup(value) {
|
|
68
|
+
return value.replace(
|
|
69
|
+
/<skill-context\b[^>]*>[\s\S]*?<\/skill-context\s*>/giu,
|
|
70
|
+
" "
|
|
71
|
+
).replace(/<skill-context\b[^>]*>[\s\S]*/giu, " ").replace(/<\/?system_notification\b[^>]*>/giu, " ");
|
|
72
|
+
}
|
|
73
|
+
function stripInternalMarkup(value) {
|
|
74
|
+
return removeInternalMarkup(value).replace(/\s+/gu, " ").trim();
|
|
75
|
+
}
|
|
76
|
+
function limitSummary(value, limit = SUMMARY_LIMIT) {
|
|
77
|
+
if (value.length <= limit) {
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
return `${value.slice(0, limit - 1)}\u2026`;
|
|
81
|
+
}
|
|
82
|
+
function boundedText(value, limit, fallback) {
|
|
83
|
+
const normalized = stripInternalMarkup(value);
|
|
84
|
+
return limitSummary(normalized.length === 0 ? fallback : normalized, limit);
|
|
85
|
+
}
|
|
86
|
+
function boundedOptionalText(value, limit) {
|
|
87
|
+
if (value === void 0) {
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
const normalized = stripInternalMarkup(value);
|
|
91
|
+
return normalized.length === 0 ? void 0 : limitSummary(normalized, limit);
|
|
92
|
+
}
|
|
93
|
+
function boundedOptionalMessageContent(value, limit) {
|
|
94
|
+
if (value === void 0) {
|
|
95
|
+
return void 0;
|
|
96
|
+
}
|
|
97
|
+
const normalized = removeInternalMarkup(value).trim();
|
|
98
|
+
return normalized.length === 0 ? void 0 : limitSummary(normalized, limit);
|
|
99
|
+
}
|
|
100
|
+
function appendSourceEventId(sourceEventIds, eventId) {
|
|
101
|
+
return [...sourceEventIds, eventId].slice(-CONVERSATION_SOURCE_ID_LIMIT);
|
|
102
|
+
}
|
|
103
|
+
function appendOptionalSourceId(sourceIds, sourceId) {
|
|
104
|
+
if (sourceId === void 0) {
|
|
105
|
+
return sourceIds === void 0 ? void 0 : [...sourceIds];
|
|
106
|
+
}
|
|
107
|
+
return [...sourceIds ?? [], sourceId].filter((candidate, index, all) => all.indexOf(candidate) === index).slice(-CONVERSATION_SOURCE_ID_LIMIT);
|
|
108
|
+
}
|
|
109
|
+
function appendAssistantContent(existing, next) {
|
|
110
|
+
const combined = existing.length > 0 && next.length > 0 ? `${existing}
|
|
111
|
+
|
|
112
|
+
${next}` : existing || next;
|
|
113
|
+
if (combined.length <= 5e5) {
|
|
114
|
+
return combined;
|
|
115
|
+
}
|
|
116
|
+
return `\u2026${combined.slice(combined.length - 499999)}`;
|
|
117
|
+
}
|
|
118
|
+
function boundAttachment(attachment) {
|
|
119
|
+
return {
|
|
120
|
+
...attachment,
|
|
121
|
+
name: boundedText(attachment.name, 1024, "Attachment"),
|
|
122
|
+
...attachment.mimeType === void 0 ? {} : {
|
|
123
|
+
mimeType: boundedText(
|
|
124
|
+
attachment.mimeType,
|
|
125
|
+
256,
|
|
126
|
+
"application/octet-stream"
|
|
127
|
+
)
|
|
128
|
+
},
|
|
129
|
+
...attachment.uri === void 0 ? {} : { uri: boundedText(attachment.uri, 32768, "unknown") }
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function summarizeUnknown(value) {
|
|
133
|
+
if (typeof value === "string") {
|
|
134
|
+
return limitSummary(stripInternalMarkup(value));
|
|
135
|
+
}
|
|
136
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
137
|
+
return String(value);
|
|
138
|
+
}
|
|
139
|
+
if (value === null) {
|
|
140
|
+
return "null";
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
return limitSummary(stripInternalMarkup(JSON.stringify(value)));
|
|
144
|
+
} catch {
|
|
145
|
+
return "[unserializable]";
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function isDisplayableHumanMessage(event) {
|
|
149
|
+
if (event.type !== "user.message" || event.agentId !== void 0) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const source = event.data.source;
|
|
153
|
+
return event.data.isAutopilotContinuation !== true && (source === void 0 || source === "user" || source === "mobile" || /^schedule-[1-9]\d*$/.test(source));
|
|
154
|
+
}
|
|
155
|
+
function projectAttachment(attachment) {
|
|
156
|
+
switch (attachment.type) {
|
|
157
|
+
case "file":
|
|
158
|
+
return {
|
|
159
|
+
kind: attachment.mimeType?.startsWith("image/") ? "image" : "file",
|
|
160
|
+
name: attachment.displayName,
|
|
161
|
+
...attachment.mimeType === void 0 ? {} : { mimeType: attachment.mimeType },
|
|
162
|
+
uri: attachment.path
|
|
163
|
+
};
|
|
164
|
+
case "directory":
|
|
165
|
+
return {
|
|
166
|
+
kind: "file",
|
|
167
|
+
name: attachment.displayName,
|
|
168
|
+
uri: attachment.path
|
|
169
|
+
};
|
|
170
|
+
case "selection":
|
|
171
|
+
return {
|
|
172
|
+
kind: "selection",
|
|
173
|
+
name: attachment.displayName,
|
|
174
|
+
uri: attachment.filePath
|
|
175
|
+
};
|
|
176
|
+
case "github_reference":
|
|
177
|
+
return {
|
|
178
|
+
kind: "github-reference",
|
|
179
|
+
name: attachment.title,
|
|
180
|
+
uri: attachment.url
|
|
181
|
+
};
|
|
182
|
+
case "github_commit":
|
|
183
|
+
return {
|
|
184
|
+
kind: "github-reference",
|
|
185
|
+
name: attachment.message,
|
|
186
|
+
uri: attachment.url
|
|
187
|
+
};
|
|
188
|
+
case "github_release":
|
|
189
|
+
return {
|
|
190
|
+
kind: "github-reference",
|
|
191
|
+
name: attachment.name,
|
|
192
|
+
uri: attachment.url
|
|
193
|
+
};
|
|
194
|
+
case "github_actions_job":
|
|
195
|
+
return {
|
|
196
|
+
kind: "github-reference",
|
|
197
|
+
name: attachment.jobName,
|
|
198
|
+
uri: attachment.url
|
|
199
|
+
};
|
|
200
|
+
case "github_repository":
|
|
201
|
+
return {
|
|
202
|
+
kind: "github-reference",
|
|
203
|
+
name: `${attachment.repo.owner}/${attachment.repo.name}`,
|
|
204
|
+
uri: attachment.url
|
|
205
|
+
};
|
|
206
|
+
case "github_file_diff":
|
|
207
|
+
return {
|
|
208
|
+
kind: "github-reference",
|
|
209
|
+
name: "GitHub file diff",
|
|
210
|
+
uri: attachment.url
|
|
211
|
+
};
|
|
212
|
+
case "github_tree_comparison":
|
|
213
|
+
return {
|
|
214
|
+
kind: "github-reference",
|
|
215
|
+
name: "GitHub tree comparison",
|
|
216
|
+
uri: attachment.url
|
|
217
|
+
};
|
|
218
|
+
case "github_url":
|
|
219
|
+
return {
|
|
220
|
+
kind: "github-reference",
|
|
221
|
+
name: attachment.url,
|
|
222
|
+
uri: attachment.url
|
|
223
|
+
};
|
|
224
|
+
case "github_file":
|
|
225
|
+
case "github_snippet":
|
|
226
|
+
return {
|
|
227
|
+
kind: "github-reference",
|
|
228
|
+
name: attachment.path,
|
|
229
|
+
uri: attachment.url
|
|
230
|
+
};
|
|
231
|
+
case "blob":
|
|
232
|
+
return {
|
|
233
|
+
kind: attachment.mimeType.startsWith("image/") ? "image" : "file",
|
|
234
|
+
name: attachment.displayName ?? "Attachment",
|
|
235
|
+
mimeType: attachment.mimeType
|
|
236
|
+
};
|
|
237
|
+
case "extension_context":
|
|
238
|
+
return {
|
|
239
|
+
kind: "unknown",
|
|
240
|
+
name: attachment.title
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function projectCitation(source) {
|
|
245
|
+
if (source.url !== void 0) {
|
|
246
|
+
return {
|
|
247
|
+
kind: "github-reference",
|
|
248
|
+
name: source.title ?? source.path ?? source.id,
|
|
249
|
+
uri: source.url
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
if (source.path !== void 0) {
|
|
253
|
+
return {
|
|
254
|
+
kind: "file",
|
|
255
|
+
name: source.title ?? source.path,
|
|
256
|
+
uri: source.path
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
kind: "unknown",
|
|
261
|
+
name: source.title ?? source.id
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function normalizeImageMimeType(value) {
|
|
265
|
+
return value.toLowerCase() === "image/jpg" ? "image/jpeg" : value.toLowerCase();
|
|
266
|
+
}
|
|
267
|
+
function hasImageSignature(bytes, mimeType) {
|
|
268
|
+
switch (mimeType) {
|
|
269
|
+
case "image/jpeg":
|
|
270
|
+
return bytes.length >= 3 && bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255;
|
|
271
|
+
case "image/png":
|
|
272
|
+
return bytes.length >= 8 && bytes.subarray(0, 8).equals(
|
|
273
|
+
Buffer.from([137, 80, 78, 71, 13, 10, 26, 10])
|
|
274
|
+
);
|
|
275
|
+
case "image/webp":
|
|
276
|
+
return bytes.length >= 12 && bytes.subarray(0, 4).toString("ascii") === "RIFF" && bytes.subarray(8, 12).toString("ascii") === "WEBP";
|
|
277
|
+
case "image/gif":
|
|
278
|
+
return bytes.length >= 6 && (bytes.subarray(0, 6).toString("ascii") === "GIF87a" || bytes.subarray(0, 6).toString("ascii") === "GIF89a");
|
|
279
|
+
default:
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function decodeImageBase64(value, expectedMimeType) {
|
|
284
|
+
const dataUrlMatch = /^data:([^;,]+);base64,(.*)$/isu.exec(value);
|
|
285
|
+
if (dataUrlMatch !== null && normalizeImageMimeType(dataUrlMatch[1] ?? "") !== expectedMimeType) {
|
|
286
|
+
return void 0;
|
|
287
|
+
}
|
|
288
|
+
const payload = dataUrlMatch?.[2] ?? value;
|
|
289
|
+
const normalized = payload.replaceAll("-", "+").replaceAll("_", "/");
|
|
290
|
+
if (normalized.length === 0 || normalized.length % 4 === 1 || !/^[A-Za-z0-9+/]*={0,2}$/u.test(normalized)) {
|
|
291
|
+
return void 0;
|
|
292
|
+
}
|
|
293
|
+
const bytes = Buffer.from(normalized, "base64");
|
|
294
|
+
return bytes.toString("base64").replace(/=+$/u, "") === normalized.replace(/=+$/u, "") ? bytes : void 0;
|
|
295
|
+
}
|
|
296
|
+
function projectInlineToolImage(data, mimeTypeValue, name, expectedByteLength, source = "binary-result") {
|
|
297
|
+
const mimeType = normalizeImageMimeType(mimeTypeValue);
|
|
298
|
+
const metadata = {
|
|
299
|
+
kind: "image",
|
|
300
|
+
name: boundedText(name, 1024, "Copilot image"),
|
|
301
|
+
mimeType: boundedText(mimeType, 256, "application/octet-stream")
|
|
302
|
+
};
|
|
303
|
+
if (!SUPPORTED_IMAGE_MIME_TYPES.has(mimeType)) {
|
|
304
|
+
return {
|
|
305
|
+
attachment: metadata,
|
|
306
|
+
failure: { source, reason: "mime-unsupported", mimeType }
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
const bytes = decodeImageBase64(data, mimeType);
|
|
310
|
+
if (bytes === void 0) {
|
|
311
|
+
return {
|
|
312
|
+
attachment: metadata,
|
|
313
|
+
failure: { source, reason: "data-invalid", mimeType }
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (bytes.length === 0) {
|
|
317
|
+
return {
|
|
318
|
+
attachment: metadata,
|
|
319
|
+
failure: { source, reason: "data-empty", mimeType, actualByteLength: 0 }
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
if (bytes.length > MAX_INLINE_IMAGE_BYTES) {
|
|
323
|
+
return {
|
|
324
|
+
attachment: metadata,
|
|
325
|
+
failure: {
|
|
326
|
+
source,
|
|
327
|
+
reason: "data-too-large",
|
|
328
|
+
mimeType,
|
|
329
|
+
actualByteLength: bytes.length
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
if (expectedByteLength !== void 0 && bytes.length !== expectedByteLength) {
|
|
334
|
+
return {
|
|
335
|
+
attachment: metadata,
|
|
336
|
+
failure: {
|
|
337
|
+
source,
|
|
338
|
+
reason: "length-mismatch",
|
|
339
|
+
mimeType,
|
|
340
|
+
expectedByteLength,
|
|
341
|
+
actualByteLength: bytes.length
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
if (!hasImageSignature(bytes, mimeType)) {
|
|
346
|
+
return {
|
|
347
|
+
attachment: metadata,
|
|
348
|
+
failure: {
|
|
349
|
+
source,
|
|
350
|
+
reason: "signature-mismatch",
|
|
351
|
+
mimeType,
|
|
352
|
+
actualByteLength: bytes.length
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
attachment: {
|
|
358
|
+
...metadata,
|
|
359
|
+
data: bytes.toString("base64"),
|
|
360
|
+
byteLength: bytes.length,
|
|
361
|
+
sha256: createHash("sha256").update(bytes).digest("hex")
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function projectToolBinaryResult(result, name, assets) {
|
|
366
|
+
if (result.type !== "image") {
|
|
367
|
+
return void 0;
|
|
368
|
+
}
|
|
369
|
+
if ("data" in result) {
|
|
370
|
+
return projectInlineToolImage(
|
|
371
|
+
result.data,
|
|
372
|
+
result.mimeType,
|
|
373
|
+
result.description ?? name
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
if ("assetId" in result) {
|
|
377
|
+
const asset = assets.get(result.assetId);
|
|
378
|
+
if (asset === void 0 || asset.data.type !== "image") {
|
|
379
|
+
return {
|
|
380
|
+
attachment: {
|
|
381
|
+
kind: "image",
|
|
382
|
+
name: boundedText(result.description ?? name, 1024, "Copilot image"),
|
|
383
|
+
mimeType: boundedText(
|
|
384
|
+
normalizeImageMimeType(result.mimeType),
|
|
385
|
+
256,
|
|
386
|
+
"application/octet-stream"
|
|
387
|
+
)
|
|
388
|
+
},
|
|
389
|
+
failure: {
|
|
390
|
+
source: "binary-asset",
|
|
391
|
+
reason: "asset-missing",
|
|
392
|
+
mimeType: normalizeImageMimeType(result.mimeType),
|
|
393
|
+
expectedByteLength: result.byteLength
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
const projected = projectInlineToolImage(
|
|
398
|
+
asset.data.data,
|
|
399
|
+
asset.data.mimeType,
|
|
400
|
+
result.description ?? asset.data.description ?? name,
|
|
401
|
+
void 0,
|
|
402
|
+
"binary-asset"
|
|
403
|
+
);
|
|
404
|
+
const attachment = projected.attachment;
|
|
405
|
+
if (projected.failure !== void 0) {
|
|
406
|
+
return { ...projected, sourceEventId: asset.eventId, sourceTimestamp: asset.timestamp };
|
|
407
|
+
}
|
|
408
|
+
if (attachment.sha256 !== void 0 && result.assetId !== `sha256:${attachment.sha256}`) {
|
|
409
|
+
return {
|
|
410
|
+
attachment: {
|
|
411
|
+
kind: "image",
|
|
412
|
+
name: attachment.name,
|
|
413
|
+
...attachment.mimeType === void 0 ? {} : { mimeType: attachment.mimeType }
|
|
414
|
+
},
|
|
415
|
+
sourceEventId: asset.eventId,
|
|
416
|
+
sourceTimestamp: asset.timestamp,
|
|
417
|
+
failure: {
|
|
418
|
+
source: "binary-asset",
|
|
419
|
+
reason: "asset-hash-mismatch",
|
|
420
|
+
mimeType: attachment.mimeType ?? "application/octet-stream",
|
|
421
|
+
expectedByteLength: result.byteLength,
|
|
422
|
+
...attachment.byteLength === void 0 ? {} : { actualByteLength: attachment.byteLength }
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
return {
|
|
427
|
+
attachment,
|
|
428
|
+
sourceEventId: asset.eventId,
|
|
429
|
+
sourceTimestamp: asset.timestamp
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
attachment: {
|
|
434
|
+
kind: "image",
|
|
435
|
+
name: boundedText(result.description ?? name, 1024, "Copilot image"),
|
|
436
|
+
mimeType: boundedText(
|
|
437
|
+
normalizeImageMimeType(result.mimeType),
|
|
438
|
+
256,
|
|
439
|
+
"application/octet-stream"
|
|
440
|
+
)
|
|
441
|
+
},
|
|
442
|
+
failure: {
|
|
443
|
+
source: "binary-result",
|
|
444
|
+
reason: "asset-missing",
|
|
445
|
+
mimeType: normalizeImageMimeType(result.mimeType),
|
|
446
|
+
expectedByteLength: result.byteLength
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function projectToolContentImage(image, name) {
|
|
451
|
+
return projectInlineToolImage(
|
|
452
|
+
image.data,
|
|
453
|
+
image.mimeType,
|
|
454
|
+
name,
|
|
455
|
+
void 0,
|
|
456
|
+
"content"
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
function permissionName(kind) {
|
|
460
|
+
const words = kind.replaceAll("_", " ").replaceAll("-", " ");
|
|
461
|
+
return `${words.charAt(0).toUpperCase()}${words.slice(1)} permission`;
|
|
462
|
+
}
|
|
463
|
+
var ConversationToolLifecycles = class {
|
|
464
|
+
#starts = /* @__PURE__ */ new Map();
|
|
465
|
+
get(toolCallId) {
|
|
466
|
+
return this.#starts.get(toolCallId);
|
|
467
|
+
}
|
|
468
|
+
remember(toolCallId, start) {
|
|
469
|
+
const previous = this.#starts.get(toolCallId);
|
|
470
|
+
const lifecycle = previous?.sourceEventId === start.sourceEventId && !start.hasTurnContext ? previous : start;
|
|
471
|
+
this.#starts.delete(toolCallId);
|
|
472
|
+
this.#starts.set(toolCallId, lifecycle);
|
|
473
|
+
while (this.#starts.size > 1024) {
|
|
474
|
+
const oldest = this.#starts.keys().next().value;
|
|
475
|
+
if (oldest === void 0) break;
|
|
476
|
+
this.#starts.delete(oldest);
|
|
477
|
+
}
|
|
478
|
+
return lifecycle;
|
|
479
|
+
}
|
|
480
|
+
clear() {
|
|
481
|
+
this.#starts.clear();
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
var ConversationNormalizer = class {
|
|
485
|
+
constructor(onImageFailure, toolLifecycles = new ConversationToolLifecycles(), resolveInputProvenance) {
|
|
486
|
+
this.onImageFailure = onImageFailure;
|
|
487
|
+
this.toolLifecycles = toolLifecycles;
|
|
488
|
+
this.resolveInputProvenance = resolveInputProvenance;
|
|
489
|
+
}
|
|
490
|
+
onImageFailure;
|
|
491
|
+
toolLifecycles;
|
|
492
|
+
resolveInputProvenance;
|
|
493
|
+
#items = /* @__PURE__ */ new Map();
|
|
494
|
+
#binaryAssets = /* @__PURE__ */ new Map();
|
|
495
|
+
#pendingToolImages = /* @__PURE__ */ new Map();
|
|
496
|
+
#toolTurns = /* @__PURE__ */ new Map();
|
|
497
|
+
#taskCompletionToolCalls = /* @__PURE__ */ new Map();
|
|
498
|
+
#subagentTurns = /* @__PURE__ */ new Map();
|
|
499
|
+
#permissionTurns = /* @__PURE__ */ new Map();
|
|
500
|
+
#loopTurns = /* @__PURE__ */ new Map();
|
|
501
|
+
#currentTurnId;
|
|
502
|
+
#hasCurrentTurnContext = false;
|
|
503
|
+
#reportedImageFailures = /* @__PURE__ */ new Set();
|
|
504
|
+
apply(events) {
|
|
505
|
+
const changed = [];
|
|
506
|
+
for (const event of events) {
|
|
507
|
+
if (event.type === "session.binary_asset" && event.data.type === "image") {
|
|
508
|
+
this.#binaryAssets.set(event.data.assetId, {
|
|
509
|
+
eventId: event.id,
|
|
510
|
+
timestamp: event.timestamp,
|
|
511
|
+
data: event.data
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
if (event.type === "tool.execution_start" && event.data.toolName === TASK_COMPLETE_TOOL_NAME) {
|
|
515
|
+
this.#taskCompletionToolCalls.set(
|
|
516
|
+
event.data.toolCallId,
|
|
517
|
+
event.agentId !== void 0 || event.data.parentToolCallId !== void 0 || this.#taskCompletionToolCalls.get(event.data.toolCallId) === "subagent" ? "subagent" : "assistant"
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (const event of events) {
|
|
522
|
+
if (event.type === "session.binary_asset") {
|
|
523
|
+
const pending = this.#pendingToolImages.get(event.data.assetId);
|
|
524
|
+
if (pending !== void 0) {
|
|
525
|
+
for (const projection of pending.values()) {
|
|
526
|
+
if (this.#unresolvedToolImageAssetIds(projection.event).length === 0) {
|
|
527
|
+
this.#removePendingToolImage(projection.event);
|
|
528
|
+
changed.push(
|
|
529
|
+
...this.#projectToolImages(
|
|
530
|
+
projection.event,
|
|
531
|
+
projection.turnId,
|
|
532
|
+
projection.toolName
|
|
533
|
+
)
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
const inputProvenance = event.type === "user.message" && event.agentId === void 0 && event.data.isAutopilotContinuation !== true ? this.resolveInputProvenance?.(event) : void 0;
|
|
541
|
+
if (event.type === "user.message" && (inputProvenance !== void 0 || isDisplayableHumanMessage(event))) {
|
|
542
|
+
const turnId = protocolId(event.data.interactionId ?? event.id);
|
|
543
|
+
const item = {
|
|
544
|
+
id: scopedId("message", event.id),
|
|
545
|
+
revision: 1,
|
|
546
|
+
turnId,
|
|
547
|
+
timestamp: event.timestamp,
|
|
548
|
+
...inputProvenance?.actor === "subagent" ? {
|
|
549
|
+
kind: "assistant-message",
|
|
550
|
+
actor: "subagent",
|
|
551
|
+
agentId: inputProvenance.agentId,
|
|
552
|
+
final: true,
|
|
553
|
+
sourceMessageIds: [event.id]
|
|
554
|
+
} : { kind: "human-message", actor: "human" },
|
|
555
|
+
sourceEventIds: [event.id],
|
|
556
|
+
...event.data.messageId === void 0 ? {} : { sourceMessageIds: [protocolId(event.data.messageId)] },
|
|
557
|
+
...event.data.interactionId === void 0 ? {} : { sourceInteractionIds: [protocolId(event.data.interactionId)] },
|
|
558
|
+
content: limitSummary(event.data.content, 1e5),
|
|
559
|
+
attachments: event.data.attachments?.slice(0, 100).map(projectAttachment).map(boundAttachment) ?? []
|
|
560
|
+
};
|
|
561
|
+
this.#currentTurnId = turnId;
|
|
562
|
+
this.#hasCurrentTurnContext = true;
|
|
563
|
+
this.#items.set(item.id, item);
|
|
564
|
+
changed.push(item);
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
if (event.type === "assistant.turn_start") {
|
|
568
|
+
if (event.agentId !== void 0) {
|
|
569
|
+
this.#loopTurns.set(event.data.turnId, {
|
|
570
|
+
turnId: protocolId(
|
|
571
|
+
this.#currentTurnId ?? event.data.interactionId ?? event.data.turnId
|
|
572
|
+
),
|
|
573
|
+
hasTurnContext: this.#currentTurnId === void 0 ? event.data.interactionId !== void 0 : this.#hasCurrentTurnContext
|
|
574
|
+
});
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
const turnId = protocolId(
|
|
578
|
+
event.data.interactionId ?? this.#currentTurnId ?? event.data.turnId
|
|
579
|
+
);
|
|
580
|
+
const hasTurnContext = event.data.interactionId !== void 0 || this.#hasCurrentTurnContext;
|
|
581
|
+
this.#loopTurns.set(event.data.turnId, { turnId, hasTurnContext });
|
|
582
|
+
this.#currentTurnId = turnId;
|
|
583
|
+
this.#hasCurrentTurnContext = hasTurnContext;
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
if (event.type === "skill.invoked") {
|
|
587
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
588
|
+
const step = {
|
|
589
|
+
id: scopedId("skill", event.id),
|
|
590
|
+
kind: "skill",
|
|
591
|
+
name: boundedText(event.data.name, 256, "Skill"),
|
|
592
|
+
...boundedOptionalText(event.data.description, 2048) === void 0 ? {} : {
|
|
593
|
+
description: boundedOptionalText(
|
|
594
|
+
event.data.description,
|
|
595
|
+
2048
|
|
596
|
+
)
|
|
597
|
+
},
|
|
598
|
+
status: "completed",
|
|
599
|
+
...boundedOptionalText(event.data.source, 256) === void 0 ? {} : { source: boundedOptionalText(event.data.source, 256) }
|
|
600
|
+
};
|
|
601
|
+
const item = this.#upsertActivityStep(
|
|
602
|
+
event.id,
|
|
603
|
+
event.timestamp,
|
|
604
|
+
turnId,
|
|
605
|
+
step
|
|
606
|
+
);
|
|
607
|
+
changed.push(item);
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
if (event.type === "tool.execution_start") {
|
|
611
|
+
const lifecycle = this.toolLifecycles.remember(event.data.toolCallId, {
|
|
612
|
+
sourceEventId: protocolId(event.id),
|
|
613
|
+
timestamp: event.timestamp,
|
|
614
|
+
turnId: this.#resolveTurnId(
|
|
615
|
+
event.data.turnId,
|
|
616
|
+
this.#currentTurnId ?? protocolId(event.id)
|
|
617
|
+
),
|
|
618
|
+
hasTurnContext: event.data.turnId === void 0 ? this.#hasCurrentTurnContext : this.#loopTurns.get(event.data.turnId)?.hasTurnContext ?? false,
|
|
619
|
+
name: boundedText(event.data.toolName, 256, "Tool"),
|
|
620
|
+
actor: event.agentId !== void 0 || event.data.parentToolCallId !== void 0 ? "subagent" : "assistant"
|
|
621
|
+
});
|
|
622
|
+
const turnId = lifecycle.turnId;
|
|
623
|
+
this.#toolTurns.set(event.data.toolCallId, turnId);
|
|
624
|
+
if (event.data.toolName === TASK_COMPLETE_TOOL_NAME) {
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
const step = {
|
|
628
|
+
id: scopedId("tool", event.data.toolCallId),
|
|
629
|
+
kind: "tool",
|
|
630
|
+
name: lifecycle.name,
|
|
631
|
+
status: "running"
|
|
632
|
+
};
|
|
633
|
+
changed.push(
|
|
634
|
+
this.#upsertActivityStep(
|
|
635
|
+
protocolId(event.id),
|
|
636
|
+
event.timestamp,
|
|
637
|
+
turnId,
|
|
638
|
+
step
|
|
639
|
+
)
|
|
640
|
+
);
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
if (event.type === "tool.execution_complete") {
|
|
644
|
+
const lifecycle = this.toolLifecycles.get(event.data.toolCallId);
|
|
645
|
+
const turnId = lifecycle?.turnId ?? this.#resolveTurnId(
|
|
646
|
+
event.data.turnId,
|
|
647
|
+
this.#toolTurns.get(event.data.toolCallId) ?? this.#currentTurnId ?? event.id
|
|
648
|
+
);
|
|
649
|
+
if (this.#taskCompletionToolCalls.has(event.data.toolCallId) || lifecycle?.name === TASK_COMPLETE_TOOL_NAME || event.data.toolDescription?.name === TASK_COMPLETE_TOOL_NAME) {
|
|
650
|
+
if (lifecycle !== void 0) {
|
|
651
|
+
this.#taskCompletionToolCalls.set(event.data.toolCallId, lifecycle.actor);
|
|
652
|
+
}
|
|
653
|
+
const taskCompletion = this.#projectTaskCompletion(event, turnId);
|
|
654
|
+
if (taskCompletion !== void 0) {
|
|
655
|
+
changed.push(taskCompletion);
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
const step2 = {
|
|
659
|
+
id: scopedId("tool", event.data.toolCallId),
|
|
660
|
+
kind: "tool",
|
|
661
|
+
name: TASK_COMPLETE_TOOL_NAME,
|
|
662
|
+
status: event.data.success ? "completed" : "failed"
|
|
663
|
+
};
|
|
664
|
+
changed.push(
|
|
665
|
+
this.#upsertActivityStep(
|
|
666
|
+
event.id,
|
|
667
|
+
event.timestamp,
|
|
668
|
+
turnId,
|
|
669
|
+
step2
|
|
670
|
+
)
|
|
671
|
+
);
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
const existingStep = this.#findActivityStep(
|
|
675
|
+
scopedId("tool", event.data.toolCallId)
|
|
676
|
+
);
|
|
677
|
+
const step = {
|
|
678
|
+
id: scopedId("tool", event.data.toolCallId),
|
|
679
|
+
kind: "tool",
|
|
680
|
+
name: lifecycle?.name ?? (existingStep?.kind === "tool" ? existingStep.name : boundedText(event.data.toolDescription?.name ?? "unknown-tool", 256, "Tool")),
|
|
681
|
+
status: event.data.success ? "completed" : "failed"
|
|
682
|
+
};
|
|
683
|
+
changed.push(
|
|
684
|
+
this.#upsertActivityStep(
|
|
685
|
+
event.id,
|
|
686
|
+
event.timestamp,
|
|
687
|
+
turnId,
|
|
688
|
+
step,
|
|
689
|
+
lifecycle
|
|
690
|
+
)
|
|
691
|
+
);
|
|
692
|
+
const toolName = lifecycle?.name ?? existingStep?.name ?? "Copilot tool";
|
|
693
|
+
const unresolvedAssetIds = this.#unresolvedToolImageAssetIds(event);
|
|
694
|
+
if (unresolvedAssetIds.length > 0) {
|
|
695
|
+
const projection = { event, turnId, toolName };
|
|
696
|
+
for (const assetId of unresolvedAssetIds) {
|
|
697
|
+
let projections = this.#pendingToolImages.get(assetId);
|
|
698
|
+
if (projections === void 0) {
|
|
699
|
+
projections = /* @__PURE__ */ new Map();
|
|
700
|
+
this.#pendingToolImages.set(assetId, projections);
|
|
701
|
+
}
|
|
702
|
+
projections.set(event.data.toolCallId, projection);
|
|
703
|
+
}
|
|
704
|
+
} else {
|
|
705
|
+
changed.push(
|
|
706
|
+
...this.#projectToolImages(event, turnId, toolName)
|
|
707
|
+
);
|
|
708
|
+
}
|
|
709
|
+
continue;
|
|
710
|
+
}
|
|
711
|
+
if (event.type === "subagent.started") {
|
|
712
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
713
|
+
const subagentId = event.agentId ?? event.data.toolCallId;
|
|
714
|
+
this.#subagentTurns.set(subagentId, turnId);
|
|
715
|
+
const step = {
|
|
716
|
+
id: scopedId("subagent", subagentId),
|
|
717
|
+
kind: "subagent",
|
|
718
|
+
name: boundedText(event.data.agentDisplayName, 256, "Subagent"),
|
|
719
|
+
status: "running",
|
|
720
|
+
description: boundedText(
|
|
721
|
+
event.data.agentDescription,
|
|
722
|
+
2048,
|
|
723
|
+
"Subagent task"
|
|
724
|
+
),
|
|
725
|
+
...boundedOptionalText(event.data.model, 256) === void 0 ? {} : { model: boundedOptionalText(event.data.model, 256) }
|
|
726
|
+
};
|
|
727
|
+
changed.push(
|
|
728
|
+
this.#upsertActivityStep(
|
|
729
|
+
event.id,
|
|
730
|
+
event.timestamp,
|
|
731
|
+
turnId,
|
|
732
|
+
step
|
|
733
|
+
)
|
|
734
|
+
);
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
if (event.type === "subagent.completed") {
|
|
738
|
+
const subagentId = event.agentId ?? event.data.toolCallId;
|
|
739
|
+
const turnId = this.#subagentTurns.get(subagentId) ?? this.#currentTurnId ?? protocolId(event.id);
|
|
740
|
+
const existingStep = this.#findActivityStep(
|
|
741
|
+
scopedId("subagent", subagentId)
|
|
742
|
+
);
|
|
743
|
+
const step = {
|
|
744
|
+
id: scopedId("subagent", subagentId),
|
|
745
|
+
kind: "subagent",
|
|
746
|
+
name: boundedText(event.data.agentDisplayName, 256, "Subagent"),
|
|
747
|
+
status: "completed",
|
|
748
|
+
...existingStep?.kind === "subagent" && existingStep.description !== void 0 ? { description: existingStep.description } : {},
|
|
749
|
+
...event.data.model === void 0 ? existingStep?.kind === "subagent" && existingStep.model !== void 0 ? { model: existingStep.model } : {} : {
|
|
750
|
+
model: boundedText(event.data.model, 256, "unknown-model")
|
|
751
|
+
},
|
|
752
|
+
...event.data.durationMs === void 0 ? {} : { durationMs: event.data.durationMs }
|
|
753
|
+
};
|
|
754
|
+
changed.push(
|
|
755
|
+
this.#upsertActivityStep(
|
|
756
|
+
event.id,
|
|
757
|
+
event.timestamp,
|
|
758
|
+
turnId,
|
|
759
|
+
step
|
|
760
|
+
)
|
|
761
|
+
);
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
if (event.type === "subagent.failed") {
|
|
765
|
+
const subagentId = event.agentId ?? event.data.toolCallId;
|
|
766
|
+
const turnId = this.#subagentTurns.get(subagentId) ?? this.#currentTurnId ?? protocolId(event.id);
|
|
767
|
+
const existingStep = this.#findActivityStep(
|
|
768
|
+
scopedId("subagent", subagentId)
|
|
769
|
+
);
|
|
770
|
+
const step = {
|
|
771
|
+
id: scopedId("subagent", subagentId),
|
|
772
|
+
kind: "subagent",
|
|
773
|
+
name: boundedText(event.data.agentDisplayName, 256, "Subagent"),
|
|
774
|
+
status: "failed",
|
|
775
|
+
...existingStep?.kind === "subagent" && existingStep.description !== void 0 ? { description: existingStep.description } : {},
|
|
776
|
+
...event.data.model === void 0 ? existingStep?.kind === "subagent" && existingStep.model !== void 0 ? { model: existingStep.model } : {} : {
|
|
777
|
+
model: boundedText(event.data.model, 256, "unknown-model")
|
|
778
|
+
},
|
|
779
|
+
...event.data.durationMs === void 0 ? {} : { durationMs: event.data.durationMs },
|
|
780
|
+
error: summarizeUnknown(event.data.error)
|
|
781
|
+
};
|
|
782
|
+
changed.push(
|
|
783
|
+
this.#upsertActivityStep(
|
|
784
|
+
event.id,
|
|
785
|
+
event.timestamp,
|
|
786
|
+
turnId,
|
|
787
|
+
step
|
|
788
|
+
)
|
|
789
|
+
);
|
|
790
|
+
continue;
|
|
791
|
+
}
|
|
792
|
+
if (event.type === "permission.requested") {
|
|
793
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
794
|
+
const request = event.data.permissionRequest;
|
|
795
|
+
this.#permissionTurns.set(event.data.requestId, turnId);
|
|
796
|
+
const step = {
|
|
797
|
+
id: scopedId("permission", event.data.requestId),
|
|
798
|
+
kind: "permission",
|
|
799
|
+
name: permissionName(request.kind),
|
|
800
|
+
status: "running",
|
|
801
|
+
...request.kind === "shell" ? {
|
|
802
|
+
description: boundedText(
|
|
803
|
+
request.intention,
|
|
804
|
+
2048,
|
|
805
|
+
"Shell command"
|
|
806
|
+
)
|
|
807
|
+
} : {}
|
|
808
|
+
};
|
|
809
|
+
changed.push(
|
|
810
|
+
this.#upsertActivityStep(
|
|
811
|
+
event.id,
|
|
812
|
+
event.timestamp,
|
|
813
|
+
turnId,
|
|
814
|
+
step
|
|
815
|
+
)
|
|
816
|
+
);
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
if (event.type === "permission.completed") {
|
|
820
|
+
const turnId = this.#permissionTurns.get(event.data.requestId) ?? this.#currentTurnId ?? protocolId(event.id);
|
|
821
|
+
const stepId = scopedId("permission", event.data.requestId);
|
|
822
|
+
const existingStep = this.#findActivityStep(stepId);
|
|
823
|
+
const result = event.data.result.kind;
|
|
824
|
+
const failed = result === "cancelled" || result.startsWith("denied");
|
|
825
|
+
const step = {
|
|
826
|
+
id: stepId,
|
|
827
|
+
kind: "permission",
|
|
828
|
+
name: existingStep?.kind === "permission" ? existingStep.name : "Permission",
|
|
829
|
+
status: failed ? "failed" : "completed",
|
|
830
|
+
...existingStep?.kind === "permission" && existingStep.description !== void 0 ? { description: existingStep.description } : {},
|
|
831
|
+
resultSummary: result
|
|
832
|
+
};
|
|
833
|
+
changed.push(
|
|
834
|
+
this.#upsertActivityStep(
|
|
835
|
+
event.id,
|
|
836
|
+
event.timestamp,
|
|
837
|
+
turnId,
|
|
838
|
+
step
|
|
839
|
+
)
|
|
840
|
+
);
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
if (event.type === "user_input.requested") {
|
|
844
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
845
|
+
this.#permissionTurns.set(event.data.requestId, turnId);
|
|
846
|
+
const step = {
|
|
847
|
+
id: scopedId("permission", event.data.requestId),
|
|
848
|
+
kind: "permission",
|
|
849
|
+
name: "User input",
|
|
850
|
+
status: "running",
|
|
851
|
+
description: boundedText(event.data.question, 2048, "User input")
|
|
852
|
+
};
|
|
853
|
+
changed.push(
|
|
854
|
+
this.#upsertActivityStep(
|
|
855
|
+
event.id,
|
|
856
|
+
event.timestamp,
|
|
857
|
+
turnId,
|
|
858
|
+
step
|
|
859
|
+
)
|
|
860
|
+
);
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
if (event.type === "user_input.completed") {
|
|
864
|
+
const turnId = this.#permissionTurns.get(event.data.requestId) ?? this.#currentTurnId ?? protocolId(event.id);
|
|
865
|
+
const stepId = scopedId("permission", event.data.requestId);
|
|
866
|
+
const existingStep = this.#findActivityStep(stepId);
|
|
867
|
+
const step = {
|
|
868
|
+
id: stepId,
|
|
869
|
+
kind: "permission",
|
|
870
|
+
name: "User input",
|
|
871
|
+
status: "completed",
|
|
872
|
+
...existingStep?.kind === "permission" && existingStep.description !== void 0 ? { description: existingStep.description } : {},
|
|
873
|
+
resultSummary: event.data.answer === void 0 ? "completed" : boundedText(event.data.answer, 4096, "completed")
|
|
874
|
+
};
|
|
875
|
+
changed.push(
|
|
876
|
+
this.#upsertActivityStep(
|
|
877
|
+
event.id,
|
|
878
|
+
event.timestamp,
|
|
879
|
+
turnId,
|
|
880
|
+
step
|
|
881
|
+
)
|
|
882
|
+
);
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
if (event.type === "session.warning") {
|
|
886
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
887
|
+
const warningType = event.data.warningType.replaceAll("_", " ");
|
|
888
|
+
const warningLabel = boundedText(warningType, 240, "Session");
|
|
889
|
+
const item = {
|
|
890
|
+
id: scopedId("notice", event.id),
|
|
891
|
+
kind: "notice",
|
|
892
|
+
revision: 1,
|
|
893
|
+
turnId,
|
|
894
|
+
timestamp: event.timestamp,
|
|
895
|
+
actor: event.agentId === void 0 ? "system" : "subagent",
|
|
896
|
+
sourceEventIds: [event.id],
|
|
897
|
+
severity: "warning",
|
|
898
|
+
title: limitSummary(
|
|
899
|
+
`${warningLabel.charAt(0).toUpperCase()}${warningLabel.slice(1)} warning`,
|
|
900
|
+
256
|
|
901
|
+
),
|
|
902
|
+
content: boundedText(event.data.message, 4096, "Session warning")
|
|
903
|
+
};
|
|
904
|
+
this.#items.set(item.id, item);
|
|
905
|
+
changed.push(item);
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
if (event.type === "session.error") {
|
|
909
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
910
|
+
const errorType = event.data.errorType.replaceAll("_", " ");
|
|
911
|
+
const errorLabel = boundedText(errorType, 240, "Session");
|
|
912
|
+
const title = `${errorLabel.charAt(0).toUpperCase()}${errorLabel.slice(1)} error`;
|
|
913
|
+
const item = {
|
|
914
|
+
id: scopedId("notice", event.id),
|
|
915
|
+
kind: "notice",
|
|
916
|
+
revision: 1,
|
|
917
|
+
turnId,
|
|
918
|
+
timestamp: event.timestamp,
|
|
919
|
+
actor: event.agentId === void 0 ? "system" : "subagent",
|
|
920
|
+
sourceEventIds: [event.id],
|
|
921
|
+
severity: "error",
|
|
922
|
+
title: limitSummary(
|
|
923
|
+
event.data.errorCode === void 0 ? title : `${title} \xB7 ${event.data.errorCode}`,
|
|
924
|
+
256
|
|
925
|
+
),
|
|
926
|
+
content: boundedText(event.data.message, 4096, "Session error")
|
|
927
|
+
};
|
|
928
|
+
this.#items.set(item.id, item);
|
|
929
|
+
changed.push(item);
|
|
930
|
+
continue;
|
|
931
|
+
}
|
|
932
|
+
if (event.type === "system.notification" && event.data.kind.type === "shell_completed") {
|
|
933
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
934
|
+
const kind = event.data.kind;
|
|
935
|
+
const description = boundedText(
|
|
936
|
+
kind.description ?? `Shell ${kind.shellId}`,
|
|
937
|
+
4096,
|
|
938
|
+
"Shell completed"
|
|
939
|
+
);
|
|
940
|
+
const item = {
|
|
941
|
+
id: scopedId("notice", event.id),
|
|
942
|
+
kind: "notice",
|
|
943
|
+
revision: 1,
|
|
944
|
+
turnId,
|
|
945
|
+
timestamp: event.timestamp,
|
|
946
|
+
actor: event.agentId === void 0 ? "system" : "subagent",
|
|
947
|
+
sourceEventIds: [event.id],
|
|
948
|
+
severity: kind.exitCode === void 0 || kind.exitCode === 0 ? "success" : "error",
|
|
949
|
+
title: "Shell completed",
|
|
950
|
+
content: kind.exitCode === void 0 ? description : limitSummary(`${description} \xB7 exit ${kind.exitCode}`, 4096)
|
|
951
|
+
};
|
|
952
|
+
this.#items.set(item.id, item);
|
|
953
|
+
changed.push(item);
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
if (event.type === "session.plan_changed") {
|
|
957
|
+
const turnId = this.#currentTurnId ?? protocolId(event.id);
|
|
958
|
+
const id = scopedId("plan", turnId);
|
|
959
|
+
const existing = this.#items.get(id);
|
|
960
|
+
const operation = event.data.operation === "create" ? "created" : event.data.operation === "update" ? "updated" : "deleted";
|
|
961
|
+
const item = {
|
|
962
|
+
id,
|
|
963
|
+
kind: "plan-update",
|
|
964
|
+
revision: eventRevision(
|
|
965
|
+
event.timestamp,
|
|
966
|
+
existing?.kind === "plan-update" ? existing.revision : void 0
|
|
967
|
+
),
|
|
968
|
+
turnId,
|
|
969
|
+
timestamp: existing?.kind === "plan-update" ? existing.timestamp : event.timestamp,
|
|
970
|
+
actor: event.agentId === void 0 ? "assistant" : "subagent",
|
|
971
|
+
sourceEventIds: existing?.kind === "plan-update" ? appendSourceEventId(existing.sourceEventIds, event.id) : [event.id],
|
|
972
|
+
operation,
|
|
973
|
+
content: `Plan ${operation}`,
|
|
974
|
+
todos: []
|
|
975
|
+
};
|
|
976
|
+
this.#items.set(id, item);
|
|
977
|
+
changed.push(item);
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (event.type === "assistant.message") {
|
|
981
|
+
if (event.agentId !== void 0 || event.data.parentToolCallId !== void 0) {
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
if (event.data.content.trim().length === 0 && (event.data.citations?.sources.length ?? 0) === 0) {
|
|
985
|
+
continue;
|
|
986
|
+
}
|
|
987
|
+
const turnId = protocolId(
|
|
988
|
+
event.data.interactionId ?? (event.data.turnId === void 0 ? this.#currentTurnId : this.#loopTurns.get(event.data.turnId)?.turnId) ?? event.data.turnId ?? event.id
|
|
989
|
+
);
|
|
990
|
+
const id = assistantMessageId(event.data);
|
|
991
|
+
const existing = this.#items.get(id);
|
|
992
|
+
const citationAttachments = event.data.citations?.sources.slice(0, 100).map(projectCitation).map(boundAttachment) ?? [];
|
|
993
|
+
const attachments = [
|
|
994
|
+
...existing?.kind === "assistant-message" ? existing.attachments ?? [] : [],
|
|
995
|
+
...citationAttachments
|
|
996
|
+
].filter(
|
|
997
|
+
(attachment, index, all) => all.findIndex(
|
|
998
|
+
(candidate) => candidate.kind === attachment.kind && candidate.name === attachment.name && candidate.uri === attachment.uri
|
|
999
|
+
) === index
|
|
1000
|
+
).slice(-100);
|
|
1001
|
+
const sourceMessageIds = appendOptionalSourceId(
|
|
1002
|
+
existing?.kind === "assistant-message" ? existing.sourceMessageIds : void 0,
|
|
1003
|
+
protocolId(event.data.messageId)
|
|
1004
|
+
);
|
|
1005
|
+
const sourceInteractionIds = appendOptionalSourceId(
|
|
1006
|
+
existing?.kind === "assistant-message" ? existing.sourceInteractionIds : void 0,
|
|
1007
|
+
event.data.interactionId === void 0 ? void 0 : protocolId(event.data.interactionId)
|
|
1008
|
+
);
|
|
1009
|
+
const item = {
|
|
1010
|
+
id,
|
|
1011
|
+
kind: "assistant-message",
|
|
1012
|
+
revision: eventRevision(
|
|
1013
|
+
event.timestamp,
|
|
1014
|
+
existing?.kind === "assistant-message" ? existing.revision : void 0
|
|
1015
|
+
),
|
|
1016
|
+
turnId,
|
|
1017
|
+
timestamp: existing?.kind === "assistant-message" ? existing.timestamp : event.timestamp,
|
|
1018
|
+
actor: "assistant",
|
|
1019
|
+
sourceEventIds: existing?.kind === "assistant-message" ? appendSourceEventId(existing.sourceEventIds, event.id) : [event.id],
|
|
1020
|
+
...sourceMessageIds === void 0 ? {} : { sourceMessageIds },
|
|
1021
|
+
...sourceInteractionIds === void 0 ? {} : { sourceInteractionIds },
|
|
1022
|
+
content: appendAssistantContent(
|
|
1023
|
+
existing?.kind === "assistant-message" ? existing.content : "",
|
|
1024
|
+
event.data.content
|
|
1025
|
+
),
|
|
1026
|
+
...attachments.length === 0 ? {} : { attachments },
|
|
1027
|
+
...event.data.model === void 0 ? existing?.kind === "assistant-message" && existing.model !== void 0 ? { model: existing.model } : {} : {
|
|
1028
|
+
model: boundedText(event.data.model, 256, "unknown-model")
|
|
1029
|
+
},
|
|
1030
|
+
final: event.data.chunkCount === void 0 || event.data.chunkIndex === void 0 || event.data.chunkIndex + 1 >= event.data.chunkCount
|
|
1031
|
+
};
|
|
1032
|
+
this.#currentTurnId = turnId;
|
|
1033
|
+
this.#hasCurrentTurnContext = event.data.interactionId !== void 0 || (event.data.turnId === void 0 ? this.#hasCurrentTurnContext : this.#loopTurns.get(event.data.turnId)?.hasTurnContext ?? false);
|
|
1034
|
+
this.#items.set(id, item);
|
|
1035
|
+
changed.push(item);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
return changed;
|
|
1039
|
+
}
|
|
1040
|
+
snapshot() {
|
|
1041
|
+
return [...this.#items.values()];
|
|
1042
|
+
}
|
|
1043
|
+
pendingImageAssetIds() {
|
|
1044
|
+
return [...this.#pendingToolImages.keys()];
|
|
1045
|
+
}
|
|
1046
|
+
#projectTaskCompletion(event, turnId) {
|
|
1047
|
+
if (!event.data.success) {
|
|
1048
|
+
return void 0;
|
|
1049
|
+
}
|
|
1050
|
+
const content = boundedOptionalMessageContent(
|
|
1051
|
+
event.data.result?.content,
|
|
1052
|
+
4096
|
|
1053
|
+
);
|
|
1054
|
+
if (content === void 0) {
|
|
1055
|
+
return void 0;
|
|
1056
|
+
}
|
|
1057
|
+
const id = scopedId("notice", event.data.toolCallId);
|
|
1058
|
+
const existing = this.#items.get(id);
|
|
1059
|
+
const item = {
|
|
1060
|
+
id,
|
|
1061
|
+
kind: "notice",
|
|
1062
|
+
revision: eventRevision(
|
|
1063
|
+
event.timestamp,
|
|
1064
|
+
existing?.kind === "notice" ? existing.revision : void 0
|
|
1065
|
+
),
|
|
1066
|
+
turnId,
|
|
1067
|
+
timestamp: existing?.kind === "notice" ? existing.timestamp : event.timestamp,
|
|
1068
|
+
actor: event.agentId !== void 0 || event.data.parentToolCallId !== void 0 || this.#taskCompletionToolCalls.get(event.data.toolCallId) === "subagent" || existing?.kind === "notice" && existing.actor === "subagent" ? "subagent" : "assistant",
|
|
1069
|
+
sourceEventIds: existing?.kind === "notice" ? appendSourceEventId(existing.sourceEventIds, event.id) : [event.id],
|
|
1070
|
+
severity: "success",
|
|
1071
|
+
title: "Task completed",
|
|
1072
|
+
content
|
|
1073
|
+
};
|
|
1074
|
+
this.#items.set(id, item);
|
|
1075
|
+
return item;
|
|
1076
|
+
}
|
|
1077
|
+
#unresolvedToolImageAssetIds(event) {
|
|
1078
|
+
return (event.data.result?.binaryResultsForLlm?.filter(
|
|
1079
|
+
(result) => result.type === "image" && "assetId" in result
|
|
1080
|
+
).map((result) => result.assetId).filter((assetId) => !this.#binaryAssets.has(assetId)) ?? []).filter((assetId, index, all) => all.indexOf(assetId) === index);
|
|
1081
|
+
}
|
|
1082
|
+
#removePendingToolImage(event) {
|
|
1083
|
+
for (const [assetId, projections] of this.#pendingToolImages) {
|
|
1084
|
+
projections.delete(event.data.toolCallId);
|
|
1085
|
+
if (projections.size === 0) {
|
|
1086
|
+
this.#pendingToolImages.delete(assetId);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
#projectToolImages(event, turnId, toolName) {
|
|
1091
|
+
const binaryImages = event.data.result?.binaryResultsForLlm?.map(
|
|
1092
|
+
(result, index) => projectToolBinaryResult(
|
|
1093
|
+
result,
|
|
1094
|
+
`${toolName} image ${index + 1}`,
|
|
1095
|
+
this.#binaryAssets
|
|
1096
|
+
)
|
|
1097
|
+
).filter(
|
|
1098
|
+
(image) => image !== void 0
|
|
1099
|
+
) ?? [];
|
|
1100
|
+
const contentImages = event.data.result?.contents?.filter(
|
|
1101
|
+
(content) => content.type === "image"
|
|
1102
|
+
).map(
|
|
1103
|
+
(image, index) => projectToolContentImage(
|
|
1104
|
+
image,
|
|
1105
|
+
`${toolName} image ${binaryImages.length + index + 1}`
|
|
1106
|
+
)
|
|
1107
|
+
) ?? [];
|
|
1108
|
+
const projectedImages = [...binaryImages, ...contentImages].filter(
|
|
1109
|
+
(image, index, all) => all.findIndex(
|
|
1110
|
+
(candidate) => [
|
|
1111
|
+
candidate.attachment.mimeType,
|
|
1112
|
+
candidate.attachment.sha256 ?? candidate.attachment.data ?? candidate.attachment.name
|
|
1113
|
+
].join("\0") === [
|
|
1114
|
+
image.attachment.mimeType,
|
|
1115
|
+
image.attachment.sha256 ?? image.attachment.data ?? image.attachment.name
|
|
1116
|
+
].join("\0")
|
|
1117
|
+
) === index
|
|
1118
|
+
);
|
|
1119
|
+
for (const image of projectedImages) {
|
|
1120
|
+
if (image.failure === void 0) {
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
const fingerprint = [
|
|
1124
|
+
event.data.toolCallId,
|
|
1125
|
+
image.failure.source,
|
|
1126
|
+
image.failure.reason,
|
|
1127
|
+
image.failure.mimeType,
|
|
1128
|
+
image.failure.expectedByteLength,
|
|
1129
|
+
image.failure.actualByteLength
|
|
1130
|
+
].join("\0");
|
|
1131
|
+
if (this.#reportedImageFailures.has(fingerprint)) {
|
|
1132
|
+
continue;
|
|
1133
|
+
}
|
|
1134
|
+
this.#reportedImageFailures.add(fingerprint);
|
|
1135
|
+
this.onImageFailure?.({
|
|
1136
|
+
...image.failure,
|
|
1137
|
+
toolCallId: event.data.toolCallId,
|
|
1138
|
+
toolName
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
const imageGroups = splitToolImages(projectedImages);
|
|
1142
|
+
return imageGroups.map((imageGroup, groupIndex) => {
|
|
1143
|
+
const multipleGroups = imageGroups.length > 1;
|
|
1144
|
+
const id = scopedId(
|
|
1145
|
+
"tool-image",
|
|
1146
|
+
multipleGroups ? `${event.data.toolCallId}:${groupIndex + 1}` : event.data.toolCallId
|
|
1147
|
+
);
|
|
1148
|
+
const existing = this.#items.get(id);
|
|
1149
|
+
const imageItem = {
|
|
1150
|
+
id,
|
|
1151
|
+
kind: "assistant-message",
|
|
1152
|
+
// The same source events must produce the same revision in live and batched history reads.
|
|
1153
|
+
revision: Math.max(
|
|
1154
|
+
eventRevision(event.timestamp, existing?.kind === "assistant-message" ? existing.revision : void 0),
|
|
1155
|
+
...imageGroup.flatMap((image) => image.sourceTimestamp === void 0 ? [] : [eventRevision(image.sourceTimestamp)])
|
|
1156
|
+
),
|
|
1157
|
+
turnId,
|
|
1158
|
+
timestamp: existing?.kind === "assistant-message" ? existing.timestamp : event.timestamp,
|
|
1159
|
+
actor: event.agentId !== void 0 || event.data.parentToolCallId !== void 0 || this.toolLifecycles.get(event.data.toolCallId)?.actor === "subagent" ? "subagent" : "assistant",
|
|
1160
|
+
sourceEventIds: [
|
|
1161
|
+
...imageGroup.flatMap(
|
|
1162
|
+
(image) => image.sourceEventId === void 0 ? [] : [image.sourceEventId]
|
|
1163
|
+
),
|
|
1164
|
+
event.id
|
|
1165
|
+
].filter(
|
|
1166
|
+
(sourceEventId, index, all) => all.indexOf(sourceEventId) === index
|
|
1167
|
+
),
|
|
1168
|
+
content: "",
|
|
1169
|
+
attachments: imageGroup.map((image) => image.attachment),
|
|
1170
|
+
...event.agentId === void 0 ? {} : { agentId: protocolId(event.agentId) },
|
|
1171
|
+
final: true
|
|
1172
|
+
};
|
|
1173
|
+
this.#items.set(imageItem.id, imageItem);
|
|
1174
|
+
return imageItem;
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
#resolveTurnId(loopTurnId, fallback) {
|
|
1178
|
+
return protocolId(
|
|
1179
|
+
(loopTurnId === void 0 ? void 0 : this.#loopTurns.get(loopTurnId)?.turnId) ?? loopTurnId ?? fallback
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
#findActivityStep(stepId) {
|
|
1183
|
+
const activity = this.#items.get(scopedId("activity", stepId));
|
|
1184
|
+
return activity?.kind === "activity-group" ? activity.steps[0] : void 0;
|
|
1185
|
+
}
|
|
1186
|
+
#upsertActivityStep(eventId, timestamp, turnId, step, lifecycle) {
|
|
1187
|
+
const id = scopedId("activity", step.id);
|
|
1188
|
+
const existing = this.#items.get(id);
|
|
1189
|
+
const existingGroup = existing?.kind === "activity-group" ? existing : void 0;
|
|
1190
|
+
const item = {
|
|
1191
|
+
id,
|
|
1192
|
+
kind: "activity-group",
|
|
1193
|
+
revision: eventRevision(
|
|
1194
|
+
timestamp,
|
|
1195
|
+
existingGroup?.revision ?? (lifecycle === void 0 ? void 0 : Date.parse(lifecycle.timestamp))
|
|
1196
|
+
),
|
|
1197
|
+
turnId,
|
|
1198
|
+
timestamp: lifecycle?.timestamp ?? existingGroup?.timestamp ?? timestamp,
|
|
1199
|
+
actor: "assistant",
|
|
1200
|
+
sourceEventIds: existingGroup === void 0 ? lifecycle === void 0 ? [eventId] : appendSourceEventId([lifecycle.sourceEventId], eventId) : appendSourceEventId(existingGroup.sourceEventIds, eventId),
|
|
1201
|
+
status: step.status,
|
|
1202
|
+
steps: [{
|
|
1203
|
+
...step,
|
|
1204
|
+
timestamp: lifecycle?.timestamp ?? existingGroup?.steps[0]?.timestamp ?? timestamp
|
|
1205
|
+
}]
|
|
1206
|
+
};
|
|
1207
|
+
this.#items.set(id, item);
|
|
1208
|
+
return item;
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
|
|
1212
|
+
export {
|
|
1213
|
+
assistantMessageId,
|
|
1214
|
+
isDisplayableHumanMessage,
|
|
1215
|
+
ConversationToolLifecycles,
|
|
1216
|
+
ConversationNormalizer
|
|
1217
|
+
};
|