@@ -26,7 +26,8 @@ public abstract class RootExecutor<TInput> : Executor<TInput>, IResettableExecut
2626 private readonly bool _allowProcessEnvironmentVariableFallback ;
2727 private readonly FrozenSet < string > _allowedEnvironmentVariables ;
2828
29- private string ? _conversationId ;
29+ private readonly string ? _conversationId ;
30+ private string ? _generatedConversationId ;
3031
3132 /// <summary>
3233 /// Get the shared formula session to provide to workflow <see cref="ActionExecutor"/> instances.
@@ -48,14 +49,18 @@ protected RootExecutor(string id, DeclarativeWorkflowOptions options, Func<TInpu
4849 this . _inputTransform = inputTransform ;
4950 this . _allowProcessEnvironmentVariableFallback = options . AllowProcessEnvironmentVariableFallback ;
5051 this . _allowedEnvironmentVariables = ( options . AllowedEnvironmentVariables ?? [ ] ) . ToFrozenSet ( StringComparer . Ordinal ) ;
51- this . _state = new WorkflowFormulaState ( options . CreateRecalcEngine ( ) ) ;
52+ this . _state = new WorkflowFormulaState ( options . CreateRecalcEngine ( ) , restorableEnvironmentVariables : this . _allowedEnvironmentVariables ) ;
5253 this . _state . InitializeSystem ( ) ;
54+ this . _state . CaptureInitialState ( ) ;
5355 this . Session = new RootFormulaSession ( this . _state ) ;
56+ this . RegisterCheckpointRestoringCallback ( this . _state , this . _state . RestoreCheckpointAsync ) ;
5457 }
5558
5659 /// <inheritdoc/>
5760 public ValueTask ResetAsync ( )
5861 {
62+ this . _state . Reset ( ) ;
63+ this . _generatedConversationId = null ;
5964 return default ;
6065 }
6166
@@ -67,13 +72,22 @@ public override async ValueTask HandleAsync(TInput message, IWorkflowContext con
6772
6873 ChatMessage input = ( this . _inputTransform ?? DefaultInputTransform ) . Invoke ( message ) ;
6974
70- if ( string . IsNullOrWhiteSpace ( this . _conversationId ) )
75+ string ? conversationId = declarativeContext . GetWorkflowConversation ( ) ;
76+ if ( string . IsNullOrWhiteSpace ( conversationId ) )
7177 {
72- this . _conversationId = await this . _agentProvider . CreateConversationAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
78+ conversationId = this . _conversationId ;
7379 }
74- await declarativeContext . QueueConversationUpdateAsync ( this . _conversationId , isExternal : true , cancellationToken ) . ConfigureAwait ( false ) ;
80+ if ( string . IsNullOrWhiteSpace ( conversationId ) )
81+ {
82+ if ( string . IsNullOrWhiteSpace ( this . _generatedConversationId ) )
83+ {
84+ this . _generatedConversationId = await this . _agentProvider . CreateConversationAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
85+ }
86+ conversationId = this . _generatedConversationId ;
87+ }
88+ await declarativeContext . QueueConversationUpdateAsync ( conversationId , isExternal : true , cancellationToken ) . ConfigureAwait ( false ) ;
7589
76- ChatMessage inputMessage = await this . _agentProvider . CreateMessageAsync ( this . _conversationId , input , cancellationToken ) . ConfigureAwait ( false ) ;
90+ ChatMessage inputMessage = await this . _agentProvider . CreateMessageAsync ( conversationId , input , cancellationToken ) . ConfigureAwait ( false ) ;
7791
7892 // Use the original input for System.LastMessage to ensure Text is preserved (the
7993 // service may strip text on round-trip), but substitute server-side media references
0 commit comments