@absolutejs/ai 0.6.5 → 0.6.7

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.
@@ -1,3 +1,4 @@
1
+ import { type AIProviderFallback } from "./providerFallback";
1
2
  import type { AIChunk, AIProviderConfig, AIProviderMessage, AIProviderStreamParams } from "../../types/ai";
2
3
  import { type AIInputCapacity } from "./inputCapacity";
3
4
  /** Token budgets, never character approximations or guessed model limits. */
@@ -26,6 +27,7 @@ export type AIContextEvent = {
26
27
  after: AIContextCapacity;
27
28
  };
28
29
  export type AIContextPolicy = AIContextBudget & {
30
+ providerFallback?: AIProviderFallback;
29
31
  /** Called at most once per model turn, before any output. Preserve originals
30
32
  * in application storage; return compacted/retrieved messages, never effects.
31
33
  * Tool IDs/inputs, signed thinking, media, provider data and system messages
@@ -37,3 +37,5 @@ export declare class ProviderError extends Error {
37
37
  */
38
38
  static from(err: unknown, provider: string): ProviderError;
39
39
  }
40
+ /** Provider entrypoints are independently bundled; preserve their error contract. */
41
+ export declare const isProviderError: (error: unknown) => error is ProviderError;
@@ -51,3 +51,6 @@ export type { AIContextBudget, AIContextCapacity, AIContextPolicy, AIContextEven
51
51
  export { createAIStoredToolResultRecovery } from "./storedToolResultRecovery";
52
52
  export { createAIProviderRouter } from "./providerRouter";
53
53
  export { streamWithProviderFallback } from "./providerFallback";
54
+ export { isProviderError } from "./errors/providerError";
55
+ export { isAIProviderFailure, isPermanentAIProviderFailure, inspectWithProviderFallback, } from "./providerFallback";
56
+ export type { AIProviderFallback } from "./providerFallback";
@@ -1,3 +1,4 @@
1
+ import { ProviderError } from "./errors/providerError";
1
2
  import type { AIProviderConfig, AIProviderStreamParams } from "../../types/ai";
2
3
  /** Provider-reported limits. Some providers have separate input/output windows. */
3
4
  export type AIModelLimits = {
@@ -20,7 +21,8 @@ export declare class AIInputError extends Error {
20
21
  readonly code: "capacity_unavailable" | "input_too_large" | "unsupported_content";
21
22
  readonly providerStatus?: number | undefined;
22
23
  readonly requestId?: string | undefined;
23
- constructor(code: "capacity_unavailable" | "input_too_large" | "unsupported_content", message: string, providerStatus?: number | undefined, requestId?: string | undefined);
24
+ readonly providerError?: ProviderError | undefined;
25
+ constructor(code: "capacity_unavailable" | "input_too_large" | "unsupported_content", message: string, providerStatus?: number | undefined, requestId?: string | undefined, providerError?: ProviderError | undefined);
24
26
  }
25
27
  export declare const positiveTokenLimit: (value: unknown) => number;
26
28
  export declare const inputTokenCount: (value: unknown) => number;
@@ -28,4 +30,4 @@ export declare const inputTokenCount: (value: unknown) => number;
28
30
  export declare const inspectAIInput: (provider: AIProviderConfig, params: AIProviderStreamParams) => Promise<AIInputCapacity>;
29
31
  /** Cache metadata per provider instance; use a zero TTL with rotating auth/headers. never cache failures. */
30
32
  export declare const cacheModelLimits: (load: (params: AIProviderStreamParams) => Promise<AIModelLimits>, ttlMs?: number) => (params: AIProviderStreamParams) => Promise<AIModelLimits>;
31
- export declare const capacityJson: (response: Response) => Promise<Record<string, unknown>>;
33
+ export declare const capacityJson: (response: Response, provider?: string) => Promise<Record<string, unknown>>;
@@ -1,5 +1,6 @@
1
1
  import { type AIContextBudget } from "./contextPolicy";
2
2
  import type { AIProviderConfig, AIProviderStreamParams, AIUsage } from "../../types/ai";
3
+ import { type AIProviderFallback } from "./providerFallback";
3
4
  export type AITextPreparationCheckpoint = {
4
5
  version: 1;
5
6
  taskHash: string;
@@ -14,6 +15,7 @@ export type PreparedAITextInput = {
14
15
  sectionsProcessed: number;
15
16
  };
16
17
  export type PrepareAITextInputOptions = {
18
+ providerFallback?: AIProviderFallback;
17
19
  contextBudget?: AIContextBudget;
18
20
  /** Optional model on the same provider for source-note preparation only.
19
21
  * The final request retains params.model. Validate quality for your workload
@@ -1,14 +1,33 @@
1
1
  import type { AIChunk } from "../../types/ai";
2
- import { ProviderError } from "./errors/providerError";
3
- /** Execute approved, individually metered alternatives. Never replay a stream
4
- * after any chunk (including tools or usage), or after caller cancellation. */
5
- export declare function streamWithProviderFallback(options: {
6
- models: readonly string[];
7
- stream: (model: string) => AsyncIterable<AIChunk>;
8
- signal?: AbortSignal;
2
+ export type AIProviderFallback = {
3
+ models: readonly string[] | ((model: string) => readonly string[]);
9
4
  onFallback?: (event: {
10
5
  from: string;
11
6
  to: string;
12
- error: ProviderError;
7
+ error: Error;
13
8
  }) => void | Promise<void>;
9
+ };
10
+ export declare const providerFallbackModels: (model: string, recovery?: AIProviderFallback) => string[];
11
+ /** Provider-specific failures may use an approved alternative. Application,
12
+ * cancellation and accounting errors must never be hidden by fallback. */
13
+ export declare const isAIProviderFailure: (error: unknown) => error is Error;
14
+ export declare const isPermanentAIProviderFailure: (error: unknown) => boolean;
15
+ /** Only for side-effect-free checks. Each selected provider performs its own
16
+ * tokenizer/limits validation; no cross-provider token estimate is reused. */
17
+ export declare const inspectWithProviderFallback: <T>(options: {
18
+ model: string;
19
+ recovery?: AIProviderFallback;
20
+ signal?: AbortSignal;
21
+ inspect: (model: string) => Promise<T>;
22
+ }) => Promise<{
23
+ model: string;
24
+ value: T;
25
+ }>;
26
+ /** Never replay a stream after any chunk (including tools or usage), or after
27
+ * caller cancellation. Each candidate owns its own preflight and metering. */
28
+ export declare function streamWithProviderFallback(options: {
29
+ models: readonly string[];
30
+ stream: (model: string) => AsyncIterable<AIChunk>;
31
+ signal?: AbortSignal;
32
+ onFallback?: AIProviderFallback["onFallback"];
14
33
  }): AsyncIterable<AIChunk>;
@@ -0,0 +1,4 @@
1
+ /** Losslessly lower finite-key records (including Zod partialRecord) into
2
+ * named optional properties. Never discard arbitrary key constraints. */
3
+ export declare const openaiSchema: (schema: Record<string, unknown>) => Record<string, unknown>;
4
+ export declare const isStrictCompatible: (schema: unknown) => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@absolutejs/ai",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "homepage": "https://github.com/absolutejs/ai",
5
5
  "bugs": {
6
6
  "url": "https://github.com/absolutejs/ai/issues"