@wrongstack/providers 0.260.0 → 0.265.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.
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { expectDefined, ProviderError, StreamHangError, safeParse, sanitizeJsonString, WrongStackError, ERROR_CODES } from '@wrongstack/core';
2
2
  import { Readable } from 'stream';
3
+ import { toErrorMessage } from '@wrongstack/core/utils';
3
4
  import { randomUUID } from 'crypto';
4
5
 
5
6
  var __defProp = Object.defineProperty;
@@ -536,8 +537,11 @@ function normalizeGemini(stop) {
536
537
  }
537
538
 
538
539
  // src/tool-format/to-anthropic.ts
540
+ var _cache = /* @__PURE__ */ new WeakMap();
539
541
  function toolsToAnthropic(tools) {
540
- return tools.map((t) => ({
542
+ const hit = _cache.get(tools);
543
+ if (hit) return hit;
544
+ const result = tools.map((t) => ({
541
545
  name: t.name,
542
546
  description: t.description,
543
547
  input_schema: t.inputSchema ?? {
@@ -545,6 +549,8 @@ function toolsToAnthropic(tools) {
545
549
  properties: {}
546
550
  }
547
551
  }));
552
+ _cache.set(tools, result);
553
+ return result;
548
554
  }
549
555
 
550
556
  // src/stream-debug-state.ts
@@ -657,9 +663,9 @@ var WireAdapter = class {
657
663
  httpRes = raw;
658
664
  } catch (err) {
659
665
  if (opts.signal.aborted) throw err;
660
- throw new ProviderError(err instanceof Error ? err.message : String(err), 0, true, this.id, {
666
+ throw new ProviderError(toErrorMessage(err), 0, true, this.id, {
661
667
  cause: err,
662
- body: { message: err instanceof Error ? err.message : String(err) }
668
+ body: { message: toErrorMessage(err) }
663
669
  });
664
670
  }
665
671
  if (!httpRes.ok) {
@@ -1195,18 +1201,23 @@ async function* parseGoogleStream(body, fallbackModel) {
1195
1201
  init_tool_input();
1196
1202
 
1197
1203
  // src/tool-format/to-openai.ts
1204
+ var _cache2 = /* @__PURE__ */ new WeakMap();
1198
1205
  function toolsToOpenAI(tools) {
1199
- return tools.map((t) => ({
1206
+ const hit = _cache2.get(tools);
1207
+ if (hit) return hit;
1208
+ const result = tools.map((t) => ({
1200
1209
  type: "function",
1201
1210
  function: {
1202
1211
  name: t.name,
1203
- description: t.description,
1212
+ description: t.description ?? "",
1204
1213
  parameters: t.inputSchema ?? {
1205
1214
  type: "object",
1206
1215
  properties: {}
1207
1216
  }
1208
1217
  }
1209
1218
  }));
1219
+ _cache2.set(tools, result);
1220
+ return result;
1210
1221
  }
1211
1222
  function messagesToOpenAI(system, messages, opts = {}) {
1212
1223
  const emptyContentMode = opts.emptyToolCallContent ?? "empty_string";
@@ -2441,10 +2452,17 @@ async function buildProviderFactoriesFromRegistry(opts) {
2441
2452
  }
2442
2453
  return factories;
2443
2454
  }
2455
+ function resolveActiveKey(cfg) {
2456
+ if (Array.isArray(cfg.apiKeys) && cfg.apiKeys.length > 0) {
2457
+ const active = cfg.activeKey ? cfg.apiKeys.find((k) => k.label === cfg.activeKey) : void 0;
2458
+ return (active ?? cfg.apiKeys[0])?.apiKey;
2459
+ }
2460
+ return cfg.apiKey && cfg.apiKey.length > 0 ? cfg.apiKey : void 0;
2461
+ }
2444
2462
  function makeProvider(p, cfg) {
2445
2463
  const family = cfg.family ?? p.family;
2446
2464
  const envVars = cfg.envVars && cfg.envVars.length > 0 ? cfg.envVars : p.envVars;
2447
- const apiKey = cfg.apiKey ?? readFromEnv(envVars);
2465
+ const apiKey = resolveActiveKey(cfg) ?? readFromEnv(envVars);
2448
2466
  if (!apiKey && family !== "unsupported") {
2449
2467
  throw new Error(
2450
2468
  `Provider "${p.id}" requires an API key. Set ${envVars.join(" or ") || "apiKey in config"} or run \`wstack auth ${p.id}\`.`
@@ -2509,7 +2527,8 @@ function readFromEnv(vars) {
2509
2527
  return void 0;
2510
2528
  }
2511
2529
  function requireKey(cfg) {
2512
- if (cfg.apiKey) return cfg.apiKey;
2530
+ const key = resolveActiveKey(cfg);
2531
+ if (key) return key;
2513
2532
  throw new Error("Provider config requires apiKey (or set the corresponding env var).");
2514
2533
  }
2515
2534
  function validateQuirks(providerId, quirks) {