190proof 1.0.95 → 1.0.97

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;
@@ -783,11 +853,34 @@ async function prepareGoogleAIPayload(_identifier, payload) {
783
853
  }))
784
854
  } : void 0
785
855
  };
856
+ const toolNameById = /* @__PURE__ */ new Map();
857
+ for (const m of payload.messages) {
858
+ for (const fc of m.functionCalls || []) {
859
+ if (fc.id)
860
+ toolNameById.set(fc.id, fc.name);
861
+ }
862
+ }
786
863
  for (const message of payload.messages) {
787
864
  if (message.role === "system") {
788
865
  preparedPayload.systemInstruction = message.content;
789
866
  continue;
790
867
  }
868
+ if (message.role === "tool") {
869
+ preparedPayload.messages.push({
870
+ role: "user",
871
+ parts: (message.toolResults || []).map((tr) => {
872
+ var _a, _b;
873
+ return {
874
+ functionResponse: {
875
+ id: tr.toolCallId,
876
+ name: (_b = (_a = tr.name) != null ? _a : toolNameById.get(tr.toolCallId)) != null ? _b : "",
877
+ response: { output: tr.content }
878
+ }
879
+ };
880
+ })
881
+ });
882
+ continue;
883
+ }
791
884
  const parts = [];
792
885
  if (message.content) {
793
886
  parts.push({ text: message.content });
@@ -816,6 +909,17 @@ async function prepareGoogleAIPayload(_identifier, payload) {
816
909
  });
817
910
  }
818
911
  }
912
+ for (const fc of message.functionCalls || []) {
913
+ parts.push({
914
+ functionCall: {
915
+ id: fc.id,
916
+ name: fc.name,
917
+ args: fc.arguments
918
+ },
919
+ // Gemini requires its thoughtSignature echoed back on the call part.
920
+ ...fc.thoughtSignature ? { thoughtSignature: fc.thoughtSignature } : {}
921
+ });
922
+ }
819
923
  preparedPayload.messages.push({
820
924
  role: message.role === "assistant" ? "model" : message.role,
821
925
  parts
@@ -824,39 +928,70 @@ async function prepareGoogleAIPayload(_identifier, payload) {
824
928
  return preparedPayload;
825
929
  }
826
930
  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 });
931
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
932
+ const contents = jigGoogleMessages(payload.messages);
933
+ const requestBody = {
934
+ contents,
935
+ generationConfig: { responseModalities: ["TEXT"] }
936
+ };
937
+ if (payload.tools)
938
+ requestBody.tools = [payload.tools];
939
+ if (payload.systemInstruction) {
940
+ requestBody.systemInstruction = {
941
+ parts: [{ text: payload.systemInstruction }]
942
+ };
943
+ }
944
+ let response;
945
+ try {
946
+ const httpResponse = await axios.post(
947
+ `https://generativelanguage.googleapis.com/v1beta/models/${payload.model}:generateContent`,
948
+ requestBody,
949
+ {
950
+ headers: {
951
+ "content-type": "application/json",
952
+ "x-goog-api-key": process.env.GEMINI_API_KEY
953
+ },
954
+ timeout: 6e4
955
+ }
956
+ );
957
+ response = httpResponse.data;
958
+ } catch (err) {
959
+ const apiError = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) == null ? void 0 : _b.error;
960
+ const wrapped = new Error(
961
+ (apiError == null ? void 0 : apiError.message) || (err == null ? void 0 : err.message) || "Google AI API request failed"
962
+ );
963
+ wrapped.status = (_d = apiError == null ? void 0 : apiError.status) != null ? _d : (_c = err == null ? void 0 : err.response) == null ? void 0 : _c.status;
964
+ wrapped.code = apiError == null ? void 0 : apiError.code;
965
+ wrapped.details = apiError == null ? void 0 : apiError.details;
966
+ wrapped.promptFeedback = (_f = (_e = err == null ? void 0 : err.response) == null ? void 0 : _e.data) == null ? void 0 : _f.promptFeedback;
967
+ throw wrapped;
968
+ }
842
969
  let text = "";
