@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 55 lines 2.16 KB
Raw
1 import * as Cause from "effect/Cause";
2 import * as Context from "effect/Context";
3 import * as Effect from "effect/Effect";
4 import { identity } from "effect/Function";
5 import { authRegistryLayer } from "../../Alchemist/AuthRegistry.ts";
6 import { recordCli } from "../../Telemetry/Metrics.ts";
7 import { globalLogs } from "../../Util/LogRecord.ts";
8
9 /**
10 * Commands about the account, not a project: their logs go to the global run
11 * log (`~/.alchemy/logs`), so running one outside a project creates nothing
12 * there.
13 */
14 const isGlobalCommand = (command: string) =>
15 /^(profile|provider)(\.|$)/.test(command) ||
16 command === "state.bootstrap" ||
17 command === "state.teardown";
18
19 /**
20 * The terminal shows a failed command's friendly message; this keeps its
21 * full cause chain in the command's run log.
22 */
23 export const logCommandFailure = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
24 Effect.tapCause(effect, (cause) => Effect.logDebug(`command failed:\n${Cause.pretty(cause)}`));
25
26 /**
27 * The pre-command version check. The CLI entrypoint sets it with its
28 * services already provided; elsewhere (tests, the API) it does nothing.
29 */
30 export const VersionCheck = Context.Reference<Effect.Effect<void>>("alchemy/Cli/VersionCheck", {
31 defaultValue: () => Effect.void,
32 });
33
34 /**
35 * Wraps a command handler as one traced, metered unit. A best-effort check
36 * for a newer published version runs first, so its warning prints before
37 * any interactive prompt; it is cached for a day and bounded by a short
38 * timeout, so this stays fast.
39 */
40 export const instrumentCommand =
41 <AttrsArgs = unknown>(command: string, attrs?: (args: AttrsArgs) => Record<string, unknown>) =>
42 <Args extends AttrsArgs, A, E, R>(
43 handler: (args: Args) => Effect.Effect<A, E, R>,
44 ): ((args: Args) => Effect.Effect<A, E, R>) =>
45 (args) =>
46 Effect.andThen(Effect.flatten(VersionCheck), handler(args)).pipe(
47 Effect.withSpan(`cli.${command}`, {
48 attributes: attrs ? attrs(args) : {},
49 }),
50 recordCli(command),
51 logCommandFailure,
52 isGlobalCommand(command) ? globalLogs : identity,
53 // Auth registries are memoized for the command's duration.
54 Effect.provide(authRegistryLayer),
55 );