promptlayer 1.0.60 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/esm/chunk-SWBNW72U.js +2 -0
- package/dist/esm/chunk-SWBNW72U.js.map +1 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/openai-agents.js +3 -0
- package/dist/esm/openai-agents.js.map +1 -0
- package/dist/index.d.mts +229 -9
- package/dist/index.d.ts +229 -9
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/openai-agents.d.mts +42 -0
- package/dist/openai-agents.d.ts +42 -0
- package/dist/openai-agents.js +3 -0
- package/dist/openai-agents.js.map +1 -0
- package/package.json +24 -3
- package/src/integrations/openai-agents/helpers.test.ts +254 -0
- package/src/integrations/openai-agents/ids.ts +27 -0
- package/src/integrations/openai-agents/index.ts +8 -0
- package/src/integrations/openai-agents/instrumentation.test.ts +46 -0
- package/src/integrations/openai-agents/instrumentation.ts +47 -0
- package/src/integrations/openai-agents/mapping.ts +714 -0
- package/src/integrations/openai-agents/otlp-json.ts +120 -0
- package/src/integrations/openai-agents/processor.test.ts +509 -0
- package/src/integrations/openai-agents/processor.ts +388 -0
- package/src/integrations/openai-agents/time.ts +56 -0
- package/src/integrations/openai-agents/types.ts +49 -0
- package/src/integrations/openai-agents/url.ts +9 -0
- package/src/openai-agents.ts +1 -0
- package/src/types.ts +302 -9
- package/src/utils/blueprint-builder.test.ts +727 -0
- package/src/utils/blueprint-builder.ts +957 -126
- package/src/utils/streaming.test.ts +498 -0
- package/src/utils/streaming.ts +471 -43
- package/src/utils/utils.ts +4 -0
- package/tsup.config.ts +4 -1
- package/vitest.config.ts +3 -0
package/src/utils/streaming.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
buildPromptBlueprintFromBedrockEvent,
|
|
14
14
|
buildPromptBlueprintFromGoogleEvent,
|
|
15
15
|
buildPromptBlueprintFromOpenAIEvent,
|
|
16
|
+
buildPromptBlueprintFromOpenAIImagesEvent,
|
|
16
17
|
buildPromptBlueprintFromOpenAIResponsesEvent,
|
|
17
18
|
} from "./blueprint-builder";
|
|
18
19
|
|
|
@@ -47,6 +48,7 @@ export const openaiResponsesStreamChat = (results: any[]) => {
|
|
|
47
48
|
};
|
|
48
49
|
|
|
49
50
|
const current_items: Record<string, any> = {};
|
|
51
|
+
const output_index_to_item_id: Record<number, string> = {};
|
|
50
52
|
|
|
51
53
|
for (const chunk of results as any[]) {
|
|
52
54
|
const event_type = chunk?.type;
|
|
@@ -94,6 +96,10 @@ export const openaiResponsesStreamChat = (results: any[]) => {
|
|
|
94
96
|
const item = chunk.item || {};
|
|
95
97
|
const item_id = item.id;
|
|
96
98
|
const item_type = item.type;
|
|
99
|
+
const output_index = chunk.output_index;
|
|
100
|
+
if (output_index != null && item_id) {
|
|
101
|
+
output_index_to_item_id[output_index] = item_id;
|
|
102
|
+
}
|
|
97
103
|
|
|
98
104
|
if (item_type === "reasoning") {
|
|
99
105
|
current_items[item_id] = {
|
|
@@ -127,6 +133,90 @@ export const openaiResponsesStreamChat = (results: any[]) => {
|
|
|
127
133
|
code: item.code ?? "",
|
|
128
134
|
status: item.status ?? "in_progress",
|
|
129
135
|
};
|
|
136
|
+
} else if (item_type === "image_generation_call") {
|
|
137
|
+
current_items[item_id] = {
|
|
138
|
+
type: "image_generation_call",
|
|
139
|
+
id: item_id,
|
|
140
|
+
status: item.status ?? "in_progress",
|
|
141
|
+
revised_prompt: item.revised_prompt ?? "",
|
|
142
|
+
result: item.result ?? "",
|
|
143
|
+
background: item.background,
|
|
144
|
+
size: item.size,
|
|
145
|
+
quality: item.quality,
|
|
146
|
+
output_format: item.output_format,
|
|
147
|
+
};
|
|
148
|
+
} else if (item_type === "web_search_call") {
|
|
149
|
+
current_items[item_id] = {
|
|
150
|
+
type: "web_search_call",
|
|
151
|
+
id: item_id,
|
|
152
|
+
status: item.status ?? "in_progress",
|
|
153
|
+
};
|
|
154
|
+
} else if (item_type === "file_search_call") {
|
|
155
|
+
current_items[item_id] = {
|
|
156
|
+
type: "file_search_call",
|
|
157
|
+
id: item_id,
|
|
158
|
+
status: item.status ?? "in_progress",
|
|
159
|
+
};
|
|
160
|
+
} else if (item_type === "mcp_list_tools") {
|
|
161
|
+
current_items[item_id] = {
|
|
162
|
+
type: "mcp_list_tools",
|
|
163
|
+
id: item_id,
|
|
164
|
+
server_label: item.server_label ?? "",
|
|
165
|
+
tools: item.tools ?? [],
|
|
166
|
+
error: item.error ?? null,
|
|
167
|
+
};
|
|
168
|
+
} else if (item_type === "mcp_call") {
|
|
169
|
+
current_items[item_id] = {
|
|
170
|
+
type: "mcp_call",
|
|
171
|
+
id: item_id,
|
|
172
|
+
name: item.name ?? "",
|
|
173
|
+
server_label: item.server_label ?? "",
|
|
174
|
+
arguments: item.arguments ?? "",
|
|
175
|
+
output: item.output ?? null,
|
|
176
|
+
error: item.error ?? null,
|
|
177
|
+
approval_request_id: item.approval_request_id ?? null,
|
|
178
|
+
status: item.status ?? "in_progress",
|
|
179
|
+
};
|
|
180
|
+
} else if (item_type === "mcp_approval_request") {
|
|
181
|
+
current_items[item_id] = {
|
|
182
|
+
type: "mcp_approval_request",
|
|
183
|
+
id: item_id,
|
|
184
|
+
name: item.name ?? "",
|
|
185
|
+
arguments: item.arguments ?? "",
|
|
186
|
+
server_label: item.server_label ?? "",
|
|
187
|
+
};
|
|
188
|
+
} else if (item_type === "shell_call") {
|
|
189
|
+
current_items[item_id] = {
|
|
190
|
+
type: "shell_call",
|
|
191
|
+
id: item_id,
|
|
192
|
+
call_id: item.call_id ?? "",
|
|
193
|
+
action: item.action ?? {},
|
|
194
|
+
status: item.status ?? "in_progress",
|
|
195
|
+
};
|
|
196
|
+
} else if (item_type === "shell_call_output") {
|
|
197
|
+
current_items[item_id] = {
|
|
198
|
+
type: "shell_call_output",
|
|
199
|
+
id: item_id,
|
|
200
|
+
call_id: item.call_id ?? "",
|
|
201
|
+
output: item.output ?? [],
|
|
202
|
+
status: item.status ?? "in_progress",
|
|
203
|
+
};
|
|
204
|
+
} else if (item_type === "apply_patch_call") {
|
|
205
|
+
current_items[item_id] = {
|
|
206
|
+
type: "apply_patch_call",
|
|
207
|
+
id: item_id,
|
|
208
|
+
call_id: item.call_id ?? "",
|
|
209
|
+
operation: item.operation ?? {},
|
|
210
|
+
status: item.status ?? "in_progress",
|
|
211
|
+
};
|
|
212
|
+
} else if (item_type === "apply_patch_call_output") {
|
|
213
|
+
current_items[item_id] = {
|
|
214
|
+
type: "apply_patch_call_output",
|
|
215
|
+
id: item_id,
|
|
216
|
+
call_id: item.call_id ?? "",
|
|
217
|
+
output: item.output ?? "",
|
|
218
|
+
status: item.status ?? "in_progress",
|
|
219
|
+
};
|
|
130
220
|
}
|
|
131
221
|
continue;
|
|
132
222
|
}
|
|
@@ -269,12 +359,181 @@ export const openaiResponsesStreamChat = (results: any[]) => {
|
|
|
269
359
|
current_items[item_id].content =
|
|
270
360
|
item.content ?? current_items[item_id].content;
|
|
271
361
|
current_items[item_id].role = item.role ?? current_items[item_id].role;
|
|
362
|
+
} else if (item.type === "image_generation_call") {
|
|
363
|
+
current_items[item_id].result =
|
|
364
|
+
item.result ?? current_items[item_id].result;
|
|
365
|
+
current_items[item_id].revised_prompt =
|
|
366
|
+
item.revised_prompt ?? current_items[item_id].revised_prompt;
|
|
367
|
+
current_items[item_id].background =
|
|
368
|
+
item.background ?? current_items[item_id].background;
|
|
369
|
+
current_items[item_id].size =
|
|
370
|
+
item.size ?? current_items[item_id].size;
|
|
371
|
+
current_items[item_id].quality =
|
|
372
|
+
item.quality ?? current_items[item_id].quality;
|
|
373
|
+
current_items[item_id].output_format =
|
|
374
|
+
item.output_format ?? current_items[item_id].output_format;
|
|
375
|
+
} else if (item.type === "code_interpreter_call") {
|
|
376
|
+
current_items[item_id].code =
|
|
377
|
+
item.code ?? current_items[item_id].code;
|
|
378
|
+
current_items[item_id].container_id =
|
|
379
|
+
item.container_id ?? current_items[item_id].container_id;
|
|
380
|
+
} else if (item.type === "mcp_list_tools") {
|
|
381
|
+
current_items[item_id].tools =
|
|
382
|
+
item.tools ?? current_items[item_id].tools;
|
|
383
|
+
current_items[item_id].error =
|
|
384
|
+
item.error ?? current_items[item_id].error;
|
|
385
|
+
} else if (item.type === "mcp_call") {
|
|
386
|
+
current_items[item_id].name =
|
|
387
|
+
item.name ?? current_items[item_id].name;
|
|
388
|
+
current_items[item_id].arguments =
|
|
389
|
+
item.arguments ?? current_items[item_id].arguments;
|
|
390
|
+
current_items[item_id].output =
|
|
391
|
+
item.output ?? current_items[item_id].output;
|
|
392
|
+
current_items[item_id].error =
|
|
393
|
+
item.error ?? current_items[item_id].error;
|
|
394
|
+
current_items[item_id].server_label =
|
|
395
|
+
item.server_label ?? current_items[item_id].server_label;
|
|
396
|
+
} else if (item.type === "mcp_approval_request") {
|
|
397
|
+
current_items[item_id].name =
|
|
398
|
+
item.name ?? current_items[item_id].name;
|
|
399
|
+
current_items[item_id].arguments =
|
|
400
|
+
item.arguments ?? current_items[item_id].arguments;
|
|
401
|
+
current_items[item_id].server_label =
|
|
402
|
+
item.server_label ?? current_items[item_id].server_label;
|
|
403
|
+
} else if (item.type === "shell_call") {
|
|
404
|
+
current_items[item_id].action =
|
|
405
|
+
item.action ?? current_items[item_id].action;
|
|
406
|
+
current_items[item_id].call_id =
|
|
407
|
+
item.call_id ?? current_items[item_id].call_id;
|
|
408
|
+
} else if (item.type === "shell_call_output") {
|
|
409
|
+
current_items[item_id].output =
|
|
410
|
+
item.output ?? current_items[item_id].output;
|
|
411
|
+
current_items[item_id].call_id =
|
|
412
|
+
item.call_id ?? current_items[item_id].call_id;
|
|
413
|
+
} else if (item.type === "apply_patch_call") {
|
|
414
|
+
current_items[item_id].operation =
|
|
415
|
+
item.operation ?? current_items[item_id].operation;
|
|
416
|
+
current_items[item_id].call_id =
|
|
417
|
+
item.call_id ?? current_items[item_id].call_id;
|
|
418
|
+
} else if (item.type === "apply_patch_call_output") {
|
|
419
|
+
current_items[item_id].output =
|
|
420
|
+
item.output ?? current_items[item_id].output;
|
|
421
|
+
current_items[item_id].call_id =
|
|
422
|
+
item.call_id ?? current_items[item_id].call_id;
|
|
423
|
+
} else if (item.type === "web_search_call") {
|
|
424
|
+
current_items[item_id].action = item.action;
|
|
425
|
+
} else if (item.type === "file_search_call") {
|
|
426
|
+
current_items[item_id].action = item.action;
|
|
272
427
|
}
|
|
273
428
|
response_data.output.push(current_items[item_id]);
|
|
274
429
|
}
|
|
275
430
|
continue;
|
|
276
431
|
}
|
|
277
432
|
|
|
433
|
+
if (event_type === "response.image_generation_call.in_progress") {
|
|
434
|
+
const item_id = chunk.item_id;
|
|
435
|
+
if (item_id in current_items) {
|
|
436
|
+
current_items[item_id].status = "in_progress";
|
|
437
|
+
}
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
if (event_type === "response.image_generation_call.generating") {
|
|
442
|
+
const item_id = chunk.item_id;
|
|
443
|
+
if (item_id in current_items) {
|
|
444
|
+
current_items[item_id].status = "generating";
|
|
445
|
+
}
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (event_type === "response.image_generation_call.partial_image") {
|
|
450
|
+
const item_id = chunk.item_id;
|
|
451
|
+
if (item_id in current_items) {
|
|
452
|
+
current_items[item_id].result = chunk.partial_image_b64 ?? current_items[item_id].result;
|
|
453
|
+
current_items[item_id].background = chunk.background ?? current_items[item_id].background;
|
|
454
|
+
current_items[item_id].size = chunk.size ?? current_items[item_id].size;
|
|
455
|
+
current_items[item_id].quality = chunk.quality ?? current_items[item_id].quality;
|
|
456
|
+
current_items[item_id].output_format = chunk.output_format ?? current_items[item_id].output_format;
|
|
457
|
+
}
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (event_type === "response.shell_call_command.added") {
|
|
462
|
+
const item_id = output_index_to_item_id[chunk.output_index];
|
|
463
|
+
if (item_id && item_id in current_items) {
|
|
464
|
+
const action = current_items[item_id].action || { commands: [] };
|
|
465
|
+
if (!action.commands) action.commands = [];
|
|
466
|
+
action.commands[chunk.command_index] = chunk.command ?? "";
|
|
467
|
+
current_items[item_id].action = action;
|
|
468
|
+
}
|
|
469
|
+
continue;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (event_type === "response.shell_call_command.delta") {
|
|
473
|
+
const item_id = output_index_to_item_id[chunk.output_index];
|
|
474
|
+
if (item_id && item_id in current_items) {
|
|
475
|
+
const action = current_items[item_id].action || { commands: [] };
|
|
476
|
+
if (!action.commands) action.commands = [];
|
|
477
|
+
const idx = chunk.command_index ?? 0;
|
|
478
|
+
action.commands[idx] = (action.commands[idx] ?? "") + (chunk.delta ?? "");
|
|
479
|
+
current_items[item_id].action = action;
|
|
480
|
+
}
|
|
481
|
+
continue;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (event_type === "response.shell_call_command.done") {
|
|
485
|
+
const item_id = output_index_to_item_id[chunk.output_index];
|
|
486
|
+
if (item_id && item_id in current_items) {
|
|
487
|
+
const action = current_items[item_id].action || { commands: [] };
|
|
488
|
+
if (!action.commands) action.commands = [];
|
|
489
|
+
action.commands[chunk.command_index] = chunk.command ?? "";
|
|
490
|
+
current_items[item_id].action = action;
|
|
491
|
+
}
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (event_type === "response.shell_call_output_content.delta") {
|
|
496
|
+
const item_id = chunk.item_id;
|
|
497
|
+
if (item_id && item_id in current_items) {
|
|
498
|
+
if (!current_items[item_id].output) current_items[item_id].output = [];
|
|
499
|
+
const idx = chunk.command_index ?? 0;
|
|
500
|
+
const existing = current_items[item_id].output[idx] ?? { stdout: "", stderr: "" };
|
|
501
|
+
const delta = chunk.delta ?? {};
|
|
502
|
+
if (delta.stdout) existing.stdout = (existing.stdout ?? "") + delta.stdout;
|
|
503
|
+
if (delta.stderr) existing.stderr = (existing.stderr ?? "") + delta.stderr;
|
|
504
|
+
current_items[item_id].output[idx] = existing;
|
|
505
|
+
}
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (event_type === "response.shell_call_output_content.done") {
|
|
510
|
+
const item_id = chunk.item_id;
|
|
511
|
+
if (item_id && item_id in current_items) {
|
|
512
|
+
current_items[item_id].output = chunk.output ?? current_items[item_id].output;
|
|
513
|
+
}
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (event_type === "response.apply_patch_call_operation_diff.delta") {
|
|
518
|
+
const item_id = chunk.item_id;
|
|
519
|
+
if (item_id && item_id in current_items) {
|
|
520
|
+
const operation = current_items[item_id].operation || {};
|
|
521
|
+
operation.diff = (operation.diff ?? "") + (chunk.delta ?? "");
|
|
522
|
+
current_items[item_id].operation = operation;
|
|
523
|
+
}
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (event_type === "response.apply_patch_call_operation_diff.done") {
|
|
528
|
+
const item_id = chunk.item_id;
|
|
529
|
+
if (item_id && item_id in current_items) {
|
|
530
|
+
const operation = current_items[item_id].operation || {};
|
|
531
|
+
operation.diff = chunk.diff ?? operation.diff;
|
|
532
|
+
current_items[item_id].operation = operation;
|
|
533
|
+
}
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
|
|
278
537
|
if (event_type === "response.completed") {
|
|
279
538
|
const response = chunk.response || {};
|
|
280
539
|
response_data.status = response.status ?? response_data.status ?? "completed";
|
|
@@ -418,16 +677,19 @@ export const anthropicStreamMessage = (results: MessageStreamEvent[]): Message =
|
|
|
418
677
|
const lastResult = results.at(-1);
|
|
419
678
|
if (!lastResult) return response;
|
|
420
679
|
let currentBlock: any = null;
|
|
680
|
+
let currentBlockIndex: number | null = null;
|
|
421
681
|
let currentSignature = "";
|
|
422
682
|
let currentThinking = "";
|
|
423
683
|
let currentText = "";
|
|
424
684
|
let currentToolInputJson = "";
|
|
685
|
+
const citationsByBlockIndex: Record<number, any[]> = {};
|
|
425
686
|
|
|
426
687
|
for (const event of results) {
|
|
427
688
|
if (event.type === "message_start") {
|
|
428
689
|
response = { ...event.message };
|
|
429
690
|
} else if (event.type === "content_block_start") {
|
|
430
691
|
currentBlock = { ...event.content_block };
|
|
692
|
+
currentBlockIndex = "index" in event && typeof event.index === "number" ? event.index : null;
|
|
431
693
|
if (currentBlock.type === "thinking") {
|
|
432
694
|
currentSignature = "";
|
|
433
695
|
currentThinking = "";
|
|
@@ -439,25 +701,50 @@ export const anthropicStreamMessage = (results: MessageStreamEvent[]): Message =
|
|
|
439
701
|
) {
|
|
440
702
|
currentToolInputJson = "";
|
|
441
703
|
}
|
|
442
|
-
} else if (event.type === "content_block_delta"
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
704
|
+
} else if (event.type === "content_block_delta") {
|
|
705
|
+
const delta = event.delta as unknown as Record<string, unknown> | undefined;
|
|
706
|
+
const eventIndex = "index" in event && typeof event.index === "number" ? event.index : null;
|
|
707
|
+
|
|
708
|
+
if (delta?.type === "citations_delta") {
|
|
709
|
+
const citation = delta.citation as Record<string, unknown> | undefined;
|
|
710
|
+
if (
|
|
711
|
+
citation &&
|
|
712
|
+
typeof citation === "object" &&
|
|
713
|
+
citation.type === "web_search_result_location" &&
|
|
714
|
+
eventIndex !== null
|
|
715
|
+
) {
|
|
716
|
+
const annotation = {
|
|
717
|
+
type: "url_citation",
|
|
718
|
+
url: citation.url ?? "",
|
|
719
|
+
title: citation.title ?? "",
|
|
720
|
+
start_index: citation.start_index ?? 0,
|
|
721
|
+
end_index: citation.end_index ?? 0,
|
|
722
|
+
...(citation.cited_text != null ? { cited_text: citation.cited_text } : {}),
|
|
723
|
+
...(citation.encrypted_index != null ? { encrypted_index: citation.encrypted_index } : {}),
|
|
724
|
+
};
|
|
725
|
+
if (!citationsByBlockIndex[eventIndex]) citationsByBlockIndex[eventIndex] = [];
|
|
726
|
+
citationsByBlockIndex[eventIndex].push(annotation);
|
|
453
727
|
}
|
|
454
|
-
} else if (
|
|
455
|
-
currentBlock.type === "
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
728
|
+
} else if (currentBlock !== null) {
|
|
729
|
+
if (currentBlock.type === "thinking") {
|
|
730
|
+
if (delta && "signature" in delta) {
|
|
731
|
+
currentSignature = (delta.signature as string) || "";
|
|
732
|
+
}
|
|
733
|
+
if (delta && "thinking" in delta) {
|
|
734
|
+
currentThinking += (delta.thinking as string) || "";
|
|
735
|
+
}
|
|
736
|
+
} else if (currentBlock.type === "text") {
|
|
737
|
+
if (delta && "text" in delta) {
|
|
738
|
+
currentText += (delta.text as string) || "";
|
|
739
|
+
}
|
|
740
|
+
} else if (
|
|
741
|
+
currentBlock.type === "tool_use" ||
|
|
742
|
+
currentBlock.type === "server_tool_use"
|
|
743
|
+
) {
|
|
744
|
+
if (delta?.type === "input_json_delta") {
|
|
745
|
+
const inputJsonDelta = delta as { partial_json?: string };
|
|
746
|
+
currentToolInputJson += inputJsonDelta.partial_json || "";
|
|
747
|
+
}
|
|
461
748
|
}
|
|
462
749
|
}
|
|
463
750
|
} else if (event.type === "content_block_stop" && currentBlock !== null) {
|
|
@@ -467,6 +754,9 @@ export const anthropicStreamMessage = (results: MessageStreamEvent[]): Message =
|
|
|
467
754
|
} else if (currentBlock.type === "text") {
|
|
468
755
|
currentBlock.text = currentText;
|
|
469
756
|
currentBlock.citations = null;
|
|
757
|
+
if (currentBlockIndex !== null && citationsByBlockIndex[currentBlockIndex]?.length) {
|
|
758
|
+
currentBlock.annotations = citationsByBlockIndex[currentBlockIndex];
|
|
759
|
+
}
|
|
470
760
|
} else if (
|
|
471
761
|
currentBlock.type === "tool_use" ||
|
|
472
762
|
currentBlock.type === "server_tool_use"
|
|
@@ -481,6 +771,7 @@ export const anthropicStreamMessage = (results: MessageStreamEvent[]): Message =
|
|
|
481
771
|
}
|
|
482
772
|
response.content!.push(currentBlock);
|
|
483
773
|
currentBlock = null;
|
|
774
|
+
currentBlockIndex = null;
|
|
484
775
|
currentSignature = "";
|
|
485
776
|
currentThinking = "";
|
|
486
777
|
currentText = "";
|
|
@@ -687,38 +978,63 @@ const buildGoogleResponseFromParts = (
|
|
|
687
978
|
thoughtContent: string,
|
|
688
979
|
regularContent: string,
|
|
689
980
|
functionCalls: any[],
|
|
690
|
-
|
|
981
|
+
inlineDataParts: any[],
|
|
982
|
+
executableCodeParts: any[],
|
|
983
|
+
codeExecutionResultParts: any[],
|
|
984
|
+
lastResult: any,
|
|
985
|
+
lastThoughtSignature: string | null,
|
|
986
|
+
lastRegularThoughtSignature: string | null
|
|
691
987
|
) => {
|
|
692
988
|
const response = { ...lastResult };
|
|
693
|
-
const finalParts = [];
|
|
989
|
+
const finalParts: any[] = [];
|
|
694
990
|
|
|
695
991
|
if (thoughtContent) {
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
};
|
|
700
|
-
finalParts.push(thoughtPart);
|
|
992
|
+
const part: any = { text: thoughtContent, thought: true };
|
|
993
|
+
if (lastThoughtSignature) part.thoughtSignature = lastThoughtSignature;
|
|
994
|
+
finalParts.push(part);
|
|
701
995
|
}
|
|
702
996
|
|
|
703
997
|
if (regularContent) {
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
998
|
+
const part: any = { text: regularContent, thought: null };
|
|
999
|
+
if (lastRegularThoughtSignature) part.thoughtSignature = lastRegularThoughtSignature;
|
|
1000
|
+
finalParts.push(part);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
for (const executableCode of executableCodeParts) {
|
|
1004
|
+
finalParts.push({ executableCode });
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
for (const codeExecutionResult of codeExecutionResultParts) {
|
|
1008
|
+
finalParts.push({ codeExecutionResult });
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
for (const inlineData of inlineDataParts) {
|
|
1012
|
+
finalParts.push({ inlineData });
|
|
709
1013
|
}
|
|
710
1014
|
|
|
711
1015
|
for (const functionCall of functionCalls) {
|
|
712
|
-
|
|
713
|
-
function_call: functionCall,
|
|
714
|
-
};
|
|
715
|
-
finalParts.push(functionPart);
|
|
1016
|
+
finalParts.push({ functionCall });
|
|
716
1017
|
}
|
|
717
1018
|
|
|
718
1019
|
if (finalParts.length > 0 && response.candidates?.[0]?.content) {
|
|
719
1020
|
response.candidates[0].content.parts = finalParts;
|
|
720
1021
|
}
|
|
721
1022
|
|
|
1023
|
+
const lastCandidate = lastResult?.candidates?.[0];
|
|
1024
|
+
if (lastCandidate) {
|
|
1025
|
+
if (!response.candidates) response.candidates = [];
|
|
1026
|
+
if (!response.candidates[0]) response.candidates[0] = { content: { parts: [] } };
|
|
1027
|
+
if (lastCandidate.groundingMetadata != null) {
|
|
1028
|
+
response.candidates[0].groundingMetadata = lastCandidate.groundingMetadata;
|
|
1029
|
+
}
|
|
1030
|
+
if (lastCandidate.urlContextMetadata != null) {
|
|
1031
|
+
response.candidates[0].urlContextMetadata = lastCandidate.urlContextMetadata;
|
|
1032
|
+
}
|
|
1033
|
+
if (lastCandidate.citationMetadata != null) {
|
|
1034
|
+
response.candidates[0].citationMetadata = lastCandidate.citationMetadata;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
722
1038
|
return response;
|
|
723
1039
|
};
|
|
724
1040
|
|
|
@@ -732,18 +1048,41 @@ const googleStreamResponse = (results: any[]) => {
|
|
|
732
1048
|
let thoughtContent = "";
|
|
733
1049
|
let regularContent = "";
|
|
734
1050
|
const functionCalls: any[] = [];
|
|
1051
|
+
const inlineDataParts: any[] = [];
|
|
1052
|
+
const executableCodeParts: any[] = [];
|
|
1053
|
+
const codeExecutionResultParts: any[] = [];
|
|
1054
|
+
let lastThoughtSignature: string | null = null;
|
|
1055
|
+
let lastRegularThoughtSignature: string | null = null;
|
|
735
1056
|
|
|
736
1057
|
for (const result of results) {
|
|
737
1058
|
if (result.candidates && result.candidates[0]?.content?.parts) {
|
|
738
1059
|
for (const part of result.candidates[0].content.parts) {
|
|
739
|
-
if (part.text) {
|
|
1060
|
+
if (part.text != null) {
|
|
740
1061
|
if (part.thought === true) {
|
|
741
1062
|
thoughtContent += part.text;
|
|
1063
|
+
if (part.thoughtSignature) lastThoughtSignature = part.thoughtSignature;
|
|
742
1064
|
} else {
|
|
743
1065
|
regularContent += part.text;
|
|
1066
|
+
if (part.thoughtSignature) lastRegularThoughtSignature = part.thoughtSignature;
|
|
744
1067
|
}
|
|
745
1068
|
} else if (part.functionCall) {
|
|
746
1069
|
functionCalls.push(part.functionCall);
|
|
1070
|
+
} else if (part.inlineData) {
|
|
1071
|
+
const raw = part.inlineData;
|
|
1072
|
+
inlineDataParts.push({
|
|
1073
|
+
data: raw.data ?? "",
|
|
1074
|
+
mimeType: raw.mimeType ?? "image/png",
|
|
1075
|
+
});
|
|
1076
|
+
} else if (part.executableCode) {
|
|
1077
|
+
executableCodeParts.push({
|
|
1078
|
+
code: part.executableCode.code ?? "",
|
|
1079
|
+
language: part.executableCode.language,
|
|
1080
|
+
});
|
|
1081
|
+
} else if (part.codeExecutionResult) {
|
|
1082
|
+
codeExecutionResultParts.push({
|
|
1083
|
+
output: part.codeExecutionResult.output ?? "",
|
|
1084
|
+
outcome: part.codeExecutionResult.outcome ?? "OUTCOME_OK",
|
|
1085
|
+
});
|
|
747
1086
|
}
|
|
748
1087
|
}
|
|
749
1088
|
}
|
|
@@ -753,7 +1092,12 @@ const googleStreamResponse = (results: any[]) => {
|
|
|
753
1092
|
thoughtContent,
|
|
754
1093
|
regularContent,
|
|
755
1094
|
functionCalls,
|
|
756
|
-
|
|
1095
|
+
inlineDataParts,
|
|
1096
|
+
executableCodeParts,
|
|
1097
|
+
codeExecutionResultParts,
|
|
1098
|
+
results[results.length - 1],
|
|
1099
|
+
lastThoughtSignature,
|
|
1100
|
+
lastRegularThoughtSignature
|
|
757
1101
|
);
|
|
758
1102
|
};
|
|
759
1103
|
|
|
@@ -765,15 +1109,76 @@ export const googleStreamCompletion = (results: any[]) => {
|
|
|
765
1109
|
return googleStreamResponse(results);
|
|
766
1110
|
};
|
|
767
1111
|
|
|
1112
|
+
const _RESPONSE_METADATA_KEYS = ["size", "quality", "background", "output_format"] as const;
|
|
1113
|
+
|
|
1114
|
+
export const openaiImagesStream = (results: any[]) => {
|
|
1115
|
+
const response_data: any = {
|
|
1116
|
+
created: null,
|
|
1117
|
+
data: [],
|
|
1118
|
+
usage: null,
|
|
1119
|
+
};
|
|
1120
|
+
|
|
1121
|
+
const partial_images_by_index: Record<number, string> = {};
|
|
1122
|
+
let output_format = "png";
|
|
1123
|
+
|
|
1124
|
+
for (const chunk of results as any[]) {
|
|
1125
|
+
const event_type = chunk?.type ?? "";
|
|
1126
|
+
|
|
1127
|
+
if (event_type === "image_generation.partial_image") {
|
|
1128
|
+
const b64 = chunk.b64_json;
|
|
1129
|
+
if (b64 != null) {
|
|
1130
|
+
const idx = chunk.partial_image_index ?? 0;
|
|
1131
|
+
partial_images_by_index[idx] = b64;
|
|
1132
|
+
}
|
|
1133
|
+
} else if (event_type === "image_generation.completed") {
|
|
1134
|
+
const b64 = chunk.b64_json;
|
|
1135
|
+
if (b64 != null) {
|
|
1136
|
+
const idx = chunk.partial_image_index ?? 0;
|
|
1137
|
+
partial_images_by_index[idx] = b64;
|
|
1138
|
+
}
|
|
1139
|
+
response_data.created = chunk.created_at ?? response_data.created;
|
|
1140
|
+
response_data.usage = chunk.usage ?? response_data.usage;
|
|
1141
|
+
for (const key of _RESPONSE_METADATA_KEYS) {
|
|
1142
|
+
if (chunk[key] != null) response_data[key] = chunk[key];
|
|
1143
|
+
}
|
|
1144
|
+
if (chunk.output_format) output_format = chunk.output_format;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
if (!response_data.output_format) {
|
|
1149
|
+
response_data.output_format = output_format;
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
const indices = Object.keys(partial_images_by_index)
|
|
1153
|
+
.map(Number)
|
|
1154
|
+
.sort((a, b) => a - b);
|
|
1155
|
+
if (indices.length > 0) {
|
|
1156
|
+
response_data.data = indices.map((idx) => ({
|
|
1157
|
+
b64_json: partial_images_by_index[idx],
|
|
1158
|
+
}));
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
return response_data;
|
|
1162
|
+
};
|
|
1163
|
+
|
|
768
1164
|
export const cleaned_result = (
|
|
769
1165
|
results: any[],
|
|
770
1166
|
function_name = "openai.chat.completions.create"
|
|
771
1167
|
) => {
|
|
772
|
-
|
|
773
|
-
|
|
1168
|
+
if (
|
|
1169
|
+
function_name === "openai.responses.create" ||
|
|
1170
|
+
function_name === "openai.AzureOpenAI.responses.create"
|
|
1171
|
+
) {
|
|
774
1172
|
return openaiResponsesStreamChat(results);
|
|
775
1173
|
}
|
|
776
1174
|
|
|
1175
|
+
if (
|
|
1176
|
+
function_name === "openai.images.generate" ||
|
|
1177
|
+
function_name === "openai.AzureOpenAI.images.generate"
|
|
1178
|
+
) {
|
|
1179
|
+
return openaiImagesStream(results);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
777
1182
|
if ("completion" in results[0]) {
|
|
778
1183
|
return results.reduce(
|
|
779
1184
|
(prev, current) => ({
|
|
@@ -810,12 +1215,16 @@ export const cleaned_result = (
|
|
|
810
1215
|
};
|
|
811
1216
|
|
|
812
1217
|
|
|
813
|
-
const buildStreamBlueprint = (
|
|
1218
|
+
const buildStreamBlueprint = (
|
|
1219
|
+
result: any,
|
|
1220
|
+
metadata: any,
|
|
1221
|
+
streamContext?: { anthropicBlockTypeByIndex?: Record<number, string> }
|
|
1222
|
+
) => {
|
|
814
1223
|
const provider = metadata.model.provider;
|
|
815
1224
|
const model = metadata.model.name;
|
|
816
1225
|
|
|
817
1226
|
if (provider === "anthropic" || provider === "anthropic.bedrock" || (provider === "vertexai" && model.startsWith("claude"))) {
|
|
818
|
-
return buildPromptBlueprintFromAnthropicEvent(result, metadata);
|
|
1227
|
+
return buildPromptBlueprintFromAnthropicEvent(result, metadata, streamContext?.anthropicBlockTypeByIndex);
|
|
819
1228
|
}
|
|
820
1229
|
|
|
821
1230
|
if (provider === "google" || (provider === "vertexai" && model.startsWith("gemini"))) {
|
|
@@ -835,6 +1244,9 @@ const buildStreamBlueprint = (result: any, metadata: any) => {
|
|
|
835
1244
|
if (api_type === "responses") {
|
|
836
1245
|
return buildPromptBlueprintFromOpenAIResponsesEvent(result, metadata);
|
|
837
1246
|
}
|
|
1247
|
+
if (api_type === "images") {
|
|
1248
|
+
return buildPromptBlueprintFromOpenAIImagesEvent(result, metadata);
|
|
1249
|
+
}
|
|
838
1250
|
return buildPromptBlueprintFromOpenAIEvent(result, metadata);
|
|
839
1251
|
}
|
|
840
1252
|
|
|
@@ -863,10 +1275,15 @@ export async function* streamResponse<Item>(
|
|
|
863
1275
|
generator = generator?.stream;
|
|
864
1276
|
}
|
|
865
1277
|
const results = [];
|
|
1278
|
+
const isAnthropic = provider === "anthropic" || provider === "anthropic.bedrock" || (provider === "vertexai" && metadata.model?.name?.startsWith?.("claude"));
|
|
1279
|
+
const anthropicBlockTypeByIndex: Record<number, string> = {};
|
|
866
1280
|
for await (const result of generator) {
|
|
867
1281
|
results.push(result);
|
|
1282
|
+
if (isAnthropic && result?.type === "content_block_start" && result.content_block) {
|
|
1283
|
+
anthropicBlockTypeByIndex[result.index] = result.content_block.type;
|
|
1284
|
+
}
|
|
868
1285
|
data.raw_response = result;
|
|
869
|
-
data.prompt_blueprint = buildStreamBlueprint(result, metadata);
|
|
1286
|
+
data.prompt_blueprint = buildStreamBlueprint(result, metadata, { anthropicBlockTypeByIndex });
|
|
870
1287
|
|
|
871
1288
|
yield data;
|
|
872
1289
|
}
|
|
@@ -874,6 +1291,9 @@ export async function* streamResponse<Item>(
|
|
|
874
1291
|
if (provider === "amazon.bedrock") {
|
|
875
1292
|
request_response.ResponseMetadata = response_metadata;
|
|
876
1293
|
}
|
|
1294
|
+
data.raw_response = request_response;
|
|
1295
|
+
data.prompt_blueprint = buildStreamBlueprint(request_response, metadata);
|
|
1296
|
+
yield data;
|
|
877
1297
|
const response = await afterStream({ request_response });
|
|
878
1298
|
data.request_id = response.request_id;
|
|
879
1299
|
data.prompt_blueprint = response.prompt_blueprint;
|
|
@@ -896,9 +1316,11 @@ export const MAP_PROVIDER_TO_FUNCTION_NAME = {
|
|
|
896
1316
|
function_name: "openai.responses.create",
|
|
897
1317
|
stream_function: openaiResponsesStreamChat,
|
|
898
1318
|
},
|
|
1319
|
+
},
|
|
1320
|
+
"openai:images": {
|
|
899
1321
|
completion: {
|
|
900
|
-
function_name: "openai.
|
|
901
|
-
stream_function:
|
|
1322
|
+
function_name: "openai.images.generate",
|
|
1323
|
+
stream_function: openaiImagesStream,
|
|
902
1324
|
},
|
|
903
1325
|
},
|
|
904
1326
|
anthropic: {
|
|
@@ -931,6 +1353,12 @@ export const MAP_PROVIDER_TO_FUNCTION_NAME = {
|
|
|
931
1353
|
stream_function: openaiResponsesStreamChat,
|
|
932
1354
|
},
|
|
933
1355
|
},
|
|
1356
|
+
"openai.azure:images": {
|
|
1357
|
+
completion: {
|
|
1358
|
+
function_name: "openai.AzureOpenAI.images.generate",
|
|
1359
|
+
stream_function: openaiImagesStream,
|
|
1360
|
+
},
|
|
1361
|
+
},
|
|
934
1362
|
google: {
|
|
935
1363
|
chat: {
|
|
936
1364
|
function_name: "google.convo.send_message",
|