@@ -396,20 +396,87 @@ export default function useInterruptedActions<
396396 toast . error ( "Thread data is not available" ) ;
397397 return ;
398398 }
399+ if ( ! humanResponse ) {
400+ toast . error ( "Please enter a response." ) ;
401+ return ;
402+ }
403+ if ( ! selectedInbox ) {
404+ toast . error ( "No inbox selected" ) ;
405+ return ;
406+ }
399407
400- // For now, this is a placeholder that shows a success message
401- // In a real implementation, this would save the scheduled date to the backend
402- // and handle the scheduling logic
403- const formattedDate = scheduledDate . toLocaleDateString ( ) ;
404- const formattedTime = scheduledDate . toLocaleTimeString ( ) ;
408+ // Calculate afterSeconds from the scheduled date
409+ const now = new Date ( ) ;
410+ const afterSeconds = Math . max (
411+ 0 ,
412+ Math . floor ( ( scheduledDate . getTime ( ) - now . getTime ( ) ) / 1000 ) ,
413+ ) ;
405414
406- toast . success ( `Thread scheduled successfully!` , {
407- description : `This interrupt will be processed on ${ formattedDate } at ${ formattedTime } ` ,
408- duration : 5000 ,
409- } ) ;
415+ if ( afterSeconds <= 0 ) {
416+ toast . error ( "Scheduled time must be in the future" ) ;
417+ return ;
418+ }
410419
411- // Clear the selected thread ID to go back to inbox view
412- await setSelectedThreadId ( null ) ;
420+ try {
421+ // Create the human response similar to handleSubmit
422+ const humanResponseInput : HumanResponse [ ] = humanResponse . flatMap ( ( r ) => {
423+ if ( r . type === "edit" ) {
424+ if ( r . acceptAllowed && ! r . editsMade ) {
425+ return {
426+ type : "accept" ,
427+ args : r . args ,
428+ } ;
429+ } else {
430+ return {
431+ type : "edit" ,
432+ args : r . args ,
433+ } ;
434+ }
435+ }
436+
437+ if ( r . type === "response" && ! r . args ) {
438+ // If response was allowed but no response was given, do not include in the response
439+ return [ ] ;
440+ }
441+ return {
442+ type : r . type ,
443+ args : r . args ,
444+ } ;
445+ } ) ;
446+
447+ const input = humanResponseInput . find (
448+ ( r ) => r . type === selectedSubmitType ,
449+ ) ;
450+ if ( ! input ) {
451+ toast . error ( "No response found." ) ;
452+ return ;
453+ }
454+
455+ setLoading ( true ) ;
456+
457+ // Send the human response with afterSeconds for scheduling
458+ await sendHumanResponse ( threadData . thread . thread_id , [ input ] , {
459+ afterSeconds,
460+ } ) ;
461+
462+ const formattedDate = scheduledDate . toLocaleDateString ( ) ;
463+ const formattedTime = scheduledDate . toLocaleTimeString ( ) ;
464+
465+ toast . success ( `Thread scheduled successfully!` , {
466+ description : `This interrupt will be processed on ${ formattedDate } at ${ formattedTime } ` ,
467+ duration : 5000 ,
468+ } ) ;
469+
470+ // Clear the selected thread ID to go back to inbox view
471+ await setSelectedThreadId ( null ) ;
472+ } catch ( e : any ) {
473+ logger . error ( "Error scheduling response" , e ) ;
474+ toast . error ( "Failed to schedule response." , {
475+ duration : 5000 ,
476+ } ) ;
477+ } finally {
478+ setLoading ( false ) ;
479+ }
413480 } ;
414481
415482 return {
0 commit comments