@x12i/ai-dispatcher 1.4.0 → 2.0.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/README.md +160 -31
- package/dist/index.cjs +2118 -198
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +157 -7
- package/dist/index.d.ts +157 -7
- package/dist/index.js +2127 -200
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,83 @@
|
|
|
1
1
|
import * as _x12i_openrouter_runtime from '@x12i/openrouter-runtime';
|
|
2
|
-
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
|
-
export { CompiledOpenRouterRequest, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent } from '@x12i/openrouter-runtime';
|
|
2
|
+
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeUsage, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
|
+
export { CompiledOpenRouterRequest, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent, decodeProviderMetadata } from '@x12i/openrouter-runtime';
|
|
4
4
|
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
5
5
|
export { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
6
6
|
import { CompiledBedrockRequest, BedrockRuntimeOptions } from '@x12i/bedrock-runtime';
|
|
7
7
|
export { BedrockClientLike, BedrockCredentials, BedrockRuntimeOptions, CompiledBedrockRequest } from '@x12i/bedrock-runtime';
|
|
8
8
|
|
|
9
|
-
type AiProviderId = "openrouter" | "openai" | "anthropic" | "google" | "azure" | "bedrock" | "groq" | "mistral" | "cohere" | "together" | "fireworks" | "xai" | "deepseek" | "custom";
|
|
10
|
-
declare const IMPLEMENTED_AI_PROVIDERS: readonly ["openrouter", "bedrock", "openai"];
|
|
9
|
+
type AiProviderId = "openrouter" | "openai" | "anthropic" | "google" | "azure" | "bedrock" | "groq" | "mistral" | "cohere" | "together" | "fireworks" | "xai" | "deepseek" | "cloudflare" | "custom";
|
|
10
|
+
declare const IMPLEMENTED_AI_PROVIDERS: readonly ["openrouter", "bedrock", "openai", "cloudflare"];
|
|
11
11
|
type ImplementedAiProvider = (typeof IMPLEMENTED_AI_PROVIDERS)[number];
|
|
12
12
|
declare function isImplementedAiProvider(provider: string): provider is ImplementedAiProvider;
|
|
13
13
|
|
|
14
|
+
/** Concrete Cloudflare AI REST endpoint. */
|
|
15
|
+
type CloudflareEndpoint = "chat" | "responses" | "messages" | "run";
|
|
16
|
+
/** `auto` picks chat, responses, or messages from the model id. */
|
|
17
|
+
type CloudflareEndpointChoice = CloudflareEndpoint | "auto";
|
|
18
|
+
type CloudflareBackoff = "constant" | "linear" | "exponential";
|
|
19
|
+
type CloudflareWebhookFormat = "raw" | "chat";
|
|
20
|
+
/** Per-request AI Gateway headers (`cf-aig-*`). */
|
|
21
|
+
interface CloudflareGatewayControls {
|
|
22
|
+
skipCache?: boolean;
|
|
23
|
+
/** Seconds. Sent as `cf-aig-cache-ttl`. */
|
|
24
|
+
cacheTtlSeconds?: number;
|
|
25
|
+
cacheKey?: string;
|
|
26
|
+
collectLog?: boolean;
|
|
27
|
+
requestTimeoutMs?: number;
|
|
28
|
+
/** Sent as `cf-aig-max-attempts`. Cloudflare caps this at 5. */
|
|
29
|
+
maxGatewayAttempts?: number;
|
|
30
|
+
/** Sent as `cf-aig-retry-delay`. Cloudflare caps this at 60000. */
|
|
31
|
+
retryDelayMs?: number;
|
|
32
|
+
backoff?: CloudflareBackoff;
|
|
33
|
+
/** Merged into `cf-aig-metadata`. Request metadata wins on the same key. */
|
|
34
|
+
metadata?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
/** `/ai/run` background execution. `webhookUrl` requires `background: true`. */
|
|
37
|
+
interface CloudflareRunOptions {
|
|
38
|
+
background?: boolean;
|
|
39
|
+
webhookUrl?: string;
|
|
40
|
+
webhookFormat?: CloudflareWebhookFormat;
|
|
41
|
+
}
|
|
42
|
+
/** Per-request overrides. Credentials stay on dispatcher options. */
|
|
43
|
+
interface CloudflareRequestOptions extends CloudflareGatewayControls {
|
|
44
|
+
endpoint?: CloudflareEndpointChoice;
|
|
45
|
+
/** Workers AI (`@cf/`) defaults to `"default"` when this is omitted. */
|
|
46
|
+
gatewayId?: string;
|
|
47
|
+
run?: CloudflareRunOptions;
|
|
48
|
+
}
|
|
49
|
+
interface CloudflareRuntimeOptions extends CloudflareRequestOptions {
|
|
50
|
+
/** Or `CLOUDFLARE_API_TOKEN`. Needs Account > Workers AI > Read. */
|
|
51
|
+
apiToken?: string;
|
|
52
|
+
/** Or `CLOUDFLARE_ACCOUNT_ID`. */
|
|
53
|
+
accountId?: string;
|
|
54
|
+
/** Defaults to `https://api.cloudflare.com/client/v4`. */
|
|
55
|
+
baseUrl?: string;
|
|
56
|
+
defaultModel?: string;
|
|
57
|
+
timeoutMs?: number;
|
|
58
|
+
/** HTTP attempts for 429, 408, and 5xx. Defaults to 2. */
|
|
59
|
+
maxAttempts?: number;
|
|
60
|
+
fetch?: typeof globalThis.fetch;
|
|
61
|
+
/** Executors for function tools that do not carry their own `executor`. */
|
|
62
|
+
tools?: Record<string, RuntimeToolExecutor>;
|
|
63
|
+
logger?: RuntimeLogger;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolved call settings passed from preparation into the adapter.
|
|
67
|
+
* Request fields win over dispatcher options. `cache` comes from the dispatcher cache policy.
|
|
68
|
+
*/
|
|
69
|
+
interface CloudflareCallContext {
|
|
70
|
+
endpoint?: CloudflareEndpoint;
|
|
71
|
+
gatewayId?: string;
|
|
72
|
+
run?: CloudflareRunOptions;
|
|
73
|
+
gateway?: CloudflareGatewayControls;
|
|
74
|
+
cache?: {
|
|
75
|
+
skip: boolean;
|
|
76
|
+
ttlSeconds?: number;
|
|
77
|
+
key?: string;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
14
81
|
interface OpenAiRuntimeOptions {
|
|
15
82
|
apiKey?: string;
|
|
16
83
|
/** Defaults to `https://api.openai.com/v1`. Requests are sent to `{baseUrl}/responses`. */
|
|
@@ -39,8 +106,52 @@ interface ReasoningResolution {
|
|
|
39
106
|
outcome?: ReasoningOutcome;
|
|
40
107
|
bypassed: boolean;
|
|
41
108
|
}
|
|
109
|
+
/** How a call asks for prompt caching. Omitted `mode` means `explicit`. */
|
|
110
|
+
type CacheMode = "auto" | "explicit" | "disabled";
|
|
111
|
+
/** Normalized cache lifetime. Providers fold this to a supported value. */
|
|
112
|
+
type CacheTtl = "5m" | "30m" | "1h";
|
|
113
|
+
/**
|
|
114
|
+
* `applied` — the requested caching was written.
|
|
115
|
+
* `degraded` — a nearby TTL was written and the call still runs.
|
|
116
|
+
* `ignored` — no cache marker was written and the call still runs.
|
|
117
|
+
*/
|
|
118
|
+
type CacheOutcome = "applied" | "degraded" | "ignored";
|
|
119
|
+
/**
|
|
120
|
+
* Provider-neutral prompt-cache intent.
|
|
121
|
+
* One explicit breakpoint is placed at the end of the stable prefix
|
|
122
|
+
* (`system` or `instructions`) when the model supports it.
|
|
123
|
+
*/
|
|
124
|
+
interface CachePolicy {
|
|
125
|
+
mode?: CacheMode;
|
|
126
|
+
/** Stable id for caches that route on a key. Omitted from the wire when the dialect has no key field. */
|
|
127
|
+
key?: string;
|
|
128
|
+
ttl?: CacheTtl;
|
|
129
|
+
}
|
|
130
|
+
/** What the dispatcher did with `cache` for one call. */
|
|
131
|
+
interface CacheResolution {
|
|
132
|
+
mode: CacheMode;
|
|
133
|
+
target: ImplementedAiProvider;
|
|
134
|
+
requestedModel: string;
|
|
135
|
+
effectiveModel: string;
|
|
136
|
+
outcome: CacheOutcome;
|
|
137
|
+
/** TTL written on the wire. Omitted when no TTL was sent. */
|
|
138
|
+
ttl?: CacheTtl;
|
|
139
|
+
keyPresent: boolean;
|
|
140
|
+
}
|
|
42
141
|
interface AiDispatcherRequest extends RuntimeRequest {
|
|
43
142
|
provider?: AiProviderId;
|
|
143
|
+
/**
|
|
144
|
+
* Caller organization id. Stored in the one provider metadata record as `orgId`.
|
|
145
|
+
*/
|
|
146
|
+
orgId?: string;
|
|
147
|
+
/**
|
|
148
|
+
* Caller step id. Stored in the one provider metadata record as `stepId`.
|
|
149
|
+
*/
|
|
150
|
+
stepId?: string;
|
|
151
|
+
/**
|
|
152
|
+
* Caller skill id. Stored in the one provider metadata record as `skillId`.
|
|
153
|
+
*/
|
|
154
|
+
skillId?: string;
|
|
44
155
|
/**
|
|
45
156
|
* Provider-neutral reasoning intent. The bundled catalog chooses the wire action.
|
|
46
157
|
* An explicit provider control on the same request wins and the catalog is not called.
|
|
@@ -59,6 +170,17 @@ interface AiDispatcherRequest extends RuntimeRequest {
|
|
|
59
170
|
* A string list attaches only those exposed names.
|
|
60
171
|
*/
|
|
61
172
|
mcp?: false | string[];
|
|
173
|
+
/**
|
|
174
|
+
* Optional prompt-cache intent. Omit it and the compiled body is unchanged.
|
|
175
|
+
* `explicit` marks the end of `system` or `instructions` when the model supports it.
|
|
176
|
+
* On Cloudflare this also sets AI Gateway cache headers.
|
|
177
|
+
*/
|
|
178
|
+
cache?: CachePolicy;
|
|
179
|
+
/**
|
|
180
|
+
* Cloudflare AI REST overrides for this call.
|
|
181
|
+
* `endpoint` wins over `apiMode`. Stripped before the provider request is compiled.
|
|
182
|
+
*/
|
|
183
|
+
cloudflare?: CloudflareRequestOptions;
|
|
62
184
|
}
|
|
63
185
|
interface DispatcherMcpTool {
|
|
64
186
|
name: string;
|
|
@@ -82,13 +204,15 @@ interface DispatcherMcpOptions {
|
|
|
82
204
|
interface AiDispatcherOptions {
|
|
83
205
|
/**
|
|
84
206
|
* Default provider for run / stream / compile.
|
|
85
|
-
* Implemented: `"openrouter"` | `"bedrock"` | `"openai"`.
|
|
207
|
+
* Implemented: `"openrouter"` | `"bedrock"` | `"openai"` | `"cloudflare"`.
|
|
86
208
|
*/
|
|
87
209
|
provider?: AiProviderId;
|
|
88
210
|
openrouter?: OpenRouterRuntimeOptions;
|
|
89
211
|
bedrock?: BedrockRuntimeOptions;
|
|
90
212
|
/** Direct OpenAI Responses API. */
|
|
91
213
|
openai?: OpenAiRuntimeOptions;
|
|
214
|
+
/** Cloudflare AI REST API: Workers AI and third-party models. */
|
|
215
|
+
cloudflare?: CloudflareRuntimeOptions;
|
|
92
216
|
/** Connected MCP servers. The dispatcher does not open transports. */
|
|
93
217
|
mcp?: DispatcherMcpOptions;
|
|
94
218
|
logger?: RuntimeLogger;
|
|
@@ -102,27 +226,53 @@ interface CompiledOpenAiRequest {
|
|
|
102
226
|
requestedModel: string;
|
|
103
227
|
effectiveModel: string;
|
|
104
228
|
reasoningResolution?: ReasoningResolution;
|
|
229
|
+
cacheResolution?: CacheResolution;
|
|
230
|
+
}
|
|
231
|
+
interface CompiledCloudflareRequest {
|
|
232
|
+
provider: "cloudflare";
|
|
233
|
+
/** REST dialect selected for this call. */
|
|
234
|
+
endpoint: "chat" | "responses" | "messages" | "run";
|
|
235
|
+
url: string;
|
|
236
|
+
headers: Record<string, string>;
|
|
237
|
+
body: Record<string, unknown>;
|
|
238
|
+
warnings: _x12i_openrouter_runtime.RuntimeWarning[];
|
|
239
|
+
requestedModel: string;
|
|
240
|
+
effectiveModel: string;
|
|
241
|
+
reasoningResolution?: ReasoningResolution;
|
|
242
|
+
cacheResolution?: CacheResolution;
|
|
105
243
|
}
|
|
106
244
|
type CompiledAiRequest = (CompiledOpenRouterRequest & {
|
|
107
245
|
provider: "openrouter";
|
|
108
246
|
requestedModel: string;
|
|
109
247
|
effectiveModel: string;
|
|
110
248
|
reasoningResolution?: ReasoningResolution;
|
|
249
|
+
cacheResolution?: CacheResolution;
|
|
111
250
|
}) | (CompiledBedrockRequest & {
|
|
112
251
|
provider: "bedrock";
|
|
113
252
|
requestedModel: string;
|
|
114
253
|
effectiveModel: string;
|
|
115
254
|
reasoningResolution?: ReasoningResolution;
|
|
116
|
-
|
|
255
|
+
cacheResolution?: CacheResolution;
|
|
256
|
+
}) | CompiledOpenAiRequest | CompiledCloudflareRequest;
|
|
257
|
+
interface AiDispatcherUsage extends RuntimeUsage {
|
|
258
|
+
/** Tokens read from a prompt cache. Omitted when the provider did not report any. */
|
|
259
|
+
cachedTokens?: number;
|
|
260
|
+
/** Tokens written to a prompt cache. Omitted when the provider did not report any. */
|
|
261
|
+
cacheWriteTokens?: number;
|
|
262
|
+
}
|
|
117
263
|
interface AiDispatcherResponse extends RuntimeResponse {
|
|
118
264
|
/** Provider-exposed reasoning text. Omitted when the provider did not return any. */
|
|
119
265
|
reasoningText?: string;
|
|
120
266
|
reasoningResolution?: ReasoningResolution;
|
|
267
|
+
cacheResolution?: CacheResolution;
|
|
268
|
+
usage?: AiDispatcherUsage;
|
|
121
269
|
}
|
|
122
270
|
interface ProviderCallOptions {
|
|
123
271
|
finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
|
|
124
272
|
extractStreamReasoning?: (event: unknown) => string | undefined;
|
|
125
273
|
suppressNestedReasoningFallback?: boolean;
|
|
274
|
+
/** Set by preparation for the Cloudflare adapter. Callers do not set this. */
|
|
275
|
+
cloudflare?: CloudflareCallContext;
|
|
126
276
|
}
|
|
127
277
|
interface AiProviderAdapter {
|
|
128
278
|
run(request: RuntimeRequest, call?: ProviderCallOptions): Promise<RuntimeResponse>;
|
|
@@ -161,4 +311,4 @@ declare function stripReasoningHistoryFields<T>(message: T, paths: readonly stri
|
|
|
161
311
|
|
|
162
312
|
declare function resolveProvider(requestProvider: AiProviderId | undefined, defaultProvider: AiProviderId): ImplementedAiProvider;
|
|
163
313
|
|
|
164
|
-
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiProviderAdapter, type AiProviderId, type CompiledAiRequest, type CompiledOpenAiRequest, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
|
314
|
+
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,83 @@
|
|
|
1
1
|
import * as _x12i_openrouter_runtime from '@x12i/openrouter-runtime';
|
|
2
|
-
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
|
-
export { CompiledOpenRouterRequest, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent } from '@x12i/openrouter-runtime';
|
|
2
|
+
import { RuntimeToolExecutor, RuntimeLogger, RuntimeRequest, RuntimeResponse, RuntimeUsage, RuntimeStreamEvent, CompiledOpenRouterRequest, OpenRouterRuntimeOptions } from '@x12i/openrouter-runtime';
|
|
3
|
+
export { CompiledOpenRouterRequest, OpenRouterRuntimeOptions, RuntimeDefaults, RuntimeLogger, RuntimeMessage, RuntimeRequest, RuntimeResponse, RuntimeServerToolsPolicy, RuntimeStreamEvent, decodeProviderMetadata } from '@x12i/openrouter-runtime';
|
|
4
4
|
import { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
5
5
|
export { ReasoningEffort, ReasoningOutcome } from '@x12i/ai-profiles';
|
|
6
6
|
import { CompiledBedrockRequest, BedrockRuntimeOptions } from '@x12i/bedrock-runtime';
|
|
7
7
|
export { BedrockClientLike, BedrockCredentials, BedrockRuntimeOptions, CompiledBedrockRequest } from '@x12i/bedrock-runtime';
|
|
8
8
|
|
|
9
|
-
type AiProviderId = "openrouter" | "openai" | "anthropic" | "google" | "azure" | "bedrock" | "groq" | "mistral" | "cohere" | "together" | "fireworks" | "xai" | "deepseek" | "custom";
|
|
10
|
-
declare const IMPLEMENTED_AI_PROVIDERS: readonly ["openrouter", "bedrock", "openai"];
|
|
9
|
+
type AiProviderId = "openrouter" | "openai" | "anthropic" | "google" | "azure" | "bedrock" | "groq" | "mistral" | "cohere" | "together" | "fireworks" | "xai" | "deepseek" | "cloudflare" | "custom";
|
|
10
|
+
declare const IMPLEMENTED_AI_PROVIDERS: readonly ["openrouter", "bedrock", "openai", "cloudflare"];
|
|
11
11
|
type ImplementedAiProvider = (typeof IMPLEMENTED_AI_PROVIDERS)[number];
|
|
12
12
|
declare function isImplementedAiProvider(provider: string): provider is ImplementedAiProvider;
|
|
13
13
|
|
|
14
|
+
/** Concrete Cloudflare AI REST endpoint. */
|
|
15
|
+
type CloudflareEndpoint = "chat" | "responses" | "messages" | "run";
|
|
16
|
+
/** `auto` picks chat, responses, or messages from the model id. */
|
|
17
|
+
type CloudflareEndpointChoice = CloudflareEndpoint | "auto";
|
|
18
|
+
type CloudflareBackoff = "constant" | "linear" | "exponential";
|
|
19
|
+
type CloudflareWebhookFormat = "raw" | "chat";
|
|
20
|
+
/** Per-request AI Gateway headers (`cf-aig-*`). */
|
|
21
|
+
interface CloudflareGatewayControls {
|
|
22
|
+
skipCache?: boolean;
|
|
23
|
+
/** Seconds. Sent as `cf-aig-cache-ttl`. */
|
|
24
|
+
cacheTtlSeconds?: number;
|
|
25
|
+
cacheKey?: string;
|
|
26
|
+
collectLog?: boolean;
|
|
27
|
+
requestTimeoutMs?: number;
|
|
28
|
+
/** Sent as `cf-aig-max-attempts`. Cloudflare caps this at 5. */
|
|
29
|
+
maxGatewayAttempts?: number;
|
|
30
|
+
/** Sent as `cf-aig-retry-delay`. Cloudflare caps this at 60000. */
|
|
31
|
+
retryDelayMs?: number;
|
|
32
|
+
backoff?: CloudflareBackoff;
|
|
33
|
+
/** Merged into `cf-aig-metadata`. Request metadata wins on the same key. */
|
|
34
|
+
metadata?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
/** `/ai/run` background execution. `webhookUrl` requires `background: true`. */
|
|
37
|
+
interface CloudflareRunOptions {
|
|
38
|
+
background?: boolean;
|
|
39
|
+
webhookUrl?: string;
|
|
40
|
+
webhookFormat?: CloudflareWebhookFormat;
|
|
41
|
+
}
|
|
42
|
+
/** Per-request overrides. Credentials stay on dispatcher options. */
|
|
43
|
+
interface CloudflareRequestOptions extends CloudflareGatewayControls {
|
|
44
|
+
endpoint?: CloudflareEndpointChoice;
|
|
45
|
+
/** Workers AI (`@cf/`) defaults to `"default"` when this is omitted. */
|
|
46
|
+
gatewayId?: string;
|
|
47
|
+
run?: CloudflareRunOptions;
|
|
48
|
+
}
|
|
49
|
+
interface CloudflareRuntimeOptions extends CloudflareRequestOptions {
|
|
50
|
+
/** Or `CLOUDFLARE_API_TOKEN`. Needs Account > Workers AI > Read. */
|
|
51
|
+
apiToken?: string;
|
|
52
|
+
/** Or `CLOUDFLARE_ACCOUNT_ID`. */
|
|
53
|
+
accountId?: string;
|
|
54
|
+
/** Defaults to `https://api.cloudflare.com/client/v4`. */
|
|
55
|
+
baseUrl?: string;
|
|
56
|
+
defaultModel?: string;
|
|
57
|
+
timeoutMs?: number;
|
|
58
|
+
/** HTTP attempts for 429, 408, and 5xx. Defaults to 2. */
|
|
59
|
+
maxAttempts?: number;
|
|
60
|
+
fetch?: typeof globalThis.fetch;
|
|
61
|
+
/** Executors for function tools that do not carry their own `executor`. */
|
|
62
|
+
tools?: Record<string, RuntimeToolExecutor>;
|
|
63
|
+
logger?: RuntimeLogger;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Resolved call settings passed from preparation into the adapter.
|
|
67
|
+
* Request fields win over dispatcher options. `cache` comes from the dispatcher cache policy.
|
|
68
|
+
*/
|
|
69
|
+
interface CloudflareCallContext {
|
|
70
|
+
endpoint?: CloudflareEndpoint;
|
|
71
|
+
gatewayId?: string;
|
|
72
|
+
run?: CloudflareRunOptions;
|
|
73
|
+
gateway?: CloudflareGatewayControls;
|
|
74
|
+
cache?: {
|
|
75
|
+
skip: boolean;
|
|
76
|
+
ttlSeconds?: number;
|
|
77
|
+
key?: string;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
14
81
|
interface OpenAiRuntimeOptions {
|
|
15
82
|
apiKey?: string;
|
|
16
83
|
/** Defaults to `https://api.openai.com/v1`. Requests are sent to `{baseUrl}/responses`. */
|
|
@@ -39,8 +106,52 @@ interface ReasoningResolution {
|
|
|
39
106
|
outcome?: ReasoningOutcome;
|
|
40
107
|
bypassed: boolean;
|
|
41
108
|
}
|
|
109
|
+
/** How a call asks for prompt caching. Omitted `mode` means `explicit`. */
|
|
110
|
+
type CacheMode = "auto" | "explicit" | "disabled";
|
|
111
|
+
/** Normalized cache lifetime. Providers fold this to a supported value. */
|
|
112
|
+
type CacheTtl = "5m" | "30m" | "1h";
|
|
113
|
+
/**
|
|
114
|
+
* `applied` — the requested caching was written.
|
|
115
|
+
* `degraded` — a nearby TTL was written and the call still runs.
|
|
116
|
+
* `ignored` — no cache marker was written and the call still runs.
|
|
117
|
+
*/
|
|
118
|
+
type CacheOutcome = "applied" | "degraded" | "ignored";
|
|
119
|
+
/**
|
|
120
|
+
* Provider-neutral prompt-cache intent.
|
|
121
|
+
* One explicit breakpoint is placed at the end of the stable prefix
|
|
122
|
+
* (`system` or `instructions`) when the model supports it.
|
|
123
|
+
*/
|
|
124
|
+
interface CachePolicy {
|
|
125
|
+
mode?: CacheMode;
|
|
126
|
+
/** Stable id for caches that route on a key. Omitted from the wire when the dialect has no key field. */
|
|
127
|
+
key?: string;
|
|
128
|
+
ttl?: CacheTtl;
|
|
129
|
+
}
|
|
130
|
+
/** What the dispatcher did with `cache` for one call. */
|
|
131
|
+
interface CacheResolution {
|
|
132
|
+
mode: CacheMode;
|
|
133
|
+
target: ImplementedAiProvider;
|
|
134
|
+
requestedModel: string;
|
|
135
|
+
effectiveModel: string;
|
|
136
|
+
outcome: CacheOutcome;
|
|
137
|
+
/** TTL written on the wire. Omitted when no TTL was sent. */
|
|
138
|
+
ttl?: CacheTtl;
|
|
139
|
+
keyPresent: boolean;
|
|
140
|
+
}
|
|
42
141
|
interface AiDispatcherRequest extends RuntimeRequest {
|
|
43
142
|
provider?: AiProviderId;
|
|
143
|
+
/**
|
|
144
|
+
* Caller organization id. Stored in the one provider metadata record as `orgId`.
|
|
145
|
+
*/
|
|
146
|
+
orgId?: string;
|
|
147
|
+
/**
|
|
148
|
+
* Caller step id. Stored in the one provider metadata record as `stepId`.
|
|
149
|
+
*/
|
|
150
|
+
stepId?: string;
|
|
151
|
+
/**
|
|
152
|
+
* Caller skill id. Stored in the one provider metadata record as `skillId`.
|
|
153
|
+
*/
|
|
154
|
+
skillId?: string;
|
|
44
155
|
/**
|
|
45
156
|
* Provider-neutral reasoning intent. The bundled catalog chooses the wire action.
|
|
46
157
|
* An explicit provider control on the same request wins and the catalog is not called.
|
|
@@ -59,6 +170,17 @@ interface AiDispatcherRequest extends RuntimeRequest {
|
|
|
59
170
|
* A string list attaches only those exposed names.
|
|
60
171
|
*/
|
|
61
172
|
mcp?: false | string[];
|
|
173
|
+
/**
|
|
174
|
+
* Optional prompt-cache intent. Omit it and the compiled body is unchanged.
|
|
175
|
+
* `explicit` marks the end of `system` or `instructions` when the model supports it.
|
|
176
|
+
* On Cloudflare this also sets AI Gateway cache headers.
|
|
177
|
+
*/
|
|
178
|
+
cache?: CachePolicy;
|
|
179
|
+
/**
|
|
180
|
+
* Cloudflare AI REST overrides for this call.
|
|
181
|
+
* `endpoint` wins over `apiMode`. Stripped before the provider request is compiled.
|
|
182
|
+
*/
|
|
183
|
+
cloudflare?: CloudflareRequestOptions;
|
|
62
184
|
}
|
|
63
185
|
interface DispatcherMcpTool {
|
|
64
186
|
name: string;
|
|
@@ -82,13 +204,15 @@ interface DispatcherMcpOptions {
|
|
|
82
204
|
interface AiDispatcherOptions {
|
|
83
205
|
/**
|
|
84
206
|
* Default provider for run / stream / compile.
|
|
85
|
-
* Implemented: `"openrouter"` | `"bedrock"` | `"openai"`.
|
|
207
|
+
* Implemented: `"openrouter"` | `"bedrock"` | `"openai"` | `"cloudflare"`.
|
|
86
208
|
*/
|
|
87
209
|
provider?: AiProviderId;
|
|
88
210
|
openrouter?: OpenRouterRuntimeOptions;
|
|
89
211
|
bedrock?: BedrockRuntimeOptions;
|
|
90
212
|
/** Direct OpenAI Responses API. */
|
|
91
213
|
openai?: OpenAiRuntimeOptions;
|
|
214
|
+
/** Cloudflare AI REST API: Workers AI and third-party models. */
|
|
215
|
+
cloudflare?: CloudflareRuntimeOptions;
|
|
92
216
|
/** Connected MCP servers. The dispatcher does not open transports. */
|
|
93
217
|
mcp?: DispatcherMcpOptions;
|
|
94
218
|
logger?: RuntimeLogger;
|
|
@@ -102,27 +226,53 @@ interface CompiledOpenAiRequest {
|
|
|
102
226
|
requestedModel: string;
|
|
103
227
|
effectiveModel: string;
|
|
104
228
|
reasoningResolution?: ReasoningResolution;
|
|
229
|
+
cacheResolution?: CacheResolution;
|
|
230
|
+
}
|
|
231
|
+
interface CompiledCloudflareRequest {
|
|
232
|
+
provider: "cloudflare";
|
|
233
|
+
/** REST dialect selected for this call. */
|
|
234
|
+
endpoint: "chat" | "responses" | "messages" | "run";
|
|
235
|
+
url: string;
|
|
236
|
+
headers: Record<string, string>;
|
|
237
|
+
body: Record<string, unknown>;
|
|
238
|
+
warnings: _x12i_openrouter_runtime.RuntimeWarning[];
|
|
239
|
+
requestedModel: string;
|
|
240
|
+
effectiveModel: string;
|
|
241
|
+
reasoningResolution?: ReasoningResolution;
|
|
242
|
+
cacheResolution?: CacheResolution;
|
|
105
243
|
}
|
|
106
244
|
type CompiledAiRequest = (CompiledOpenRouterRequest & {
|
|
107
245
|
provider: "openrouter";
|
|
108
246
|
requestedModel: string;
|
|
109
247
|
effectiveModel: string;
|
|
110
248
|
reasoningResolution?: ReasoningResolution;
|
|
249
|
+
cacheResolution?: CacheResolution;
|
|
111
250
|
}) | (CompiledBedrockRequest & {
|
|
112
251
|
provider: "bedrock";
|
|
113
252
|
requestedModel: string;
|
|
114
253
|
effectiveModel: string;
|
|
115
254
|
reasoningResolution?: ReasoningResolution;
|
|
116
|
-
|
|
255
|
+
cacheResolution?: CacheResolution;
|
|
256
|
+
}) | CompiledOpenAiRequest | CompiledCloudflareRequest;
|
|
257
|
+
interface AiDispatcherUsage extends RuntimeUsage {
|
|
258
|
+
/** Tokens read from a prompt cache. Omitted when the provider did not report any. */
|
|
259
|
+
cachedTokens?: number;
|
|
260
|
+
/** Tokens written to a prompt cache. Omitted when the provider did not report any. */
|
|
261
|
+
cacheWriteTokens?: number;
|
|
262
|
+
}
|
|
117
263
|
interface AiDispatcherResponse extends RuntimeResponse {
|
|
118
264
|
/** Provider-exposed reasoning text. Omitted when the provider did not return any. */
|
|
119
265
|
reasoningText?: string;
|
|
120
266
|
reasoningResolution?: ReasoningResolution;
|
|
267
|
+
cacheResolution?: CacheResolution;
|
|
268
|
+
usage?: AiDispatcherUsage;
|
|
121
269
|
}
|
|
122
270
|
interface ProviderCallOptions {
|
|
123
271
|
finalizeBody?: (body: Record<string, unknown>) => Record<string, unknown>;
|
|
124
272
|
extractStreamReasoning?: (event: unknown) => string | undefined;
|
|
125
273
|
suppressNestedReasoningFallback?: boolean;
|
|
274
|
+
/** Set by preparation for the Cloudflare adapter. Callers do not set this. */
|
|
275
|
+
cloudflare?: CloudflareCallContext;
|
|
126
276
|
}
|
|
127
277
|
interface AiProviderAdapter {
|
|
128
278
|
run(request: RuntimeRequest, call?: ProviderCallOptions): Promise<RuntimeResponse>;
|
|
@@ -161,4 +311,4 @@ declare function stripReasoningHistoryFields<T>(message: T, paths: readonly stri
|
|
|
161
311
|
|
|
162
312
|
declare function resolveProvider(requestProvider: AiProviderId | undefined, defaultProvider: AiProviderId): ImplementedAiProvider;
|
|
163
313
|
|
|
164
|
-
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiProviderAdapter, type AiProviderId, type CompiledAiRequest, type CompiledOpenAiRequest, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|
|
314
|
+
export { type AiDispatcher, AiDispatcherError, type AiDispatcherOptions, type AiDispatcherRequest, type AiDispatcherResponse, type AiDispatcherUsage, type AiProviderAdapter, type AiProviderId, type CacheMode, type CacheOutcome, type CachePolicy, type CacheResolution, type CacheTtl, type CloudflareBackoff, type CloudflareCallContext, type CloudflareEndpoint, type CloudflareEndpointChoice, type CloudflareGatewayControls, type CloudflareRequestOptions, type CloudflareRunOptions, type CloudflareRuntimeOptions, type CloudflareWebhookFormat, type CompiledAiRequest, type CompiledCloudflareRequest, type CompiledOpenAiRequest, type DispatcherMcpOptions, type DispatcherMcpServer, type DispatcherMcpTool, IMPLEMENTED_AI_PROVIDERS, type ImplementedAiProvider, type OpenAiRuntimeOptions, type ReasoningResolution, createAiDispatcher, isImplementedAiProvider, providerNotImplemented, resolveProvider, stripReasoningHistoryFields };
|