cloudaxe-opencode/packages/opencode/src/index.ts

121 lines
3.5 KiB
TypeScript
Raw Normal View History

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 { Message } from "./session/message"
2025-05-31 01:26:53 +00:00
import { Global } from "./global"
2025-05-31 20:05:12 +00:00
import { Provider } from "./provider/provider"
2025-05-18 01:31:42 +00:00
2025-05-31 22:10:49 +00:00
declare global {
const OPENCODE_VERSION: string
}
2025-05-31 00:47:56 +00:00
const cli = cac("opencode")
const version = typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev"
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 () => {
await App.provide({ cwd: process.cwd(), version }, async () => {
2025-05-31 00:47:56 +00:00
await Share.init()
2025-05-31 01:26:53 +00:00
const server = Server.listen()
let cmd = ["go", "run", "./main.go"]
2025-06-02 15:40:28 +00:00
let cwd = new URL("../../tui", import.meta.url).pathname
2025-05-31 01:26:53 +00:00
if (Bun.embeddedFiles.length > 0) {
const blob = Bun.embeddedFiles[0] as File
const binary = path.join(Global.cache(), "tui", blob.name)
const file = Bun.file(binary)
if (!(await file.exists())) {
console.log("installing tui binary...")
await Bun.write(file, blob, { mode: 0o755 })
2025-05-31 18:41:00 +00:00
await fs.chmod(binary, 0o755)
2025-05-31 01:26:53 +00:00
}
cwd = process.cwd()
cmd = [binary]
}
const proc = Bun.spawn({
cmd,
cwd,
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
onExit: () => {
server.stop()
},
})
await proc.exited
await server.stop()
2025-05-31 00:47:56 +00:00
})
})
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"),
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) => {
await App.provide({ cwd: process.cwd(), version }, 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)
Bus.subscribe(Message.Event.Updated, async () => {
2025-05-31 00:47:56 +00:00
console.log("Thinking...")
})
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 20:05:12 +00:00
const [provider] = await Provider.active().then((val) =>
val.values().toArray(),
)
if (!provider) throw new Error("no providers found")
const model = provider.models[0]
if (!model) throw new Error("no models found")
console.log("using", provider.id, model.id)
2025-05-28 16:53:22 +00:00
const result = await Session.chat({
sessionID: session.id,
2025-05-31 20:05:12 +00:00
providerID: provider.id,
modelID: model.id,
2025-05-28 16:53:22 +00:00
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
}
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 22:10:49 +00:00
cli.version(typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev")
2025-05-31 00:47:56 +00:00
cli.help()
cli.parse()