@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.cjs CHANGED
@@ -1073,9 +1073,66 @@ __export(openai_exports, {
1073
1073
  translateStream: () => translateStream2
1074
1074
  });
1075
1075
 
1076
+ // src/translate/openai/toolSchema.ts
1077
+ function sanitizeJsonSchema(schema, depth = 0) {
1078
+ if (!schema || typeof schema !== "object" || Array.isArray(schema) || depth > 20) {
1079
+ return schema;
1080
+ }
1081
+ const src = schema;
1082
+ if ("$ref" in src) {
1083
+ return src.description ? { description: src.description } : {};
1084
+ }
1085
+ const out = {};
1086
+ for (const [key, val] of Object.entries(src)) {
1087
+ if (isDroppedSchemaKeyword(key)) {
1088
+ continue;
1089
+ }
1090
+ if (key === "properties" && val && typeof val === "object" && !Array.isArray(val)) {
1091
+ const props = {};
1092
+ for (const [propName, propSchema] of Object.entries(val)) {
1093
+ props[propName] = sanitizeJsonSchema(propSchema, depth + 1);
1094
+ }
1095
+ out[key] = props;
1096
+ } else if (key === "additionalProperties") {
1097
+ if (typeof val !== "boolean") {
1098
+ out[key] = sanitizeJsonSchema(val, depth + 1);
1099
+ }
1100
+ } else if (key === "items") {
1101
+ out[key] = sanitizeJsonSchema(val, depth + 1);
1102
+ } else if (isSchemaCompositionKeyword(key) && Array.isArray(val)) {
1103
+ out[key] = val.map((schemaItem) => sanitizeJsonSchema(schemaItem, depth + 1));
1104
+ } else {
1105
+ out[key] = val;
1106
+ }
1107
+ }
1108
+ return out;
1109
+ }
1110
+ function getValidFunctionNames(tools) {
1111
+ const names = /* @__PURE__ */ new Set();
1112
+ for (const tool of tools) {
1113
+ const maybeTool = tool;
1114
+ if (maybeTool.type === "function" && typeof maybeTool.function?.name === "string") {
1115
+ names.add(maybeTool.function.name);
1116
+ }
1117
+ }
1118
+ return names.size ? names : void 0;
1119
+ }
1120
+ function isDroppedSchemaKeyword(key) {
1121
+ return key === "$schema" || key === "$defs" || key === "definitions" || key === "$id" || key === "$anchor" || key === "$comment";
1122
+ }
1123
+ function isSchemaCompositionKeyword(key) {
1124
+ return key === "anyOf" || key === "oneOf" || key === "allOf" || key === "not";
1125
+ }
1126
+
1076
1127
  // src/translate/openai/translateRequest.ts
