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

main
ts 250 lines 7.62 KB
Raw
1 import * as sasportal from "@distilled.cloud/gcp/sasportal_v1alpha1";
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 encodeOwnershipLine,
12 expandPath,
13 findOwnedNodeNodeNode,
14 getNodeNode,
15 hasOwnershipMarker,
16 lastSegment,
17 listNodeNodeNodes,
18 MAX_DISPLAY_NAME_LENGTH,
19 ownedByAlchemy,
20 ownershipLabels,
21 parentOf,
22 parseOwnership,
23 replaceOnIdentity,
24 retryDelete,
25 sameStringList,
26 sameText,
27 scanOwnedNodeNodeNode,
28 toDisplayName,
29 updateMaskOf,
30 waitUntilGone,
31 walkNodeChildren,
32 } from "./internal.ts";
33
34 export type NodesNodesNodeProps = {
35 /**
36 * Parent nested SAS node, as `nodes/{node}/nodes/{node}`.
37 * Immutable — changing it replaces the child node.
38 */
39 parent: string;
40 /**
41 * Server-assigned node resource name (`nodes/{node}/nodes/{node}/nodes/{node}`).
42 * Immutable — changing it replaces the node.
43 */
44 name?: string;
45 /**
46 * Display name. SAS Portal nodes have no labels field, so Alchemy
47 * stamps ownership into a `[alchemy …]` prefix and strips it from
48 * attributes.
49 */
50 displayName?: string;
51 /**
52 * SAS user ids used by devices that belong to this node.
53 */
54 sasUserIds?: string[];
55 };
56
57 export type NodesNodesNode = Resource<
58 "GCP.SasPortal.NodesNodesNode",
59 NodesNodesNodeProps,
60 {
61 /** Resource name `nodes/{node}/nodes/{node}/nodes/{node}`. */
62 name: string;
63 /** Node id (last path segment). */
64 nodeId: string;
65 /** Parent resource name. */
66 parent: string;
67 /** Project id used when the node was reconciled. */
68 project: string;
69 /** User-facing display name with the Alchemy prefix stripped. */
70 displayName: string | undefined;
71 /** SAS user ids on the node. */
72 sasUserIds: string[] | undefined;
73 },
74 never,
75 Providers
76 >;
77
78 /**
79 * A Spectrum Access System (SAS) Portal node nested under another nested node.
80 *
81 * SAS Portal nodes have no labels field, so Alchemy stamps ownership
82 * into `displayName` for `list` / nuke. Parent is identity. Display
83 * name and SAS user ids update in place.
84 *
85 * ### Creating a Child Node
86 * **Example:** Nested node
87 * ```typescript
88 * const child = yield* GCP.SasPortal.NodesNodesNode("Sector", {
89 * parent: child.name,
90 * displayName: "sector-1",
91 * });
92 * ```
93 *
94 * ### Updating a Child Node
95 * Change props on the same logical id; the engine keeps the physical id.
96 *
97 * **Example:** Rename
98 * ```typescript
99 * const child = yield* GCP.SasPortal.NodesNodesNode("Sector", {
100 * parent: child.name,
101 * displayName: "sector-1-2",
102 * });
103 * ```
104 *
105 * @resource
106 * @category SasPortal
107 */
108 export const NodesNodesNode = Resource<NodesNodesNode>("GCP.SasPortal.NodesNodesNode");
109
110 export class NodesNodesNodeNotResolved extends Data.TaggedError(
111 "GCP.SasPortal.NodesNodesNodeNotResolved",
112 )<{
113 parent: string;
114 resourceName: string;
115 }> {}
116
117 const toAttrs = (node: sasportal.SasPortalNode, parent: string, project: string) => {
118 const name = node.name ?? "";
119 return {
120 name,
121 nodeId: lastSegment(name),
122 parent: parentOf(name) || parent,
123 project,
124 displayName: parseOwnership(node.displayName).text,
125 sasUserIds: node.sasUserIds,
126 };
127 };
128
129 export const NodesNodesNodeProvider = () =>
130 Provider.succeed(NodesNodesNode, {
131 stables: ["name", "nodeId", "parent", "project"],
132
133 diff: Effect.fn(function* ({ news, olds, output }) {
134 if (!isResolved(news)) return undefined;
135 return replaceOnIdentity({
136 previousParent: olds?.parent ?? output?.parent,
137 nextParent: expandPath(news.parent),
138 previousId: olds?.name ?? output?.name,
139 nextId: news.name,
140 });
141 }),
142
143 read: Effect.fn(function* ({ id, olds, output }) {
144 const env = yield* GcpEnvironment.current;
145 const parent = expandPath(olds?.parent ?? output?.parent ?? "");
146 const name = olds?.name ?? output?.name ?? "";
147 let existing = yield* getNodeNode(name);
148 let locatedParent = existing ? parentOf(existing.name ?? "") || parent : parent;
149 if (existing === undefined) {
150 const found =
151 (yield* findOwnedNodeNodeNode(id, parent)) ?? (yield* scanOwnedNodeNodeNode(id, parent));
152 existing = found?.row;
153 locatedParent = found?.parent ?? locatedParent;
154 }
155 if (existing === undefined) return undefined;
156 const attrs = toAttrs(existing, locatedParent, env.project);
157 return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);
158 }),
159
160 list: () =>
161 Effect.gen(function* () {
162 const env = yield* GcpEnvironment.current;
163 const parents = yield* walkNodeChildren();
164 const pages = yield* Effect.forEach(
165 parents,
166 (entry) => {
167 const parent = entry.node.name ?? "";
168 return parent.length === 0
169 ? Effect.succeed([])
170 : listNodeNodeNodes(parent).pipe(
171 Effect.map((rows) =>
172 rows
173 .filter((row) => hasOwnershipMarker(row.displayName))
174 .map((row) => toAttrs(row, parent, env.project)),
175 ),
176 );
177 },
178 { concurrency: 4 },
179 );
180 return pages.flat();
181 }),
182
183 reconcile: Effect.fn(function* ({ id, news, output }) {
184 const env = yield* GcpEnvironment.current;
185 const parent = expandPath(news.parent);
186 const ownership = yield* ownershipLabels(id);
187 const displayName = encodeOwnershipLine(
188 ownership,
189 yield* toDisplayName(id, news.displayName, output?.displayName),
190 MAX_DISPLAY_NAME_LENGTH,
191 );
192 const desired: sasportal.SasPortalNode = {
193 displayName,
194 sasUserIds: news.sasUserIds,
195 };
196
197 let current = yield* getNodeNode(news.name ?? output?.name ?? "");
198 if (current === undefined) {
199 const found =
200 (yield* findOwnedNodeNodeNode(id, parent)) ?? (yield* scanOwnedNodeNodeNode(id, parent));
201 current = found?.row;
202 }
203
204 if (current === undefined) {
205 const created = yield* sasportal
206 .createNodesNodesNodes({
207 parent,
208 body: desired,
209 })
210 .pipe(
211 Effect.catchTag("Conflict", () =>
212 findOwnedNodeNodeNode(id, parent).pipe(Effect.map((found) => found?.row)),
213 ),
214 );
215 current = created ?? undefined;
216 }
217
218 if (current === undefined) {
219 return yield* new NodesNodesNodeNotResolved({
220 parent,
221 resourceName: news.name ?? output?.name ?? displayName,
222 });
223 }
224
225 const name = current.name ?? news.name ?? output?.name ?? "";
226 const nameChanged = !sameText(current.displayName, displayName);
227 const usersChanged =
228 news.sasUserIds !== undefined && !sameStringList(current.sasUserIds, news.sasUserIds);
229 if (nameChanged || usersChanged) {
230 current = yield* sasportal.patchNodesNodes({
231 name,
232 updateMask: updateMaskOf(
233 "displayName",
234 news.sasUserIds !== undefined ? "sasUserIds" : undefined,
235 ),
236 body: desired,
237 });
238 }
239
240 return toAttrs(current, parent, env.project);
241 }),
242
243 delete: Effect.fn(function* ({ output }) {
244 if (!output.name) return;
245 yield* retryDelete(sasportal.deleteNodesNodes({ name: output.name })).pipe(
246 Effect.catchTag("NotFound", () => Effect.void),
247 );
248 yield* waitUntilGone(getNodeNode(output.name));
249 }),
250 });