@jeffusion/bungee-llms 6.8.1 → 6.8.2
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 +128 -41
- package/dist/plugin-api.js +128 -41
- package/dist/protocol-session/gemini.d.ts +6 -1
- package/dist/responses-codec/request.d.ts +1 -0
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -3780,14 +3780,20 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
3780
3780
|
fail("unsupported_request", "Only one response candidate is supported");
|
|
3781
3781
|
if (body.truncation !== undefined && body.truncation !== "disabled")
|
|
3782
3782
|
fail("unsupported_request", "Automatic input truncation cannot be represented");
|
|
3783
|
-
|
|
3784
|
-
|
|
3783
|
+
const diagnostics = [];
|
|
3784
|
+
if (body.include !== undefined)
|
|
3785
|
+
atParam("include", () => {
|
|
3786
|
+
for (const [index, field] of list(body.include, "include").entries()) {
|
|
3787
|
+
if (field !== "reasoning.encrypted_content" && field !== "output.encrypted_content")
|
|
3788
|
+
fail("unsupported_request", "Requested extra Responses output fields cannot be represented", `include[${index}]`);
|
|
3789
|
+
diagnostics.push({ param: `include[${index}]`, action: "omitted", reason: "target_encrypted_output_unavailable" });
|
|
3790
|
+
}
|
|
3791
|
+
});
|
|
3785
3792
|
if (body.prompt_cache_key !== undefined)
|
|
3786
3793
|
string(body.prompt_cache_key, "prompt_cache_key");
|
|
3787
3794
|
if (body.client_metadata !== undefined)
|
|
3788
3795
|
record(body.client_metadata, "client_metadata");
|
|
3789
3796
|
const model = string(body.model, "model", true);
|
|
3790
|
-
const diagnostics = [];
|
|
3791
3797
|
const input = atParam("input", () => typeof body.input === "string" ? [{ role: "user", content: body.input }] : list(body.input, "input"));
|
|
3792
3798
|
if (input.length > budget.maxItems)
|
|
3793
3799
|
fail("resource_limit", "Too many input items", "input");
|
|
@@ -3920,29 +3926,30 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
3920
3926
|
fail("tool_name_collision", "Historical tool kind conflicts with its current declaration");
|
|
3921
3927
|
return wire;
|
|
3922
3928
|
};
|
|
3923
|
-
const normalizeContent = (rawContent, role) => {
|
|
3929
|
+
const normalizeContent = (rawContent, role, path) => {
|
|
3924
3930
|
if (typeof rawContent === "string")
|
|
3925
3931
|
return rawContent;
|
|
3926
3932
|
const parts = list(rawContent, "message content");
|
|
3927
|
-
for (const rawPart of parts)
|
|
3928
|
-
|
|
3929
|
-
|
|
3930
|
-
|
|
3931
|
-
|
|
3932
|
-
|
|
3933
|
-
|
|
3934
|
-
|
|
3935
|
-
|
|
3936
|
-
if (
|
|
3937
|
-
|
|
3938
|
-
|
|
3939
|
-
|
|
3940
|
-
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3933
|
+
for (const [index, rawPart] of parts.entries())
|
|
3934
|
+
atParam(path ? `${path}[${index}]` : messageParam, () => {
|
|
3935
|
+
const part = record(rawPart, "content part");
|
|
3936
|
+
if (part.type === "input_text" || part.type === "output_text" || part.type === "text")
|
|
3937
|
+
string(part.text, "content text");
|
|
3938
|
+
else if (part.type === "refusal" && role === "assistant") {
|
|
3939
|
+
string(part.refusal, "refusal");
|
|
3940
|
+
if (capabilities.preserveRefusalContent && protocol !== "chat_completions")
|
|
3941
|
+
fail("unsupported_content", "Target protocol cannot preserve refusal history authority");
|
|
3942
|
+
} else if (part.type === "input_image" && (role === "user" || role === "tool" && options.toolOutputImages)) {
|
|
3943
|
+
if (protocol !== "chat_completions")
|
|
3944
|
+
fail("unsupported_content", "Anthropic image conversion requires provider-specific URL/data support");
|
|
3945
|
+
string(part.image_url, "image_url", true);
|
|
3946
|
+
if (part.detail !== undefined && !["auto", "low", "high"].includes(String(part.detail)))
|
|
3947
|
+
fail("unsupported_content", "Unknown image detail", path ? `${path}[${index}].detail` : undefined);
|
|
3948
|
+
} else
|
|
3949
|
+
fail("unsupported_content", "Unsupported message content type");
|
|
3950
|
+
if (part.annotations !== undefined && list(part.annotations, "annotations").length)
|
|
3951
|
+
fail("unsupported_content", "Output annotations cannot be represented in input history");
|
|
3952
|
+
});
|
|
3946
3953
|
const normalized = contentNormalizer.normalizeResponsesMessageContent(parts.map((raw) => {
|
|
3947
3954
|
const part = raw;
|
|
3948
3955
|
return part.type === "refusal" ? { type: "output_text", text: part.refusal } : part;
|
|
@@ -4037,7 +4044,7 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
4037
4044
|
if (!callIds.has(id) || answered.has(id))
|
|
4038
4045
|
fail("invalid_tool_history", "Unmatched or duplicate tool result");
|
|
4039
4046
|
answered.add(id);
|
|
4040
|
-
const output = typeof item.output === "string" ? item.output : normalizeContent(item.output, "tool");
|
|
4047
|
+
const output = typeof item.output === "string" ? item.output : normalizeContent(item.output, "tool", `${messageParam}.output`);
|
|
4041
4048
|
addMessage({ role: "tool", tool_call_id: id, content: output, ...item.is_error === true ? { is_error: true } : {} });
|
|
4042
4049
|
return;
|
|
4043
4050
|
}
|
|
@@ -4638,6 +4645,38 @@ function textParts(raw, path, style, role) {
|
|
|
4638
4645
|
|
|
4639
4646
|
// src/protocol-session/gemini.ts
|
|
4640
4647
|
var signatureSeparator = "__thought__";
|
|
4648
|
+
function geminiInlineImage(image, path, budget) {
|
|
4649
|
+
const param = `${path}.image_url`;
|
|
4650
|
+
const url = bounded(string(image.url, "image URL", true), budget.maxRequestBytes, param);
|
|
4651
|
+
const match = /^data:([^;,]+);base64,([\s\S]*)$/.exec(url);
|
|
4652
|
+
if (!match || !["image/png", "image/jpeg", "image/webp"].includes(match[1]))
|
|
4653
|
+
fail("unsupported_content", "Gemini images require inline PNG, JPEG or WebP data", param);
|
|
4654
|
+
const data = match[2];
|
|
4655
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(data) || data.length % 4 === 1 || data.includes("=") && data.length % 4 !== 0 || Buffer.from(data, "base64").toString("base64").replace(/=+$/, "") !== data.replace(/=+$/, ""))
|
|
4656
|
+
fail("invalid_payload", "Invalid image Base64 encoding", param);
|
|
4657
|
+
return { mimeType: match[1], data };
|
|
4658
|
+
}
|
|
4659
|
+
function geminiToolResponse(content, path, budget, diagnostics) {
|
|
4660
|
+
if (!Array.isArray(content) || !content.some((part) => part.type === "image_url"))
|
|
4661
|
+
return { response: { output: content } };
|
|
4662
|
+
if (content.length > budget.maxItems)
|
|
4663
|
+
fail("resource_limit", "Too many tool output parts", path);
|
|
4664
|
+
const parts = [];
|
|
4665
|
+
const output = content.map((part, index) => atParam(`${path}[${index}]`, () => {
|
|
4666
|
+
if (part.type === "text")
|
|
4667
|
+
return part;
|
|
4668
|
+
if (part.type !== "image_url")
|
|
4669
|
+
fail("unsupported_content", "Unsupported Gemini tool result content");
|
|
4670
|
+
const image = record(part.image_url, "tool image");
|
|
4671
|
+
const inlineData = geminiInlineImage(image, `${path}[${index}]`, budget);
|
|
4672
|
+
const displayName = `tool-image-${index}.${inlineData.mimeType.slice("image/".length)}`;
|
|
4673
|
+
parts.push({ inlineData: { ...inlineData, displayName } });
|
|
4674
|
+
if (image.detail !== undefined)
|
|
4675
|
+
diagnostics.push({ param: `${path}[${index}].detail`, action: "omitted", reason: "gemini_tool_image_detail_unavailable" });
|
|
4676
|
+
return { $ref: displayName };
|
|
4677
|
+
}));
|
|
4678
|
+
return { response: { output }, parts };
|
|
4679
|
+
}
|
|
4641
4680
|
function checkedSignature(value, maxBytes) {
|
|
4642
4681
|
const signature = bounded(string(value, "thoughtSignature", true), maxBytes, "thoughtSignature");
|
|
4643
4682
|
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(signature) || signature.length % 4 === 1 || signature.includes("=") && signature.length % 4 !== 0 || Buffer.from(signature, "base64").toString("base64").replace(/=+$/, "") !== signature.replace(/=+$/, ""))
|
|
@@ -5168,7 +5207,31 @@ function validateCanonical(b, context) {
|
|
|
5168
5207
|
for (const [j, part] of t.content.entries()) {
|
|
5169
5208
|
const c = record(part, "part");
|
|
5170
5209
|
fields2(c, c.type === "input_image" ? ["type", "image_url", "detail"] : c.type === "refusal" ? ["type", "refusal"] : ["type", "text", "annotations"], `${p}.content[${j}]`, "unsupported_content");
|
|
5210
|
+
if (context.sourceProtocol === "responses" && context.targetProtocol === "gemini_generate_content" && t.role === "user" && c.type === "input_image") {
|
|
5211
|
+
atParam(`${p}.content[${j}].image_url`, () => string(c.image_url, "image URL", true));
|
|
5212
|
+
if (c.detail !== undefined && (typeof c.detail !== "string" || !["auto", "low", "high"].includes(c.detail)))
|
|
5213
|
+
fail("unsupported_content", "Unknown image detail", `${p}.content[${j}].detail`);
|
|
5214
|
+
}
|
|
5171
5215
|
}
|
|
5216
|
+
if (context.sourceProtocol === "responses" && context.targetProtocol === "gemini_generate_content" && Array.isArray(t.output))
|
|
5217
|
+
for (const [j, part] of t.output.entries())
|
|
5218
|
+
atParam(`${p}.output[${j}]`, () => {
|
|
5219
|
+
const c = record(part, "tool output part");
|
|
5220
|
+
fields2(c, c.type === "input_image" ? ["type", "image_url", "detail"] : ["type", "text", "annotations"], `${p}.output[${j}]`, "unsupported_content");
|
|
5221
|
+
if (c.type === "input_image") {
|
|
5222
|
+
atParam(`${p}.output[${j}].image_url`, () => string(c.image_url, "image URL", true));
|
|
5223
|
+
if (c.detail !== undefined && (typeof c.detail !== "string" || !["auto", "low", "high"].includes(c.detail)))
|
|
5224
|
+
fail("unsupported_content", "Unknown image detail", `${p}.output[${j}].detail`);
|
|
5225
|
+
} else if (c.type === "input_text" || c.type === "output_text" || c.type === "text")
|
|
5226
|
+
atParam(`${p}.output[${j}].text`, () => string(c.text, "tool output text"));
|
|
5227
|
+
else
|
|
5228
|
+
fail("unsupported_content", "Unsupported tool output content type", `${p}.output[${j}].type`);
|
|
5229
|
+
if (c.annotations !== undefined)
|
|
5230
|
+
atParam(`${p}.output[${j}].annotations`, () => {
|
|
5231
|
+
if (list(c.annotations, "annotations").length)
|
|
5232
|
+
fail("unsupported_content", "Output annotations cannot be represented in tool history");
|
|
5233
|
+
});
|
|
5234
|
+
});
|
|
5172
5235
|
}
|
|
5173
5236
|
const inspectTool = (raw, path, nested = false) => {
|
|
5174
5237
|
const t = record(raw, "tool");
|
|
@@ -5201,6 +5264,7 @@ function validateCanonical(b, context) {
|
|
|
5201
5264
|
}
|
|
5202
5265
|
function convertProtocolRequest(raw, context) {
|
|
5203
5266
|
const b = canonicalRequest(raw, context.sourceProtocol, context.model);
|
|
5267
|
+
const originalInput = b.input;
|
|
5204
5268
|
if (context.streaming !== undefined) {
|
|
5205
5269
|
bool(context.streaming, "context.streaming");
|
|
5206
5270
|
if (b.stream !== undefined && b.stream !== context.streaming)
|
|
@@ -5298,13 +5362,13 @@ function convertProtocolRequest(raw, context) {
|
|
|
5298
5362
|
const decodingTarget = target === "gemini_generate_content" ? "chat_completions" : target;
|
|
5299
5363
|
if (target === "gemini_generate_content" && reasoning)
|
|
5300
5364
|
b.reasoning = { ...reasoning, effort: undefined };
|
|
5301
|
-
const decoded = decodeResponsesRequest(b, decodingTarget, { ...cap, reasoningHistory: target !== "anthropic_messages" && cap.reasoningHistory === true, reasoningEffort: target === "chat_completions" && cap.reasoningEffort === true, anthropicThinkingBudget: undefined }, { preserveContentOrder: target === "gemini_generate_content" });
|
|
5365
|
+
const decoded = decodeResponsesRequest(b, decodingTarget, { ...cap, reasoningHistory: target !== "anthropic_messages" && cap.reasoningHistory === true, reasoningEffort: target === "chat_completions" && cap.reasoningEffort === true, anthropicThinkingBudget: undefined }, { preserveContentOrder: target === "gemini_generate_content", toolOutputImages: context.sourceProtocol === "responses" && target === "gemini_generate_content" });
|
|
5302
5366
|
const diagnostics = decoded.diagnostics;
|
|
5303
5367
|
if (target === "gemini_generate_content" && decoded.body.parallel_tool_calls === true) {
|
|
5304
5368
|
delete decoded.body.parallel_tool_calls;
|
|
5305
5369
|
diagnostics.push({ param: "parallel_tool_calls", action: "omitted", reason: "gemini_parallel_tool_calls_allowed" });
|
|
5306
5370
|
}
|
|
5307
|
-
const body = target === "gemini_generate_content" ? renderGemini(decoded.body, context, geminiHistory?.parts, diagnostics) : decoded.body;
|
|
5371
|
+
const body = target === "gemini_generate_content" ? renderGemini(decoded.body, context, originalInput, geminiHistory?.parts, diagnostics) : decoded.body;
|
|
5308
5372
|
if (stop !== undefined)
|
|
5309
5373
|
body[target === "chat_completions" ? "stop" : target === "anthropic_messages" ? "stop_sequences" : "generationConfig"] = target === "gemini_generate_content" ? { ...record(body.generationConfig ?? {}, "generationConfig"), stopSequences: typeof stop === "string" ? [stop] : stop } : typeof stop === "string" ? [stop] : stop;
|
|
5310
5374
|
if (target === "anthropic_messages") {
|
|
@@ -5341,9 +5405,19 @@ function convertProtocolRequest(raw, context) {
|
|
|
5341
5405
|
diagnostics.push({ param: "reasoning", action: "mapped", reason: "explicit_verified_wire_policy" });
|
|
5342
5406
|
return { body, canonicalHistory: geminiHistory?.canonicalHistory(decoded.canonicalInput) ?? decoded.canonicalInput, diagnostics, toolNames: decoded.toolNames, streaming: b.stream === true };
|
|
5343
5407
|
}
|
|
5344
|
-
function renderGemini(chat, context, signedParts, diagnostics) {
|
|
5408
|
+
function renderGemini(chat, context, input, signedParts, diagnostics) {
|
|
5345
5409
|
const result = { contents: [] }, contents = result.contents, system = [], calls = new Map;
|
|
5346
5410
|
const nativeIds = new Set;
|
|
5411
|
+
const budget = limits(context.capabilities?.limits);
|
|
5412
|
+
const toolOutputParams = new Map;
|
|
5413
|
+
const userMessageParams = [];
|
|
5414
|
+
let userMessageIndex = 0;
|
|
5415
|
+
for (const [index, item] of (Array.isArray(input) ? input : []).entries()) {
|
|
5416
|
+
if (item.type === "function_call_output" || item.type === "custom_tool_call_output")
|
|
5417
|
+
toolOutputParams.set(item.call_id, `input[${index}].output`);
|
|
5418
|
+
if ((item.type === undefined || item.type === "message") && item.role === "user" || item.type === "agent_message")
|
|
5419
|
+
userMessageParams.push(`input[${index}].content`);
|
|
5420
|
+
}
|
|
5347
5421
|
const append = (role, parts) => {
|
|
5348
5422
|
const prev = contents.at(-1);
|
|
5349
5423
|
if (prev?.role === role)
|
|
@@ -5357,10 +5431,33 @@ function renderGemini(chat, context, signedParts, diagnostics) {
|
|
|
5357
5431
|
append("model", signed);
|
|
5358
5432
|
continue;
|
|
5359
5433
|
}
|
|
5360
|
-
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5434
|
+
if (m.role === "tool") {
|
|
5435
|
+
const name = calls.get(string(m.tool_call_id, "tool_call_id"));
|
|
5436
|
+
if (!name)
|
|
5437
|
+
fail("invalid_tool_history", "Unknown tool result", "input");
|
|
5438
|
+
if (m.is_error === true)
|
|
5439
|
+
fail("unsupported_content", "Tool error authority cannot be represented by Gemini", "input");
|
|
5440
|
+
const { id } = context.sourceProtocol === "responses" ? decodeGeminiCallId(m.tool_call_id, budget.maxArgumentBytes) : { id: m.tool_call_id };
|
|
5441
|
+
const response = geminiToolResponse(m.content, toolOutputParams.get(m.tool_call_id) ?? "input", budget, diagnostics);
|
|
5442
|
+
append("user", [{ functionResponse: { id, name, ...response } }]);
|
|
5443
|
+
continue;
|
|
5444
|
+
}
|
|
5445
|
+
const contentParam = m.role === "user" ? userMessageParams[userMessageIndex++] ?? "input" : "input";
|
|
5446
|
+
if (Array.isArray(m.content) && m.content.some((p) => p.type === "image_url") && m.content.length > budget.maxItems)
|
|
5447
|
+
fail("resource_limit", "Too many message content parts", contentParam);
|
|
5448
|
+
const parts = m.content == null ? [] : typeof m.content === "string" ? [{ text: m.content }] : m.content.map((p, index) => {
|
|
5449
|
+
if (p.type === "text")
|
|
5450
|
+
return { text: p.text };
|
|
5451
|
+
const path = `${contentParam}[${index}]`;
|
|
5452
|
+
if (context.sourceProtocol !== "responses" || m.role !== "user" || p.type !== "image_url")
|
|
5453
|
+
fail("unsupported_content", "This content cannot cross to Gemini", contentParam);
|
|
5454
|
+
const image = record(p.image_url, "image"), part = { inlineData: geminiInlineImage(image, path, budget) };
|
|
5455
|
+
if (image.detail === "low" || image.detail === "high") {
|
|
5456
|
+
part.mediaResolution = { level: image.detail === "high" ? "MEDIA_RESOLUTION_HIGH" : "MEDIA_RESOLUTION_LOW" };
|
|
5457
|
+
diagnostics.push({ param: `${path}.detail`, action: "mapped", reason: "gemini_image_media_resolution" });
|
|
5458
|
+
} else if (image.detail === "auto")
|
|
5459
|
+
diagnostics.push({ param: `${path}.detail`, action: "omitted", reason: "gemini_image_detail_auto" });
|
|
5460
|
+
return part;
|
|
5364
5461
|
});
|
|
5365
5462
|
if (m.role === "system" || m.role === "developer") {
|
|
5366
5463
|
if (contents.length) {
|
|
@@ -5371,16 +5468,6 @@ function renderGemini(chat, context, signedParts, diagnostics) {
|
|
|
5371
5468
|
system.push(...parts);
|
|
5372
5469
|
continue;
|
|
5373
5470
|
}
|
|
5374
|
-
if (m.role === "tool") {
|
|
5375
|
-
const name = calls.get(string(m.tool_call_id, "tool_call_id"));
|
|
5376
|
-
if (!name)
|
|
5377
|
-
fail("invalid_tool_history", "Unknown tool result", "input");
|
|
5378
|
-
if (m.is_error === true)
|
|
5379
|
-
fail("unsupported_content", "Tool error authority cannot be represented by Gemini", "input");
|
|
5380
|
-
const { id } = context.sourceProtocol === "responses" ? decodeGeminiCallId(m.tool_call_id, limits(context.capabilities?.limits).maxArgumentBytes) : { id: m.tool_call_id };
|
|
5381
|
-
append("user", [{ functionResponse: { id, name, response: { output: m.content } } }]);
|
|
5382
|
-
continue;
|
|
5383
|
-
}
|
|
5384
5471
|
if (m.reasoning_content !== undefined)
|
|
5385
5472
|
parts.unshift({ text: m.reasoning_content, thought: true });
|
|
5386
5473
|
for (const c of m.tool_calls ?? []) {
|
package/dist/plugin-api.js
CHANGED
|
@@ -2820,14 +2820,20 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
2820
2820
|
fail("unsupported_request", "Only one response candidate is supported");
|
|
2821
2821
|
if (body.truncation !== undefined && body.truncation !== "disabled")
|
|
2822
2822
|
fail("unsupported_request", "Automatic input truncation cannot be represented");
|
|
2823
|
-
|
|
2824
|
-
|
|
2823
|
+
const diagnostics = [];
|
|
2824
|
+
if (body.include !== undefined)
|
|
2825
|
+
atParam("include", () => {
|
|
2826
|
+
for (const [index, field] of list(body.include, "include").entries()) {
|
|
2827
|
+
if (field !== "reasoning.encrypted_content" && field !== "output.encrypted_content")
|
|
2828
|
+
fail("unsupported_request", "Requested extra Responses output fields cannot be represented", `include[${index}]`);
|
|
2829
|
+
diagnostics.push({ param: `include[${index}]`, action: "omitted", reason: "target_encrypted_output_unavailable" });
|
|
2830
|
+
}
|
|
2831
|
+
});
|
|
2825
2832
|
if (body.prompt_cache_key !== undefined)
|
|
2826
2833
|
string(body.prompt_cache_key, "prompt_cache_key");
|
|
2827
2834
|
if (body.client_metadata !== undefined)
|
|
2828
2835
|
record(body.client_metadata, "client_metadata");
|
|
2829
2836
|
const model = string(body.model, "model", true);
|
|
2830
|
-
const diagnostics = [];
|
|
2831
2837
|
const input = atParam("input", () => typeof body.input === "string" ? [{ role: "user", content: body.input }] : list(body.input, "input"));
|
|
2832
2838
|
if (input.length > budget.maxItems)
|
|
2833
2839
|
fail("resource_limit", "Too many input items", "input");
|
|
@@ -2960,29 +2966,30 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
2960
2966
|
fail("tool_name_collision", "Historical tool kind conflicts with its current declaration");
|
|
2961
2967
|
return wire;
|
|
2962
2968
|
};
|
|
2963
|
-
const normalizeContent = (rawContent, role) => {
|
|
2969
|
+
const normalizeContent = (rawContent, role, path) => {
|
|
2964
2970
|
if (typeof rawContent === "string")
|
|
2965
2971
|
return rawContent;
|
|
2966
2972
|
const parts = list(rawContent, "message content");
|
|
2967
|
-
for (const rawPart of parts)
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
if (
|
|
2977
|
-
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2973
|
+
for (const [index, rawPart] of parts.entries())
|
|
2974
|
+
atParam(path ? `${path}[${index}]` : messageParam, () => {
|
|
2975
|
+
const part = record(rawPart, "content part");
|
|
2976
|
+
if (part.type === "input_text" || part.type === "output_text" || part.type === "text")
|
|
2977
|
+
string(part.text, "content text");
|
|
2978
|
+
else if (part.type === "refusal" && role === "assistant") {
|
|
2979
|
+
string(part.refusal, "refusal");
|
|
2980
|
+
if (capabilities.preserveRefusalContent && protocol !== "chat_completions")
|
|
2981
|
+
fail("unsupported_content", "Target protocol cannot preserve refusal history authority");
|
|
2982
|
+
} else if (part.type === "input_image" && (role === "user" || role === "tool" && options.toolOutputImages)) {
|
|
2983
|
+
if (protocol !== "chat_completions")
|
|
2984
|
+
fail("unsupported_content", "Anthropic image conversion requires provider-specific URL/data support");
|
|
2985
|
+
string(part.image_url, "image_url", true);
|
|
2986
|
+
if (part.detail !== undefined && !["auto", "low", "high"].includes(String(part.detail)))
|
|
2987
|
+
fail("unsupported_content", "Unknown image detail", path ? `${path}[${index}].detail` : undefined);
|
|
2988
|
+
} else
|
|
2989
|
+
fail("unsupported_content", "Unsupported message content type");
|
|
2990
|
+
if (part.annotations !== undefined && list(part.annotations, "annotations").length)
|
|
2991
|
+
fail("unsupported_content", "Output annotations cannot be represented in input history");
|
|
2992
|
+
});
|
|
2986
2993
|
const normalized = contentNormalizer.normalizeResponsesMessageContent(parts.map((raw) => {
|
|
2987
2994
|
const part = raw;
|
|
2988
2995
|
return part.type === "refusal" ? { type: "output_text", text: part.refusal } : part;
|
|
@@ -3077,7 +3084,7 @@ function decodeResponsesRequest(raw, protocol, capabilities = {}, options = {})
|
|
|
3077
3084
|
if (!callIds.has(id) || answered.has(id))
|
|
3078
3085
|
fail("invalid_tool_history", "Unmatched or duplicate tool result");
|
|
3079
3086
|
answered.add(id);
|
|
3080
|
-
const output = typeof item.output === "string" ? item.output : normalizeContent(item.output, "tool");
|
|
3087
|
+
const output = typeof item.output === "string" ? item.output : normalizeContent(item.output, "tool", `${messageParam}.output`);
|
|
3081
3088
|
addMessage({ role: "tool", tool_call_id: id, content: output, ...item.is_error === true ? { is_error: true } : {} });
|
|
3082
3089
|
return;
|
|
3083
3090
|
}
|
|
@@ -3678,6 +3685,38 @@ function textParts(raw, path, style, role) {
|
|
|
3678
3685
|
|
|
3679
3686
|
// src/protocol-session/gemini.ts
|
|
3680
3687
|
var signatureSeparator = "__thought__";
|
|
3688
|
+
function geminiInlineImage(image, path, budget) {
|
|
3689
|
+
const param = `${path}.image_url`;
|
|
3690
|
+
const url = bounded(string(image.url, "image URL", true), budget.maxRequestBytes, param);
|
|
3691
|
+
const match = /^data:([^;,]+);base64,([\s\S]*)$/.exec(url);
|
|
3692
|
+
if (!match || !["image/png", "image/jpeg", "image/webp"].includes(match[1]))
|
|
3693
|
+
fail("unsupported_content", "Gemini images require inline PNG, JPEG or WebP data", param);
|
|
3694
|
+
const data = match[2];
|
|
3695
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(data) || data.length % 4 === 1 || data.includes("=") && data.length % 4 !== 0 || Buffer.from(data, "base64").toString("base64").replace(/=+$/, "") !== data.replace(/=+$/, ""))
|
|
3696
|
+
fail("invalid_payload", "Invalid image Base64 encoding", param);
|
|
3697
|
+
return { mimeType: match[1], data };
|
|
3698
|
+
}
|
|
3699
|
+
function geminiToolResponse(content, path, budget, diagnostics) {
|
|
3700
|
+
if (!Array.isArray(content) || !content.some((part) => part.type === "image_url"))
|
|
3701
|
+
return { response: { output: content } };
|
|
3702
|
+
if (content.length > budget.maxItems)
|
|
3703
|
+
fail("resource_limit", "Too many tool output parts", path);
|
|
3704
|
+
const parts = [];
|
|
3705
|
+
const output = content.map((part, index) => atParam(`${path}[${index}]`, () => {
|
|
3706
|
+
if (part.type === "text")
|
|
3707
|
+
return part;
|
|
3708
|
+
if (part.type !== "image_url")
|
|
3709
|
+
fail("unsupported_content", "Unsupported Gemini tool result content");
|
|
3710
|
+
const image = record(part.image_url, "tool image");
|
|
3711
|
+
const inlineData = geminiInlineImage(image, `${path}[${index}]`, budget);
|
|
3712
|
+
const displayName = `tool-image-${index}.${inlineData.mimeType.slice("image/".length)}`;
|
|
3713
|
+
parts.push({ inlineData: { ...inlineData, displayName } });
|
|
3714
|
+
if (image.detail !== undefined)
|
|
3715
|
+
diagnostics.push({ param: `${path}[${index}].detail`, action: "omitted", reason: "gemini_tool_image_detail_unavailable" });
|
|
3716
|
+
return { $ref: displayName };
|
|
3717
|
+
}));
|
|
3718
|
+
return { response: { output }, parts };
|
|
3719
|
+
}
|
|
3681
3720
|
function checkedSignature(value, maxBytes) {
|
|
3682
3721
|
const signature = bounded(string(value, "thoughtSignature", true), maxBytes, "thoughtSignature");
|
|
3683
3722
|
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(signature) || signature.length % 4 === 1 || signature.includes("=") && signature.length % 4 !== 0 || Buffer.from(signature, "base64").toString("base64").replace(/=+$/, "") !== signature.replace(/=+$/, ""))
|
|
@@ -4208,7 +4247,31 @@ function validateCanonical(b, context) {
|
|
|
4208
4247
|
for (const [j, part] of t.content.entries()) {
|
|
4209
4248
|
const c = record(part, "part");
|
|
4210
4249
|
fields2(c, c.type === "input_image" ? ["type", "image_url", "detail"] : c.type === "refusal" ? ["type", "refusal"] : ["type", "text", "annotations"], `${p}.content[${j}]`, "unsupported_content");
|
|
4250
|
+
if (context.sourceProtocol === "responses" && context.targetProtocol === "gemini_generate_content" && t.role === "user" && c.type === "input_image") {
|
|
4251
|
+
atParam(`${p}.content[${j}].image_url`, () => string(c.image_url, "image URL", true));
|
|
4252
|
+
if (c.detail !== undefined && (typeof c.detail !== "string" || !["auto", "low", "high"].includes(c.detail)))
|
|
4253
|
+
fail("unsupported_content", "Unknown image detail", `${p}.content[${j}].detail`);
|
|
4254
|
+
}
|
|
4211
4255
|
}
|
|
4256
|
+
if (context.sourceProtocol === "responses" && context.targetProtocol === "gemini_generate_content" && Array.isArray(t.output))
|
|
4257
|
+
for (const [j, part] of t.output.entries())
|
|
4258
|
+
atParam(`${p}.output[${j}]`, () => {
|
|
4259
|
+
const c = record(part, "tool output part");
|
|
4260
|
+
fields2(c, c.type === "input_image" ? ["type", "image_url", "detail"] : ["type", "text", "annotations"], `${p}.output[${j}]`, "unsupported_content");
|
|
4261
|
+
if (c.type === "input_image") {
|
|
4262
|
+
atParam(`${p}.output[${j}].image_url`, () => string(c.image_url, "image URL", true));
|
|
4263
|
+
if (c.detail !== undefined && (typeof c.detail !== "string" || !["auto", "low", "high"].includes(c.detail)))
|
|
4264
|
+
fail("unsupported_content", "Unknown image detail", `${p}.output[${j}].detail`);
|
|
4265
|
+
} else if (c.type === "input_text" || c.type === "output_text" || c.type === "text")
|
|
4266
|
+
atParam(`${p}.output[${j}].text`, () => string(c.text, "tool output text"));
|
|
4267
|
+
else
|
|
4268
|
+
fail("unsupported_content", "Unsupported tool output content type", `${p}.output[${j}].type`);
|
|
4269
|
+
if (c.annotations !== undefined)
|
|
4270
|
+
atParam(`${p}.output[${j}].annotations`, () => {
|
|
4271
|
+
if (list(c.annotations, "annotations").length)
|
|
4272
|
+
fail("unsupported_content", "Output annotations cannot be represented in tool history");
|
|
4273
|
+
});
|
|
4274
|
+
});
|
|
4212
4275
|
}
|
|
4213
4276
|
const inspectTool = (raw, path, nested = false) => {
|
|
4214
4277
|
const t = record(raw, "tool");
|
|
@@ -4241,6 +4304,7 @@ function validateCanonical(b, context) {
|
|
|
4241
4304
|
}
|
|
4242
4305
|
function convertProtocolRequest(raw, context) {
|
|
4243
4306
|
const b = canonicalRequest(raw, context.sourceProtocol, context.model);
|
|
4307
|
+
const originalInput = b.input;
|
|
4244
4308
|
if (context.streaming !== undefined) {
|
|
4245
4309
|
bool(context.streaming, "context.streaming");
|
|
4246
4310
|
if (b.stream !== undefined && b.stream !== context.streaming)
|
|
@@ -4338,13 +4402,13 @@ function convertProtocolRequest(raw, context) {
|
|
|
4338
4402
|
const decodingTarget = target === "gemini_generate_content" ? "chat_completions" : target;
|
|
4339
4403
|
if (target === "gemini_generate_content" && reasoning)
|
|
4340
4404
|
b.reasoning = { ...reasoning, effort: undefined };
|
|
4341
|
-
const decoded = decodeResponsesRequest(b, decodingTarget, { ...cap, reasoningHistory: target !== "anthropic_messages" && cap.reasoningHistory === true, reasoningEffort: target === "chat_completions" && cap.reasoningEffort === true, anthropicThinkingBudget: undefined }, { preserveContentOrder: target === "gemini_generate_content" });
|
|
4405
|
+
const decoded = decodeResponsesRequest(b, decodingTarget, { ...cap, reasoningHistory: target !== "anthropic_messages" && cap.reasoningHistory === true, reasoningEffort: target === "chat_completions" && cap.reasoningEffort === true, anthropicThinkingBudget: undefined }, { preserveContentOrder: target === "gemini_generate_content", toolOutputImages: context.sourceProtocol === "responses" && target === "gemini_generate_content" });
|
|
4342
4406
|
const diagnostics = decoded.diagnostics;
|
|
4343
4407
|
if (target === "gemini_generate_content" && decoded.body.parallel_tool_calls === true) {
|
|
4344
4408
|
delete decoded.body.parallel_tool_calls;
|
|
4345
4409
|
diagnostics.push({ param: "parallel_tool_calls", action: "omitted", reason: "gemini_parallel_tool_calls_allowed" });
|
|
4346
4410
|
}
|
|
4347
|
-
const body = target === "gemini_generate_content" ? renderGemini(decoded.body, context, geminiHistory?.parts, diagnostics) : decoded.body;
|
|
4411
|
+
const body = target === "gemini_generate_content" ? renderGemini(decoded.body, context, originalInput, geminiHistory?.parts, diagnostics) : decoded.body;
|
|
4348
4412
|
if (stop !== undefined)
|
|
4349
4413
|
body[target === "chat_completions" ? "stop" : target === "anthropic_messages" ? "stop_sequences" : "generationConfig"] = target === "gemini_generate_content" ? { ...record(body.generationConfig ?? {}, "generationConfig"), stopSequences: typeof stop === "string" ? [stop] : stop } : typeof stop === "string" ? [stop] : stop;
|
|
4350
4414
|
if (target === "anthropic_messages") {
|
|
@@ -4381,9 +4445,19 @@ function convertProtocolRequest(raw, context) {
|
|
|
4381
4445
|
diagnostics.push({ param: "reasoning", action: "mapped", reason: "explicit_verified_wire_policy" });
|
|
4382
4446
|
return { body, canonicalHistory: geminiHistory?.canonicalHistory(decoded.canonicalInput) ?? decoded.canonicalInput, diagnostics, toolNames: decoded.toolNames, streaming: b.stream === true };
|
|
4383
4447
|
}
|
|
4384
|
-
function renderGemini(chat, context, signedParts, diagnostics) {
|
|
4448
|
+
function renderGemini(chat, context, input, signedParts, diagnostics) {
|
|
4385
4449
|
const result = { contents: [] }, contents = result.contents, system = [], calls = new Map;
|
|
4386
4450
|
const nativeIds = new Set;
|
|
4451
|
+
const budget = limits(context.capabilities?.limits);
|
|
4452
|
+
const toolOutputParams = new Map;
|
|
4453
|
+
const userMessageParams = [];
|
|
4454
|
+
let userMessageIndex = 0;
|
|
4455
|
+
for (const [index, item] of (Array.isArray(input) ? input : []).entries()) {
|
|
4456
|
+
if (item.type === "function_call_output" || item.type === "custom_tool_call_output")
|
|
4457
|
+
toolOutputParams.set(item.call_id, `input[${index}].output`);
|
|
4458
|
+
if ((item.type === undefined || item.type === "message") && item.role === "user" || item.type === "agent_message")
|
|
4459
|
+
userMessageParams.push(`input[${index}].content`);
|
|
4460
|
+
}
|
|
4387
4461
|
const append = (role, parts) => {
|
|
4388
4462
|
const prev = contents.at(-1);
|
|
4389
4463
|
if (prev?.role === role)
|
|
@@ -4397,10 +4471,33 @@ function renderGemini(chat, context, signedParts, diagnostics) {
|
|
|
4397
4471
|
append("model", signed);
|
|
4398
4472
|
continue;
|
|
4399
4473
|
}
|
|
4400
|
-
|
|
4401
|
-
|
|
4402
|
-
|
|
4403
|
-
|
|
4474
|
+
if (m.role === "tool") {
|
|
4475
|
+
const name = calls.get(string(m.tool_call_id, "tool_call_id"));
|
|
4476
|
+
if (!name)
|
|
4477
|
+
fail("invalid_tool_history", "Unknown tool result", "input");
|
|
4478
|
+
if (m.is_error === true)
|
|
4479
|
+
fail("unsupported_content", "Tool error authority cannot be represented by Gemini", "input");
|
|
4480
|
+
const { id } = context.sourceProtocol === "responses" ? decodeGeminiCallId(m.tool_call_id, budget.maxArgumentBytes) : { id: m.tool_call_id };
|
|
4481
|
+
const response = geminiToolResponse(m.content, toolOutputParams.get(m.tool_call_id) ?? "input", budget, diagnostics);
|
|
4482
|
+
append("user", [{ functionResponse: { id, name, ...response } }]);
|
|
4483
|
+
continue;
|
|
4484
|
+
}
|
|
4485
|
+
const contentParam = m.role === "user" ? userMessageParams[userMessageIndex++] ?? "input" : "input";
|
|
4486
|
+
if (Array.isArray(m.content) && m.content.some((p) => p.type === "image_url") && m.content.length > budget.maxItems)
|
|
4487
|
+
fail("resource_limit", "Too many message content parts", contentParam);
|
|
4488
|
+
const parts = m.content == null ? [] : typeof m.content === "string" ? [{ text: m.content }] : m.content.map((p, index) => {
|
|
4489
|
+
if (p.type === "text")
|
|
4490
|
+
return { text: p.text };
|
|
4491
|
+
const path = `${contentParam}[${index}]`;
|
|
4492
|
+
if (context.sourceProtocol !== "responses" || m.role !== "user" || p.type !== "image_url")
|
|
4493
|
+
fail("unsupported_content", "This content cannot cross to Gemini", contentParam);
|
|
4494
|
+
const image = record(p.image_url, "image"), part = { inlineData: geminiInlineImage(image, path, budget) };
|
|
4495
|
+
if (image.detail === "low" || image.detail === "high") {
|
|
4496
|
+
part.mediaResolution = { level: image.detail === "high" ? "MEDIA_RESOLUTION_HIGH" : "MEDIA_RESOLUTION_LOW" };
|
|
4497
|
+
diagnostics.push({ param: `${path}.detail`, action: "mapped", reason: "gemini_image_media_resolution" });
|
|
4498
|
+
} else if (image.detail === "auto")
|
|
4499
|
+
diagnostics.push({ param: `${path}.detail`, action: "omitted", reason: "gemini_image_detail_auto" });
|
|
4500
|
+
return part;
|
|
4404
4501
|
});
|
|
4405
4502
|
if (m.role === "system" || m.role === "developer") {
|
|
4406
4503
|
if (contents.length) {
|
|
@@ -4411,16 +4508,6 @@ function renderGemini(chat, context, signedParts, diagnostics) {
|
|
|
4411
4508
|
system.push(...parts);
|
|
4412
4509
|
continue;
|
|
4413
4510
|
}
|
|
4414
|
-
if (m.role === "tool") {
|
|
4415
|
-
const name = calls.get(string(m.tool_call_id, "tool_call_id"));
|
|
4416
|
-
if (!name)
|
|
4417
|
-
fail("invalid_tool_history", "Unknown tool result", "input");
|
|
4418
|
-
if (m.is_error === true)
|
|
4419
|
-
fail("unsupported_content", "Tool error authority cannot be represented by Gemini", "input");
|
|
4420
|
-
const { id } = context.sourceProtocol === "responses" ? decodeGeminiCallId(m.tool_call_id, limits(context.capabilities?.limits).maxArgumentBytes) : { id: m.tool_call_id };
|
|
4421
|
-
append("user", [{ functionResponse: { id, name, response: { output: m.content } } }]);
|
|
4422
|
-
continue;
|
|
4423
|
-
}
|
|
4424
4511
|
if (m.reasoning_content !== undefined)
|
|
4425
4512
|
parts.unshift({ text: m.reasoning_content, thought: true });
|
|
4426
4513
|
for (const c of m.tool_calls ?? []) {
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import { type JsonRecord, type ResponsesCodecLimits } from '../responses-codec/common';
|
|
1
|
+
import { type JsonRecord, type ResponsesCodecLimits, type ResponsesConversionDiagnostic } from '../responses-codec/common';
|
|
2
|
+
export declare function geminiInlineImage(image: JsonRecord, path: string, budget: ResponsesCodecLimits): {
|
|
3
|
+
mimeType: string;
|
|
4
|
+
data: string;
|
|
5
|
+
};
|
|
6
|
+
export declare function geminiToolResponse(content: unknown, path: string, budget: ResponsesCodecLimits, diagnostics: ResponsesConversionDiagnostic[]): JsonRecord;
|
|
2
7
|
export declare function checkedSignature(value: unknown, maxBytes: number): string;
|
|
3
8
|
export declare const geminiEnvelopeBytes: (payloadBytes: number) => number;
|
|
4
9
|
type SignedPart = {
|
|
@@ -8,4 +8,5 @@ export interface DecodedResponsesRequest {
|
|
|
8
8
|
/** Converts only self-contained history. Reference resolution belongs to the caller. */
|
|
9
9
|
export declare function decodeResponsesRequest(raw: unknown, protocol: ResponsesProtocol, capabilities?: ResponsesCodecCapabilities, options?: {
|
|
10
10
|
preserveContentOrder?: boolean;
|
|
11
|
+
toolOutputImages?: boolean;
|
|
11
12
|
}): DecodedResponsesRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeffusion/bungee-llms",
|
|
3
|
-
"version": "6.8.
|
|
3
|
+
"version": "6.8.2",
|
|
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.8.
|
|
14
|
+
"@jeffusion/bungee-types": "^6.8.2"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"bun-types": "^1.3.2",
|