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
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Stream from "effect/Stream"; |
| 3 | import { findProviderByType } from "../Provider.ts"; |
| 4 | import { stampedMode } from "../ProviderMode.ts"; |
| 5 | import type { ConfiguredStack } from "../Stack.ts"; |
| 6 | import * as State from "../State/index.ts"; |
| 7 | import { AlchemistInvalidInput } from "./Errors.ts"; |
| 8 | import { buildStack, type BuildOptions } from "./Operations.ts"; |
| 9 | |
| 10 | export interface ResourceIdentity { |
| 11 | readonly fqn: string; |
| 12 | readonly logicalId: string; |
| 13 | readonly resourceType: string; |
| 14 | } |
| 15 | |
| 16 | export interface LogResource extends ResourceIdentity { |
| 17 | readonly supportsQuery: boolean; |
| 18 | readonly supportsTail: boolean; |
| 19 | } |
| 20 | |
| 21 | export interface LogEntry { |
| 22 | readonly resource: ResourceIdentity; |
| 23 | readonly timestamp: Date; |
| 24 | readonly message: string; |
| 25 | } |
| 26 | |
| 27 | export interface LogOptions extends BuildOptions { |
| 28 | readonly stack: ConfiguredStack<unknown, unknown>; |
| 29 | /** Logical IDs to include. Empty or omitted means every resource. */ |
| 30 | readonly resources?: ReadonlyArray<string>; |
| 31 | } |
| 32 | |
| 33 | export interface QueryOptions extends LogOptions { |
| 34 | readonly limit?: number; |
| 35 | readonly since?: Date; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Build the stack and pair every deployed resource with the provider that |
| 40 | * can speak for it. `entries` and `tail` differ only in which provider hook |
| 41 | * they then call, so the selection lives here once. The build lives in the |
| 42 | * ambient `Scope`; each public function closes its own. |
| 43 | */ |
| 44 | const select = Effect.fn(function* (input: LogOptions) { |
| 45 | const built = yield* buildStack(input.stack, input); |
| 46 | const context = built.context; |
| 47 | const selected = new Set(input.resources ?? []); |
| 48 | const available = [ |
| 49 | ...new Set(Object.values(built.stack.resources).map((resource) => resource.LogicalId)), |
| 50 | ].sort(); |
| 51 | const unknown = [...selected].find((name) => !available.includes(name)); |
| 52 | if (unknown !== undefined) { |
| 53 | return yield* new AlchemistInvalidInput({ |
| 54 | field: "resources", |
| 55 | message: `Unknown resource '${unknown}'. Available: ${available.join(", ") || "(none)"}`, |
| 56 | }); |
| 57 | } |
| 58 | const rows = yield* Effect.provide( |
| 59 | Effect.gen(function* () { |
| 60 | const state = yield* Effect.flatten(State.State); |
| 61 | return yield* Effect.forEach(Object.entries(built.stack.resources), ([fqn, resource]) => |
| 62 | Effect.gen(function* () { |
| 63 | if (selected.size > 0 && !selected.has(resource.LogicalId)) { |
| 64 | return []; |
| 65 | } |
| 66 | const stored = yield* state.get({ |
| 67 | stack: built.stack.name, |
| 68 | stage: built.stack.stage, |
| 69 | fqn, |
| 70 | }); |
| 71 | if (!State.isResourceState(stored) || stored.attr === undefined) { |
| 72 | return []; |
| 73 | } |
| 74 | return [ |
| 75 | { |
| 76 | resource: { |
| 77 | fqn, |
| 78 | logicalId: resource.LogicalId, |
| 79 | resourceType: resource.Type, |
| 80 | } satisfies ResourceIdentity, |
| 81 | provider: yield* findProviderByType(resource.Type, stampedMode(stored)), |
| 82 | request: { |
| 83 | id: resource.LogicalId, |
| 84 | fqn, |
| 85 | instanceId: stored.instanceId, |
| 86 | props: stored.props, |
| 87 | output: stored.attr, |
| 88 | }, |
| 89 | }, |
| 90 | ]; |
| 91 | }), |
| 92 | ).pipe(Effect.map((rows) => rows.flat())); |
| 93 | }), |
| 94 | context, |
| 95 | ); |
| 96 | return { context, rows }; |
| 97 | }); |
| 98 | |
| 99 | /** Every deployed resource, with the log capabilities its provider offers. */ |
| 100 | export const resources = Effect.fn("Alchemist.Logs.resources")(function* ( |
| 101 | options: BuildOptions & { readonly stack: ConfiguredStack<unknown, unknown> }, |
| 102 | ) { |
| 103 | const { rows } = yield* select(options); |
| 104 | return rows.map(({ resource, provider }): LogResource => ({ |
| 105 | ...resource, |
| 106 | supportsQuery: provider.logs !== undefined, |
| 107 | supportsTail: provider.tail !== undefined, |
| 108 | })); |
| 109 | }, Effect.scoped); |
| 110 | |
| 111 | /** Query past log entries across the selected resources, oldest first. */ |
| 112 | export const entries = Effect.fn("Alchemist.Logs.entries")(function* (input: QueryOptions) { |
| 113 | const { context, rows } = yield* select(input); |
| 114 | const entries = yield* Effect.provide( |
| 115 | Effect.forEach(rows, ({ resource, provider, request }) => |
| 116 | provider.logs === undefined |
| 117 | ? Effect.succeed([]) |
| 118 | : provider |
| 119 | .logs({ |
| 120 | ...request, |
| 121 | options: { |
| 122 | limit: input.limit ?? 100, |
| 123 | since: input.since, |
| 124 | }, |
| 125 | }) |
| 126 | .pipe( |
| 127 | Effect.map((lines) => |
| 128 | lines.map((line) => ({ |
| 129 | resource, |
| 130 | timestamp: line.timestamp, |
| 131 | message: line.message, |
| 132 | })), |
| 133 | ), |
| 134 | ), |
| 135 | ), |
| 136 | context, |
| 137 | ); |
| 138 | return entries.flat().sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()); |
| 139 | }, Effect.scoped); |
| 140 | |
| 141 | /** Live-stream log entries from every selected resource that supports it. */ |
| 142 | export const tail = (input: LogOptions) => |
| 143 | Stream.unwrap( |
| 144 | Effect.gen(function* () { |
| 145 | const { context, rows } = yield* select(input); |
| 146 | return Stream.mergeAll( |
| 147 | rows.flatMap(({ resource, provider, request }) => |
| 148 | provider.tail === undefined |
| 149 | ? [] |
| 150 | : [ |
| 151 | Stream.provide(provider.tail(request), context).pipe( |
| 152 | Stream.map((line): LogEntry => ({ |
| 153 | resource, |
| 154 | timestamp: line.timestamp, |
| 155 | message: line.message, |
| 156 | })), |
| 157 | ), |
| 158 | ], |
| 159 | ), |
| 160 | { concurrency: "unbounded" }, |
| 161 | ); |
| 162 | }), |
| 163 | ); |