cloudaxe-opencode/packages/opencode/src/index.ts

153 lines
4.2 KiB
TypeScript
Raw Normal View History

2025-06-05 15:50:54 +00:00
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
import { RunCommand } from "./cli/cmd/run"
import { GenerateCommand } from "./cli/cmd/generate"
import { Log } from "./util/log"
2025-06-27 15:29:20 +00:00
import { AuthCommand } from "./cli/cmd/auth"
import { AgentCommand } from "./cli/cmd/agent"
2025-06-15 02:05:41 +00:00
import { UpgradeCommand } from "./cli/cmd/upgrade"
import { ModelsCommand } from "./cli/cmd/models"
2025-06-10 19:43:14 +00:00
import { UI } from "./cli/ui"
2025-06-17 05:05:05 +00:00
import { Installation } from "./installation"
2025-11-24 17:56:00 +00:00
import { NamedError } from "@opencode-ai/util/error"
2025-06-20 04:57:28 +00:00
import { FormatError } from "./cli/error"
2025-06-25 00:52:09 +00:00
import { ServeCommand } from "./cli/cmd/serve"
import { DebugCommand } from "./cli/cmd/debug"
2025-07-11 21:37:41 +00:00
import { StatsCommand } from "./cli/cmd/stats"
2025-07-13 00:25:41 +00:00
import { McpCommand } from "./cli/cmd/mcp"
2025-07-25 21:34:47 +00:00
import { GithubCommand } from "./cli/cmd/github"
import { ExportCommand } from "./cli/cmd/export"
import { ImportCommand } from "./cli/cmd/import"
2025-10-31 19:07:36 +00:00
import { AttachCommand } from "./cli/cmd/tui/attach"
import { TuiThreadCommand } from "./cli/cmd/tui/thread"
import { TuiSpawnCommand } from "./cli/cmd/tui/spawn"
import { AcpCommand } from "./cli/cmd/acp"
import { EOL } from "os"
2025-11-03 21:10:13 +00:00
import { WebCommand } from "./cli/cmd/web"
2025-11-11 23:01:16 +00:00
import { PrCommand } from "./cli/cmd/pr"
2025-06-23 01:10:05 +00:00
2025-06-25 12:41:10 +00:00
process.on("unhandledRejection", (e) => {
Log.Default.error("rejection", {
e: e instanceof Error ? e.message : e,
})
})
process.on("uncaughtException", (e) => {
Log.Default.error("exception", {
e: e instanceof Error ? e.message : e,
})
})
const cli = yargs(hideBin(process.argv))
.scriptName("opencode")
2025-06-23 17:00:21 +00:00
.help("help", "show help")
.alias("help", "h")
2025-06-23 17:00:21 +00:00
.version("version", "show version number", Installation.VERSION)
.alias("version", "v")
.option("print-logs", {
2025-06-23 17:00:21 +00:00
describe: "print logs to stderr",
type: "boolean",
})
2025-07-19 17:35:42 +00:00
.option("log-level", {
describe: "log level",
type: "string",
choices: ["DEBUG", "INFO", "WARN", "ERROR"],
})
.middleware(async (opts) => {
await Log.init({
print: process.argv.includes("--print-logs"),
2025-10-14 18:56:21 +00:00
dev: Installation.isLocal(),
2025-07-19 17:35:42 +00:00
level: (() => {
if (opts.logLevel) return opts.logLevel as Log.Level
2025-10-14 18:56:21 +00:00
if (Installation.isLocal()) return "DEBUG"
2025-07-19 17:35:42 +00:00
return "INFO"
})(),
})
2025-07-09 15:00:03 +00:00
process.env.AGENT = "1"
process.env.OPENCODE = "1"
2025-08-11 05:56:42 +00:00
Log.Default.info("opencode", {
version: Installation.VERSION,
args: process.argv.slice(2),
})
})
2025-06-11 23:00:09 +00:00
.usage("\n" + UI.logo())
.command(AcpCommand)
2025-07-13 00:25:41 +00:00
.command(McpCommand)
2025-10-31 19:07:36 +00:00
.command(TuiThreadCommand)
.command(TuiSpawnCommand)
.command(AttachCommand)
.command(RunCommand)
.command(GenerateCommand)
.command(DebugCommand)
.command(AuthCommand)
.command(AgentCommand)
2025-06-15 02:05:41 +00:00
.command(UpgradeCommand)
2025-06-25 00:52:09 +00:00
.command(ServeCommand)
2025-11-03 21:10:13 +00:00
.command(WebCommand)
.command(ModelsCommand)
2025-07-11 21:37:41 +00:00
.command(StatsCommand)
.command(ExportCommand)
.command(ImportCommand)
2025-07-25 21:34:47 +00:00
.command(GithubCommand)
2025-11-11 23:01:16 +00:00
.command(PrCommand)
2025-06-18 17:40:21 +00:00
.fail((msg) => {
if (
msg.startsWith("Unknown argument") ||
msg.startsWith("Not enough non-option arguments") ||
msg.startsWith("Invalid values:")
) {
cli.showHelp("log")
}
2025-08-04 10:03:27 +00:00
process.exit(1)
})
.strict()
try {
await cli.parse()
} catch (e) {
let data: Record<string, any> = {}
2025-06-20 04:57:28 +00:00
if (e instanceof NamedError) {
const obj = e.toObject()
Object.assign(data, {
...obj.data,
})
}
2025-06-27 20:09:33 +00:00
2025-06-20 04:57:28 +00:00
if (e instanceof Error) {
Object.assign(data, {
name: e.name,
message: e.message,
cause: e.cause?.toString(),
stack: e.stack,
2025-06-20 04:57:28 +00:00
})
2025-06-27 20:09:33 +00:00
}
if (e instanceof ResolveMessage) {
Object.assign(data, {
name: e.name,
message: e.message,
code: e.code,
specifier: e.specifier,
referrer: e.referrer,
position: e.position,
importKind: e.importKind,
2025-06-27 20:09:33 +00:00
})
2025-06-20 04:57:28 +00:00
}
Log.Default.error("fatal", data)
const formatted = FormatError(e)
if (formatted) UI.error(formatted)
if (formatted === undefined) {
UI.error("Unexpected error, check log file at " + Log.file() + " for more details" + EOL)
console.error(e)
}
2025-06-27 20:09:33 +00:00
process.exitCode = 1
} finally {
// Some subprocesses don't react properly to SIGTERM and similar signals.
// Most notably, some docker-container-based MCP servers don't handle such signals unless
// run using `docker run --init`.
// Explicitly exit to avoid any hanging subprocesses.
process.exit()
}