Runs the agent. NOTE: This sync interface is only for local testing and convenience purpose. Consider using `run_async` for production usage. If event compaction is enabled in the App configuration, it will be performed after all agent events for the current invocation have
(
self,
*,
user_id: str,
session_id: str,
new_message: types.Content,
state_delta: Optional[dict[str, Any]] = None,
run_config: Optional[RunConfig] = None,
)
| 862 | return session |
| 863 | |
| 864 | def run( |
| 865 | self, |
| 866 | *, |
| 867 | user_id: str, |
| 868 | session_id: str, |
| 869 | new_message: types.Content, |
| 870 | state_delta: Optional[dict[str, Any]] = None, |
| 871 | run_config: Optional[RunConfig] = None, |
| 872 | ) -> Generator[Event, None, None]: |
| 873 | """Runs the agent. |
| 874 | |
| 875 | NOTE: |
| 876 | This sync interface is only for local testing and convenience purpose. |
| 877 | Consider using `run_async` for production usage. |
| 878 | |
| 879 | If event compaction is enabled in the App configuration, it will be |
| 880 | performed after all agent events for the current invocation have been |
| 881 | yielded. The generator will only finish iterating after event |
| 882 | compaction is complete. |
| 883 | |
| 884 | Args: |
| 885 | user_id: The user ID of the session. |
| 886 | session_id: The session ID of the session. |
| 887 | new_message: A new message to append to the session. |
| 888 | state_delta: Optional state changes to apply to the session. |
| 889 | run_config: The run config for the agent. |
| 890 | |
| 891 | Yields: |
| 892 | The events generated by the agent. |
| 893 | """ |
| 894 | run_config = run_config or RunConfig() |
| 895 | event_queue = queue.Queue() |
| 896 | |
| 897 | async def _invoke_run_async(): |
| 898 | try: |
| 899 | async with aclosing( |
| 900 | self.run_async( |
| 901 | user_id=user_id, |
| 902 | session_id=session_id, |
| 903 | new_message=new_message, |
| 904 | state_delta=state_delta, |
| 905 | run_config=run_config, |
| 906 | ) |
| 907 | ) as agen: |
| 908 | async for event in agen: |
| 909 | event_queue.put(event) |
| 910 | finally: |
| 911 | event_queue.put(None) |
| 912 | |
| 913 | def _asyncio_thread_main(): |
| 914 | try: |
| 915 | asyncio.run(_invoke_run_async()) |
| 916 | finally: |
| 917 | event_queue.put(None) |
| 918 | |
| 919 | thread = create_thread(target=_asyncio_thread_main) |
| 920 | thread.start() |
| 921 |