@@ -145,119 +145,92 @@ ThreadMessage message = await client.CreateMessageAsync(
145145 " What's the weather like in my favorite city?" );
146146```
147147
148- 6 . Create a stream and wait for the stream update of the `RequiredActionUpdate ` type . This update will mark the point , when we need to submit tool outputs to the stream . We will submit outputs in the inner cycle . Please note that `RequiredActionUpdate ` keeps only one required action , while our run may require multiple function calls , this case is handled in the inner cycle , so that we can add tool output to the existing array of outputs . After all required actions were submitted we clean up the array of required actions .
148+ 6 . Create a stream and wait for the stream update of the `RequiredActionUpdate ` type . This update will mark the point , when we need to submit tool outputs to the stream . `RequiredActionUpdate ` keeps only one required action , while our run may require multiple function calls , when the last required action has been read , the stream terminates and we start a new stream by submitting tool call results . In the begin of each cycle up the array of required actions .
149149
150150Synchronous sample :
151151```C # Snippet :FunctionsWithStreamingSyncUpdateCycle
152152List <ToolOutput > toolOutputs = [];
153153ThreadRun streamRun = null ;
154- foreach (StreamingUpdate streamingUpdate in client .CreateRunStreaming (thread .Id , agent .Id ))
154+ CollectionResult <StreamingUpdate > stream = client .CreateRunStreaming (thread .Id , agent .Id );
155+ do
155156{
156- if (streamingUpdate .UpdateKind == StreamingUpdateReason .RunCreated )
157+ toolOutputs .Clear ();
158+ foreach (StreamingUpdate streamingUpdate in stream )
157159 {
158- Console .WriteLine (" --- Run started! ---" );
159- }
160- else if (streamingUpdate is RequiredActionUpdate submitToolOutputsUpdate )
161- {
162- streamRun = submitToolOutputsUpdate .Value ;
163- RequiredActionUpdate newActionUpdate = submitToolOutputsUpdate ;
164- while (streamRun .Status == RunStatus .RequiresAction )
160+ if (streamingUpdate .UpdateKind == StreamingUpdateReason .RunCreated )
161+ {
162+ Console .WriteLine (" --- Run started! ---" );
163+ }
164+ else if (streamingUpdate is RequiredActionUpdate submitToolOutputsUpdate )
165165 {
166+ RequiredActionUpdate newActionUpdate = submitToolOutputsUpdate ;
166167 toolOutputs .Add (
167168 GetResolvedToolOutput (
168169 newActionUpdate .FunctionName ,
169170 newActionUpdate .ToolCallId ,
170171 newActionUpdate .FunctionArguments
171172 ));
172- foreach (StreamingUpdate actionUpdate in client .SubmitToolOutputsToStream (streamRun , toolOutputs ))
173- {
174- if (actionUpdate is MessageContentUpdate contentUpdate )
175- {
176- Console .Write (contentUpdate .Text );
177- }
178- else if (actionUpdate is RequiredActionUpdate newAction )
179- {
180- newActionUpdate = newAction ;
181- toolOutputs .Add (
182- GetResolvedToolOutput (
183- newActionUpdate .FunctionName ,
184- newActionUpdate .ToolCallId ,
185- newActionUpdate .FunctionArguments
186- )
187- );
188- }
189- else if (actionUpdate .UpdateKind == StreamingUpdateReason .RunCompleted )
190- {
191- Console .WriteLine ();
192- Console .WriteLine (" --- Run completed! ---" );
193- }
194- }
195- streamRun = client .GetRun (thread .Id , streamRun .Id );
196- toolOutputs .Clear ();
173+ streamRun = submitToolOutputsUpdate .Value ;
174+ }
175+ else if (streamingUpdate is MessageContentUpdate contentUpdate )
176+ {
177+ Console .Write (contentUpdate .Text );
178+ }
179+ else if (streamingUpdate .UpdateKind == StreamingUpdateReason .RunCompleted )
180+ {
181+ Console .WriteLine ();
182+ Console .WriteLine (" --- Run completed! ---" );
197183 }
198- break ;
199184 }
200- else if (streamingUpdate is MessageContentUpdate contentUpdate )
185+ if (toolOutputs . Count > 0 )
201186 {
202- Console . Write ( contentUpdate . Text );
187+ stream = client . SubmitToolOutputsToStream ( streamRun , toolOutputs );
203188 }
204189}
190+ while (toolOutputs .Count > 0);
205191```
206192
207193Asynchronous sample :
208194```C # Snippet :FunctionsWithStreamingUpdateCycle
209195List <ToolOutput > toolOutputs = [];
210196ThreadRun streamRun = null ;
211- await foreach (StreamingUpdate streamingUpdate in client .CreateRunStreamingAsync (thread .Id , agent .Id ))
197+ AsyncCollectionResult <StreamingUpdate > stream = client .CreateRunStreamingAsync (thread .Id , agent .Id );
198+ do
212199{
213- if (streamingUpdate .UpdateKind == StreamingUpdateReason .RunCreated )
200+ toolOutputs .Clear ();
201+ await foreach (StreamingUpdate streamingUpdate in stream )
214202 {
215- Console . WriteLine ( " --- Run started! --- " );
216- }
217- else if ( streamingUpdate is RequiredActionUpdate submitToolOutputsUpdate )
218- {
219- streamRun = submitToolOutputsUpdate . Value ;
220- RequiredActionUpdate newActionUpdate = submitToolOutputsUpdate ;
221- while ( streamRun . Status == RunStatus . RequiresAction ) {
203+ if ( streamingUpdate . UpdateKind == StreamingUpdateReason . RunCreated )
204+ {
205+ Console . WriteLine ( " --- Run started! --- " );
206+ }
207+ else if ( streamingUpdate is RequiredActionUpdate submitToolOutputsUpdate )
208+ {
209+ RequiredActionUpdate newActionUpdate = submitToolOutputsUpdate ;
222210 toolOutputs .Add (
223211 GetResolvedToolOutput (
224212 newActionUpdate .FunctionName ,
225213 newActionUpdate .ToolCallId ,
226214 newActionUpdate .FunctionArguments
227215 ));
228- await foreach (StreamingUpdate actionUpdate in client .SubmitToolOutputsToStreamAsync (streamRun , toolOutputs ))
229- {
230- if (actionUpdate is MessageContentUpdate contentUpdate )
231- {
232- Console .Write (contentUpdate .Text );
233- }
234- else if (actionUpdate is RequiredActionUpdate newAction )
235- {
236- newActionUpdate = newAction ;
237- toolOutputs .Add (
238- GetResolvedToolOutput (
239- newActionUpdate .FunctionName ,
240- newActionUpdate .ToolCallId ,
241- newActionUpdate .FunctionArguments
242- )
243- );
244- }
245- else if (actionUpdate .UpdateKind == StreamingUpdateReason .RunCompleted )
246- {
247- Console .WriteLine ();
248- Console .WriteLine (" --- Run completed! ---" );
249- }
250- }
251- streamRun = client .GetRun (thread .Id , streamRun .Id );
252- toolOutputs .Clear ();
216+ streamRun = submitToolOutputsUpdate .Value ;
217+ }
218+ else if (streamingUpdate is MessageContentUpdate contentUpdate )
219+ {
220+ Console .Write (contentUpdate .Text );
221+ }
222+ else if (streamingUpdate .UpdateKind == StreamingUpdateReason .RunCompleted )
223+ {
224+ Console .WriteLine ();
225+ Console .WriteLine (" --- Run completed! ---" );
253226 }
254- break ;
255227 }
256- else if (streamingUpdate is MessageContentUpdate contentUpdate )
228+ if (toolOutputs . Count > 0 )
257229 {
258- Console . Write ( contentUpdate . Text );
230+ stream = client . SubmitToolOutputsToStreamAsync ( streamRun , toolOutputs );
259231 }
260232}
233+ while (toolOutputs .Count > 0);
261234```
262235
2632367 . Finally , we delete all the resources , we have created in this sample .
0 commit comments