1077
1128
  function translateRequest2(data, options = {}) {
1078
1129
  const messages = [];
1130
+ const tools = mapTools2(data.tools ?? []);
1131
+ const validFunctionNames = getValidFunctionNames(tools);
1132
+ const context = {
1133
+ validFunctionNames,
1134
+ ignoredToolCallIds: /* @__PURE__ */ new Set()
1135
+ };
1079
1136
  const systemContent = buildSystemContent(data.instructions);
1080
1137
  if (systemContent) {
1081
1138
  messages.push({ role: "system", content: systemContent });
@@ -1090,7 +1147,7 @@ function translateRequest2(data, options = {}) {
1090
1147
  continue;
1091
1148
  }
1092
1149
  const rawItem = raw;
1093
- processInputItem(rawItem, messages, options);
1150
+ processInputItem(rawItem, messages, options, context);
1094
1151
  }
1095
1152
  const request = {
1096
1153
  model: data.model,
@@ -1111,7 +1168,6 @@ function translateRequest2(data, options = {}) {
1111
1168
  if (typeof maxTokens === "number") {
1112
1169
  request.max_tokens = maxTokens;
1113
1170
  }
1114
- const tools = mapTools2(data.tools ?? []);
1115
1171
  if (tools.length) {
1116
1172
  request.tools = tools;
1117
1173
  const toolChoice = mapToolChoice2(data.tool_choice);
@@ -1150,7 +1206,7 @@ function buildSystemContent(instructions) {
1150
1206
  }
1151
1207
  return out;
1152
1208
  }
1153
- function processInputItem(item, messages, options) {
1209
+ function processInputItem(item, messages, options, context) {
1154
1210
  const itemType = String(item.type) || "message";
1155
1211
  const getLastAssistant = () => {
1156
1212
  const last = messages[messages.length - 1];
@@ -1287,15 +1343,15 @@ function processInputItem(item, messages, options) {
1287
1343
  return;
1288
1344
  }
1289
1345
  if (itemType === "function_call" || itemType === "commandExecution" || itemType === "local_shell_call" || itemType === "fileChange" || itemType === "custom_tool_call" || itemType === "web_search_call") {
1290
- processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature);
1346
+ processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature, context);
1291
1347
  return;
1292
1348
  }
1293
1349
  if (itemType === "function_call_output" || itemType === "commandExecutionOutput" || itemType === "fileChangeOutput" || itemType === "custom_tool_call_output") {
1294
- processToolOutput(item, messages);
1350
+ processToolOutput(item, messages, context);
1295
1351
  return;
1296
1352
  }
1297
1353
  }
1298
- function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature) {
1354
+ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature, context) {
1299
1355
  const callId = String(item.call_id ?? "") || String(item.id ?? "") || makeId("call");
1300
1356
  let name = item.name === void 0 ? void 0 : String(item.name);
1301
1357
  const itemType = item.type === void 0 ? void 0 : String(item.type);
@@ -1337,6 +1393,14 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
1337
1393
  if (!name) {
1338
1394
  return;
1339
1395
  }
1396
+ if (context?.validFunctionNames?.size && !context.validFunctionNames.has(name)) {
1397
+ context.ignoredToolCallIds.add(callId);
1398
+ const amsg2 = getLastAssistant();
1399
+ const note = `Unsupported tool call omitted: ${name}`;
1400
+ amsg2.content = typeof amsg2.content === "string" && amsg2.content ? `${amsg2.content}
1401
+ ${note}` : note;
1402
+ return;
1403
+ }
1340
1404
  const amsg = getLastAssistant();
1341
1405
  if (!amsg.tool_calls) {
1342
1406
  amsg.tool_calls = [];
@@ -1357,7 +1421,7 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
1357
1421
  amsg.reasoning_content = (amsg.reasoning_content ?? "") + thought;
1358
1422
  }
1359
1423
  }
1360
- function processToolOutput(item, messages) {
1424
+ function processToolOutput(item, messages, context) {
1361
1425
  const callId = item.call_id === void 0 ? void 0 : String(item.call_id);
1362
1426
  const outputRaw = item.output ?? item.content ?? item.stdout ?? "";
1363
1427
  let content = "";
@@ -1384,6 +1448,15 @@ function processToolOutput(item, messages) {
1384
1448
  if (!content && typeof item.stderr === "string" && item.stderr) {
1385
1449
  content = `Error: ${item.stderr}`;
1386
1450
  }
1451
+ if (callId && context?.ignoredToolCallIds.has(callId)) {
1452
+ if (content) {
1453
+ messages.push({
1454
+ role: "user",
1455
+ content: `Output for omitted unsupported tool call: ${content}`
1456
+ });
1457
+ }
1458
+ return;
1459
+ }
1387
1460
  messages.push({
1388
1461
  role: "tool",
1389
1462
  tool_call_id: callId,
@@ -1403,7 +1476,8 @@ function mapTools2(tools) {
1403
1476
  if (!name) {
1404
1477
  continue;
1405
1478
  }
1406
- const params = fn?.parameters ?? tool.parameters ?? { type: "object" };
1479
+ const rawParams = fn?.parameters ?? tool.parameters ?? { type: "object" };
1480
+ const params = sanitizeJsonSchema(rawParams);
1407
1481
  out.push({
1408
1482
  type: "function",
1409
1483
  function: {