@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 314 lines 10.5 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 * as Schedule from "effect/Schedule";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 parentOf,
14 parseResourceName,
15 siteSearchEngineParent,
16 matchesUriPattern,
17 } from "./internal.ts";
18 import { waitForOperation } from "./operations.ts";
19
20 export type CollectionsDataStoresSiteSearchEngineTargetSiteProps = {
21 /**
22 * Parent data store resource name (must use `PUBLIC_WEBSITE` content
23 * config). Immutable — changing it replaces the target site.
24 */
25 dataStore: string;
26 /**
27 * User-provided URI pattern used to generate the crawl pattern. Required.
28 */
29 providedUriPattern: string;
30 /**
31 * Whether the site is included or excluded.
32 * @default "INCLUDE"
33 */
34 type?: "TYPE_UNSPECIFIED" | "INCLUDE" | "EXCLUDE" | (string & {});
35 /**
36 * Exact-match URI pattern generation. Immutable after create.
37 * @default false
38 */
39 exactMatch?: boolean;
40 };
41
42 export type CollectionsDataStoresSiteSearchEngineTargetSite = Resource<
43 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSite",
44 CollectionsDataStoresSiteSearchEngineTargetSiteProps,
45 {
46 /** Full resource name. */
47 name: string;
48 /** System-generated target site id. */
49 targetSiteId: string;
50 /** Parent data store resource name. */
51 dataStore: string;
52 /** Project id. */
53 project: string;
54 /** Location id. */
55 location: string;
56 /** User-provided URI pattern. */
57 providedUriPattern: string;
58 /** Generated URI pattern. */
59 generatedUriPattern: string | undefined;
60 /** Include or exclude. */
61 type: string | undefined;
62 /** Exact-match flag. */
63 exactMatch: boolean;
64 /** Indexing status. */
65 indexingStatus: string | undefined;
66 /** Root domain of the provided URI pattern. */
67 rootDomainUri: string | undefined;
68 /** RFC3339 last-update timestamp. */
69 updateTime: string | undefined;
70 },
71 never,
72 Providers
73 >;
74
75 /**
76 * A Discovery Engine target site on a collection data store site-search
77 * engine.
78 *
79 * Its id is assigned by the API, so only a recorded resource can be read
80 * back. Parent data store and `exactMatch` are immutable. Type updates in
81 * place. The target site id is assigned by the API.
82 *
83 * ### Creating a Target Site
84 * **Example:** Include a site
85 * ```typescript
86 * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Web", {
87 * contentConfig: "PUBLIC_WEBSITE",
88 * });
89 * const site = yield* GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSite(
90 * "Docs",
91 * {
92 * dataStore: store.name,
93 * providedUriPattern: "https://example.com/docs/",
94 * },
95 * );
96 * ```
97 *
98 * @resource
99 * @category DiscoveryEngine
100 */
101 export const CollectionsDataStoresSiteSearchEngineTargetSite =
102 Resource<CollectionsDataStoresSiteSearchEngineTargetSite>(
103 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSite",
104 );
105
106 export class CollectionsDataStoresSiteSearchEngineTargetSiteNotResolved extends Data.TaggedError(
107 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSiteNotResolved",
108 )<{
109 resourceName: string;
110 }> {}
111
112 export class CollectionsDataStoresSiteSearchEngineTargetSiteStillExists extends Data.TaggedError(
113 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSiteStillExists",
114 )<{
115 resourceName: string;
116 }> {}
117
118 const toAttrs = (
119 site: discoveryengine.GoogleCloudDiscoveryengineV1TargetSite,
120 project: string,
121 pattern?: string,
122 ) => {
123 const name = site.name ?? "";
124 const parsed = parseResourceName(name, "targetSites");
125 return {
126 name,
127 targetSiteId: parsed.id,
128 dataStore: parentOf(name, "siteSearchEngine"),
129 project: parsed.project || project,
130 location: parsed.location,
131 providedUriPattern: site.providedUriPattern ?? pattern ?? site.generatedUriPattern ?? "",
132 generatedUriPattern: site.generatedUriPattern,
133 type: site.type,
134 exactMatch: site.exactMatch === true,
135 indexingStatus: site.indexingStatus,
136 rootDomainUri: site.rootDomainUri,
137 updateTime: site.updateTime,
138 };
139 };
140
141 const getByName = (name: string) =>
142 name.length === 0
143 ? Effect.undefined
144 : discoveryengine
145 .getProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites({
146 name,
147 })
148 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
149
150 const listAtParent = (parent: string) =>
151 discoveryengine.listProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites
152 .pages({ parent, pageSize: 1000 })
153 .pipe(
154 Stream.flatMap((page) => Stream.fromIterable(page.targetSites ?? [])),
155 Stream.runCollect,
156 Effect.map((chunk) => Array.from(chunk)),
157 Effect.catchTag("NotFound", () => Effect.succeed([])),
158 );
159
160 /** Target sites are identified by their URI pattern within a data store. */
161 const findByPattern = (dataStore: string, pattern: string, hinted?: string) =>
162 Effect.gen(function* () {
163 if (hinted !== undefined && hinted.length > 0) {
164 const existing = yield* getByName(hinted);
165 if (existing !== undefined) return existing;
166 }
167 const sites = yield* listAtParent(siteSearchEngineParent(dataStore));
168 return sites.find((site) => matchesUriPattern(site, pattern));
169 });
170
171 const waitUntilExists = (name: string) =>
172 getByName(name).pipe(
173 Effect.flatMap((site) =>
174 site
175 ? Effect.succeed(site)
176 : Effect.fail(
177 new CollectionsDataStoresSiteSearchEngineTargetSiteNotResolved({
178 resourceName: name,
179 }),
180 ),
181 ),
182 Effect.retry({
183 while: (error) =>
184 error._tag ===
185 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSiteNotResolved",
186 times: 8,
187 schedule: Schedule.spaced("2 seconds"),
188 }),
189 );
190
191 const waitUntilGone = (name: string) =>
192 getByName(name).pipe(
193 Effect.flatMap((site) =>
194 site === undefined
195 ? Effect.void
196 : Effect.fail(
197 new CollectionsDataStoresSiteSearchEngineTargetSiteStillExists({
198 resourceName: name,
199 }),
200 ),
201 ),
202 Effect.retry({
203 while: (error) =>
204 error._tag ===
205 "GCP.DiscoveryEngine.CollectionsDataStoresSiteSearchEngineTargetSiteStillExists",
206 times: 10,
207 schedule: Schedule.spaced("3 seconds"),
208 }),
209 );
210
211 export const CollectionsDataStoresSiteSearchEngineTargetSiteProvider = () =>
212 Provider.succeed(CollectionsDataStoresSiteSearchEngineTargetSite, {
213 stables: ["name", "targetSiteId", "dataStore", "project", "location", "exactMatch"],
214
215 diff: Effect.fn(function* ({ news, olds, output }) {
216 if (!isResolved(news)) return undefined;
217 const previousParent = olds?.dataStore ?? output?.dataStore;
218 const previousExact = olds?.exactMatch ?? output?.exactMatch ?? false;
219 const nextExact = news.exactMatch ?? previousExact;
220 if (
221 (previousParent !== undefined && news.dataStore !== previousParent) ||
222 previousExact !== nextExact
223 ) {
224 return { action: "replace" as const, deleteFirst: false };
225 }
226 return undefined;
227 }),
228
229 read: Effect.fn(function* ({ olds, output }) {
230 const env = yield* GcpEnvironment.current;
231 const dataStore = olds?.dataStore ?? output?.dataStore;
232 const pattern = olds?.providedUriPattern ?? output?.providedUriPattern;
233 const existing =
234 output?.name !== undefined
235 ? yield* getByName(output.name)
236 : dataStore !== undefined && pattern !== undefined
237 ? yield* findByPattern(dataStore, pattern)
238 : undefined;
239 if (existing === undefined) return undefined;
240 const attrs = toAttrs(existing, env.project, pattern);
241 // No labels field: without prior state it may not be ours.
242 return output !== undefined ? attrs : Unowned(attrs);
243 }),
244
245 reconcile: Effect.fn(function* ({ news, output }) {
246 const env = yield* GcpEnvironment.current;
247 const providedUriPattern = news.providedUriPattern;
248 const type = news.type ?? "INCLUDE";
249 const exactMatch = news.exactMatch === true;
250 const parent = siteSearchEngineParent(news.dataStore);
251
252 let current = yield* findByPattern(news.dataStore, providedUriPattern, output?.name);
253
254 if (current === undefined) {
255 const created = yield* discoveryengine
256 .createProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites({
257 parent,
258 body: {
259 providedUriPattern,
260 type,
261 exactMatch: exactMatch ? true : undefined,
262 },
263 })
264 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
265 if (created !== undefined) {
266 yield* waitForOperation(created);
267 }
268 current = yield* findByPattern(news.dataStore, providedUriPattern);
269 }
270
271 if (current === undefined) {
272 return yield* new CollectionsDataStoresSiteSearchEngineTargetSiteNotResolved({
273 resourceName: output?.name ?? `${parent}/targetSites/-`,
274 });
275 }
276
277 const resource = current.name ?? "";
278 const typeChanged = (current.type ?? "INCLUDE") !== type;
279 const patternChanged = !matchesUriPattern(current, providedUriPattern);
280
281 if (typeChanged || patternChanged) {
282 const patched =
283 yield* discoveryengine.patchProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites(
284 {
285 name: resource,
286 body: {
287 name: resource,
288 providedUriPattern,
289 type,
290 exactMatch: current.exactMatch,
291 },
292 },
293 );
294 yield* waitForOperation(patched);
295 current = yield* waitUntilExists(resource);
296 }
297
298 return toAttrs(current, env.project, providedUriPattern);
299 }),
300
301 delete: Effect.fn(function* ({ output }) {
302 const existing = yield* getByName(output.name);
303 if (existing === undefined) return;
304 const operation = yield* discoveryengine
305 .deleteProjectsLocationsCollectionsDataStoresSiteSearchEngineTargetSites({
306 name: output.name,
307 })
308 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
309 if (operation !== undefined) {
310 yield* waitForOperation(operation, { notFoundOk: true });
311 }
312 yield* waitUntilGone(output.name);
313 }),
314 });