@jaypie/llm 1.2.23 → 1.2.24

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,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var errors = require('@jaypie/errors');
4
- var log$2 = require('@jaypie/logger');
4
+ var log$1 = require('@jaypie/logger');
5
5
  var v4 = require('zod/v4');
6
6
  var kit = require('@jaypie/kit');
7
7
  var RandomLib = require('random');
@@ -554,8 +554,7 @@ function formatOperateInput(input, options) {
554
554
  return [input];
555
555
  }
556
556
 
557
- const getLogger$5 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
558
- const log$1 = getLogger$5();
557
+ const getLogger$5 = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
559
558
 
560
559
  // Turn policy constants
561
560
  const MAX_TURNS_ABSOLUTE_LIMIT = 72;
@@ -2662,16 +2661,16 @@ function convertContentToOpenRouter(content) {
2662
2661
  }
2663
2662
  // Image content - warn and discard
2664
2663
  if (item.type === exports.LlmMessageType.InputImage) {
2665
- log$2.log.warn("OpenRouter does not support image uploads; image discarded");
2664
+ log$1.log.warn("OpenRouter does not support image uploads; image discarded");
2666
2665
  continue;
2667
2666
  }
2668
2667
  // File/Document content - warn and discard
2669
2668
  if (item.type === exports.LlmMessageType.InputFile) {
2670
- log$2.log.warn({ filename: item.filename }, "OpenRouter does not support file uploads; file discarded");
2669
+ log$1.log.warn({ filename: item.filename }, "OpenRouter does not support file uploads; file discarded");
2671
2670
  continue;
2672
2671
  }
2673
2672
  // Unknown type - warn and skip
2674
- log$2.log.warn({ item }, "Unknown content type for OpenRouter; discarded");
2673
+ log$1.log.warn({ item }, "Unknown content type for OpenRouter; discarded");
2675
2674
  }
2676
2675
  // If no text parts remain, return empty string to avoid empty array
