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

main
ts 391 lines 12.5 KB
Raw
1 import * as retail from "@distilled.cloud/gcp/retail_v2";
2 import { unownedUnlessPrevious } 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 MAX_ID_LENGTH,
12 expandCatalog,
13 normalizeLocation,
14 parseResourceName,
15 replaceOnIdentity,
16 sameStringList,
17 sameText,
18 slugNoDigits,
19 toPhysical,
20 updateMaskOf,
21 } from "./internal.ts";
22
23 export type CatalogsServingConfigProps = {
24 /**
25 * Parent catalog resource name
26 * `projects/{project}/locations/{location}/catalogs/{catalog}` or a
27 * catalog id (combined with `location`). Immutable — changing it
28 * replaces the serving config.
29 * @default "default_catalog"
30 */
31 catalog?: string;
32 /**
33 * Location used when `catalog` is a bare id. Immutable.
34 * @default "global"
35 */
36 location?: string;
37 /**
38 * Serving config id (`[a-z-_]`, 4-63 characters). If omitted, a unique
39 * id is generated. Immutable — changing it replaces the serving config.
40 */
41 servingConfigId?: string;
42 /**
43 * Human-readable name (max 128 characters).
44 */
45 displayName?: string;
46 /**
47 * Solution types. Immutable. Only one type may be set.
48 * @default ["SOLUTION_TYPE_SEARCH"]
49 */
50 solutionTypes?: Array<
51 | "SOLUTION_TYPE_UNSPECIFIED"
52 | "SOLUTION_TYPE_RECOMMENDATION"
53 | "SOLUTION_TYPE_SEARCH"
54 | (string & {})
55 >;
56 /**
57 * Recommendation model id. Required when `solutionTypes` is
58 * `SOLUTION_TYPE_RECOMMENDATION`.
59 */
60 modelId?: string;
61 /**
62 * Filter control ids.
63 */
64 filterControlIds?: string[];
65 /**
66 * Boost control ids.
67 */
68 boostControlIds?: string[];
69 /**
70 * Redirect control ids.
71 */
72 redirectControlIds?: string[];
73 /**
74 * Two-way synonym control ids.
75 */
76 twowaySynonymsControlIds?: string[];
77 /**
78 * One-way synonym control ids.
79 */
80 onewaySynonymsControlIds?: string[];
81 /**
82 * Replacement control ids.
83 */
84 replacementControlIds?: string[];
85 /**
86 * Ignore control ids.
87 */
88 ignoreControlIds?: string[];
89 /**
90 * Do-not-associate control ids.
91 */
92 doNotAssociateControlIds?: string[];
93 /**
94 * Facet control ids.
95 */
96 facetControlIds?: string[];
97 /**
98 * Recommendation diversity level (`no-diversity`, `low-diversity`, …).
99 */
100 diversityLevel?: string;
101 /**
102 * Price reranking level.
103 */
104 priceRerankingLevel?: string;
105 };
106
107 export type CatalogsServingConfig = Resource<
108 "GCP.Retail.CatalogsServingConfig",
109 CatalogsServingConfigProps,
110 {
111 /** Full resource name. */
112 name: string;
113 /** Serving config id. */
114 servingConfigId: string;
115 /** Parent catalog resource name. */
116 catalog: string;
117 /** Project id. */
118 project: string;
119 /** Location id. */
120 location: string;
121 /** Display name. */
122 displayName: string | undefined;
123 /** Solution types. */
124 solutionTypes: string[];
125 /** Recommendation model id. */
126 modelId: string | undefined;
127 /** Filter control ids. */
128 filterControlIds: string[];
129 /** Boost control ids. */
130 boostControlIds: string[];
131 /** Redirect control ids. */
132 redirectControlIds: string[];
133 /** Two-way synonym control ids. */
134 twowaySynonymsControlIds: string[];
135 /** One-way synonym control ids. */
136 onewaySynonymsControlIds: string[];
137 /** Replacement control ids. */
138 replacementControlIds: string[];
139 /** Ignore control ids. */
140 ignoreControlIds: string[];
141 /** Do-not-associate control ids. */
142 doNotAssociateControlIds: string[];
143 /** Facet control ids. */
144 facetControlIds: string[];
145 /** Diversity level. */
146 diversityLevel: string | undefined;
147 /** Price reranking level. */
148 priceRerankingLevel: string | undefined;
149 },
150 never,
151 Providers
152 >;
153
154 /**
155 * A Retail serving config on a catalog.
156 *
157 * Without labels, ownership rests on the deterministic id: `read` reports a
158 * resource it finds without prior state as unowned (adopt it with `--adopt`).
159 * Parent, serving config id, and solution types are immutable. Display name,
160 * model id, and control-id lists update in place.
161 *
162 * ### Creating a Serving Config
163 * **Example:** Extra search serving config
164 * ```typescript
165 * const serving = yield* GCP.Retail.CatalogsServingConfig("Preview", {
166 * displayName: "preview search",
167 * });
168 * ```
169 *
170 * @resource
171 * @category Retail
172 */
173 export const CatalogsServingConfig = Resource<CatalogsServingConfig>(
174 "GCP.Retail.CatalogsServingConfig",
175 );
176
177 export class CatalogsServingConfigNotResolved extends Data.TaggedError(
178 "GCP.Retail.CatalogsServingConfigNotResolved",
179 )<{
180 resourceName: string;
181 }> {}
182
183 const defaultSolutionTypes = ["SOLUTION_TYPE_SEARCH"] as const;
184
185 const toAttrs = (config: retail.GoogleCloudRetailV2ServingConfig, project: string) => {
186 const name = config.name ?? "";
187 const parsed = parseResourceName(name, "servingConfigs");
188 return {
189 name,
190 servingConfigId: parsed.id,
191 catalog: parsed.catalog,
192 project: parsed.project || project,
193 location: parsed.location,
194 displayName: config.displayName,
195 solutionTypes: [...(config.solutionTypes ?? [])],
196 modelId: config.modelId,
197 filterControlIds: [...(config.filterControlIds ?? [])],
198 boostControlIds: [...(config.boostControlIds ?? [])],
199 redirectControlIds: [...(config.redirectControlIds ?? [])],
200 twowaySynonymsControlIds: [...(config.twowaySynonymsControlIds ?? [])],
201 onewaySynonymsControlIds: [...(config.onewaySynonymsControlIds ?? [])],
202 replacementControlIds: [...(config.replacementControlIds ?? [])],
203 ignoreControlIds: [...(config.ignoreControlIds ?? [])],
204 doNotAssociateControlIds: [...(config.doNotAssociateControlIds ?? [])],
205 facetControlIds: [...(config.facetControlIds ?? [])],
206 diversityLevel: config.diversityLevel,
207 priceRerankingLevel: config.priceRerankingLevel,
208 };
209 };
210
211 const resourceName = (catalog: string, servingConfigId: string) =>
212 `${catalog}/servingConfigs/${servingConfigId}`;
213
214 const toBody = (
215 news: CatalogsServingConfigProps,
216 displayName: string,
217 ): retail.GoogleCloudRetailV2ServingConfig => ({
218 displayName,
219 solutionTypes: news.solutionTypes ?? [...defaultSolutionTypes],
220 modelId: news.modelId,
221 filterControlIds: news.filterControlIds,
222 boostControlIds: news.boostControlIds,
223 redirectControlIds: news.redirectControlIds,
224 twowaySynonymsControlIds: news.twowaySynonymsControlIds,
225 onewaySynonymsControlIds: news.onewaySynonymsControlIds,
226 replacementControlIds: news.replacementControlIds,
227 ignoreControlIds: news.ignoreControlIds,
228 doNotAssociateControlIds: news.doNotAssociateControlIds,
229 facetControlIds: news.facetControlIds,
230 diversityLevel: news.diversityLevel,
231 priceRerankingLevel: news.priceRerankingLevel,
232 });
233
234 const getByName = (name: string) =>
235 name.length === 0
236 ? Effect.undefined
237 : retail
238 .getProjectsLocationsCatalogsServingConfigs({ name })
239 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
240
241 export const CatalogsServingConfigProvider = () =>
242 Provider.succeed(CatalogsServingConfig, {
243 stables: ["name", "servingConfigId", "catalog", "project", "location"],
244
245 diff: Effect.fn(function* ({ news, olds, output }) {
246 if (!isResolved(news)) return undefined;
247 const env = yield* GcpEnvironment.current;
248 const previousParent = olds?.catalog ?? output?.catalog;
249 const nextParent = expandCatalog(
250 news.catalog,
251 env.project,
252 normalizeLocation(news.location ?? output?.location),
253 );
254 const previousTypes = olds?.solutionTypes ?? output?.solutionTypes;
255 const nextTypes = news.solutionTypes ?? previousTypes;
256 const identity = replaceOnIdentity({
257 previousId: olds?.servingConfigId ?? output?.servingConfigId,
258 nextId: news.servingConfigId,
259 previousParent,
260 nextParent,
261 });
262 if (
263 identity !== undefined ||
264 (previousTypes !== undefined &&
265 nextTypes !== undefined &&
266 !sameStringList(previousTypes, nextTypes))
267 ) {
268 return (
269 identity ?? {
270 action: "replace" as const,
271 deleteFirst: true,
272 }
273 );
274 }
275 return undefined;
276 }),
277
278 read: Effect.fn(function* ({ id, olds, output, previous }) {
279 const env = yield* GcpEnvironment.current;
280 const catalog = olds?.catalog ?? output?.catalog;
281 const servingConfigId = yield* toPhysical(
282 id,
283 olds?.servingConfigId,
284 output?.servingConfigId,
285 (name) => slugNoDigits(name, MAX_ID_LENGTH),
286 MAX_ID_LENGTH,
287 );
288 const name =
289 output?.name ??
290 (catalog !== undefined
291 ? resourceName(
292 expandCatalog(catalog, env.project, normalizeLocation(output?.location)),
293 servingConfigId,
294 )
295 : undefined);
296 if (name === undefined) return undefined;
297 const existing = yield* getByName(name);
298 if (existing === undefined) return undefined;
299 const attrs = toAttrs(existing, env.project);
300 // No labels field: without prior state it may not be ours.
301 return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "name");
302 }),
303
304 reconcile: Effect.fn(function* ({ id, news, output }) {
305 const env = yield* GcpEnvironment.current;
306 const location = normalizeLocation(news.location ?? output?.location);
307 const catalog = expandCatalog(news.catalog, env.project, location);
308 const servingConfigId = yield* toPhysical(
309 id,
310 news.servingConfigId,
311 output?.servingConfigId,
312 (name) => slugNoDigits(name, MAX_ID_LENGTH),
313 MAX_ID_LENGTH,
314 );
315 const name = resourceName(catalog, servingConfigId);
316 const displayName = news.displayName ?? servingConfigId;
317 const body = toBody(news, displayName);
318
319 let current = yield* getByName(output?.name ?? name);
320
321 if (current === undefined) {
322 const created = yield* retail
323 .createProjectsLocationsCatalogsServingConfigs({
324 parent: catalog,
325 servingConfigId,
326 body,
327 })
328 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
329 current = created ?? undefined;
330 }
331
332 if (current === undefined) {
333 return yield* new CatalogsServingConfigNotResolved({ resourceName: name });
334 }
335
336 const resource = current.name ?? name;
337 const mask = updateMaskOf(
338 (current.displayName ?? "") !== displayName ? "display_name" : undefined,
339 sameText(current.modelId, news.modelId) ? undefined : "model_id",
340 sameStringList(current.filterControlIds, news.filterControlIds)
341 ? undefined
342 : "filter_control_ids",
343 sameStringList(current.boostControlIds, news.boostControlIds)
344 ? undefined
345 : "boost_control_ids",
346 sameStringList(current.redirectControlIds, news.redirectControlIds)
347 ? undefined
348 : "redirect_control_ids",
349 sameStringList(current.twowaySynonymsControlIds, news.twowaySynonymsControlIds)
350 ? undefined
351 : "twoway_synonyms_control_ids",
352 sameStringList(current.onewaySynonymsControlIds, news.onewaySynonymsControlIds)
353 ? undefined
354 : "oneway_synonyms_control_ids",
355 sameStringList(current.replacementControlIds, news.replacementControlIds)
356 ? undefined
357 : "replacement_control_ids",
358 sameStringList(current.ignoreControlIds, news.ignoreControlIds)
359 ? undefined
360 : "ignore_control_ids",
361 sameStringList(current.doNotAssociateControlIds, news.doNotAssociateControlIds)
362 ? undefined
363 : "do_not_associate_control_ids",
364 sameStringList(current.facetControlIds, news.facetControlIds)
365 ? undefined
366 : "facet_control_ids",
367 sameText(current.diversityLevel, news.diversityLevel) ? undefined : "diversity_level",
368 sameText(current.priceRerankingLevel, news.priceRerankingLevel)
369 ? undefined
370 : "price_reranking_level",
371 );
372
373 if (mask.length > 0) {
374 current = yield* retail.patchProjectsLocationsCatalogsServingConfigs({
375 name: resource,
376 updateMask: mask,
377 body: { ...body, name: resource },
378 });
379 }
380
381 return toAttrs(current, env.project);
382 }),
383
384 delete: Effect.fn(function* ({ output }) {
385 yield* retail
386 .deleteProjectsLocationsCatalogsServingConfigs({
387 name: output.name,
388 })
389 .pipe(Effect.catchTag("NotFound", () => Effect.void));
390 }),
391 });