The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as Logs from "@alchemy.run/aws/Logs"; |
| 3 | import * as Clock from "effect/Clock"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Duration from "effect/Duration"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 8 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import path from "pathe"; |
| 12 | |
| 13 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 14 | |
| 15 | export class LogsTestFunction extends Lambda.Function<Lambda.Function>()("LogsTestFunction") {} |
| 16 | |
| 17 | class QueryNotComplete extends Data.TaggedError("QueryNotComplete")<{ |
| 18 | readonly status: string | undefined; |
| 19 | }> {} |
| 20 | |
| 21 | export default LogsTestFunction.make( |
| 22 | { |
| 23 | main, |
| 24 | functionUrl: true, |
| 25 | timeout: Duration.seconds(30), |
| 26 | }, |
| 27 | Effect.gen(function* () { |
| 28 | const logGroup = yield* Logs.LogGroup("BindingsLogGroup", { |
| 29 | retention: "1 day", |
| 30 | }); |
| 31 | const logStream = yield* Logs.LogStream("BindingsLogStream", { |
| 32 | logGroupName: logGroup.logGroupName, |
| 33 | logStreamName: "alchemy-test-bindings-stream", |
| 34 | }); |
| 35 | |
| 36 | const putLogEvents = yield* Logs.PutLogEvents(logGroup); |
| 37 | const filterLogEvents = yield* Logs.FilterLogEvents(logGroup); |
| 38 | const getLogEvents = yield* Logs.GetLogEvents(logGroup); |
| 39 | const startQuery = yield* Logs.StartQuery(logGroup); |
| 40 | const getQueryResults = yield* Logs.GetQueryResults(logGroup); |
| 41 | const stopQuery = yield* Logs.StopQuery(logGroup); |
| 42 | const getLogRecord = yield* Logs.GetLogRecord(logGroup); |
| 43 | const getLogGroupFields = yield* Logs.GetLogGroupFields(logGroup); |
| 44 | const describeLogStreams = yield* Logs.DescribeLogStreams(logGroup); |
| 45 | const createLogStream = yield* Logs.CreateLogStream(logGroup); |
| 46 | const deleteLogStream = yield* Logs.DeleteLogStream(logGroup); |
| 47 | |
| 48 | const LogStreamName = yield* logStream.logStreamName; |
| 49 | |
| 50 | return { |
| 51 | fetch: Effect.gen(function* () { |
| 52 | const request = yield* HttpServerRequest; |
| 53 | const url = new URL(request.originalUrl); |
| 54 | const pathname = url.pathname; |
| 55 | const logStreamName = yield* LogStreamName; |
| 56 | |
| 57 | if (request.method === "POST" && pathname === "/put") { |
| 58 | const message = url.searchParams.get("message") ?? "no-message"; |
| 59 | const timestamp = yield* Clock.currentTimeMillis; |
| 60 | const result = yield* putLogEvents({ |
| 61 | logStreamName, |
| 62 | logEvents: [{ timestamp, message }], |
| 63 | }); |
| 64 | return yield* HttpServerResponse.json({ |
| 65 | ok: true, |
| 66 | rejected: result.rejectedLogEventsInfo ?? null, |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | if (request.method === "GET" && pathname === "/filter") { |
| 71 | const pattern = url.searchParams.get("pattern") ?? ""; |
| 72 | const result = yield* filterLogEvents({ |
| 73 | filterPattern: `"${pattern}"`, |
| 74 | }); |
| 75 | return yield* HttpServerResponse.json({ |
| 76 | messages: (result.events ?? []).map((event) => event.message), |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | if (request.method === "GET" && pathname === "/get-events") { |
| 81 | const result = yield* getLogEvents({ |
| 82 | logStreamName, |
| 83 | startFromHead: true, |
| 84 | }); |
| 85 | return yield* HttpServerResponse.json({ |
| 86 | messages: (result.events ?? []).map((event) => event.message), |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if (request.method === "GET" && pathname === "/query") { |
| 91 | const now = yield* Clock.currentTimeMillis; |
| 92 | const { queryId } = yield* startQuery({ |
| 93 | queryString: "fields @timestamp, @message | limit 10", |
| 94 | startTime: Math.floor(now / 1000) - 3600, |
| 95 | endTime: Math.floor(now / 1000) + 60, |
| 96 | }); |
| 97 | if (!queryId) { |
| 98 | return yield* HttpServerResponse.json({ error: "no queryId" }, { status: 500 }); |
| 99 | } |
| 100 | // Poll bounded (~20s) — well inside the 30s function timeout. |
| 101 | const results = yield* getQueryResults({ queryId }).pipe( |
| 102 | Effect.flatMap((response) => |
| 103 | response.status === "Complete" |
| 104 | ? Effect.succeed(response) |
| 105 | : Effect.fail(new QueryNotComplete({ status: response.status })), |
| 106 | ), |
| 107 | Effect.retry({ |
| 108 | while: (error) => error._tag === "QueryNotComplete", |
| 109 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 110 | }), |
| 111 | ); |
| 112 | // Surface the first row's @ptr so the test can exercise GetLogRecord. |
| 113 | const ptr = |
| 114 | (results.results ?? []).flat().find((field) => field.field === "@ptr")?.value ?? null; |
| 115 | return yield* HttpServerResponse.json({ |
| 116 | queryId, |
| 117 | status: results.status, |
| 118 | resultCount: (results.results ?? []).length, |
| 119 | ptr, |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | if (request.method === "GET" && pathname === "/record") { |
| 124 | const ptr = url.searchParams.get("ptr"); |
| 125 | if (!ptr) { |
| 126 | return yield* HttpServerResponse.json({ error: "missing ptr" }, { status: 400 }); |
| 127 | } |
| 128 | const { logRecord } = yield* getLogRecord({ logRecordPointer: ptr }); |
| 129 | return yield* HttpServerResponse.json({ |
| 130 | message: logRecord?.["@message"] ?? null, |
| 131 | }); |
| 132 | } |
| 133 | |
| 134 | if (request.method === "GET" && pathname === "/stop-query") { |
| 135 | const now = yield* Clock.currentTimeMillis; |
| 136 | const { queryId } = yield* startQuery({ |
| 137 | queryString: "fields @timestamp, @message | limit 10000", |
| 138 | startTime: Math.floor(now / 1000) - 86_400, |
| 139 | endTime: Math.floor(now / 1000) + 60, |
| 140 | }); |
| 141 | if (!queryId) { |
| 142 | return yield* HttpServerResponse.json({ error: "no queryId" }, { status: 500 }); |
| 143 | } |
| 144 | // Give the query registration a beat to propagate before stopping. |
| 145 | yield* Effect.sleep("500 millis"); |
| 146 | const outcome = yield* stopQuery({ queryId }).pipe( |
| 147 | Effect.map((response) => ({ |
| 148 | stopped: response.success ?? true, |
| 149 | error: null as string | null, |
| 150 | })), |
| 151 | // Benign races: the query can complete before the stop lands |
| 152 | // (InvalidParameterException) or the stop can outrun the query's |
| 153 | // registration (ResourceNotFoundException). Both still prove the |
| 154 | // binding round-tripped with valid credentials. |
| 155 | Effect.catchTag(["InvalidParameterException", "ResourceNotFoundException"], (error) => |
| 156 | Effect.succeed({ stopped: false, error: error._tag }), |
| 157 | ), |
| 158 | ); |
| 159 | return yield* HttpServerResponse.json({ ok: true, ...outcome }); |
| 160 | } |
| 161 | |
| 162 | if (request.method === "GET" && pathname === "/fields") { |
| 163 | const { logGroupFields } = yield* getLogGroupFields(); |
| 164 | return yield* HttpServerResponse.json({ |
| 165 | fields: (logGroupFields ?? []).map((field) => field.name), |
| 166 | }); |
| 167 | } |
| 168 | |
| 169 | if (request.method === "GET" && pathname === "/streams") { |
| 170 | const { logStreams } = yield* describeLogStreams({ limit: 10 }); |
| 171 | return yield* HttpServerResponse.json({ |
| 172 | streams: (logStreams ?? []).map((stream) => stream.logStreamName), |
| 173 | }); |
| 174 | } |
| 175 | |
| 176 | if (request.method === "POST" && pathname === "/stream-lifecycle") { |
| 177 | const name = url.searchParams.get("name"); |
| 178 | if (!name) { |
| 179 | return yield* HttpServerResponse.json({ error: "missing name" }, { status: 400 }); |
| 180 | } |
| 181 | yield* createLogStream({ logStreamName: name }).pipe( |
| 182 | Effect.catchTag("ResourceAlreadyExistsException", () => Effect.void), |
| 183 | ); |
| 184 | const { logStreams } = yield* describeLogStreams({ |
| 185 | logStreamNamePrefix: name, |
| 186 | }); |
| 187 | const seen = (logStreams ?? []).some((stream) => stream.logStreamName === name); |
| 188 | yield* deleteLogStream({ logStreamName: name }).pipe( |
| 189 | Effect.catchTag("ResourceNotFoundException", () => Effect.void), |
| 190 | ); |
| 191 | const after = yield* describeLogStreams({ |
| 192 | logStreamNamePrefix: name, |
| 193 | }); |
| 194 | const gone = !(after.logStreams ?? []).some((stream) => stream.logStreamName === name); |
| 195 | return yield* HttpServerResponse.json({ seen, gone }); |
| 196 | } |
| 197 | |
| 198 | return yield* HttpServerResponse.json( |
| 199 | { error: "Not found", method: request.method, pathname }, |
| 200 | { status: 404 }, |
| 201 | ); |
| 202 | }).pipe(Effect.orDie), |
| 203 | }; |
| 204 | }).pipe( |
| 205 | Effect.provide( |
| 206 | Layer.mergeAll( |
| 207 | Logs.PutLogEventsHttp, |
| 208 | Logs.FilterLogEventsHttp, |
| 209 | Logs.GetLogEventsHttp, |
| 210 | Logs.StartQueryHttp, |
| 211 | Logs.GetQueryResultsHttp, |
| 212 | Logs.StopQueryHttp, |
| 213 | Logs.GetLogRecordHttp, |
| 214 | Logs.GetLogGroupFieldsHttp, |
| 215 | Logs.DescribeLogStreamsHttp, |
| 216 | Logs.CreateLogStreamHttp, |
| 217 | Logs.DeleteLogStreamHttp, |
| 218 | ), |
| 219 | ), |
| 220 | ), |
| 221 | ); |