The best way to control Claude Code, Codex and more from any computer or phone
| 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 | listDatasets, |
| 17 | noRetryLayer, |
| 18 | ownedByAlchemy, |
| 19 | parseOwnership, |
| 20 | parseResourceName, |
| 21 | projectParent, |
| 22 | replaceOnIdentity, |
| 23 | retryDelete, |
| 24 | retryTransient, |
| 25 | sameText, |
| 26 | toDisplayName, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | export type DatasetProps = { |
| 31 | /** |
| 32 | * Dataset id (the `{dataset}` segment of |
| 33 | * `projects/{project}/datasets/{dataset}`). Server-assigned on create. |
| 34 | * Immutable — changing it replaces the dataset. |
| 35 | */ |
| 36 | datasetId?: string; |
| 37 | /** |
| 38 | * Display name. Maximum 64 characters. Required by the API; Alchemy |
| 39 | * falls back to a generated name. Immutable — changing it replaces |
| 40 | * the dataset. |
| 41 | */ |
| 42 | displayName?: string; |
| 43 | /** |
| 44 | * Human-readable description. Datasets have no labels field, so |
| 45 | * Alchemy stamps ownership into this field. Immutable — changing it |
| 46 | * replaces the dataset. |
| 47 | */ |
| 48 | description?: string; |
| 49 | }; |
| 50 | |
| 51 | export type Dataset = Resource< |
| 52 | "GCP.DataLabeling.Dataset", |
| 53 | DatasetProps, |
| 54 | { |
| 55 | /** Full resource name `projects/{project}/datasets/{dataset}`. */ |
| 56 | name: string; |
| 57 | /** Dataset id (last path segment). */ |
| 58 | datasetId: string; |
| 59 | /** Project id. */ |
| 60 | project: string; |
| 61 | /** Display name. */ |
| 62 | displayName: string | undefined; |
| 63 | /** User description with the Alchemy ownership prefix stripped. */ |
| 64 | description: string | undefined; |
| 65 | /** Number of data items in the dataset. */ |
| 66 | dataItemCount: string | undefined; |
| 67 | /** Original import configs, if data has been imported. */ |
| 68 | inputConfigs: datalabeling.GoogleCloudDatalabelingV1beta1InputConfigList | undefined; |
| 69 | /** RFC3339 creation timestamp. */ |
| 70 | createTime: string | undefined; |
| 71 | /** Last migration time to Vertex AI, if any. */ |
| 72 | lastMigrateTime: string | undefined; |
| 73 | /** Related resources blocking changes. */ |
| 74 | blockingResources: string[] | undefined; |
| 75 | }, |
| 76 | never, |
| 77 | Providers |
| 78 | >; |
| 79 | |
| 80 | /** |
| 81 | * A Data Labeling dataset — a container for data items and annotated |
| 82 | * datasets produced by labeling tasks. |
| 83 | * |
| 84 | * Dataset ids are server-assigned. Display name and description are |
| 85 | * immutable after create. There is no labels API, so Alchemy stamps |
| 86 | * ownership into `description` so `list` / nuke can find them. |
| 87 | * |
| 88 | * ### Creating a Dataset |
| 89 | * **Example:** Generated display name |
| 90 | * ```typescript |
| 91 | * const dataset = yield* GCP.DataLabeling.Dataset("Images", {}); |
| 92 | * ``` |
| 93 | * |
| 94 | * **Example:** Named dataset with a description |
| 95 | * ```typescript |
| 96 | * const dataset = yield* GCP.DataLabeling.Dataset("Images", { |
| 97 | * displayName: "product-photos", |
| 98 | * description: "sku images for classification", |
| 99 | * }); |
| 100 | * ``` |
| 101 | * |
| 102 | * @resource |
| 103 | * @category DataLabeling |
| 104 | */ |
| 105 | export const Dataset = Resource<Dataset>("GCP.DataLabeling.Dataset"); |
| 106 | |
| 107 | export class DatasetNotResolved extends Data.TaggedError("GCP.DataLabeling.DatasetNotResolved")<{ |
| 108 | resourceName: string; |
| 109 | }> {} |
| 110 | |
| 111 | const resourceName = (project: string, datasetId: string) => |
| 112 | `${projectParent(project)}/datasets/${datasetId}`; |
| 113 | |
| 114 | const toAttrs = (dataset: datalabeling.GoogleCloudDatalabelingV1beta1Dataset, project: string) => { |
| 115 | const name = dataset.name ?? ""; |
| 116 | const parsed = parseResourceName(name, "datasets"); |
| 117 | return { |
| 118 | name, |
| 119 | datasetId: parsed.id, |
| 120 | project: parsed.project || project, |
| 121 | displayName: dataset.displayName, |
| 122 | description: parseOwnership(dataset.description).text, |
| 123 | dataItemCount: dataset.dataItemCount, |
| 124 | inputConfigs: dataset.inputConfigs, |
| 125 | createTime: dataset.createTime, |
| 126 | lastMigrateTime: dataset.lastMigrateTime, |
| 127 | blockingResources: dataset.blockingResources, |
| 128 | }; |
| 129 | }; |
| 130 | |
| 131 | const getByName = (name: string) => |
| 132 | name.length === 0 |
| 133 | ? Effect.undefined |
| 134 | : datalabeling.getProjectsDatasets({ name }).pipe( |
| 135 | Effect.provide(noRetryLayer), |
| 136 | Effect.catchTags({ |
| 137 | NotFound: () => Effect.undefined, |
| 138 | BadGateway: () => Effect.undefined, |
| 139 | }), |
| 140 | ); |
| 141 | |
| 142 | const findByOwnership = (id: string, project: string) => |
| 143 | Effect.gen(function* () { |
| 144 | const rows = yield* listDatasets(projectParent(project)); |
| 145 | return yield* findOwned(id, rows, (row) => row.description); |
| 146 | }); |
| 147 | |
| 148 | export const DatasetProvider = () => |
| 149 | Provider.succeed(Dataset, { |
| 150 | stables: ["name", "datasetId", "project", "createTime"], |
| 151 | |
| 152 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 153 | if (!isResolved(news)) return undefined; |
| 154 | const extra = |
| 155 | (news.displayName !== undefined && |
| 156 | output?.displayName !== undefined && |
| 157 | !sameText(news.displayName, output.displayName)) || |
| 158 | (olds !== undefined && !sameText(news.description, output?.description)); |
| 159 | return replaceOnIdentity({ |
| 160 | previousId: olds?.datasetId ?? output?.datasetId, |
| 161 | nextId: news.datasetId, |
| 162 | extra, |
| 163 | }); |
| 164 | }), |
| 165 | |
| 166 | read: Effect.fn(function* ({ id, olds, output }) { |
| 167 | const env = yield* GcpEnvironment.current; |
| 168 | const datasetId = |
| 169 | olds?.datasetId ?? |
| 170 | output?.datasetId ?? |
| 171 | (output?.name ? parseResourceName(output.name, "datasets").id : ""); |
| 172 | const name = |
| 173 | output?.name ?? (datasetId.length > 0 ? resourceName(env.project, datasetId) : ""); |
| 174 | const existing = (yield* getByName(name)) ?? (yield* findByOwnership(id, env.project)); |
| 175 | if (existing === undefined) return undefined; |
| 176 | const attrs = toAttrs(existing, env.project); |
| 177 | return (yield* ownedByAlchemy(id, existing.description)) ? attrs : Unowned(attrs); |
| 178 | }), |
| 179 | |
| 180 | list: () => |
| 181 | Effect.gen(function* () { |
| 182 | const env = yield* GcpEnvironment.current; |
| 183 | const rows = yield* listDatasets(projectParent(env.project)); |
| 184 | return rows |
| 185 | .filter((row) => hasOwnershipMarker(row.description)) |
| 186 | .map((row) => toAttrs(row, env.project)); |
| 187 | }), |
| 188 | |
| 189 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 190 | const env = yield* GcpEnvironment.current; |
| 191 | const datasetId = news.datasetId ?? output?.datasetId; |
| 192 | const name = |
| 193 | output?.name ?? (datasetId !== undefined ? resourceName(env.project, datasetId) : ""); |
| 194 | const ownership = yield* createInternalLabels(id); |
| 195 | const displayName = yield* toDisplayName(id, news.displayName, output?.displayName); |
| 196 | const description = encodeOwnership(ownership, news.description); |
| 197 | |
| 198 | let current = (yield* getByName(name)) ?? (yield* findByOwnership(id, env.project)); |
| 199 | |
| 200 | if (current === undefined) { |
| 201 | const created = yield* retryTransient( |
| 202 | datalabeling.createProjectsDatasets({ |
| 203 | parent: projectParent(env.project), |
| 204 | body: { |
| 205 | dataset: { |
| 206 | displayName, |
| 207 | description, |
| 208 | }, |
| 209 | }, |
| 210 | }), |
| 211 | ).pipe(Effect.catchTag("Conflict", () => findByOwnership(id, env.project))); |
| 212 | current = created ?? undefined; |
| 213 | } |
| 214 | |
| 215 | if (current === undefined) { |
| 216 | return yield* new DatasetNotResolved({ |
| 217 | resourceName: name || projectParent(env.project), |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | return toAttrs(current, env.project); |
| 222 | }), |
| 223 | |
| 224 | delete: Effect.fn(function* ({ output }) { |
| 225 | if (!output.name) return; |
| 226 | yield* datalabeling |
| 227 | .deleteProjectsDatasets({ name: output.name }) |
| 228 | .pipe(retryDelete, ignoreGone); |
| 229 | yield* waitUntilGone(getByName(output.name)); |
| 230 | }), |
| 231 | }); |