@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 286 lines 9.49 KB
Raw
1 import * as observability from "@distilled.cloud/gcp/observability_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import * as Effect from "effect/Effect";
4 import * as Stream from "effect/Stream";
5 import { GcpEnvironment } from "../Environment.ts";
6 import { alchemyLabelKeys } from "../Labels.ts";
7
8 export const MAX_NAME_LENGTH = 100;
9 export const DEFAULT_TRACE_LOCATION = "global";
10 export const DEFAULT_BUCKET_ID = "_Trace";
11 export const DEFAULT_DATASET_ID = "Spans";
12
13 export const lastSegment = (value: string) => {
14 const trimmed = value.replace(/\/+$/, "");
15 const parts = trimmed.split("/");
16 return parts[parts.length - 1] || trimmed;
17 };
18
19 export const encodeDescription = (
20 labels: Record<string, string>,
21 description: string | undefined,
22 ): string => {
23 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
24 return description ? `${marker}\n${description}` : marker;
25 };
26
27 export const parseDescription = (
28 description: string | undefined,
29 ): {
30 labels: Record<string, string>;
31 description: string | undefined;
32 } => {
33 if (!description?.startsWith("[alchemy ")) {
34 return { labels: {}, description };
35 }
36 const end = description.indexOf("]");
37 if (end < 0) return { labels: {}, description };
38 const labels: Record<string, string> = {};
39 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
40 const eq = part.indexOf("=");
41 if (eq > 0) {
42 labels[part.slice(0, eq)] = part.slice(eq + 1);
43 }
44 }
45 const rest = description.slice(end + 1).replace(/^\n/, "");
46 return { labels, description: rest.length > 0 ? rest : undefined };
47 };
48
49 export const hasOwnershipMarker = (description: string | undefined) =>
50 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
51
52 export const sortedStrings = (values: readonly string[] | undefined) =>
53 [...(values ?? [])].slice().sort();
54
55 export const sameStringList = (
56 left: readonly string[] | undefined,
57 right: readonly string[] | undefined,
58 ) => JSON.stringify(sortedStrings(left)) === JSON.stringify(sortedStrings(right));
59
60 export const normalizeLocation = (location: string | undefined, fallback: string) =>
61 lastSegment(location ?? fallback).toLowerCase();
62
63 export const expandProjectName = (value: string, project: string) => {
64 const trimmed = value.replace(/\/+$/, "");
65 if (trimmed.startsWith("projects/")) return trimmed;
66 if (trimmed.length === 0) return `projects/${project}`;
67 return `projects/${lastSegment(trimmed)}`;
68 };
69
70 export const expandResourceNames = (names: readonly string[] | undefined, project: string) =>
71 (names === undefined || names.length === 0 ? [`projects/${project}`] : [...names]).map((name) =>
72 expandProjectName(name, project),
73 );
74
75 export const toScopeId = (id: string, explicit: string | undefined, existing?: string) =>
76 Effect.gen(function* () {
77 if (explicit !== undefined) return explicit;
78 if (existing !== undefined) return existing;
79 const generated = yield* createPhysicalName({
80 id,
81 maxLength: MAX_NAME_LENGTH,
82 lowercase: true,
83 });
84 return /^[a-z0-9]/.test(generated) ? generated : `s${generated}`.slice(0, MAX_NAME_LENGTH);
85 });
86
87 export const toLinkId = (id: string, explicit: string | undefined, existing?: string) =>
88 Effect.gen(function* () {
89 if (explicit !== undefined) return explicit;
90 if (existing !== undefined) return existing;
91 const generated = yield* createPhysicalName({
92 id,
93 maxLength: MAX_NAME_LENGTH,
94 lowercase: true,
95 delimiter: "_",
96 });
97 const cleaned = generated.replace(/-/g, "_").replace(/[^a-z0-9_]/g, "_");
98 return /^[a-z]/.test(cleaned) ? cleaned : `l${cleaned}`.slice(0, MAX_NAME_LENGTH);
99 });
100
101 export type ParsedLinkName = {
102 project: string;
103 location: string;
104 bucketId: string;
105 datasetId: string;
106 linkId: string;
107 };
108
109 export const parseLinkName = (name: string): ParsedLinkName | undefined => {
110 const match = name.match(
111 /^projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)\/datasets\/([^/]+)\/links\/([^/]+)$/,
112 );
113 if (!match) return undefined;
114 return {
115 project: match[1]!,
116 location: match[2]!,
117 bucketId: match[3]!,
118 datasetId: match[4]!,
119 linkId: match[5]!,
120 };
121 };
122
123 export const parseDatasetName = (name: string) => {
124 const match = name.match(
125 /^projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)\/datasets\/([^/]+)$/,
126 );
127 if (!match) return undefined;
128 return {
129 name: match[0]!,
130 project: match[1]!,
131 location: match[2]!,
132 bucketId: match[3]!,
133 datasetId: match[4]!,
134 };
135 };
136
137 export const parseBucketName = (name: string) => {
138 const match = name.match(/^projects\/([^/]+)\/locations\/([^/]+)\/buckets\/([^/]+)$/);
139 if (!match) return undefined;
140 return {
141 name: match[0]!,
142 project: match[1]!,
143 location: match[2]!,
144 bucketId: match[3]!,
145 };
146 };
147
148 export const parseScopeName = (name: string) => {
149 const match = name.match(/^projects\/([^/]+)\/locations\/([^/]+)\/traceScopes\/([^/]+)$/);
150 if (!match) return undefined;
151 return {
152 project: match[1]!,
153 location: match[2]!,
154 traceScopeId: match[3]!,
155 };
156 };
157
158 export const datasetResourceName = (
159 project: string,
160 location: string,
161 bucketId: string,
162 datasetId: string,
163 ) => `projects/${project}/locations/${location}/buckets/${bucketId}/datasets/${datasetId}`;
164
165 export const linkResourceName = (
166 project: string,
167 location: string,
168 bucketId: string,
169 datasetId: string,
170 linkId: string,
171 ) => `${datasetResourceName(project, location, bucketId, datasetId)}/links/${linkId}`;
172
173 export const scopeResourceName = (project: string, location: string, traceScopeId: string) =>
174 `projects/${project}/locations/${location}/traceScopes/${traceScopeId}`;
175
176 export const resolveDatasetParent = (
177 dataset: string,
178 bucket: string | undefined,
179 location: string,
180 project: string,
181 ) => {
182 const fromDataset = parseDatasetName(dataset);
183 if (fromDataset) return fromDataset;
184 const fromBucket = bucket ? parseBucketName(bucket) : undefined;
185 const bucketId =
186 fromBucket?.bucketId ??
187 (bucket !== undefined && bucket.length > 0 ? lastSegment(bucket) : DEFAULT_BUCKET_ID);
188 const resolvedLocation = fromBucket?.location ?? location;
189 const resolvedProject = fromBucket?.project ?? project;
190 const datasetId = dataset.length > 0 ? lastSegment(dataset) : DEFAULT_DATASET_ID;
191 return {
192 name: datasetResourceName(resolvedProject, resolvedLocation, bucketId, datasetId),
193 project: resolvedProject,
194 location: resolvedLocation,
195 bucketId,
196 datasetId,
197 };
198 };
199
200 export const listLocationIds = (project: string, region: string) =>
201 observability.listProjectsLocations
202 .pages({
203 name: `projects/${project}`,
204 pageSize: 200,
205 })
206 .pipe(
207 Stream.flatMap((page) => Stream.fromIterable(page.locations ?? [])),
208 Stream.map((location) => location.locationId ?? lastSegment(location.name ?? "")),
209 Stream.filter((id) => id.length > 0),
210 Stream.runCollect,
211 Effect.map((chunk) => {
212 const ids = new Set(Array.from(chunk));
213 ids.add(DEFAULT_TRACE_LOCATION);
214 ids.add(region);
215 return [...ids];
216 }),
217 Effect.catchTag("NotFound", () => Effect.succeed([DEFAULT_TRACE_LOCATION, region])),
218 );
219
220 const listBucketsAt = (parent: string) =>
221 observability.listProjectsLocationsBuckets
222 .pages({
223 parent,
224 pageSize: 1000,
225 })
226 .pipe(
227 Stream.flatMap((page) => Stream.fromIterable(page.buckets ?? [])),
228 Stream.filter((bucket) => (bucket.name ?? "").length > 0),
229 Stream.runCollect,
230 Effect.map((chunk) => Array.from(chunk)),
231 Effect.catchTag("NotFound", () => Effect.succeed([] as observability.Bucket[])),
232 );
233
234 export const listProjectBuckets = () =>
235 Effect.gen(function* () {
236 const env = yield* GcpEnvironment.current;
237 const wildcard = yield* listBucketsAt(`projects/${env.project}/locations/-`);
238 if (wildcard.length > 0) return wildcard;
239 const locations = yield* listLocationIds(env.project, env.region);
240 const pages = yield* Effect.forEach(
241 locations,
242 (location) => listBucketsAt(`projects/${env.project}/locations/${location}`),
243 { concurrency: 4 },
244 );
245 const byName = new Map<string, observability.Bucket>();
246 for (const bucket of pages.flat()) {
247 if (bucket.name) byName.set(bucket.name, bucket);
248 }
249 return [...byName.values()];
250 });
251
252 const listDatasetsAt = (parent: string) =>
253 observability.listProjectsLocationsBucketsDatasets
254 .pages({
255 parent,
256 pageSize: 1000,
257 })
258 .pipe(
259 Stream.flatMap((page) => Stream.fromIterable(page.datasets ?? [])),
260 Stream.filter((dataset) => (dataset.name ?? "").length > 0),
261 Stream.runCollect,
262 Effect.map((chunk) => Array.from(chunk)),
263 Effect.catchTag("NotFound", () => Effect.succeed([] as observability.Dataset[])),
264 );
265
266 export const listProjectDatasets = () =>
267 Effect.gen(function* () {
268 const buckets = yield* listProjectBuckets();
269 const pages = yield* Effect.forEach(buckets, (bucket) => listDatasetsAt(bucket.name!), {
270 concurrency: 4,
271 });
272 return pages.flat();
273 });
274
275 export const listLinksAt = (parent: string) =>
276 observability.listProjectsLocationsBucketsDatasetsLinks
277 .pages({
278 parent,
279 pageSize: 1000,
280 })
281 .pipe(
282 Stream.flatMap((page) => Stream.fromIterable(page.links ?? [])),
283 Stream.runCollect,
284 Effect.map((chunk) => Array.from(chunk)),
285 Effect.catchTag("NotFound", () => Effect.succeed([] as observability.Link[])),
286 );