The best way to control Claude Code, Codex and more from any computer or phone

main
ts 128 lines 3.99 KB
Raw
1 import { Command, Flag } from "effect/cli";
2 import * as Console from "effect/Console";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Stream from "effect/Stream";
6 import * as Alchemist from "../../Alchemist/index.ts";
7 import { paint } from "../../Util/Terminal.ts";
8 import { formatLocalTimestamp, TAIL_COLORS } from "../Format.ts";
9 import { config, envFile, parseSince, profile, resolveStage, stage } from "./flags.ts";
10 import { instrumentCommand } from "./instrument.ts";
11
12 const logsLimit = Flag.Int("limit").pipe(
13 Flag.withDescription("Number of log entries to fetch (default: 100)"),
14 Flag.withDefault(100),
15 );
16
17 const tail = Flag.Boolean("tail").pipe(
18 Flag.withAlias("t"),
19 Flag.withDescription("Continue streaming new log entries"),
20 Flag.withDefault(false),
21 );
22
23 const resources = Flag.String("resource").pipe(
24 Flag.withAlias("r"),
25 Flag.withDescription("Comma-separated logical resource IDs to include (for example Worker,Api)"),
26 Flag.optional,
27 Flag.map(Option.getOrUndefined),
28 );
29
30 const logsSince = Flag.String("since").pipe(
31 Flag.withDescription("Fetch logs since this time (e.g. '1h', '30m', '2024-01-01T00:00:00Z')"),
32 Flag.optional,
33 Flag.map(Option.getOrUndefined),
34 );
35
36 interface LogsArgs {
37 readonly main: string;
38 readonly stage: string;
39 readonly envFile: Option.Option<string>;
40 readonly profile: string | undefined;
41 readonly resources: string | undefined;
42 readonly limit: number;
43 readonly since: string | undefined;
44 readonly tail: boolean;
45 }
46
47 const runLogs = Effect.fn(function* ({
48 main,
49 stage,
50 envFile,
51 profile,
52 resources,
53 limit,
54 since,
55 tail,
56 }: LogsArgs) {
57 const sinceDate = since ? yield* parseSince(since) : undefined;
58 const selected = (resources ?? "")
59 .split(",")
60 .map((resource) => resource.trim())
61 .filter((resource) => resource.length > 0);
62 const stack = yield* Alchemist.importStack(main);
63 const options = { stage, profile, envFile: Option.getOrUndefined(envFile) };
64 // Colors are assigned as each resource first appears, so the command
65 // builds the stack once instead of listing resources up front.
66 const colors = new Map<string, (typeof TAIL_COLORS)[number]>();
67 const format = (entry: Alchemist.Logs.LogEntry) => {
68 const fqn = entry.resource.fqn;
69 if (!colors.has(fqn)) colors.set(fqn, TAIL_COLORS[colors.size % TAIL_COLORS.length]!);
70 return `${paint(colors.get(fqn)!, `${formatLocalTimestamp(entry.timestamp)} [${fqn}]`)} ${entry.message}`;
71 };
72
73 if (tail) {
74 // The merged stream ends at once when no selected resource can tail.
75 let received = false;
76 yield* Alchemist.Logs.tail({ ...options, stack, resources: selected }).pipe(
77 Stream.runForEach((entry) => {
78 received = true;
79 return Console.log(format(entry));
80 }),
81 );
82 if (!received) yield* Console.log("No matching resources support live logs.");
83 return;
84 }
85
86 const entries = yield* Alchemist.Logs.entries({
87 ...options,
88 stack,
89 resources: selected,
90 limit,
91 since: sinceDate,
92 });
93 if (entries.length === 0) {
94 yield* Console.log(
95 selected.length > 0
96 ? "No resources with logs match --resource (deploy first, or selected resources may not expose logs)."
97 : "No resources with logs found. Deploy first, then run logs.",
98 );
99 return;
100 }
101 for (const entry of entries) yield* Console.log(format(entry));
102 });
103
104 export const logsCommand = Command.make(
105 "logs",
106 {
107 main: config,
108 envFile,
109 stage,
110 profile,
111 resources,
112 limit: logsLimit,
113 since: logsSince,
114 tail,
115 },
116 (args) =>
117 resolveStage("live", args.stage, args.envFile).pipe(
118 Effect.flatMap((stage) =>
119 instrumentCommand("logs", (a: LogsArgs) => ({
120 "alchemy.stage": a.stage,
121 "alchemy.profile": a.profile ?? "",
122 "alchemy.main": a.main,
123 "alchemy.limit": a.limit,
124 "alchemy.tail": a.tail,
125 }))(runLogs)({ ...args, stage }),
126 ),
127 ),
128 ).pipe(Command.withDescription("Fetch or tail logs from stack resources"));