feat: ✨ Remove TTY requirement for the cli in headless mode

This commit is contained in:
Shawn Smith 2025-11-11 13:43:53 -08:00
parent f608296d55
commit 68c463b7ae
2 changed files with 16 additions and 40 deletions

View file

@ -444,6 +444,16 @@ async function runHeadlessMode(
prompt: string | undefined,
options: ChatOptions,
): Promise<void> {
// Critical validation: Ensure we have a prompt in headless mode
// This prevents the CLI from hanging in TTY-less environments
if (!prompt) {
throw new Error(
'Headless mode requires a prompt. Use: cn -p "your prompt"\n' +
'Or pipe input: echo "prompt" | cn -p\n' +
"Or use agent files: cn -p --agent my-org/my-agent",
);
}
// Initialize services for headless mode
const { permissionOverrides } = processCommandFlags(options);

View file

@ -46,46 +46,12 @@ export function hasSuppliedPrompt(): boolean {
return false;
}
// Check if --prompt flag is present (indicates a prompt is coming)
// Note: --agent doesn't supply a prompt, it just specifies which agent to use
// Piped stdin should still be read when --agent is present
const hasPromptFlag = args.includes("--prompt");
if (hasPromptFlag) {
return true;
}
// Check if there's a non-flag argument after -p/--print
// We need to skip flags that take values (like --config, --model, etc.)
// Known flags that take values
const flagsWithValues = new Set([
"--config",
"--model",
"--output",
"--mode",
"--workflow",
"-m",
"-c",
"-o",
]);
// or if there are --prompt or --agent flags
const hasPromptArg =
args.length > printIndex + 1 && !args[printIndex + 1].startsWith("-");
const hasPromptFlag = args.includes("--prompt");
const hasAgentFlag = args.includes("--agent");
const argsAfterPrint = args.slice(printIndex + 1);
for (let i = 0; i < argsAfterPrint.length; i++) {
const arg = argsAfterPrint[i];
// If this is a flag that takes a value, skip both the flag and its value
if (flagsWithValues.has(arg)) {
i++; // Skip the next argument (the value)
continue;
}
// If this is any other flag (starts with -), skip it
if (arg.startsWith("-")) {
continue;
}
// Found a non-flag argument - this is the prompt
return true;
}
return false;
return hasPromptArg || hasPromptFlag || hasAgentFlag;
}