@contentgrowth/llm-service 1.2.0 → 1.2.2

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