@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.js CHANGED
@@ -637,7 +637,7 @@ var GoogleProvider = class extends BaseLLMProvider {
637
637
  );
638
638
  }
639
639
  async _chatCompletionWithModel(messages, systemPrompt, tools, modelName, maxTokens, temperature, options = {}) {
640
- var _a, _b, _c, _d, _e, _f, _g;
640
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
641
641
  const generationConfig = {
642
642
  temperature: (_a = options.temperature) != null ? _a : temperature,
643
643
  maxOutputTokens: (_b = options.maxTokens) != null ? _b : maxTokens
@@ -663,8 +663,41 @@ ${msg.content}`;
663
663
  }
664
664
  }
665
665
  }
666
- const contents = geminiMessages.map((msg, index) => {
667
- var _a2, _b2, _c2, _d2;
666
+ const contents = [];
667
+ let pendingToolParts = [];
668
+ for (let index = 0; index < geminiMessages.length; index++) {
669
+ const msg = geminiMessages[index];
670
+ if (msg.role === "tool") {
671
+ let assistantMsg = null;
672
+ for (let j = index - 1; j >= 0; j--) {
673
+ if (geminiMessages[j].role === "assistant" && geminiMessages[j].tool_calls) {
674
+ assistantMsg = geminiMessages[j];
675
+ break;
676
+ }
677
+ }
678
+ const toolCall = (_c = assistantMsg == null ? void 0 : assistantMsg.tool_calls) == null ? void 0 : _c.find((tc) => tc.id === msg.tool_call_id);
679
+ pendingToolParts.push({
680
+ functionResponse: {
681
+ name: ((_d = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _d.name) || "unknown_tool",
682
+ response: { content: msg.content }
683
+ }
684
+ });
685
+ const nextMsg = geminiMessages[index + 1];
686
+ if (!nextMsg || nextMsg.role !== "tool") {
687
+ if (options.responseFormat === "json" || ((_e = options.responseFormat) == null ? void 0 : _e.type) === "json_schema" || options.responseSchema) {
688
+ pendingToolParts.push({ text: "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]" });
689
+ } else {
690
+ pendingToolParts.push({ text: "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]" });
691
+ }
692
+ contents.push({ role: "user", parts: pendingToolParts });
693
+ pendingToolParts = [];
694
+ }
695
+ continue;
696
+ }
697
+ if (pendingToolParts.length > 0) {
698
+ contents.push({ role: "user", parts: pendingToolParts });
699
+ pendingToolParts = [];
700
+ }
668
701
  let role = "";
669
702
  let parts2;
670
703
  switch (msg.role) {
@@ -673,7 +706,7 @@ ${msg.content}`;
673
706
  parts2 = [{ text: msg.content }];
674
707
  if (index === geminiMessages.length - 1) {
675
708
  let reminder = "";
676
- if (options.responseFormat === "json" || ((_a2 = options.responseFormat) == null ? void 0 : _a2.type) === "json_schema" || options.responseSchema) {
709
+ if (options.responseFormat === "json" || ((_f = options.responseFormat) == null ? void 0 : _f.type) === "json_schema" || options.responseSchema) {
677
710
  reminder = "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]";
678
711
  } else {
679
712
  reminder = "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]";
@@ -707,27 +740,11 @@ ${msg.content}`;
707
740
  parts2 = [part];
708
741
  }
709
742
  break;
710
- case "tool":
711
- role = "user";
712
- const preceding_message = messages[index - 1];
713
- 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);
714
- parts2 = [{
715
- functionResponse: {
716
- name: ((_c2 = tool_call == null ? void 0 : tool_call.function) == null ? void 0 : _c2.name) || "unknown_tool",
717
- response: { content: msg.content }
718
- }
719
- }];
720
- if (options.responseFormat === "json" || ((_d2 = options.responseFormat) == null ? void 0 : _d2.type) === "json_schema" || options.responseSchema) {
721
- parts2.push({ text: "\n\n[SYSTEM NOTE: The output MUST be valid JSON as per the schema. Do not include markdown formatting or explanations.]" });
722
- } else {
723
- parts2.push({ text: "\n\n[SYSTEM NOTE: Please ensure your response adheres strictly to the constraints defined in the System Prompt.]" });
724
- }
725
- break;
726
743
  default:
727
- return null;
744
+ continue;
728
745
  }
729
- return { role, parts: parts2 };
730
- }).filter(Boolean);
746
+ contents.push({ role, parts: parts2 });
747
+ }
731
748
  while (contents.length > 0 && contents[0].role !== "user") {
732
749
  contents.shift();
733
750
  }
@@ -751,11 +768,11 @@ ${msg.content}`;
751
768
  }
752
769
  }
753
770
  const response = await this._generateContent(requestOptions);
754
- const candidate = (_c = response.candidates) == null ? void 0 : _c[0];
771
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
755
772
  if (!candidate) {
756
773
  throw new LLMServiceException("No candidates returned from model", 500);
757
774
  }
758
- const parts = ((_d = candidate.content) == null ? void 0 : _d.parts) || [];
775
+ const parts = ((_h = candidate.content) == null ? void 0 : _h.parts) || [];
759
776
  let textContent = "";
760
777
  let toolCalls = null;
761
778
  let responseThoughtSignature = null;
@@ -800,9 +817,9 @@ ${msg.content}`;
800
817
  _rawFinishReason: candidate.finishReason,
801
818
  _responseFormat: options.responseFormat,
802
819
  usage: {
803
- prompt_tokens: ((_e = response.usageMetadata) == null ? void 0 : _e.promptTokenCount) || 0,
804
- completion_tokens: ((_f = response.usageMetadata) == null ? void 0 : _f.candidatesTokenCount) || 0,
805
- total_tokens: ((_g = response.usageMetadata) == null ? void 0 : _g.totalTokenCount) || 0
820
+ prompt_tokens: ((_i = response.usageMetadata) == null ? void 0 : _i.promptTokenCount) || 0,
821
+ completion_tokens: ((_j = response.usageMetadata) == null ? void 0 : _j.candidatesTokenCount) || 0,
822
+ total_tokens: ((_k = response.usageMetadata) == null ? void 0 : _k.totalTokenCount) || 0
806
823
  },
807
824
  ...options.responseFormat && this._shouldAutoParse(options) ? {
808
825
  parsedContent: this._safeJsonParse(textContent)