@kenkaiiii/gg-ai 4.3.185 → 4.3.186

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
@@ -767,6 +767,9 @@ function normalizeOpenAIStopReason(reason) {
767
767
  }
768
768
 
769
769
  // src/providers/anthropic.ts
770
+ function isJsonObject(value) {
771
+ return value != null && typeof value === "object" && !Array.isArray(value);
772
+ }
770
773
  function createClient(options) {
771
774
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
772
775
  return new import_sdk.default({
@@ -925,6 +928,7 @@ async function* runStream(options) {
925
928
  if (block.type === "tool_use") {
926
929
  accum.toolId = block.id;
927
930
  accum.toolName = block.name;
931
+ accum.input = block.input;
928
932
  } else if (block.type === "server_tool_use") {
929
933
  accum.toolId = block.id;
930
934
  accum.toolName = block.name;
@@ -980,10 +984,13 @@ async function* runStream(options) {
980
984
  });
981
985
  yield keepalive;
982
986
  } else if (accum.type === "tool_use") {
983
- let args = {};
984
- try {
985
- args = JSON.parse(accum.argsJson);
986
- } catch {
987
+ let args = isJsonObject(accum.input) ? accum.input : {};
988
+ if (accum.argsJson) {
989
+ try {
990
+ const parsed = JSON.parse(accum.argsJson);
991
+ args = isJsonObject(parsed) ? parsed : {};
992
+ } catch {
993
+ }
987
994
  }
988
995
  const tc = {
989
996
  type: "tool_call",
@@ -1181,8 +1188,13 @@ function messageToResponse(message) {
1181
1188
  }
1182
1189
  function toError(err) {
1183
1190
  if (err instanceof import_sdk.default.APIError) {
1184
- const requestId = err.request_id ?? err.error?.request_id;
1185
- return new ProviderError("anthropic", err.message, {
1191
+ const errorBody = err.error;
1192
+ const nestedError = errorBody?.error;
1193
+ const requestId = err.requestID ?? err.request_id ?? (typeof errorBody?.request_id === "string" ? errorBody.request_id : void 0) ?? (typeof nestedError?.request_id === "string" ? nestedError.request_id : void 0) ?? void 0;
1194
+ const bodyMessage = typeof nestedError?.message === "string" ? nestedError.message : typeof errorBody?.message === "string" ? errorBody.message : void 0;
1195
+ const bodyType = typeof nestedError?.type === "string" ? nestedError.type : typeof errorBody?.type === "string" ? errorBody.type : typeof err.type === "string" ? err.type : void 0;
1196
+ const message = bodyType && bodyMessage ? `${bodyType}: ${bodyMessage}` : bodyMessage ?? err.message;
1197
+ return new ProviderError("anthropic", message, {
1186
1198
  statusCode: err.status,
1187
1199
  ...requestId ? { requestId } : {},
1188
1200
  cause: err
@@ -1196,6 +1208,19 @@ function toError(err) {
1196
1208
 
1197
1209
  // src/providers/openai.ts
1198
1210
  var import_openai = __toESM(require("openai"), 1);
1211
+ function isJsonObject2(value) {
1212
+ return value != null && typeof value === "object" && !Array.isArray(value);
1213
+ }
1214
+ function parseToolArguments(argsJson) {
1215
+ if (!argsJson) return {};
1216
+ try {
1217
+ const parsed = JSON.parse(argsJson);
1218
+ const unwrapped = typeof parsed === "string" ? JSON.parse(parsed) : parsed;
1219
+ return isJsonObject2(unwrapped) ? unwrapped : {};
1220
+ } catch {
1221
+ return {};
1222
+ }
1223
+ }
1199
1224
  function createClient2(options) {
1200
1225
  return new import_openai.default({
1201
1226
  apiKey: options.apiKey,
@@ -1353,11 +1378,7 @@ async function* runStream2(options) {
1353
1378
  contentParts.push({ type: "text", text: textAccum });
1354
1379
  }
1355
1380
  for (const [, tc] of toolCallAccum) {
1356
- let args = {};
1357
- try {
1358
- args = JSON.parse(tc.argsJson);
1359
- } catch {
1360
- }
1381
+ const args = parseToolArguments(tc.argsJson);
1361
1382
  const toolCall = {
1362
1383
  type: "tool_call",
1363
1384
  id: tc.id,
@@ -1410,11 +1431,7 @@ function* synthesizeEventsFromCompletion(completion, thinkingEnabled) {
1410
1431
  argsJson
1411
1432
  };
1412
1433
  }
1413
- let args = {};
1414
- try {
1415
- args = JSON.parse(argsJson);
1416
- } catch {
1417
- }
1434
+ const args = parseToolArguments(argsJson);
1418
1435
  yield {
1419
1436
  type: "toolcall_done",
1420
1437
  id: tc.id,
@@ -1442,11 +1459,7 @@ function completionToResponse(completion) {
1442
1459
  const toolCalls = msg.tool_calls;
1443
1460
  if (toolCalls) {
1444
1461
  for (const tc of toolCalls) {
1445
- let args = {};
1446
- try {
1447
- args = JSON.parse(tc.function?.arguments ?? "{}");
1448
- } catch {
1449
- }
1462
+ const args = parseToolArguments(tc.function?.arguments ?? "");
1450
1463
  const toolCall = {
1451
1464
  type: "tool_call",
1452
1465
  id: tc.id,
@@ -1521,6 +1534,19 @@ function providerDiag(phase, data) {
1521
1534
 
1522
1535
  // src/providers/openai-codex.ts
1523
1536
  var DEFAULT_BASE_URL = "https://chatgpt.com/backend-api";
1537
+ function isJsonObject3(value) {
1538
+ return value != null && typeof value === "object" && !Array.isArray(value);
1539
+ }
1540
+ function parseToolArguments2(argsJson) {
1541
+ if (!argsJson) return {};
1542
+ try {
1543
+ const parsed = JSON.parse(argsJson);
1544
+ const unwrapped = typeof parsed === "string" ? JSON.parse(parsed) : parsed;
1545
+ return isJsonObject3(unwrapped) ? unwrapped : {};
1546
+ } catch {
1547
+ return {};
1548
+ }
1549
+ }
1524
1550
  function streamOpenAICodex(options) {
1525
1551
  return new StreamResult(runStream3(options));
1526
1552
  }
@@ -1693,11 +1719,7 @@ async function* runStream3(options) {
1693
1719
  const id = `${callId}|${itemId}`;
1694
1720
  const tc = toolCalls.get(id);
1695
1721
  if (tc) {
1696
- let args = {};
1697
- try {
1698
- args = JSON.parse(tc.argsJson);
1699
- } catch {
1700
- }
1722
+ const args = parseToolArguments2(tc.argsJson);
1701
1723
  yield {
1702
1724
  type: "toolcall_done",
1703
1725
  id: tc.id,
@@ -1721,11 +1743,7 @@ async function* runStream3(options) {
1721
1743
  contentParts.push({ type: "text", text: textAccum });
1722
1744
  }
1723
1745
  for (const [, tc] of toolCalls) {
1724
- let args = {};
1725
- try {
1726
- args = JSON.parse(tc.argsJson);
1727
- } catch {
1728
- }
1746
+ const args = parseToolArguments2(tc.argsJson);
1729
1747
  const toolCall = {
1730
1748
  type: "tool_call",
1731
1749
  id: tc.id,