@plurnk/plurnk-providers 6.1.1 → 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.
Files changed (49) hide show
  1. package/.env.defaults +5 -4
  2. package/README.md +3 -3
  3. package/SPEC.md +18 -15
  4. package/dist/InferenceAdmission.d.ts +10 -1
  5. package/dist/InferenceAdmission.d.ts.map +1 -1
  6. package/dist/InferenceAdmission.js +79 -15
  7. package/dist/InferenceAdmission.js.map +1 -1
  8. package/dist/Mock.d.ts +9 -0
  9. package/dist/Mock.d.ts.map +1 -1
  10. package/dist/Mock.js +13 -1
  11. package/dist/Mock.js.map +1 -1
  12. package/dist/ModelProvider.d.ts +3 -1
  13. package/dist/ModelProvider.d.ts.map +1 -1
  14. package/dist/ModelProvider.js +27 -9
  15. package/dist/ModelProvider.js.map +1 -1
  16. package/dist/ModelRequestBody.js +3 -3
  17. package/dist/ModelRequestBody.js.map +1 -1
  18. package/dist/catalogProvider.d.ts +2 -2
  19. package/dist/catalogProvider.d.ts.map +1 -1
  20. package/dist/catalogProvider.js +2 -1
  21. package/dist/catalogProvider.js.map +1 -1
  22. package/dist/compatibleProvider.d.ts.map +1 -1
  23. package/dist/compatibleProvider.js +6 -3
  24. package/dist/compatibleProvider.js.map +1 -1
  25. package/dist/env.d.ts +3 -1
  26. package/dist/env.d.ts.map +1 -1
  27. package/dist/env.js +24 -2
  28. package/dist/env.js.map +1 -1
  29. package/dist/index.d.ts +5 -3
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +3 -2
  32. package/dist/index.js.map +1 -1
  33. package/dist/modelAdapters.d.ts +2 -2
  34. package/dist/modelAdapters.d.ts.map +1 -1
  35. package/dist/modelAdapters.js +53 -4
  36. package/dist/modelAdapters.js.map +1 -1
  37. package/dist/pi-transport.d.ts +11 -10
  38. package/dist/pi-transport.d.ts.map +1 -1
  39. package/dist/pi-transport.js +752 -93
  40. package/dist/pi-transport.js.map +1 -1
  41. package/dist/stream-forensics.d.ts +4 -8
  42. package/dist/stream-forensics.d.ts.map +1 -1
  43. package/dist/stream-forensics.js +61 -192
  44. package/dist/stream-forensics.js.map +1 -1
  45. package/dist/types.d.ts +38 -5
  46. package/dist/types.d.ts.map +1 -1
  47. package/dist/types.js +15 -3
  48. package/dist/types.js.map +1 -1
  49. package/package.json +4 -4
@@ -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);
@@ -120,20 +280,125 @@ const toOpenAIPart = (part) => {
120
280
  image_url: { url: `data:${part.mediaType};base64,${base64}` },
121
281
  };
122
282
  };
283
+ export const formatCacheAffinityValue = (affinity, sessionId) => {
284
+ if (affinity.name !== "prompt_cache_key")
285
+ return sessionId;
286
+ const chars = Array.from(sessionId);
287
+ return chars.length <= 64 ? sessionId : chars.slice(0, 64).join("");
288
+ };
289
+ const firstNonSystemMessageIndex = (messages) => {
290
+ for (let i = 0; i < messages.length; i += 1) {
291
+ if (messages[i].role !== "system")
292
+ return i;
293
+ }
294
+ return messages.length;
295
+ };
296
+ const splitHistoricalUserChars = (text, prefix) => {
297
+ if (prefix?.kind !== "chars" || prefix.length <= 0 || prefix.length >= text.length)
298
+ return null;
299
+ const durable = text.slice(0, prefix.length);
300
+ const tail = text.slice(prefix.length);
301
+ return durable.trim().length > 0 && tail.trim().length > 0 ? [durable, tail] : null;
302
+ };
123
303
  const withEphemeralCacheControl = (parts, atIndex) => {
124
304
  if (atIndex >= 0 && atIndex < parts.length) {
125
305
  parts[atIndex] = { ...parts[atIndex], cache_control: { type: "ephemeral" } };
126
306
  }
127
307
  return parts;
128
308
  };
