@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.
@@ -0,0 +1,271 @@
1
+ /**
2
+ * The AI-spend budget wall (GENC-1464 workstream C/D).
3
+ *
4
+ * Lives in its own module rather than beside `ResponseTruncatedError` in
5
+ * `anthropic-transport.ts` because it is raised from `post-with-retry.ts`, which
6
+ * both transports import — homing it in a transport would close an import cycle
7
+ * (transport → post-with-retry → transport) that the repo's `circular` check
8
+ * rejects.
9
+ */
10
+ /** The proxy's machine-readable rejection code for an exhausted AI budget. */
11
+ export const BUDGET_EXCEEDED_CODE = 'BUDGET_EXCEEDED';
12
+ /**
13
+ * Display name for each concrete AI vendor: the **single source** every
14
+ * transport's `vendorLabel` is read from, and the only place a vendor's name is
15
+ * spelled for a user.
16
+ *
17
+ * This map is the hinge of the per-vendor budget model (GENC-1464). A wall is
18
+ * attributed to a vendor by reverse-looking-up the label the refused transport
19
+ * stamped on its {@link BudgetExhaustedError} — so a label that drifts from this
20
+ * map (a literal typo, a casing change, a new vendor added with a hardcoded
21
+ * string) silently degrades every wall from that transport to "unattributable"
22
+ * and re-locks the whole session instead of just that vendor. It fails safe, but
23
+ * the feature quietly stops working, which is why the transports read their label
24
+ * from here rather than restating it.
25
+ *
26
+ * The map has **two kinds of entry**, and the difference matters:
27
+ *
28
+ * - The {@link BUDGETED_VENDORS} — `anthropic` and `gemini` — reach the model
29
+ * through the ai-service proxy, are metered by it, and stamp their label on a
30
+ * {@link BudgetExhaustedError}. For these the label is both an attribution key
31
+ * and display copy.
32
+ * - `chrome` and `openai` are **display-only**. `ChromeProvider` talks to the
33
+ * on-device Prompt API and the proxy refuses `openai` outright, so neither can
34
+ * be refused for budget or stamp a label — but both report a `provider` from
35
+ * `getStatus()`, which puts them in the assistant's reachable set, which is
36
+ * what the blocked banner names when it tells a walled user where they can
37
+ * still go ("Switch to Chrome to keep going."). Dropping either entry would
38
+ * print the raw type there.
39
+ *
40
+ * `'none'` is excluded deliberately: it is the "no provider configured" sentinel,
41
+ * not a vendor, and nothing can be refused by it — nor switched to.
42
+ *
43
+ * @beta
44
+ */
45
+ export const VENDOR_LABELS = Object.freeze({
46
+ anthropic: 'Anthropic',
47
+ gemini: 'Gemini',
48
+ openai: 'OpenAI',
49
+ chrome: 'Chrome',
50
+ });
51
+ /**
52
+ * The vendors whose spend the ai-service proxy meters — i.e. the only vendors
53
+ * that can raise a {@link BudgetExhaustedError}, and therefore the only ones a
54
+ * 402's `otherVendorAvailable: false` is a statement about.
55
+ *
56
+ * Deliberately narrower than the keys of {@link VENDOR_LABELS}, and it must stay
57
+ * exactly the proxy's own list — `VENDORS` in ai-service's `utils/aiVendor.js`.
58
+ * A vendor listed here that the proxy does not meter gets walled by the
59
+ * `otherVendorAvailable: false` sweep on a verdict that says nothing about it,
60
+ * and if it is reachable, `blocked` then derives true and locks a composer with
61
+ * headroom left.
62
+ *
63
+ * Two vendors are excluded for that reason:
64
+ * - `chrome` runs on-device — no pot to exhaust, and free.
65
+ * - `openai` is **not budgeted by the proxy**. ai-service rejects it up front
66
+ * with `400 UNSUPPORTED_PROVIDER` (`SUPPORTED_PROVIDERS` in `aiVendor.js`), no
67
+ * `MODEL_PRICING` row names it, and `otherVendorAvailable` is computed over
68
+ * Anthropic and Gemini alone. So it can never raise a 402, and a 402 is never
69
+ * a statement about it.
70
+ *
71
+ * @beta
72
+ */
73
+ export const BUDGETED_VENDORS = Object.freeze([
74
+ 'anthropic',
75
+ 'gemini',
76
+ ]);
77
+ /**
78
+ * Whether this vendor's spend is metered by the proxy — see
79
+ * {@link BUDGETED_VENDORS}.
80
+ *
81
+ * @beta
82
+ */
83
+ export const isBudgetedVendor = (vendor) => vendor != null && BUDGETED_VENDORS.includes(vendor);
84
+ /** Reverse index of {@link VENDOR_LABELS}, lower-cased, built once. */
85
+ const VENDOR_TYPE_BY_LABEL = new Map(Object.entries(VENDOR_LABELS).map(([type, label]) => [
86
+ label.toLowerCase(),
87
+ type,
88
+ ]));
89
+ /**
90
+ * The {@link AIProviderType} behind a vendor label, or `undefined` for a label
91
+ * no vendor claims.
92
+ *
93
+ * Case-insensitive and whitespace-tolerant, because the label travels as free
94
+ * text on the wire (the driver contract carries `vendorLabel`, not the type) and
95
+ * an unrecognised label must degrade to "unattributable" rather than to a wrong
96
+ * attribution — blocking the wrong vendor is worse than blocking none.
97
+ *
98
+ * @beta
99
+ */
100
+ export function vendorTypeOfLabel(label) {
101
+ if (typeof label !== 'string')
102
+ return undefined;
103
+ return VENDOR_TYPE_BY_LABEL.get(label.trim().toLowerCase());
104
+ }
105
+ /**
106
+ * HTTP status the ai-service proxy rejects an over-budget request with, in both
107
+ * the legacy JSON mode (the response status) and the NDJSON framed mode (the
108
+ * `status` on the terminal `err` frame).
109
+ *
110
+ * Deliberately **absent** from every transport's `RETRYABLE_STATUSES`: the budget
111
+ * does not refill on a backoff ladder, so retrying only delays the inevitable
112
+ * five times over.
113
+ */
114
+ export const BUDGET_EXCEEDED_STATUS = 402;
115
+ /**
116
+ * The single copy shown to a user whose AI budget is gone — the transcript bubble
117
+ * the chat driver appends, and the default text of the assistant's blocked banner.
118
+ *
119
+ * Deliberately **vendor-neutral**: the same bundle ships to white-labelled
120
+ * deployments where "contact Genesis" is simply wrong. A host that wants
121
+ * branded wording overrides both surfaces — the banner via
122
+ * `FoundationAiAssistant.setBlocked(true, reason)`, the transcript bubble via
123
+ * `ChatDriverConfig.budgetExhaustedMessage` — so neither is stuck with this
124
+ * default while the other is customised.
125
+ *
126
+ * @beta
127
+ */
128
+ export const DEFAULT_BUDGET_EXHAUSTED_MESSAGE = "You've reached your AI usage limit. Contact your administrator to raise it.";
129
+ /**
130
+ * Thrown when the AI-spend budget for the caller (user, tenant, or project) is
131
+ * exhausted and the ai-service proxy refuses the request outright — HTTP `402`
132
+ * with `code: 'BUDGET_EXCEEDED'`, in either the legacy JSON or the NDJSON framed
133
+ * mode.
134
+ *
135
+ * This is a **terminal, non-transient** condition, in the same family as
136
+ * `ResponseTruncatedError`: no amount of retrying clears it, because nothing
137
+ * about the request is wrong. The budget has to be raised out-of-band before any
138
+ * further call can succeed, so both the transport retry ladder and the driver's
139
+ * transient-retry catch step aside for it and the failure surfaces immediately
140
+ * as the `'budget-exhausted'` `TurnFailureReason`.
141
+ *
142
+ * `budgetUsd`/`spentUsd` are populated from the rejection body when the proxy
143
+ * supplies them (the workstream-C contract does); they are optional because a
144
+ * plain-text or truncated 402 from an older proxy still has to classify.
145
+ *
146
+ * @beta
147
+ */
148
+ export class BudgetExhaustedError extends Error {
149
+ constructor(
150
+ /** Vendor label of the transport that was refused (e.g. `'Anthropic'`). */
151
+ vendorLabel,
152
+ /** The configured spend cap in USD, when the proxy reported one. */
153
+ budgetUsd,
154
+ /** Spend already booked against that cap in USD, when the proxy reported it. */
155
+ spentUsd,
156
+ /** The proxy's human-readable rejection message, when present. */
157
+ detail,
158
+ /**
159
+ * The fields the proxy reports about the wider budget picture. An object
160
+ * rather than two more positional arguments: the four above are already the
161
+ * limit of what reads at a call site, and these two are set together or not
162
+ * at all.
163
+ */
164
+ extra) {
165
+ super(`${vendorLabel} request refused: the AI usage budget is exhausted` +
166
+ (budgetUsd != null
167
+ ? ` (spent ${spentUsd != null ? `$${spentUsd}` : 'an unknown amount'} of a $${budgetUsd} budget)`
168
+ : '') +
169
+ (detail ? `: ${detail}` : '') +
170
+ '. Retrying will not clear this — the budget must be raised.');
171
+ this.vendorLabel = vendorLabel;
172
+ this.budgetUsd = budgetUsd;
173
+ this.spentUsd = spentUsd;
174
+ this.detail = detail;
175
+ this.name = 'BudgetExhaustedError';
176
+ this.otherVendorAvailable = extra === null || extra === void 0 ? void 0 : extra.otherVendorAvailable;
177
+ this.serverVendor = extra === null || extra === void 0 ? void 0 : extra.serverVendor;
178
+ }
179
+ }
180
+ /**
181
+ * True for a budget rejection in either wire mode: the contracted `402`, or the
182
+ * `BUDGET_EXCEEDED` code under **any** status.
183
+ *
184
+ * Both keys are load-bearing, and they are not redundant:
185
+ *
186
+ * - `status` is the key the workstream-C contract guarantees ("402 always"), and
187
+ * the only one a plain-text or unparseable rejection carries.
188
+ * - `code` is the cheap defence against that contract drifting. Nothing stops a
189
+ * proxy from mapping the refusal onto `403` or `429` — and `429` is in *both*
190
+ * transports' `RETRYABLE_STATUSES`, so a status-only check would hand the user
191
+ * the exact bug GENC-1464 exists to fix: five doomed requests and ~31s of backoff
192
+ * before an opaque failure. A well-formed `{ t: 'err', status: 429, code:
193
+ * 'BUDGET_EXCEEDED' }` frame is accepted by `readFramedBody` (it only rejects a
194
+ * non-numeric status), so this is a reachable shape, not a hypothetical one.
195
+ *
196
+ * The false-positive cost is a proxy sending `code: 'BUDGET_EXCEEDED'` on a
197
+ * genuinely retryable failure — a shape the contract does not permit.
198
+ *
199
+ * Internal to the transports (unlike `BudgetExhaustedError`, which hosts branch
200
+ * on): deliberately **not** re-exported from the package index.
201
+ *
202
+ * @internal
203
+ */
204
+ export const isBudgetRejection = (status, code) => status === BUDGET_EXCEEDED_STATUS || code === BUDGET_EXCEEDED_CODE;
205
+ /**
206
+ * Best-effort JSON parse of an error body — `undefined` rather than a throw.
207
+ * Shared by every caller that has to classify a rejection body before it knows
208
+ * whether the body is JSON at all.
209
+ *
210
+ * @internal
211
+ */
212
+ export const parseJsonOrUndefined = (text) => {
213
+ try {
214
+ return JSON.parse(text);
215
+ }
216
+ catch (_a) {
217
+ return undefined;
218
+ }
219
+ };
220
+ /**
221
+ * Read the `code` field off an already-parsed rejection body, ignoring anything
222
+ * that is not a string. Pairs with {@link isBudgetRejection} at the legacy call
223
+ * sites, which have a parsed body rather than a typed frame.
224
+ *
225
+ * @internal
226
+ */
227
+ export const codeOf = (payload) => {
228
+ const code = payload === null || payload === void 0 ? void 0 : payload.code;
229
+ return typeof code === 'string' ? code : undefined;
230
+ };
231
+ /** Narrow an unknown JSON value to a number, ignoring anything else. */
232
+ const numberOrUndefined = (value) => typeof value === 'number' && Number.isFinite(value) ? value : undefined;
233
+ /** Narrow an unknown JSON value to a boolean, ignoring anything else. */
234
+ const booleanOrUndefined = (value) => typeof value === 'boolean' ? value : undefined;
235
+ /** Narrow an unknown JSON value to a non-empty string, ignoring anything else. */
236
+ const stringOrUndefined = (value) => typeof value === 'string' && value.trim() !== '' ? value : undefined;
237
+ /**
238
+ * Build a {@link BudgetExhaustedError} from a rejection payload of either shape:
239
+ * the legacy body `{ error, code, budgetUsd, spentUsd, vendor,
240
+ * otherVendorAvailable }` or the framed err frame's `details` (the same fields
241
+ * minus `error`) plus its top-level `error`.
242
+ *
243
+ * The fields read off the wire, and why each is here:
244
+ *
245
+ * | field | used for |
246
+ * | ---------------------- | ----------------------------------------------------- |
247
+ * | `budgetUsd` | the cap in the banner's figures |
248
+ * | `spentUsd` | the spend in the banner's figures |
249
+ * | `error` | the proxy's human-readable detail |
250
+ * | `vendor` | attribution when the transport's own label is unclaimed |
251
+ * | `otherVendorAvailable` | whether "switch vendor" is honest advice at all |
252
+ *
253
+ * Tolerant by design — a `402` classifies even when the payload is missing,
254
+ * unparseable, or carries none of these. Each field is narrowed independently,
255
+ * so a proxy that sends `null` for a figure it does not know (which the
256
+ * ai-service side now does, in preference to a misleading `0`) still yields the
257
+ * vendor and the availability verdict.
258
+ */
259
+ export function budgetExhaustedFrom(vendorLabel, payload, fallbackDetail) {
260
+ var _a, _b, _c, _d;
261
+ const body = (typeof payload === 'object' && payload !== null ? payload : {});
262
+ // The framed shape nests everything but `error` under `details`; the legacy
263
+ // body has it all at the top level. Read both so one helper serves both call
264
+ // sites.
265
+ const details = (typeof body.details === 'object' && body.details !== null ? body.details : {});
266
+ const detail = typeof body.error === 'string' ? body.error : fallbackDetail;
267
+ return new BudgetExhaustedError(vendorLabel, (_a = numberOrUndefined(body.budgetUsd)) !== null && _a !== void 0 ? _a : numberOrUndefined(details.budgetUsd), (_b = numberOrUndefined(body.spentUsd)) !== null && _b !== void 0 ? _b : numberOrUndefined(details.spentUsd), detail, {
268
+ otherVendorAvailable: (_c = booleanOrUndefined(body.otherVendorAvailable)) !== null && _c !== void 0 ? _c : booleanOrUndefined(details.otherVendorAvailable),
269
+ serverVendor: (_d = stringOrUndefined(body.vendor)) !== null && _d !== void 0 ? _d : stringOrUndefined(details.vendor),
270
+ });
271
+ }
@@ -3,6 +3,7 @@ import { SUPPORTED_GEMINI_MODEL_IDS, } from '../types';
3
3
  import { logger } from '../utils/logger';
