@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 126 lines 4.43 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as Binding from "alchemy/Binding";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import type { SearchInstance } from "./SearchInstance.ts";
7
8 /**
9 * Bind a {@link SearchInstance} to a Worker and obtain the Effect-native
10 * AI Search client (`search`, `chatCompletions`, `info`, `stats`). The
11 * single-instance `ai_search` binding resolves directly to a runtime
12 * `SearchInstance`.
13 *
14 * `QuerySearch` is a single identifier that is simultaneously the binding's Context
15 * tag, its type, and the callable — `yield* Cloudflare.AI.QuerySearch(instance)`.
16 *
17 * Provide {@link QuerySearchBinding} in the Worker's runtime layer.
18 *
19 *
20 * ### Querying AI Search
21 * **Example:** Retrieve and generate from a Worker
22 * Bind the instance during the Worker's init phase, then use `search`
23 * (retrieval only) or `chatCompletions` (retrieval + generation) from request
24 * handlers.
25 * ```typescript
26 * const search = yield* Cloudflare.AI.QuerySearch(instance);
27 *
28 * return {
29 * fetch: Effect.gen(function* () {
30 * const answer = yield* search.chatCompletions({
31 * messages: [{ role: "user", content: "How do I deploy?" }],
32 * });
33 * return yield* HttpServerResponse.json(answer);
34 * }),
35 * };
36 * ```
37 *
38 * @binding
39 * @category AI
40 */
41 export interface QuerySearch extends Binding.Service<
42 QuerySearch,
43 "Cloudflare.AI.QuerySearch",
44 (instance: SearchInstance) => Effect.Effect<QuerySearchClient>
45 > {}
46
47 export const QuerySearch = Binding.Service<QuerySearch>("Cloudflare.AI.QuerySearch");
48
49 /**
50 * Error raised by AI Search runtime binding operations.
51 */
52 export class SearchError extends Data.TaggedError("AiSearchError")<{
53 /**
54 * Human-readable runtime error message.
55 */
56 message: string;
57 /**
58 * Original error thrown by the Cloudflare.AI. Search runtime binding.
59 */
60 cause: unknown;
61 }> {}
62
63 /**
64 * Effect-native client for a Cloudflare.AI. Search Worker binding.
65 *
66 * Wraps the standalone `SearchInstance` runtime binding (the binding the
67 * `ai_search` type resolves to — the deprecated `AutoRAG` shape is gone) so
68 * each operation returns an Effect tagged with {@link SearchError}. Obtain
69 * one from `Cloudflare.AI.QuerySearch(instance)` (or a namespace client's
70 * `.get(instanceName)`) during the Worker's init phase.
71 */
72 export interface QuerySearchClient {
73 /**
74 * Effect resolving to the raw underlying Cloudflare `SearchInstance`
75 * binding. Use this for operations not surfaced below (`items`, `jobs`,
76 * `update`, or streaming `chatCompletions`).
77 */
78 raw: Effect.Effect<runtime.AiSearchInstance, never, RuntimeContext>;
79 /**
80 * Retrieve the chunks most relevant to a query without generation.
81 */
82 search(
83 params: runtime.AiSearchSearchRequest,
84 ): Effect.Effect<runtime.AiSearchSearchResponse, SearchError, RuntimeContext>;
85 /**
86 * Run retrieval-augmented generation: retrieve relevant chunks and answer
87 * with the configured model (non-streaming).
88 */
89 chatCompletions(
90 params: runtime.AiSearchChatCompletionsRequest,
91 ): Effect.Effect<runtime.AiSearchChatCompletionsResponse, SearchError, RuntimeContext>;
92 /**
93 * Metadata about this instance (id, models, source, status, …).
94 */
95 info(): Effect.Effect<runtime.AiSearchInstanceInfo, SearchError, RuntimeContext>;
96 /**
97 * Indexing statistics (item counts per status, last activity, engine).
98 */
99 stats(): Effect.Effect<runtime.AiSearchStatsResponse, SearchError, RuntimeContext>;
100 }
101
102 export const tryAiSearch = <A>(fn: () => Promise<A>): Effect.Effect<A, SearchError> =>
103 Effect.tryPromise({
104 try: fn,
105 catch: (cause) =>
106 new SearchError({
107 message: cause instanceof Error ? cause.message : "Unknown AI Search runtime error",
108 cause,
109 }),
110 });
111
112 /**
113 * Build a {@link QuerySearchClient} from an Effect that lazily resolves the raw
114 * `SearchInstance` runtime binding.
115 */
116 export const makeClient = (rawEff: Effect.Effect<runtime.AiSearchInstance>): QuerySearchClient => {
117 const use = <A>(fn: (raw: runtime.AiSearchInstance) => Promise<A>) =>
118 Effect.flatMap(rawEff, (raw) => tryAiSearch(() => fn(raw)));
119 return {
120 raw: rawEff,
121 search: (params) => use((raw) => raw.search(params)),
122 chatCompletions: (params) => use((raw) => raw.chatCompletions(params)),
123 info: () => use((raw) => raw.info()),
124 stats: () => use((raw) => raw.stats()),
125 } satisfies QuerySearchClient;
126 };