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 logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | |
| 11 | export interface LogStreamProps { |
| 12 | /** |
| 13 | * Name of the log group the stream belongs to. |
| 14 | * Changing this value replaces the log stream. |
| 15 | */ |
| 16 | logGroupName: string; |
| 17 | /** |
| 18 | * Name of the log stream. If omitted, a unique name is generated. |
| 19 | * Changing this value replaces the log stream. |
| 20 | */ |
| 21 | logStreamName?: string; |
| 22 | } |
| 23 | |
| 24 | export interface LogStream extends Resource< |
| 25 | "AWS.Logs.LogStream", |
| 26 | LogStreamProps, |
| 27 | { |
| 28 | logStreamName: string; |
| 29 | logGroupName: string; |
| 30 | logStreamArn?: string; |
| 31 | }, |
| 32 | never, |
| 33 | Providers |
| 34 | > {} |
| 35 | |
| 36 | /** |
| 37 | * A CloudWatch Logs log stream — a sequence of log events within a log group. |
| 38 | * |
| 39 | * Most log streams are created automatically by the emitting service (Lambda |
| 40 | * creates its own streams under `/aws/lambda/...`); declare one explicitly |
| 41 | * only when writing custom log events via `putLogEvents`. |
| 42 | * ### Creating Log Streams |
| 43 | * **Example:** Custom Audit Stream |
| 44 | * ```typescript |
| 45 | * const stream = yield* LogStream("AuditStream", { |
| 46 | * logGroupName: logGroup.logGroupName, |
| 47 | * }); |
| 48 | * ``` |
| 49 | * |
| 50 | * @resource |
| 51 | */ |
| 52 | export const LogStream = Resource<LogStream>("AWS.Logs.LogStream"); |
| 53 | |
| 54 | export const LogStreamProvider = () => |
| 55 | Provider.effect( |
| 56 | LogStream, |
| 57 | Effect.gen(function* () { |
| 58 | const toLogStreamName = (id: string, props: { logStreamName?: string } = {}) => |
| 59 | props.logStreamName |
| 60 | ? Effect.succeed(props.logStreamName) |
| 61 | : createPhysicalName({ id, maxLength: 512 }); |
| 62 | |
| 63 | const observe = Effect.fn(function* (logGroupName: string, logStreamName: string) { |
| 64 | const described = yield* logs |
| 65 | .describeLogStreams({ |
| 66 | logGroupName, |
| 67 | logStreamNamePrefix: logStreamName, |
| 68 | }) |
| 69 | .pipe( |
| 70 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({ logStreams: [] })), |
| 71 | ); |
| 72 | return (described.logStreams ?? []).find( |
| 73 | (stream) => stream.logStreamName === logStreamName, |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | return { |
| 78 | stables: ["logStreamName", "logGroupName", "logStreamArn"], |
| 79 | // No account-wide stream list — fan out over every log group. |
| 80 | list: () => |
| 81 | Effect.gen(function* () { |
| 82 | const groups = yield* logs.describeLogGroups.pages({}).pipe( |
| 83 | Stream.runCollect, |
| 84 | Effect.map((chunk) => |
| 85 | Array.from(chunk) |
| 86 | .flatMap((page) => page.logGroups ?? []) |
| 87 | .map((group) => group.logGroupName) |
| 88 | .filter((name): name is string => name != null), |
| 89 | ), |
| 90 | ); |
| 91 | const perGroup = yield* Effect.forEach( |
| 92 | groups, |
| 93 | (logGroupName) => |
| 94 | logs.describeLogStreams.pages({ logGroupName }).pipe( |
| 95 | Stream.runCollect, |
| 96 | Effect.map((chunk) => |
| 97 | Array.from(chunk) |
| 98 | .flatMap((page) => page.logStreams ?? []) |
| 99 | .filter( |
| 100 | ( |
| 101 | stream, |
| 102 | ): stream is logs.LogStream & { |
| 103 | logStreamName: string; |
| 104 | } => stream.logStreamName != null, |
| 105 | ) |
| 106 | .map((stream) => ({ |
| 107 | logStreamName: stream.logStreamName, |
| 108 | logGroupName, |
| 109 | logStreamArn: stream.arn, |
| 110 | })), |
| 111 | ), |
| 112 | // group deleted between list and describe — skip |
| 113 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed([])), |
| 114 | ), |
| 115 | { concurrency: 10 }, |
| 116 | ); |
| 117 | return perGroup.flat(); |
| 118 | }), |
| 119 | diff: Effect.fn(function* ({ id, olds, news }) { |
| 120 | if (!isResolved(news)) return; |
| 121 | if (olds.logGroupName !== news.logGroupName) { |
| 122 | return { action: "replace" } as const; |
| 123 | } |
| 124 | if ((yield* toLogStreamName(id, olds)) !== (yield* toLogStreamName(id, news))) { |
| 125 | return { action: "replace" } as const; |
| 126 | } |
| 127 | }), |
| 128 | read: Effect.fn(function* ({ id, olds, output }) { |
| 129 | const logGroupName = output?.logGroupName ?? olds?.logGroupName; |
| 130 | if (logGroupName === undefined) return undefined; |
| 131 | const logStreamName = output?.logStreamName ?? (yield* toLogStreamName(id, olds ?? {})); |
| 132 | const observed = yield* observe(logGroupName, logStreamName); |
| 133 | if (!observed) return undefined; |
| 134 | return { |
| 135 | logStreamName, |
| 136 | logGroupName, |
| 137 | logStreamArn: observed.arn, |
| 138 | }; |
| 139 | }), |
| 140 | // Existence-only resource: observe → if missing, create. Nothing on a |
| 141 | // log stream is mutable. |
| 142 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 143 | const logGroupName = news.logGroupName; |
| 144 | const logStreamName = output?.logStreamName ?? (yield* toLogStreamName(id, news)); |
| 145 | |
| 146 | let observed = yield* observe(logGroupName, logStreamName); |
| 147 | if (!observed) { |
| 148 | yield* logs |
| 149 | .createLogStream({ logGroupName, logStreamName }) |
| 150 | .pipe(Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void)); |
| 151 | observed = yield* observe(logGroupName, logStreamName); |
| 152 | } |
| 153 | |
| 154 | yield* session.note(`${logGroupName}:${logStreamName}`); |
| 155 | |
| 156 | return { |
| 157 | logStreamName, |
| 158 | logGroupName, |
| 159 | logStreamArn: observed?.arn, |
| 160 | }; |
| 161 | }), |
| 162 | delete: Effect.fn(function* ({ output }) { |
| 163 | yield* logs |
| 164 | .deleteLogStream({ |
| 165 | logGroupName: output.logGroupName, |
| 166 | logStreamName: output.logStreamName, |
| 167 | }) |
| 168 | .pipe( |
| 169 | Effect.retry({ |
| 170 | while: (error) => |
| 171 | error._tag === "OperationAbortedException" || |
| 172 | error._tag === "ServiceUnavailableException", |
| 173 | schedule: Schedule.exponential(100), |
| 174 | times: 8, |
| 175 | }), |
| 176 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 177 | ); |
| 178 | |
| 179 | const remaining = yield* Effect.repeat( |
| 180 | observe(output.logGroupName, output.logStreamName), |
| 181 | { |
| 182 | schedule: Schedule.fixed("250 millis"), |
| 183 | until: (stream) => stream === undefined, |
| 184 | times: 20, |
| 185 | }, |
| 186 | ); |
| 187 | if (remaining !== undefined) { |
| 188 | return yield* Effect.die( |
| 189 | new Error( |
| 190 | `CloudWatch log stream ${output.logGroupName}:${output.logStreamName} remained observable after delete`, |
| 191 | ), |
| 192 | ); |
| 193 | } |
| 194 | }), |
| 195 | }; |
| 196 | }), |
| 197 | ); |