@lunora/ai 1.0.0-alpha.16 → 1.0.0-alpha.18

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/dist/index.d.mts CHANGED
@@ -1,6 +1,28 @@
1
- import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-C6dA8LCy.mjs";
2
- export type { A as AiBindingLike, b as AiGatewayOptions, E as EmbeddingModelInput, M as ModelInput, W as WorkersAiProviderLike } from "./packem_shared/types.d-C6dA8LCy.mjs";
1
+ import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-BXCiRv1x.mjs";
2
+ export type { A as AiBindingLike, b as AiGatewayOptions, E as EmbeddingModelInput, M as ModelInput, W as WorkersAiProviderLike } from "./packem_shared/types.d-BXCiRv1x.mjs";
3
3
  export { type EmbeddingModel, type LanguageModel, embed, embedMany, generateObject, generateText, hasToolCall, jsonSchema, streamObject, streamText, tool } from 'ai';
4
4
  export { createWorkersAI } from 'workers-ai-provider';
5
+ /**
6
+ * Create the `ctx.ai` helper over a Workers `AI` binding.
7
+ *
8
+ * Workers AI is the zero-config default, but `@lunora/ai` is provider-agnostic:
9
+ * every helper takes either a model id string (resolved against the Workers AI
10
+ * provider) or any AI SDK {@link LanguageModel}/{@link EmbeddingModel} object
11
+ * (`@ai-sdk/openai`, `@ai-sdk/anthropic`, OpenRouter, …), so apps are never
12
+ * locked to Workers AI. Pair `embed` with `@lunora/bindings/vectors` for RAG.
13
+ *
14
+ * Combine with the re-exported `generateText`/`streamText`/`generateObject`/
15
+ * `embed`/`tool` from this package:
16
+ *
17
+ * ```ts
18
+ * import { streamText } from "@lunora/ai";
19
+ *
20
+ * const result = streamText({
21
+ * model: ctx.ai.model("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
22
+ * messages,
23
+ * });
24
+ * ```
25
+ * @experimental
26
+ */
5
27
  declare const createAi: (options: LunoraAiOptions) => LunoraAi;
6
28
  export { type LunoraAi, type LunoraAiOptions, createAi };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,28 @@
1
- import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-C6dA8LCy.js";
2
- export type { A as AiBindingLike, b as AiGatewayOptions, E as EmbeddingModelInput, M as ModelInput, W as WorkersAiProviderLike } from "./packem_shared/types.d-C6dA8LCy.js";
1
+ import { L as LunoraAiOptions, a as LunoraAi } from "./packem_shared/types.d-BXCiRv1x.js";
2
+ export type { A as AiBindingLike, b as AiGatewayOptions, E as EmbeddingModelInput, M as ModelInput, W as WorkersAiProviderLike } from "./packem_shared/types.d-BXCiRv1x.js";
3
3
  export { type EmbeddingModel, type LanguageModel, embed, embedMany, generateObject, generateText, hasToolCall, jsonSchema, streamObject, streamText, tool } from 'ai';
4
4
  export { createWorkersAI } from 'workers-ai-provider';
5
+ /**
6
+ * Create the `ctx.ai` helper over a Workers `AI` binding.
7
+ *
8
+ * Workers AI is the zero-config default, but `@lunora/ai` is provider-agnostic:
9
+ * every helper takes either a model id string (resolved against the Workers AI
10
+ * provider) or any AI SDK {@link LanguageModel}/{@link EmbeddingModel} object
11
+ * (`@ai-sdk/openai`, `@ai-sdk/anthropic`, OpenRouter, …), so apps are never
12
+ * locked to Workers AI. Pair `embed` with `@lunora/bindings/vectors` for RAG.
13
+ *
14
+ * Combine with the re-exported `generateText`/`streamText`/`generateObject`/
15
+ * `embed`/`tool` from this package:
16
+ *
17
+ * ```ts
18
+ * import { streamText } from "@lunora/ai";
19
+ *
20
+ * const result = streamText({
21
+ * model: ctx.ai.model("@cf/meta/llama-3.3-70b-instruct-fp8-fast"),
22
+ * messages,
23
+ * });
24
+ * ```
25
+ * @experimental
26
+ */
5
27
  declare const createAi: (options: LunoraAiOptions) => LunoraAi;
6
28
  export { type LunoraAi, type LunoraAiOptions, createAi };
@@ -109,6 +109,10 @@ const resolveEmbeddingModel = (input, ai) => {
109
109
  }
110
110
  return ai.embeddingModel(input);
111
111
  };
