@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 425 lines 14.9 KB
Raw
1 import { createHash } from "node:crypto";
2 import * as apigee from "@distilled.cloud/gcp/apigee_v1";
3 import { Unowned } from "alchemy/AdoptPolicy";
4 import { isResolved } from "alchemy/Diff";
5 import { createPhysicalName } from "alchemy/PhysicalName";
6 import * as Provider from "alchemy/Provider";
7 import { Resource } from "alchemy/Resource";
8 import { unzipFiles, zipFiles } from "alchemy/Util/zip";
9 import * as Data from "effect/Data";
10 import * as Effect from "effect/Effect";
11 import * as Schedule from "effect/Schedule";
12 import { GcpEnvironment } from "../Environment.ts";
13 import type { Providers } from "../Providers.ts";
14 import {
15 createInternalLabels,
16 encodeOwnership,
17 hasAlchemyLabels,
18 hasOwnershipMarker,
19 lastSegment,
20 orgIdOf,
21 orgParent,
22 parseOwnership,
23 xmlEscape,
24 xmlUnescape,
25 } from "./ownership.ts";
26
27 const MAX_ID_LENGTH = 255;
28
29 export type SharedflowProps = {
30 /**
31 * Shared flow id (the `{sharedflow}` segment of
32 * `organizations/{org}/sharedflows/{sharedflow}`). If omitted, a unique
33 * name is generated from the stack, stage, and logical id. Restrict
34 * characters to `A-Za-z0-9._-`. Immutable — changing it replaces the
35 * shared flow.
36 */
37 sharedflowId?: string;
38 /**
39 * Apigee organization id. Defaults to the stack GCP project. Immutable —
40 * changing it replaces the shared flow.
41 */
42 organization?: string;
43 /**
44 * Space id to associate with this shared flow. Only applied on create;
45 * subsequent space changes call `move`.
46 */
47 space?: string;
48 /**
49 * Base64-encoded ZIP shared-flow bundle. If omitted, Alchemy uploads a
50 * minimal empty shared flow. Shared flows have no labels field, so
51 * Alchemy ownership is stored in the bundle `Description` for `list` /
52 * nuke. Uploading a new bundle (or changing `description`) creates a
53 * new revision.
54 */
55 bundle?: string;
56 /**
57 * Human-readable description stored in the shared-flow bundle. Alchemy
58 * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is
59 * prefixed and stripped from attributes.
60 */
61 description?: string;
62 };
63
64 export type Sharedflow = Resource<
65 "GCP.Apigee.Sharedflow",
66 SharedflowProps,
67 {
68 /** Full resource name `organizations/{org}/sharedflows/{sharedflow}`. */
69 name: string;
70 /** Shared flow id (last path segment). */
71 sharedflowId: string;
72 /** Apigee organization id. */
73 organization: string;
74 /** Space id, if the shared flow is associated with a space. */
75 space: string | undefined;
76 /** Latest revision id. */
77 latestRevisionId: string | undefined;
78 /** Known revision ids. */
79 revision: string[];
80 /** User description with the Alchemy ownership prefix stripped. */
81 description: string | undefined;
82 /** Created-at timestamp in milliseconds since epoch. */
83 createdAt: string | undefined;
84 /** Last-modified timestamp in milliseconds since epoch. */
85 lastModifiedAt: string | undefined;
86 },
87 never,
88 Providers
89 >;
90
91 /**
92 * An Apigee shared flow — a reusable sequence of policies that API
93 * proxies (or other shared flows) invoke with a FlowCallout.
94 *
95 * Shared flows have no labels field, so Alchemy stamps ownership into
96 * the bundle `Description` for `list` / nuke. `sharedflowId` and
97 * `organization` are identity — changing either replaces the shared
98 * flow. Uploading a bundle or changing `description` creates a new
99 * revision. `space` is moved in place.
100 *
101 * ### Creating a Shared Flow
102 * **Example:** Generated empty bundle
103 * ```typescript
104 * const flow = yield* GCP.Apigee.Sharedflow("Traffic", {
105 * description: "rate limit and spike arrest",
106 * });
107 * ```
108 *
109 * **Example:** Explicit id and space
110 * ```typescript
111 * const flow = yield* GCP.Apigee.Sharedflow("Traffic", {
112 * sharedflowId: "traffic-management",
113 * space: team.spaceId,
114 * description: "rate limit and spike arrest",
115 * });
116 * ```
117 *
118 * ### Updating a Shared Flow
119 * **Example:** New description (creates a revision)
120 * ```typescript
121 * const flow = yield* GCP.Apigee.Sharedflow("Traffic", {
122 * description: "rate limit only",
123 * });
124 * ```
125 *
126 * @resource
127 * @category Apigee
128 */
129 export const Sharedflow = Resource<Sharedflow>("GCP.Apigee.Sharedflow");
130
131 export class SharedflowNotResolved extends Data.TaggedError("GCP.Apigee.SharedflowNotResolved")<{
132 resourceName: string;
133 }> {}
134
135 const sanitizeId = (name: string): string => {
136 let next = name.replace(/[^A-Za-z0-9._-]/g, "-").replace(/^-+/, "");
137 if (!/^[A-Za-z]/.test(next)) {
138 next = `s${next}`;
139 }
140 next = next.slice(0, MAX_ID_LENGTH).replace(/[^A-Za-z0-9]+$/g, "");
141 return next.length > 0 ? next : "sharedflow";
142 };
143
144 const resourceName = (org: string, sharedflowId: string) =>
145 `${orgParent(org)}/sharedflows/${sharedflowId}`;
146
147 const sharedflowIdOf = (flow: apigee.GoogleCloudApigeeV1SharedFlow) => {
148 const raw = flow.name ?? "";
149 return raw.includes("/") ? lastSegment(raw) : raw;
150 };
151
152 const toId = (id: string, sharedflowId: string | undefined, existing?: string) =>
153 Effect.gen(function* () {
154 if (sharedflowId !== undefined) return sanitizeId(sharedflowId);
155 if (existing !== undefined) return existing;
156 return sanitizeId(
157 yield* createPhysicalName({
158 id,
159 maxLength: MAX_ID_LENGTH,
160 lowercase: true,
161 }),
162 );
163 });
164
165 const sha12 = (value: string) =>
166 Effect.sync(() => createHash("sha256").update(value).digest("hex").slice(0, 12));
167
168 const bundleXml = (sharedflowId: string, description: string) =>
169 `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
170 <SharedFlowBundle name="${xmlEscape(sharedflowId)}">
171 <ConfigurationVersion majorVersion="4" minorVersion="0"/>
172 <Description>${xmlEscape(description)}</Description>
173 <DisplayName>${xmlEscape(sharedflowId)}</DisplayName>
174 <SharedFlows>
175 <SharedFlow>default</SharedFlow>
176 </SharedFlows>
177 </SharedFlowBundle>
178 `;
179
180 const defaultFlowXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
181 <SharedFlow name="default">
182 </SharedFlow>
183 `;
184
185 const toBase64 = (bytes: Uint8Array) => Effect.sync(() => Buffer.from(bytes).toString("base64"));
186
187 const makeEmptyBundle = (sharedflowId: string, description: string) =>
188 zipFiles([
189 {
190 path: `sharedflowbundle/${sharedflowId}.xml`,
191 content: bundleXml(sharedflowId, description),
192 },
193 {
194 path: "sharedflowbundle/sharedflows/default.xml",
195 content: defaultFlowXml,
196 },
197 ]).pipe(Effect.flatMap(toBase64));
198
199 const bundleXmlPath = (paths: ReadonlyArray<string>) =>
200 paths.find((path) => /^sharedflowbundle\/[^/]+\.xml$/i.test(path));
201
202 const stampBundle = (base64: string, description: string) =>
203 Effect.gen(function* () {
204 const bytes = yield* Effect.sync(() => Buffer.from(base64, "base64"));
205 const entries = yield* unzipFiles(bytes);
206 const xmlPath = bundleXmlPath(Object.keys(entries));
207 if (xmlPath !== undefined) {
208 const xml = yield* Effect.sync(() => Buffer.from(entries[xmlPath]!).toString("utf8"));
209 const escaped = `<Description>${xmlEscape(description)}</Description>`;
210 const next = xml.includes("<Description>")
211 ? xml.replace(/<Description>[\s\S]*?<\/Description>/, escaped)
212 : xml.replace(/<\/SharedFlowBundle>/, ` ${escaped}\n</SharedFlowBundle>`);
213 entries[xmlPath] = yield* Effect.sync(() => Buffer.from(next, "utf8"));
214 }
215 // zipFiles sorts entries and stamps a fixed timestamp, so restamping the
216 // bundle is byte-identical for identical content.
217 const archive = yield* zipFiles(
218 Object.entries(entries).map(([path, content]) => ({ path, content })),
219 );
220 return yield* toBase64(archive);
221 });
222
223 const desiredBundle = (sharedflowId: string, description: string, bundle: string | undefined) =>
224 bundle !== undefined
225 ? stampBundle(bundle, description)
226 : makeEmptyBundle(sharedflowId, description);
227
228 const toAttrs = (
229 flow: apigee.GoogleCloudApigeeV1SharedFlow,
230 org: string,
231 description: string | undefined,
232 ) => {
233 const sharedflowId = sharedflowIdOf(flow);
234 const parsed = parseOwnership(description);
235 return {
236 name: resourceName(org, sharedflowId),
237 sharedflowId,
238 organization: org,
239 space: flow.space,
240 latestRevisionId: flow.latestRevisionId,
241 revision: [...(flow.revision ?? [])],
242 description: parsed.text,
243 createdAt: flow.metaData?.createdAt,
244 lastModifiedAt: flow.metaData?.lastModifiedAt,
245 };
246 };
247
248 const getByName = (name: string) =>
249 apigee
250 .getOrganizationsSharedflows({ name })
251 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined));
252
253 const getRevision = (name: string) =>
254 apigee
255 .getOrganizationsSharedflowsRevisions({ name, format: "bundle" })
256 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.undefined));
257
258 const descriptionFromBundle = (body: apigee.GoogleApiHttpBody | undefined) =>
259 Effect.gen(function* () {
260 const data = body?.data;
261 if (data === undefined || data.length === 0) return undefined;
262 const bytes = yield* Effect.sync(() => Buffer.from(data, "base64"));
263 const entries = yield* unzipFiles(bytes);
264 const xmlPath = bundleXmlPath(Object.keys(entries));
265 if (xmlPath === undefined) return undefined;
266 const xml = yield* Effect.sync(() => Buffer.from(entries[xmlPath]!).toString("utf8"));
267 const match = xml.match(/<Description>([\s\S]*?)<\/Description>/);
268 if (match?.[1] === undefined) return undefined;
269 return xmlUnescape(match[1]);
270 });
271
272 const revisionName = (org: string, sharedflowId: string, revisionId: string) =>
273 `${resourceName(org, sharedflowId)}/revisions/${revisionId}`;
274
275 const observedDescription = (org: string, flow: apigee.GoogleCloudApigeeV1SharedFlow) =>
276 Effect.gen(function* () {
277 const sharedflowId = sharedflowIdOf(flow);
278 const latest = flow.latestRevisionId;
279 if (!sharedflowId || !latest) return undefined;
280 const revision = yield* getRevision(revisionName(org, sharedflowId, latest));
281 return yield* descriptionFromBundle(revision);
282 });
283
284 const importBundle = (input: { org: string; sharedflowId: string; space?: string; data: string }) =>
285 apigee.createOrganizationsSharedflows({
286 parent: orgParent(input.org),
287 action: "import",
288 name: input.sharedflowId,
289 space: input.space,
290 body: {
291 contentType: "application/octet-stream",
292 data: input.data,
293 },
294 });
295
296 const waitUntilExists = (name: string) =>
297 getByName(name).pipe(
298 Effect.flatMap((flow) =>
299 flow ? Effect.succeed(flow) : Effect.fail(new SharedflowNotResolved({ resourceName: name })),
300 ),
301 Effect.retry({
302 while: (error) => error._tag === "GCP.Apigee.SharedflowNotResolved",
303 times: 8,
304 schedule: Schedule.exponential("250 millis"),
305 }),
306 );
307
308 export const SharedflowProvider = () =>
309 Provider.succeed(Sharedflow, {
310 stables: ["name", "sharedflowId", "organization", "createdAt"],
311
312 diff: Effect.fn(function* ({ news, olds, output }) {
313 if (!isResolved(news)) return undefined;
314 const previousId = olds?.sharedflowId ?? output?.sharedflowId;
315 const idChanged =
316 previousId !== undefined &&
317 news.sharedflowId !== undefined &&
318 sanitizeId(news.sharedflowId) !== previousId;
319 const previousOrg = olds?.organization ?? output?.organization;
320 const orgChanged =
321 previousOrg !== undefined &&
322 news.organization !== undefined &&
323 orgIdOf(news.organization, previousOrg) !== previousOrg;
324 if (idChanged || orgChanged) {
325 return { action: "replace" as const, deleteFirst: false };
326 }
327 return undefined;
328 }),
329
330 read: Effect.fn(function* ({ id, olds, output }) {
331 const env = yield* GcpEnvironment.current;
332 const org = orgIdOf(olds?.organization ?? output?.organization, env.project);
333 const sharedflowId = yield* toId(id, olds?.sharedflowId, output?.sharedflowId);
334 const name = output?.name ?? resourceName(org, sharedflowId);
335 const existing = yield* getByName(name);
336 if (existing === undefined) return undefined;
337 const description = yield* observedDescription(org, existing);
338 const attrs = toAttrs(existing, org, description);
339 const { labels } = parseOwnership(description);
340 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
341 }),
342
343 list: () =>
344 Effect.gen(function* () {
345 const env = yield* GcpEnvironment.current;
346 const org = env.project;
347 const page = yield* apigee
348 .listOrganizationsSharedflows({
349 parent: orgParent(org),
350 includeRevisions: true,
351 includeMetaData: true,
352 })
353 .pipe(
354 Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () =>
355 Effect.succeed({ sharedFlows: [] }),
356 ),
357 );
358 const owned = yield* Effect.forEach(
359 page.sharedFlows ?? [],
360 (flow) =>
361 Effect.gen(function* () {
362 const description = yield* observedDescription(org, flow);
363 if (!hasOwnershipMarker(description)) return undefined;
364 return toAttrs(flow, org, description);
365 }),
366 { concurrency: 8 },
367 );
368 return owned.filter((flow) => flow !== undefined);
369 }),
370
371 reconcile: Effect.fn(function* ({ id, news, output }) {
372 const env = yield* GcpEnvironment.current;
373 const org = orgIdOf(news.organization ?? output?.organization, env.project);
374 const sharedflowId = yield* toId(id, news.sharedflowId, output?.sharedflowId);
375 const name = resourceName(org, sharedflowId);
376 const ownership = yield* createInternalLabels(id);
377 const extra = news.bundle !== undefined ? { bundle: yield* sha12(news.bundle) } : undefined;
378 const desiredDescription = encodeOwnership(ownership, news.description, extra);
379 const data = yield* desiredBundle(sharedflowId, desiredDescription, news.bundle);
380
381 let current = yield* getByName(output?.name ?? name);
382
383 if (current === undefined) {
384 yield* importBundle({
385 org,
386 sharedflowId,
387 space: news.space,
388 data,
389 }).pipe(Effect.catchTag("Conflict", () => Effect.void));
390 current = yield* waitUntilExists(name);
391 }
392
393 if (current === undefined) {
394 return yield* new SharedflowNotResolved({ resourceName: name });
395 }
396
397 const observed = yield* observedDescription(org, current);
398 if ((observed ?? "") !== desiredDescription) {
399 yield* importBundle({
400 org,
401 sharedflowId,
402 data,
403 });
404 current = yield* waitUntilExists(name);
405 }
406
407 const desiredSpace = news.space ?? "";
408 const observedSpace = current.space ?? "";
409 if (desiredSpace !== observedSpace) {
410 current = yield* apigee.moveOrganizationsSharedflows({
411 name,
412 body: { space: news.space },
413 });
414 }
415
416 const description = yield* observedDescription(org, current);
417 return toAttrs(current, org, description);
418 }),
419
420 delete: Effect.fn(function* ({ output }) {
421 yield* apigee
422 .deleteOrganizationsSharedflows({ name: output.name })
423 .pipe(Effect.catchTag(["NotFound", "ApigeeResourceNotFound"], () => Effect.void));
424 }),
425 });