190proof 1.0.96 → 1.0.98

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.mjs CHANGED
@@ -108,7 +108,6 @@ function isHeicImage(name, mime) {
108
108
  }
109
109
 
110
110
  // index.ts
111
- import { GoogleGenAI } from "@google/genai";
112
111
  var sharp = __require("sharp");
113
112
  var decode = __require("heic-decode");
114
113
  async function withRetries(identifier, apiName, fn, options = {}) {
@@ -139,9 +138,10 @@ async function withRetries(identifier, apiName, fn, options = {}) {
139
138
  error2.cause = lastError;
140
139
  throw error2;
141
140
  }
142
- function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allowedFunctionNames) {
141
+ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allowedFunctionNames, reasoning) {
143
142
  const functionCalls = [];
144
- for (const acc of toolCallAccumulators) {
143
+ for (let i = 0; i < toolCallAccumulators.length; i++) {
144
+ const acc = toolCallAccumulators[i];
145
145
  if (!acc.name || !acc.arguments)
146
146
  continue;
147
147
  if (allowedFunctionNames && !allowedFunctionNames.has(acc.name)) {
@@ -151,6 +151,7 @@ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allo
151
151
  }
152
152
  try {
153
153
  functionCalls.push({
154
+ id: acc.id || `call_${i}`,
154
155
  name: acc.name,
155
156
  arguments: JSON.parse(acc.arguments)
156
157
  });
@@ -179,6 +180,7 @@ function parseStreamedResponse(identifier, paragraph, toolCallAccumulators, allo
179
180
  function_call: functionCalls[0] || null,
180
181
  function_calls: functionCalls,
181
182
  files: [],
183
+ reasoning: reasoning || void 0,
182
184
  usage: null
183
185
  };
184
186
  }
@@ -280,7 +282,7 @@ function buildOpenAIRequestConfig(identifier, model, config) {
280
282
  };
281
283
  }
282
284
  async function prepareOpenAIPayload(identifier, payload) {
283
- var _a;
285
+ var _a, _b;
284
286
  const preparedPayload = {
285
287
  model: payload.model,
286
288
  messages: [],
@@ -291,6 +293,16 @@ async function prepareOpenAIPayload(identifier, payload) {
291
293
  tool_choice: payload.function_call ? typeof payload.function_call === "string" ? payload.function_call : { type: "function", function: payload.function_call } : void 0
292
294
  };
293
295
  for (const message of payload.messages) {
296
+ if (message.role === "tool") {
297
+ for (const tr of message.toolResults || []) {
298
+ preparedPayload.messages.push({
299
+ role: "tool",
300
+ tool_call_id: tr.toolCallId,
301
+ content: tr.content
302
+ });
303
+ }
304
+ continue;
305
+ }
294
306
  const contentBlocks = [];
295
307
  if (message.content) {
296
308
  contentBlocks.push({ type: "text", text: message.content });
@@ -316,15 +328,34 @@ async function prepareOpenAIPayload(identifier, payload) {
316
328
  });
317
329
  }
318
330
  }
319
- preparedPayload.messages.push({
331
+ const outMessage = {
320
332
  role: message.role,
321
- content: contentBlocks
322
- });
333
+ // OpenAI wants null (not []) content on a tool-call-only assistant turn.
334
+ content: contentBlocks.length ? contentBlocks : null
335
+ };
336
+ if ((_b = message.functionCalls) == null ? void 0 : _b.length) {
337
+ outMessage.tool_calls = message.functionCalls.map((fc, i) => {
338
+ var _a2;
339
+ return {
340
+ id: (_a2 = fc.id) != null ? _a2 : `call_${i}`,
341
+ type: "function",
342
+ function: {
343
+ name: fc.name,
344
+ arguments: JSON.stringify(fc.arguments)
345
+ }
346
+ };
347
+ });
348
+ }
349
+ if (message.reasoning)
350
+ outMessage.reasoning = message.reasoning;
351
+ if (message.reasoningDetails)
352
+ outMessage.reasoning_details = message.reasoningDetails;
353
+ preparedPayload.messages.push(outMessage);
323
354
  }
324
355
  return preparedPayload;
325
356
  }
326
357
  async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs) {
327
- var _a, _b, _c, _d, _e, _f, _g, _h;
358
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
328
359
  const functionNames = openAiPayload.tools ? new Set(openAiPayload.tools.map((fn) => fn.function.name)) : null;
329
360
  const { endpoint, headers } = buildOpenAIRequestConfig(
330
361
  id,
@@ -342,6 +373,7 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
342
373
  throw new Error("Stream error: no response body");
343
374
  }
344
375
  let paragraph = "";
376
+ let reasoning = "";
345
377
  const toolCallAccumulators = [];
346
378
  const reader = response.body.getReader();
347
379
  let partialChunk = "";
@@ -376,7 +408,8 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
376
408
  id,
377
409
  paragraph,
378
410
  toolCallAccumulators,
379
- functionNames
411
+ functionNames,
412
+ reasoning
380
413
  );
