@@ -53,18 +53,17 @@ const ItemNotification = Schema.Struct({
5353 truncated : Schema . optionalKey ( Schema . Boolean ) ,
5454 } ) ,
5555} ) ;
56+ const TurnError = Schema . Struct ( {
57+ kind : Schema . optionalKey ( Schema . String ) ,
58+ message : Schema . String ,
59+ quota : Schema . optionalKey ( Schema . Unknown ) ,
60+ } ) ;
5661const TurnCompleted = Schema . Struct ( {
5762 sessionId : Schema . String ,
5863 turnId : Schema . String ,
5964 terminal : Schema . String ,
6065 reason : Schema . optionalKey ( Schema . String ) ,
61- error : Schema . optionalKey (
62- Schema . Struct ( {
63- kind : Schema . optionalKey ( Schema . String ) ,
64- message : Schema . String ,
65- quota : Schema . optionalKey ( Schema . Unknown ) ,
66- } ) ,
67- ) ,
66+ error : Schema . optionalKey ( TurnError ) ,
6867} ) ;
6968const ApprovalRequested = Schema . Struct ( {
7069 sessionId : Schema . String ,
@@ -88,21 +87,18 @@ const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
8887const decodeJson = Schema . decodeEffect ( Schema . fromJsonString ( Schema . Unknown ) ) ;
8988
9089type Operation = keyof ProviderTextGeneration ;
91- type TurnError = NonNullable < ( typeof TurnCompleted . Type ) [ "error" ] > ;
92- // @effect -diagnostics-next-line extendsNativeError:off - rejects a plain promise; MuseSdkError carries it as its cause.
93- class MuseTurnFailure extends Error {
94- readonly turnError : TurnError ;
95- constructor ( turnError : TurnError ) {
96- super ( turnError . message ) ;
97- this . turnError = turnError ;
90+ class MuseTurnFailure extends Schema . TaggedError < MuseTurnFailure > ( ) ( "MuseTurnFailure" , {
91+ turnError : TurnError ,
92+ } ) {
93+ override get message ( ) {
94+ return this . turnError . message ;
9895 }
9996}
97+ const isMuseTurnFailure = Schema . is ( MuseTurnFailure ) ;
10098
10199function museFailureDetail ( cause : unknown ) : string {
102100 const error =
103- isMuseSdkError ( cause ) && cause . cause instanceof MuseTurnFailure
104- ? cause . cause . turnError
105- : undefined ;
101+ isMuseSdkError ( cause ) && isMuseTurnFailure ( cause . cause ) ? cause . cause . turnError : undefined ;
106102 // Muse sends `quota` only for quota exhaustion, which it still labels `modelError`.
107103 if ( error ?. quota !== undefined || isMuseUsageLimitFailure ( error ?. message ) )
108104 return "Muse Code usage limit reached. Try again after it resets." ;
@@ -181,7 +177,9 @@ async function generateMuseText(
181177 completed . error ??
182178 ( completed . reason !== undefined ? { message : completed . reason } : undefined ) ;
183179 rejectCompletion (
184- error ? new MuseTurnFailure ( error ) : new Error ( `Muse turn ${ completed . terminal } .` ) ,
180+ error
181+ ? new MuseTurnFailure ( { turnError : error } )
182+ : new Error ( `Muse turn ${ completed . terminal } .` ) ,
185183 ) ;
186184 }
187185 } else if (
0 commit comments