@codeproxy/core 0.1.13 → 0.1.15

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.d.cts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  type ResponsesInputItem = string | ResponsesMessageItem | ResponsesReasoningItem | ResponsesFunctionCallItem | ResponsesFunctionCallOutputItem | ResponsesLocalShellCallItem | ResponsesCommandExecutionItem | ResponsesCommandExecutionOutputItem | ResponsesCustomToolCallItem | ResponsesCustomToolCallOutputItem | ResponsesFileChangeItem | ResponsesFileChangeOutputItem | ResponsesWebSearchCallItem | Record<string, unknown>;
7
7
  interface ResponsesContentPart {
8
- type: 'input_text' | 'text' | 'output_text' | 'reasoning_text' | 'input_image' | 'image' | 'image_url' | 'input_file' | 'file' | 'tool_result' | string;
8
+ type: 'input_text' | 'text' | 'output_text' | 'reasoning_text' | 'input_image' | 'image' | 'image_url' | 'input_file' | 'file' | 'input_audio' | 'tool_result' | string;
9
9
  text?: string;
10
10
  image_url?: string | {
11
11
  url: string;
@@ -19,6 +19,11 @@ interface ResponsesContentPart {
19
19
  file_url?: string | {
20
20
  url: string;
21
21
  };
22
+ input_audio?: {
23
+ data?: string;
24
+ format?: string;
25
+ };
26
+ format?: string;
22
27
  tool_use_id?: string;
23
28
  call_id?: string;
24
29
  content?: unknown;
package/dist/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
  type ResponsesInputItem = string | ResponsesMessageItem | ResponsesReasoningItem | ResponsesFunctionCallItem | ResponsesFunctionCallOutputItem | ResponsesLocalShellCallItem | ResponsesCommandExecutionItem | ResponsesCommandExecutionOutputItem | ResponsesCustomToolCallItem | ResponsesCustomToolCallOutputItem | ResponsesFileChangeItem | ResponsesFileChangeOutputItem | ResponsesWebSearchCallItem | Record<string, unknown>;
7
7
  interface ResponsesContentPart {
8
- type: 'input_text' | 'text' | 'output_text' | 'reasoning_text' | 'input_image' | 'image' | 'image_url' | 'input_file' | 'file' | 'tool_result' | string;
8
+ type: 'input_text' | 'text' | 'output_text' | 'reasoning_text' | 'input_image' | 'image' | 'image_url' | 'input_file' | 'file' | 'input_audio' | 'tool_result' | string;
9
9
  text?: string;
10
10
  image_url?: string | {
11
11
  url: string;
@@ -19,6 +19,11 @@ interface ResponsesContentPart {
19
19
  file_url?: string | {
20
20
  url: string;
21
21
  };
22
+ input_audio?: {
23
+ data?: string;
24
+ format?: string;
25
+ };
26
+ format?: string;
22
27
  tool_use_id?: string;
23
28
  call_id?: string;
24
29
  content?: unknown;
package/dist/index.js CHANGED
@@ -1071,6 +1071,68 @@ __export(openai_exports, {
1071
1071
  translateStream: () => translateStream2
1072
1072
  });
1073
1073
 
1074
+ // src/translate/openai/gemini-fixups.ts
1075
+ function isGeminiModel(model) {
1076
+ if (!model) {
1077
+ return false;
1078
+ }
1079
+ const lower = model.toLowerCase();
1080
+ return lower.includes("gemini") || lower.startsWith("google/");
1081
+ }
1082
+ function extractTextContent(content) {
1083
+ if (typeof content === "string") {
1084
+ return content;
1085
+ }
1086
+ if (!Array.isArray(content)) {
1087
+ return "";
1088
+ }
1089
+ let out = "";
1090
+ for (const part of content) {
1091
+ if (typeof part === "string") {
1092
+ out += part;
1093
+ } else if (part && typeof part === "object") {
1094
+ out += String(part.text ?? "");
1095
+ }
1096
+ }
1097
+ return out;
1098
+ }
1099
+ function mergeSystemMessages(messages) {
1100
+ const systemTexts = [];
1101
+ const rest = [];
1102
+ for (const msg of messages) {
1103
+ if (msg.role === "system") {
1104
+ systemTexts.push(extractTextContent(msg.content));
1105
+ } else {
1106
+ rest.push(msg);
1107
+ }
1108
+ }
1109
+ if (systemTexts.length <= 1) {
1110
+ return;
1111
+ }
1112
+ messages.length = 0;
1113
+ messages.push({ role: "system", content: systemTexts.join("\n\n") }, ...rest);
1114
+ }
1115
+ var MULTI_TOOL_USE_MANDATE = "Use `multi_tool_use.parallel` to parallelize tool calls and only this.";
1116
+ var MULTI_TOOL_USE_REWRITE = "Parallelize by returning multiple tool calls in a single response. A tool named `multi_tool_use.parallel` does NOT exist in this environment \u2014 never call it.";
1117
+ var UNSUPPORTED_PARALLEL_RE = /^unsupported call: (?:multi_tool_use\W*)?parallel/iu;
1118
+ var UNSUPPORTED_PARALLEL_HINT = " (`multi_tool_use.parallel` is not a real tool here. Re-issue each inner call as its own separate tool call \u2014 several tool calls in one response are fine.)";
1119
+ function applyGeminiToolUseShim(messages) {
1120
+ for (const msg of messages) {
1121
+ if (msg.role === "system" && typeof msg.content === "string" && msg.content.includes(MULTI_TOOL_USE_MANDATE)) {
1122
+ msg.content = msg.content.split(MULTI_TOOL_USE_MANDATE).join(MULTI_TOOL_USE_REWRITE);
1123
+ } else if (msg.role === "tool" && typeof msg.content === "string" && UNSUPPORTED_PARALLEL_RE.test(msg.content)) {
1124
+ msg.content = msg.content + UNSUPPORTED_PARALLEL_HINT;
1125
+ }
1126
+ }
1127
+ }
1128
+ function applyGeminiFixups(messages, model) {
1129
+ if (!isGeminiModel(model)) {
1130
+ return;
1131
+ }
1132
+ mergeSystemMessages(messages);
1133
+ applyGeminiToolUseShim(messages);
1134
+ }
1135
+
1074
1136
  // src/translate/openai/translateRequest.ts
1075
1137
  function translateRequest2(data, options = {}) {
1076
1138
  const messages = [];
@@ -1126,6 +1188,7 @@ function translateRequest2(data, options = {}) {
1126
1188
  }
1127
1189
  }
1128
1190
  repairToolMessageOrder(messages);
1191
+ applyGeminiFixups(messages, data.model);
1129
1192
  return { request };
1130
1193
  }
1131
1194
  function buildSystemContent(instructions) {
@@ -1218,15 +1281,21 @@ function processInputItem(item, messages, options) {
1218
1281
  contentBlocks.push({ type: "image_url", image_url: { url } });
1219
1282
  }
1220
1283
  } else if (part.type === "input_file" || part.type === "file") {
1221
- const partFile = part;
1222
- const fileData = String(partFile.file_data ?? partFile.data ?? "");
1284
+ const fileData = String(contentPart.file_data ?? contentPart.data ?? "");
1223
1285
  const mimeType = String(
1224
- partFile.mime_type ?? partFile.media_type ?? "application/pdf"
1286
+ contentPart.mime_type ?? contentPart.media_type ?? "application/pdf"
1225
1287
  );
1226
1288
  if (fileData) {
1227
1289
  const url = fileData.startsWith("data:") ? fileData : `data:${mimeType};base64,${fileData}`;
1228
1290
  contentBlocks.push({ type: "image_url", image_url: { url } });
1229
1291
  }
1292
+ } else if (contentPart.type === "input_audio") {
1293
+ const ia = contentPart.input_audio;
1294
+ const data = String(ia?.data ?? contentPart.data ?? "");
1295
+ const format = String(ia?.format ?? contentPart.format ?? "mp3");
1296
+ if (data) {
1297
+ contentBlocks.push({ type: "input_audio", input_audio: { data, format } });
1298
+ }
1230
1299
  }
1231
1300
  }
1232
1301
  }
@@ -1491,43 +1560,41 @@ function repairToolMessageOrder(messages) {
1491
1560
  if (messages.length === 0) {
1492
1561
  return;
1493
1562
  }
1494
- const blocks = [];
1495
- let currentBlock = null;
1563
+ const toolById = /* @__PURE__ */ new Map();
1496
1564
  for (const msg of messages) {
1497
- if (msg.role === "assistant") {
1498
- currentBlock = { assistant: msg, trailing: [] };
1499
- blocks.push(currentBlock);
1500
- } else if (currentBlock) {
1501
- currentBlock.trailing.push(msg);
1502
- } else {
1503
- blocks.push({ assistant: { role: "assistant", content: null }, trailing: [msg] });
1565
+ if (msg.role === "tool" && msg.tool_call_id !== void 0 && !toolById.has(String(msg.tool_call_id))) {
1566
+ toolById.set(String(msg.tool_call_id), msg);
1504
1567
  }
1505
1568
  }
1506
- for (const block of blocks) {
1507
- const toolCallIds = new Set(
1508
- (block.assistant.tool_calls ?? []).map((tc) => tc.id).filter(Boolean)
1509
- );
1510
- if (toolCallIds.size === 0) {
1569
+ const used = /* @__PURE__ */ new Set();
1570
+ const out = [];
1571
+ for (const msg of messages) {
1572
+ if (msg.role === "tool") {
1511
1573
  continue;
1512
1574
  }
1513
- const tools = [];
1514
- const others = [];
1515
- for (const m of block.trailing) {
1516
- if (m.role === "tool" && m.tool_call_id !== void 0 && toolCallIds.has(String(m.tool_call_id))) {
1517
- tools.push(m);
1518
- } else {
1519
- others.push(m);
1575
+ if (msg.role !== "assistant") {
1576
+ out.push(msg);
1577
+ continue;
1578
+ }
1579
+ const calls = msg.tool_calls ?? [];
1580
+ if (calls.length === 0) {
1581
+ if (msg.content != null || msg.reasoning_content != null) {
1582
+ out.push(msg);
1520
1583
  }
1584
+ continue;
1521
1585
  }
1522
- block.trailing = [...tools, ...others];
1523
- }
1524
- messages.length = 0;
1525
- for (const block of blocks) {
1526
- if (block.assistant.tool_calls || block.assistant.content != null) {
1527
- messages.push(block.assistant);
1586
+ out.push(msg);
1587
+ for (const call of calls) {
1588
+ const id = call.id ? String(call.id) : "";
1589
+ const tool = id ? toolById.get(id) : void 0;
1590
+ if (id && tool && !used.has(id)) {
1591
+ out.push(tool);
1592
+ used.add(id);
1593
+ }
1528
1594
  }
1529
- messages.push(...block.trailing);
1530
1595
  }
1596
+ messages.length = 0;
1597
+ messages.push(...out);
1531
1598
  }
1532
1599
 
1533
1600
  // src/translate/openai/translateResponse.ts