@broberg/ai-sdk 0.62.2 → 0.63.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
@@ -10,7 +10,7 @@ import {
10
10
  resolveModel,
11
11
  resolveTier,
12
12
  setAvailability
13
- } from "./chunk-KWHOVIZO.js";
13
+ } from "./chunk-2BNVXTGO.js";
14
14
  import {
15
15
  DEFAULT_CLIP_SEC,
16
16
  MEDIA_PRICING,
@@ -20,11 +20,11 @@ import {
20
20
  listMediaPrices,
21
21
  pricingFreshness,
22
22
  pricingGeneratedAt
23
- } from "./chunk-DLBOYUNF.js";
23
+ } from "./chunk-FPW2WGJS.js";
24
24
  import {
25
25
  PRICING,
26
26
  getPrice
27
- } from "./chunk-D332SR5V.js";
27
+ } from "./chunk-BWBRED4O.js";
28
28
 
29
29
  // src/search/brave.ts
30
30
  async function braveSearch(req, apiKey, f) {
@@ -3910,6 +3910,9 @@ var aiConfigSchema = z.object({
3910
3910
  * to opt OUT explicitly — for a consumer that already reports its own costs
3911
3911
  * and would otherwise be counted twice (F034.3). An object is used as-is. */
3912
3912
  costSink: z.custom().nullable().optional(),
3913
+ /** F034.4 — who is calling, as Upmetrics groups it (e.g. "cms"). Required when
3914
+ * `costSink` is omitted, unless UPMETRICS_AGENT_NAME is set. */
3915
+ agentName: z.string().optional(),
3913
3916
  budget: budgetSchema.optional(),
3914
3917
  availability: availabilitySchema.optional()
3915
3918
  });
@@ -3918,8 +3921,8 @@ var aiConfigSchema = z.object({
3918
3921
  import { randomUUID } from "crypto";
3919
3922
 
3920
3923
  // src/version.ts
3921
- var VERSION = "0.62.2";
3922
- var SDK_TAG = "@broberg/ai-sdk@0.62.2";
3924
+ var VERSION = "0.63.1";
3925
+ var SDK_TAG = "@broberg/ai-sdk@0.63.1";
3923
3926
 
3924
3927
  // src/cost/sinks/upmetrics.ts
3925
3928
  function isRetryableStatus(status) {
@@ -3929,6 +3932,7 @@ var MAX_QUEUE = 30;
3929
3932
  var MAX_ATTEMPTS = 5;
3930
3933
  var MAX_BACKOFF_MS = 3e4;
3931
3934
  function upmetricsSink(config) {
3935
+ if (!config.agentName?.trim()) throw new Error("upmetricsSink(): agentName is required (e.g. your repo name) \u2014 anonymous cost rows are not allowed");
3932
3936
  const doFetch = config.fetch ?? fetch;
3933
3937
  const url = `${config.baseUrl.replace(/\/$/, "")}/api/agent`;
3934
3938
  const retry = config.retry ?? true;
@@ -4258,21 +4262,28 @@ var ROUTED_SPECS = [
4258
4262
  DEFAULT_BATCH_SPEC,
4259
4263
  DEFAULT_TRANSCRIBE_SPEC
4260
4264
  ];
4261
- function defaultCostSink() {
4265
+ function defaultCostSink(agentName) {
4262
4266
  const apiKey = process.env.UPMETRICS_API_KEY;
4263
4267
  if (!apiKey) return void 0;
4264
4268
  return upmetricsSink({
4265
4269
  baseUrl: process.env.UPMETRICS_BASE_URL ?? "https://upmetrics.org",
4266
4270
  apiKey,
4267
- agentName: process.env.UPMETRICS_AGENT_NAME ?? process.env.npm_package_name ?? "unknown",
4271
+ agentName,
4268
4272
  complianceMode: process.env.UPMETRICS_COMPLIANCE === "1"
4269
4273
  });
4270
4274
  }
4275
+ function requireAgentName(configured) {
4276
+ const name = (configured ?? process.env.UPMETRICS_AGENT_NAME ?? "").trim();
4277
+ if (name) return name;
4278
+ throw new Error(
4279
+ 'createAI(): no agent name \u2014 every AI call must say who made it (owner order 2026-10-15). Set UPMETRICS_AGENT_NAME=<your repo> (production AND test env), or pass createAI({ agentName: "<your repo>" }). Not needed with an explicit costSink, or costSink: null.'
4280
+ );
4281
+ }
4271
4282
  function createAI(config = {}) {
4272
4283
  const cfg = aiConfigSchema.parse(config);
4273
4284
  if (cfg.byok) assertByokProviders(cfg.providers);
4274
4285
  const providers = cfg.providers ?? defaultProviders;
4275
- const costSink = cfg.costSink === void 0 ? defaultCostSink() : cfg.costSink ?? void 0;
4286
+ const costSink = cfg.costSink === void 0 ? defaultCostSink(requireAgentName(cfg.agentName)) : cfg.costSink ?? void 0;
4276
4287
  const budget = cfg.budget ? new BudgetGuard(cfg.budget) : void 0;
4277
4288
  const estTokens = (s) => Math.ceil(s.length / 4);
4278
4289
  async function preflight(spec, estInTokens, estOutTokens) {
@@ -4870,7 +4881,7 @@ async function getJson(f, url, headers) {
4870
4881
  }
4871
4882
  function per1M(v) {
4872
4883
  if (v == null) return void 0;
4873
- const n = Number(v) * 1e6;
4884
+ const n = Number((Number(v) * 1e6).toPrecision(12));
4874
4885
  return Number.isFinite(n) && n >= 0 ? n : void 0;
4875
4886
  }
4876
4887
  async function fetchOpenRouterCatalogue(opts = {}) {