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

main
ts 99 lines 3.69 KB
Raw
1 import * as Category from "@distilled.cloud/core/category";
2 import * as dialogflow from "@distilled.cloud/gcp/dialogflow_v3";
3 import * as GcpRetry from "@distilled.cloud/gcp/Retry";
4 import * as Effect from "effect/Effect";
5 import * as Schedule from "effect/Schedule";
6 import * as Stream from "effect/Stream";
7
8 /**
9 * The Dialogflow suite runs ~20 files at once against one project, which
10 * overruns the per-minute "All other requests" quota. Distilled's default
11 * backoff gives up before the minute rolls over, so the suite rides it out.
12 */
13 export const quotaTolerant = GcpRetry.policy({
14 while: (error) => Category.isThrottling(error) || Category.isTransientError(error),
15 schedule: Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(12)]),
16 });
17
18 export const DEFAULT_LOCATION = "global";
19
20 export const locationParent = (project: string, location = DEFAULT_LOCATION) =>
21 `projects/${project}/locations/${location}`;
22
23 export const getAgent = (name: string) =>
24 dialogflow
25 .getProjectsLocationsAgents({ name })
26 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
27
28 const listAgents = (parent: string) =>
29 dialogflow.listProjectsLocationsAgents.pages({ parent, pageSize: 100 }).pipe(
30 Stream.flatMap((page) => Stream.fromIterable(page.agents ?? [])),
31 Stream.runCollect,
32 Effect.map((chunk) => Array.from(chunk)),
33 Effect.catchTag("NotFound", () => Effect.succeed([])),
34 );
35
36 export const ensureAgent = (project: string, displayName: string, location = DEFAULT_LOCATION) =>
37 Effect.gen(function* () {
38 const parent = locationParent(project, location);
39 const agents = yield* listAgents(parent);
40 const existing = agents.find((agent) => agent.displayName === displayName);
41 if (existing?.name) {
42 const current = yield* getAgent(existing.name);
43 if (current !== undefined) return current;
44 }
45 return yield* dialogflow.createProjectsLocationsAgents({
46 parent,
47 body: {
48 displayName,
49 defaultLanguageCode: "en",
50 timeZone: "America/Los_Angeles",
51 description: displayName,
52 },
53 });
54 });
55
56 export const deleteAgent = (name: string) =>
57 Effect.gen(function* () {
58 if (name.length === 0) return;
59 const existing = yield* getAgent(name);
60 if (existing === undefined) return;
61 yield* dialogflow
62 .deleteProjectsLocationsAgents({ name })
63 .pipe(Effect.catchTag("NotFound", () => Effect.void));
64 }).pipe(Effect.ignore);
65
66 export const getEntityType = (name: string) =>
67 dialogflow
68 .getProjectsLocationsAgentsEntityTypes({ name })
69 .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined)));
70
71 export const ensureEntityType = (agent: string, displayName: string) =>
72 Effect.gen(function* () {
73 const listed = yield* dialogflow.listProjectsLocationsAgentsEntityTypes
74 .pages({ parent: agent, pageSize: 100 })
75 .pipe(
76 Stream.flatMap((page) => Stream.fromIterable(page.entityTypes ?? [])),
77 Stream.runCollect,
78 Effect.map((chunk) => Array.from(chunk)),
79 Effect.catchTag("NotFound", () => Effect.succeed([])),
80 );
81 const existing = listed.find((entityType) => entityType.displayName === displayName);
82 if (existing?.name) {
83 const current = yield* getEntityType(existing.name);
84 if (current !== undefined) return current;
85 }
86 return yield* dialogflow.createProjectsLocationsAgentsEntityTypes({
87 parent: agent,
88 body: {
89 displayName,
90 kind: "KIND_MAP",
91 entities: [{ value: "blue", synonyms: ["blue", "navy"] }],
92 },
93 });
94 });
95
96 export const deleteEntityType = (name: string) =>
97 dialogflow
98 .deleteProjectsLocationsAgentsEntityTypes({ name, force: true })
99 .pipe(Effect.catchTag("NotFound", () => Effect.void));