@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, 06:00 UTC

main
ts 329 lines 10.9 KB
Raw
1 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
2 import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 DEFAULT_BRANCH,
12 expandDataStore,
13 fingerprint,
14 MAX_DOCUMENT_ID_LENGTH,
15 parseResourceName,
16 rfc1035,
17 toPhysical,
18 } from "./internal.ts";
19
20 export type DataStoresDocumentContent = {
21 /**
22 * MIME type (`text/plain`, `text/html`, `application/pdf`,
23 * `application/json`, …).
24 */
25 mimeType?: string;
26 /**
27 * Cloud Storage URI (`gs://bucket/path`). Max 2.5 MB for text, 200 MB
28 * otherwise.
29 */
30 uri?: string;
31 /**
32 * Raw bytes as a base64 string. Max 1,000,000 bytes.
33 */
34 rawBytes?: string;
35 };
36
37 export type DataStoresBranchesDocumentProps = {
38 /**
39 * Parent Data Store resource name
40 * `projects/{project}/locations/{location}/dataStores/{dataStore}`.
41 * Immutable — changing it replaces the document.
42 */
43 dataStore: string;
44 /**
45 * Branch id. Use `default_branch` for the default branch.
46 * Immutable — changing it replaces the document.
47 * @default "default_branch"
48 */
49 branch?: string;
50 /**
51 * Document id (RFC-1034, max 128 characters). If omitted, a unique id
52 * is generated. Immutable — changing it replaces the document.
53 */
54 documentId?: string;
55 /**
56 * JSON document payload. Must conform to the data store schema.
57 */
58 jsonData?: string;
59 /**
60 * Structured document payload. Ignored when `jsonData` is set.
61 */
62 structData?: Record<string, unknown>;
63 /**
64 * Unstructured content. Required for `CONTENT_REQUIRED` data stores.
65 */
66 content?: DataStoresDocumentContent;
67 /**
68 * Parent document id for two-level hierarchies (RFC-1034, max 63).
69 */
70 parentDocumentId?: string;
71 /**
72 * Schema id in the same data store.
73 */
74 schemaId?: string;
75 };
76
77 export type DataStoresBranchesDocument = Resource<
78 "GCP.DiscoveryEngine.DataStoresBranchesDocument",
79 DataStoresBranchesDocumentProps,
80 {
81 /** Full resource name `.../branches/{branch}/documents/{document}`. */
82 name: string;
83 /** Document id (last path segment). */
84 documentId: string;
85 /** Parent data store resource name. */
86 dataStore: string;
87 /** Branch id. */
88 branch: string;
89 /** Project id. */
90 project: string;
91 /** Location id. */
92 location: string;
93 /** JSON payload. */
94 jsonData: string | undefined;
95 /** Structured payload, if set. */
96 structData: Record<string, unknown> | undefined;
97 /** Unstructured content, if set. */
98 content: DataStoresDocumentContent | undefined;
99 /** Parent document id, if set. */
100 parentDocumentId: string | undefined;
101 /** Schema id, if set. */
102 schemaId: string | undefined;
103 /** Last index timestamp, if indexed. */
104 indexTime: string | undefined;
105 },
106 never,
107 Providers
108 >;
109
110 /**
111 * A Vertex AI Search Document in a Data Store branch.
112 *
113 * Without labels, ownership rests on the deterministic id: `read` reports a
114 * resource it finds without prior state as unowned (adopt it with `--adopt`).
115 * Parent data store, branch, and document id are immutable; payload and
116 * content update in place.
117 *
118 * ### Creating a Document
119 * **Example:** JSON document on the default branch
120 * ```typescript
121 * const document = yield* GCP.DiscoveryEngine.DataStoresBranchesDocument(
122 * "About",
123 * {
124 * dataStore: dataStore.name,
125 * jsonData: JSON.stringify({
126 * title: "About us",
127 * uri: "https://example.com/about",
128 * }),
129 * },
130 * );
131 * ```
132 *
133 * ### Updating a Document
134 * **Example:** Change the title
135 * ```typescript
136 * // Same logical id as before; only the changed props differ.
137 * const document = yield* GCP.DiscoveryEngine.DataStoresBranchesDocument(
138 * "About",
139 * {
140 * dataStore: dataStore.name,
141 * jsonData: JSON.stringify({
142 * title: "About the team",
143 * uri: "https://example.com/about",
144 * }),
145 * },
146 * );
147 * ```
148 *
149 * @resource
150 * @category DiscoveryEngine
151 */
152 export const DataStoresBranchesDocument = Resource<DataStoresBranchesDocument>(
153 "GCP.DiscoveryEngine.DataStoresBranchesDocument",
154 );
155
156 export class DataStoresBranchesDocumentNotResolved extends Data.TaggedError(
157 "GCP.DiscoveryEngine.DataStoresBranchesDocumentNotResolved",
158 )<{
159 resourceName: string;
160 }> {}
161
162 const branchParent = (dataStore: string, branch: string) => `${dataStore}/branches/${branch}`;
163
164 const resourceName = (dataStore: string, branch: string, documentId: string) =>
165 `${branchParent(dataStore, branch)}/documents/${documentId}`;
166
167 const toAttrs = (
168 document: discoveryengine.GoogleCloudDiscoveryengineV1Document,
169 project: string,
170 ) => {
171 const name = document.name ?? "";
172 const parsed = parseResourceName(name, "documents");
173 const branchParsed = parseResourceName(name, "branches");
174 return {
175 name,
176 documentId: document.id ?? parsed.id,
177 dataStore: parsed.dataStore,
178 branch: branchParsed.id || DEFAULT_BRANCH,
179 project: parsed.project || project,
180 location: parsed.location,
181 jsonData: document.jsonData,
182 structData: document.structData as Record<string, unknown> | undefined,
183 content: document.content
184 ? {
185 mimeType: document.content.mimeType,
186 uri: document.content.uri,
187 rawBytes: document.content.rawBytes,
188 }
189 : undefined,
190 parentDocumentId: document.parentDocumentId,
191 schemaId: document.schemaId,
192 indexTime: document.indexTime,
193 };
194 };
195
196 const getByName = (name: string) =>
197 name.length === 0
198 ? Effect.undefined
199 : discoveryengine
200 .getProjectsLocationsDataStoresBranchesDocuments({ name })
201 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
202
203 export const DataStoresBranchesDocumentProvider = () =>
204 Provider.succeed(DataStoresBranchesDocument, {
205 stables: ["name", "documentId", "dataStore", "branch", "project", "location"],
206
207 diff: Effect.fn(function* ({ news, olds, output }) {
208 if (!isResolved(news)) return undefined;
209 const previousParent = olds?.dataStore ?? output?.dataStore;
210 if (previousParent !== undefined && news.dataStore !== previousParent) {
211 return { action: "replace" as const, deleteFirst: false };
212 }
213 const previousBranch = olds?.branch ?? output?.branch ?? DEFAULT_BRANCH;
214 const nextBranch = news.branch ?? previousBranch;
215 if (previousBranch !== nextBranch) {
216 return { action: "replace" as const, deleteFirst: false };
217 }
218 const previousId = olds?.documentId ?? output?.documentId;
219 if (
220 previousId !== undefined &&
221 news.documentId !== undefined &&
222 news.documentId !== previousId
223 ) {
224 return { action: "replace" as const, deleteFirst: true };
225 }
226 return undefined;
227 }),
228
229 read: Effect.fn(function* ({ id, olds, output, previous }) {
230 const env = yield* GcpEnvironment.current;
231 const documentId = yield* toPhysical(
232 id,
233 olds?.documentId,
234 output?.documentId,
235 (name) => rfc1035(name, MAX_DOCUMENT_ID_LENGTH),
236 MAX_DOCUMENT_ID_LENGTH,
237 );
238 const branch = olds?.branch ?? output?.branch ?? DEFAULT_BRANCH;
239 const parent = olds?.dataStore
240 ? expandDataStore(olds.dataStore, env.project, output?.location ?? "global")
241 : undefined;
242 const name = output?.name ?? (parent ? resourceName(parent, branch, documentId) : "");
243 const existing = yield* getByName(name);
244 if (existing === undefined) return undefined;
245 const attrs = toAttrs(existing, env.project);
246 // No labels field: without prior state it may not be ours.
247 return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "name");
248 }),
249
250 reconcile: Effect.fn(function* ({ id, news, output }) {
251 const env = yield* GcpEnvironment.current;
252 const parent = expandDataStore(news.dataStore, env.project, output?.location ?? "global");
253 const branch = news.branch ?? output?.branch ?? DEFAULT_BRANCH;
254 const documentId = yield* toPhysical(
255 id,
256 news.documentId,
257 output?.documentId,
258 (name) => rfc1035(name, MAX_DOCUMENT_ID_LENGTH),
259 MAX_DOCUMENT_ID_LENGTH,
260 );
261 const name = resourceName(parent, branch, documentId);
262 const jsonData = news.jsonData;
263 const body: discoveryengine.GoogleCloudDiscoveryengineV1Document = {
264 id: documentId,
265 jsonData,
266 structData: news.jsonData === undefined ? news.structData : undefined,
267 content: news.content,
268 parentDocumentId: news.parentDocumentId,
269 schemaId: news.schemaId,
270 };
271
272 let current = yield* getByName(output?.name ?? name);
273
274 if (current === undefined) {
275 const created = yield* discoveryengine
276 .createProjectsLocationsDataStoresBranchesDocuments({
277 parent: branchParent(parent, branch),
278 documentId,
279 body,
280 })
281 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
282 current = created ?? undefined;
283 }
284
285 if (current === undefined) {
286 return yield* new DataStoresBranchesDocumentNotResolved({ resourceName: name });
287 }
288
289 const jsonChanged = fingerprint(current.jsonData) !== fingerprint(jsonData);
290 const structChanged =
291 news.jsonData === undefined &&
292 fingerprint(current.structData) !== fingerprint(news.structData);
293 const contentChanged = fingerprint(current.content) !== fingerprint(news.content);
294 const parentChanged =
295 news.parentDocumentId !== undefined &&
296 (current.parentDocumentId ?? "") !== news.parentDocumentId;
297 const schemaChanged =
298 news.schemaId !== undefined && (current.schemaId ?? "") !== news.schemaId;
299
300 if (jsonChanged || structChanged || contentChanged || parentChanged || schemaChanged) {
301 current = yield* discoveryengine.patchProjectsLocationsDataStoresBranchesDocuments({
302 name: current.name ?? name,
303 allowMissing: true,
304 updateMask: [
305 jsonChanged ? "json_data" : undefined,
306 structChanged ? "struct_data" : undefined,
307 contentChanged ? "content" : undefined,
308 parentChanged ? "parent_document_id" : undefined,
309 schemaChanged ? "schema_id" : undefined,
310 ]
311 .filter((field): field is string => field !== undefined)
312 .join(","),
313 body: { ...body, name: current.name ?? name },
314 });
315 }
316
317 return toAttrs(current, env.project);
318 }),
319
320 delete: Effect.fn(function* ({ output }) {
321 const existing = yield* getByName(output.name);
322 if (existing === undefined) return;
323 yield* discoveryengine
324 .deleteProjectsLocationsDataStoresBranchesDocuments({
325 name: output.name,
326 })
327 .pipe(Effect.catchTag("NotFound", () => Effect.void));
328 }),
329 });