index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { App } from "./app";
  2. import { Server } from "./server/server";
  3. import fs from "fs/promises";
  4. import path from "path";
  5. import { Bus } from "./bus";
  6. import { Session } from "./session/session";
  7. import cac from "cac";
  8. import { Share } from "./share/share";
  9. import { Storage } from "./storage/storage";
  10. import { LLM } from "./llm/llm";
  11. const cli = cac("opencode");
  12. cli.command("", "Start the opencode in interactive mode").action(async () => {
  13. await App.provide({ directory: process.cwd() }, async () => {
  14. await Share.init();
  15. Server.listen();
  16. });
  17. });
  18. cli.command("generate", "Generate OpenAPI and event specs").action(async () => {
  19. const specs = await Server.openapi();
  20. const dir = "gen";
  21. await fs.rmdir(dir, { recursive: true }).catch(() => {});
  22. await fs.mkdir(dir, { recursive: true });
  23. await Bun.write(
  24. path.join(dir, "openapi.json"),
  25. JSON.stringify(specs, null, 2),
  26. );
  27. await Bun.write(
  28. path.join(dir, "event.json"),
  29. JSON.stringify(Bus.specs(), null, 2),
  30. );
  31. });
  32. cli
  33. .command("run [...message]", "Run a chat message")
  34. .action(async (message: string[]) => {
  35. await App.provide({ directory: process.cwd() }, async () => {
  36. console.log("Thinking...");
  37. await Share.init();
  38. const session = await Session.create();
  39. console.log(
  40. `Share ID: ${Share.URL.replace("api.", "")}/share?id=${session.id}`,
  41. );
  42. let index = 0;
  43. Bus.subscribe(Storage.Event.Write, async (payload) => {
  44. const [root, , type, messageID] = payload.properties.key.split("/");
  45. if (root !== "session" && type !== "message") return;
  46. const message = await Session.messages(session.id).then((x) =>
  47. x.find((x) => x.id === messageID),
  48. );
  49. if (!message) return;
  50. for (; index < message.parts.length; index++) {
  51. const part = message.parts[index];
  52. if (part.type === "text") continue;
  53. if (part.type === "step-start") continue;
  54. if (
  55. part.type === "tool-invocation" &&
  56. part.toolInvocation.state !== "result"
  57. )
  58. break;
  59. if (part.type === "tool-invocation") {
  60. console.log(`🔧 ${part.toolInvocation.toolName}`);
  61. if (
  62. part.toolInvocation.state === "result" &&
  63. "result" in part.toolInvocation
  64. ) {
  65. const result = part.toolInvocation.result;
  66. if (typeof result === "string") {
  67. const lines = result.split("\n");
  68. const truncated = lines.slice(0, 4);
  69. if (lines.length > 4) truncated.push("...");
  70. console.log(truncated.join("\n"));
  71. } else if (result && typeof result === "object") {
  72. const jsonStr = JSON.stringify(result, null, 2);
  73. const lines = jsonStr.split("\n");
  74. const truncated = lines.slice(0, 4);
  75. if (lines.length > 4) truncated.push("...");
  76. console.log(truncated.join("\n"));
  77. }
  78. }
  79. continue;
  80. }
  81. console.log(part);
  82. }
  83. });
  84. const providers = await LLM.providers();
  85. const providerID = Object.keys(providers)[0];
  86. const modelID = Object.keys(providers[providerID].info.models!)[0];
  87. console.log("using", providerID, modelID);
  88. const result = await Session.chat({
  89. sessionID: session.id,
  90. providerID,
  91. modelID,
  92. parts: [
  93. {
  94. type: "text",
  95. text: message.join(" "),
  96. },
  97. ],
  98. });
  99. for (const part of result.parts) {
  100. if (part.type === "text") {
  101. console.log("opencode:", part.text);
  102. }
  103. }
  104. });
  105. });
  106. cli.help();
  107. cli.version("1.0.0");
  108. cli.parse();