2
0
Эх сурвалжийг харах

allow selecting model and continuing previous session for opencode run

Dax Raad 8 сар өмнө
parent
commit
7d1a1663c8

+ 30 - 4
packages/opencode/src/cli/cmd/run.ts

@@ -33,7 +33,13 @@ export const RunCommand = cmd({
         array: true,
         default: [],
       })
+      .option("continue", {
+        alias: ["c"],
+        describe: "Continue the last session",
+        type: "boolean",
+      })
       .option("session", {
+        alias: ["s"],
         describe: "Session ID to continue",
         type: "string",
       })
@@ -41,6 +47,11 @@ export const RunCommand = cmd({
         type: "boolean",
         describe: "Share the session",
       })
+      .option("model", {
+        type: "string",
+        alias: ["m"],
+        describe: "Model to use in the format of provider/model",
+      })
   },
   handler: async (args) => {
     const message = args.message.join(" ")
@@ -50,9 +61,22 @@ export const RunCommand = cmd({
       },
       async () => {
         await Share.init()
-        const session = args.session
-          ? await Session.get(args.session)
-          : await Session.create()
+        const session = await (async () => {
+          if (args.continue) {
+            const first = await Session.list().next()
+            if (first.done) return
+            return first.value
+          }
+
+          if (args.session) return Session.get(args.session)
+
+          return Session.create()
+        })()
+
+        if (!session) {
+          UI.error("Session not found")
+          return
+        }
 
         UI.empty()
         UI.println(UI.logo())
@@ -71,7 +95,9 @@ export const RunCommand = cmd({
         }
         UI.empty()
 
-        const { providerID, modelID } = await Provider.defaultModel()
+        const { providerID, modelID } = args.model
+          ? Provider.parseModel(args.model)
+          : await Provider.defaultModel()
         UI.println(
           UI.Style.TEXT_NORMAL_BOLD + "@ ",
           UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`,

+ 4 - 0
packages/opencode/src/cli/ui.ts

@@ -71,4 +71,8 @@ export namespace UI {
       })
     })
   }
+
+  export function error(message: string) {
+    println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message)
+  }
 }

+ 14 - 1
packages/opencode/src/provider/provider.ts

@@ -272,9 +272,14 @@ export namespace Provider {
 
   export async function defaultModel() {
     const cfg = await Config.get()
+    if (cfg.model) return parseModel(cfg.model)
     const provider = await list()
       .then((val) => Object.values(val))
-      .then((x) => x.find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id)))
+      .then((x) =>
+        x.find(
+          (p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id),
+        ),
+      )
     if (!provider) throw new Error("no providers found")
     const [model] = sort(Object.values(provider.info.models))
     if (!model) throw new Error("no models found")
@@ -284,6 +289,14 @@ export namespace Provider {
     }
   }
 
+  export function parseModel(model: string) {
+    const [providerID, ...rest] = model.split("/")
+    return {
+      providerID: providerID,
+      modelID: rest.join("/"),
+    }
+  }
+
   const TOOLS = [
     BashTool,
     EditTool,