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

main
ts 194 lines 7.07 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as ai from "@distilled.cloud/cloudflare/ai";
6 import * as queues from "@distilled.cloud/cloudflare/queues";
7 import * as Output from "alchemy/Output";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as Schedule from "effect/Schedule";
11 import * as Schema from "effect/Schema";
12
13 const { test } = Test.make({ providers: Cloudflare.providers() });
14
15 test.provider(
16 "a model handle validates, replaces and preserves catalog models on destroy",
17 (stack) =>
18 Effect.gen(function* () {
19 yield* stack.destroy();
20 const { accountId } = yield* yield* CloudflareEnvironment;
21 const first = yield* stack.deploy(
22 Cloudflare.AI.Model("Model", { modelName: "@cf/baai/bge-m3" }),
23 );
24 expect(first).toEqual({ accountId, modelName: "@cf/baai/bge-m3" });
25 const updated = yield* stack.deploy(
26 Cloudflare.AI.Model("Model", {
27 modelName: "@cf/baai/bge-base-en-v1.5",
28 }),
29 );
30 expect(updated.modelName).toBe("@cf/baai/bge-base-en-v1.5");
31 yield* ai.getModelSchema({ accountId, model: first.modelName });
32 yield* stack.destroy();
33 yield* ai.getModelSchema({ accountId, model: updated.modelName });
34 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
35 {
36 tags: ["provider:cloudflare", "provider:cloudflare:ai", "live"],
37 timeout: 120_000,
38 exclusive: "cloudflare:event-subscriptions",
39 },
40 );
41
42 test.provider(
43 "an unresolved model identity replaces its downstream subscription",
44 (stack) =>
45 Effect.gen(function* () {
46 yield* stack.destroy();
47 const program = (suffix: string) =>
48 Effect.gen(function* () {
49 const identity = yield* Cloudflare.R2.Bucket("Identity", {
50 name: `alchemy-model-subscription-identity-${suffix}`,
51 });
52 const model = yield* Cloudflare.AI.Model("Model", {
53 modelName: identity.bucketName.pipe(
54 Output.map((name) =>
55 name.endsWith("-a") ? "@cf/baai/bge-m3" : "@cf/baai/bge-base-en-v1.5",
56 ),
57 ),
58 });
59 const queue = yield* Cloudflare.Queues.Queue("Queue");
60 const subscription = yield* Cloudflare.Queues.Subscription("Events", {
61 source: model,
62 events: ["batch.queued"],
63 queueId: queue.queueId,
64 });
65 return { model, subscription };
66 });
67 const initial = yield* stack.deploy(program("a"));
68 const replaced = yield* stack.deploy(program("b"));
69 expect(replaced.model.modelName).toBe("@cf/baai/bge-base-en-v1.5");
70 expect(replaced.subscription.subscriptionId).not.toBe(initial.subscription.subscriptionId);
71 const observed = yield* queues.getSubscription({
72 accountId: replaced.subscription.accountId,
73 subscriptionId: replaced.subscription.subscriptionId,
74 });
75 expect(observed.source).toEqual(
76 expect.objectContaining({
77 type: "workersAi.model",
78 modelName: replaced.model.modelName,
79 }),
80 );
81 yield* stack.destroy();
82 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
83 {
84 tags: [
85 "provider:cloudflare",
86 "provider:cloudflare:ai",
87 "provider:cloudflare:queue",
88 "provider:cloudflare:r2",
89 "live",
90 ],
91 timeout: 120_000,
92 exclusive: "cloudflare:event-subscriptions",
93 },
94 );
95
96 test.provider(
97 "a persisted model reference receives a real asynchronous batch event",
98 (stack) =>
99 Effect.gen(function* () {
100 yield* stack.destroy();
101 const { accountId } = yield* yield* CloudflareEnvironment;
102 const model = Cloudflare.AI.Model("Model", {
103 modelName: "@cf/baai/bge-m3",
104 });
105 yield* stack.deploy(model);
106 const deployed = yield* stack.deploy(
107 Effect.gen(function* () {
108 yield* model;
109 const queue = yield* Cloudflare.Queues.Queue("BatchEvents");
110 const worker = yield* Cloudflare.Worker("BatchWorker", {
111 main: `${import.meta.dirname}/fixtures/model-batch.ts`,
112 env: { AI: Cloudflare.Workers.AI() },
113 });
114 const subscription = yield* Cloudflare.Queues.Subscription("ModelEvents", {
115 source: yield* Cloudflare.AI.Model.ref("Model"),
116 events: ["batch.queued", "batch.succeeded", "batch.failed"],
117 queueId: queue.queueId,
118 });
119 return { queue, worker, subscription };
120 }),
121 );
122 yield* queues.createConsumer({
123 accountId,
124 queueId: deployed.queue.queueId,
125 type: "http_pull",
126 });
127 const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient);
128 yield* client.get(deployed.worker.url!).pipe(
129 Effect.flatMap((response) => response.text),
130 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 8 }),
131 );
132 const batch = yield* client.post(deployed.worker.url!).pipe(
133 Effect.flatMap((response) => response.json),
134 Effect.flatMap(Schema.decodeUnknownEffect(Schema.Struct({ request_id: Schema.String }))),
135 );
136 const bodies: string[] = [];
137 const delivered = () =>
138 bodies.some(
139 (body) =>
140 body.includes("cf.workersAi.model.batch.") &&
141 body.includes(batch.request_id) &&
142 body.includes(deployed.subscription.subscriptionId) &&
143 body.includes(accountId),
144 );
145 yield* Effect.gen(function* () {
146 const pulled = yield* queues
147 .pullMessage({
148 accountId,
149 queueId: deployed.queue.queueId,
150 batchSize: 100,
151 visibilityTimeoutMs: 1000,
152 })
153 .pipe(
154 Effect.retry({
155 while: (error) => error._tag === "QueueHttpPullNotEnabled",
156 schedule: Schedule.spaced("2 seconds"),
157 times: 8,
158 }),
159 );
160 for (const message of pulled.messages ?? []) if (message.body) bodies.push(message.body);
161 const acks = (pulled.messages ?? []).flatMap((message) =>
162 message.leaseId ? [{ leaseId: message.leaseId }] : [],
163 );
164 if (acks.length)
165 yield* queues.ackMessage({
166 accountId,
167 queueId: deployed.queue.queueId,
168 acks,
169 });
170 }).pipe(
171 Effect.repeat({
172 schedule: Schedule.spaced("5 seconds"),
173 times: 10,
174 until: delivered,
175 }),
176 Effect.timeout("60 seconds"),
177 );
178 expect(delivered()).toBe(true);
179 yield* stack.deploy(model);
180 yield* ai.getModelSchema({ accountId, model: "@cf/baai/bge-m3" });
181 yield* stack.destroy();
182 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
183 {
184 tags: [
185 "provider:cloudflare",
186 "provider:cloudflare:ai",
187 "provider:cloudflare:queue",
188 "provider:cloudflare:worker",
189 "live",
190 ],
191 timeout: 120_000,
192 exclusive: "cloudflare:event-subscriptions",
193 },
194 );