The best way to control Claude Code, Codex and more from any computer or phone

main
ts 322 lines 10.7 KB
Raw
1 import * as documentai from "@distilled.cloud/gcp/documentai_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { tagRecord } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 DEFAULT_LOCATION,
13 ResourceNotResolved,
14 expandParent,
15 findOwnedByLabels,
16 hasAlchemyLabelMap,
17 listProjectSchemas,
18 listSchemaVersionsAt,
19 normalizeLocation,
20 parseResourceName,
21 replaceOnIdentity,
22 retryTransient,
23 sameJson,
24 sameText,
25 toPhysicalId,
26 updateMaskOf,
27 userLabels,
28 waitUntilGone,
29 } from "./internal.ts";
30 import { waitForOperation } from "./operations.ts";
31
32 export type DocumentSchemaSpec = documentai.GoogleCloudDocumentaiV1DocumentSchema;
33
34 export type SchemasSchemaVersionProps = {
35 /**
36 * Parent schema. Full name
37 * `projects/{project}/locations/{location}/schemas/{schema}` or the
38 * schema id (combined with `location`). Immutable — changing it
39 * replaces the schema version.
40 */
41 schema: string;
42 /**
43 * Region used when `schema` is a bare id.
44 * @default "us"
45 */
46 location?: string;
47 /**
48 * Schema version id (the `{schemaVersion}` segment). Assigned by the
49 * API on create. Immutable — changing it replaces the version. Supply
50 * it to adopt an existing version.
51 */
52 schemaVersionId?: string;
53 /**
54 * User-defined display name. Defaults to a generated id.
55 */
56 displayName?: string;
57 /**
58 * Document schema describing extraction output. Immutable — changing it
59 * replaces the schema version.
60 */
61 documentSchema: DocumentSchemaSpec;
62 /**
63 * User labels. Alchemy ownership labels are merged in automatically.
64 */
65 labels?: Record<string, string>;
66 };
67
68 export type SchemasSchemaVersion = Resource<
69 "GCP.DocumentAI.SchemasSchemaVersion",
70 SchemasSchemaVersionProps,
71 {
72 /** Full resource name `.../schemas/{schema}/schemaVersions/{schemaVersion}`. */
73 name: string;
74 /** Schema version id. */
75 schemaVersionId: string;
76 /** Parent schema resource name. */
77 schema: string;
78 /** Project id. */
79 project: string;
80 /** Location id. */
81 location: string;
82 /** User-defined display name. */
83 displayName: string | undefined;
84 /** Document schema describing extraction output. */
85 documentSchema: DocumentSchemaSpec | undefined;
86 /** User labels (Alchemy ownership labels stripped). */
87 labels: Record<string, string>;
88 /** RFC3339 creation timestamp. */
89 createTime: string | undefined;
90 },
91 never,
92 Providers
93 >;
94
95 /**
96 * A Document AI schema version — an immutable snapshot of a document
97 * schema under a Schema.
98 *
99 * Parent schema, version id, and the document schema body are immutable.
100 * Display name and labels update in place.
101 *
102 * ### Creating a Schema Version
103 * **Example:** Invoice schema version
104 * ```typescript
105 * const version = yield* GCP.DocumentAI.SchemasSchemaVersion("V1", {
106 * schema: schema.name,
107 * displayName: "v1",
108 * documentSchema: {
109 * displayName: "invoice",
110 * entityTypes: [
111 * {
112 * name: "invoice",
113 * baseTypes: ["document"],
114 * properties: [
115 * {
116 * name: "invoice_id",
117 * valueType: "string",
118 * occurrenceType: "OPTIONAL_ONCE",
119 * },
120 * ],
121 * },
122 * ],
123 * },
124 * });
125 * ```
126 *
127 * @resource
128 * @category DocumentAI
129 */
130 export const SchemasSchemaVersion = Resource<SchemasSchemaVersion>(
131 "GCP.DocumentAI.SchemasSchemaVersion",
132 );
133
134 const schemaOf = (schema: string, project: string, location: string) =>
135 expandParent(schema, project, location, "schemas");
136
137 const resourceName = (schema: string, schemaVersionId: string) =>
138 `${schema}/schemaVersions/${schemaVersionId}`;
139
140 const toAttrs = (version: documentai.GoogleCloudDocumentaiV1SchemaVersion, project: string) => {
141 const name = version.name ?? "";
142 const parsed = parseResourceName(name, "schemaVersions");
143 return {
144 name,
145 schemaVersionId: parsed.id,
146 schema: parsed.parent,
147 project: parsed.project || project,
148 location: parsed.location,
149 displayName: version.displayName,
150 documentSchema: version.schema,
151 labels: userLabels(version.labels),
152 createTime: version.createTime,
153 };
154 };
155
156 const getByName = (name: string) =>
157 name.length === 0
158 ? Effect.undefined
159 : documentai
160 .getProjectsLocationsSchemasSchemaVersions({ name })
161 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
162
163 const listOwnedVersions = (project: string) =>
164 Effect.gen(function* () {
165 const schemas = yield* listProjectSchemas(project);
166 const groups = yield* Effect.forEach(
167 schemas,
168 (schema) => listSchemaVersionsAt(schema.name ?? ""),
169 { concurrency: 2 },
170 );
171 return groups.flat();
172 });
173
174 const findOwned = (id: string, project: string, parent: string, hinted?: string) =>
175 Effect.gen(function* () {
176 if (hinted !== undefined && hinted.length > 0) {
177 const existing = yield* getByName(hinted);
178 if (existing !== undefined) return existing;
179 }
180 const local = yield* findOwnedByLabels(id, yield* listSchemaVersionsAt(parent));
181 if (local !== undefined) return local;
182 return yield* findOwnedByLabels(id, yield* listOwnedVersions(project));
183 });
184
185 export const SchemasSchemaVersionProvider = () =>
186 Provider.succeed(SchemasSchemaVersion, {
187 stables: ["name", "schemaVersionId", "schema", "project", "location", "createTime"],
188
189 diff: Effect.fn(function* ({ news, olds, output }) {
190 if (!isResolved(news)) return undefined;
191 const env = yield* GcpEnvironment.current;
192 const location = normalizeLocation(news.location ?? output?.location ?? DEFAULT_LOCATION);
193 const nextParent = schemaOf(news.schema, env.project, location);
194 const previousParent = olds?.schema ?? output?.schema;
195 const previousSchema = olds?.documentSchema ?? output?.documentSchema;
196 const extra =
197 (previousParent !== undefined && previousParent !== nextParent) ||
198 (previousSchema !== undefined && !sameJson(previousSchema, news.documentSchema));
199 return replaceOnIdentity({
200 previousId: olds?.schemaVersionId ?? output?.schemaVersionId,
201 nextId: news.schemaVersionId,
202 previousLocation: olds?.location ?? output?.location,
203 nextLocation: news.location ?? olds?.location ?? output?.location,
204 extra,
205 });
206 }),
207
208 read: Effect.fn(function* ({ id, olds, output }) {
209 const env = yield* GcpEnvironment.current;
210 const location = normalizeLocation(olds?.location ?? output?.location);
211 const schemaVersionId = olds?.schemaVersionId ?? output?.schemaVersionId;
212 const schema =
213 olds?.schema !== undefined
214 ? schemaOf(olds.schema, env.project, location)
215 : (output?.schema ?? "");
216 const name =
217 output?.name ??
218 (schema.length > 0 && schemaVersionId ? resourceName(schema, schemaVersionId) : "");
219 const existing = yield* findOwned(id, env.project, schema, name);
220 if (existing === undefined) return undefined;
221 const attrs = toAttrs(existing, env.project);
222 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
223 }),
224
225 list: () =>
226 Effect.gen(function* () {
227 const env = yield* GcpEnvironment.current;
228 const versions = yield* listOwnedVersions(env.project);
229 return versions
230 .filter((version) => hasAlchemyLabelMap(version.labels))
231 .map((version) => toAttrs(version, env.project));
232 }),
233
234 reconcile: Effect.fn(function* ({ id, news, output }) {
235 const env = yield* GcpEnvironment.current;
236 const location = normalizeLocation(news.location ?? output?.location ?? DEFAULT_LOCATION);
237 const schema = schemaOf(news.schema, env.project, location);
238 const desiredLabels = {
239 ...toLabels(news.labels),
240 ...(yield* createInternalLabels(id)),
241 };
242 const fallbackName = yield* toPhysicalId(
243 id,
244 undefined,
245 output?.displayName ?? output?.schemaVersionId,
246 );
247 const displayName = news.displayName ?? fallbackName;
248 const hinted =
249 output?.name ?? (news.schemaVersionId ? resourceName(schema, news.schemaVersionId) : "");
250
251 let current = yield* findOwned(id, env.project, schema, hinted);
252
253 if (current === undefined) {
254 current = yield* retryTransient(
255 documentai.createProjectsLocationsSchemasSchemaVersions({
256 parent: schema,
257 body: {
258 displayName,
259 labels: desiredLabels,
260 schema: news.documentSchema,
261 },
262 }),
263 ).pipe(
264 Effect.catchTag("Conflict", (error) =>
265 findOwned(id, env.project, schema, hinted).pipe(
266 Effect.flatMap((found) =>
267 found !== undefined ? Effect.succeed(found) : Effect.fail(error),
268 ),
269 ),
270 ),
271 );
272 }
273
274 if (current === undefined) {
275 return yield* new ResourceNotResolved({
276 resourceName: hinted || `${schema}/schemaVersions`,
277 });
278 }
279
280 const currentName = current.name ?? hinted;
281 const observedLabels = tagRecord(current.labels);
282 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
283 const labelsChanged = upsert.length > 0 || removed.length > 0;
284 const displayChanged = !sameText(current.displayName, displayName);
285
286 if (labelsChanged || displayChanged) {
287 current = yield* retryTransient(
288 documentai.patchProjectsLocationsSchemasSchemaVersions({
289 name: currentName,
290 updateMask: updateMaskOf(
291 displayChanged ? "displayName" : undefined,
292 labelsChanged ? "labels" : undefined,
293 ),
294 body: {
295 displayName,
296 labels: desiredLabels,
297 },
298 }),
299 );
300 }
301
302 return toAttrs(current, env.project);
303 }),
304
305 delete: Effect.fn(function* ({ output }) {
306 if (!output.name) return;
307 const operation = yield* retryTransient(
308 documentai.deleteProjectsLocationsSchemasSchemaVersions({
309 name: output.name,
310 }),
311 ).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
312 if (operation !== undefined) {
313 // NOT_FOUND (5): already gone.
314 yield* waitForOperation(operation).pipe(
315 Effect.catchTag("GCP.OperationFailed", (error) =>
316 error.code === 5 ? Effect.void : Effect.fail(error),
317 ),
318 );
319 }
320 yield* waitUntilGone(getByName(output.name), output.name);
321 }),
322 });