@broberg/ai-sdk 0.23.0 → 0.24.0

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
@@ -2017,8 +2017,8 @@ declare const falStubAdapter: ProviderAdapter;
2017
2017
  * wires the live adapters. */
2018
2018
  declare const stubProviders: Record<string, ProviderAdapter>;
2019
2019
 
2020
- declare const VERSION: "0.23.0";
2021
- declare const SDK_TAG: "@broberg/ai-sdk@0.23.0";
2020
+ declare const VERSION: "0.24.0";
2021
+ declare const SDK_TAG: "@broberg/ai-sdk@0.24.0";
2022
2022
 
2023
2023
  /** Built-in defaults. Every entry is overridable via AiConfig.defaults or a
2024
2024
  * per-call override.
package/dist/index.js CHANGED
@@ -2571,6 +2571,78 @@ var aiConfigSchema = z.object({
2571
2571
  availability: availabilitySchema.optional()
2572
2572
  });
2573
2573
 
2574
+ // src/version.ts
2575
+ var VERSION = "0.24.0";
2576
+ var SDK_TAG = "@broberg/ai-sdk@0.24.0";
2577
+
2578
+ // src/cost/sinks/upmetrics.ts
2579
+ function upmetricsSink(config) {
2580
+ const doFetch = config.fetch ?? fetch;
2581
+ const url = `${config.baseUrl.replace(/\/$/, "")}/api/agent`;
2582
+ return {
2583
+ async record(usage) {
2584
+ try {
2585
+ const startedAt = usage.ts || (/* @__PURE__ */ new Date()).toISOString();
2586
+ const endedAt = new Date(
2587
+ new Date(startedAt).getTime() + (usage.latencyMs || 0)
2588
+ ).toISOString();
2589
+ const agentKind = config.agentKind ?? (usage.capability === "embedding" ? "embedding" : "chatbot");
2590
+ const body = {
2591
+ mode: "record",
2592
+ agent_kind: agentKind,
2593
+ agent_name: config.agentName,
2594
+ provider: usage.provider,
2595
+ model: usage.model,
2596
+ status: "success",
2597
+ input_tokens: usage.inputTokens,
2598
+ output_tokens: usage.outputTokens,
2599
+ cache_read_tokens: usage.cacheReadTokens,
2600
+ cache_creation_tokens: usage.cacheCreationTokens,
2601
+ cost_usd: usage.costUsd,
2602
+ duration_ms: usage.latencyMs,
2603
+ started_at: startedAt,
2604
+ ended_at: endedAt,
2605
+ tags: {
2606
+ // Consumer attribution labels (e.g. tenantId) ride in tags so no new
2607
+ // top-level field risks the strict-shape ingest schema (F011). The
2608
+ // SDK-owned keys win — a label can never clobber capability/transport/sdk.
2609
+ ...usage.labels,
2610
+ capability: usage.capability,
2611
+ transport: usage.transport,
2612
+ sdk: SDK_TAG
2613
+ }
2614
+ };
2615
+ if (usage.tier !== void 0) body.tier = usage.tier;
2616
+ if (usage.purpose !== void 0) body.purpose = usage.purpose;
2617
+ if (usage.toolCalls) {
2618
+ body.tool_calls = usage.toolCalls.map((t) => ({
2619
+ name: t.name,
2620
+ count: t.count,
2621
+ error_count: t.errorCount ?? 0
2622
+ }));
2623
+ }
2624
+ void config.complianceMode;
2625
+ const res = await doFetch(url, {
2626
+ method: "POST",
2627
+ headers: {
2628
+ "content-type": "application/json",
2629
+ "X-Upmetrics-Key": config.apiKey
2630
+ },
2631
+ body: JSON.stringify(body)
2632
+ });
2633
+ if (!res.ok) {
2634
+ const text = await res.text().catch(() => "");
2635
+ config.onError?.(
2636
+ new Error(`upmetricsSink: ingest returned ${res.status}: ${text.slice(0, 200)}`)
2637
+ );
2638
+ }
2639
+ } catch (err) {
2640
+ config.onError?.(err);
2641
+ }
2642
+ }
2643
+ };
2644
+ }
2645
+
2574
2646
  // src/client.ts
