@jeffusion/bungee-llms 6.11.2 → 6.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3984,6 +3984,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3984
3984
  const callIds = new Set;
3985
3985
  const callToolNames = new Map;
3986
3986
  const answered = new Set;
3987
+ const deferredInstructions = [];
3988
+ const normalizeChatHistory = protocol === "chat_completions" && !options.preserveContentOrder;
3987
3989
  const projectedToolImages = [];
3988
3990
  let pendingReasoning = "";
3989
3991
  const addMessage = (message) => {
@@ -4009,6 +4011,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
4009
4011
  flushToolImages();
4010
4012
  if (item.type === "additional_tools")
4011
4013
  return;
4014
+ if (deferredInstructions.length && item.type !== "function_call_output" && item.type !== "custom_tool_call_output" && !((item.type === undefined || item.type === "message") && (item.role === "system" || item.role === "developer")))
4015
+ fail("invalid_tool_history", "Only instructions and tool results may follow a deferred instruction until all tool results arrive", messageParam);
4012
4016
  if (item.type === "agent_message") {
4013
4017
  validateAgentMessage(item, messageParam, capabilities.limits);
4014
4018
  if (capabilities.agentMessageDelivery !== "plaintext")
@@ -4087,6 +4091,13 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
4087
4091
  }
4088
4092
  }
4089
4093
  addMessage({ role: "tool", tool_call_id: id, content: output, ...item.is_error === true ? { is_error: true } : {} });
4094
+ if (callIds.size === answered.size) {
4095
+ for (const instruction of deferredInstructions) {
4096
+ messageParam = instruction.param;
4097
+ addMessage(instruction.message);
4098
+ }
4099
+ deferredInstructions.length = 0;
4100
+ }
4090
4101
  return;
4091
4102
  }
4092
4103
  if (item.type !== undefined && item.type !== "message")
@@ -4103,6 +4114,11 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
4103
4114
  message.reasoning_content = pendingReasoning;
4104
4115
  pendingReasoning = "";
4105
4116
  }
4117
+ if (normalizeChatHistory && (role === "system" || role === "developer") && callIds.size !== answered.size) {
4118
+ deferredInstructions.push({ message, param: messageParam });
4119
+ diagnostics.push({ param: messageParam, action: "mapped", reason: "chat_instruction_deferred_until_tool_results" });
4120
+ return;
4121
+ }
4106
4122
  if (protocol === "chat_completions" && !options.preserveContentOrder && role === "assistant") {
4107
4123
  const previous = chat.at(-1);
4108
4124
  if (previous?.role === "assistant") {
@@ -4116,6 +4132,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
4116
4132
  });
4117
4133
  if (pendingReasoning)
4118
4134
  fail("unsupported_reasoning", "Orphan reasoning history cannot be represented");
4135
+ if (deferredInstructions.length)
4136
+ fail("invalid_tool_history", "Deferred instructions require all pending tool results", deferredInstructions[0].param);
4119
4137
  flushToolImages(true);
4120
4138
  const result = { model, messages: chat };
4121
4139
  const omittedAllTools = !tools.length && diagnostics.some((item) => item.reason === "optional_hosted_web_search_unavailable");
@@ -4219,6 +4237,28 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
4219
4237
  return { body: result, toolNames, canonicalInput, diagnostics };
4220
4238
  }
4221
4239
  // src/responses-codec/events.ts
