@threaded/ai 1.0.11 → 1.0.13

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
@@ -760,42 +760,52 @@ var callGoogle = async (config, ctx) => {
760
760
  for (let i = 0; i < ctx.history.length; i++) {
761
761
  const msg2 = ctx.history[i];
762
762
  if (msg2.role === "assistant") {
763
- const parts = [];
763
+ const parts2 = [];
764
764
  if (msg2.content) {
765
- parts.push({ text: msg2.content });
765
+ parts2.push({ text: msg2.content });
766
766
  }
767
767
  if (msg2.tool_calls?.length) {
768
768
  for (const tc of msg2.tool_calls) {
769
769
  toolCallMap.set(tc.id, tc.function.name);
770
- parts.push({
770
+ const part = {
771
771
  functionCall: {
772
772
  name: tc.function.name,
773
773
  args: JSON.parse(tc.function.arguments)
774
774
  }
775
- });
775
+ };
776
+ if (tc.thoughtSignature) {
777
+ part.thoughtSignature = tc.thoughtSignature;
778
+ }
779
+ parts2.push(part);
776
780
  }
777
781
  }
778
- if (parts.length > 0) {
779
- contents.push({ role: "model", parts });
782
+ if (parts2.length > 0) {
783
+ contents.push({ role: "model", parts: parts2 });
780
784
  }
781
785
  } else if (msg2.role === "tool") {
782
- const functionName = toolCallMap.get(msg2.tool_call_id);
783
- if (functionName) {
784
- let responseData;
785
- try {
786
- responseData = JSON.parse(msg2.content);
787
- } catch {
788
- responseData = { result: msg2.content };
789
- }
790
- contents.push({
791
- role: "user",
792
- parts: [{
786
+ const responseParts = [];
787
+ while (i < ctx.history.length && ctx.history[i].role === "tool") {
788
+ const toolMsg = ctx.history[i];
789
+ const functionName = toolCallMap.get(toolMsg.tool_call_id);
790
+ if (functionName) {
791
+ let responseData;
792
+ try {
793
+ responseData = JSON.parse(toolMsg.content);
794
+ } catch {
795
+ responseData = { result: toolMsg.content };
796
+ }
797
+ responseParts.push({
793
798
  functionResponse: {
794
799
  name: functionName,
795
800
  response: responseData
796
801
  }
797
- }]
798
- });
802
+ });
803
+ }
804
+ i++;
805
+ }
806
+ i--;
807
+ if (responseParts.length > 0) {
808
+ contents.push({ role: "user", parts: responseParts });
799
809
  }
800
810
  } else if (msg2.role === "user") {
801
811
  contents.push({
@@ -839,22 +849,33 @@ var callGoogle = async (config, ctx) => {
839
849
  }
840
850
  const data = await response.json();
841
851
  const candidate = data.candidates[0];
842
- const part = candidate.content.parts[0];
852
+ const parts = candidate.content.parts || [];
843
853
  const msg = {
844
854
  role: "assistant",
845
- content: part.text || ""
855
+ content: ""
846
856
  };
847
- if (part.functionCall) {
848
- msg.tool_calls = [
849
- {
857
+ const toolCalls = [];
858
+ for (const part of parts) {
859
+ if (part.text) {
860
+ msg.content += part.text;
861
+ }
862
+ if (part.functionCall) {
863
+ const tc = {
850
864
  id: Math.random().toString(36).substring(2, 9),
851
865
  type: "function",
852
866
  function: {
853
867
  name: part.functionCall.name,
854
868
  arguments: JSON.stringify(part.functionCall.args)
855
869
  }
870
+ };
871
+ if (part.thoughtSignature) {
872
+ tc.thoughtSignature = part.thoughtSignature;
856
873
  }
857
- ];
874
+ toolCalls.push(tc);
875
+ }
876
+ }
877
+ if (toolCalls.length > 0) {
878
+ msg.tool_calls = toolCalls;
858
879
  }
859
880
  return {
860
881
  ...ctx,
@@ -885,22 +906,28 @@ var handleGoogleStream = async (response, ctx) => {
885
906
  try {
886
907
  const parsed = JSON.parse(data);
887
908
  const candidate = parsed.candidates?.[0];
888
- const part = candidate?.content?.parts?.[0];
889
- if (part?.text) {
890
- fullContent += part.text;
891
- if (ctx.stream) {
892
- ctx.stream({ type: "content", content: part.text });
909
+ const parts = candidate?.content?.parts || [];
910
+ for (const part of parts) {
911
+ if (part?.text) {
912
+ fullContent += part.text;
913
+ if (ctx.stream) {
914
+ ctx.stream({ type: "content", content: part.text });
915
+ }
893
916
  }
894
- }
895
- if (part?.functionCall) {
896
- toolCalls.push({
897
- id: Math.random().toString(36).substring(2, 9),
898
- type: "function",
899
- function: {
900
- name: part.functionCall.name,
901
- arguments: JSON.stringify(part.functionCall.args)
917
+ if (part?.functionCall) {
918
+ const tc = {
919
+ id: Math.random().toString(36).substring(2, 9),
920
+ type: "function",
921
+ function: {
922
+ name: part.functionCall.name,
923
+ arguments: JSON.stringify(part.functionCall.args)
924
+ }
925
+ };
926
+ if (part.thoughtSignature) {
927
+ tc.thoughtSignature = part.thoughtSignature;
902
928
  }
903
- });
929
+ toolCalls.push(tc);
930
+ }
904
931
  }
905
932
  } catch (e) {
906
933
  }