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

main
ts 270 lines 8.73 KB
Raw
1 import * as datalabeling from "@distilled.cloud/gcp/datalabeling_v1beta1";
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 Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 encodeOwnership,
13 findOwned,
14 hasOwnershipMarker,
15 ignoreGone,
16 listAnnotationSpecSets,
17 noRetryLayer,
18 ownedByAlchemy,
19 parseOwnership,
20 parseResourceName,
21 projectParent,
22 replaceOnIdentity,
23 retryDelete,
24 retryTransient,
25 sameJson,
26 sameText,
27 toDisplayName,
28 waitUntilGone,
29 } from "./internal.ts";
30
31 export type AnnotationSpec = {
32 /**
33 * Display name of this spec (the class label). Maximum 64 characters.
34 */
35 displayName: string;
36 /**
37 * Optional description of this spec. Maximum 10,000 characters.
38 */
39 description?: string;
40 };
41
42 export type AnnotationSpecSetProps = {
43 /**
44 * Annotation spec set id (the last segment of
45 * `projects/{project}/annotationSpecSets/{annotation_spec_set}`).
46 * Server-assigned on create. Immutable — changing it replaces the set.
47 */
48 annotationSpecSetId?: string;
49 /**
50 * Display name. Maximum 64 characters. Required by the API; Alchemy
51 * falls back to a generated name. Immutable — changing it replaces
52 * the set.
53 */
54 displayName?: string;
55 /**
56 * Human-readable description. Annotation spec sets have no labels
57 * field, so Alchemy stamps ownership into this field. Immutable —
58 * changing it replaces the set.
59 */
60 description?: string;
61 /**
62 * The possible labels for a labeling task. Required. Immutable —
63 * changing them replaces the set.
64 */
65 annotationSpecs: AnnotationSpec[];
66 };
67
68 export type AnnotationSpecSet = Resource<
69 "GCP.DataLabeling.AnnotationSpecSet",
70 AnnotationSpecSetProps,
71 {
72 /** Full resource name `projects/{project}/annotationSpecSets/{id}`. */
73 name: string;
74 /** Annotation spec set id (last path segment). */
75 annotationSpecSetId: string;
76 /** Project id. */
77 project: string;
78 /** Display name. */
79 displayName: string | undefined;
80 /** User description with the Alchemy ownership prefix stripped. */
81 description: string | undefined;
82 /** Annotation specs (class labels). */
83 annotationSpecs: AnnotationSpec[];
84 /** Related resources blocking changes. */
85 blockingResources: string[] | undefined;
86 },
87 never,
88 Providers
89 >;
90
91 /**
92 * A Data Labeling annotation spec set — a collection of class labels
93 * used by labeling tasks.
94 *
95 * Spec sets are immutable after create. There is no labels API, so
96 * Alchemy stamps ownership into `description` so `list` / nuke can find
97 * them. Ids are server-assigned.
98 *
99 * ### Creating an Annotation Spec Set
100 * **Example:** Image classes
101 * ```typescript
102 * const specs = yield* GCP.DataLabeling.AnnotationSpecSet("Classes", {
103 * displayName: "pets",
104 * annotationSpecs: [
105 * { displayName: "dog" },
106 * { displayName: "cat" },
107 * ],
108 * });
109 * ```
110 *
111 * **Example:** Specs with descriptions
112 * ```typescript
113 * const specs = yield* GCP.DataLabeling.AnnotationSpecSet("Classes", {
114 * displayName: "sentiment",
115 * description: "review polarity",
116 * annotationSpecs: [
117 * { displayName: "positive", description: "favorable" },
118 * { displayName: "negative", description: "unfavorable" },
119 * ],
120 * });
121 * ```
122 *
123 * @resource
124 * @category DataLabeling
125 */
126 export const AnnotationSpecSet = Resource<AnnotationSpecSet>("GCP.DataLabeling.AnnotationSpecSet");
127
128 export class AnnotationSpecSetNotResolved extends Data.TaggedError(
129 "GCP.DataLabeling.AnnotationSpecSetNotResolved",
130 )<{
131 resourceName: string;
132 }> {}
133
134 const resourceName = (project: string, annotationSpecSetId: string) =>
135 `${projectParent(project)}/annotationSpecSets/${annotationSpecSetId}`;
136
137 const specsOf = (
138 specs:
139 | readonly datalabeling.GoogleCloudDatalabelingV1beta1AnnotationSpec[]
140 | readonly AnnotationSpec[]
141 | undefined,
142 ): AnnotationSpec[] =>
143 (specs ?? []).map((spec) => ({
144 displayName: spec.displayName ?? "",
145 description: spec.description,
146 }));
147
148 const toAttrs = (
149 set: datalabeling.GoogleCloudDatalabelingV1beta1AnnotationSpecSet,
150 project: string,
151 ) => {
152 const name = set.name ?? "";
153 const parsed = parseResourceName(name, "annotationSpecSets");
154 return {
155 name,
156 annotationSpecSetId: parsed.id,
157 project: parsed.project || project,
158 displayName: set.displayName,
159 description: parseOwnership(set.description).text,
160 annotationSpecs: specsOf(set.annotationSpecs),
161 blockingResources: set.blockingResources,
162 };
163 };
164
165 const getByName = (name: string) =>
166 name.length === 0
167 ? Effect.undefined
168 : datalabeling.getProjectsAnnotationSpecSets({ name }).pipe(
169 Effect.provide(noRetryLayer),
170 Effect.catchTags({
171 NotFound: () => Effect.undefined,
172 BadGateway: () => Effect.undefined,
173 }),
174 );
175
176 const findByOwnership = (id: string, project: string) =>
177 Effect.gen(function* () {
178 const rows = yield* listAnnotationSpecSets(projectParent(project));
179 return yield* findOwned(id, rows, (row) => row.description);
180 });
181
182 export const AnnotationSpecSetProvider = () =>
183 Provider.succeed(AnnotationSpecSet, {
184 stables: ["name", "annotationSpecSetId", "project"],
185
186 diff: Effect.fn(function* ({ news, olds, output }) {
187 if (!isResolved(news)) return undefined;
188 const extra =
189 (news.displayName !== undefined &&
190 output?.displayName !== undefined &&
191 !sameText(news.displayName, output.displayName)) ||
192 (olds !== undefined && !sameText(news.description, output?.description)) ||
193 (output !== undefined && !sameJson(specsOf(news.annotationSpecs), output.annotationSpecs));
194 return replaceOnIdentity({
195 previousId: olds?.annotationSpecSetId ?? output?.annotationSpecSetId,
196 nextId: news.annotationSpecSetId,
197 extra,
198 });
199 }),
200
201 read: Effect.fn(function* ({ id, olds, output }) {
202 const env = yield* GcpEnvironment.current;
203 const annotationSpecSetId =
204 olds?.annotationSpecSetId ??
205 output?.annotationSpecSetId ??
206 (output?.name ? parseResourceName(output.name, "annotationSpecSets").id : "");
207 const name =
208 output?.name ??
209 (annotationSpecSetId.length > 0 ? resourceName(env.project, annotationSpecSetId) : "");
210 const existing = (yield* getByName(name)) ?? (yield* findByOwnership(id, env.project));
211 if (existing === undefined) return undefined;
212 const attrs = toAttrs(existing, env.project);
213 return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs);
214 }),
215
216 list: () =>
217 Effect.gen(function* () {
218 const env = yield* GcpEnvironment.current;
219 const rows = yield* listAnnotationSpecSets(projectParent(env.project));
220 return rows
221 .filter((row) => hasOwnershipMarker(row.description))
222 .map((row) => toAttrs(row, env.project));
223 }),
224
225 reconcile: Effect.fn(function* ({ id, news, output }) {
226 const env = yield* GcpEnvironment.current;
227 const annotationSpecSetId = news.annotationSpecSetId ?? output?.annotationSpecSetId;
228 const name =
229 output?.name ??
230 (annotationSpecSetId !== undefined ? resourceName(env.project, annotationSpecSetId) : "");
231 const ownership = yield* createInternalLabels(id);
232 const displayName = yield* toDisplayName(id, news.displayName, output?.displayName);
233 const description = encodeOwnership(ownership, news.description);
234 const annotationSpecs = specsOf(news.annotationSpecs);
235
236 let current = (yield* getByName(name)) ?? (yield* findByOwnership(id, env.project));
237
238 if (current === undefined) {
239 const created = yield* retryTransient(
240 datalabeling.createProjectsAnnotationSpecSets({
241 parent: projectParent(env.project),
242 body: {
243 annotationSpecSet: {
244 displayName,
245 description,
246 annotationSpecs,
247 },
248 },
249 }),
250 ).pipe(Effect.catchTag("Conflict", () => findByOwnership(id, env.project)));
251 current = created ?? undefined;
252 }
253
254 if (current === undefined) {
255 return yield* new AnnotationSpecSetNotResolved({
256 resourceName: name || projectParent(env.project),
257 });
258 }
259
260 return toAttrs(current, env.project);
261 }),
262
263 delete: Effect.fn(function* ({ output }) {
264 if (!output.name) return;
265 yield* datalabeling
266 .deleteProjectsAnnotationSpecSets({ name: output.name })
267 .pipe(retryDelete, ignoreGone);
268 yield* waitUntilGone(getByName(output.name));
269 }),
270 });