@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.js CHANGED
@@ -691,42 +691,52 @@ var callGoogle = async (config, ctx) => {
691
691
  for (let i = 0; i < ctx.history.length; i++) {
692
692
  const msg2 = ctx.history[i];
693
693
  if (msg2.role === "assistant") {
694
- const parts = [];
694
+ const parts2 = [];
695
695
  if (msg2.content) {
696
- parts.push({ text: msg2.content });
696
+ parts2.push({ text: msg2.content });
697
697
  }
698
698
  if (msg2.tool_calls?.length) {
699
699
  for (const tc of msg2.tool_calls) {
700
700
  toolCallMap.set(tc.id, tc.function.name);
701
- parts.push({
701
+ const part = {
702
702
  functionCall: {
703
703
  name: tc.function.name,
704
704
  args: JSON.parse(tc.function.arguments)
705
705
  }
706
- });
706
+ };
707
+ if (tc.thoughtSignature) {
708
+ part.thoughtSignature = tc.thoughtSignature;
709
+ }
710
+ parts2.push(part);
707
711
  }
708
712
  }
709
- if (parts.length > 0) {
710
- contents.push({ role: "model", parts });
713
+ if (parts2.length > 0) {
714
+ contents.push({ role: "model", parts: parts2 });
711
715
  }
712
716
  } else if (msg2.role === "tool") {
713
- const functionName = toolCallMap.get(msg2.tool_call_id);
714
- if (functionName) {
715
- let responseData;
716
- try {
717
- responseData = JSON.parse(msg2.content);
718
- } catch {
719
- responseData = { result: msg2.content };
720
- }
721
- contents.push({
722
- role: "user",
723
- parts: [{
717
+ const responseParts = [];
718
+ while (i < ctx.history.length && ctx.history[i].role === "tool") {
719
+ const toolMsg = ctx.history[i];
720
+ const functionName = toolCallMap.get(toolMsg.tool_call_id);
721
+ if (functionName) {
722
+ let responseData;
723
+ try {
724
+ responseData = JSON.parse(toolMsg.content);
725
+ } catch {
726
+ responseData = { result: toolMsg.content };
727
+ }
728
+ responseParts.push({
724
729
  functionResponse: {
725
730
  name: functionName,
726
731
  response: responseData
727
732
  }
728
- }]
729
- });
733
+ });
734
+ }
735
+ i++;
736
+ }
737
+ i--;
738
+ if (responseParts.length > 0) {
739
+ contents.push({ role: "user", parts: responseParts });
730
740
  }
731
741
  } else if (msg2.role === "user") {
732
742
  contents.push({
@@ -770,22 +780,33 @@ var callGoogle = async (config, ctx) => {
770
780
  }
771
781
  const data = await response.json();
772
782
  const candidate = data.candidates[0];
773
- const part = candidate.content.parts[0];
783
+ const parts = candidate.content.parts || [];
774
784
  const msg = {
775
785
  role: "assistant",
776
- content: part.text || ""
786
+ content: ""
777
787
  };
778
- if (part.functionCall) {
779
- msg.tool_calls = [
780
- {
788
+ const toolCalls = [];
789
+ for (const part of parts) {
790
+ if (part.text) {
791
+ msg.content += part.text;
792
+ }
793
+ if (part.functionCall) {
794
+ const tc = {
781
795
  id: Math.random().toString(36).substring(2, 9),
782
796
  type: "function",
783
797
  function: {
784
798
  name: part.functionCall.name,
785
799
  arguments: JSON.stringify(part.functionCall.args)
786
800
  }
801
+ };
802
+ if (part.thoughtSignature) {
803
+ tc.thoughtSignature = part.thoughtSignature;
787
804
  }
788
- ];
805
+ toolCalls.push(tc);
806
+ }
807
+ }
808
+ if (toolCalls.length > 0) {
809
+ msg.tool_calls = toolCalls;
789
810
  }
790
811
  return {
791
812
  ...ctx,
@@ -816,22 +837,28 @@ var handleGoogleStream = async (response, ctx) => {
816
837
  try {
817
838
  const parsed = JSON.parse(data);
818
839
  const candidate = parsed.candidates?.[0];
819
- const part = candidate?.content?.parts?.[0];
820
- if (part?.text) {
821
- fullContent += part.text;
822
- if (ctx.stream) {
823
- ctx.stream({ type: "content", content: part.text });
840
+ const parts = candidate?.content?.parts || [];
841
+ for (const part of parts) {
842
+ if (part?.text) {
843
+ fullContent += part.text;
844
+ if (ctx.stream) {
845
+ ctx.stream({ type: "content", content: part.text });
846
+ }
824
847
  }
825
- }
826
- if (part?.functionCall) {
827
- toolCalls.push({
828
- id: Math.random().toString(36).substring(2, 9),
829
- type: "function",
830
- function: {
831
- name: part.functionCall.name,
832
- arguments: JSON.stringify(part.functionCall.args)
848
+ if (part?.functionCall) {
849
+ const tc = {
850
+ id: Math.random().toString(36).substring(2, 9),
851
+ type: "function",
852
+ function: {
853
+ name: part.functionCall.name,
854
+ arguments: JSON.stringify(part.functionCall.args)
855
+ }
856
+ };
857
+ if (part.thoughtSignature) {
858
+ tc.thoughtSignature = part.thoughtSignature;
833
859
  }
834
- });
860
+ toolCalls.push(tc);
861
+ }
835
862
  }
836
863
  } catch (e) {
837
864
  }