843
970
  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) || []) {
971
+ const reasoningParts = [];
972
+ const functionCalls = [];
973
+ for (const part of ((_i = (_h = (_g = response.candidates) == null ? void 0 : _g[0]) == null ? void 0 : _h.content) == null ? void 0 : _i.parts) || []) {
974
+ if (part.thought) {
975
+ reasoningParts.push(part);
976
+ continue;
977
+ }
978
+ if (part.functionCall) {
979
+ functionCalls.push({
980
+ id: (_j = part.functionCall.id) != null ? _j : `call_${functionCalls.length}`,
981
+ name: (_k = part.functionCall.name) != null ? _k : "",
982
+ arguments: (_l = part.functionCall.args) != null ? _l : {},
983
+ thoughtSignature: part.thoughtSignature
984
+ });
985
+ continue;
986
+ }
845
987
  if (part.text)
846
988
  text += part.text;
847
- if ((_d = part.inlineData) == null ? void 0 : _d.data) {
989
+ if ((_m = part.inlineData) == null ? void 0 : _m.data) {
848
990
  files.push({ mimeType: "image/png", data: part.inlineData.data });
849
991
  }
850
992
  }
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];
993
+ if (!text && !functionCalls.length && !files.length) {
994
+ const candidate = (_n = response.candidates) == null ? void 0 : _n[0];
860
995
  const finishReason = candidate == null ? void 0 : candidate.finishReason;
861
996
  logger_default.error(id, "Missing text & functions in Google AI API response:", {
862
997
  finishReason,
@@ -887,13 +1022,14 @@ async function callGoogleAI(id, payload) {
887
1022
  role: "assistant",
888
1023
  content: text || null,
889
1024
  files,
890
- function_call: (functionCalls == null ? void 0 : functionCalls[0]) || null,
891
- function_calls: functionCalls || [],
1025
+ function_call: functionCalls[0] || null,
1026
+ function_calls: functionCalls,
1027
+ reasoningDetails: reasoningParts.length ? reasoningParts : void 0,
892
1028
  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
1029
+ prompt_tokens: (_o = response.usageMetadata.promptTokenCount) != null ? _o : 0,
1030
+ completion_tokens: (_p = response.usageMetadata.candidatesTokenCount) != null ? _p : 0,
1031
+ total_tokens: (_q = response.usageMetadata.totalTokenCount) != null ? _q : 0,
1032
+ cached_tokens: (_r = response.usageMetadata.cachedContentTokenCount) != null ? _r : 0
897
1033
  } : null
898
1034
  };
899
1035
  }
@@ -989,14 +1125,52 @@ async function callGoogleAIWithRetries(id, payload, retries = 5) {
989
1125
  function normalizeMessageContent(content) {
990
1126
  return Array.isArray(content) ? content.map((c) => c.type === "text" ? c.text : `[${c.type}]`).join("\n") : content;
991
1127
  }
1128
+ function prepareOpenAICompatMessages(messages) {
1129
+ var _a;
1130
+ const out = [];
1131
+ for (const message of messages) {
1132
+ if (message.role === "tool") {
1133
+ for (const tr of message.toolResults || []) {
1134
+ out.push({
1135
+ role: "tool",
1136
+ tool_call_id: tr.toolCallId,
1137
+ content: tr.content
1138
+ });
1139
+ }
1140
+ continue;
1141
+ }
1142
+ const outMessage = {
1143
+ role: message.role,
1144
+ content: normalizeMessageContent(message.content)
1145
+ };
1146
+ if ((_a = message.functionCalls) == null ? void 0 : _a.length) {
1147
+ outMessage.tool_calls = message.functionCalls.map((fc, i) => {
1148
+ var _a2;
1149
+ return {
1150
+ id: (_a2 = fc.id) != null ? _a2 : `call_${i}`,
1151
+ type: "function",
1152
+ function: {
1153
+ name: fc.name,
1154
+ arguments: JSON.stringify(fc.arguments)
1155
+ }
1156
+ };
1157
+ });
1158
+ if (!message.content)
1159
+ outMessage.content = null;
1160
+ }
1161
+ if (message.reasoning)
1162
+ outMessage.reasoning = message.reasoning;
1163
+ if (message.reasoningDetails)
1164
+ outMessage.reasoning_details = message.reasoningDetails;
1165
+ out.push(outMessage);
1166
+ }
1167
+ return out;
1168
+ }
992
1169
  function prepareGroqPayload(payload) {
993
1170
  var _a;
994
1171
  return {
995
1172
  model: payload.model,
996
- messages: payload.messages.map((message) => ({
997
- role: message.role,
998
- content: normalizeMessageContent(message.content)
999
- })),
1173
+ messages: prepareOpenAICompatMessages(payload.messages),
1000
1174
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
1001
1175
  type: "function",
1002
1176
  function: fn
@@ -1006,7 +1180,7 @@ function prepareGroqPayload(payload) {
1006
1180
  };
1007
1181
  }
1008
1182
  async function callGroq(id, payload) {
1009
- var _a, _b, _c, _d;
1183
+ var _a, _b, _c, _d, _e, _f, _g;
1010
1184
  const response = await axios.post(
1011
1185
  "https://api.groq.com/openai/v1/chat/completions",
1012
1186
  payload,
@@ -1017,15 +1191,21 @@ async function callGroq(id, payload) {
1017
1191
  }
1018
1192
  }
1019
1193
  );
1020
- const answer = (_a = response.data.choices[0]) == null ? void 0 : _a.message;
1194
+ if (response.data.error) {
1195
+ logger_default.error(id, "Groq error:", response.data.error);
1196
+ throw new Error(`Groq error: ${response.data.error.message}`);
1197
+ }
1198
+ const answer = (_b = (_a = response.data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1021
1199
  if (!answer) {
1022
1200
  logger_default.error(id, "Missing answer in Groq API response:", response.data);
1023
1201
  throw new Error("Missing answer in Groq API");
1024
1202
  }
1025
1203
  const functionCalls = [];
1026
- if ((_b = answer.tool_calls) == null ? void 0 : _b.length) {
1027
- for (const tc of answer.tool_calls) {
1204
+ if ((_c = answer.tool_calls) == null ? void 0 : _c.length) {
1205
+ for (let i = 0; i < answer.tool_calls.length; i++) {
1206
+ const tc = answer.tool_calls[i];
1028
1207
  functionCalls.push({
1208
+ id: (_d = tc.id) != null ? _d : `call_${i}`,
1029
1209
  name: tc.function.name,
1030
1210
  arguments: JSON.parse(tc.function.arguments)
1031
1211
  });
@@ -1047,11 +1227,12 @@ async function callGroq(id, payload) {
1047
1227
  function_call: functionCalls[0] || null,
1048
1228
  function_calls: functionCalls,
1049
1229
  files: [],
1230
+ reasoning: (_e = answer.reasoning) != null ? _e : void 0,
1050
1231
  usage: response.data.usage ? {
1051
1232
  prompt_tokens: response.data.usage.prompt_tokens,
1052
1233
  completion_tokens: response.data.usage.completion_tokens,
1053
1234
  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
1235
+ cached_tokens: (_g = (_f = response.data.usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : 0
1055
1236
  } : null
1056
1237
  };
1057
1238
  }
@@ -1062,20 +1243,18 @@ function prepareOpenRouterPayload(payload) {
1062
1243
  var _a;
1063
1244
  return {
1064
1245
  model: payload.model,
1065
- messages: payload.messages.map((message) => ({
1066
- role: message.role,
1067
- content: normalizeMessageContent(message.content)
1068
- })),
1246
+ messages: prepareOpenAICompatMessages(payload.messages),
1069
1247
  tools: (_a = payload.functions) == null ? void 0 : _a.map((fn) => ({
1070
1248
  type: "function",
1071
1249
  function: fn
1072
1250
  })),
1073
1251
  tool_choice: payload.function_call ? typeof payload.function_call === "string" ? payload.function_call : { type: "function", function: payload.function_call } : void 0,
1074
- temperature: payload.temperature
1252
+ temperature: payload.temperature,
1253
+ provider: payload.provider
1075
1254
  };
1076
1255
  }
1077
1256
  async function callOpenRouter(id, payload) {
1078
- var _a, _b, _c, _d;
1257
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1079
1258
  const response = await axios.post(
1080
1259
  "https://openrouter.ai/api/v1/chat/completions",
1081
1260
  payload,
@@ -1086,15 +1265,21 @@ async function callOpenRouter(id, payload) {
1086
1265
  }
1087
1266
  }
1088
1267
  );
1089
- const answer = (_a = response.data.choices[0]) == null ? void 0 : _a.message;
1268
+ if (response.data.error) {
1269
+ logger_default.error(id, "OpenRouter error:", response.data.error);
1270
+ throw new Error(`OpenRouter error: ${response.data.error.message}`);
1271
+ }
1272
+ const answer = (_b = (_a = response.data.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.message;
1090
1273
  if (!answer) {
1091
1274
  logger_default.error(id, "Missing answer in OpenRouter API response:", response.data);
1092
1275
  throw new Error("Missing answer in OpenRouter API");
1093
1276
  }
1094
1277
  const functionCalls = [];
1095
- if ((_b = answer.tool_calls) == null ? void 0 : _b.length) {
1096
- for (const tc of answer.tool_calls) {
1278
+ if ((_c = answer.tool_calls) == null ? void 0 : _c.length) {
1279
+ for (let i = 0; i < answer.tool_calls.length; i++) {
1280
+ const tc = answer.tool_calls[i];
1097
1281
  functionCalls.push({
1282
+ id: (_d = tc.id) != null ? _d : `call_${i}`,
1098
1283
  name: tc.function.name,
1099
1284
  arguments: JSON.parse(tc.function.arguments)
1100
1285
  });
@@ -1116,11 +1301,13 @@ async function callOpenRouter(id, payload) {
1116
1301
  function_call: functionCalls[0] || null,
1117
1302
  function_calls: functionCalls,
1118
1303
  files: [],
1304
+ reasoning: (_e = answer.reasoning) != null ? _e : void 0,
1305
+ reasoningDetails: (_f = answer.reasoning_details) != null ? _f : void 0,
1119
1306
  usage: response.data.usage ? {
1120
1307
  prompt_tokens: response.data.usage.prompt_tokens,
1121
1308
  completion_tokens: response.data.usage.completion_tokens,
1122
1309
  total_tokens: response.data.usage.total_tokens,
1123
- cached_tokens: (_d = (_c = response.data.usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0
1310
+ cached_tokens: (_h = (_g = response.data.usage.prompt_tokens_details) == null ? void 0 : _g.cached_tokens) != null ? _h : 0
1124
1311
  } : null
1125
1312
  };
1126
1313
  }