|
@@ -1,89 +1,73 @@
|
|
|
import { App } from "./app";
|
|
import { App } from "./app";
|
|
|
import { Server } from "./server/server";
|
|
import { Server } from "./server/server";
|
|
|
-import { Cli, Command, Option } from "clipanion";
|
|
|
|
|
import fs from "fs/promises";
|
|
import fs from "fs/promises";
|
|
|
import path from "path";
|
|
import path from "path";
|
|
|
import { Bus } from "./bus";
|
|
import { Bus } from "./bus";
|
|
|
import { Session } from "./session/session";
|
|
import { Session } from "./session/session";
|
|
|
-import { LSP } from "./lsp";
|
|
|
|
|
|
|
+import cac from "cac";
|
|
|
|
|
+import { Storage } from "./storage/storage";
|
|
|
|
|
|
|
|
-const cli = new Cli({
|
|
|
|
|
- binaryLabel: `opencode`,
|
|
|
|
|
- binaryName: `opencode`,
|
|
|
|
|
- binaryVersion: `1.0.0`,
|
|
|
|
|
-});
|
|
|
|
|
-
|
|
|
|
|
-cli.register(
|
|
|
|
|
- class extends Command {
|
|
|
|
|
- async execute() {
|
|
|
|
|
- const app = await App.create({
|
|
|
|
|
- directory: process.cwd(),
|
|
|
|
|
- });
|
|
|
|
|
|
|
+const cli = cac("opencode");
|
|
|
|
|
|
|
|
- await App.provide(app, async () => {
|
|
|
|
|
- const server = Server.listen();
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
-);
|
|
|
|
|
|
|
+cli.command("", "Start the opencode in interactive mode").action(async () => {
|
|
|
|
|
+ await App.provide({ directory: process.cwd() }, () => {
|
|
|
|
|
+ Server.listen();
|
|
|
|
|
+ });
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
-cli.register(
|
|
|
|
|
- class extends Command {
|
|
|
|
|
- static paths = [["generate"]];
|
|
|
|
|
- async execute() {
|
|
|
|
|
- const specs = await Server.openapi();
|
|
|
|
|
- const dir = "gen";
|
|
|
|
|
- await fs.rmdir(dir, { recursive: true }).catch(() => {});
|
|
|
|
|
- await fs.mkdir(dir, { recursive: true });
|
|
|
|
|
- await Bun.write(
|
|
|
|
|
- path.join(dir, "openapi.json"),
|
|
|
|
|
- JSON.stringify(specs, null, 2),
|
|
|
|
|
- );
|
|
|
|
|
- await Bun.write(
|
|
|
|
|
- path.join(dir, "event.json"),
|
|
|
|
|
- JSON.stringify(Bus.specs(), null, 2),
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
-);
|
|
|
|
|
|
|
+cli.command("generate", "Generate OpenAPI and event specs").action(async () => {
|
|
|
|
|
+ const specs = await Server.openapi();
|
|
|
|
|
+ const dir = "gen";
|
|
|
|
|
+ await fs.rmdir(dir, { recursive: true }).catch(() => {});
|
|
|
|
|
+ await fs.mkdir(dir, { recursive: true });
|
|
|
|
|
+ await Bun.write(
|
|
|
|
|
+ path.join(dir, "openapi.json"),
|
|
|
|
|
+ JSON.stringify(specs, null, 2),
|
|
|
|
|
+ );
|
|
|
|
|
+ await Bun.write(
|
|
|
|
|
+ path.join(dir, "event.json"),
|
|
|
|
|
+ JSON.stringify(Bus.specs(), null, 2),
|
|
|
|
|
+ );
|
|
|
|
|
+});
|
|
|
|
|
|
|
|
-cli.register(
|
|
|
|
|
- class extends Command {
|
|
|
|
|
- static paths = [["run"]];
|
|
|
|
|
- message = Option.Rest();
|
|
|
|
|
|
|
+cli
|
|
|
|
|
+ .command("run [...message]", "Run a chat message")
|
|
|
|
|
+ .action(async (message: string[]) => {
|
|
|
|
|
+ await App.provide({ directory: process.cwd() }, async () => {
|
|
|
|
|
+ console.log("Thinking...");
|
|
|
|
|
+ Bus.subscribe(Storage.Event.Write, (evt) => {
|
|
|
|
|
+ const splits = evt.properties.key.split("/");
|
|
|
|
|
|
|
|
- async execute() {
|
|
|
|
|
- const app = await App.create({
|
|
|
|
|
- directory: process.cwd(),
|
|
|
|
|
|
|
+ if (splits[0] === "session" && splits[1] === "message") {
|
|
|
|
|
+ console.log("opencode:", evt.properties.body);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ const session = await Session.create();
|
|
|
|
|
+ const result = await Session.chat(session.id, {
|
|
|
|
|
+ type: "text",
|
|
|
|
|
+ text: message.join(" "),
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- await App.provide(app, async () => {
|
|
|
|
|
- console.log("Thinking...");
|
|
|
|
|
- const session = await Session.create();
|
|
|
|
|
- const result = await Session.chat(session.id, {
|
|
|
|
|
- type: "text",
|
|
|
|
|
- text: this.message.join(" "),
|
|
|
|
|
- });
|
|
|
|
|
- for (const part of result.parts) {
|
|
|
|
|
- if (part.type === "text") {
|
|
|
|
|
- console.log("opencode:", part.text);
|
|
|
|
|
- }
|
|
|
|
|
- if (part.type === "tool-invocation") {
|
|
|
|
|
- console.log(
|
|
|
|
|
- "tool:",
|
|
|
|
|
- part.toolInvocation.toolName,
|
|
|
|
|
- part.toolInvocation.args,
|
|
|
|
|
- part.toolInvocation.state === "result"
|
|
|
|
|
- ? part.toolInvocation.result
|
|
|
|
|
- : "",
|
|
|
|
|
- );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ for (const part of result.parts) {
|
|
|
|
|
+ if (part.type === "text") {
|
|
|
|
|
+ console.log("opencode:", part.text);
|
|
|
}
|
|
}
|
|
|
- });
|
|
|
|
|
|
|
+ if (part.type === "tool-invocation") {
|
|
|
|
|
+ console.log(
|
|
|
|
|
+ "tool:",
|
|
|
|
|
+ part.toolInvocation.toolName,
|
|
|
|
|
+ part.toolInvocation.args,
|
|
|
|
|
+ part.toolInvocation.state === "result"
|
|
|
|
|
+ ? part.toolInvocation.result
|
|
|
|
|
+ : "",
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
process.exit(0);
|
|
process.exit(0);
|
|
|
- }
|
|
|
|
|
- },
|
|
|
|
|
-);
|
|
|
|
|
-const [_bun, _app, ...args] = process.argv;
|
|
|
|
|
-cli.runExit(args);
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+cli.help();
|
|
|
|
|
+cli.version("1.0.0");
|
|
|
|
|
+cli.parse();
|