Skip to content

Commit 2abd60e

Browse files
committed
extract getModel helper, simplify Promise.resolve wrapper
1 parent 1afe1f2 commit 2abd60e

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

packages/opencode/src/session/prompt.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,20 @@ export namespace SessionPrompt {
226226
yield* sessions.setTitle({ sessionID: input.session.id, title: t }).pipe(Effect.catchCause(() => Effect.void))
227227
})
228228

229+
const getModel = (providerID: ProviderID, modelID: ModelID, sessionID: SessionID) =>
230+
Effect.promise(() =>
231+
Provider.getModel(providerID, modelID).catch((e) => {
232+
if (Provider.ModelNotFoundError.isInstance(e)) {
233+
const hint = e.data.suggestions?.length ? ` Did you mean: ${e.data.suggestions.join(", ")}?` : ""
234+
Bus.publish(Session.Event.Error, {
235+
sessionID,
236+
error: new NamedError.Unknown({ message: `Model not found: ${e.data.providerID}/${e.data.modelID}.${hint}` }).toObject(),
237+
})
238+
}
239+
throw e
240+
}),
241+
)
242+
229243
const prompt = Effect.fn("SessionPrompt.prompt")(function* (input: PromptInput) {
230244
const session = yield* sessions.get(input.sessionID)
231245
yield* Effect.promise(() => SessionRevert.cleanup(session))
@@ -298,20 +312,7 @@ export namespace SessionPrompt {
298312
history: msgs,
299313
}).pipe(Effect.ignore, Effect.forkIn(scope))
300314

301-
const model = yield* Effect.promise(() =>
302-
Provider.getModel(lastUser!.model.providerID, lastUser!.model.modelID).catch((e) => {
303-
if (Provider.ModelNotFoundError.isInstance(e)) {
304-
const hint = e.data.suggestions?.length ? ` Did you mean: ${e.data.suggestions.join(", ")}?` : ""
305-
Bus.publish(Session.Event.Error, {
306-
sessionID,
307-
error: new NamedError.Unknown({
308-
message: `Model not found: ${e.data.providerID}/${e.data.modelID}.${hint}`,
309-
}).toObject(),
310-
})
311-
}
312-
throw e
313-
}),
314-
)
315+
const model = yield* getModel(lastUser!.model.providerID, lastUser!.model.modelID, sessionID)
315316
const task = tasks.pop()
316317

317318
if (task?.type === "subtask") {
@@ -596,7 +597,7 @@ export namespace SessionPrompt {
596597

597598
const raw = input.arguments.match(argsRegex) ?? []
598599
const args = raw.map((arg) => arg.replace(quoteTrimRegex, ""))
599-
const templateCommand = yield* Effect.promise(() => Promise.resolve(cmd.template))
600+
const templateCommand = yield* Effect.promise(async () => cmd.template)
600601

601602
const placeholders = templateCommand.match(placeholderRegex) ?? []
602603
let last = 0
@@ -642,20 +643,7 @@ export namespace SessionPrompt {
642643
return await lastModelImpl(input.sessionID)
643644
})
644645

645-
yield* Effect.promise(() =>
646-
Provider.getModel(taskModel.providerID, taskModel.modelID).catch((e) => {
647-
if (Provider.ModelNotFoundError.isInstance(e)) {
648-
const hint = e.data.suggestions?.length ? ` Did you mean: ${e.data.suggestions.join(", ")}?` : ""
649-
Bus.publish(Session.Event.Error, {
650-
sessionID: input.sessionID,
651-
error: new NamedError.Unknown({
652-
message: `Model not found: ${e.data.providerID}/${e.data.modelID}.${hint}`,
653-
}).toObject(),
654-
})
655-
}
656-
throw e
657-
}),
658-
)
646+
yield* getModel(taskModel.providerID, taskModel.modelID, input.sessionID)
659647

660648
const agent = yield* agents.get(agentName)
661649
if (!agent) {

0 commit comments

Comments
 (0)