mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
Co-authored-by: Aiden Cline <aiden@anoma.ly> Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
196 lines
8 KiB
TypeScript
Executable file
196 lines
8 KiB
TypeScript
Executable file
#!/usr/bin/env bun
|
|
|
|
import { ensurePluginRuntime } from "@opencode/plugin/runtime"
|
|
import { NodeRuntime, NodeServices } from "@effect/platform-node"
|
|
import { Cause, Effect } from "effect"
|
|
import { defaultTeardown, getErrorReported } from "effect/Runtime"
|
|
import { Commands } from "./commands/commands"
|
|
import { Runtime } from "./framework/runtime"
|
|
import { Observability } from "@opencode/util/observability"
|
|
import { Updater } from "./services/updater"
|
|
import { OPENCODE_ARTIFACT, OPENCODE_CHANNEL, OPENCODE_LOCAL, OPENCODE_VERSION } from "./version"
|
|
import { LayerNode } from "@opencode/util/effect/layer-node"
|
|
import { Global } from "@opencode/util/global"
|
|
import { AppProcess } from "@opencode/util/process"
|
|
import { Config } from "./config"
|
|
import { Npm } from "@opencode/util/npm"
|
|
import { EffectFlock } from "@opencode/util/effect-flock"
|
|
import { Heap } from "./heap"
|
|
import { CpuProfile } from "./cpu-profile"
|
|
import { ShutdownDeadline } from "./shutdown-deadline"
|
|
|
|
// Keep requests to the local service off any proxy. Bun reads lowercase `no_proxy` alone whenever it is set, so both
|
|
// spellings get the same merged list.
|
|
const noProxy = [
|
|
...new Set(
|
|
[process.env.NO_PROXY, process.env.no_proxy, "127.0.0.1,localhost,::1"].flatMap((value) =>
|
|
(value ?? "").split(",").flatMap((entry) => entry.trim() || []),
|
|
),
|
|
),
|
|
].join(",")
|
|
|
|
process.env.NO_PROXY = noProxy
|
|
process.env.no_proxy = noProxy
|
|
|
|
ensurePluginRuntime()
|
|
|
|
if (process.env.OPENCODE_SSH_ASKPASS_PORT) {
|
|
const { askpass } = await import("./ssh-askpass")
|
|
process.exit(await Effect.runPromise(askpass.pipe(Effect.provide(NodeServices.layer))))
|
|
}
|
|
|
|
let terminated = false
|
|
|
|
// `service set <key> <value>` and `service set env <name> <value>` carry secrets such as the password or a provider
|
|
// key; keep values out of the logged arguments. Flags and their values may come first, so find the pair anywhere.
|
|
const args = process.argv.slice(2)
|
|
const set = args.findIndex((arg, index) => arg === "service" && args[index + 1] === "set")
|
|
const kept = set === -1 ? args.length : set + (args[set + 2] === "env" ? 4 : 3)
|
|
const loggedArgs = args.map((arg, index) => (index < kept ? arg : "<redacted>"))
|
|
|
|
const Handlers = Runtime.handlers(Commands, {
|
|
$: () => import("./commands/handlers/default"),
|
|
upgrade: () => import("./commands/handlers/upgrade"),
|
|
uninstall: () => import("./commands/handlers/uninstall"),
|
|
acp: () => import("./commands/handlers/acp"),
|
|
api: () => import("./commands/handlers/api"),
|
|
auth: {
|
|
list: () => import("./commands/handlers/auth/list"),
|
|
login: () => import("./commands/handlers/auth/login"),
|
|
logout: () => import("./commands/handlers/auth/logout"),
|
|
export: () => import("./commands/handlers/auth/export"),
|
|
import: () => import("./commands/handlers/auth/import"),
|
|
switch: () => import("./commands/handlers/auth/switch"),
|
|
},
|
|
login: () => import("./commands/handlers/auth/login"),
|
|
debug: {
|
|
agents: () => import("./commands/handlers/debug/agents"),
|
|
config: () => import("./commands/handlers/debug/config"),
|
|
"heap-dump": () => import("./commands/handlers/debug/heap-dump"),
|
|
paths: () => import("./commands/handlers/debug/paths"),
|
|
},
|
|
mcp: {
|
|
list: () => import("./commands/handlers/mcp/list"),
|
|
add: () => import("./commands/handlers/mcp/add"),
|
|
auth: () => import("./commands/handlers/mcp/auth"),
|
|
logout: () => import("./commands/handlers/mcp/logout"),
|
|
},
|
|
plugin: {
|
|
list: () => import("./commands/handlers/plugin/list"),
|
|
add: () => import("./commands/handlers/plugin/add"),
|
|
check: () => import("./commands/handlers/plugin/check"),
|
|
update: () => import("./commands/handlers/plugin/update"),
|
|
remove: () => import("./commands/handlers/plugin/remove"),
|
|
},
|
|
models: () => import("./commands/handlers/models"),
|
|
stats: () => import("./commands/handlers/stats"),
|
|
mini: () => import("./commands/handlers/mini"),
|
|
run: () => import("./commands/handlers/run"),
|
|
machine: {
|
|
add: () => import("./commands/handlers/machine/add"),
|
|
list: () => import("./commands/handlers/machine/list"),
|
|
remove: () => import("./commands/handlers/machine/remove"),
|
|
},
|
|
pair: () => import("./commands/handlers/pair"),
|
|
reload: () => import("./commands/handlers/reload"),
|
|
session: {
|
|
list: () => import("./commands/handlers/session/list"),
|
|
delete: () => import("./commands/handlers/session/delete"),
|
|
export: () => import("./commands/handlers/session/export"),
|
|
import: () => import("./commands/handlers/session/import"),
|
|
},
|
|
service: {
|
|
start: () => import("./commands/handlers/service/start"),
|
|
restart: () => import("./commands/handlers/service/restart"),
|
|
status: () => import("./commands/handlers/service/status"),
|
|
stop: () => import("./commands/handlers/service/stop"),
|
|
get: () => import("./commands/handlers/service/get"),
|
|
set: () => import("./commands/handlers/service/set"),
|
|
unset: () => import("./commands/handlers/service/unset"),
|
|
},
|
|
serve: () => import("./commands/handlers/serve"),
|
|
})
|
|
|
|
Effect.gen(function* () {
|
|
yield* Heap.listen
|
|
yield* CpuProfile.listen
|
|
yield* ShutdownDeadline.install()
|
|
// runMain reports every signal interruption as 130; remember a SIGTERM so the exit code is 143.
|
|
process.once("SIGTERM", () => {
|
|
terminated = true
|
|
})
|
|
const runFork = Effect.runForkWith(yield* Effect.context<never>())
|
|
const uncaughtException = (cause: Error, origin: "uncaughtException" | "unhandledRejection") => {
|
|
runFork(Effect.logError("uncaught exception", { cause, origin }))
|
|
}
|
|
const unhandledRejection = (cause: unknown) => {
|
|
runFork(Effect.logError("unhandled rejection", { cause }))
|
|
}
|
|
process.on("uncaughtException", uncaughtException)
|
|
process.on("unhandledRejection", unhandledRejection)
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.sync(() => {
|
|
process.off("uncaughtException", uncaughtException)
|
|
process.off("unhandledRejection", unhandledRejection)
|
|
}),
|
|
)
|
|
yield* Effect.logInfo("cli starting", {
|
|
version: OPENCODE_VERSION,
|
|
channel: OPENCODE_CHANNEL,
|
|
local: OPENCODE_LOCAL,
|
|
args: loggedArgs,
|
|
})
|
|
return yield* Runtime.run(Commands, Handlers, { version: OPENCODE_VERSION })
|
|
}).pipe(
|
|
Effect.catchCause((cause) =>
|
|
Effect.logError("cli process failed", {
|
|
cause,
|
|
args: loggedArgs,
|
|
}).pipe(Effect.andThen(Effect.failCause(cause))),
|
|
),
|
|
Effect.annotateLogs({ role: "cli" }),
|
|
Effect.provide(Config.layer),
|
|
Effect.provide(Updater.layer),
|
|
Effect.provide(
|
|
LayerNode.compile(LayerNode.group([Global.node, AppProcess.node, Npm.node, EffectFlock.node]), {
|
|
replacements: [
|
|
Global.node.replace(
|
|
Global.layerWith(process.env.OPENCODE_CONFIG_DIR ? { config: process.env.OPENCODE_CONFIG_DIR } : {}),
|
|
),
|
|
],
|
|
}),
|
|
),
|
|
Effect.provide(
|
|
Observability.layer({
|
|
endpoint: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
|
|
headers: process.env.OTEL_EXPORTER_OTLP_HEADERS,
|
|
client: process.env.OPENCODE_CLIENT ?? OPENCODE_ARTIFACT,
|
|
version: OPENCODE_VERSION,
|
|
channel: OPENCODE_CHANNEL,
|
|
}),
|
|
),
|
|
Effect.provide(NodeServices.layer),
|
|
Effect.scoped,
|
|
// runMain's default reporter logs the fatal cause to stdout. Write it to stderr instead: the
|
|
// desktop and `Service.ensure` only capture stderr from `serve --service`, so this is the only
|
|
// channel through which a startup failure's reason reaches the user.
|
|
Effect.tapCause((cause) =>
|
|
Effect.sync(() => {
|
|
if (Cause.hasInterruptsOnly(cause)) return
|
|
if (!getErrorReported(Cause.squash(cause))) return
|
|
process.stderr.write(Cause.pretty(cause) + "\n")
|
|
}),
|
|
),
|
|
NodeRuntime.runMain({
|
|
disableErrorReporting: true,
|
|
teardown: (exit) =>
|
|
defaultTeardown(exit, (code) => {
|
|
// Commands report failures through process.exitCode.
|
|
const status = code === 0 ? (process.exitCode ?? 0) : code === 130 && terminated ? 143 : code
|
|
// process.exit drops output still queued for a pipe; ending the streams waits for it.
|
|
void Promise.all(
|
|
[process.stdout, process.stderr].map((stream) => new Promise<void>((resolve) => stream.end(() => resolve()))),
|
|
).then(() => process.exit(status))
|
|
}),
|
|
}),
|
|
)
|