The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 07:15 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | |
| 7 | /** |
| 8 | * Effect-native Worker fixture exercising the Effect-first AI Search bindings: |
| 9 | * - `Cloudflare.AI.Search.Search(search)` attaches the single-instance |
| 10 | * `ai_search` binding and returns an Effect-native `InstanceClient`. The |
| 11 | * `search` is the `AiSearch` construct's result — proving it's usable |
| 12 | * anywhere an `AiSearchInstance` is, here passed straight to `Search(...)`. |
| 13 | * - `Cloudflare.AI.SearchNamespace(namespace)` attaches the |
| 14 | * `ai_search_namespace` binding; `.get(name)` scopes to an instance. |
| 15 | * |
| 16 | * The `/bindings` route resolves each client's `raw` runtime handle (an |
| 17 | * `AiSearchInstance` / `AiSearchNamespace`) and reports its runtime shape — |
| 18 | * proving the Effect clients wire through to the live runtime bindings (it |
| 19 | * does not query, which needs indexed data). |
| 20 | */ |
| 21 | export default class AiSearchEffectBindingsWorker extends Cloudflare.Worker<AiSearchEffectBindingsWorker>()( |
| 22 | "AiSearchEffectBindingsWorker", |
| 23 | { |
| 24 | main: import.meta.url, |
| 25 | }, |
| 26 | Effect.gen(function* () { |
| 27 | const bucket = yield* Cloudflare.R2.Bucket("AiSearchEffectBindingBucket", { |
| 28 | forceDestroy: true, |
| 29 | }); |
| 30 | const namespace = yield* Cloudflare.AI.SearchNamespace("AiSearchEffectBindingNs"); |
| 31 | const aiSearch = yield* Cloudflare.AI.Search("AiSearchEffectBindingInstance", { |
| 32 | source: bucket, |
| 33 | }); |
| 34 | const search = yield* Cloudflare.AI.QuerySearch(aiSearch); |
| 35 | const ns = yield* Cloudflare.AI.QuerySearchNamespace(namespace); |
| 36 | |
| 37 | return { |
| 38 | fetch: Effect.gen(function* () { |
| 39 | const request = yield* HttpServerRequest; |
| 40 | if (request.url.includes("/bindings")) { |
| 41 | const raw = yield* search.raw; |
| 42 | const nsRaw = yield* ns.raw; |
| 43 | return yield* HttpServerResponse.json({ |
| 44 | mode: "effect", |
| 45 | searchRaw: typeof raw, |
| 46 | searchChatCompletions: typeof raw.chatCompletions, |
| 47 | searchSearch: typeof raw.search, |
| 48 | nsRaw: typeof nsRaw, |
| 49 | nsChatCompletions: typeof nsRaw.get("docs-search").chatCompletions, |
| 50 | }); |
| 51 | } |
| 52 | return HttpServerResponse.text("ok"); |
| 53 | }), |
| 54 | }; |
| 55 | }).pipe( |
| 56 | Effect.provide( |
| 57 | Layer.mergeAll(Cloudflare.AI.QuerySearchBinding, Cloudflare.AI.QuerySearchNamespaceBinding), |
| 58 | ), |
| 59 | ), |
| 60 | ) {} |