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

main
ts 264 lines 8.35 KB
Raw
1 import * as vision from "@distilled.cloud/gcp/vision_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 Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 DEFAULT_PRODUCT_CATEGORY,
12 deleteProduct,
13 getProduct,
14 locationParent,
15 normalizeLocation,
16 parseResourceName,
17 productLabelsOf,
18 productNameOf,
19 replaceOnIdentity,
20 sameProductLabels,
21 sameText,
22 toResourceId,
23 updateMaskOf,
24 waitUntilGone,
25 } from "./internal.ts";
26
27 export type ProductLabel = {
28 /** Label key (max 128 bytes). */
29 key: string;
30 /** Label value (max 128 bytes). */
31 value: string;
32 };
33
34 export type ProductProps = {
35 /**
36 * Product Search location. Immutable — changing it replaces the
37 * product.
38 * @default "us-west1"
39 */
40 location?: string;
41 /**
42 * Product id (the `{product}` segment of
43 * `projects/{project}/locations/{location}/products/{product}`). If
44 * omitted, a unique id is generated. Immutable — changing it replaces
45 * the product. At most 128 characters; cannot contain `/`.
46 */
47 productId?: string;
48 /**
49 * User-facing name (max 4096 characters).
50 * @default the product id
51 */
52 displayName?: string;
53 /**
54 * User-provided metadata (max 4096 characters).
55 */
56 description?: string;
57 /**
58 * Immutable product category. One of `homegoods-v2`, `apparel-v2`,
59 * `toys-v2`, `packagedgoods-v1`, or `general-v1`. Changing it replaces
60 * the product.
61 * @default "homegoods-v2"
62 */
63 productCategory?: string;
64 /**
65 * Search labels used to filter Product Search results.
66 */
67 productLabels?: ProductLabel[];
68 };
69
70 export type Product = Resource<
71 "GCP.Vision.Product",
72 ProductProps,
73 {
74 /** Full resource name `projects/{project}/locations/{location}/products/{product}`. */
75 name: string;
76 /** Product id (last path segment). */
77 productId: string;
78 /** Project id. */
79 project: string;
80 /** Product Search location. */
81 location: string;
82 /** User-facing display name. */
83 displayName: string | undefined;
84 /** User-provided metadata. */
85 description: string | undefined;
86 /** Product category. */
87 productCategory: string | undefined;
88 /** Product search labels. */
89 productLabels: ProductLabel[];
90 },
91 never,
92 Providers
93 >;
94
95 /**
96 * A Cloud Vision Product Search product. Products hold reference images
97 * and can be added to one or more product sets.
98 *
99 * Products have no labels field (`productLabels` are search filters), so
100 * ownership rests on the deterministic product id: a product found without
101 * prior state is reported as unowned and only taken over with `--adopt`.
102 * Location, product id, and `productCategory` are identity — changing any
103 * of them replaces the product. Display name, description, and labels
104 * update in place.
105 *
106 * ### Creating a Product
107 * **Example:** Generated id
108 * ```typescript
109 * const product = yield* GCP.Vision.Product("Shoe", {
110 * displayName: "Trail runner",
111 * productCategory: "apparel-v2",
112 * });
113 * ```
114 *
115 * **Example:** Explicit id and labels
116 * ```typescript
117 * const product = yield* GCP.Vision.Product("Shoe", {
118 * location: "us-west1",
119 * productId: "trail-runner",
120 * displayName: "Trail runner",
121 * productCategory: "apparel-v2",
122 * productLabels: [{ key: "color", value: "blue" }],
123 * });
124 * ```
125 *
126 * ### Updating a Product
127 * **Example:** Rename and add a label
128 * ```typescript
129 * // Same logical id as before; only the changed props differ.
130 * const product = yield* GCP.Vision.Product("Shoe", {
131 * location: "us-west1",
132 * productId: "trail-runner",
133 * displayName: "Trail runner v2",
134 * productCategory: "apparel-v2",
135 * productLabels: [{ key: "color", value: "green" }],
136 * });
137 * ```
138 *
139 * @resource
140 * @category Vision
141 */
142 export const Product = Resource<Product>("GCP.Vision.Product");
143
144 export class ProductNotResolved extends Data.TaggedError("GCP.Vision.ProductNotResolved")<{
145 resourceName: string;
146 }> {}
147
148 const toAttrs = (product: vision.Product, project: string) => {
149 const name = product.name ?? "";
150 const parsed = parseResourceName(name, project, "products");
151 return {
152 name,
153 productId: parsed.id,
154 project: parsed.project || project,
155 location: parsed.location,
156 displayName: product.displayName,
157 description: product.description,
158 productCategory: product.productCategory,
159 productLabels: productLabelsOf(product.productLabels),
160 };
161 };
162
163 export const ProductProvider = () =>
164 Provider.succeed(Product, {
165 stables: ["name", "productId", "project", "location", "productCategory"],
166
167 diff: Effect.fn(function* ({ news, olds, output }) {
168 if (!isResolved(news)) return undefined;
169 const env = yield* GcpEnvironment.current;
170 const previousCategory = olds?.productCategory ?? output?.productCategory;
171 const nextCategory = news.productCategory ?? previousCategory ?? DEFAULT_PRODUCT_CATEGORY;
172 return replaceOnIdentity({
173 previousId: olds?.productId ?? output?.productId,
174 nextId: news.productId,
175 previousParent: locationParent(
176 env.project,
177 normalizeLocation(olds?.location ?? output?.location),
178 ),
179 nextParent: locationParent(
180 env.project,
181 normalizeLocation(news.location ?? output?.location),
182 ),
183 extra: previousCategory !== undefined && previousCategory !== nextCategory,
184 });
185 }),
186
187 read: Effect.fn(function* ({ olds, output }) {
188 const env = yield* GcpEnvironment.current;
189 const location = normalizeLocation(olds?.location ?? output?.location);
190 const name =
191 output?.name ??
192 productNameOf(env.project, location, olds?.productId ?? output?.productId ?? "");
193 const existing = yield* getProduct(name);
194 if (existing === undefined) return undefined;
195 const attrs = toAttrs(existing, env.project);
196 // No labels field: without prior state it may not be ours.
197 return output !== undefined ? attrs : Unowned(attrs);
198 }),
199
200 reconcile: Effect.fn(function* ({ id, news, output }) {
201 const env = yield* GcpEnvironment.current;
202 const location = normalizeLocation(news.location ?? output?.location);
203 const productId = yield* toResourceId(id, news.productId, output?.productId);
204 const displayName = news.displayName ?? productId;
205 const description = news.description;
206 const productCategory =
207 news.productCategory ?? output?.productCategory ?? DEFAULT_PRODUCT_CATEGORY;
208 const productLabels = productLabelsOf(news.productLabels);
209 const name = output?.name ?? productNameOf(env.project, location, productId);
210
211 let current = yield* getProduct(name);
212
213 if (current === undefined) {
214 const created = yield* vision
215 .createProjectsLocationsProducts({
216 parent: locationParent(env.project, location),
217 productId,
218 body: {
219 displayName,
220 description,
221 productCategory,
222 productLabels,
223 },
224 })
225 .pipe(Effect.catchTag("Conflict", () => getProduct(name)));
226 current = created ?? undefined;
227 }
228
229 if (current === undefined) {
230 return yield* new ProductNotResolved({
231 resourceName: name || productNameOf(env.project, location, productId),
232 });
233 }
234
235 const currentName = current.name ?? name;
236 const observedLabels = productLabelsOf(current.productLabels);
237 const updateMask = updateMaskOf(
238 sameText(current.displayName, displayName) ? undefined : "display_name",
239 sameText(current.description, description) ? undefined : "description",
240 sameProductLabels(observedLabels, productLabels) ? undefined : "product_labels",
241 );
242
243 if (updateMask.length > 0) {
244 current = yield* vision.patchProjectsLocationsProducts({
245 name: currentName,
246 updateMask,
247 body: {
248 name: currentName,
249 displayName,
250 description,
251 productLabels,
252 },
253 });
254 }
255
256 return toAttrs(current, env.project);
257 }),
258
259 delete: Effect.fn(function* ({ output }) {
260 if (!output.name) return;
261 yield* deleteProduct(output.name);
262 yield* waitUntilGone(getProduct(output.name));
263 }),
264 });