@cuylabs/agent-core 0.5.0 → 0.7.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.
Files changed (57) hide show
  1. package/README.md +85 -372
  2. package/dist/{builder-RcTZuYnO.d.ts → builder-BRvqCcIk.d.ts} +2 -2
  3. package/dist/{resolver-DOfZ-xuk.d.ts → capability-resolver-CgRGsWVX.d.ts} +1 -1
  4. package/dist/{chunk-IMGQOTU2.js → chunk-3HNO5SVI.js} +286 -690
  5. package/dist/chunk-5K7AQVOU.js +619 -0
  6. package/dist/{chunk-QAQADS4X.js → chunk-BNSHUWCV.js} +1 -0
  7. package/dist/{chunk-OTUGSCED.js → chunk-CDTV2UYU.js} +159 -1
  8. package/dist/chunk-IEFIQENH.js +73 -0
  9. package/dist/chunk-N7P4PN3O.js +84 -0
  10. package/dist/{chunk-QWFMX226.js → chunk-QGOGIP7T.js} +148 -15
  11. package/dist/chunk-VNQBHPCT.js +398 -0
  12. package/dist/{chunk-X635CM2F.js → chunk-ZPMACVZK.js} +1 -1
  13. package/dist/context/index.js +1 -1
  14. package/dist/host/index.d.ts +45 -0
  15. package/dist/host/index.js +8 -0
  16. package/dist/{index-p0kOsVsE.d.ts → index-C33hlD6H.d.ts} +12 -7
  17. package/dist/{index-tmhaADz5.d.ts → index-CfBGYrpd.d.ts} +121 -2
  18. package/dist/index.d.ts +107 -126
  19. package/dist/index.js +322 -597
  20. package/dist/inference/index.d.ts +59 -0
  21. package/dist/inference/index.js +25 -0
  22. package/dist/middleware/index.d.ts +8 -4
  23. package/dist/middleware/index.js +5 -3
  24. package/dist/models/index.d.ts +104 -2
  25. package/dist/models/index.js +40 -6
  26. package/dist/prompt/index.d.ts +10 -6
  27. package/dist/reasoning/index.d.ts +54 -8
  28. package/dist/reasoning/index.js +2 -3
  29. package/dist/{registry-CuRWWtcT.d.ts → registry-BDLIHOQB.d.ts} +1 -1
  30. package/dist/{runner-C7aMP_x3.d.ts → runner-DSKaEz3z.d.ts} +290 -7
  31. package/dist/runtime/index.d.ts +41 -7
  32. package/dist/runtime/index.js +15 -6
  33. package/dist/scope/index.d.ts +10 -0
  34. package/dist/scope/index.js +14 -0
  35. package/dist/{session-manager-Uawm2Le7.d.ts → session-manager-B_CWGTsl.d.ts} +1 -1
  36. package/dist/skill/index.d.ts +7 -5
  37. package/dist/storage/index.d.ts +2 -2
  38. package/dist/sub-agent/index.d.ts +12 -8
  39. package/dist/tool/index.d.ts +8 -4
  40. package/dist/tool/index.js +4 -3
  41. package/dist/{tool-pFAnJc5Y.d.ts → tool-Db1Ue-1U.d.ts} +1 -1
  42. package/dist/{tool-DYp6-cC3.d.ts → tool-HUtkiVBx.d.ts} +5 -99
  43. package/dist/tracking/index.d.ts +3 -1
  44. package/dist/types-9jGQUjqW.d.ts +29 -0
  45. package/dist/types-CHiPh8U2.d.ts +100 -0
  46. package/dist/types-CqDZTh4d.d.ts +335 -0
  47. package/dist/types-FRpzzg_9.d.ts +355 -0
  48. package/package.json +19 -8
  49. package/dist/capabilities/index.d.ts +0 -97
  50. package/dist/capabilities/index.js +0 -46
  51. package/dist/chunk-6TDTQJ4P.js +0 -116
  52. package/dist/chunk-DWYX7ASF.js +0 -26
  53. package/dist/chunk-FG4MD5MU.js +0 -54
  54. package/dist/config-D2xeGEHK.d.ts +0 -52
  55. package/dist/identifiers-BLUxFqV_.d.ts +0 -12
  56. package/dist/network-D76DS5ot.d.ts +0 -5
  57. package/dist/types-MM1JoX5T.d.ts +0 -810
