opencode
opencode copied to clipboard
fix: publish session.error event for invalid model selection
Fix: Publish session.error event for invalid model selection
Problem
When sending a message via the API with an invalid provider or model ID, the error was silently swallowed. API consumers had no way to know something went wrong.
What happened:
- POST to
/session/{id}/messagewith{ model: { providerID: "invalid", ... } } - HTTP response returns 200 immediately (before validation)
- User message is created successfully
- Session goes
busy→idlewith no error indication - No
session.errorevent is published to/event
Root cause: The Provider.getModel() call happens inside loop() after the HTTP response has already started streaming. When it throws ModelNotFoundError, the error propagates up but is never converted to a session.error event.
Solution
Added error handling around Provider.getModel() in loop() to publish a session.error event before re-throwing. This matches the existing pattern used in the command() function.
After this fix:
data: {"type":"session.error","properties":{"sessionID":"ses_...","error":{"name":"UnknownError","data":{"message":"Model not found: invalid/some-model. Did you mean: anthropic, openai, ...?"}}}}
Changes
-
packages/opencode/src/session/prompt.ts: WrapProvider.getModel()in try-catch, publishsession.erroronModelNotFoundError
Fixes #8460