@genesislcap/foundation-ai 15.3.2 → 15.3.3-alpha-4899ddd.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.
@@ -378,6 +378,102 @@ declare interface AnthropicTransportConfig {
378
378
  maxTokens?: number;
379
379
  }
380
380
 
381
+ /**
382
+ * The vendors whose spend the ai-service proxy meters — i.e. the only vendors
383
+ * that can raise a {@link BudgetExhaustedError}, and therefore the only ones a
384
+ * 402's `otherVendorAvailable: false` is a statement about.
385
+ *
386
+ * Deliberately narrower than the keys of {@link VENDOR_LABELS}, and it must stay
387
+ * exactly the proxy's own list — `VENDORS` in ai-service's `utils/aiVendor.js`.
388
+ * A vendor listed here that the proxy does not meter gets walled by the
389
+ * `otherVendorAvailable: false` sweep on a verdict that says nothing about it,
390
+ * and if it is reachable, `blocked` then derives true and locks a composer with
391
+ * headroom left.
392
+ *
393
+ * Two vendors are excluded for that reason:
394
+ * - `chrome` runs on-device — no pot to exhaust, and free.
395
+ * - `openai` is **not budgeted by the proxy**. ai-service rejects it up front
396
+ * with `400 UNSUPPORTED_PROVIDER` (`SUPPORTED_PROVIDERS` in `aiVendor.js`), no
397
+ * `MODEL_PRICING` row names it, and `otherVendorAvailable` is computed over
398
+ * Anthropic and Gemini alone. So it can never raise a 402, and a 402 is never
399
+ * a statement about it.
400
+ *
401
+ * @beta
402
+ */
403
+ export declare const BUDGETED_VENDORS: readonly AIProviderType[];
404
+
405
+ /**
406
+ * Thrown when the AI-spend budget for the caller (user, tenant, or project) is
407
+ * exhausted and the ai-service proxy refuses the request outright — HTTP `402`
408
+ * with `code: 'BUDGET_EXCEEDED'`, in either the legacy JSON or the NDJSON framed
409
+ * mode.
410
+ *
411
+ * This is a **terminal, non-transient** condition, in the same family as
412
+ * `ResponseTruncatedError`: no amount of retrying clears it, because nothing
413
+ * about the request is wrong. The budget has to be raised out-of-band before any
414
+ * further call can succeed, so both the transport retry ladder and the driver's
415
+ * transient-retry catch step aside for it and the failure surfaces immediately
416
+ * as the `'budget-exhausted'` `TurnFailureReason`.
417
+ *
418
+ * `budgetUsd`/`spentUsd` are populated from the rejection body when the proxy
419
+ * supplies them (the workstream-C contract does); they are optional because a
420
+ * plain-text or truncated 402 from an older proxy still has to classify.
421
+ *
422
+ * @beta
423
+ */
424
+ export declare class BudgetExhaustedError extends Error {
425
+ /** Vendor label of the transport that was refused (e.g. `'Anthropic'`). */
426
+ readonly vendorLabel: string;
427
+ /** The configured spend cap in USD, when the proxy reported one. */
428
+ readonly budgetUsd?: number;
429
+ /** Spend already booked against that cap in USD, when the proxy reported it. */
430
+ readonly spentUsd?: number;
431
+ /** The proxy's human-readable rejection message, when present. */
432
+ readonly detail?: string;
433
+ /**
434
+ * The proxy's own verdict on whether **any other** vendor it meters still has
435
+ * headroom, from the 402's `otherVendorAvailable`.
436
+ *
437
+ * The one field on this error a client cannot re-derive. A client can only
438
+ * infer "another vendor is free" from registry membership — which knows
439
+ * nothing about that vendor's remaining spend — so a `false` here is the only
440
+ * thing standing between the user and "Switch to Gemini to keep going" for a
441
+ * Gemini pot that is already empty. `undefined` means the proxy did not say
442
+ * (an older build), and the client is back to inferring.
443
+ */
444
+ readonly otherVendorAvailable?: boolean;
445
+ /**
446
+ * The vendor the **proxy** says it refused, verbatim from the 402's `vendor`.
447
+ *
448
+ * A second opinion, not the first: {@link BudgetExhaustedError.vendorLabel}
449
+ * comes from the transport that was actually called and wins wherever it
450
+ * resolves. This one rescues attribution when the transport's label does not —
451
+ * a white-labelled or multiplexing gateway whose single transport fronts more
452
+ * than one upstream stamps one static label for all of them, and only the
453
+ * proxy knows which pot the request was actually booked against.
454
+ */
455
+ readonly serverVendor?: string;
456
+ constructor(
457
+ /** Vendor label of the transport that was refused (e.g. `'Anthropic'`). */
458
+ vendorLabel: string,
459
+ /** The configured spend cap in USD, when the proxy reported one. */
460
+ budgetUsd?: number,
461
+ /** Spend already booked against that cap in USD, when the proxy reported it. */
462
+ spentUsd?: number,
463
+ /** The proxy's human-readable rejection message, when present. */
464
+ detail?: string,
465
+ /**
466
+ * The fields the proxy reports about the wider budget picture. An object
467
+ * rather than two more positional arguments: the four above are already the
468
+ * limit of what reads at a call site, and these two are set together or not
469
+ * at all.
470
+ */
471
+ extra?: {
472
+ otherVendorAvailable?: boolean;
473
+ serverVendor?: string;
474
+ });
475
+ }
476
+
381
477
  /**
382
478
  * Prompt-cache policy for a chat request, resolved per turn. **Provider-neutral intent,
383
479
  * provider-specific effect** — read this carefully, the two providers differ sharply:
@@ -573,6 +669,65 @@ export declare type ChatDriverResult = {
573
669
  * successful turn, a user cancel, and a driver dispose (none are failures).
574
670
  */
