Skip to content

Commit beb2060

Browse files
committed
allow selecting model and continuing previous session for opencode run
1 parent f1f3f8d commit beb2060

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

packages/opencode/src/cli/cmd/run.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ export const RunCommand = cmd({
3333
array: true,
3434
default: [],
3535
})
36+
.option("continue", {
37+
alias: ["c"],
38+
describe: "Continue the last session",
39+
type: "boolean",
40+
})
3641
.option("session", {
42+
alias: ["s"],
3743
describe: "Session ID to continue",
3844
type: "string",
3945
})
4046
.option("share", {
4147
type: "boolean",
4248
describe: "Share the session",
4349
})
50+
.option("model", {
51+
type: "string",
52+
alias: ["m"],
53+
describe: "Model to use in the format of provider/model",
54+
})
4455
},
4556
handler: async (args) => {
4657
const message = args.message.join(" ")
@@ -50,9 +61,22 @@ export const RunCommand = cmd({
5061
},
5162
async () => {
5263
await Share.init()
53-
const session = args.session
54-
? await Session.get(args.session)
55-
: await Session.create()
64+
const session = await (async () => {
65+
if (args.continue) {
66+
const first = await Session.list().next()
67+
if (first.done) return
68+
return first.value
69+
}
70+
71+
if (args.session) return Session.get(args.session)
72+
73+
return Session.create()
74+
})()
75+
76+
if (!session) {
77+
UI.error("Session not found")
78+
return
79+
}
5680

5781
UI.empty()
5882
UI.println(UI.logo())
@@ -71,7 +95,9 @@ export const RunCommand = cmd({
7195
}
7296
UI.empty()
7397

74-
const { providerID, modelID } = await Provider.defaultModel()
98+
const { providerID, modelID } = args.model
99+
? Provider.parseModel(args.model)
100+
: await Provider.defaultModel()
75101
UI.println(
76102
UI.Style.TEXT_NORMAL_BOLD + "@ ",
77103
UI.Style.TEXT_NORMAL + `${providerID}/${modelID}`,

packages/opencode/src/cli/ui.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ export namespace UI {
7171
})
7272
})
7373
}
74+
75+
export function error(message: string) {
76+
println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message)
77+
}
7478
}

packages/opencode/src/provider/provider.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,14 @@ export namespace Provider {
272272

273273
export async function defaultModel() {
274274
const cfg = await Config.get()
275+
if (cfg.model) return parseModel(cfg.model)
275276
const provider = await list()
276277
.then((val) => Object.values(val))
277-
.then((x) => x.find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id)))
278+
.then((x) =>
279+
x.find(
280+
(p) => !cfg.provider || Object.keys(cfg.provider).includes(p.info.id),
281+
),
282+
)
278283
if (!provider) throw new Error("no providers found")
279284
const [model] = sort(Object.values(provider.info.models))
280285
if (!model) throw new Error("no models found")
@@ -284,6 +289,14 @@ export namespace Provider {
284289
}
285290
}
286291

292+
export function parseModel(model: string) {
293+
const [providerID, ...rest] = model.split("/")
294+
return {
295+
providerID: providerID,
296+
modelID: rest.join("/"),
297+
}
298+
}
299+
287300
const TOOLS = [
288301
BashTool,
289302
EditTool,

0 commit comments

Comments
 (0)