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

133 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-06-05 15:50:54 +00: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-05 16:06:21 +00:00
import { UI } from "../ui"
2025-06-06 03:42:04 +00:00
import { VERSION } from "../version"
2025-06-05 15:50:54 +00:00
2025-06-11 22:19:15 +00:00
const COLOR = [
UI.Style.TEXT_SUCCESS_BOLD,
UI.Style.TEXT_INFO_BOLD,
UI.Style.TEXT_HIGHLIGHT_BOLD,
UI.Style.TEXT_WARNING_BOLD,
]
const TOOL: Record<string, [string, string]> = {
opencode_todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
opencode_todoread: ["Todo", UI.Style.TEXT_WARNING_BOLD],
opencode_bash: ["Bash", UI.Style.TEXT_DANGER_BOLD],
opencode_edit: ["Edit", UI.Style.TEXT_SUCCESS_BOLD],
opencode_glob: ["Glob", UI.Style.TEXT_INFO_BOLD],
opencode_grep: ["Grep", UI.Style.TEXT_INFO_BOLD],
opencode_list: ["List", UI.Style.TEXT_INFO_BOLD],
opencode_read: ["Read", UI.Style.TEXT_HIGHLIGHT_BOLD],
opencode_write: ["Write", UI.Style.TEXT_SUCCESS_BOLD],
}
2025-06-05 15:50:54 +00:00
export const RunCommand = {
command: "run [message..]",
describe: "Run OpenCode with a message",
builder: (yargs: Argv) => {
return yargs
.positional("message", {
describe: "Message to send",
type: "string",
array: true,
default: [],
})
.option("session", {
describe: "Session ID to continue",
type: "string",
})
},
2025-06-07 03:58:09 +00:00
handler: async (args: {
message: string[]
session?: string
printLogs?: boolean
}) => {
2025-06-05 15:50:54 +00:00
const message = args.message.join(" ")
2025-06-07 03:58:09 +00:00
await App.provide(
{
cwd: process.cwd(),
version: VERSION,
2025-06-07 03:58:09 +00:00
},
async () => {
await Share.init()
const session = args.session
? await Session.get(args.session)
: await Session.create()
2025-06-05 15:50:54 +00:00
2025-06-10 19:43:14 +00:00
UI.println(UI.Style.TEXT_HIGHLIGHT_BOLD + "◍ OpenCode", VERSION)
2025-06-07 03:58:09 +00:00
UI.empty()
2025-06-10 19:43:14 +00:00
UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", message)
2025-06-07 03:58:09 +00:00
UI.empty()
2025-06-10 19:43:14 +00:00
UI.println(
2025-06-07 03:58:09 +00:00
UI.Style.TEXT_INFO_BOLD +
"~ https://dev.opencode.ai/s/" +
2025-06-07 03:58:09 +00:00
session.id.slice(-8),
2025-06-05 15:50:54 +00:00
)
2025-06-07 03:58:09 +00:00
UI.empty()
2025-06-05 15:50:54 +00:00
2025-06-07 03:58:09 +00:00
function printEvent(color: string, type: string, title: string) {
2025-06-10 19:43:14 +00:00
UI.println(
2025-06-07 03:58:09 +00:00
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
2025-06-11 22:19:15 +00:00
Bus.subscribe(Message.Event.PartUpdated, async (evt) => {
const part = evt.properties.part
const message = await Session.getMessage(
evt.properties.sessionID,
evt.properties.messageID,
)
2025-06-07 03:58:09 +00:00
if (
part.type === "tool-invocation" &&
part.toolInvocation.state === "result"
) {
2025-06-11 22:19:15 +00: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)
2025-06-05 15:50:54 +00:00
}
2025-06-07 03:58:09 +00:00
if (part.type === "text") {
if (part.text.includes("\n")) {
UI.empty()
2025-06-10 19:43:14 +00:00
UI.println(part.text)
2025-06-07 03:58:09 +00:00
UI.empty()
return
}
printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
}
})
const { providerID, modelID } = await Provider.defaultModel()
await Session.chat({
sessionID: session.id,
providerID,
modelID,
parts: [
{
type: "text",
text: message,
},
],
})
UI.empty()
},
)
2025-06-05 15:50:54 +00:00
},
}