Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

binCli.ts

149 lines5.5 KB

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