@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 CHANGED
@@ -1073,66 +1073,9 @@ __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
-
1127
1076
  // src/translate/openai/translateRequest.ts
1128
1077
  function translateRequest2(data, options = {}) {
1129
1078
  const messages = [];
1130
- const tools = mapTools2(data.tools ?? []);
1131
- const validFunctionNames = getValidFunctionNames(tools);
1132
- const context = {
1133
- validFunctionNames,
1134
- ignoredToolCallIds: /* @__PURE__ */ new Set()
1135
- };
1136
1079
  const systemContent = buildSystemContent(data.instructions);
1137
1080
  if (systemContent) {
1138
1081
  messages.push({ role: "system", content: systemContent });
@@ -1147,7 +1090,7 @@ function translateRequest2(data, options = {}) {
1147
1090
  continue;
1148
1091
  }
1149
1092
  const rawItem = raw;
1150
- processInputItem(rawItem, messages, options, context);
1093
+ processInputItem(rawItem, messages, options);
1151
1094
  }
1152
1095
  const request = {
1153
1096
  model: data.model,
@@ -1168,6 +1111,7 @@ function translateRequest2(data, options = {}) {
1168
1111
  if (typeof maxTokens === "number") {
1169
1112
  request.max_tokens = maxTokens;
1170
1113
  }
1114
+ const tools = mapTools2(data.tools ?? []);
1171
1115
  if (tools.length) {
1172
1116
  request.tools = tools;
1173
1117
  const toolChoice = mapToolChoice2(data.tool_choice);
@@ -1206,7 +1150,7 @@ function buildSystemContent(instructions) {
1206
1150
  }
1207
1151
  return out;
1208
1152
  }
1209
- function processInputItem(item, messages, options, context) {
1153
+ function processInputItem(item, messages, options) {
1210
1154
  const itemType = String(item.type) || "message";
1211
1155
  const getLastAssistant = () => {
1212
1156
  const last = messages[messages.length - 1];
@@ -1343,15 +1287,15 @@ function processInputItem(item, messages, options, context) {
1343
1287
  return;
1344
1288
  }
1345
1289
  if (itemType === "function_call" || itemType === "commandExecution" || itemType === "local_shell_call" || itemType === "fileChange" || itemType === "custom_tool_call" || itemType === "web_search_call") {
1346
- processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature, context);
1290
+ processToolCall(item, messages, getLastAssistant, options.fallbackThoughtSignature);
1347
1291
  return;
1348
1292
  }
1349
1293
  if (itemType === "function_call_output" || itemType === "commandExecutionOutput" || itemType === "fileChangeOutput" || itemType === "custom_tool_call_output") {
1350
- processToolOutput(item, messages, context);
1294
+ processToolOutput(item, messages);
1351
1295
  return;
1352
1296
  }
1353
1297
  }
1354
- function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature, context) {
1298
+ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignature) {
1355
1299
  const callId = String(item.call_id ?? "") || String(item.id ?? "") || makeId("call");
1356
1300
  let name = item.name === void 0 ? void 0 : String(item.name);
1357
1301
  const itemType = item.type === void 0 ? void 0 : String(item.type);
@@ -1393,14 +1337,6 @@ function processToolCall(item, messages, getLastAssistant, fallbackThoughtSignat
1393
1337
  if (!name) {
1394
1338
  return;
1395
1339
  }
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
- }
1404
1340
  const amsg = getLastAssistant();
1405
1341
  if (!amsg.tool_calls) {
1406
1342
  amsg.tool_calls = [];
@@ -1421,7 +1357,7 @@ ${note}` : note;
1421
1357
  amsg.reasoning_content = (amsg.reasoning_content ?? "") + thought;
1422
1358
  }
1423
1359
  }
1424
- function processToolOutput(item, messages, context) {
1360
+ function processToolOutput(item, messages) {
1425
1361
  const callId = item.call_id === void 0 ? void 0 : String(item.call_id);
1426
1362
  const outputRaw = item.output ?? item.content ?? item.stdout ?? "";
1427
1363
  let content = "";
@@ -1448,15 +1384,6 @@ function processToolOutput(item, messages, context) {
1448
1384
  if (!content && typeof item.stderr === "string" && item.stderr) {
1449
1385
  content = `Error: ${item.stderr}`;
1450
1386
  }
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
- }
1460
1387
  messages.push({
1461
1388
  role: "tool",
1462
1389
  tool_call_id: callId,
@@ -1476,8 +1403,7 @@ function mapTools2(tools) {
1476
1403
  if (!name) {
1477
1404
  continue;
1478
1405
  }
1479
- const rawParams = fn?.parameters ?? tool.parameters ?? { type: "object" };
1480
- const params = sanitizeJsonSchema(rawParams);
1406
+ const params = fn?.parameters ?? tool.parameters ?? { type: "object" };
1481
1407
  out.push({
1482
1408
  type: "function",
1483
1409
  function: {
@@ -1666,6 +1592,15 @@ async function* translateStream2(stream, options = {}) {
1666
1592
  function isDoneMessage(msg) {
1667
1593
  return msg.data.trim() === "[DONE]";
1668
1594
  }
1595
+ function getToolCallKey(tc, ordinal) {
1596
+ if (typeof tc.index === "number") {
1597
+ return `index:${tc.index}`;
1598
+ }
1599
+ if (tc.id) {
1600
+ return `id:${tc.id}`;
1601
+ }
1602
+ return `ordinal:${ordinal}`;
1603
+ }
1669
1604
  var StreamTranslator2 = class {
1670
1605
  constructor(options) {
1671
1606
  __publicField(this, "model");
@@ -1724,9 +1659,9 @@ var StreamTranslator2 = class {
1724
1659
  return;
1725
1660
  }
1726
1661
  if (delta.tool_calls?.length) {
1727
- for (const tc of delta.tool_calls) {
1728
- const idx = tc.index ?? 0;
1729
- let state = this.toolCalls.get(idx);
1662
+ for (const [ordinal, tc] of delta.tool_calls.entries()) {
1663
+ const key = getToolCallKey(tc, ordinal);
1664
+ let state = this.toolCalls.get(key);
1730
1665
  if (!state) {
1731
1666
  const outputIndex = this.outputCounter++;
1732
1667
  const callId = tc.id ?? makeId("call");
@@ -1739,7 +1674,7 @@ var StreamTranslator2 = class {
1739
1674
  call_id: callId
1740
1675
  };
1741
1676
  state = { outputIndex, item };
1742
- this.toolCalls.set(idx, state);
1677
+ this.toolCalls.set(key, state);
1743
1678
  yield this.makeEvent("response.output_item.added", {
1744
1679
  response_id: this.responseId,
1745
1680
  output_index: outputIndex,