112
+ const modelIdOf = (model) => {
113
+ const id = model.modelId;
114
+ return typeof id === "string" && id.length > 0 ? id : void 0;
115
+ };
112
116
  const defineRag = (config) => {
113
117
  if (typeof config.index !== "string" || config.index.length === 0) {
114
118
  throw new LunoraError("BAD_REQUEST", "@lunora/ai/rag: `index` must be a non-empty Vectorize index name");
@@ -143,10 +147,21 @@ const defineRag = (config) => {
143
147
  };
144
148
  return (context) => {
145
149
  let model;
150
+ const tracer = typeof context.trace === "function" ? context.trace : void 0;
146
151
  const embedText = async (text) => {
147
- model ??= resolveEmbeddingModel(config.embeddingModel, context.ai);
148
- const { embedding } = await embed({ model, value: text });
149
- return embedding;
152
+ const resolvedModel = model ??= resolveEmbeddingModel(config.embeddingModel, context.ai);
153
+ const run = async () => {
154
+ const { embedding } = await embed({ model: resolvedModel, value: text });
155
+ return embedding;
156
+ };
157
+ if (tracer === void 0) {
158
+ return run();
159
+ }
160
+ const modelId = modelIdOf(resolvedModel);
161
+ return tracer("ai.embed", run, {
162
+ "gen_ai.operation.name": "embeddings",
163
+ ...modelId === void 0 ? {} : { "gen_ai.request.model": modelId }
164
+ });
150
165
  };
151
166
  const checkNamespace = (namespace) => {
152
167
  if (namespace !== void 0) {
@@ -0,0 +1,109 @@
1
+ import { EmbeddingModel, LanguageModel } from 'ai';
2
+ /**
3
+ * Structural projection of the Cloudflare Workers `AI` binding (`env.AI`).
4
+ * Declared locally so unit tests can pass a plain-object double and the real
5
+ * binding satisfies the same shape without importing `@cloudflare/workers-types`
6
+ * into the public surface. Mirrors the `run` method documented at
7
+ * https://developers.cloudflare.com/workers-ai/.
8
+ * @experimental
9
+ */
10
+ interface AiBindingLike {
11
+ run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
12
+ }
13
+ /**
14
+ * A Workers AI provider instance — the value returned by `createWorkersAI(...)`.
15
+ * Calling it with a model id yields an AI SDK {@link LanguageModel}; the
16
+ * optional `textEmbeddingModel` factory yields an {@link EmbeddingModel}.
17
+ * Typed structurally so `@lunora/ai` neither re-declares the provider's full
18
+ * surface nor hard-pins its exact type across minor releases.
19
+ * @experimental
20
+ */
21
+ interface WorkersAiProviderLike {
22
+ (modelId: string, settings?: Record<string, unknown>): LanguageModel;
23
+ textEmbeddingModel?: (modelId: string) => EmbeddingModel;
24
+ }
25
+ /**
26
+ * AI Gateway options forwarded to `createWorkersAI`. Lets inference route
27
+ * through a Cloudflare AI Gateway for caching, rate-limiting, and observability.
28
+ * @experimental
29
+ */
30
+ interface AiGatewayOptions {
31
+ [key: string]: unknown;
32
+ id: string;
33
+ }
34
+ /**
35
+ * `LunoraAiOptions` is part of the experimental `@lunora/ai` API and may change without a major version bump.
36
+ * @experimental
37
+ */
38
+ interface LunoraAiOptions {
39
+ /**
40
+ * The Workers `AI` binding (`env.AI`). Required for the zero-config Workers
41
+ * AI default and for the raw `ai.run(...)` passthrough. May be omitted when
42
+ * a pre-built `provider` is supplied (e.g. in tests or a custom setup).
43
+ */
44
+ binding?: AiBindingLike;
45
+ /**
46
+ * Default Workers AI **embedding** model id used by `embeddingModel()` when no
47
+ * explicit model is passed (e.g. `@cf/baai/bge-base-en-v1.5`). Kept separate
48
+ * from `defaultModel` because a language-model id and an embedding-model
49
+ * id belong to different Workers AI families and are never interchangeable —
50
+ * reusing the language-model default here would defer a wrong-family error to
51
+ * inference time. Has no effect on bring-your-own providers.
52
+ */
53
+ defaultEmbeddingModel?: string;
54
+ /**
55
+ * Default Workers AI **language** model id used by `model()` when no explicit
56
+ * model is passed. For embeddings, set `defaultEmbeddingModel` instead.
57
+ * Has no effect on bring-your-own providers.
58
+ */
59
+ defaultModel?: string;
60
+ /** Route Workers AI inference through a Cloudflare AI Gateway. */
61
+ gateway?: AiGatewayOptions;
62
+ /**
63
+ * Pre-built Workers AI provider. When omitted, one is constructed from
64
+ * `binding` via `createWorkersAI`. Supplying it directly is the seam used by
65
+ * tests and advanced setups; it also lets callers configure the provider
66
+ * (e.g. `safePrompt`) before handing it to `@lunora/ai`.
67
+ */
68
+ provider?: WorkersAiProviderLike;
69
+ }
70
+ /**
71
+ * A model to run against. The AI SDK's {@link LanguageModel} already admits a
72
+ * bare `string`, so this alias covers both arms of the provider-agnostic seam:
73
+ * a string id is the Workers AI convenience path (resolved by `ctx.ai.model`),
74
+ * a built model object is bring-your-own (`@ai-sdk/openai`, `@ai-sdk/anthropic`,
75
+ * `@ai-sdk/google`, OpenRouter, …).
76
+ * @experimental
77
+ */
78
+ type ModelInput = LanguageModel;
79
+ /**
80
+ * Likewise for embeddings: a Workers AI embedding model id (e.g.
81
+ * `@cf/baai/bge-base-en-v1.5`) or any AI SDK {@link EmbeddingModel}.
82
+ * @experimental
83
+ */
84
+ type EmbeddingModelInput = EmbeddingModel | string;
85
+ /**
86
+ * The `ctx.ai` surface. `model`/`embeddingModel` resolve a Workers AI model from
87
+ * a string (the default provider) and pass any non-string model straight through,
88
+ * so both accept Workers AI and bring-your-own providers. Feed the resolved model
89
+ * to the AI SDK functions re-exported from `@lunora/ai` (`generateText`,
90
+ * `streamText`, `generateObject`, `embed`, …); `run` is the raw binding escape
91
+ * hatch, and `workersai` is the underlying provider for direct model access.
92
+ * @experimental
93
+ */
94
+ interface LunoraAi {
95
+ /** Resolve an {@link EmbeddingModel}: a string → Workers AI, an object → passthrough. */
96
+ embeddingModel: (model?: EmbeddingModelInput) => EmbeddingModel;
97
+ /** Resolve a {@link LanguageModel}: a string → Workers AI, an object → passthrough. */
98
+ model: (model?: ModelInput) => LanguageModel;
99
+ /**
100
+ * Raw Workers AI binding passthrough (void-style `ai.run`). Bypasses the AI
101
+ * SDK entirely — useful for Workers-AI-only model families (image, ASR,
102
+ * translation) not surfaced through the provider. Throws if no binding was
103
+ * supplied.
104
+ */
105
+ run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
106
+ /** The underlying Workers AI provider — `ai.workersai("@cf/...")` for a raw model. */
107
+ workersai: WorkersAiProviderLike;
108
+ }
109
+ export { AiBindingLike as A, EmbeddingModelInput as E, LunoraAiOptions as L, ModelInput as M, WorkersAiProviderLike as W, LunoraAi as a, AiGatewayOptions as b };
@@ -0,0 +1,109 @@
1
+ import { EmbeddingModel, LanguageModel } from 'ai';
2
+ /**
3
+ * Structural projection of the Cloudflare Workers `AI` binding (`env.AI`).
4
+ * Declared locally so unit tests can pass a plain-object double and the real
5
+ * binding satisfies the same shape without importing `@cloudflare/workers-types`
6
+ * into the public surface. Mirrors the `run` method documented at
7
+ * https://developers.cloudflare.com/workers-ai/.
8
+ * @experimental
9
+ */
10
+ interface AiBindingLike {
11
+ run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
12
+ }
13
+ /**
14
+ * A Workers AI provider instance — the value returned by `createWorkersAI(...)`.
15
+ * Calling it with a model id yields an AI SDK {@link LanguageModel}; the
16
+ * optional `textEmbeddingModel` factory yields an {@link EmbeddingModel}.
17
+ * Typed structurally so `@lunora/ai` neither re-declares the provider's full
18
+ * surface nor hard-pins its exact type across minor releases.
19
+ * @experimental
20
+ */
21
+ interface WorkersAiProviderLike {
22
+ (modelId: string, settings?: Record<string, unknown>): LanguageModel;
23
+ textEmbeddingModel?: (modelId: string) => EmbeddingModel;
24
+ }
25
+ /**
26
+ * AI Gateway options forwarded to `createWorkersAI`. Lets inference route
27
+ * through a Cloudflare AI Gateway for caching, rate-limiting, and observability.
28
+ * @experimental
29
+ */
30
+ interface AiGatewayOptions {
31
+ [key: string]: unknown;
32
+ id: string;
33
+ }
34
+ /**
35
+ * `LunoraAiOptions` is part of the experimental `@lunora/ai` API and may change without a major version bump.
36
+ * @experimental
37
+ */
38
+ interface LunoraAiOptions {
39
+ /**
40
+ * The Workers `AI` binding (`env.AI`). Required for the zero-config Workers
41
+ * AI default and for the raw `ai.run(...)` passthrough. May be omitted when
42
+ * a pre-built `provider` is supplied (e.g. in tests or a custom setup).
43
+ */
44
+ binding?: AiBindingLike;
45
+ /**
46
+ * Default Workers AI **embedding** model id used by `embeddingModel()` when no
47
+ * explicit model is passed (e.g. `@cf/baai/bge-base-en-v1.5`). Kept separate
48
+ * from `defaultModel` because a language-model id and an embedding-model
49
+ * id belong to different Workers AI families and are never interchangeable —
50
+ * reusing the language-model default here would defer a wrong-family error to
51
+ * inference time. Has no effect on bring-your-own providers.
52
+ */
53
+ defaultEmbeddingModel?: string;
54
+ /**
55
+ * Default Workers AI **language** model id used by `model()` when no explicit
56
+ * model is passed. For embeddings, set `defaultEmbeddingModel` instead.
57
+ * Has no effect on bring-your-own providers.
58
+ */
59
+ defaultModel?: string;
60
+ /** Route Workers AI inference through a Cloudflare AI Gateway. */
61
+ gateway?: AiGatewayOptions;
62
+ /**
63
+ * Pre-built Workers AI provider. When omitted, one is constructed from
64
+ * `binding` via `createWorkersAI`. Supplying it directly is the seam used by
65
+ * tests and advanced setups; it also lets callers configure the provider
66
+ * (e.g. `safePrompt`) before handing it to `@lunora/ai`.
67
+ */
68
+ provider?: WorkersAiProviderLike;
69
+ }
70
+ /**
71
+ * A model to run against. The AI SDK's {@link LanguageModel} already admits a
72
+ * bare `string`, so this alias covers both arms of the provider-agnostic seam:
73
+ * a string id is the Workers AI convenience path (resolved by `ctx.ai.model`),
74
+ * a built model object is bring-your-own (`@ai-sdk/openai`, `@ai-sdk/anthropic`,
75
+ * `@ai-sdk/google`, OpenRouter, …).
76
+ * @experimental
77
+ */
78
+ type ModelInput = LanguageModel;
79
+ /**
80
+ * Likewise for embeddings: a Workers AI embedding model id (e.g.
81
+ * `@cf/baai/bge-base-en-v1.5`) or any AI SDK {@link EmbeddingModel}.
82
+ * @experimental
83
+ */
84
+ type EmbeddingModelInput = EmbeddingModel | string;
85
+ /**
86
+ * The `ctx.ai` surface. `model`/`embeddingModel` resolve a Workers AI model from
87
+ * a string (the default provider) and pass any non-string model straight through,
88
+ * so both accept Workers AI and bring-your-own providers. Feed the resolved model
89
+ * to the AI SDK functions re-exported from `@lunora/ai` (`generateText`,
90
+ * `streamText`, `generateObject`, `embed`, …); `run` is the raw binding escape
91
+ * hatch, and `workersai` is the underlying provider for direct model access.
92
+ * @experimental
93
+ */
94
+ interface LunoraAi {
95
+ /** Resolve an {@link EmbeddingModel}: a string → Workers AI, an object → passthrough. */
96
+ embeddingModel: (model?: EmbeddingModelInput) => EmbeddingModel;
97
+ /** Resolve a {@link LanguageModel}: a string → Workers AI, an object → passthrough. */
98
+ model: (model?: ModelInput) => LanguageModel;
99
+ /**
100
+ * Raw Workers AI binding passthrough (void-style `ai.run`). Bypasses the AI
101
+ * SDK entirely — useful for Workers-AI-only model families (image, ASR,
102
+ * translation) not surfaced through the provider. Throws if no binding was
103
+ * supplied.
104
+ */
105
+ run: (model: string, inputs: Record<string, unknown>, options?: Record<string, unknown>) => Promise<unknown>;
106
+ /** The underlying Workers AI provider — `ai.workersai("@cf/...")` for a raw model. */
107
+ workersai: WorkersAiProviderLike;
108
+ }
109
+ export { AiBindingLike as A, EmbeddingModelInput as E, LunoraAiOptions as L, ModelInput as M, WorkersAiProviderLike as W, LunoraAi as a, AiGatewayOptions as b };