@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 291 lines 10.1 KB
Raw
1 import * as spanner from "@distilled.cloud/gcp/spanner_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5 import * as Stream from "effect/Stream";
6 import { ALCHEMY_LABEL_PREFIX } from "../Labels.ts";
7 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
8
9 /** Default instance config: the regional config of the stack's GCP region. */
10 export const defaultConfigId = (region: string) => `regional-${region}`;
11 export const DEFAULT_PROCESSING_UNITS = 100;
12 export const DEFAULT_PARTITION_PROCESSING_UNITS = 1000;
13 export const MAX_INSTANCE_ID_LENGTH = 64;
14 export const MAX_INSTANCE_CONFIG_ID_LENGTH = 64;
15 export const MAX_BACKUP_ID_LENGTH = 60;
16 export const MAX_BACKUP_SCHEDULE_ID_LENGTH = 60;
17 export const MAX_PARTITION_ID_LENGTH = 64;
18 export const MAX_DISPLAY_NAME_LENGTH = 30;
19 export const MIN_DISPLAY_NAME_LENGTH = 4;
20 export const CUSTOM_CONFIG_PREFIX = "custom-";
21
22 export const lastSegment = (value: string) => {
23 const trimmed = value.replace(/\/+$/, "");
24 const parts = trimmed.split("/");
25 return parts[parts.length - 1] || trimmed;
26 };
27
28 export const parseResourceName = (name: string) => {
29 const parts = name.split("/").filter((part) => part.length > 0);
30 const after = (segment: string) => {
31 const at = parts.lastIndexOf(segment);
32 return at >= 0 && parts[at + 1] ? parts[at + 1]! : "";
33 };
34 const project = after("projects");
35 const instanceId = after("instances");
36 const databaseId = after("databases");
37 return {
38 project,
39 instanceId,
40 databaseId,
41 backupId: after("backups"),
42 backupScheduleId: after("backupSchedules"),
43 instancePartitionId: after("instancePartitions"),
44 instanceConfigId: after("instanceConfigs"),
45 instance: project && instanceId ? `projects/${project}/instances/${instanceId}` : "",
46 database:
47 project && instanceId && databaseId
48 ? `projects/${project}/instances/${instanceId}/databases/${databaseId}`
49 : "",
50 };
51 };
52
53 export const instanceIdOf = (value: string) =>
54 value.includes("/instances/") ? parseResourceName(value).instanceId : lastSegment(value);
55
56 export const databaseIdOf = (value: string) =>
57 value.includes("/databases/") ? parseResourceName(value).databaseId : lastSegment(value);
58
59 export const configIdOf = (config: string | undefined, region: string) =>
60 lastSegment(config ?? defaultConfigId(region)).toLowerCase();
61
62 export const instanceName = (project: string, instanceId: string) =>
63 `projects/${project}/instances/${instanceId}`;
64
65 export const instanceNameOf = (project: string, instance: string) =>
66 instance.includes("/instances/")
67 ? instanceName(
68 parseResourceName(instance).project || project,
69 parseResourceName(instance).instanceId,
70 )
71 : instanceName(project, lastSegment(instance));
72
73 export const databaseName = (project: string, instanceId: string, databaseId: string) =>
74 `${instanceName(project, instanceId)}/databases/${databaseId}`;
75
76 export const databaseNameOf = (project: string, instance: string, database: string) => {
77 if (database.includes("/databases/")) {
78 const parsed = parseResourceName(database);
79 return databaseName(
80 parsed.project || project,
81 parsed.instanceId || instanceIdOf(instance),
82 parsed.databaseId,
83 );
84 }
85 return databaseName(project, instanceIdOf(instance), lastSegment(database));
86 };
87
88 export const backupName = (project: string, instanceId: string, backupId: string) =>
89 `${instanceName(project, instanceId)}/backups/${backupId}`;
90
91 export const backupScheduleName = (
92 project: string,
93 instanceId: string,
94 databaseId: string,
95 scheduleId: string,
96 ) => `${databaseName(project, instanceId, databaseId)}/backupSchedules/${scheduleId}`;
97
98 export const instanceConfigName = (project: string, configId: string) =>
99 `projects/${project}/instanceConfigs/${configId}`;
100
101 export const configNameOf = (project: string, config: string | undefined, region: string) => {
102 const raw = (config ?? defaultConfigId(region)).trim();
103 if (raw.includes("/")) return raw;
104 return instanceConfigName(project, raw);
105 };
106
107 export const instancePartitionName = (project: string, instanceId: string, partitionId: string) =>
108 `${instanceName(project, instanceId)}/instancePartitions/${partitionId}`;
109
110 export const toSpannerId = (name: string, maxLength: number) => {
111 let next = name
112 .toLowerCase()
113 .replace(/[^a-z0-9-]/g, "-")
114 .replace(/-+/g, "-");
115 next = next.replace(/^-+/, "").replace(/-+$/g, "");
116 if (!/^[a-z]/.test(next)) next = `s${next}`;
117 next = next.slice(0, maxLength).replace(/-+$/g, "");
118 if (next.length < 2) next = `${next}xx`.slice(0, maxLength);
119 return next;
120 };
121
122 export const toCustomConfigId = (name: string) => {
123 const maxBody = MAX_INSTANCE_CONFIG_ID_LENGTH - CUSTOM_CONFIG_PREFIX.length;
124 const body = toSpannerId(name, maxBody);
125 if (body.startsWith(CUSTOM_CONFIG_PREFIX)) {
126 return body.slice(0, MAX_INSTANCE_CONFIG_ID_LENGTH);
127 }
128 let next = `${CUSTOM_CONFIG_PREFIX}${body}`;
129 next = next.slice(0, MAX_INSTANCE_CONFIG_ID_LENGTH).replace(/-+$/g, "");
130 if (!/[a-z0-9]$/.test(next)) {
131 next = `${next.slice(0, MAX_INSTANCE_CONFIG_ID_LENGTH - 1)}x`;
132 }
133 return next;
134 };
135
136 export const toPhysicalId = (
137 id: string,
138 explicit: string | undefined,
139 existing: string | undefined,
140 maxLength: number,
141 ) =>
142 Effect.gen(function* () {
143 if (explicit !== undefined) return explicit;
144 if (existing !== undefined) return existing;
145 return toSpannerId(
146 yield* createPhysicalName({
147 id,
148 maxLength,
149 lowercase: true,
150 }),
151 maxLength,
152 );
153 });
154
155 export const toConfigId = (id: string, explicit: string | undefined, existing?: string) =>
156 Effect.gen(function* () {
157 if (explicit !== undefined) {
158 return explicit.startsWith(CUSTOM_CONFIG_PREFIX) ? explicit : toCustomConfigId(explicit);
159 }
160 if (existing !== undefined) return existing;
161 return toCustomConfigId(
162 yield* createPhysicalName({
163 id,
164 maxLength: MAX_INSTANCE_CONFIG_ID_LENGTH,
165 lowercase: true,
166 }),
167 );
168 });
169
170 export const displayNameOf = (sourceId: string, displayName?: string) => {
171 const source = (displayName ?? sourceId).trim();
172 let next = source.slice(0, MAX_DISPLAY_NAME_LENGTH);
173 if (next.length < MIN_DISPLAY_NAME_LENGTH) {
174 next = `${next}inst`.slice(0, MAX_DISPLAY_NAME_LENGTH);
175 }
176 return next;
177 };
178
179 export const normalizeEnum = (value: string | undefined, fallback: string) => {
180 const next = (value ?? fallback).toUpperCase();
181 return next.endsWith("_UNSPECIFIED") ? fallback : next;
182 };
183
184 export const hasAlchemyPrefix = (labels: Record<string, string | undefined> | null | undefined) =>
185 Object.keys(labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX));
186
187 const getOperation = (name: string) => {
188 if (name.includes("/instanceConfigs/")) {
189 return spanner.getProjectsInstanceConfigsOperations({ name });
190 }
191 if (name.includes("/instancePartitions/")) {
192 return spanner.getProjectsInstancesInstancePartitionsOperations({ name });
193 }
194 if (name.includes("/backups/")) {
195 return spanner.getProjectsInstancesBackupsOperations({ name });
196 }
197 if (name.includes("/databases/")) {
198 return spanner.getProjectsInstancesDatabasesOperations({ name });
199 }
200 return spanner.getProjectsInstancesOperations({ name });
201 };
202
203 /**
204 * Wait for a Spanner operation. Instance and database provisioning can take
205 * many minutes, so the budget is generous.
206 */
207 export const waitForOperation = (
208 operation: spanner.Operation,
209 options?: { notFoundOk?: boolean; alreadyExistsOk?: boolean },
210 ) =>
211 waitForGcpOperation(
212 operation,
213 (name) =>
214 getOperation(name).pipe(
215 Effect.catchTag("NotFound", (error) =>
216 options?.notFoundOk === true
217 ? Effect.succeed<spanner.Operation>({ name, done: true })
218 : Effect.fail(error),
219 ),
220 ),
221 { budget: "30 minutes" },
222 ).pipe(
223 // ALREADY_EXISTS (6) / NOT_FOUND (5) when the caller tolerates them.
224 Effect.catchIf(
225 (error) =>
226 error._tag === "GCP.OperationFailed" &&
227 ((options?.alreadyExistsOk === true && error.code === 6) ||
228 (options?.notFoundOk === true && error.code === 5)),
229 () => Effect.void,
230 ),
231 );
232
233 export const retryConcurrentChanges = <A, E extends { readonly _tag: string }, R>(
234 effect: Effect.Effect<A, E, R>,
235 ) =>
236 effect.pipe(
237 Effect.retry({
238 while: (error) => error._tag === "Conflict",
239 times: 8,
240 schedule: Schedule.spaced("5 seconds"),
241 }),
242 );
243
244 export const getInstanceByName = (name: string) =>
245 spanner.getProjectsInstances({ name }).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
246
247 export const listAlchemyInstances = (project: string) =>
248 spanner.listProjectsInstances
249 .pages({
250 parent: `projects/${project}`,
251 pageSize: 1000,
252 })
253 .pipe(
254 Stream.flatMap((page) => Stream.fromIterable(page.instances ?? [])),
255 Stream.filter((instance) => hasAlchemyPrefix(instance.labels)),
256 Stream.runCollect,
257 Effect.map((chunk) => Array.from(chunk)),
258 );
259
260 export const listAlchemyDatabases = (project: string) =>
261 Effect.gen(function* () {
262 const instances = yield* listAlchemyInstances(project);
263 const pages = yield* Effect.forEach(
264 instances,
265 (instance) => {
266 const parent = instance.name;
267 if (parent === undefined || parent.length === 0) {
268 return Effect.succeed([] as spanner.Database[]);
269 }
270 return spanner.listProjectsInstancesDatabases
271 .pages({
272 parent,
273 pageSize: 1000,
274 })
275 .pipe(
276 Stream.flatMap((page) => Stream.fromIterable(page.databases ?? [])),
277 Stream.runCollect,
278 Effect.map((chunk) => Array.from(chunk)),
279 // The instance was deleted while listing.
280 Effect.catchTag("NotFound", () => Effect.succeed([] as spanner.Database[])),
281 );
282 },
283 { concurrency: 4 },
284 );
285 return pages.flat();
286 });
287
288 export const parentOwned = (instanceNameValue: string) =>
289 getInstanceByName(instanceNameValue).pipe(
290 Effect.map((instance) => (instance === undefined ? true : hasAlchemyPrefix(instance.labels))),
291 );