@contentgrowth/llm-service 1.2.2 → 1.2.4

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.d.cts CHANGED
@@ -155,6 +155,44 @@ declare class OpenAIProvider extends BaseLLMProvider {
155
155
  getDeepResearchStatus(operationId: any): Promise<void>;
156
156
  }
157
157
 
158
+ declare class AnthropicProvider extends BaseLLMProvider {
159
+ models: any;
160
+ defaultModel: any;
161
+ chat(userMessage: any, systemPrompt?: string, options?: {}): Promise<{
162
+ text: any;
163
+ }>;
164
+ chatCompletion(messages: any, systemPrompt: any, tools?: any, options?: {}): Promise<{
165
+ content: any;
166
+ tool_calls: any;
167
+ finishReason: string;
168
+ _rawFinishReason: any;
169
+ _responseFormat: any;
170
+ usage: {
171
+ prompt_tokens: any;
172
+ completion_tokens: any;
173
+ total_tokens: any;
174
+ };
175
+ model: any;
176
+ }>;
177
+ _chatCompletionWithModel(messages: any, systemPrompt: any, tools: any, modelName: any, maxTokens: any, temperature: any, options?: {}): Promise<{
178
+ content: any;
179
+ tool_calls: any;
180
+ finishReason: string;
181
+ _rawFinishReason: any;
182
+ _responseFormat: any;
183
+ usage: {
184
+ prompt_tokens: any;
185
+ completion_tokens: any;
186
+ total_tokens: any;
187
+ };
188
+ model: any;
189
+ }>;
190
+ _getModelForTier(tier: any): any;
191
+ _shouldAutoParse(options: any): boolean;
192
+ _safeJsonParse(text: any): any;
193
+ normalizeFinishReason(providerReason: any): string;
194
+ }
195
+
158
196
  /**
159
197
  * Create a Hono handler for speech transcription.
160
198
  *
@@ -411,6 +449,18 @@ declare namespace MODEL_CONFIGS {
411
449
  let image_1: string;
412
450
  export { image_1 as image };
413
451
  }
452
+ namespace anthropic {
453
+ let _default_3: string;
454
+ export { _default_3 as default };
455
+ let edge_3: string;
456
+ export { edge_3 as edge };
457
+ let fast_3: string;
458
+ export { fast_3 as fast };
459
+ let cost_3: string;
460
+ export { cost_3 as cost };
461
+ let free_3: string;
462
+ export { free_3 as free };
463
+ }
414
464
  }
415
465
  declare class ConfigManager {
416
466
  static _provider: DefaultConfigProvider;
@@ -467,7 +517,10 @@ declare class GoogleProvider extends BaseLLMProvider {
467
517
  thought_signature: any;
468
518
  tool_calls: {
469
519
  type: string;
470
- function: _google_genai.FunctionCall;
520
+ function: {
521
+ name: string;
522
+ arguments: string;
523
+ };
471
524
  thought_signature: any;
472
525
  }[];
473
526
  finishReason: string;
@@ -486,7 +539,10 @@ declare class GoogleProvider extends BaseLLMProvider {
486
539
  thought_signature: any;
487
540
  tool_calls: {
488
541
  type: string;
489
- function: _google_genai.FunctionCall;
542
+ function: {
543
+ name: string;
544
+ arguments: string;
545
+ };
490
546
  thought_signature: any;
491
547
  }[];
492
548
  finishReason: string;
@@ -667,4 +723,4 @@ declare class TranscriptionServiceException extends Error {
667
723
  statusCode: number;
668
724
  }
669
725
 
670
- export { BaseConfigProvider$1 as BaseConfigProvider, ConfigManager, DefaultConfigProvider, FINISH_REASONS, GoogleProvider as GeminiProvider, GoogleProvider, LLMService, LLMServiceException, MODEL_CONFIGS, OpenAIProvider, TranscriptionService, TranscriptionServiceException, GoogleProvider as VertexProvider, createSpeechHandler, extractJsonFromResponse, extractTextAndJson, handleApiError, sanitizeError };
726
+ export { AnthropicProvider, BaseConfigProvider$1 as BaseConfigProvider, ConfigManager, DefaultConfigProvider, FINISH_REASONS, GoogleProvider as GeminiProvider, GoogleProvider, LLMService, LLMServiceException, MODEL_CONFIGS, OpenAIProvider, TranscriptionService, TranscriptionServiceException, GoogleProvider as VertexProvider, createSpeechHandler, extractJsonFromResponse, extractTextAndJson, handleApiError, sanitizeError };
package/dist/index.d.ts CHANGED
@@ -155,6 +155,44 @@ declare class OpenAIProvider extends BaseLLMProvider {
155
155
  getDeepResearchStatus(operationId: any): Promise<void>;
156
156
  }
157
157
 
158
+ declare class AnthropicProvider extends BaseLLMProvider {
159
+ models: any;
160
+ defaultModel: any;
161
+ chat(userMessage: any, systemPrompt?: string, options?: {}): Promise<{
162
+ text: any;
163
+ }>;
164
+ chatCompletion(messages: any, systemPrompt: any, tools?: any, options?: {}): Promise<{
165
+ content: any;
166
+ tool_calls: any;
167
+ finishReason: string;
168
+ _rawFinishReason: any;
169
+ _responseFormat: any;
170
+ usage: {
171
+ prompt_tokens: any;
172
+ completion_tokens: any;
173
+ total_tokens: any;
174
+ };
175
+ model: any;
176
+ }>;
177
+ _chatCompletionWithModel(messages: any, systemPrompt: any, tools: any, modelName: any, maxTokens: any, temperature: any, options?: {}): Promise<{
178
+ content: any;
179
+ tool_calls: any;
180
+ finishReason: string;
181
+ _rawFinishReason: any;
182
+ _responseFormat: any;
183
+ usage: {
184
+ prompt_tokens: any;
185
+ completion_tokens: any;
186
+ total_tokens: any;
187
+ };
188
+ model: any;
189
+ }>;
190
+ _getModelForTier(tier: any): any;
191
+ _shouldAutoParse(options: any): boolean;
192
+ _safeJsonParse(text: any): any;
193
+ normalizeFinishReason(providerReason: any): string;
194
+ }
195
+
158
196
  /**
159
197
  * Create a Hono handler for speech transcription.
160
198
  *
@@ -411,6 +449,18 @@ declare namespace MODEL_CONFIGS {
411
449
  let image_1: string;
412
450
  export { image_1 as image };
413
451
  }
452
+ namespace anthropic {
453
+ let _default_3: string;
454
+ export { _default_3 as default };
455
+ let edge_3: string;
456
+ export { edge_3 as edge };
457
+ let fast_3: string;
458
+ export { fast_3 as fast };
459
+ let cost_3: string;
460
+ export { cost_3 as cost };
461
+ let free_3: string;
462
+ export { free_3 as free };
463
+ }
414
464
  }
415
465
  declare class ConfigManager {
416
466
  static _provider: DefaultConfigProvider;
@@ -467,7 +517,10 @@ declare class GoogleProvider extends BaseLLMProvider {
467
517
  thought_signature: any;
468
518
  tool_calls: {
469
519
  type: string;
470
- function: _google_genai.FunctionCall;
520
+ function: {
521
+ name: string;
522
+ arguments: string;
523
+ };
471
524
  thought_signature: any;
472
525
  }[];
473
526
  finishReason: string;
@@ -486,7 +539,10 @@ declare class GoogleProvider extends BaseLLMProvider {
486
539
  thought_signature: any;
487
540
  tool_calls: {
488
541
  type: string;
489
- function: _google_genai.FunctionCall;
542
+ function: {
543
+ name: string;
544
+ arguments: string;
545
+ };
490
546
  thought_signature: any;
491
547
  }[];
492
548
  finishReason: string;
@@ -667,4 +723,4 @@ declare class TranscriptionServiceException extends Error {
667
723
  statusCode: number;
668
724
  }
669
725
 
670
- export { BaseConfigProvider$1 as BaseConfigProvider, ConfigManager, DefaultConfigProvider, FINISH_REASONS, GoogleProvider as GeminiProvider, GoogleProvider, LLMService, LLMServiceException, MODEL_CONFIGS, OpenAIProvider, TranscriptionService, TranscriptionServiceException, GoogleProvider as VertexProvider, createSpeechHandler, extractJsonFromResponse, extractTextAndJson, handleApiError, sanitizeError };
726
+ export { AnthropicProvider, BaseConfigProvider$1 as BaseConfigProvider, ConfigManager, DefaultConfigProvider, FINISH_REASONS, GoogleProvider as GeminiProvider, GoogleProvider, LLMService, LLMServiceException, MODEL_CONFIGS, OpenAIProvider, TranscriptionService, TranscriptionServiceException, GoogleProvider as VertexProvider, createSpeechHandler, extractJsonFromResponse, extractTextAndJson, handleApiError, sanitizeError };
package/dist/index.js CHANGED
@@ -144,35 +144,36 @@ var DefaultConfigProvider = class extends BaseConfigProvider {
144
144
  // src/llm/config-manager.js
145
145
  var MODEL_CONFIGS = {
146
146
  openai: {
147
- default: "gpt-4o",
148
- edge: "gpt-4o",
149
- fast: "gpt-4o-mini",
150
- cost: "gpt-4o-mini",
151
- free: "gpt-4o-mini"
147
+ default: "gpt-5.5-instant",
148
+ edge: "gpt-5.5-thinking",
149
+ fast: "gpt-5.5-instant",
150
+ cost: "gpt-5.5-instant",
151
+ free: "gpt-5.5-instant"
152
152
  },
153
153
  gemini: {
154
- default: "gemini-3-flash-preview",
155
- // 'gemini-2.5-flash',
156
- edge: "gemini-3-pro-preview",
157
- // 'gemini-2.5-pro',
158
- fast: "gemini-3-flash-preview",
159
- // 'gemini-2.5-flash-lite',
160
- cost: "gemini-3-flash-preview",
161
- // 'gemini-2.5-flash-lite',
162
- free: "gemini-3-flash-preview",
163
- // 'gemini-2.0-flash-lite',
154
+ default: "gemini-3.5-flash",
155
+ edge: "gemini-3.5-pro",
156
+ fast: "gemini-3.5-flash",
157
+ cost: "gemini-3.5-flash",
158
+ free: "gemini-3.5-flash",
164
159
  video: "veo",
165
- image: "gemini-3-pro-image-preview"
166
- // Default image generation model
160
+ image: "gemini-3.5-pro-image-preview"
167
161
  },
168
162
  vertex: {
169
- default: "gemini-3-flash-preview",
170
- edge: "gemini-3-pro-preview",
171
- fast: "gemini-3-flash-preview",
172
- cost: "gemini-3-flash-preview",
173
- free: "gemini-3-flash-preview",
163
+ default: "gemini-3.5-flash",
164
+ edge: "gemini-3.5-pro",
165
+ fast: "gemini-3.5-flash",
166
+ cost: "gemini-3.5-flash",
167
+ free: "gemini-3.5-flash",
174
168
  video: "veo",
175
- image: "gemini-3-pro-image-preview"
169
+ image: "gemini-3.5-pro-image-preview"
170
+ },
171
+ anthropic: {
172
+ default: "claude-4.6-sonnet",
173
+ edge: "claude-4.8-opus",
174
+ fast: "claude-4.5-haiku",
175
+ cost: "claude-4.5-haiku",
176
+ free: "claude-4.5-haiku"
176
177
  }
177
178
  };
178
179
  var ConfigManager = class {
@@ -725,8 +726,20 @@ ${msg.content}`;
725
726
  const isLastAssistantMessage = index === geminiMessages.map((m, i) => m.role === "assistant" ? i : -1).filter((i) => i >= 0).pop();
726
727
  if (msg.tool_calls) {
727
728
  parts2 = msg.tool_calls.map((tc) => {
729
+ let args = tc.function.args;
730
+ if (tc.function.arguments) {
731
+ if (typeof tc.function.arguments === "string") {
732
+ try {
733
+ args = JSON.parse(tc.function.arguments);
734
+ } catch (e) {
735
+ args = {};
736
+ }
737
+ } else {
738
+ args = tc.function.arguments;
739
+ }
740
+ }
728
741
  const part = {
729
- functionCall: { name: tc.function.name, args: tc.function.arguments || tc.function.args }
742
+ functionCall: { name: tc.function.name, args }
730
743
  };
731
744
  if (tc.thought_signature) {
732
745
  part.thoughtSignature = tc.thought_signature;
@@ -811,7 +824,10 @@ ${msg.content}`;
811
824
  thought_signature: responseThoughtSignature,
812
825
  tool_calls: toolCalls ? (Array.isArray(toolCalls) ? toolCalls : [toolCalls]).map((fc) => ({
813
826
  type: "function",
814
- function: fc,
827
+ function: {
828
+ name: fc.name,
829
+ arguments: JSON.stringify(fc.args || {})
830
+ },
815
831
  thought_signature: fc.thought_signature
816
832
  })) : null,
817
833
  finishReason: normalizedFinishReason,
@@ -1132,6 +1148,170 @@ ${prompt}` : prompt;
1132
1148
  }
1133
1149
  };
1134
1150
 
1151
+ // src/llm/providers/anthropic-provider.js
1152
+ var AnthropicProvider = class extends BaseLLMProvider {
1153
+ constructor(config) {
1154
+ super(config);
1155
+ this.models = config.models || {};
1156
+ this.defaultModel = this.models.default || "claude-3-5-sonnet-20241022";
1157
+ }
1158
+ async chat(userMessage, systemPrompt = "", options = {}) {
1159
+ var _a;
1160
+ const messages = [{ role: "user", content: userMessage }];
1161
+ const tier = options.tier || "default";
1162
+ const effectiveModel = this._getModelForTier(tier);
1163
+ const effectiveMaxTokens = options.maxTokens || this.config.maxTokens || 4096;
1164
+ const effectiveTemperature = options.temperature !== void 0 ? options.temperature : (_a = this.config.temperature) != null ? _a : 0.7;
1165
+ const response = await this._chatCompletionWithModel(
1166
+ messages,
1167
+ systemPrompt,
1168
+ null,
1169
+ effectiveModel,
1170
+ effectiveMaxTokens,
1171
+ effectiveTemperature,
1172
+ options
1173
+ );
1174
+ return { text: response.content };
1175
+ }
1176
+ async chatCompletion(messages, systemPrompt, tools = null, options = {}) {
1177
+ var _a;
1178
+ const tier = options.tier || "default";
1179
+ const effectiveModel = this._getModelForTier(tier);
1180
+ const effectiveMaxTokens = options.maxTokens || this.config.maxTokens || 4096;
1181
+ const effectiveTemperature = options.temperature !== void 0 ? options.temperature : (_a = this.config.temperature) != null ? _a : 0.7;
1182
+ return this._chatCompletionWithModel(
1183
+ messages,
1184
+ systemPrompt,
1185
+ tools,
1186
+ effectiveModel,
1187
+ effectiveMaxTokens,
1188
+ effectiveTemperature,
1189
+ options
1190
+ );
1191
+ }
1192
+ async _chatCompletionWithModel(messages, systemPrompt, tools, modelName, maxTokens, temperature, options = {}) {
1193
+ const baseURL = this.config.baseURL || "https://api.anthropic.com";
1194
+ const url = `${baseURL.replace(/\/$/, "")}/v1/messages`;
1195
+ const formattedMessages = messages.map((m) => ({
1196
+ role: m.role === "assistant" ? "assistant" : "user",
1197
+ content: m.content
1198
+ }));
1199
+ const headers = {
1200
+ "Content-Type": "application/json",
1201
+ "x-api-key": this.config.apiKey || "",
1202
+ "anthropic-version": "2023-06-01"
1203
+ };
1204
+ const payload = {
1205
+ model: modelName,
1206
+ messages: formattedMessages,
1207
+ max_tokens: maxTokens,
1208
+ temperature
1209
+ };
1210
+ if (systemPrompt) {
1211
+ payload.system = systemPrompt;
1212
+ }
1213
+ if (options.responseFormat) {
1214
+ const formatType = typeof options.responseFormat === "string" ? options.responseFormat : options.responseFormat.type;
1215
+ if (formatType === "json" || formatType === "json_schema") {
1216
+ const schemaText = options.responseFormat.schema || options.responseSchema ? `
1217
+
1218
+ Your output must comply strictly with this JSON Schema: ${JSON.stringify(options.responseFormat.schema || options.responseSchema)}` : "";
1219
+ const jsonPrompt = `IMPORTANT: You must respond ONLY in raw JSON format. Do not write any conversational text, explanations, or markdown code blocks (e.g. do NOT wrap your answer in \`\`\`json ... \`\`\`). Your response must start with '{' and end with '}'.${schemaText}`;
1220
+ if (payload.system) {
1221
+ payload.system = `${payload.system}
1222
+
1223
+ ${jsonPrompt}`;
1224
+ } else {
1225
+ payload.system = jsonPrompt;
1226
+ }
1227
+ }
1228
+ }
1229
+ let responseText;
1230
+ let responseData;
1231
+ try {
1232
+ const res = await fetch(url, {
1233
+ method: "POST",
1234
+ headers,
1235
+ body: JSON.stringify(payload)
1236
+ });
1237
+ responseText = await res.text();
1238
+ if (!res.ok) {
1239
+ console.error(`[AnthropicProvider] API request failed with status ${res.status}:`, responseText);
1240
+ throw new LLMServiceException(`Anthropic API Error: ${responseText}`, res.status);
1241
+ }
1242
+ responseData = JSON.parse(responseText);
1243
+ } catch (error) {
1244
+ console.error(`[AnthropicProvider] request failed (API Key: ${this._getMaskedApiKey()}):`, error);
1245
+ throw error;
1246
+ }
1247
+ const contentBlock = responseData.content && responseData.content[0];
1248
+ const contentText = contentBlock ? contentBlock.text : "";
1249
+ if (!contentText) {
1250
+ console.error("[AnthropicProvider] Model returned empty response content");
1251
+ throw new LLMServiceException(
1252
+ "Model returned empty response. This usually means the prompt or schema is confusing the model.",
1253
+ 500
1254
+ );
1255
+ }
1256
+ const rawFinishReason = responseData.stop_reason;
1257
+ const normalizedFinishReason = this.normalizeFinishReason(rawFinishReason);
1258
+ const result = {
1259
+ content: contentText,
1260
+ tool_calls: null,
1261
+ // REST client tool calls mapping not required for playbook studio
1262
+ finishReason: normalizedFinishReason,
1263
+ _rawFinishReason: rawFinishReason,
1264
+ _responseFormat: options.responseFormat,
1265
+ usage: {
1266
+ prompt_tokens: responseData.usage ? responseData.usage.input_tokens : 0,
1267
+ completion_tokens: responseData.usage ? responseData.usage.output_tokens : 0,
1268
+ total_tokens: responseData.usage ? responseData.usage.input_tokens + responseData.usage.output_tokens : 0
1269
+ },
1270
+ model: modelName
1271
+ };
1272
+ if (options.responseFormat && this._shouldAutoParse(options)) {
1273
+ result.parsedContent = this._safeJsonParse(contentText);
1274
+ }
1275
+ return result;
1276
+ }
1277
+ _getModelForTier(tier) {
1278
+ if (this.models[tier]) {
1279
+ return this.models[tier];
1280
+ }
1281
+ return this.defaultModel;
1282
+ }
1283
+ _shouldAutoParse(options) {
1284
+ if (!options.responseFormat) return false;
1285
+ const formatType = typeof options.responseFormat === "string" ? options.responseFormat : options.responseFormat.type;
1286
+ return formatType === "json" || formatType === "json_schema";
1287
+ }
1288
+ _safeJsonParse(text) {
1289
+ try {
1290
+ return extractJsonFromResponse(text);
1291
+ } catch (error) {
1292
+ console.error("[AnthropicProvider] Failed to extract JSON from response:", text, error);
1293
+ try {
1294
+ return JSON.parse(text);
1295
+ } catch (_) {
1296
+ return null;
1297
+ }
1298
+ }
1299
+ }
1300
+ normalizeFinishReason(providerReason) {
1301
+ const lower = (providerReason || "").toLowerCase();
1302
+ if (lower === "end_turn") {
1303
+ return "completed";
1304
+ }
1305
+ if (lower === "max_tokens") {
1306
+ return "truncated";
1307
+ }
1308
+ if (lower === "stop_sequence") {
1309
+ return "completed";
1310
+ }
1311
+ return super.normalizeFinishReason(providerReason);
1312
+ }
1313
+ };
1314
+
1135
1315
  // src/llm-service.js
1136
1316
  var LLMService = class {
1137
1317
  constructor(env, toolImplementations = {}) {
@@ -1153,6 +1333,8 @@ var LLMService = class {
1153
1333
  provider = new OpenAIProvider(config);
1154
1334
  } else if (config.provider === "gemini" || config.provider === "vertex") {
1155
1335
  provider = new GoogleProvider(config);
1336
+ } else if (config.provider === "anthropic") {
1337
+ provider = new AnthropicProvider(config);
1156
1338
  } else {
1157
1339
  throw new LLMServiceException(`Unsupported LLM provider: ${config.provider}`, 500);
1158
1340
  }
@@ -1720,6 +1902,7 @@ function createSpeechHandler(app, getConfig) {
1720
1902
  return app;
1721
1903
  }
1722
1904
  export {
1905
+ AnthropicProvider,
1723
1906
  BaseConfigProvider,
1724
1907
  ConfigManager,
1725
1908
  DefaultConfigProvider,