381
414
  }
382
415
  let json;
@@ -406,6 +439,8 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
406
439
  while (toolCallAccumulators.length <= idx) {
407
440
  toolCallAccumulators.push({ name: "", arguments: "" });
408
441
  }
442
+ if (toolCall.id)
443
+ toolCallAccumulators[idx].id = toolCall.id;
409
444
  if ((_e = toolCall.function) == null ? void 0 : _e.name)
410
445
  toolCallAccumulators[idx].name += toolCall.function.name;
411
446
  if ((_f = toolCall.function) == null ? void 0 : _f.arguments)
@@ -415,11 +450,14 @@ async function callOpenAIStream(id, openAiPayload, openAiConfig, chunkTimeoutMs)
415
450
  const text = (_h = (_g = json.choices[0]) == null ? void 0 : _g.delta) == null ? void 0 : _h.content;
416
451
  if (text)
417
452
  paragraph += text;
453
+ const reasoningDelta = (_j = (_i = json.choices[0]) == null ? void 0 : _i.delta) == null ? void 0 : _j.reasoning;
454
+ if (reasoningDelta)
455
+ reasoning += reasoningDelta;
418
456
  }
419
457
  }
420
458
  }
421
459
  async function callOpenAI(id, openAiPayload, openAiConfig) {
422
- var _a, _b, _c, _d, _e;
460
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
423
461
  const { endpoint, headers } = buildOpenAIRequestConfig(
424
462
  id,
425
463
  openAiPayload.model,
@@ -447,19 +485,22 @@ async function callOpenAI(id, openAiPayload, openAiConfig) {
447
485
  const toolCalls = (_b = choice.message) == null ? void 0 : _b.tool_calls;
448
486
  const functionCalls = [];
449
487
  if (toolCalls == null ? void 0 : toolCalls.length) {
450
- for (const tc of toolCalls) {
488
+ for (let i = 0; i < toolCalls.length; i++) {
489
+ const tc = toolCalls[i];
451
490
  functionCalls.push({
491
+ id: (_c = tc.id) != null ? _c : `call_${i}`,
452
492
  name: tc.function.name,
453
493
  arguments: JSON.parse(tc.function.arguments)
454
494
  });
455
495
  }
456
496
  } else if (choice.function_call) {
457
497
  functionCalls.push({
498
+ id: "call_0",
458
499
  name: choice.function_call.name,
459
500
  arguments: JSON.parse(choice.function_call.arguments)
460
501
  });
461
502
  }
462
- if (!((_c = choice.message) == null ? void 0 : _c.content) && !functionCalls.length) {
503
+ if (!((_d = choice.message) == null ? void 0 : _d.content) && !functionCalls.length) {
463
504
  logger_default.error(
464
505
  id,
465
506
  "OpenAI: received message without content or function_call:",
@@ -475,11 +516,13 @@ async function callOpenAI(id, openAiPayload, openAiConfig) {
475
516
  function_call: functionCalls[0] || null,
476
517
  function_calls: functionCalls,
477
518
  files: [],
519
+ reasoning: (_f = (_e = choice.message) == null ? void 0 : _e.reasoning) != null ? _f : void 0,
520
+ reasoningDetails: (_h = (_g = choice.message) == null ? void 0 : _g.reasoning_details) != null ? _h : void 0,
478
521
  usage: data.usage ? {
479
522
  prompt_tokens: data.usage.prompt_tokens,
480
523
  completion_tokens: data.usage.completion_tokens,
481
524
  total_tokens: data.usage.total_tokens,
482
- cached_tokens: (_e = (_d = data.usage.prompt_tokens_details) == null ? void 0 : _d.cached_tokens) != null ? _e : 0
525
+ cached_tokens: (_j = (_i = data.usage.prompt_tokens_details) == null ? void 0 : _i.cached_tokens) != null ? _j : 0
483
526
  } : null
484
527
  };
485
528
  }
@@ -532,7 +575,8 @@ async function callOpenAiWithRetries(id, openAiPayload, openAiConfig, retries =
532
575
  );
533
576
  }
534
577
  function jigAnthropicMessages(messages) {
535
- var _a, _b;
578
+ var _a;
579
+ const hasToolBlock = (content) => Array.isArray(content) && content.some((b) => b.type === "tool_use" || b.type === "tool_result");
536
580
  let jiggedMessages = messages.slice();
537
581
  if (((_a = jiggedMessages[0]) == null ? void 0 : _a.role) !== "user") {
538
582
  jiggedMessages = [
@@ -547,11 +591,8 @@ function jigAnthropicMessages(messages) {
547
591
  if (lastMessage.role === message.role) {
548
592
  const lastContent = Array.isArray(lastMessage.content) ? lastMessage.content : [{ type: "text", text: lastMessage.content }];
549
593
  const newContent = Array.isArray(message.content) ? message.content : [{ type: "text", text: message.content }];
550
- lastMessage.content = [
551
- ...lastContent,
552
- { type: "text", text: "\n\n---\n\n" },
553
- ...newContent
554
- ];
594
+ const separator = hasToolBlock(lastMessage.content) || hasToolBlock(message.content) ? [] : [{ type: "text", text: "\n\n---\n\n" }];
595
+ lastMessage.content = [...lastContent, ...separator, ...newContent];
555
596
  return acc;
556
597
  }
557
598
  if (typeof message.content === "string") {
@@ -559,7 +600,8 @@ function jigAnthropicMessages(messages) {
559
600
  }
560
601
  return [...acc, message];
561
602
  }, []);
562
- if (((_b = jiggedMessages[jiggedMessages.length - 1]) == null ? void 0 : _b.role) === "assistant") {
603
+ const last = jiggedMessages[jiggedMessages.length - 1];
604
+ if ((last == null ? void 0 : last.role) === "assistant" && !hasToolBlock(last.content)) {
563
605
  jiggedMessages.push({ role: "user", content: "..." });
564
606
  }
565
607
  return jiggedMessages;
@@ -576,6 +618,17 @@ async function prepareAnthropicPayload(_identifier, payload) {
576
618
  preparedPayload.system = message.content;
577
619
  continue;
578
620
  }
621
+ if (message.role === "tool") {
622
+ preparedPayload.messages.push({
623
+ role: "user",
624
+ content: (message.toolResults || []).map((tr) => ({
625
+ type: "tool_result",
626
+ tool_use_id: tr.toolCallId,
627
+ content: tr.content
628
+ }))
629
+ });
630
+ continue;
631
+ }
579
632
  const contentBlocks = [];
580
633
  if (message.content) {
581
634
  contentBlocks.push({ type: "text", text: message.content });
@@ -613,9 +666,19 @@ async function prepareAnthropicPayload(_identifier, payload) {
613
666
  });
614
667
  }
615
668
  }
669
+ const leadingBlocks = message.role === "assistant" && Array.isArray(message.reasoningDetails) ? message.reasoningDetails : [];
670
+ const toolUseBlocks = (message.functionCalls || []).map((fc, i) => {
671
+ var _a;
672
+ return {
673
+ type: "tool_use",
674
+ id: (_a = fc.id) != null ? _a : `call_${i}`,
675
+ name: fc.name,
676
+ input: fc.arguments
677
+ };
678
+ });
616
679
  preparedPayload.messages.push({
617
680
  role: message.role,
618
- content: contentBlocks
681
+ content: [...leadingBlocks, ...contentBlocks, ...toolUseBlocks]
619
682
  });
620
683
  }
621
684
  return preparedPayload;
@@ -682,6 +745,7 @@ async function callAnthropic(id, payload, config) {
682
745
  }
683
746
  let textResponse = "";
684
747
  const functionCalls = [];
748
+ const reasoningBlocks = [];
685
749
  for (const answer of answers) {
686
750
  if (!answer.type) {
687
751
  logger_default.error(id, "Missing answer type in Anthropic API response:", data);
@@ -701,9 +765,12 @@ async function callAnthropic(id, payload, config) {
701
765
  ${text}` : text;
702
766
  } else if (answer.type === "tool_use") {
703
767
  functionCalls.push({
768
+ id: answer.id,
704
769
  name: answer.name,
705
770
  arguments: answer.input
706
771
  });
772
+ } else if (answer.type === "thinking" || answer.type === "redacted_thinking") {
773
+ reasoningBlocks.push(answer);
707
774
  }
708
775
  }
709
776
  if (!textResponse && !functionCalls.length) {
@@ -732,6 +799,7 @@ ${text}` : text;
732
799
  function_call: functionCalls[0] || null,
733
800
  function_calls: functionCalls,
734
801
  files: [],
802
+ reasoningDetails: reasoningBlocks.length ? reasoningBlocks : void 0,
735
803
  usage
736
804
  };
737
805
  }
@@ -746,7 +814,8 @@ async function callAnthropicWithRetries(id, payload, config, retries = 5) {
746
814
  );
747
815
  }
748
816
  function jigGoogleMessages(messages) {
749
- var _a, _b;
817
+ var _a;
818
+ const hasFunctionPart = (parts) => parts.some((p) => "functionCall" in p || "functionResponse" in p);
750
819
  let jiggedMessages = messages.slice();
751
820
  if (((_a = jiggedMessages[0]) == null ? void 0 : _a.role) === "model") {
752
821
  jiggedMessages = [
@@ -764,7 +833,8 @@ function jigGoogleMessages(messages) {
764
833
  }
765
834
  return [...acc, message];
766
835
  }, []);
767
- if (((_b = jiggedMessages[jiggedMessages.length - 1]) == null ? void 0 : _b.role) === "model") {
836
+ const last = jiggedMessages[jiggedMessages.length - 1];
837
+ if ((last == null ? void 0 : last.role) === "model" && !hasFunctionPart(last.parts)) {
768
838
  jiggedMessages.push({ role: "user", parts: [{ text: "..." }] });
769
839
  }
770
840
  return jiggedMessages;
@@ -773,6 +843,7 @@ async function prepareGoogleAIPayload(_identifier, payload) {
773
843
  const preparedPayload = {
774
844
  model: payload.model,
775
845
  messages: [],
846
+ requestTimeoutMs: payload.requestTimeoutMs,
776
847
  tools: payload.functions ? {
777
848
  functionDeclarations: payload.functions.map((fn) => ({
778
849
  name: fn.name,
@@ -783,11 +854,34 @@ async function prepareGoogleAIPayload(_identifier, payload) {
783
854
  }))
784
855
  } : void 0
785
856
  };
857
+ const toolNameById = /* @__PURE__ */ new Map();
858
+ for (const m of payload.messages) {
859
+ for (const fc of m.functionCalls || []) {
860
+ if (fc.id)
861
+ toolNameById.set(fc.id, fc.name);
862
+ }
863
+ }
786
864
  for (const message of payload.messages) {
787
865
  if (message.role === "system") {
788
866
  preparedPayload.systemInstruction = message.content;
789
867
  continue;
790
868
  }
869
+ if (message.role === "tool") {
870
+ preparedPayload.messages.push({
871
+ role: "user",
872
+ parts: (message.toolResults || []).map((tr) => {
873
+ var _a, _b;
874
+ return {
875
+ functionResponse: {
876
+ id: tr.toolCallId,
877
+ name: (_b = (_a = tr.name) != null ? _a : toolNameById.get(tr.toolCallId)) != null ? _b : "",
878
+ response: { output: tr.content }
879
+ }
880
+ };
881
+ })
882
+ });
883
+ continue;
884
+ }
791
885
  const parts = [];
792
886
  if (message.content) {
793
887
  parts.push({ text: message.content });
@@ -816,6 +910,17 @@ async function prepareGoogleAIPayload(_identifier, payload) {
816
910
  });
817
911
  }
818
912
  }
913
+ for (const fc of message.functionCalls || []) {
914
+ parts.push({
915
+ functionCall: {
916
+ id: fc.id,
917
+ name: fc.name,
918
+ args: fc.arguments
919
+ },
920
+ // Gemini requires its thoughtSignature echoed back on the call part.
921
+ ...fc.thoughtSignature ? { thoughtSignature: fc.thoughtSignature } : {}
922
+ });
923
+ }
819
924
  preparedPayload.messages.push({
820
925
  role: message.role === "assistant" ? "model" : message.role,
821
926
  parts
@@ -824,39 +929,73 @@ async function prepareGoogleAIPayload(_identifier, payload) {
824
929
  return preparedPayload;
825
930
  }
826
931
  async function callGoogleAI(id, payload) {
827
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
828
- const googleMessages = jigGoogleMessages(payload.messages);
829
- const history = googleMessages.slice(0, -1);
830
- const lastMessage = googleMessages.slice(-1)[0];
831
- const genAI = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
832
- const chat = genAI.chats.create({
833
- model: payload.model,
834
- history,
835
- config: {
836
- responseModalities: ["Text"],
837
- tools: payload.tools ? [payload.tools] : void 0,
838
- systemInstruction: payload.systemInstruction
839
- }
840
- });
841
- const response = await chat.sendMessage({ message: lastMessage.parts });
932
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
933
+ const contents = jigGoogleMessages(payload.messages);
934
+ const requestBody = {
935
+ contents,
936
+ generationConfig: { responseModalities: ["TEXT"] }
937
+ };
938
+ if (payload.tools)
939
+ requestBody.tools = [payload.tools];
940
+ if (payload.systemInstruction) {
941
+ requestBody.systemInstruction = {
942
+ parts: [{ text: payload.systemInstruction }]
943
+ };
944
+ }
945
+ let response;
946
+ try {
947
+ const httpResponse = await axios.post(
948
+ `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
949
+ requestBody,
950
+ {
951
+ headers: {
952
+ "content-type": "application/json",
953
+ "x-goog-api-key": process.env.GEMINI_API_KEY
954
+ },
955
+ // Per-attempt timeout. Defaults to 60s; callers pass a larger value via
956
+ // payload.requestTimeoutMs for slow, large generations (e.g. single-file
957
+ // app codegen) so a long-but-valid response isn't cut short.
958
+ timeout: (_a = payload.requestTimeoutMs) != null ? _a : 6e4
959
+ }
960
+ );
961
+ response = httpResponse.data;
962
+ } catch (err) {
963
+ const apiError = (_c = (_b = err == null ? void 0 : err.response) == null ? void 0 : _b.data) == null ? void 0 : _c.error;
964
+ const wrapped = new Error(
965
+ (apiError == null ? void 0 : apiError.message) || (err == null ? void 0 : err.message) || "Google AI API request failed"
966
+ );
967
+ wrapped.status = (_e = apiError == null ? void 0 : apiError.status) != null ? _e : (_d = err == null ? void 0 : err.response) == null ? void 0 : _d.status;
968
+ wrapped.code = apiError == null ? void 0 : apiError.code;
969
+ wrapped.details = apiError == null ? void 0 : apiError.details;
970
+ wrapped.promptFeedback = (_g = (_f = err == null ? void 0 : err.response) == null ? void 0 : _f.data) == null ? void 0 : _g.promptFeedback;
971
+ throw wrapped;
972
+ }
842
973
  let text = "";
843
974
  const files = [];
844
- for (const part of ((_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) || []) {
975
+ const reasoningParts = [];
976
+ const functionCalls = [];
977
+ for (const part of ((_j = (_i = (_h = response.candidates) == null ? void 0 : _h[0]) == null ? void 0 : _i.content) == null ? void 0 : _j.parts) || []) {
978
+ if (part.thought) {
979
+ reasoningParts.push(part);
980
+ continue;
981
+ }
982
+ if (part.functionCall) {
983
+ functionCalls.push({
984
+ id: (_k = part.functionCall.id) != null ? _k : `call_${functionCalls.length}`,
985
+ name: (_l = part.functionCall.name) != null ? _l : "",
986
+ arguments: (_m = part.functionCall.args) != null ? _m : {},
987
+ thoughtSignature: part.thoughtSignature
988
+ });
989
+ continue;
990
+ }
845
991
  if (part.text)
846
992
  text += part.text;
847
- if ((_d = part.inlineData) == null ? void 0 : _d.data) {
993
+ if ((_n = part.inlineData) == null ? void 0 : _n.data) {
848
994
  files.push({ mimeType: "image/png", data: part.inlineData.data });
849
995
  }
850
996
  }
851
- const functionCalls = (_e = response.functionCalls) == null ? void 0 : _e.map((fc) => {
852
- var _a2, _b2;
853
- return {
854
- name: (_a2 = fc.name) != null ? _a2 : "",
855
- arguments: (_b2 = fc.args) != null ? _b2 : {}
856
- };
857
- });
858
- if (!text && !(functionCalls == null ? void 0 : functionCalls.length) && !files.length) {
859
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
997
+ if (!text && !functionCalls.length && !files.length) {
998
+ const candidate = (_o = response.candidates) == null ? void 0 : _o[0];
860
999
  const finishReason = candidate == null ? void 0 : candidate.finishReason;
861
1000
  logger_default.error(id, "Missing text & functions in Google AI API response:", {
862
1001
  finishReason,
@@ -887,13 +1026,14 @@ async function callGoogleAI(id, payload) {
887
1026
  role: "assistant",
888
1027
  content: text || null,
889
1028
  files,
890
- function_call: (functionCalls == null ? void 0 : functionCalls[0]) || null,
891
- function_calls: functionCalls || [],
1029
+ function_call: functionCalls[0] || null,
1030
+ function_calls: functionCalls,
1031
+ reasoningDetails: reasoningParts.length ? reasoningParts : void 0,
892
1032
  usage: response.usageMetadata ? {
893
- prompt_tokens: (_g = response.usageMetadata.promptTokenCount) != null ? _g : 0,
894
- completion_tokens: (_h = response.usageMetadata.candidatesTokenCount) != null ? _h : 0,
895
- total_tokens: (_i = response.usageMetadata.totalTokenCount) != null ? _i : 0,
896
- cached_tokens: (_j = response.usageMetadata.cachedContentTokenCount) != null ? _j : 0
1033
+ prompt_tokens: (_p = response.usageMetadata.promptTokenCount) != null ? _p : 0,
1034
+ completion_tokens: (_q = response.usageMetadata.candidatesTokenCount) != null ? _q : 0,
1035
+ total_tokens: (_r = response.usageMetadata.totalTokenCount) != null ? _r : 0,
1036
+ cached_tokens: (_s = response.usageMetadata.cachedContentTokenCount) != null ? _s : 0
897
1037
  } : null
898
1038
  };
899
1039
  }
@@ -989,14 +1129,52 @@ async function callGoogleAIWithRetries(id, payload, retries = 5) {
989
1129
  function normalizeMessageContent(content) {
990
1130
  return Array.isArray(content) ? content.map((c) => c.type === "text" ? c.text : `[${c.type}]`).join("\n") : content;
991
1131
  }
1132
+ function prepareOpenAICompatMessages(messages) {
1133
+ var _a;
1134
+ const out = [];
1135
+ for (const message of messages) {
1136
+ if (message.role === "tool") {
1137
+ for (const tr of message.toolResults || []) {
1138
+ out.push({
1139
+ role: "tool",
1140
+ tool_call_id: tr.toolCallId,
1141
+ content: tr.content
1142
+ });
1143
+ }
1144
+ continue;
1145
+ }
1146
+ const outMessage = {
1147
+ role: message.role,
1148
+ content: normalizeMessageContent(message.content)
1149
+ };
1150
+ if ((_a = message.functionCalls) == null ? void 0 : _a.length) {
1151
+ outMessage.tool_calls = message.functionCalls.map((fc, i) => {
1152
+ var _a2;
1153
+ return {
1154
+ id: (_a2 = fc.id) != null ? _a2 : `call_${i}`,
1155
+ type: "function",
1156
+ function: {
1157
+ name: fc.name,
1158
+ arguments: JSON.stringify(fc.arguments)
1159
+ }
1160
+ };
1161
+ });
1162
+ if (!message.content)
1163
+ outMessage.content = null;
1164
+ }
1165
+ if (message.reasoning)
1166
+ outMessage.reasoning = message.reasoning;
1167
+ if (message.reasoningDetails)
1168
+ outMessage.reasoning_details = message.reasoningDetails;
1169
+ out.push(outMessage);
1170
+ }
1171
+ return out;
1172
+ }
992
1173
  function prepareGroqPayload(payload) {
993
1174
  var _a;
994
1175
  return {
995
1176
  model: payload.model,
996
- messages: payload.messages.map((message) => ({
997
- role: message.role,
998
- content: normalizeMessageContent(message.content)
999
- })),
1177
+ messages: prepareOpenAICompatMessages(payload.messages),
1000
1178
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
1001
1179
  type: "function",
1002
1180
  function: fn
@@ -1006,7 +1184,7 @@ function prepareGroqPayload(payload) {
1006
1184
  };
1007
1185
  }
1008
1186
  async function callGroq(id, payload) {
1009
- var _a, _b, _c, _d;
1187
+ var _a, _b, _c, _d, _e, _f, _g;
1010
1188
  const response = await axios.post(
1011
1189
  "https://api.groq.com/openai/v1/chat/completions",
1012
1190
  payload,
@@ -1017,15 +1195,21 @@ async function callGroq(id, payload) {
1017
1195
  }
1018
1196
  }
1019
1197
  );
1020
- const answer = (_a = response.data.choices[0]) == null ? void 0 : _a.message;
1198
+ if (response.data.error) {
1199
+ logger_default.error(id, "Groq error:", response.data.error);
1200
+ throw new Error(`Groq error: ${response.data.error.message}`);
1201
+ }
1202
+ const answer = (_b = (_a = response.data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1021
1203
  if (!answer) {
1022
1204
  logger_default.error(id, "Missing answer in Groq API response:", response.data);
1023
1205
  throw new Error("Missing answer in Groq API");
1024
1206
  }
1025
1207
  const functionCalls = [];
1026
- if ((_b = answer.tool_calls) == null ? void 0 : _b.length) {
1027
- for (const tc of answer.tool_calls) {
1208
+ if ((_c = answer.tool_calls) == null ? void 0 : _c.length) {
1209
+ for (let i = 0; i < answer.tool_calls.length; i++) {
1210
+ const tc = answer.tool_calls[i];
1028
1211
  functionCalls.push({
1212
+ id: (_d = tc.id) != null ? _d : `call_${i}`,
1029
1213
  name: tc.function.name,
1030
1214
  arguments: JSON.parse(tc.function.arguments)
1031
1215
  });
@@ -1047,11 +1231,12 @@ async function callGroq(id, payload) {
1047
1231
  function_call: functionCalls[0] || null,
1048
1232
  function_calls: functionCalls,
1049
1233
  files: [],
1234
+ reasoning: (_e = answer.reasoning) != null ? _e : void 0,
1050
1235
  usage: response.data.usage ? {
1051
1236
  prompt_tokens: response.data.usage.prompt_tokens,
1052
1237
  completion_tokens: response.data.usage.completion_tokens,
1053
1238
  total_tokens: response.data.usage.total_tokens,
1054
- cached_tokens: (_d = (_c = response.data.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0
1239
+ cached_tokens: (_g = (_f = response.data.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : 0
1055
1240
  } : null
1056
1241
  };
1057
1242
  }
@@ -1062,10 +1247,7 @@ function prepareOpenRouterPayload(payload) {
1062
1247
  var _a;
1063
1248
  return {
1064
1249
  model: payload.model,
1065
- messages: payload.messages.map((message) => ({
1066
- role: message.role,
1067
- content: normalizeMessageContent(message.content)
1068
- })),
1250
+ messages: prepareOpenAICompatMessages(payload.messages),
1069
1251
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
1070
1252
  type: "function",
1071
1253
  function: fn
@@ -1076,7 +1258,7 @@ function prepareOpenRouterPayload(payload) {
1076
1258
  };
1077
1259
  }
1078
1260
  async function callOpenRouter(id, payload) {
1079
- var _a, _b, _c, _d;
1261
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1080
1262
  const response = await axios.post(
1081
1263
  "https://openrouter.ai/api/v1/chat/completions",
1082
1264
  payload,
@@ -1087,15 +1269,21 @@ async function callOpenRouter(id, payload) {
1087
1269
  }
1088
1270
  }
1089
1271
  );
1090
- const answer = (_a = response.data.choices[0]) == null ? void 0 : _a.message;
1272
+ if (response.data.error) {
1273
+ logger_default.error(id, "OpenRouter error:", response.data.error);
1274
+ throw new Error(`OpenRouter error: ${response.data.error.message}`);
1275
+ }
1276
+ const answer = (_b = (_a = response.data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1091
1277
  if (!answer) {
1092
1278
  logger_default.error(id, "Missing answer in OpenRouter API response:", response.data);
1093
1279
  throw new Error("Missing answer in OpenRouter API");
1094
1280
  }
1095
1281
  const functionCalls = [];
1096
- if ((_b = answer.tool_calls) == null ? void 0 : _b.length) {
1097
- for (const tc of answer.tool_calls) {
1282
+ if ((_c = answer.tool_calls) == null ? void 0 : _c.length) {
1283
+ for (let i = 0; i < answer.tool_calls.length; i++) {
1284
+ const tc = answer.tool_calls[i];
1098
1285
  functionCalls.push({
1286
+ id: (_d = tc.id) != null ? _d : `call_${i}`,
1099
1287
  name: tc.function.name,
1100
1288
  arguments: JSON.parse(tc.function.arguments)
1101
1289
  });
@@ -1117,11 +1305,13 @@ async function callOpenRouter(id, payload) {
1117
1305
  function_call: functionCalls[0] || null,
1118
1306
  function_calls: functionCalls,
1119
1307
  files: [],
1308
+ reasoning: (_e = answer.reasoning) != null ? _e : void 0,
1309
+ reasoningDetails: (_f = answer.reasoning_details) != null ? _f : void 0,
1120
1310
  usage: response.data.usage ? {
1121
1311
  prompt_tokens: response.data.usage.prompt_tokens,
1122
1312
  completion_tokens: response.data.usage.completion_tokens,
1123
1313
  total_tokens: response.data.usage.total_tokens,
1124
- cached_tokens: (_d = (_c = response.data.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0
1314
+ cached_tokens: (_h = (_g = response.data.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
1125
1315
  } : null
1126
1316
  };
1127
1317
  }