@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, 03:37 UTC

main
ts 145 lines 5.35 KB
Raw
1 import { doctorCommand } from "./cli/doctor.ts";
2 import { policyCommand } from "./cli/policy.ts";
3 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
5 import * as NodeServices from "@effect/platform-node/NodeServices";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import { Argument, Command } from "effect/cli";
9 import * as CliError from "effect/cli/CliError";
10
11 import * as NetService from "@t3tools/shared/Net";
12 import packageJson from "../package.json" with { type: "json" };
13 import { acpMcpBridgeCommand, acpMcpCallCommand } from "./cli/acpMcpBridge.ts";
14 import { authCommand } from "./cli/auth.ts";
15 import { environmentCommand } from "./cli/environment.ts";
16 import { appCommand } from "./cli/app.ts";
17 import { browserCommand } from "./cli/browser.ts";
18 import { pairCommand } from "./cli/pair.ts";
19 import { hasCloudPublicConfig } from "./cloud/publicConfig.ts";
20 import { extendConnectionAttemptTimeout, keepOutboundFetchOnHttp1 } from "./outboundHttp.ts";
21 import { sharedServerCommandFlags } from "./cli/config.ts";
22 import { projectCommand } from "./cli/project.ts";
23 import { threadCommand } from "./cli/thread.ts";
24 import { runDefaultServerCommand, serveCommand, startCommand } from "./cli/server.ts";
25 import { updateCommand } from "./cli/update.ts";
26 import {
27 confirmCliInstall,
28 inTerminalForeground,
29 installCommand,
30 offerCliInstall,
31 } from "./cli/install.ts";
32 import { uninstallCommand } from "./cli/uninstall.ts";
33 import { runServiceLauncherCli } from "./cli/serviceLauncher.ts";
34 import { speechWorkerCommand } from "./cli/speechWorker.ts";
35 import { claudeHistoryCommand } from "./cli/claudeHistory.ts";
36 import { sshHelperCommand } from "./cli/sshHelper.ts";
37 import { serviceCommand } from "./cli/service.ts";
38 import { servicePreflightCommand } from "./cli/servicePreflight.ts";
39 import { themeCommand } from "./cli/theme.ts";
40 import { traceCommand } from "./cli/trace.ts";
41 import { triageCommand } from "./cli/triage.ts";
42 import { connectCommand, statusCommand, upCommand } from "./cli/up.ts";
43
44 const layerCliRuntime = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer);
45
46 const connectPublicConfigMissingMessage =
47 "T3 Connect commands are unavailable: this build is missing T3 Connect public configuration.";
48
49 class ConnectPublicConfigMissingError extends CliError.UserError {
50 override get message() {
51 return connectPublicConfigMissingMessage;
52 }
53 }
54
55 const connectUnavailableCommand = Command.make("connect", {
56 command: Argument.String("command").pipe(Argument.variadic),
57 }).pipe(
58 Command.withDescription("T3 Connect is unavailable in builds without public configuration."),
59 Command.unlisted,
60 Command.withHandler(() =>
61 Effect.fail(
62 new CliError.ShowHelp({
63 commandPath: ["t3", "connect"],
64 errors: [new ConnectPublicConfigMissingError({ cause: connectPublicConfigMissingMessage })],
65 }),
66 ),
67 ),
68 );
69
70 export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) =>
71 Command.make("t3", { ...sharedServerCommandFlags }).pipe(
72 Command.withDescription(
73 "Run T3 Code in this terminal and open it in your browser. To use it from your other devices, run `t3 up`.",
74 ),
75 Command.withHandler(runDefaultServerCommand),
76 Command.withSubcommands([
77 Command.make("help").pipe(
78 Command.withDescription("Show command help."),
79 Command.withHandler(() =>
80 Effect.fail(new CliError.ShowHelp({ commandPath: ["t3"], errors: [] })),
81 ),
82 ),
83 acpMcpBridgeCommand,
84 acpMcpCallCommand,
85 startCommand,
86 serveCommand,
87 appCommand,
88 pairCommand,
89 authCommand,
90 environmentCommand,
91 browserCommand,
92 projectCommand,
93 threadCommand,
94 upCommand,
95 statusCommand,
96 serviceCommand,
97 updateCommand,
98 installCommand,
99 uninstallCommand,
100 // bin.ts runs the bare service invocation; this keeps flags and help working like other commands.
101 Command.make("__service-launcher").pipe(
102 Command.unlisted,
103 // The launcher owns shutdown until its child exits; `runMain` would interrupt that shutdown.
104 Command.withHandler(() => Effect.sync(() => void runServiceLauncherCli())),
105 ),
106 claudeHistoryCommand,
107 speechWorkerCommand,
108 sshHelperCommand,
109
110 servicePreflightCommand,
111 themeCommand,
112 traceCommand,
113 doctorCommand,
114 policyCommand,
115 triageCommand,
116 cloudEnabled ? connectCommand : connectUnavailableCommand,
117 ]),
118 );
119
120 export const cli = makeCli();
121
122 export function runCli() {
123 extendConnectionAttemptTimeout();
124 keepOutboundFetchOnHttp1();
125 offerCliInstall({
126 args: process.argv.slice(2),
127 // A child started over IPC (a service launcher) has no one to answer.
128 interactive: Boolean(process.stdin.isTTY && process.stdout.isTTY) && process.send === undefined,
129 foreground: inTerminalForeground,
130 confirm: confirmCliInstall,
131 }).pipe(
132 // The offer handles its own errors; even an unexpected defect must not stop the command.
133 Effect.catchDefect(() => Effect.succeed(true)),
134 Effect.flatMap((proceed) =>
135 proceed
136 ? Command.run(cli, { version: packageJson.version })
137 : Effect.sync(() => {
138 process.exitCode = 130;
139 }),
140 ),
141 Effect.scoped,
142 Effect.provide(layerCliRuntime),
143 NodeRuntime.runMain,
144 );
145 }