@copilotkit/aimock 1.16.1 → 1.16.3
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/CHANGELOG.md +22 -0
- package/dist/bedrock-converse.cjs +193 -15
- package/dist/bedrock-converse.cjs.map +1 -1
- package/dist/bedrock-converse.d.cts +2 -1
- package/dist/bedrock-converse.d.cts.map +1 -1
- package/dist/bedrock-converse.d.ts +2 -1
- package/dist/bedrock-converse.d.ts.map +1 -1
- package/dist/bedrock-converse.js +190 -12
- package/dist/bedrock-converse.js.map +1 -1
- package/dist/bedrock.cjs +204 -152
- package/dist/bedrock.cjs.map +1 -1
- package/dist/bedrock.d.cts +2 -1
- package/dist/bedrock.d.cts.map +1 -1
- package/dist/bedrock.d.ts +2 -1
- package/dist/bedrock.d.ts.map +1 -1
- package/dist/bedrock.js +205 -150
- package/dist/bedrock.js.map +1 -1
- package/dist/config-loader.d.cts.map +1 -1
- package/dist/responses.cjs +2 -0
- package/dist/responses.cjs.map +1 -1
- package/dist/responses.d.cts.map +1 -1
- package/dist/responses.d.ts.map +1 -1
- package/dist/responses.js +2 -0
- package/dist/responses.js.map +1 -1
- package/package.json +1 -1
package/dist/bedrock.js
CHANGED
|
@@ -14,6 +14,15 @@ function bedrockStopReason(overrideFinishReason, defaultReason) {
|
|
|
14
14
|
if (overrideFinishReason === "length") return "max_tokens";
|
|
15
15
|
return overrideFinishReason;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Build a Bedrock-style usage object from optional overrides.
|
|
19
|
+
*
|
|
20
|
+
* When no overrides are provided (the common case for mock fixtures),
|
|
21
|
+
* returns all-zero token counts. This is intentional — aimock does not
|
|
22
|
+
* attempt to estimate token usage from fixture content. Callers that
|
|
23
|
+
* need realistic usage numbers should set `usage` in their fixture's
|
|
24
|
+
* response overrides.
|
|
25
|
+
*/
|
|
17
26
|
function bedrockUsage(overrides) {
|
|
18
27
|
if (!overrides?.usage) return {
|
|
19
28
|
input_tokens: 0,
|
|
@@ -28,7 +37,7 @@ function extractTextContent(content) {
|
|
|
28
37
|
if (typeof content === "string") return content;
|
|
29
38
|
return content.filter((b) => b.type === "text").map((b) => b.text ?? "").join("");
|
|
30
39
|
}
|
|
31
|
-
function bedrockToCompletionRequest(req, modelId) {
|
|
40
|
+
function bedrockToCompletionRequest(req, modelId, logger) {
|
|
32
41
|
const messages = [];
|
|
33
42
|
if (req.system) {
|
|
34
43
|
const systemText = typeof req.system === "string" ? req.system : req.system.filter((b) => b.type === "text").map((b) => b.text ?? "").join("");
|
|
@@ -39,6 +48,11 @@ function bedrockToCompletionRequest(req, modelId) {
|
|
|
39
48
|
}
|
|
40
49
|
for (const msg of req.messages) if (msg.role === "user") {
|
|
41
50
|
if (typeof msg.content !== "string" && Array.isArray(msg.content)) {
|
|
51
|
+
const unsupportedBlocks = msg.content.filter((b) => b.type !== "text" && b.type !== "tool_result");
|
|
52
|
+
if (unsupportedBlocks.length > 0 && logger) {
|
|
53
|
+
const types = [...new Set(unsupportedBlocks.map((b) => b.type))].join(", ");
|
|
54
|
+
logger.warn(`Bedrock user message contains unsupported content block types [${types}] — these will be dropped during conversion`);
|
|
55
|
+
}
|
|
42
56
|
const toolResults = msg.content.filter((b) => b.type === "tool_result");
|
|
43
57
|
const textBlocks = msg.content.filter((b) => b.type === "text");
|
|
44
58
|
if (toolResults.length > 0) {
|
|
@@ -70,24 +84,28 @@ function bedrockToCompletionRequest(req, modelId) {
|
|
|
70
84
|
const textContent = extractTextContent(msg.content);
|
|
71
85
|
if (toolUseBlocks.length > 0) messages.push({
|
|
72
86
|
role: "assistant",
|
|
73
|
-
content: textContent
|
|
74
|
-
tool_calls: toolUseBlocks.map((b) =>
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
87
|
+
content: textContent ?? null,
|
|
88
|
+
tool_calls: toolUseBlocks.map((b, index) => {
|
|
89
|
+
if (!b.id && logger) logger.warn(`Bedrock assistant tool_use block at index ${index} is missing an id — using deterministic fallback "tool_use_${index}"`);
|
|
90
|
+
return {
|
|
91
|
+
id: b.id ?? `tool_use_${index}`,
|
|
92
|
+
type: "function",
|
|
93
|
+
function: {
|
|
94
|
+
name: b.name ?? "",
|
|
95
|
+
arguments: typeof b.input === "string" ? b.input : JSON.stringify(b.input ?? {})
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
})
|
|
82
99
|
});
|
|
83
100
|
else messages.push({
|
|
84
101
|
role: "assistant",
|
|
85
|
-
content: textContent
|
|
102
|
+
content: textContent ?? null
|
|
86
103
|
});
|
|
87
104
|
} else messages.push({
|
|
88
105
|
role: "assistant",
|
|
89
106
|
content: null
|
|
90
107
|
});
|
|
108
|
+
else if (logger) logger.warn(`Bedrock message has unexpected role "${msg.role}" — skipping`);
|
|
91
109
|
let tools;
|
|
92
110
|
if (req.tools && req.tools.length > 0) tools = req.tools.map((t) => ({
|
|
93
111
|
type: "function",
|
|
@@ -193,7 +211,7 @@ async function handleBedrock(req, res, raw, modelId, fixtures, journal, defaults
|
|
|
193
211
|
} }));
|
|
194
212
|
return;
|
|
195
213
|
}
|
|
196
|
-
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);
|
|
214
|
+
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId, logger);
|
|
197
215
|
completionReq._endpointType = "chat";
|
|
198
216
|
const testId = getTestId(req);
|
|
199
217
|
const fixture = matchFixture(fixtures, completionReq, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
|
|
@@ -308,6 +326,7 @@ async function handleBedrock(req, res, raw, modelId, fixtures, journal, defaults
|
|
|
308
326
|
return;
|
|
309
327
|
}
|
|
310
328
|
if (isToolCallResponse(response)) {
|
|
329
|
+
if ("webSearches" in response) logger.warn("webSearches in fixture response are not supported for Bedrock API — ignoring");
|
|
311
330
|
const overrides = extractOverrides(response);
|
|
312
331
|
journal.add({
|
|
313
332
|
method: req.method ?? "POST",
|
|
@@ -339,151 +358,198 @@ async function handleBedrock(req, res, raw, modelId, fixtures, journal, defaults
|
|
|
339
358
|
type: "server_error"
|
|
340
359
|
} }));
|
|
341
360
|
}
|
|
342
|
-
|
|
361
|
+
const BEDROCK_INVOKE_STREAM_EVENT_TYPE = "chunk";
|
|
362
|
+
function buildBedrockInvokeMessageStart(model, overrides) {
|
|
363
|
+
return {
|
|
364
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
365
|
+
payload: {
|
|
366
|
+
type: "message_start",
|
|
367
|
+
message: {
|
|
368
|
+
id: overrides?.id ?? generateMessageId(),
|
|
369
|
+
type: "message",
|
|
370
|
+
role: "assistant",
|
|
371
|
+
content: [],
|
|
372
|
+
model: overrides?.model ?? model,
|
|
373
|
+
stop_reason: null,
|
|
374
|
+
stop_sequence: null,
|
|
375
|
+
usage: bedrockUsage(overrides)
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function buildBedrockInvokeMessageDelta(stopReason) {
|
|
381
|
+
return {
|
|
382
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
383
|
+
payload: {
|
|
384
|
+
type: "message_delta",
|
|
385
|
+
delta: {
|
|
386
|
+
stop_reason: stopReason,
|
|
387
|
+
stop_sequence: null
|
|
388
|
+
},
|
|
389
|
+
usage: { output_tokens: 0 }
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function buildBedrockInvokeMessageStop() {
|
|
394
|
+
return {
|
|
395
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
396
|
+
payload: { type: "message_stop" }
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function parseToolArgumentsForStream(toolCall, logger) {
|
|
400
|
+
try {
|
|
401
|
+
const parsed = JSON.parse(toolCall.arguments || "{}");
|
|
402
|
+
return JSON.stringify(parsed);
|
|
403
|
+
} catch {
|
|
404
|
+
logger.warn(`Malformed JSON in fixture tool call arguments for "${toolCall.name}": ${toolCall.arguments}`);
|
|
405
|
+
return "{}";
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
function buildBedrockStreamTextEvents(content, model, chunkSize, reasoning, overrides) {
|
|
343
409
|
const events = [];
|
|
344
|
-
events.push(
|
|
345
|
-
eventType: "messageStart",
|
|
346
|
-
payload: { messageStart: { role: "assistant" } }
|
|
347
|
-
});
|
|
410
|
+
events.push(buildBedrockInvokeMessageStart(model, overrides));
|
|
348
411
|
if (reasoning) {
|
|
349
412
|
const blockIndex = 0;
|
|
350
413
|
events.push({
|
|
351
|
-
eventType:
|
|
414
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
352
415
|
payload: {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
416
|
+
type: "content_block_start",
|
|
417
|
+
index: blockIndex,
|
|
418
|
+
content_block: {
|
|
419
|
+
type: "thinking",
|
|
420
|
+
thinking: ""
|
|
357
421
|
}
|
|
358
422
|
}
|
|
359
423
|
});
|
|
360
424
|
for (let i = 0; i < reasoning.length; i += chunkSize) {
|
|
361
425
|
const slice = reasoning.slice(i, i + chunkSize);
|
|
362
426
|
events.push({
|
|
363
|
-
eventType:
|
|
427
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
364
428
|
payload: {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
thinking: slice
|
|
371
|
-
}
|
|
429
|
+
type: "content_block_delta",
|
|
430
|
+
index: blockIndex,
|
|
431
|
+
delta: {
|
|
432
|
+
type: "thinking_delta",
|
|
433
|
+
thinking: slice
|
|
372
434
|
}
|
|
373
435
|
}
|
|
374
436
|
});
|
|
375
437
|
}
|
|
376
438
|
events.push({
|
|
377
|
-
eventType:
|
|
378
|
-
payload: {
|
|
439
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
440
|
+
payload: {
|
|
441
|
+
type: "content_block_stop",
|
|
442
|
+
index: blockIndex
|
|
443
|
+
}
|
|
379
444
|
});
|
|
380
445
|
}
|
|
381
446
|
const textBlockIndex = reasoning ? 1 : 0;
|
|
382
447
|
events.push({
|
|
383
|
-
eventType:
|
|
448
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
384
449
|
payload: {
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
450
|
+
type: "content_block_start",
|
|
451
|
+
index: textBlockIndex,
|
|
452
|
+
content_block: {
|
|
453
|
+
type: "text",
|
|
454
|
+
text: ""
|
|
389
455
|
}
|
|
390
456
|
}
|
|
391
457
|
});
|
|
392
458
|
for (let i = 0; i < content.length; i += chunkSize) {
|
|
393
459
|
const slice = content.slice(i, i + chunkSize);
|
|
394
460
|
events.push({
|
|
395
|
-
eventType:
|
|
461
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
396
462
|
payload: {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
text: slice
|
|
403
|
-
}
|
|
463
|
+
type: "content_block_delta",
|
|
464
|
+
index: textBlockIndex,
|
|
465
|
+
delta: {
|
|
466
|
+
type: "text_delta",
|
|
467
|
+
text: slice
|
|
404
468
|
}
|
|
405
469
|
}
|
|
406
470
|
});
|
|
407
471
|
}
|
|
408
472
|
events.push({
|
|
409
|
-
eventType:
|
|
410
|
-
payload: {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
payload: { stopReason: bedrockStopReason(overrides?.finishReason, "end_turn") }
|
|
473
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
474
|
+
payload: {
|
|
475
|
+
type: "content_block_stop",
|
|
476
|
+
index: textBlockIndex
|
|
477
|
+
}
|
|
415
478
|
});
|
|
479
|
+
events.push(buildBedrockInvokeMessageDelta(bedrockStopReason(overrides?.finishReason, "end_turn")));
|
|
480
|
+
events.push(buildBedrockInvokeMessageStop());
|
|
416
481
|
return events;
|
|
417
482
|
}
|
|
418
|
-
function buildBedrockStreamContentWithToolCallsEvents(content, toolCalls, chunkSize, logger, reasoning, overrides) {
|
|
483
|
+
function buildBedrockStreamContentWithToolCallsEvents(content, toolCalls, model, chunkSize, logger, reasoning, overrides) {
|
|
419
484
|
const events = [];
|
|
420
|
-
events.push(
|
|
421
|
-
eventType: "messageStart",
|
|
422
|
-
payload: { messageStart: { role: "assistant" } }
|
|
423
|
-
});
|
|
485
|
+
events.push(buildBedrockInvokeMessageStart(model, overrides));
|
|
424
486
|
let blockIndex = 0;
|
|
425
487
|
if (reasoning) {
|
|
426
488
|
events.push({
|
|
427
|
-
eventType:
|
|
489
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
428
490
|
payload: {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
491
|
+
type: "content_block_start",
|
|
492
|
+
index: blockIndex,
|
|
493
|
+
content_block: {
|
|
494
|
+
type: "thinking",
|
|
495
|
+
thinking: ""
|
|
433
496
|
}
|
|
434
497
|
}
|
|
435
498
|
});
|
|
436
499
|
for (let i = 0; i < reasoning.length; i += chunkSize) {
|
|
437
500
|
const slice = reasoning.slice(i, i + chunkSize);
|
|
438
501
|
events.push({
|
|
439
|
-
eventType:
|
|
502
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
440
503
|
payload: {
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
thinking: slice
|
|
447
|
-
}
|
|
504
|
+
type: "content_block_delta",
|
|
505
|
+
index: blockIndex,
|
|
506
|
+
delta: {
|
|
507
|
+
type: "thinking_delta",
|
|
508
|
+
thinking: slice
|
|
448
509
|
}
|
|
449
510
|
}
|
|
450
511
|
});
|
|
451
512
|
}
|
|
452
513
|
events.push({
|
|
453
|
-
eventType:
|
|
454
|
-
payload: {
|
|
514
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
515
|
+
payload: {
|
|
516
|
+
type: "content_block_stop",
|
|
517
|
+
index: blockIndex
|
|
518
|
+
}
|
|
455
519
|
});
|
|
456
520
|
blockIndex++;
|
|
457
521
|
}
|
|
458
522
|
events.push({
|
|
459
|
-
eventType:
|
|
523
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
460
524
|
payload: {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
525
|
+
type: "content_block_start",
|
|
526
|
+
index: blockIndex,
|
|
527
|
+
content_block: {
|
|
528
|
+
type: "text",
|
|
529
|
+
text: ""
|
|
465
530
|
}
|
|
466
531
|
}
|
|
467
532
|
});
|
|
468
533
|
for (let i = 0; i < content.length; i += chunkSize) {
|
|
469
534
|
const slice = content.slice(i, i + chunkSize);
|
|
470
535
|
events.push({
|
|
471
|
-
eventType:
|
|
536
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
472
537
|
payload: {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
text: slice
|
|
479
|
-
}
|
|
538
|
+
type: "content_block_delta",
|
|
539
|
+
index: blockIndex,
|
|
540
|
+
delta: {
|
|
541
|
+
type: "text_delta",
|
|
542
|
+
text: slice
|
|
480
543
|
}
|
|
481
544
|
}
|
|
482
545
|
});
|
|
483
546
|
}
|
|
484
547
|
events.push({
|
|
485
|
-
eventType:
|
|
486
|
-
payload: {
|
|
548
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
549
|
+
payload: {
|
|
550
|
+
type: "content_block_stop",
|
|
551
|
+
index: blockIndex
|
|
552
|
+
}
|
|
487
553
|
});
|
|
488
554
|
blockIndex++;
|
|
489
555
|
for (let tcIdx = 0; tcIdx < toolCalls.length; tcIdx++) {
|
|
@@ -491,102 +557,89 @@ function buildBedrockStreamContentWithToolCallsEvents(content, toolCalls, chunkS
|
|
|
491
557
|
const toolUseId = tc.id || generateToolUseId();
|
|
492
558
|
const currentBlock = blockIndex + tcIdx;
|
|
493
559
|
events.push({
|
|
494
|
-
eventType:
|
|
560
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
495
561
|
payload: {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
562
|
+
type: "content_block_start",
|
|
563
|
+
index: currentBlock,
|
|
564
|
+
content_block: {
|
|
565
|
+
type: "tool_use",
|
|
566
|
+
id: toolUseId,
|
|
567
|
+
name: tc.name,
|
|
568
|
+
input: {}
|
|
503
569
|
}
|
|
504
570
|
}
|
|
505
571
|
});
|
|
506
|
-
|
|
507
|
-
try {
|
|
508
|
-
const parsed = JSON.parse(tc.arguments || "{}");
|
|
509
|
-
argsStr = JSON.stringify(parsed);
|
|
510
|
-
} catch {
|
|
511
|
-
logger.warn(`Malformed JSON in fixture tool call arguments for "${tc.name}": ${tc.arguments}`);
|
|
512
|
-
argsStr = "{}";
|
|
513
|
-
}
|
|
572
|
+
const argsStr = parseToolArgumentsForStream(tc, logger);
|
|
514
573
|
for (let i = 0; i < argsStr.length; i += chunkSize) {
|
|
515
574
|
const slice = argsStr.slice(i, i + chunkSize);
|
|
516
575
|
events.push({
|
|
517
|
-
eventType:
|
|
576
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
518
577
|
payload: {
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
578
|
+
type: "content_block_delta",
|
|
579
|
+
index: currentBlock,
|
|
580
|
+
delta: {
|
|
581
|
+
type: "input_json_delta",
|
|
582
|
+
partial_json: slice
|
|
523
583
|
}
|
|
524
584
|
}
|
|
525
585
|
});
|
|
526
586
|
}
|
|
527
587
|
events.push({
|
|
528
|
-
eventType:
|
|
529
|
-
payload: {
|
|
588
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
589
|
+
payload: {
|
|
590
|
+
type: "content_block_stop",
|
|
591
|
+
index: currentBlock
|
|
592
|
+
}
|
|
530
593
|
});
|
|
531
594
|
}
|
|
532
|
-
events.push(
|
|
533
|
-
|
|
534
|
-
payload: { stopReason: bedrockStopReason(overrides?.finishReason, "tool_use") }
|
|
535
|
-
});
|
|
595
|
+
events.push(buildBedrockInvokeMessageDelta(bedrockStopReason(overrides?.finishReason, "tool_use")));
|
|
596
|
+
events.push(buildBedrockInvokeMessageStop());
|
|
536
597
|
return events;
|
|
537
598
|
}
|
|
538
|
-
function buildBedrockStreamToolCallEvents(toolCalls, chunkSize, logger, overrides) {
|
|
599
|
+
function buildBedrockStreamToolCallEvents(toolCalls, model, chunkSize, logger, overrides) {
|
|
539
600
|
const events = [];
|
|
540
|
-
events.push(
|
|
541
|
-
eventType: "messageStart",
|
|
542
|
-
payload: { messageStart: { role: "assistant" } }
|
|
543
|
-
});
|
|
601
|
+
events.push(buildBedrockInvokeMessageStart(model, overrides));
|
|
544
602
|
for (let tcIdx = 0; tcIdx < toolCalls.length; tcIdx++) {
|
|
545
603
|
const tc = toolCalls[tcIdx];
|
|
546
604
|
const toolUseId = tc.id || generateToolUseId();
|
|
547
605
|
events.push({
|
|
548
|
-
eventType:
|
|
606
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
549
607
|
payload: {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
608
|
+
type: "content_block_start",
|
|
609
|
+
index: tcIdx,
|
|
610
|
+
content_block: {
|
|
611
|
+
type: "tool_use",
|
|
612
|
+
id: toolUseId,
|
|
613
|
+
name: tc.name,
|
|
614
|
+
input: {}
|
|
557
615
|
}
|
|
558
616
|
}
|
|
559
617
|
});
|
|
560
|
-
|
|
561
|
-
try {
|
|
562
|
-
const parsed = JSON.parse(tc.arguments || "{}");
|
|
563
|
-
argsStr = JSON.stringify(parsed);
|
|
564
|
-
} catch {
|
|
565
|
-
logger.warn(`Malformed JSON in fixture tool call arguments for "${tc.name}": ${tc.arguments}`);
|
|
566
|
-
argsStr = "{}";
|
|
567
|
-
}
|
|
618
|
+
const argsStr = parseToolArgumentsForStream(tc, logger);
|
|
568
619
|
for (let i = 0; i < argsStr.length; i += chunkSize) {
|
|
569
620
|
const slice = argsStr.slice(i, i + chunkSize);
|
|
570
621
|
events.push({
|
|
571
|
-
eventType:
|
|
622
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
572
623
|
payload: {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
624
|
+
type: "content_block_delta",
|
|
625
|
+
index: tcIdx,
|
|
626
|
+
delta: {
|
|
627
|
+
type: "input_json_delta",
|
|
628
|
+
partial_json: slice
|
|
577
629
|
}
|
|
578
630
|
}
|
|
579
631
|
});
|
|
580
632
|
}
|
|
581
633
|
events.push({
|
|
582
|
-
eventType:
|
|
583
|
-
payload: {
|
|
634
|
+
eventType: BEDROCK_INVOKE_STREAM_EVENT_TYPE,
|
|
635
|
+
payload: {
|
|
636
|
+
type: "content_block_stop",
|
|
637
|
+
index: tcIdx
|
|
638
|
+
}
|
|
584
639
|
});
|
|
585
640
|
}
|
|
586
|
-
events.push(
|
|
587
|
-
|
|
588
|
-
payload: { stopReason: bedrockStopReason(overrides?.finishReason, "tool_use") }
|
|
589
|
-
});
|
|
641
|
+
events.push(buildBedrockInvokeMessageDelta(bedrockStopReason(overrides?.finishReason, "tool_use")));
|
|
642
|
+
events.push(buildBedrockInvokeMessageStop());
|
|
590
643
|
return events;
|
|
591
644
|
}
|
|
592
645
|
async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, defaults, setCorsHeaders) {
|
|
@@ -630,7 +683,8 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
630
683
|
} }));
|
|
631
684
|
return;
|
|
632
685
|
}
|
|
633
|
-
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);
|
|
686
|
+
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId, logger);
|
|
687
|
+
completionReq.stream = true;
|
|
634
688
|
completionReq._endpointType = "chat";
|
|
635
689
|
const testId = getTestId(req);
|
|
636
690
|
const fixture = matchFixture(fixtures, completionReq, journal.getFixtureMatchCountsForTest(testId), defaults.requestTransform);
|
|
@@ -717,7 +771,7 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
717
771
|
fixture
|
|
718
772
|
}
|
|
719
773
|
});
|
|
720
|
-
const events = buildBedrockStreamContentWithToolCallsEvents(response.content, response.toolCalls, chunkSize, logger, response.reasoning, overrides);
|
|
774
|
+
const events = buildBedrockStreamContentWithToolCallsEvents(response.content, response.toolCalls, completionReq.model, chunkSize, logger, response.reasoning, overrides);
|
|
721
775
|
const interruption = createInterruptionSignal(fixture);
|
|
722
776
|
if (!await writeEventStream(res, events, {
|
|
723
777
|
latency,
|
|
@@ -745,7 +799,7 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
745
799
|
fixture
|
|
746
800
|
}
|
|
747
801
|
});
|
|
748
|
-
const events = buildBedrockStreamTextEvents(response.content, chunkSize, response.reasoning, overrides);
|
|
802
|
+
const events = buildBedrockStreamTextEvents(response.content, completionReq.model, chunkSize, response.reasoning, overrides);
|
|
749
803
|
const interruption = createInterruptionSignal(fixture);
|
|
750
804
|
if (!await writeEventStream(res, events, {
|
|
751
805
|
latency,
|
|
@@ -761,6 +815,7 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
761
815
|
return;
|
|
762
816
|
}
|
|
763
817
|
if (isToolCallResponse(response)) {
|
|
818
|
+
if ("webSearches" in response) logger.warn("webSearches in fixture response are not supported for Bedrock API — ignoring");
|
|
764
819
|
const overrides = extractOverrides(response);
|
|
765
820
|
const journalEntry = journal.add({
|
|
766
821
|
method: req.method ?? "POST",
|
|
@@ -772,7 +827,7 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
772
827
|
fixture
|
|
773
828
|
}
|
|
774
829
|
});
|
|
775
|
-
const events = buildBedrockStreamToolCallEvents(response.toolCalls, chunkSize, logger, overrides);
|
|
830
|
+
const events = buildBedrockStreamToolCallEvents(response.toolCalls, completionReq.model, chunkSize, logger, overrides);
|
|
776
831
|
const interruption = createInterruptionSignal(fixture);
|
|
777
832
|
if (!await writeEventStream(res, events, {
|
|
778
833
|
latency,
|
|
@@ -804,5 +859,5 @@ async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, de
|
|
|
804
859
|
}
|
|
805
860
|
|
|
806
861
|
//#endregion
|
|
807
|
-
export { bedrockToCompletionRequest,
|
|
862
|
+
export { bedrockToCompletionRequest, handleBedrock, handleBedrockStream };
|
|
808
863
|
//# sourceMappingURL=bedrock.js.map
|