@@ -0,0 +1,355 @@
1
+ import { StreamTextResult, ToolSet, Output, LanguageModel, ModelMessage, TelemetrySettings } from 'ai';
2
+ import { T as Tool } from './tool-Db1Ue-1U.js';
3
+ import { T as ToolHost } from './types-CHiPh8U2.js';
4
+ import { S as StreamChunk, d as StreamProvider, M as MiddlewareRunner, e as ModelCallInput } from './runner-DSKaEz3z.js';
5
+ import { R as ReasoningLevel } from './types-CQaXbRsS.js';
6
+ import { d as TurnTrackerContext } from './tool-HUtkiVBx.js';
7
+
8
+ /**
9
+ * Intervention Controller for @cuylabs/agent-core
10
+ *
11
+ * Manages mid-turn message injection into running agent turns.
12
+ * Uses Vercel AI SDK v6's `prepareStep` hook for zero-overhead
13
+ * integration at step boundaries (between LLM calls in a multi-step turn).
14
+ *
15
+ * Instead of polling a queue after each tool execution, this leverages
16
+ * the SDK's native step lifecycle. The
17
+ * `prepareStep` callback fires before every LLM call, giving us a
18
+ * natural injection point with no custom loop machinery.
19
+ *
20
+ * Two injection modes:
21
+ * - **Immediate**: `intervene(msg)` — injected at the next step boundary
22
+ * - **Deferred**: `queueNext(msg)` — held until the turn completes
23
+ */
24
+ /** A queued intervention message waiting to be applied */
25
+ interface PendingIntervention {
26
+ /** Unique identifier for tracking */
27
+ readonly id: string;
28
+ /** The user message to inject */
29
+ readonly message: string;
30
+ /** When the intervention was created */
31
+ readonly createdAt: Date;
32
+ }
33
+ /**
34
+ * Callback fired when an intervention is applied to a step.
35
+ *
36
+ * The agent uses this to push `intervention-applied` events into
37
+ * the streaming event queue so they reach the consumer in the
38
+ * correct order relative to other events.
39
+ */
40
+ type OnInterventionApplied = (intervention: PendingIntervention) => void;
41
+ /**
42
+ * Manages mid-turn message injection for agents.
43
+ *
44
+ * This is the core primitive that connects user redirection intent
45
+ * to the AI SDK's multi-step loop. The flow:
46
+ *
47
+ * 1. Consumer calls `agent.intervene("focus on auth.ts")` from any async context
48
+ * 2. The message is queued as a `PendingIntervention`
49
+ * 3. At the next step boundary, `prepareStep` drains the queue
50
+ * 4. The message is appended to the LLM's input messages
51
+ * 5. The LLM responds to the redirect naturally
52
+ *
53
+ * Thread safety: All operations are synchronous and run on the
54
+ * same event loop. No locks needed — Node.js guarantees ordering.
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const ctrl = new InterventionController();
59
+ *
60
+ * // Queue an intervention (from a UI handler, WebSocket, etc.)
61
+ * const id = ctrl.intervene("stop refactoring, fix the failing test first");
62
+ *
63
+ * // Later, in prepareStep:
64
+ * const pending = ctrl.drainImmediate();
65
+ * // → [{ id, message: "stop refactoring...", createdAt }]
66
+ * ```
67
+ */
68
+ declare class InterventionController {
69
+ /** Immediate interventions — applied at the next step boundary */
70
+ private immediate;
71
+ /** Deferred messages — held until the turn completes */
72
+ private deferred;
73
+ /**
74
+ * Callback fired when an intervention is wired into a step.
75
+ * Set by the Agent before starting a chat turn, cleared after.
76
+ */
77
+ onApplied?: OnInterventionApplied;
78
+ /**
79
+ * Inject a message at the next LLM step boundary.
80
+ *
81
+ * The message is appended as a user message to the conversation
82
+ * before the next LLM call in the current multi-step turn. The
83
+ * LLM will see it and can adjust its behavior accordingly.
84
+ *
85
+ * Safe to call from any async context while `chat()` is running.
86
+ * If called when no turn is active, the message will be picked up
87
+ * by the first step of the next `chat()` call.
88
+ *
89
+ * @param message - The user message to inject
90
+ * @returns Intervention ID for tracking
91
+ */
92
+ intervene(message: string): string;
93
+ /**
94
+ * Drain and return all pending immediate interventions.
95
+ * The internal queue is cleared atomically.
96
+ *
97
+ * @internal Called by the LLM stream's `prepareStep` hook
98
+ */
99
+ drainImmediate(): PendingIntervention[];
100
+ /** Whether there are pending immediate interventions */
101
+ get hasPending(): boolean;
102
+ /** Number of pending immediate interventions */
103
+ get pendingCount(): number;
104
+ /**
105
+ * Queue a message for after the current turn completes.
106
+ *
107
+ * Unlike `intervene()`, this does **not** inject mid-turn. The
108
+ * message is held and available via `drainDeferred()` after
109
+ * `chat()` finishes. The consumer decides whether to send it
110
+ * as a new turn.
111
+ *
112
+ * @param message - The message to queue
113
+ * @returns Intervention ID for tracking
114
+ */
115
+ queueNext(message: string): string;
116
+ /**
117
+ * Drain and return all deferred messages.
118
+ * The internal queue is cleared atomically.
119
+ */
120
+ drainDeferred(): PendingIntervention[];
121
+ /** Whether there are deferred messages */
122
+ get hasDeferred(): boolean;
123
+ /** Number of deferred messages */
124
+ get deferredCount(): number;
125
+ /** Clear all queues (immediate + deferred) */
126
+ clear(): void;
127
+ /** Reset the controller for a new turn (clears onApplied, keeps queues) */
128
+ resetCallbacks(): void;
129
+ }
130
+
131
+ type ErrorCategory = "rate_limit" | "overloaded" | "auth" | "invalid_request" | "context_overflow" | "content_filter" | "network" | "timeout" | "cancelled" | "unknown";
132
+ interface ResponseHeaders {
133
+ "retry-after"?: string;
134
+ "retry-after-ms"?: string;
135
+ "x-ratelimit-remaining"?: string;
136
+ "x-ratelimit-reset"?: string;
137
+ [key: string]: string | undefined;
138
+ }
139
+ interface LLMErrorOptions {
140
+ message: string;
141
+ category?: ErrorCategory;
142
+ status?: number;
143
+ headers?: ResponseHeaders;
144
+ cause?: Error;
145
+ provider?: string;
146
+ model?: string;
147
+ }
148
+
149
+ declare class LLMError extends Error {
150
+ readonly category: ErrorCategory;
151
+ readonly status?: number;
152
+ readonly headers?: ResponseHeaders;
153
+ readonly provider?: string;
154
+ readonly model?: string;
155
+ readonly isRetryable: boolean;
156
+ readonly retryDelayMs?: number;
157
+ constructor(options: LLMErrorOptions);
158
+ static from(error: unknown, context?: Partial<LLMErrorOptions>): LLMError;
159
+ get description(): string;
160
+ }
161
+
162
+ /**
163
+ * Retry logic with exponential backoff for @cuylabs/agent-core
164
+ *
165
+ * Provides configurable retry behavior with header-aware delays,
166
+ * exponential backoff, and abort signal support.
167
+ */
168
+
169
+ /**
170
+ * Retry configuration
171
+ */
172
+ interface RetryConfig {
173
+ /** Maximum number of retry attempts (default: 3) */
174
+ maxAttempts?: number;
175
+ /** Initial delay in ms (default: 2000) */
176
+ initialDelayMs?: number;
177
+ /** Backoff multiplier (default: 2) */
178
+ backoffFactor?: number;
179
+ /** Maximum delay without headers in ms (default: 30000) */
180
+ maxDelayMs?: number;
181
+ /** Whether to jitter delays (default: true) */
182
+ jitter?: boolean;
183
+ /** Callback when retrying */
184
+ onRetry?: (attempt: number, delayMs: number, error: LLMError) => void;
185
+ }
186
+ /**
187
+ * Default retry configuration
188
+ */
189
+ declare const DEFAULT_RETRY_CONFIG: Required<Omit<RetryConfig, "onRetry">>;
190
+ /**
191
+ * Tracks retry state across attempts
192
+ */
193
+ interface RetryState {
194
+ /** Current attempt number (1-indexed) */
195
+ attempt: number;
196
+ /** Total errors encountered */
197
+ errors: LLMError[];
198
+ /** Whether more retries are available */
199
+ canRetry: boolean;
200
+ /** Delay until next retry (if applicable) */
201
+ nextDelayMs?: number;
202
+ }
203
+ /**
204
+ * Creates initial retry state
205
+ */
206
+ declare function createRetryState(): RetryState;
207
+ /**
208
+ * Calculate delay for a retry attempt
209
+ */
210
+ declare function calculateDelay(attempt: number, error: LLMError | undefined, config: Required<Omit<RetryConfig, "onRetry">>): number;
211
+ /**
212
+ * Sleep for a duration, respecting abort signal
213
+ */
214
+ declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
215
+ /**
216
+ * Execute a function with retry logic
217
+ */
218
+ declare function withRetry<T>(fn: (attempt: number) => Promise<T>, config?: RetryConfig, signal?: AbortSignal): Promise<T>;
219
+ /**
220
+ * Options for retry handler
221
+ */
222
+ interface RetryHandlerOptions extends RetryConfig {
223
+ /** Abort signal */
224
+ signal?: AbortSignal;
225
+ }
226
+ /**
227
+ * Creates a retry handler that wraps stream creation
228
+ */
229
+ declare function createRetryHandler(options?: RetryHandlerOptions): <T>(createStream: (attempt: number) => Promise<T>) => Promise<T>;
230
+ /**
231
+ * Checks if more retries should be attempted
232
+ */
233
+ declare function shouldRetry(error: unknown, attempt: number, maxAttempts?: number): boolean;
234
+
235
+ /** Stream result type - uses default Output for text streaming. */
236
+ type InferenceStreamResult = StreamTextResult<ToolSet, Output.Output<string, string, never>>;
237
+ /**
238
+ * @deprecated Use `InferenceStreamResult`.
239
+ */
240
+ type LLMStreamResult = InferenceStreamResult;
241
+
242
+ /**
243
+ * Custom stream result type - compatible shape from external providers.
244
+ */
245
+ type InferenceCustomResult = {
246
+ fullStream: AsyncIterable<StreamChunk>;
247
+ text: Promise<string>;
248
+ usage: Promise<{
249
+ inputTokens: number;
250
+ outputTokens: number;
251
+ totalTokens: number;
252
+ }>;
253
+ finishReason: Promise<string>;
254
+ };
255
+ /** Union type for stream results - either AI SDK or custom. */
256
+ type AnyInferenceResult = InferenceStreamResult | InferenceCustomResult;
257
+ /**
258
+ * @deprecated Use `InferenceCustomResult`.
259
+ */
260
+ type CustomStreamResult = InferenceCustomResult;
261
+ /**
262
+ * @deprecated Use `AnyInferenceResult`.
263
+ */
264
+ type AnyStreamResult = AnyInferenceResult;
265
+ /** Default max output tokens. */
266
+ declare const DEFAULT_MAX_OUTPUT_TOKENS = 32000;
267
+ /**
268
+ * @deprecated Use `DEFAULT_MAX_OUTPUT_TOKENS`.
269
+ */
270
+ declare const OUTPUT_TOKEN_MAX = 32000;
271
+ /**
272
+ * @deprecated Use StreamProvider from types instead.
273
+ */
274
+ type CustomStreamProvider = StreamProvider;
275
+ /** Control whether AI SDK tool definitions auto-execute or only expose calls. */
276
+ type ToolExecutionMode = "auto" | "plan";
277
+ /**
278
+ * Input for model inference stream creation.
279
+ */
280
+ interface InferenceStreamInput {
281
+ /** Session ID */
282
+ sessionID: string;
283
+ /** Step number within the current turn */
284
+ step?: number;
285
+ /** Model to use */
286
+ model: LanguageModel;
287
+ /** System prompt parts */
288
+ system: string[];
289
+ /** Messages to send */
290
+ messages: ModelMessage[];
291
+ /** Abort signal */
292
+ abort: AbortSignal;
293
+ /** Tools to use */
294
+ tools: Record<string, Tool.Info>;
295
+ /** Whether tools should auto-execute or only be surfaced as tool calls. */
296
+ toolExecutionMode?: ToolExecutionMode;
297
+ /** Working directory */
298
+ cwd: string;
299
+ /** Execution environment for tools */
300
+ host?: ToolHost;
301
+ /** Temperature */
302
+ temperature?: number;
303
+ /** Top-p */
304
+ topP?: number;
305
+ /** Max output tokens */
306
+ maxOutputTokens?: number;
307
+ /** Max steps (tool iterations) */
308
+ maxSteps?: number;
309
+ /** Reasoning level for extended-thinking models */
310
+ reasoningLevel?: ReasoningLevel;
311
+ /** Retry configuration */
312
+ retry?: RetryConfig;
313
+ /** Callback for step completion */
314
+ onStepFinish?: (step: InferenceStepInfo) => void | Promise<void>;
315
+ /** Custom stream provider */
316
+ customStreamProvider?: StreamProvider;
317
+ /** Turn tracker for automatic file baseline capture */
318
+ turnTracker?: TurnTrackerContext;
319
+ /** Pre-built MCP tools */
320
+ mcpTools?: ToolSet;
321
+ /** Intervention controller for mid-turn message injection */
322
+ intervention?: InterventionController;
323
+ /** Middleware runner for lifecycle hooks */
324
+ middleware?: MiddlewareRunner;
325
+ /** AI SDK telemetry settings */
326
+ telemetry?: TelemetrySettings;
327
+ /** Internal snapshot of the resolved model request after middleware input hooks. */
328
+ activeModelCall?: ModelCallInput;
329
+ }
330
+ /**
331
+ * Step information surfaced to callers.
332
+ */
333
+ interface InferenceStepInfo {
334
+ toolResults?: Array<{
335
+ toolName: string;
336
+ toolCallId: string;
337
+ output: unknown;
338
+ }>;
339
+ usage?: {
340
+ inputTokens?: number;
341
+ outputTokens?: number;
342
+ totalTokens?: number;
343
+ };
344
+ finishReason?: string;
345
+ }
346
+ /**
347
+ * @deprecated Use `InferenceStreamInput`.
348
+ */
349
+ type LLMStreamInput = InferenceStreamInput;
350
+ /**
351
+ * @deprecated Use `InferenceStepInfo`.
352
+ */
353
+ type StepInfo = InferenceStepInfo;
354
+
355
+ export { type AnyInferenceResult as A, type CustomStreamProvider as C, DEFAULT_MAX_OUTPUT_TOKENS as D, type ErrorCategory as E, type InferenceStreamInput as I, type LLMStreamInput as L, OUTPUT_TOKEN_MAX as O, type PendingIntervention as P, type ResponseHeaders as R, type StepInfo as S, type ToolExecutionMode as T, type AnyStreamResult as a, type CustomStreamResult as b, type InferenceCustomResult as c, type InferenceStepInfo as d, type InferenceStreamResult as e, type LLMStreamResult as f, InterventionController as g, DEFAULT_RETRY_CONFIG as h, LLMError as i, type LLMErrorOptions as j, type OnInterventionApplied as k, type RetryConfig as l, type RetryHandlerOptions as m, type RetryState as n, calculateDelay as o, createRetryHandler as p, createRetryState as q, sleep as r, shouldRetry as s, withRetry as w };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuylabs/agent-core",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Embeddable AI agent infrastructure — execution, sessions, tools, skills, sub-agents, tracing",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,6 +21,11 @@
21
21
  "import": "./dist/runtime/index.js",
