The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import { expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import { Action } from "alchemy/Action"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpClient from "effect/http/HttpClient"; |
| 7 | import { MinimumLogLevel } from "effect/References"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import AiSearchCrawlTargetWorker from "./fixtures/crawl-target-worker.ts"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: Cloudflare.providers() }); |
| 12 | |
| 13 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 14 | |
| 15 | // Exercise `QuerySearchLocal` — the current-credentials HTTP implementation of |
| 16 | // the `QuerySearch` binding — from inside an Action. A web-crawler instance |
| 17 | // needs no service token (unlike an R2 source), so teardown stays fast. The |
| 18 | // Action reads instance metadata (`info`) and indexing stats (`stats`), both |
| 19 | // available immediately after create (no waiting on async indexing), proving |
| 20 | // the client talks to the live AI Search REST API with the current creds. |
| 21 | test.provider( |
| 22 | "QuerySearchLocal: read instance info/stats from an Action", |
| 23 | (stack) => |
| 24 | Effect.gen(function* () { |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | // Warm the crawl target first — AI Search fetches the seed synchronously |
| 28 | // when it validates a web-crawler at create time. |
| 29 | const warmed = yield* stack.deploy( |
| 30 | Effect.gen(function* () { |
| 31 | return yield* AiSearchCrawlTargetWorker; |
| 32 | }), |
| 33 | ); |
| 34 | const client = yield* HttpClient.HttpClient; |
| 35 | yield* client.get(warmed.url as string).pipe( |
| 36 | Effect.flatMap((res) => |
| 37 | res.status === 200 |
| 38 | ? Effect.succeed(res) |
| 39 | : Effect.fail(new Error(`crawl target not ready: ${res.status}`)), |
| 40 | ), |
| 41 | Effect.retry({ schedule: Schedule.spaced("3 seconds"), times: 20 }), |
| 42 | ); |
| 43 | |
| 44 | const out = yield* stack.deploy( |
| 45 | Effect.gen(function* () { |
| 46 | const target = yield* AiSearchCrawlTargetWorker; |
| 47 | const instance = yield* Cloudflare.AI.SearchInstance("Search", { |
| 48 | type: "web-crawler", |
| 49 | source: target.url.as<string>(), |
| 50 | sourceParams: { |
| 51 | webCrawler: { |
| 52 | // Cloudflare renamed `crawl` → `discover` and removed |
| 53 | // `crawlOptions` from the API. The fixture serves a |
| 54 | // sitemap, so discovery always finds content. |
| 55 | parseType: "discover", |
| 56 | }, |
| 57 | }, |
| 58 | }); |
| 59 | |
| 60 | const Probe = Action( |
| 61 | "Probe", |
| 62 | Effect.gen(function* () { |
| 63 | const search = yield* Cloudflare.AI.QuerySearch(instance); |
| 64 | const instanceId = yield* instance.instanceId; |
| 65 | const namespace = yield* instance.namespace; |
| 66 | |
| 67 | return Effect.fn(function* () { |
| 68 | const info = yield* search.info().pipe( |
| 69 | Effect.retry({ |
| 70 | schedule: Schedule.exponential("500 millis"), |
| 71 | times: 6, |
| 72 | }), |
| 73 | ); |
| 74 | const stats = yield* search.stats().pipe( |
| 75 | Effect.retry({ |
| 76 | schedule: Schedule.exponential("500 millis"), |
| 77 | times: 6, |
| 78 | }), |
| 79 | ); |
| 80 | return { |
| 81 | instanceId: yield* instanceId, |
| 82 | namespace: yield* namespace, |
| 83 | info, |
| 84 | stats, |
| 85 | }; |
| 86 | }); |
| 87 | }).pipe(Effect.provide(Cloudflare.AI.QuerySearchLocal)), |
| 88 | ); |
| 89 | |
| 90 | return yield* Probe({}); |
| 91 | }), |
| 92 | ); |
| 93 | |
| 94 | expect(out.instanceId).toBeTruthy(); |
| 95 | // `info()` round-trips the AI Search REST read through the current creds. |
| 96 | expect(out.info.id).toEqual(out.instanceId); |
| 97 | expect(out.info.type).toEqual("web-crawler"); |
| 98 | expect(out.info.namespace).toEqual(out.namespace); |
| 99 | // `stats()` returns the indexing status counts object. |
| 100 | expect(out.stats).toBeTypeOf("object"); |
| 101 | |
| 102 | yield* stack.destroy(); |
| 103 | }).pipe(logLevel), |
| 104 | { |
| 105 | tags: ["provider:cloudflare", "provider:cloudflare:ai", "provider:cloudflare:worker", "live"], |
| 106 | timeout: 300_000, |
| 107 | }, |
| 108 | ); |