@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 213 lines 6.73 KB
Raw
1 import * as datastore from "@distilled.cloud/gcp/datastore_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import * as Data from "effect/Data";
4 import * as Effect from "effect/Effect";
5 import * as Schedule from "effect/Schedule";
6 import * as Stream from "effect/Stream";
7 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
8
9 export const DEFAULT_ANCESTOR = "NONE";
10 export const DEFAULT_DIRECTION = "ASCENDING";
11 export const MAX_KIND_LENGTH = 63;
12
13 export class IndexNotResolved extends Data.TaggedError("GCP.Datastore.IndexNotResolved")<{
14 indexId: string;
15 }> {}
16
17 export class IndexStillExists extends Data.TaggedError("GCP.Datastore.IndexStillExists")<{
18 indexId: string;
19 }> {}
20
21 export const stringFromMap = (
22 map: datastore.DocumentMap | undefined,
23 key: string,
24 ): string | undefined => {
25 const value = map?.[key];
26 if (typeof value === "string" && value.length > 0) return value;
27 if (value !== null && typeof value === "object") {
28 const record = value as { stringValue?: unknown };
29 if (typeof record.stringValue === "string" && record.stringValue.length > 0) {
30 return record.stringValue;
31 }
32 }
33 return undefined;
34 };
35
36 export const normalizeEnum = (value: string | undefined, fallback: string) => {
37 const next = (value ?? fallback).toUpperCase();
38 return next.endsWith("_UNSPECIFIED") ? fallback : next;
39 };
40
41 export const rfc1035Kind = (name: string, maxLength = MAX_KIND_LENGTH) => {
42 let next = name
43 .replace(/[^a-zA-Z0-9_]/g, "_")
44 .replace(/_+/g, "_")
45 .replace(/^_+|_+$/g, "");
46 if (!/^[A-Za-z]/.test(next)) next = `K${next}`;
47 next = next.slice(0, maxLength).replace(/_+$/g, "");
48 if (next.length === 0) next = "Kind";
49 return next.slice(0, maxLength);
50 };
51
52 export const toKind = (id: string, kind: string | undefined, existing?: string) =>
53 Effect.gen(function* () {
54 if (kind !== undefined && kind.length > 0) return kind;
55 if (existing !== undefined && existing.length > 0) return existing;
56 return rfc1035Kind(
57 yield* createPhysicalName({
58 id,
59 maxLength: MAX_KIND_LENGTH,
60 lowercase: false,
61 }),
62 );
63 });
64
65 export type IndexedProperty = {
66 /** Property name to index. */
67 name: string;
68 /**
69 * Sort direction.
70 * @default "ASCENDING"
71 */
72 direction?: datastore.GoogleDatastoreAdminV1IndexedPropertyDirectionEnum | (string & {});
73 };
74
75 export const propertyOf = (
76 property: IndexedProperty | datastore.GoogleDatastoreAdminV1IndexedProperty,
77 ): IndexedProperty => ({
78 name: property.name ?? "",
79 direction: normalizeEnum(property.direction, DEFAULT_DIRECTION),
80 });
81
82 export const propertiesKey = (
83 properties:
84 | readonly IndexedProperty[]
85 | readonly datastore.GoogleDatastoreAdminV1IndexedProperty[]
86 | undefined,
87 ) => JSON.stringify((properties ?? []).map(propertyOf));
88
89 export const desiredBody = (input: {
90 kind: string;
91 ancestor?: string;
92 properties: IndexedProperty[];
93 }): datastore.GoogleDatastoreAdminV1Index => ({
94 kind: input.kind,
95 ancestor: normalizeEnum(input.ancestor, DEFAULT_ANCESTOR),
96 properties: input.properties.map(propertyOf),
97 });
98
99 export const matchesDesired = (
100 index: datastore.GoogleDatastoreAdminV1Index,
101 input: {
102 kind: string;
103 ancestor?: string;
104 properties: IndexedProperty[];
105 },
106 ) => {
107 const desired = desiredBody(input);
108 return (
109 (index.kind ?? "") === desired.kind &&
110 normalizeEnum(index.ancestor, DEFAULT_ANCESTOR) ===
111 normalizeEnum(desired.ancestor, DEFAULT_ANCESTOR) &&
112 propertiesKey(index.properties ?? []) === propertiesKey(desired.properties ?? [])
113 );
114 };
115
116 export const toAttrs = (index: datastore.GoogleDatastoreAdminV1Index, project: string) => {
117 const indexId = index.indexId ?? "";
118 return {
119 name:
120 indexId.length > 0
121 ? `projects/${index.projectId ?? project}/indexes/${indexId}`
122 : `projects/${index.projectId ?? project}/indexes`,
123 indexId,
124 project: index.projectId ?? project,
125 kind: index.kind ?? "",
126 ancestor: index.ancestor,
127 state: index.state,
128 properties: (index.properties ?? []).map(propertyOf),
129 };
130 };
131
132 export const getById = (projectId: string, indexId: string) =>
133 datastore
134 .getProjectsIndexes({ projectId, indexId })
135 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
136
137 export const listIndexes = (projectId: string) =>
138 datastore.listProjectsIndexes
139 .pages({
140 projectId,
141 })
142 .pipe(
143 Stream.runCollect,
144 Effect.map((pages) => Array.from(pages).flatMap((page) => page.indexes ?? [])),
145 );
146
147 /** Wait for a Datastore admin operation (index builds can take minutes). */
148 export const waitForOperation = (operation: datastore.GoogleLongrunningOperation) =>
149 waitForGcpOperation(operation, (name) => datastore.getProjectsOperations({ name }), {
150 budget: "20 minutes",
151 }).pipe(
152 // ALREADY_EXISTS (6): an identical index already exists.
153 Effect.catchIf(
154 (error) => error._tag === "GCP.OperationFailed" && error.code === 6,
155 () => Effect.void,
156 ),
157 // Re-read the finished operation for its typed response/metadata.
158 Effect.flatMap(() =>
159 operation.name === undefined || operation.name.length === 0
160 ? Effect.succeed(operation)
161 : datastore.getProjectsOperations({ name: operation.name }),
162 ),
163 );
164
165 export const indexIdFromOperation = (
166 operation: datastore.GoogleLongrunningOperation | undefined,
167 ): string | undefined => {
168 if (operation === undefined) return undefined;
169 return (
170 stringFromMap(operation.response, "indexId") ??
171 stringFromMap(operation.metadata, "indexId") ??
172 stringFromMap(operation.metadata, "index")
173 );
174 };
175
176 export const waitUntilExists = (projectId: string, indexId: string) =>
177 getById(projectId, indexId).pipe(
178 Effect.flatMap((index) =>
179 index ? Effect.succeed(index) : Effect.fail(new IndexNotResolved({ indexId })),
180 ),
181 Effect.retry({
182 while: (error) => error._tag === "GCP.Datastore.IndexNotResolved",
183 times: 8,
184 schedule: Schedule.spaced("2 seconds"),
185 }),
186 );
187
188 export const waitUntilGone = (projectId: string, indexId: string) =>
189 getById(projectId, indexId).pipe(
190 Effect.filterOrFail(
191 (index) => index === undefined,
192 () => new IndexStillExists({ indexId }),
193 ),
194 Effect.retry({
195 while: (error) => error._tag === "GCP.Datastore.IndexStillExists",
196 times: 24,
197 schedule: Schedule.spaced("5 seconds"),
198 }),
199 Effect.asVoid,
200 );
201
202 export const waitUntilDeletable = (projectId: string, indexId: string) =>
203 getById(projectId, indexId).pipe(
204 Effect.filterOrFail(
205 (index) => index === undefined || index.state !== "DELETING",
206 () => new IndexNotResolved({ indexId }),
207 ),
208 Effect.retry({
209 while: (error) => error._tag === "GCP.Datastore.IndexNotResolved",
210 times: 8,
211 schedule: Schedule.spaced("2 seconds"),
212 }),
213 );