22
22
  "default": "./dist/runtime/index.js"
23
23
  },
24
+ "./inference": {
25
+ "types": "./dist/inference/index.d.ts",
26
+ "import": "./dist/inference/index.js",
27
+ "default": "./dist/inference/index.js"
28
+ },
24
29
  "./tracking": {
25
30
  "types": "./dist/tracking/index.d.ts",
26
31
  "import": "./dist/tracking/index.js",
@@ -61,11 +66,6 @@
61
66
  "import": "./dist/reasoning/index.js",
62
67
  "default": "./dist/reasoning/index.js"
63
68
  },
64
- "./capabilities": {
65
- "types": "./dist/capabilities/index.d.ts",
66
- "import": "./dist/capabilities/index.js",
67
- "default": "./dist/capabilities/index.js"
68
- },
69
69
  "./models": {
70
70
  "types": "./dist/models/index.d.ts",
71
71
  "import": "./dist/models/index.js",
@@ -75,6 +75,16 @@
75
75
  "types": "./dist/mcp/index.d.ts",
76
76
  "import": "./dist/mcp/index.js",
77
77
  "default": "./dist/mcp/index.js"
78
+ },
79
+ "./host": {
80
+ "types": "./dist/host/index.d.ts",
81
+ "import": "./dist/host/index.js",
82
+ "default": "./dist/host/index.js"
83
+ },
84
+ "./scope": {
85
+ "types": "./dist/scope/index.d.ts",
86
+ "import": "./dist/scope/index.js",
87
+ "default": "./dist/scope/index.js"
78
88
  }