575
671
  failureReason?: TurnFailureReason;
672
+ /**
673
+ * Budget figures reported by the proxy, present **only** when
674
+ * `failureReason === 'budget-exhausted'` and the rejection carried them.
675
+ * Lifted off `BudgetExhaustedError` so a host can render the real numbers
676
+ * ("$25.40 of $25") without catching the transport error itself — before
677
+ * this the figures reached the debug log but not the caller, so the
678
+ * assistant's blocked banner could only ever show generic copy.
679
+ *
680
+ * Present whenever the wall carried anything worth reporting — figures, or
681
+ * an attributable vendor. Omitted entirely (not set to `undefined`) when
682
+ * it carried neither, and absent from every non-budget result — a clean
683
+ * turn's shape stays byte-identical to the historical `{ reason: 'done' }`.
684
+ */
685
+ budget?: {
686
+ /** The configured spend cap in USD, when the proxy reported one. */
687
+ budgetUsd?: number;
688
+ /** Spend already booked against that cap in USD, when reported. */
689
+ spentUsd?: number;
690
+ /**
691
+ * Vendor label of the transport that was refused (e.g. `'Anthropic'`).
692
+ * Always present when `budget` is: it is the one field that keeps a
693
+ * per-vendor budget model open without a breaking change later.
694
+ */
695
+ vendorLabel: string;
696
+ /**
697
+ * The refusing vendor as a typed {@link AIProviderType}: the normalised
698
+ * key of `vendorLabel` where that label is one a vendor claims, otherwise
699
+ * the normalised key of the proxy's own `vendor` field on the 402.
700
+ *
701
+ * So it is **not** always a normalisation of `vendorLabel`, and the two
702
+ * fields can legitimately disagree — a white-labelled or multiplexing
703
+ * gateway stamps one static transport label in front of several upstreams,
704
+ * and only the proxy can say which of them refused. The fallback is
705
+ * deliberate: it is what keeps attribution working for those deployments
706
+ * instead of degrading them all to "unattributable".
707
+ *
708
+ * Optional because a label no vendor claims and a 402 that names no vendor
709
+ * must degrade to "unattributable" rather than to a wrong attribution.
710
+ * Derived from the label first, rather than from the driver's
711
+ * last-resolved provider, **on purpose**: the label originates at the
712
+ * transport that was actually refused, whereas the last-resolved provider
713
+ * is stale on the classification seam (an orchestrated turn classifies
714
+ * against the registry default, which the chat driver may never have
715
+ * resolved).
716
+ */
717
+ vendor?: AIProviderType;
718
+ /**
719
+ * The proxy's verdict on whether any OTHER vendor it meters still has
720
+ * headroom (the 402's `otherVendorAvailable`).
721
+ *
722
+ * The only field here a client cannot re-derive, and the reason it is
723
+ * carried: a client can infer "another vendor exists" from registry
724
+ * membership, but never "another vendor has budget left". A `false` is
725
+ * therefore server-known truth that must beat that inference —
726
+ * `FoundationAiAssistant` uses it to stop advising a switch to a vendor
727
+ * whose pot is already empty. `undefined` means the proxy did not say.
728
+ */
729
+ otherVendorAvailable?: boolean;
730
+ };
576
731
  } | {
577
732
  reason: 'agent-handoff';
578
733
  summary: string;
@@ -1466,6 +1621,21 @@ export declare interface CriteriaInterpretContext {
1466
1621
  fields?: FieldLike[];
1467
1622
  }
1468
1623
 
1624
+ /**
1625
+ * The single copy shown to a user whose AI budget is gone — the transcript bubble
1626
+ * the chat driver appends, and the default text of the assistant's blocked banner.
1627
+ *
1628
+ * Deliberately **vendor-neutral**: the same bundle ships to white-labelled
1629
+ * deployments where "contact Genesis" is simply wrong. A host that wants
1630
+ * branded wording overrides both surfaces — the banner via
1631
+ * `FoundationAiAssistant.setBlocked(true, reason)`, the transcript bubble via
1632
+ * `ChatDriverConfig.budgetExhaustedMessage` — so neither is stuck with this
1633
+ * default while the other is customised.
1634
+ *
1635
+ * @beta
1636
+ */
1637
+ export declare const DEFAULT_BUDGET_EXHAUSTED_MESSAGE = "You've reached your AI usage limit. Contact your administrator to raise it.";
1638
+
1469
1639
  /**
1470
1640
  * A field descriptor accepted by criteria interpretation utilities.
1471
1641
  * Can be a plain string name or an object with common field metadata properties.
@@ -1753,6 +1923,14 @@ export declare type InteractionResult<T = unknown> = {
1753
1923
  /** Returns true when AI features (beta) are enabled via ?feature.ai URL param or GENX_ENABLE_AI=true env var. */
1754
1924
  export declare const isAIFeatureEnabled: () => boolean;
1755
1925
 
1926
+ /**
1927
+ * Whether this vendor's spend is metered by the proxy — see
1928
+ * {@link BUDGETED_VENDORS}.
1929
+ *
1930
+ * @beta
1931
+ */
1932
+ export declare const isBudgetedVendor: (vendor: AIProviderType | undefined) => boolean;
1933
+
1756
1934
  /**
1757
1935
  * Type guard that narrows a `ChatToolCall` to `ChatToolCallUnknown`.
1758
1936
  *
@@ -2112,10 +2290,20 @@ declare interface StructuredPromptOptions {
2112
2290
  * an incomplete tool call; deterministic, so it is not retried.
2113
2291
  * - `refusal` — a safety-classifier decline (e.g. Fable 5 `stop_reason: 'refusal'`),
2114
2292
  * which returns empty content; deterministic, so it is not retried.
2293
+ * - `budget_exhausted` — the AI-spend budget is used up and the proxy refused the
2294
+ * request (HTTP 402). **Terminal for the parent too, and enforced**: the parent
2295
+ * driver ends its own turn as soon as this outcome comes back, instead of
2296
+ * appending it as a tool result and calling the model again into the same wall.
2297
+ * So a handler that receives it will not be called a second time this turn, and
2298
+ * the parent turn reports `'budget-exhausted'` in its own right.
2299
+ *
2300
+ * Recovery guidance differs sharply by reason — see `docs/sub_agent.md`. Only
2301
+ * `budget_exhausted` and `refusal` are terminal; retrying either just reproduces
2302
+ * it. The rest are worth a retry or a question to the user.
2115
2303
  *
2116
2304
  * @beta
2117
2305
  */
2118
- export declare type SubAgentFailureReason = 'max_iterations' | 'malformed_tool_call' | 'empty_response' | 'unknown_tool_limit' | 'timeout' | 'response_truncated' | 'refusal';
2306
+ export declare type SubAgentFailureReason = 'max_iterations' | 'malformed_tool_call' | 'empty_response' | 'unknown_tool_limit' | 'timeout' | 'response_truncated' | 'refusal' | 'budget_exhausted';
2119
2307
 
2120
2308
  /**
2121
2309
  * Options passed to `requestSubAgent` at call time.
@@ -2173,9 +2361,60 @@ export declare const SUPPORTED_GEMINI_MODEL_IDS: readonly GeminiModelId[];
2173
2361
  * incomplete tool call; deterministic, so it bails without retry.
2174
2362
  * - `refusal` — a safety-classifier decline (e.g. Fable 5 `stop_reason: 'refusal'`),
2175
2363
  * which returns empty content; deterministic, so it is not retried.
2364
+ * - `budget-exhausted` — the AI-spend budget is used up and the proxy refused the request
2365
+ * (`BudgetExhaustedError`, HTTP 402). Terminal, not retried: the
2366
+ * budget must be raised out-of-band before any call can succeed.
2367
+ *
2368
+ * @beta
2369
+ */
2370
+ export declare type TurnFailureReason = 'exception' | 'malformed-function-call' | 'empty-response' | 'unknown-tool-limit' | 'max-iterations' | 'response-truncated' | 'refusal' | 'budget-exhausted';
2371
+
2372
+ /**
2373
+ * Display name for each concrete AI vendor: the **single source** every
2374
+ * transport's `vendorLabel` is read from, and the only place a vendor's name is
2375
+ * spelled for a user.
2376
+ *
2377
+ * This map is the hinge of the per-vendor budget model (GENC-1464). A wall is
2378
+ * attributed to a vendor by reverse-looking-up the label the refused transport
2379
+ * stamped on its {@link BudgetExhaustedError} — so a label that drifts from this
2380
+ * map (a literal typo, a casing change, a new vendor added with a hardcoded
2381
+ * string) silently degrades every wall from that transport to "unattributable"
2382
+ * and re-locks the whole session instead of just that vendor. It fails safe, but
2383
+ * the feature quietly stops working, which is why the transports read their label
2384
+ * from here rather than restating it.
2385
+ *
2386
+ * The map has **two kinds of entry**, and the difference matters:
2387
+ *
2388
+ * - The {@link BUDGETED_VENDORS} — `anthropic` and `gemini` — reach the model
2389
+ * through the ai-service proxy, are metered by it, and stamp their label on a
2390
+ * {@link BudgetExhaustedError}. For these the label is both an attribution key
2391
+ * and display copy.
2392
+ * - `chrome` and `openai` are **display-only**. `ChromeProvider` talks to the
2393
+ * on-device Prompt API and the proxy refuses `openai` outright, so neither can
2394
+ * be refused for budget or stamp a label — but both report a `provider` from
2395
+ * `getStatus()`, which puts them in the assistant's reachable set, which is
2396
+ * what the blocked banner names when it tells a walled user where they can
2397
+ * still go ("Switch to Chrome to keep going."). Dropping either entry would
2398
+ * print the raw type there.
2399
+ *
2400
+ * `'none'` is excluded deliberately: it is the "no provider configured" sentinel,
2401
+ * not a vendor, and nothing can be refused by it — nor switched to.
2402
+ *
2403
+ * @beta
2404
+ */
2405
+ export declare const VENDOR_LABELS: Readonly<Record<Exclude<AIProviderType, 'none'>, string>>;
2406
+
2407
+ /**
2408
+ * The {@link AIProviderType} behind a vendor label, or `undefined` for a label
2409
+ * no vendor claims.
2410
+ *
2411
+ * Case-insensitive and whitespace-tolerant, because the label travels as free
2412
+ * text on the wire (the driver contract carries `vendorLabel`, not the type) and
2413
+ * an unrecognised label must degrade to "unattributable" rather than to a wrong
2414
+ * attribution — blocking the wrong vendor is worse than blocking none.
2176
2415
  *
2177
2416
  * @beta
2178
2417
  */
2179
- export declare type TurnFailureReason = 'exception' | 'malformed-function-call' | 'empty-response' | 'unknown-tool-limit' | 'max-iterations' | 'response-truncated' | 'refusal';
2418
+ export declare function vendorTypeOfLabel(label?: string): AIProviderType | undefined;
2180
2419
 
2181
2420
  export { }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@genesislcap/foundation-ai",
3
3
  "description": "Genesis Foundation AI - Provider-agnostic AI configuration and shared utilities",
4
- "version": "15.3.2",
4
+ "version": "15.3.3-alpha-4899ddd.0",
5
5
  "sideEffects": false,
6
6
  "license": "SEE LICENSE IN license.txt",
7
7
  "main": "dist/esm/index.js",
@@ -52,17 +52,17 @@
52
52
  }
