176 lines
No EOL
5.3 KiB
JavaScript
176 lines
No EOL
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
import { Command } from "commander";
|
|
import { chat } from "./commands/chat.js";
|
|
import { login } from "./commands/login.js";
|
|
import { logout } from "./commands/logout.js";
|
|
import { remoteTest } from "./commands/remote-test.js";
|
|
import { remote } from "./commands/remote.js";
|
|
import { serve } from "./commands/serve.js";
|
|
import { addCommonOptions, mergeParentOptions } from "./shared-options.js";
|
|
import { configureConsoleForHeadless } from "./util/consoleOverride.js";
|
|
import logger from "./util/logger.js";
|
|
import { readStdinSync } from "./util/stdin.js";
|
|
import { getVersion } from "./version.js";
|
|
|
|
// Add global error handlers to prevent uncaught errors from crashing the process
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
logger.error("Unhandled Rejection at:", { promise, reason });
|
|
// Don't exit the process, just log the error
|
|
});
|
|
|
|
process.on("uncaughtException", (error) => {
|
|
logger.error("Uncaught Exception:", error);
|
|
// Don't exit the process, just log the error
|
|
});
|
|
|
|
const program = new Command();
|
|
|
|
program
|
|
.name("cn")
|
|
.description(
|
|
"Continue CLI - AI-powered development assistant. Starts an interactive session by default, use -p/--print for non-interactive output."
|
|
)
|
|
.version(getVersion());
|
|
|
|
// Root command - chat functionality (default)
|
|
// Add common options to the root command
|
|
addCommonOptions(program)
|
|
.argument("[prompt]", "Optional prompt to send to the assistant")
|
|
.option("-p, --print", "Print response and exit (useful for pipes)")
|
|
.option(
|
|
"--format <format>",
|
|
"Output format for headless mode (json). Only works with -p/--print flag."
|
|
)
|
|
.option("--resume", "Resume from last session")
|
|
.action(async (prompt, options) => {
|
|
// Configure console overrides FIRST, before any other logging
|
|
const isHeadless = options.print;
|
|
configureConsoleForHeadless(isHeadless);
|
|
|
|
// Validate --format flag only works with -p/--print
|
|
if (options.format && !options.print) {
|
|
console.error(
|
|
"Error: --format flag can only be used with -p/--print flag"
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Validate format value
|
|
if (options.format && options.format !== "json") {
|
|
console.error("Error: --format currently only supports 'json'");
|
|
process.exit(1);
|
|
}
|
|
|
|
if (options.verbose) {
|
|
logger.setLevel("debug");
|
|
const logPath = logger.getLogPath();
|
|
const sessionId = logger.getSessionId();
|
|
// In headless mode, suppress these verbose logs
|
|
if (!isHeadless) {
|
|
console.log(`Verbose logging enabled (session: ${sessionId})`);
|
|
console.log(`Logs: ${logPath}`);
|
|
console.log(
|
|
`Filter this session: grep '\\[${sessionId}\\]' ${logPath}`
|
|
);
|
|
}
|
|
logger.debug("Verbose logging enabled");
|
|
}
|
|
|
|
// Check for piped input when using -p flag
|
|
if (options.print && !prompt) {
|
|
const stdinInput = readStdinSync();
|
|
if (stdinInput) {
|
|
prompt = stdinInput;
|
|
}
|
|
}
|
|
|
|
// In headless mode, ensure we have a prompt
|
|
if (options.print && !prompt) {
|
|
console.error("Error: A prompt is required when using the -p/--print flag.\n");
|
|
console.error("Usage examples:");
|
|
console.error(' cn -p "please review my current git diff"');
|
|
console.error(' echo "hello" | cn -p');
|
|
console.error(' cn -p "analyze the code in src/"');
|
|
process.exit(1);
|
|
}
|
|
|
|
// Map --print to headless mode
|
|
options.headless = options.print;
|
|
options.print = undefined;
|
|
await chat(prompt, options);
|
|
});
|
|
|
|
// Login subcommand
|
|
program
|
|
.command("login")
|
|
.description("Authenticate with Continue")
|
|
.action(async () => {
|
|
await login();
|
|
});
|
|
|
|
// Logout subcommand
|
|
program
|
|
.command("logout")
|
|
.description("Log out from Continue")
|
|
.action(async () => {
|
|
await logout();
|
|
});
|
|
|
|
// Remote subcommand
|
|
program
|
|
.command("remote [prompt]")
|
|
.description("Launch a remote instance of the cn agent")
|
|
.option(
|
|
"--url <url>",
|
|
"Connect directly to the specified URL instead of creating a new remote environment"
|
|
)
|
|
.action(async (prompt: string | undefined, options) => {
|
|
await remote(prompt, options);
|
|
});
|
|
|
|
// Serve subcommand
|
|
program
|
|
.command("serve [prompt]")
|
|
.description("Start an HTTP server with /state and /message endpoints")
|
|
.option(
|
|
"--timeout <seconds>",
|
|
"Inactivity timeout in seconds (default: 300)",
|
|
"300"
|
|
)
|
|
.option("--port <port>", "Port to run the server on (default: 8000)", "8000")
|
|
.action(async (prompt, options) => {
|
|
// Merge parent options with subcommand options
|
|
const mergedOptions = mergeParentOptions(program, options);
|
|
|
|
if (mergedOptions.verbose) {
|
|
logger.setLevel("debug");
|
|
logger.debug("Verbose logging enabled");
|
|
}
|
|
|
|
await serve(prompt, mergedOptions);
|
|
});
|
|
|
|
// Remote test subcommand (for development)
|
|
program
|
|
.command("remote-test [prompt]")
|
|
.description("Test remote TUI mode with a local server")
|
|
.option("--url <url>", "Server URL (default: http://localhost:8000)")
|
|
.action(async (prompt: string | undefined, options) => {
|
|
await remoteTest(prompt, options.url);
|
|
});
|
|
|
|
// Handle unknown commands
|
|
program.on("command:*", () => {
|
|
console.error(`Error: Unknown command '${program.args.join(" ")}'\n`);
|
|
program.outputHelp();
|
|
process.exit(1);
|
|
});
|
|
|
|
// Parse arguments and handle errors
|
|
try {
|
|
program.parse();
|
|
} catch (error) {
|
|
console.error(error);
|
|
process.exit(1);
|
|
} |