@ultimat3/ai 31.34.0 → 31.36.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/CLAUDE.md +1 -1
- package/package.json +11 -11
- package/src/answer-errors.ts +1 -1
- package/src/budget-fix.ts +51 -0
- package/src/errors.ts +20 -13
- package/src/models.ts +1 -1
- package/src/openai-body.ts +3 -3
- package/src/tool-idempotency.ts +2 -2
package/CLAUDE.md
CHANGED
|
@@ -36,7 +36,7 @@ Keep `policy` dev-only: tests and `*-fixture.ts` harnesses import `allow`. The
|
|
|
36
36
|
| `gateway.ts` / `gateway-settle.ts` | routing, cache, budget wiring; a failed settle, the cache key |
|
|
37
37
|
| `model-fallback.ts` / `provider-options.ts` | `MODEL_FALLBACK_ON`, the chain walk; the `providerOptions` screen |
|
|
38
38
|
| `gateway-providers.ts` | the retry loop across providers, `backoffMs`, `isRetryable`, the breakers |
|
|
39
|
-
| `budget
|
|
39
|
+
| `budget*.ts` | ledgers per request/actor/org, ALS carrier; `BudgetStore`, `memoryBudgetStore()`; the refusal's fix |
|
|
40
40
|
| `prompt.ts` | `definePrompt`, `promptHash`, version registry |
|
|
41
41
|
| `prompt-fence.ts` | a template's data fences: which tag pairs enclose each slot (`promptFences`), and the closer-breaking `render` applies inside one |
|
|
42
42
|
| `embeddings.ts` | `Embedder`, `hashEmbedder()`, cosine helpers |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/ai",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.36.0",
|
|
4
4
|
"description": "LLM gateway, versioned prompts, evals as tests, embeddings, hybrid vector search, RAG",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -45,17 +45,17 @@
|
|
|
45
45
|
"test": "bun test"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ultimat3/action": "31.
|
|
49
|
-
"@ultimat3/cache": "31.
|
|
50
|
-
"@ultimat3/core": "31.
|
|
51
|
-
"@ultimat3/db": "31.
|
|
52
|
-
"@ultimat3/http": "31.
|
|
53
|
-
"@ultimat3/jobs": "31.
|
|
54
|
-
"@ultimat3/money": "31.
|
|
55
|
-
"@ultimat3/schema": "31.
|
|
56
|
-
"@ultimat3/time": "31.
|
|
48
|
+
"@ultimat3/action": "31.36.0",
|
|
49
|
+
"@ultimat3/cache": "31.36.0",
|
|
50
|
+
"@ultimat3/core": "31.36.0",
|
|
51
|
+
"@ultimat3/db": "31.36.0",
|
|
52
|
+
"@ultimat3/http": "31.36.0",
|
|
53
|
+
"@ultimat3/jobs": "31.36.0",
|
|
54
|
+
"@ultimat3/money": "31.36.0",
|
|
55
|
+
"@ultimat3/schema": "31.36.0",
|
|
56
|
+
"@ultimat3/time": "31.36.0"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@ultimat3/policy": "31.
|
|
59
|
+
"@ultimat3/policy": "31.36.0"
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/answer-errors.ts
CHANGED
|
@@ -23,7 +23,7 @@ export class LlmTruncatedError extends UltimateError {
|
|
|
23
23
|
super({
|
|
24
24
|
code: 'X_LLM_TRUNCATED',
|
|
25
25
|
cause: `prompt "${input.prompt}" was cut off at its ${input.maxTokens}-token ceiling`,
|
|
26
|
-
fix: `maxTokens: ${input.maxTokens * 2}
|
|
26
|
+
fix: `{ maxTokens: ${input.maxTokens * 2} } // on the ${input.subject ?? 'llm'}() declaration — or drop fields from its output schema`,
|
|
27
27
|
meta: { prompt: input.prompt, maxTokens: input.maxTokens },
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// The ceiling an `X_AI_BUDGET_EXCEEDED` refusal hands back: the budget key the scope is declared
|
|
2
|
+
// under, the value that would have covered this request, and where that key is written. A ledger
|
|
3
|
+
// scope is not always the declaration's key name, so the mapping lives here, next to nothing else.
|
|
4
|
+
|
|
5
|
+
/** What `AiBudgetExceededError` carries, minus the code. */
|
|
6
|
+
export interface BudgetRefusal {
|
|
7
|
+
readonly scope: string;
|
|
8
|
+
readonly requested: number;
|
|
9
|
+
readonly remaining: number;
|
|
10
|
+
readonly limit: number;
|
|
11
|
+
readonly unit?: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Where each ledger scope is declared. `request` is two keys: `providerGateway({ budget })` writes
|
|
16
|
+
* it as itself, and an `agent()` writes `tokensPerRun`, which arrives at the ledger as `request`.
|
|
17
|
+
*/
|
|
18
|
+
const DECLARED_ON: Readonly<Record<string, string>> = {
|
|
19
|
+
request: 'providerGateway({ budget }) — on an agent() it is budget.tokensPerRun',
|
|
20
|
+
tokensIn: 'the llm()/agent() declaration, or providerGateway({ budget })',
|
|
21
|
+
costPerCall: 'the llm()/agent() declaration, or providerGateway({ budget })',
|
|
22
|
+
costPerRun: 'the agent() declaration',
|
|
23
|
+
actor: 'providerGateway({ budget }) — a ceiling over the whole window',
|
|
24
|
+
org: 'providerGateway({ budget }) — a ceiling over the whole window',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** `USD minor units` or `USD at scale 6`, as the money scopes word their unit. */
|
|
28
|
+
const MONEY_UNIT = /^([A-Z]{3}) (?:minor units|at scale (\d+))$/;
|
|
29
|
+
|
|
30
|
+
/** One refusal's ceiling, in the three parts its fix line names. */
|
|
31
|
+
export interface BudgetCeiling {
|
|
32
|
+
/** The budget key the scope is declared under: `actor:<kind>:<id>` is `actor`. */
|
|
33
|
+
readonly key: string;
|
|
34
|
+
/** Already spent plus this request — the smallest value that lets this exact call through. */
|
|
35
|
+
readonly value: string;
|
|
36
|
+
/** Where that key is written. */
|
|
37
|
+
readonly where: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function budgetCeiling(refusal: BudgetRefusal): BudgetCeiling {
|
|
41
|
+
const key = refusal.scope.split(':')[0] ?? refusal.scope;
|
|
42
|
+
const needed = refusal.limit - refusal.remaining + refusal.requested;
|
|
43
|
+
const money = refusal.unit === undefined ? null : MONEY_UNIT.exec(refusal.unit);
|
|
44
|
+
const value =
|
|
45
|
+
money === null
|
|
46
|
+
? String(needed)
|
|
47
|
+
: `{ minor: ${needed}, currency: '${money[1]}'${money[2] === undefined ? '' : `, scale: ${money[2]}`} }`;
|
|
48
|
+
// A scope comes off the ledger, so `constructor` must not reach the prototype: own keys only.
|
|
49
|
+
const where = Object.hasOwn(DECLARED_ON, key) ? DECLARED_ON[key] : undefined;
|
|
50
|
+
return { key, value, where: where ?? 'providerGateway({ budget })' };
|
|
51
|
+
}
|
package/src/errors.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { renderFixShellArg, UltimateError } from '@ultimat3/core';
|
|
|
5
5
|
// The codes, their titles and the single registration call are `./error-codes`, imported here for
|
|
6
6
|
// that side effect.
|
|
7
7
|
import './error-codes';
|
|
8
|
+
import { budgetCeiling } from './budget-fix';
|
|
8
9
|
import type { ModelFallback, ModelFallbackClass } from './model-fallback';
|
|
9
10
|
|
|
10
11
|
// No `docs:` on the subclasses below. `UltimateError` fills it from `describeErrorCode(code).docs`,
|
|
@@ -41,7 +42,12 @@ export class AiProviderUnavailableError extends UltimateError {
|
|
|
41
42
|
super({
|
|
42
43
|
code: 'X_AI_PROVIDER_UNAVAILABLE',
|
|
43
44
|
cause: `no provider could serve model "${input.model}" (${input.attempts.join(' | ')})`,
|
|
44
|
-
|
|
45
|
+
// Unserved is a gateway built without a provider for this model: the fix is the wiring.
|
|
46
|
+
// Otherwise every provider listing it was asked and failed, each failure named in cause.
|
|
47
|
+
fix:
|
|
48
|
+
input.unserved === true
|
|
49
|
+
? `providerGateway({ providers: [provider] }) // apps/<app>/runtime.ts — a provider whose { models } lists '${input.model}'`
|
|
50
|
+
: "x doctor --json # egress from this process; each provider's failure is in cause — a 401 is its API key env var, a 5xx or an open breaker the provider's own outage",
|
|
45
51
|
retry: input.unserved === true ? 'terminal' : undefined,
|
|
46
52
|
});
|
|
47
53
|
this.fallback = input.fallback;
|
|
@@ -63,12 +69,13 @@ export class AiBudgetExceededError extends UltimateError {
|
|
|
63
69
|
/** What the numbers count. Money scopes pass minor units so nothing reads as a float. */
|
|
64
70
|
unit?: string;
|
|
65
71
|
}) {
|
|
72
|
+
const ceiling = budgetCeiling(input);
|
|
66
73
|
super({
|
|
67
74
|
code: 'X_AI_BUDGET_EXCEEDED',
|
|
68
75
|
cause:
|
|
69
76
|
`request needs ${input.requested} ${input.unit ?? 'tokens'} but scope ` +
|
|
70
77
|
`"${input.scope}" has ${input.remaining} of ${input.limit} left`,
|
|
71
|
-
fix: `
|
|
78
|
+
fix: `budget: { ${ceiling.key}: ${ceiling.value} } // on ${ceiling.where} — or shorten the prompt`,
|
|
72
79
|
});
|
|
73
80
|
}
|
|
74
81
|
}
|
|
@@ -139,7 +146,7 @@ export class LlmStreamInvalidError extends UltimateError {
|
|
|
139
146
|
super({
|
|
140
147
|
code: 'X_LLM_STREAM_INVALID',
|
|
141
148
|
cause: `streamed answer to prompt "${input.prompt}" failed its output schema: ${input.issues}`,
|
|
142
|
-
fix:
|
|
149
|
+
fix: `await action(input, { ctx }) // call "${input.prompt}" itself, not .stream(), when the answer must satisfy a structured schema — a stream has already delivered its tokens and cannot take a repair turn`,
|
|
143
150
|
});
|
|
144
151
|
}
|
|
145
152
|
}
|
|
@@ -167,7 +174,7 @@ export class LlmOutputInvalidError extends UltimateError {
|
|
|
167
174
|
cause:
|
|
168
175
|
`prompt "${input.prompt}" returned output failing its schema on all ` +
|
|
169
176
|
`${input.attempts} attempts: ${input.issues}`,
|
|
170
|
-
fix: 'describe the output shape in the
|
|
177
|
+
fix: 'definePrompt({ id, version, template }) // describe the output shape in the template and bump its version — or widen `output` in the llm() declaration',
|
|
171
178
|
});
|
|
172
179
|
}
|
|
173
180
|
}
|
|
@@ -189,7 +196,7 @@ export class AgentMaxTurnsError extends UltimateError {
|
|
|
189
196
|
cause:
|
|
190
197
|
`agent "${input.agent}" used all ${input.turns} turns and ${input.calls} tool calls ` +
|
|
191
198
|
`without calling the respond tool`,
|
|
192
|
-
fix: 'tell the template when to stop and answer through the respond tool, then bump its version — raise maxTurns only once the run demonstrably converges',
|
|
199
|
+
fix: 'definePrompt({ id, version, template }) // tell the template when to stop and answer through the respond tool, then bump its version — raise maxTurns only once the run demonstrably converges',
|
|
193
200
|
meta: { agent: input.agent, turns: input.turns },
|
|
194
201
|
});
|
|
195
202
|
}
|
|
@@ -243,8 +250,8 @@ export class LlmRefusedError extends UltimateError {
|
|
|
243
250
|
`${input.explanation === undefined ? '' : `: ${input.explanation}`}`,
|
|
244
251
|
fix:
|
|
245
252
|
input.alternative === undefined
|
|
246
|
-
? `edit the template
|
|
247
|
-
: `
|
|
253
|
+
? `definePrompt({ id, version, template }) // edit the template of "${input.prompt}" and bump its version — no model your app registered in its family is more capable than '${input.model}'`
|
|
254
|
+
: `model: '${input.alternative}' // on the "${input.prompt}" declaration — or edit its prompt's template and bump the version`,
|
|
248
255
|
meta: { model: input.model, category: input.category },
|
|
249
256
|
});
|
|
250
257
|
}
|
|
@@ -257,7 +264,7 @@ export class AiPromptVersionError extends UltimateError {
|
|
|
257
264
|
cause: `prompt "${input.id}" has no version "${input.requested}" (have: ${
|
|
258
265
|
input.available.length > 0 ? input.available.join(', ') : 'none'
|
|
259
266
|
})`,
|
|
260
|
-
fix: '
|
|
267
|
+
fix: `definePrompt({ id: '${input.id}', version, template }) // a new version after editing the template, then x manifest — getPrompt() asks for one cause lists`,
|
|
261
268
|
});
|
|
262
269
|
}
|
|
263
270
|
}
|
|
@@ -272,7 +279,7 @@ export class AiPromptRenderError extends UltimateError {
|
|
|
272
279
|
super({
|
|
273
280
|
code: 'X_AI_PROMPT_VERSION',
|
|
274
281
|
cause: `prompt "${input.ref}" was rendered without: ${input.missing.join(', ')}`,
|
|
275
|
-
fix: '
|
|
282
|
+
fix: `{ ${input.missing.join(', ')} } // returned from vars(): every {{variable}} the template declares — or remove it from the template`,
|
|
276
283
|
});
|
|
277
284
|
}
|
|
278
285
|
}
|
|
@@ -285,7 +292,7 @@ export class VectorDimMismatchError extends UltimateError {
|
|
|
285
292
|
cause: `store "${input.store}" expects ${input.expected} dimensions, got ${input.received}`,
|
|
286
293
|
// Not `x ai reindex`: that command is PLANNED and throws, so a fix line naming it sends an
|
|
287
294
|
// operator to a wall. A fix has to be performable today, which here means app code.
|
|
288
|
-
fix:
|
|
295
|
+
fix: `{ dimension: ${input.expected} } // the embedder that created "${input.store}" — or re-embed every record at the new width and upsert it`,
|
|
289
296
|
});
|
|
290
297
|
}
|
|
291
298
|
}
|
|
@@ -302,7 +309,7 @@ export class VectorScopeWidenedError extends UltimateError {
|
|
|
302
309
|
cause:
|
|
303
310
|
`store "${input.store}" is bound to tenant "${input.held}" and cannot be re-scoped ` +
|
|
304
311
|
`to "${input.requested}"`,
|
|
305
|
-
fix: `
|
|
312
|
+
fix: `vectorStore.scoped({ tenant: '${input.requested}' }) // derived from the unscoped store, never from the one bound to '${input.held}'`,
|
|
306
313
|
});
|
|
307
314
|
}
|
|
308
315
|
}
|
|
@@ -319,7 +326,7 @@ export class EmbedderDimMismatchError extends UltimateError {
|
|
|
319
326
|
cause:
|
|
320
327
|
`embedder "${input.embedder}" is declared with ${input.expected} dimensions but the ` +
|
|
321
328
|
`provider returned ${input.received}`,
|
|
322
|
-
fix: `
|
|
329
|
+
fix: `{ dimension: ${input.received} } // on the embedder "${input.embedder}", then re-embed every record at that width and upsert it`,
|
|
323
330
|
});
|
|
324
331
|
}
|
|
325
332
|
}
|
|
@@ -344,7 +351,7 @@ export class AiEmbedderInvalidError extends UltimateError {
|
|
|
344
351
|
// The `${…}` the fix used to carry is unreadable to the `errors` gate, which blanks every
|
|
345
352
|
// interpolation — so the literal half alone has to name the call. Which embedder broke the
|
|
346
353
|
// invariant is a fact of the failure, and the cause and `meta` are where facts live.
|
|
347
|
-
fix: '
|
|
354
|
+
fix: 'embed: (texts) => Promise.all(texts.map(vectorFor)) // one vector per input text, in the order the texts arrived',
|
|
348
355
|
meta: {
|
|
349
356
|
embedder: input.embedder,
|
|
350
357
|
...(input.expected === undefined ? {} : { expected: input.expected }),
|
package/src/models.ts
CHANGED
|
@@ -387,6 +387,6 @@ function assertDisableAllowed(model: ModelId, rules: ModelReasoning, effort: Eff
|
|
|
387
387
|
if (cap === undefined || rankOf(effort) <= rankOf(cap)) return;
|
|
388
388
|
throw new AiRequestInvalidError({
|
|
389
389
|
detail: `model "${model}" allows thinking: 'disabled' only at effort '${cap}' or below, not '${effort}'`,
|
|
390
|
-
fix: `
|
|
390
|
+
fix: `effort: '${cap}' # in definePrompt, alongside thinking: 'disabled' — or drop thinking from it`,
|
|
391
391
|
});
|
|
392
392
|
}
|
package/src/openai-body.ts
CHANGED
|
@@ -138,13 +138,13 @@ export function reasoningFields(
|
|
|
138
138
|
if (effort !== undefined && !rules.effort) {
|
|
139
139
|
throw new AiRequestInvalidError({
|
|
140
140
|
detail: `model "${model}" is registered with no effort control, so reasoning_effort cannot be sent to it`,
|
|
141
|
-
fix:
|
|
141
|
+
fix: `reasoning: { effort: true } # on the registerModel({ id: '${model}', … }) row, only if its endpoint accepts reasoning_effort — otherwise drop effort from definePrompt`,
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
144
|
if (thinking === 'adaptive' && !rules.adaptive) {
|
|
145
145
|
throw new AiRequestInvalidError({
|
|
146
146
|
detail: `model "${model}" has no adaptive thinking; the OpenAI format's only depth control is reasoning_effort`,
|
|
147
|
-
fix: '
|
|
147
|
+
fix: "effort: 'high' # in definePrompt, in place of thinking: 'adaptive' — or route the prompt to a model registered with reasoning: { adaptive: true }",
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
150
|
// The row says reasoning cannot be switched off, whatever the wire would spell the switch as.
|
|
@@ -157,7 +157,7 @@ export function reasoningFields(
|
|
|
157
157
|
if (thinking === 'disabled' && effort !== undefined) {
|
|
158
158
|
throw new AiRequestInvalidError({
|
|
159
159
|
detail: `model "${model}" writes both thinking and effort onto one reasoning_effort field, and the request asked for both`,
|
|
160
|
-
fix: "
|
|
160
|
+
fix: "thinking: 'disabled' # in definePrompt, with effort dropped — or keep effort and drop thinking",
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
// `none` IS the off switch on this wire, and a model with no effort control has nothing to
|
package/src/tool-idempotency.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// tier — and held equal to `mcp/src/idempotency-arg.ts` by `scripts/ai-mcp-tool-parity.test.ts`.
|
|
5
5
|
|
|
6
6
|
import { MAX_IDEMPOTENCY_KEY_LENGTH } from '@ultimat3/action';
|
|
7
|
-
import { UltimateError } from '@ultimat3/core';
|
|
7
|
+
import { renderFixShellArg, UltimateError } from '@ultimat3/core';
|
|
8
8
|
import type { JsonSchema } from './tools';
|
|
9
9
|
|
|
10
10
|
/** The reserved argument name on every idempotent action's tool — the one MCP reserves. */
|
|
@@ -33,7 +33,7 @@ export class ToolIdempotencyKeyShadowedError extends UltimateError {
|
|
|
33
33
|
super({
|
|
34
34
|
code: 'X_MCP_IDEMPOTENCY_KEY_SHADOWED',
|
|
35
35
|
cause: `${input.name} is idempotent and its input declares "${input.argument}", the MCP argument reserved for the idempotency key`,
|
|
36
|
-
fix: `
|
|
36
|
+
fix: `x actions show ${renderFixShellArg(input.name, '<action>')} --json # rename its "${input.argument}" input field — MCP clients send the Idempotency-Key as the reserved "${input.argument}" argument, and the action receives the rest`,
|
|
37
37
|
meta: { action: input.name, argument: input.argument },
|
|
38
38
|
});
|
|
39
39
|
}
|