@codeproxy/core 0.1.8 → 0.1.9
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 +21 -86
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +21 -86
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1071,66 +1071,9 @@ __export(openai_exports, {
|
|
|
1071
1071
|
translateStream: () => translateStream2
|
|
1072
1072
|
});
|
|
1073
1073
|
|
|
1074
|
-
// src/translate/openai/toolSchema.ts
|
|
1075
|
-
function sanitizeJsonSchema(schema, depth = 0) {
|
|
1076
|
-
if (!schema || typeof schema !== "object" || Array.isArray(schema) || depth > 20) {
|
|
1077
|
-
return schema;
|
|
1078
|
-
}
|
|
1079
|
-
const src = schema;
|
|
1080
|
-
if ("$ref" in src) {
|
|
1081
|
-
return src.description ? { description: src.description } : {};
|
|
1082
|
-
}
|
|
1083
|
-
const out = {};
|
|
1084
|
-
for (const [key, val] of Object.entries(src)) {
|
|
1085
|
-
if (isDroppedSchemaKeyword(key)) {
|
|
1086
|
-
continue;
|
|
1087
|
-
}
|
|
1088
|
-
if (key === "properties" && val && typeof val === "object" && !Array.isArray(val)) {
|
|
1089
|
-
const props = {};
|
|
1090
|
-
for (const [propName, propSchema] of Object.entries(val)) {
|
|
1091
|
-
props[propName] = sanitizeJsonSchema(propSchema, depth + 1);
|
|
1092
|
-
}
|
|
1093
|
-
out[key] = props;
|
|
1094
|
-
} else if (key === "additionalProperties") {
|
|
1095
|
-
if (typeof val !== "boolean") {
|
|
1096
|
-
out[key] = sanitizeJsonSchema(val, depth + 1);
|
|
1097
|
-
}
|
|
1098
|
-
} else if (key === "items") {
|
|
1099
|
-
out[key] = sanitizeJsonSchema(val, depth + 1);
|
|
1100
|
-
} else if (isSchemaCompositionKeyword(key) && Array.isArray(val)) {
|
|
1101
|
-
out[key] = val.map((schemaItem) => sanitizeJsonSchema(schemaItem, depth + 1));
|
|
1102
|
-
} else {
|
|
1103
|
-
out[key] = val;
|
|
1104
|
-
}
|
|
1105
|
-
}
|
|
1106
|
-
return out;
|
|
1107
|
-
}
|
|
1108
|
-
function getValidFunctionNames(tools) {
|
|
1109
|
-
const names = /* @__PURE__ */ new Set();
|
|
1110
|
-
for (const tool of tools) {
|
|
1111
|
-
const maybeTool = tool;
|
|
1112
|
-
if (maybeTool.type === "function" && typeof maybeTool.function?.name === "string") {
|
|
1113
|
-
names.add(maybeTool.function.name);
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
return names.size ? names : void 0;
|
|
1117
|
-
}
|
|
1118
|
-
function isDroppedSchemaKeyword(key) {
|
|
1119
|
-
return key === "$schema" || key === "$defs" || key === "definitions" || key === "$id" || key === "$anchor" || key === "$comment";
|
|
1120
|
-
}
|
|
1121
|
-
function isSchemaCompositionKeyword(key) {
|
|
1122
|
-
return key === "anyOf" || key === "oneOf" || key === "allOf" || key === "not";
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
1074
|
// src/translate/openai/translateRequest.ts
|
|
1126
1075
|
function translateRequest2(data, options = {}) {
|
|
1127
1076
|
const messages = [];
|
|
1128
|
-
const tools = mapTools2(data.tools ?? []);
|
|
1129
|
-
const validFunctionNames = getValidFunctionNames(tools);
|
|
1130
|
-
const context = {
|
|
1131
|
-
validFunctionNames,
|
|
1132
|
-
ignoredToolCallIds: /* @__PURE__ */ new Set()
|
|
1133
|
-
};
|
|
1134
1077
|
const systemContent = buildSystemContent(data.instructions);
|
|
1135
1078
|
if (systemContent) {
|
|
1136
1079
|
messages.push({ role: "system", content: systemContent });
|
|
@@ -1145,7 +1088,7 @@ function translateRequest2(data, options = {}) {
|
|
|
1145
1088
|
continue;
|
|
1146
1089
|
}
|
|
1147
1090
|
const rawItem = raw;
|
|
1148
|
-
processInputItem(rawItem, messages, options
|
|
1091
|
+
processInputItem(rawItem, messages, options);
|
|
1149
1092
|
}
|
|
1150
1093
|
const request = {
|
|
1151
1094
|
model: data.model,
|
|
@@ -1166,6 +1109,7 @@ function translateRequest2(data, options = {}) {
|
|
|
1166
1109
|
if (typeof maxTokens === "number") {
|
|
1167
1110
|
request.max_tokens = maxTokens;
|
|
1168
1111
|
}
|
|
1112
|
+
const tools = mapTools2(data.tools ?? []);
|
|
1169
1113
|
if (tools.length) {
|
|
1170
1114
|
request.tools = tools;
|
|
1171
1115
|
const toolChoice = mapToolChoice2(data.tool_choice);
|
|
@@ -1204,7 +1148,7 @@ function buildSystemContent(instructions) {
|
|
|
1204
1148
|
}
|
|
1205
1149
|
return out;
|
|
1206
1150
|
}
|
|
1207
|
-
function processInputItem(item, messages, options
|
|
1151
|
+
function processInputItem(item, messages, options) {
|
|
1208
1152
|
const itemType = String(item.type) || "message";
|
|
1209
1153
|
const getLastAssistant = () => {
|
|
1210
1154
|
const last = messages[messages.length - 1];
|
|
@@ -1341,15 +1285,15 @@ function processInputItem(item, messages, options, context) {
|
|
|
1341
1285
|
return;
|
|
1342
1286
|
}
|
|
1343
1287
|
if (itemType === "function_call" || itemType === "commandExecution" || itemType === "local_shell_call" || itemType === "fileChange" || itemType === "custom_tool_call" || itemType === "web_search_call") {
|
|
1344
|
-
processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature
|
|
1288
|
+
processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature);
|
|
1345
1289
|
return;
|
|
1346
1290
|
}
|
|
1347
1291
|
if (itemType === "function_call_output" || itemType === "commandExecutionOutput" || itemType === "fileChangeOutput" || itemType === "custom_tool_call_output") {
|
|
1348
|
-
processToolOutput(item, messages
|
|
1292
|
+
processToolOutput(item, messages);
|
|
1349
1293
|
return;
|
|
1350
1294
|
}
|
|
1351
1295
|
}
|
|
1352
|
-
function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature
|
|
1296
|
+
function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature) {
|
|
1353
1297
|
const callId = String(item.call_id ?? "") || String(item.id ?? "") || makeId("call");
|
|
1354
1298
|
let name = item.name === void 0 ? void 0 : String(item.name);
|
|
1355
1299
|
const itemType = item.type === void 0 ? void 0 : String(item.type);
|
|
@@ -1391,14 +1335,6 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
|
|
|
1391
1335
|
if (!name) {
|
|
1392
1336
|
return;
|
|
1393
1337
|
}
|
|
1394
|
-
if (context?.validFunctionNames?.size && !context.validFunctionNames.has(name)) {
|
|
1395
|
-
context.ignoredToolCallIds.add(callId);
|
|
1396
|
-
const amsg2 = getLastAssistant();
|
|
1397
|
-
const note = `Unsupported tool call omitted: ${name}`;
|
|
1398
|
-
amsg2.content = typeof amsg2.content === "string" && amsg2.content ? `${amsg2.content}
|
|
1399
|
-
${note}` : note;
|
|
1400
|
-
return;
|
|
1401
|
-
}
|
|
1402
1338
|
const amsg = getLastAssistant();
|
|
1403
1339
|
if (!amsg.tool_calls) {
|
|
1404
1340
|
amsg.tool_calls = [];
|
|
@@ -1419,7 +1355,7 @@ ${note}` : note;
|
|
|
1419
1355
|
amsg.reasoning_content = (amsg.reasoning_content ?? "") + thought;
|
|
1420
1356
|
}
|
|
1421
1357
|
}
|
|
1422
|
-
function processToolOutput(item, messages
|
|
1358
|
+
function processToolOutput(item, messages) {
|
|
1423
1359
|
const callId = item.call_id === void 0 ? void 0 : String(item.call_id);
|
|
1424
1360
|
const outputRaw = item.output ?? item.content ?? item.stdout ?? "";
|
|
1425
1361
|
let content = "";
|
|
@@ -1446,15 +1382,6 @@ function processToolOutput(item, messages, context) {
|
|
|
1446
1382
|
if (!content && typeof item.stderr === "string" && item.stderr) {
|
|
1447
1383
|
content = `Error: ${item.stderr}`;
|
|
1448
1384
|
}
|
|
1449
|
-
if (callId && context?.ignoredToolCallIds.has(callId)) {
|
|
1450
|
-
if (content) {
|
|
1451
|
-
messages.push({
|
|
1452
|
-
role: "user",
|
|
1453
|
-
content: `Output for omitted unsupported tool call: ${content}`
|
|
1454
|
-
});
|
|
1455
|
-
}
|
|
1456
|
-
return;
|
|
1457
|
-
}
|
|
1458
1385
|
messages.push({
|
|
1459
1386
|
role: "tool",
|
|
1460
1387
|
tool_call_id: callId,
|
|
@@ -1474,8 +1401,7 @@ function mapTools2(tools) {
|
|
|
1474
1401
|
if (!name) {
|
|
1475
1402
|
continue;
|
|
1476
1403
|
}
|
|
1477
|
-
const
|
|
1478
|
-
const params = sanitizeJsonSchema(rawParams);
|
|
1404
|
+
const params = fn?.parameters ?? tool.parameters ?? { type: "object" };
|
|
1479
1405
|
out.push({
|
|
1480
1406
|
type: "function",
|
|
1481
1407
|
function: {
|
|
@@ -1664,6 +1590,15 @@ async function* translateStream2(stream, options = {}) {
|
|
|
1664
1590
|
function isDoneMessage(msg) {
|
|
1665
1591
|
return msg.data.trim() === "[DONE]";
|
|
1666
1592
|
}
|
|
1593
|
+
function getToolCallKey(tc, ordinal) {
|
|
1594
|
+
if (typeof tc.index === "number") {
|
|
1595
|
+
return `index:${tc.index}`;
|
|
1596
|
+
}
|
|
1597
|
+
if (tc.id) {
|
|
1598
|
+
return `id:${tc.id}`;
|
|
1599
|
+
}
|
|
1600
|
+
return `ordinal:${ordinal}`;
|
|
1601
|
+
}
|
|
1667
1602
|
var StreamTranslator2 = class {
|
|
1668
1603
|
constructor(options) {
|
|
1669
1604
|
__publicField(this, "model");
|
|
@@ -1722,9 +1657,9 @@ var StreamTranslator2 = class {
|
|
|
1722
1657
|
return;
|
|
1723
1658
|
}
|
|
1724
1659
|
if (delta.tool_calls?.length) {
|
|
1725
|
-
for (const tc of delta.tool_calls) {
|
|
1726
|
-
const
|
|
1727
|
-
let state = this.toolCalls.get(
|
|
1660
|
+
for (const [ordinal, tc] of delta.tool_calls.entries()) {
|
|
1661
|
+
const key = getToolCallKey(tc, ordinal);
|
|
1662
|
+
let state = this.toolCalls.get(key);
|
|
1728
1663
|
if (!state) {
|
|
1729
1664
|
const outputIndex = this.outputCounter++;
|
|
1730
1665
|
const callId = tc.id ?? makeId("call");
|
|
@@ -1737,7 +1672,7 @@ var StreamTranslator2 = class {
|
|
|
1737
1672
|
call_id: callId
|
|
1738
1673
|
};
|
|
1739
1674
|
state = { outputIndex, item };
|
|
1740
|
-
this.toolCalls.set(
|
|
1675
|
+
this.toolCalls.set(key, state);
|
|
1741
1676
|
yield this.makeEvent("response.output_item.added", {
|
|
1742
1677
|
response_id: this.responseId,
|
|
1743
1678
|
output_index: outputIndex,
|