@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 241 lines 8.3 KB
Raw
1 import * as vectorize from "@distilled.cloud/cloudflare/vectorize";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Stream from "effect/Stream";
7 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
8 import type { Providers } from "../Providers.ts";
9
10 export type MetadataIndexType = "string" | "number" | "boolean";
11
12 export type MetadataIndexProps = {
13 /**
14 * Name of the parent Vectorize index. Pass `index.indexName` from a
15 * `VectorizeIndex` to track the dependency. Changing the parent index
16 * triggers a replacement.
17 */
18 indexName: string;
19 /**
20 * The metadata property to index. Filter expressions in `query` use this
21 * name (e.g. `{ category: { $eq: "books" } }`). Cannot be changed after
22 * creation — updating triggers a replacement.
23 */
24 propertyName: string;
25 /**
26 * The type of metadata values stored under `propertyName`. Cannot be
27 * changed after creation — updating triggers a replacement.
28 */
29 indexType: MetadataIndexType;
30 };
31
32 export type MetadataIndexAttributes = {
33 propertyName: string;
34 indexType: MetadataIndexType;
35 indexName: string;
36 accountId: string;
37 mutationId: string | undefined;
38 };
39
40 export type MetadataIndex = Resource<
41 "Cloudflare.VectorizeMetadataIndex",
42 MetadataIndexProps,
43 MetadataIndexAttributes,
44 never,
45 Providers
46 >;
47
48 /**
49 * A metadata index on a Cloudflare Vectorize index.
50 *
51 * Metadata indexes enable filtering query results by metadata properties.
52 * Without a metadata index on a property, that property cannot be used in
53 * the `filter` of a `query()` call.
54 *
55 * A metadata index is identified by its parent index and `propertyName` and
56 * is immutable — changing the property name, type, or parent index triggers
57 * a replacement.
58 * ### Creating a Metadata Index
59 * **Example:** Index a string metadata property
60 * ```typescript
61 * const index = yield* Cloudflare.Vectorize.Index("my-index", {
62 * dimensions: 768,
63 * metric: "cosine",
64 * });
65 *
66 * yield* Cloudflare.Vectorize.MetadataIndex("CategoryMetaIndex", {
67 * indexName: index.indexName,
68 * propertyName: "category",
69 * indexType: "string",
70 * });
71 * ```
72 *
73 * **Example:** Index a numeric metadata property
74 * ```typescript
75 * yield* Cloudflare.Vectorize.MetadataIndex("PriceMetaIndex", {
76 * indexName: index.indexName,
77 * propertyName: "price",
78 * indexType: "number",
79 * });
80 * ```
81 *
82 * @see https://developers.cloudflare.com/vectorize/reference/metadata-filtering/
83 *
84 * @resource
85 * @product Vectorize
86 * @category AI
87 */
88 export const MetadataIndex = Resource<MetadataIndex>("Cloudflare.VectorizeMetadataIndex");
89
90 export const MetadataIndexProvider = () =>
91 Provider.succeed(MetadataIndex, {
92 stables: ["propertyName", "indexName", "accountId"],
93 diff: Effect.fn(function* ({ olds, news, output }) {
94 const { accountId } = yield* yield* CloudflareEnvironment;
95 if (!isResolved(news)) return undefined;
96 if ((output?.accountId ?? accountId) !== accountId) {
97 return { action: "replace" } as const;
98 }
99 const newIndexName = news.indexName;
100 const oldIndexName = output?.indexName ?? olds.indexName;
101 if (
102 (oldIndexName ?? newIndexName) !== newIndexName ||
103 (olds.propertyName ?? news.propertyName) !== news.propertyName ||
104 (olds.indexType ?? news.indexType) !== news.indexType
105 ) {
106 return { action: "replace" } as const;
107 }
108 return undefined;
109 }),
110 read: Effect.fn(function* ({ output, olds }) {
111 const { accountId } = yield* yield* CloudflareEnvironment;
112 const acct = output?.accountId ?? accountId;
113 const indexName = output?.indexName ?? olds?.indexName;
114 const propertyName = output?.propertyName ?? olds?.propertyName;
115 if (!indexName || !propertyName) return undefined;
116 const existing = yield* findExisting(acct, indexName, propertyName);
117 if (!existing?.propertyName || !existing.indexType) return undefined;
118 return {
119 propertyName: existing.propertyName,
120 indexType: existing.indexType,
121 indexName,
122 accountId: acct,
123 mutationId: output?.mutationId,
124 };
125 }),
126 reconcile: Effect.fn(function* ({ news, output }) {
127 const { accountId } = yield* yield* CloudflareEnvironment;
128 const acct = output?.accountId ?? accountId;
129 const indexName = news.indexName;
130
131 // Observe — list metadata indexes on the parent and look for one
132 // matching propertyName.
133 const existing = yield* findExisting(acct, indexName, news.propertyName);
134
135 // Ensure — create if missing. Cloudflare returns 409 Conflict on
136 // duplicate; tolerate the race by reusing the prior mutationId.
137 let mutationId = output?.mutationId;
138 if (!existing) {
139 const created = yield* vectorize
140 .createIndexMetadataIndex({
141 accountId: acct,
142 indexName,
143 propertyName: news.propertyName,
144 indexType: news.indexType,
145 })
146 .pipe(
147 Effect.catchTag("MetadataIndexAlreadyExists", () =>
148 Effect.succeed({ mutationId: output?.mutationId }),
149 ),
150 );
151 mutationId = created.mutationId ?? undefined;
152 }
153
154 return {
155 propertyName: news.propertyName,
156 indexType: news.indexType,
157 indexName,
158 accountId: acct,
159 mutationId,
160 };
161 }),
162 delete: Effect.fn(function* ({ output }) {
163 yield* vectorize
164 .deleteIndexMetadataIndex({
165 accountId: output.accountId,
166 indexName: output.indexName,
167 propertyName: output.propertyName,
168 })
169 .pipe(Effect.catchTag(["NotFound", "Gone", "MetadataIndexNotFound"], () => Effect.void));
170 }),
171 list: Effect.fn(function* () {
172 const { accountId } = yield* yield* CloudflareEnvironment;
173
174 // Parent fan-out: metadata indexes are sub-resources of a Vectorize
175 // index. Enumerate every parent index (account-scoped, paginated),
176 // then list metadata indexes per index with bounded concurrency.
177 const indexNames = yield* vectorize.listIndexes.pages({ accountId }).pipe(
178 Stream.runCollect,
179 Effect.map((chunk) =>
180 Array.from(chunk).flatMap((page) =>
181 (page.result ?? [])
182 .map((index) => index.name)
183 .filter((name): name is string => name != null),
184 ),
185 ),
186 // A parent index deleted by a concurrent operation mid-enumeration
187 // can fail the account-scoped pagination with "index deleted" (typed
188 // as Gone) — skip the whole enumeration rather than throw.
189 Effect.catchTag(["NotFound", "Gone"], () => Effect.succeed<string[]>([])),
190 );
191
192 const rows = yield* Effect.forEach(
193 indexNames,
194 (indexName) =>
195 vectorize.listIndexMetadataIndexes({ accountId, indexName }).pipe(
196 Effect.map((res) =>
197 (res.metadataIndexes ?? []).flatMap((m): MetadataIndexAttributes[] => {
198 if (m.propertyName == null || m.indexType == null) {
199 return [];
200 }
201 return [
202 {
203 propertyName: m.propertyName,
204 indexType: m.indexType.toLowerCase() as MetadataIndexType,
205 indexName,
206 accountId,
207 mutationId: undefined,
208 },
209 ];
210 }),
211 ),
212 // Parent index removed between enumeration and read; skip it.
213 Effect.catchTag(["NotFound", "Gone"], () =>
214 Effect.succeed<MetadataIndexAttributes[]>([]),
215 ),
216 ),
217 { concurrency: 10 },
218 );
219
220 return rows.flat();
221 }),
222 });
223
224 const findExisting = (acct: string, indexName: string, propertyName: string) =>
225 vectorize
226 .listIndexMetadataIndexes({
227 accountId: acct,
228 indexName,
229 })
230 .pipe(
231 Effect.map((res) => {
232 const index = res.metadataIndexes?.find((m) => m.propertyName === propertyName);
233 return index
234 ? {
235 propertyName: index.propertyName,
236 indexType: index.indexType?.toLowerCase() as MetadataIndexType,
237 }
238 : undefined;
239 }),
240 Effect.catchTag(["NotFound", "Gone"], () => Effect.undefined),
241 );