cloudaxe-opencode/packages/opencode/src/cli/cmd/run.ts

148 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-06-05 15:50:54 +00:00
import type { Argv } from "yargs"
import { Bus } from "../../bus"
import { Provider } from "../../provider/provider"
import { Session } from "../../session"
2025-06-05 16:06:21 +00:00
import { UI } from "../ui"
import { cmd } from "./cmd"
import { Flag } from "../../flag/flag"
2025-06-19 02:20:03 +00:00
import { Config } from "../../config/config"
2025-06-27 15:29:20 +00:00
import { bootstrap } from "../bootstrap"
import { MessageV2 } from "../../session/message-v2"
2025-06-11 22:19:15 +00:00
const TOOL: Record<string, [string, string]> = {
todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
todoread: ["Todo", UI.Style.TEXT_WARNING_BOLD],
bash: ["Bash", UI.Style.TEXT_DANGER_BOLD],
edit: ["Edit", UI.Style.TEXT_SUCCESS_BOLD],
glob: ["Glob", UI.Style.TEXT_INFO_BOLD],
grep: ["Grep", UI.Style.TEXT_INFO_BOLD],
list: ["List", UI.Style.TEXT_INFO_BOLD],
read: ["Read", UI.Style.TEXT_HIGHLIGHT_BOLD],
write: ["Write", UI.Style.TEXT_SUCCESS_BOLD],
2025-06-19 18:57:25 +00:00
websearch: ["Search", UI.Style.TEXT_DIM_BOLD],
2025-06-11 22:19:15 +00:00
}
export const RunCommand = cmd({
2025-06-05 15:50:54 +00:00
command: "run [message..]",
2025-06-23 17:00:21 +00:00
describe: "run opencode with a message",
2025-06-05 15:50:54 +00:00
builder: (yargs: Argv) => {
return yargs
.positional("message", {
2025-06-19 20:28:03 +00:00
describe: "message to send",
2025-06-05 15:50:54 +00:00
type: "string",
array: true,
default: [],
})
.option("continue", {
alias: ["c"],
2025-06-23 17:00:21 +00:00
describe: "continue the last session",
type: "boolean",
})
2025-06-05 15:50:54 +00:00
.option("session", {
alias: ["s"],
2025-06-23 17:00:21 +00:00
describe: "session id to continue",
2025-06-05 15:50:54 +00:00
type: "string",
})
.option("share", {
type: "boolean",
2025-06-19 20:28:03 +00:00
describe: "share the session",
})
.option("model", {
type: "string",
alias: ["m"],
2025-06-23 17:00:21 +00:00
describe: "model to use in the format of provider/model",
})
2025-06-05 15:50:54 +00:00
},
handler: async (args) => {
let message = args.message.join(" ")
2025-07-07 20:32:48 +00:00
if (!process.stdin.isTTY) message += "\n" + (await Bun.stdin.text())
2025-06-27 15:29:20 +00:00
await bootstrap({ cwd: process.cwd() }, async () => {
const session = await (async () => {
if (args.continue) {
const first = await Session.list().next()
if (first.done) return
return first.value
}
2025-06-27 15:29:20 +00:00
if (args.session) return Session.get(args.session)
2025-06-27 15:29:20 +00:00
return Session.create()
})()
2025-06-05 15:50:54 +00:00
2025-06-27 15:29:20 +00:00
if (!session) {
UI.error("Session not found")
return
}
2025-06-27 15:29:20 +00:00
const isPiped = !process.stdout.isTTY
2025-06-27 15:29:20 +00:00
UI.empty()
UI.println(UI.logo())
UI.empty()
2025-07-07 20:41:28 +00:00
const displayMessage = message.length > 300 ? message.slice(0, 300) + "..." : message
UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", displayMessage)
2025-06-27 15:29:20 +00:00
UI.empty()
2025-06-27 15:29:20 +00:00
const cfg = await Config.get()
if (cfg.autoshare || Flag.OPENCODE_AUTO_SHARE || args.share) {
await Session.share(session.id)
UI.println(UI.Style.TEXT_INFO_BOLD + "~ https://opencode.ai/s/" + session.id.slice(-8))
2025-06-27 15:29:20 +00:00
}
UI.empty()
2025-06-05 15:50:54 +00:00
const { providerID, modelID } = args.model ? Provider.parseModel(args.model) : await Provider.defaultModel()
UI.println(UI.Style.TEXT_NORMAL_BOLD + "@ ", UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`)
2025-06-27 15:29:20 +00:00
UI.empty()
function printEvent(color: string, type: string, title: string) {
UI.println(
color + `|`,
UI.Style.TEXT_NORMAL + UI.Style.TEXT_DIM + ` ${type.padEnd(7, " ")}`,
"",
UI.Style.TEXT_NORMAL + title,
)
}
2025-06-05 16:06:21 +00:00
Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
2025-06-27 15:29:20 +00:00
if (evt.properties.sessionID !== session.id) return
const part = evt.properties.part
2025-06-11 22:19:15 +00:00
if (part.type === "tool" && part.state.status === "completed") {
const [tool, color] = TOOL[part.tool] ?? [part.tool, UI.Style.TEXT_INFO_BOLD]
printEvent(color, tool, part.state.title || "Unknown")
2025-06-27 15:29:20 +00:00
}
2025-06-05 15:50:54 +00:00
2025-06-27 15:29:20 +00:00
if (part.type === "text") {
if (part.text.includes("\n")) {
UI.empty()
UI.println(part.text)
UI.empty()
return
2025-06-07 03:58:09 +00:00
}
2025-06-27 15:29:20 +00:00
printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
}
})
2025-06-27 15:29:20 +00:00
const result = await Session.chat({
sessionID: session.id,
providerID,
modelID,
parts: [
{
type: "text",
text: message,
},
],
})
2025-06-27 15:29:20 +00:00
if (isPiped) {
const match = result.parts.findLast((x) => x.type === "text")
if (match) process.stdout.write(match.text)
}
UI.empty()
})
2025-06-05 15:50:54 +00:00
},
})