4240
+ function chatOutput(raw) {
4241
+ const keys = ["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "function_call", "annotations"];
4242
+ const delta = Object.fromEntries(keys.filter((key) => raw[key] != null).map((key) => [key, raw[key]]));
4243
+ const provider = raw.provider_specific_fields == null ? {} : record(raw.provider_specific_fields, "provider_specific_fields");
4244
+ for (const key of ["refusal", "reasoning_content", "reasoning"]) {
4245
+ if (provider[key] == null)
4246
+ continue;
4247
+ if (delta[key] != null && delta[key] !== provider[key])
4248
+ fail("unsupported_content", "Conflicting provider output fields", `provider_specific_fields.${key}`);
4249
+ delta[key] = provider[key];
4250
+ }
4251
+ for (const key of ["audio", "thinking_blocks", "reasoning_details", "signature", "thought_signature", "thoughtSignature", "encrypted_content", "encrypted_function_args"])
4252
+ if (raw[key] != null || provider[key] != null)
4253
+ fail("unsupported_content", "Provider output authority cannot cross protocols", key);
4254
+ for (const key of ["content", "tool_calls", "function_call", "annotations"])
4255
+ if (provider[key] != null)
4256
+ fail("unsupported_content", "Nested provider output cannot cross protocols", `provider_specific_fields.${key}`);
4257
+ if (delta.reasoning_content != null && delta.reasoning != null && delta.reasoning_content !== delta.reasoning)
4258
+ fail("unsupported_content", "Conflicting reasoning fields", "reasoning");
4259
+ return delta;
4260
+ }
4261
+
4222
4262
  class ResponsesEventEncoder {
4223
4263
  protocol;
4224
4264
  model;
@@ -4428,10 +4468,7 @@ class ResponsesEventEncoder {
4428
4468
  const choice = record(choices[0], "choice");
4429
4469
  if (choice.index !== undefined && choice.index !== 0)
4430
4470
  fail("unsupported_response", "Only choice index 0 is supported");
4431
- const delta = record(choice.delta ?? {}, "choice delta");
4432
- for (const key of Object.keys(delta))
4433
- if (!["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "function_call", "annotations"].includes(key))
4434
- fail("unsupported_content", `Unsupported Chat output field ${key}`);
4471
+ const delta = chatOutput(record(choice.delta ?? {}, "choice delta"));
4435
4472
  if (delta.annotations !== undefined && list(delta.annotations, "annotations").length)
4436
4473
  fail("unsupported_content", "Chat output annotations cannot be faithfully converted");
4437
4474
  if (delta.role !== undefined && delta.role !== "assistant")
@@ -4629,7 +4666,7 @@ function encodeResponsesResult(raw, protocol, model, toolNames = new Map, config
4629
4666
  const choice = record(choices[0], "choice");
4630
4667
  const message = record(choice.message, "message");
4631
4668
  const delta = { ...message };
4632
- if (message.tool_calls !== undefined)
4669
+ if (message.tool_calls != null)
4633
4670
  delta.tool_calls = list(message.tool_calls, "tool_calls").map((tool, index) => ({ ...record(tool, "tool call"), index }));
4634
4671
  encoder.push({ choices: [{ index: choice.index ?? 0, delta, finish_reason: choice.finish_reason }], usage: body.usage });
4635
4672
  } else {
@@ -5359,7 +5396,7 @@ function convertProtocolRequest(raw, context) {
5359
5396
  fail("unsupported_reasoning", "Source thinking mode needs an explicit target wire policy", "thinking");
5360
5397
  }
5361
5398
  if (target === "responses") {
5362
- const validation = decodeResponsesRequest({ ...b, reasoning: reasoning ? { ...reasoning, effort: undefined } : undefined }, "chat_completions", { ...cap, reasoningHistory: true });
5399
+ const validation = decodeResponsesRequest({ ...b, reasoning: reasoning ? { ...reasoning, effort: undefined } : undefined }, "chat_completions", { ...cap, reasoningHistory: true }, { preserveContentOrder: true, toolOutputImages: true });
5363
5400
  if (mappedEffort !== undefined)
5364
5401
  b.reasoning = { ...reasoning, effort: mappedEffort };
5365
5402
  else if (reasoning?.effort !== undefined)
@@ -5618,20 +5655,14 @@ function validateProviderResponse(raw, protocol) {
5618
5655
  if (b.error !== undefined || b.type === "error")
5619
5656
  return b;
5620
5657
  if (protocol === "chat_completions") {
5621
- fields2(b, ["id", "object", "created", "model", "choices", "usage", "system_fingerprint", "service_tier"], "", "unsupported_response");
5622
5658
  for (const rawChoice of list(b.choices, "choices")) {
5623
5659
  const c = record(rawChoice, "choice");
5624
- fields2(c, ["index", "message", "finish_reason", "logprobs", "provider_specific_fields"], "choices", "unsupported_response");
5625
- if (c.provider_specific_fields !== undefined) {
5626
- const providerFields = c.provider_specific_fields;
5627
- if (providerFields === null || typeof providerFields !== "object" || Array.isArray(providerFields) || Object.keys(providerFields).length)
5628
- fail("unsupported_response", "Only empty provider-specific choice fields are portable", "choices.provider_specific_fields");
5629
- }
5660
+ if (c.provider_specific_fields != null)
5661
+ record(c.provider_specific_fields, "choices.provider_specific_fields");
5630
5662
  if (c.logprobs != null)
5631
5663
  fail("unsupported_response", "Log probabilities cannot cross protocols", "choices.logprobs");
5632
5664
  const m = record(c.message, "message");
5633
- fields2(m, ["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "annotations"], "message", "unsupported_content");
5634
- for (const [callIndex, rawCall] of (m.tool_calls === undefined ? [] : list(m.tool_calls, "tool_calls")).entries()) {
5665
+ for (const [callIndex, rawCall] of (m.tool_calls == null ? [] : list(m.tool_calls, "tool_calls")).entries()) {
5635
5666
  const call = record(rawCall, "call");
5636
5667
  fields2(call, ["id", "type", "function", "index"], "tool_calls", "unsupported_tool");
5637
5668
  if (call.index !== undefined && integer(call.index, `tool_calls[${callIndex}].index`) !== callIndex)
@@ -5641,7 +5672,6 @@ function validateProviderResponse(raw, protocol) {
5641
5672
  }
5642
5673
  validateUsage(b.usage, protocol);
5643
5674
  } else if (protocol === "anthropic_messages") {
5644
- fields2(b, ["id", "type", "role", "model", "content", "stop_reason", "stop_sequence", "usage"], "", "unsupported_response");
5645
5675
  if (b.role !== undefined && b.role !== "assistant")
5646
5676
  fail("unsupported_response", "Response role must be assistant", "role");
5647
5677
  for (const rawPart of list(b.content, "content"))
@@ -5847,7 +5877,6 @@ class GeminiCanonicalDecoder {
5847
5877
  }
5848
5878
  push(raw) {
5849
5879
  const b = record(raw, "Gemini response");
5850
- fields2(b, ["candidates", "usageMetadata", "modelVersion", "responseId", "promptFeedback", "error"], "", "unsupported_response");
5851
5880
  if (b.error !== undefined)
5852
5881
  return this.decorate(this.encoder.push({ error: safeProtocolError() }));
5853
5882
  const events = [];
@@ -5864,7 +5893,6 @@ class GeminiCanonicalDecoder {
5864
5893
  fail("unsupported_response", "Multiple Gemini candidates cannot cross protocols", "candidates");
5865
5894
  if (candidates.length) {
5866
5895
  const c = record(candidates[0], "candidate");
5867
- fields2(c, ["index", "content", "finishReason", "safetyRatings", "finishMessage"], "candidates", "unsupported_response");
5868
5896
  if (c.index !== undefined && c.index !== 0)
5869
5897
  fail("unsupported_response", "Only candidate index zero is supported", "candidates.index");
5870
5898
  if (c.content !== undefined && c.content !== null) {
@@ -6470,9 +6498,9 @@ function createProtocolSession(context) {
6470
6498
  fail("invalid_session", "A session can convert one JSON response or one stream");
6471
6499
  mode = "json";
6472
6500
  finished = true;
6501
+ const body = record(raw, "response");
6502
+ serialized(body, limits(context.capabilities?.limits).maxOutputBytes, "response");
6473
6503
  if (same) {
6474
- const body = record(raw, "response");
6475
- serialized(body, limits(context.capabilities?.limits).maxOutputBytes, "response");
6476
6504
  return body;
6477
6505
  }
6478
6506
  const canonical = canonicalResponse(raw, context, names);
@@ -6486,9 +6514,9 @@ function createProtocolSession(context) {
6486
6514
  if (finished || mode === "json")
6487
6515
  fail("invalid_stream", "Event arrived after session completion");
6488
6516
  mode = "stream";
6517
+ const e = record(raw, "event");
6518
+ serialized(e, limits(context.capabilities?.limits).maxOutputBytes, "event");
6489
6519
  if (same) {
6490
- const e = record(raw, "event");
6491
- serialized(e, limits(context.capabilities?.limits).maxOutputBytes, "event");
6492
6520
  if (context.targetProtocol === "anthropic_messages") {
6493
6521
  if (passAnthropicStopped)
6494
6522
  fail("invalid_stream", "Event arrived after message_stop");
@@ -6566,11 +6594,11 @@ function validateStreamEnvelope(raw, protocol) {
6566
6594
  if (e.error !== undefined || e.type === "error")
6567
6595
  return;
6568
6596
  if (protocol === "chat_completions") {
6569
- fields2(e, ["id", "object", "created", "model", "choices", "usage", "system_fingerprint", "service_tier"], "", "unsupported_response");
6570
6597
  if (Array.isArray(e.choices))
6571
6598
  for (const rawChoice of e.choices) {
6572
6599
  const c = record(rawChoice, "choice");
6573
- fields2(c, ["index", "delta", "finish_reason", "logprobs"], "choices", "unsupported_response");
6600
+ if (c.provider_specific_fields != null)
6601
+ record(c.provider_specific_fields, "choices.provider_specific_fields");
6574
6602
  if (c.logprobs != null)
6575
6603
  fail("unsupported_response", "Log probabilities cannot cross protocols", "choices.logprobs");
6576
6604
  const d = record(c.delta ?? {}, "delta");
@@ -6584,8 +6612,6 @@ function validateStreamEnvelope(raw, protocol) {
6584
6612
  }
6585
6613
  validateUsage(e.usage, protocol);
6586
6614
  } else {
6587
- const allowed = e.type === "message_start" ? ["type", "message"] : e.type === "message_delta" ? ["type", "delta", "usage"] : e.type === "content_block_start" ? ["type", "index", "content_block"] : e.type === "content_block_delta" ? ["type", "index", "delta"] : e.type === "content_block_stop" ? ["type", "index"] : ["type"];
6588
- fields2(e, allowed, "", "unsupported_response");
6589
6615
  if (e.type === "content_block_start")
6590
6616
  validateAnthropicBlock(record(e.content_block, "block"), "content_block");
6591
6617
  if (e.type === "content_block_delta") {
@@ -6598,7 +6624,6 @@ function validateStreamEnvelope(raw, protocol) {
6598
6624
  fields2(record(e.delta, "delta"), ["stop_reason", "stop_sequence"], "delta", "unsupported_response");
6599
6625
  if (e.type === "message_start") {
6600
6626
  const m = record(e.message, "message");
6601
- fields2(m, ["id", "type", "role", "model", "content", "stop_reason", "stop_sequence", "usage"], "message", "unsupported_response");
6602
6627
  validateUsage(m.usage, protocol);
6603
6628
  if (m.content !== undefined && (!Array.isArray(m.content) || m.content.length))
6604
6629
  fail("unsupported_content", "Initial stream message content must be empty", "message.content");
@@ -3024,6 +3024,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3024
3024
  const callIds = new Set;
3025
3025
  const callToolNames = new Map;
3026
3026
  const answered = new Set;
3027
+ const deferredInstructions = [];
3028
+ const normalizeChatHistory = protocol === "chat_completions" && !options.preserveContentOrder;
3027
3029
  const projectedToolImages = [];
3028
3030
  let pendingReasoning = "";
3029
3031
  const addMessage = (message) => {
@@ -3049,6 +3051,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3049
3051
  flushToolImages();
3050
3052
  if (item.type === "additional_tools")
3051
3053
  return;
3054
+ if (deferredInstructions.length && item.type !== "function_call_output" && item.type !== "custom_tool_call_output" && !((item.type === undefined || item.type === "message") && (item.role === "system" || item.role === "developer")))
3055
+ fail("invalid_tool_history", "Only instructions and tool results may follow a deferred instruction until all tool results arrive", messageParam);
3052
3056
  if (item.type === "agent_message") {
3053
3057
  validateAgentMessage(item, messageParam, capabilities.limits);
3054
3058
  if (capabilities.agentMessageDelivery !== "plaintext")
@@ -3127,6 +3131,13 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3127
3131
  }
3128
3132
  }
3129
3133
  addMessage({ role: "tool", tool_call_id: id, content: output, ...item.is_error === true ? { is_error: true } : {} });
3134
+ if (callIds.size === answered.size) {
3135
+ for (const instruction of deferredInstructions) {
3136
+ messageParam = instruction.param;
3137
+ addMessage(instruction.message);
3138
+ }
3139
+ deferredInstructions.length = 0;
3140
+ }
3130
3141
  return;
3131
3142
  }
3132
3143
  if (item.type !== undefined && item.type !== "message")
@@ -3143,6 +3154,11 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3143
3154
  message.reasoning_content = pendingReasoning;
3144
3155
  pendingReasoning = "";
3145
3156
  }
3157
+ if (normalizeChatHistory && (role === "system" || role === "developer") && callIds.size !== answered.size) {
3158
+ deferredInstructions.push({ message, param: messageParam });
3159
+ diagnostics.push({ param: messageParam, action: "mapped", reason: "chat_instruction_deferred_until_tool_results" });
3160
+ return;
3161
+ }
3146
3162
  if (protocol === "chat_completions" && !options.preserveContentOrder && role === "assistant") {
3147
3163
  const previous = chat.at(-1);
3148
3164
  if (previous?.role === "assistant") {
@@ -3156,6 +3172,8 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3156
3172
  });
3157
3173
  if (pendingReasoning)
3158
3174
  fail("unsupported_reasoning", "Orphan reasoning history cannot be represented");
3175
+ if (deferredInstructions.length)
3176
+ fail("invalid_tool_history", "Deferred instructions require all pending tool results", deferredInstructions[0].param);
3159
3177
  flushToolImages(true);
3160
3178
  const result = { model, messages: chat };
3161
3179
  const omittedAllTools = !tools.length && diagnostics.some((item) => item.reason === "optional_hosted_web_search_unavailable");
@@ -3259,6 +3277,28 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
3259
3277
  return { body: result, toolNames, canonicalInput, diagnostics };
3260
3278
  }
3261
3279
  // src/responses-codec/events.ts
3280
+ function chatOutput(raw) {
3281
+ const keys = ["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "function_call", "annotations"];
3282
+ const delta = Object.fromEntries(keys.filter((key) => raw[key] != null).map((key) => [key, raw[key]]));
3283
+ const provider = raw.provider_specific_fields == null ? {} : record(raw.provider_specific_fields, "provider_specific_fields");
3284
+ for (const key of ["refusal", "reasoning_content", "reasoning"]) {
3285
+ if (provider[key] == null)
3286
+ continue;
3287
+ if (delta[key] != null && delta[key] !== provider[key])
3288
+ fail("unsupported_content", "Conflicting provider output fields", `provider_specific_fields.${key}`);
3289
+ delta[key] = provider[key];
3290
+ }
3291
+ for (const key of ["audio", "thinking_blocks", "reasoning_details", "signature", "thought_signature", "thoughtSignature", "encrypted_content", "encrypted_function_args"])
3292
+ if (raw[key] != null || provider[key] != null)
3293
+ fail("unsupported_content", "Provider output authority cannot cross protocols", key);
3294
+ for (const key of ["content", "tool_calls", "function_call", "annotations"])
3295
+ if (provider[key] != null)
3296
+ fail("unsupported_content", "Nested provider output cannot cross protocols", `provider_specific_fields.${key}`);
3297
+ if (delta.reasoning_content != null && delta.reasoning != null && delta.reasoning_content !== delta.reasoning)
3298
+ fail("unsupported_content", "Conflicting reasoning fields", "reasoning");
3299
+ return delta;
3300
+ }
3301
+
3262
3302
  class ResponsesEventEncoder {
3263
3303
  protocol;
3264
3304
  model;
@@ -3468,10 +3508,7 @@ class ResponsesEventEncoder {
3468
3508
  const choice = record(choices[0], "choice");
3469
3509
  if (choice.index !== undefined && choice.index !== 0)
3470
3510
  fail("unsupported_response", "Only choice index 0 is supported");
3471
- const delta = record(choice.delta ?? {}, "choice delta");
3472
- for (const key of Object.keys(delta))
3473
- if (!["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "function_call", "annotations"].includes(key))
3474
- fail("unsupported_content", `Unsupported Chat output field ${key}`);
3511
+ const delta = chatOutput(record(choice.delta ?? {}, "choice delta"));
3475
3512
  if (delta.annotations !== undefined && list(delta.annotations, "annotations").length)
3476
3513
  fail("unsupported_content", "Chat output annotations cannot be faithfully converted");
3477
3514
  if (delta.role !== undefined && delta.role !== "assistant")
@@ -3669,7 +3706,7 @@ function encodeResponsesResult(raw, protocol, model, toolNames = new Map, config
3669
3706
  const choice = record(choices[0], "choice");
3670
3707
  const message = record(choice.message, "message");
3671
3708
  const delta = { ...message };
3672
- if (message.tool_calls !== undefined)
3709
+ if (message.tool_calls != null)
3673
3710
  delta.tool_calls = list(message.tool_calls, "tool_calls").map((tool, index) => ({ ...record(tool, "tool call"), index }));
3674
3711
  encoder.push({ choices: [{ index: choice.index ?? 0, delta, finish_reason: choice.finish_reason }], usage: body.usage });
3675
3712
  } else {
@@ -4399,7 +4436,7 @@ function convertProtocolRequest(raw, context) {
4399
4436
  fail("unsupported_reasoning", "Source thinking mode needs an explicit target wire policy", "thinking");
4400
4437
  }
4401
4438
  if (target === "responses") {
4402
- const validation = decodeResponsesRequest({ ...b, reasoning: reasoning ? { ...reasoning, effort: undefined } : undefined }, "chat_completions", { ...cap, reasoningHistory: true });
4439
+ const validation = decodeResponsesRequest({ ...b, reasoning: reasoning ? { ...reasoning, effort: undefined } : undefined }, "chat_completions", { ...cap, reasoningHistory: true }, { preserveContentOrder: true, toolOutputImages: true });
4403
4440
  if (mappedEffort !== undefined)
4404
4441
  b.reasoning = { ...reasoning, effort: mappedEffort };
4405
4442
  else if (reasoning?.effort !== undefined)
@@ -4658,20 +4695,14 @@ function validateProviderResponse(raw, protocol) {
4658
4695
  if (b.error !== undefined || b.type === "error")
4659
4696
  return b;
4660
4697
  if (protocol === "chat_completions") {
4661
- fields2(b, ["id", "object", "created", "model", "choices", "usage", "system_fingerprint", "service_tier"], "", "unsupported_response");
4662
4698
  for (const rawChoice of list(b.choices, "choices")) {
4663
4699
  const c = record(rawChoice, "choice");
4664
- fields2(c, ["index", "message", "finish_reason", "logprobs", "provider_specific_fields"], "choices", "unsupported_response");
4665
- if (c.provider_specific_fields !== undefined) {
4666
- const providerFields = c.provider_specific_fields;
4667
- if (providerFields === null || typeof providerFields !== "object" || Array.isArray(providerFields) || Object.keys(providerFields).length)
4668
- fail("unsupported_response", "Only empty provider-specific choice fields are portable", "choices.provider_specific_fields");
4669
- }
4700
+ if (c.provider_specific_fields != null)
4701
+ record(c.provider_specific_fields, "choices.provider_specific_fields");
4670
4702
  if (c.logprobs != null)
4671
4703
  fail("unsupported_response", "Log probabilities cannot cross protocols", "choices.logprobs");
4672
4704
  const m = record(c.message, "message");
4673
- fields2(m, ["role", "content", "refusal", "reasoning_content", "reasoning", "tool_calls", "annotations"], "message", "unsupported_content");
4674
- for (const [callIndex, rawCall] of (m.tool_calls === undefined ? [] : list(m.tool_calls, "tool_calls")).entries()) {
4705
+ for (const [callIndex, rawCall] of (m.tool_calls == null ? [] : list(m.tool_calls, "tool_calls")).entries()) {
4675
4706
  const call = record(rawCall, "call");
4676
4707
  fields2(call, ["id", "type", "function", "index"], "tool_calls", "unsupported_tool");
4677
4708
  if (call.index !== undefined && integer(call.index, `tool_calls[${callIndex}].index`) !== callIndex)
@@ -4681,7 +4712,6 @@ function validateProviderResponse(raw, protocol) {
4681
4712
  }
4682
4713
  validateUsage(b.usage, protocol);
4683
4714
  } else if (protocol === "anthropic_messages") {
4684
- fields2(b, ["id", "type", "role", "model", "content", "stop_reason", "stop_sequence", "usage"], "", "unsupported_response");
4685
4715
  if (b.role !== undefined && b.role !== "assistant")
4686
4716
  fail("unsupported_response", "Response role must be assistant", "role");
4687
4717
  for (const rawPart of list(b.content, "content"))
@@ -4887,7 +4917,6 @@ class GeminiCanonicalDecoder {
4887
4917
  }
4888
4918
  push(raw) {
4889
4919
  const b = record(raw, "Gemini response");
4890
- fields2(b, ["candidates", "usageMetadata", "modelVersion", "responseId", "promptFeedback", "error"], "", "unsupported_response");
4891
4920
  if (b.error !== undefined)
4892
4921
  return this.decorate(this.encoder.push({ error: safeProtocolError() }));
4893
4922
  const events = [];
@@ -4904,7 +4933,6 @@ class GeminiCanonicalDecoder {
4904
4933
  fail("unsupported_response", "Multiple Gemini candidates cannot cross protocols", "candidates");
4905
4934
  if (candidates.length) {
4906
4935
  const c = record(candidates[0], "candidate");
4907
- fields2(c, ["index", "content", "finishReason", "safetyRatings", "finishMessage"], "candidates", "unsupported_response");
4908
4936
  if (c.index !== undefined && c.index !== 0)
4909
4937
  fail("unsupported_response", "Only candidate index zero is supported", "candidates.index");
4910
4938
  if (c.content !== undefined && c.content !== null) {
@@ -5510,9 +5538,9 @@ function createProtocolSession(context) {
5510
5538
  fail("invalid_session", "A session can convert one JSON response or one stream");
5511
5539
  mode = "json";
5512
5540
  finished = true;
5541
+ const body = record(raw, "response");
5542
+ serialized(body, limits(context.capabilities?.limits).maxOutputBytes, "response");
5513
5543
  if (same) {
5514
- const body = record(raw, "response");
5515
- serialized(body, limits(context.capabilities?.limits).maxOutputBytes, "response");
5516
5544
  return body;
5517
5545
  }
5518
5546
  const canonical = canonicalResponse(raw, context, names);
@@ -5526,9 +5554,9 @@ function createProtocolSession(context) {
5526
5554
  if (finished || mode === "json")
5527
5555
  fail("invalid_stream", "Event arrived after session completion");
5528
5556
  mode = "stream";
5557
+ const e = record(raw, "event");
5558
+ serialized(e, limits(context.capabilities?.limits).maxOutputBytes, "event");
5529
5559
  if (same) {
5530
- const e = record(raw, "event");
5531
- serialized(e, limits(context.capabilities?.limits).maxOutputBytes, "event");
5532
5560
  if (context.targetProtocol === "anthropic_messages") {
5533
5561
  if (passAnthropicStopped)
5534
5562
  fail("invalid_stream", "Event arrived after message_stop");
@@ -5606,11 +5634,11 @@ function validateStreamEnvelope(raw, protocol) {
5606
5634
  if (e.error !== undefined || e.type === "error")
5607
5635
  return;
5608
5636
  if (protocol === "chat_completions") {
5609
- fields2(e, ["id", "object", "created", "model", "choices", "usage", "system_fingerprint", "service_tier"], "", "unsupported_response");
5610
5637
  if (Array.isArray(e.choices))
5611
5638
  for (const rawChoice of e.choices) {
5612
5639
  const c = record(rawChoice, "choice");
5613
- fields2(c, ["index", "delta", "finish_reason", "logprobs"], "choices", "unsupported_response");
5640
+ if (c.provider_specific_fields != null)
5641
+ record(c.provider_specific_fields, "choices.provider_specific_fields");
5614
5642
  if (c.logprobs != null)
5615
5643
  fail("unsupported_response", "Log probabilities cannot cross protocols", "choices.logprobs");
5616
5644
  const d = record(c.delta ?? {}, "delta");
@@ -5624,8 +5652,6 @@ function validateStreamEnvelope(raw, protocol) {
5624
5652
  }
5625
5653
  validateUsage(e.usage, protocol);
5626
5654
  } else {
5627
- const allowed = e.type === "message_start" ? ["type", "message"] : e.type === "message_delta" ? ["type", "delta", "usage"] : e.type === "content_block_start" ? ["type", "index", "content_block"] : e.type === "content_block_delta" ? ["type", "index", "delta"] : e.type === "content_block_stop" ? ["type", "index"] : ["type"];
5628
- fields2(e, allowed, "", "unsupported_response");
5629
5655
  if (e.type === "content_block_start")
5630
5656
  validateAnthropicBlock(record(e.content_block, "block"), "content_block");
5631
5657
  if (e.type === "content_block_delta") {
@@ -5638,7 +5664,6 @@ function validateStreamEnvelope(raw, protocol) {
5638
5664
  fields2(record(e.delta, "delta"), ["stop_reason", "stop_sequence"], "delta", "unsupported_response");
5639
5665
  if (e.type === "message_start") {
5640
5666
  const m = record(e.message, "message");
5641
- fields2(m, ["id", "type", "role", "model", "content", "stop_reason", "stop_sequence", "usage"], "message", "unsupported_response");
5642
5667
  validateUsage(m.usage, protocol);
5643
5668
  if (m.content !== undefined && (!Array.isArray(m.content) || m.content.length))
5644
5669
  fail("unsupported_content", "Initial stream message content must be empty", "message.content");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jeffusion/bungee-llms",
3
- "version": "6.11.2",
3
+ "version": "6.12.0",
4
4
  "description": "Extensible LLM adapters and protocol conversion package for Bungee",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,7 @@
11
11
  "prepublishOnly": "bun run build"
12
12
  },
13
13
  "dependencies": {
14
- "@jeffusion/bungee-types": "^6.11.2"
14
+ "@jeffusion/bungee-types": "^6.12.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "bun-types": "^1.3.2",