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

main
ts 230 lines 8.11 KB
Raw
1 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
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 { lastSegment, parentBefore, parseResourceName, toResourceId } from "./internal.ts";
11
12 export type CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigProps = {
13 /**
14 * A2A tenant resource name, typically the assistant or agent:
15 * `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}/assistants/{assistant}`.
16 * Immutable — changing it replaces the config.
17 */
18 tenant: string;
19 /**
20 * Parent task resource (`tasks/{task}`). Immutable — changing it
21 * replaces the config.
22 */
23 parent: string;
24 /**
25 * Config id. If omitted, a unique id is generated. Immutable —
26 * changing it replaces the config.
27 */
28 configId?: string;
29 /**
30 * Notification callback URL.
31 */
32 url: string;
33 /**
34 * Token sent with the notification.
35 */
36 token?: string;
37 /**
38 * Authentication schemes (Basic, Bearer, …).
39 */
40 schemes?: string[];
41 /**
42 * Authentication credentials.
43 */
44 credentials?: string;
45 };
46
47 export type CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig = Resource<
48 "GCP.DiscoveryEngine.CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig",
49 CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigProps,
50 {
51 /** Full resource name (`tasks/{task}/pushNotificationConfigs/{config}`). */
52 name: string;
53 /** Config id (last path segment). */
54 configId: string;
55 /** A2A tenant resource name. */
56 tenant: string;
57 /** Parent task resource. */
58 parent: string;
59 /** Project id. */
60 project: string;
61 /** Notification callback URL. */
62 url: string | undefined;
63 /** Token. */
64 token: string | undefined;
65 },
66 never,
67 Providers
68 >;
69
70 /**
71 * An A2A task push-notification config on a Discovery Engine Assistant
72 * agent.
73 *
74 * Experimental A2A API. There is no patch RPC, so URL / token / auth
75 * changes replace the config. The config id is deterministic: `read`
76 * reports a config it finds without prior state as unowned (adopt it
77 * with `--adopt`).
78 *
79 * ### Creating a Push Notification Config
80 * **Example:** Webhook on a task
81 * ```typescript
82 * const config =
83 * yield* GCP.DiscoveryEngine.CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig(
84 * "Notify",
85 * {
86 * tenant: assistant.name,
87 * parent: "tasks/support-1",
88 * url: "https://example.com/hooks/a2a",
89 * },
90 * );
91 * ```
92 *
93 * @resource
94 * @category DiscoveryEngine
95 */
96 export const CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig =
97 Resource<CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig>(
98 "GCP.DiscoveryEngine.CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig",
99 );
100
101 export class CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigNotResolved extends Data.TaggedError(
102 "GCP.DiscoveryEngine.CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigNotResolved",
103 )<{
104 resourceName: string;
105 }> {}
106
107 const resourceName = (parent: string, configId: string) =>
108 `${parent}/pushNotificationConfigs/${configId}`;
109
110 const getByName = (tenant: string, name: string) =>
111 discoveryengine
112 .getProjectsLocationsCollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigs({
113 tenant,
114 name,
115 })
116 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
117
118 const toAttrs = (
119 config: discoveryengine.A2aV1TaskPushNotificationConfig,
120 project: string,
121 tenant: string,
122 ) => {
123 const name = config.name ?? "";
124 const parsed = parseResourceName(name, "pushNotificationConfigs");
125 return {
126 name,
127 configId: parsed.id || lastSegment(name),
128 tenant,
129 parent: parentBefore(name, "pushNotificationConfigs"),
130 project: parsed.project || project,
131 url: config.pushNotificationConfig?.url,
132 token: config.pushNotificationConfig?.token,
133 };
134 };
135
136 export const CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigProvider = () =>
137 Provider.succeed(CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfig, {
138 stables: ["name", "configId", "tenant", "parent", "project"],
139
140 diff: Effect.fn(function* ({ news, olds, output }) {
141 if (!isResolved(news)) return undefined;
142 const previousTenant = olds?.tenant ?? output?.tenant;
143 const previousParent = olds?.parent ?? output?.parent;
144 const previousId = olds?.configId ?? output?.configId;
145 const nextId = news.configId ?? previousId;
146 const previousUrl = olds?.url ?? output?.url;
147 if (
148 (previousTenant !== undefined && news.tenant !== previousTenant) ||
149 (previousParent !== undefined && news.parent !== previousParent) ||
150 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
151 (previousUrl !== undefined && news.url !== previousUrl)
152 ) {
153 return { action: "replace" as const, deleteFirst: true };
154 }
155 return undefined;
156 }),
157
158 read: Effect.fn(function* ({ id, olds, output, previous }) {
159 const env = yield* GcpEnvironment.current;
160 const tenant = olds?.tenant ?? output?.tenant;
161 const parent = olds?.parent ?? output?.parent;
162 const configId = yield* toResourceId(id, olds?.configId, output?.configId);
163 const name =
164 output?.name ?? (parent !== undefined ? resourceName(parent, configId) : undefined);
165 if (tenant === undefined || name === undefined) return undefined;
166 const existing = yield* getByName(tenant, name);
167 if (existing === undefined) return undefined;
168 const attrs = toAttrs(existing, env.project, tenant);
169 // No labels field: without prior state it may not be ours.
170 return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "name");
171 }),
172
173 reconcile: Effect.fn(function* ({ id, news, output }) {
174 const env = yield* GcpEnvironment.current;
175 const configId = yield* toResourceId(id, news.configId, output?.configId);
176 const token = news.token;
177 const fallbackName = output?.name ?? resourceName(news.parent, configId);
178
179 let current = yield* getByName(news.tenant, fallbackName);
180
181 if (current === undefined) {
182 const created = yield* discoveryengine
183 .createProjectsLocationsCollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigs(
184 {
185 tenant: news.tenant,
186 parent: news.parent,
187 configId,
188 body: {
189 name: fallbackName,
190 pushNotificationConfig: {
191 url: news.url,
192 token,
193 id: configId,
194 authentication:
195 news.schemes !== undefined || news.credentials !== undefined
196 ? {
197 schemes: news.schemes,
198 credentials: news.credentials,
199 }
200 : undefined,
201 },
202 },
203 },
204 )
205 .pipe(Effect.catchTag("Conflict", () => getByName(news.tenant, fallbackName)));
206 current = created ?? undefined;
207 }
208
209 if (current === undefined) {
210 return yield* new CollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigNotResolved(
211 { resourceName: fallbackName },
212 );
213 }
214
215 return toAttrs(current, env.project, news.tenant);
216 }),
217
218 delete: Effect.fn(function* ({ output }) {
219 const existing = yield* getByName(output.tenant, output.name);
220 if (existing === undefined) return;
221 yield* discoveryengine
222 .deleteProjectsLocationsCollectionsEnginesAssistantsAgentsA2aV1TasksPushNotificationConfigs(
223 {
224 tenant: output.tenant,
225 name: output.name,
226 },
227 )
228 .pipe(Effect.catchTag("NotFound", () => Effect.void));
229 }),
230 });