@@ -24,6 +24,7 @@ import { Filesystem } from "@/util/filesystem"
2424import { createOpencodeClient , type OpencodeClient , type ToolPart } from "@opencode-ai/sdk/v2"
2525import { Agent } from "@/agent/agent"
2626import { Permission } from "@/permission"
27+ import { FormatError , FormatUnknownError } from "../error"
2728import { INTERACTIVE_INPUT_ERROR , resolveInteractiveStdin } from "./run/runtime.stdin"
2829
2930const runtimeTask = import ( "./run/runtime" )
@@ -82,6 +83,10 @@ function block(info: Inline, output?: string) {
8283 UI . empty ( )
8384}
8485
86+ function formatRunError ( error : unknown ) {
87+ return FormatError ( error ) ?? FormatUnknownError ( error )
88+ }
89+
8590async function tool ( part : ToolPart ) {
8691 try {
8792 const { toolInlineInfo } = await import ( "./run/tool" )
@@ -731,32 +736,39 @@ export const RunCommand = effectCmd({
731736
732737 if ( ! args . interactive ) {
733738 const events = await client . event . subscribe ( )
734- const completed = loop ( client , events )
739+ loop ( client , events ) . catch ( ( e ) => {
740+ console . error ( e )
741+ process . exit ( 1 )
742+ } )
735743
736744 if ( args . command ) {
737- await client . session . command ( {
745+ const result = await client . session . command ( {
738746 sessionID,
739747 agent,
740748 model : args . model ,
741749 command : args . command ,
742750 arguments : message ,
743751 variant : args . variant ,
744752 } )
745- const error = await completed
746- if ( error ) process . exitCode = 1
753+ if ( result . error ) {
754+ if ( ! emit ( "error" , { error : result . error } ) ) UI . error ( formatRunError ( result . error ) )
755+ process . exitCode = 1
756+ }
747757 return
748758 }
749759
750760 const model = pick ( args . model )
751- await client . session . prompt ( {
761+ const result = await client . session . prompt ( {
752762 sessionID,
753763 agent,
754764 model,
755765 variant : args . variant ,
756766 parts : [ ...files , { type : "text" , text : message } ] ,
757767 } )
758- const error = await completed
759- if ( error ) process . exitCode = 1
768+ if ( result . error ) {
769+ if ( ! emit ( "error" , { error : result . error } ) ) UI . error ( formatRunError ( result . error ) )
770+ process . exitCode = 1
771+ }
760772 return
761773 }
762774
0 commit comments