129
- const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, finalUserCachePrefix) => {
309
+ const withExplicitCacheBreakpoint = (parts, atIndex) => {
310
+ if (atIndex >= 0 && atIndex < parts.length) {
311
+ parts[atIndex] = { ...parts[atIndex], prompt_cache_breakpoint: { mode: "explicit" } };
312
+ }
313
+ return parts;
314
+ };
315
+ const mapOpenAIExplicitCacheMessages = (messages, cacheBreakpoints, userCachePrefixes) => {
130
316
  let lastLeadingSystemIndex = -1;
131
- let firstNonSystemIndex = messages.length;
132
- for (let i = 0; i < messages.length; i += 1) {
133
- if (messages[i].role !== "system") {
134
- firstNonSystemIndex = i;
135
- break;
317
+ const firstNonSystemIndex = firstNonSystemMessageIndex(messages);
318
+ for (let i = 0; i < firstNonSystemIndex; i += 1) {
319
+ if (cacheBreakpoints !== undefined && hasWireContent(messages[i])) {
320
+ lastLeadingSystemIndex = i;
136
321
  }
322
+ }
323
+ const unscopedConversation = cacheBreakpoints === "conversation" && userCachePrefixes === undefined;
324
+ return messages.map((message, index) => {
325
+ if (index === lastLeadingSystemIndex) {
326
+ const parts = typeof message.content === "string"
327
+ ? [{ type: "text", text: message.content }]
328
+ : message.content.map(toOpenAIPart);
329
+ return {
330
+ role: message.role,
331
+ content: withExplicitCacheBreakpoint(parts, parts.length - 1),
332
+ };
333
+ }
334
+ if (cacheBreakpoints === "conversation" && index >= firstNonSystemIndex && message.role === "user") {
335
+ const prefix = userCachePrefixes?.[index];
336
+ if (prefix === undefined) {
337
+ if (unscopedConversation && hasWireContent(message)) {
338
+ const parts = typeof message.content === "string"
339
+ ? [{ type: "text", text: message.content }]
340
+ : message.content.map(toOpenAIPart);
341
+ return {
342
+ role: message.role,
343
+ content: withExplicitCacheBreakpoint(parts, parts.length - 1),
344
+ };
345
+ }
346
+ }
347
+ else if (prefix.kind === "chars" && typeof message.content === "string") {
348
+ if (prefix.length <= 0) {
349
+ return { role: message.role, content: message.content };
350
+ }
351
+ if (prefix.length >= message.content.length) {
352
+ return message.content.trim().length > 0
353
+ ? {
354
+ role: message.role,
355
+ content: [{
356
+ type: "text",
357
+ text: message.content,
358
+ prompt_cache_breakpoint: { mode: "explicit" },
359
+ }],
360
+ }
361
+ : { role: message.role, content: message.content };
362
+ }
363
+ const durable = message.content.slice(0, prefix.length);
364
+ const tail = message.content.slice(prefix.length);
365
+ if (durable.trim().length === 0) {
366
+ return { role: message.role, content: message.content };
367
+ }
368
+ return {
369
+ role: message.role,
370
+ content: tail.trim().length > 0
371
+ ? [
372
+ { type: "text", text: durable, prompt_cache_breakpoint: { mode: "explicit" } },
373
+ { type: "text", text: tail },
374
+ ]
375
+ : [{ type: "text", text: durable, prompt_cache_breakpoint: { mode: "explicit" } }],
376
+ };
377
+ }
378
+ else if (prefix.kind === "parts" && Array.isArray(message.content)) {
379
+ const parts = message.content.map(toOpenAIPart);
380
+ const targetIndex = Math.min(prefix.count, parts.length) - 1;
381
+ return {
382
+ role: message.role,
383
+ content: prefix.count > 0
384
+ ? withExplicitCacheBreakpoint(parts, targetIndex)
385
+ : parts,
386
+ };
387
+ }
388
+ }
389
+ if (typeof message.content === "string") {
390
+ return { role: message.role, content: message.content };
391
+ }
392
+ return {
393
+ role: message.role,
394
+ content: message.content.map(toOpenAIPart),
395
+ };
396
+ });
397
+ };
398
+ const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, userCachePrefixes) => {
399
+ let lastLeadingSystemIndex = -1;
400
+ const firstNonSystemIndex = firstNonSystemMessageIndex(messages);
401
+ for (let i = 0; i < firstNonSystemIndex; i += 1) {
137
402
  if (cacheBreakpoints !== undefined && hasWireContent(messages[i])) {
138
403
  lastLeadingSystemIndex = i;
139
404
  }
@@ -155,7 +420,7 @@ const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, finalUserCacheP
155
420
  }
156
421
  }
157
422
  return messages.map((message, index) => {
158
- if (index === lastLeadingSystemIndex || index === penultimateNonSystemIndex) {
423
+ if (index === lastLeadingSystemIndex) {
159
424
  const parts = typeof message.content === "string"
160
425
  ? [{ type: "text", text: message.content }]
161
426
  : message.content.map(toOpenAIPart);
@@ -165,7 +430,8 @@ const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, finalUserCacheP
165
430
  };
166
431
  }
167
432
  if (index === finalUserIndex) {
168
- if (finalUserCachePrefix === undefined) {
433
+ const finalPrefix = userCachePrefixes?.[index];
434
+ if (finalPrefix === undefined) {
169
435
  const parts = typeof message.content === "string"
170
436
  ? [{ type: "text", text: message.content }]
171
437
  : message.content.map(toOpenAIPart);
@@ -174,18 +440,18 @@ const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, finalUserCacheP
174
440
  content: withEphemeralCacheControl(parts, parts.length - 1),
175
441
  };
176
442
  }
177
- if (finalUserCachePrefix.kind === "chars" && typeof message.content === "string") {
178
- if (finalUserCachePrefix.length <= 0) {
443
+ if (finalPrefix.kind === "chars" && typeof message.content === "string") {
444
+ if (finalPrefix.length <= 0) {
179
445
  return { role: message.role, content: message.content };
180
446
  }
181
- if (finalUserCachePrefix.length >= message.content.length) {
447
+ if (finalPrefix.length >= message.content.length) {
182
448
  return {
183
449
  role: message.role,
184
450
  content: [{ type: "text", text: message.content, cache_control: { type: "ephemeral" } }],
185
451
  };
186
452
  }
187
- const durable = message.content.slice(0, finalUserCachePrefix.length);
188
- const volatile = message.content.slice(finalUserCachePrefix.length);
453
+ const durable = message.content.slice(0, finalPrefix.length);
454
+ const volatile = message.content.slice(finalPrefix.length);
189
455
  if (durable.trim().length === 0) {
190
456
  return { role: message.role, content: message.content };
191
457
  }
@@ -199,17 +465,43 @@ const mapOpenAICompatibleMessages = (messages, cacheBreakpoints, finalUserCacheP
199
465
  : [{ type: "text", text: durable, cache_control: { type: "ephemeral" } }],
200
466
  };
201
467
  }
202
- if (finalUserCachePrefix.kind === "parts" && Array.isArray(message.content)) {
468
+ if (finalPrefix.kind === "parts" && Array.isArray(message.content)) {
203
469
  const parts = message.content.map(toOpenAIPart);
204
- const targetIndex = Math.min(finalUserCachePrefix.count, parts.length) - 1;
470
+ const targetIndex = Math.min(finalPrefix.count, parts.length) - 1;
205
471
  return {
206
472
  role: message.role,
207
- content: finalUserCachePrefix.count > 0
473
+ content: finalPrefix.count > 0
208
474
  ? withEphemeralCacheControl(parts, targetIndex)
209
475
  : parts,
210
476
  };
211
477
  }
212
478
  }
479
+ const historicalSplit = cacheBreakpoints === "conversation"
480
+ && index >= firstNonSystemIndex
481
+ && message.role === "user"
482
+ && typeof message.content === "string"
483
+ ? splitHistoricalUserChars(message.content, userCachePrefixes?.[index])
484
+ : null;
485
+ if (index === penultimateNonSystemIndex) {
486
+ const parts = historicalSplit !== null
487
+ ? [{ type: "text", text: historicalSplit[0] }, { type: "text", text: historicalSplit[1] }]
488
+ : typeof message.content === "string"
489
+ ? [{ type: "text", text: message.content }]
490
+ : message.content.map(toOpenAIPart);
491
+ return {
492
+ role: message.role,
493
+ content: withEphemeralCacheControl(parts, parts.length - 1),
494
+ };
495
+ }
496
+ if (historicalSplit !== null) {
497
+ return {
498
+ role: message.role,
499
+ content: [
500
+ { type: "text", text: historicalSplit[0] },
501
+ { type: "text", text: historicalSplit[1] },
502
+ ],
503
+ };
504
+ }
213
505
  if (typeof message.content === "string") {
214
506
  return { role: message.role, content: message.content };
215
507
  }
@@ -229,13 +521,16 @@ const markAnthropicMessageEnd = (wireMsg) => {
229
521
  return;
230
522
  }
231
523
  if (Array.isArray(wireMsg.content) && wireMsg.content.length > 0) {
232
- const last = wireMsg.content.at(-1);
233
- if (isRecord(last)) {
234
- 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
+ }
235
530
  }
236
531
  }
237
532
  };
238
- const applyAnthropicPayloadAdjustments = (params, messages, cacheBreakpoints, finalUserCachePrefix, serviceTier) => {
533
+ const applyAnthropicPayloadAdjustments = (params, messages, cacheBreakpoints, userCachePrefixes, serviceTier) => {
239
534
  if (serviceTier !== undefined) {
240
535
  params.service_tier = serviceTier;
241
536
  }
@@ -248,73 +543,161 @@ const applyAnthropicPayloadAdjustments = (params, messages, cacheBreakpoints, fi
248
543
  const wireMessages = Array.isArray(params.messages) ? params.messages : undefined;
249
544
  if (wireMessages === undefined)
250
545
  return;
251
- const effectiveNonSystem = messages.filter((m) => m.role !== "system" && hasWireContent(m));
252
- for (let i = 0; i < Math.min(wireMessages.length, effectiveNonSystem.length); i += 1) {
253
- const sourceMsg = effectiveNonSystem[i];
254
- const wireMsg = wireMessages[i];
255
- 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))
256
551
  continue;
257
- const nonBlankSourceParts = sourceMsg.content.filter((p) => p.type !== "text" || p.text.trim().length > 0);
258
- for (let j = 0; j < Math.min(nonBlankSourceParts.length, wireMsg.content.length); j += 1) {
259
- const sourcePart = nonBlankSourceParts[j];
260
- const wireBlock = wireMsg.content[j];
261
- if (sourcePart.type === "file"
262
- && sourcePart.mediaType === "application/pdf"
263
- && isRecord(wireBlock)
264
- && wireBlock.type === "image") {
265
- 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
+ }
266
569
  }
570
+ blockCursor += nonBlankSourceParts.length;
267
571
  }
268
572
  }
269
573
  if (cacheBreakpoints !== "conversation" || wireMessages.length === 0)
270
574
  return;
575
+ for (let w = 0; w < Math.min(wireMessages.length - 1, groups.length); w += 1) {
576
+ const group = groups[w];
577
+ const wireMsg = wireMessages[w];
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);
611
+ }
612
+ }
613
+ }
271
614
  if (wireMessages.length >= 2) {
272
615
  markAnthropicMessageEnd(wireMessages[wireMessages.length - 2]);
273
616
  }
274
617
  const lastWire = wireMessages.at(-1);
275
- const lastSource = effectiveNonSystem.at(-1);
276
- if (lastSource?.role !== "user" || !isRecord(lastWire) || lastWire.role !== "user")
618
+ const lastGroup = groups.at(-1);
619
+ const lastSourceIdx = lastGroup?.sourceIndices.at(-1);
620
+ const lastSource = lastSourceIdx === undefined ? undefined : messages[lastSourceIdx];
621
+ if (lastGroup === undefined || lastGroup.role !== "user" || lastSourceIdx === undefined || lastSource === undefined || !isRecord(lastWire) || lastWire.role !== "user")
622
+ return;
623
+ if (lastSource.role === "tool") {
624
+ markAnthropicMessageEnd(lastWire);
277
625
  return;
278
- if (finalUserCachePrefix === undefined) {
626
+ }
627
+ const precedingBlockCount = lastGroup.sourceIndices
628
+ .slice(0, -1)
629
+ .reduce((sum, idx) => sum + sourceWireBlockCount(messages[idx]), 0);
630
+ const finalPrefix = userCachePrefixes?.[lastSourceIdx];
631
+ if (finalPrefix === undefined) {
279
632
  markAnthropicMessageEnd(lastWire);
280
633
  return;
281
634
  }
282
- if (finalUserCachePrefix.kind === "chars" && typeof lastSource.content === "string") {
283
- if (finalUserCachePrefix.length <= 0)
635
+ if (finalPrefix.kind === "chars" && typeof lastSource.content === "string") {
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
+ }
284
642
  return;
285
- if (finalUserCachePrefix.length >= lastSource.content.length) {
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();
650
+ if (finalPrefix.length >= text.length) {
286
651
  markAnthropicMessageEnd(lastWire);
287
652
  return;
288
653
  }
289
- const durable = lastSource.content.slice(0, finalUserCachePrefix.length);
290
- const volatile = lastSource.content.slice(finalUserCachePrefix.length);
291
- if (durable.trim().length === 0)
654
+ const durable = text.slice(0, finalPrefix.length);
655
+ const volatile = text.slice(finalPrefix.length);
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
+ }
292
662
  return;
293
- lastWire.content = volatile.trim().length > 0
663
+ }
664
+ const splitBlocks = volatile.trim().length > 0
294
665
  ? [
295
666
  { type: "text", text: durable, cache_control: { type: "ephemeral" } },
296
667
  { type: "text", text: volatile },
297
668
  ]
298
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
+ }
299
679
  return;
300
680
  }
301
- if (finalUserCachePrefix.kind === "parts" && Array.isArray(lastSource.content) && Array.isArray(lastWire.content)) {
302
- const k = countWireParts(lastSource.content, finalUserCachePrefix.count);
303
- if (k > 0 && k <= lastWire.content.length) {
304
- const target = lastWire.content[k - 1];
681
+ if (finalPrefix.kind === "parts" && Array.isArray(lastSource.content) && Array.isArray(lastWire.content)) {
682
+ const k = countWireParts(lastSource.content, finalPrefix.count);
683
+ const targetIdx = precedingBlockCount + k - 1;
684
+ if (targetIdx >= 0 && targetIdx < lastWire.content.length) {
685
+ const target = lastWire.content[targetIdx];
305
686
  if (isRecord(target))
306
687
  target.cache_control = { type: "ephemeral" };
307
688
  }
308
689
  }
309
690
  };
310
- const appendBedrockCachePoint = (wireMsg, sourceMsg) => {
311
- if (sourceMsg === undefined || !hasWireContent(sourceMsg))
691
+ const appendBedrockCachePoint = (wireMsg, hasContent = true) => {
692
+ if (!hasContent)
312
693
  return;
313
694
  if (!isRecord(wireMsg) || !Array.isArray(wireMsg.content) || wireMsg.content.length === 0)
314
695
  return;
696
+ if (wireMsg.content.every((block) => isRecord(block) && "reasoningContent" in block))
697
+ return;
315
698
  wireMsg.content = [...wireMsg.content, { cachePoint: { type: "default" } }];
316
699
  };
317
- const applyBedrockPayloadAdjustments = (params, messages, cacheBreakpoints, finalUserCachePrefix) => {
700
+ const applyBedrockPayloadAdjustments = (params, messages, cacheBreakpoints, userCachePrefixes) => {
318
701
  if (cacheBreakpoints === undefined)
319
702
  return;
320
703
  if (Array.isArray(params.system) && params.system.length > 0) {
@@ -325,48 +708,111 @@ const applyBedrockPayloadAdjustments = (params, messages, cacheBreakpoints, fina
325
708
  const wireMessages = Array.isArray(params.messages) ? params.messages : undefined;
326
709
  if (wireMessages === undefined || wireMessages.length === 0)
327
710
  return;
328
- const bedrockNonSystem = messages.filter((m) => m.role === "user" || (m.role === "assistant" && hasWireContent(m)));
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];
714
+ const wireMsg = wireMessages[w];
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
+ }
731
+ }
732
+ blockCursor += sourceWireBlockCount(sourceMsg);
733
+ }
734
+ }
329
735
  if (wireMessages.length >= 2) {
330
- appendBedrockCachePoint(wireMessages[wireMessages.length - 2], bedrockNonSystem[bedrockNonSystem.length - 2]);
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);
331
740
  }
332
741
  const lastWire = wireMessages.at(-1);
333
- const lastSource = bedrockNonSystem.at(-1);
334
- if (lastSource?.role !== "user" || !hasWireContent(lastSource) || !isRecord(lastWire) || lastWire.role !== "user" || !Array.isArray(lastWire.content)) {
742
+ const lastGroup = groups.at(-1);
743
+ const lastSourceIdx = lastGroup?.sourceIndices.at(-1);
744
+ const lastSource = lastSourceIdx === undefined ? undefined : messages[lastSourceIdx];
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);
335
750
  return;
336
751
  }
337
- if (finalUserCachePrefix === undefined) {
338
- appendBedrockCachePoint(lastWire, lastSource);
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];
758
+ if (finalPrefix === undefined) {
759
+ appendBedrockCachePoint(lastWire, true);
339
760
  return;
340
761
  }
341
- if (finalUserCachePrefix.kind === "chars" && typeof lastSource.content === "string") {
342
- if (finalUserCachePrefix.length <= 0)
762
+ if (finalPrefix.kind === "chars" && typeof lastSource.content === "string") {
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
+ }
343
771
  return;
344
- if (finalUserCachePrefix.length >= lastSource.content.length) {
345
- appendBedrockCachePoint(lastWire, lastSource);
772
+ }
773
+ const trailingBlock = lastWire.content[precedingBlockCount];
774
+ const text = isRecord(trailingBlock) && typeof trailingBlock.text === "string"
775
+ ? trailingBlock.text
776
+ : lastSource.content.toWellFormed();
777
+ if (finalPrefix.length >= text.length) {
778
+ appendBedrockCachePoint(lastWire, true);
346
779
  return;
347
780
  }
348
- const durable = lastSource.content.slice(0, finalUserCachePrefix.length);
349
- const volatile = lastSource.content.slice(finalUserCachePrefix.length);
350
- if (durable.trim().length === 0)
781
+ const durable = text.slice(0, finalPrefix.length);
782
+ const volatile = text.slice(finalPrefix.length);
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
+ }
351
791
  return;
352
- lastWire.content = volatile.trim().length > 0
792
+ }
793
+ const splitBlocks = volatile.trim().length > 0
353
794
  ? [{ text: durable }, { cachePoint: { type: "default" } }, { text: volatile }]
