@plurnk/plurnk-providers 6.2.0 → 6.3.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.
@@ -38,10 +38,88 @@ const lookupPiBuiltinCompat = (providerId, modelId) => {
38
38
  : {}),
39
39
  };
40
40
  };
41
- const toPiContext = (messages) => {
41
+ export const MCP_WIRE_TOOL = {
42
+ name: "mcp",
43
+ description: "Call an MCP tool.",
44
+ parameters: {
45
+ type: "object",
46
+ required: ["server", "tool", "arguments"],
47
+ properties: {
48
+ server: { type: "string" },
49
+ tool: { type: "string" },
50
+ arguments: {
51
+ type: "object",
52
+ additionalProperties: true,
53
+ },
54
+ aside: {
55
+ type: "string",
56
+ description: "Optional terse intent note.",
57
+ },
58
+ },
59
+ additionalProperties: false,
60
+ },
61
+ };
62
+ export const toOpenAIChatMessages = (messages) => messages.map((message) => {
63
+ if (message.role === "tool") {
64
+ const text = typeof message.content === "string"
65
+ ? message.content
66
+ : message.content.map((part) => part.type === "text" ? part.text : "(attached file)").join("");
67
+ return {
68
+ role: "tool",
69
+ tool_call_id: message.toolCallId ?? "",
70
+ content: text,
71
+ };
72
+ }
73
+ if (message.role === "assistant" && message.toolCalls && message.toolCalls.length > 0) {
74
+ return {
75
+ role: "assistant",
76
+ content: null,
77
+ tool_calls: message.toolCalls.map((tc) => ({
78
+ id: tc.id,
79
+ type: "function",
80
+ function: {
81
+ name: tc.name,
82
+ arguments: JSON.stringify(tc.arguments),
83
+ },
84
+ })),
85
+ };
86
+ }
87
+ if (typeof message.content === "string") {
88
+ return { role: message.role, content: message.content };
89
+ }
90
+ return { role: message.role, content: message.content.map(toOpenAIPart) };
91
+ });
92
+ const toJsonValue = (value) => {
93
+ if (value === null || typeof value === "string" || typeof value === "boolean")
94
+ return value;
95
+ if (typeof value === "number")
96
+ return Number.isFinite(value) ? value : null;
97
+ if (Array.isArray(value))
98
+ return value.map((item) => toJsonValue(item));
99
+ if (isRecord(value))
100
+ return toJsonObject(value);
101
+ return null;
102
+ };
103
+ const toJsonObject = (record) => {
104
+ const out = {};
105
+ for (const [key, val] of Object.entries(record)) {
106
+ if (val !== undefined)
107
+ out[key] = toJsonValue(val);
108
+ }
109
+ return out;
110
+ };
111
+ const toPiContext = (messages, effectiveModel, callKind) => {
42
112
  const systemParts = [];
43
113
  const contextMessages = [];
44
114
  let leadingSystem = true;
115
+ const zeroUsage = {
116
+ input: 0,
117
+ output: 0,
118
+ cacheRead: 0,
119
+ cacheWrite: 0,
120
+ totalTokens: 0,
121
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
122
+ };
45
123
  for (const message of messages) {
46
124
  if (leadingSystem && message.role === "system") {
47
125
  systemParts.push(typeof message.content === "string"
@@ -51,28 +129,77 @@ const toPiContext = (messages) => {
51
129
  }
52
130
  leadingSystem = false;
53
131
  if (message.role === "assistant") {
132
+ const replay = message.replay;
133
+ const api = replay?.api ?? effectiveModel.api;
134
+ const provider = replay?.provider ?? effectiveModel.provider;
135
+ const model = replay?.model ?? effectiveModel.id;
136
+ const thinkingBlocks = (replay?.thinking ?? []).map((tb) => ({
137
+ type: "thinking",
138
+ thinking: tb.thinking,
139
+ ...(tb.thinkingSignature !== undefined ? { thinkingSignature: tb.thinkingSignature } : {}),
140
+ ...(tb.redacted === true ? { redacted: true } : {}),
141
+ }));
142
+ if (message.toolCalls && message.toolCalls.length > 0) {
143
+ const toolBlocks = message.toolCalls.map((tc) => ({
144
+ type: "toolCall",
145
+ id: tc.id,
146
+ name: tc.name,
147
+ arguments: toJsonObject(tc.arguments),
148
+ ...(tc.thoughtSignature !== undefined ? { thoughtSignature: tc.thoughtSignature } : {}),
149
+ }));
150
+ const textBlocks = replay?.textSignature !== undefined
151
+ ? [{ type: "text", text: "", textSignature: replay.textSignature }]
152
+ : [];
153
+ contextMessages.push({
154
+ role: "assistant",
155
+ content: [...thinkingBlocks, ...textBlocks, ...toolBlocks],
156
+ api,
157
+ provider,
158
+ model,
159
+ usage: zeroUsage,
160
+ stopReason: "toolUse",
161
+ timestamp: 0,
162
+ });
163
+ continue;
164
+ }
54
165
  const text = typeof message.content === "string"
55
166
  ? message.content
56
167
  : message.content.map((part) => part.type === "text" ? part.text : "").join("");
168
+ const textBlocks = text.length > 0 || replay?.textSignature !== undefined || thinkingBlocks.length === 0
169
+ ? [{
170
+ type: "text",
171
+ text,
172
+ ...(replay?.textSignature !== undefined ? { textSignature: replay.textSignature } : {}),
173
+ }]
174
+ : [];
57
175
  contextMessages.push({
58
176
  role: "assistant",
59
- content: [{ type: "text", text }],
60
- api: "openai-completions",
61
- provider: "openai",
62
- model: "unknown",
63
- usage: {
64
- input: 0,
65
- output: 0,
66
- cacheRead: 0,
67
- cacheWrite: 0,
68
- totalTokens: 0,
69
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
70
- },
177
+ content: [...thinkingBlocks, ...textBlocks],
178
+ api,
179
+ provider,
180
+ model,
181
+ usage: zeroUsage,
71
182
  stopReason: "stop",
72
183
  timestamp: 0,
73
184
  });
74
185
  continue;
75
186
  }
187
+ if (message.role === "tool") {
188
+ const content = typeof message.content === "string"
189
+ ? [{ type: "text", text: message.content }]
190
+ : message.content.map((part) => part.type === "text"
191
+ ? { type: "text", text: part.text }
192
+ : { type: "image", data: Buffer.from(part.data).toString("base64"), mimeType: part.mediaType });
193
+ contextMessages.push({
194
+ role: "toolResult",
195
+ toolCallId: message.toolCallId ?? "",
196
+ toolName: message.toolName ?? "mcp",
197
+ content,
198
+ isError: message.isError ?? false,
199
+ timestamp: 0,
200
+ });
201
+ continue;
202
+ }
76
203
  const content = typeof message.content === "string"
77
204
  ? message.content
78
205
  : message.content.map((part) => part.type === "text"
@@ -86,12 +213,45 @@ const toPiContext = (messages) => {
86
213
  }
87
214
  return normalizeContext({
88
215
  ...(systemParts.length > 0 ? { systemPrompt: systemParts.join("\n\n") } : {}),
216
+ ...(callKind === "emission" ? { tools: [MCP_WIRE_TOOL] } : {}),
89
217
  messages: contextMessages,
90
218
  });
91
219
  };
92
220
  const hasWireContent = (message) => typeof message.content === "string"
93
221
  ? message.content.trim().length > 0
94
222
  : message.content.some((part) => part.type !== "text" || part.text.trim().length > 0);
223
+ const hasEffectiveWireMessage = (message, includeEmptyAssistant = false) => message.role === "tool"
224
+ || (message.role === "assistant" && (includeEmptyAssistant || (message.toolCalls?.length ?? 0) > 0))
225
+ || hasWireContent(message);
226
+ const groupCoalescedWireSourceRanges = (messages, includeEmptyAssistant = false) => {
227
+ const firstNonSystem = firstNonSystemMessageIndex(messages);
228
+ const groups = [];
229
+ for (let i = firstNonSystem; i < messages.length; i += 1) {
230
+ const msg = messages[i];
231
+ if (!hasEffectiveWireMessage(msg, includeEmptyAssistant))
232
+ continue;
233
+ if (msg.role === "assistant") {
234
+ groups.push({ role: "assistant", sourceIndices: [i] });
235
+ }
236
+ else {
237
+ const prev = groups.at(-1);
238
+ if (prev?.role === "user") {
239
+ prev.sourceIndices.push(i);
240
+ }
241
+ else {
242
+ groups.push({ role: "user", sourceIndices: [i] });
243
+ }
244
+ }
245
+ }
246
+ return groups;
247
+ };
248
+ const sourceWireBlockCount = (message) => {
249
+ if (message.role === "tool")
250
+ return 1;
251
+ if (typeof message.content === "string")
252
+ return hasWireContent(message) ? 1 : 0;
253
+ return countWireParts(message.content, message.content.length);
254
+ };
95
255
  const countWireParts = (parts, limit) => {
96
256
  let count = 0;
97
257
  const end = Math.min(limit, parts.length);
@@ -361,9 +521,12 @@ const markAnthropicMessageEnd = (wireMsg) => {
361
521
  return;
362
522
  }
363
523
  if (Array.isArray(wireMsg.content) && wireMsg.content.length > 0) {
364
- const last = wireMsg.content.at(-1);
365
- if (isRecord(last)) {
366
- last.cache_control = { type: "ephemeral" };
524
+ for (let i = wireMsg.content.length - 1; i >= 0; i -= 1) {
525
+ const candidate = wireMsg.content[i];
526
+ if (isRecord(candidate) && candidate.type !== "thinking" && candidate.type !== "redacted_thinking") {
527
+ candidate.cache_control = { type: "ephemeral" };
528
+ return;
529
+ }
367
530
  }
368
531
  }
369
532
  };
@@ -380,43 +543,71 @@ const applyAnthropicPayloadAdjustments = (params, messages, cacheBreakpoints, us
380
543
  const wireMessages = Array.isArray(params.messages) ? params.messages : undefined;
381
544
  if (wireMessages === undefined)
382
545
  return;
383
- const firstNonSystem = firstNonSystemMessageIndex(messages);
384
- const effectiveSourceIndices = [];
385
- for (let i = firstNonSystem; i < messages.length; i += 1) {
386
- if (hasWireContent(messages[i]))
387
- effectiveSourceIndices.push(i);
388
- }
389
- for (let i = 0; i < Math.min(wireMessages.length, effectiveSourceIndices.length); i += 1) {
390
- const sourceMsg = messages[effectiveSourceIndices[i]];
391
- const wireMsg = wireMessages[i];
392
- if (!Array.isArray(sourceMsg.content) || !isRecord(wireMsg) || !Array.isArray(wireMsg.content))
546
+ const groups = groupCoalescedWireSourceRanges(messages, false);
547
+ for (let w = 0; w < Math.min(wireMessages.length, groups.length); w += 1) {
548
+ const group = groups[w];
549
+ const wireMsg = wireMessages[w];
550
+ if (group.role !== "user" || !isRecord(wireMsg) || !Array.isArray(wireMsg.content))
393
551
  continue;
394
- const nonBlankSourceParts = sourceMsg.content.filter((p) => p.type !== "text" || p.text.trim().length > 0);
395
- for (let j = 0; j < Math.min(nonBlankSourceParts.length, wireMsg.content.length); j += 1) {
396
- const sourcePart = nonBlankSourceParts[j];
397
- const wireBlock = wireMsg.content[j];
398
- if (sourcePart.type === "file"
399
- && sourcePart.mediaType === "application/pdf"
400
- && isRecord(wireBlock)
401
- && wireBlock.type === "image") {
402
- wireBlock.type = "document";
552
+ let blockCursor = 0;
553
+ for (const srcIdx of group.sourceIndices) {
554
+ const sourceMsg = messages[srcIdx];
555
+ if (!Array.isArray(sourceMsg.content) || sourceMsg.role === "tool") {
556
+ blockCursor += sourceWireBlockCount(sourceMsg);
557
+ continue;
558
+ }
559
+ const nonBlankSourceParts = sourceMsg.content.filter((p) => p.type !== "text" || p.text.trim().length > 0);
560
+ for (let j = 0; j < nonBlankSourceParts.length && blockCursor + j < wireMsg.content.length; j += 1) {
561
+ const sourcePart = nonBlankSourceParts[j];
562
+ const wireBlock = wireMsg.content[blockCursor + j];
563
+ if (sourcePart.type === "file"
564
+ && sourcePart.mediaType === "application/pdf"
565
+ && isRecord(wireBlock)
566
+ && wireBlock.type === "image") {
567
+ wireBlock.type = "document";
568
+ }
403
569
  }
570
+ blockCursor += nonBlankSourceParts.length;
404
571
  }
405
572
  }
406
573
  if (cacheBreakpoints !== "conversation" || wireMessages.length === 0)
407
574
  return;
408
- for (let w = 0; w < Math.min(wireMessages.length - 1, effectiveSourceIndices.length); w += 1) {
409
- const sourceIdx = effectiveSourceIndices[w];
410
- const sourceMsg = messages[sourceIdx];
575
+ for (let w = 0; w < Math.min(wireMessages.length - 1, groups.length); w += 1) {
576
+ const group = groups[w];
411
577
  const wireMsg = wireMessages[w];
412
- if (sourceMsg.role !== "assistant" && typeof sourceMsg.content === "string" && isRecord(wireMsg)) {
413
- const text = typeof wireMsg.content === "string" ? wireMsg.content : sourceMsg.content.toWellFormed();
414
- const split = splitHistoricalUserChars(text, userCachePrefixes?.[sourceIdx]);
415
- if (split !== null) {
416
- wireMsg.content = [
417
- { type: "text", text: split[0] },
418
- { type: "text", text: split[1] },
419
- ];
578
+ if (group.role !== "user" || !isRecord(wireMsg))
579
+ continue;
580
+ if (typeof wireMsg.content === "string" && group.sourceIndices.length === 1) {
581
+ const srcIdx = group.sourceIndices[0];
582
+ const sourceMsg = messages[srcIdx];
583
+ if (sourceMsg.role === "user" && typeof sourceMsg.content === "string") {
584
+ const split = splitHistoricalUserChars(wireMsg.content, userCachePrefixes?.[srcIdx]);
585
+ if (split !== null) {
586
+ wireMsg.content = [
587
+ { type: "text", text: split[0] },
588
+ { type: "text", text: split[1] },
589
+ ];
590
+ }
591
+ }
592
+ continue;
593
+ }
594
+ if (Array.isArray(wireMsg.content)) {
595
+ let blockCursor = 0;
596
+ for (const srcIdx of group.sourceIndices) {
597
+ const sourceMsg = messages[srcIdx];
598
+ if (sourceMsg.role === "user" && typeof sourceMsg.content === "string" && blockCursor < wireMsg.content.length) {
599
+ const wireBlock = wireMsg.content[blockCursor];
600
+ const text = isRecord(wireBlock) && typeof wireBlock.text === "string"
601
+ ? wireBlock.text
602
+ : sourceMsg.content.toWellFormed();
603
+ const split = splitHistoricalUserChars(text, userCachePrefixes?.[srcIdx]);
604
+ if (split !== null) {
605
+ wireMsg.content.splice(blockCursor, 1, { type: "text", text: split[0] }, { type: "text", text: split[1] });
606
+ blockCursor += 2;
607
+ continue;
608
+ }
609
+ }
610
+ blockCursor += sourceWireBlockCount(sourceMsg);
420
611
  }
421
612
  }
422
613
  }
@@ -424,49 +615,86 @@ const applyAnthropicPayloadAdjustments = (params, messages, cacheBreakpoints, us
424
615
  markAnthropicMessageEnd(wireMessages[wireMessages.length - 2]);
425
616
  }
426
617
  const lastWire = wireMessages.at(-1);
427
- const lastSourceIdx = effectiveSourceIndices.at(-1);
618
+ const lastGroup = groups.at(-1);
619
+ const lastSourceIdx = lastGroup?.sourceIndices.at(-1);
428
620
  const lastSource = lastSourceIdx === undefined ? undefined : messages[lastSourceIdx];
429
- if (lastSource === undefined || lastSource.role === "assistant" || !isRecord(lastWire) || lastWire.role !== "user")
621
+ if (lastGroup === undefined || lastGroup.role !== "user" || lastSourceIdx === undefined || lastSource === undefined || !isRecord(lastWire) || lastWire.role !== "user")
430
622
  return;
431
- const finalPrefix = lastSourceIdx === undefined ? undefined : userCachePrefixes?.[lastSourceIdx];
623
+ if (lastSource.role === "tool") {
624
+ markAnthropicMessageEnd(lastWire);
625
+ return;
626
+ }
627
+ const precedingBlockCount = lastGroup.sourceIndices
628
+ .slice(0, -1)
629
+ .reduce((sum, idx) => sum + sourceWireBlockCount(messages[idx]), 0);
630
+ const finalPrefix = userCachePrefixes?.[lastSourceIdx];
432
631
  if (finalPrefix === undefined) {
433
632
  markAnthropicMessageEnd(lastWire);
434
633
  return;
435
634
  }
436
635
  if (finalPrefix.kind === "chars" && typeof lastSource.content === "string") {
437
- if (finalPrefix.length <= 0)
636
+ if (finalPrefix.length <= 0) {
637
+ if (precedingBlockCount > 0 && Array.isArray(lastWire.content) && precedingBlockCount <= lastWire.content.length) {
638
+ const target = lastWire.content[precedingBlockCount - 1];
639
+ if (isRecord(target))
640
+ target.cache_control = { type: "ephemeral" };
641
+ }
438
642
  return;
439
- const text = typeof lastWire.content === "string" ? lastWire.content : lastSource.content.toWellFormed();
643
+ }
644
+ const trailingBlock = Array.isArray(lastWire.content) ? lastWire.content[precedingBlockCount] : undefined;
645
+ const text = typeof lastWire.content === "string"
646
+ ? lastWire.content
647
+ : isRecord(trailingBlock) && typeof trailingBlock.text === "string"
648
+ ? trailingBlock.text
649
+ : lastSource.content.toWellFormed();
440
650
  if (finalPrefix.length >= text.length) {
441
651
  markAnthropicMessageEnd(lastWire);
442
652
  return;
443
653
  }
444
654
  const durable = text.slice(0, finalPrefix.length);
445
655
  const volatile = text.slice(finalPrefix.length);
446
- if (durable.trim().length === 0)
656
+ if (durable.trim().length === 0) {
657
+ if (precedingBlockCount > 0 && Array.isArray(lastWire.content) && precedingBlockCount <= lastWire.content.length) {
658
+ const target = lastWire.content[precedingBlockCount - 1];
659
+ if (isRecord(target))
660
+ target.cache_control = { type: "ephemeral" };
661
+ }
447
662
  return;
448
- lastWire.content = volatile.trim().length > 0
663
+ }
664
+ const splitBlocks = volatile.trim().length > 0
449
665
  ? [
450
666
  { type: "text", text: durable, cache_control: { type: "ephemeral" } },
451
667
  { type: "text", text: volatile },
452
668
  ]
453
669
  : [{ type: "text", text: durable, cache_control: { type: "ephemeral" } }];
670
+ if (Array.isArray(lastWire.content) && precedingBlockCount > 0) {
671
+ lastWire.content = [
672
+ ...lastWire.content.slice(0, precedingBlockCount),
673
+ ...splitBlocks,
674
+ ];
675
+ }
676
+ else {
677
+ lastWire.content = splitBlocks;
678
+ }
454
679
  return;
455
680
  }
456
681
  if (finalPrefix.kind === "parts" && Array.isArray(lastSource.content) && Array.isArray(lastWire.content)) {
457
682
  const k = countWireParts(lastSource.content, finalPrefix.count);
458
- if (k > 0 && k <= lastWire.content.length) {
459
- const target = lastWire.content[k - 1];
683
+ const targetIdx = precedingBlockCount + k - 1;
684
+ if (targetIdx >= 0 && targetIdx < lastWire.content.length) {
685
+ const target = lastWire.content[targetIdx];
460
686
  if (isRecord(target))
461
687
  target.cache_control = { type: "ephemeral" };
462
688
  }
463
689
  }
464
690
  };
465
- const appendBedrockCachePoint = (wireMsg, sourceMsg) => {
466
- if (sourceMsg === undefined || !hasWireContent(sourceMsg))
691
+ const appendBedrockCachePoint = (wireMsg, hasContent = true) => {
692
+ if (!hasContent)
467
693
  return;
468
694
  if (!isRecord(wireMsg) || !Array.isArray(wireMsg.content) || wireMsg.content.length === 0)
469
695
  return;
696
+ if (wireMsg.content.every((block) => isRecord(block) && "reasoningContent" in block))
697
+ return;
470
698
  wireMsg.content = [...wireMsg.content, { cachePoint: { type: "default" } }];
471
699
  };
472
700
  const applyBedrockPayloadAdjustments = (params, messages, cacheBreakpoints, userCachePrefixes) => {
@@ -480,76 +708,111 @@ const applyBedrockPayloadAdjustments = (params, messages, cacheBreakpoints, user
480
708
  const wireMessages = Array.isArray(params.messages) ? params.messages : undefined;
481
709
  if (wireMessages === undefined || wireMessages.length === 0)
482
710
  return;
483
- const firstNonSystem = firstNonSystemMessageIndex(messages);
484
- const bedrockSourceIndices = [];
485
- for (let i = firstNonSystem; i < messages.length; i += 1) {
486
- const m = messages[i];
487
- if (m.role !== "assistant" || hasWireContent(m))
488
- bedrockSourceIndices.push(i);
489
- }
490
- for (let w = 0; w < Math.min(wireMessages.length - 1, bedrockSourceIndices.length); w += 1) {
491
- const sourceIdx = bedrockSourceIndices[w];
492
- const sourceMsg = messages[sourceIdx];
711
+ const groups = groupCoalescedWireSourceRanges(messages, true);
712
+ for (let w = 0; w < Math.min(wireMessages.length - 1, groups.length); w += 1) {
713
+ const group = groups[w];
493
714
  const wireMsg = wireMessages[w];
494
- if (sourceMsg.role !== "assistant" && typeof sourceMsg.content === "string" && isRecord(wireMsg) && Array.isArray(wireMsg.content)) {
495
- const firstBlock = wireMsg.content[0];
496
- const text = wireMsg.content.length === 1 && isRecord(firstBlock) && typeof firstBlock.text === "string"
497
- ? firstBlock.text
498
- : sourceMsg.content.toWellFormed();
499
- const split = splitHistoricalUserChars(text, userCachePrefixes?.[sourceIdx]);
500
- if (split !== null) {
501
- wireMsg.content = [{ text: split[0] }, { text: split[1] }];
715
+ if (group.role !== "user" || !isRecord(wireMsg) || !Array.isArray(wireMsg.content))
716
+ continue;
717
+ let blockCursor = 0;
718
+ for (const srcIdx of group.sourceIndices) {
719
+ const sourceMsg = messages[srcIdx];
720
+ if (sourceMsg.role === "user" && typeof sourceMsg.content === "string" && blockCursor < wireMsg.content.length) {
721
+ const wireBlock = wireMsg.content[blockCursor];
722
+ const text = isRecord(wireBlock) && typeof wireBlock.text === "string"
723
+ ? wireBlock.text
724
+ : sourceMsg.content.toWellFormed();
725
+ const split = splitHistoricalUserChars(text, userCachePrefixes?.[srcIdx]);
726
+ if (split !== null) {
727
+ wireMsg.content.splice(blockCursor, 1, { text: split[0] }, { text: split[1] });
728
+ blockCursor += 2;
729
+ continue;
730
+ }
502
731
  }
732
+ blockCursor += sourceWireBlockCount(sourceMsg);
503
733
  }
504
734
  }
505
735
  if (wireMessages.length >= 2) {
506
- const penultSourceIdx = bedrockSourceIndices[wireMessages.length - 2];
507
- appendBedrockCachePoint(wireMessages[wireMessages.length - 2], penultSourceIdx === undefined ? undefined : messages[penultSourceIdx]);
736
+ const penultGroup = groups[wireMessages.length - 2];
737
+ const penultHasContent = penultGroup !== undefined
738
+ && penultGroup.sourceIndices.some((idx) => hasEffectiveWireMessage(messages[idx], false));
739
+ appendBedrockCachePoint(wireMessages[wireMessages.length - 2], penultHasContent);
508
740
  }
509
741
  const lastWire = wireMessages.at(-1);
510
- const lastSourceIdx = bedrockSourceIndices.at(-1);
742
+ const lastGroup = groups.at(-1);
743
+ const lastSourceIdx = lastGroup?.sourceIndices.at(-1);
511
744
  const lastSource = lastSourceIdx === undefined ? undefined : messages[lastSourceIdx];
512
- if (lastSource === undefined || lastSource.role === "assistant" || !hasWireContent(lastSource) || !isRecord(lastWire) || lastWire.role !== "user" || !Array.isArray(lastWire.content)) {
745
+ if (lastGroup === undefined || lastGroup.role !== "user" || lastSourceIdx === undefined || lastSource === undefined || !isRecord(lastWire) || lastWire.role !== "user" || !Array.isArray(lastWire.content)) {
746
+ return;
747
+ }
748
+ if (lastSource.role === "tool") {
749
+ appendBedrockCachePoint(lastWire, true);
513
750
  return;
514
751
  }
515
- const finalPrefix = lastSourceIdx === undefined ? undefined : userCachePrefixes?.[lastSourceIdx];
752
+ if (!hasWireContent(lastSource))
753
+ return;
754
+ const precedingBlockCount = lastGroup.sourceIndices
755
+ .slice(0, -1)
756
+ .reduce((sum, idx) => sum + sourceWireBlockCount(messages[idx]), 0);
757
+ const finalPrefix = userCachePrefixes?.[lastSourceIdx];
516
758
  if (finalPrefix === undefined) {
517
- appendBedrockCachePoint(lastWire, lastSource);
759
+ appendBedrockCachePoint(lastWire, true);
518
760
  return;
519
761
  }
520
762
  if (finalPrefix.kind === "chars" && typeof lastSource.content === "string") {
521
- if (finalPrefix.length <= 0)
763
+ if (finalPrefix.length <= 0) {
764
+ if (precedingBlockCount > 0 && precedingBlockCount <= lastWire.content.length) {
765
+ lastWire.content = [
766
+ ...lastWire.content.slice(0, precedingBlockCount),
767
+ { cachePoint: { type: "default" } },
768
+ ...lastWire.content.slice(precedingBlockCount),
769
+ ];
770
+ }
522
771
  return;
523
- const firstBlock = lastWire.content[0];
524
- const text = lastWire.content.length === 1 && isRecord(firstBlock) && typeof firstBlock.text === "string"
525
- ? firstBlock.text
772
+ }
773
+ const trailingBlock = lastWire.content[precedingBlockCount];
774
+ const text = isRecord(trailingBlock) && typeof trailingBlock.text === "string"
775
+ ? trailingBlock.text
526
776
  : lastSource.content.toWellFormed();
527
777
  if (finalPrefix.length >= text.length) {
528
- appendBedrockCachePoint(lastWire, lastSource);
778
+ appendBedrockCachePoint(lastWire, true);
529
779
  return;
530
780
  }
531
781
  const durable = text.slice(0, finalPrefix.length);
532
782
  const volatile = text.slice(finalPrefix.length);
533
- if (durable.trim().length === 0)
783
+ if (durable.trim().length === 0) {
784
+ if (precedingBlockCount > 0 && precedingBlockCount <= lastWire.content.length) {
785
+ lastWire.content = [
786
+ ...lastWire.content.slice(0, precedingBlockCount),
787
+ { cachePoint: { type: "default" } },
788
+ ...lastWire.content.slice(precedingBlockCount),
789
+ ];
790
+ }
534
791
  return;
535
- lastWire.content = volatile.trim().length > 0
792
+ }
793
+ const splitBlocks = volatile.trim().length > 0
536
794
  ? [{ text: durable }, { cachePoint: { type: "default" } }, { text: volatile }]
537
795
  : [{ text: durable }, { cachePoint: { type: "default" } }];
796
+ lastWire.content = [
797
+ ...lastWire.content.slice(0, precedingBlockCount),
798
+ ...splitBlocks,
799
+ ];
538
800
  return;
539
801
  }
540
802
  if (finalPrefix.kind === "parts" && Array.isArray(lastSource.content)) {
541
803
  const k = countWireParts(lastSource.content, finalPrefix.count);
542
- if (k > 0 && k <= lastWire.content.length) {
804
+ const splitAt = precedingBlockCount + k;
805
+ if (splitAt > 0 && splitAt <= lastWire.content.length) {
543
806
  lastWire.content = [
544
- ...lastWire.content.slice(0, k),
807
+ ...lastWire.content.slice(0, splitAt),
545
808
  { cachePoint: { type: "default" } },
546
- ...lastWire.content.slice(k),
809
+ ...lastWire.content.slice(splitAt),
547
810
  ];
548
811
  }
549
812
  }
550
813
  };
551
814
  export const executePiTransportStream = async (opts) => {
552
- const { model, source, url, providerModel, fetch: configuredFetch, requestHeaders, messages, sessionId, operationSignal, requestSampling, body, outputTokenField = "max_tokens", effectiveMaxOutputTokens, nativeReasoningBudget, capacity, requestBody, effort, adaptiveEffort, nativeAdaptiveThinking, reasoningRequestBody, cacheBreakpoints, promptCacheStyle, userCachePrefixes, cacheAffinity, resolvedControls, rawBody, capture, reasoningStepDown = 0, observers, } = opts;
815
+ const { model, source, url, providerModel, fetch: configuredFetch, requestHeaders, messages, sessionId, operationSignal, requestSampling, body, outputTokenField = "max_tokens", effectiveMaxOutputTokens, nativeReasoningBudget, capacity, requestBody, effort, adaptiveEffort, nativeAdaptiveThinking, reasoningRequestBody, cacheBreakpoints, promptCacheStyle, userCachePrefixes, cacheAffinity, callKind = "emission", resolvedControls, rawBody, capture, reasoningStepDown = 0, observers, } = opts;
553
816
  const providerId = source.replace(/^provider:/, "");
554
817
  const effectiveModel = providerModel ?? {
555
818
  id: model,
@@ -579,7 +842,7 @@ export const executePiTransportStream = async (opts) => {
579
842
  maxTokens: effectiveModel.maxTokens ?? 32_768,
580
843
  ...(effectiveModel.headers === undefined ? {} : { headers: { ...effectiveModel.headers } }),
581
844
  });
582
- const context = toPiContext(messages);
845
+ const context = toPiContext(messages, effectiveModel, callKind);
583
846
  const rawChunks = [];
584
847
  let httpResponse;
585
848
  let httpErrorBody;
@@ -660,16 +923,131 @@ export const executePiTransportStream = async (opts) => {
660
923
  rawChunks.push(rawEvent);
661
924
  },
662
925
  };
926
+ const mergeConsecutiveUserMessages = (wireMessages, toTextBlock) => {
927
+ for (let i = 1; i < wireMessages.length; i += 1) {
928
+ const prev = wireMessages[i - 1];
929
+ const curr = wireMessages[i];
930
+ if (!isRecord(prev) || !isRecord(curr) || prev.role !== "user" || curr.role !== "user")
931
+ continue;
932
+ const prevBlocks = Array.isArray(prev.content)
933
+ ? [...prev.content]
934
+ : typeof prev.content === "string"
935
+ ? [toTextBlock(prev.content)]
936
+ : [];
937
+ const currBlocks = Array.isArray(curr.content)
938
+ ? curr.content
939
+ : typeof curr.content === "string"
940
+ ? [toTextBlock(curr.content)]
941
+ : [];
942
+ prev.content = [...prevBlocks, ...currBlocks];
943
+ wireMessages.splice(i, 1);
944
+ i -= 1;
945
+ }
946
+ };
947
+ const applyInPlaceOpenAICacheMarkers = (wireMessages, markerField, markerValue) => {
948
+ const markMsg = (msg) => {
949
+ if (!isRecord(msg))
950
+ return;
951
+ if (typeof msg.content === "string" && msg.content.trim().length > 0) {
952
+ msg.content = [{ type: "text", text: msg.content, [markerField]: markerValue }];
953
+ }
954
+ else if (Array.isArray(msg.content) && msg.content.length > 0) {
955
+ const last = msg.content.at(-1);
956
+ if (isRecord(last))
957
+ last[markerField] = markerValue;
958
+ }
959
+ };
960
+ if (cacheBreakpoints !== undefined) {
961
+ const first = wireMessages[0];
962
+ if (isRecord(first) && (first.role === "system" || first.role === "developer")) {
963
+ markMsg(first);
964
+ }
965
+ }
966
+ if (cacheBreakpoints !== "conversation")
967
+ return;
968
+ const firstNonSystem = firstNonSystemMessageIndex(messages);
969
+ const sourceIndices = [];
970
+ for (let i = firstNonSystem; i < messages.length; i += 1) {
971
+ if (hasEffectiveWireMessage(messages[i], true))
972
+ sourceIndices.push(i);
973
+ }
974
+ const nonSystem = wireMessages.filter((m) => isRecord(m) && m.role !== "system" && m.role !== "developer");
975
+ for (let w = 0; w < Math.min(nonSystem.length - 1, sourceIndices.length); w += 1) {
976
+ const srcIdx = sourceIndices[w];
977
+ const sourceMsg = messages[srcIdx];
978
+ const wireMsg = nonSystem[w];
979
+ if (sourceMsg.role === "user" && typeof sourceMsg.content === "string" && isRecord(wireMsg) && typeof wireMsg.content === "string") {
980
+ const split = splitHistoricalUserChars(wireMsg.content, userCachePrefixes?.[srcIdx]);
981
+ if (split !== null) {
982
+ wireMsg.content = [
983
+ { type: "text", text: split[0] },
984
+ { type: "text", text: split[1] },
985
+ ];
986
+ }
987
+ }
988
+ }
989
+ if (nonSystem.length >= 2)
990
+ markMsg(nonSystem[nonSystem.length - 2]);
991
+ const last = nonSystem.at(-1);
992
+ const lastSourceIdx = sourceIndices.at(-1);
993
+ const lastSource = lastSourceIdx === undefined ? undefined : messages[lastSourceIdx];
994
+ if (!isRecord(last) || last.role !== "user" || lastSourceIdx === undefined || lastSource === undefined || lastSource.role !== "user")
995
+ return;
996
+ const finalPrefix = userCachePrefixes?.[lastSourceIdx];
997
+ if (finalPrefix === undefined) {
998
+ markMsg(last);
999
+ return;
1000
+ }
1001
+ if (finalPrefix.kind === "chars" && typeof lastSource.content === "string") {
1002
+ if (finalPrefix.length <= 0)
1003
+ return;
1004
+ const text = typeof last.content === "string" ? last.content : lastSource.content.toWellFormed();
1005
+ if (finalPrefix.length >= text.length) {
1006
+ markMsg(last);
1007
+ return;
1008
+ }
1009
+ const durable = text.slice(0, finalPrefix.length);
1010
+ const volatile = text.slice(finalPrefix.length);
1011
+ if (durable.trim().length === 0)
1012
+ return;
1013
+ last.content = volatile.trim().length > 0
1014
+ ? [
1015
+ { type: "text", text: durable, [markerField]: markerValue },
1016
+ { type: "text", text: volatile },
1017
+ ]
1018
+ : [{ type: "text", text: durable, [markerField]: markerValue }];
1019
+ return;
1020
+ }
1021
+ if (finalPrefix.kind === "parts" && Array.isArray(lastSource.content) && Array.isArray(last.content)) {
1022
+ const k = countWireParts(lastSource.content, finalPrefix.count);
1023
+ if (k > 0 && k <= last.content.length && isRecord(last.content[k - 1])) {
1024
+ last.content[k - 1][markerField] = markerValue;
1025
+ }
1026
+ }
1027
+ };
663
1028
  const onPayload = async (payload) => {
664
1029
  const transformed = effectiveModel.onPayload !== undefined
665
1030
  ? await effectiveModel.onPayload(payload, effectiveModel)
666
1031
  : payload;
667
1032
  const params = isRecord(transformed) ? { ...transformed } : {};
1033
+ // {§provider-request-authority} — `"bare"` calls strip tool fields, and an empty `tools: []` array is never sent.
1034
+ if (callKind === "bare") {
1035
+ for (const field of ["tools", "tool_choice", "toolConfig", "functions", "function_call", "parallel_tool_calls"]) {
1036
+ delete params[field];
1037
+ }
1038
+ }
1039
+ else if (Array.isArray(params.tools) && params.tools.length === 0) {
1040
+ delete params.tools;
1041
+ }
668
1042
  if (effectiveModel.api === "openai-completions") {
1043
+ const hasToolMessages = messages.some((m) => m.role === "tool" || (m.toolCalls?.length ?? 0) > 0);
669
1044
  const hasNonImageFile = messages.some((m) => Array.isArray(m.content) && m.content.some((p) => p.type === "file" && !p.mediaType.startsWith("image/")));
670
1045
  if (promptCacheStyle === "openai-explicit") {
671
1046
  params.prompt_cache_options = { mode: "explicit" };
672
- if (cacheBreakpoints !== undefined || hasNonImageFile) {
1047
+ if (hasToolMessages && Array.isArray(params.messages)) {
1048
+ applyInPlaceOpenAICacheMarkers(params.messages, "prompt_cache_breakpoint", { mode: "explicit" });
1049
+ }
1050
+ else if (cacheBreakpoints !== undefined || hasNonImageFile) {
673
1051
  params.messages = mapOpenAIExplicitCacheMessages(messages, cacheBreakpoints, userCachePrefixes);
674
1052
  }
675
1053
  else if (messages[0]?.role === "system" && Array.isArray(params.messages)) {
@@ -679,6 +1057,11 @@ export const executePiTransportStream = async (opts) => {
679
1057
  }
680
1058
  }
681
1059
  }
1060
+ else if (hasToolMessages && Array.isArray(params.messages)) {
1061
+ if (cacheBreakpoints !== undefined) {
1062
+ applyInPlaceOpenAICacheMarkers(params.messages, "cache_control", { type: "ephemeral" });
1063
+ }
1064
+ }
682
1065
  else if (cacheBreakpoints !== undefined || hasNonImageFile) {
683
1066
  params.messages = mapOpenAICompatibleMessages(messages, cacheBreakpoints, userCachePrefixes);
684
1067
  }
@@ -692,7 +1075,7 @@ export const executePiTransportStream = async (opts) => {
692
1075
  else if (effectiveModel.api === "openai-responses") {
693
1076
  if (resolvedControls.topLogprobs !== undefined) {
694
1077
  const existingInclude = Array.isArray(params.include)
695
- ? params.include.filter((item) => typeof item === "string")
1078
+ ? params.include.filter((item) => typeof item !== "undefined" && typeof item === "string")
696
1079
  : [];
697
1080
  if (!existingInclude.includes("message.output_text.logprobs")) {
698
1081
  params.include = [...existingInclude, "message.output_text.logprobs"];
@@ -706,10 +1089,76 @@ export const executePiTransportStream = async (opts) => {
706
1089
  params.top_k = topK;
707
1090
  if (stopSequences !== undefined)
708
1091
  params.stop_sequences = stopSequences;
1092
+ if (Array.isArray(params.messages)) {
1093
+ mergeConsecutiveUserMessages(params.messages, (text) => ({ type: "text", text }));
1094
+ }
709
1095
  applyAnthropicPayloadAdjustments(params, messages, cacheBreakpoints, userCachePrefixes, resolvedControls.serviceTier);
710
1096
  }
711
1097
  else if (effectiveModel.api === "google-generative-ai") {
712
1098
  const config = isRecord(params.config) ? { ...params.config } : {};
1099
+ if (callKind === "bare") {
1100
+ delete config.tools;
1101
+ delete config.toolConfig;
1102
+ }
1103
+ if (Array.isArray(params.contents)) {
1104
+ const contents = params.contents;
1105
+ for (let i = 1; i < contents.length; i += 1) {
1106
+ const prev = contents[i - 1];
1107
+ const curr = contents[i];
1108
+ if (!isRecord(prev) || !isRecord(curr) || prev.role !== "user" || curr.role !== "user")
1109
+ continue;
1110
+ if (!Array.isArray(prev.parts) || !Array.isArray(curr.parts))
1111
+ continue;
1112
+ const prevParts = prev.parts;
1113
+ const currParts = curr.parts;
1114
+ const lastFunctionResponsePart = prevParts.findLast((part) => isRecord(part) && isRecord(part.functionResponse) && isRecord(part.functionResponse.response));
1115
+ const currHasFunctionResponse = currParts.some((part) => isRecord(part) && isRecord(part.functionResponse));
1116
+ if (isRecord(lastFunctionResponsePart)
1117
+ && isRecord(lastFunctionResponsePart.functionResponse)
1118
+ && isRecord(lastFunctionResponsePart.functionResponse.response)
1119
+ && !currHasFunctionResponse) {
1120
+ const fnResp = { ...lastFunctionResponsePart.functionResponse };
1121
+ const respObj = { ...lastFunctionResponsePart.functionResponse.response };
1122
+ const textChunks = currParts.flatMap((part) => isRecord(part) && typeof part.text === "string" && part.text.length > 0 ? [part.text] : []);
1123
+ if (textChunks.length > 0) {
1124
+ const respKey = "error" in respObj ? "error" : "output";
1125
+ const existing = typeof respObj[respKey] === "string" ? respObj[respKey] : "";
1126
+ const joined = textChunks.join("\n\n");
1127
+ respObj[respKey] = existing.length > 0 ? `${existing}\n\n${joined}` : joined;
1128
+ fnResp.response = respObj;
1129
+ }
1130
+ const inlineParts = currParts.filter((part) => isRecord(part) && isRecord(part.inlineData));
1131
+ if (inlineParts.length > 0) {
1132
+ const existingParts = Array.isArray(fnResp.parts) ? fnResp.parts : [];
1133
+ fnResp.parts = [...existingParts, ...inlineParts];
1134
+ }
1135
+ lastFunctionResponsePart.functionResponse = fnResp;
1136
+ contents.splice(i, 1);
1137
+ i -= 1;
1138
+ continue;
1139
+ }
1140
+ prev.parts = [...prevParts, ...currParts];
1141
+ contents.splice(i, 1);
1142
+ i -= 1;
1143
+ }
1144
+ for (const entry of params.contents) {
1145
+ if (isRecord(entry) && entry.role === "model" && Array.isArray(entry.parts)) {
1146
+ const firstFunctionCall = entry.parts.find((part) => isRecord(part) && isRecord(part.functionCall));
1147
+ if (isRecord(firstFunctionCall)) {
1148
+ const firstHasSignature = typeof firstFunctionCall.thoughtSignature === "string"
1149
+ && firstFunctionCall.thoughtSignature.length > 0;
1150
+ if (!firstHasSignature) {
1151
+ for (const part of entry.parts) {
1152
+ if (isRecord(part) && isRecord(part.functionCall)) {
1153
+ delete part.thoughtSignature;
1154
+ }
1155
+ }
1156
+ firstFunctionCall.thoughtSignature = "skip_thought_signature_validator";
1157
+ }
1158
+ }
1159
+ }
1160
+ }
1161
+ }
713
1162
  if (topP !== undefined)
714
1163
  config.topP = topP;
715
1164
  if (topK !== undefined)
@@ -752,6 +1201,12 @@ export const executePiTransportStream = async (opts) => {
752
1201
  if (resolvedControls.serviceTier !== undefined) {
753
1202
  params.serviceTier = { type: resolvedControls.serviceTier };
754
1203
  }
1204
+ if (callKind === "bare") {
1205
+ delete params.toolConfig;
1206
+ }
1207
+ if (Array.isArray(params.messages)) {
1208
+ mergeConsecutiveUserMessages(params.messages, (text) => ({ text }));
1209
+ }
755
1210
  applyBedrockPayloadAdjustments(params, messages, cacheBreakpoints, userCachePrefixes);
756
1211
  try {
757
1212
  capture?.record({
@@ -1110,6 +1565,13 @@ export const executePiTransportStream = async (opts) => {
1110
1565
  fields: {},
1111
1566
  channels: forensics.channels,
1112
1567
  toolCalls: messageInfo.toolCalls,
1568
+ replay: {
1569
+ api: effectiveModel.api,
1570
+ provider: effectiveModel.provider,
1571
+ model: effectiveModel.id,
1572
+ ...(messageInfo.thinking.length > 0 ? { thinking: messageInfo.thinking } : {}),
1573
+ ...(messageInfo.textSignature === undefined ? {} : { textSignature: messageInfo.textSignature }),
1574
+ },
1113
1575
  unmappedChunks: [],
1114
1576
  },
1115
1577
  ...(rawBody ? { rawBody: rawChunks } : {}),