@jaypie/llm 1.1.30 → 1.2.0-rc.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.
@@ -1,15 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var errors = require('@jaypie/errors');
4
- var Anthropic = require('@anthropic-ai/sdk');
5
4
  var v4 = require('zod/v4');
6
- var core = require('@jaypie/core');
5
+ var kit = require('@jaypie/kit');
6
+ var logger = require('@jaypie/logger');
7
7
  var RandomLib = require('random');
8
8
  var openai = require('openai');
9
9
  var zod = require('openai/helpers/zod');
10
10
  var aws = require('@jaypie/aws');
11
- var genai = require('@google/genai');
12
- var sdk = require('@openrouter/sdk');
13
11
  var openmeteo = require('openmeteo');
14
12
 
15
13
  const PROVIDER = {
@@ -349,7 +347,7 @@ var LlmResponseStatus;
349
347
  * @returns LlmInputMessage
350
348
  */
351
349
  function formatOperateMessage(input, options) {
352
- const content = options?.data ? core.placeholders(input, options.data) : input;
350
+ const content = options?.data ? kit.placeholders(input, options.data) : input;
353
351
  return {
354
352
  content,
355
353
  role: options?.role || exports.LlmMessageRole.User,
@@ -387,7 +385,7 @@ function formatOperateInput(input, options) {
387
385
  return [input];
388
386
  }
389
387
 
390
- const getLogger$4 = () => core.log.lib({ lib: core.JAYPIE.LIB.LLM });
388
+ const getLogger$4 = () => logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
391
389
  const log$1 = getLogger$4();
392
390
 
393
391
  // Turn policy constants
@@ -692,16 +690,17 @@ var ErrorCategory;
692
690
  // Constants
693
691
  //
694
692
  const STRUCTURED_OUTPUT_TOOL_NAME$2 = "structured_output";
695
- const RETRYABLE_ERROR_TYPES$1 = [
696
- Anthropic.APIConnectionError,
697
- Anthropic.APIConnectionTimeoutError,
698
- Anthropic.InternalServerError,
693
+ // Error names for classification (using string names since SDK is optional)
694
+ const RETRYABLE_ERROR_NAMES = [
695
+ "APIConnectionError",
696
+ "APIConnectionTimeoutError",
697
+ "InternalServerError",
699
698
  ];
700
- const NOT_RETRYABLE_ERROR_TYPES$1 = [
701
- Anthropic.AuthenticationError,
702
- Anthropic.BadRequestError,
703
- Anthropic.NotFoundError,
704
- Anthropic.PermissionDeniedError,
699
+ const NOT_RETRYABLE_ERROR_NAMES = [
700
+ "AuthenticationError",
701
+ "BadRequestError",
702
+ "NotFoundError",
703
+ "PermissionDeniedError",
705
704
  ];
706
705
  //
707
706
  //
@@ -909,8 +908,9 @@ class AnthropicAdapter extends BaseProviderAdapter {
909
908
  // Error Classification
910
909
  //
911
910
  classifyError(error) {
911
+ const errorName = error?.constructor?.name;
912
912
  // Check for rate limit error
913
- if (error instanceof Anthropic.RateLimitError) {
913
+ if (errorName === "RateLimitError") {
914
914
  return {
915
915
  error,
916
916
  category: ErrorCategory.RateLimit,
@@ -919,24 +919,20 @@ class AnthropicAdapter extends BaseProviderAdapter {
919
919
  };
920
920
  }
921
921
  // Check for retryable errors
922
- for (const ErrorType of RETRYABLE_ERROR_TYPES$1) {
923
- if (error instanceof ErrorType) {
924
- return {
925
- error,
926
- category: ErrorCategory.Retryable,
927
- shouldRetry: true,
928
- };
929
- }
922
+ if (RETRYABLE_ERROR_NAMES.includes(errorName)) {
923
+ return {
924
+ error,
925
+ category: ErrorCategory.Retryable,
926
+ shouldRetry: true,
927
+ };
930
928
  }
931
929
  // Check for non-retryable errors
932
- for (const ErrorType of NOT_RETRYABLE_ERROR_TYPES$1) {
933
- if (error instanceof ErrorType) {
934
- return {
935
- error,
936
- category: ErrorCategory.Unrecoverable,
937
- shouldRetry: false,
938
- };
939
- }
930
+ if (NOT_RETRYABLE_ERROR_NAMES.includes(errorName)) {
931
+ return {
932
+ error,
933
+ category: ErrorCategory.Unrecoverable,
934
+ shouldRetry: false,
935
+ };
940
936
  }
941
937
  // Unknown error - treat as potentially retryable
942
938
  return {
@@ -2213,7 +2209,7 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2213
2209
  const openRouterAdapter = new OpenRouterAdapter();
2214
2210
 
2215
2211
  const DEFAULT_TOOL_TYPE = "function";
2216
- const log = core.log.lib({ lib: core.JAYPIE.LIB.LLM });
2212
+ const log = logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
2217
2213
  function logToolMessage(message, context) {
2218
2214
  log.trace.var({ [context.name]: message });
2219
2215
  }
@@ -2279,7 +2275,7 @@ class Toolkit {
2279
2275
  else if (typeof tool.message === "function") {
2280
2276
  log.trace("[Toolkit] Tool provided function message");
2281
2277
  log.trace("[Toolkit] Resolving message result");
2282
- message = await core.resolveValue(tool.message(parsedArgs, { name }));
2278
+ message = await kit.resolveValue(tool.message(parsedArgs, { name }));
2283
2279
  }
2284
2280
  else {
2285
2281
  log.warn("[Toolkit] Tool provided unknown message type");
@@ -2292,7 +2288,7 @@ class Toolkit {
2292
2288
  }
2293
2289
  if (typeof this.log === "function") {
2294
2290
  log.trace("[Toolkit] Log tool call with custom logger");
2295
- await core.resolveValue(this.log(message, context));
2291
+ await kit.resolveValue(this.log(message, context));
2296
2292
  }
2297
2293
  else {
2298
2294
  log.trace("[Toolkit] Log tool call with default logger");
@@ -2305,7 +2301,7 @@ class Toolkit {
2305
2301
  log.debug("[Toolkit] Continuing...");
2306
2302
  }
2307
2303
  }
2308
- return await core.resolveValue(tool.call(parsedArgs));
2304
+ return await kit.resolveValue(tool.call(parsedArgs));
2309
2305
  }
2310
2306
  extend(tools, options = {}) {
2311
2307
  for (const tool of tools) {
@@ -2353,7 +2349,7 @@ class HookRunner {
2353
2349
  */
2354
2350
  async runBeforeModelRequest(hooks, context) {
2355
2351
  if (hooks?.beforeEachModelRequest) {
2356
- await core.resolveValue(hooks.beforeEachModelRequest(context));
2352
+ await kit.resolveValue(hooks.beforeEachModelRequest(context));
2357
2353
  }
2358
2354
  }
2359
2355
  /**
@@ -2361,7 +2357,7 @@ class HookRunner {
2361
2357
  */
2362
2358
  async runAfterModelResponse(hooks, context) {
2363
2359
  if (hooks?.afterEachModelResponse) {
2364
- await core.resolveValue(hooks.afterEachModelResponse(context));
2360
+ await kit.resolveValue(hooks.afterEachModelResponse(context));
2365
2361
  }
2366
2362
  }
2367
2363
  /**
@@ -2369,7 +2365,7 @@ class HookRunner {
2369
2365
  */
2370
2366
  async runBeforeTool(hooks, context) {
2371
2367
  if (hooks?.beforeEachTool) {
2372
- await core.resolveValue(hooks.beforeEachTool(context));
2368
+ await kit.resolveValue(hooks.beforeEachTool(context));
2373
2369
  }
2374
2370
  }
2375
2371
  /**
@@ -2377,7 +2373,7 @@ class HookRunner {
2377
2373
  */
2378
2374
  async runAfterTool(hooks, context) {
2379
2375
  if (hooks?.afterEachTool) {
2380
- await core.resolveValue(hooks.afterEachTool(context));
2376
+ await kit.resolveValue(hooks.afterEachTool(context));
2381
2377
  }
2382
2378
  }
2383
2379
  /**
@@ -2385,7 +2381,7 @@ class HookRunner {
2385
2381
  */
2386
2382
  async runOnToolError(hooks, context) {
2387
2383
  if (hooks?.onToolError) {
2388
- await core.resolveValue(hooks.onToolError(context));
2384
+ await kit.resolveValue(hooks.onToolError(context));
2389
2385
  }
2390
2386
  }
2391
2387
  /**
@@ -2393,7 +2389,7 @@ class HookRunner {
2393
2389
  */
2394
2390
  async runOnRetryableError(hooks, context) {
2395
2391
  if (hooks?.onRetryableModelError) {
2396
- await core.resolveValue(hooks.onRetryableModelError(context));
2392
+ await kit.resolveValue(hooks.onRetryableModelError(context));
2397
2393
  }
2398
2394
  }
2399
2395
  /**
@@ -2401,7 +2397,7 @@ class HookRunner {
2401
2397
  */
2402
2398
  async runOnUnrecoverableError(hooks, context) {
2403
2399
  if (hooks?.onUnrecoverableModelError) {
2404
- await core.resolveValue(hooks.onUnrecoverableModelError(context));
2400
+ await kit.resolveValue(hooks.onUnrecoverableModelError(context));
2405
2401
  }
2406
2402
  }
2407
2403
  }
@@ -2467,7 +2463,7 @@ class InputProcessor {
2467
2463
  if (options.data &&
2468
2464
  (options.placeholders?.instructions === undefined ||
2469
2465
  options.placeholders?.instructions)) {
2470
- return core.placeholders(options.instructions, options.data);
2466
+ return kit.placeholders(options.instructions, options.data);
2471
2467
  }
2472
2468
  return options.instructions;
2473
2469
  }
@@ -2480,7 +2476,7 @@ class InputProcessor {
2480
2476
  }
2481
2477
  // Apply placeholders to system if data is provided and placeholders.system is not false
2482
2478
  if (options.data && options.placeholders?.system !== false) {
2483
- return core.placeholders(options.system, options.data);
2479
+ return kit.placeholders(options.system, options.data);
2484
2480
  }
2485
2481
  return options.system;
2486
2482
  }
@@ -2764,7 +2760,7 @@ class RetryExecutor {
2764
2760
  providerRequest: options.context.providerRequest,
2765
2761
  error,
2766
2762
  });
2767
- await core.sleep(delay);
2763
+ await kit.sleep(delay);
2768
2764
  attempt++;
2769
2765
  }
2770
2766
  }
@@ -3155,16 +3151,30 @@ function createOperateLoop(config) {
3155
3151
  return new OperateLoop(config);
3156
3152
  }
3157
3153
 
3154
+ // SDK loader with caching
3155
+ let cachedSdk$2 = null;
3156
+ async function loadSdk$2() {
3157
+ if (cachedSdk$2)
3158
+ return cachedSdk$2;
3159
+ try {
3160
+ cachedSdk$2 = await import('@anthropic-ai/sdk');
3161
+ return cachedSdk$2;
3162
+ }
3163
+ catch {
3164
+ throw new errors.ConfigurationError("@anthropic-ai/sdk is required but not installed. Run: npm install @anthropic-ai/sdk");
3165
+ }
3166
+ }
3158
3167
  // Logger
3159
- const getLogger$3 = () => core.log.lib({ lib: core.JAYPIE.LIB.LLM });
3168
+ const getLogger$3 = () => logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3160
3169
  // Client initialization
3161
3170
  async function initializeClient$3({ apiKey, } = {}) {
3162
3171
  const logger = getLogger$3();
3163
3172
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("ANTHROPIC_API_KEY"));
3164
3173
  if (!resolvedApiKey) {
3165
- throw new core.ConfigurationError("The application could not resolve the required API key: ANTHROPIC_API_KEY");
3174
+ throw new errors.ConfigurationError("The application could not resolve the required API key: ANTHROPIC_API_KEY");
3166
3175
  }
3167
- const client = new Anthropic({ apiKey: resolvedApiKey });
3176
+ const sdk = await loadSdk$2();
3177
+ const client = new sdk.default({ apiKey: resolvedApiKey });
3168
3178
  logger.trace("Initialized Anthropic client");
3169
3179
  return client;
3170
3180
  }
@@ -3172,12 +3182,12 @@ async function initializeClient$3({ apiKey, } = {}) {
3172
3182
  function formatSystemMessage$2(systemPrompt, { data, placeholders } = {}) {
3173
3183
  return placeholders?.system === false
3174
3184
  ? systemPrompt
3175
- : core.placeholders(systemPrompt, data);
3185
+ : kit.placeholders(systemPrompt, data);
3176
3186
  }
3177
3187
  function formatUserMessage$3(message, { data, placeholders } = {}) {
3178
3188
  const content = placeholders?.message === false
3179
3189
  ? message
3180
- : core.placeholders(message, data);
3190
+ : kit.placeholders(message, data);
3181
3191
  return {
3182
3192
  role: PROVIDER.ANTHROPIC.ROLE.USER,
3183
3193
  content,
@@ -3194,7 +3204,7 @@ function prepareMessages$3(message, { data, placeholders } = {}) {
3194
3204
  }
3195
3205
  // Basic text completion
3196
3206
  async function createTextCompletion$1(client, messages, model, systemMessage) {
3197
- core.log.trace("Using text output (unstructured)");
3207
+ logger.log.trace("Using text output (unstructured)");
3198
3208
  const params = {
3199
3209
  model,
3200
3210
  messages,
@@ -3203,17 +3213,17 @@ async function createTextCompletion$1(client, messages, model, systemMessage) {
3203
3213
  // Add system instruction if provided
3204
3214
  if (systemMessage) {
3205
3215
  params.system = systemMessage;
3206
- core.log.trace(`System message: ${systemMessage.length} characters`);
3216
+ logger.log.trace(`System message: ${systemMessage.length} characters`);
3207
3217
  }
3208
3218
  const response = await client.messages.create(params);
3209
3219
  const firstContent = response.content[0];
3210
3220
  const text = firstContent && "text" in firstContent ? firstContent.text : "";
3211
- core.log.trace(`Assistant reply: ${text.length} characters`);
3221
+ logger.log.trace(`Assistant reply: ${text.length} characters`);
3212
3222
  return text;
3213
3223
  }
3214
3224
  // Structured output completion
3215
3225
  async function createStructuredCompletion$1(client, messages, model, responseSchema, systemMessage) {
3216
- core.log.trace("Using structured output");
3226
+ logger.log.trace("Using structured output");
3217
3227
  // Get the JSON schema for the response
3218
3228
  const schema = responseSchema instanceof v4.z.ZodType
3219
3229
  ? responseSchema
@@ -3246,7 +3256,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
3246
3256
  if (!schema.parse(result)) {
3247
3257
  throw new Error(`JSON response from Anthropic does not match schema: ${responseText}`);
3248
3258
  }
3249
- core.log.trace("Received structured response", { result });
3259
+ logger.log.trace("Received structured response", { result });
3250
3260
  return result;
3251
3261
  }
3252
3262
  catch {
@@ -3257,7 +3267,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
3257
3267
  throw new Error("Failed to parse structured response from Anthropic");
3258
3268
  }
3259
3269
  catch (error) {
3260
- core.log.error("Error creating structured completion", { error });
3270
+ logger.log.error("Error creating structured completion", { error });
3261
3271
  throw error;
3262
3272
  }
3263
3273
  }
@@ -3334,23 +3344,37 @@ class AnthropicProvider {
3334
3344
  }
3335
3345
  }
3336
3346
 
3347
+ // SDK loader with caching
3348
+ let cachedSdk$1 = null;
3349
+ async function loadSdk$1() {
3350
+ if (cachedSdk$1)
3351
+ return cachedSdk$1;
3352
+ try {
3353
+ cachedSdk$1 = await import('@google/genai');
3354
+ return cachedSdk$1;
3355
+ }
3356
+ catch {
3357
+ throw new errors.ConfigurationError("@google/genai is required but not installed. Run: npm install @google/genai");
3358
+ }
3359
+ }
3337
3360
  // Logger
3338
- const getLogger$2 = () => core.log.lib({ lib: core.JAYPIE.LIB.LLM });
3361
+ const getLogger$2 = () => logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3339
3362
  // Client initialization
3340
3363
  async function initializeClient$2({ apiKey, } = {}) {
3341
3364
  const logger = getLogger$2();
3342
3365
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("GEMINI_API_KEY"));
3343
3366
  if (!resolvedApiKey) {
3344
- throw new core.ConfigurationError("The application could not resolve the requested keys");
3367
+ throw new errors.ConfigurationError("The application could not resolve the requested keys");
3345
3368
  }
3346
- const client = new genai.GoogleGenAI({ apiKey: resolvedApiKey });
3369
+ const sdk = await loadSdk$1();
3370
+ const client = new sdk.GoogleGenAI({ apiKey: resolvedApiKey });
3347
3371
  logger.trace("Initialized Gemini client");
3348
3372
  return client;
3349
3373
  }
3350
3374
  function formatUserMessage$2(message, { data, placeholders } = {}) {
3351
3375
  const content = placeholders?.message === false
3352
3376
  ? message
3353
- : core.placeholders(message, data);
3377
+ : kit.placeholders(message, data);
3354
3378
  return {
3355
3379
  role: "user",
3356
3380
  content,
@@ -3451,13 +3475,13 @@ class GeminiProvider {
3451
3475
  }
3452
3476
 
3453
3477
  // Logger
3454
- const getLogger$1 = () => core.log.lib({ lib: core.JAYPIE.LIB.LLM });
3478
+ const getLogger$1 = () => logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3455
3479
  // Client initialization
3456
3480
  async function initializeClient$1({ apiKey, } = {}) {
3457
3481
  const logger = getLogger$1();
3458
3482
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENAI_API_KEY"));
3459
3483
  if (!resolvedApiKey) {
3460
- throw new core.ConfigurationError("The application could not resolve the requested keys");
3484
+ throw new errors.ConfigurationError("The application could not resolve the requested keys");
3461
3485
  }
3462
3486
  const client = new openai.OpenAI({ apiKey: resolvedApiKey });
3463
3487
  logger.trace("Initialized OpenAI client");
@@ -3466,7 +3490,7 @@ async function initializeClient$1({ apiKey, } = {}) {
3466
3490
  function formatSystemMessage$1(systemPrompt, { data, placeholders } = {}) {
3467
3491
  const content = placeholders?.system === false
3468
3492
  ? systemPrompt
3469
- : core.placeholders(systemPrompt, data);
3493
+ : kit.placeholders(systemPrompt, data);
3470
3494
  return {
3471
3495
  role: "developer",
3472
3496
  content,
@@ -3475,7 +3499,7 @@ function formatSystemMessage$1(systemPrompt, { data, placeholders } = {}) {
3475
3499
  function formatUserMessage$1(message, { data, placeholders } = {}) {
3476
3500
  const content = placeholders?.message === false
3477
3501
  ? message
3478
- : core.placeholders(message, data);
3502
+ : kit.placeholders(message, data);
3479
3503
  return {
3480
3504
  role: "user",
3481
3505
  content,
@@ -3598,15 +3622,29 @@ class OpenAiProvider {
3598
3622
  }
3599
3623
  }
3600
3624
 
3625
+ // SDK loader with caching
3626
+ let cachedSdk = null;
3627
+ async function loadSdk() {
3628
+ if (cachedSdk)
3629
+ return cachedSdk;
3630
+ try {
3631
+ cachedSdk = await import('@openrouter/sdk');
3632
+ return cachedSdk;
3633
+ }
3634
+ catch {
3635
+ throw new errors.ConfigurationError("@openrouter/sdk is required but not installed. Run: npm install @openrouter/sdk");
3636
+ }
3637
+ }
3601
3638
  // Logger
3602
- const getLogger = () => core.log.lib({ lib: core.JAYPIE.LIB.LLM });
3639
+ const getLogger = () => logger.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3603
3640
  // Client initialization
3604
3641
  async function initializeClient({ apiKey, } = {}) {
3605
3642
  const logger = getLogger();
3606
3643
  const resolvedApiKey = apiKey || (await aws.getEnvSecret("OPENROUTER_API_KEY"));
3607
3644
  if (!resolvedApiKey) {
3608
- throw new core.ConfigurationError("The application could not resolve the requested keys");
3645
+ throw new errors.ConfigurationError("The application could not resolve the requested keys");
3609
3646
  }
3647
+ const sdk = await loadSdk();
3610
3648
  const client = new sdk.OpenRouter({ apiKey: resolvedApiKey });
3611
3649
  logger.trace("Initialized OpenRouter client");
3612
3650
  return client;
@@ -3618,7 +3656,7 @@ function getDefaultModel() {
3618
3656
  function formatSystemMessage(systemPrompt, { data, placeholders } = {}) {
3619
3657
  const content = placeholders?.system === false
3620
3658
  ? systemPrompt
3621
- : core.placeholders(systemPrompt, data);
3659
+ : kit.placeholders(systemPrompt, data);
3622
3660
  return {
3623
3661
  role: "system",
3624
3662
  content,
@@ -3627,7 +3665,7 @@ function formatSystemMessage(systemPrompt, { data, placeholders } = {}) {
3627
3665
  function formatUserMessage(message, { data, placeholders } = {}) {
3628
3666
  const content = placeholders?.message === false
3629
3667
  ? message
3630
- : core.placeholders(message, data);
3668
+ : kit.placeholders(message, data);
3631
3669
  return {
3632
3670
  role: "user",
3633
3671
  content,