@broberg/ai-sdk 0.62.0 → 0.62.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.d.ts CHANGED
@@ -2334,8 +2334,8 @@ declare const falStubAdapter: ProviderAdapter;
2334
2334
  * wires the live adapters. */
2335
2335
  declare const stubProviders: Record<string, ProviderAdapter>;
2336
2336
 
2337
- declare const VERSION: "0.62.0";
2338
- declare const SDK_TAG: "@broberg/ai-sdk@0.62.0";
2337
+ declare const VERSION: "0.62.1";
2338
+ declare const SDK_TAG: "@broberg/ai-sdk@0.62.1";
2339
2339
 
2340
2340
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
2341
2341
  * per-call override.
package/dist/index.js CHANGED
@@ -185,6 +185,9 @@ function classifyFailure(err) {
185
185
  return { errorCode: "unknown", errorKind: "other" };
186
186
  }
187
187
 
188
+ // src/search/index.ts
189
+ import { createHash } from "crypto";
190
+
188
191
  // src/search/types.ts
189
192
  var SearchKeyMissingError = class extends Error {
190
193
  constructor(provider, envVar) {
@@ -270,6 +273,15 @@ var sharedCapStore = /* @__PURE__ */ (() => {
270
273
  getSpent: (k) => m.get(k) ?? 0
271
274
  };
272
275
  })();
276
+ var COOLDOWN_MS = 10 * 6e4;
277
+ var COOLDOWN_STATUS = /* @__PURE__ */ new Set(["401", "402", "403"]);
278
+ var coolingUntil = /* @__PURE__ */ new Map();
279
+ function cooldownKey(provider, opts) {
280
+ const c = opts.credentials ?? {};
281
+ const env = opts.byok ? {} : process.env;
282
+ const secret = provider === "brave" ? c.braveApiKey ?? env.BRAVE_API_KEY : provider === "tavily" ? c.tavilyApiKey ?? env.TAVILY_API_KEY : `${c.cloudflareAccountId ?? env.CLOUDFLARE_ACCOUNT_ID}:${c.cloudflareApiToken ?? env.CLOUDFLARE_API_TOKEN}`;
283
+ return `${provider}:${createHash("sha256").update(String(secret)).digest("hex").slice(0, 16)}`;
284
+ }
273
285
  var copenhagenDay = (d) => new Intl.DateTimeFormat("en-CA", { timeZone: "Europe/Copenhagen" }).format(d);
274
286
  async function callProvider(provider, req, opts) {
275
287
  const f = opts.fetch ?? fetch;
@@ -325,8 +337,11 @@ async function search(req, opts = {}) {
325
337
  const tenant = req.labels?.tenantId ?? "_";
326
338
  const day = copenhagenDay((opts.now ?? (() => /* @__PURE__ */ new Date()))());
327
339
  const capKey = JSON.stringify([tenant, day]);
340
+ const nowMs = () => (opts.now ?? (() => /* @__PURE__ */ new Date()))().getTime();
328
341
  let lastErr;
329
- for (const provider of chain) {
342
+ for (const [i, provider] of chain.entries()) {
343
+ const coolKey = cooldownKey(provider, opts);
344
+ if (i < chain.length - 1 && (coolingUntil.get(coolKey) ?? 0) > nowMs()) continue;
330
345
  const key = searchCacheKey(provider, req);
331
346
  const hit = cache?.get(key);
332
347
  if (hit) return { items: hit.items, meta: { ...hit.meta, cached: true, costUsd: 0, latencyMs: 0 } };
@@ -343,6 +358,7 @@ async function search(req, opts = {}) {
343
358
  items: dedupe(out.items),
344
359
  meta: { provider, latencyMs, cached: false, costUsd: SEARCH_PRICE_USD[provider], ...out.requestId ? { requestId: out.requestId } : {} }
345
360
  };
361
+ coolingUntil.delete(coolKey);
346
362
  cache?.set(key, result, ttl);
347
363
  try {
348
364
  await sink?.record(usageRow(provider, req, result.meta.costUsd, latencyMs));
@@ -352,6 +368,7 @@ async function search(req, opts = {}) {
352
368
  } catch (e) {
353
369
  lastErr = e;
354
370
  await capStore.release(capKey, price);
371
+ if (COOLDOWN_STATUS.has(classifyFailure(e).errorCode)) coolingUntil.set(coolKey, nowMs() + COOLDOWN_MS);
355
372
  if (sink?.recordFailure) {
356
373
  try {
357
374
  await sink.recordFailure({
@@ -3899,8 +3916,8 @@ var aiConfigSchema = z.object({
3899
3916
  import { randomUUID } from "crypto";
3900
3917
 
3901
3918
  // src/version.ts
3902
- var VERSION = "0.62.0";
3903
- var SDK_TAG = "@broberg/ai-sdk@0.62.0";
3919
+ var VERSION = "0.62.1";
3920
+ var SDK_TAG = "@broberg/ai-sdk@0.62.1";
3904
3921
 
3905
3922
  // src/cost/sinks/upmetrics.ts
3906
3923
  function isRetryableStatus(status) {