opencode_offline/packages/opencode/src/cli/cmd/run.ts

166 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-06-05 23:50:54 +08:00
import type { Argv } from "yargs"
import { App } from "../../app/app"
import { Bus } from "../../bus"
import { Provider } from "../../provider/provider"
import { Session } from "../../session"
import { Share } from "../../share/share"
import { Message } from "../../session/message"
2025-06-06 00:06:21 +08:00
import { UI } from "../ui"
import { cmd } from "./cmd"
import { Flag } from "../../flag/flag"
2025-06-19 10:20:03 +08:00
import { Config } from "../../config/config"
2025-06-12 06:19:15 +08: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-20 02:57:25 +08:00
websearch: ["Search", UI.Style.TEXT_DIM_BOLD],
2025-06-12 06:19:15 +08:00
}
export const RunCommand = cmd({
2025-06-05 23:50:54 +08:00
command: "run [message..]",
2025-06-24 01:00:21 +08:00
describe: "run opencode with a message",
2025-06-05 23:50:54 +08:00
builder: (yargs: Argv) => {
return yargs
.positional("message", {
2025-06-20 04:28:03 +08:00
describe: "message to send",
2025-06-05 23:50:54 +08:00
type: "string",
array: true,
default: [],
})
.option("continue", {
alias: ["c"],
2025-06-24 01:00:21 +08:00
describe: "continue the last session",
type: "boolean",
})
2025-06-05 23:50:54 +08:00
.option("session", {
alias: ["s"],
2025-06-24 01:00:21 +08:00
describe: "session id to continue",
2025-06-05 23:50:54 +08:00
type: "string",
})
.option("share", {
type: "boolean",
2025-06-20 04:28:03 +08:00
describe: "share the session",
})
.option("model", {
type: "string",
alias: ["m"],
2025-06-24 01:00:21 +08:00
describe: "model to use in the format of provider/model",
})
2025-06-05 23:50:54 +08:00
},
handler: async (args) => {
2025-06-05 23:50:54 +08:00
const message = args.message.join(" ")
2025-06-07 11:58:09 +08:00
await App.provide(
{
cwd: process.cwd(),
},
async () => {
await Share.init()
const session = await (async () => {
if (args.continue) {
const first = await Session.list().next()
if (first.done) return
return first.value
}
if (args.session) return Session.get(args.session)
return Session.create()
})()
if (!session) {
UI.error("Session not found")
return
}
2025-06-05 23:50:54 +08:00
UI.empty()
UI.println(UI.logo())
2025-06-07 11:58:09 +08:00
UI.empty()
2025-06-11 03:43:14 +08:00
UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
2025-06-07 11:58:09 +08:00
UI.empty()
2025-06-19 10:20:03 +08: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 +
2025-06-20 04:28:03 +08:00
"~ https://opencode.ai/s/" +
session.id.slice(-8),
)
}
UI.empty()
const { providerID, modelID } = args.model
? Provider.parseModel(args.model)
: await Provider.defaultModel()
2025-06-11 03:43:14 +08:00
UI.println(
UI.Style.TEXT_NORMAL_BOLD + "@ ",
UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`,
2025-06-05 23:50:54 +08:00
)
2025-06-07 11:58:09 +08:00
UI.empty()
2025-06-05 23:50:54 +08:00
2025-06-07 11:58:09 +08:00
function printEvent(color: string, type: string, title: string) {
2025-06-11 03:43:14 +08:00
UI.println(
2025-06-07 11:58:09 +08:00
color + `|`,
UI.Style.TEXT_NORMAL +
2025-06-20 04:28:03 +08:00
UI.Style.TEXT_DIM +
` ${type.padEnd(7, " ")}`,
2025-06-07 11:58:09 +08:00
"",
UI.Style.TEXT_NORMAL + title,
)
}
2025-06-06 00:06:21 +08:00
2025-06-12 06:19:15 +08:00
Bus.subscribe(Message.Event.PartUpdated, async (evt) => {
if (evt.properties.sessionID !== session.id) return
2025-06-12 06:19:15 +08:00
const part = evt.properties.part
const message = await Session.getMessage(
evt.properties.sessionID,
evt.properties.messageID,
)
2025-06-07 11:58:09 +08:00
if (
part.type === "tool-invocation" &&
part.toolInvocation.state === "result"
) {
2025-06-12 06:19:15 +08:00
const metadata =
message.metadata.tool[part.toolInvocation.toolCallId]
const [tool, color] = TOOL[part.toolInvocation.toolName] ?? [
part.toolInvocation.toolName,
UI.Style.TEXT_INFO_BOLD,
]
printEvent(color, tool, metadata?.title || 'Unknown')
2025-06-05 23:50:54 +08:00
}
2025-06-07 11:58:09 +08:00
if (part.type === "text") {
if (part.text.includes("\n")) {
UI.empty()
2025-06-11 03:43:14 +08:00
UI.println(part.text)
2025-06-07 11:58:09 +08:00
UI.empty()
return
}
printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
}
})
await Session.chat({
sessionID: session.id,
providerID,
modelID,
parts: [
{
type: "text",
text: message,
},
],
})
UI.empty()
},
)
2025-06-05 23:50:54 +08:00
},
})