@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, 04:45 UTC

main
ts 113 lines 4 KB
Raw
1 import * as Effect from "effect/Effect";
2
3 /**
4 * Returns true when the current process looks like it's being driven by a
5 * coding agent, CI runner, test runner, or anything else that won't render an
6 * interactive TUI / prompt well.
7 *
8 * This is the process-level source of truth used by terminal capability
9 * detection, auth configuration guards, and spawned-command stdin policy.
10 *
11 * Kept in `Util` (rather than `Cli`) so `Auth` can depend on it without
12 * pulling in the CLI layer.
13 */
14 // An explicit CLI flag beats every env-based heuristic. Checked via argv
15 // because capability detection runs while the CLI's service layers are
16 // built, before the command parser has produced flag values. Only scan up
17 // to a `--` separator so a positional argument that happens to be the
18 // literal text "--no-input" is not mistaken for the flag.
19 const hasNoInputFlag = (): boolean => {
20 const separator = process.argv.indexOf("--");
21 const flagArgs = separator === -1 ? process.argv : process.argv.slice(0, separator);
22 return flagArgs.includes("--no-input");
23 };
24
25 // Known coding-agent env vars, ported from unjs/std-env (src/agents.ts).
26 // Best-effort — the isTTY checks at the call sites already catch most cases
27 // since agents typically pipe stdin/stdout. std-env's kiro check
28 // (TERM_PROGRAM=kiro gated on no-TTY) is deliberately omitted: both call
29 // sites only reach this function when stdin/stdout ARE a TTY, where that
30 // check would never match.
31 const isAgentEnv = (env: NodeJS.ProcessEnv): boolean =>
32 !!(
33 // explicit override supported by std-env
34 env.AI_AGENT ||
35 // claude
36 env.CLAUDECODE ||
37 env.CLAUDE_CODE ||
38 env.CLAUDE_CODE_ENTRYPOINT ||
39 // replit
40 env.REPL_ID ||
41 // gemini
42 env.GEMINI_CLI ||
43 // codex
44 env.CODEX_SANDBOX ||
45 env.CODEX_THREAD_ID ||
46 env.CODEX_CLI ||
47 // opencode
48 env.OPENCODE ||
49 // auggie
50 env.AUGMENT_AGENT ||
51 // goose
52 env.GOOSE_PROVIDER ||
53 // junie
54 env.JUNIE_DATA ||
55 env.JUNIE_SHIM_PATH ||
56 // aider
57 env.AIDER_MODEL ||
58 // pi
59 /\.pi[\\/]agent/.test(env.PATH ?? "") ||
60 // devin
61 /devin/.test(env.EDITOR ?? "") ||
62 // cursor
63 env.CURSOR_AGENT
64 );
65
66 export const isNonInteractive = (): boolean => {
67 const env = process.env;
68 if (hasNoInputFlag()) return true;
69 if (env.ALCHEMY_PLAIN === "1" || env.ALCHEMY_NO_TUI === "1") return true;
70 if (env.ALCHEMY_TUI === "1") return false;
71 if (!process.stdin.isTTY || !process.stdout.isTTY) return true;
72 if (env.CI) return true;
73 if (isAgentEnv(env)) return true;
74 return false;
75 };
76
77 /**
78 * Whether a plain line-based prompt can still read an answer from stdin.
79 *
80 * Distinct from {@link isNonInteractive}: `ALCHEMY_PLAIN` / `ALCHEMY_NO_TUI`
81 * turn off the TUI *rendering*, not input — a human running plain mode in a
82 * terminal can still answer a `[y/n]` question. Agents and CI pipe stdin
83 * (or set their env markers) and land `false`.
84 */
85 export const canPromptOnStdin = (): boolean => {
86 const env = process.env;
87 if (hasNoInputFlag()) return false;
88 if (!process.stdin.isTTY || !process.stdout.isTTY) return false;
89 if (env.CI) return false;
90 if (isAgentEnv(env)) return false;
91 return true;
92 };
93
94 export interface InteractionCapabilities {
95 readonly input: boolean;
96 }
97
98 /** Process capabilities as an Effect so callers can replace them in tests. */
99 export const processInteractionCapabilities: Effect.Effect<InteractionCapabilities> = Effect.sync(
100 () => ({ input: !isNonInteractive() }),
101 );
102
103 /** Select user-facing copy from an injected capability Effect. */
104 export const messageForCapabilities = <E, R>(
105 capabilities: Effect.Effect<InteractionCapabilities, E, R>,
106 interactive: string,
107 nonInteractive: string,
108 ): Effect.Effect<string, E, R> =>
109 Effect.map(capabilities, ({ input }) => (input ? interactive : nonInteractive));
110
111 /** Prefer the profile dashboard when this process can own a TUI screen. */
112 export const profileCommandHint = (nonInteractiveCommand: string) =>
113 messageForCapabilities(processInteractionCapabilities, "alchemy profile", nonInteractiveCommand);