promptlayer 1.1.0 → 1.2.1

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.
Files changed (64) hide show
  1. package/README.md +22 -0
  2. package/dist/claude-agents.d.mts +20 -0
  3. package/dist/claude-agents.d.ts +20 -0
  4. package/dist/claude-agents.js +2 -0
  5. package/dist/claude-agents.js.map +1 -0
  6. package/dist/esm/{chunk-SWBNW72U.js → chunk-UKSCOWKT.js} +2 -2
  7. package/dist/esm/{chunk-SWBNW72U.js.map → chunk-UKSCOWKT.js.map} +1 -1
  8. package/dist/esm/claude-agents.js +2 -0
  9. package/dist/esm/claude-agents.js.map +1 -0
  10. package/dist/esm/index.js +1 -1
  11. package/dist/esm/openai-agents.js +2 -2
  12. package/dist/esm/openai-agents.js.map +1 -1
  13. package/dist/index.js +2 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/openai-agents.js +2 -2
  16. package/dist/openai-agents.js.map +1 -1
  17. package/package.json +18 -1
  18. package/vendor/claude-agents/trace/.claude-plugin/plugin.json +8 -0
  19. package/vendor/claude-agents/trace/hooks/hook_utils.py +38 -0
  20. package/vendor/claude-agents/trace/hooks/hooks.json +60 -0
  21. package/vendor/claude-agents/trace/hooks/lib.sh +577 -0
  22. package/vendor/claude-agents/trace/hooks/parse_stop_transcript.py +375 -0
  23. package/vendor/claude-agents/trace/hooks/post_tool_use.sh +41 -0
  24. package/vendor/claude-agents/trace/hooks/session_end.sh +37 -0
  25. package/vendor/claude-agents/trace/hooks/session_start.sh +57 -0
  26. package/vendor/claude-agents/trace/hooks/stop_hook.sh +123 -0
  27. package/vendor/claude-agents/trace/hooks/user_prompt_submit.sh +25 -0
  28. package/vendor/claude-agents/vendor_metadata.json +5 -0
  29. package/.github/CODEOWNERS +0 -1
  30. package/.github/workflows/node.js.yml +0 -30
  31. package/.github/workflows/npm-publish.yml +0 -35
  32. package/src/groups.ts +0 -16
  33. package/src/index.ts +0 -383
  34. package/src/integrations/openai-agents/helpers.test.ts +0 -254
  35. package/src/integrations/openai-agents/ids.ts +0 -27
  36. package/src/integrations/openai-agents/index.ts +0 -8
  37. package/src/integrations/openai-agents/instrumentation.test.ts +0 -46
  38. package/src/integrations/openai-agents/instrumentation.ts +0 -47
  39. package/src/integrations/openai-agents/mapping.ts +0 -714
  40. package/src/integrations/openai-agents/otlp-json.ts +0 -120
  41. package/src/integrations/openai-agents/processor.test.ts +0 -509
  42. package/src/integrations/openai-agents/processor.ts +0 -388
  43. package/src/integrations/openai-agents/time.ts +0 -56
  44. package/src/integrations/openai-agents/types.ts +0 -49
  45. package/src/integrations/openai-agents/url.ts +0 -9
  46. package/src/openai-agents.ts +0 -1
  47. package/src/promptlayer.ts +0 -125
  48. package/src/run-error-tracking.test.ts +0 -146
  49. package/src/span-exporter.ts +0 -120
  50. package/src/span-wrapper.ts +0 -51
  51. package/src/templates.ts +0 -37
  52. package/src/tracing.ts +0 -20
  53. package/src/track.ts +0 -84
  54. package/src/types.ts +0 -689
  55. package/src/utils/blueprint-builder.test.ts +0 -727
  56. package/src/utils/blueprint-builder.ts +0 -1453
  57. package/src/utils/errors.test.ts +0 -68
  58. package/src/utils/errors.ts +0 -62
  59. package/src/utils/streaming.test.ts +0 -498
  60. package/src/utils/streaming.ts +0 -1402
  61. package/src/utils/utils.ts +0 -1228
  62. package/tsconfig.json +0 -115
  63. package/tsup.config.ts +0 -20
  64. package/vitest.config.ts +0 -9
