@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 65 lines 2.74 KB
Raw
1 import type { Credentials } from "@distilled.cloud/cloudflare/Credentials";
2 import * as Effect from "effect/Effect";
3 import type * as HttpClient from "effect/http/HttpClient";
4 import * as Layer from "effect/Layer";
5 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
6 import { SearchIndex } from "./SearchIndex.ts";
7 import { type SearchIndexAuth, makeHttpSearchIndexClient } from "./SearchIndexHttpClient.ts";
8 import type { Index } from "./VectorizeIndex.ts";
9
10 /**
11 * Local implementation of the {@link SearchIndex} binding — talks to a
12 * Vectorize index over the Cloudflare HTTP API using the **current
13 * credentials** instead of a native Worker binding (`SearchIndexBinding`).
14 *
15 * Provide it on an {@link Action} (or any deploy-time Effect) so you can
16 * insert, upsert, query, and fetch vectors with the same client you'd use
17 * inside a Worker — no Worker host, no `host.bind`, no minted token:
18 *
19 * @example Seeding an index from an Action
20 * ```typescript
21 * const Seed = Alchemy.Action(
22 * "Seed",
23 * Effect.gen(function* () {
24 * const vec = yield* Cloudflare.Vectorize.SearchIndex(index);
25 * return Effect.fn(function* () {
26 * yield* vec.upsert([
27 * { id: "1", values: [0.1, 0.2, 0.3, 0.4] },
28 * { id: "2", values: [0.9, 0.8, 0.7, 0.6] },
29 * ]);
30 * const matches = yield* vec.query([0.1, 0.2, 0.3, 0.4], { topK: 1 });
31 * return matches;
32 * });
33 * }).pipe(Effect.provide(Cloudflare.Vectorize.SearchIndexLocal)),
34 * );
35 * ```
36 *
37 * The index name is resolved at apply time through the ambient
38 * {@link RuntimeContext} (in an Action, that's the resolve context the engine
39 * provides around the body), so `SearchIndex(index)` works even though the
40 * index is created in the same deploy.
41 *
42 * `raw` and `queryById` have no Cloudflare HTTP equivalent and `Effect.die` —
43 * see {@link makeHttpSearchIndexClient}.
44 */
45 export const SearchIndexLocal = Layer.effect(
46 SearchIndex,
47 Effect.gen(function* () {
48 // Account + credentials are ambient during stack-eval (the stack's
49 // providers layer). Capture the full context so the HTTP ops run with the
50 // current credentials — no `host.bind`, no minted token.
51 const { accountId } = yield* yield* CloudflareEnvironment;
52 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
53 const auth: SearchIndexAuth = {
54 authorize: (eff) => eff.pipe(Effect.provideContext(context)),
55 accountId,
56 };
57
58 return Effect.fn(function* (index: Index) {
59 // Deferred accessor — resolves the index name against the tracker at
60 // apply time (in an Action, that's the engine's resolve context).
61 const indexName = yield* index.indexName;
62 return makeHttpSearchIndexClient(auth, indexName);
63 });
64 }),
65 );