@codeproxy/core 0.1.7 → 0.1.8

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,9 +1071,66 @@ __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
+
1074
1125
  // src/translate/openai/translateRequest.ts
1075
1126
  function translateRequest2(data, options = {}) {
1076
1127
  const messages = [];
1128
+ const tools = mapTools2(data.tools ?? []);
1129
+ const validFunctionNames = getValidFunctionNames(tools);
1130
+ const context = {
1131
+ validFunctionNames,
1132
+ ignoredToolCallIds: /* @__PURE__ */ new Set()
1133
+ };
1077
1134
  const systemContent = buildSystemContent(data.instructions);
1078
1135
  if (systemContent) {
1079
1136
  messages.push({ role: "system", content: systemContent });
@@ -1088,7 +1145,7 @@ function translateRequest2(data, options = {}) {
1088
1145
  continue;
1089
1146
  }
1090
1147
  const rawItem = raw;
1091
- processInputItem(rawItem, messages, options);
1148
+ processInputItem(rawItem, messages, options, context);
1092
1149
  }
1093
1150
  const request = {
1094
1151
  model: data.model,
@@ -1109,7 +1166,6 @@ function translateRequest2(data, options = {}) {
1109
1166
  if (typeof maxTokens === "number") {
1110
1167
  request.max_tokens = maxTokens;
1111
1168
  }
1112
- const tools = mapTools2(data.tools ?? []);
1113
1169
  if (tools.length) {
1114
1170
  request.tools = tools;
1115
1171
  const toolChoice = mapToolChoice2(data.tool_choice);
@@ -1148,7 +1204,7 @@ function buildSystemContent(instructions) {
1148
1204
  }
1149
1205
  return out;
1150
1206
  }
1151
- function processInputItem(item, messages, options) {
1207
+ function processInputItem(item, messages, options, context) {
1152
1208
  const itemType = String(item.type) || "message";
1153
1209
  const getLastAssistant = () => {
1154
1210
  const last = messages[messages.length - 1];
@@ -1285,15 +1341,15 @@ function processInputItem(item, messages, options) {
1285
1341
  return;
1286
1342
  }
1287
1343
  if (itemType === "function_call" || itemType === "commandExecution" || itemType === "local_shell_call" || itemType === "fileChange" || itemType === "custom_tool_call" || itemType === "web_search_call") {
1288
- processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature);
1344
+ processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature, context);
1289
1345
  return;
1290
1346
  }
1291
1347
  if (itemType === "function_call_output" || itemType === "commandExecutionOutput" || itemType === "fileChangeOutput" || itemType === "custom_tool_call_output") {
1292
- processToolOutput(item, messages);
1348
+ processToolOutput(item, messages, context);
1293
1349
  return;
1294
1350
  }
1295
1351
  }
1296
- function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature) {
1352
+ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature, context) {
1297
1353
  const callId = String(item.call_id ?? "") || String(item.id ?? "") || makeId("call");
1298
1354
  let name = item.name === void 0 ? void 0 : String(item.name);
1299
1355
  const itemType = item.type === void 0 ? void 0 : String(item.type);
@@ -1335,6 +1391,14 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
1335
1391
  if (!name) {
1336
1392
  return;
1337
1393
  }
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
+ }
1338
1402
  const amsg = getLastAssistant();
1339
1403
  if (!amsg.tool_calls) {
1340
1404
  amsg.tool_calls = [];
@@ -1355,7 +1419,7 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
1355
1419
  amsg.reasoning_content = (amsg.reasoning_content ?? "") + thought;
1356
1420
  }
1357
1421
  }
1358
- function processToolOutput(item, messages) {
1422
+ function processToolOutput(item, messages, context) {
1359
1423
  const callId = item.call_id === void 0 ? void 0 : String(item.call_id);
1360
1424
  const outputRaw = item.output ?? item.content ?? item.stdout ?? "";
1361
1425
  let content = "";
@@ -1382,6 +1446,15 @@ function processToolOutput(item, messages) {
1382
1446
  if (!content && typeof item.stderr === "string" && item.stderr) {
1383
1447
  content = `Error: ${item.stderr}`;
1384
1448
  }
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
+ }
1385
1458
  messages.push({
1386
1459
  role: "tool",
1387
1460
  tool_call_id: callId,
@@ -1401,7 +1474,8 @@ function mapTools2(tools) {
1401
1474
  if (!name) {
1402
1475
  continue;
1403
1476
  }
1404
- const params = fn?.parameters ?? tool.parameters ?? { type: "object" };
1477
+ const rawParams = fn?.parameters ?? tool.parameters ?? { type: "object" };
1478
+ const params = sanitizeJsonSchema(rawParams);
1405
1479
  out.push({
1406
1480
  type: "function",
1407
1481
  function: {