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 type * as Logs from "@distilled.cloud/aws/cloudwatch-logs"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import { makeBatchedSink } from "alchemy/Util/BatchedSink"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as AWSBindings from "../AWSBindings.ts"; |
| 7 | import { LogEventSink, type LogEventSinkProps } from "./LogEventSink.ts"; |
| 8 | import type { LogGroup } from "./LogGroup.ts"; |
| 9 | import { PutLogEvents } from "./PutLogEvents.ts"; |
| 10 | |
| 11 | const encoder = new TextEncoder(); |
| 12 | |
| 13 | /** |
| 14 | * The `PutLogEvents` batch-size formula charges each event its UTF-8 message |
| 15 | * size plus 26 bytes of overhead. |
| 16 | */ |
| 17 | const PER_EVENT_OVERHEAD = 26; |
| 18 | |
| 19 | /** |
| 20 | * Select the events `rejectedLogEventsInfo` marks as permanently rejected, |
| 21 | * preserving input order. The API skips (never ingests) these while still |
| 22 | * processing the remaining valid events, so they must be dropped — not |
| 23 | * retried: |
| 24 | * |
| 25 | * - `tooOldLogEventEndIndex` / `expiredLogEventEndIndex` — everything up to |
| 26 | * and including that index is too old / past retention; |
| 27 | * - `tooNewLogEventStartIndex` — everything from that index on is more than |
| 28 | * 2 hours in the future. |
| 29 | */ |
| 30 | const selectRejected = ( |
| 31 | out: Logs.PutLogEventsResponse, |
| 32 | batch: readonly Logs.InputLogEvent[], |
| 33 | ): readonly Logs.InputLogEvent[] => { |
| 34 | const info = out.rejectedLogEventsInfo; |
| 35 | if (info === undefined) { |
| 36 | return []; |
| 37 | } |
| 38 | const rejectedHeadEnd = Math.max( |
| 39 | info.tooOldLogEventEndIndex ?? -1, |
| 40 | info.expiredLogEventEndIndex ?? -1, |
| 41 | ); |
| 42 | const tooNewStart = info.tooNewLogEventStartIndex ?? batch.length; |
| 43 | return batch.filter((_, index) => index <= rejectedHeadEnd || index >= tooNewStart); |
| 44 | }; |
| 45 | |
| 46 | export const LogEventSinkHttp = Layer.effect( |
| 47 | LogEventSink, |
| 48 | Effect.gen(function* () { |
| 49 | const bind = yield* AWSBindings.Bind; |
| 50 | const putLogEvents = yield* PutLogEvents; |
| 51 | |
| 52 | return Effect.fn(function* (logGroup: LogGroup, props: LogEventSinkProps) { |
| 53 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 54 | yield* bind`AWS.Logs.LogEventSink(${logGroup})`({ |
| 55 | policyStatements: [ |
| 56 | { |
| 57 | // PutLogEvents authorizes against the log-stream resource |
| 58 | // (`log-group:{name}:log-stream:{stream}`), covered by the |
| 59 | // `:*` wildcard on the group ARN. |
| 60 | Effect: "Allow", |
| 61 | Action: ["logs:PutLogEvents"], |
| 62 | Resource: [Output.interpolate`${logGroup.logGroupArn}:*`], |
| 63 | }, |
| 64 | ], |
| 65 | }); |
| 66 | } |
| 67 | const put = yield* putLogEvents(logGroup); |
| 68 | return makeBatchedSink<Logs.InputLogEvent, Logs.PutLogEventsResponse, Logs.PutLogEventsError>( |
| 69 | { |
| 70 | maxRecords: 10_000, |
| 71 | maxBytes: 1_048_576, |
| 72 | sizeOf: (event) => encoder.encode(event.message).length + PER_EVENT_OVERHEAD, |
| 73 | send: (batch) => |
| 74 | put({ |
| 75 | logStreamName: props.logStreamName, |
| 76 | logEvents: [...batch], |
| 77 | }), |
| 78 | // No `unprocessed` extractor: PutLogEvents has no transient per-event |
| 79 | // failure mode — timestamp rejections are permanent, so drop + surface. |
| 80 | rejected: selectRejected, |
| 81 | }, |
| 82 | ); |
| 83 | }); |
| 84 | }), |
| 85 | ); |