@codeproxy/core 0.1.14 → 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.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) {
@@ -1497,43 +1560,41 @@ function repairToolMessageOrder(messages) {
1497
1560
  if (messages.length === 0) {
1498
1561
  return;
1499
1562
  }
1500
- const blocks = [];
1501
- let currentBlock = null;
1563
+ const toolById = /* @__PURE__ */ new Map();
1502
1564
  for (const msg of messages) {
1503
- if (msg.role === "assistant") {
1504
- currentBlock = { assistant: msg, trailing: [] };
1505
- blocks.push(currentBlock);
1506
- } else if (currentBlock) {
1507
- currentBlock.trailing.push(msg);
1508
- } else {
1509
- 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);
1510
1567
  }
1511
1568
  }
1512
- for (const block of blocks) {
1513
- const toolCallIds = new Set(
1514
- (block.assistant.tool_calls ?? []).map((tc) => tc.id).filter(Boolean)
1515
- );
1516
- if (toolCallIds.size === 0) {
1569
+ const used = /* @__PURE__ */ new Set();
1570
+ const out = [];
1571
+ for (const msg of messages) {
1572
+ if (msg.role === "tool") {
1517
1573
  continue;
1518
1574
  }
1519
- const tools = [];
1520
- const others = [];
1521
- for (const m of block.trailing) {
1522
- if (m.role === "tool" && m.tool_call_id !== void 0 && toolCallIds.has(String(m.tool_call_id))) {
1523
- tools.push(m);
1524
- } else {
1525
- 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);
1583
+ }
1584
+ continue;
1585
+ }
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);
1526
1593
  }
1527
1594
  }
1528
- block.trailing = [...tools, ...others];
1529
1595
  }
1530
1596
  messages.length = 0;
1531
- for (const block of blocks) {
1532
- if (block.assistant.tool_calls || block.assistant.content != null) {
1533
- messages.push(block.assistant);
1534
- }
1535
- messages.push(...block.trailing);
1536
- }
1597
+ messages.push(...out);
1537
1598
  }
1538
1599
 
1539
1600
  // src/translate/openai/translateResponse.ts