| 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, QuerySearch } from "./QuerySearch.ts"; |
| 6 | import type { SearchInstance } from "./SearchInstance.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Runtime layer for {@link QuerySearch}. |
| 10 | */ |
| 11 | export const QuerySearchBinding = Layer.effect( |
| 12 | QuerySearch, |
| 13 | Effect.gen(function* () { |
| 14 | const env = yield* WorkerEnvironment; |
| 15 | const host = yield* Worker; |
| 16 | |
| 17 | return Effect.fn(function* (instance: SearchInstance) { |
| 18 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 19 | yield* host.bind`${instance}`({ |
| 20 | bindings: [ |
| 21 | { |
| 22 | type: "ai_search", |
| 23 | name: instance.LogicalId, |
| 24 | instanceName: instance.instanceId, |
| 25 | namespace: instance.namespace, |
| 26 | }, |
| 27 | ], |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | const rawEff = Effect.sync( |
| 32 | () => |
| 33 | (env as Record<string, runtime.AiSearchInstance>)[ |
| 34 | instance.LogicalId |
| 35 | ]!, |
| 36 | ); |
| 37 | return makeClient(rawEff); |
| 38 | }); |
| 39 | }), |
| 40 | ); |