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