@jaypie/llm 1.2.11 → 1.2.12

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.
@@ -1128,17 +1128,24 @@ class AnthropicAdapter extends BaseProviderAdapter {
1128
1128
  //
1129
1129
  // API Execution
1130
1130
  //
1131
- async executeRequest(client, request) {
1131
+ async executeRequest(client, request, signal) {
1132
1132
  const anthropic = client;
1133
- return (await anthropic.messages.create(request));
1133
+ try {
1134
+ return (await anthropic.messages.create(request, signal ? { signal } : undefined));
1135
+ }
1136
+ catch (error) {
1137
+ if (signal?.aborted)
1138
+ return undefined;
1139
+ throw error;
1140
+ }
1134
1141
  }
1135
- async *executeStreamRequest(client, request) {
1142
+ async *executeStreamRequest(client, request, signal) {
1136
1143
  const anthropic = client;
1137
1144
  const streamRequest = {
1138
1145
  ...request,
1139
1146
  stream: true,
1140
1147
  };
1141
- const stream = await anthropic.messages.create(streamRequest);
1148
+ const stream = await anthropic.messages.create(streamRequest, signal ? { signal } : undefined);
1142
1149
  // Track current tool call being built
1143
1150
  let currentToolCall = null;
1144
1151
  // Track usage for final chunk
@@ -1568,19 +1575,26 @@ class GeminiAdapter extends BaseProviderAdapter {
1568
1575
  //
1569
1576
  // API Execution
1570
1577
  //
1571
- async executeRequest(client, request) {
1578
+ async executeRequest(client, request, signal) {
1572
1579
  const genAI = client;
1573
1580
  const geminiRequest = request;
1574
- // Cast config to any to bypass strict type checking between our internal types
1575
- // and the SDK's types. The SDK will validate at runtime.
1576
- const response = await genAI.models.generateContent({
1577
- model: geminiRequest.model,
1578
- contents: geminiRequest.contents,
1579
- config: geminiRequest.config,
1580
- });
1581
- return response;
1581
+ try {
1582
+ // Cast config to any to bypass strict type checking between our internal types
1583
+ // and the SDK's types. The SDK will validate at runtime.
1584
+ const response = await genAI.models.generateContent({
1585
+ model: geminiRequest.model,
1586
+ contents: geminiRequest.contents,
1587
+ config: geminiRequest.config,
1588
+ });
1589
+ return response;
1590
+ }
1591
+ catch (error) {
1592
+ if (signal?.aborted)
1593
+ return undefined;
1594
+ throw error;
1595
+ }
1582
1596
  }
1583
- async *executeStreamRequest(client, request) {
1597
+ async *executeStreamRequest(client, request, signal) {
1584
1598
  const genAI = client;
1585
1599
  const geminiRequest = request;
1586
1600
  // Use generateContentStream for streaming
@@ -2230,19 +2244,26 @@ class OpenAiAdapter extends BaseProviderAdapter {
2230
2244
  //
2231
2245
  // API Execution
2232
2246
  //
2233
- async executeRequest(client, request) {
2247
+ async executeRequest(client, request, signal) {
2234
2248
  const openai = client;
2235
- // @ts-expect-error OpenAI SDK types don't match our request format exactly
2236
- return await openai.responses.create(request);
2249
+ try {
2250
+ // @ts-expect-error OpenAI SDK types don't match our request format exactly
2251
+ return await openai.responses.create(request, signal ? { signal } : undefined);
2252
+ }
2253
+ catch (error) {
2254
+ if (signal?.aborted)
2255
+ return undefined;
2256
+ throw error;
2257
+ }
2237
2258
  }
2238
- async *executeStreamRequest(client, request) {
2259
+ async *executeStreamRequest(client, request, signal) {
2239
2260
  const openai = client;
2240
2261
  const baseRequest = request;
2241
2262
  const streamRequest = {
2242
2263
  ...baseRequest,
2243
2264
  stream: true,
2244
2265
  };
2245
- const stream = await openai.responses.create(streamRequest);
2266
+ const stream = await openai.responses.create(streamRequest, signal ? { signal } : undefined);
2246
2267
  // Track current function call being built
2247
2268
  let currentFunctionCall = null;
2248
2269
  // Track usage for final chunk
@@ -2676,14 +2697,21 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2676
2697
  async executeRequest(client, request, signal) {
2677
2698
  const openRouter = client;
2678
2699
  const openRouterRequest = request;
2679
- const response = await openRouter.chat.send({
2680
- model: openRouterRequest.model,
2681
- messages: openRouterRequest.messages,
2682
- tools: openRouterRequest.tools,
2683
- toolChoice: openRouterRequest.tool_choice,
2684
- user: openRouterRequest.user,
2685
- }, signal ? { signal } : undefined);
2686
- return response;
2700
+ try {
2701
+ const response = await openRouter.chat.send({
2702
+ model: openRouterRequest.model,
2703
+ messages: openRouterRequest.messages,
2704
+ tools: openRouterRequest.tools,
2705
+ toolChoice: openRouterRequest.tool_choice,
2706
+ user: openRouterRequest.user,
2707
+ }, signal ? { signal } : undefined);
2708
+ return response;
2709
+ }
2710
+ catch (error) {
2711
+ if (signal?.aborted)
2712
+ return undefined;
2713
+ throw error;
2714
+ }
2687
2715
  }
2688
2716
  async *executeStreamRequest(client, request, signal) {
2689
2717
  const openRouter = client;
@@ -3970,7 +3998,19 @@ class RetryExecutor {
3970
3998
  providerRequest: options.context.providerRequest,
3971
3999
  error,
3972
4000
  });
3973
- await kit.sleep(delay);
4001
+ // Guard against stale socket errors that fire during sleep
4002
+ const staleHandler = (reason) => {
4003
+ if (isTransientNetworkError(reason)) {
4004
+ log$1.trace("Suppressed stale socket error during retry sleep");
4005
+ }
4006
+ };
4007
+ process.on("unhandledRejection", staleHandler);
4008
+ try {
4009
+ await kit.sleep(delay);
4010
+ }
4011
+ finally {
4012
+ process.removeListener("unhandledRejection", staleHandler);
4013
+ }
3974
4014
  attempt++;
3975
4015
  }
3976
4016
  }
@@ -4614,7 +4654,19 @@ class StreamLoop {
4614
4654
  const delay = this.retryPolicy.getDelayForAttempt(attempt);
4615
4655
  log$1.warn(`Stream request failed. Retrying in ${delay}ms...`);
4616
4656
  log$1.var({ error });
4617
- await kit.sleep(delay);
4657
+ // Guard against stale socket errors that fire during sleep
4658
+ const staleHandler = (reason) => {
4659
+ if (isTransientNetworkError(reason)) {
4660
+ log$1.trace("Suppressed stale socket error during retry sleep");
4661
+ }
4662
+ };
4663
+ process.on("unhandledRejection", staleHandler);
4664
+ try {
4665
+ await kit.sleep(delay);
4666
+ }
4667
+ finally {
4668
+ process.removeListener("unhandledRejection", staleHandler);
4669
+ }
4618
4670
  attempt++;
4619
4671
  }
4620
4672
  }