The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { Worker, WorkerEnvironment } from "../Workers/Worker.ts"; |
| 5 | import { makeClient, tryAiSearch } from "./QuerySearch.ts"; |
| 6 | import { QuerySearchNamespace, type QuerySearchNamespaceClient } from "./QuerySearchNamespace.ts"; |
| 7 | import type { SearchNamespace } from "./SearchNamespace.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Runtime layer for {@link QuerySearchNamespace}. |
| 11 | */ |
| 12 | export const QuerySearchNamespaceBinding = Layer.effect( |
| 13 | QuerySearchNamespace, |
| 14 | Effect.gen(function* () { |
| 15 | const env = yield* WorkerEnvironment; |
| 16 | const host = yield* Worker; |
| 17 | |
| 18 | return Effect.fn(function* (namespace: SearchNamespace) { |
| 19 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 20 | yield* host.bind`${namespace}`({ |
| 21 | bindings: [ |
| 22 | { |
| 23 | type: "ai_search_namespace", |
| 24 | name: namespace.LogicalId, |
| 25 | namespace: namespace.name, |
| 26 | }, |
| 27 | ], |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | const nsEff = Effect.sync( |
| 32 | () => (env as Record<string, runtime.AiSearchNamespace>)[namespace.LogicalId]!, |
| 33 | ); |
| 34 | return { |
| 35 | raw: nsEff, |
| 36 | get: (instanceName) => makeClient(nsEff.pipe(Effect.map((ns) => ns.get(instanceName)))), |
| 37 | list: (params) => Effect.flatMap(nsEff, (ns) => tryAiSearch(() => ns.list(params))), |
| 38 | search: (params) => Effect.flatMap(nsEff, (ns) => tryAiSearch(() => ns.search(params))), |
| 39 | } satisfies QuerySearchNamespaceClient; |
| 40 | }); |
| 41 | }), |
| 42 | ); |