354
795
  : [{ text: durable }, { cachePoint: { type: "default" } }];
796
+ lastWire.content = [
797
+ ...lastWire.content.slice(0, precedingBlockCount),
798
+ ...splitBlocks,
799
+ ];
355
800
  return;
356
801
  }
357
- if (finalUserCachePrefix.kind === "parts" && Array.isArray(lastSource.content)) {
358
- const k = countWireParts(lastSource.content, finalUserCachePrefix.count);
359
- if (k > 0 && k <= lastWire.content.length) {
802
+ if (finalPrefix.kind === "parts" && Array.isArray(lastSource.content)) {
803
+ const k = countWireParts(lastSource.content, finalPrefix.count);
804
+ const splitAt = precedingBlockCount + k;
805
+ if (splitAt > 0 && splitAt <= lastWire.content.length) {
360
806
  lastWire.content = [
361
- ...lastWire.content.slice(0, k),
807
+ ...lastWire.content.slice(0, splitAt),
362
808
  { cachePoint: { type: "default" } },
363
- ...lastWire.content.slice(k),
809
+ ...lastWire.content.slice(splitAt),
364
810
  ];
365
811
  }
366
812
  }
367
813
  };
368
814
  export const executePiTransportStream = async (opts) => {
369
- 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, finalUserCachePrefix, 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;
370
816
  const providerId = source.replace(/^provider:/, "");
371
817
  const effectiveModel = providerModel ?? {
372
818
  id: model,
@@ -396,7 +842,7 @@ export const executePiTransportStream = async (opts) => {
396
842
  maxTokens: effectiveModel.maxTokens ?? 32_768,
397
843
  ...(effectiveModel.headers === undefined ? {} : { headers: { ...effectiveModel.headers } }),
398
844
  });
399
- const context = toPiContext(messages);
845
+ const context = toPiContext(messages, effectiveModel, callKind);
400
846
  const rawChunks = [];
401
847
  let httpResponse;
402
848
  let httpErrorBody;
@@ -477,15 +923,147 @@ export const executePiTransportStream = async (opts) => {
477
923
  rawChunks.push(rawEvent);
478
924
  },
479
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
+ };
480
1028
  const onPayload = async (payload) => {
481
1029
  const transformed = effectiveModel.onPayload !== undefined
482
1030
  ? await effectiveModel.onPayload(payload, effectiveModel)
483
1031
  : payload;
484
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
+ }
485
1042
  if (effectiveModel.api === "openai-completions") {
486
- if (cacheBreakpoints !== undefined
487
- || messages.some((m) => Array.isArray(m.content) && m.content.some((p) => p.type === "file" && !p.mediaType.startsWith("image/")))) {
488
- params.messages = mapOpenAICompatibleMessages(messages, cacheBreakpoints, finalUserCachePrefix);
1043
+ const hasToolMessages = messages.some((m) => m.role === "tool" || (m.toolCalls?.length ?? 0) > 0);
1044
+ const hasNonImageFile = messages.some((m) => Array.isArray(m.content) && m.content.some((p) => p.type === "file" && !p.mediaType.startsWith("image/")));
1045
+ if (promptCacheStyle === "openai-explicit") {
1046
+ params.prompt_cache_options = { mode: "explicit" };
1047
+ if (hasToolMessages && Array.isArray(params.messages)) {
1048
+ applyInPlaceOpenAICacheMarkers(params.messages, "prompt_cache_breakpoint", { mode: "explicit" });
1049
+ }
1050
+ else if (cacheBreakpoints !== undefined || hasNonImageFile) {
1051
+ params.messages = mapOpenAIExplicitCacheMessages(messages, cacheBreakpoints, userCachePrefixes);
1052
+ }
1053
+ else if (messages[0]?.role === "system" && Array.isArray(params.messages)) {
1054
+ const first = params.messages[0];
1055
+ if (!isRecord(first) || (first.role !== "system" && first.role !== "developer")) {
1056
+ params.messages.unshift({ role: "system", content: "" });
1057
+ }
1058
+ }
1059
+ }
1060
+ else if (hasToolMessages && Array.isArray(params.messages)) {
1061
+ if (cacheBreakpoints !== undefined) {
1062
+ applyInPlaceOpenAICacheMarkers(params.messages, "cache_control", { type: "ephemeral" });
1063
+ }
1064
+ }
1065
+ else if (cacheBreakpoints !== undefined || hasNonImageFile) {
1066
+ params.messages = mapOpenAICompatibleMessages(messages, cacheBreakpoints, userCachePrefixes);
489
1067
  }
490
1068
  else if (messages[0]?.role === "system" && Array.isArray(params.messages)) {
491
1069
  const first = params.messages[0];
@@ -497,7 +1075,7 @@ export const executePiTransportStream = async (opts) => {
497
1075
  else if (effectiveModel.api === "openai-responses") {
498
1076
  if (resolvedControls.topLogprobs !== undefined) {
499
1077
  const existingInclude = Array.isArray(params.include)
500
- ? params.include.filter((item) => typeof item === "string")
1078
+ ? params.include.filter((item) => typeof item !== "undefined" && typeof item === "string")
501
1079
  : [];
502
1080
  if (!existingInclude.includes("message.output_text.logprobs")) {
503
1081
  params.include = [...existingInclude, "message.output_text.logprobs"];
@@ -511,10 +1089,76 @@ export const executePiTransportStream = async (opts) => {
511
1089
  params.top_k = topK;
512
1090
  if (stopSequences !== undefined)
513
1091
  params.stop_sequences = stopSequences;
514
- applyAnthropicPayloadAdjustments(params, messages, cacheBreakpoints, finalUserCachePrefix, resolvedControls.serviceTier);
1092
+ if (Array.isArray(params.messages)) {
1093
+ mergeConsecutiveUserMessages(params.messages, (text) => ({ type: "text", text }));
1094
+ }
1095
+ applyAnthropicPayloadAdjustments(params, messages, cacheBreakpoints, userCachePrefixes, resolvedControls.serviceTier);
515
1096
  }
516
1097
  else if (effectiveModel.api === "google-generative-ai") {
517
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
+ }
518
1162
  if (topP !== undefined)
519
1163
  config.topP = topP;
520
1164
  if (topK !== undefined)
@@ -557,7 +1201,13 @@ export const executePiTransportStream = async (opts) => {
557
1201
  if (resolvedControls.serviceTier !== undefined) {
558
1202
  params.serviceTier = { type: resolvedControls.serviceTier };
559
1203
  }
560
- applyBedrockPayloadAdjustments(params, messages, cacheBreakpoints, finalUserCachePrefix);
1204
+ if (callKind === "bare") {
1205
+ delete params.toolConfig;
1206
+ }
1207
+ if (Array.isArray(params.messages)) {
1208
+ mergeConsecutiveUserMessages(params.messages, (text) => ({ text }));
1209
+ }
1210
+ applyBedrockPayloadAdjustments(params, messages, cacheBreakpoints, userCachePrefixes);
561
1211
  try {
562
1212
  capture?.record({
563
1213
  method: "POST",
@@ -591,7 +1241,7 @@ export const executePiTransportStream = async (opts) => {
591
1241
  ...(stopSequences === undefined ? {} : { stop: stopSequences }),
592
1242
  ...(seed === undefined ? {} : { seed }),
593
1243
  ...(reasoningRequestBody === undefined ? {} : reasoningRequestBody(effort.mode, capacity.reasoningBudget)),
594
- ...(cacheAffinity?.target === "body" ? { [cacheAffinity.name]: sessionId } : {}),
1244
+ ...(cacheAffinity?.target === "body" ? { [cacheAffinity.name]: formatCacheAffinityValue(cacheAffinity, sessionId) } : {}),
595
1245
  ...(resolvedControls.topLogprobs === undefined ? {} : { logprobs: true, top_logprobs: resolvedControls.topLogprobs }),
596
1246
  ...(resolvedControls.serviceTier === undefined ? {} : { service_tier: resolvedControls.serviceTier }),
597
1247
  };
@@ -602,6 +1252,7 @@ export const executePiTransportStream = async (opts) => {
602
1252
  ...commonOptions,
603
1253
  fetch: customFetch,
604
1254
  apiKey: effectiveModel.apiKey ?? "plurnk-unset",
1255
+ cacheRetention: "none",
605
1256
  ...(reasoningEffort === undefined ? {} : { reasoningEffort }),
606
1257
  ...(Object.keys(samplingParams).length === 0 ? {} : { samplingParams }),
607
1258
  onPayload,
@@ -611,7 +1262,7 @@ export const executePiTransportStream = async (opts) => {
611
1262
  const reasoningEffort = reasoning === "none" || reasoning === "provider-default" ? undefined : reasoning;
612
1263
  const samplingParams = {
613
1264
  ...(topP === undefined ? {} : { top_p: topP }),
614
- ...(cacheAffinity?.target === "body" ? { [cacheAffinity.name]: sessionId } : {}),
1265
+ ...(cacheAffinity?.target === "body" ? { [cacheAffinity.name]: formatCacheAffinityValue(cacheAffinity, sessionId) } : {}),
615
1266
  ...(resolvedControls.topLogprobs === undefined ? {} : { top_logprobs: resolvedControls.topLogprobs }),
616
1267
  ...(resolvedControls.serviceTier === undefined ? {} : { service_tier: resolvedControls.serviceTier }),
617
1268
  };
@@ -619,6 +1270,7 @@ export const executePiTransportStream = async (opts) => {
619
1270
  ...commonOptions,
620
1271
  fetch: customFetch,
621
1272
  apiKey: effectiveModel.apiKey ?? "plurnk-unset",
1273
+ cacheRetention: "none",
622
1274
  ...(reasoningEffort === undefined ? {} : { reasoningEffort }),
623
1275
  ...(Object.keys(samplingParams).length === 0 ? {} : { samplingParams }),
624
1276
  onPayload,
@@ -913,6 +1565,13 @@ export const executePiTransportStream = async (opts) => {
913
1565
  fields: {},
914
1566
  channels: forensics.channels,
915
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
+ },
916
1575
  unmappedChunks: [],
917
1576
  },
918
1577
  ...(rawBody ? { rawBody: rawChunks } : {}),