2025-05-31 00:47:56 +00:00
|
|
|
import "zod-openapi/extend"
|
|
|
|
|
import { App } from "./app/app"
|
|
|
|
|
import { Server } from "./server/server"
|
|
|
|
|
import fs from "fs/promises"
|
|
|
|
|
import path from "path"
|
|
|
|
|
import { Bus } from "./bus"
|
|
|
|
|
import { Session } from "./session/session"
|
|
|
|
|
import cac from "cac"
|
|
|
|
|
import { Share } from "./share/share"
|
|
|
|
|
import { Storage } from "./storage/storage"
|
|
|
|
|
import { LLM } from "./llm/llm"
|
|
|
|
|
import { Message } from "./session/message"
|
2025-05-18 01:31:42 +00:00
|
|
|
|
2025-05-31 00:47:56 +00:00
|
|
|
const cli = cac("opencode")
|
2025-05-19 02:30:41 +00:00
|
|
|
|
2025-05-21 14:30:39 +00:00
|
|
|
cli.command("", "Start the opencode in interactive mode").action(async () => {
|
2025-05-23 20:20:21 +00:00
|
|
|
await App.provide({ directory: process.cwd() }, async () => {
|
2025-05-31 00:47:56 +00:00
|
|
|
await Share.init()
|
|
|
|
|
Server.listen()
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-05-21 02:00:00 +00:00
|
|
|
|
2025-05-21 14:30:39 +00:00
|
|
|
cli.command("generate", "Generate OpenAPI and event specs").action(async () => {
|
2025-05-31 00:47:56 +00:00
|
|
|
const specs = await Server.openapi()
|
|
|
|
|
const dir = "gen"
|
|
|
|
|
await fs.rmdir(dir, { recursive: true }).catch(() => {})
|
|
|
|
|
await fs.mkdir(dir, { recursive: true })
|
2025-05-21 14:30:39 +00:00
|
|
|
await Bun.write(
|
|
|
|
|
path.join(dir, "openapi.json"),
|
2025-05-27 01:22:38 +00:00
|
|
|
JSON.stringify(specs, null, 2),
|
2025-05-31 00:47:56 +00:00
|
|
|
)
|
|
|
|
|
})
|
2025-05-19 23:29:38 +00:00
|
|
|
|
2025-05-21 14:30:39 +00:00
|
|
|
cli
|
|
|
|
|
.command("run [...message]", "Run a chat message")
|
2025-05-29 18:29:00 +00:00
|
|
|
.option("--session <id>", "Session ID")
|
|
|
|
|
.action(async (message: string[], options) => {
|
2025-05-21 14:30:39 +00:00
|
|
|
await App.provide({ directory: process.cwd() }, async () => {
|
2025-05-31 00:47:56 +00:00
|
|
|
await Share.init()
|
2025-05-29 18:29:00 +00:00
|
|
|
const session = options.session
|
|
|
|
|
? await Session.get(options.session)
|
2025-05-31 00:47:56 +00:00
|
|
|
: await Session.create()
|
|
|
|
|
console.log("Session:", session.id)
|
2025-05-27 06:17:35 +00:00
|
|
|
|
2025-05-29 18:16:15 +00:00
|
|
|
Bus.subscribe(Message.Event.Updated, async (message) => {
|
2025-05-31 00:47:56 +00:00
|
|
|
console.log("Thinking...")
|
|
|
|
|
})
|
2025-05-27 06:17:35 +00:00
|
|
|
|
2025-05-30 18:45:10 +00:00
|
|
|
const unsub = Bus.subscribe(Session.Event.Updated, async (message) => {
|
|
|
|
|
if (message.properties.info.share?.url)
|
2025-05-31 00:47:56 +00:00
|
|
|
console.log("Share:", message.properties.info.share.url)
|
|
|
|
|
unsub()
|
|
|
|
|
})
|
2025-05-30 18:45:10 +00:00
|
|
|
|
2025-05-31 00:47:56 +00:00
|
|
|
const providers = await LLM.providers()
|
|
|
|
|
const providerID = Object.keys(providers)[0]
|
|
|
|
|
const modelID = providers[providerID].info.models[0].id
|
|
|
|
|
console.log("using", providerID, modelID)
|
2025-05-28 16:53:22 +00:00
|
|
|
const result = await Session.chat({
|
|
|
|
|
sessionID: session.id,
|
|
|
|
|
providerID,
|
|
|
|
|
modelID,
|
|
|
|
|
parts: [
|
|
|
|
|
{
|
|
|
|
|
type: "text",
|
|
|
|
|
text: message.join(" "),
|
|
|
|
|
},
|
|
|
|
|
],
|
2025-05-31 00:47:56 +00:00
|
|
|
})
|
2025-05-19 23:29:38 +00:00
|
|
|
|
2025-05-21 14:30:39 +00:00
|
|
|
for (const part of result.parts) {
|
|
|
|
|
if (part.type === "text") {
|
2025-05-31 00:47:56 +00:00
|
|
|
console.log("opencode:", part.text)
|
2025-05-19 23:29:38 +00:00
|
|
|
}
|
2025-05-21 14:30:39 +00:00
|
|
|
}
|
2025-05-29 18:16:15 +00:00
|
|
|
console.log({
|
|
|
|
|
cost: result.metadata.assistant?.cost,
|
|
|
|
|
tokens: result.metadata.assistant?.tokens,
|
2025-05-31 00:47:56 +00:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-05-21 14:30:39 +00:00
|
|
|
|
2025-05-31 00:47:56 +00:00
|
|
|
cli.help()
|
|
|
|
|
cli.version("1.0.0")
|
|
|
|
|
cli.parse()
|