@@ -616,6 +616,46 @@ export const useSendMessage = ({
616616
617617 const abortController = new AbortController ( )
618618 abortControllerRef . current = abortController
619+ abortController . signal . addEventListener ( 'abort' , ( ) => {
620+ setIsStreaming ( false )
621+ setCanProcessQueue ( true )
622+ updateChainInProgress ( false )
623+ setIsWaitingForResponse ( false )
624+ timerController . stop ( 'aborted' )
625+
626+ applyMessageUpdate ( ( prev ) =>
627+ prev . map ( ( msg ) => {
628+ if ( msg . id !== aiMessageId ) {
629+ return msg
630+ }
631+
632+ const blocks : ContentBlock [ ] = msg . blocks ? [ ...msg . blocks ] : [ ]
633+ const lastBlock = blocks [ blocks . length - 1 ]
634+
635+ if ( lastBlock && lastBlock . type === 'text' ) {
636+ const interruptedBlock : ContentBlock = {
637+ type : 'text' ,
638+ content : `${ lastBlock . content } \n\n[response interrupted]` ,
639+ }
640+ return {
641+ ...msg ,
642+ blocks : [ ...blocks . slice ( 0 , - 1 ) , interruptedBlock ] ,
643+ isComplete : true ,
644+ }
645+ }
646+
647+ const interruptionNotice : ContentBlock = {
648+ type : 'text' ,
649+ content : '[response interrupted]' ,
650+ }
651+ return {
652+ ...msg ,
653+ blocks : [ ...blocks , interruptionNotice ] ,
654+ isComplete : true ,
655+ }
656+ } ) ,
657+ )
658+ } )
619659
620660 try {
621661 // Load local agent definitions from .agents directory
@@ -633,7 +673,7 @@ export const useSendMessage = ({
633673 : agentMode === 'MAX'
634674 ? 'base2-max'
635675 : 'base2-plan'
636- const result = await client . run ( {
676+ const runState = await client . run ( {
637677 logger,
638678 agent : selectedAgentDefinition ?? agentId ?? fallbackAgent ,
639679 prompt : content ,
@@ -1056,7 +1096,8 @@ export const useSendMessage = ({
10561096 }
10571097
10581098 if ( event . type === 'tool_call' && event . toolCallId ) {
1059- const { toolCallId, toolName, input, agentId, includeToolCall } = event
1099+ const { toolCallId, toolName, input, agentId, includeToolCall } =
1100+ event
10601101
10611102 if ( toolName === 'spawn_agents' && input ?. agents ) {
10621103 const agents = Array . isArray ( input . agents ) ? input . agents : [ ]
@@ -1136,7 +1177,9 @@ export const useSendMessage = ({
11361177 toolName,
11371178 input,
11381179 agentId,
1139- ...( includeToolCall !== undefined && { includeToolCall } ) ,
1180+ ...( includeToolCall !== undefined && {
1181+ includeToolCall,
1182+ } ) ,
11401183 }
11411184
11421185 return {
@@ -1306,6 +1349,14 @@ export const useSendMessage = ({
13061349 } ,
13071350 } )
13081351
1352+ if ( runState . output . type === 'error' ) {
1353+ logger . warn (
1354+ { errorMessage : runState . output . message } ,
1355+ 'Agent run failed' ,
1356+ )
1357+ return
1358+ }
1359+
13091360 setIsStreaming ( false )
13101361 setCanProcessQueue ( true )
13111362 updateChainInProgress ( false )
@@ -1316,10 +1367,6 @@ export const useSendMessage = ({
13161367 setHasReceivedPlanResponse ( true )
13171368 }
13181369
1319- if ( ( result as any ) ?. credits !== undefined ) {
1320- actualCredits = ( result as any ) . credits
1321- }
1322-
13231370 const elapsedMs = timerResult ?. elapsedMs ?? 0
13241371 const elapsedSeconds = Math . floor ( elapsedMs / 1000 )
13251372 const completionTime =
@@ -1340,10 +1387,8 @@ export const useSendMessage = ({
13401387 ) ,
13411388 )
13421389
1343- previousRunStateRef . current = result
1390+ previousRunStateRef . current = runState
13441391 } catch ( error ) {
1345- const isAborted = error instanceof Error && error . name === 'AbortError'
1346-
13471392 logger . error (
13481393 { error : getErrorObject ( error ) } ,
13491394 'SDK client.run() failed' ,
@@ -1352,61 +1397,26 @@ export const useSendMessage = ({
13521397 setCanProcessQueue ( true )
13531398 updateChainInProgress ( false )
13541399 setIsWaitingForResponse ( false )
1355- timerController . stop ( isAborted ? 'aborted' : 'error' )
1356-
1357- if ( isAborted ) {
1358- applyMessageUpdate ( ( prev ) =>
1359- prev . map ( ( msg ) => {
1360- if ( msg . id !== aiMessageId ) {
1361- return msg
1362- }
1400+ timerController . stop ( 'error' )
13631401
1364- const blocks : ContentBlock [ ] = msg . blocks ? [ ...msg . blocks ] : [ ]
1365- const lastBlock = blocks [ blocks . length - 1 ]
1366-
1367- if ( lastBlock && lastBlock . type === 'text' ) {
1368- const interruptedBlock : ContentBlock = {
1369- type : 'text' ,
1370- content : `${ lastBlock . content } \n\n[response interrupted]` ,
1371- }
1372- return {
1402+ const errorMessage =
1403+ error instanceof Error ? error . message : 'Unknown error occurred'
1404+ applyMessageUpdate ( ( prev ) =>
1405+ prev . map ( ( msg ) =>
1406+ msg . id === aiMessageId
1407+ ? {
13731408 ...msg ,
1374- blocks : [ ...blocks . slice ( 0 , - 1 ) , interruptedBlock ] ,
1375- isComplete : true ,
1409+ content : msg . content + `\n\n**Error:** ${ errorMessage } ` ,
13761410 }
1377- }
1378-
1379- const interruptionNotice : ContentBlock = {
1380- type : 'text' ,
1381- content : '[response interrupted]' ,
1382- }
1383- return {
1384- ...msg ,
1385- blocks : [ ...blocks , interruptionNotice ] ,
1386- isComplete : true ,
1387- }
1388- } ) ,
1389- )
1390- } else {
1391- const errorMessage =
1392- error instanceof Error ? error . message : 'Unknown error occurred'
1393- applyMessageUpdate ( ( prev ) =>
1394- prev . map ( ( msg ) =>
1395- msg . id === aiMessageId
1396- ? {
1397- ...msg ,
1398- content : msg . content + `\n\n**Error:** ${ errorMessage } ` ,
1399- }
1400- : msg ,
1401- ) ,
1402- )
1411+ : msg ,
1412+ ) ,
1413+ )
14031414
1404- applyMessageUpdate ( ( prev ) =>
1405- prev . map ( ( msg ) =>
1406- msg . id === aiMessageId ? { ...msg , isComplete : true } : msg ,
1407- ) ,
1408- )
1409- }
1415+ applyMessageUpdate ( ( prev ) =>
1416+ prev . map ( ( msg ) =>
1417+ msg . id === aiMessageId ? { ...msg , isComplete : true } : msg ,
1418+ ) ,
1419+ )
14101420 }
14111421 } ,
14121422 [
0 commit comments