@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 46 lines 1.67 KB
Raw
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 { type SearchIndexClient, SearchIndex } from "./SearchIndex.ts";
6 import type { Index } from "./VectorizeIndex.ts";
7
8 export const SearchIndexBinding = Layer.effect(
9 SearchIndex,
10 Effect.gen(function* () {
11 const env = yield* WorkerEnvironment;
12 const host = yield* Worker;
13
14 return Effect.fn(function* (index: Index) {
15 if (!globalThis.__ALCHEMY_RUNTIME__) {
16 yield* host.bind`${index}`({
17 bindings: [
18 {
19 type: "vectorize",
20 name: index.LogicalId,
21 indexName: index.indexName,
22 },
23 ],
24 });
25 }
26
27 const rawEff = Effect.sync(
28 () => (env as Record<string, runtime.Vectorize>)[index.LogicalId]!,
29 );
30
31 const withRuntime = <A>(fn: (raw: runtime.Vectorize) => Promise<A>) =>
32 Effect.flatMap(rawEff, (raw) => Effect.promise(() => fn(raw)));
33
34 return {
35 raw: rawEff,
36 describe: () => withRuntime((raw) => raw.describe()),
37 query: (vector, options) => withRuntime((raw) => raw.query(vector, options)),
38 queryById: (vectorId, options) => withRuntime((raw) => raw.queryById(vectorId, options)),
39 insert: (vectors) => withRuntime((raw) => raw.insert(vectors)),
40 upsert: (vectors) => withRuntime((raw) => raw.upsert(vectors)),
41 deleteByIds: (ids) => withRuntime((raw) => raw.deleteByIds(ids)),
42 getByIds: (ids) => withRuntime((raw) => raw.getByIds(ids)),
43 } satisfies SearchIndexClient;
44 });
45 }),
46 );