53
53
  },
54
54
  "devDependencies": {
55
- "@genesislcap/foundation-testing": "15.3.2",
56
- "@genesislcap/genx": "15.3.2",
57
- "@genesislcap/rollup-builder": "15.3.2",
58
- "@genesislcap/ts-builder": "15.3.2",
59
- "@genesislcap/uvu-playwright-builder": "15.3.2",
60
- "@genesislcap/vite-builder": "15.3.2",
61
- "@genesislcap/webpack-builder": "15.3.2"
55
+ "@genesislcap/foundation-testing": "15.3.3-alpha-4899ddd.0",
56
+ "@genesislcap/genx": "15.3.3-alpha-4899ddd.0",
57
+ "@genesislcap/rollup-builder": "15.3.3-alpha-4899ddd.0",
58
+ "@genesislcap/ts-builder": "15.3.3-alpha-4899ddd.0",
59
+ "@genesislcap/uvu-playwright-builder": "15.3.3-alpha-4899ddd.0",
60
+ "@genesislcap/vite-builder": "15.3.3-alpha-4899ddd.0",
61
+ "@genesislcap/webpack-builder": "15.3.3-alpha-4899ddd.0"
62
62
  },
63
63
  "dependencies": {
64
- "@genesislcap/foundation-logger": "15.3.2",
65
- "@genesislcap/foundation-utils": "15.3.2",
64
+ "@genesislcap/foundation-logger": "15.3.3-alpha-4899ddd.0",
65
+ "@genesislcap/foundation-utils": "15.3.3-alpha-4899ddd.0",
66
66
  "@microsoft/fast-foundation": "2.50.0"
67
67
  },
68
68
  "repository": {
@@ -73,5 +73,5 @@
73
73
  "publishConfig": {
74
74
  "access": "public"
75
75
  },
76
- "gitHead": "9b3b32a9d7642518469a81d1ed9cc79b3a6c0ece"
76
+ "gitHead": "aaf601ba5464fd53d1edc1a690443b6e38f5dff3"
77
77
  }