@@ -1,727 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import {
3
- buildPromptBlueprintFromAnthropicEvent,
4
- buildPromptBlueprintFromGoogleEvent,
5
- buildPromptBlueprintFromOpenAIEvent,
6
- buildPromptBlueprintFromOpenAIResponsesEvent,
7
- buildPromptBlueprintFromBedrockEvent,
8
- buildPromptBlueprintFromOpenAIImagesEvent,
9
- } from "@/utils/blueprint-builder";
10
- import type { Metadata } from "@/types";
11
-
12
- const metadata: Metadata = {
13
- model: { provider: "anthropic", name: "claude-sonnet-4-6" },
14
- };
15
-
16
- describe("buildPromptBlueprintFromAnthropicEvent", () => {
17
- describe("merged message (event.content array)", () => {
18
- it("maps server_tool_use block to content", () => {
19
- const event = {
20
- content: [
21
- {
22
- type: "server_tool_use",
23
- id: "srvtoolu_01",
24
- name: "bash_code_execution",
25
- input: { command: "echo hi" },
26
- },
27
- ],
28
- };
29
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
30
- expect(blueprint.prompt_template.messages).toHaveLength(1);
31
- const msg = blueprint.prompt_template.messages[0];
32
- expect(msg.role).toBe("assistant");
33
- expect(msg.content).toHaveLength(1);
34
- expect(msg.content![0]).toMatchObject({
35
- type: "server_tool_use",
36
- id: "srvtoolu_01",
37
- name: "bash_code_execution",
38
- input: { command: "echo hi" },
39
- });
40
- expect(msg.tool_calls).toEqual([]);
41
- });
42
-
43
- it("maps web_search_tool_result block to content", () => {
44
- const event = {
45
- content: [
46
- {
47
- type: "web_search_tool_result",
48
- tool_use_id: "srvtoolu_02",
49
- content: [
50
- {
51
- type: "web_search_result",
52
- url: "https://example.com",
53
- title: "Example",
54
- encrypted_content: "enc",
55
- page_age: "1 day",
56
- },
57
- ],
58
- },
59
- ],
60
- };
61
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
62
- const msg = blueprint.prompt_template.messages[0];
63
- expect(msg.content).toHaveLength(1);
64
- expect(msg.content![0]).toMatchObject({
65
- type: "web_search_tool_result",
66
- tool_use_id: "srvtoolu_02",
67
- content: [
68
- {
69
- type: "web_search_result",
70
- url: "https://example.com",
71
- title: "Example",
72
- encrypted_content: "enc",
73
- page_age: "1 day",
74
- },
75
- ],
76
- });
77
- });
78
-
79
- it("maps bash_code_execution_tool_result block to content", () => {
80
- const event = {
81
- content: [
82
- {
83
- type: "bash_code_execution_tool_result",
84
- tool_use_id: "srvtoolu_01F9YUsLV5DCRx2JbnBXL1hL",
85
- content: {
86
- type: "bash_code_execution_result",
87
- stdout: "Hello\n",
88
- stderr: "",
89
- return_code: 0,
90
- content: [],
91
- },
92
- },
93
- ],
94
- };
95
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
96
- const msg = blueprint.prompt_template.messages[0];
97
- expect(msg.content).toHaveLength(1);
98
- expect(msg.content![0]).toMatchObject({
99
- type: "bash_code_execution_tool_result",
100
- tool_use_id: "srvtoolu_01F9YUsLV5DCRx2JbnBXL1hL",
101
- content: {
102
- type: "bash_code_execution_result",
103
- stdout: "Hello\n",
104
- stderr: "",
105
- return_code: 0,
106
- content: [],
107
- },
108
- });
109
- });
110
-
111
- it("maps text block with annotations (citations) to content", () => {
112
- const event = {
113
- content: [
114
- {
115
- type: "text",
116
- text: "Some cited text.",
117
- annotations: [
118
- {
119
- url: "https://example.com",
120
- title: "Example",
121
- cited_text: "cited",
122
- start_index: 0,
123
- end_index: 14,
124
- },
125
- ],
126
- },
127
- ],
128
- };
129
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
130
- const msg = blueprint.prompt_template.messages[0];
131
- expect(msg.content).toHaveLength(1);
132
- expect(msg.content![0]).toMatchObject({
133
- type: "text",
134
- text: "Some cited text.",
135
- annotations: [
136
- expect.objectContaining({
137
- type: "url_citation",
138
- url: "https://example.com",
139
- title: "Example",
140
- cited_text: "cited",
141
- }),
142
- ],
143
- });
144
- });
145
- });
146
-
147
- describe("content_block_start (single stream event)", () => {
148
- it("maps server_tool_use content_block_start to server_tool_use content", () => {
149
- const event = {
150
- type: "content_block_start",
151
- index: 0,
152
- content_block: {
153
- type: "server_tool_use",
154
- id: "srvtoolu_01",
155
- name: "bash_code_execution",
156
- input: {},
157
- },
158
- };
159
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
160
- const msg = blueprint.prompt_template.messages[0];
161
- expect(msg.content).toHaveLength(1);
162
- expect(msg.content![0]).toMatchObject({
163
- type: "server_tool_use",
164
- id: "srvtoolu_01",
165
- name: "bash_code_execution",
166
- input: {},
167
- });
168
- });
169
-
170
- it("maps bash_code_execution_tool_result content_block_start to content", () => {
171
- const event = {
172
- type: "content_block_start",
173
- index: 1,
174
- content_block: {
175
- type: "bash_code_execution_tool_result",
176
- tool_use_id: "srvtoolu_01",
177
- content: {
178
- type: "bash_code_execution_result",
179
- stdout: "First 10 primes\n",
180
- stderr: "",
181
- return_code: 0,
182
- content: [],
183
- },
184
- },
185
- };
186
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
187
- const msg = blueprint.prompt_template.messages[0];
188
- expect(msg.content).toHaveLength(1);
189
- expect(msg.content![0]).toMatchObject({
190
- type: "bash_code_execution_tool_result",
191
- tool_use_id: "srvtoolu_01",
192
- content: {
193
- type: "bash_code_execution_result",
194
- stdout: "First 10 primes\n",
195
- stderr: "",
196
- return_code: 0,
197
- content: [],
198
- },
199
- });
200
- });
201
-
202
- it("maps web_search_tool_result content_block_start to content", () => {
203
- const event = {
204
- type: "content_block_start",
205
- index: 0,
206
- content_block: {
207
- type: "web_search_tool_result",
208
- tool_use_id: "srvtoolu_02",
209
- content: [{ type: "web_search_result", url: "https://a.com", title: "A" }],
210
- },
211
- };
212
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
213
- const msg = blueprint.prompt_template.messages[0];
214
- expect(msg.content).toHaveLength(1);
215
- expect(msg.content![0]).toMatchObject({
216
- type: "web_search_tool_result",
217
- tool_use_id: "srvtoolu_02",
218
- content: [{ type: "web_search_result", url: "https://a.com", title: "A" }],
219
- });
220
- });
221
-
222
- it("maps tool_use content_block_start to tool_calls", () => {
223
- const event = {
224
- type: "content_block_start",
225
- index: 0,
226
- content_block: {
227
- type: "tool_use",
228
- id: "toolu_01",
229
- name: "get_weather",
230
- input: {},
231
- },
232
- };
233
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
234
- const msg = blueprint.prompt_template.messages[0];
235
- expect(msg.tool_calls).toHaveLength(1);
236
- expect(msg.tool_calls![0]).toMatchObject({
237
- id: "toolu_01",
238
- type: "function",
239
- function: { name: "get_weather", arguments: "{}" },
240
- });
241
- });
242
- });
243
-
244
- describe("content_block_delta", () => {
245
- it("maps input_json_delta with blockTypeByIndex server_tool_use to server_tool_use content", () => {
246
- const event = {
247
- type: "content_block_delta",
248
- index: 0,
249
- delta: {
250
- type: "input_json_delta",
251
- partial_json: '{"command":"echo hi"}',
252
- },
253
- };
254
- const blockTypeByIndex: Record<number, string> = { 0: "server_tool_use" };
255
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata, blockTypeByIndex);
256
- const msg = blueprint.prompt_template.messages[0];
257
- expect(msg.content).toHaveLength(1);
258
- expect(msg.content![0]).toMatchObject({
259
- type: "server_tool_use",
260
- id: "",
261
- name: "",
262
- input: '{"command":"echo hi"}',
263
- });
264
- expect(msg.tool_calls).toEqual([]);
265
- });
266
-
267
- it("maps input_json_delta with blockTypeByIndex tool_use to tool_calls", () => {
268
- const event = {
269
- type: "content_block_delta",
270
- index: 0,
271
- delta: {
272
- type: "input_json_delta",
273
- partial_json: '{"location":"NYC"}',
274
- },
275
- };
276
- const blockTypeByIndex: Record<number, string> = { 0: "tool_use" };
277
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata, blockTypeByIndex);
278
- const msg = blueprint.prompt_template.messages[0];
279
- expect(msg.tool_calls).toHaveLength(1);
280
- expect(msg.tool_calls![0]).toMatchObject({
281
- id: "",
282
- type: "function",
283
- function: { name: "", arguments: '{"location":"NYC"}' },
284
- });
285
- expect(msg.content).toEqual([]);
286
- });
287
-
288
- it("maps input_json_delta without blockTypeByIndex to tool_calls (default)", () => {
289
- const event = {
290
- type: "content_block_delta",
291
- index: 0,
292
- delta: { type: "input_json_delta", partial_json: "{}" },
293
- };
294
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
295
- const msg = blueprint.prompt_template.messages[0];
296
- expect(msg.tool_calls).toHaveLength(1);
297
- expect(msg.content).toEqual([]);
298
- });
299
-
300
- it("maps citations_delta to text content with annotations", () => {
301
- const event = {
302
- type: "content_block_delta",
303
- index: 3,
304
- delta: {
305
- type: "citations_delta",
306
- citation: {
307
- type: "web_search_result_location",
308
- cited_text: "The USD/PKR rate fell to 279.25.",
309
- url: "https://tradingeconomics.com/pakistan/currency",
310
- title: "Pakistan Rupee - News",
311
- encrypted_index: "Eo8BCioI...",
312
- },
313
- },
314
- };
315
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
316
- const msg = blueprint.prompt_template.messages[0];
317
- expect(msg.content).toHaveLength(1);
318
- expect(msg.content![0]).toMatchObject({
319
- type: "text",
320
- text: "",
321
- annotations: [
322
- expect.objectContaining({
323
- type: "url_citation",
324
- url: "https://tradingeconomics.com/pakistan/currency",
325
- title: "Pakistan Rupee - News",
326
- cited_text: "The USD/PKR rate fell to 279.25.",
327
- encrypted_index: "Eo8BCioI...",
328
- }),
329
- ],
330
- });
331
- });
332
-
333
- it("maps text_delta to text content", () => {
334
- const event = {
335
- type: "content_block_delta",
336
- index: 0,
337
- delta: { type: "text_delta", text: "Hello " },
338
- };
339
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
340
- const msg = blueprint.prompt_template.messages[0];
341
- expect(msg.content).toHaveLength(1);
342
- expect(msg.content![0]).toEqual({ type: "text", text: "Hello " });
343
- });
344
-
345
- it("maps thinking_delta to thinking content", () => {
346
- const event = {
347
- type: "content_block_delta",
348
- index: 0,
349
- delta: { type: "thinking_delta", thinking: "Let me check." },
350
- };
351
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
352
- const msg = blueprint.prompt_template.messages[0];
353
- expect(msg.content).toHaveLength(1);
354
- expect(msg.content![0]).toMatchObject({
355
- type: "thinking",
356
- thinking: "Let me check.",
357
- signature: "",
358
- });
359
- });
360
-
361
- it("maps signature_delta to thinking content with signature", () => {
362
- const event = {
363
- type: "content_block_delta",
364
- index: 0,
365
- delta: { type: "signature_delta", signature: "sig123" },
366
- };
367
- const blueprint = buildPromptBlueprintFromAnthropicEvent(event, metadata);
368
- const msg = blueprint.prompt_template.messages[0];
369
- expect(msg.content).toHaveLength(1);
370
- expect(msg.content![0]).toMatchObject({
371
- type: "thinking",
372
- thinking: "",
373
- signature: "sig123",
374
- });
375
- });
376
- });
377
- });
378
-
379
- // --- Google (Gemini) ---
380
- const googleMetadata: Metadata = {
381
- model: { provider: "google", name: "gemini-2.0-flash" },
382
- };
383
-
384
- describe("buildPromptBlueprintFromGoogleEvent", () => {
385
- it("maps candidates with text part to text content", () => {
386
- const event = {
387
- candidates: [
388
- {
389
- content: {
390
- parts: [{ text: "Hello from Gemini" }],
391
- },
392
- },
393
- ],
394
- };
395
- const blueprint = buildPromptBlueprintFromGoogleEvent(event, googleMetadata);
396
- expect(blueprint.prompt_template.messages).toHaveLength(1);
397
- const msg = blueprint.prompt_template.messages[0];
398
- expect(msg.content).toHaveLength(1);
399
- expect(msg.content![0]).toMatchObject({ type: "text", text: "Hello from Gemini" });
400
- });
401
-
402
- it("maps thought part to thinking content", () => {
403
- const event = {
404
- candidates: [
405
- {
406
- content: {
407
- parts: [{ text: "Reasoning step.", thought: true, thoughtSignature: "sig1" }],
408
- },
409
- },
410
- ],
411
- };
412
- const blueprint = buildPromptBlueprintFromGoogleEvent(event, googleMetadata);
413
- const msg = blueprint.prompt_template.messages[0];
414
- expect(msg.content).toHaveLength(1);
415
- expect(msg.content![0]).toMatchObject({
416
- type: "thinking",
417
- thinking: "Reasoning step.",
418
- signature: "sig1",
419
- });
420
- });
421
-
422
- it("maps functionCall part to tool_calls", () => {
423
- const event = {
424
- candidates: [
425
- {
426
- content: {
427
- parts: [
428
- {
429
- functionCall: {
430
- id: "fc_01",
431
- name: "get_weather",
432
- args: { location: "NYC" },
433
- },
434
- },
435
- ],
436
- },
437
- },
438
- ],
439
- };
440
- const blueprint = buildPromptBlueprintFromGoogleEvent(event, googleMetadata);
441
- const msg = blueprint.prompt_template.messages[0];
442
- expect(msg.tool_calls).toHaveLength(1);
443
- expect(msg.tool_calls![0]).toMatchObject({
444
- id: "fc_01",
445
- function: { name: "get_weather", arguments: '{"location":"NYC"}' },
446
- });
447
- });
448
-
449
- it("maps executableCode and codeExecutionResult to content", () => {
450
- const event = {
451
- candidates: [
452
- {
453
- content: {
454
- parts: [
455
- { executableCode: { code: "print(1+1)", language: "python" } },
456
- { codeExecutionResult: { output: "2", outcome: "OUTCOME_OK" } },
457
- ],
458
- },
459
- },
460
- ],
461
- };
462
- const blueprint = buildPromptBlueprintFromGoogleEvent(event, googleMetadata);
463
- const msg = blueprint.prompt_template.messages[0];
464
- expect(msg.content).toHaveLength(2);
465
- expect(msg.content![0]).toMatchObject({ type: "code", code: "print(1+1)", language: "python" });
466
- expect(msg.content![1]).toMatchObject({
467
- type: "code_execution_result",
468
- output: "2",
469
- outcome: "OUTCOME_OK",
470
- });
471
- });
472
- });
473
-
474
- // --- OpenAI (chat-completions) ---
475
- const openaiMetadata: Metadata = {
476
- model: { provider: "openai", name: "gpt-4o" },
477
- };
478
-
479
- describe("buildPromptBlueprintFromOpenAIEvent", () => {
480
- it("maps choices[].delta.content to text content", () => {
481
- const event = {
482
- choices: [{ delta: { content: "Hello" } }],
483
- };
484
- const blueprint = buildPromptBlueprintFromOpenAIEvent(event, openaiMetadata);
485
- const msg = blueprint.prompt_template.messages[0];
486
- expect(msg.content).toHaveLength(1);
487
- expect(msg.content![0]).toMatchObject({ type: "text", text: "Hello" });
488
- });
489
-
490
- it("maps choices[].delta.tool_calls to tool_calls", () => {
491
- const event = {
492
- choices: [
493
- {
494
- delta: {
495
- tool_calls: [
496
- {
497
- id: "call_01",
498
- function: { name: "get_weather", arguments: '{"city":"Boston"}' },
499
- },
500
- ],
501
- },
502
- },
503
- ],
504
- };
505
- const blueprint = buildPromptBlueprintFromOpenAIEvent(event, openaiMetadata);
506
- const msg = blueprint.prompt_template.messages[0];
507
- expect(msg.tool_calls).toHaveLength(1);
508
- expect(msg.tool_calls![0]).toMatchObject({
509
- id: "call_01",
510
- function: { name: "get_weather", arguments: '{"city":"Boston"}' },
511
- });
512
- });
513
-
514
- it("handles toolCalls (camelCase) alias", () => {
515
- const event = {
516
- choices: [
517
- {
518
- delta: {
519
- toolCalls: [
520
- { id: "call_02", function: { name: "search", arguments: "{}" } },
521
- ],
522
- },
523
- },
524
- ],
525
- };
526
- const blueprint = buildPromptBlueprintFromOpenAIEvent(event, openaiMetadata);
527
- const msg = blueprint.prompt_template.messages[0];
528
- expect(msg.tool_calls).toHaveLength(1);
529
- expect(msg.tool_calls![0].function.name).toBe("search");
530
- });
531
- });
532
-
533
- // --- OpenAI Responses API ---
534
- const openaiResponsesMetadata: Metadata = {
535
- model: { provider: "openai", name: "gpt-4o", api_type: "responses" },
536
- };
537
-
538
- describe("buildPromptBlueprintFromOpenAIResponsesEvent", () => {
539
- it("maps response.reasoning_summary_text.delta to thinking content", () => {
540
- const event = {
541
- type: "response.reasoning_summary_text.delta",
542
- item_id: "item_1",
543
- delta: "Some reasoning.",
544
- };
545
- const blueprint = buildPromptBlueprintFromOpenAIResponsesEvent(event, openaiResponsesMetadata);
546
- const msg = blueprint.prompt_template.messages[0];
547
- expect(msg.content).toHaveLength(1);
548
- expect(msg.content![0]).toMatchObject({
549
- type: "thinking",
550
- id: "item_1",
551
- thinking: "Some reasoning.",
552
- signature: "",
553
- });
554
- });
555
-
556
- it("maps response.output_text.delta to text content", () => {
557
- const event = {
558
- type: "response.output_text.delta",
559
- item_id: "msg_01",
560
- delta: "Hello world",
561
- };
562
- const blueprint = buildPromptBlueprintFromOpenAIResponsesEvent(event, openaiResponsesMetadata);
563
- const msg = blueprint.prompt_template.messages[0];
564
- expect(msg.content).toHaveLength(1);
565
- expect(msg.content![0]).toMatchObject({ type: "text", id: "msg_01", text: "Hello world" });
566
- });
567
-
568
- it("maps response.output_item.added (message) to text content", () => {
569
- const event = {
570
- type: "response.output_item.added",
571
- item: { type: "message", id: "msg_01" },
572
- item_id: "msg_01",
573
- };
574
- const blueprint = buildPromptBlueprintFromOpenAIResponsesEvent(event, openaiResponsesMetadata);
575
- const msg = blueprint.prompt_template.messages[0];
576
- expect(msg.content).toHaveLength(1);
577
- expect(msg.content![0]).toMatchObject({ type: "text", id: "msg_01", text: "" });
578
- });
579
-
580
- it("maps response.output_item.added (function_call) to tool_calls", () => {
581
- const event = {
582
- type: "response.output_item.added",
583
- item: { type: "function_call", id: "fc_01", call_id: "call_01", name: "get_weather" },
584
- item_id: "fc_01",
585
- };
586
- const blueprint = buildPromptBlueprintFromOpenAIResponsesEvent(event, openaiResponsesMetadata);
587
- const msg = blueprint.prompt_template.messages[0];
588
- expect(msg.tool_calls).toHaveLength(1);
589
- expect(msg.tool_calls![0]).toMatchObject({
590
- id: "call_01",
591
- function: { name: "get_weather", arguments: "" },
592
- tool_id: "fc_01",
593
- });
594
- });
595
-
596
- it("maps response.function_call_arguments.done to tool_calls", () => {
597
- const event = {
598
- type: "response.function_call_arguments.done",
599
- item_id: "fc_01",
600
- arguments: '{"location":"NYC"}',
601
- };
602
- const blueprint = buildPromptBlueprintFromOpenAIResponsesEvent(event, openaiResponsesMetadata);
603
- const msg = blueprint.prompt_template.messages[0];
604
- expect(msg.tool_calls).toHaveLength(1);
605
- expect(msg.tool_calls![0].function.arguments).toBe('{"location":"NYC"}');
606
- });
607
- });
608
-
609
- // --- Amazon Bedrock ---
610
- const bedrockMetadata: Metadata = {
611
- model: { provider: "amazon.bedrock", name: "anthropic.claude-3-sonnet" },
612
- };
613
-
614
- describe("buildPromptBlueprintFromBedrockEvent", () => {
615
- it("maps contentBlockDelta with text to text content", () => {
616
- const event = {
617
- contentBlockDelta: { delta: { text: "Hello from Bedrock" } },
618
- };
619
- const blueprint = buildPromptBlueprintFromBedrockEvent(event, bedrockMetadata);
620
- const msg = blueprint.prompt_template.messages[0];
621
- expect(msg.content).toHaveLength(1);
622
- expect(msg.content![0]).toMatchObject({ type: "text", text: "Hello from Bedrock" });
623
- });
624
-
625
- it("maps contentBlockDelta with reasoningContent to thinking content", () => {
626
- const event = {
627
- contentBlockDelta: {
628
- delta: {
629
- reasoningContent: { text: "Thinking...", signature: "sig123" },
630
- },
631
- },
632
- };
633
- const blueprint = buildPromptBlueprintFromBedrockEvent(event, bedrockMetadata);
634
- const msg = blueprint.prompt_template.messages[0];
635
- expect(msg.content).toHaveLength(1);
636
- expect(msg.content![0]).toMatchObject({
637
- type: "thinking",
638
- thinking: "Thinking...",
639
- signature: "sig123",
640
- });
641
- });
642
-
643
- it("maps contentBlockDelta with toolUse to tool_calls", () => {
644
- const event = {
645
- contentBlockDelta: {
646
- delta: {
647
- toolUse: {
648
- toolUseId: "toolu_01",
649
- name: "search",
650
- input: '{"query":"test"}',
651
- },
652
- },
653
- },
654
- };
655
- const blueprint = buildPromptBlueprintFromBedrockEvent(event, bedrockMetadata);
656
- const msg = blueprint.prompt_template.messages[0];
657
- expect(msg.tool_calls).toHaveLength(1);
658
- expect(msg.tool_calls![0]).toMatchObject({
659
- id: "toolu_01",
660
- function: { name: "search", arguments: '{"query":"test"}' },
661
- });
662
- });
663
-
664
- it("maps contentBlockStart with toolUse to tool_calls", () => {
665
- const event = {
666
- contentBlockStart: {
667
- start: {
668
- toolUse: { toolUseId: "toolu_02", name: "get_weather" },
669
- },
670
- },
671
- };
672
- const blueprint = buildPromptBlueprintFromBedrockEvent(event, bedrockMetadata);
673
- const msg = blueprint.prompt_template.messages[0];
674
- expect(msg.tool_calls).toHaveLength(1);
675
- expect(msg.tool_calls![0]).toMatchObject({
676
- id: "toolu_02",
677
- function: { name: "get_weather", arguments: "" },
678
- });
679
- });
680
- });
681
-
682
- // --- OpenAI Images ---
683
- const openaiImagesMetadata: Metadata = {
684
- model: { provider: "openai", name: "dall-e-3", api_type: "images" },
685
- };
686
-
687
- describe("buildPromptBlueprintFromOpenAIImagesEvent", () => {
688
- it("maps image_generation.partial_image to output_media content", () => {
689
- const event = {
690
- type: "image_generation.partial_image",
691
- b64_json: "base64encodeddata",
692
- output_format: "png",
693
- partial_image_index: 0,
694
- };
695
- const blueprint = buildPromptBlueprintFromOpenAIImagesEvent(event, openaiImagesMetadata);
696
- expect(blueprint.prompt_template.type).toBe("completion");
697
- const content = (blueprint.prompt_template as { content: unknown[] }).content;
698
- expect(content).toHaveLength(1);
699
- expect(content[0]).toMatchObject({
700
- type: "output_media",
701
- url: "base64encodeddata",
702
- mime_type: "image/png",
703
- media_type: "image",
704
- provider_metadata: { partial_image_index: 0 },
705
- });
706
- });
707
-
708
- it("maps image_generation.completed to output_media content", () => {
709
- const event = {
710
- type: "image_generation.completed",
711
- b64_json: "finalbase64",
712
- output_format: "webp",
713
- size: "1024x1024",
714
- quality: "hd",
715
- };
716
- const blueprint = buildPromptBlueprintFromOpenAIImagesEvent(event, openaiImagesMetadata);
717
- const content = (blueprint.prompt_template as { content: unknown[] }).content;
718
- expect(content).toHaveLength(1);
719
- expect(content[0]).toMatchObject({
720
- type: "output_media",
721
- url: "finalbase64",
722
- mime_type: "image/webp",
723
- media_type: "image",
724
- provider_metadata: expect.objectContaining({ size: "1024x1024", quality: "hd" }),
725
- });
726
- });
727
- });