11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Reflection ;
45using System . Text ;
56using System . Threading . Tasks ;
67using System . Windows . Forms ;
78using FlowVision . lib . Plugins ;
8- using Microsoft . Extensions . DependencyInjection ;
9- using Microsoft . SemanticKernel ;
10- using Microsoft . SemanticKernel . ChatCompletion ;
11- using Microsoft . SemanticKernel . Connectors . OpenAI ;
9+ using Microsoft . Extensions . AI ;
10+ using Azure . AI . OpenAI ;
11+ using Azure ;
12+ using ChatMessage = Microsoft . Extensions . AI . ChatMessage ;
1213
1314namespace FlowVision . lib . Classes
1415{
1516 public class Actioner
1617 {
17- private IChatCompletionService actionerChat ;
18- private ChatHistory actionerHistory ;
19- private Kernel actionerKernel ;
18+ private IChatClient actionerChat ;
19+ private List < ChatMessage > actionerHistory ;
2020 private const string ACTIONER_CONFIG = "actioner" ;
21- private const string TOOL_CONFIG = "toolsconfig" ; // Added constant for tool config
21+ private const string TOOL_CONFIG = "toolsconfig" ;
2222
2323 private MultiAgentActioner multiAgentActioner ;
2424 private bool useMultiAgentMode = false ;
@@ -28,7 +28,7 @@ public class Actioner
2828 // Update the Actioner constructor to support both the delegate and the RichTextBox approaches
2929 public Actioner ( Form1 . PluginOutputHandler outputHandler )
3030 {
31- actionerHistory = new ChatHistory ( ) ;
31+ actionerHistory = new List < ChatMessage > ( ) ;
3232
3333 // Create a RichTextBox that isn't displayed but used for logging
3434 var hiddenTextBox = new RichTextBox { Visible = false } ;
@@ -102,10 +102,10 @@ public async Task<string> ExecuteAction(string actionPrompt)
102102 try
103103 {
104104 // Add system message to actioner history
105- actionerHistory . AddSystemMessage ( toolConfig . ActionerSystemPrompt + toolDescriptions ) ;
105+ actionerHistory . Add ( new ChatMessage ( ChatRole . System , toolConfig . ActionerSystemPrompt + toolDescriptions ) ) ;
106106
107107 // Add action prompt to actioner history
108- actionerHistory . AddUserMessage ( actionPrompt ) ;
108+ actionerHistory . Add ( new ChatMessage ( ChatRole . User , actionPrompt ) ) ;
109109
110110 // Load actioner model config
111111 APIConfig config = APIConfig . LoadConfig ( ACTIONER_CONFIG ) ;
@@ -118,93 +118,91 @@ public async Task<string> ExecuteAction(string actionPrompt)
118118 return "Error: Actioner model not configured" ;
119119 }
120120
121- // Setup the kernel for actioner with plugins
122- var builder = Kernel . CreateBuilder ( ) ;
123- builder . AddAzureOpenAIChatCompletion (
124- config . DeploymentName ,
125- config . EndpointURL ,
126- config . APIKey ) ;
121+ // Create Azure OpenAI chat client with IChatClient interface
122+ var azureClient = new AzureOpenAIClient ( new Uri ( config . EndpointURL ) , new AzureKeyCredential ( config . APIKey ) ) ;
123+ IChatClient baseChatClient = azureClient . GetChatClient ( config . DeploymentName ) . AsIChatClient ( ) ;
127124
128- // Configure OpenAI settings based on toolConfig
129- var settings = new OpenAIPromptExecutionSettings
130- {
131- Temperature = toolConfig . Temperature ,
132- ToolCallBehavior = toolConfig . AutoInvokeKernelFunctions
133- ? Microsoft . SemanticKernel . Connectors . OpenAI . ToolCallBehavior . AutoInvokeKernelFunctions
134- : Microsoft . SemanticKernel . Connectors . OpenAI . ToolCallBehavior . EnableKernelFunctions
135- } ;
125+ // Collect tools based on configuration
126+ var tools = new List < AITool > ( ) ;
136127
137- // Add plugins dynamically based on tool configuration
138128 if ( toolConfig . EnableCMDPlugin )
139129 {
140- builder . Plugins . AddFromType < CMDPlugin > ( ) ;
130+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new CMDPlugin ( ) ) ) ;
141131 }
142132
143133 if ( toolConfig . EnablePowerShellPlugin )
144134 {
145- builder . Plugins . AddFromType < PowerShellPlugin > ( ) ;
135+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new PowerShellPlugin ( ) ) ) ;
146136 }
147137
148138 if ( toolConfig . EnableScreenCapturePlugin )
149139 {
150- builder . Plugins . AddFromType < ScreenCaptureOmniParserPlugin > ( ) ;
140+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new ScreenCaptureOmniParserPlugin ( ) ) ) ;
151141 }
152142
153143 if ( toolConfig . EnableKeyboardPlugin )
154144 {
155- builder . Plugins . AddFromType < KeyboardPlugin > ( ) ;
145+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new KeyboardPlugin ( ) ) ) ;
156146 }
157147
158148 if ( toolConfig . EnableMousePlugin )
159149 {
160- builder . Plugins . AddFromType < MousePlugin > ( ) ;
150+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new MousePlugin ( ) ) ) ;
161151 }
162152
163153 if ( toolConfig . EnableWindowSelectionPlugin )
164154 {
165- builder . Plugins . AddFromType < WindowSelectionPlugin > ( ) ;
155+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new WindowSelectionPlugin ( ) ) ) ;
166156 }
167157
168158 if ( toolConfig . EnablePlaywrightPlugin )
169159 {
170- // Expose browser automation utilities including ExecuteScript and CloseBrowser
171- builder . Plugins . AddFromType < PlaywrightPlugin > ( ) ;
160+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new PlaywrightPlugin ( ) ) ) ;
172161 }
173162
174163 if ( toolConfig . EnableRemoteControl )
175164 {
176- builder . Plugins . AddFromType < RemoteControlPlugin > ( ) ;
165+ tools . AddRange ( PluginToolExtractor . ExtractTools ( new RemoteControlPlugin ( ) ) ) ;
177166 }
178167
179- actionerKernel = builder . Build ( ) ;
180- actionerChat = actionerKernel . GetRequiredService < IChatCompletionService > ( ) ;
168+ // Configure chat options with tools
169+ var chatOptions = new ChatOptions
170+ {
171+ Temperature = ( float ) toolConfig . Temperature ,
172+ Tools = tools
173+ } ;
174+
175+ // Build chat client with function invocation if enabled
176+ actionerChat = toolConfig . AutoInvokeKernelFunctions
177+ ? new ChatClientBuilder ( baseChatClient ) . UseFunctionInvocation ( ) . Build ( )
178+ : baseChatClient ;
181179
182180 // Update loading message to show we're now processing the response
183181 PluginLogger . StopLoadingIndicator ( ) ;
184182 PluginLogger . StartLoadingIndicator ( "AI response" ) ;
185183
186- // Process the response
184+ // Process the response with streaming
187185 var responseBuilder = new StringBuilder ( ) ;
188- var responseStream = actionerChat . GetStreamingChatMessageContentsAsync ( actionerHistory , settings , actionerKernel ) ;
189- var enumerator = responseStream . GetAsyncEnumerator ( ) ;
190- try
186+ await foreach ( var update in actionerChat . GetStreamingResponseAsync ( actionerHistory , chatOptions ) )
191187 {
192- while ( await enumerator . MoveNextAsync ( ) )
188+ if ( update . Text != null )
193189 {
194- var message = enumerator . Current ;
195- if ( message . Content == "None" ) continue ;
196- responseBuilder . Append ( message . Content ) ;
190+ responseBuilder . Append ( update . Text ) ;
197191 }
198192 }
199- finally
200- {
201- await enumerator . DisposeAsync ( ) ;
202- }
203193
204194 // Task completed successfully
205195 PluginLogger . NotifyTaskComplete ( "Action Execution" , true ) ;
206196
207- return responseBuilder . ToString ( ) ;
197+ var response = responseBuilder . ToString ( ) ;
198+
199+ // Add assistant response to history
200+ if ( ! string . IsNullOrEmpty ( response ) )
201+ {
202+ actionerHistory . Add ( new ChatMessage ( ChatRole . Assistant , response ) ) ;
203+ }
204+
205+ return response ;
208206 }
209207 catch ( Exception ex )
210208 {
@@ -221,11 +219,11 @@ internal void SetChatHistory(List<LocalChatMessage> chatHistory)
221219 {
222220 if ( message . Author == "You" )
223221 {
224- actionerHistory . AddUserMessage ( message . Content ) ;
222+ actionerHistory . Add ( new ChatMessage ( ChatRole . User , message . Content ) ) ;
225223 }
226224 else if ( message . Author == "AI" )
227225 {
228- actionerHistory . AddAssistantMessage ( message . Content ) ;
226+ actionerHistory . Add ( new ChatMessage ( ChatRole . Assistant , message . Content ) ) ;
229227 }
230228 }
231229
0 commit comments