google-adk 0.0.1__py3-none-any.whl → 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/adk/._version.py +0 -0
- google/adk/__init__.py +20 -0
- google/adk/agents/__init__.py +32 -0
- google/adk/agents/active_streaming_tool.py +38 -0
- google/adk/agents/base_agent.py +345 -0
- google/adk/agents/callback_context.py +112 -0
- google/adk/agents/invocation_context.py +181 -0
- google/adk/agents/langgraph_agent.py +140 -0
- google/adk/agents/live_request_queue.py +64 -0
- google/adk/agents/llm_agent.py +376 -0
- google/adk/agents/loop_agent.py +62 -0
- google/adk/agents/parallel_agent.py +96 -0
- google/adk/agents/readonly_context.py +46 -0
- google/adk/agents/remote_agent.py +50 -0
- google/adk/agents/run_config.py +87 -0
- google/adk/agents/sequential_agent.py +45 -0
- google/adk/agents/transcription_entry.py +34 -0
- google/adk/artifacts/__init__.py +23 -0
- google/adk/artifacts/base_artifact_service.py +128 -0
- google/adk/artifacts/gcs_artifact_service.py +195 -0
- google/adk/artifacts/in_memory_artifact_service.py +133 -0
- google/adk/auth/__init__.py +22 -0
- google/adk/auth/auth_credential.py +220 -0
- google/adk/auth/auth_handler.py +268 -0
- google/adk/auth/auth_preprocessor.py +116 -0
- google/adk/auth/auth_schemes.py +67 -0
- google/adk/auth/auth_tool.py +55 -0
- google/adk/cli/__init__.py +15 -0
- google/adk/cli/__main__.py +18 -0
- google/adk/cli/agent_graph.py +148 -0
- google/adk/cli/browser/adk_favicon.svg +17 -0
- google/adk/cli/browser/assets/audio-processor.js +51 -0
- google/adk/cli/browser/assets/config/runtime-config.json +3 -0
- google/adk/cli/browser/index.html +33 -0
- google/adk/cli/browser/main-SY2WYYGV.js +75 -0
- google/adk/cli/browser/polyfills-FFHMD2TL.js +18 -0
- google/adk/cli/browser/styles-4VDSPQ37.css +17 -0
- google/adk/cli/cli.py +181 -0
- google/adk/cli/cli_deploy.py +181 -0
- google/adk/cli/cli_eval.py +282 -0
- google/adk/cli/cli_tools_click.py +524 -0
- google/adk/cli/fast_api.py +784 -0
- google/adk/cli/utils/__init__.py +49 -0
- google/adk/cli/utils/envs.py +57 -0
- google/adk/cli/utils/evals.py +93 -0
- google/adk/cli/utils/logs.py +72 -0
- google/adk/code_executors/__init__.py +49 -0
- google/adk/code_executors/base_code_executor.py +97 -0
- google/adk/code_executors/code_execution_utils.py +256 -0
- google/adk/code_executors/code_executor_context.py +202 -0
- google/adk/code_executors/container_code_executor.py +196 -0
- google/adk/code_executors/unsafe_local_code_executor.py +71 -0
- google/adk/code_executors/vertex_ai_code_executor.py +234 -0
- google/adk/docs/Makefile +20 -0
- google/adk/docs/build/doctrees/google-adk.doctree +0 -0
- google/adk/docs/build/html/_sources/google-adk.rst.txt +98 -0
- google/adk/docs/build/html/_sources/index.rst.txt +7 -0
- google/adk/docs/build/html/_static/autodoc_pydantic.css +27 -0
- google/adk/docs/build/html/_static/basic.css +925 -0
- google/adk/docs/build/html/_static/debug.css +85 -0
- google/adk/docs/build/html/_static/doctools.js +156 -0
- google/adk/docs/build/html/_static/documentation_options.js +29 -0
- google/adk/docs/build/html/_static/file.png +0 -0
- google/adk/docs/build/html/_static/language_data.js +199 -0
- google/adk/docs/build/html/_static/minus.png +0 -0
- google/adk/docs/build/html/_static/plus.png +0 -0
- google/adk/docs/build/html/_static/pygments.css +274 -0
- google/adk/docs/build/html/_static/scripts/furo-extensions.js +16 -0
- google/adk/docs/build/html/_static/scripts/furo.js +19 -0
- google/adk/docs/build/html/_static/scripts/furo.js.LICENSE.txt +7 -0
- google/adk/docs/build/html/_static/scripts/furo.js.map +1 -0
- google/adk/docs/build/html/_static/searchtools.js +620 -0
- google/adk/docs/build/html/_static/skeleton.css +312 -0
- google/adk/docs/build/html/_static/sphinx_highlight.js +170 -0
- google/adk/docs/build/html/_static/styles/furo-extensions.css +18 -0
- google/adk/docs/build/html/_static/styles/furo-extensions.css.map +1 -0
- google/adk/docs/build/html/_static/styles/furo.css +18 -0
- google/adk/docs/build/html/_static/styles/furo.css.map +1 -0
- google/adk/docs/build/html/genindex.html +861 -0
- google/adk/docs/build/html/google-adk.html +5461 -0
- google/adk/docs/build/html/index.html +567 -0
- google/adk/docs/build/html/objects.inv +0 -0
- google/adk/docs/build/html/py-modindex.html +373 -0
- google/adk/docs/build/html/search.html +333 -0
- google/adk/docs/build/html/searchindex.js +17 -0
- google/adk/docs/source/conf.py +133 -0
- google/adk/docs/source/google-adk.rst +98 -0
- google/adk/docs/source/index.rst +7 -0
- google/adk/evaluation/__init__.py +31 -0
- google/adk/evaluation/agent_evaluator.py +329 -0
- google/adk/evaluation/evaluation_constants.py +24 -0
- google/adk/evaluation/evaluation_generator.py +270 -0
- google/adk/evaluation/response_evaluator.py +135 -0
- google/adk/evaluation/trajectory_evaluator.py +184 -0
- google/adk/events/__init__.py +21 -0
- google/adk/events/event.py +130 -0
- google/adk/events/event_actions.py +55 -0
- google/adk/examples/__init__.py +28 -0
- google/adk/examples/base_example_provider.py +35 -0
- google/adk/examples/example.py +27 -0
- google/adk/examples/example_util.py +123 -0
- google/adk/examples/vertex_ai_example_store.py +104 -0
- google/adk/flows/__init__.py +14 -0
- google/adk/flows/llm_flows/__init__.py +20 -0
- google/adk/flows/llm_flows/_base_llm_processor.py +52 -0
- google/adk/flows/llm_flows/_code_execution.py +458 -0
- google/adk/flows/llm_flows/_nl_planning.py +129 -0
- google/adk/flows/llm_flows/agent_transfer.py +132 -0
- google/adk/flows/llm_flows/audio_transcriber.py +109 -0
- google/adk/flows/llm_flows/auto_flow.py +49 -0
- google/adk/flows/llm_flows/base_llm_flow.py +559 -0
- google/adk/flows/llm_flows/basic.py +72 -0
- google/adk/flows/llm_flows/contents.py +370 -0
- google/adk/flows/llm_flows/functions.py +486 -0
- google/adk/flows/llm_flows/identity.py +47 -0
- google/adk/flows/llm_flows/instructions.py +137 -0
- google/adk/flows/llm_flows/single_flow.py +57 -0
- google/adk/memory/__init__.py +35 -0
- google/adk/memory/base_memory_service.py +74 -0
- google/adk/memory/in_memory_memory_service.py +62 -0
- google/adk/memory/vertex_ai_rag_memory_service.py +177 -0
- google/adk/models/__init__.py +31 -0
- google/adk/models/anthropic_llm.py +243 -0
- google/adk/models/base_llm.py +87 -0
- google/adk/models/base_llm_connection.py +76 -0
- google/adk/models/gemini_llm_connection.py +200 -0
- google/adk/models/google_llm.py +331 -0
- google/adk/models/lite_llm.py +673 -0
- google/adk/models/llm_request.py +98 -0
- google/adk/models/llm_response.py +111 -0
- google/adk/models/registry.py +102 -0
- google/adk/planners/__init__.py +23 -0
- google/adk/planners/base_planner.py +66 -0
- google/adk/planners/built_in_planner.py +75 -0
- google/adk/planners/plan_re_act_planner.py +208 -0
- google/adk/runners.py +456 -0
- google/adk/sessions/__init__.py +41 -0
- google/adk/sessions/base_session_service.py +133 -0
- google/adk/sessions/database_session_service.py +522 -0
- google/adk/sessions/in_memory_session_service.py +206 -0
- google/adk/sessions/session.py +54 -0
- google/adk/sessions/state.py +71 -0
- google/adk/sessions/vertex_ai_session_service.py +356 -0
- google/adk/telemetry.py +189 -0
- google/adk/tests/__init__.py +14 -0
- google/adk/tests/integration/.env.example +10 -0
- google/adk/tests/integration/__init__.py +18 -0
- google/adk/tests/integration/conftest.py +119 -0
- google/adk/tests/integration/fixture/__init__.py +14 -0
- google/adk/tests/integration/fixture/agent_with_config/__init__.py +15 -0
- google/adk/tests/integration/fixture/agent_with_config/agent.py +88 -0
- google/adk/tests/integration/fixture/callback_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/callback_agent/agent.py +105 -0
- google/adk/tests/integration/fixture/context_update_test/OWNERS +1 -0
- google/adk/tests/integration/fixture/context_update_test/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_update_test/agent.py +43 -0
- google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +582 -0
- google/adk/tests/integration/fixture/context_variable_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_variable_agent/agent.py +115 -0
- google/adk/tests/integration/fixture/customer_support_ma/__init__.py +15 -0
- google/adk/tests/integration/fixture/customer_support_ma/agent.py +172 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +338 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +69 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +15 -0
- google/adk/tests/integration/fixture/flow_complex_spark/agent.py +182 -0
- google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +190 -0
- google/adk/tests/integration/fixture/hello_world_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/hello_world_agent/agent.py +95 -0
- google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +24 -0
- google/adk/tests/integration/fixture/hello_world_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/home_automation_agent/agent.py +304 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/tool_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/tool_agent/agent.py +218 -0
- google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
- google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/trip_planner_agent/agent.py +110 -0
- google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +7 -0
- google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +19 -0
- google/adk/tests/integration/models/__init__.py +14 -0
- google/adk/tests/integration/models/test_google_llm.py +65 -0
- google/adk/tests/integration/test_callback.py +70 -0
- google/adk/tests/integration/test_context_variable.py +67 -0
- google/adk/tests/integration/test_evalute_agent_in_fixture.py +76 -0
- google/adk/tests/integration/test_multi_agent.py +28 -0
- google/adk/tests/integration/test_multi_turn.py +42 -0
- google/adk/tests/integration/test_single_agent.py +23 -0
- google/adk/tests/integration/test_sub_agent.py +26 -0
- google/adk/tests/integration/test_system_instruction.py +177 -0
- google/adk/tests/integration/test_tools.py +287 -0
- google/adk/tests/integration/test_with_test_file.py +34 -0
- google/adk/tests/integration/tools/__init__.py +14 -0
- google/adk/tests/integration/utils/__init__.py +16 -0
- google/adk/tests/integration/utils/asserts.py +75 -0
- google/adk/tests/integration/utils/test_runner.py +97 -0
- google/adk/tests/unittests/__init__.py +14 -0
- google/adk/tests/unittests/agents/__init__.py +14 -0
- google/adk/tests/unittests/agents/test_base_agent.py +407 -0
- google/adk/tests/unittests/agents/test_langgraph_agent.py +191 -0
- google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +138 -0
- google/adk/tests/unittests/agents/test_llm_agent_fields.py +231 -0
- google/adk/tests/unittests/agents/test_loop_agent.py +136 -0
- google/adk/tests/unittests/agents/test_parallel_agent.py +92 -0
- google/adk/tests/unittests/agents/test_sequential_agent.py +114 -0
- google/adk/tests/unittests/artifacts/__init__.py +14 -0
- google/adk/tests/unittests/artifacts/test_artifact_service.py +276 -0
- google/adk/tests/unittests/auth/test_auth_handler.py +575 -0
- google/adk/tests/unittests/conftest.py +73 -0
- google/adk/tests/unittests/fast_api/__init__.py +14 -0
- google/adk/tests/unittests/fast_api/test_fast_api.py +269 -0
- google/adk/tests/unittests/flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/_test_examples.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +311 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +244 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +346 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +93 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +258 -0
- google/adk/tests/unittests/flows/llm_flows/test_identity.py +66 -0
- google/adk/tests/unittests/flows/llm_flows/test_instructions.py +164 -0
- google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +46 -0
- google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +269 -0
- google/adk/tests/unittests/models/__init__.py +14 -0
- google/adk/tests/unittests/models/test_google_llm.py +224 -0
- google/adk/tests/unittests/models/test_litellm.py +804 -0
- google/adk/tests/unittests/models/test_models.py +60 -0
- google/adk/tests/unittests/sessions/__init__.py +14 -0
- google/adk/tests/unittests/sessions/test_session_service.py +227 -0
- google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +246 -0
- google/adk/tests/unittests/streaming/__init__.py +14 -0
- google/adk/tests/unittests/streaming/test_streaming.py +50 -0
- google/adk/tests/unittests/tools/__init__.py +14 -0
- google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +499 -0
- google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +204 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +600 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +630 -0
- google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +345 -0
- google/adk/tests/unittests/tools/google_api_tool/__init__.py +13 -0
- google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +657 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +145 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +68 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +153 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +196 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +573 -0
- google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +436 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +1367 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +628 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +139 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +406 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +966 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +201 -0
- google/adk/tests/unittests/tools/retrieval/__init__.py +14 -0
- google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +147 -0
- google/adk/tests/unittests/tools/test_agent_tool.py +167 -0
- google/adk/tests/unittests/tools/test_base_tool.py +141 -0
- google/adk/tests/unittests/tools/test_build_function_declaration.py +277 -0
- google/adk/tests/unittests/utils.py +304 -0
- google/adk/tools/__init__.py +51 -0
- google/adk/tools/_automatic_function_calling_util.py +346 -0
- google/adk/tools/agent_tool.py +176 -0
- google/adk/tools/apihub_tool/__init__.py +19 -0
- google/adk/tools/apihub_tool/apihub_toolset.py +209 -0
- google/adk/tools/apihub_tool/clients/__init__.py +13 -0
- google/adk/tools/apihub_tool/clients/apihub_client.py +332 -0
- google/adk/tools/apihub_tool/clients/secret_client.py +115 -0
- google/adk/tools/application_integration_tool/__init__.py +19 -0
- google/adk/tools/application_integration_tool/application_integration_toolset.py +230 -0
- google/adk/tools/application_integration_tool/clients/connections_client.py +903 -0
- google/adk/tools/application_integration_tool/clients/integration_client.py +253 -0
- google/adk/tools/base_tool.py +144 -0
- google/adk/tools/built_in_code_execution_tool.py +59 -0
- google/adk/tools/crewai_tool.py +72 -0
- google/adk/tools/example_tool.py +62 -0
- google/adk/tools/exit_loop_tool.py +23 -0
- google/adk/tools/function_parameter_parse_util.py +307 -0
- google/adk/tools/function_tool.py +87 -0
- google/adk/tools/get_user_choice_tool.py +28 -0
- google/adk/tools/google_api_tool/__init__.py +14 -0
- google/adk/tools/google_api_tool/google_api_tool.py +59 -0
- google/adk/tools/google_api_tool/google_api_tool_set.py +107 -0
- google/adk/tools/google_api_tool/google_api_tool_sets.py +55 -0
- google/adk/tools/google_api_tool/googleapi_to_openapi_converter.py +521 -0
- google/adk/tools/google_search_tool.py +68 -0
- google/adk/tools/langchain_tool.py +86 -0
- google/adk/tools/load_artifacts_tool.py +113 -0
- google/adk/tools/load_memory_tool.py +58 -0
- google/adk/tools/load_web_page.py +41 -0
- google/adk/tools/long_running_tool.py +39 -0
- google/adk/tools/mcp_tool/__init__.py +42 -0
- google/adk/tools/mcp_tool/conversion_utils.py +161 -0
- google/adk/tools/mcp_tool/mcp_tool.py +113 -0
- google/adk/tools/mcp_tool/mcp_toolset.py +272 -0
- google/adk/tools/openapi_tool/__init__.py +21 -0
- google/adk/tools/openapi_tool/auth/__init__.py +19 -0
- google/adk/tools/openapi_tool/auth/auth_helpers.py +498 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/__init__.py +25 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.py +105 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/base_credential_exchanger.py +55 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.py +117 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.py +97 -0
- google/adk/tools/openapi_tool/common/__init__.py +19 -0
- google/adk/tools/openapi_tool/common/common.py +300 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/__init__.py +32 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +231 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py +144 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py +260 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py +496 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py +268 -0
- google/adk/tools/preload_memory_tool.py +72 -0
- google/adk/tools/retrieval/__init__.py +36 -0
- google/adk/tools/retrieval/base_retrieval_tool.py +37 -0
- google/adk/tools/retrieval/files_retrieval.py +33 -0
- google/adk/tools/retrieval/llama_index_retrieval.py +41 -0
- google/adk/tools/retrieval/vertex_ai_rag_retrieval.py +107 -0
- google/adk/tools/tool_context.py +90 -0
- google/adk/tools/toolbox_tool.py +46 -0
- google/adk/tools/transfer_to_agent_tool.py +21 -0
- google/adk/tools/vertex_ai_search_tool.py +96 -0
- google/adk/version.py +16 -0
- google_adk-0.0.3.dist-info/METADATA +73 -0
- google_adk-0.0.3.dist-info/RECORD +340 -0
- {google_adk-0.0.1.dist-info → google_adk-0.0.3.dist-info}/WHEEL +1 -2
- google_adk-0.0.3.dist-info/entry_points.txt +3 -0
- agent_kit/__init__.py +0 -0
- google_adk-0.0.1.dist-info/LICENSE.txt +0 -170
- google_adk-0.0.1.dist-info/METADATA +0 -15
- google_adk-0.0.1.dist-info/RECORD +0 -6
- google_adk-0.0.1.dist-info/top_level.txt +0 -1
@@ -0,0 +1,486 @@
|
|
1
|
+
# Copyright 2025 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Handles function callings for LLM flow."""
|
16
|
+
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
import asyncio
|
20
|
+
import inspect
|
21
|
+
import logging
|
22
|
+
from typing import Any
|
23
|
+
from typing import AsyncGenerator
|
24
|
+
from typing import cast
|
25
|
+
from typing import Optional
|
26
|
+
import uuid
|
27
|
+
|
28
|
+
from google.genai import types
|
29
|
+
|
30
|
+
from ...agents.active_streaming_tool import ActiveStreamingTool
|
31
|
+
from ...agents.invocation_context import InvocationContext
|
32
|
+
from ...auth.auth_tool import AuthToolArguments
|
33
|
+
from ...events.event import Event
|
34
|
+
from ...events.event_actions import EventActions
|
35
|
+
from ...telemetry import trace_tool_call
|
36
|
+
from ...telemetry import trace_tool_response
|
37
|
+
from ...telemetry import tracer
|
38
|
+
from ...tools.base_tool import BaseTool
|
39
|
+
from ...tools.tool_context import ToolContext
|
40
|
+
|
41
|
+
AF_FUNCTION_CALL_ID_PREFIX = 'adk-'
|
42
|
+
REQUEST_EUC_FUNCTION_CALL_NAME = 'adk_request_credential'
|
43
|
+
|
44
|
+
logger = logging.getLogger(__name__)
|
45
|
+
|
46
|
+
|
47
|
+
def generate_client_function_call_id() -> str:
|
48
|
+
return f'{AF_FUNCTION_CALL_ID_PREFIX}{uuid.uuid4()}'
|
49
|
+
|
50
|
+
|
51
|
+
def populate_client_function_call_id(model_response_event: Event) -> None:
|
52
|
+
if not model_response_event.get_function_calls():
|
53
|
+
return
|
54
|
+
for function_call in model_response_event.get_function_calls():
|
55
|
+
if not function_call.id:
|
56
|
+
function_call.id = generate_client_function_call_id()
|
57
|
+
|
58
|
+
|
59
|
+
def remove_client_function_call_id(content: types.Content) -> None:
|
60
|
+
if content and content.parts:
|
61
|
+
for part in content.parts:
|
62
|
+
if (
|
63
|
+
part.function_call
|
64
|
+
and part.function_call.id
|
65
|
+
and part.function_call.id.startswith(AF_FUNCTION_CALL_ID_PREFIX)
|
66
|
+
):
|
67
|
+
part.function_call.id = None
|
68
|
+
if (
|
69
|
+
part.function_response
|
70
|
+
and part.function_response.id
|
71
|
+
and part.function_response.id.startswith(AF_FUNCTION_CALL_ID_PREFIX)
|
72
|
+
):
|
73
|
+
part.function_response.id = None
|
74
|
+
|
75
|
+
|
76
|
+
def get_long_running_function_calls(
|
77
|
+
function_calls: list[types.FunctionCall],
|
78
|
+
tools_dict: dict[str, BaseTool],
|
79
|
+
) -> set[str]:
|
80
|
+
long_running_tool_ids = set()
|
81
|
+
for function_call in function_calls:
|
82
|
+
if (
|
83
|
+
function_call.name in tools_dict
|
84
|
+
and tools_dict[function_call.name].is_long_running
|
85
|
+
):
|
86
|
+
long_running_tool_ids.add(function_call.id)
|
87
|
+
|
88
|
+
return long_running_tool_ids
|
89
|
+
|
90
|
+
|
91
|
+
def generate_auth_event(
|
92
|
+
invocation_context: InvocationContext,
|
93
|
+
function_response_event: Event,
|
94
|
+
) -> Optional[Event]:
|
95
|
+
if not function_response_event.actions.requested_auth_configs:
|
96
|
+
return None
|
97
|
+
parts = []
|
98
|
+
long_running_tool_ids = set()
|
99
|
+
for (
|
100
|
+
function_call_id,
|
101
|
+
auth_config,
|
102
|
+
) in function_response_event.actions.requested_auth_configs.items():
|
103
|
+
|
104
|
+
request_euc_function_call = types.FunctionCall(
|
105
|
+
name=REQUEST_EUC_FUNCTION_CALL_NAME,
|
106
|
+
args=AuthToolArguments(
|
107
|
+
function_call_id=function_call_id,
|
108
|
+
auth_config=auth_config,
|
109
|
+
).model_dump(exclude_none=True),
|
110
|
+
)
|
111
|
+
request_euc_function_call.id = generate_client_function_call_id()
|
112
|
+
long_running_tool_ids.add(request_euc_function_call.id)
|
113
|
+
parts.append(types.Part(function_call=request_euc_function_call))
|
114
|
+
|
115
|
+
return Event(
|
116
|
+
invocation_id=invocation_context.invocation_id,
|
117
|
+
author=invocation_context.agent.name,
|
118
|
+
branch=invocation_context.branch,
|
119
|
+
content=types.Content(parts=parts),
|
120
|
+
long_running_tool_ids=long_running_tool_ids,
|
121
|
+
)
|
122
|
+
|
123
|
+
|
124
|
+
async def handle_function_calls_async(
|
125
|
+
invocation_context: InvocationContext,
|
126
|
+
function_call_event: Event,
|
127
|
+
tools_dict: dict[str, BaseTool],
|
128
|
+
filters: Optional[set[str]] = None,
|
129
|
+
) -> Optional[Event]:
|
130
|
+
"""Calls the functions and returns the function response event."""
|
131
|
+
from ...agents.llm_agent import LlmAgent
|
132
|
+
|
133
|
+
agent = invocation_context.agent
|
134
|
+
if not isinstance(agent, LlmAgent):
|
135
|
+
return
|
136
|
+
|
137
|
+
function_calls = function_call_event.get_function_calls()
|
138
|
+
|
139
|
+
function_response_events: list[Event] = []
|
140
|
+
for function_call in function_calls:
|
141
|
+
if filters and function_call.id not in filters:
|
142
|
+
continue
|
143
|
+
tool, tool_context = _get_tool_and_context(
|
144
|
+
invocation_context,
|
145
|
+
function_call_event,
|
146
|
+
function_call,
|
147
|
+
tools_dict,
|
148
|
+
)
|
149
|
+
# do not use "args" as the variable name, because it is a reserved keyword
|
150
|
+
# in python debugger.
|
151
|
+
function_args = function_call.args or {}
|
152
|
+
function_response = None
|
153
|
+
# Calls the tool if before_tool_callback does not exist or returns None.
|
154
|
+
if agent.before_tool_callback:
|
155
|
+
function_response = agent.before_tool_callback(
|
156
|
+
tool=tool, args=function_args, tool_context=tool_context
|
157
|
+
)
|
158
|
+
|
159
|
+
if not function_response:
|
160
|
+
function_response = await __call_tool_async(
|
161
|
+
tool, args=function_args, tool_context=tool_context
|
162
|
+
)
|
163
|
+
|
164
|
+
# Calls after_tool_callback if it exists.
|
165
|
+
if agent.after_tool_callback:
|
166
|
+
new_response = agent.after_tool_callback(
|
167
|
+
tool=tool,
|
168
|
+
args=function_args,
|
169
|
+
tool_context=tool_context,
|
170
|
+
tool_response=function_response,
|
171
|
+
)
|
172
|
+
if new_response:
|
173
|
+
function_response = new_response
|
174
|
+
|
175
|
+
if tool.is_long_running:
|
176
|
+
# Allow long running function to return None to not provide function response.
|
177
|
+
if not function_response:
|
178
|
+
continue
|
179
|
+
|
180
|
+
# Builds the function response event.
|
181
|
+
function_response_event = __build_response_event(
|
182
|
+
tool, function_response, tool_context, invocation_context
|
183
|
+
)
|
184
|
+
function_response_events.append(function_response_event)
|
185
|
+
|
186
|
+
if not function_response_events:
|
187
|
+
return None
|
188
|
+
merged_event = merge_parallel_function_response_events(
|
189
|
+
function_response_events
|
190
|
+
)
|
191
|
+
if len(function_response_events) > 1:
|
192
|
+
# this is needed for debug traces of parallel calls
|
193
|
+
# individual response with tool.name is traced in __build_response_event
|
194
|
+
# (we drop tool.name from span name here as this is merged event)
|
195
|
+
with tracer.start_as_current_span('tool_response'):
|
196
|
+
trace_tool_response(
|
197
|
+
invocation_context=invocation_context,
|
198
|
+
event_id=merged_event.id,
|
199
|
+
function_response_event=merged_event,
|
200
|
+
)
|
201
|
+
return merged_event
|
202
|
+
|
203
|
+
|
204
|
+
async def handle_function_calls_live(
|
205
|
+
invocation_context: InvocationContext,
|
206
|
+
function_call_event: Event,
|
207
|
+
tools_dict: dict[str, BaseTool],
|
208
|
+
) -> Event:
|
209
|
+
"""Calls the functions and returns the function response event."""
|
210
|
+
from ...agents.llm_agent import LlmAgent
|
211
|
+
|
212
|
+
agent = cast(LlmAgent, invocation_context.agent)
|
213
|
+
function_calls = function_call_event.get_function_calls()
|
214
|
+
|
215
|
+
function_response_events: list[Event] = []
|
216
|
+
for function_call in function_calls:
|
217
|
+
tool, tool_context = _get_tool_and_context(
|
218
|
+
invocation_context, function_call_event, function_call, tools_dict
|
219
|
+
)
|
220
|
+
# do not use "args" as the variable name, because it is a reserved keyword
|
221
|
+
# in python debugger.
|
222
|
+
function_args = function_call.args or {}
|
223
|
+
function_response = None
|
224
|
+
# Calls the tool if before_tool_callback does not exist or returns None.
|
225
|
+
if agent.before_tool_callback:
|
226
|
+
function_response = agent.before_tool_callback(
|
227
|
+
tool, function_args, tool_context
|
228
|
+
)
|
229
|
+
|
230
|
+
if not function_response:
|
231
|
+
function_response = await _process_function_live_helper(
|
232
|
+
tool, tool_context, function_call, function_args, invocation_context
|
233
|
+
)
|
234
|
+
|
235
|
+
# Calls after_tool_callback if it exists.
|
236
|
+
if agent.after_tool_callback:
|
237
|
+
new_response = agent.after_tool_callback(
|
238
|
+
tool,
|
239
|
+
function_args,
|
240
|
+
tool_context,
|
241
|
+
function_response,
|
242
|
+
)
|
243
|
+
if new_response:
|
244
|
+
function_response = new_response
|
245
|
+
|
246
|
+
if tool.is_long_running:
|
247
|
+
# Allow async function to return None to not provide function response.
|
248
|
+
if not function_response:
|
249
|
+
continue
|
250
|
+
|
251
|
+
# Builds the function response event.
|
252
|
+
function_response_event = __build_response_event(
|
253
|
+
tool, function_response, tool_context, invocation_context
|
254
|
+
)
|
255
|
+
function_response_events.append(function_response_event)
|
256
|
+
|
257
|
+
if not function_response_events:
|
258
|
+
return None
|
259
|
+
merged_event = merge_parallel_function_response_events(
|
260
|
+
function_response_events
|
261
|
+
)
|
262
|
+
return merged_event
|
263
|
+
|
264
|
+
|
265
|
+
async def _process_function_live_helper(
|
266
|
+
tool, tool_context, function_call, function_args, invocation_context
|
267
|
+
):
|
268
|
+
function_response = None
|
269
|
+
# Check if this is a stop_streaming function call
|
270
|
+
if (
|
271
|
+
function_call.name == 'stop_streaming'
|
272
|
+
and 'function_name' in function_args
|
273
|
+
):
|
274
|
+
function_name = function_args['function_name']
|
275
|
+
active_tasks = invocation_context.active_streaming_tools
|
276
|
+
if (
|
277
|
+
function_name in active_tasks
|
278
|
+
and active_tasks[function_name].task
|
279
|
+
and not active_tasks[function_name].task.done()
|
280
|
+
):
|
281
|
+
task = active_tasks[function_name].task
|
282
|
+
task.cancel()
|
283
|
+
try:
|
284
|
+
# Wait for the task to be cancelled
|
285
|
+
await asyncio.wait_for(task, timeout=1.0)
|
286
|
+
except (asyncio.CancelledError, asyncio.TimeoutError):
|
287
|
+
# Log the specific condition
|
288
|
+
if task.cancelled():
|
289
|
+
logging.info(f'Task {function_name} was cancelled successfully')
|
290
|
+
elif task.done():
|
291
|
+
logging.info(f'Task {function_name} completed during cancellation')
|
292
|
+
else:
|
293
|
+
logging.warning(
|
294
|
+
f'Task {function_name} might still be running after'
|
295
|
+
' cancellation timeout'
|
296
|
+
)
|
297
|
+
function_response = {
|
298
|
+
'status': f'The task is not cancelled yet for {function_name}.'
|
299
|
+
}
|
300
|
+
if not function_response:
|
301
|
+
# Clean up the reference
|
302
|
+
active_tasks[function_name].task = None
|
303
|
+
|
304
|
+
function_response = {
|
305
|
+
'status': f'Successfully stopped streaming function {function_name}'
|
306
|
+
}
|
307
|
+
else:
|
308
|
+
function_response = {
|
309
|
+
'status': f'No active streaming function named {function_name} found'
|
310
|
+
}
|
311
|
+
elif inspect.isasyncgenfunction(tool.func):
|
312
|
+
print('is async')
|
313
|
+
|
314
|
+
# for streaming tool use case
|
315
|
+
# we require the function to be a async generator function
|
316
|
+
async def run_tool_and_update_queue(tool, function_args, tool_context):
|
317
|
+
try:
|
318
|
+
async for result in __call_tool_live(
|
319
|
+
tool=tool,
|
320
|
+
args=function_args,
|
321
|
+
tool_context=tool_context,
|
322
|
+
invocation_context=invocation_context,
|
323
|
+
):
|
324
|
+
updated_content = types.Content(
|
325
|
+
role='user',
|
326
|
+
parts=[
|
327
|
+
types.Part.from_text(
|
328
|
+
text=f'Function {tool.name} returned: {result}'
|
329
|
+
)
|
330
|
+
],
|
331
|
+
)
|
332
|
+
invocation_context.live_request_queue.send_content(updated_content)
|
333
|
+
except asyncio.CancelledError:
|
334
|
+
raise # Re-raise to properly propagate the cancellation
|
335
|
+
|
336
|
+
task = asyncio.create_task(
|
337
|
+
run_tool_and_update_queue(tool, function_args, tool_context)
|
338
|
+
)
|
339
|
+
if invocation_context.active_streaming_tools is None:
|
340
|
+
invocation_context.active_streaming_tools = {}
|
341
|
+
if tool.name in invocation_context.active_streaming_tools:
|
342
|
+
invocation_context.active_streaming_tools[tool.name].task = task
|
343
|
+
else:
|
344
|
+
invocation_context.active_streaming_tools[tool.name] = (
|
345
|
+
ActiveStreamingTool(task=task)
|
346
|
+
)
|
347
|
+
# Immediately return a pending response.
|
348
|
+
# This is required by current live model.
|
349
|
+
function_response = {
|
350
|
+
'status': (
|
351
|
+
'The function is running asynchronously and the results are'
|
352
|
+
' pending.'
|
353
|
+
)
|
354
|
+
}
|
355
|
+
else:
|
356
|
+
function_response = await __call_tool_async(
|
357
|
+
tool, args=function_args, tool_context=tool_context
|
358
|
+
)
|
359
|
+
return function_response
|
360
|
+
|
361
|
+
|
362
|
+
def _get_tool_and_context(
|
363
|
+
invocation_context: InvocationContext,
|
364
|
+
function_call_event: Event,
|
365
|
+
function_call: types.FunctionCall,
|
366
|
+
tools_dict: dict[str, BaseTool],
|
367
|
+
):
|
368
|
+
if function_call.name not in tools_dict:
|
369
|
+
raise ValueError(
|
370
|
+
f'Function {function_call.name} is not found in the tools_dict.'
|
371
|
+
)
|
372
|
+
|
373
|
+
tool_context = ToolContext(
|
374
|
+
invocation_context=invocation_context,
|
375
|
+
function_call_id=function_call.id,
|
376
|
+
)
|
377
|
+
|
378
|
+
tool = tools_dict[function_call.name]
|
379
|
+
|
380
|
+
return (tool, tool_context)
|
381
|
+
|
382
|
+
|
383
|
+
async def __call_tool_live(
|
384
|
+
tool: BaseTool,
|
385
|
+
args: dict[str, object],
|
386
|
+
tool_context: ToolContext,
|
387
|
+
invocation_context: InvocationContext,
|
388
|
+
) -> AsyncGenerator[Event, None]:
|
389
|
+
"""Calls the tool asynchronously (awaiting the coroutine)."""
|
390
|
+
with tracer.start_as_current_span(f'tool_call [{tool.name}]'):
|
391
|
+
trace_tool_call(args=args)
|
392
|
+
async for item in tool._call_live(
|
393
|
+
args=args,
|
394
|
+
tool_context=tool_context,
|
395
|
+
invocation_context=invocation_context,
|
396
|
+
):
|
397
|
+
yield item
|
398
|
+
|
399
|
+
|
400
|
+
async def __call_tool_async(
|
401
|
+
tool: BaseTool,
|
402
|
+
args: dict[str, Any],
|
403
|
+
tool_context: ToolContext,
|
404
|
+
) -> Any:
|
405
|
+
"""Calls the tool."""
|
406
|
+
with tracer.start_as_current_span(f'tool_call [{tool.name}]'):
|
407
|
+
trace_tool_call(args=args)
|
408
|
+
return await tool.run_async(args=args, tool_context=tool_context)
|
409
|
+
|
410
|
+
|
411
|
+
def __build_response_event(
|
412
|
+
tool: BaseTool,
|
413
|
+
function_result: dict[str, object],
|
414
|
+
tool_context: ToolContext,
|
415
|
+
invocation_context: InvocationContext,
|
416
|
+
) -> Event:
|
417
|
+
with tracer.start_as_current_span(f'tool_response [{tool.name}]'):
|
418
|
+
# Specs requires the result to be a dict.
|
419
|
+
if not isinstance(function_result, dict):
|
420
|
+
function_result = {'result': function_result}
|
421
|
+
|
422
|
+
part_function_response = types.Part.from_function_response(
|
423
|
+
name=tool.name, response=function_result
|
424
|
+
)
|
425
|
+
part_function_response.function_response.id = tool_context.function_call_id
|
426
|
+
|
427
|
+
content = types.Content(
|
428
|
+
role='user',
|
429
|
+
parts=[part_function_response],
|
430
|
+
)
|
431
|
+
|
432
|
+
function_response_event = Event(
|
433
|
+
invocation_id=invocation_context.invocation_id,
|
434
|
+
author=invocation_context.agent.name,
|
435
|
+
content=content,
|
436
|
+
actions=tool_context.actions,
|
437
|
+
branch=invocation_context.branch,
|
438
|
+
)
|
439
|
+
|
440
|
+
trace_tool_response(
|
441
|
+
invocation_context=invocation_context,
|
442
|
+
event_id=function_response_event.id,
|
443
|
+
function_response_event=function_response_event,
|
444
|
+
)
|
445
|
+
return function_response_event
|
446
|
+
|
447
|
+
|
448
|
+
def merge_parallel_function_response_events(
|
449
|
+
function_response_events: list['Event'],
|
450
|
+
) -> 'Event':
|
451
|
+
if not function_response_events:
|
452
|
+
raise ValueError('No function response events provided.')
|
453
|
+
|
454
|
+
if len(function_response_events) == 1:
|
455
|
+
return function_response_events[0]
|
456
|
+
merged_parts = []
|
457
|
+
for event in function_response_events:
|
458
|
+
if event.content:
|
459
|
+
for part in event.content.parts or []:
|
460
|
+
merged_parts.append(part)
|
461
|
+
|
462
|
+
# Use the first event as the "base" for common attributes
|
463
|
+
base_event = function_response_events[0]
|
464
|
+
|
465
|
+
# Merge actions from all events
|
466
|
+
|
467
|
+
merged_actions = EventActions()
|
468
|
+
merged_requested_auth_configs = {}
|
469
|
+
for event in function_response_events:
|
470
|
+
merged_requested_auth_configs.update(event.actions.requested_auth_configs)
|
471
|
+
merged_actions = merged_actions.model_copy(
|
472
|
+
update=event.actions.model_dump()
|
473
|
+
)
|
474
|
+
merged_actions.requested_auth_configs = merged_requested_auth_configs
|
475
|
+
# Create the new merged event
|
476
|
+
merged_event = Event(
|
477
|
+
invocation_id=Event.new_id(),
|
478
|
+
author=base_event.author,
|
479
|
+
branch=base_event.branch,
|
480
|
+
content=types.Content(role='user', parts=merged_parts),
|
481
|
+
actions=merged_actions, # Optionally merge actions if required
|
482
|
+
)
|
483
|
+
|
484
|
+
# Use the base_event as the timestamp
|
485
|
+
merged_event.timestamp = base_event.timestamp
|
486
|
+
return merged_event
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# Copyright 2025 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Gives the agent identity from the framework."""
|
16
|
+
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
from typing import AsyncGenerator
|
20
|
+
|
21
|
+
from typing_extensions import override
|
22
|
+
|
23
|
+
from ...agents.invocation_context import InvocationContext
|
24
|
+
from ...events.event import Event
|
25
|
+
from ...models.llm_request import LlmRequest
|
26
|
+
from ._base_llm_processor import BaseLlmRequestProcessor
|
27
|
+
|
28
|
+
|
29
|
+
class _IdentityLlmRequestProcessor(BaseLlmRequestProcessor):
|
30
|
+
"""Gives the agent identity from the framework."""
|
31
|
+
|
32
|
+
@override
|
33
|
+
async def run_async(
|
34
|
+
self, invocation_context: InvocationContext, llm_request: LlmRequest
|
35
|
+
) -> AsyncGenerator[Event, None]:
|
36
|
+
agent = invocation_context.agent
|
37
|
+
si = [f'You are an agent. Your internal name is "{agent.name}".']
|
38
|
+
if agent.description:
|
39
|
+
si.append(f' The description about you is "{agent.description}"')
|
40
|
+
llm_request.append_instructions(si)
|
41
|
+
|
42
|
+
# Maintain async generator behavior
|
43
|
+
if False: # Ensures it behaves as a generator
|
44
|
+
yield # This is a no-op but maintains generator structure
|
45
|
+
|
46
|
+
|
47
|
+
request_processor = _IdentityLlmRequestProcessor()
|
@@ -0,0 +1,137 @@
|
|
1
|
+
# Copyright 2025 Google LLC
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
"""Handles instructions and global instructions for LLM flow."""
|
16
|
+
|
17
|
+
from __future__ import annotations
|
18
|
+
|
19
|
+
import re
|
20
|
+
from typing import AsyncGenerator
|
21
|
+
from typing import Generator
|
22
|
+
from typing import TYPE_CHECKING
|
23
|
+
|
24
|
+
from typing_extensions import override
|
25
|
+
|
26
|
+
from ...agents.readonly_context import ReadonlyContext
|
27
|
+
from ...events.event import Event
|
28
|
+
from ...sessions.state import State
|
29
|
+
from ._base_llm_processor import BaseLlmRequestProcessor
|
30
|
+
|
31
|
+
if TYPE_CHECKING:
|
32
|
+
from ...agents.invocation_context import InvocationContext
|
33
|
+
from ...models.llm_request import LlmRequest
|
34
|
+
|
35
|
+
|
36
|
+
class _InstructionsLlmRequestProcessor(BaseLlmRequestProcessor):
|
37
|
+
"""Handles instructions and global instructions for LLM flow."""
|
38
|
+
|
39
|
+
@override
|
40
|
+
async def run_async(
|
41
|
+
self, invocation_context: InvocationContext, llm_request: LlmRequest
|
42
|
+
) -> AsyncGenerator[Event, None]:
|
43
|
+
from ...agents.base_agent import BaseAgent
|
44
|
+
from ...agents.llm_agent import LlmAgent
|
45
|
+
|
46
|
+
agent = invocation_context.agent
|
47
|
+
if not isinstance(agent, LlmAgent):
|
48
|
+
return
|
49
|
+
|
50
|
+
root_agent: BaseAgent = agent.root_agent
|
51
|
+
|
52
|
+
# Appends global instructions if set.
|
53
|
+
if (
|
54
|
+
isinstance(root_agent, LlmAgent) and root_agent.global_instruction
|
55
|
+
): # not emtpy str
|
56
|
+
raw_si = root_agent.canonical_global_instruction(
|
57
|
+
ReadonlyContext(invocation_context)
|
58
|
+
)
|
59
|
+
si = _populate_values(raw_si, invocation_context)
|
60
|
+
llm_request.append_instructions([si])
|
61
|
+
|
62
|
+
# Appends agent instructions if set.
|
63
|
+
if agent.instruction: # not emtpy str
|
64
|
+
raw_si = agent.canonical_instruction(ReadonlyContext(invocation_context))
|
65
|
+
si = _populate_values(raw_si, invocation_context)
|
66
|
+
llm_request.append_instructions([si])
|
67
|
+
|
68
|
+
# Maintain async generator behavior
|
69
|
+
if False: # Ensures it behaves as a generator
|
70
|
+
yield # This is a no-op but maintains generator structure
|
71
|
+
|
72
|
+
|
73
|
+
request_processor = _InstructionsLlmRequestProcessor()
|
74
|
+
|
75
|
+
|
76
|
+
def _populate_values(
|
77
|
+
instruction_template: str,
|
78
|
+
context: InvocationContext,
|
79
|
+
) -> str:
|
80
|
+
"""Populates values in the instruction template, e.g. state, artifact, etc."""
|
81
|
+
|
82
|
+
def _replace_match(match) -> str:
|
83
|
+
var_name = match.group().lstrip('{').rstrip('}').strip()
|
84
|
+
optional = False
|
85
|
+
if var_name.endswith('?'):
|
86
|
+
optional = True
|
87
|
+
var_name = var_name.removesuffix('?')
|
88
|
+
if var_name.startswith('artifact.'):
|
89
|
+
var_name = var_name.removeprefix('artifact.')
|
90
|
+
if context.artifact_service is None:
|
91
|
+
raise ValueError('Artifact service is not initialized.')
|
92
|
+
artifact = context.artifact_service.load_artifact(
|
93
|
+
app_name=context.session.app_name,
|
94
|
+
user_id=context.session.user_id,
|
95
|
+
session_id=context.session.id,
|
96
|
+
filename=var_name,
|
97
|
+
)
|
98
|
+
if not var_name:
|
99
|
+
raise KeyError(f'Artifact {var_name} not found.')
|
100
|
+
return str(artifact)
|
101
|
+
else:
|
102
|
+
if not _is_valid_state_name(var_name):
|
103
|
+
return match.group()
|
104
|
+
if var_name in context.session.state:
|
105
|
+
return str(context.session.state[var_name])
|
106
|
+
else:
|
107
|
+
if optional:
|
108
|
+
return ''
|
109
|
+
else:
|
110
|
+
raise KeyError(f'Context variable not found: `{var_name}`.')
|
111
|
+
|
112
|
+
return re.sub(r'{+[^{}]*}+', _replace_match, instruction_template)
|
113
|
+
|
114
|
+
|
115
|
+
def _is_valid_state_name(var_name):
|
116
|
+
"""Checks if the variable name is a valid state name.
|
117
|
+
|
118
|
+
Valid state is either:
|
119
|
+
- Valid identifier
|
120
|
+
- <Valid prefix>:<Valid identifier>
|
121
|
+
All the others will just return as it is.
|
122
|
+
|
123
|
+
Args:
|
124
|
+
var_name: The variable name to check.
|
125
|
+
|
126
|
+
Returns:
|
127
|
+
True if the variable name is a valid state name, False otherwise.
|
128
|
+
"""
|
129
|
+
parts = var_name.split(':')
|
130
|
+
if len(parts) == 1:
|
131
|
+
return var_name.isidentifier()
|
132
|
+
|
133
|
+
if len(parts) == 2:
|
134
|
+
prefixes = [State.APP_PREFIX, State.USER_PREFIX, State.TEMP_PREFIX]
|
135
|
+
if (parts[0] + ':') in prefixes:
|
136
|
+
return parts[1].isidentifier()
|
137
|
+
return False
|