mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 10:14:22 +01:00
added opencode serve command
This commit is contained in:
50
packages/opencode/src/cli/cmd/serve.ts
Normal file
50
packages/opencode/src/cli/cmd/serve.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import { App } from "../../app/app"
|
||||||
|
import { Provider } from "../../provider/provider"
|
||||||
|
import { Server } from "../../server/server"
|
||||||
|
import { Share } from "../../share/share"
|
||||||
|
import { cmd } from "./cmd"
|
||||||
|
|
||||||
|
export const ServeCommand = cmd({
|
||||||
|
command: "serve",
|
||||||
|
builder: (yargs) =>
|
||||||
|
yargs
|
||||||
|
.option("port", {
|
||||||
|
alias: ["p"],
|
||||||
|
type: "number",
|
||||||
|
describe: "port to listen on",
|
||||||
|
default: 4096,
|
||||||
|
})
|
||||||
|
.option("hostname", {
|
||||||
|
alias: ["h"],
|
||||||
|
type: "string",
|
||||||
|
describe: "hostname to listen on",
|
||||||
|
default: "127.0.0.1",
|
||||||
|
}),
|
||||||
|
describe: "starts a headless opencode server",
|
||||||
|
handler: async (args) => {
|
||||||
|
const cwd = process.cwd()
|
||||||
|
await App.provide({ cwd }, async () => {
|
||||||
|
const providers = await Provider.list()
|
||||||
|
if (Object.keys(providers).length === 0) {
|
||||||
|
return "needs_provider"
|
||||||
|
}
|
||||||
|
|
||||||
|
const hostname = args.hostname
|
||||||
|
const port = args.port
|
||||||
|
|
||||||
|
await Share.init()
|
||||||
|
const server = Server.listen({
|
||||||
|
port,
|
||||||
|
hostname,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
`opencode server listening on http://${server.hostname}:${server.port}`,
|
||||||
|
)
|
||||||
|
|
||||||
|
await new Promise(() => {})
|
||||||
|
|
||||||
|
server.stop()
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
@@ -21,13 +21,13 @@ import { Bus } from "./bus"
|
|||||||
import { Config } from "./config/config"
|
import { Config } from "./config/config"
|
||||||
import { NamedError } from "./util/error"
|
import { NamedError } from "./util/error"
|
||||||
import { FormatError } from "./cli/error"
|
import { FormatError } from "./cli/error"
|
||||||
|
import { ServeCommand } from "./cli/cmd/serve"
|
||||||
|
|
||||||
const cancel = new AbortController()
|
const cancel = new AbortController()
|
||||||
|
|
||||||
const cli = yargs(hideBin(process.argv))
|
const cli = yargs(hideBin(process.argv))
|
||||||
.scriptName("opencode")
|
.scriptName("opencode")
|
||||||
.help("help", "show help")
|
.help("help", "show help")
|
||||||
.alias("help", "h")
|
|
||||||
.version("version", "show version number", Installation.VERSION)
|
.version("version", "show version number", Installation.VERSION)
|
||||||
.alias("version", "v")
|
.alias("version", "v")
|
||||||
.option("print-logs", {
|
.option("print-logs", {
|
||||||
@@ -61,10 +61,14 @@ const cli = yargs(hideBin(process.argv))
|
|||||||
}
|
}
|
||||||
|
|
||||||
await Share.init()
|
await Share.init()
|
||||||
const server = Server.listen()
|
const server = Server.listen({
|
||||||
|
port: 0,
|
||||||
|
})
|
||||||
|
|
||||||
let cmd = ["go", "run", "./main.go"]
|
let cmd = ["go", "run", "./main.go"]
|
||||||
let cwd = url.fileURLToPath(new URL("../../tui/cmd/opencode", import.meta.url))
|
let cwd = url.fileURLToPath(
|
||||||
|
new URL("../../tui/cmd/opencode", import.meta.url),
|
||||||
|
)
|
||||||
if (Bun.embeddedFiles.length > 0) {
|
if (Bun.embeddedFiles.length > 0) {
|
||||||
const blob = Bun.embeddedFiles[0] as File
|
const blob = Bun.embeddedFiles[0] as File
|
||||||
let binaryName = blob.name
|
let binaryName = blob.name
|
||||||
@@ -134,6 +138,7 @@ const cli = yargs(hideBin(process.argv))
|
|||||||
.command(ScrapCommand)
|
.command(ScrapCommand)
|
||||||
.command(AuthCommand)
|
.command(AuthCommand)
|
||||||
.command(UpgradeCommand)
|
.command(UpgradeCommand)
|
||||||
|
.command(ServeCommand)
|
||||||
.fail((msg) => {
|
.fail((msg) => {
|
||||||
if (
|
if (
|
||||||
msg.startsWith("Unknown argument") ||
|
msg.startsWith("Unknown argument") ||
|
||||||
|
|||||||
@@ -579,10 +579,10 @@ export namespace Server {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export function listen() {
|
export function listen(opts: { port: number; hostname: string }) {
|
||||||
const server = Bun.serve({
|
const server = Bun.serve({
|
||||||
port: 0,
|
port: opts.port,
|
||||||
hostname: "0.0.0.0",
|
hostname: opts.hostname,
|
||||||
idleTimeout: 0,
|
idleTimeout: 0,
|
||||||
fetch: app().fetch,
|
fetch: app().fetch,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user