@@ -163,13 +163,7 @@ defmodule AgentForge.Flow do
163163
164164 # Handle legacy result format
165165 { signal_result , new_state } ->
166- case signal_result do
167- { :emit , new_signal } -> { :ok , new_signal , new_state , stats }
168- :skip -> { :ok , nil , new_state , stats }
169- { :halt , result } -> { :ok , result , new_state , stats }
170- { :error , reason } -> { :error , reason , new_state , stats }
171- _ -> { :error , "Invalid result format: #{ inspect ( signal_result ) } " , state , stats }
172- end
166+ normalize_legacy_result ( signal_result , new_state , state , stats )
173167
174168 # State-only result (treat as a successful flow with no output signal)
175169 % { } = new_state ->
@@ -181,6 +175,17 @@ defmodule AgentForge.Flow do
181175 end
182176 end
183177
178+ # Handle legacy result format
179+ defp normalize_legacy_result ( signal_result , new_state , state , stats ) do
180+ case signal_result do
181+ { :emit , new_signal } -> { :ok , new_signal , new_state , stats }
182+ :skip -> { :ok , nil , new_state , stats }
183+ { :halt , result } -> { :ok , result , new_state , stats }
184+ { :error , reason } -> { :error , reason , new_state , stats }
185+ _ -> { :error , "Invalid result format: #{ inspect ( signal_result ) } " , state , stats }
186+ end
187+ end
188+
184189 @ doc """
185190 Processes a signal through a list of handlers with execution limits.
186191 Supports timeout to prevent long-running processes.
@@ -393,70 +398,125 @@ defmodule AgentForge.Flow do
393398 else: nil
394399
395400 # Process handler
396- case process_handler ( handler , current_signal , current_state ) do
397- # Enhanced emit handling with signal strategies
398- { { :emit , new_signal } , new_state } ->
399- case signal_strategy do
400- :forward ->
401- # Default behavior: forward signal to next handler
402- { :cont , { :ok , new_signal , new_state , updated_stats } }
403-
404- :restart ->
405- # Restart processing chain with new signal
406- result = process_handlers ( handlers , new_signal , new_state , opts )
407- { :halt , result }
408-
409- :transform ->
410- # Transform signal using provided function
411- transformed_signal = transform_fn . ( new_signal )
412- { :cont , { :ok , transformed_signal , new_state , updated_stats } }
413- end
414-
415- # Support for emit_many format
416- { { :emit_many , signals } , new_state } when is_list ( signals ) ->
417- # When multiple signals are emitted, use the last one for continuation
418- last_signal = List . last ( signals )
419- { :cont , { :ok , last_signal , new_state , updated_stats } }
420-
421- # Enhanced skip handling with continue_on_skip option
422- { :skip , new_state } ->
423- if continue_on_skip do
424- # Continue to next handler with current signal
425- { :cont , { :ok , current_signal , new_state , updated_stats } }
426- else
427- # Original behavior: halt processing
428- { :halt , { :ok , nil , new_state , updated_stats } }
429- end
430-
431- # Error handling (unchanged)
432- { { :error , reason } , new_state } ->
433- { :halt , { :error , reason , new_state , updated_stats } }
434-
435- # Support for alternative halt format
436- { :halt , result } ->
437- { :halt , { :ok , result , current_state , updated_stats } }
438-
439- # Support for halt with state
440- { { :halt , result } , new_state } ->
441- { :halt , { :ok , result , new_state , updated_stats } }
442-
443- # New branch control flow
444- { :branch , condition , true_state , false_state } ->
445- if condition do
446- # Take the true branch
447- { :cont , { :ok , current_signal , true_state , updated_stats } }
448- else
449- # Take the false branch
450- { :cont , { :ok , current_signal , false_state , updated_stats } }
451- end
452-
453- # Invalid result handling
454- other ->
455- raise "Invalid handler result: #{ inspect ( other ) } "
456- end
401+ process_handler_result (
402+ handler ,
403+ current_signal ,
404+ current_state ,
405+ updated_stats ,
406+ continue_on_skip ,
407+ signal_strategy ,
408+ transform_fn ,
409+ opts
410+ )
457411 end )
458412 end
459413
414+ # Process the result from a handler and determine what to do next
415+ defp process_handler_result (
416+ handler ,
417+ current_signal ,
418+ current_state ,
419+ stats ,
420+ continue_on_skip ,
421+ signal_strategy ,
422+ transform_fn ,
423+ opts
424+ ) do
425+ case process_handler ( handler , current_signal , current_state ) do
426+ # Enhanced emit handling with signal strategies
427+ { { :emit , new_signal } , new_state } ->
428+ handle_emit_result (
429+ new_signal ,
430+ new_state ,
431+ stats ,
432+ signal_strategy ,
433+ transform_fn ,
434+ handler ,
435+ opts
436+ )
437+
438+ # Support for emit_many format
439+ { { :emit_many , signals } , new_state } when is_list ( signals ) ->
440+ # When multiple signals are emitted, use the last one for continuation
441+ last_signal = List . last ( signals )
442+ { :cont , { :ok , last_signal , new_state , stats } }
443+
444+ # Enhanced skip handling with continue_on_skip option
445+ { :skip , new_state } ->
446+ handle_skip_result ( current_signal , new_state , stats , continue_on_skip )
447+
448+ # Error handling
449+ { { :error , reason } , new_state } ->
450+ { :halt , { :error , reason , new_state , stats } }
451+
452+ # Support for alternative halt format
453+ { :halt , result } ->
454+ { :halt , { :ok , result , current_state , stats } }
455+
456+ # Support for halt with state
457+ { { :halt , result } , new_state } ->
458+ { :halt , { :ok , result , new_state , stats } }
459+
460+ # Branch control flow
461+ { :branch , condition , true_state , false_state } ->
462+ handle_branch_result ( condition , current_signal , true_state , false_state , stats )
463+
464+ # Invalid result handling
465+ other ->
466+ raise "Invalid handler result: #{ inspect ( other ) } "
467+ end
468+ end
469+
470+ # Handle emit result based on signal strategy
471+ defp handle_emit_result (
472+ new_signal ,
473+ new_state ,
474+ stats ,
475+ signal_strategy ,
476+ transform_fn ,
477+ _handler ,
478+ opts
479+ ) do
480+ case signal_strategy do
481+ :forward ->
482+ # Default behavior: forward signal to next handler
483+ { :cont , { :ok , new_signal , new_state , stats } }
484+
485+ :restart ->
486+ # Restart processing chain with new signal
487+ handlers = Keyword . get ( opts , :handlers , [ ] )
488+ result = process_handlers ( handlers , new_signal , new_state , opts )
489+ { :halt , result }
490+
491+ :transform ->
492+ # Transform signal using provided function
493+ transformed_signal = transform_fn . ( new_signal )
494+ { :cont , { :ok , transformed_signal , new_state , stats } }
495+ end
496+ end
497+
498+ # Handle skip result based on continue_on_skip option
499+ defp handle_skip_result ( current_signal , new_state , stats , continue_on_skip ) do
500+ if continue_on_skip do
501+ # Continue to next handler with current signal
502+ { :cont , { :ok , current_signal , new_state , stats } }
503+ else
504+ # Original behavior: halt processing
505+ { :halt , { :ok , nil , new_state , stats } }
506+ end
507+ end
508+
509+ # Handle branch result based on condition
510+ defp handle_branch_result ( condition , current_signal , true_state , false_state , stats ) do
511+ if condition do
512+ # Take the true branch
513+ { :cont , { :ok , current_signal , true_state , stats } }
514+ else
515+ # Take the false branch
516+ { :cont , { :ok , current_signal , false_state , stats } }
517+ end
518+ end
519+
460520 @ doc """
461521 Creates a handler that always emits a signal of the given type and data.
462522
0 commit comments