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, 04:45 UTC
| 1 | import * as logging from "@distilled.cloud/gcp/logging_v2"; |
| 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 * as Stream from "effect/Stream"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | DEFAULT_LOCATION, |
| 13 | createOwnership, |
| 14 | encodeDescription, |
| 15 | hasOwnershipMarker, |
| 16 | jsonEqual, |
| 17 | lastSegment, |
| 18 | locationParent, |
| 19 | ownedBy, |
| 20 | parseDescription, |
| 21 | parseLoggingName, |
| 22 | scopeParent, |
| 23 | toPhysicalId, |
| 24 | } from "./internal.ts"; |
| 25 | |
| 26 | export type FolderLogScopeProps = { |
| 27 | /** |
| 28 | * Folder id (`folders/{folder}` or the numeric id). When omitted, the |
| 29 | * stack project is used. Immutable — changing it replaces the log |
| 30 | * scope. |
| 31 | */ |
| 32 | folderId?: string; |
| 33 | /** |
| 34 | * Location of the log scope. Log scopes are only available in `global`. |
| 35 | * Immutable — changing it replaces the log scope. |
| 36 | * @default "global" |
| 37 | */ |
| 38 | location?: string; |
| 39 | /** |
| 40 | * Log scope id (the `{logScope}` segment of |
| 41 | * `{parent}/locations/{location}/logScopes/{logScope}`). If omitted, a |
| 42 | * unique name is generated. Limited to 100 characters: letters, digits, |
| 43 | * underscores, hyphens, periods; first character must be alphanumeric. |
| 44 | * Immutable — changing it replaces the log scope. |
| 45 | */ |
| 46 | logScopeId?: string; |
| 47 | /** |
| 48 | * Parent resources to read log entries from. Project names |
| 49 | * (`projects/{project}`) and/or log views |
| 50 | * (`projects/{project}/locations/{location}/buckets/{bucket}/views/{view}`). |
| 51 | * Organizations and folders are not supported. Max 5 projects and 100 |
| 52 | * resources total. |
| 53 | */ |
| 54 | resourceNames: string[]; |
| 55 | /** |
| 56 | * Human-readable description (max 8000 characters). Log scopes have no |
| 57 | * labels field, so Alchemy ownership is stored in a `[alchemy …]` prefix |
| 58 | * and stripped from attributes. |
| 59 | */ |
| 60 | description?: string; |
| 61 | }; |
| 62 | |
| 63 | export type FolderLogScope = Resource< |
| 64 | "GCP.Logging.FolderLogScope", |
| 65 | FolderLogScopeProps, |
| 66 | { |
| 67 | /** Full resource name `{parent}/locations/{location}/logScopes/{logScopeId}`. */ |
| 68 | name: string; |
| 69 | /** Log scope id (last path segment). */ |
| 70 | logScopeId: string; |
| 71 | /** Parent resource (`folders/{folder}` or `projects/{project}`). */ |
| 72 | parent: string; |
| 73 | /** Folder id when the parent is a folder. */ |
| 74 | folderId: string | undefined; |
| 75 | /** Location (`global`). */ |
| 76 | location: string; |
| 77 | /** Resource names included in the scope. */ |
| 78 | resourceNames: string[]; |
| 79 | /** User description with the Alchemy ownership prefix stripped. */ |
| 80 | description: string | undefined; |
| 81 | /** RFC3339 creation timestamp. */ |
| 82 | createTime: string | undefined; |
| 83 | /** RFC3339 last-update timestamp. */ |
| 84 | updateTime: string | undefined; |
| 85 | }, |
| 86 | never, |
| 87 | Providers |
| 88 | >; |
| 89 | |
| 90 | /** |
| 91 | * A Cloud Logging log scope grouping projects and views to read together. |
| 92 | * |
| 93 | * Log scopes exist only in `global`. They have no labels field, so Alchemy |
| 94 | * stamps ownership into the description for `list` / nuke. `folderId`, |
| 95 | * `logScopeId`, and `location` are identity — changing any of them |
| 96 | * replaces the log scope. `resourceNames` and `description` update in |
| 97 | * place. The `_Default` log scope cannot be modified or deleted. |
| 98 | * |
| 99 | * ### Creating a Folder Log Scope |
| 100 | * **Example:** Scope the stack project |
| 101 | * ```typescript |
| 102 | * const scope = yield* GCP.Logging.FolderLogScope("App", { |
| 103 | * resourceNames: ["projects/my-project"], |
| 104 | * description: "application logs", |
| 105 | * }); |
| 106 | * ``` |
| 107 | * |
| 108 | * ### Updating a Folder Log Scope |
| 109 | * **Example:** Change the description |
| 110 | * ```typescript |
| 111 | * const scope = yield* GCP.Logging.FolderLogScope("App", { |
| 112 | * resourceNames: ["projects/my-project"], |
| 113 | * description: "updated application logs", |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * @resource |
| 118 | * @category Logging |
| 119 | */ |
| 120 | export const FolderLogScope = Resource<FolderLogScope>("GCP.Logging.FolderLogScope"); |
| 121 | |
| 122 | export class FolderLogScopeNotResolved extends Data.TaggedError( |
| 123 | "GCP.Logging.FolderLogScopeNotResolved", |
| 124 | )<{ |
| 125 | resourceName: string; |
| 126 | }> {} |
| 127 | |
| 128 | const resourceName = (parent: string, location: string, logScopeId: string) => |
| 129 | `${locationParent(parent, location)}/logScopes/${logScopeId}`; |
| 130 | |
| 131 | const folderIdOf = (parent: string) => |
| 132 | parent.startsWith("folders/") ? lastSegment(parent) : undefined; |
| 133 | |
| 134 | const sortedNames = (names: readonly string[] | undefined) => [...(names ?? [])].slice().sort(); |
| 135 | |
| 136 | const toAttrs = (scope: logging.LogScope, parent: string, location: string) => { |
| 137 | const parsed = parseLoggingName(scope.name ?? ""); |
| 138 | const logScopeId = parsed.logScopeId ?? lastSegment(scope.name ?? ""); |
| 139 | const resolvedParent = parsed.parent || parent; |
| 140 | const resolvedLocation = parsed.location ?? location; |
| 141 | const description = parseDescription(scope.description); |
| 142 | return { |
| 143 | name: |
| 144 | scope.name ?? (logScopeId ? resourceName(resolvedParent, resolvedLocation, logScopeId) : ""), |
| 145 | logScopeId, |
| 146 | parent: resolvedParent, |
| 147 | folderId: folderIdOf(resolvedParent), |
| 148 | location: resolvedLocation, |
| 149 | resourceNames: [...(scope.resourceNames ?? [])], |
| 150 | description: description.description, |
| 151 | createTime: scope.createTime, |
| 152 | updateTime: scope.updateTime, |
| 153 | }; |
| 154 | }; |
| 155 | |
| 156 | const getByName = (name: string) => |
| 157 | logging |
| 158 | .getFoldersLocationsLogScopes({ name }) |
| 159 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 160 | |
| 161 | export const FolderLogScopeProvider = () => |
| 162 | Provider.succeed(FolderLogScope, { |
| 163 | stables: ["name", "logScopeId", "parent", "folderId", "location", "createTime"], |
| 164 | |
| 165 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 166 | if (!isResolved(news)) return undefined; |
| 167 | const previousId = olds?.logScopeId ?? output?.logScopeId; |
| 168 | const idChanged = |
| 169 | previousId !== undefined && news.logScopeId !== undefined && news.logScopeId !== previousId; |
| 170 | const previousLocation = olds?.location ?? output?.location; |
| 171 | const locationChanged = |
| 172 | previousLocation !== undefined && |
| 173 | news.location !== undefined && |
| 174 | news.location !== previousLocation; |
| 175 | const previousFolder = olds?.folderId ?? output?.folderId; |
| 176 | const folderChanged = news.folderId !== undefined && news.folderId !== previousFolder; |
| 177 | if (!idChanged && !locationChanged && !folderChanged) return undefined; |
| 178 | return { action: "replace" as const, deleteFirst: false }; |
| 179 | }), |
| 180 | |
| 181 | read: Effect.fn(function* ({ id, olds, output }) { |
| 182 | const env = yield* GcpEnvironment.current; |
| 183 | const parent = scopeParent(env.project, olds?.folderId ?? output?.folderId); |
| 184 | const location = olds?.location ?? output?.location ?? DEFAULT_LOCATION; |
| 185 | const logScopeId = yield* toPhysicalId(id, olds?.logScopeId, output?.logScopeId, "s"); |
| 186 | const name = output?.name ?? resourceName(parent, location, logScopeId); |
| 187 | const existing = yield* getByName(name); |
| 188 | if (existing === undefined) return undefined; |
| 189 | const attrs = toAttrs(existing, parent, location); |
| 190 | const { labels } = parseDescription(existing.description); |
| 191 | return (yield* ownedBy(id, labels)) ? attrs : Unowned(attrs); |
| 192 | }), |
| 193 | |
| 194 | list: () => |
| 195 | Effect.gen(function* () { |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | return yield* logging.listFoldersLocationsLogScopes |
| 198 | .pages({ |
| 199 | parent: `projects/${env.project}/locations/-`, |
| 200 | pageSize: 1000, |
| 201 | }) |
| 202 | .pipe( |
| 203 | Stream.flatMap((page) => Stream.fromIterable(page.logScopes ?? [])), |
| 204 | Stream.filter((scope) => hasOwnershipMarker(scope.description)), |
| 205 | Stream.map((scope) => |
| 206 | toAttrs( |
| 207 | scope, |
| 208 | parseLoggingName(scope.name ?? "").parent || `projects/${env.project}`, |
| 209 | parseLoggingName(scope.name ?? "").location ?? DEFAULT_LOCATION, |
| 210 | ), |
| 211 | ), |
| 212 | Stream.runCollect, |
| 213 | Effect.map((chunk) => Array.from(chunk)), |
| 214 | ); |
| 215 | }), |
| 216 | |
| 217 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 218 | const env = yield* GcpEnvironment.current; |
| 219 | const parent = scopeParent(env.project, news.folderId ?? output?.folderId); |
| 220 | const location = news.location ?? output?.location ?? DEFAULT_LOCATION; |
| 221 | const logScopeId = yield* toPhysicalId(id, news.logScopeId, output?.logScopeId, "s"); |
| 222 | const name = resourceName(parent, location, logScopeId); |
| 223 | const ownership = yield* createOwnership(id); |
| 224 | const desiredDescription = encodeDescription(ownership, news.description); |
| 225 | |
| 226 | let current = yield* getByName(output?.name ?? name); |
| 227 | |
| 228 | if (current === undefined) { |
| 229 | const created = yield* logging |
| 230 | .createFoldersLocationsLogScopes({ |
| 231 | parent: locationParent(parent, location), |
| 232 | logScopeId, |
| 233 | body: { |
| 234 | resourceNames: [...news.resourceNames], |
| 235 | description: desiredDescription, |
| 236 | }, |
| 237 | }) |
| 238 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 239 | current = created ?? undefined; |
| 240 | } |
| 241 | |
| 242 | if (current === undefined) { |
| 243 | return yield* new FolderLogScopeNotResolved({ resourceName: name }); |
| 244 | } |
| 245 | |
| 246 | const namesChanged = !jsonEqual( |
| 247 | sortedNames(current.resourceNames), |
| 248 | sortedNames(news.resourceNames), |
| 249 | ); |
| 250 | const descriptionChanged = (current.description ?? "") !== desiredDescription; |
| 251 | const updateMask = [ |
| 252 | namesChanged ? "resourceNames" : undefined, |
| 253 | descriptionChanged ? "description" : undefined, |
| 254 | ].filter((field): field is string => field !== undefined); |
| 255 | |
| 256 | if (updateMask.length > 0) { |
| 257 | current = yield* logging.patchFoldersLocationsLogScopes({ |
| 258 | name: current.name ?? name, |
| 259 | updateMask: updateMask.join(","), |
| 260 | body: { |
| 261 | resourceNames: [...news.resourceNames], |
| 262 | description: desiredDescription, |
| 263 | }, |
| 264 | }); |
| 265 | } |
| 266 | |
| 267 | return toAttrs(current, parent, location); |
| 268 | }), |
| 269 | |
| 270 | delete: Effect.fn(function* ({ output }) { |
| 271 | yield* logging |
| 272 | .deleteFoldersLocationsLogScopes({ name: output.name }) |
| 273 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 274 | }), |
| 275 | }); |