@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 423 lines 14.3 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 * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 DEFAULT_LOCATION,
13 DEFAULT_PROCESSOR_TYPE,
14 MAX_PROCESSOR_DISPLAY_NAME_LENGTH,
15 ProcessorFailed,
16 ProcessorPending,
17 ResourceNotResolved,
18 encodeOwnershipLine,
19 findOwnedByDisplayName,
20 hasOwnershipMarker,
21 listProcessorsAt,
22 listProjectProcessors,
23 locationParent,
24 normalizeLocation,
25 ownedByAlchemy,
26 parseOwnership,
27 parseResourceName,
28 replaceOnIdentity,
29 retryTransient,
30 sameText,
31 toPhysicalId,
32 waitUntilGone,
33 } from "./internal.ts";
34 import { waitForOperation } from "./operations.ts";
35
36 export type ProcessorProps = {
37 /**
38 * Processor id (the `{processor}` segment of
39 * `projects/{project}/locations/{location}/processors/{processor}`).
40 * Assigned by the API on create. Immutable — changing it replaces the
41 * processor. Supply it to adopt an existing processor.
42 */
43 processorId?: string;
44 /**
45 * Multi-region location (`us` or `eu`). Some processor types are also
46 * available in regional locations. Immutable — changing it replaces the
47 * processor.
48 * @default "us"
49 */
50 location?: string;
51 /**
52 * Processor type such as `OCR_PROCESSOR` or `FORM_PARSER_PROCESSOR`.
53 * Immutable — changing it replaces the processor.
54 * @default "OCR_PROCESSOR"
55 */
56 type?: string;
57 /**
58 * User-facing display name. Processors have no labels field, so Alchemy
59 * stamps ownership into this field for list / nuke. There is no update
60 * API for display name — changing it replaces the processor.
61 */
62 displayName?: string;
63 /**
64 * Cloud KMS key used for CMEK. Immutable — changing it replaces the
65 * processor.
66 */
67 kmsKeyName?: string;
68 /**
69 * Default processor version resource name. Updates in place via
70 * `setDefaultProcessorVersion`.
71 */
72 defaultProcessorVersion?: string;
73 /**
74 * Schema version resource name used by the processor. Immutable —
75 * changing it replaces the processor.
76 */
77 activeSchemaVersion?: string;
78 /**
79 * Whether the processor is enabled. Created processors start enabled.
80 * Updates in place via enable / disable.
81 * @default true
82 */
83 enabled?: boolean;
84 };
85
86 export type Processor = Resource<
87 "GCP.DocumentAI.Processor",
88 ProcessorProps,
89 {
90 /** Full resource name `projects/{project}/locations/{location}/processors/{processor}`. */
91 name: string;
92 /** Processor id (last path segment). */
93 processorId: string;
94 /** Project id. */
95 project: string;
96 /** Location id. */
97 location: string;
98 /** Processor type. */
99 type: string | undefined;
100 /** User display name with the Alchemy ownership prefix stripped. */
101 displayName: string | undefined;
102 /** Processor state (`ENABLED`, `DISABLED`, …). */
103 state: string | undefined;
104 /** Default processor version resource name. */
105 defaultProcessorVersion: string | undefined;
106 /** HTTP endpoint that invokes processing. */
107 processEndpoint: string | undefined;
108 /** Cloud KMS key used for CMEK, if any. */
109 kmsKeyName: string | undefined;
110 /** Schema version resource name used by the processor. */
111 activeSchemaVersion: string | undefined;
112 /** RFC3339 creation timestamp. */
113 createTime: string | undefined;
114 },
115 never,
116 Providers
117 >;
118
119 /**
120 * A Document AI processor that extracts structure from documents.
121 *
122 * Processors are location-scoped. The API assigns the processor id. Type,
123 * location, KMS key, and display name are immutable (there is no processor
124 * patch RPC). Enable / disable and the default processor version update in
125 * place. Processors have no labels, so Alchemy stamps ownership into
126 * `displayName` for `list` / nuke.
127 *
128 * ### Creating a Processor
129 * **Example:** OCR processor
130 * ```typescript
131 * const processor = yield* GCP.DocumentAI.Processor("Ocr", {
132 * type: "OCR_PROCESSOR",
133 * displayName: "ocr",
134 * });
135 * ```
136 *
137 * **Example:** Explicit location
138 * ```typescript
139 * const processor = yield* GCP.DocumentAI.Processor("Ocr", {
140 * location: "us",
141 * type: "OCR_PROCESSOR",
142 * });
143 * ```
144 *
145 * ### Updating a Processor
146 * **Example:** Disable
147 * ```typescript
148 * // Same logical id, changed props: the engine updates it in place.
149 * const processor = yield* GCP.DocumentAI.Processor("Ocr", {
150 * location: "us",
151 * type: "OCR_PROCESSOR",
152 * enabled: false,
153 * });
154 * ```
155 *
156 * @resource
157 * @category DocumentAI
158 */
159 export const Processor = Resource<Processor>("GCP.DocumentAI.Processor");
160
161 const resourceName = (project: string, location: string, processorId: string) =>
162 `${locationParent(project, location)}/processors/${processorId}`;
163
164 const typeOf = (value: string | undefined) => value ?? DEFAULT_PROCESSOR_TYPE;
165
166 const toAttrs = (processor: documentai.GoogleCloudDocumentaiV1Processor, project: string) => {
167 const name = processor.name ?? "";
168 const parsed = parseResourceName(name, "processors");
169 const ownership = parseOwnership(processor.displayName);
170 return {
171 name,
172 processorId: parsed.id,
173 project: parsed.project || project,
174 location: parsed.location,
175 type: processor.type,
176 displayName: ownership.text,
177 state: processor.state,
178 defaultProcessorVersion: processor.defaultProcessorVersion,
179 processEndpoint: processor.processEndpoint,
180 kmsKeyName: processor.kmsKeyName,
181 activeSchemaVersion: processor.activeSchemaVersion,
182 createTime: processor.createTime,
183 };
184 };
185
186 const getByName = (name: string) =>
187 name.length === 0
188 ? Effect.undefined
189 : documentai
190 .getProjectsLocationsProcessors({ name })
191 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
192
193 const findOwned = (id: string, project: string, parent: string, hinted?: string) =>
194 Effect.gen(function* () {
195 if (hinted !== undefined && hinted.length > 0) {
196 const existing = yield* getByName(hinted);
197 if (existing !== undefined) return existing;
198 }
199 const local = yield* findOwnedByDisplayName(id, yield* listProcessorsAt(parent));
200 if (local !== undefined) return local;
201 return yield* findOwnedByDisplayName(id, yield* listProjectProcessors(project));
202 });
203
204 const waitUntilReady = (name: string) =>
205 getByName(name).pipe(
206 Effect.filterOrFail(
207 (processor): processor is documentai.GoogleCloudDocumentaiV1Processor =>
208 processor !== undefined,
209 () => new ResourceNotResolved({ resourceName: name }),
210 ),
211 Effect.filterOrFail(
212 (processor) => processor.state !== "FAILED",
213 (processor) =>
214 new ProcessorFailed({
215 resourceName: name,
216 state: processor.state ?? "STATE_UNSPECIFIED",
217 }),
218 ),
219 Effect.filterOrFail(
220 (processor) => processor.state === "ENABLED" || processor.state === "DISABLED",
221 (processor) =>
222 new ProcessorPending({
223 resourceName: name,
224 state: processor.state ?? "STATE_UNSPECIFIED",
225 }),
226 ),
227 Effect.retry({
228 while: (error) => error instanceof ProcessorPending || error instanceof ResourceNotResolved,
229 times: 10,
230 schedule: Schedule.spaced("2 seconds"),
231 }),
232 );
233
234 const waitUntilState = (name: string, desired: "ENABLED" | "DISABLED") =>
235 getByName(name).pipe(
236 Effect.filterOrFail(
237 (processor): processor is documentai.GoogleCloudDocumentaiV1Processor =>
238 processor !== undefined,
239 () => new ResourceNotResolved({ resourceName: name }),
240 ),
241 Effect.filterOrFail(
242 (processor) => processor.state !== "FAILED",
243 (processor) =>
244 new ProcessorFailed({
245 resourceName: name,
246 state: processor.state ?? "STATE_UNSPECIFIED",
247 }),
248 ),
249 Effect.filterOrFail(
250 (processor) => processor.state === desired,
251 (processor) =>
252 new ProcessorPending({
253 resourceName: name,
254 state: processor.state ?? "STATE_UNSPECIFIED",
255 }),
256 ),
257 Effect.retry({
258 while: (error) => error instanceof ProcessorPending || error instanceof ResourceNotResolved,
259 times: 10,
260 schedule: Schedule.spaced("2 seconds"),
261 }),
262 );
263
264 export const ProcessorProvider = () =>
265 Provider.succeed(Processor, {
266 stables: ["name", "processorId", "project", "location", "type", "createTime"],
267
268 diff: Effect.fn(function* ({ news, olds, output }) {
269 if (!isResolved(news)) return undefined;
270 const previousType = olds?.type ?? output?.type;
271 const nextType = typeOf(news.type);
272 const previousKms = olds?.kmsKeyName ?? output?.kmsKeyName;
273 const previousSchema = olds?.activeSchemaVersion ?? output?.activeSchemaVersion;
274 const previousDisplay = olds?.displayName ?? output?.displayName;
275 const extra =
276 (previousType !== undefined && previousType !== nextType) ||
277 (previousKms ?? "") !== (news.kmsKeyName ?? "") ||
278 (previousSchema ?? "") !== (news.activeSchemaVersion ?? "") ||
279 (news.displayName !== undefined &&
280 previousDisplay !== undefined &&
281 news.displayName !== previousDisplay);
282 return replaceOnIdentity({
283 previousId: olds?.processorId ?? output?.processorId,
284 nextId: news.processorId,
285 previousLocation: olds?.location ?? output?.location,
286 nextLocation: news.location ?? olds?.location ?? output?.location,
287 extra,
288 });
289 }),
290
291 read: Effect.fn(function* ({ id, olds, output }) {
292 const env = yield* GcpEnvironment.current;
293 const location = normalizeLocation(olds?.location ?? output?.location);
294 const processorId = olds?.processorId ?? output?.processorId;
295 const name =
296 output?.name ?? (processorId ? resourceName(env.project, location, processorId) : "");
297 const parent = locationParent(env.project, location);
298 const existing = yield* findOwned(id, env.project, parent, name);
299 if (existing === undefined) return undefined;
300 const attrs = toAttrs(existing, env.project);
301 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
302 }),
303
304 list: () =>
305 Effect.gen(function* () {
306 const env = yield* GcpEnvironment.current;
307 const processors = yield* listProjectProcessors(env.project);
308 return processors
309 .filter((processor) => hasOwnershipMarker(processor.displayName))
310 .map((processor) => toAttrs(processor, env.project));
311 }),
312
313 reconcile: Effect.fn(function* ({ id, news, output }) {
314 const env = yield* GcpEnvironment.current;
315 const location = normalizeLocation(news.location ?? output?.location ?? DEFAULT_LOCATION);
316 const parent = locationParent(env.project, location);
317 const ownership = yield* createInternalLabels(id);
318 const fallbackName = yield* toPhysicalId(id, undefined, output?.displayName);
319 const displayName = encodeOwnershipLine(
320 ownership,
321 news.displayName ?? output?.displayName ?? fallbackName,
322 MAX_PROCESSOR_DISPLAY_NAME_LENGTH,
323 );
324 const type = typeOf(news.type);
325 const hinted =
326 output?.name ??
327 (news.processorId ? resourceName(env.project, location, news.processorId) : "");
328
329 let current = yield* findOwned(id, env.project, parent, hinted);
330
331 if (current === undefined) {
332 current = yield* retryTransient(
333 documentai.createProjectsLocationsProcessors({
334 parent,
335 body: {
336 type,
337 displayName,
338 kmsKeyName: news.kmsKeyName,
339 defaultProcessorVersion: news.defaultProcessorVersion,
340 activeSchemaVersion: news.activeSchemaVersion,
341 },
342 }),
343 ).pipe(
344 Effect.catchTag("Conflict", (error) =>
345 findOwned(id, env.project, parent, hinted).pipe(
346 Effect.flatMap((found) =>
347 found !== undefined ? Effect.succeed(found) : Effect.fail(error),
348 ),
349 ),
350 ),
351 );
352 }
353
354 if (current === undefined) {
355 return yield* new ResourceNotResolved({
356 resourceName: hinted || `${parent}/processors`,
357 });
358 }
359
360 let live = current;
361 const currentName = live.name ?? hinted;
362 if (live.state !== "ENABLED" && live.state !== "DISABLED") {
363 live = yield* waitUntilReady(currentName);
364 }
365
366 const wantEnabled = news.enabled !== false;
367 if (wantEnabled && live.state === "DISABLED") {
368 const operation = yield* retryTransient(
369 documentai.enableProjectsLocationsProcessors({
370 name: currentName,
371 body: {},
372 }),
373 );
374 yield* waitForOperation(operation);
375 live = yield* waitUntilState(currentName, "ENABLED");
376 } else if (!wantEnabled && live.state === "ENABLED") {
377 const operation = yield* retryTransient(
378 documentai.disableProjectsLocationsProcessors({
379 name: currentName,
380 body: {},
381 }),
382 );
383 yield* waitForOperation(operation);
384 live = yield* waitUntilState(currentName, "DISABLED");
385 }
386
387 if (
388 news.defaultProcessorVersion !== undefined &&
389 !sameText(live.defaultProcessorVersion, news.defaultProcessorVersion)
390 ) {
391 const operation = yield* retryTransient(
392 documentai.setDefaultProcessorVersionProjectsLocationsProcessors({
393 processor: currentName,
394 body: {
395 defaultProcessorVersion: news.defaultProcessorVersion,
396 },
397 }),
398 );
399 yield* waitForOperation(operation);
400 live = (yield* getByName(currentName)) ?? live;
401 }
402
403 return toAttrs(live, env.project);
404 }),
405
406 delete: Effect.fn(function* ({ output }) {
407 if (!output.name) return;
408 const operation = yield* retryTransient(
409 documentai.deleteProjectsLocationsProcessors({
410 name: output.name,
411 }),
412 ).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
413 if (operation !== undefined) {
414 // NOT_FOUND (5): already gone.
415 yield* waitForOperation(operation).pipe(
416 Effect.catchTag("GCP.OperationFailed", (error) =>
417 error.code === 5 ? Effect.void : Effect.fail(error),
418 ),
419 );
420 }
421 yield* waitUntilGone(getByName(output.name), output.name);
422 }),
423 });