The best way to control Claude Code, Codex and more from any computer or phone

main
ts 52 lines 1.81 KB
Raw
1 /// <reference types="@cloudflare/workers-types" />
2
3 import * as Effect from "effect/Effect";
4 import { makeLanguageModelLayer } from "../AI/LanguageModel.ts";
5 import { AI, WorkersAIError, type AIClient } from "./AI.ts";
6 import * as Binding from "./Binding.ts";
7 import { makeBindingLayer } from "./BindingLayer.ts";
8
9 /** The binding value produced by calling {@link AI} (declared on `env` or `yield*`-ed). */
10 export type AIBinding = Binding.Binding<AI["key"], AIClient, AI>;
11
12 /**
13 * The layer that provides the Effect-native interface for the Cloudflare
14 * Workers AI binding.
15 *
16 * Provide it on the Worker effect (`Effect.provide(Cloudflare.Workers.AIBinding)`)
17 * so that yielding an {@link AI} binding attaches the native `ai` binding to
18 * the surrounding Worker at deploy time and, at runtime, resolves to the
19 * Effect-native {@link AIClient} (wrapping the raw `Ai` handle so `run` /
20 * `models` return Effects and `model(...)` yields a `LanguageModel` layer).
21 */
22 export const AIBinding = makeBindingLayer<AI, Ai, AIClient>(AI, (raw) => {
23 const self: AIClient = {
24 raw,
25 run: (model, inputs, options) =>
26 Effect.gen(function* () {
27 const ai = yield* raw;
28 return yield* tryPromise(() => ai.run(model, inputs, options));
29 }),
30 models: (params) =>
31 Effect.gen(function* () {
32 const ai = yield* raw;
33 return yield* tryPromise(() => ai.models(params));
34 }),
35 model: (options) =>
36 makeLanguageModelLayer({
37 ...options,
38 client: self,
39 }),
40 };
41 return self;
42 });
43
44 const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, WorkersAIError> =>
45 Effect.tryPromise({
46 try: fn,
47 catch: (error) =>
48 new WorkersAIError({
49 message: error instanceof Error ? error.message : "Unknown Workers AI runtime error",
50 cause: error,
51 }),
52 });