79
89
  },
80
90
  "files": [
@@ -179,10 +189,11 @@
179
189
  "access": "public"
180
190
  },
181
191
  "scripts": {
182
- "build": "tsup src/index.ts src/tool/index.ts src/runtime/index.ts src/tracking/index.ts src/middleware/index.ts src/prompt/index.ts src/skill/index.ts src/sub-agent/index.ts src/storage/index.ts src/context/index.ts src/reasoning/index.ts src/capabilities/index.ts src/models/index.ts src/mcp/index.ts --format esm --dts --clean",
183
- "dev": "tsup src/index.ts src/tool/index.ts src/runtime/index.ts src/tracking/index.ts src/middleware/index.ts src/prompt/index.ts src/skill/index.ts src/sub-agent/index.ts src/storage/index.ts src/context/index.ts src/reasoning/index.ts src/capabilities/index.ts src/models/index.ts src/mcp/index.ts --format esm --dts --watch",
192
+ "build": "tsup src/index.ts src/tool/index.ts src/runtime/index.ts src/inference/index.ts src/tracking/index.ts src/middleware/index.ts src/prompt/index.ts src/skill/index.ts src/sub-agent/index.ts src/storage/index.ts src/context/index.ts src/reasoning/index.ts src/models/index.ts src/mcp/index.ts src/host/index.ts src/scope/index.ts --format esm --dts --clean",
193
+ "dev": "tsup src/index.ts src/tool/index.ts src/runtime/index.ts src/inference/index.ts src/tracking/index.ts src/middleware/index.ts src/prompt/index.ts src/skill/index.ts src/sub-agent/index.ts src/storage/index.ts src/context/index.ts src/reasoning/index.ts src/models/index.ts src/mcp/index.ts src/host/index.ts src/scope/index.ts --format esm --dts --watch",
184
194
  "test": "vitest run",
185
195
  "test:watch": "vitest",
196
+ "lint": "eslint src/",
186
197
  "typecheck": "tsc --noEmit",
187
198
  "clean": "rm -rf dist"
188
199
  }
