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

main
ts 336 lines 11 KB
Raw
1 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_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 parentOf,
12 parseResourceName,
13 sameStringList,
14 servingConfigIdOf,
15 toPhysical,
16 } from "./internal.ts";
17
18 export type CollectionsDataStoresServingConfigProps = {
19 /**
20 * Parent data store resource name. Immutable — changing it replaces
21 * the serving config.
22 */
23 dataStore: string;
24 /**
25 * Serving config id (4-63 characters, `[a-zA-Z0-9_]`). If omitted, a
26 * unique id is generated. Immutable — changing it replaces the serving
27 * config.
28 */
29 servingConfigId?: string;
30 /**
31 * Human-readable name (max 128 characters).
32 */
33 displayName?: string;
34 /**
35 * Solution type. Immutable.
36 * @default "SOLUTION_TYPE_SEARCH"
37 */
38 solutionType?:
39 | "SOLUTION_TYPE_UNSPECIFIED"
40 | "SOLUTION_TYPE_RECOMMENDATION"
41 | "SOLUTION_TYPE_SEARCH"
42 | "SOLUTION_TYPE_CHAT"
43 | "SOLUTION_TYPE_GENERATIVE_CHAT"
44 | "SOLUTION_TYPE_AI_MODE"
45 | (string & {});
46 /**
47 * Filter control ids applied at serving time.
48 */
49 filterControlIds?: string[];
50 /**
51 * Boost control ids applied at serving time.
52 */
53 boostControlIds?: string[];
54 /**
55 * Redirect control ids.
56 */
57 redirectControlIds?: string[];
58 /**
59 * Synonyms control ids.
60 */
61 synonymsControlIds?: string[];
62 /**
63 * Ranking expression, e.g. `0.5 * relevance_score`.
64 */
65 rankingExpression?: string;
66 /**
67 * Recommendation diversity level.
68 */
69 diversityLevel?: string;
70 /**
71 * Recommendation model id. Required for `SOLUTION_TYPE_RECOMMENDATION`.
72 */
73 modelId?: string;
74 };
75
76 export type CollectionsDataStoresServingConfig = Resource<
77 "GCP.DiscoveryEngine.CollectionsDataStoresServingConfig",
78 CollectionsDataStoresServingConfigProps,
79 {
80 /** Full resource name. */
81 name: string;
82 /** Serving config id. */
83 servingConfigId: string;
84 /** Parent data store resource name. */
85 dataStore: string;
86 /** Project id. */
87 project: string;
88 /** Location id. */
89 location: string;
90 /** Display name. */
91 displayName: string | undefined;
92 /** Solution type. */
93 solutionType: string | undefined;
94 /** Filter control ids. */
95 filterControlIds: string[];
96 /** Boost control ids. */
97 boostControlIds: string[];
98 /** Redirect control ids. */
99 redirectControlIds: string[];
100 /** Synonyms control ids. */
101 synonymsControlIds: string[];
102 /** Ranking expression. */
103 rankingExpression: string | undefined;
104 /** Diversity level. */
105 diversityLevel: string | undefined;
106 /** Model id. */
107 modelId: string | undefined;
108 /** RFC3339 creation timestamp. */
109 createTime: string | undefined;
110 /** RFC3339 last-update timestamp. */
111 updateTime: string | undefined;
112 },
113 never,
114 Providers
115 >;
116
117 /**
118 * A Discovery Engine serving config on a collection data store.
119 *
120 * Without labels, ownership rests on the deterministic id: `read` reports a
121 * resource it finds without prior state as unowned (adopt it with `--adopt`).
122 * Parent, serving config id, and solution type are immutable. Display name
123 * and control-id lists update in place.
124 *
125 * ### Creating a Serving Config
126 * **Example:** Extra search serving config
127 * ```typescript
128 * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", {});
129 * const serving = yield* GCP.DiscoveryEngine.CollectionsDataStoresServingConfig(
130 * "Preview",
131 * {
132 * dataStore: store.name,
133 * displayName: "preview search",
134 * },
135 * );
136 * ```
137 *
138 * @resource
139 * @category DiscoveryEngine
140 */
141 export const CollectionsDataStoresServingConfig = Resource<CollectionsDataStoresServingConfig>(
142 "GCP.DiscoveryEngine.CollectionsDataStoresServingConfig",
143 );
144
145 export class CollectionsDataStoresServingConfigNotResolved extends Data.TaggedError(
146 "GCP.DiscoveryEngine.CollectionsDataStoresServingConfigNotResolved",
147 )<{
148 resourceName: string;
149 }> {}
150
151 const toAttrs = (
152 config: discoveryengine.GoogleCloudDiscoveryengineV1ServingConfig,
153 project: string,
154 ) => {
155 const name = config.name ?? "";
156 const parsed = parseResourceName(name, "servingConfigs");
157 return {
158 name,
159 servingConfigId: parsed.id,
160 dataStore: parentOf(name, "servingConfigs"),
161 project: parsed.project || project,
162 location: parsed.location,
163 displayName: config.displayName,
164 solutionType: config.solutionType,
165 filterControlIds: [...(config.filterControlIds ?? [])],
166 boostControlIds: [...(config.boostControlIds ?? [])],
167 redirectControlIds: [...(config.redirectControlIds ?? [])],
168 synonymsControlIds: [...(config.synonymsControlIds ?? [])],
169 rankingExpression: config.rankingExpression,
170 diversityLevel: config.diversityLevel,
171 modelId: config.modelId,
172 createTime: config.createTime,
173 updateTime: config.updateTime,
174 };
175 };
176
177 const resourceName = (dataStore: string, servingConfigId: string) =>
178 `${dataStore}/servingConfigs/${servingConfigId}`;
179
180 const toBody = (
181 news: CollectionsDataStoresServingConfigProps,
182 displayName: string,
183 ): discoveryengine.GoogleCloudDiscoveryengineV1ServingConfig => ({
184 displayName,
185 solutionType: news.solutionType ?? "SOLUTION_TYPE_SEARCH",
186 filterControlIds: news.filterControlIds,
187 boostControlIds: news.boostControlIds,
188 redirectControlIds: news.redirectControlIds,
189 synonymsControlIds: news.synonymsControlIds,
190 rankingExpression: news.rankingExpression,
191 diversityLevel: news.diversityLevel,
192 modelId: news.modelId,
193 });
194
195 const getByName = (name: string) =>
196 name.length === 0
197 ? Effect.undefined
198 : discoveryengine
199 .getProjectsLocationsCollectionsDataStoresServingConfigs({ name })
200 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
201
202 export const CollectionsDataStoresServingConfigProvider = () =>
203 Provider.succeed(CollectionsDataStoresServingConfig, {
204 stables: [
205 "name",
206 "servingConfigId",
207 "dataStore",
208 "project",
209 "location",
210 "solutionType",
211 "createTime",
212 ],
213
214 diff: Effect.fn(function* ({ news, olds, output }) {
215 if (!isResolved(news)) return undefined;
216 const previousParent = olds?.dataStore ?? output?.dataStore;
217 const previousId = olds?.servingConfigId ?? output?.servingConfigId;
218 const previousType = olds?.solutionType ?? output?.solutionType;
219 const nextType = news.solutionType ?? previousType;
220 if (
221 (previousParent !== undefined && news.dataStore !== previousParent) ||
222 (previousId !== undefined &&
223 news.servingConfigId !== undefined &&
224 news.servingConfigId !== previousId) ||
225 (previousType !== undefined && nextType !== undefined && previousType !== nextType)
226 ) {
227 return {
228 action: "replace" as const,
229 deleteFirst:
230 previousParent === news.dataStore &&
231 previousId !== undefined &&
232 news.servingConfigId === previousId,
233 };
234 }
235 return undefined;
236 }),
237
238 read: Effect.fn(function* ({ id, olds, output }) {
239 const env = yield* GcpEnvironment.current;
240 const parent = olds?.dataStore ?? output?.dataStore;
241 const childId = yield* toPhysical(
242 id,
243 olds?.servingConfigId,
244 output?.servingConfigId,
245 servingConfigIdOf,
246 );
247 const name =
248 output?.name ?? (parent !== undefined ? resourceName(parent, childId) : undefined);
249 if (name === undefined) return undefined;
250 const existing = yield* getByName(name);
251 if (existing === undefined) return undefined;
252 const attrs = toAttrs(existing, env.project);
253 // No labels field: without prior state it may not be ours.
254 return output !== undefined ? attrs : Unowned(attrs);
255 }),
256
257 reconcile: Effect.fn(function* ({ id, news, output }) {
258 const env = yield* GcpEnvironment.current;
259 const servingConfigId = yield* toPhysical(
260 id,
261 news.servingConfigId,
262 output?.servingConfigId,
263 servingConfigIdOf,
264 );
265 const name = resourceName(news.dataStore, servingConfigId);
266 const displayName = news.displayName ?? servingConfigId;
267 const body = toBody(news, displayName);
268
269 let current = yield* getByName(output?.name ?? name);
270
271 if (current === undefined) {
272 const created = yield* discoveryengine
273 .createProjectsLocationsCollectionsDataStoresServingConfigs({
274 parent: news.dataStore,
275 servingConfigId,
276 body,
277 })
278 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
279 current = created ?? undefined;
280 }
281
282 if (current === undefined) {
283 return yield* new CollectionsDataStoresServingConfigNotResolved({
284 resourceName: name,
285 });
286 }
287
288 const resource = current.name ?? name;
289 const displayNameChanged = (current.displayName ?? "") !== displayName;
290 const filterChanged = !sameStringList(current.filterControlIds, news.filterControlIds);
291 const boostChanged = !sameStringList(current.boostControlIds, news.boostControlIds);
292 const redirectChanged = !sameStringList(current.redirectControlIds, news.redirectControlIds);
293 const synonymsChanged = !sameStringList(current.synonymsControlIds, news.synonymsControlIds);
294 const rankingChanged = (current.rankingExpression ?? "") !== (news.rankingExpression ?? "");
295 const diversityChanged = (current.diversityLevel ?? "") !== (news.diversityLevel ?? "");
296 const modelChanged = (current.modelId ?? "") !== (news.modelId ?? "");
297
298 if (
299 displayNameChanged ||
300 filterChanged ||
301 boostChanged ||
302 redirectChanged ||
303 synonymsChanged ||
304 rankingChanged ||
305 diversityChanged ||
306 modelChanged
307 ) {
308 current = yield* discoveryengine.patchProjectsLocationsCollectionsDataStoresServingConfigs({
309 name: resource,
310 updateMask: [
311 displayNameChanged ? "display_name" : undefined,
312 filterChanged ? "filter_control_ids" : undefined,
313 boostChanged ? "boost_control_ids" : undefined,
314 redirectChanged ? "redirect_control_ids" : undefined,
315 synonymsChanged ? "synonyms_control_ids" : undefined,
316 rankingChanged ? "ranking_expression" : undefined,
317 diversityChanged ? "diversity_level" : undefined,
318 modelChanged ? "model_id" : undefined,
319 ]
320 .filter((field): field is string => field !== undefined)
321 .join(","),
322 body: { ...body, name: resource },
323 });
324 }
325
326 return toAttrs(current, env.project);
327 }),
328
329 delete: Effect.fn(function* ({ output }) {
330 yield* discoveryengine
331 .deleteProjectsLocationsCollectionsDataStoresServingConfigs({
332 name: output.name,
333 })
334 .pipe(Effect.catchTag("NotFound", () => Effect.void));
335 }),
336 });