@compose-market/core 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.
@@ -1,1257 +0,0 @@
1
- const EOL = /\r\n|\r|\n/;
2
- function lines(buffer) {
3
- const parts = buffer.split(EOL);
4
- const rest = parts.pop() ?? "";
5
- return { lines: parts, rest };
6
- }
7
- export async function* parseSSEStream(stream, options = {}) {
8
- const reader = stream.getReader();
9
- const decoder = new TextDecoder("utf-8");
10
- let buffer = "";
11
- let event = "message";
12
- let data = [];
13
- let id;
14
- const abort = () => {
15
- try {
16
- reader.cancel();
17
- }
18
- catch { /* best effort */ }
19
- };
20
- options.signal?.addEventListener("abort", abort);
21
- try {
22
- while (true) {
23
- const { value, done } = await reader.read();
24
- if (done)
25
- break;
26
- buffer += decoder.decode(value, { stream: true });
27
- const split = lines(buffer);
28
- buffer = split.rest;
29
- for (const line of split.lines) {
30
- if (line === "") {
31
- if (data.length > 0) {
32
- yield { event, data: data.join("\n"), ...(id ? { id } : {}) };
33
- }
34
- event = "message";
35
- data = [];
36
- id = undefined;
37
- continue;
38
- }
39
- if (line.startsWith(":"))
40
- continue;
41
- const colon = line.indexOf(":");
42
- const field = colon === -1 ? line : line.slice(0, colon);
43
- let value = colon === -1 ? "" : line.slice(colon + 1);
44
- if (value.startsWith(" "))
45
- value = value.slice(1);
46
- if (field === "event")
47
- event = value || "message";
48
- else if (field === "data")
49
- data.push(value);
50
- else if (field === "id")
51
- id = value;
52
- }
53
- }
54
- buffer += decoder.decode();
55
- if (buffer.length > 0) {
56
- const split = lines(`${buffer}\n`);
57
- for (const line of split.lines) {
58
- if (line === "")
59
- break;
60
- if (line.startsWith(":"))
61
- continue;
62
- const colon = line.indexOf(":");
63
- const field = colon === -1 ? line : line.slice(0, colon);
64
- let value = colon === -1 ? "" : line.slice(colon + 1);
65
- if (value.startsWith(" "))
66
- value = value.slice(1);
67
- if (field === "event")
68
- event = value || "message";
69
- else if (field === "data")
70
- data.push(value);
71
- else if (field === "id")
72
- id = value;
73
- }
74
- }
75
- if (data.length > 0) {
76
- yield { event, data: data.join("\n"), ...(id ? { id } : {}) };
77
- }
78
- }
79
- finally {
80
- options.signal?.removeEventListener("abort", abort);
81
- try {
82
- reader.releaseLock();
83
- }
84
- catch { /* best effort */ }
85
- }
86
- }
87
- export function encode(event) {
88
- return `data: ${JSON.stringify(event)}\n\n`;
89
- }
90
- export function createStreamTree() {
91
- return {
92
- roots: [],
93
- nodes: {},
94
- seen: {},
95
- text: "",
96
- reasoning: "",
97
- artifacts: [],
98
- receipts: [],
99
- errors: [],
100
- };
101
- }
102
- export function reduce(tree, event) {
103
- const next = {
104
- roots: tree.roots.slice(),
105
- nodes: { ...tree.nodes },
106
- seen: { ...tree.seen },
107
- text: tree.text,
108
- reasoning: tree.reasoning,
109
- artifacts: tree.artifacts.slice(),
110
- receipts: tree.receipts.slice(),
111
- errors: tree.errors.slice(),
112
- };
113
- if (!event.delta) {
114
- const serial = stable(event);
115
- if (next.seen[event.id] !== undefined && next.seen[event.id] === serial) {
116
- return next;
117
- }
118
- next.seen[event.id] = serial;
119
- }
120
- const current = next.nodes[event.id];
121
- const previousParentId = current?.parentId;
122
- const node = {
123
- id: event.id,
124
- kind: event.kind,
125
- source: event.source,
126
- ...(event.parentId ? { parentId: event.parentId } : current?.parentId ? { parentId: current.parentId } : {}),
127
- ...(event.rootId ? { rootId: event.rootId } : current?.rootId ? { rootId: current.rootId } : {}),
128
- ...(event.runId ? { runId: event.runId } : current?.runId ? { runId: current.runId } : {}),
129
- status: event.status ?? current?.status ?? "running",
130
- path: event.path ?? current?.path ?? [],
131
- display: { ...(current?.display ?? {}), ...(event.display ?? {}) },
132
- payload: merge(current?.payload, event.payload),
133
- ...(event.raw !== undefined ? { raw: event.raw } : current?.raw !== undefined ? { raw: current.raw } : {}),
134
- text: current?.text ?? "",
135
- children: current?.children.slice() ?? [],
136
- updatedAt: event.ts ?? Date.now(),
137
- events: (current?.events ?? 0) + 1,
138
- };
139
- if (event.delta) {
140
- node.text += event.delta;
141
- if (event.kind === "text")
142
- next.text += event.delta;
143
- if (event.kind === "reasoning")
144
- next.reasoning += event.delta;
145
- }
146
- next.nodes[event.id] = node;
147
- if (previousParentId && previousParentId !== node.parentId) {
148
- const previousParent = next.nodes[previousParentId];
149
- if (previousParent) {
150
- next.nodes[previousParentId] = {
151
- ...previousParent,
152
- children: previousParent.children.filter((idValue) => idValue !== node.id),
153
- };
154
- }
155
- }
156
- if (node.parentId) {
157
- const parent = next.nodes[node.parentId] ?? placeholder(node.parentId, event);
158
- if (!parent.children.includes(node.id))
159
- parent.children.push(node.id);
160
- next.nodes[parent.id] = parent;
161
- next.roots = next.roots.filter((idValue) => idValue !== node.id);
162
- if (!parent.parentId && !next.roots.includes(parent.id)) {
163
- next.roots.push(parent.id);
164
- }
165
- }
166
- else if (!next.roots.includes(node.id)) {
167
- next.roots.push(node.id);
168
- }
169
- if (event.kind === "artifact" && !next.artifacts.some((item) => item.id === event.id)) {
170
- next.artifacts.push(event);
171
- }
172
- if ((event.kind === "receipt" || event.kind === "payment") && !next.receipts.some((item) => item.id === event.id)) {
173
- next.receipts.push(event);
174
- }
175
- if (event.kind === "error" && !next.errors.some((item) => item.id === event.id)) {
176
- next.errors.push(event);
177
- }
178
- return next;
179
- }
180
- export function decode(input, options = {}) {
181
- const raw = framePayload(input);
182
- if (raw === "[DONE]") {
183
- const source = options.source ?? "stream";
184
- const targetId = terminalId(options, source);
185
- return event({
186
- id: targetId,
187
- kind: terminalKind(targetId),
188
- source,
189
- status: "completed",
190
- display: { summary: "Done" },
191
- raw,
192
- }, options);
193
- }
194
- if (!raw)
195
- return null;
196
- if (isRecord(raw) && raw.type === "stream") {
197
- const decoded = raw;
198
- if (!decoded.id || !decoded.kind || !decoded.source)
199
- return null;
200
- return decoded;
201
- }
202
- if (typeof raw === "string") {
203
- return event({
204
- id: id("text", options.runId),
205
- kind: "text",
206
- source: options.source ?? "stream",
207
- status: "running",
208
- delta: raw,
209
- raw,
210
- }, options);
211
- }
212
- if (!isRecord(raw))
213
- return null;
214
- return decodeRecord(raw, options);
215
- }
216
- function decodeRecord(raw, options) {
217
- const type = string(raw.type) ?? string(raw.eventName) ?? string(raw.event) ?? "";
218
- const runId = string(raw.composeRunId) ?? string(raw.runId) ?? options.runId;
219
- const rootId = string(raw.rootComposeRunId) ?? options.rootId ?? runId;
220
- const source = options.source ?? sourceOf(type, raw);
221
- const base = { ...options, runId, rootId, source };
222
- if (type === "text-delta") {
223
- const delta = string(raw.delta) ?? string(raw.text) ?? string(raw.content) ?? "";
224
- return delta ? event({
225
- id: id("text", runId),
226
- kind: "text",
227
- source,
228
- status: "running",
229
- delta,
230
- display: { title: "Response" },
231
- raw,
232
- }, base) : null;
233
- }
234
- if (type === "reasoning-delta" || type === "reasoning_delta" || type === "thinking" || type === "thinking-delta" || type === "thinking_delta") {
235
- const delta = string(raw.delta) ?? string(raw.text) ?? string(raw.thinking) ?? string(raw.content) ?? "";
236
- return delta ? event({
237
- id: id("reasoning", runId),
238
- kind: "reasoning",
239
- source,
240
- status: "running",
241
- delta,
242
- display: { title: "Reasoning" },
243
- raw,
244
- }, base) : null;
245
- }
246
- if (type === "warning" || type === "response.warning") {
247
- const warning = isRecord(raw.warning) ? raw.warning : raw;
248
- return event({
249
- id: id("warning", string(warning.code), string(warning.message), runId),
250
- kind: "debug",
251
- source,
252
- status: "info",
253
- display: { title: "Warning", summary: string(warning.message) ?? string(raw.message) },
254
- payload: { warning },
255
- raw,
256
- }, base);
257
- }
258
- if (type === "citation" || type === "response.citation") {
259
- const citation = isRecord(raw.citation) ? raw.citation : raw;
260
- return event({
261
- id: id("citation", string(citation.url), string(citation.title), string(citation.marker), runId),
262
- kind: "catalog",
263
- source,
264
- parentId: modelParent(base, string(raw.response_id)),
265
- status: "completed",
266
- display: {
267
- title: string(citation.title) ?? "Citation",
268
- summary: string(citation.snippet),
269
- target: string(citation.url) ?? string(citation.marker),
270
- },
271
- payload: { citation },
272
- raw,
273
- }, base);
274
- }
275
- if (type === "thinking-start" || type === "thinking_start" || type === "thinking-end" || type === "thinking_end") {
276
- return event({
277
- id: id("debug", type, runId),
278
- kind: "debug",
279
- source,
280
- status: type.endsWith("end") ? "completed" : "running",
281
- display: { title: "Runtime status", summary: string(raw.message) ?? type },
282
- payload: raw,
283
- raw,
284
- }, base);
285
- }
286
- if (type === "stopped") {
287
- return event({
288
- id: id("stopped", runId),
289
- kind: "run",
290
- source,
291
- status: "cancelled",
292
- display: { title: "Stopped", summary: string(raw.reason) },
293
- payload: raw,
294
- raw,
295
- }, base);
296
- }
297
- if (type === "tool-args-delta") {
298
- return event({
299
- id: id("tool", string(raw.id) ?? string(raw.toolName), runId),
300
- kind: "tool",
301
- source,
302
- status: "running",
303
- display: { title: string(raw.toolName) ?? "Tool" },
304
- payload: { arguments: string(raw.argsDelta) },
305
- raw,
306
- }, base);
307
- }
308
- if (type === "tool-start")
309
- raw.type = "tool_start";
310
- if (type === "tool-end")
311
- raw.type = "tool_end";
312
- if (Array.isArray(raw.choices)) {
313
- const choice = raw.choices[0];
314
- const delta = isRecord(choice) && isRecord(choice.delta) ? choice.delta : null;
315
- const reasoning = string(delta?.reasoning_content);
316
- if (reasoning) {
317
- return event({
318
- id: id("reasoning", runId),
319
- kind: "reasoning",
320
- source,
321
- parentId: modelParent(base, string(raw.id)),
322
- status: "running",
323
- delta: reasoning,
324
- display: { title: "Reasoning" },
325
- raw,
326
- }, base);
327
- }
328
- const text = string(delta?.content);
329
- if (text) {
330
- return event({
331
- id: id("text", runId),
332
- kind: "text",
333
- source,
334
- parentId: modelParent(base, string(raw.id)),
335
- status: "running",
336
- delta: text,
337
- display: { title: "Response" },
338
- raw,
339
- }, base);
340
- }
341
- if (raw.usage) {
342
- return event({
343
- id: id("model", string(raw.id) ?? runId),
344
- kind: "model",
345
- source,
346
- status: "completed",
347
- display: { title: string(raw.model) ?? "Model", summary: "Usage", target: string(raw.id) },
348
- payload: { usage: raw.usage },
349
- raw,
350
- }, base);
351
- }
352
- const finish = isRecord(choice) ? string(choice.finish_reason) : undefined;
353
- if (finish) {
354
- return event({
355
- id: id("model", string(raw.id) ?? runId),
356
- kind: "model",
357
- source,
358
- status: "completed",
359
- display: { title: string(raw.model) ?? "Model", summary: finish, target: string(raw.id) },
360
- payload: { finishReason: finish, usage: raw.usage },
361
- raw,
362
- }, base);
363
- }
364
- return null;
365
- }
366
- if (Array.isArray(raw.candidates)) {
367
- const candidate = isRecord(raw.candidates[0]) ? raw.candidates[0] : null;
368
- const content = isRecord(candidate?.content) ? candidate.content : {};
369
- const parts = Array.isArray(content.parts) ? content.parts : [];
370
- const part = parts.find(isRecord);
371
- if (part) {
372
- const text = string(part.text);
373
- if (text) {
374
- const reasoning = part.thought === true;
375
- return event({
376
- id: id(reasoning ? "reasoning" : "text", runId),
377
- kind: reasoning ? "reasoning" : "text",
378
- source,
379
- parentId: modelParent(base, string(raw.response_id)),
380
- status: "running",
381
- delta: text,
382
- display: { title: reasoning ? "Reasoning" : "Response" },
383
- raw,
384
- }, base);
385
- }
386
- const inline = isRecord(part.inlineData) ? part.inlineData : isRecord(part.inline_data) ? part.inline_data : null;
387
- const data = string(inline?.data);
388
- if (data) {
389
- return event({
390
- id: id("artifact", string(raw.response_id) ?? runId, number(raw.index), data),
391
- kind: "artifact",
392
- source,
393
- parentId: modelParent(base, string(raw.response_id)),
394
- status: "running",
395
- display: { title: "Media", target: string(raw.model) },
396
- payload: {
397
- artifactType: mediaKind(string(inline?.mimeType) ?? string(inline?.mime_type)),
398
- responseId: string(raw.response_id),
399
- mimeType: string(inline?.mimeType) ?? string(inline?.mime_type),
400
- inline: true,
401
- base64: data,
402
- },
403
- raw,
404
- }, base);
405
- }
406
- const call = isRecord(part.functionCall) ? part.functionCall : isRecord(part.function_call) ? part.function_call : null;
407
- if (call) {
408
- return event({
409
- id: id("tool", string(call.id) ?? string(call.name), runId),
410
- kind: "tool",
411
- source,
412
- parentId: modelParent(base, string(raw.response_id)),
413
- status: "completed",
414
- display: { title: string(call.name) ?? "Tool" },
415
- payload: {
416
- name: string(call.name),
417
- arguments: JSON.stringify(call.args ?? {}),
418
- },
419
- raw,
420
- }, base);
421
- }
422
- }
423
- if (raw.usageMetadata) {
424
- return event({
425
- id: id("model", string(raw.response_id) ?? runId),
426
- kind: "model",
427
- source,
428
- parentId: modelParent(base, string(raw.response_id)),
429
- status: "info",
430
- display: { title: string(raw.model) ?? "Model", summary: "Usage" },
431
- payload: { usage: raw.usageMetadata },
432
- raw,
433
- }, base);
434
- }
435
- }
436
- if (type === "response.created") {
437
- const response = isRecord(raw.response) ? raw.response : {};
438
- const responseId = string(response.id) ?? string(raw.response_id) ?? runId;
439
- return event({
440
- id: id("model", responseId),
441
- kind: "model",
442
- source,
443
- status: "running",
444
- display: { title: string(response.model) ?? string(raw.model) ?? "Model", target: responseId },
445
- payload: { response },
446
- raw,
447
- }, base);
448
- }
449
- if (type === "response.output_text.delta") {
450
- const delta = string(raw.delta) ?? "";
451
- return delta ? event({
452
- id: id("text", string(raw.response_id) ?? runId),
453
- kind: "text",
454
- source,
455
- parentId: modelParent(base, string(raw.response_id)),
456
- status: "running",
457
- delta,
458
- display: { title: "Response", target: string(raw.model) },
459
- raw,
460
- }, base) : null;
461
- }
462
- if (type === "response.reasoning.delta" || type === "response.reasoning_text.delta" || type === "response.reasoning_summary_text.delta") {
463
- const delta = string(raw.delta) ?? "";
464
- return delta ? event({
465
- id: id("reasoning", string(raw.response_id) ?? runId),
466
- kind: "reasoning",
467
- source,
468
- parentId: modelParent(base, string(raw.response_id)),
469
- status: "running",
470
- delta,
471
- display: { title: "Reasoning", target: string(raw.model) },
472
- raw,
473
- }, base) : null;
474
- }
475
- if (type === "content_block_start") {
476
- const block = isRecord(raw.content_block) ? raw.content_block : {};
477
- if (string(block.type) === "tool_use") {
478
- return event({
479
- id: id("tool", string(block.id), string(block.name), runId),
480
- kind: "tool",
481
- source,
482
- parentId: modelParent(base, string(raw.message_id)),
483
- status: "running",
484
- display: { title: string(block.name) ?? "Tool" },
485
- payload: { name: string(block.name), input: block.input, index: number(raw.index) },
486
- raw,
487
- }, base);
488
- }
489
- }
490
- if (type === "message_delta") {
491
- const delta = isRecord(raw.delta) ? raw.delta : {};
492
- return event({
493
- id: id("model", string(raw.message_id) ?? runId),
494
- kind: "model",
495
- source,
496
- status: "info",
497
- display: { title: string(raw.model) ?? "Model", summary: string(delta.stop_reason) },
498
- payload: { usage: raw.usage, stopReason: string(delta.stop_reason) },
499
- raw,
500
- }, base);
501
- }
502
- if (type === "content_block_delta") {
503
- const delta = isRecord(raw.delta) ? raw.delta : {};
504
- const deltaType = string(delta.type);
505
- const text = string(delta.text) ?? string(delta.thinking);
506
- if (!text)
507
- return null;
508
- const reasoning = deltaType === "thinking_delta" || string(delta.thinking) !== undefined;
509
- return event({
510
- id: id(reasoning ? "reasoning" : "text", string(raw.message_id) ?? runId),
511
- kind: reasoning ? "reasoning" : "text",
512
- source,
513
- parentId: modelParent(base, string(raw.message_id)),
514
- status: "running",
515
- delta: text,
516
- display: { title: reasoning ? "Reasoning" : "Response" },
517
- raw,
518
- }, base);
519
- }
520
- if (type === "response.audio.delta" || type === "response.output_audio.delta") {
521
- const delta = string(raw.delta) ?? "";
522
- return event({
523
- id: id("audio", string(raw.response_id) ?? runId),
524
- kind: "artifact",
525
- source,
526
- parentId: modelParent(base, string(raw.response_id)),
527
- status: "running",
528
- display: { title: "Audio", target: string(raw.model) },
529
- payload: {
530
- artifactType: "audio",
531
- responseId: string(raw.response_id),
532
- mimeType: string(raw.mime_type),
533
- inline: true,
534
- base64: delta,
535
- },
536
- raw,
537
- }, base);
538
- }
539
- if (type === "audio-chunk") {
540
- const chunk = raw.chunk;
541
- const base64 = typeof chunk === "string"
542
- ? chunk
543
- : isRecord(chunk) && typeof chunk.base64 === "string"
544
- ? chunk.base64
545
- : undefined;
546
- return event({
547
- id: id("audio", string(raw.response_id) ?? runId, number(raw.sequenceIndex)),
548
- kind: "artifact",
549
- source,
550
- parentId: modelParent(base, string(raw.response_id)),
551
- status: "running",
552
- display: { title: "Audio", target: string(raw.model) },
553
- payload: {
554
- artifactType: "audio",
555
- responseId: string(raw.response_id),
556
- sequenceIndex: number(raw.sequenceIndex),
557
- mimeType: string(raw.mime_type) ?? string(raw.mimeType),
558
- inline: true,
559
- base64,
560
- },
561
- raw,
562
- }, base);
563
- }
564
- if (type === "subtitle" || type === "response.output_audio.subtitle") {
565
- const subtitle = isRecord(raw.subtitle) ? raw.subtitle : raw;
566
- return event({
567
- id: id("subtitle", string(raw.response_id), number(subtitle.start), number(subtitle.end), runId),
568
- kind: "artifact",
569
- source,
570
- parentId: modelParent(base, string(raw.response_id)),
571
- status: "running",
572
- display: { title: "Subtitle", summary: string(subtitle.text), target: string(raw.model) },
573
- payload: {
574
- artifactType: "text",
575
- responseId: string(raw.response_id),
576
- subtitle,
577
- },
578
- raw,
579
- }, base);
580
- }
581
- if (type === "response.text.delta") {
582
- const delta = string(raw.delta) ?? "";
583
- return delta ? event({
584
- id: id("text", string(raw.response_id) ?? runId),
585
- kind: "text",
586
- source,
587
- parentId: modelParent(base, string(raw.response_id)),
588
- status: "running",
589
- delta,
590
- display: { title: "Response", target: string(raw.model) },
591
- raw,
592
- }, base) : null;
593
- }
594
- if (type === "response.image_generation_call.partial_image" || type === "response.image_generation_call.completed") {
595
- const complete = type.endsWith(".completed");
596
- return event({
597
- id: id("artifact", string(raw.response_id), number(raw.partial_image_index)),
598
- kind: "artifact",
599
- source,
600
- parentId: modelParent(base, string(raw.response_id)),
601
- status: complete ? "completed" : "running",
602
- display: {
603
- title: complete ? "Image complete" : "Image partial",
604
- target: string(raw.model),
605
- summary: string(raw.revised_prompt),
606
- },
607
- payload: {
608
- artifactType: "image",
609
- responseId: string(raw.response_id),
610
- mimeType: string(raw.mime_type) ?? "image/png",
611
- inline: true,
612
- partial: !complete,
613
- base64: string(raw.partial_image_b64) ?? string(raw.image_b64),
614
- usage: raw.usage,
615
- },
616
- raw,
617
- }, base);
618
- }
619
- if (type === "image-partial" || type === "image-complete") {
620
- const image = isRecord(raw.image) ? raw.image : raw;
621
- const complete = type === "image-complete";
622
- return event({
623
- id: id("artifact", string(raw.response_id) ?? runId, number(image.index), string(image.url), string(image.base64)),
624
- kind: "artifact",
625
- source,
626
- parentId: modelParent(base, string(raw.response_id)),
627
- status: complete ? "completed" : "running",
628
- display: {
629
- title: complete ? "Image complete" : "Image partial",
630
- target: string(raw.model),
631
- summary: string(image.revisedPrompt),
632
- },
633
- payload: {
634
- artifactType: "image",
635
- responseId: string(raw.response_id),
636
- mimeType: string(image.mediaType) ?? string(image.mimeType) ?? "image/png",
637
- url: string(image.url),
638
- inline: string(image.base64) !== undefined,
639
- partial: !complete,
640
- base64: string(image.base64),
641
- usage: raw.usage,
642
- },
643
- raw,
644
- }, base);
645
- }
646
- if (type === "response.output_item.completed") {
647
- const item = isRecord(raw.item) ? raw.item : {};
648
- const itemType = string(item.type);
649
- if (itemType === "output_text") {
650
- const text = string(item.text) ?? "";
651
- return text ? event({
652
- id: id("text", string(raw.response_id) ?? runId),
653
- kind: "text",
654
- source,
655
- parentId: modelParent(base, string(raw.response_id)),
656
- status: "completed",
657
- delta: text,
658
- display: { title: "Response", target: string(raw.model) },
659
- raw,
660
- }, base) : null;
661
- }
662
- const asset = media(item);
663
- if (asset) {
664
- return event({
665
- id: id("artifact", string(raw.response_id), number(raw.output_index), asset.url ?? asset.base64),
666
- kind: "artifact",
667
- source,
668
- parentId: modelParent(base, string(raw.response_id)),
669
- status: "completed",
670
- display: {
671
- title: asset.kind,
672
- target: string(raw.model),
673
- summary: string(item.text) ?? string(item.status),
674
- },
675
- payload: {
676
- artifactType: asset.kind,
677
- responseId: string(raw.response_id),
678
- mimeType: string(item.mime_type),
679
- url: asset.url,
680
- inline: asset.base64 !== undefined,
681
- base64: asset.base64,
682
- status: string(item.status),
683
- jobId: string(item.job_id),
684
- },
685
- raw,
686
- }, base);
687
- }
688
- }
689
- if (type === "response.output_video.status" || type === "compose.video.status" || type === "status") {
690
- return event({
691
- id: id("video", string(raw.job_id) ?? string(raw.jobId) ?? string(raw.response_id)),
692
- kind: "artifact",
693
- source,
694
- parentId: modelParent(base, string(raw.response_id)),
695
- status: raw.status === "failed" ? "failed" : raw.status === "completed" ? "completed" : "running",
696
- display: {
697
- title: "Video",
698
- target: string(raw.model),
699
- summary: string(raw.status),
700
- },
701
- payload: {
702
- artifactType: "video",
703
- responseId: string(raw.response_id),
704
- jobId: string(raw.job_id) ?? string(raw.jobId),
705
- status: string(raw.status),
706
- progress: number(raw.progress),
707
- url: string(raw.url),
708
- error: string(raw.error),
709
- },
710
- raw,
711
- }, base);
712
- }
713
- if (type === "video-complete") {
714
- const video = isRecord(raw.video) ? raw.video : raw;
715
- return event({
716
- id: id("video", string(video.jobId), string(video.url), string(raw.response_id), runId),
717
- kind: "artifact",
718
- source,
719
- parentId: modelParent(base, string(raw.response_id)),
720
- status: "completed",
721
- display: { title: "Video", target: string(raw.model), summary: string(video.status) },
722
- payload: {
723
- artifactType: "video",
724
- responseId: string(raw.response_id),
725
- jobId: string(video.jobId),
726
- status: string(video.status) ?? "completed",
727
- progress: number(video.progress),
728
- url: string(video.url),
729
- mimeType: string(video.mediaType) ?? string(video.mimeType),
730
- inline: string(video.base64) !== undefined,
731
- base64: string(video.base64),
732
- },
733
- raw,
734
- }, base);
735
- }
736
- if (type === "start" || type === "step" || type === "agent" || type === "progress" || type === "complete") {
737
- const kind = type === "agent" ? "agent" : type === "progress" || type === "step" ? "action" : "run";
738
- return event({
739
- id: id(kind, string(raw.stepName) ?? string(raw.agentName) ?? type, runId),
740
- kind,
741
- source,
742
- status: type === "complete" ? "completed" : "running",
743
- display: {
744
- title: string(raw.agentName) ?? string(raw.stepName) ?? type,
745
- summary: string(raw.message),
746
- },
747
- payload: raw,
748
- raw,
749
- }, base);
750
- }
751
- if (type === "result") {
752
- const output = raw.output;
753
- const outputRecord = isRecord(output) ? output : {};
754
- const mediaOutput = media(outputRecord);
755
- if (mediaOutput) {
756
- return event({
757
- id: id("artifact", mediaOutput.url ?? mediaOutput.base64, runId),
758
- kind: "artifact",
759
- source,
760
- status: "completed",
761
- display: { title: mediaOutput.kind, summary: string(outputRecord.status) },
762
- payload: {
763
- artifactType: mediaOutput.kind,
764
- url: mediaOutput.url,
765
- inline: mediaOutput.base64 !== undefined,
766
- base64: mediaOutput.base64,
767
- mimeType: string(outputRecord.mimeType) ?? string(outputRecord.mime_type),
768
- },
769
- raw,
770
- }, base);
771
- }
772
- const text = string(output) ?? (isRecord(output) ? JSON.stringify(output) : undefined);
773
- return text ? event({
774
- id: id("text", runId),
775
- kind: "text",
776
- source,
777
- status: "completed",
778
- delta: text,
779
- display: { title: "Result" },
780
- payload: raw,
781
- raw,
782
- }, base) : null;
783
- }
784
- if (type === "tool-call-start" || type === "tool-call" || type === "tool-call-delta" || type === "response.tool_call" || type === "response.tool_call.delta" || type === "tool_args_delta") {
785
- const call = isRecord(raw.tool_call) ? raw.tool_call : raw;
786
- const delta = isRecord(raw.delta) ? raw.delta : raw;
787
- return event({
788
- id: id("tool", string(call.id) ?? string(delta.id) ?? string(call.name) ?? string(raw.toolName), runId),
789
- kind: "tool",
790
- source,
791
- parentId: modelParent(base, string(raw.response_id)),
792
- status: type.endsWith(".delta") || type === "tool_args_delta" || type === "tool-call-delta" || type === "tool-call-start" ? "running" : "completed",
793
- display: { title: string(call.name) ?? string(delta.name) ?? string(raw.toolName) ?? "Tool" },
794
- payload: {
795
- name: string(call.name) ?? string(delta.name) ?? string(raw.toolName),
796
- arguments: string(call.arguments) ?? string(delta.arguments) ?? string(raw.argumentsDelta) ?? string(raw.argsDelta),
797
- index: number(raw.index),
798
- },
799
- raw,
800
- }, base);
801
- }
802
- if (type === "tool_start" || type === "tool-start") {
803
- const display = displayOf(raw);
804
- return event({
805
- id: id("tool", string(raw.toolName) ?? display.target, runId),
806
- kind: displayKind(display, "tool"),
807
- source,
808
- parentId: agentParent(base),
809
- status: "running",
810
- display: { title: display.title ?? string(raw.toolName) ?? "Tool", summary: string(raw.content) ?? display.summary, target: display.target, kind: display.kind, metadata: display.metadata },
811
- raw,
812
- }, base);
813
- }
814
- if (type === "tool_end" || type === "tool-end") {
815
- const display = displayOf(raw);
816
- const failed = Boolean(raw.failed) || Boolean(string(raw.error));
817
- return event({
818
- id: id("tool", string(raw.toolName) ?? display.target, runId),
819
- kind: displayKind(display, "tool"),
820
- source,
821
- parentId: agentParent(base),
822
- status: failed ? "failed" : "completed",
823
- display: { title: display.title ?? string(raw.toolName) ?? "Tool", summary: string(raw.message) ?? display.summary, target: display.target, kind: display.kind, metadata: display.metadata },
824
- payload: { output: raw.output, error: string(raw.error) },
825
- raw,
826
- }, base);
827
- }
828
- if (type === "harness_plan_proposed" || type === "harness_plan_decided") {
829
- const decided = type === "harness_plan_decided";
830
- return event({
831
- id: id("approval", string(raw.proposalId), string(raw.version)),
832
- kind: "approval",
833
- source,
834
- parentId: agentParent(base),
835
- status: decided ? "completed" : "pending",
836
- display: {
837
- title: decided ? "Plan decision" : "Plan review",
838
- summary: string(raw.state),
839
- target: string(raw.composeRunId),
840
- },
841
- payload: {
842
- proposalId: string(raw.proposalId),
843
- version: number(raw.version),
844
- state: string(raw.state),
845
- decision: string(raw.decision),
846
- proposal: raw.proposal,
847
- markdown: string(raw.markdown),
848
- approver: string(raw.approver),
849
- reason: string(raw.reason),
850
- feedback: string(raw.feedback),
851
- },
852
- raw,
853
- }, base);
854
- }
855
- if (type.startsWith("swarm_child_") || type === "child") {
856
- return childEvent(raw, type, base);
857
- }
858
- if (type === "artifact") {
859
- const runKey = string(raw.runKey);
860
- const parentRunId = string(raw.parentRunId) ?? runId;
861
- return event({
862
- id: id("artifact", string(raw.responseId), string(raw.jobId), string(raw.runKey)),
863
- kind: "artifact",
864
- source,
865
- parentId: runKey ? id("agent", runKey, parentRunId) : agentParent(base),
866
- status: raw.status === "failed" ? "failed" : raw.status === "completed" ? "completed" : "running",
867
- display: { title: string(raw.artifactType) ?? "Artifact", summary: string(raw.status) },
868
- payload: raw,
869
- raw,
870
- }, base);
871
- }
872
- if (type === "trace") {
873
- const display = displayOf(raw);
874
- return event({
875
- id: id("debug", string(raw.source), string(raw.stage), string(raw.action), display.target ?? display.summary ?? display.title),
876
- kind: "debug",
877
- source,
878
- parentId: agentParent(base),
879
- status: "info",
880
- display,
881
- payload: { stage: string(raw.stage), action: string(raw.action), message: string(raw.message), details: raw.details },
882
- raw,
883
- }, base);
884
- }
885
- if (type === "conclave") {
886
- const key = string(raw.key);
887
- return event({
888
- id: id("conclave", string(raw.action), key, runId),
889
- kind: "conclave",
890
- source,
891
- parentId: agentParent(base),
892
- status: raw.success === false ? "failed" : "completed",
893
- display: { title: "Conclave", summary: string(raw.action), target: key },
894
- payload: raw,
895
- raw,
896
- }, base);
897
- }
898
- if (type === "compose.receipt") {
899
- return receipt(raw, base);
900
- }
901
- if (type === "session-active" || type === "session-expired" || type === "session-lease") {
902
- return event({
903
- id: id("session", type, string(raw.userAddress), string(raw.chainId)),
904
- kind: "session",
905
- source,
906
- status: type === "session-active" ? "running" : "info",
907
- display: { title: "Session", summary: type },
908
- payload: raw,
909
- raw,
910
- }, base);
911
- }
912
- if (type === "error" || type === "compose.error" || type === "response.error") {
913
- const err = isRecord(raw.error) ? raw.error : raw;
914
- return event({
915
- id: id("error", string(err.code), string(err.message) ?? string(raw.message), runId),
916
- kind: "error",
917
- source,
918
- status: "failed",
919
- display: { title: "Error", summary: string(err.message) ?? string(raw.message) ?? string(raw.content) },
920
- payload: { ...raw, error: err },
921
- raw,
922
- }, base);
923
- }
924
- if (type === "done" || type === "finish" || type === "message-stop" || type === "message_stop" || type === "response.completed") {
925
- if (type === "response.completed") {
926
- const responseId = string(raw.response_id) ?? runId;
927
- return event({
928
- id: id("model", responseId),
929
- kind: "model",
930
- source,
931
- status: "completed",
932
- display: { title: string(raw.model) ?? "Model", summary: string(raw.finish_reason), target: responseId },
933
- payload: raw,
934
- raw,
935
- }, base);
936
- }
937
- const targetId = terminalId(base, source, string(raw.response_id));
938
- return event({
939
- id: targetId,
940
- kind: terminalKind(targetId),
941
- source,
942
- status: "completed",
943
- display: { summary: string(raw.finish_reason) ?? string(raw.finishReason) ?? "Done" },
944
- payload: raw,
945
- raw,
946
- }, base);
947
- }
948
- const content = string(raw.content) ?? string(raw.text);
949
- if (content) {
950
- return event({
951
- id: id("text", runId),
952
- kind: "text",
953
- source,
954
- status: "running",
955
- delta: content,
956
- display: { title: "Response" },
957
- raw,
958
- }, base);
959
- }
960
- return null;
961
- }
962
- function childEvent(raw, type, options) {
963
- const eventType = type === "child" ? string(raw.event) ?? "" : type.replace(/^swarm_child_/, "");
964
- const runKey = string(raw.runKey);
965
- const parentRunId = string(raw.parentRunId) ?? options.runId;
966
- const agentId = id("agent", runKey ?? string(raw.agentWallet), parentRunId);
967
- const parentRun = parentRunId ? id("agent", parentRunId) : options.parentId;
968
- if (eventType === "start") {
969
- return event({
970
- id: agentId,
971
- kind: "agent",
972
- source: options.source ?? "agent",
973
- parentId: parentRun,
974
- status: "running",
975
- path: array(raw.runKeyChain),
976
- display: { title: string(raw.agentWallet) ?? "Agent", target: runKey },
977
- payload: raw,
978
- raw,
979
- }, options);
980
- }
981
- if (eventType === "delta") {
982
- const delta = string(raw.delta) ?? "";
983
- return delta ? event({
984
- id: id("text", runKey),
985
- kind: "text",
986
- source: options.source ?? "agent",
987
- parentId: agentId,
988
- status: "running",
989
- path: array(raw.runKeyChain),
990
- delta,
991
- display: { title: "Agent response" },
992
- raw,
993
- }, options) : null;
994
- }
995
- if (eventType === "tool-start" || eventType === "tool_start") {
996
- return event({
997
- id: id("tool", runKey, string(raw.toolName)),
998
- kind: "tool",
999
- source: options.source ?? "agent",
1000
- parentId: agentId,
1001
- status: "running",
1002
- path: array(raw.runKeyChain),
1003
- display: { title: string(raw.toolName) ?? "Tool" },
1004
- payload: { input: raw.input },
1005
- raw,
1006
- }, options);
1007
- }
1008
- if (eventType === "tool-end" || eventType === "tool_end") {
1009
- return event({
1010
- id: id("tool", runKey, string(raw.toolName)),
1011
- kind: "tool",
1012
- source: options.source ?? "agent",
1013
- parentId: agentId,
1014
- status: raw.failed === true ? "failed" : "completed",
1015
- path: array(raw.runKeyChain),
1016
- display: { title: string(raw.toolName) ?? "Tool" },
1017
- payload: { output: raw.output, error: string(raw.error) },
1018
- raw,
1019
- }, options);
1020
- }
1021
- if (eventType === "done" || eventType === "error") {
1022
- return event({
1023
- id: agentId,
1024
- kind: "agent",
1025
- source: options.source ?? "agent",
1026
- parentId: parentRun,
1027
- status: eventType === "error" ? "failed" : "completed",
1028
- path: array(raw.runKeyChain),
1029
- display: { title: string(raw.agentWallet) ?? "Agent", summary: string(raw.stopReason) ?? string(raw.error), target: runKey },
1030
- payload: raw,
1031
- raw,
1032
- }, options);
1033
- }
1034
- return null;
1035
- }
1036
- function receipt(raw, options) {
1037
- return event({
1038
- id: id("receipt", string(raw.id), string(raw.runId), string(raw.txHash), string(raw.settleTxHash)),
1039
- kind: "receipt",
1040
- source: options.source ?? "payment",
1041
- status: "completed",
1042
- display: { title: "Receipt", summary: string(raw.settlementStatus), target: string(raw.txHash) ?? string(raw.settleTxHash) },
1043
- payload: raw,
1044
- raw,
1045
- }, options);
1046
- }
1047
- function event(input, options) {
1048
- return {
1049
- type: "stream",
1050
- ...input,
1051
- ...(input.rootId || options.rootId ? { rootId: input.rootId ?? options.rootId } : {}),
1052
- ...(input.runId || options.runId ? { runId: input.runId ?? options.runId } : {}),
1053
- ...(input.parentId || options.parentId ? { parentId: input.parentId ?? options.parentId } : {}),
1054
- ts: input.ts ?? Date.now(),
1055
- };
1056
- }
1057
- function framePayload(input) {
1058
- if (isRecord(input) && typeof input.data === "string" && typeof input.event === "string") {
1059
- if (input.data === "[DONE]")
1060
- return "[DONE]";
1061
- if (input.event !== "message") {
1062
- try {
1063
- const parsed = JSON.parse(input.data);
1064
- return isRecord(parsed) ? { ...parsed, type: input.event } : parsed;
1065
- }
1066
- catch {
1067
- return { type: input.event, message: input.data };
1068
- }
1069
- }
1070
- try {
1071
- return JSON.parse(input.data);
1072
- }
1073
- catch {
1074
- return input.data;
1075
- }
1076
- }
1077
- return input;
1078
- }
1079
- function displayOf(raw) {
1080
- const display = isRecord(raw.display) ? raw.display : {};
1081
- const kind = streamKind(string(display.kind));
1082
- const details = isRecord(display.details) ? display.details : isRecord(raw.details) ? raw.details : undefined;
1083
- const metadata = kind || details ? { ...(details ?? {}), ...(kind ? { kind } : {}) } : undefined;
1084
- return {
1085
- title: string(display.name) ?? string(raw.source) ?? string(raw.toolName),
1086
- label: string(display.label),
1087
- summary: string(display.summary) ?? string(raw.message),
1088
- target: string(display.target) ?? string(display.id),
1089
- ...(kind ? { kind } : {}),
1090
- ...(metadata ? { metadata } : {}),
1091
- };
1092
- }
1093
- function displayKind(display, fallback) {
1094
- const kind = display.kind ?? (display.metadata && typeof display.metadata.kind === "string" ? streamKind(display.metadata.kind) : undefined);
1095
- if (kind === "model" || kind === "connector" || kind === "agent" || kind === "harness" || kind === "conclave" || kind === "catalog")
1096
- return kind;
1097
- return fallback;
1098
- }
1099
- function streamKind(value) {
1100
- switch (value) {
1101
- case "run":
1102
- case "agent":
1103
- case "text":
1104
- case "reasoning":
1105
- case "action":
1106
- case "tool":
1107
- case "catalog":
1108
- case "model":
1109
- case "connector":
1110
- case "harness":
1111
- case "conclave":
1112
- case "approval":
1113
- case "artifact":
1114
- case "receipt":
1115
- case "payment":
1116
- case "session":
1117
- case "error":
1118
- case "debug":
1119
- return value;
1120
- case "search":
1121
- return "catalog";
1122
- default:
1123
- return undefined;
1124
- }
1125
- }
1126
- function agentParent(options) {
1127
- return options.runId ? id("agent", options.runId) : options.parentId;
1128
- }
1129
- function modelParent(options, responseId) {
1130
- const value = responseId ?? options.runId;
1131
- return value ? id("model", value) : options.parentId;
1132
- }
1133
- function terminalId(options, source, responseId) {
1134
- const value = responseId ?? options.runId;
1135
- if ((source === "inference" || responseId) && value)
1136
- return id("model", value);
1137
- if (value)
1138
- return id("agent", value);
1139
- return options.parentId ?? "done";
1140
- }
1141
- function terminalKind(idValue) {
1142
- if (idValue.startsWith("agent:"))
1143
- return "agent";
1144
- if (idValue.startsWith("model:"))
1145
- return "model";
1146
- return "run";
1147
- }
1148
- function sourceOf(type, raw) {
1149
- const source = string(raw.source);
1150
- if (source)
1151
- return source;
1152
- if (type.startsWith("response."))
1153
- return "inference";
1154
- if (type.startsWith("swarm_child_"))
1155
- return "agent";
1156
- if (type.startsWith("harness_"))
1157
- return "harness";
1158
- if (type.startsWith("session-"))
1159
- return "session";
1160
- return "stream";
1161
- }
1162
- function id(...parts) {
1163
- const clean = parts
1164
- .filter((part) => part !== undefined && part !== null && String(part).trim().length > 0)
1165
- .map((part) => String(part).trim().replace(/\s+/g, "_"));
1166
- return clean.length > 0 ? clean.join(":") : "stream";
1167
- }
1168
- function placeholder(idValue, eventValue) {
1169
- const kind = idValue.startsWith("agent:")
1170
- ? "agent"
1171
- : idValue.startsWith("model:")
1172
- ? "model"
1173
- : "run";
1174
- return {
1175
- id: idValue,
1176
- kind,
1177
- source: eventValue.source,
1178
- status: "running",
1179
- path: [],
1180
- display: { title: kind === "agent" ? "Agent" : kind === "model" ? "Model" : idValue, target: idValue },
1181
- text: "",
1182
- children: [],
1183
- updatedAt: eventValue.ts ?? Date.now(),
1184
- events: 0,
1185
- };
1186
- }
1187
- function stable(eventValue) {
1188
- let hash = 5381;
1189
- const text = JSON.stringify({
1190
- id: eventValue.id,
1191
- kind: eventValue.kind,
1192
- status: eventValue.status,
1193
- delta: eventValue.delta,
1194
- display: eventValue.display,
1195
- payload: eventValue.payload,
1196
- });
1197
- for (let i = 0; i < text.length; i += 1) {
1198
- hash = ((hash << 5) + hash) ^ text.charCodeAt(i);
1199
- }
1200
- return hash >>> 0;
1201
- }
1202
- function merge(a, b) {
1203
- if (!a && !b)
1204
- return undefined;
1205
- return { ...(a ?? {}), ...(b ?? {}) };
1206
- }
1207
- function isRecord(value) {
1208
- return value !== null && typeof value === "object" && !Array.isArray(value);
1209
- }
1210
- function string(value) {
1211
- return typeof value === "string" && value.length > 0 ? value : undefined;
1212
- }
1213
- function number(value) {
1214
- return typeof value === "number" && Number.isFinite(value) ? value : undefined;
1215
- }
1216
- function array(value) {
1217
- return Array.isArray(value) && value.every((item) => typeof item === "string") ? value : undefined;
1218
- }
1219
- function media(value) {
1220
- const type = string(value.type) ?? string(value.kind);
1221
- const normalized = type === "output_image" || type === "image"
1222
- ? "image"
1223
- : type === "output_audio" || type === "audio"
1224
- ? "audio"
1225
- : type === "output_video" || type === "video"
1226
- ? "video"
1227
- : type === "output_embedding" || type === "embedding"
1228
- ? "embedding"
1229
- : type === "file" || type === "artifact"
1230
- ? "file"
1231
- : undefined;
1232
- if (!normalized)
1233
- return null;
1234
- const url = string(value.url)
1235
- ?? string(value.image_url)
1236
- ?? string(value.imageUrl)
1237
- ?? string(value.audio_url)
1238
- ?? string(value.audioUrl)
1239
- ?? string(value.video_url)
1240
- ?? string(value.videoUrl);
1241
- const base64 = string(value.base64)
1242
- ?? string(value.b64_json)
1243
- ?? string(value.data);
1244
- return { kind: normalized, ...(url ? { url } : {}), ...(base64 ? { base64 } : {}) };
1245
- }
1246
- function mediaKind(mimeType) {
1247
- if (!mimeType)
1248
- return "file";
1249
- if (mimeType.startsWith("image/"))
1250
- return "image";
1251
- if (mimeType.startsWith("audio/"))
1252
- return "audio";
1253
- if (mimeType.startsWith("video/"))
1254
- return "video";
1255
- return "file";
1256
- }
1257
- //# sourceMappingURL=index.js.map