alchemy 2.0.0-beta.46 → 2.0.0-beta.47
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/bin/exec.js +18 -10
- package/bin/exec.js.map +1 -1
- package/lib/Apply.js +2 -2
- package/lib/Apply.js.map +1 -1
- package/lib/Cli/commands/_shared.d.ts +7 -1
- package/lib/Cli/commands/_shared.d.ts.map +1 -1
- package/lib/Cli/commands/_shared.js +1 -0
- package/lib/Cli/commands/_shared.js.map +1 -1
- package/lib/Cli/commands/login.d.ts +1 -1
- package/lib/Cli/commands/login.d.ts.map +1 -1
- package/lib/Cli/commands/login.js +45 -42
- package/lib/Cli/commands/login.js.map +1 -1
- package/lib/Cli/commands/logs.js +1 -1
- package/lib/Cli/commands/logs.js.map +1 -1
- package/lib/Cli/commands/state.js +1 -1
- package/lib/Cli/commands/state.js.map +1 -1
- package/lib/Cli/commands/tail.js +1 -1
- package/lib/Cli/commands/tail.js.map +1 -1
- package/lib/Cloudflare/AiGateway/AiGateway.d.ts +112 -1
- package/lib/Cloudflare/AiGateway/AiGateway.d.ts.map +1 -1
- package/lib/Cloudflare/AiGateway/AiGateway.js +112 -1
- package/lib/Cloudflare/AiGateway/AiGateway.js.map +1 -1
- package/lib/Cloudflare/AiGateway/AiGatewayBinding.d.ts +43 -10
- package/lib/Cloudflare/AiGateway/AiGatewayBinding.d.ts.map +1 -1
- package/lib/Cloudflare/AiGateway/AiGatewayBinding.js +38 -10
- package/lib/Cloudflare/AiGateway/AiGatewayBinding.js.map +1 -1
- package/lib/Cloudflare/AiGateway/LanguageModel.d.ts +35 -0
- package/lib/Cloudflare/AiGateway/LanguageModel.d.ts.map +1 -0
- package/lib/Cloudflare/AiGateway/LanguageModel.js +576 -0
- package/lib/Cloudflare/AiGateway/LanguageModel.js.map +1 -0
- package/lib/Cloudflare/AiGateway/index.d.ts +1 -0
- package/lib/Cloudflare/AiGateway/index.d.ts.map +1 -1
- package/lib/Cloudflare/AiGateway/index.js +1 -0
- package/lib/Cloudflare/AiGateway/index.js.map +1 -1
- package/lib/Cloudflare/StateStore/State.d.ts.map +1 -1
- package/lib/Cloudflare/StateStore/State.js +82 -78
- package/lib/Cloudflare/StateStore/State.js.map +1 -1
- package/lib/Cloudflare/Workers/DurableObjectBridge.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/DurableObjectBridge.js +10 -1
- package/lib/Cloudflare/Workers/DurableObjectBridge.js.map +1 -1
- package/lib/Cloudflare/Workers/LocalWorkerProvider.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/LocalWorkerProvider.js +13 -1
- package/lib/Cloudflare/Workers/LocalWorkerProvider.js.map +1 -1
- package/lib/Cloudflare/Workers/Rpc.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/Rpc.js +20 -2
- package/lib/Cloudflare/Workers/Rpc.js.map +1 -1
- package/lib/Cloudflare/Workers/WorkerBridge.d.ts.map +1 -1
- package/lib/Cloudflare/Workers/WorkerBridge.js +20 -2
- package/lib/Cloudflare/Workers/WorkerBridge.js.map +1 -1
- package/lib/Output.js +2 -2
- package/lib/Output.js.map +1 -1
- package/lib/Plan.d.ts +2 -2
- package/lib/Plan.d.ts.map +1 -1
- package/lib/Plan.js +3 -3
- package/lib/Plan.js.map +1 -1
- package/lib/Stack.d.ts.map +1 -1
- package/lib/Stack.js +6 -0
- package/lib/Stack.js.map +1 -1
- package/lib/State/InMemoryState.d.ts +2 -1
- package/lib/State/InMemoryState.d.ts.map +1 -1
- package/lib/State/InMemoryState.js +3 -3
- package/lib/State/InMemoryState.js.map +1 -1
- package/lib/State/LocalState.d.ts.map +1 -1
- package/lib/State/LocalState.js +5 -1
- package/lib/State/LocalState.js.map +1 -1
- package/lib/State/State.d.ts +2 -2
- package/lib/State/State.d.ts.map +1 -1
- package/lib/State/State.js.map +1 -1
- package/lib/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/Apply.ts +2 -2
- package/src/Cli/commands/_shared.ts +7 -1
- package/src/Cli/commands/login.ts +69 -60
- package/src/Cli/commands/logs.ts +1 -1
- package/src/Cli/commands/state.ts +1 -1
- package/src/Cli/commands/tail.ts +1 -1
- package/src/Cloudflare/AiGateway/AiGateway.ts +112 -1
- package/src/Cloudflare/AiGateway/AiGatewayBinding.ts +52 -10
- package/src/Cloudflare/AiGateway/LanguageModel.ts +907 -0
- package/src/Cloudflare/AiGateway/index.ts +1 -0
- package/src/Cloudflare/StateStore/State.ts +113 -107
- package/src/Cloudflare/Workers/DurableObjectBridge.ts +10 -1
- package/src/Cloudflare/Workers/LocalWorkerProvider.ts +13 -1
- package/src/Cloudflare/Workers/Rpc.ts +38 -9
- package/src/Cloudflare/Workers/WorkerBridge.ts +20 -2
- package/src/Output.ts +2 -2
- package/src/Plan.ts +4 -4
- package/src/Stack.ts +6 -0
- package/src/State/InMemoryState.ts +87 -85
- package/src/State/LocalState.ts +15 -1
- package/src/State/State.ts +5 -4
package/package.json
CHANGED
package/src/Apply.ts
CHANGED
|
@@ -138,7 +138,7 @@ export const apply = <P extends Plan>(
|
|
|
138
138
|
Effect.gen(function* () {
|
|
139
139
|
const cli = yield* Cli;
|
|
140
140
|
const session = yield* cli.startApplySession(plan);
|
|
141
|
-
const state = yield* State;
|
|
141
|
+
const state = yield* yield* State;
|
|
142
142
|
const stack = yield* Stack;
|
|
143
143
|
const stage = yield* Stage;
|
|
144
144
|
const stackName = stack.name;
|
|
@@ -1397,7 +1397,7 @@ const collectGarbage = Effect.fnUntraced(function* (
|
|
|
1397
1397
|
plan: Plan,
|
|
1398
1398
|
session: PlanStatusSession,
|
|
1399
1399
|
) {
|
|
1400
|
-
const state = yield* State;
|
|
1400
|
+
const state = yield* yield* State;
|
|
1401
1401
|
const stack = yield* Stack;
|
|
1402
1402
|
const stackName = stack.name;
|
|
1403
1403
|
const stage = yield* Stage;
|
|
@@ -2,6 +2,7 @@ import * as Cause from "effect/Cause";
|
|
|
2
2
|
import * as Config from "effect/Config";
|
|
3
3
|
import * as Console from "effect/Console";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Layer from "effect/Layer";
|
|
5
6
|
import * as Option from "effect/Option";
|
|
6
7
|
import * as Path from "effect/Path";
|
|
7
8
|
import * as S from "effect/Schema";
|
|
@@ -312,5 +313,10 @@ export const importStack = Effect.fn(function* (main: string) {
|
|
|
312
313
|
),
|
|
313
314
|
);
|
|
314
315
|
}
|
|
315
|
-
return stackEffect
|
|
316
|
+
return stackEffect as typeof stackEffect & {
|
|
317
|
+
stackName: string;
|
|
318
|
+
stage: string;
|
|
319
|
+
providers: Layer.Layer<never>;
|
|
320
|
+
state: Layer.Layer<never>;
|
|
321
|
+
};
|
|
316
322
|
});
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as Cause from "effect/Cause";
|
|
2
1
|
import * as Config from "effect/Config";
|
|
3
2
|
import * as ConfigProvider from "effect/ConfigProvider";
|
|
4
3
|
import * as Console from "effect/Console";
|
|
@@ -10,10 +9,10 @@ import { Command, Flag } from "effect/unstable/cli";
|
|
|
10
9
|
import { AuthProviders } from "../../Auth/AuthProvider.ts";
|
|
11
10
|
import { Profile, withProfileOverride } from "../../Auth/Profile.ts";
|
|
12
11
|
import { Stage } from "../../Stage.ts";
|
|
13
|
-
import * as State from "../../State/index.ts";
|
|
14
12
|
import { loadConfigProvider } from "../../Util/ConfigProvider.ts";
|
|
15
13
|
import { fileLogger } from "../../Util/FileLogger.ts";
|
|
16
14
|
|
|
15
|
+
import { Stack } from "../../Stack.ts";
|
|
17
16
|
import {
|
|
18
17
|
envFile,
|
|
19
18
|
importStack,
|
|
@@ -59,73 +58,83 @@ export const loginCommand = Command.make(
|
|
|
59
58
|
|
|
60
59
|
const authProviders: AuthProviders["Service"] = {};
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
// build the state + providers layer to capture the Auth Providers
|
|
62
|
+
yield* Layer.build(
|
|
63
|
+
(stackEffect.providers ?? Layer.empty).pipe(
|
|
64
|
+
Layer.provideMerge(stackEffect.state ?? Layer.empty),
|
|
65
|
+
Layer.provideMerge(
|
|
66
|
+
Layer.mergeAll(
|
|
67
|
+
Layer.succeed(AuthProviders, authProviders),
|
|
68
|
+
ConfigProvider.layer(
|
|
69
|
+
withProfileOverride(
|
|
70
|
+
yield* loadConfigProvider(envFile),
|
|
71
|
+
profile,
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
Logger.layer([fileLogger("out")], { mergeWithExisting: true }),
|
|
75
|
+
Layer.succeed(Stage, stage),
|
|
76
|
+
Layer.succeed(Stack, {
|
|
77
|
+
actions: {},
|
|
78
|
+
bindings: {},
|
|
79
|
+
name: stackEffect.stackName,
|
|
80
|
+
resources: {},
|
|
81
|
+
stage,
|
|
82
|
+
}),
|
|
83
|
+
),
|
|
84
|
+
),
|
|
66
85
|
),
|
|
67
|
-
Logger.layer([fileLogger("out")], { mergeWithExisting: true }),
|
|
68
|
-
Layer.succeed(Stage, stage),
|
|
69
|
-
State.localState(),
|
|
70
86
|
);
|
|
71
87
|
|
|
72
|
-
yield*
|
|
73
|
-
const profiles = yield* Profile;
|
|
74
|
-
yield* Effect.catchCause(stackEffect, (cause) =>
|
|
75
|
-
Console.warn(
|
|
76
|
-
`Ignoring error while building stack for login (likely due to missing or broken credentials):\n${Cause.pretty(cause)}`,
|
|
77
|
-
),
|
|
78
|
-
);
|
|
88
|
+
const profiles = yield* Profile;
|
|
79
89
|
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
const ci = yield* Config.boolean("CI").pipe(Config.withDefault(false));
|
|
91
|
+
const providers = Object.values(authProviders);
|
|
82
92
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
93
|
+
if (providers.length === 0) {
|
|
94
|
+
yield* Console.log(
|
|
95
|
+
"No AuthProviders registered. Make sure the stack's providers() layer includes AuthProviderLayer entries.",
|
|
96
|
+
);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
89
99
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
100
|
+
yield* Effect.forEach(
|
|
101
|
+
providers,
|
|
102
|
+
(provider) =>
|
|
103
|
+
Effect.gen(function* () {
|
|
104
|
+
const existing = yield* profiles.getProfile(profile);
|
|
105
|
+
// --configure treats every provider as missing, so configure
|
|
106
|
+
// runs unconditionally and overwrites the stored entry.
|
|
107
|
+
const stored = configure ? undefined : existing?.[provider.name];
|
|
98
108
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
+
let cfg: { method: string };
|
|
110
|
+
if (stored == null) {
|
|
111
|
+
cfg = yield* provider.configure(profile, { ci });
|
|
112
|
+
yield* profiles.setProfile(profile, {
|
|
113
|
+
...existing,
|
|
114
|
+
[provider.name]: cfg,
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
cfg = stored;
|
|
118
|
+
}
|
|
109
119
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
+
// `read` succeeds when creds are present and not expired
|
|
121
|
+
// (refreshing OAuth proactively if near expiry). Any failure
|
|
122
|
+
// — missing file, dead refresh token, etc. — falls through
|
|
123
|
+
// to `login`.
|
|
124
|
+
yield* provider
|
|
125
|
+
.read(profile, cfg)
|
|
126
|
+
.pipe(Effect.catch(() => provider.login(profile, cfg)));
|
|
127
|
+
}),
|
|
128
|
+
{ discard: true },
|
|
129
|
+
);
|
|
120
130
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}).pipe(Effect.provide(services));
|
|
131
|
+
// Print the resulting profile using the same renderer as
|
|
132
|
+
// `alchemy profile show`.
|
|
133
|
+
const final = yield* profiles.getProfile(profile);
|
|
134
|
+
if (final != null) {
|
|
135
|
+
yield* Console.log("");
|
|
136
|
+
yield* printProfile(profile, final, authProviders);
|
|
137
|
+
}
|
|
129
138
|
}),
|
|
130
139
|
),
|
|
131
140
|
);
|
package/src/Cli/commands/logs.ts
CHANGED
|
@@ -89,7 +89,7 @@ export const logsCommand = Command.make(
|
|
|
89
89
|
const stack = yield* stackEffect;
|
|
90
90
|
|
|
91
91
|
yield* Effect.gen(function* () {
|
|
92
|
-
const state = yield* State.State;
|
|
92
|
+
const state = yield* yield* State.State;
|
|
93
93
|
const filterSet = parseResourceFilter(filter);
|
|
94
94
|
const availableIds = [
|
|
95
95
|
...new Set(Object.values(stack.resources).map((r) => r.LogicalId)),
|
|
@@ -90,7 +90,7 @@ const withStateService = <A, E>(
|
|
|
90
90
|
return yield* Effect.gen(function* () {
|
|
91
91
|
const stack = yield* stackEffect;
|
|
92
92
|
return yield* Effect.gen(function* () {
|
|
93
|
-
const state = yield* State.State;
|
|
93
|
+
const state = yield* yield* State.State;
|
|
94
94
|
return yield* body(state);
|
|
95
95
|
}).pipe(Effect.provide(stack.services));
|
|
96
96
|
}).pipe(Effect.provide(services));
|
package/src/Cli/commands/tail.ts
CHANGED
|
@@ -62,7 +62,7 @@ export const tailCommand = Command.make(
|
|
|
62
62
|
const stack = yield* stackEffect;
|
|
63
63
|
|
|
64
64
|
yield* Effect.gen(function* () {
|
|
65
|
-
const state = yield* State.State;
|
|
65
|
+
const state = yield* yield* State.State;
|
|
66
66
|
const filterSet = parseResourceFilter(filter);
|
|
67
67
|
const availableIds = [
|
|
68
68
|
...new Set(Object.values(stack.resources).map((r) => r.LogicalId)),
|
|
@@ -231,7 +231,11 @@ export const isAiGateway = (value: unknown): value is AiGateway =>
|
|
|
231
231
|
* governance across AI provider requests.
|
|
232
232
|
*
|
|
233
233
|
* AI Gateway gives your application a stable gateway ID and account-scoped
|
|
234
|
-
* endpoint that can route model requests through Cloudflare.
|
|
234
|
+
* endpoint that can route model requests through Cloudflare. Once bound to a
|
|
235
|
+
* Worker, `aiGateway.model({...})` returns an `effect/unstable/ai`
|
|
236
|
+
* `LanguageModel` Layer so you use the standard `generateText` / `streamText`
|
|
237
|
+
* APIs — provider-agnostic, with caching, rate limiting, retries, and a
|
|
238
|
+
* unified request log handled by the gateway.
|
|
235
239
|
*
|
|
236
240
|
* @section Creating a Gateway
|
|
237
241
|
* @example Basic gateway
|
|
@@ -260,6 +264,113 @@ export const isAiGateway = (value: unknown): value is AiGateway =>
|
|
|
260
264
|
* logManagementStrategy: "STOP_INSERTING",
|
|
261
265
|
* });
|
|
262
266
|
* ```
|
|
267
|
+
*
|
|
268
|
+
* @section Binding into a Worker
|
|
269
|
+
* @example Bind the gateway and provide the runtime layer
|
|
270
|
+
* `AiGateway.bind(gateway)` returns a typed, Effect-native client during the
|
|
271
|
+
* Worker's Init phase. Provide `Cloudflare.AiGatewayBindingLive` once at the
|
|
272
|
+
* bottom of the Init layer chain so every `bind(...)` resolves at runtime.
|
|
273
|
+
* ```typescript
|
|
274
|
+
* import * as Cloudflare from "alchemy/Cloudflare";
|
|
275
|
+
* import * as Effect from "effect/Effect";
|
|
276
|
+
* import { Gateway } from "./AiGateway.ts";
|
|
277
|
+
*
|
|
278
|
+
* export default class Api extends Cloudflare.Worker<Api>()(
|
|
279
|
+
* "Api",
|
|
280
|
+
* { main: import.meta.path },
|
|
281
|
+
* Effect.gen(function* () {
|
|
282
|
+
* const aiGateway = yield* Cloudflare.AiGateway.bind(Gateway);
|
|
283
|
+
*
|
|
284
|
+
* return {
|
|
285
|
+
* fetch: Effect.gen(function* () {
|
|
286
|
+
* // …routes
|
|
287
|
+
* }),
|
|
288
|
+
* };
|
|
289
|
+
* }).pipe(Effect.provide(Cloudflare.AiGatewayBindingLive)),
|
|
290
|
+
* ) {}
|
|
291
|
+
* ```
|
|
292
|
+
*
|
|
293
|
+
* @section Building a LanguageModel
|
|
294
|
+
* @example `aiGateway.model(...)` -> Effect AI `LanguageModel`
|
|
295
|
+
* Call `aiGateway.model({...})` with a Workers AI model id. It returns a
|
|
296
|
+
* `Layer<LanguageModel, never, RuntimeContext>` directly — no API key and no
|
|
297
|
+
* `Layer.unwrap`, since the binding handles auth and the gateway URL. Build it
|
|
298
|
+
* in the Init phase; construction is pure.
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const aiGateway = yield* Cloudflare.AiGateway.bind(Gateway);
|
|
301
|
+
*
|
|
302
|
+
* const languageModel = aiGateway.model({
|
|
303
|
+
* client: aiGateway,
|
|
304
|
+
* model: "@cf/meta/llama-3.1-8b-instruct",
|
|
305
|
+
* parameters: { temperature: 0.7, maxTokens: 1024 },
|
|
306
|
+
* });
|
|
307
|
+
* ```
|
|
308
|
+
*
|
|
309
|
+
* @section Generating Text
|
|
310
|
+
* @example Generate text on a route
|
|
311
|
+
* Provide the `languageModel` layer to the handler and call
|
|
312
|
+
* `LanguageModel.generateText` like any other Effect. `Effect.orDie` collapses
|
|
313
|
+
* `AiError` to a defect (a 500); use `Effect.catchTag("AiError", …)` for typed
|
|
314
|
+
* handling instead.
|
|
315
|
+
* ```typescript
|
|
316
|
+
* import { LanguageModel } from "effect/unstable/ai";
|
|
317
|
+
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
|
318
|
+
*
|
|
319
|
+
* fetch: Effect.gen(function* () {
|
|
320
|
+
* const response = yield* LanguageModel.generateText({
|
|
321
|
+
* prompt: "Say hello.",
|
|
322
|
+
* }).pipe(Effect.orDie);
|
|
323
|
+
* return yield* HttpServerResponse.json({
|
|
324
|
+
* text: response.text,
|
|
325
|
+
* usage: {
|
|
326
|
+
* inputTokens: response.usage.inputTokens.total,
|
|
327
|
+
* outputTokens: response.usage.outputTokens.total,
|
|
328
|
+
* },
|
|
329
|
+
* });
|
|
330
|
+
* }).pipe(Effect.provide(languageModel));
|
|
331
|
+
* ```
|
|
332
|
+
*
|
|
333
|
+
* @section Streaming Text
|
|
334
|
+
* @example Stream tokens as Server-Sent Events
|
|
335
|
+
* `LanguageModel.streamText` returns a `Stream` of typed response parts.
|
|
336
|
+
* `Stream.provide(languageModel)` keeps the model available for the whole
|
|
337
|
+
* stream lifetime; pipe through `Sse.encode` for an SSE response.
|
|
338
|
+
* ```typescript
|
|
339
|
+
* import { LanguageModel } from "effect/unstable/ai";
|
|
340
|
+
* import * as Stream from "effect/Stream";
|
|
341
|
+
* import * as Sse from "effect/unstable/encoding/Sse";
|
|
342
|
+
* import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
|
343
|
+
*
|
|
344
|
+
* const stream = LanguageModel.streamText({ prompt }).pipe(
|
|
345
|
+
* Stream.provide(languageModel),
|
|
346
|
+
* Sse.encode,
|
|
347
|
+
* );
|
|
348
|
+
* return HttpServerResponse.stream(stream, {
|
|
349
|
+
* headers: {
|
|
350
|
+
* "content-type": "text/event-stream",
|
|
351
|
+
* "cache-control": "no-cache",
|
|
352
|
+
* "x-accel-buffering": "no",
|
|
353
|
+
* },
|
|
354
|
+
* });
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* @section Tuning the Gateway
|
|
358
|
+
* @example Production-grade caching, rate limits, and DLP
|
|
359
|
+
* Every prop maps to an in-place update — no replacement, no downtime.
|
|
360
|
+
* ```typescript
|
|
361
|
+
* export const Gateway = Cloudflare.AiGateway("Gateway", {
|
|
362
|
+
* id: "prod-gateway",
|
|
363
|
+
* cacheTtl: 300,
|
|
364
|
+
* cacheInvalidateOnUpdate: true,
|
|
365
|
+
* rateLimitingInterval: 60,
|
|
366
|
+
* rateLimitingLimit: 100,
|
|
367
|
+
* rateLimitingTechnique: "sliding",
|
|
368
|
+
* collectLogs: true,
|
|
369
|
+
* logManagement: 100_000,
|
|
370
|
+
* logManagementStrategy: "DELETE_OLDEST",
|
|
371
|
+
* authentication: true,
|
|
372
|
+
* });
|
|
373
|
+
* ```
|
|
263
374
|
*/
|
|
264
375
|
export const AiGateway = Resource<AiGateway>("Cloudflare.AiGateway")({
|
|
265
376
|
/**
|
|
@@ -3,15 +3,18 @@
|
|
|
3
3
|
import * as Data from "effect/Data";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
5
5
|
import * as Layer from "effect/Layer";
|
|
6
|
+
import type { LanguageModel } from "effect/unstable/ai/LanguageModel";
|
|
6
7
|
import * as Binding from "../../Binding.ts";
|
|
7
8
|
import type { ResourceLike } from "../../Resource.ts";
|
|
8
9
|
import type { RuntimeContext } from "../../RuntimeContext.ts";
|
|
9
10
|
import { isWorker, WorkerEnvironment } from "../Workers/Worker.ts";
|
|
10
11
|
import type { AiGateway as AiGatewayResource } from "./AiGateway.ts";
|
|
12
|
+
import {
|
|
13
|
+
makeLanguageModelLayer,
|
|
14
|
+
type LanguageModelOptions,
|
|
15
|
+
} from "./LanguageModel.ts";
|
|
11
16
|
|
|
12
|
-
|
|
13
|
-
* Error raised by AI Gateway runtime operations.
|
|
14
|
-
*/
|
|
17
|
+
// Error raised by AI Gateway runtime operations.
|
|
15
18
|
export class AiGatewayError extends Data.TaggedError("AiGatewayError")<{
|
|
16
19
|
/**
|
|
17
20
|
* Human-readable runtime error message.
|
|
@@ -39,6 +42,12 @@ export interface AiGatewayClient {
|
|
|
39
42
|
* Effect resolving to the raw AI Gateway runtime binding.
|
|
40
43
|
*/
|
|
41
44
|
gateway: Effect.Effect<AiGateway, never, RuntimeContext>;
|
|
45
|
+
/**
|
|
46
|
+
* Effect resolving to the gateway id (the resource attribute, captured at
|
|
47
|
+
* bind time). Useful when calling `ai.run(model, inputs, { gateway: { id } })`
|
|
48
|
+
* — the in-account path for first-party services like Workers AI.
|
|
49
|
+
*/
|
|
50
|
+
id: Effect.Effect<string, never, RuntimeContext>;
|
|
42
51
|
/**
|
|
43
52
|
* Update metadata on an existing AI Gateway log entry.
|
|
44
53
|
*/
|
|
@@ -65,17 +74,27 @@ export interface AiGatewayClient {
|
|
|
65
74
|
data: Parameters<AiGateway["run"]>[0],
|
|
66
75
|
options?: Parameters<AiGateway["run"]>[1],
|
|
67
76
|
): Effect.Effect<Response, AiGatewayError, RuntimeContext>;
|
|
77
|
+
|
|
78
|
+
model(
|
|
79
|
+
options: LanguageModelOptions,
|
|
80
|
+
): Layer.Layer<LanguageModel, never, RuntimeContext>;
|
|
68
81
|
}
|
|
69
82
|
|
|
70
83
|
/**
|
|
71
|
-
* Binding service that turns an {@link AiGatewayResource} resource
|
|
72
|
-
* {@link AiGatewayClient} for Worker runtime code.
|
|
84
|
+
* Binding service that turns an {@link AiGatewayResource} resource
|
|
85
|
+
* into a typed {@link AiGatewayClient} for Worker runtime code. Wraps
|
|
86
|
+
* the Cloudflare AI Gateway runtime binding so each operation returns
|
|
87
|
+
* an Effect tagged with {@link AiGatewayError}, exposes the raw
|
|
88
|
+
* Workers AI handle for `ai.run(...)`, and provides a `model(options)`
|
|
89
|
+
* factory that produces an `effect/unstable/ai` `LanguageModel`
|
|
90
|
+
* `Layer`.
|
|
73
91
|
*
|
|
74
|
-
* @
|
|
75
|
-
* Bind the gateway during the Worker's init phase, then use `run` or `getUrl`
|
|
76
|
-
* from request handlers.
|
|
92
|
+
* @binding
|
|
77
93
|
*
|
|
94
|
+
* @section Calling AI Gateway
|
|
78
95
|
* @example Run through a gateway
|
|
96
|
+
* Bind the gateway during the Worker's init phase, then use `run` or
|
|
97
|
+
* `getUrl` from request handlers.
|
|
79
98
|
* ```typescript
|
|
80
99
|
* const aiGateway = yield* Cloudflare.AiGatewayBinding.bind(gateway);
|
|
81
100
|
*
|
|
@@ -91,8 +110,29 @@ export interface AiGatewayClient {
|
|
|
91
110
|
* };
|
|
92
111
|
* ```
|
|
93
112
|
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
113
|
+
* @section Driving Effect AI through the gateway
|
|
114
|
+
* @example `aiGateway.model(...)` -> Effect AI `LanguageModel`
|
|
115
|
+
* `model(options)` produces a `Layer<LanguageModel, never,
|
|
116
|
+
* RuntimeContext>` that translates `LanguageModel.generateText` /
|
|
117
|
+
* `streamText` calls (including tool calls and structured outputs)
|
|
118
|
+
* into `ai.run(...)` against the bound Workers AI model, routed
|
|
119
|
+
* through the gateway.
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const aiGateway = yield* Cloudflare.AiGatewayBinding.bind(gateway);
|
|
122
|
+
*
|
|
123
|
+
* const languageModel = aiGateway.model({
|
|
124
|
+
* client: aiGateway,
|
|
125
|
+
* model: "@cf/meta/llama-3.1-8b-instruct",
|
|
126
|
+
* parameters: { temperature: 0.7, maxTokens: 1024 },
|
|
127
|
+
* });
|
|
128
|
+
*
|
|
129
|
+
* const response = yield* AiLanguageModel.generateText({ prompt }).pipe(
|
|
130
|
+
* Effect.provide(languageModel),
|
|
131
|
+
* );
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* Provide {@link AiGatewayBindingLive} in the worker's runtime layer
|
|
135
|
+
* to resolve the underlying Cloudflare AI binding at request time.
|
|
96
136
|
*/
|
|
97
137
|
export class AiGatewayBinding extends Binding.Service<
|
|
98
138
|
AiGatewayBinding,
|
|
@@ -129,11 +169,13 @@ export const AiGatewayBindingLive = Layer.effect(
|
|
|
129
169
|
return {
|
|
130
170
|
raw: ai,
|
|
131
171
|
gateway: runtimeGateway,
|
|
172
|
+
id: gatewayIdAccessor,
|
|
132
173
|
patchLog: (logId, data) =>
|
|
133
174
|
use((gateway) => gateway.patchLog(logId, data)),
|
|
134
175
|
getLog: (logId) => use((gateway) => gateway.getLog(logId)),
|
|
135
176
|
getUrl: (provider) => use((gateway) => gateway.getUrl(provider)),
|
|
136
177
|
run: (data, options) => use((gateway) => gateway.run(data, options)),
|
|
178
|
+
model: (options) => makeLanguageModelLayer(options),
|
|
137
179
|
} satisfies AiGatewayClient;
|
|
138
180
|
});
|
|
139
181
|
}),
|