@@ -152,6 +152,8 @@ void ACPClient::processResponse( const json& msg ) {
152152
153153 if ( handler ) {
154154 handler ( id, msg );
155+ } else if ( msg.contains ( " error" ) && onError ) {
156+ onError ( ResponseError ( msg[" error" ] ) );
155157 }
156158}
157159
@@ -194,54 +196,74 @@ void ACPClient::sendError( const json& id, int code, const std::string& message
194196 }
195197}
196198
197- void ACPClient::initialize ( const InitializeRequest& req,
198- const std::function<void ( const InitializeResponse& )>& cb ) {
199+ void ACPClient::initialize (
200+ const InitializeRequest& req,
201+ const std::function<void ( const InitializeResponse&, const std::optional<ResponseError>& )>&
202+ cb ) {
199203 write ( { { " method" , " initialize" }, { " params" , req.toJson () } },
200204 [this , cb]( const IdType&, const json& resp ) {
201205 if ( resp.contains ( " result" ) ) {
202206 mReady = true ;
203207 if ( cb )
204- cb ( InitializeResponse ( resp[" result" ] ) );
208+ cb ( InitializeResponse ( resp[" result" ] ), std::nullopt );
209+ } else if ( resp.contains ( " error" ) ) {
210+ if ( cb )
211+ cb ( {}, ResponseError ( resp[" error" ] ) );
205212 }
206213 } );
207214}
208215
209- void ACPClient::newSession ( const NewSessionRequest& req,
210- const std::function<void ( const NewSessionResponse& )>& cb ) {
216+ void ACPClient::newSession (
217+ const NewSessionRequest& req,
218+ const std::function<void ( const NewSessionResponse&, const std::optional<ResponseError>& )>&
219+ cb ) {
211220 write ( { { " method" , " session/new" }, { " params" , req.toJson () } },
212221 [cb]( const IdType&, const json& resp ) {
213222 if ( resp.contains ( " result" ) && cb ) {
214- cb ( NewSessionResponse ( resp[" result" ] ) );
223+ cb ( NewSessionResponse ( resp[" result" ] ), std::nullopt );
224+ } else if ( resp.contains ( " error" ) && cb ) {
225+ cb ( {}, ResponseError ( resp[" error" ] ) );
215226 }
216227 } );
217228}
218229
219- void ACPClient::loadSession ( const LoadSessionRequest& req,
220- const std::function<void ( const LoadSessionResponse& )>& cb ) {
230+ void ACPClient::loadSession (
231+ const LoadSessionRequest& req,
232+ const std::function<void ( const LoadSessionResponse&, const std::optional<ResponseError>& )>&
233+ cb ) {
221234 write ( { { " method" , " session/load" }, { " params" , req.toJson () } },
222235 [cb]( const IdType&, const json& resp ) {
223236 if ( resp.contains ( " result" ) && cb ) {
224- cb ( LoadSessionResponse ( resp[" result" ] ) );
237+ cb ( LoadSessionResponse ( resp[" result" ] ), std::nullopt );
238+ } else if ( resp.contains ( " error" ) && cb ) {
239+ cb ( {}, ResponseError ( resp[" error" ] ) );
225240 }
226241 } );
227242}
228243
229- void ACPClient::listSessions ( const ListSessionsRequest& req,
230- const std::function<void ( const ListSessionsResponse& )>& cb ) {
244+ void ACPClient::listSessions (
245+ const ListSessionsRequest& req,
246+ const std::function<void ( const ListSessionsResponse&, const std::optional<ResponseError>& )>&
247+ cb ) {
231248 write ( { { " method" , " session/list" }, { " params" , req.toJson () } },
232249 [cb]( const IdType&, const json& resp ) {
233250 if ( resp.contains ( " result" ) && cb ) {
234- cb ( ListSessionsResponse ( resp[" result" ] ) );
251+ cb ( ListSessionsResponse ( resp[" result" ] ), std::nullopt );
252+ } else if ( resp.contains ( " error" ) && cb ) {
253+ cb ( {}, ResponseError ( resp[" error" ] ) );
235254 }
236255 } );
237256}
238257
239- void ACPClient::prompt ( const PromptRequest& req,
240- const std::function<void ( const PromptResponse& )>& cb ) {
258+ void ACPClient::prompt (
259+ const PromptRequest& req,
260+ const std::function<void ( const PromptResponse&, const std::optional<ResponseError>& )>& cb ) {
241261 write ( { { " method" , " session/prompt" }, { " params" , req.toJson () } },
242262 [cb]( const IdType&, const json& resp ) {
243263 if ( resp.contains ( " result" ) && cb ) {
244- cb ( PromptResponse ( resp[" result" ] ) );
264+ cb ( PromptResponse ( resp[" result" ] ), std::nullopt );
265+ } else if ( resp.contains ( " error" ) && cb ) {
266+ cb ( {}, ResponseError ( resp[" error" ] ) );
245267 }
246268 } );
247269}
0 commit comments