@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 365 lines 12.1 KB
Raw
1 import * as speech from "@distilled.cloud/gcp/speech_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 { alchemyLabelKeys } from "../Labels.ts";
8
9 /** Speech-to-Text v1 Adaptation is served from the global endpoint. */
10 export const DEFAULT_LOCATION = "global";
11 /** The global Speech endpoint only serves the `global` location. */
12 export const LIST_LOCATIONS = ["global"] as const;
13 export const MAX_ID_LENGTH = 63;
14 export const MAX_ITEM_LENGTH = 100;
15
16 export class ResourceNotResolved extends Data.TaggedError("GCP.Speech.ResourceNotResolved")<{
17 resourceName: string;
18 }> {}
19
20 export const parseOwnershipMarker = (
21 text: string | undefined,
22 ): {
23 labels: Record<string, string>;
24 text: string | undefined;
25 } => {
26 const value = text?.trim() ?? "";
27 if (!value.startsWith("alc ")) {
28 return { labels: {}, text };
29 }
30 const parts = value.slice("alc ".length).trim().split(/\s+/);
31 const labels: Record<string, string> = {};
32 if (parts[0]) labels[alchemyLabelKeys.stack] = parts[0]!;
33 if (parts[1]) labels[alchemyLabelKeys.stage] = parts[1]!;
34 if (parts[2]) labels[alchemyLabelKeys.id] = parts[2]!;
35 return { labels, text: undefined };
36 };
37
38 export const isOwnershipMarker = (text: string | undefined) =>
39 (text ?? "").trim().startsWith("alc ");
40
41 export const hasOwnershipMarker = (text: string | undefined) => {
42 const { labels } = parseOwnershipMarker(text);
43 return Object.keys(labels).some((key) => key.startsWith("alchemy-"));
44 };
45
46 export const lastSegment = (value: string) => {
47 const trimmed = value.replace(/\/+$/, "");
48 const parts = trimmed.split("/");
49 return parts[parts.length - 1] || trimmed;
50 };
51
52 export const normalizeLocation = (location: string | undefined) =>
53 lastSegment(location ?? DEFAULT_LOCATION).toLowerCase();
54
55 export const locationParent = (project: string, location: string | undefined) =>
56 `projects/${project}/locations/${normalizeLocation(location)}`;
57
58 export const parseResourceName = (name: string, collection: string) => {
59 const parts = name.split("/").filter((part) => part.length > 0);
60 const collectionAt = parts.lastIndexOf(collection);
61 const locationsAt = parts.lastIndexOf("locations");
62 const projectsAt = parts.lastIndexOf("projects");
63 return {
64 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
65 location:
66 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION,
67 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
68 };
69 };
70
71 export const resourceNameOf = (
72 project: string,
73 location: string,
74 collection: string,
75 id: string,
76 ) => {
77 if (id.length === 0) return "";
78 if (id.includes(`/${collection}/`)) return id.replace(/\/+$/, "");
79 return `${locationParent(project, location)}/${collection}/${lastSegment(id)}`;
80 };
81
82 export const sameText = (left: string | undefined, right: string | undefined) =>
83 (left ?? "") === (right ?? "");
84
85 export const sameNumber = (left: number | undefined, right: number | undefined) =>
86 (left ?? 0) === (right ?? 0);
87
88 export const canonical = (value: unknown): unknown => {
89 if (value === undefined || value === null) return undefined;
90 if (typeof value === "boolean" || typeof value === "number") return value;
91 if (typeof value === "string") {
92 return value.length === 0 ? undefined : value;
93 }
94 if (Array.isArray(value)) {
95 const items = value.map(canonical).filter((item) => item !== undefined);
96 return items.length === 0 ? undefined : items;
97 }
98 if (typeof value === "object") {
99 const entries = Object.entries(value as Record<string, unknown>)
100 .map(([key, item]) => [key, canonical(item)] as const)
101 .filter(([, item]) => item !== undefined)
102 .sort(([left], [right]) => left.localeCompare(right));
103 if (entries.length === 0) return undefined;
104 return Object.fromEntries(entries);
105 }
106 return undefined;
107 };
108
109 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
110
111 export const sameJson = (left: unknown, right: unknown) => fingerprint(left) === fingerprint(right);
112
113 export const updateMaskOf = (...fields: Array<string | undefined>) =>
114 fields.filter((field): field is string => field !== undefined).join(",");
115
116 export const replaceOnIdentity = (input: {
117 previousId?: string;
118 nextId?: string;
119 previousLocation?: string;
120 nextLocation?: string;
121 }) => {
122 const previousLocation =
123 input.previousLocation !== undefined ? normalizeLocation(input.previousLocation) : undefined;
124 const nextLocation =
125 input.nextLocation !== undefined ? normalizeLocation(input.nextLocation) : undefined;
126 const replace =
127 (input.previousId !== undefined &&
128 input.nextId !== undefined &&
129 input.nextId !== input.previousId) ||
130 (previousLocation !== undefined &&
131 nextLocation !== undefined &&
132 previousLocation !== nextLocation);
133 if (!replace) return undefined;
134 const samePhysical =
135 (previousLocation === undefined || previousLocation === nextLocation) &&
136 input.previousId !== undefined &&
137 input.nextId === input.previousId;
138 return {
139 action: "replace" as const,
140 deleteFirst: samePhysical,
141 };
142 };
143
144 export const toPhysicalId = (
145 id: string,
146 explicit: string | undefined,
147 existing: string | undefined,
148 ) =>
149 Effect.gen(function* () {
150 if (explicit !== undefined && explicit.length > 0) {
151 return lastSegment(explicit);
152 }
153 if (existing !== undefined && existing.length > 0) {
154 return existing;
155 }
156 const generated = yield* createPhysicalName({
157 id,
158 maxLength: MAX_ID_LENGTH,
159 lowercase: true,
160 });
161 let next = generated.replace(/-+$/g, "").replace(/^-+/g, "");
162 if (!/^[a-z]/.test(next)) {
163 next = `s${next}`.slice(0, MAX_ID_LENGTH);
164 }
165 if (next.length < 4) {
166 next = `${next}xxxx`.slice(0, MAX_ID_LENGTH);
167 }
168 if (!/[a-z0-9]$/.test(next)) {
169 next = `${next.slice(0, Math.max(0, MAX_ID_LENGTH - 1))}x`;
170 }
171 return next;
172 });
173
174 export type ClassItem = {
175 value: string;
176 };
177
178 export type Phrase = {
179 value: string;
180 boost?: number;
181 };
182
183 export const markerFromItems = (items: readonly { value?: string }[] | null | undefined) =>
184 (items ?? []).find((item) => isOwnershipMarker(item.value))?.value;
185
186 export const markerFromPhrases = (phrases: readonly { value?: string }[] | null | undefined) =>
187 (phrases ?? []).find((phrase) => isOwnershipMarker(phrase.value))?.value;
188
189 export const stripOwnershipItems = (
190 items: readonly { value?: string }[] | null | undefined,
191 ): ClassItem[] =>
192 (items ?? [])
193 .filter((item) => !isOwnershipMarker(item.value))
194 .map((item) => ({ value: item.value ?? "" }))
195 .filter((item) => item.value.length > 0);
196
197 export const stripOwnershipPhrases = (
198 phrases: readonly { value?: string; boost?: number }[] | null | undefined,
199 ): Phrase[] =>
200 (phrases ?? [])
201 .filter((phrase) => !isOwnershipMarker(phrase.value))
202 .map((phrase) => ({
203 value: phrase.value ?? "",
204 boost: phrase.boost,
205 }))
206 .filter((phrase) => phrase.value.length > 0);
207
208 export const sameItems = (
209 left: readonly { value?: string }[] | undefined,
210 right: readonly { value?: string }[] | undefined,
211 ) =>
212 sameJson(
213 stripOwnershipItems(left)
214 .map((item) => item.value)
215 .sort(),
216 stripOwnershipItems(right)
217 .map((item) => item.value)
218 .sort(),
219 );
220
221 export const samePhrases = (
222 left: readonly { value?: string; boost?: number }[] | undefined,
223 right: readonly { value?: string; boost?: number }[] | undefined,
224 ) =>
225 sameJson(
226 stripOwnershipPhrases(left)
227 .map((phrase) => ({
228 value: phrase.value,
229 boost: phrase.boost ?? 0,
230 }))
231 .sort((a, b) => a.value.localeCompare(b.value)),
232 stripOwnershipPhrases(right)
233 .map((phrase) => ({
234 value: phrase.value,
235 boost: phrase.boost ?? 0,
236 }))
237 .sort((a, b) => a.value.localeCompare(b.value)),
238 );
239
240 const emptyList = <A>() => Effect.succeed([] as A[]);
241
242 export class DeleteNotConfirmed extends Data.TaggedError("GCP.Speech.DeleteNotConfirmed")<{}> {}
243
244 /** Poll until the resource is gone; fails if it is still readable after ~60s. */
245 export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>) =>
246 get.pipe(
247 Effect.repeat({
248 schedule: Schedule.spaced("2 seconds"),
249 until: (value) => value === undefined,
250 times: 30,
251 }),
252 Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> =>
253 value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()),
254 ),
255 );
256
257 const collectPages = <Page, Item, E, R>(
258 pages: Stream.Stream<Page, E, R>,
259 items: (page: Page) => readonly Item[] | null | undefined,
260 ) =>
261 pages.pipe(
262 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
263 Stream.runCollect,
264 Effect.map((chunk): Item[] => Array.from(chunk)),
265 );
266
267 export const getCustomClass = (name: string) =>
268 name.length === 0
269 ? Effect.undefined
270 : speech
271 .getProjectsLocationsCustomClasses({ name })
272 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
273
274 export const getPhraseSet = (name: string) =>
275 name.length === 0
276 ? Effect.undefined
277 : speech
278 .getProjectsLocationsPhraseSets({ name })
279 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
280
281 export const listCustomClassesAt = (parent: string) =>
282 parent.length === 0
283 ? emptyList<speech.CustomClass>()
284 : collectPages(
285 speech.listProjectsLocationsCustomClasses.pages({
286 parent,
287 pageSize: 100,
288 }),
289 (page) => page.customClasses,
290 ).pipe(Effect.catchTag("NotFound", () => emptyList<speech.CustomClass>()));
291
292 export const listPhraseSetsAt = (parent: string) =>
293 parent.length === 0
294 ? emptyList<speech.PhraseSet>()
295 : collectPages(
296 speech.listProjectsLocationsPhraseSets.pages({
297 parent,
298 pageSize: 100,
299 }),
300 (page) => page.phraseSets,
301 ).pipe(Effect.catchTag("NotFound", () => emptyList<speech.PhraseSet>()));
302
303 const listLocationParents = (project: string) =>
304 LIST_LOCATIONS.map((location) => locationParent(project, location));
305
306 export const listProjectCustomClasses = (project: string) =>
307 Effect.gen(function* () {
308 const groups = yield* Effect.forEach(listLocationParents(project), listCustomClassesAt, {
309 concurrency: 2,
310 });
311 const seen = new Set<string>();
312 const classes: speech.CustomClass[] = [];
313 for (const customClass of groups.flat()) {
314 const name = customClass.name ?? "";
315 if (name.length === 0 || seen.has(name)) continue;
316 seen.add(name);
317 classes.push(customClass);
318 }
319 return classes;
320 });
321
322 export const listProjectPhraseSets = (project: string) =>
323 Effect.gen(function* () {
324 const groups = yield* Effect.forEach(listLocationParents(project), listPhraseSetsAt, {
325 concurrency: 2,
326 });
327 const seen = new Set<string>();
328 const phraseSets: speech.PhraseSet[] = [];
329 for (const phraseSet of groups.flat()) {
330 const name = phraseSet.name ?? "";
331 if (name.length === 0 || seen.has(name)) continue;
332 seen.add(name);
333 phraseSets.push(phraseSet);
334 }
335 return phraseSets;
336 });
337
338 /**
339 * Classes and phrase sets have no labels. Only ones stamped with a legacy
340 * `alc …` ownership item by earlier Alchemy versions are discoverable here;
341 * current ones are tracked through state alone.
342 */
343 export const listOwnedCustomClasses = (project: string) =>
344 listProjectCustomClasses(project).pipe(
345 Effect.map((classes) =>
346 classes.filter((customClass) => hasOwnershipMarker(markerFromItems(customClass.items))),
347 ),
348 );
349
350 export const listOwnedPhraseSets = (project: string) =>
351 listProjectPhraseSets(project).pipe(
352 Effect.map((phraseSets) =>
353 phraseSets.filter((phraseSet) => hasOwnershipMarker(markerFromPhrases(phraseSet.phrases))),
354 ),
355 );
356
357 export const deleteCustomClass = (name: string) =>
358 speech
359 .deleteProjectsLocationsCustomClasses({ name })
360 .pipe(Effect.catchTag("NotFound", () => Effect.void));
361
362 export const deletePhraseSet = (name: string) =>
363 speech
364 .deleteProjectsLocationsPhraseSets({ name })
365 .pipe(Effect.catchTag("NotFound", () => Effect.void));