@codeproxy/core 0.1.14 → 0.1.16
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.cjs +122 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +122 -28
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1071,6 +1071,101 @@ __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
|
+
var SYNTHETIC_TOOL_RESPONSE = '{"status":"no_result","note":"No tool result was recorded for this call before the request was sent (the turn was interrupted or the result was not persisted). Treat the call as unfinished \u2014 do not assume it succeeded; re-check the underlying state before retrying."}';
|
|
1129
|
+
function synthesizeMissingToolResponses(messages) {
|
|
1130
|
+
const answered = /* @__PURE__ */ new Set();
|
|
1131
|
+
for (const msg of messages) {
|
|
1132
|
+
if (msg.role === "tool" && msg.tool_call_id !== void 0) {
|
|
1133
|
+
answered.add(String(msg.tool_call_id));
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
const out = [];
|
|
1137
|
+
let cursor = 0;
|
|
1138
|
+
while (cursor < messages.length) {
|
|
1139
|
+
const msg = messages[cursor];
|
|
1140
|
+
out.push(msg);
|
|
1141
|
+
cursor += 1;
|
|
1142
|
+
if (msg.role !== "assistant" || !msg.tool_calls?.length) {
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
while (cursor < messages.length && messages[cursor].role === "tool") {
|
|
1146
|
+
out.push(messages[cursor]);
|
|
1147
|
+
cursor += 1;
|
|
1148
|
+
}
|
|
1149
|
+
for (const call of msg.tool_calls) {
|
|
1150
|
+
const id = call.id ? String(call.id) : "";
|
|
1151
|
+
if (id && !answered.has(id)) {
|
|
1152
|
+
out.push({ role: "tool", tool_call_id: id, content: SYNTHETIC_TOOL_RESPONSE });
|
|
1153
|
+
answered.add(id);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
messages.length = 0;
|
|
1158
|
+
messages.push(...out);
|
|
1159
|
+
}
|
|
1160
|
+
function applyGeminiFixups(messages, model) {
|
|
1161
|
+
if (!isGeminiModel(model)) {
|
|
1162
|
+
return;
|
|
1163
|
+
}
|
|
1164
|
+
mergeSystemMessages(messages);
|
|
1165
|
+
applyGeminiToolUseShim(messages);
|
|
1166
|
+
synthesizeMissingToolResponses(messages);
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1074
1169
|
// src/translate/openai/translateRequest.ts
|
|
1075
1170
|
function translateRequest2(data, options = {}) {
|
|
1076
1171
|
const messages = [];
|
|
@@ -1126,6 +1221,7 @@ function translateRequest2(data, options = {}) {
|
|
|
1126
1221
|
}
|
|
1127
1222
|
}
|
|
1128
1223
|
repairToolMessageOrder(messages);
|
|
1224
|
+
applyGeminiFixups(messages, data.model);
|
|
1129
1225
|
return { request };
|
|
1130
1226
|
}
|
|
1131
1227
|
function buildSystemContent(instructions) {
|
|
@@ -1497,43 +1593,41 @@ function repairToolMessageOrder(messages) {
|
|
|
1497
1593
|
if (messages.length === 0) {
|
|
1498
1594
|
return;
|
|
1499
1595
|
}
|
|
1500
|
-
const
|
|
1501
|
-
let currentBlock = null;
|
|
1596
|
+
const toolById = /* @__PURE__ */ new Map();
|
|
1502
1597
|
for (const msg of messages) {
|
|
1503
|
-
if (msg.role === "
|
|
1504
|
-
|
|
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] });
|
|
1598
|
+
if (msg.role === "tool" && msg.tool_call_id !== void 0 && !toolById.has(String(msg.tool_call_id))) {
|
|
1599
|
+
toolById.set(String(msg.tool_call_id), msg);
|
|
1510
1600
|
}
|
|
1511
1601
|
}
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
)
|
|
1516
|
-
if (toolCallIds.size === 0) {
|
|
1602
|
+
const used = /* @__PURE__ */ new Set();
|
|
1603
|
+
const out = [];
|
|
1604
|
+
for (const msg of messages) {
|
|
1605
|
+
if (msg.role === "tool") {
|
|
1517
1606
|
continue;
|
|
1518
1607
|
}
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1608
|
+
if (msg.role !== "assistant") {
|
|
1609
|
+
out.push(msg);
|
|
1610
|
+
continue;
|
|
1611
|
+
}
|
|
1612
|
+
const calls = msg.tool_calls ?? [];
|
|
1613
|
+
if (calls.length === 0) {
|
|
1614
|
+
if (msg.content != null || msg.reasoning_content != null) {
|
|
1615
|
+
out.push(msg);
|
|
1526
1616
|
}
|
|
1617
|
+
continue;
|
|
1527
1618
|
}
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1619
|
+
out.push(msg);
|
|
1620
|
+
for (const call of calls) {
|
|
1621
|
+
const id = call.id ? String(call.id) : "";
|
|
1622
|
+
const tool = id ? toolById.get(id) : void 0;
|
|
1623
|
+
if (id && tool && !used.has(id)) {
|
|
1624
|
+
out.push(tool);
|
|
1625
|
+
used.add(id);
|
|
1626
|
+
}
|
|
1534
1627
|
}
|
|
1535
|
-
messages.push(...block.trailing);
|
|
1536
1628
|
}
|
|
1629
|
+
messages.length = 0;
|
|
1630
|
+
messages.push(...out);
|
|
1537
1631
|
}
|
|
1538
1632
|
|
|
1539
1633
|
// src/translate/openai/translateResponse.ts
|