-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathagent_thread_actions.py
More file actions
1163 lines (1059 loc) · 55.3 KB
/
agent_thread_actions.py
File metadata and controls
1163 lines (1059 loc) · 55.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Microsoft. All rights reserved.
import asyncio
import logging
from collections.abc import AsyncIterable
from typing import TYPE_CHECKING, Any, ClassVar, Literal, TypeVar, cast
from azure.ai.agents.models import (
AgentsNamedToolChoiceType,
AgentStreamEvent,
AgentsResponseFormat,
AgentsResponseFormatMode,
AsyncAgentEventHandler,
AsyncAgentRunStream,
BaseAsyncAgentEventHandler,
FunctionToolDefinition,
RequiredMcpToolCall,
ResponseFormatJsonSchemaType,
RunStep,
RunStepAzureAISearchToolCall,
RunStepBingCustomSearchToolCall,
RunStepBingGroundingToolCall,
RunStepCodeInterpreterToolCall,
RunStepDeepResearchToolCall,
RunStepDeltaChunk,
RunStepDeltaToolCallObject,
RunStepFileSearchToolCall,
RunStepMcpToolCall,
RunStepMessageCreationDetails,
RunStepOpenAPIToolCall,
RunStepToolCallDetails,
RunStepType,
SubmitToolApprovalAction,
SubmitToolOutputsAction,
ThreadMessage,
ThreadRun,
ToolApproval,
ToolDefinition,
TruncationObject,
)
from azure.ai.agents.models._enums import MessageRole
from semantic_kernel.agents.azure_ai.agent_content_generation import (
THREAD_MESSAGE_ID,
generate_azure_ai_search_content,
generate_bing_grounding_content,
generate_code_interpreter_content,
generate_deep_research_content,
generate_file_search_content,
generate_function_call_content,
generate_function_call_streaming_content,
generate_function_result_content,
generate_mcp_call_content,
generate_mcp_content,
generate_message_content,
generate_openapi_content,
generate_streaming_azure_ai_search_content,
generate_streaming_bing_grounding_content,
generate_streaming_code_interpreter_content,
generate_streaming_deep_research_content,
generate_streaming_file_search_content,
generate_streaming_mcp_call_content,
generate_streaming_mcp_content,
generate_streaming_message_content,
generate_streaming_openapi_content,
get_function_call_contents,
)
from semantic_kernel.agents.azure_ai.azure_ai_agent_utils import AzureAIAgentUtils
from semantic_kernel.agents.open_ai.assistant_content_generation import merge_streaming_function_results
from semantic_kernel.agents.open_ai.function_action_result import FunctionActionResult
from semantic_kernel.agents.open_ai.run_polling_options import RunPollingOptions
from semantic_kernel.connectors.ai.function_calling_utils import kernel_function_metadata_to_function_call_format
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.exceptions.agent_exceptions import AgentInvokeException, AgentThreadOperationException
from semantic_kernel.functions import KernelArguments
from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadata
from semantic_kernel.utils.feature_stage_decorator import experimental
if TYPE_CHECKING:
from azure.ai.projects.aio import AIProjectClient
from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
from semantic_kernel.filters.auto_function_invocation.auto_function_invocation_context import (
AutoFunctionInvocationContext,
)
from semantic_kernel.kernel import Kernel
_T = TypeVar("_T", bound="AgentThreadActions")
logger: logging.Logger = logging.getLogger(__name__)
@experimental
class AgentThreadActions:
"""AzureAI Agent Thread Actions."""
polling_status: ClassVar[list[str]] = ["queued", "in_progress", "cancelling"]
error_message_states: ClassVar[list[str]] = ["failed", "cancelled", "expired", "incomplete"]
# region Invocation Methods
@classmethod
async def invoke(
cls: type[_T],
*,
agent: "AzureAIAgent",
thread_id: str,
arguments: KernelArguments | None = None,
kernel: "Kernel | None" = None,
# Run-level parameters:
model: str | None = None,
instructions_override: str | None = None,
additional_instructions: str | None = None,
additional_messages: "list[ChatMessageContent] | None" = None,
tools: list[ToolDefinition] | None = None,
temperature: float | None = None,
top_p: float | None = None,
max_prompt_tokens: int | None = None,
max_completion_tokens: int | None = None,
truncation_strategy: TruncationObject | None = None,
response_format: ResponseFormatJsonSchemaType | None = None,
parallel_tool_calls: bool | None = None,
metadata: dict[str, str] | None = None,
polling_options: RunPollingOptions | None = None,
**kwargs: Any,
) -> AsyncIterable[tuple[bool, "ChatMessageContent"]]:
"""Invoke the message in the thread.
Args:
agent: The agent to invoke.
thread_id: The thread id.
arguments: The kernel arguments.
kernel: The kernel.
model: The model.
instructions_override: The instructions override.
additional_instructions: The additional instructions.
additional_messages: The additional messages to add to the thread. Only supports messages with
role = User or Assistant.
https://platform.openai.com/docs/api-reference/runs/createRun#runs-createrun-additional_messages
tools: The tools.
temperature: The temperature.
top_p: The top p.
max_prompt_tokens: The max prompt tokens.
max_completion_tokens: The max completion tokens.
truncation_strategy: The truncation strategy.
response_format: The response format.
parallel_tool_calls: The parallel tool calls.
metadata: The metadata.
polling_options: The polling options defined at the run-level. These will override the agent-level
polling options.
kwargs: Additional keyword arguments.
Returns:
A tuple of the visibility flag and the invoked message.
"""
arguments = KernelArguments() if arguments is None else KernelArguments(**arguments, **kwargs)
kernel = kernel or agent.kernel
tools = cls._get_tools(agent=agent, kernel=kernel) # type: ignore
base_instructions = await agent.format_instructions(kernel=kernel, arguments=arguments)
merged_instructions: str = ""
if instructions_override is not None:
merged_instructions = instructions_override
elif base_instructions and additional_instructions:
merged_instructions = f"{base_instructions}\n\n{additional_instructions}"
else:
merged_instructions = base_instructions or additional_instructions or ""
run_options = cls._generate_options(
agent=agent,
model=model,
additional_messages=additional_messages,
max_completion_tokens=max_completion_tokens,
max_prompt_tokens=max_prompt_tokens,
temperature=temperature,
top_p=top_p,
metadata=metadata,
truncation_strategy=truncation_strategy,
response_format=response_format,
parallel_tool_calls=parallel_tool_calls,
)
# Remove keys with None values.
run_options = {k: v for k, v in run_options.items() if v is not None}
run: ThreadRun = await agent.client.agents.runs.create(
agent_id=agent.id,
thread_id=thread_id,
instructions=merged_instructions or agent.instructions,
tools=tools,
**run_options,
)
processed_step_ids = set()
function_steps: dict[str, "FunctionCallContent"] = {}
while run.status != "completed":
run = await cls._poll_run_status(
agent=agent, run=run, thread_id=thread_id, polling_options=polling_options or agent.polling_options
)
if run.status in cls.error_message_states:
error_message = "None"
if run.last_error and run.last_error.message:
error_message = run.last_error.message
incomplete_details_reason = "None"
if run.incomplete_details and run.incomplete_details.reason:
incomplete_details_reason = run.incomplete_details.reason
raise AgentInvokeException(
f"Run failed with status: `{run.status}` for agent `{agent.name}` and thread `{thread_id}` "
f"with error: {error_message} and incomplete details reason: {incomplete_details_reason}"
)
# Check if function calling is required
if run.status == "requires_action":
if isinstance(run.required_action, SubmitToolOutputsAction):
logger.debug(
f"Run [{run.id}] requires tool action for agent `{agent.name}` and thread `{thread_id}`"
)
fccs = get_function_call_contents(run, function_steps)
if fccs:
logger.debug(
f"Yielding generate_function_call_content for agent `{agent.name}` and "
f"thread `{thread_id}`, visibility False"
)
yield False, generate_function_call_content(agent_name=agent.name, fccs=fccs)
from semantic_kernel.contents.chat_history import ChatHistory
chat_history = ChatHistory() if kwargs.get("chat_history") is None else kwargs["chat_history"]
_ = await cls._invoke_function_calls(
kernel=kernel, fccs=fccs, chat_history=chat_history, arguments=arguments
)
tool_outputs = cls._format_tool_outputs(fccs, chat_history)
await agent.client.agents.runs.submit_tool_outputs(
run_id=run.id,
thread_id=thread_id,
tool_outputs=tool_outputs, # type: ignore
)
logger.debug(f"Submitted tool outputs for agent `{agent.name}` and thread `{thread_id}`")
continue
# Check if MCP tool approval is required
elif isinstance(run.required_action, SubmitToolApprovalAction):
logger.debug(
f"Run [{run.id}] requires MCP tool approval for agent `{agent.name}` and thread `{thread_id}`"
)
tool_calls = run.required_action.submit_tool_approval.tool_calls
if not tool_calls:
logger.warning(f"No tool calls provided for MCP approval - cancelling run [{run.id}]")
await agent.client.agents.runs.cancel(run_id=run.id, thread_id=thread_id)
continue
mcp_tool_calls = [tc for tc in tool_calls if isinstance(tc, RequiredMcpToolCall)]
if mcp_tool_calls:
logger.debug(
f"Yielding generate_mcp_call_content for agent `{agent.name}` and "
f"thread `{thread_id}`, visibility False"
)
yield False, generate_mcp_call_content(agent_name=agent.name, mcp_tool_calls=mcp_tool_calls)
# Create tool approvals for MCP calls
tool_approvals = []
for mcp_call in mcp_tool_calls:
tool_approvals.append(
ToolApproval(
tool_call_id=mcp_call.id,
# TODO(evmattso): we don't support manual tool calling yet
# so we always approve
approve=True,
)
)
await agent.client.agents.runs.submit_tool_outputs(
run_id=run.id,
thread_id=thread_id,
tool_approvals=tool_approvals, # type: ignore
)
logger.debug(f"Submitted MCP tool approvals for agent `{agent.name}` and thread `{thread_id}`")
continue
steps: list[RunStep] = []
async for steps_response in agent.client.agents.run_steps.list(thread_id=thread_id, run_id=run.id):
steps.append(steps_response)
logger.debug(f"Call for steps_response for run [{run.id}] agent `{agent.name}` and thread `{thread_id}`")
def sort_key(step: RunStep):
# Put tool_calls first, then message_creation.
# If multiple steps share a type, break ties by completed_at.
return (0 if step.type == "tool_calls" else 1, step.completed_at)
completed_steps_to_process = sorted(
[s for s in steps if s.completed_at is not None and s.id not in processed_step_ids],
key=sort_key,
)
logger.debug(
f"Completed steps to process for run [{run.id}] agent `{agent.name}` and thread `{thread_id}` "
f"with length `{len(completed_steps_to_process)}`"
)
message_count = 0
for completed_step in completed_steps_to_process:
match completed_step.type:
case RunStepType.TOOL_CALLS:
logger.debug(
f"Entering step type tool_calls for run [{run.id}], agent `{agent.name}` and "
f"thread `{thread_id}`"
)
tool_call_details: RunStepToolCallDetails = cast(
RunStepToolCallDetails, completed_step.step_details
)
for tool_call in tool_call_details.tool_calls:
is_visible = False
content: "ChatMessageContent | None" = None
match tool_call.type:
case AgentsNamedToolChoiceType.CODE_INTERPRETER:
logger.debug(
f"Entering tool_calls (code_interpreter) for run [{run.id}], agent "
f"`{agent.name}` and thread `{thread_id}`"
)
code_call: RunStepCodeInterpreterToolCall = cast(
RunStepCodeInterpreterToolCall, tool_call
)
content = generate_code_interpreter_content(
agent.name,
code_call.code_interpreter.input,
)
is_visible = True
case AgentsNamedToolChoiceType.FUNCTION:
logger.debug(
f"Entering tool_calls (function) for run [{run.id}], agent `{agent.name}` "
f"and thread `{thread_id}`"
)
function_step = function_steps.get(tool_call.id)
assert function_step is not None # nosec
content = generate_function_result_content(
agent_name=agent.name,
function_step=function_step,
tool_call=tool_call, # type: ignore
)
case (
AgentsNamedToolChoiceType.BING_GROUNDING
| AgentsNamedToolChoiceType.BING_CUSTOM_SEARCH
):
logger.debug(
f"Entering tool_calls (bing_grounding/bing_custom_search) for run [{run.id}], "
f"agent `{agent.name}` and thread `{thread_id}`"
)
# Handle both Bing grounding and custom search tool calls
bing_call: RunStepBingGroundingToolCall | RunStepBingCustomSearchToolCall = cast(
RunStepBingGroundingToolCall | RunStepBingCustomSearchToolCall, tool_call
)
content = generate_bing_grounding_content(
agent_name=agent.name, bing_tool_call=bing_call
)
case AgentsNamedToolChoiceType.AZURE_AI_SEARCH:
logger.debug(
f"Entering tool_calls (azure_ai_search) for run [{run.id}], agent "
f" `{agent.name}` and thread `{thread_id}`"
)
azure_ai_search_call: RunStepAzureAISearchToolCall = cast(
RunStepAzureAISearchToolCall, tool_call
)
content = generate_azure_ai_search_content(
agent_name=agent.name, azure_ai_search_tool_call=azure_ai_search_call
)
case AgentsNamedToolChoiceType.FILE_SEARCH:
logger.debug(
f"Entering tool_calls (file_search) for run [{run.id}], agent "
f" `{agent.name}` and thread `{thread_id}`"
)
file_search_call: RunStepFileSearchToolCall = cast(
RunStepFileSearchToolCall, tool_call
)
content = generate_file_search_content(
agent_name=agent.name, file_search_tool_call=file_search_call
)
case "openapi":
logger.debug(
f"Entering tool_calls (openapi) for run [{run.id}], agent "
f" `{agent.name}` and thread `{thread_id}`"
)
openapi_tool_call: RunStepOpenAPIToolCall = cast(RunStepOpenAPIToolCall, tool_call)
content = generate_openapi_content(
agent_name=agent.name,
openapi_tool_call=openapi_tool_call,
)
case AgentsNamedToolChoiceType.MCP:
logger.debug(
f"Entering tool_calls (mcp) for run [{run.id}], agent "
f" `{agent.name}` and thread `{thread_id}`"
)
mcp_tool_call: RunStepMcpToolCall = cast(RunStepMcpToolCall, tool_call)
content = generate_mcp_content(
agent_name=agent.name,
mcp_tool_call=mcp_tool_call,
)
case AgentsNamedToolChoiceType.DEEP_RESEARCH:
logger.debug(
f"Entering tool_calls (deep_research) for run [{run.id}], agent "
f" `{agent.name}` and thread `{thread_id}`"
)
deep_research_call: RunStepDeepResearchToolCall = cast(
RunStepDeepResearchToolCall, tool_call
)
content = generate_deep_research_content(
agent_name=agent.name,
deep_research_tool_call=deep_research_call,
)
if content:
message_count += 1
logger.debug(
f"Yielding tool_message for run [{run.id}], agent `{agent.name}`, "
f"thread `{thread_id}`, message count `{message_count}`, "
f"is_visible `{is_visible}`"
)
yield is_visible, content
case RunStepType.MESSAGE_CREATION:
logger.debug(
f"Entering message_creation for run [{run.id}], agent `{agent.name}` and thread "
f"`{thread_id}`"
)
message_call_details: RunStepMessageCreationDetails = cast(
RunStepMessageCreationDetails, completed_step.step_details
)
message = await cls._retrieve_message(
agent=agent,
thread_id=thread_id,
message_id=message_call_details.message_creation.message_id, # type: ignore
)
if message:
content = generate_message_content(agent.name, message, completed_step)
if content and len(content.items) > 0:
message_count += 1
logger.debug(
f"Yielding message_creation for run [{run.id}], agent `{agent.name}`, "
f"thread `{thread_id}`, message count `{message_count}`, is_visible `True`"
)
yield True, content
processed_step_ids.add(completed_step.id)
@classmethod
async def invoke_stream(
cls: type[_T],
*,
agent: "AzureAIAgent",
thread_id: str,
additional_instructions: str | None = None,
additional_messages: "list[ChatMessageContent] | None" = None,
arguments: KernelArguments | None = None,
instructions_override: str | None = None,
kernel: "Kernel | None" = None,
metadata: dict[str, str] | None = None,
model: str | None = None,
max_prompt_tokens: int | None = None,
max_completion_tokens: int | None = None,
output_messages: list[ChatMessageContent] | None = None,
parallel_tool_calls: bool | None = None,
response_format: ResponseFormatJsonSchemaType | None = None,
tools: list[ToolDefinition] | None = None,
temperature: float | None = None,
top_p: float | None = None,
truncation_strategy: TruncationObject | None = None,
**kwargs: Any,
) -> AsyncIterable["StreamingChatMessageContent"]:
"""Invoke the agent stream and yield ChatMessageContent continuously.
Args:
agent: The agent to invoke.
thread_id: The thread id.
additional_instructions: The additional instructions.
additional_messages: The additional messages to add to the thread. Only supports messages with
role = User or Assistant.
https://platform.openai.com/docs/api-reference/runs/createRun
arguments: The kernel arguments.
instructions_override: The instructions override.
kernel: The kernel.
metadata: The metadata.
model: The model.
max_prompt_tokens: The max prompt tokens.
max_completion_tokens: The max completion tokens.
output_messages: The output messages received from the agent. These are full content messages
formed from the streamed chunks.
parallel_tool_calls: Whether to configure parallel tool calls.
response_format: The response format.
tools: The tools.
temperature: The temperature.
top_p: The top p.
truncation_strategy: The truncation strategy.
kwargs: Additional keyword arguments.
Returns:
An async iterable of StreamingChatMessageContent.
"""
arguments = KernelArguments() if arguments is None else KernelArguments(**arguments, **kwargs)
kernel = kernel or agent.kernel
arguments = agent._merge_arguments(arguments)
tools = cls._get_tools(agent=agent, kernel=kernel) # type: ignore
base_instructions = await agent.format_instructions(kernel=kernel, arguments=arguments)
merged_instructions: str = ""
if instructions_override is not None:
merged_instructions = instructions_override
elif base_instructions and additional_instructions:
merged_instructions = f"{base_instructions}\n\n{additional_instructions}"
else:
merged_instructions = base_instructions or additional_instructions or ""
run_options = cls._generate_options(
agent=agent,
model=model,
additional_messages=additional_messages,
max_completion_tokens=max_completion_tokens,
max_prompt_tokens=max_prompt_tokens,
temperature=temperature,
top_p=top_p,
metadata=metadata,
truncation_strategy=truncation_strategy,
response_format=response_format,
parallel_tool_calls=parallel_tool_calls,
)
run_options = {k: v for k, v in run_options.items() if v is not None}
stream: AsyncAgentRunStream = await agent.client.agents.runs.stream(
agent_id=agent.id,
thread_id=thread_id,
instructions=merged_instructions or agent.instructions,
tools=tools,
**run_options,
)
function_steps: dict[str, FunctionCallContent] = {}
active_messages: dict[str, RunStep] = {}
async for content in cls._process_stream_events(
stream=stream,
agent=agent,
thread_id=thread_id,
output_messages=output_messages,
kernel=kernel,
arguments=arguments,
function_steps=function_steps,
active_messages=active_messages,
):
if content:
yield content
@classmethod
async def _process_stream_events(
cls: type[_T],
stream: AsyncAgentRunStream,
agent: "AzureAIAgent",
thread_id: str,
kernel: "Kernel",
arguments: KernelArguments,
function_steps: dict[str, FunctionCallContent],
active_messages: dict[str, RunStep],
output_messages: "list[ChatMessageContent] | None" = None,
) -> AsyncIterable["StreamingChatMessageContent"]:
"""Process events from the main stream and delegate tool output handling as needed."""
thread_msg_id = None
while True:
# Use 'async with' only if the stream supports async context management (main agent stream).
# Tool output handlers only support async iteration, not context management.
if hasattr(stream, "__aenter__") and hasattr(stream, "__aexit__"):
async with stream as response_stream:
stream_iter = response_stream
else:
stream_iter = stream
async for event_type, event_data, _ in stream_iter:
if event_type == AgentStreamEvent.THREAD_RUN_CREATED:
run = event_data
logger.info(f"Assistant run created with ID: {run.id}")
elif event_type == AgentStreamEvent.THREAD_RUN_IN_PROGRESS:
run_step = cast(RunStep, event_data)
logger.info(f"Assistant run in progress with ID: {run_step.id}")
elif event_type == AgentStreamEvent.THREAD_MESSAGE_CREATED:
# Keep the current message id stable unless a new one arrives
if thread_msg_id != event_data.id:
thread_msg_id = event_data.id
logger.info(f"Assistant message created with ID: {thread_msg_id}")
elif event_type == AgentStreamEvent.THREAD_MESSAGE_DELTA:
yield generate_streaming_message_content(agent.name, event_data, thread_msg_id)
elif event_type == AgentStreamEvent.THREAD_RUN_STEP_COMPLETED:
step_completed = cast(RunStep, event_data)
logger.info(f"Run step completed with ID: {step_completed.id}")
if isinstance(step_completed.step_details, RunStepMessageCreationDetails):
msg_id = step_completed.step_details.message_creation.message_id
active_messages.setdefault(msg_id, step_completed)
elif event_type == AgentStreamEvent.THREAD_RUN_STEP_DELTA:
run_step_event: RunStepDeltaChunk = event_data
details = run_step_event.delta.step_details
if not details:
continue
if isinstance(details, RunStepDeltaToolCallObject) and details.tool_calls:
content_is_visible = False
for tool_call in details.tool_calls:
logger.debug(
f"Generating content for tool call type `{tool_call.type}`, agent `{agent.name}` and "
f"thread `{thread_id}` with tool call details: {details}"
)
content = None
match tool_call.type:
# Function Calling-related content is emitted as a single message
# via the `on_intermediate_message` callback.
case AgentsNamedToolChoiceType.CODE_INTERPRETER:
content = generate_streaming_code_interpreter_content(agent.name, details)
content_is_visible = True
case (
AgentsNamedToolChoiceType.BING_GROUNDING
| AgentsNamedToolChoiceType.BING_CUSTOM_SEARCH
):
content = generate_streaming_bing_grounding_content(
agent_name=agent.name, step_details=details
)
case AgentsNamedToolChoiceType.AZURE_AI_SEARCH:
content = generate_streaming_azure_ai_search_content(
agent_name=agent.name, step_details=details
)
case AgentsNamedToolChoiceType.FILE_SEARCH:
content = generate_streaming_file_search_content(
agent_name=agent.name, step_details=details
)
case "openapi":
# There's no enum for OpenAPI tool calls as part of `AgentsNamedToolChoiceType`
# so we handle it separately.
content = generate_streaming_openapi_content(
agent_name=agent.name, step_details=details
)
case AgentsNamedToolChoiceType.MCP:
content = generate_streaming_mcp_content(
agent_name=agent.name, step_details=details
)
case AgentsNamedToolChoiceType.DEEP_RESEARCH:
content = generate_streaming_deep_research_content(
agent_name=agent.name, step_details=details
)
if content:
if thread_msg_id and THREAD_MESSAGE_ID not in content.metadata:
content.metadata[THREAD_MESSAGE_ID] = thread_msg_id
if output_messages is not None:
output_messages.append(content)
if content_is_visible:
yield content
elif event_type == AgentStreamEvent.THREAD_RUN_REQUIRES_ACTION:
logger.debug(
f"Entering step type {event_type}, agent `{agent.name}` and "
f"thread `{thread_id}` with event data: {event_data}"
)
run = cast(ThreadRun, event_data)
# Check if this is a function call request
if isinstance(run.required_action, SubmitToolOutputsAction):
action_result = await cls._handle_streaming_requires_action(
agent_name=agent.name,
kernel=kernel,
run=run,
function_steps=function_steps,
arguments=arguments,
)
if action_result is None:
raise RuntimeError(
f"Function call required but no function steps found for agent `{agent.name}` "
f"thread: {thread_id}."
)
for content in (
action_result.function_call_streaming_content,
action_result.function_result_streaming_content,
):
if content and output_messages is not None:
if thread_msg_id and THREAD_MESSAGE_ID not in content.metadata:
content.metadata[THREAD_MESSAGE_ID] = thread_msg_id
output_messages.append(content)
handler: BaseAsyncAgentEventHandler = AsyncAgentEventHandler()
await agent.client.agents.runs.submit_tool_outputs_stream(
run_id=run.id,
thread_id=thread_id,
tool_outputs=action_result.tool_outputs, # type: ignore
event_handler=handler,
)
# Pass the handler to the stream to continue processing
stream = handler # type: ignore
logger.debug(
f"Submitted tool outputs stream for agent `{agent.name}` and "
f"thread `{thread_id}` and run id `{run.id}`"
)
break
# Check if this is an MCP tool approval request
elif isinstance(run.required_action, SubmitToolApprovalAction):
tool_calls = run.required_action.submit_tool_approval.tool_calls
if not tool_calls:
logger.warning(f"No tool calls provided for MCP approval - cancelling run [{run.id}]")
await agent.client.agents.runs.cancel(run_id=run.id, thread_id=thread_id)
break
mcp_tool_calls = [tc for tc in tool_calls if isinstance(tc, RequiredMcpToolCall)]
if mcp_tool_calls:
logger.debug(
f"Processing MCP tool approvals for agent `{agent.name}` and "
f"thread `{thread_id}` and run id `{run.id}`"
)
if output_messages is not None:
content = generate_streaming_mcp_call_content(
agent_name=agent.name, mcp_tool_calls=mcp_tool_calls
)
if content:
if thread_msg_id and THREAD_MESSAGE_ID not in content.metadata:
content.metadata[THREAD_MESSAGE_ID] = thread_msg_id
output_messages.append(content)
# Create tool approvals for MCP calls
tool_approvals = []
for mcp_call in mcp_tool_calls:
tool_approvals.append(
ToolApproval(
tool_call_id=mcp_call.id,
approve=True,
# Note: headers would need to be provided by the MCP tool configuration
# This is a simplified implementation
headers={},
)
)
handler: BaseAsyncAgentEventHandler = AsyncAgentEventHandler() # type: ignore
await agent.client.agents.runs.submit_tool_outputs_stream(
run_id=run.id,
thread_id=thread_id,
tool_approvals=tool_approvals, # type: ignore
event_handler=handler,
)
# Pass the handler to the stream to continue processing
stream = handler # type: ignore
logger.debug(
f"Submitted MCP tool approvals stream for agent `{agent.name}` and "
f"thread `{thread_id}` and run id `{run.id}`"
)
break
elif event_type == AgentStreamEvent.THREAD_RUN_COMPLETED:
logger.debug(
f"Entering step type {event_type}, agent `{agent.name}` and "
f"thread `{thread_id}` and run id `{run.id}`"
)
run = cast(ThreadRun, event_data)
logger.info(f"Run completed with ID: {run.id}")
if active_messages:
for msg_id, step in active_messages.items():
message = await cls._retrieve_message(agent=agent, thread_id=thread_id, message_id=msg_id)
if message and hasattr(message, "content"):
final_content = generate_message_content(agent.name, message, step)
if output_messages is not None:
output_messages.append(final_content)
return
elif event_type == AgentStreamEvent.THREAD_RUN_FAILED:
run_failed = cast(ThreadRun, event_data)
error_message = "None"
if run_failed.last_error and run_failed.last_error.message:
error_message = run_failed.last_error.message
incomplete_details_reason = "None"
if run_failed.incomplete_details and run_failed.incomplete_details.reason:
incomplete_details_reason = run_failed.incomplete_details.reason
raise RuntimeError(
f"Run failed with status: `{run_failed.status}` for agent `{agent.name}` "
f"thread `{thread_id}` with error: {error_message} and incomplete details reason: "
f"{incomplete_details_reason}"
)
else:
break
return
# endregion
# region Messaging Handling Methods
@classmethod
async def create_thread(
cls: type[_T],
client: "AIProjectClient",
**kwargs: Any,
) -> str:
"""Create a thread.
Args:
client: The client to use to create the thread.
kwargs: Additional keyword arguments.
Returns:
The ID of the created thread.
"""
thread = await client.agents.threads.create(**kwargs)
return thread.id
@classmethod
async def create_message(
cls: type[_T],
client: "AIProjectClient",
thread_id: str,
message: "str | ChatMessageContent",
**kwargs: Any,
) -> "ThreadMessage | None":
"""Create a message in the thread.
Args:
client: The client to use to create the message.
thread_id: The ID of the thread to create the message in.
message: The message to create.
kwargs: Additional keyword arguments.
Returns:
The created message.
"""
if isinstance(message, str):
message = ChatMessageContent(role=AuthorRole.USER, content=message)
if any(isinstance(item, FunctionCallContent) for item in message.items):
return None
if not message.content.strip():
return None
return await client.agents.messages.create(
thread_id=thread_id,
role=MessageRole.USER if message.role == AuthorRole.USER else MessageRole.AGENT,
content=message.content,
attachments=AzureAIAgentUtils.get_attachments(message),
metadata=AzureAIAgentUtils.get_metadata(message),
**kwargs,
)
@classmethod
async def get_messages(
cls: type[_T],
client: "AIProjectClient",
thread_id: str,
sort_order: Literal["asc", "desc"] = "desc",
) -> AsyncIterable["ChatMessageContent"]:
"""Get messages from a thread.
Args:
client: The client to use to get the messages.
thread_id: The ID of the thread to get the messages from.
sort_order: The order to sort the messages in.
Yields:
An AsyncIterable of ChatMessageContent that includes the thread messages.
Raises:
AgentThreadOperationException: If the messages cannot be retrieved.
"""
try:
async for message in client.agents.messages.list(
thread_id=thread_id,
run_id=None,
limit=None,
order=sort_order,
before=None,
):
agent_id = (message.agent_id or message.metadata.get("agent_id") or "").strip() or "agent"
yield generate_message_content(agent_id, message)
except Exception as e:
logger.error(f"Failed to retrieve messages for thread {thread_id}: {e}")
raise AgentThreadOperationException(f"Failed to retrieve messages for thread `{thread_id}`.") from e
# endregion
# region Internal Methods
@classmethod
def _merge_options(
cls: type[_T],
*,
agent: "AzureAIAgent",
model: str | None = None,
response_format: ResponseFormatJsonSchemaType | None = None,
temperature: float | None = None,
top_p: float | None = None,
metadata: dict[str, str] | None = None,
**kwargs: Any,
) -> dict[str, Any]:
"""Merge run-time options with the agent-level options.
Run-level parameters take precedence.
"""
return {
"model": model if model is not None else agent.definition.model,
"response_format": response_format if response_format is not None else agent.definition.response_format,
"temperature": temperature if temperature is not None else None,
"top_p": top_p if top_p is not None else None,
"metadata": metadata if metadata is not None else agent.definition.metadata,
**kwargs,
}
@classmethod
def _generate_options(cls: type[_T], **kwargs: Any) -> dict[str, Any]:
"""Generate a dictionary of options that can be passed directly to create_run."""
merged = cls._merge_options(**kwargs)
truncation_strategy = merged.get("truncation_strategy", None)
max_completion_tokens = merged.get("max_completion_tokens", None)
max_prompt_tokens = merged.get("max_prompt_tokens", None)
parallel_tool_calls = merged.get("parallel_tool_calls_enabled", None)
additional_messages = cls._translate_additional_messages(merged.get("additional_messages", None))
return {
"model": merged.get("model"),
"top_p": merged.get("top_p"),
"response_format": cls._coerce_response_format(merged.get("response_format")),
"temperature": merged.get("temperature"),
"truncation_strategy": truncation_strategy,
"metadata": merged.get("metadata"),
"max_completion_tokens": max_completion_tokens,
"max_prompt_tokens": max_prompt_tokens,
"parallel_tool_calls": parallel_tool_calls,
"additional_messages": additional_messages,
}
@staticmethod
def _coerce_response_format(
response_format: Any,
) -> "str | AgentsResponseFormatMode | AgentsResponseFormat | ResponseFormatJsonSchemaType | None":
"""Coerce a plain dict response_format to the appropriate Azure AI typed model.
When users supply ``response_format`` as a plain Python :class:`dict` (e.g.
``{"type": "json_object"}``), the Azure AI telemetry instrumentor raises
``ValueError: Unknown response format <class 'dict'>`` because it only handles
the typed SDK models. This method converts plain dicts to the correct type so
that the Azure AI telemetry layer can serialize them without error.
"""
if response_format is None or isinstance(
response_format, (str, AgentsResponseFormatMode, AgentsResponseFormat, ResponseFormatJsonSchemaType)
):
return response_format
if isinstance(response_format, dict):
if response_format.get("type") == "json_schema":
return ResponseFormatJsonSchemaType(response_format)
return AgentsResponseFormat(response_format)
return response_format
@classmethod
def _translate_additional_messages(
cls: type[_T], messages: "list[ChatMessageContent] | None"
) -> list[ThreadMessage] | None:
"""Translate additional messages to the required format."""
if not messages:
return None
return AzureAIAgentUtils.get_thread_messages(messages)
@classmethod
def _prepare_tool_definition(cls: type[_T], tool: dict | ToolDefinition) -> dict | ToolDefinition:
"""Prepare the tool definition."""
if tool.get("type") == "openapi" and "openapi" in tool:
openapi_data = dict(tool["openapi"])
openapi_data.pop("functions", None)
tool = dict(tool)
tool["openapi"] = openapi_data
return tool
@staticmethod
def _deduplicate_tools(existing_tools: list[dict], new_tools: list[dict]) -> list[dict]:
existing_names = {
tool["function"]["name"] for tool in existing_tools if "function" in tool and "name" in tool["function"]
}
return [tool for tool in new_tools if tool.get("function", {}).get("name") not in existing_names]
@classmethod
def _get_tools(cls: type[_T], agent: "AzureAIAgent", kernel: "Kernel") -> list[dict[str, Any] | ToolDefinition]:
"""Get the tools for the agent."""
tools: list[Any] = list(agent.definition.tools)
funcs = kernel.get_full_list_of_function_metadata()
cls._validate_function_tools_registered(tools, funcs)
dict_defs = [kernel_function_metadata_to_function_call_format(f) for f in funcs]
deduped_defs = cls._deduplicate_tools(tools, dict_defs)
tools.extend(deduped_defs)
return [cls._prepare_tool_definition(tool) for tool in tools]
@staticmethod
def _validate_function_tools_registered(
tools: list[Any],
funcs: list[Any],