@lunora/ai 1.0.0-alpha.7 → 1.0.0-alpha.71
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 +3 -1
- package/dist/index.d.mts +88 -27
- package/dist/index.d.ts +88 -27
- package/dist/index.mjs +1 -3
- package/dist/packem_shared/AI_DEFAULT_EMBEDDING_MODEL_ENV-mi9Aaq1z.mjs +1 -0
- package/dist/packem_shared/DEFAULT_MODEL_PRICES-Q8uxdiuV.mjs +1 -0
- package/dist/packem_shared/VECTORIZE_CAPABILITIES-CUQDoxis.mjs +1 -0
- package/dist/packem_shared/batchReranker-Bc38FBLH.mjs +1 -0
- package/dist/packem_shared/bm25-9q0Avwi-.mjs +1 -0
- package/dist/packem_shared/bm25LexicalStore-DMUzAL0O.mjs +1 -0
- package/dist/packem_shared/concurrent-C6nqBv41.mjs +1 -0
- package/dist/packem_shared/contentHash-BIn6ECP8.mjs +1 -0
- package/dist/packem_shared/createAi-CwY7eL7P.mjs +1 -0
- package/dist/packem_shared/defineRag-wBDjkuHP.mjs +8 -0
- package/dist/packem_shared/defineRagSource-Q3f3niU8.mjs +1 -0
- package/dist/packem_shared/fixedWindowChunks-C461ahRE.mjs +1 -0
- package/dist/packem_shared/hybridRank-DejmVw2I.mjs +1 -0
- package/dist/packem_shared/markdownChunker-Bcv56GEz.mjs +5 -0
- package/dist/packem_shared/matchesMetadataFilter-BbIOyA5g.mjs +1 -0
- package/dist/packem_shared/ragSyncTriggers-DPqzBNFw.mjs +1 -0
- package/dist/packem_shared/sql-D5aqEMCY.mjs +1 -0
- package/dist/packem_shared/sqlLexicalStore-4C_cIwef.mjs +1 -0
- package/dist/packem_shared/sqliteVectorStore-D32l9lP0.mjs +1 -0
- package/dist/packem_shared/types.d-eaM7juQg.d.mts +271 -0
- package/dist/packem_shared/types.d-eaM7juQg.d.ts +271 -0
- package/dist/rag/index.d.mts +1200 -0
- package/dist/rag/index.d.ts +1200 -0
- package/dist/rag/index.mjs +1 -0
- package/package.json +12 -6
- package/dist/packem_shared/createAi-Bq_4LMcp.mjs +0 -54
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { EmbeddingModel, LanguageModel } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
4
|
+
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
5
|
+
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
6
|
+
* header (bring-your-own providers) and the Workers AI binding's native
|
|
7
|
+
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Which surface resolved the gateway. The two paths handle the auth token
|
|
13
|
+
* differently: a bring-your-own AI SDK provider sends it as the
|
|
14
|
+
* `cf-aig-authorization` header (`ResolvedAiGateway.headers`), but the Workers AI
|
|
15
|
+
* **binding** routes through the gateway using the account's own credentials and
|
|
16
|
+
* its native `gateway` option (Cloudflare `GatewayOptions`) has **no** field to
|
|
17
|
+
* carry an authorization token — so a token set for the binding path cannot be
|
|
18
|
+
* delivered and is warned about instead of silently dropped.
|
|
19
|
+
* @experimental
|
|
20
|
+
*/
|
|
21
|
+
type AiGatewayConsumer = "byo-provider" | "workers-ai-binding";
|
|
22
|
+
/**
|
|
23
|
+
* Correlation metadata folded into the `cf-aig-metadata` request header so a
|
|
24
|
+
* gateway log entry can be tied back to the Lunora function + trace that made
|
|
25
|
+
* the call. Every field is optional — only defined ones are sent.
|
|
26
|
+
* @experimental
|
|
27
|
+
*/
|
|
28
|
+
interface AiGatewayMetadata {
|
|
29
|
+
/** The Lunora function path that issued the model call (e.g. `messages:send`). */
|
|
30
|
+
functionPath?: string;
|
|
31
|
+
/** The 32-hex trace id the call belongs to, so the gateway log joins the trace. */
|
|
32
|
+
traceId?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The resolved AI Gateway coordinates.
|
|
36
|
+
*
|
|
37
|
+
* `gatewayId`/`accountId` drive the Workers AI provider's native `gateway` option
|
|
38
|
+
* (Cloudflare routes the binding call internally). `baseURL` + `headers` are for
|
|
39
|
+
* bring-your-own AI SDK providers (`@ai-sdk/openai`, …): append the provider
|
|
40
|
+
* slug to `baseURL` and spread `headers` into the provider config, e.g.
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const gw = resolveAiGateway(env);
|
|
44
|
+
* const openai = createOpenAI(gw ? { baseURL: `${gw.baseURL}/openai`, headers: gw.headers } : {});
|
|
45
|
+
* ```
|
|
46
|
+
* @experimental
|
|
47
|
+
*/
|
|
48
|
+
interface ResolvedAiGateway {
|
|
49
|
+
/** The Cloudflare account id owning the gateway. */
|
|
50
|
+
accountId: string;
|
|
51
|
+
/**
|
|
52
|
+
* The universal gateway base URL:
|
|
53
|
+
* `https://gateway.ai.cloudflare.com/v1/{account}/{gateway}`. Append the
|
|
54
|
+
* provider slug (`/openai`, `/anthropic`, `/workers-ai`, …) per provider.
|
|
55
|
+
*/
|
|
56
|
+
baseURL: string;
|
|
57
|
+
/** The gateway id (slug). */
|
|
58
|
+
gatewayId: string;
|
|
59
|
+
/**
|
|
60
|
+
* Request headers for a gateway-routed call: `cf-aig-authorization` when the
|
|
61
|
+
* gateway is authenticated, and `cf-aig-metadata` when correlation metadata
|
|
62
|
+
* was supplied. Empty object when neither applies.
|
|
63
|
+
*/
|
|
64
|
+
headers: Record<string, string>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Env var carrying the default Workers AI **language** model id, used by
|
|
68
|
+
* `ctx.ai.model()` when it is called with no argument.
|
|
69
|
+
*
|
|
70
|
+
* This exists because `env` is the only configuration seam `ctx.ai` has. The
|
|
71
|
+
* generated shard constructs the facade as `createAi({ binding, env, metadata })`
|
|
72
|
+
* with those three fields fixed, so `LunoraAiOptions.defaultModel` — the field
|
|
73
|
+
* `model()` reads — was unreachable from an app: `ctx.ai.model()` with no
|
|
74
|
+
* argument threw, always. Reading the default off `env` puts it back in the
|
|
75
|
+
* app's hands (a wrangler `vars` entry or `.dev.vars` line) without a new
|
|
76
|
+
* constructor argument codegen would have to learn to emit.
|
|
77
|
+
*
|
|
78
|
+
* An explicit `defaultModel` option still wins — the env var is
|
|
79
|
+
* the fallback, exactly as with the gateway vars above.
|
|
80
|
+
*/
|
|
81
|
+
declare const AI_DEFAULT_MODEL_ENV = "LUNORA_AI_DEFAULT_MODEL";
|
|
82
|
+
/**
|
|
83
|
+
* Env var carrying the default Workers AI **embedding** model id, used by
|
|
84
|
+
* `ctx.ai.embeddingModel()` with no argument — and so by `defineRag`, whose
|
|
85
|
+
* `embeddingModel` is documented as optional and resolves through exactly that
|
|
86
|
+
* call. Separate from {@link AI_DEFAULT_MODEL_ENV} because a language-model id
|
|
87
|
+
* and an embedding-model id are never interchangeable.
|
|
88
|
+
*/
|
|
89
|
+
declare const AI_DEFAULT_EMBEDDING_MODEL_ENV = "LUNORA_AI_DEFAULT_EMBEDDING_MODEL";
|
|
90
|
+
/** Env var naming the Cloudflare account that owns the gateway. */
|
|
91
|
+
declare const AI_GATEWAY_ACCOUNT_ID_ENV = "LUNORA_AI_GATEWAY_ACCOUNT_ID";
|
|
92
|
+
/** Env var naming the AI Gateway id (the gateway's slug). */
|
|
93
|
+
declare const AI_GATEWAY_ID_ENV = "LUNORA_AI_GATEWAY_ID";
|
|
94
|
+
/**
|
|
95
|
+
* Env var carrying the gateway's authentication token (only for authenticated
|
|
96
|
+
* gateways).
|
|
97
|
+
*
|
|
98
|
+
* **Workers AI binding limitation.** This token is delivered only on the
|
|
99
|
+
* bring-your-own AI SDK provider path, as the `cf-aig-authorization` header (see
|
|
100
|
+
* {@link ResolvedAiGateway.headers}). The Workers AI **binding** (`ctx.ai` over
|
|
101
|
+
* `env.AI`) routes through the gateway with the account's own credentials and its
|
|
102
|
+
* native `gateway` option (Cloudflare's `GatewayOptions`: `id` / `cacheKey` /
|
|
103
|
+
* `metadata` / …) has no authorization field — so an *authenticated* gateway that
|
|
104
|
+
* requires a token cannot be reached on the binding path. Set this only for a BYO
|
|
105
|
+
* provider; for Workers AI, leave the gateway unauthenticated (or front it with a
|
|
106
|
+
* BYO provider). {@link resolveAiGateway} warns once per isolate if the token is
|
|
107
|
+
* set on the binding path.
|
|
108
|
+
*/
|
|
109
|
+
declare const AI_GATEWAY_TOKEN_ENV = "LUNORA_AI_GATEWAY_TOKEN";
|
|
110
|
+
/**
|
|
111
|
+
* Resolve the Cloudflare AI Gateway coordinates from the Worker `env`, or
|
|
112
|
+
* `undefined` when the gateway is not configured (both `LUNORA_AI_GATEWAY_ACCOUNT_ID`
|
|
113
|
+
* and `LUNORA_AI_GATEWAY_ID` must be present). Opt-in and backward-compatible:
|
|
114
|
+
* an unconfigured app gets `undefined` and every caller keeps its direct-provider
|
|
115
|
+
* behavior.
|
|
116
|
+
*
|
|
117
|
+
* Pass optional {@link AiGatewayMetadata} to fold a `cf-aig-metadata` correlation
|
|
118
|
+
* header into `headers` — only its defined fields are sent.
|
|
119
|
+
*
|
|
120
|
+
* `consumer` names the surface resolving the gateway (default `"byo-provider"`).
|
|
121
|
+
* When it is `"workers-ai-binding"` and an {@link AI_GATEWAY_TOKEN_ENV} token is
|
|
122
|
+
* configured, this warns once per isolate: the binding path cannot carry the
|
|
123
|
+
* token (see {@link AI_GATEWAY_TOKEN_ENV}), so it would otherwise be dropped with
|
|
124
|
+
* no diagnostic and every `ctx.ai.model(...)` call would fail against an
|
|
125
|
+
* authenticated gateway while the token var reads as "configured".
|
|
126
|
+
* @experimental
|
|
127
|
+
*/
|
|
128
|
+
declare const resolveAiGateway: (env: Record<string, unknown>, metadata?: AiGatewayMetadata, consumer?: AiGatewayConsumer) => ResolvedAiGateway | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Structural projection of the Cloudflare Workers `AI` binding (`env.AI`).
|
|
131
|
+
* Declared locally so unit tests can pass a plain-object double and the real
|
|
132
|
+
* binding satisfies the same shape without importing `@cloudflare/workers-types`
|
|
133
|
+
* into the public surface. Mirrors the `run` method documented at
|
|
134
|
+
* https://developers.cloudflare.com/workers-ai/.
|
|
135
|
+
* @experimental
|
|
136
|
+
*/
|
|
137
|
+
interface AiBindingLike {
|
|
138
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A Workers AI provider instance — the value returned by `createWorkersAI(...)`.
|
|
142
|
+
* Calling it with a model id yields an AI SDK {@link LanguageModel}; the
|
|
143
|
+
* optional `textEmbeddingModel` factory yields an {@link EmbeddingModel}.
|
|
144
|
+
* Typed structurally so `@lunora/ai` neither re-declares the provider's full
|
|
145
|
+
* surface nor hard-pins its exact type across minor releases.
|
|
146
|
+
* @experimental
|
|
147
|
+
*/
|
|
148
|
+
interface WorkersAiProviderLike {
|
|
149
|
+
(modelId: string, settings?: Record<string, unknown>): LanguageModel;
|
|
150
|
+
textEmbeddingModel?: (modelId: string) => EmbeddingModel;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* AI Gateway options forwarded to `createWorkersAI`. Lets inference route
|
|
154
|
+
* through a Cloudflare AI Gateway for caching, rate-limiting, and observability.
|
|
155
|
+
* @experimental
|
|
156
|
+
*/
|
|
157
|
+
interface AiGatewayOptions {
|
|
158
|
+
[key: string]: unknown;
|
|
159
|
+
id: string;
|
|
160
|
+
/**
|
|
161
|
+
* Custom correlation metadata surfaced in the AI Gateway logs (Cloudflare's
|
|
162
|
+
* native `gateway.metadata`). `@lunora/ai` folds `{ functionPath, traceId }`
|
|
163
|
+
* here from {@link LunoraAiOptions.metadata} when a gateway is configured.
|
|
164
|
+
*/
|
|
165
|
+
metadata?: Record<string, string>;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* `LunoraAiOptions` is part of the experimental `@lunora/ai` API and may change without a major version bump.
|
|
169
|
+
* @experimental
|
|
170
|
+
*/
|
|
171
|
+
interface LunoraAiOptions {
|
|
172
|
+
/**
|
|
173
|
+
* The Workers `AI` binding (`env.AI`). Required for the zero-config Workers
|
|
174
|
+
* AI default and for the raw `ai.run(...)` passthrough. May be omitted when
|
|
175
|
+
* a pre-built `provider` is supplied (e.g. in tests or a custom setup).
|
|
176
|
+
*/
|
|
177
|
+
binding?: AiBindingLike;
|
|
178
|
+
/**
|
|
179
|
+
* Default Workers AI **embedding** model id used by `embeddingModel()` when no
|
|
180
|
+
* explicit model is passed (e.g. `@cf/baai/bge-base-en-v1.5`). Kept separate
|
|
181
|
+
* from `defaultModel` because a language-model id and an embedding-model
|
|
182
|
+
* id belong to different Workers AI families and are never interchangeable —
|
|
183
|
+
* reusing the language-model default here would defer a wrong-family error to
|
|
184
|
+
* inference time. Has no effect on bring-your-own providers.
|
|
185
|
+
*
|
|
186
|
+
* Falls back to `LUNORA_AI_DEFAULT_EMBEDDING_MODEL` in {@link LunoraAiOptions.env}
|
|
187
|
+
* — which is how an app sets it, since the generated `ctx.ai` is constructed
|
|
188
|
+
* with a fixed `{ binding, env, metadata }`.
|
|
189
|
+
*/
|
|
190
|
+
defaultEmbeddingModel?: string;
|
|
191
|
+
/**
|
|
192
|
+
* Default Workers AI **language** model id used by `model()` when no explicit
|
|
193
|
+
* model is passed. For embeddings, set `defaultEmbeddingModel` instead.
|
|
194
|
+
* Has no effect on bring-your-own providers.
|
|
195
|
+
*
|
|
196
|
+
* Falls back to `LUNORA_AI_DEFAULT_MODEL` in {@link LunoraAiOptions.env} — the
|
|
197
|
+
* seam a Lunora app actually has (see `defaultEmbeddingModel`).
|
|
198
|
+
*/
|
|
199
|
+
defaultModel?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The Worker `env`, read for opt-in Cloudflare AI Gateway routing (the
|
|
202
|
+
* `LUNORA_AI_GATEWAY_*` vars, via `resolveAiGateway`) and for the default
|
|
203
|
+
* model ids (`LUNORA_AI_DEFAULT_MODEL` / `LUNORA_AI_DEFAULT_EMBEDDING_MODEL`).
|
|
204
|
+
* When it configures a gateway and no explicit {@link LunoraAiOptions.gateway}
|
|
205
|
+
* is given, the Workers AI provider is routed through that gateway so token +
|
|
206
|
+
* dollar-cost telemetry is computed on the app's behalf. Unset, or with no
|
|
207
|
+
* gateway vars, behavior is unchanged (calls go straight to Workers AI).
|
|
208
|
+
*/
|
|
209
|
+
env?: Record<string, unknown>;
|
|
210
|
+
/**
|
|
211
|
+
* Route Workers AI inference through a Cloudflare AI Gateway. An explicit
|
|
212
|
+
* value wins over anything derived from {@link LunoraAiOptions.env}.
|
|
213
|
+
*/
|
|
214
|
+
gateway?: AiGatewayOptions;
|
|
215
|
+
/**
|
|
216
|
+
* Correlation metadata folded into the active gateway call (the Workers AI
|
|
217
|
+
* binding's native `gateway.metadata`) so an AI Gateway log entry ties back
|
|
218
|
+
* to the Lunora function + trace that made it. Only applied when a gateway is
|
|
219
|
+
* actually configured (explicit or env-derived) and only its defined fields
|
|
220
|
+
* are sent — absent otherwise, so behavior is unchanged. The generated
|
|
221
|
+
* `ctx.ai` facade threads `{ functionPath, traceId }` here automatically.
|
|
222
|
+
*/
|
|
223
|
+
metadata?: AiGatewayMetadata;
|
|
224
|
+
/**
|
|
225
|
+
* Pre-built Workers AI provider. When omitted, one is constructed from
|
|
226
|
+
* `binding` via `createWorkersAI`. Supplying it directly is the seam used by
|
|
227
|
+
* tests and advanced setups; it also lets callers configure the provider
|
|
228
|
+
* (e.g. `safePrompt`) before handing it to `@lunora/ai`.
|
|
229
|
+
*/
|
|
230
|
+
provider?: WorkersAiProviderLike;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* A model to run against. The AI SDK's {@link LanguageModel} already admits a
|
|
234
|
+
* bare `string`, so this alias covers both arms of the provider-agnostic seam:
|
|
235
|
+
* a string id is the Workers AI convenience path (resolved by `ctx.ai.model`),
|
|
236
|
+
* a built model object is bring-your-own (`@ai-sdk/openai`, `@ai-sdk/anthropic`,
|
|
237
|
+
* `@ai-sdk/google`, OpenRouter, …).
|
|
238
|
+
* @experimental
|
|
239
|
+
*/
|
|
240
|
+
type ModelInput = LanguageModel;
|
|
241
|
+
/**
|
|
242
|
+
* Likewise for embeddings: a Workers AI embedding model id (e.g.
|
|
243
|
+
* `@cf/baai/bge-base-en-v1.5`) or any AI SDK {@link EmbeddingModel}.
|
|
244
|
+
* @experimental
|
|
245
|
+
*/
|
|
246
|
+
type EmbeddingModelInput = EmbeddingModel | string;
|
|
247
|
+
/**
|
|
248
|
+
* The `ctx.ai` surface. `model`/`embeddingModel` resolve a Workers AI model from
|
|
249
|
+
* a string (the default provider) and pass any non-string model straight through,
|
|
250
|
+
* so both accept Workers AI and bring-your-own providers. Feed the resolved model
|
|
251
|
+
* to the AI SDK functions re-exported from `@lunora/ai` (`generateText`,
|
|
252
|
+
* `streamText`, `generateObject`, `embed`, …); `run` is the raw binding escape
|
|
253
|
+
* hatch, and `workersai` is the underlying provider for direct model access.
|
|
254
|
+
* @experimental
|
|
255
|
+
*/
|
|
256
|
+
interface LunoraAi {
|
|
257
|
+
/** Resolve an {@link EmbeddingModel}: a string → Workers AI, an object → passthrough. */
|
|
258
|
+
embeddingModel: (model?: EmbeddingModelInput) => EmbeddingModel;
|
|
259
|
+
/** Resolve a {@link LanguageModel}: a string → Workers AI, an object → passthrough. */
|
|
260
|
+
model: (model?: ModelInput) => LanguageModel;
|
|
261
|
+
/**
|
|
262
|
+
* Raw Workers AI binding passthrough (void-style `ai.run`). Bypasses the AI
|
|
263
|
+
* SDK entirely — useful for Workers-AI-only model families (image, ASR,
|
|
264
|
+
* translation) not surfaced through the provider. Throws if no binding was
|
|
265
|
+
* supplied.
|
|
266
|
+
*/
|
|
267
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
268
|
+
/** The underlying Workers AI provider — `ai.workersai("@cf/...")` for a raw model. */
|
|
269
|
+
workersai: WorkersAiProviderLike;
|
|
270
|
+
}
|
|
271
|
+
export { AI_DEFAULT_EMBEDDING_MODEL_ENV as A, EmbeddingModelInput as E, LunoraAiOptions as L, ModelInput as M, ResolvedAiGateway as R, WorkersAiProviderLike as W, LunoraAi as a, AI_DEFAULT_MODEL_ENV as b, AI_GATEWAY_ACCOUNT_ID_ENV as c, AI_GATEWAY_ID_ENV as d, AI_GATEWAY_TOKEN_ENV as e, AiBindingLike as f, AiGatewayMetadata as g, AiGatewayOptions as h, buildAiGatewayMetadataFields as i, resolveAiGateway as r };
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { EmbeddingModel, LanguageModel } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
4
|
+
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
5
|
+
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
6
|
+
* header (bring-your-own providers) and the Workers AI binding's native
|
|
7
|
+
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
8
|
+
* @experimental
|
|
9
|
+
*/
|
|
10
|
+
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Which surface resolved the gateway. The two paths handle the auth token
|
|
13
|
+
* differently: a bring-your-own AI SDK provider sends it as the
|
|
14
|
+
* `cf-aig-authorization` header (`ResolvedAiGateway.headers`), but the Workers AI
|
|
15
|
+
* **binding** routes through the gateway using the account's own credentials and
|
|
16
|
+
* its native `gateway` option (Cloudflare `GatewayOptions`) has **no** field to
|
|
17
|
+
* carry an authorization token — so a token set for the binding path cannot be
|
|
18
|
+
* delivered and is warned about instead of silently dropped.
|
|
19
|
+
* @experimental
|
|
20
|
+
*/
|
|
21
|
+
type AiGatewayConsumer = "byo-provider" | "workers-ai-binding";
|
|
22
|
+
/**
|
|
23
|
+
* Correlation metadata folded into the `cf-aig-metadata` request header so a
|
|
24
|
+
* gateway log entry can be tied back to the Lunora function + trace that made
|
|
25
|
+
* the call. Every field is optional — only defined ones are sent.
|
|
26
|
+
* @experimental
|
|
27
|
+
*/
|
|
28
|
+
interface AiGatewayMetadata {
|
|
29
|
+
/** The Lunora function path that issued the model call (e.g. `messages:send`). */
|
|
30
|
+
functionPath?: string;
|
|
31
|
+
/** The 32-hex trace id the call belongs to, so the gateway log joins the trace. */
|
|
32
|
+
traceId?: string;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The resolved AI Gateway coordinates.
|
|
36
|
+
*
|
|
37
|
+
* `gatewayId`/`accountId` drive the Workers AI provider's native `gateway` option
|
|
38
|
+
* (Cloudflare routes the binding call internally). `baseURL` + `headers` are for
|
|
39
|
+
* bring-your-own AI SDK providers (`@ai-sdk/openai`, …): append the provider
|
|
40
|
+
* slug to `baseURL` and spread `headers` into the provider config, e.g.
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const gw = resolveAiGateway(env);
|
|
44
|
+
* const openai = createOpenAI(gw ? { baseURL: `${gw.baseURL}/openai`, headers: gw.headers } : {});
|
|
45
|
+
* ```
|
|
46
|
+
* @experimental
|
|
47
|
+
*/
|
|
48
|
+
interface ResolvedAiGateway {
|
|
49
|
+
/** The Cloudflare account id owning the gateway. */
|
|
50
|
+
accountId: string;
|
|
51
|
+
/**
|
|
52
|
+
* The universal gateway base URL:
|
|
53
|
+
* `https://gateway.ai.cloudflare.com/v1/{account}/{gateway}`. Append the
|
|
54
|
+
* provider slug (`/openai`, `/anthropic`, `/workers-ai`, …) per provider.
|
|
55
|
+
*/
|
|
56
|
+
baseURL: string;
|
|
57
|
+
/** The gateway id (slug). */
|
|
58
|
+
gatewayId: string;
|
|
59
|
+
/**
|
|
60
|
+
* Request headers for a gateway-routed call: `cf-aig-authorization` when the
|
|
61
|
+
* gateway is authenticated, and `cf-aig-metadata` when correlation metadata
|
|
62
|
+
* was supplied. Empty object when neither applies.
|
|
63
|
+
*/
|
|
64
|
+
headers: Record<string, string>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Env var carrying the default Workers AI **language** model id, used by
|
|
68
|
+
* `ctx.ai.model()` when it is called with no argument.
|
|
69
|
+
*
|
|
70
|
+
* This exists because `env` is the only configuration seam `ctx.ai` has. The
|
|
71
|
+
* generated shard constructs the facade as `createAi({ binding, env, metadata })`
|
|
72
|
+
* with those three fields fixed, so `LunoraAiOptions.defaultModel` — the field
|
|
73
|
+
* `model()` reads — was unreachable from an app: `ctx.ai.model()` with no
|
|
74
|
+
* argument threw, always. Reading the default off `env` puts it back in the
|
|
75
|
+
* app's hands (a wrangler `vars` entry or `.dev.vars` line) without a new
|
|
76
|
+
* constructor argument codegen would have to learn to emit.
|
|
77
|
+
*
|
|
78
|
+
* An explicit `defaultModel` option still wins — the env var is
|
|
79
|
+
* the fallback, exactly as with the gateway vars above.
|
|
80
|
+
*/
|
|
81
|
+
declare const AI_DEFAULT_MODEL_ENV = "LUNORA_AI_DEFAULT_MODEL";
|
|
82
|
+
/**
|
|
83
|
+
* Env var carrying the default Workers AI **embedding** model id, used by
|
|
84
|
+
* `ctx.ai.embeddingModel()` with no argument — and so by `defineRag`, whose
|
|
85
|
+
* `embeddingModel` is documented as optional and resolves through exactly that
|
|
86
|
+
* call. Separate from {@link AI_DEFAULT_MODEL_ENV} because a language-model id
|
|
87
|
+
* and an embedding-model id are never interchangeable.
|
|
88
|
+
*/
|
|
89
|
+
declare const AI_DEFAULT_EMBEDDING_MODEL_ENV = "LUNORA_AI_DEFAULT_EMBEDDING_MODEL";
|
|
90
|
+
/** Env var naming the Cloudflare account that owns the gateway. */
|
|
91
|
+
declare const AI_GATEWAY_ACCOUNT_ID_ENV = "LUNORA_AI_GATEWAY_ACCOUNT_ID";
|
|
92
|
+
/** Env var naming the AI Gateway id (the gateway's slug). */
|
|
93
|
+
declare const AI_GATEWAY_ID_ENV = "LUNORA_AI_GATEWAY_ID";
|
|
94
|
+
/**
|
|
95
|
+
* Env var carrying the gateway's authentication token (only for authenticated
|
|
96
|
+
* gateways).
|
|
97
|
+
*
|
|
98
|
+
* **Workers AI binding limitation.** This token is delivered only on the
|
|
99
|
+
* bring-your-own AI SDK provider path, as the `cf-aig-authorization` header (see
|
|
100
|
+
* {@link ResolvedAiGateway.headers}). The Workers AI **binding** (`ctx.ai` over
|
|
101
|
+
* `env.AI`) routes through the gateway with the account's own credentials and its
|
|
102
|
+
* native `gateway` option (Cloudflare's `GatewayOptions`: `id` / `cacheKey` /
|
|
103
|
+
* `metadata` / …) has no authorization field — so an *authenticated* gateway that
|
|
104
|
+
* requires a token cannot be reached on the binding path. Set this only for a BYO
|
|
105
|
+
* provider; for Workers AI, leave the gateway unauthenticated (or front it with a
|
|
106
|
+
* BYO provider). {@link resolveAiGateway} warns once per isolate if the token is
|
|
107
|
+
* set on the binding path.
|
|
108
|
+
*/
|
|
109
|
+
declare const AI_GATEWAY_TOKEN_ENV = "LUNORA_AI_GATEWAY_TOKEN";
|
|
110
|
+
/**
|
|
111
|
+
* Resolve the Cloudflare AI Gateway coordinates from the Worker `env`, or
|
|
112
|
+
* `undefined` when the gateway is not configured (both `LUNORA_AI_GATEWAY_ACCOUNT_ID`
|
|
113
|
+
* and `LUNORA_AI_GATEWAY_ID` must be present). Opt-in and backward-compatible:
|
|
114
|
+
* an unconfigured app gets `undefined` and every caller keeps its direct-provider
|
|
115
|
+
* behavior.
|
|
116
|
+
*
|
|
117
|
+
* Pass optional {@link AiGatewayMetadata} to fold a `cf-aig-metadata` correlation
|
|
118
|
+
* header into `headers` — only its defined fields are sent.
|
|
119
|
+
*
|
|
120
|
+
* `consumer` names the surface resolving the gateway (default `"byo-provider"`).
|
|
121
|
+
* When it is `"workers-ai-binding"` and an {@link AI_GATEWAY_TOKEN_ENV} token is
|
|
122
|
+
* configured, this warns once per isolate: the binding path cannot carry the
|
|
123
|
+
* token (see {@link AI_GATEWAY_TOKEN_ENV}), so it would otherwise be dropped with
|
|
124
|
+
* no diagnostic and every `ctx.ai.model(...)` call would fail against an
|
|
125
|
+
* authenticated gateway while the token var reads as "configured".
|
|
126
|
+
* @experimental
|
|
127
|
+
*/
|
|
128
|
+
declare const resolveAiGateway: (env: Record<string, unknown>, metadata?: AiGatewayMetadata, consumer?: AiGatewayConsumer) => ResolvedAiGateway | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Structural projection of the Cloudflare Workers `AI` binding (`env.AI`).
|
|
131
|
+
* Declared locally so unit tests can pass a plain-object double and the real
|
|
132
|
+
* binding satisfies the same shape without importing `@cloudflare/workers-types`
|
|
133
|
+
* into the public surface. Mirrors the `run` method documented at
|
|
134
|
+
* https://developers.cloudflare.com/workers-ai/.
|
|
135
|
+
* @experimental
|
|
136
|
+
*/
|
|
137
|
+
interface AiBindingLike {
|
|
138
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A Workers AI provider instance — the value returned by `createWorkersAI(...)`.
|
|
142
|
+
* Calling it with a model id yields an AI SDK {@link LanguageModel}; the
|
|
143
|
+
* optional `textEmbeddingModel` factory yields an {@link EmbeddingModel}.
|
|
144
|
+
* Typed structurally so `@lunora/ai` neither re-declares the provider's full
|
|
145
|
+
* surface nor hard-pins its exact type across minor releases.
|
|
146
|
+
* @experimental
|
|
147
|
+
*/
|
|
148
|
+
interface WorkersAiProviderLike {
|
|
149
|
+
(modelId: string, settings?: Record<string, unknown>): LanguageModel;
|
|
150
|
+
textEmbeddingModel?: (modelId: string) => EmbeddingModel;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* AI Gateway options forwarded to `createWorkersAI`. Lets inference route
|
|
154
|
+
* through a Cloudflare AI Gateway for caching, rate-limiting, and observability.
|
|
155
|
+
* @experimental
|
|
156
|
+
*/
|
|
157
|
+
interface AiGatewayOptions {
|
|
158
|
+
[key: string]: unknown;
|
|
159
|
+
id: string;
|
|
160
|
+
/**
|
|
161
|
+
* Custom correlation metadata surfaced in the AI Gateway logs (Cloudflare's
|
|
162
|
+
* native `gateway.metadata`). `@lunora/ai` folds `{ functionPath, traceId }`
|
|
163
|
+
* here from {@link LunoraAiOptions.metadata} when a gateway is configured.
|
|
164
|
+
*/
|
|
165
|
+
metadata?: Record<string, string>;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* `LunoraAiOptions` is part of the experimental `@lunora/ai` API and may change without a major version bump.
|
|
169
|
+
* @experimental
|
|
170
|
+
*/
|
|
171
|
+
interface LunoraAiOptions {
|
|
172
|
+
/**
|
|
173
|
+
* The Workers `AI` binding (`env.AI`). Required for the zero-config Workers
|
|
174
|
+
* AI default and for the raw `ai.run(...)` passthrough. May be omitted when
|
|
175
|
+
* a pre-built `provider` is supplied (e.g. in tests or a custom setup).
|
|
176
|
+
*/
|
|
177
|
+
binding?: AiBindingLike;
|
|
178
|
+
/**
|
|
179
|
+
* Default Workers AI **embedding** model id used by `embeddingModel()` when no
|
|
180
|
+
* explicit model is passed (e.g. `@cf/baai/bge-base-en-v1.5`). Kept separate
|
|
181
|
+
* from `defaultModel` because a language-model id and an embedding-model
|
|
182
|
+
* id belong to different Workers AI families and are never interchangeable —
|
|
183
|
+
* reusing the language-model default here would defer a wrong-family error to
|
|
184
|
+
* inference time. Has no effect on bring-your-own providers.
|
|
185
|
+
*
|
|
186
|
+
* Falls back to `LUNORA_AI_DEFAULT_EMBEDDING_MODEL` in {@link LunoraAiOptions.env}
|
|
187
|
+
* — which is how an app sets it, since the generated `ctx.ai` is constructed
|
|
188
|
+
* with a fixed `{ binding, env, metadata }`.
|
|
189
|
+
*/
|
|
190
|
+
defaultEmbeddingModel?: string;
|
|
191
|
+
/**
|
|
192
|
+
* Default Workers AI **language** model id used by `model()` when no explicit
|
|
193
|
+
* model is passed. For embeddings, set `defaultEmbeddingModel` instead.
|
|
194
|
+
* Has no effect on bring-your-own providers.
|
|
195
|
+
*
|
|
196
|
+
* Falls back to `LUNORA_AI_DEFAULT_MODEL` in {@link LunoraAiOptions.env} — the
|
|
197
|
+
* seam a Lunora app actually has (see `defaultEmbeddingModel`).
|
|
198
|
+
*/
|
|
199
|
+
defaultModel?: string;
|
|
200
|
+
/**
|
|
201
|
+
* The Worker `env`, read for opt-in Cloudflare AI Gateway routing (the
|
|
202
|
+
* `LUNORA_AI_GATEWAY_*` vars, via `resolveAiGateway`) and for the default
|
|
203
|
+
* model ids (`LUNORA_AI_DEFAULT_MODEL` / `LUNORA_AI_DEFAULT_EMBEDDING_MODEL`).
|
|
204
|
+
* When it configures a gateway and no explicit {@link LunoraAiOptions.gateway}
|
|
205
|
+
* is given, the Workers AI provider is routed through that gateway so token +
|
|
206
|
+
* dollar-cost telemetry is computed on the app's behalf. Unset, or with no
|
|
207
|
+
* gateway vars, behavior is unchanged (calls go straight to Workers AI).
|
|
208
|
+
*/
|
|
209
|
+
env?: Record<string, unknown>;
|
|
210
|
+
/**
|
|
211
|
+
* Route Workers AI inference through a Cloudflare AI Gateway. An explicit
|
|
212
|
+
* value wins over anything derived from {@link LunoraAiOptions.env}.
|
|
213
|
+
*/
|
|
214
|
+
gateway?: AiGatewayOptions;
|
|
215
|
+
/**
|
|
216
|
+
* Correlation metadata folded into the active gateway call (the Workers AI
|
|
217
|
+
* binding's native `gateway.metadata`) so an AI Gateway log entry ties back
|
|
218
|
+
* to the Lunora function + trace that made it. Only applied when a gateway is
|
|
219
|
+
* actually configured (explicit or env-derived) and only its defined fields
|
|
220
|
+
* are sent — absent otherwise, so behavior is unchanged. The generated
|
|
221
|
+
* `ctx.ai` facade threads `{ functionPath, traceId }` here automatically.
|
|
222
|
+
*/
|
|
223
|
+
metadata?: AiGatewayMetadata;
|
|
224
|
+
/**
|
|
225
|
+
* Pre-built Workers AI provider. When omitted, one is constructed from
|
|
226
|
+
* `binding` via `createWorkersAI`. Supplying it directly is the seam used by
|
|
227
|
+
* tests and advanced setups; it also lets callers configure the provider
|
|
228
|
+
* (e.g. `safePrompt`) before handing it to `@lunora/ai`.
|
|
229
|
+
*/
|
|
230
|
+
provider?: WorkersAiProviderLike;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* A model to run against. The AI SDK's {@link LanguageModel} already admits a
|
|
234
|
+
* bare `string`, so this alias covers both arms of the provider-agnostic seam:
|
|
235
|
+
* a string id is the Workers AI convenience path (resolved by `ctx.ai.model`),
|
|
236
|
+
* a built model object is bring-your-own (`@ai-sdk/openai`, `@ai-sdk/anthropic`,
|
|
237
|
+
* `@ai-sdk/google`, OpenRouter, …).
|
|
238
|
+
* @experimental
|
|
239
|
+
*/
|
|
240
|
+
type ModelInput = LanguageModel;
|
|
241
|
+
/**
|
|
242
|
+
* Likewise for embeddings: a Workers AI embedding model id (e.g.
|
|
243
|
+
* `@cf/baai/bge-base-en-v1.5`) or any AI SDK {@link EmbeddingModel}.
|
|
244
|
+
* @experimental
|
|
245
|
+
*/
|
|
246
|
+
type EmbeddingModelInput = EmbeddingModel | string;
|
|
247
|
+
/**
|
|
248
|
+
* The `ctx.ai` surface. `model`/`embeddingModel` resolve a Workers AI model from
|
|
249
|
+
* a string (the default provider) and pass any non-string model straight through,
|
|
250
|
+
* so both accept Workers AI and bring-your-own providers. Feed the resolved model
|
|
251
|
+
* to the AI SDK functions re-exported from `@lunora/ai` (`generateText`,
|
|
252
|
+
* `streamText`, `generateObject`, `embed`, …); `run` is the raw binding escape
|
|
253
|
+
* hatch, and `workersai` is the underlying provider for direct model access.
|
|
254
|
+
* @experimental
|
|
255
|
+
*/
|
|
256
|
+
interface LunoraAi {
|
|
257
|
+
/** Resolve an {@link EmbeddingModel}: a string → Workers AI, an object → passthrough. */
|
|
258
|
+
embeddingModel: (model?: EmbeddingModelInput) => EmbeddingModel;
|
|
259
|
+
/** Resolve a {@link LanguageModel}: a string → Workers AI, an object → passthrough. */
|
|
260
|
+
model: (model?: ModelInput) => LanguageModel;
|
|
261
|
+
/**
|
|
262
|
+
* Raw Workers AI binding passthrough (void-style `ai.run`). Bypasses the AI
|
|
263
|
+
* SDK entirely — useful for Workers-AI-only model families (image, ASR,
|
|
264
|
+
* translation) not surfaced through the provider. Throws if no binding was
|
|
265
|
+
* supplied.
|
|
266
|
+
*/
|
|
267
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
268
|
+
/** The underlying Workers AI provider — `ai.workersai("@cf/...")` for a raw model. */
|
|
269
|
+
workersai: WorkersAiProviderLike;
|
|
270
|
+
}
|
|
271
|
+
export { AI_DEFAULT_EMBEDDING_MODEL_ENV as A, EmbeddingModelInput as E, LunoraAiOptions as L, ModelInput as M, ResolvedAiGateway as R, WorkersAiProviderLike as W, LunoraAi as a, AI_DEFAULT_MODEL_ENV as b, AI_GATEWAY_ACCOUNT_ID_ENV as c, AI_GATEWAY_ID_ENV as d, AI_GATEWAY_TOKEN_ENV as e, AiBindingLike as f, AiGatewayMetadata as g, AiGatewayOptions as h, buildAiGatewayMetadataFields as i, resolveAiGateway as r };
|