@@ -1,97 +0,0 @@
1
- import { C as CapabilitySource, S as SourcePriority, b as SourceResult, P as ProviderCompatibility, R as ResolverOptions, M as ModelEntry, c as ModelCapabilities } from '../resolver-DOfZ-xuk.js';
2
- export { D as DEFAULT_RESOLVER_OPTIONS, I as InputModality, d as ModelCapabilityResolver, N as NetworkStatus, O as OutputModality, f as ResolutionResult, g as configureResolver, e as extractModelId, a as extractProvider, h as getDefaultResolver } from '../resolver-DOfZ-xuk.js';
3
- export { g as getModelId, a as getProviderId } from '../identifiers-BLUxFqV_.js';
4
- export { g as getNetworkStatus } from '../network-D76DS5ot.js';
5
- import 'ai';
6
-
7
- /**
8
- * Pattern-Based Capability Detection for @cuylabs/agent-core
9
- *
10
- * Static pattern matching for model capabilities when no external data is available.
11
- * This is the fallback layer - always works offline.
12
- */
13
-
14
- /**
15
- * Infer provider from model ID
16
- */
17
- declare function inferProvider(modelId: string): string | undefined;
18
- /**
19
- * Pattern-based capability source
20
- * Always available, uses heuristics to detect capabilities
21
- */
22
- declare class PatternCapabilitySource implements CapabilitySource {
23
- readonly priority = SourcePriority.PatternMatch;
24
- readonly name = "Pattern Matching";
25
- lookup(modelId: string, providerHint?: string): Promise<SourceResult>;
26
- isAvailable(): Promise<boolean>;
27
- }
28
- /**
29
- * Quick check if a model ID likely supports reasoning
30
- */
31
- declare function likelySupportsReasoning(modelId: string): boolean;
32
- /**
33
- * Get provider compatibility for a model
34
- */
35
- declare function getProviderCompatibility(modelId: string, provider?: string): ProviderCompatibility | undefined;
36
-
37
- type CapabilityOverrides = ResolverOptions["modelOverrides"];
38
- type OverrideLookup = {
39
- override?: Partial<ModelCapabilities>;
40
- matchedKey?: string;
41
- };
42
- declare function findCapabilityOverride(overrides: CapabilityOverrides, modelId: string, provider?: string): OverrideLookup;
43
- declare function applyCapabilityOverride(entry: ModelEntry, override?: Partial<ModelCapabilities>): ModelEntry;
44
-
45
- declare class CapabilityCache {
46
- private adapter;
47
- private memoryCache;
48
- private ttlMs;
49
- private loaded;
50
- constructor(options?: Partial<ResolverOptions>);
51
- private load;
52
- get(modelId: string, provider?: string): Promise<ModelEntry | undefined>;
53
- set(entry: ModelEntry): Promise<void>;
54
- setMany(entries: ModelEntry[]): Promise<void>;
55
- persist(): Promise<void>;
56
- clear(): Promise<void>;
57
- stats(): {
58
- size: number;
59
- loaded: boolean;
60
- };
61
- getAll(): Promise<ModelEntry[]>;
62
- getAllByProvider(): Promise<Record<string, ModelEntry[]>>;
63
- }
64
- declare class CacheCapabilitySource implements CapabilitySource {
65
- private cache;
66
- readonly priority = SourcePriority.LocalCache;
67
- readonly name = "Local Cache";
68
- constructor(cache: CapabilityCache);
69
- lookup(modelId: string, provider?: string): Promise<SourceResult>;
70
- isAvailable(): Promise<boolean>;
71
- }
72
-
73
- declare class RemoteCapabilityFetcher {
74
- private apiUrl;
75
- private timeoutMs;
76
- private cache;
77
- private lastFetchTime;
78
- private minFetchInterval;
79
- constructor(cache: CapabilityCache, options?: Partial<ResolverOptions>);
80
- fetchAll(): Promise<ModelEntry[]>;
81
- ping(): Promise<boolean>;
82
- }
83
-
84
- declare class RemoteCapabilitySource implements CapabilitySource {
85
- readonly priority = SourcePriority.RemoteAPI;
86
- readonly name = "Remote API (models.dev)";
87
- private fetcher;
88
- private cache;
89
- private fetchPromise;
90
- private enabled;
91
- constructor(cache: CapabilityCache, options?: Partial<ResolverOptions>);
92
- lookup(modelId: string, provider?: string): Promise<SourceResult>;
93
- isAvailable(): Promise<boolean>;
94
- refresh(): Promise<void>;
95
- }
96
-
97
- export { CacheCapabilitySource, CapabilityCache, type CapabilityOverrides, CapabilitySource, ModelCapabilities, ModelEntry, PatternCapabilitySource, ProviderCompatibility, RemoteCapabilityFetcher, RemoteCapabilitySource, ResolverOptions, SourcePriority, SourceResult, applyCapabilityOverride, findCapabilityOverride, getProviderCompatibility, inferProvider, likelySupportsReasoning };
@@ -1,46 +0,0 @@
1
- import {
2
- CacheCapabilitySource,
3
- CapabilityCache,
4
- DEFAULT_RESOLVER_OPTIONS,
5
- ModelCapabilityResolver,
6
- PatternCapabilitySource,
7
- RemoteCapabilityFetcher,
8
- RemoteCapabilitySource,
9
- SourcePriority,
10
- applyCapabilityOverride,
11
- configureResolver,
12
- extractModelId,
13
- extractProvider,
14
- findCapabilityOverride,
15
- getDefaultResolver,
16
- getNetworkStatus,
17
- getProviderCompatibility,
18
- inferProvider,
19
- likelySupportsReasoning
20
- } from "../chunk-QWFMX226.js";
21
- import {
22
- getModelId,
23
- getProviderId
24
- } from "../chunk-DWYX7ASF.js";
25
- export {
26
- CacheCapabilitySource,
27
- CapabilityCache,
28
- DEFAULT_RESOLVER_OPTIONS,
29
- ModelCapabilityResolver,
30
- PatternCapabilitySource,
31
- RemoteCapabilityFetcher,
32
- RemoteCapabilitySource,
33
- SourcePriority,
34
- applyCapabilityOverride,
35
- configureResolver,
36
- extractModelId,
37
- extractProvider,
38
- findCapabilityOverride,
39
- getDefaultResolver,
40
- getModelId,
41
- getNetworkStatus,
42
- getProviderCompatibility,
43
- getProviderId,
44
- inferProvider,
45
- likelySupportsReasoning
46
- };
@@ -1,116 +0,0 @@
1
- // src/models/resolver.ts
2
- function parseKey(input) {
3
- const [engineId, ...rest] = input.split("/");
4
- if (!engineId || rest.length === 0) return null;
5
- return { engineId, modelId: rest.join("/") };
6
- }
7
- function mergeSettings(base, override) {
8
- return {
9
- apiKey: override?.apiKey ?? base?.apiKey,
10
- baseUrl: override?.baseUrl ?? base?.baseUrl,
11
- headers: {
12
- ...base?.headers ?? {},
13
- ...override?.headers ?? {}
14
- },
15
- extra: {
16
- ...base?.extra ?? {},
17
- ...override?.extra ?? {}
18
- }
19
- };
20
- }
21
- function settingsKey(settings, adapter, engineId) {
22
- return JSON.stringify({
23
- engineId,
24
- adapter,
25
- apiKey: settings.apiKey ?? "",
26
- baseUrl: settings.baseUrl ?? "",
27
- headers: settings.headers ?? {},
28
- extra: settings.extra ?? {}
29
- });
30
- }
31
- function buildOptions(settings) {
32
- const opts = { ...settings.extra ?? {} };
33
- if (settings.apiKey) opts.apiKey = settings.apiKey;
34
- if (settings.baseUrl) opts.baseURL = settings.baseUrl;
35
- if (settings.headers && Object.keys(settings.headers).length > 0) opts.headers = settings.headers;
36
- return opts;
37
- }
38
- async function createFactory(adapter, settings) {
39
- const asModel = (m) => m;
40
- const opts = buildOptions(settings);
41
- switch (adapter) {
42
- case "openai": {
43
- const { createOpenAI } = await import("@ai-sdk/openai").catch(() => {
44
- throw new Error(
45
- `Provider "@ai-sdk/openai" is required for the "openai" adapter. Install it with: pnpm add @ai-sdk/openai`
46
- );
47
- });
48
- const provider = createOpenAI(opts);
49
- return (modelId) => provider.languageModel(modelId);
50
- }
51
- case "anthropic": {
52
- const { createAnthropic } = await import("@ai-sdk/anthropic").catch(() => {
53
- throw new Error(
54
- `Provider "@ai-sdk/anthropic" is required for the "anthropic" adapter. Install it with: pnpm add @ai-sdk/anthropic`
55
- );
56
- });
57
- const provider = createAnthropic(opts);
58
- return (modelId) => provider.languageModel(modelId);
59
- }
60
- case "google": {
61
- const { createGoogleGenerativeAI } = await import("@ai-sdk/google").catch(() => {
62
- throw new Error(
63
- `Provider "@ai-sdk/google" is required for the "google" adapter. Install it with: pnpm add @ai-sdk/google`
64
- );
65
- });
66
- const provider = createGoogleGenerativeAI(opts);
67
- return (modelId) => asModel(provider.languageModel(modelId));
68
- }
69
- case "openai-compatible": {
70
- const { createOpenAICompatible } = await import("@ai-sdk/openai-compatible").catch(() => {
71
- throw new Error(
72
- `Provider "@ai-sdk/openai-compatible" is required for the "openai-compatible" adapter. Install it with: pnpm add @ai-sdk/openai-compatible`
73
- );
74
- });
75
- const provider = createOpenAICompatible({
76
- name: opts.name ?? "custom",
77
- baseURL: opts.baseURL ?? "",
78
- ...opts.apiKey ? { apiKey: opts.apiKey } : {},
79
- ...opts.headers ? { headers: opts.headers } : {}
80
- });
81
- return (modelId) => provider.languageModel(modelId);
82
- }
83
- default:
84
- throw new Error(`No factory registered for adapter: ${adapter}`);
85
- }
86
- }
87
- function createResolver(directory) {
88
- const factoryCache = /* @__PURE__ */ new Map();
89
- return async (key) => {
90
- const parsed = parseKey(key);
91
- const entry = parsed ? void 0 : directory.entries?.[key];
92
- const engineId = parsed?.engineId ?? entry?.engine;
93
- const modelId = parsed?.modelId ?? entry?.id;
94
- if (!engineId || !modelId) {
95
- throw new Error(`Unknown model reference: ${key}`);
96
- }
97
- const engine = directory.engines[engineId];
98
- if (!engine) {
99
- throw new Error(`Unknown engine: ${engineId}`);
100
- }
101
- const settings = mergeSettings(engine.settings, entry?.settings);
102
- if (engine.build) {
103
- return engine.build(modelId, settings);
104
- }
105
- const cacheKey = settingsKey(settings, engine.adapter, engineId);
106
- const cached = factoryCache.get(cacheKey);
107
- if (cached) return cached(modelId);
108
- const factory = await createFactory(engine.adapter, settings);
109
- factoryCache.set(cacheKey, factory);
110
- return factory(modelId);
111
- };
112
- }
113
-
114
- export {
115
- createResolver
116
- };