4
4
  import { scaleTemperature } from '../utils/temperature';
5
5
  import { ResponseTruncatedError } from './anthropic-transport';
6
+ import { VENDOR_LABELS } from './budget-exhausted-error';
6
7
  import { repairMalformedFunctionCall } from './gemini-malformed-call';
7
8
  import { postWithRetry } from './post-with-retry';
8
9
  const GEMINI_DIRECT_URL = (model) => `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`;
@@ -587,7 +588,7 @@ export class GeminiTransport {
587
588
  headers,
588
589
  body: payload,
589
590
  credentials,
590
- vendorLabel: 'Gemini',
591
+ vendorLabel: VENDOR_LABELS.gemini,
591
592
  retryableStatuses: GeminiTransport.RETRYABLE_STATUSES,
592
593
  timeout: this.timeout,
593
594
  stallTimeout: this.stallTimeout,
@@ -3,6 +3,7 @@ import { authoritativeAbortReason } from '../utils/abort-reason';
3
3
  import { abortableDelay } from '../utils/abortable-delay';
4
4
  import { combineSignals } from '../utils/combine-signals';
5
5
  import { logger } from '../utils/logger';
6
+ import { budgetExhaustedFrom, codeOf, isBudgetRejection, parseJsonOrUndefined, } from './budget-exhausted-error';
6
7
  import { readFramedBody } from './ndjson-frames';
7
8
  const MAX_RETRIES = 5;
8
9
  /**
@@ -56,15 +57,44 @@ export function postWithRetry(options) {
56
57
  signal: combinedSignal,
57
58
  credentials,
58
59
  });
59
- if (retryableStatuses.includes(response.status) && attempt < MAX_RETRIES) {
60
- logger.warn(`${vendorLabel}Transport: retryable status ${response.status}, retrying (attempt ${attempt + 1}/${MAX_RETRIES})`);
61
- clearTimeout(timeoutId); // this attempt is over — don't let its timer tick through the backoff
62
- yield backoff(attempt);
63
- continue;
64
- }
65
60
  if (!response.ok) {
66
- const err = yield response.text();
67
- throw new Error(`${vendorLabel} request error ${response.status}: ${err}`);
61
+ // One read of the error body per failing attempt — it feeds BOTH the
62
+ // budget classification and the thrown message, and the response is
63
+ // discarded either way. Note this means a retryable attempt now reads
64
+ // the (small, proxy-authored) error body before backing off. That is the
65
+ // price of the classification winning over the ladder in this branch as
66
+ // well as the framed one; the attempt's wall-clock timer is still armed
67
+ // and the read runs on the fetch's combined signal, so a pathological
68
+ // non-terminating body times out rather than hanging.
69
+ //
70
+ // That timeout is a real behaviour change for a RETRYABLE status, and the
71
+ // accepted cost of the symmetry: a 503 used to reach the ladder without
72
+ // the body being touched, so a non-terminating one still backed off and
73
+ // retried. Now the abort propagates out of `postWithRetry` instead.
74
+ // Acceptable because these bodies are proxy-authored and small — if that
75
+ // ever stops being true, read the body only when the status is
76
+ // non-retryable or already budget-shaped.
77
+ const errText = yield response.text();
78
+ const parsed = parseJsonOrUndefined(errText);
79
+ // The budget wall (GENC-1464) wins over ANY status-based retry — the same
80
+ // rule the framed branch below applies, now symmetric. It must be
81
+ // classified BEFORE the ladder, not after it: 402 is absent from every
82
+ // RETRYABLE_STATUSES today, but a proxy that maps the refusal onto a
83
+ // retryable status (or a future set that includes 402) would otherwise
84
+ // cost five doomed requests and ~31s of backoff before an opaque error —
85
+ // exactly the bug this classification exists to remove. Parsing is
86
+ // best-effort: an unparseable or plain-text 402 still classifies, just
87
+ // without the budget/spent figures.
88
+ if (isBudgetRejection(response.status, codeOf(parsed))) {
89
+ throw budgetExhaustedFrom(vendorLabel, parsed, errText || undefined);
90
+ }
91
+ if (retryableStatuses.includes(response.status) && attempt < MAX_RETRIES) {
92
+ logger.warn(`${vendorLabel}Transport: retryable status ${response.status}, retrying (attempt ${attempt + 1}/${MAX_RETRIES})`);
93
+ clearTimeout(timeoutId); // this attempt is over — don't let its timer tick through the backoff
94
+ yield backoff(attempt);
95
+ continue;
96
+ }
97
+ throw new Error(`${vendorLabel} request error ${response.status}: ${errText}`);
68
98
  }
69
99
  // The proxy streams NDJSON heartbeat frames when it supports them; an old
70
100
  // proxy ignores our Accept header and returns plain JSON, so branch on
@@ -83,6 +113,15 @@ export function postWithRetry(options) {
83
113
  // local stall, so the driver's TimeoutError branch handles both.
84
114
  throw new DOMException(`${vendorLabel} request stalled: ${(_c = frame.error) !== null && _c !== void 0 ? _c : 'no upstream data'}`, 'TimeoutError');
85
115
  }
116
+ // The budget wall, framed (GENC-1464) — the in-band twin of the 402 above,
117
+ // through the same predicate, keyed on status OR code. Classified before
118
+ // the retryable ladder for the same reason UPSTREAM_STALLED is: the
119
+ // classification must win over any status-based retry. That ordering is
120
+ // no longer unique to this branch — the legacy branch above now applies
121
+ // the identical rule, so the two read as one policy rather than two.
122
+ if (isBudgetRejection(frame.status, frame.code)) {
123
+ throw budgetExhaustedFrom(vendorLabel, frame);
124
+ }
86
125
  // An in-band retryable status joins the same attempt/backoff ladder as
87
126
  // its HTTP-status equivalent (shared attempt counter).
88
127
  if (retryableStatuses.includes(frame.status) && attempt < MAX_RETRIES) {
@@ -1,4 +1,5 @@
1
1
  import { __awaiter } from "tslib";
2
+ import { budgetExhaustedFrom, codeOf, isBudgetRejection, parseJsonOrUndefined, VENDOR_LABELS, } from './budget-exhausted-error';
2
3
  const AI_SERVER_PATH = '/gwf/ai-service/chat-completions';
3
4
  const DEFAULT_MODEL = 'gpt-4o-mini';
4
5
  const DEFAULT_TIMEOUT = 30000;
@@ -56,6 +57,24 @@ export class ServerOpenAITransport {
56
57
  clearTimeout(timeoutId);
57
58
  if (!response.ok) {
58
59
  const err = yield response.text();
60
+ const parsed = parseJsonOrUndefined(err);
61
+ // This transport does its own bare `fetch` rather than going through
62
+ // `postWithRetry` (it has no retry ladder, no stall timer and no framed
63
+ // mode), so it would otherwise be the one proxy call where a 402 stays an
64
+ // opaque `AI proxy error 402`. Classify it through the same shared
65
+ // predicate the envelope uses, so the typed error is uniform across
66
+ // transports even though the envelope is not.
67
+ //
68
+ // Scope, stated plainly: this transport serves only `sendStructuredPrompt`
69
+ // — it has no `chat()` — and its single caller
70
+ // (`OpenAIProvider.interpretCriteria`) catches everything and returns
71
+ // null. So a 402 here degrades smart-search silently and can never latch
72
+ // the assistant's `blocked` state, before or after this change. The typed
73
+ // throw improves the log line and any host that catches it directly. See
74
+ // `docs/migration-GENC-1464.md` §"Transport coverage".
75
+ if (isBudgetRejection(response.status, codeOf(parsed))) {
76
+ throw budgetExhaustedFrom(VENDOR_LABELS.openai, parsed, err || undefined);
77
+ }
59
78
  throw new Error(`AI proxy error ${response.status}: ${err}`);
60
79
  }
61
80
  const data = (yield response.json());