@lunora/ai 1.0.0-alpha.4 → 1.0.0-alpha.41
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/LICENSE.md +6 -0
- package/README.md +3 -1
- package/dist/index.d.mts +26 -28
- package/dist/index.d.ts +26 -28
- package/dist/index.mjs +1 -3
- package/dist/packem_shared/AI_GATEWAY_ACCOUNT_ID_ENV-CPzc-So1.mjs +1 -0
- package/dist/packem_shared/bm25LexicalStore-RA9sesFC.mjs +1 -0
- package/dist/packem_shared/contentHash-BIn6ECP8.mjs +1 -0
- package/dist/packem_shared/createAi-C2ExoUDR.mjs +1 -0
- package/dist/packem_shared/defineRag-DUOcnSgC.mjs +8 -0
- package/dist/packem_shared/fixedWindowChunks-XJRXHEoz.mjs +1 -0
- package/dist/packem_shared/hybridRank-U6PmGuz1.mjs +1 -0
- package/dist/packem_shared/types.d-BcLGTChd.d.mts +239 -0
- package/dist/packem_shared/types.d-BcLGTChd.d.ts +239 -0
- package/dist/rag/index.d.mts +558 -0
- package/dist/rag/index.d.ts +558 -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,239 @@
|
|
|
1
|
+
import { EmbeddingModel, LanguageModel } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Which surface resolved the gateway. The two paths handle the auth token
|
|
4
|
+
* differently: a bring-your-own AI SDK provider sends it as the
|
|
5
|
+
* `cf-aig-authorization` header (`ResolvedAiGateway.headers`), but the Workers AI
|
|
6
|
+
* **binding** routes through the gateway using the account's own credentials and
|
|
7
|
+
* its native `gateway` option (Cloudflare `GatewayOptions`) has **no** field to
|
|
8
|
+
* carry an authorization token — so a token set for the binding path cannot be
|
|
9
|
+
* delivered and is warned about instead of silently dropped.
|
|
10
|
+
* @experimental
|
|
11
|
+
*/
|
|
12
|
+
type AiGatewayConsumer = "byo-provider" | "workers-ai-binding";
|
|
13
|
+
/**
|
|
14
|
+
* Project {@link AiGatewayMetadata} to a plain string-map of only its defined,
|
|
15
|
+
* non-empty fields, or `undefined` when nothing is set. This is the shared source
|
|
16
|
+
* of truth behind both gateway-correlation forms: the `cf-aig-metadata` HTTP
|
|
17
|
+
* header (bring-your-own providers) and the Workers AI binding's native
|
|
18
|
+
* `gateway.metadata` option — both encode the same `{ functionPath, traceId }`.
|
|
19
|
+
* @experimental
|
|
20
|
+
*/
|
|
21
|
+
declare const buildAiGatewayMetadataFields: (metadata: AiGatewayMetadata | undefined) => Record<string, string> | undefined;
|
|
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
|
+
/** Env var naming the Cloudflare account that owns the gateway. */
|
|
67
|
+
declare const AI_GATEWAY_ACCOUNT_ID_ENV = "LUNORA_AI_GATEWAY_ACCOUNT_ID";
|
|
68
|
+
/** Env var naming the AI Gateway id (the gateway's slug). */
|
|
69
|
+
declare const AI_GATEWAY_ID_ENV = "LUNORA_AI_GATEWAY_ID";
|
|
70
|
+
/**
|
|
71
|
+
* Env var carrying the gateway's authentication token (only for authenticated
|
|
72
|
+
* gateways).
|
|
73
|
+
*
|
|
74
|
+
* **Workers AI binding limitation.** This token is delivered only on the
|
|
75
|
+
* bring-your-own AI SDK provider path, as the `cf-aig-authorization` header (see
|
|
76
|
+
* {@link ResolvedAiGateway.headers}). The Workers AI **binding** (`ctx.ai` over
|
|
77
|
+
* `env.AI`) routes through the gateway with the account's own credentials and its
|
|
78
|
+
* native `gateway` option (Cloudflare's `GatewayOptions`: `id` / `cacheKey` /
|
|
79
|
+
* `metadata` / …) has no authorization field — so an *authenticated* gateway that
|
|
80
|
+
* requires a token cannot be reached on the binding path. Set this only for a BYO
|
|
81
|
+
* provider; for Workers AI, leave the gateway unauthenticated (or front it with a
|
|
82
|
+
* BYO provider). {@link resolveAiGateway} warns once per isolate if the token is
|
|
83
|
+
* set on the binding path.
|
|
84
|
+
*/
|
|
85
|
+
declare const AI_GATEWAY_TOKEN_ENV = "LUNORA_AI_GATEWAY_TOKEN";
|
|
86
|
+
/**
|
|
87
|
+
* Resolve the Cloudflare AI Gateway coordinates from the Worker `env`, or
|
|
88
|
+
* `undefined` when the gateway is not configured (both `LUNORA_AI_GATEWAY_ACCOUNT_ID`
|
|
89
|
+
* and `LUNORA_AI_GATEWAY_ID` must be present). Opt-in and backward-compatible:
|
|
90
|
+
* an unconfigured app gets `undefined` and every caller keeps its direct-provider
|
|
91
|
+
* behavior.
|
|
92
|
+
*
|
|
93
|
+
* Pass optional {@link AiGatewayMetadata} to fold a `cf-aig-metadata` correlation
|
|
94
|
+
* header into `headers` — only its defined fields are sent.
|
|
95
|
+
*
|
|
96
|
+
* `consumer` names the surface resolving the gateway (default `"byo-provider"`).
|
|
97
|
+
* When it is `"workers-ai-binding"` and an {@link AI_GATEWAY_TOKEN_ENV} token is
|
|
98
|
+
* configured, this warns once per isolate: the binding path cannot carry the
|
|
99
|
+
* token (see {@link AI_GATEWAY_TOKEN_ENV}), so it would otherwise be dropped with
|
|
100
|
+
* no diagnostic and every `ctx.ai.model(...)` call would fail against an
|
|
101
|
+
* authenticated gateway while the token var reads as "configured".
|
|
102
|
+
* @experimental
|
|
103
|
+
*/
|
|
104
|
+
declare const resolveAiGateway: (env: Record<string, unknown>, metadata?: AiGatewayMetadata, consumer?: AiGatewayConsumer) => ResolvedAiGateway | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* Structural projection of the Cloudflare Workers `AI` binding (`env.AI`).
|
|
107
|
+
* Declared locally so unit tests can pass a plain-object double and the real
|
|
108
|
+
* binding satisfies the same shape without importing `@cloudflare/workers-types`
|
|
109
|
+
* into the public surface. Mirrors the `run` method documented at
|
|
110
|
+
* https://developers.cloudflare.com/workers-ai/.
|
|
111
|
+
* @experimental
|
|
112
|
+
*/
|
|
113
|
+
interface AiBindingLike {
|
|
114
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* A Workers AI provider instance — the value returned by `createWorkersAI(...)`.
|
|
118
|
+
* Calling it with a model id yields an AI SDK {@link LanguageModel}; the
|
|
119
|
+
* optional `textEmbeddingModel` factory yields an {@link EmbeddingModel}.
|
|
120
|
+
* Typed structurally so `@lunora/ai` neither re-declares the provider's full
|
|
121
|
+
* surface nor hard-pins its exact type across minor releases.
|
|
122
|
+
* @experimental
|
|
123
|
+
*/
|
|
124
|
+
interface WorkersAiProviderLike {
|
|
125
|
+
(modelId: string, settings?: Record<string, unknown>): LanguageModel;
|
|
126
|
+
textEmbeddingModel?: (modelId: string) => EmbeddingModel;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* AI Gateway options forwarded to `createWorkersAI`. Lets inference route
|
|
130
|
+
* through a Cloudflare AI Gateway for caching, rate-limiting, and observability.
|
|
131
|
+
* @experimental
|
|
132
|
+
*/
|
|
133
|
+
interface AiGatewayOptions {
|
|
134
|
+
[key: string]: unknown;
|
|
135
|
+
id: string;
|
|
136
|
+
/**
|
|
137
|
+
* Custom correlation metadata surfaced in the AI Gateway logs (Cloudflare's
|
|
138
|
+
* native `gateway.metadata`). `@lunora/ai` folds `{ functionPath, traceId }`
|
|
139
|
+
* here from {@link LunoraAiOptions.metadata} when a gateway is configured.
|
|
140
|
+
*/
|
|
141
|
+
metadata?: Record<string, string>;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* `LunoraAiOptions` is part of the experimental `@lunora/ai` API and may change without a major version bump.
|
|
145
|
+
* @experimental
|
|
146
|
+
*/
|
|
147
|
+
interface LunoraAiOptions {
|
|
148
|
+
/**
|
|
149
|
+
* The Workers `AI` binding (`env.AI`). Required for the zero-config Workers
|
|
150
|
+
* AI default and for the raw `ai.run(...)` passthrough. May be omitted when
|
|
151
|
+
* a pre-built `provider` is supplied (e.g. in tests or a custom setup).
|
|
152
|
+
*/
|
|
153
|
+
binding?: AiBindingLike;
|
|
154
|
+
/**
|
|
155
|
+
* Default Workers AI **embedding** model id used by `embeddingModel()` when no
|
|
156
|
+
* explicit model is passed (e.g. `@cf/baai/bge-base-en-v1.5`). Kept separate
|
|
157
|
+
* from `defaultModel` because a language-model id and an embedding-model
|
|
158
|
+
* id belong to different Workers AI families and are never interchangeable —
|
|
159
|
+
* reusing the language-model default here would defer a wrong-family error to
|
|
160
|
+
* inference time. Has no effect on bring-your-own providers.
|
|
161
|
+
*/
|
|
162
|
+
defaultEmbeddingModel?: string;
|
|
163
|
+
/**
|
|
164
|
+
* Default Workers AI **language** model id used by `model()` when no explicit
|
|
165
|
+
* model is passed. For embeddings, set `defaultEmbeddingModel` instead.
|
|
166
|
+
* Has no effect on bring-your-own providers.
|
|
167
|
+
*/
|
|
168
|
+
defaultModel?: string;
|
|
169
|
+
/**
|
|
170
|
+
* The Worker `env`, read for opt-in Cloudflare AI Gateway routing (the
|
|
171
|
+
* `LUNORA_AI_GATEWAY_*` vars, via `resolveAiGateway`). When it configures a
|
|
172
|
+
* gateway and no explicit {@link LunoraAiOptions.gateway} is given, the
|
|
173
|
+
* Workers AI provider is routed through that gateway so token + dollar-cost
|
|
174
|
+
* telemetry is computed on the app's behalf. Unset, or with no gateway vars,
|
|
175
|
+
* behavior is unchanged (calls go straight to Workers AI).
|
|
176
|
+
*/
|
|
177
|
+
env?: Record<string, unknown>;
|
|
178
|
+
/**
|
|
179
|
+
* Route Workers AI inference through a Cloudflare AI Gateway. An explicit
|
|
180
|
+
* value wins over anything derived from {@link LunoraAiOptions.env}.
|
|
181
|
+
*/
|
|
182
|
+
gateway?: AiGatewayOptions;
|
|
183
|
+
/**
|
|
184
|
+
* Correlation metadata folded into the active gateway call (the Workers AI
|
|
185
|
+
* binding's native `gateway.metadata`) so an AI Gateway log entry ties back
|
|
186
|
+
* to the Lunora function + trace that made it. Only applied when a gateway is
|
|
187
|
+
* actually configured (explicit or env-derived) and only its defined fields
|
|
188
|
+
* are sent — absent otherwise, so behavior is unchanged. The generated
|
|
189
|
+
* `ctx.ai` facade threads `{ functionPath, traceId }` here automatically.
|
|
190
|
+
*/
|
|
191
|
+
metadata?: AiGatewayMetadata;
|
|
192
|
+
/**
|
|
193
|
+
* Pre-built Workers AI provider. When omitted, one is constructed from
|
|
194
|
+
* `binding` via `createWorkersAI`. Supplying it directly is the seam used by
|
|
195
|
+
* tests and advanced setups; it also lets callers configure the provider
|
|
196
|
+
* (e.g. `safePrompt`) before handing it to `@lunora/ai`.
|
|
197
|
+
*/
|
|
198
|
+
provider?: WorkersAiProviderLike;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* A model to run against. The AI SDK's {@link LanguageModel} already admits a
|
|
202
|
+
* bare `string`, so this alias covers both arms of the provider-agnostic seam:
|
|
203
|
+
* a string id is the Workers AI convenience path (resolved by `ctx.ai.model`),
|
|
204
|
+
* a built model object is bring-your-own (`@ai-sdk/openai`, `@ai-sdk/anthropic`,
|
|
205
|
+
* `@ai-sdk/google`, OpenRouter, …).
|
|
206
|
+
* @experimental
|
|
207
|
+
*/
|
|
208
|
+
type ModelInput = LanguageModel;
|
|
209
|
+
/**
|
|
210
|
+
* Likewise for embeddings: a Workers AI embedding model id (e.g.
|
|
211
|
+
* `@cf/baai/bge-base-en-v1.5`) or any AI SDK {@link EmbeddingModel}.
|
|
212
|
+
* @experimental
|
|
213
|
+
*/
|
|
214
|
+
type EmbeddingModelInput = EmbeddingModel | string;
|
|
215
|
+
/**
|
|
216
|
+
* The `ctx.ai` surface. `model`/`embeddingModel` resolve a Workers AI model from
|
|
217
|
+
* a string (the default provider) and pass any non-string model straight through,
|
|
218
|
+
* so both accept Workers AI and bring-your-own providers. Feed the resolved model
|
|
219
|
+
* to the AI SDK functions re-exported from `@lunora/ai` (`generateText`,
|
|
220
|
+
* `streamText`, `generateObject`, `embed`, …); `run` is the raw binding escape
|
|
221
|
+
* hatch, and `workersai` is the underlying provider for direct model access.
|
|
222
|
+
* @experimental
|
|
223
|
+
*/
|
|
224
|
+
interface LunoraAi {
|
|
225
|
+
/** Resolve an {@link EmbeddingModel}: a string → Workers AI, an object → passthrough. */
|
|
226
|
+
embeddingModel: (model?: EmbeddingModelInput) => EmbeddingModel;
|
|
227
|
+
/** Resolve a {@link LanguageModel}: a string → Workers AI, an object → passthrough. */
|
|
228
|
+
model: (model?: ModelInput) => LanguageModel;
|
|
229
|
+
/**
|
|
230
|
+
* Raw Workers AI binding passthrough (void-style `ai.run`). Bypasses the AI
|
|
231
|
+
* SDK entirely — useful for Workers-AI-only model families (image, ASR,
|
|
232
|
+
* translation) not surfaced through the provider. Throws if no binding was
|
|
233
|
+
* supplied.
|
|
234
|
+
*/
|
|
235
|
+
run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
|
|
236
|
+
/** The underlying Workers AI provider — `ai.workersai("@cf/...")` for a raw model. */
|
|
237
|
+
workersai: WorkersAiProviderLike;
|
|
238
|
+
}
|
|
239
|
+
export { AI_GATEWAY_ACCOUNT_ID_ENV as A, EmbeddingModelInput as E, LunoraAiOptions as L, ModelInput as M, ResolvedAiGateway as R, WorkersAiProviderLike as W, LunoraAi as a, AI_GATEWAY_ID_ENV as b, AI_GATEWAY_TOKEN_ENV as c, AiBindingLike as d, AiGatewayMetadata as e, AiGatewayOptions as f, buildAiGatewayMetadataFields as g, resolveAiGateway as r };
|