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, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type * as HttpClient from "effect/http/HttpClient"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 5 | import type { Credentials } from "../Credentials.ts"; |
| 6 | import { QuerySearchNamespace } from "./QuerySearchNamespace.ts"; |
| 7 | import { makeLocalSearchNamespaceClient, type SearchAuth } from "./SearchHttpClient.ts"; |
| 8 | import type { SearchNamespace } from "./SearchNamespace.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Local implementation of the {@link QuerySearchNamespace} binding — queries an |
| 12 | * AI Search namespace over the Cloudflare HTTP API using the **current |
| 13 | * credentials** instead of a native Worker binding |
| 14 | * (`QuerySearchNamespaceBinding`). |
| 15 | * |
| 16 | * Provide it on an {@link Action} (or any deploy-time Effect) to `list` |
| 17 | * instances, run a multi-instance `search`, or `.get(instanceName)` a |
| 18 | * single-instance client — the same client you'd use inside a Worker. The |
| 19 | * namespace name is resolved at apply time through the ambient RuntimeContext. |
| 20 | * |
| 21 | * `raw` (the native `AiSearchNamespace` runtime handle) is unavailable outside a |
| 22 | * deployed Worker and dies if forced. |
| 23 | * |
| 24 | * @example List a namespace's instances from an Action |
| 25 | * ```typescript |
| 26 | * const Probe = Alchemy.Action( |
| 27 | * "Probe", |
| 28 | * Effect.gen(function* () { |
| 29 | * const ns = yield* Cloudflare.AI.QuerySearchNamespace(namespace); |
| 30 | * return Effect.fn(function* () { |
| 31 | * const { result } = yield* ns.list(); |
| 32 | * return result.map((i) => i.id); |
| 33 | * }); |
| 34 | * }).pipe(Effect.provide(Cloudflare.AI.QuerySearchNamespaceLocal)), |
| 35 | * ); |
| 36 | * ``` |
| 37 | */ |
| 38 | export const QuerySearchNamespaceLocal = Layer.effect( |
| 39 | QuerySearchNamespace, |
| 40 | Effect.gen(function* () { |
| 41 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 42 | const context = yield* Effect.context<Credentials | HttpClient.HttpClient>(); |
| 43 | |
| 44 | const auth: SearchAuth = { |
| 45 | authorize: (eff) => eff.pipe(Effect.provideContext(context)), |
| 46 | accountId, |
| 47 | }; |
| 48 | |
| 49 | return Effect.fn(function* (namespace: SearchNamespace) { |
| 50 | // Deferred accessor — resolved against the tracker at apply time. |
| 51 | const name = yield* namespace.name; |
| 52 | return makeLocalSearchNamespaceClient(auth, name); |
| 53 | }); |
| 54 | }), |
| 55 | ); |