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