@contentgrowth/llm-service 1.2.0 → 1.2.1

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
@@ -687,7 +687,7 @@ var GoogleProvider = class extends BaseLLMProvider {
687
687
  );
688
688
  }
689
689
  async _chatCompletionWithModel(messages, systemPrompt, tools, modelName, maxTokens, temperature, options = {}) {
690
- var _a, _b, _c, _d, _e, _f, _g;
690
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
691
691
  const generationConfig = {
692
692
  temperature: (_a = options.temperature) != null ? _a : temperature,
693
693
  maxOutputTokens: (_b = options.maxTokens) != null ? _b : maxTokens
@@ -713,8 +713,41 @@ ${msg.content}`;
713
713
  }
714
714
  }
715
715
  }
716
- const contents = geminiMessages.map((msg, index) => {
717
- var _a2, _b2, _c2, _d2;
716
+ const contents = [];
717
+ let pendingToolParts = [];
718
+ for (let index = 0; index < geminiMessages.length; index++) {
719
+ const msg = geminiMessages[index];
720
+ if (msg.role === "tool") {
721
+ let assistantMsg = null;
722
+ for (let j = index - 1; j >= 0; j--) {
723
+ if (geminiMessages[j].role === "assistant" && geminiMessages[j].tool_calls) {
724
+ assistantMsg = geminiMessages[j];
725
+ break;
726
+ }
727
+ }
728
+ const toolCall = (_c = assistantMsg == null ? void 0 : assistantMsg.tool_calls) == null ? void 0 : _c.find((tc) => tc.id === msg.tool_call_id);
729
+ pendingToolParts.push({
730
+ functionResponse: {
731
+ name: ((_d = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _d.name) || "unknown_tool",
732
+ response: { content: msg.content }
733
+ }
734
+ });
735
+ const nextMsg = geminiMessages[index + 1];
736
+ if (!nextMsg || nextMsg.role !== "tool") {
737
+ if (options.responseFormat === "json" || ((_e = options.responseFormat) == null ? void 0 : _e.type) === "json_schema" || options.responseSchema) {
738
+ pendingToolParts.push({ text: "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]" });
739
+ } else {
740
+ pendingToolParts.push({ text: "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]" });
741
+ }
742
+ contents.push({ role: "user", parts: pendingToolParts });
743
+ pendingToolParts = [];
744
+ }
745
+ continue;
746
+ }
747
+ if (pendingToolParts.length > 0) {
748
+ contents.push({ role: "user", parts: pendingToolParts });
749
+ pendingToolParts = [];
750
+ }
718
751
  let role = "";
719
752
  let parts2;
720
753
  switch (msg.role) {
@@ -723,7 +756,7 @@ ${msg.content}`;
723
756
  parts2 = [{ text: msg.content }];
724
757
  if (index === geminiMessages.length - 1) {
725
758
  let reminder = "";
726
- if (options.responseFormat === "json" || ((_a2 = options.responseFormat) == null ? void 0 : _a2.type) === "json_schema" || options.responseSchema) {
759
+ if (options.responseFormat === "json" || ((_f = options.responseFormat) == null ? void 0 : _f.type) === "json_schema" || options.responseSchema) {
727
760
  reminder = "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]";
728
761
  } else {
729
762
  reminder = "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]";
@@ -757,27 +790,11 @@ ${msg.content}`;
757
790
  parts2 = [part];
758
791
  }
759
792
  break;
760
- case "tool":
761
- role = "user";
762
- const preceding_message = messages[index - 1];
763
- const tool_call = (_b2 = preceding_message == null ? void 0 : preceding_message.tool_calls) == null ? void 0 : _b2.find((tc) => tc.id === msg.tool_call_id);
764
- parts2 = [{
765
- functionResponse: {
766
- name: ((_c2 = tool_call == null ? void 0 : tool_call.function) == null ? void 0 : _c2.name) || "unknown_tool",
767
- response: { content: msg.content }
768
- }
769
- }];
770
- if (options.responseFormat === "json" || ((_d2 = options.responseFormat) == null ? void 0 : _d2.type) === "json_schema" || options.responseSchema) {
771
- parts2.push({ text: "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]" });
772
- } else {
773
- parts2.push({ text: "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]" });
774
- }
775
- break;
776
793
  default:
777
- return null;
794
+ continue;
778
795
  }
779
- return { role, parts: parts2 };
780
- }).filter(Boolean);
796
+ contents.push({ role, parts: parts2 });
797
+ }
781
798
  while (contents.length > 0 && contents[0].role !== "user") {
782
799
  contents.shift();
783
800
  }
@@ -801,11 +818,11 @@ ${msg.content}`;
801
818
  }
802
819
  }
803
820
  const response = await this._generateContent(requestOptions);
804
- const candidate = (_c = response.candidates) == null ? void 0 : _c[0];
821
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
805
822
  if (!candidate) {
806
823
  throw new LLMServiceException("No candidates returned from model", 500);
807
824
  }
808
- const parts = ((_d = candidate.content) == null ? void 0 : _d.parts) || [];
825
+ const parts = ((_h = candidate.content) == null ? void 0 : _h.parts) || [];
809
826
  let textContent = "";
810
827
  let toolCalls = null;
811
828
  let responseThoughtSignature = null;
@@ -850,9 +867,9 @@ ${msg.content}`;
850
867
  _rawFinishReason: candidate.finishReason,
851
868
  _responseFormat: options.responseFormat,
852
869
  usage: {
853
- prompt_tokens: ((_e = response.usageMetadata) == null ? void 0 : _e.promptTokenCount) || 0,
854
- completion_tokens: ((_f = response.usageMetadata) == null ? void 0 : _f.candidatesTokenCount) || 0,
855
- total_tokens: ((_g = response.usageMetadata) == null ? void 0 : _g.totalTokenCount) || 0
870
+ prompt_tokens: ((_i = response.usageMetadata) == null ? void 0 : _i.promptTokenCount) || 0,
871
+ completion_tokens: ((_j = response.usageMetadata) == null ? void 0 : _j.candidatesTokenCount) || 0,
872
+ total_tokens: ((_k = response.usageMetadata) == null ? void 0 : _k.totalTokenCount) || 0
856
873
  },
857
874
  ...options.responseFormat && this._shouldAutoParse(options) ? {
858
875
  parsedContent: this._safeJsonParse(textContent)