2677
2676
  if (parts.length === 0) {
@@ -3249,7 +3248,7 @@ class XaiAdapter extends OpenAiAdapter {
3249
3248
  const xaiAdapter = new XaiAdapter();
3250
3249
 
3251
3250
  const DEFAULT_TOOL_TYPE = "function";
3252
- const log = log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3251
+ const log = log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
3253
3252
  function logToolMessage(message, context) {
3254
3253
  log.trace.var({ [context.name]: message });
3255
3254
  }
@@ -4055,6 +4054,7 @@ class RetryExecutor {
4055
4054
  * @throws BadGatewayError if all retries are exhausted or error is not retryable
4056
4055
  */
4057
4056
  async execute(operation, options) {
4057
+ const log = getLogger$5();
4058
4058
  let attempt = 0;
4059
4059
  // Persistent guard against stale socket errors (TypeError: terminated).
4060
4060
  // Installed after the first abort and kept alive through subsequent attempts
@@ -4066,7 +4066,7 @@ class RetryExecutor {
4066
4066
  return;
4067
4067
  staleGuard = (reason) => {
4068
4068
  if (isTransientNetworkError(reason)) {
4069
- log$1.trace("Suppressed stale socket error during retry");
4069
+ log.trace("Suppressed stale socket error during retry");
4070
4070
  }
4071
4071
  };
4072
4072
  process.on("unhandledRejection", staleGuard);
@@ -4083,7 +4083,7 @@ class RetryExecutor {
4083
4083
  try {
4084
4084
  const result = await operation(controller.signal);
4085
4085
  if (attempt > 0) {
4086
- log$1.debug(`API call succeeded after ${attempt} retries`);
4086
+ log.debug(`API call succeeded after ${attempt} retries`);
4087
4087
  }
4088
4088
  return result;
4089
4089
  }
@@ -4096,8 +4096,8 @@ class RetryExecutor {
4096
4096
  installGuard();
4097
4097
  // Check if we've exhausted retries
4098
4098
  if (!this.policy.shouldRetry(attempt)) {
4099
- log$1.error(`API call failed after ${this.policy.maxRetries} retries`);
4100
- log$1.var({ error });
4099
+ log.error(`API call failed after ${this.policy.maxRetries} retries`);
4100
+ log.var({ error });
4101
4101
  await this.hookRunner.runOnUnrecoverableError(options.hooks, {
4102
4102
  input: options.context.input,
4103
4103
  options: options.context.options,
@@ -4109,8 +4109,8 @@ class RetryExecutor {
4109
4109
  }
4110
4110
  // Check if error is not retryable
4111
4111
  if (!this.errorClassifier.isRetryable(error)) {
4112
- log$1.error("API call failed with non-retryable error");
4113
- log$1.var({ error });
4112
+ log.error("API call failed with non-retryable error");
4113
+ log.var({ error });
4114
4114
  await this.hookRunner.runOnUnrecoverableError(options.hooks, {
4115
4115
  input: options.context.input,
4116
4116
  options: options.context.options,
@@ -4122,11 +4122,11 @@ class RetryExecutor {
4122
4122
  }
4123
4123
  // Warn if this is an unknown error type
4124
4124
  if (!this.errorClassifier.isKnownError(error)) {
4125
- log$1.warn("API returned unknown error type, will retry");
4126
- log$1.var({ error });
4125
+ log.warn("API returned unknown error type, will retry");
4126
+ log.var({ error });
4127
4127
  }
4128
4128
  const delay = this.policy.getDelayForAttempt(attempt);
4129
- log$1.warn(`API call failed. Retrying in ${delay}ms...`);
4129
+ log.warn(`API call failed. Retrying in ${delay}ms...`);
4130
4130
  await this.hookRunner.runOnRetryableError(options.hooks, {
4131
4131
  input: options.context.input,
4132
4132
  options: options.context.options,
@@ -4192,10 +4192,11 @@ class OperateLoop {
4192
4192
  * Execute the operate loop for multi-turn conversations with tool calling.
4193
4193
  */
4194
4194
  async execute(input, options = {}) {
4195
+ const log = getLogger$5();
4195
4196
  // Log what was passed to operate
4196
- log$1.trace("[operate] Starting operate loop");
4197
- log$1.var({ "operate.input": input });
4198
- log$1.var({ "operate.options": options });
4197
+ log.trace("[operate] Starting operate loop");
4198
+ log.var({ "operate.input": input });
4199
+ log.var({ "operate.options": options });
4199
4200
  // Initialize state
4200
4201
  const state = await this.initializeState(input, options);
4201
4202
  const context = this.createContext(options);
@@ -4304,6 +4305,7 @@ class OperateLoop {
4304
4305
  };
4305
4306
  }
4306
4307
  async executeOneTurn(request, state, context, options) {
4308
+ const log = getLogger$5();
4307
4309
  // Create error classifier from adapter
4308
4310
  const errorClassifier = createErrorClassifier(this.adapter);
4309
4311
  // Create retry executor for this turn
@@ -4315,8 +4317,8 @@ class OperateLoop {
4315
4317
  // Build provider-specific request
4316
4318
  const providerRequest = this.adapter.buildRequest(request);
4317
4319
  // Log what was passed to the model
4318
- log$1.trace("[operate] Calling model");
4319
- log$1.var({ "operate.request": providerRequest });
4320
+ log.trace("[operate] Calling model");
4321
+ log.var({ "operate.request": providerRequest });
4320
4322
  // Execute beforeEachModelRequest hook
4321
4323
  await this.hookRunnerInstance.runBeforeModelRequest(context.hooks, {
4322
4324
  input: state.currentInput,
@@ -4333,8 +4335,8 @@ class OperateLoop {
4333
4335
  hooks: context.hooks,
4334
4336
  });
4335
4337
  // Log what was returned from the model
4336
- log$1.trace("[operate] Model response received");
4337
- log$1.var({ "operate.response": response });
4338
+ log.trace("[operate] Model response received");
4339
+ log.var({ "operate.response": response });
4338
4340
  // Parse response
4339
4341
  const parsed = this.adapter.parseResponse(response, options);
4340
4342
  // Track usage
@@ -4382,7 +4384,7 @@ class OperateLoop {
4382
4384
  toolName: toolCall.name,
4383
4385
  });
4384
4386
  // Call the tool
4385
- log$1.trace(`[operate] Calling tool - ${toolCall.name}`);
4387
+ log.trace(`[operate] Calling tool - ${toolCall.name}`);
4386
4388
  const result = await state.toolkit.call({
4387
4389
  arguments: toolCall.arguments,
4388
4390
  name: toolCall.name,
@@ -4440,13 +4442,13 @@ class OperateLoop {
4440
4442
  };
4441
4443
  const toolResultFormatted = this.adapter.formatToolResult(toolCall, errorResult);
4442
4444
  state.responseBuilder.appendToHistory(toolResultFormatted);
4443
- log$1.error(`Error executing function call ${toolCall.name}`);
4444
- log$1.var({ error });
4445
+ log.error(`Error executing function call ${toolCall.name}`);
4446
+ log.var({ error });
4445
4447
  // Track consecutive errors and stop if threshold reached
4446
4448
  state.consecutiveToolErrors++;
4447
4449
  if (state.consecutiveToolErrors >= MAX_CONSECUTIVE_TOOL_ERRORS) {
4448
4450
  const detail = `Stopped after ${MAX_CONSECUTIVE_TOOL_ERRORS} consecutive tool errors`;
4449
- log$1.warn(detail);
4451
+ log.warn(detail);
4450
4452
  state.responseBuilder.setError({
4451
4453
  detail,
4452
4454
  status: 502,
@@ -4461,7 +4463,7 @@ class OperateLoop {
4461
4463
  if (state.currentTurn >= state.maxTurns) {
4462
4464
  const error = new errors.TooManyRequestsError();
4463
4465
  const detail = `Model requested function call but exceeded ${state.maxTurns} turns`;
4464
- log$1.warn(detail);
4466
+ log.warn(detail);
4465
4467
  state.responseBuilder.setError({
4466
4468
  detail,
4467
4469
  status: error.status,
@@ -4615,6 +4617,7 @@ class StreamLoop {
4615
4617
  * Yields stream chunks as they become available.
4616
4618
  */
4617
4619
  async *execute(input, options = {}) {
4620
+ const log = getLogger$5();
4618
4621
  // Verify adapter supports streaming
4619
4622
  if (!this.adapter.executeStreamRequest) {
4620
4623
  throw new errors.BadGatewayError(`Provider ${this.adapter.name} does not support streaming`);
@@ -4639,7 +4642,7 @@ class StreamLoop {
4639
4642
  if (state.currentTurn >= state.maxTurns) {
4640
4643
  const error = new errors.TooManyRequestsError();
4641
4644
  const detail = `Model requested function call but exceeded ${state.maxTurns} turns`;
4642
- log$1.warn(detail);
4645
+ log.warn(detail);
4643
4646
  yield {
4644
4647
  type: exports.LlmStreamChunkType.Error,
4645
4648
  error: {
@@ -4732,6 +4735,7 @@ class StreamLoop {
4732
4735
  };
4733
4736
  }
4734
4737
  async *executeOneStreamingTurn(request, state, context, options) {
4738
+ const log = getLogger$5();
4735
4739
  // Build provider-specific request
4736
4740
  const providerRequest = this.adapter.buildRequest(request);
4737
4741
  // Execute beforeEachModelRequest hook
@@ -4753,7 +4757,7 @@ class StreamLoop {
4753
4757
  return;
4754
4758
  staleGuard = (reason) => {
4755
4759
  if (isTransientNetworkError(reason)) {
4756
- log$1.trace("Suppressed stale socket error during retry");
4760
+ log.trace("Suppressed stale socket error during retry");
4757
4761
  }
4758
4762
  };
4759
4763
  process.on("unhandledRejection", staleGuard);
@@ -4799,7 +4803,7 @@ class StreamLoop {
4799
4803
  }
4800
4804
  // Stream completed successfully
4801
4805
  if (attempt > 0) {
4802
- log$1.debug(`Stream request succeeded after ${attempt} retries`);
4806
+ log.debug(`Stream request succeeded after ${attempt} retries`);
4803
4807
  }
4804
4808
  break;
4805
4809
  }
@@ -4811,8 +4815,8 @@ class StreamLoop {
4811
4815
  // If chunks were already yielded, we can't transparently retry
4812
4816
  if (chunksYielded) {
4813
4817
  const errorMessage = error instanceof Error ? error.message : String(error);
4814
- log$1.error("Stream failed after partial data was delivered");
4815
- log$1.var({ error });
4818
+ log.error("Stream failed after partial data was delivered");
4819
+ log.var({ error });
4816
4820
  yield {
4817
4821
  type: exports.LlmStreamChunkType.Error,
4818
4822
  error: {
@@ -4826,14 +4830,14 @@ class StreamLoop {
4826
4830
  // Check if we've exhausted retries or error is not retryable
4827
4831
  if (!this.retryPolicy.shouldRetry(attempt) ||
4828
4832
  !this.adapter.isRetryableError(error)) {
4829
- log$1.error(`Stream request failed after ${this.retryPolicy.maxRetries} retries`);
4830
- log$1.var({ error });
4833
+ log.error(`Stream request failed after ${this.retryPolicy.maxRetries} retries`);
4834
+ log.var({ error });
4831
4835
  const errorMessage = error instanceof Error ? error.message : String(error);
4832
4836
  throw new errors.BadGatewayError(errorMessage);
4833
4837
  }
4834
4838
  const delay = this.retryPolicy.getDelayForAttempt(attempt);
4835
- log$1.warn(`Stream request failed. Retrying in ${delay}ms...`);
4836
- log$1.var({ error });
4839
+ log.warn(`Stream request failed. Retrying in ${delay}ms...`);
4840
+ log.var({ error });
4837
4841
  await kit.sleep(delay);
4838
4842
  attempt++;
4839
4843
  }
@@ -4877,6 +4881,7 @@ class StreamLoop {
4877
4881
  return { shouldContinue: false };
4878
4882
  }
4879
4883
  async *processToolCalls(toolCalls, state, context, _options) {
4884
+ const log = getLogger$5();
4880
4885
  for (const toolCall of toolCalls) {
4881
4886
  try {
4882
4887
  // Execute beforeEachTool hook
@@ -4885,7 +4890,7 @@ class StreamLoop {
4885
4890
  toolName: toolCall.name,
4886
4891
  });
4887
4892
  // Call the tool
4888
- log$1.trace(`[stream] Calling tool - ${toolCall.name}`);
4893
+ log.trace(`[stream] Calling tool - ${toolCall.name}`);
4889
4894
  const result = await state.toolkit.call({
4890
4895
  arguments: toolCall.arguments,
4891
4896
  name: toolCall.name,
@@ -4948,13 +4953,13 @@ class StreamLoop {
4948
4953
  call_id: toolCall.callId,
4949
4954
  name: toolCall.name,
4950
4955
  });
4951
- log$1.error(`Error executing function call ${toolCall.name}`);
4952
- log$1.var({ error });
4956
+ log.error(`Error executing function call ${toolCall.name}`);
4957
+ log.var({ error });
4953
4958
  // Track consecutive errors and stop if threshold reached
4954
4959
  state.consecutiveToolErrors++;
4955
4960
  if (state.consecutiveToolErrors >= MAX_CONSECUTIVE_TOOL_ERRORS) {
4956
4961
  const stopDetail = `Stopped after ${MAX_CONSECUTIVE_TOOL_ERRORS} consecutive tool errors`;
4957
- log$1.warn(stopDetail);
4962
+ log.warn(stopDetail);
4958
4963
  yield {
4959
4964
  type: exports.LlmStreamChunkType.Error,
4960
4965
  error: {
@@ -5035,7 +5040,7 @@ async function loadSdk$2() {
5035
5040
  }
5036
5041
  }
5037
5042
  // Logger
5038
- const getLogger$4 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5043
+ const getLogger$4 = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5039
5044
  // Client initialization
5040
5045
  async function initializeClient$4({ apiKey, } = {}) {
5041
5046
  const logger = getLogger$4();
@@ -5074,7 +5079,7 @@ function prepareMessages$3(message, { data, placeholders } = {}) {
5074
5079
  }
5075
5080
  // Basic text completion
5076
5081
  async function createTextCompletion$1(client, messages, model, systemMessage) {
5077
- log$2.log.trace("Using text output (unstructured)");
5082
+ log$1.log.trace("Using text output (unstructured)");
5078
5083
  const params = {
5079
5084
  model,
5080
5085
  messages,
@@ -5083,17 +5088,17 @@ async function createTextCompletion$1(client, messages, model, systemMessage) {
5083
5088
  // Add system instruction if provided
5084
5089
  if (systemMessage) {
5085
5090
  params.system = systemMessage;
5086
- log$2.log.trace(`System message: ${systemMessage.length} characters`);
5091
+ log$1.log.trace(`System message: ${systemMessage.length} characters`);
5087
5092
  }
5088
5093
  const response = await client.messages.create(params);
5089
5094
  const firstContent = response.content[0];
5090
5095
  const text = firstContent && "text" in firstContent ? firstContent.text : "";
5091
- log$2.log.trace(`Assistant reply: ${text.length} characters`);
5096
+ log$1.log.trace(`Assistant reply: ${text.length} characters`);
5092
5097
  return text;
5093
5098
  }
5094
5099
  // Structured output completion
5095
5100
  async function createStructuredCompletion$1(client, messages, model, responseSchema, systemMessage) {
5096
- log$2.log.trace("Using structured output");
5101
+ log$1.log.trace("Using structured output");
5097
5102
  // Get the JSON schema for the response
5098
5103
  const schema = responseSchema instanceof v4.z.ZodType
5099
5104
  ? responseSchema
@@ -5126,7 +5131,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
5126
5131
  if (!schema.parse(result)) {
5127
5132
  throw new Error(`JSON response from Anthropic does not match schema: ${responseText}`);
5128
5133
  }
5129
- log$2.log.trace("Received structured response", { result });
5134
+ log$1.log.trace("Received structured response", { result });
5130
5135
  return result;
5131
5136
  }
5132
5137
  catch {
@@ -5137,7 +5142,7 @@ async function createStructuredCompletion$1(client, messages, model, responseSch
5137
5142
  throw new Error("Failed to parse structured response from Anthropic");
5138
5143
  }
5139
5144
  catch (error) {
5140
- log$2.log.error("Error creating structured completion", { error });
5145
+ log$1.log.error("Error creating structured completion", { error });
5141
5146
  throw error;
5142
5147
  }
5143
5148
  }
@@ -5251,7 +5256,7 @@ async function loadSdk$1() {
5251
5256
  }
5252
5257
  }
5253
5258
  // Logger
5254
- const getLogger$3 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5259
+ const getLogger$3 = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5255
5260
  // Client initialization
5256
5261
  async function initializeClient$3({ apiKey, } = {}) {
5257
5262
  const logger = getLogger$3();
@@ -5402,7 +5407,7 @@ class GeminiProvider {
5402
5407
  }
5403
5408
 
5404
5409
  // Logger
5405
- const getLogger$2 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5410
+ const getLogger$2 = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5406
5411
  // Client initialization
5407
5412
  async function initializeClient$2({ apiKey, } = {}) {
5408
5413
  const logger = getLogger$2();
@@ -5586,7 +5591,7 @@ async function loadSdk() {
5586
5591
  }
5587
5592
  }
5588
5593
  // Logger
5589
- const getLogger$1 = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5594
+ const getLogger$1 = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5590
5595
  // Client initialization
5591
5596
  async function initializeClient$1({ apiKey, } = {}) {
5592
5597
  const logger = getLogger$1();
@@ -5735,7 +5740,7 @@ class OpenRouterProvider {
5735
5740
  }
5736
5741
 
5737
5742
  // Logger
5738
- const getLogger = () => log$2.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5743
+ const getLogger = () => log$1.log.lib({ lib: kit.JAYPIE.LIB.LLM });
5739
5744
  // Client initialization
5740
5745
  async function initializeClient({ apiKey, } = {}) {
5741
5746
  const logger = getLogger();
@@ -5842,7 +5847,7 @@ class Llm {
5842
5847
  let finalModel = model;
5843
5848
  // Legacy: accept "gemini" but warn
5844
5849
  if (providerName === "gemini") {
5845
- log$2.warn(`Provider "gemini" is deprecated, use "${PROVIDER.GEMINI.NAME}" instead`);
5850
+ log$1.warn(`Provider "gemini" is deprecated, use "${PROVIDER.GEMINI.NAME}" instead`);
5846
5851
  }
5847
5852
  if (model) {
5848
5853
  const modelDetermined = determineModelProvider(model);
@@ -5954,7 +5959,7 @@ class Llm {
5954
5959
  }
5955
5960
  catch (error) {
5956
5961
  lastError = error;
5957
- log$2.warn(`Provider ${this._provider} failed`, {
5962
+ log$1.warn(`Provider ${this._provider} failed`, {
5958
5963
  error: lastError.message,
5959
5964
  fallbacksRemaining: fallbackChain.length,
5960
5965
  });
@@ -5974,7 +5979,7 @@ class Llm {
5974
5979
  }
5975
5980
  catch (error) {
5976
5981
  lastError = error;
5977
- log$2.warn(`Fallback provider ${fallbackConfig.provider} failed`, {
5982
+ log$1.warn(`Fallback provider ${fallbackConfig.provider} failed`, {
5978
5983
  error: lastError.message,
5979
5984
  fallbacksRemaining: fallbackChain.length - attempts + 1,
5980
5985
  });
@@ -6143,16 +6148,17 @@ const roll = {
6143
6148
  },
6144
6149
  type: "function",
6145
6150
  call: ({ number = 1, sides = 6 } = {}) => {
6151
+ const log = getLogger$5();
6146
6152
  const rng = random$1();
6147
6153
  const rolls = [];
6148
6154
  let total = 0;
6149
6155
  const parsedNumber = tryParseNumber(number, {
6150
6156
  defaultValue: 1,
6151
- warnFunction: log$1.warn,
6157
+ warnFunction: log.warn,
6152
6158
  });
6153
6159
  const parsedSides = tryParseNumber(sides, {
6154
6160
  defaultValue: 6,
6155
- warnFunction: log$1.warn,
6161
+ warnFunction: log.warn,
6156
6162
  });
6157
6163
  for (let i = 0; i < parsedNumber; i++) {
6158
6164
  const rollValue = rng({ min: 1, max: parsedSides, integer: true });