@@ -523,28 +523,30 @@ export default function useDevMode(options: UseDevModeOptions): UseDevMode {
523523 setAutoApprove ( true ) ;
524524 }
525525
526- const messages = chat . messages ;
527- if ( messages . length === 0 ) {
528- return ;
529- }
530- const lastMsg = messages [ messages . length - 1 ] ;
531- if ( ! lastMsg ) {
532- return ;
533- }
534- if ( lastMsg . role !== "assistant" ) {
535- return ;
536- }
537- if ( ! Array . isArray ( lastMsg . parts ) ) {
538- return ;
539- }
540- if ( approvalHandledRef . current === lastMsg . id ) {
526+ const lastApprovalMessage = chat . messages . reverse ( ) . find ( ( msg ) => {
527+ if ( msg . role !== "assistant" ) {
528+ return false ;
529+ }
530+ if ( ! Array . isArray ( msg . parts ) ) {
531+ return false ;
532+ }
533+ return msg . parts . some (
534+ ( part ) =>
535+ "output" in part &&
536+ isToolApprovalOutput ( part . output ) &&
537+ part . output . outcome === "pending"
538+ ) ;
539+ } ) ;
540+
541+ if ( ! lastApprovalMessage ) {
541542 return ;
542543 }
544+
543545 // CRITICAL: all code before this point must be synchronous.
544546 // Otherwise, the approval may be handled multiple times.
545- approvalHandledRef . current = lastMsg . id ;
547+ approvalHandledRef . current = lastApprovalMessage . id ;
546548 // Update all pending approval outputs
547- const updatedParts = lastMsg . parts . map ( ( part : any ) => {
549+ const updatedParts = lastApprovalMessage . parts . map ( ( part : any ) => {
548550 if (
549551 part . output &&
550552 isToolApprovalOutput ( part . output ) &&
@@ -562,7 +564,7 @@ export default function useDevMode(options: UseDevModeOptions): UseDevMode {
562564 } ) ;
563565
564566 await chat . upsertMessage ( {
565- ...lastMsg ,
567+ ...lastApprovalMessage ,
566568 parts : updatedParts ,
567569 } ) ;
568570
0 commit comments