@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.cjs
CHANGED
|
@@ -1073,6 +1073,101 @@ __export(openai_exports, {
|
|
|
1073
1073
|
translateStream: () => translateStream2
|
|
1074
1074
|
});
|
|
1075
1075
|
|
|
1076
|
+
// src/translate/openai/gemini-fixups.ts
|
|
1077
|
+
function isGeminiModel(model) {
|
|
1078
|
+
if (!model) {
|
|
1079
|
+
return false;
|
|
1080
|
+
}
|
|
1081
|
+
const lower = model.toLowerCase();
|
|
1082
|
+
return lower.includes("gemini") || lower.startsWith("google/");
|
|
1083
|
+
}
|
|
1084
|
+
function extractTextContent(content) {
|
|
1085
|
+
if (typeof content === "string") {
|
|
1086
|
+
return content;
|
|
1087
|
+
}
|
|
1088
|
+
if (!Array.isArray(content)) {
|
|
1089
|
+
return "";
|
|
1090
|
+
}
|
|
1091
|
+
let out = "";
|
|
1092
|
+
for (const part of content) {
|
|
1093
|
+
if (typeof part === "string") {
|
|
1094
|
+
out += part;
|
|
1095
|
+
} else if (part && typeof part === "object") {
|
|
1096
|
+
out += String(part.text ?? "");
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return out;
|
|
1100
|
+
}
|
|
1101
|
+
function mergeSystemMessages(messages) {
|
|
1102
|
+
const systemTexts = [];
|
|
1103
|
+
const rest = [];
|
|
1104
|
+
for (const msg of messages) {
|
|
1105
|
+
if (msg.role === "system") {
|
|
1106
|
+
systemTexts.push(extractTextContent(msg.content));
|
|
1107
|
+
} else {
|
|
1108
|
+
rest.push(msg);
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
if (systemTexts.length <= 1) {
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
messages.length = 0;
|
|
1115
|
+
messages.push({ role: "system", content: systemTexts.join("\n\n") }, ...rest);
|
|
1116
|
+
}
|
|
1117
|
+
var MULTI_TOOL_USE_MANDATE = "Use `multi_tool_use.parallel` to parallelize tool calls and only this.";
|
|
1118
|
+
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.";
|
|
1119
|
+
var UNSUPPORTED_PARALLEL_RE = /^unsupported call: (?:multi_tool_use\W*)?parallel/iu;
|
|
1120
|
+
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.)";
|
|
1121
|
+
function applyGeminiToolUseShim(messages) {
|
|
1122
|
+
for (const msg of messages) {
|
|
1123
|
+
if (msg.role === "system" && typeof msg.content === "string" && msg.content.includes(MULTI_TOOL_USE_MANDATE)) {
|
|
1124
|
+
msg.content = msg.content.split(MULTI_TOOL_USE_MANDATE).join(MULTI_TOOL_USE_REWRITE);
|
|
1125
|
+
} else if (msg.role === "tool" && typeof msg.content === "string" && UNSUPPORTED_PARALLEL_RE.test(msg.content)) {
|
|
1126
|
+
msg.content = msg.content + UNSUPPORTED_PARALLEL_HINT;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
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."}';
|
|
1131
|
+
function synthesizeMissingToolResponses(messages) {
|
|
1132
|
+
const answered = /* @__PURE__ */ new Set();
|
|
1133
|
+
for (const msg of messages) {
|
|
1134
|
+
if (msg.role === "tool" && msg.tool_call_id !== void 0) {
|
|
1135
|
+
answered.add(String(msg.tool_call_id));
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
const out = [];
|
|
1139
|
+
let cursor = 0;
|
|
1140
|
+
while (cursor < messages.length) {
|
|
1141
|
+
const msg = messages[cursor];
|
|
1142
|
+
out.push(msg);
|
|
1143
|
+
cursor += 1;
|
|
1144
|
+
if (msg.role !== "assistant" || !msg.tool_calls?.length) {
|
|
1145
|
+
continue;
|
|
1146
|
+
}
|
|
1147
|
+
while (cursor < messages.length && messages[cursor].role === "tool") {
|
|
1148
|
+
out.push(messages[cursor]);
|
|
1149
|
+
cursor += 1;
|
|
1150
|
+
}
|
|
1151
|
+
for (const call of msg.tool_calls) {
|
|
1152
|
+
const id = call.id ? String(call.id) : "";
|
|
1153
|
+
if (id && !answered.has(id)) {
|
|
1154
|
+
out.push({ role: "tool", tool_call_id: id, content: SYNTHETIC_TOOL_RESPONSE });
|
|
1155
|
+
answered.add(id);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
messages.length = 0;
|
|
1160
|
+
messages.push(...out);
|
|
1161
|
+
}
|
|
1162
|
+
function applyGeminiFixups(messages, model) {
|
|
1163
|
+
if (!isGeminiModel(model)) {
|
|
1164
|
+
return;
|
|
1165
|
+
}
|
|
1166
|
+
mergeSystemMessages(messages);
|
|
1167
|
+
applyGeminiToolUseShim(messages);
|
|
1168
|
+
synthesizeMissingToolResponses(messages);
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1076
1171
|
// src/translate/openai/translateRequest.ts
|
|
1077
1172
|
function translateRequest2(data, options = {}) {
|
|
1078
1173
|
const messages = [];
|
|
@@ -1128,6 +1223,7 @@ function translateRequest2(data, options = {}) {
|
|
|
1128
1223
|
}
|
|
1129
1224
|
}
|
|
1130
1225
|
repairToolMessageOrder(messages);
|
|
1226
|
+
applyGeminiFixups(messages, data.model);
|
|
1131
1227
|
return { request };
|
|
1132
1228
|
}
|
|
1133
1229
|
function buildSystemContent(instructions) {
|
|
@@ -1499,43 +1595,41 @@ function repairToolMessageOrder(messages) {
|
|
|
1499
1595
|
if (messages.length === 0) {
|
|
1500
1596
|
return;
|
|
1501
1597
|
}
|
|
1502
|
-
const
|
|
1503
|
-
let currentBlock = null;
|
|
1598
|
+
const toolById = /* @__PURE__ */ new Map();
|
|
1504
1599
|
for (const msg of messages) {
|
|
1505
|
-
if (msg.role === "
|
|
1506
|
-
|
|
1507
|
-
blocks.push(currentBlock);
|
|
1508
|
-
} else if (currentBlock) {
|
|
1509
|
-
currentBlock.trailing.push(msg);
|
|
1510
|
-
} else {
|
|
1511
|
-
blocks.push({ assistant: { role: "assistant", content: null }, trailing: [msg] });
|
|
1600
|
+
if (msg.role === "tool" && msg.tool_call_id !== void 0 && !toolById.has(String(msg.tool_call_id))) {
|
|
1601
|
+
toolById.set(String(msg.tool_call_id), msg);
|
|
1512
1602
|
}
|
|
1513
1603
|
}
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
)
|
|
1518
|
-
if (toolCallIds.size === 0) {
|
|
1604
|
+
const used = /* @__PURE__ */ new Set();
|
|
1605
|
+
const out = [];
|
|
1606
|
+
for (const msg of messages) {
|
|
1607
|
+
if (msg.role === "tool") {
|
|
1519
1608
|
continue;
|
|
1520
1609
|
}
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1610
|
+
if (msg.role !== "assistant") {
|
|
1611
|
+
out.push(msg);
|
|
1612
|
+
continue;
|
|
1613
|
+
}
|
|
1614
|
+
const calls = msg.tool_calls ?? [];
|
|
1615
|
+
if (calls.length === 0) {
|
|
1616
|
+
if (msg.content != null || msg.reasoning_content != null) {
|
|
1617
|
+
out.push(msg);
|
|
1528
1618
|
}
|
|
1619
|
+
continue;
|
|
1529
1620
|
}
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1621
|
+
out.push(msg);
|
|
1622
|
+
for (const call of calls) {
|
|
1623
|
+
const id = call.id ? String(call.id) : "";
|
|
1624
|
+
const tool = id ? toolById.get(id) : void 0;
|
|
1625
|
+
if (id && tool && !used.has(id)) {
|
|
1626
|
+
out.push(tool);
|
|
1627
|
+
used.add(id);
|
|
1628
|
+
}
|
|
1536
1629
|
}
|
|
1537
|
-
messages.push(...block.trailing);
|
|
1538
1630
|
}
|
|
1631
|
+
messages.length = 0;
|
|
1632
|
+
messages.push(...out);
|
|
1539
1633
|
}
|
|
1540
1634
|
|
|
1541
1635
|
// src/translate/openai/translateResponse.ts
|