2575
2647
  var DEFAULT_IMAGE_SPEC = {
2576
2648
  provider: "fal",
@@ -2608,9 +2680,20 @@ var DEFAULT_MODERATION_SPEC = { provider: "mistral", model: "mistral-moderation-
2608
2680
  var DEFAULT_PODCAST_SPEC = { provider: "elevenlabs", model: "eleven_v3", transport: "http" };
2609
2681
  var DEFAULT_TTS_SPEC = { provider: "elevenlabs", model: "eleven_multilingual_v2", transport: "http" };
2610
2682
  var DEFAULT_BATCH_SPEC = { provider: "mistral", model: "mistral-small-latest", transport: "http" };
2683
+ function defaultCostSink() {
2684
+ const apiKey = process.env.UPMETRICS_API_KEY;
2685
+ if (!apiKey) return void 0;
2686
+ return upmetricsSink({
2687
+ baseUrl: process.env.UPMETRICS_BASE_URL ?? "https://upmetrics.org",
2688
+ apiKey,
2689
+ agentName: process.env.UPMETRICS_AGENT_NAME ?? process.env.npm_package_name ?? "unknown",
2690
+ complianceMode: process.env.UPMETRICS_COMPLIANCE === "1"
2691
+ });
2692
+ }
2611
2693
  function createAI(config = {}) {
2612
2694
  const cfg = aiConfigSchema.parse(config);
2613
2695
  const providers = cfg.providers ?? defaultProviders;
2696
+ const costSink = cfg.costSink ?? defaultCostSink();
2614
2697
  const budget = cfg.budget ? new BudgetGuard(cfg.budget) : void 0;
2615
2698
  const estTokens = (s) => Math.ceil(s.length / 4);
2616
2699
  async function preflight(spec, estInTokens, estOutTokens) {
@@ -2639,9 +2722,9 @@ function createAI(config = {}) {
2639
2722
  return usage;
2640
2723
  }
2641
2724
  async function report(usage) {
2642
- if (!cfg.costSink) return;
2725
+ if (!costSink) return;
2643
2726
  try {
2644
- await cfg.costSink.record(usage);
2727
+ await costSink.record(usage);
2645
2728
  } catch {
2646
2729
  }
2647
2730
  }
@@ -3170,10 +3253,6 @@ var stubProviders = {
3170
3253
  fal: falStubAdapter
3171
3254
  };
3172
3255
 
3173
- // src/version.ts
3174
- var VERSION = "0.23.0";
3175
- var SDK_TAG = "@broberg/ai-sdk@0.23.0";
3176
-
3177
3256
  // src/availability/refresh.ts
3178
3257
  var NOT_REFRESHED = { refreshed: false, checked: 0, markedUnavailable: [] };
3179
3258
  var DEFAULT_TTL_MS = 60 * 60 * 1e3;
@@ -3263,74 +3342,6 @@ function multiSink(sinks) {
3263
3342
  };
3264
3343
  }
3265
3344
 
3266
- // src/cost/sinks/upmetrics.ts
3267
- function upmetricsSink(config) {
3268
- const doFetch = config.fetch ?? fetch;
3269
- const url = `${config.baseUrl.replace(/\/$/, "")}/api/agent`;
3270
- return {
3271
- async record(usage) {
3272
- try {
3273
- const startedAt = usage.ts || (/* @__PURE__ */ new Date()).toISOString();
3274
- const endedAt = new Date(
3275
- new Date(startedAt).getTime() + (usage.latencyMs || 0)
3276
- ).toISOString();
3277
- const agentKind = config.agentKind ?? (usage.capability === "embedding" ? "embedding" : "chatbot");
3278
- const body = {
3279
- mode: "record",
3280
- agent_kind: agentKind,
3281
- agent_name: config.agentName,
3282
- provider: usage.provider,
3283
- model: usage.model,
3284
- status: "success",
3285
- input_tokens: usage.inputTokens,
3286
- output_tokens: usage.outputTokens,
3287
- cache_read_tokens: usage.cacheReadTokens,
3288
- cache_creation_tokens: usage.cacheCreationTokens,
3289
- cost_usd: usage.costUsd,
3290
- duration_ms: usage.latencyMs,
3291
- started_at: startedAt,
3292
- ended_at: endedAt,
3293
- tags: {
3294
- // Consumer attribution labels (e.g. tenantId) ride in tags so no new
3295
- // top-level field risks the strict-shape ingest schema (F011). The
3296
- // SDK-owned keys win — a label can never clobber capability/transport/sdk.
3297
- ...usage.labels,
3298
- capability: usage.capability,
3299
- transport: usage.transport,
3300
- sdk: SDK_TAG
3301
- }
3302
- };
3303
- if (usage.tier !== void 0) body.tier = usage.tier;
3304
- if (usage.purpose !== void 0) body.purpose = usage.purpose;
3305
- if (usage.toolCalls) {
3306
- body.tool_calls = usage.toolCalls.map((t) => ({
3307
- name: t.name,
3308
- count: t.count,
3309
- error_count: t.errorCount ?? 0
3310
- }));
3311
- }
3312
- void config.complianceMode;
3313
- const res = await doFetch(url, {
3314
- method: "POST",
3315
- headers: {
3316
- "content-type": "application/json",
3317
- "X-Upmetrics-Key": config.apiKey
3318
- },
3319
- body: JSON.stringify(body)
3320
- });
3321
- if (!res.ok) {
3322
- const text = await res.text().catch(() => "");
3323
- config.onError?.(
3324
- new Error(`upmetricsSink: ingest returned ${res.status}: ${text.slice(0, 200)}`)
3325
- );
3326
- }
3327
- } catch (err) {
3328
- config.onError?.(err);
3329
- }
3330
- }
3331
- };
3332
- }
3333
-
3334
3345
  // src/cost/sinks/discord.ts
3335
3346
  function discordSink(config) {
3336
3347
  const doFetch = config.fetch ?? fetch;