The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as vision from "@distilled.cloud/gcp/vision_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | |
| 8 | // Product Search only exists in PRODUCT_SEARCH_LOCATIONS, so the stack region is not a usable default. |
| 9 | export const DEFAULT_LOCATION = "us-west1"; |
| 10 | export const DEFAULT_PRODUCT_CATEGORY = "homegoods-v2"; |
| 11 | const MAX_ID_LENGTH = 128; |
| 12 | |
| 13 | const lastSegment = (value: string) => { |
| 14 | const trimmed = value.replace(/\/+$/, ""); |
| 15 | const parts = trimmed.split("/"); |
| 16 | return parts[parts.length - 1] || trimmed; |
| 17 | }; |
| 18 | |
| 19 | const parentOf = (name: string) => { |
| 20 | const parts = name.split("/").filter((part) => part.length > 0); |
| 21 | return parts.slice(0, -2).join("/"); |
| 22 | }; |
| 23 | |
| 24 | export const normalizeLocation = (location: string | undefined) => |
| 25 | lastSegment(location ?? DEFAULT_LOCATION).toLowerCase(); |
| 26 | |
| 27 | export const locationParent = (project: string, location: string) => |
| 28 | `projects/${project}/locations/${location}`; |
| 29 | |
| 30 | export const productSetNameOf = (project: string, location: string, productSetId: string) => { |
| 31 | if (productSetId.length === 0) return ""; |
| 32 | if (productSetId.includes("/productSets/")) { |
| 33 | return productSetId.replace(/\/+$/, ""); |
| 34 | } |
| 35 | return `${locationParent(project, location)}/productSets/${lastSegment(productSetId)}`; |
| 36 | }; |
| 37 | |
| 38 | export const productNameOf = (project: string, location: string, productId: string) => { |
| 39 | if (productId.length === 0) return ""; |
| 40 | if (productId.includes("/products/")) { |
| 41 | return productId.replace(/\/+$/, ""); |
| 42 | } |
| 43 | return `${locationParent(project, location)}/products/${lastSegment(productId)}`; |
| 44 | }; |
| 45 | |
| 46 | export const referenceImageNameOf = (parent: string, imageId: string) => { |
| 47 | if (imageId.length === 0) return ""; |
| 48 | if (imageId.includes("/referenceImages/")) { |
| 49 | return imageId.replace(/\/+$/, ""); |
| 50 | } |
| 51 | return `${parent}/referenceImages/${lastSegment(imageId)}`; |
| 52 | }; |
| 53 | |
| 54 | const expandProductName = (project: string, location: string, value: string) => |
| 55 | productNameOf(project, location, value); |
| 56 | |
| 57 | export const parseResourceName = (name: string, fallbackProject: string, collection: string) => { |
| 58 | const parts = name.split("/").filter((part) => part.length > 0); |
| 59 | const collectionAt = parts.lastIndexOf(collection); |
| 60 | const locationsAt = parts.lastIndexOf("locations"); |
| 61 | const projectsAt = parts.lastIndexOf("projects"); |
| 62 | return { |
| 63 | name, |
| 64 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : fallbackProject, |
| 65 | location: |
| 66 | locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION, |
| 67 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 68 | parent: collectionAt >= 0 ? parts.slice(0, collectionAt).join("/") : parentOf(name), |
| 69 | }; |
| 70 | }; |
| 71 | |
| 72 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 73 | (left ?? "") === (right ?? ""); |
| 74 | |
| 75 | export const jsonEqual = (left: unknown, right: unknown) => |
| 76 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 77 | |
| 78 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 79 | fields.filter((field): field is string => field !== undefined).join(","); |
| 80 | |
| 81 | export const replaceOnIdentity = (input: { |
| 82 | previousId?: string; |
| 83 | nextId?: string; |
| 84 | previousParent?: string; |
| 85 | nextParent?: string; |
| 86 | extra?: boolean; |
| 87 | }) => { |
| 88 | if (input.extra === true) { |
| 89 | return { action: "replace" as const, deleteFirst: false }; |
| 90 | } |
| 91 | if ( |
| 92 | input.previousId !== undefined && |
| 93 | input.nextId !== undefined && |
| 94 | input.previousId !== input.nextId |
| 95 | ) { |
| 96 | return { action: "replace" as const, deleteFirst: false }; |
| 97 | } |
| 98 | if ( |
| 99 | input.previousParent !== undefined && |
| 100 | input.nextParent !== undefined && |
| 101 | input.previousParent !== input.nextParent |
| 102 | ) { |
| 103 | return { action: "replace" as const, deleteFirst: false }; |
| 104 | } |
| 105 | return undefined; |
| 106 | }; |
| 107 | |
| 108 | export const toResourceId = ( |
| 109 | id: string, |
| 110 | requested: string | undefined, |
| 111 | existing: string | undefined, |
| 112 | maxLength = MAX_ID_LENGTH, |
| 113 | ) => |
| 114 | Effect.gen(function* () { |
| 115 | if (requested !== undefined && requested.length > 0) { |
| 116 | return lastSegment(requested).slice(0, maxLength); |
| 117 | } |
| 118 | if (existing !== undefined && existing.length > 0) { |
| 119 | return lastSegment(existing).slice(0, maxLength); |
| 120 | } |
| 121 | const generated = yield* createPhysicalName({ |
| 122 | id, |
| 123 | maxLength, |
| 124 | lowercase: true, |
| 125 | }); |
| 126 | const next = /^[a-z]/.test(generated) ? generated : `v${generated}`.slice(0, maxLength); |
| 127 | return next.length >= 4 ? next : `${next}xxxx`.slice(0, maxLength); |
| 128 | }); |
| 129 | |
| 130 | const emptyList = <A>() => Effect.succeed([] as A[]); |
| 131 | |
| 132 | const catchMissing = <A, E extends { readonly _tag: string }, R>(effect: Effect.Effect<A, E, R>) => |
| 133 | effect.pipe( |
| 134 | Effect.catchIf( |
| 135 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 136 | () => Effect.undefined, |
| 137 | ), |
| 138 | ); |
| 139 | |
| 140 | const ignoreMissing = <E extends { readonly _tag: string }, R>( |
| 141 | effect: Effect.Effect<unknown, E, R>, |
| 142 | ) => |
| 143 | effect.pipe( |
| 144 | Effect.catchIf( |
| 145 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 146 | () => Effect.void, |
| 147 | ), |
| 148 | ); |
| 149 | |
| 150 | export class DeleteNotConfirmed extends Data.TaggedError("GCP.Vision.DeleteNotConfirmed")<{}> {} |
| 151 | |
| 152 | /** Poll until the resource is gone; fails if it is still readable after ~60s. */ |
| 153 | export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>) => |
| 154 | get.pipe( |
| 155 | Effect.repeat({ |
| 156 | schedule: Schedule.spaced("2 seconds"), |
| 157 | until: (value) => value === undefined, |
| 158 | times: 30, |
| 159 | }), |
| 160 | Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> => |
| 161 | value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()), |
| 162 | ), |
| 163 | ); |
| 164 | |
| 165 | const collectPages = <Page, Item, E extends { readonly _tag: string }, R>( |
| 166 | pages: Stream.Stream<Page, E, R>, |
| 167 | items: (page: Page) => readonly Item[] | null | undefined, |
| 168 | ) => |
| 169 | pages.pipe( |
| 170 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 171 | Stream.runCollect, |
| 172 | Effect.map((chunk) => Array.from(chunk) as Item[]), |
| 173 | Effect.catchIf( |
| 174 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 175 | () => emptyList<Item>(), |
| 176 | ), |
| 177 | ); |
| 178 | |
| 179 | export const getProductSet = (name: string) => |
| 180 | name.length === 0 |
| 181 | ? Effect.undefined |
| 182 | : catchMissing(vision.getProjectsLocationsProductSets({ name })); |
| 183 | |
| 184 | export const getProduct = (name: string) => |
| 185 | name.length === 0 |
| 186 | ? Effect.undefined |
| 187 | : catchMissing(vision.getProjectsLocationsProducts({ name })); |
| 188 | |
| 189 | export const getReferenceImage = (name: string) => |
| 190 | name.length === 0 |
| 191 | ? Effect.undefined |
| 192 | : catchMissing(vision.getProjectsLocationsProductsReferenceImages({ name })); |
| 193 | |
| 194 | export const listProductsInSet = (name: string) => |
| 195 | name.length === 0 |
| 196 | ? emptyList<vision.Product>() |
| 197 | : collectPages( |
| 198 | vision.listProjectsLocationsProductSetsProducts.pages({ |
| 199 | name, |
| 200 | pageSize: 100, |
| 201 | }), |
| 202 | (page) => page.products, |
| 203 | ); |
| 204 | |
| 205 | export const productLabelsOf = ( |
| 206 | labels: ReadonlyArray<{ key?: string; value?: string }> | undefined, |
| 207 | ): Array<{ key: string; value: string }> => |
| 208 | (labels ?? []).flatMap((item) => (item.key ? [{ key: item.key, value: item.value ?? "" }] : [])); |
| 209 | |
| 210 | export const sameProductLabels = ( |
| 211 | left: ReadonlyArray<{ key: string; value: string }> | undefined, |
| 212 | right: ReadonlyArray<{ key: string; value: string }> | undefined, |
| 213 | ) => |
| 214 | jsonEqual( |
| 215 | [...(left ?? [])].map((item) => `${item.key}=${item.value}`).sort(), |
| 216 | [...(right ?? [])].map((item) => `${item.key}=${item.value}`).sort(), |
| 217 | ); |
| 218 | |
| 219 | export const deleteProductSet = (name: string) => |
| 220 | name.length === 0 |
| 221 | ? Effect.void |
| 222 | : ignoreMissing(vision.deleteProjectsLocationsProductSets({ name })); |
| 223 | |
| 224 | export const deleteProduct = (name: string) => |
| 225 | name.length === 0 ? Effect.void : ignoreMissing(vision.deleteProjectsLocationsProducts({ name })); |
| 226 | |
| 227 | export const deleteReferenceImage = (name: string) => |
| 228 | name.length === 0 |
| 229 | ? Effect.void |
| 230 | : ignoreMissing(vision.deleteProjectsLocationsProductsReferenceImages({ name })); |
| 231 | |
| 232 | export const syncProductSetMembership = ( |
| 233 | setName: string, |
| 234 | project: string, |
| 235 | location: string, |
| 236 | desired: readonly string[] | undefined, |
| 237 | ) => |
| 238 | Effect.gen(function* () { |
| 239 | if (desired === undefined) return; |
| 240 | const wanted = new Set( |
| 241 | desired |
| 242 | .filter((value) => value.length > 0) |
| 243 | .map((value) => expandProductName(project, location, value)), |
| 244 | ); |
| 245 | const observed = yield* listProductsInSet(setName); |
| 246 | const present = new Set( |
| 247 | observed |
| 248 | .map((product) => product.name) |
| 249 | .filter((name): name is string => typeof name === "string"), |
| 250 | ); |
| 251 | for (const product of wanted) { |
| 252 | if (present.has(product)) continue; |
| 253 | yield* vision |
| 254 | .addProductProjectsLocationsProductSets({ |
| 255 | name: setName, |
| 256 | body: { product }, |
| 257 | }) |
| 258 | .pipe(Effect.catchTag("Conflict", () => Effect.void)); |
| 259 | } |
| 260 | for (const product of present) { |
| 261 | if (wanted.has(product)) continue; |
| 262 | yield* vision |
| 263 | .removeProductProjectsLocationsProductSets({ |
| 264 | name: setName, |
| 265 | body: { product }, |
| 266 | }) |
| 267 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 268 | } |
| 269 | }); |