@anyr/ai-sdk-provider 0.1.3 → 0.1.6
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 +22 -0
- package/dist/index.d.cts +155 -16
- package/dist/index.d.ts +155 -16
- package/dist/index.js +220 -38
- package/dist/index.mjs +220 -40
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -65,6 +65,28 @@ for await (const chunk of textStream) {
|
|
|
65
65
|
}
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
### Reasoning models
|
|
69
|
+
|
|
70
|
+
Reasoning models (e.g. `deepseek/deepseek-r1`) return their thinking as a
|
|
71
|
+
separate stream — the provider surfaces it as the AI SDK's standard reasoning
|
|
72
|
+
parts, so `reasoning`/`reasoningText` and the full-stream `reasoning-*` parts
|
|
73
|
+
work out of the box.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { createAnyRouter } from "@anyr/ai-sdk-provider"
|
|
77
|
+
import { generateText } from "ai"
|
|
78
|
+
|
|
79
|
+
const anyrouter = createAnyRouter()
|
|
80
|
+
|
|
81
|
+
const { text, reasoningText } = await generateText({
|
|
82
|
+
model: anyrouter("deepseek/deepseek-r1"),
|
|
83
|
+
prompt: "How many r's are in strawberry?",
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
console.log(reasoningText) // the model's chain of thought
|
|
87
|
+
console.log(text) // the final answer
|
|
88
|
+
```
|
|
89
|
+
|
|
68
90
|
### generateObject (structured output)
|
|
69
91
|
|
|
70
92
|
```ts
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnyRouter } from '@anyr/sdk';
|
|
1
|
+
import { SharedV4ProviderReference, LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4GenerateResult, LanguageModelV4StreamResult, EmbeddingModelV4, EmbeddingModelV4CallOptions, EmbeddingModelV4Result, FilesV4, FilesV4UploadFileCallOptions, FilesV4UploadFileResult, ImageModelV4, ImageModelV4CallOptions, ImageModelV4Result, ProviderV4 } from '@ai-sdk/provider';
|
|
2
|
+
import { AppAttribution, AnyRouter } from '@anyr/sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The resolved representation of a cached file — a URL string ready to drop
|
|
6
|
+
* into an OpenAI-compatible message part and the media type that accompanies it.
|
|
7
|
+
*/
|
|
8
|
+
type ResolvedFileEntry = {
|
|
9
|
+
/** Ready-to-use string: a data URL (`data:<mime>;base64,…`) or an https URL. */
|
|
10
|
+
url: string;
|
|
11
|
+
mediaType: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* In-session file reference cache.
|
|
15
|
+
*
|
|
16
|
+
* Files are keyed by a SHA-256 hex hash of their content. A single provider
|
|
17
|
+
* instance shares one cache, so the same binary content is never encoded or
|
|
18
|
+
* stored more than once within an agent loop — even across multiple `doGenerate`
|
|
19
|
+
* / `doStream` calls.
|
|
20
|
+
*
|
|
21
|
+
* This class is intentionally free of I/O and network calls. All async
|
|
22
|
+
* behaviour lives in the `hashFileData` helper below.
|
|
23
|
+
*/
|
|
24
|
+
declare class FileReferenceCache {
|
|
25
|
+
private readonly store;
|
|
26
|
+
/**
|
|
27
|
+
* Look up a cached entry via a `SharedV4ProviderReference`.
|
|
28
|
+
* Returns `undefined` when the reference was not created by this cache.
|
|
29
|
+
*/
|
|
30
|
+
lookup(reference: SharedV4ProviderReference): ResolvedFileEntry | undefined;
|
|
31
|
+
/** Add an entry and return the corresponding provider reference. */
|
|
32
|
+
set(hash: string, entry: ResolvedFileEntry): SharedV4ProviderReference;
|
|
33
|
+
/** Returns true when the content hash is already cached. */
|
|
34
|
+
has(hash: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Build a `SharedV4ProviderReference` for the given hash without writing
|
|
37
|
+
* to the cache. Use this to construct the return value of `uploadFile`
|
|
38
|
+
* after confirming the hash is already stored.
|
|
39
|
+
*/
|
|
40
|
+
referenceFor(hash: string): SharedV4ProviderReference;
|
|
41
|
+
}
|
|
3
42
|
|
|
4
43
|
/**
|
|
5
44
|
* Settings for `createAnyRouter(...)`.
|
|
@@ -11,6 +50,16 @@ interface AnyRouterProviderSettings {
|
|
|
11
50
|
* Resolved lazily at request time.
|
|
12
51
|
*/
|
|
13
52
|
apiKey?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Skip API-key resolution entirely. When `true`, no `Authorization` header is
|
|
55
|
+
* derived from `apiKey`/`ANYROUTER_API_KEY` — requests rely on ambient auth
|
|
56
|
+
* such as same-origin session cookies (the AnyRouter Playground) or an
|
|
57
|
+
* `Authorization` header supplied via {@link headers}.
|
|
58
|
+
*
|
|
59
|
+
* Without this, `createAnyRouter()` throws at request time when no key is
|
|
60
|
+
* present. Defaults to `false`.
|
|
61
|
+
*/
|
|
62
|
+
keyless?: boolean;
|
|
14
63
|
/**
|
|
15
64
|
* API base URL.
|
|
16
65
|
* Defaults to `https://anyrouter.dev/api/v1`.
|
|
@@ -20,6 +69,17 @@ interface AnyRouterProviderSettings {
|
|
|
20
69
|
* Extra headers merged into every request.
|
|
21
70
|
*/
|
|
22
71
|
headers?: Record<string, string>;
|
|
72
|
+
/**
|
|
73
|
+
* Identify your app to AnyRouter, sent as the `X-AnyRouter-*` attribution
|
|
74
|
+
* headers so your traffic shows up under your own name in usage analytics.
|
|
75
|
+
*
|
|
76
|
+
* Opt-in rather than defaulted: unlike `@anyr/sdk`, this provider is
|
|
77
|
+
* routinely used from a browser, where the gateway already attributes the
|
|
78
|
+
* request from its `Referer`. A default `X-AnyRouter-Source` would override
|
|
79
|
+
* that (the explicit header wins server-side) and misattribute browser apps
|
|
80
|
+
* to the provider library.
|
|
81
|
+
*/
|
|
82
|
+
app?: AppAttribution;
|
|
23
83
|
/**
|
|
24
84
|
* Custom fetch implementation.
|
|
25
85
|
* Defaults to the global `fetch`.
|
|
@@ -41,6 +101,26 @@ interface AnyRouterProviderSettings {
|
|
|
41
101
|
* Per-call `providerOptions.anyrouter.extraBody` takes precedence.
|
|
42
102
|
*/
|
|
43
103
|
extraBody?: Record<string, unknown>;
|
|
104
|
+
/**
|
|
105
|
+
* Cache every response for N seconds via Cloudflare AI Gateway.
|
|
106
|
+
* Sent as the `cf-aig-cache-ttl` header. Min 60, max ~1 month (2,678,400s).
|
|
107
|
+
* Per-call `providerOptions.anyrouter.cacheTtl` takes precedence.
|
|
108
|
+
*/
|
|
109
|
+
cacheTtl?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Custom cache key for Cloudflare AI Gateway response caching, so requests
|
|
112
|
+
* with different content can share a cached response. Sent as
|
|
113
|
+
* `cf-aig-cache-key`. Per-call `providerOptions.anyrouter.cacheKey` takes
|
|
114
|
+
* precedence.
|
|
115
|
+
*/
|
|
116
|
+
cacheKey?: string;
|
|
117
|
+
/**
|
|
118
|
+
* Skip Cloudflare AI Gateway response caching for every request, even if a
|
|
119
|
+
* default cache policy would otherwise apply. Sent as
|
|
120
|
+
* `cf-aig-skip-cache: true`. Per-call `providerOptions.anyrouter.skipCache`
|
|
121
|
+
* takes precedence.
|
|
122
|
+
*/
|
|
123
|
+
skipCache?: boolean;
|
|
44
124
|
}
|
|
45
125
|
|
|
46
126
|
interface AnyRouterChatConfig {
|
|
@@ -49,16 +129,22 @@ interface AnyRouterChatConfig {
|
|
|
49
129
|
headers: () => Record<string, string | undefined>;
|
|
50
130
|
fetch?: typeof globalThis.fetch;
|
|
51
131
|
settings: AnyRouterProviderSettings;
|
|
132
|
+
/**
|
|
133
|
+
* Shared file reference cache for the provider instance.
|
|
134
|
+
* When present, `{ type: 'reference' }` file parts are resolved via the cache
|
|
135
|
+
* rather than throwing `UnsupportedFunctionalityError`.
|
|
136
|
+
*/
|
|
137
|
+
fileCache?: FileReferenceCache;
|
|
52
138
|
}
|
|
53
|
-
declare class AnyRouterChatLanguageModel implements
|
|
54
|
-
readonly specificationVersion: "
|
|
139
|
+
declare class AnyRouterChatLanguageModel implements LanguageModelV4 {
|
|
140
|
+
readonly specificationVersion: "v4";
|
|
55
141
|
readonly provider: string;
|
|
56
142
|
readonly modelId: string;
|
|
57
143
|
readonly supportedUrls: Record<string, RegExp[]>;
|
|
58
144
|
private readonly config;
|
|
59
145
|
constructor(modelId: string, config: AnyRouterChatConfig);
|
|
60
|
-
doGenerate(options:
|
|
61
|
-
doStream(options:
|
|
146
|
+
doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
|
|
147
|
+
doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
|
|
62
148
|
}
|
|
63
149
|
|
|
64
150
|
interface AnyRouterEmbeddingConfig {
|
|
@@ -67,8 +153,8 @@ interface AnyRouterEmbeddingConfig {
|
|
|
67
153
|
headers: () => Record<string, string | undefined>;
|
|
68
154
|
fetch?: typeof globalThis.fetch;
|
|
69
155
|
}
|
|
70
|
-
declare class AnyRouterEmbeddingModel implements
|
|
71
|
-
readonly specificationVersion: "
|
|
156
|
+
declare class AnyRouterEmbeddingModel implements EmbeddingModelV4 {
|
|
157
|
+
readonly specificationVersion: "v4";
|
|
72
158
|
readonly provider: string;
|
|
73
159
|
readonly modelId: string;
|
|
74
160
|
/** No hard per-call limit imposed by the provider layer. */
|
|
@@ -76,7 +162,43 @@ declare class AnyRouterEmbeddingModel implements EmbeddingModelV3 {
|
|
|
76
162
|
readonly supportsParallelCalls = true;
|
|
77
163
|
private readonly config;
|
|
78
164
|
constructor(modelId: string, config: AnyRouterEmbeddingConfig);
|
|
79
|
-
doEmbed(options:
|
|
165
|
+
doEmbed(options: EmbeddingModelV4CallOptions): Promise<EmbeddingModelV4Result>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* `FilesV4` implementation for the AnyRouter provider.
|
|
170
|
+
*
|
|
171
|
+
* ## How it works
|
|
172
|
+
*
|
|
173
|
+
* `uploadFile()` computes a SHA-256 hash of the file content and stores the
|
|
174
|
+
* resolved data URL in a shared `FileReferenceCache`. It returns a
|
|
175
|
+
* `SharedV4ProviderReference` whose `anyrouter` key is that hash.
|
|
176
|
+
*
|
|
177
|
+
* When the same reference later appears as a `{ type: 'reference' }` file
|
|
178
|
+
* part in a multi-step prompt, `convertToAnyRouterChatMessages` looks up the
|
|
179
|
+
* hash in the cache and substitutes the stored URL — so the full file bytes
|
|
180
|
+
* are never re-encoded on subsequent calls within the same provider session.
|
|
181
|
+
*
|
|
182
|
+
* ## Limitations (Phase 2)
|
|
183
|
+
*
|
|
184
|
+
* The cache is in-memory and scoped to the provider instance. References do
|
|
185
|
+
* not persist across process restarts. A follow-up (Phase 3+) can wire this
|
|
186
|
+
* to the AnyRouter gateway's `/files` endpoint to get durable, cross-session
|
|
187
|
+
* file IDs that translate to `{ type: "file", file: { file_id: "…" } }` parts
|
|
188
|
+
* for providers that support it (e.g. OpenAI).
|
|
189
|
+
*/
|
|
190
|
+
declare class AnyRouterFiles implements FilesV4 {
|
|
191
|
+
readonly specificationVersion: "v4";
|
|
192
|
+
readonly provider: string;
|
|
193
|
+
private readonly cache;
|
|
194
|
+
constructor(provider: string, cache: FileReferenceCache);
|
|
195
|
+
/**
|
|
196
|
+
* Register a file in the in-session cache and return a provider reference.
|
|
197
|
+
*
|
|
198
|
+
* Calling this multiple times with the same file content is idempotent —
|
|
199
|
+
* the hash check prevents redundant re-encoding.
|
|
200
|
+
*/
|
|
201
|
+
uploadFile(options: FilesV4UploadFileCallOptions): Promise<FilesV4UploadFileResult>;
|
|
80
202
|
}
|
|
81
203
|
|
|
82
204
|
interface AnyRouterImageConfig {
|
|
@@ -85,8 +207,8 @@ interface AnyRouterImageConfig {
|
|
|
85
207
|
headers: () => Record<string, string | undefined>;
|
|
86
208
|
fetch?: typeof globalThis.fetch;
|
|
87
209
|
}
|
|
88
|
-
declare class AnyRouterImageModel implements
|
|
89
|
-
readonly specificationVersion: "
|
|
210
|
+
declare class AnyRouterImageModel implements ImageModelV4 {
|
|
211
|
+
readonly specificationVersion: "v4";
|
|
90
212
|
readonly provider: string;
|
|
91
213
|
readonly modelId: string;
|
|
92
214
|
/**
|
|
@@ -96,19 +218,36 @@ declare class AnyRouterImageModel implements ImageModelV3 {
|
|
|
96
218
|
readonly maxImagesPerCall = 1;
|
|
97
219
|
private readonly config;
|
|
98
220
|
constructor(modelId: string, config: AnyRouterImageConfig);
|
|
99
|
-
doGenerate(options:
|
|
221
|
+
doGenerate(options: ImageModelV4CallOptions): Promise<ImageModelV4Result>;
|
|
100
222
|
}
|
|
101
223
|
|
|
102
|
-
interface AnyRouterProvider extends
|
|
224
|
+
interface AnyRouterProvider extends ProviderV4 {
|
|
103
225
|
/** Create a chat language model by calling the provider directly. */
|
|
104
|
-
(modelId: string):
|
|
226
|
+
(modelId: string): LanguageModelV4;
|
|
105
227
|
/** Create a chat language model (explicit method). */
|
|
106
|
-
chat(modelId: string):
|
|
228
|
+
chat(modelId: string): LanguageModelV4;
|
|
107
229
|
/**
|
|
108
230
|
* Low-level `@anyr/sdk` client instance.
|
|
109
231
|
* Use this as an escape hatch when you need direct API access.
|
|
110
232
|
*/
|
|
111
233
|
readonly client: AnyRouter;
|
|
234
|
+
/**
|
|
235
|
+
* File management interface (AI SDK v4 `FilesV4`).
|
|
236
|
+
*
|
|
237
|
+
* Call `uploadFile()` to register a file in the session cache. The returned
|
|
238
|
+
* `providerReference` can be used in subsequent model calls as a
|
|
239
|
+
* `{ type: 'file', data: { type: 'reference', reference } }` part — the
|
|
240
|
+
* provider resolves the reference from the cache without re-encoding the file.
|
|
241
|
+
*
|
|
242
|
+
* ```ts
|
|
243
|
+
* const ref = await anyrouter.files().uploadFile({
|
|
244
|
+
* data: { type: 'data', data: pdfBytes },
|
|
245
|
+
* mediaType: 'application/pdf',
|
|
246
|
+
* })
|
|
247
|
+
* // ref.providerReference === { anyrouter: '<sha256-hex>' }
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
files(): FilesV4;
|
|
112
251
|
}
|
|
113
252
|
/**
|
|
114
253
|
* Create an AnyRouter provider for the Vercel AI SDK.
|
|
@@ -146,4 +285,4 @@ declare function createAnyRouter(options?: AnyRouterProviderSettings): AnyRouter
|
|
|
146
285
|
*/
|
|
147
286
|
declare const anyrouter: AnyRouterProvider;
|
|
148
287
|
|
|
149
|
-
export { type AnyRouterChatConfig, AnyRouterChatLanguageModel, type AnyRouterEmbeddingConfig, AnyRouterEmbeddingModel, type AnyRouterImageConfig, AnyRouterImageModel, type AnyRouterProvider, type AnyRouterProviderSettings, anyrouter, createAnyRouter };
|
|
288
|
+
export { type AnyRouterChatConfig, AnyRouterChatLanguageModel, type AnyRouterEmbeddingConfig, AnyRouterEmbeddingModel, AnyRouterFiles, type AnyRouterImageConfig, AnyRouterImageModel, type AnyRouterProvider, type AnyRouterProviderSettings, FileReferenceCache, type ResolvedFileEntry, anyrouter, createAnyRouter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnyRouter } from '@anyr/sdk';
|
|
1
|
+
import { SharedV4ProviderReference, LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4GenerateResult, LanguageModelV4StreamResult, EmbeddingModelV4, EmbeddingModelV4CallOptions, EmbeddingModelV4Result, FilesV4, FilesV4UploadFileCallOptions, FilesV4UploadFileResult, ImageModelV4, ImageModelV4CallOptions, ImageModelV4Result, ProviderV4 } from '@ai-sdk/provider';
|
|
2
|
+
import { AppAttribution, AnyRouter } from '@anyr/sdk';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The resolved representation of a cached file — a URL string ready to drop
|
|
6
|
+
* into an OpenAI-compatible message part and the media type that accompanies it.
|
|
7
|
+
*/
|
|
8
|
+
type ResolvedFileEntry = {
|
|
9
|
+
/** Ready-to-use string: a data URL (`data:<mime>;base64,…`) or an https URL. */
|
|
10
|
+
url: string;
|
|
11
|
+
mediaType: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* In-session file reference cache.
|
|
15
|
+
*
|
|
16
|
+
* Files are keyed by a SHA-256 hex hash of their content. A single provider
|
|
17
|
+
* instance shares one cache, so the same binary content is never encoded or
|
|
18
|
+
* stored more than once within an agent loop — even across multiple `doGenerate`
|
|
19
|
+
* / `doStream` calls.
|
|
20
|
+
*
|
|
21
|
+
* This class is intentionally free of I/O and network calls. All async
|
|
22
|
+
* behaviour lives in the `hashFileData` helper below.
|
|
23
|
+
*/
|
|
24
|
+
declare class FileReferenceCache {
|
|
25
|
+
private readonly store;
|
|
26
|
+
/**
|
|
27
|
+
* Look up a cached entry via a `SharedV4ProviderReference`.
|
|
28
|
+
* Returns `undefined` when the reference was not created by this cache.
|
|
29
|
+
*/
|
|
30
|
+
lookup(reference: SharedV4ProviderReference): ResolvedFileEntry | undefined;
|
|
31
|
+
/** Add an entry and return the corresponding provider reference. */
|
|
32
|
+
set(hash: string, entry: ResolvedFileEntry): SharedV4ProviderReference;
|
|
33
|
+
/** Returns true when the content hash is already cached. */
|
|
34
|
+
has(hash: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Build a `SharedV4ProviderReference` for the given hash without writing
|
|
37
|
+
* to the cache. Use this to construct the return value of `uploadFile`
|
|
38
|
+
* after confirming the hash is already stored.
|
|
39
|
+
*/
|
|
40
|
+
referenceFor(hash: string): SharedV4ProviderReference;
|
|
41
|
+
}
|
|
3
42
|
|
|
4
43
|
/**
|
|
5
44
|
* Settings for `createAnyRouter(...)`.
|
|
@@ -11,6 +50,16 @@ interface AnyRouterProviderSettings {
|
|
|
11
50
|
* Resolved lazily at request time.
|
|
12
51
|
*/
|
|
13
52
|
apiKey?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Skip API-key resolution entirely. When `true`, no `Authorization` header is
|
|
55
|
+
* derived from `apiKey`/`ANYROUTER_API_KEY` — requests rely on ambient auth
|
|
56
|
+
* such as same-origin session cookies (the AnyRouter Playground) or an
|
|
57
|
+
* `Authorization` header supplied via {@link headers}.
|
|
58
|
+
*
|
|
59
|
+
* Without this, `createAnyRouter()` throws at request time when no key is
|
|
60
|
+
* present. Defaults to `false`.
|
|
61
|
+
*/
|
|
62
|
+
keyless?: boolean;
|
|
14
63
|
/**
|
|
15
64
|
* API base URL.
|
|
16
65
|
* Defaults to `https://anyrouter.dev/api/v1`.
|
|
@@ -20,6 +69,17 @@ interface AnyRouterProviderSettings {
|
|
|
20
69
|
* Extra headers merged into every request.
|
|
21
70
|
*/
|
|
22
71
|
headers?: Record<string, string>;
|
|
72
|
+
/**
|
|
73
|
+
* Identify your app to AnyRouter, sent as the `X-AnyRouter-*` attribution
|
|
74
|
+
* headers so your traffic shows up under your own name in usage analytics.
|
|
75
|
+
*
|
|
76
|
+
* Opt-in rather than defaulted: unlike `@anyr/sdk`, this provider is
|
|
77
|
+
* routinely used from a browser, where the gateway already attributes the
|
|
78
|
+
* request from its `Referer`. A default `X-AnyRouter-Source` would override
|
|
79
|
+
* that (the explicit header wins server-side) and misattribute browser apps
|
|
80
|
+
* to the provider library.
|
|
81
|
+
*/
|
|
82
|
+
app?: AppAttribution;
|
|
23
83
|
/**
|
|
24
84
|
* Custom fetch implementation.
|
|
25
85
|
* Defaults to the global `fetch`.
|
|
@@ -41,6 +101,26 @@ interface AnyRouterProviderSettings {
|
|
|
41
101
|
* Per-call `providerOptions.anyrouter.extraBody` takes precedence.
|
|
42
102
|
*/
|
|
43
103
|
extraBody?: Record<string, unknown>;
|
|
104
|
+
/**
|
|
105
|
+
* Cache every response for N seconds via Cloudflare AI Gateway.
|
|
106
|
+
* Sent as the `cf-aig-cache-ttl` header. Min 60, max ~1 month (2,678,400s).
|
|
107
|
+
* Per-call `providerOptions.anyrouter.cacheTtl` takes precedence.
|
|
108
|
+
*/
|
|
109
|
+
cacheTtl?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Custom cache key for Cloudflare AI Gateway response caching, so requests
|
|
112
|
+
* with different content can share a cached response. Sent as
|
|
113
|
+
* `cf-aig-cache-key`. Per-call `providerOptions.anyrouter.cacheKey` takes
|
|
114
|
+
* precedence.
|
|
115
|
+
*/
|
|
116
|
+
cacheKey?: string;
|
|
117
|
+
/**
|
|
118
|
+
* Skip Cloudflare AI Gateway response caching for every request, even if a
|
|
119
|
+
* default cache policy would otherwise apply. Sent as
|
|
120
|
+
* `cf-aig-skip-cache: true`. Per-call `providerOptions.anyrouter.skipCache`
|
|
121
|
+
* takes precedence.
|
|
122
|
+
*/
|
|
123
|
+
skipCache?: boolean;
|
|
44
124
|
}
|
|
45
125
|
|
|
46
126
|
interface AnyRouterChatConfig {
|
|
@@ -49,16 +129,22 @@ interface AnyRouterChatConfig {
|
|
|
49
129
|
headers: () => Record<string, string | undefined>;
|
|
50
130
|
fetch?: typeof globalThis.fetch;
|
|
51
131
|
settings: AnyRouterProviderSettings;
|
|
132
|
+
/**
|
|
133
|
+
* Shared file reference cache for the provider instance.
|
|
134
|
+
* When present, `{ type: 'reference' }` file parts are resolved via the cache
|
|
135
|
+
* rather than throwing `UnsupportedFunctionalityError`.
|
|
136
|
+
*/
|
|
137
|
+
fileCache?: FileReferenceCache;
|
|
52
138
|
}
|
|
53
|
-
declare class AnyRouterChatLanguageModel implements
|
|
54
|
-
readonly specificationVersion: "
|
|
139
|
+
declare class AnyRouterChatLanguageModel implements LanguageModelV4 {
|
|
140
|
+
readonly specificationVersion: "v4";
|
|
55
141
|
readonly provider: string;
|
|
56
142
|
readonly modelId: string;
|
|
57
143
|
readonly supportedUrls: Record<string, RegExp[]>;
|
|
58
144
|
private readonly config;
|
|
59
145
|
constructor(modelId: string, config: AnyRouterChatConfig);
|
|
60
|
-
doGenerate(options:
|
|
61
|
-
doStream(options:
|
|
146
|
+
doGenerate(options: LanguageModelV4CallOptions): Promise<LanguageModelV4GenerateResult>;
|
|
147
|
+
doStream(options: LanguageModelV4CallOptions): Promise<LanguageModelV4StreamResult>;
|
|
62
148
|
}
|
|
63
149
|
|
|
64
150
|
interface AnyRouterEmbeddingConfig {
|
|
@@ -67,8 +153,8 @@ interface AnyRouterEmbeddingConfig {
|
|
|
67
153
|
headers: () => Record<string, string | undefined>;
|
|
68
154
|
fetch?: typeof globalThis.fetch;
|
|
69
155
|
}
|
|
70
|
-
declare class AnyRouterEmbeddingModel implements
|
|
71
|
-
readonly specificationVersion: "
|
|
156
|
+
declare class AnyRouterEmbeddingModel implements EmbeddingModelV4 {
|
|
157
|
+
readonly specificationVersion: "v4";
|
|
72
158
|
readonly provider: string;
|
|
73
159
|
readonly modelId: string;
|
|
74
160
|
/** No hard per-call limit imposed by the provider layer. */
|
|
@@ -76,7 +162,43 @@ declare class AnyRouterEmbeddingModel implements EmbeddingModelV3 {
|
|
|
76
162
|
readonly supportsParallelCalls = true;
|
|
77
163
|
private readonly config;
|
|
78
164
|
constructor(modelId: string, config: AnyRouterEmbeddingConfig);
|
|
79
|
-
doEmbed(options:
|
|
165
|
+
doEmbed(options: EmbeddingModelV4CallOptions): Promise<EmbeddingModelV4Result>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* `FilesV4` implementation for the AnyRouter provider.
|
|
170
|
+
*
|
|
171
|
+
* ## How it works
|
|
172
|
+
*
|
|
173
|
+
* `uploadFile()` computes a SHA-256 hash of the file content and stores the
|
|
174
|
+
* resolved data URL in a shared `FileReferenceCache`. It returns a
|
|
175
|
+
* `SharedV4ProviderReference` whose `anyrouter` key is that hash.
|
|
176
|
+
*
|
|
177
|
+
* When the same reference later appears as a `{ type: 'reference' }` file
|
|
178
|
+
* part in a multi-step prompt, `convertToAnyRouterChatMessages` looks up the
|
|
179
|
+
* hash in the cache and substitutes the stored URL — so the full file bytes
|
|
180
|
+
* are never re-encoded on subsequent calls within the same provider session.
|
|
181
|
+
*
|
|
182
|
+
* ## Limitations (Phase 2)
|
|
183
|
+
*
|
|
184
|
+
* The cache is in-memory and scoped to the provider instance. References do
|
|
185
|
+
* not persist across process restarts. A follow-up (Phase 3+) can wire this
|
|
186
|
+
* to the AnyRouter gateway's `/files` endpoint to get durable, cross-session
|
|
187
|
+
* file IDs that translate to `{ type: "file", file: { file_id: "…" } }` parts
|
|
188
|
+
* for providers that support it (e.g. OpenAI).
|
|
189
|
+
*/
|
|
190
|
+
declare class AnyRouterFiles implements FilesV4 {
|
|
191
|
+
readonly specificationVersion: "v4";
|
|
192
|
+
readonly provider: string;
|
|
193
|
+
private readonly cache;
|
|
194
|
+
constructor(provider: string, cache: FileReferenceCache);
|
|
195
|
+
/**
|
|
196
|
+
* Register a file in the in-session cache and return a provider reference.
|
|
197
|
+
*
|
|
198
|
+
* Calling this multiple times with the same file content is idempotent —
|
|
199
|
+
* the hash check prevents redundant re-encoding.
|
|
200
|
+
*/
|
|
201
|
+
uploadFile(options: FilesV4UploadFileCallOptions): Promise<FilesV4UploadFileResult>;
|
|
80
202
|
}
|
|
81
203
|
|
|
82
204
|
interface AnyRouterImageConfig {
|
|
@@ -85,8 +207,8 @@ interface AnyRouterImageConfig {
|
|
|
85
207
|
headers: () => Record<string, string | undefined>;
|
|
86
208
|
fetch?: typeof globalThis.fetch;
|
|
87
209
|
}
|
|
88
|
-
declare class AnyRouterImageModel implements
|
|
89
|
-
readonly specificationVersion: "
|
|
210
|
+
declare class AnyRouterImageModel implements ImageModelV4 {
|
|
211
|
+
readonly specificationVersion: "v4";
|
|
90
212
|
readonly provider: string;
|
|
91
213
|
readonly modelId: string;
|
|
92
214
|
/**
|
|
@@ -96,19 +218,36 @@ declare class AnyRouterImageModel implements ImageModelV3 {
|
|
|
96
218
|
readonly maxImagesPerCall = 1;
|
|
97
219
|
private readonly config;
|
|
98
220
|
constructor(modelId: string, config: AnyRouterImageConfig);
|
|
99
|
-
doGenerate(options:
|
|
221
|
+
doGenerate(options: ImageModelV4CallOptions): Promise<ImageModelV4Result>;
|
|
100
222
|
}
|
|
101
223
|
|
|
102
|
-
interface AnyRouterProvider extends
|
|
224
|
+
interface AnyRouterProvider extends ProviderV4 {
|
|
103
225
|
/** Create a chat language model by calling the provider directly. */
|
|
104
|
-
(modelId: string):
|
|
226
|
+
(modelId: string): LanguageModelV4;
|
|
105
227
|
/** Create a chat language model (explicit method). */
|
|
106
|
-
chat(modelId: string):
|
|
228
|
+
chat(modelId: string): LanguageModelV4;
|
|
107
229
|
/**
|
|
108
230
|
* Low-level `@anyr/sdk` client instance.
|
|
109
231
|
* Use this as an escape hatch when you need direct API access.
|
|
110
232
|
*/
|
|
111
233
|
readonly client: AnyRouter;
|
|
234
|
+
/**
|
|
235
|
+
* File management interface (AI SDK v4 `FilesV4`).
|
|
236
|
+
*
|
|
237
|
+
* Call `uploadFile()` to register a file in the session cache. The returned
|
|
238
|
+
* `providerReference` can be used in subsequent model calls as a
|
|
239
|
+
* `{ type: 'file', data: { type: 'reference', reference } }` part — the
|
|
240
|
+
* provider resolves the reference from the cache without re-encoding the file.
|
|
241
|
+
*
|
|
242
|
+
* ```ts
|
|
243
|
+
* const ref = await anyrouter.files().uploadFile({
|
|
244
|
+
* data: { type: 'data', data: pdfBytes },
|
|
245
|
+
* mediaType: 'application/pdf',
|
|
246
|
+
* })
|
|
247
|
+
* // ref.providerReference === { anyrouter: '<sha256-hex>' }
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
files(): FilesV4;
|
|
112
251
|
}
|
|
113
252
|
/**
|
|
114
253
|
* Create an AnyRouter provider for the Vercel AI SDK.
|
|
@@ -146,4 +285,4 @@ declare function createAnyRouter(options?: AnyRouterProviderSettings): AnyRouter
|
|
|
146
285
|
*/
|
|
147
286
|
declare const anyrouter: AnyRouterProvider;
|
|
148
287
|
|
|
149
|
-
export { type AnyRouterChatConfig, AnyRouterChatLanguageModel, type AnyRouterEmbeddingConfig, AnyRouterEmbeddingModel, type AnyRouterImageConfig, AnyRouterImageModel, type AnyRouterProvider, type AnyRouterProviderSettings, anyrouter, createAnyRouter };
|
|
288
|
+
export { type AnyRouterChatConfig, AnyRouterChatLanguageModel, type AnyRouterEmbeddingConfig, AnyRouterEmbeddingModel, AnyRouterFiles, type AnyRouterImageConfig, AnyRouterImageModel, type AnyRouterProvider, type AnyRouterProviderSettings, FileReferenceCache, type ResolvedFileEntry, anyrouter, createAnyRouter };
|