13
13
import json
14
14
import logging
15
15
import random
16
+ import warnings
16
17
from concurrent .futures import ThreadPoolExecutor
17
18
from typing import (
18
19
Any ,
@@ -374,7 +375,9 @@ def tool_names(self) -> list[str]:
374
375
all_tools = self .tool_registry .get_all_tools_config ()
375
376
return list (all_tools .keys ())
376
377
377
- def __call__ (self , prompt : AgentInput = None , ** kwargs : Any ) -> AgentResult :
378
+ def __call__ (
379
+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
380
+ ) -> AgentResult :
378
381
"""Process a natural language prompt through the agent's event loop.
379
382
380
383
This method implements the conversational interface with multiple input patterns:
@@ -389,7 +392,8 @@ def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult:
389
392
- list[ContentBlock]: Multi-modal content blocks
390
393
- list[Message]: Complete messages with roles
391
394
- None: Use existing conversation history
392
- **kwargs: Additional parameters to pass through the event loop.
395
+ invocation_state: Additional parameters to pass through the event loop.
396
+ **kwargs: Additional parameters to pass through the event loop.[Deprecating]
393
397
394
398
Returns:
395
399
Result object containing:
@@ -401,13 +405,15 @@ def __call__(self, prompt: AgentInput = None, **kwargs: Any) -> AgentResult:
401
405
"""
402
406
403
407
def execute () -> AgentResult :
404
- return asyncio .run (self .invoke_async (prompt , ** kwargs ))
408
+ return asyncio .run (self .invoke_async (prompt , invocation_state = invocation_state , ** kwargs ))
405
409
406
410
with ThreadPoolExecutor () as executor :
407
411
future = executor .submit (execute )
408
412
return future .result ()
409
413
410
- async def invoke_async (self , prompt : AgentInput = None , ** kwargs : Any ) -> AgentResult :
414
+ async def invoke_async (
415
+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
416
+ ) -> AgentResult :
411
417
"""Process a natural language prompt through the agent's event loop.
412
418
413
419
This method implements the conversational interface with multiple input patterns:
@@ -422,7 +428,8 @@ async def invoke_async(self, prompt: AgentInput = None, **kwargs: Any) -> AgentR
422
428
- list[ContentBlock]: Multi-modal content blocks
423
429
- list[Message]: Complete messages with roles
424
430
- None: Use existing conversation history
425
- **kwargs: Additional parameters to pass through the event loop.
431
+ invocation_state: Additional parameters to pass through the event loop.
432
+ **kwargs: Additional parameters to pass through the event loop.[Deprecating]
426
433
427
434
Returns:
428
435
Result: object containing:
@@ -432,7 +439,7 @@ async def invoke_async(self, prompt: AgentInput = None, **kwargs: Any) -> AgentR
432
439
- metrics: Performance metrics from the event loop
433
440
- state: The final state of the event loop
434
441
"""
435
- events = self .stream_async (prompt , ** kwargs )
442
+ events = self .stream_async (prompt , invocation_state = invocation_state , ** kwargs )
436
443
async for event in events :
437
444
_ = event
438
445
@@ -528,9 +535,7 @@ async def structured_output_async(self, output_model: Type[T], prompt: AgentInpu
528
535
self .hooks .invoke_callbacks (AfterInvocationEvent (agent = self ))
529
536
530
537
async def stream_async (
531
- self ,
532
- prompt : AgentInput = None ,
533
- ** kwargs : Any ,
538
+ self , prompt : AgentInput = None , * , invocation_state : dict [str , Any ] | None = None , ** kwargs : Any
534
539
) -> AsyncIterator [Any ]:
535
540
"""Process a natural language prompt and yield events as an async iterator.
536
541
@@ -546,7 +551,8 @@ async def stream_async(
546
551
- list[ContentBlock]: Multi-modal content blocks
547
552
- list[Message]: Complete messages with roles
548
553
- None: Use existing conversation history
549
- **kwargs: Additional parameters to pass to the event loop.
554
+ invocation_state: Additional parameters to pass through the event loop.
555
+ **kwargs: Additional parameters to pass to the event loop.[Deprecating]
550
556
551
557
Yields:
552
558
An async iterator that yields events. Each event is a dictionary containing
@@ -567,7 +573,19 @@ async def stream_async(
567
573
yield event["data"]
568
574
```
569
575
"""
570
- callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
576
+ merged_state = {}
577
+ if kwargs :
578
+ warnings .warn ("`**kwargs` parameter is deprecating, use `invocation_state` instead." , stacklevel = 2 )
579
+ merged_state .update (kwargs )
580
+ if invocation_state is not None :
581
+ merged_state ["invocation_state" ] = invocation_state
582
+ else :
583
+ if invocation_state is not None :
584
+ merged_state = invocation_state
585
+
586
+ callback_handler = self .callback_handler
587
+ if kwargs :
588
+ callback_handler = kwargs .get ("callback_handler" , self .callback_handler )
571
589
572
590
# Process input and get message to add (if any)
573
591
messages = self ._convert_prompt_to_messages (prompt )
@@ -576,10 +594,10 @@ async def stream_async(
576
594
577
595
with trace_api .use_span (self .trace_span ):
578
596
try :
579
- events = self ._run_loop (messages , invocation_state = kwargs )
597
+ events = self ._run_loop (messages , invocation_state = merged_state )
580
598
581
599
async for event in events :
582
- event .prepare (invocation_state = kwargs )
600
+ event .prepare (invocation_state = merged_state )
583
601
584
602
if event .is_callback_event :
585
603
as_dict = event .as_dict ()
0 commit comments