@@ -38,14 +38,14 @@ import type {
3838} from '@codebuff/common/tools/list'
3939import type { Logger } from '@codebuff/common/types/contracts/logger'
4040import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem'
41- import type { CodebuffSpawn } from '@codebuff/common/types/spawn'
4241import type {
4342 ToolResultOutput ,
4443 ToolResultPart ,
4544} from '@codebuff/common/types/messages/content-part'
4645import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
4746import type { SessionState } from '@codebuff/common/types/session-state'
4847import type { Source } from '@codebuff/common/types/source'
48+ import type { CodebuffSpawn } from '@codebuff/common/types/spawn'
4949
5050export type CodebuffClientOptions = {
5151 apiKey ?: string
@@ -194,23 +194,25 @@ export async function run({
194194 *
195195 * This includes the user'e message and pending assistant message.
196196 */
197- function getCancelledSessionState ( ) : SessionState {
197+ function getCancelledSessionState ( message : string ) : SessionState {
198198 const state = cloneDeep ( sessionState )
199199 state . mainAgentState . messageHistory . push (
200200 ...getCancelledAdditionalMessages ( {
201201 prompt,
202202 params,
203203 pendingAgentResponse,
204+ systemMessage : message ,
204205 } ) ,
205206 )
206207 return state
207208 }
208- function getCancelledRunState ( ) : RunState {
209+ function getCancelledRunState ( message ?: string ) : RunState {
210+ message = message ?? 'Run cancelled by user.'
209211 return {
210- sessionState : getCancelledSessionState ( ) ,
212+ sessionState : getCancelledSessionState ( message ) ,
211213 output : {
212214 type : 'error' ,
213- message : 'Run cancelled by user' ,
215+ message,
214216 } ,
215217 }
216218 }
@@ -416,8 +418,9 @@ export async function run({
416418 fields : [ 'id' ] ,
417419 } )
418420 if ( ! userInfo ) {
419- throw new Error ( 'No user found for key ')
421+ return getCancelledRunState ( 'Invalid API key or user not found ')
420422 }
423+
421424 const userId = userInfo . id
422425
423426 signal ?. addEventListener ( 'abort' , ( ) => {
@@ -446,7 +449,7 @@ export async function run({
446449 clientSessionId : promptId ,
447450 userId,
448451 signal : signal ?? new AbortController ( ) . signal ,
449- } )
452+ } ) . catch ( ( error ) => resolve ( getCancelledRunState ( error . message ) ) )
450453
451454 return promise
452455}
0 commit comments