@google/adk 2.2.1 → 2.4.0
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.
- package/dist/cjs/a2a/a2a_remote_agent.js +4 -1
- package/dist/cjs/a2a/agent_card.js +102 -26
- package/dist/cjs/a2a/agent_to_a2a.js +1 -1
- package/dist/cjs/agents/agent_engine_client.js +172 -0
- package/dist/cjs/agents/base_agent.js +20 -4
- package/dist/cjs/agents/functions.js +227 -25
- package/dist/cjs/agents/instructions.js +6 -2
- package/dist/cjs/agents/invocation_context.js +26 -4
- package/dist/cjs/agents/langgraph_agent.js +152 -0
- package/dist/cjs/agents/llm_agent.js +194 -107
- package/dist/cjs/agents/processors/basic_llm_request_processor.js +1 -1
- package/dist/cjs/agents/processors/code_execution_request_processor.js +18 -6
- package/dist/cjs/agents/processors/content_processor_utils.js +38 -10
- package/dist/cjs/agents/processors/content_request_processor.js +34 -2
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/cjs/agents/processors/nl_planning_processor.js +12 -3
- package/dist/cjs/artifacts/in_memory_artifact_service.js +14 -0
- package/dist/cjs/auth/auth_handler.js +37 -5
- package/dist/cjs/auth/auth_schemes.js +9 -0
- package/dist/cjs/cli/cli_eval.js +532 -0
- package/dist/cjs/cli/utils/envs.js +116 -0
- package/dist/cjs/cli/utils/evals.js +137 -0
- package/dist/cjs/cli/utils/logs.js +150 -0
- package/dist/cjs/code_executors/agent_engine_sandbox_code_executor.js +7 -2
- package/dist/cjs/code_executors/code_execution_utils.js +48 -16
- package/dist/cjs/code_executors/code_executor_context.js +6 -2
- package/dist/cjs/code_executors/container_code_executor.js +27 -16
- package/dist/cjs/code_executors/unsafe_local_code_executor.js +6 -1
- package/dist/cjs/common.js +61 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +12 -10
- package/dist/cjs/context/token_based_context_compactor.js +2 -1
- package/dist/cjs/errors/input_validation_error.js +3 -2
- package/dist/cjs/events/event.js +4 -2
- package/dist/cjs/examples/example_util.js +23 -2
- package/dist/cjs/flows/llm_flows/audio_transcriber.js +170 -0
- package/dist/cjs/index.js +104 -1
- package/dist/cjs/integrations/agent_registry/helpers.js +1 -1
- package/dist/cjs/models/gemini_llm_connection.js +16 -8
- package/dist/cjs/models/google_llm.js +13 -16
- package/dist/cjs/models/llm_response.js +9 -1
- package/dist/cjs/models/registry.js +1 -1
- package/dist/cjs/models/routed_llm.js +9 -8
- package/dist/cjs/planners/plan_re_act_planner.js +1 -1
- package/dist/cjs/plugins/base_plugin.js +13 -23
- package/dist/cjs/runner/in_memory_runner.js +6 -11
- package/dist/cjs/runner/runner.js +105 -33
- package/dist/cjs/sessions/base_session_service.js +14 -8
- package/dist/cjs/sessions/database_session_service.js +50 -28
- package/dist/cjs/sessions/db/operations.js +1 -1
- package/dist/cjs/sessions/in_memory_session_service.js +3 -1
- package/dist/cjs/sessions/stale_session_error.js +59 -0
- package/dist/cjs/sessions/state.js +8 -4
- package/dist/cjs/sessions/vertex_ai_session_service.js +34 -15
- package/dist/cjs/skills/loader.js +52 -18
- package/dist/cjs/telemetry/setup.js +0 -1
- package/dist/cjs/telemetry/tracing.js +147 -73
- package/dist/cjs/tools/agent_tool.js +44 -0
- package/dist/cjs/tools/apihub_tool/apihub_toolset.js +137 -0
- package/dist/cjs/tools/apihub_tool/clients/apihub_client.js +272 -0
- package/dist/cjs/tools/apihub_tool/clients/secret_client.js +148 -0
- package/dist/cjs/tools/apihub_tool/clients/service_account.js +48 -0
- package/dist/cjs/tools/application_integration_tool/application_integration_toolset.js +263 -0
- package/dist/cjs/tools/application_integration_tool/clients/cloud_request.js +133 -0
- package/dist/cjs/tools/application_integration_tool/clients/connections_client.js +671 -0
- package/dist/cjs/tools/application_integration_tool/clients/integration_client.js +223 -0
- package/dist/cjs/tools/application_integration_tool/integration_connector_tool.js +184 -0
- package/dist/cjs/tools/example_tool.js +10 -0
- package/dist/cjs/tools/function_tool.js +319 -0
- package/dist/cjs/tools/google_api_tool/google_api_tool.js +100 -0
- package/dist/cjs/tools/google_api_tool/google_api_toolset.js +173 -0
- package/dist/cjs/tools/google_api_tool/google_api_toolsets.js +124 -0
- package/dist/cjs/tools/google_api_tool/googleapi_to_openapi_converter.js +440 -0
- package/dist/cjs/tools/google_api_tool/index.js +61 -0
- package/dist/cjs/tools/load_artifacts_tool.js +12 -6
- package/dist/cjs/tools/load_memory_tool.js +12 -10
- package/dist/cjs/tools/mcp/index.js +12 -0
- package/dist/cjs/tools/mcp/mcp_session_manager.js +25 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +1 -14
- package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +1 -2
- package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +46 -2
- package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +4 -3
- package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +20 -6
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/operation_parser.js +17 -6
- package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +14 -2
- package/dist/cjs/tools/openapi_tool/openapi_toolset.js +7 -13
- package/dist/cjs/tools/openapi_tool/rest_api_tool.js +17 -1
- package/dist/cjs/tools/retrieval/base_retrieval_tool.js +18 -3
- package/dist/cjs/tools/toolbox_toolset.js +174 -0
- package/dist/cjs/tools/webmcp/webmcp_toolset.js +1 -14
- package/dist/cjs/utils/client_labels.js +19 -2
- package/dist/cjs/utils/env_aware_utils.js +6 -8
- package/dist/cjs/utils/error_utils.js +11 -11
- package/dist/cjs/utils/failover_utils.js +1 -1
- package/dist/cjs/utils/file_extension_utils.js +11 -0
- package/dist/cjs/utils/file_utils.js +2 -24
- package/dist/cjs/utils/gemini_schema_util.js +28 -7
- package/dist/cjs/utils/json_utils.js +49 -0
- package/dist/cjs/utils/live_connection_utils.js +4 -5
- package/dist/cjs/utils/logger_node.js +11 -1
- package/dist/cjs/utils/object_notation_utils.js +6 -3
- package/dist/cjs/utils/output_schema_utils.js +5 -2
- package/dist/cjs/utils/streaming_utils.js +12 -12
- package/dist/cjs/utils/task.js +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_remote_agent.js +4 -1
- package/dist/esm/a2a/agent_card.js +103 -27
- package/dist/esm/a2a/agent_to_a2a.js +5 -2
- package/dist/esm/agents/agent_engine_client.js +144 -0
- package/dist/esm/agents/base_agent.js +20 -4
- package/dist/esm/agents/functions.js +226 -25
- package/dist/esm/agents/instructions.js +6 -2
- package/dist/esm/agents/invocation_context.js +24 -4
- package/dist/esm/agents/langgraph_agent.js +123 -0
- package/dist/esm/agents/llm_agent.js +204 -110
- package/dist/esm/agents/processors/basic_llm_request_processor.js +1 -1
- package/dist/esm/agents/processors/code_execution_request_processor.js +21 -8
- package/dist/esm/agents/processors/content_processor_utils.js +38 -10
- package/dist/esm/agents/processors/content_request_processor.js +34 -2
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/esm/agents/processors/nl_planning_processor.js +12 -3
- package/dist/esm/artifacts/in_memory_artifact_service.js +14 -0
- package/dist/esm/auth/auth_handler.js +37 -5
- package/dist/esm/auth/auth_schemes.js +8 -0
- package/dist/esm/cli/cli_eval.js +510 -0
- package/dist/esm/cli/utils/envs.js +78 -0
- package/dist/esm/cli/utils/evals.js +109 -0
- package/dist/esm/cli/utils/logs.js +111 -0
- package/dist/esm/code_executors/agent_engine_sandbox_code_executor.js +7 -2
- package/dist/esm/code_executors/code_execution_utils.js +46 -16
- package/dist/esm/code_executors/code_executor_context.js +5 -2
- package/dist/esm/code_executors/container_code_executor.js +27 -16
- package/dist/esm/code_executors/unsafe_local_code_executor.js +6 -1
- package/dist/esm/common.js +65 -4
- package/dist/esm/context/summarizers/llm_summarizer.js +12 -10
- package/dist/esm/context/token_based_context_compactor.js +2 -1
- package/dist/esm/errors/input_validation_error.js +3 -2
- package/dist/esm/events/event.js +4 -2
- package/dist/esm/examples/example_util.js +21 -1
- package/dist/esm/flows/llm_flows/audio_transcriber.js +142 -0
- package/dist/esm/index.js +114 -2
- package/dist/esm/integrations/agent_registry/helpers.js +1 -1
- package/dist/esm/models/gemini_llm_connection.js +16 -8
- package/dist/esm/models/google_llm.js +14 -17
- package/dist/esm/models/llm_response.js +9 -1
- package/dist/esm/models/registry.js +1 -1
- package/dist/esm/models/routed_llm.js +9 -8
- package/dist/esm/planners/plan_re_act_planner.js +1 -1
- package/dist/esm/plugins/base_plugin.js +13 -23
- package/dist/esm/runner/in_memory_runner.js +6 -11
- package/dist/esm/runner/runner.js +105 -33
- package/dist/esm/sessions/base_session_service.js +12 -7
- package/dist/esm/sessions/database_session_service.js +49 -28
- package/dist/esm/sessions/db/operations.js +1 -1
- package/dist/esm/sessions/in_memory_session_service.js +3 -1
- package/dist/esm/sessions/stale_session_error.js +31 -0
- package/dist/esm/sessions/state.js +8 -4
- package/dist/esm/sessions/vertex_ai_session_service.js +34 -15
- package/dist/esm/skills/loader.js +51 -17
- package/dist/esm/telemetry/setup.js +1 -2
- package/dist/esm/telemetry/tracing.js +151 -74
- package/dist/esm/tools/agent_tool.js +44 -0
- package/dist/esm/tools/apihub_tool/apihub_toolset.js +101 -0
- package/dist/esm/tools/apihub_tool/clients/apihub_client.js +244 -0
- package/dist/esm/tools/apihub_tool/clients/secret_client.js +132 -0
- package/dist/esm/tools/apihub_tool/clients/service_account.js +21 -0
- package/dist/esm/tools/application_integration_tool/application_integration_toolset.js +245 -0
- package/dist/esm/tools/application_integration_tool/clients/cloud_request.js +103 -0
- package/dist/esm/tools/application_integration_tool/clients/connections_client.js +629 -0
- package/dist/esm/tools/application_integration_tool/clients/integration_client.js +219 -0
- package/dist/esm/tools/application_integration_tool/integration_connector_tool.js +158 -0
- package/dist/esm/tools/example_tool.js +13 -1
- package/dist/esm/tools/function_tool.js +316 -0
- package/dist/esm/tools/google_api_tool/google_api_tool.js +73 -0
- package/dist/esm/tools/google_api_tool/google_api_toolset.js +146 -0
- package/dist/esm/tools/google_api_tool/google_api_toolsets.js +94 -0
- package/dist/esm/tools/google_api_tool/googleapi_to_openapi_converter.js +393 -0
- package/dist/esm/tools/google_api_tool/index.js +31 -0
- package/dist/esm/tools/load_artifacts_tool.js +10 -5
- package/dist/esm/tools/load_memory_tool.js +12 -10
- package/dist/esm/tools/mcp/index.js +6 -0
- package/dist/esm/tools/mcp/mcp_session_manager.js +25 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -14
- package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +1 -2
- package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +50 -3
- package/dist/esm/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +4 -3
- package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +20 -6
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/operation_parser.js +17 -6
- package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -2
- package/dist/esm/tools/openapi_tool/openapi_toolset.js +6 -12
- package/dist/esm/tools/openapi_tool/rest_api_tool.js +17 -1
- package/dist/esm/tools/retrieval/base_retrieval_tool.js +16 -2
- package/dist/esm/tools/toolbox_toolset.js +148 -0
- package/dist/esm/tools/webmcp/webmcp_toolset.js +1 -14
- package/dist/esm/utils/client_labels.js +19 -2
- package/dist/esm/utils/env_aware_utils.js +6 -8
- package/dist/esm/utils/error_utils.js +9 -10
- package/dist/esm/utils/failover_utils.js +1 -1
- package/dist/esm/utils/file_extension_utils.js +11 -0
- package/dist/esm/utils/file_utils.js +2 -23
- package/dist/esm/utils/gemini_schema_util.js +27 -7
- package/dist/esm/utils/json_utils.js +21 -0
- package/dist/esm/utils/live_connection_utils.js +4 -5
- package/dist/esm/utils/logger_node.js +11 -1
- package/dist/esm/utils/object_notation_utils.js +4 -2
- package/dist/esm/utils/output_schema_utils.js +5 -2
- package/dist/esm/utils/streaming_utils.js +11 -11
- package/dist/esm/utils/task.js +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_remote_agent.d.ts +9 -0
- package/dist/types/a2a/agent_card.d.ts +44 -1
- package/dist/types/a2a/agent_to_a2a.d.ts +9 -0
- package/dist/types/a2a/index.d.ts +1 -0
- package/dist/types/agents/agent_engine_client.d.ts +37 -0
- package/dist/types/agents/base_agent.d.ts +12 -0
- package/dist/types/agents/functions.d.ts +33 -1
- package/dist/types/agents/invocation_context.d.ts +20 -1
- package/dist/types/agents/langgraph_agent.d.ts +153 -0
- package/dist/types/agents/llm_agent.d.ts +5 -1
- package/dist/types/agents/processors/content_processor_utils.d.ts +4 -2
- package/dist/types/agents/processors/nl_planning_processor.d.ts +2 -1
- package/dist/types/auth/auth_handler.d.ts +28 -1
- package/dist/types/auth/auth_schemes.d.ts +13 -0
- package/dist/types/cli/cli_eval.d.ts +173 -0
- package/dist/types/cli/utils/envs.d.ts +23 -0
- package/dist/types/cli/utils/evals.d.ts +47 -0
- package/dist/types/cli/utils/logs.d.ts +38 -0
- package/dist/types/code_executors/code_execution_utils.d.ts +39 -0
- package/dist/types/code_executors/code_executor_context.d.ts +1 -0
- package/dist/types/code_executors/container_code_executor.d.ts +17 -5
- package/dist/types/common.d.ts +24 -5
- package/dist/types/errors/input_validation_error.d.ts +2 -1
- package/dist/types/events/event.d.ts +4 -0
- package/dist/types/examples/example_util.d.ts +17 -0
- package/dist/types/flows/llm_flows/audio_transcriber.d.ts +85 -0
- package/dist/types/index.d.ts +19 -2
- package/dist/types/models/gemini_llm_connection.d.ts +6 -7
- package/dist/types/models/routed_llm.d.ts +1 -1
- package/dist/types/plugins/base_plugin.d.ts +0 -10
- package/dist/types/runner/in_memory_runner.d.ts +2 -1
- package/dist/types/runner/runner.d.ts +34 -0
- package/dist/types/sessions/base_session_service.d.ts +5 -0
- package/dist/types/sessions/database_session_service.d.ts +17 -1
- package/dist/types/sessions/stale_session_error.d.ts +21 -0
- package/dist/types/sessions/state.d.ts +5 -1
- package/dist/types/sessions/vertex_ai_session_service.d.ts +8 -0
- package/dist/types/skills/loader.d.ts +3 -2
- package/dist/types/skills/skill.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +20 -4
- package/dist/types/tools/apihub_tool/apihub_toolset.d.ts +115 -0
- package/dist/types/tools/apihub_tool/clients/apihub_client.d.ts +142 -0
- package/dist/types/tools/apihub_tool/clients/secret_client.d.ts +55 -0
- package/dist/types/tools/apihub_tool/clients/service_account.d.ts +11 -0
- package/dist/types/tools/application_integration_tool/application_integration_toolset.d.ts +109 -0
- package/dist/types/tools/application_integration_tool/clients/cloud_request.d.ts +53 -0
- package/dist/types/tools/application_integration_tool/clients/connections_client.d.ts +229 -0
- package/dist/types/tools/application_integration_tool/clients/integration_client.d.ts +72 -0
- package/dist/types/tools/application_integration_tool/integration_connector_tool.d.ts +70 -0
- package/dist/types/tools/base_toolset.d.ts +6 -5
- package/dist/types/tools/example_tool.d.ts +10 -0
- package/dist/types/tools/function_tool.d.ts +32 -0
- package/dist/types/tools/google_api_tool/google_api_tool.d.ts +37 -0
- package/dist/types/tools/google_api_tool/google_api_toolset.d.ts +55 -0
- package/dist/types/tools/google_api_tool/google_api_toolsets.d.ts +48 -0
- package/dist/types/tools/google_api_tool/googleapi_to_openapi_converter.d.ts +165 -0
- package/dist/types/tools/google_api_tool/index.d.ts +10 -0
- package/dist/types/tools/load_artifacts_tool.d.ts +14 -1
- package/dist/types/tools/load_memory_tool.d.ts +23 -1
- package/dist/types/tools/mcp/index.d.ts +3 -0
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +34 -1
- package/dist/types/tools/mcp/mcp_toolset.d.ts +15 -0
- package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +19 -0
- package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +18 -1
- package/dist/types/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.d.ts +12 -0
- package/dist/types/tools/openapi_tool/openapi_spec_parser/operation_parser.d.ts +8 -0
- package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +2 -1
- package/dist/types/tools/openapi_tool/openapi_toolset.d.ts +5 -0
- package/dist/types/tools/openapi_tool/rest_api_tool.d.ts +10 -0
- package/dist/types/tools/retrieval/base_retrieval_tool.d.ts +15 -0
- package/dist/types/tools/toolbox_toolset.d.ts +159 -0
- package/dist/types/utils/client_labels.d.ts +2 -1
- package/dist/types/utils/env_aware_utils.d.ts +18 -2
- package/dist/types/utils/error_utils.d.ts +7 -0
- package/dist/types/utils/file_utils.d.ts +6 -1
- package/dist/types/utils/gemini_schema_util.d.ts +9 -2
- package/dist/types/utils/json_utils.d.ts +11 -0
- package/dist/types/utils/object_notation_utils.d.ts +7 -0
- package/dist/types/utils/output_schema_utils.d.ts +3 -2
- package/dist/types/utils/simple_zod_to_json.d.ts +1 -2
- package/dist/types/utils/streaming_utils.d.ts +11 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/index_web.js +117 -110
- package/dist/web/index_web.js.map +4 -4
- package/package.json +38 -26
- package/dist/cjs/models/gemini_continuation.js +0 -232
- package/dist/esm/models/gemini_continuation.js +0 -205
- package/dist/types/models/gemini_continuation.d.ts +0 -59
|
@@ -21,7 +21,10 @@ import {
|
|
|
21
21
|
mergeEventActions
|
|
22
22
|
} from "../events/event_actions.js";
|
|
23
23
|
import { BaseTool } from "../tools/base_tool.js";
|
|
24
|
+
import { isFunctionTool } from "../tools/function_tool.js";
|
|
24
25
|
import { logger } from "../utils/logger.js";
|
|
26
|
+
import { Task } from "../utils/task.js";
|
|
27
|
+
import { ActiveStreamingTool } from "./active_streaming_tool.js";
|
|
25
28
|
import { Context } from "./context.js";
|
|
26
29
|
import {
|
|
27
30
|
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
|
|
@@ -50,6 +53,8 @@ import {
|
|
|
50
53
|
} from "./framework_function_calls.js";
|
|
51
54
|
const functionsExportedForTestingOnly = {
|
|
52
55
|
handleFunctionCallList,
|
|
56
|
+
handleFunctionCallsLive,
|
|
57
|
+
processFunctionLiveHelper,
|
|
53
58
|
generateAuthEvent,
|
|
54
59
|
generateRequestConfirmationEvent
|
|
55
60
|
};
|
|
@@ -147,20 +152,49 @@ async function callToolAsync(tool, args, toolContext) {
|
|
|
147
152
|
traceToolCall({
|
|
148
153
|
tool,
|
|
149
154
|
args,
|
|
155
|
+
span,
|
|
150
156
|
functionResponseEvent: buildResponseEvent(
|
|
151
157
|
tool,
|
|
152
158
|
result,
|
|
153
159
|
toolContext,
|
|
154
160
|
toolContext.invocationContext
|
|
155
|
-
)
|
|
161
|
+
),
|
|
162
|
+
errorType: getToolResponseErrorType(result),
|
|
163
|
+
invocationContext: toolContext.invocationContext
|
|
156
164
|
});
|
|
157
165
|
return result;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
traceToolCall({
|
|
168
|
+
tool,
|
|
169
|
+
args,
|
|
170
|
+
span,
|
|
171
|
+
error,
|
|
172
|
+
invocationContext: toolContext.invocationContext
|
|
173
|
+
});
|
|
174
|
+
throw error;
|
|
158
175
|
} finally {
|
|
159
176
|
span.end();
|
|
160
177
|
}
|
|
161
178
|
});
|
|
162
179
|
}
|
|
163
180
|
__name(callToolAsync, "callToolAsync");
|
|
181
|
+
function getToolResponseErrorType(result) {
|
|
182
|
+
var _a2;
|
|
183
|
+
if (result === null || typeof result !== "object" || Array.isArray(result)) {
|
|
184
|
+
return void 0;
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
if (!Object.hasOwn(result, "error")) {
|
|
188
|
+
return void 0;
|
|
189
|
+
}
|
|
190
|
+
const response = result;
|
|
191
|
+
const errorType = (_a2 = response.errorType) != null ? _a2 : response.error_type;
|
|
192
|
+
return typeof errorType === "string" ? errorType : void 0;
|
|
193
|
+
} catch {
|
|
194
|
+
return void 0;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
__name(getToolResponseErrorType, "getToolResponseErrorType");
|
|
164
198
|
function buildResponseEvent(tool, functionResult, toolContext, invocationContext) {
|
|
165
199
|
let responseResult;
|
|
166
200
|
if (typeof functionResult !== "object" || functionResult == null) {
|
|
@@ -213,6 +247,30 @@ async function handleFunctionCallsAsync({
|
|
|
213
247
|
});
|
|
214
248
|
}
|
|
215
249
|
__name(handleFunctionCallsAsync, "handleFunctionCallsAsync");
|
|
250
|
+
async function handleFunctionCallsLive({
|
|
251
|
+
invocationContext,
|
|
252
|
+
functionCallEvent,
|
|
253
|
+
toolsDict,
|
|
254
|
+
beforeToolCallbacks,
|
|
255
|
+
afterToolCallbacks,
|
|
256
|
+
filters,
|
|
257
|
+
toolConfirmationDict,
|
|
258
|
+
resumeInputsDict
|
|
259
|
+
}) {
|
|
260
|
+
const functionCalls = getFunctionCalls(functionCallEvent);
|
|
261
|
+
return handleFunctionCallList({
|
|
262
|
+
invocationContext,
|
|
263
|
+
functionCalls,
|
|
264
|
+
toolsDict,
|
|
265
|
+
beforeToolCallbacks,
|
|
266
|
+
afterToolCallbacks,
|
|
267
|
+
filters,
|
|
268
|
+
toolConfirmationDict,
|
|
269
|
+
resumeInputsDict,
|
|
270
|
+
isLive: true
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
__name(handleFunctionCallsLive, "handleFunctionCallsLive");
|
|
216
274
|
function normalizeCallbackResponse(response) {
|
|
217
275
|
if (response == null) {
|
|
218
276
|
return void 0;
|
|
@@ -298,7 +356,8 @@ async function handleFunctionCallList({
|
|
|
298
356
|
afterToolCallbacks,
|
|
299
357
|
filters,
|
|
300
358
|
toolConfirmationDict,
|
|
301
|
-
resumeInputsDict
|
|
359
|
+
resumeInputsDict,
|
|
360
|
+
isLive = false
|
|
302
361
|
}) {
|
|
303
362
|
var _a2;
|
|
304
363
|
const functionResponseEvents = [];
|
|
@@ -335,7 +394,6 @@ async function handleFunctionCallList({
|
|
|
335
394
|
logger.debug(`execute_tool ${tool.name}`);
|
|
336
395
|
const functionArgs = (_a2 = functionCall.args) != null ? _a2 : {};
|
|
337
396
|
let functionResponse = null;
|
|
338
|
-
let functionResponseError;
|
|
339
397
|
functionResponse = await invocationContext.pluginManager.runBeforeToolCallback({
|
|
340
398
|
tool,
|
|
341
399
|
toolArgs: functionArgs,
|
|
@@ -356,7 +414,18 @@ async function handleFunctionCallList({
|
|
|
356
414
|
functionResponse = normalizeCallbackResponse(functionResponse);
|
|
357
415
|
if (functionResponse == null) {
|
|
358
416
|
try {
|
|
359
|
-
|
|
417
|
+
const rawFunctionResponse = isLive ? await processFunctionLiveHelper({
|
|
418
|
+
tool,
|
|
419
|
+
toolContext,
|
|
420
|
+
functionCall,
|
|
421
|
+
functionArgs,
|
|
422
|
+
invocationContext
|
|
423
|
+
}) : await callToolAsync(tool, functionArgs, toolContext);
|
|
424
|
+
if (rawFunctionResponse == null && !tool.isLongRunning) {
|
|
425
|
+
functionResponse = { result: rawFunctionResponse };
|
|
426
|
+
} else {
|
|
427
|
+
functionResponse = normalizeCallbackResponse(rawFunctionResponse);
|
|
428
|
+
}
|
|
360
429
|
} catch (e) {
|
|
361
430
|
if (e instanceof Error) {
|
|
362
431
|
const onToolErrorResponse = await invocationContext.pluginManager.runOnToolErrorCallback({
|
|
@@ -368,18 +437,19 @@ async function handleFunctionCallList({
|
|
|
368
437
|
if (onToolErrorResponse != null) {
|
|
369
438
|
functionResponse = normalizeCallbackResponse(onToolErrorResponse);
|
|
370
439
|
} else {
|
|
371
|
-
|
|
440
|
+
functionResponse = { error: e.message };
|
|
372
441
|
}
|
|
373
442
|
} else {
|
|
374
|
-
|
|
443
|
+
functionResponse = { error: e };
|
|
375
444
|
}
|
|
376
445
|
}
|
|
377
446
|
}
|
|
447
|
+
const callbackResult = functionResponse != null ? functionResponse : {};
|
|
378
448
|
let alteredFunctionResponse = await invocationContext.pluginManager.runAfterToolCallback({
|
|
379
449
|
tool,
|
|
380
450
|
toolArgs: functionArgs,
|
|
381
451
|
toolContext,
|
|
382
|
-
result:
|
|
452
|
+
result: callbackResult
|
|
383
453
|
});
|
|
384
454
|
if (alteredFunctionResponse == null) {
|
|
385
455
|
for (const callback of afterToolCallbacks) {
|
|
@@ -387,7 +457,7 @@ async function handleFunctionCallList({
|
|
|
387
457
|
tool,
|
|
388
458
|
args: functionArgs,
|
|
389
459
|
context: toolContext,
|
|
390
|
-
response:
|
|
460
|
+
response: callbackResult
|
|
391
461
|
});
|
|
392
462
|
if (alteredFunctionResponse) {
|
|
393
463
|
break;
|
|
@@ -410,13 +480,6 @@ async function handleFunctionCallList({
|
|
|
410
480
|
}
|
|
411
481
|
continue;
|
|
412
482
|
}
|
|
413
|
-
if (functionResponseError) {
|
|
414
|
-
functionResponse = { error: functionResponseError };
|
|
415
|
-
} else if (functionResponse == null) {
|
|
416
|
-
functionResponse = { result: functionResponse };
|
|
417
|
-
} else {
|
|
418
|
-
functionResponse = normalizeCallbackResponse(functionResponse);
|
|
419
|
-
}
|
|
420
483
|
const functionResponseEvent = createEvent({
|
|
421
484
|
invocationId: invocationContext.invocationId,
|
|
422
485
|
author: toolEventAuthor(invocationContext),
|
|
@@ -430,11 +493,6 @@ async function handleFunctionCallList({
|
|
|
430
493
|
actions: toolContext.actions,
|
|
431
494
|
branch: invocationContext.branch
|
|
432
495
|
});
|
|
433
|
-
logger.debug("traceToolCall", {
|
|
434
|
-
tool: tool.name,
|
|
435
|
-
args: functionArgs,
|
|
436
|
-
functionResponseEvent: functionResponseEvent.id
|
|
437
|
-
});
|
|
438
496
|
functionResponseEvents.push(functionResponseEvent);
|
|
439
497
|
}
|
|
440
498
|
if (!functionResponseEvents.length) {
|
|
@@ -447,13 +505,10 @@ async function handleFunctionCallList({
|
|
|
447
505
|
tracer.startActiveSpan("execute_tool (merged)", (span) => {
|
|
448
506
|
try {
|
|
449
507
|
logger.debug("execute_tool (merged)");
|
|
450
|
-
logger.debug("traceMergedToolCalls", {
|
|
451
|
-
responseEventId: mergedEvent.id,
|
|
452
|
-
functionResponseEvent: mergedEvent.id
|
|
453
|
-
});
|
|
454
508
|
traceMergedToolCalls({
|
|
455
509
|
responseEventId: mergedEvent.id,
|
|
456
|
-
functionResponseEvent: mergedEvent
|
|
510
|
+
functionResponseEvent: mergedEvent,
|
|
511
|
+
invocationContext
|
|
457
512
|
});
|
|
458
513
|
} finally {
|
|
459
514
|
span.end();
|
|
@@ -491,6 +546,151 @@ function mergeParallelFunctionResponseEvents(functionResponseEvents) {
|
|
|
491
546
|
});
|
|
492
547
|
}
|
|
493
548
|
__name(mergeParallelFunctionResponseEvents, "mergeParallelFunctionResponseEvents");
|
|
549
|
+
function isAsyncGeneratorFunction(fn) {
|
|
550
|
+
return typeof fn === "function" && Object.prototype.toString.call(fn) === "[object AsyncGeneratorFunction]";
|
|
551
|
+
}
|
|
552
|
+
__name(isAsyncGeneratorFunction, "isAsyncGeneratorFunction");
|
|
553
|
+
function isStreamingTool(tool) {
|
|
554
|
+
const candidate = tool;
|
|
555
|
+
if (typeof candidate._callLive !== "function") {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
if (isFunctionTool(tool)) {
|
|
559
|
+
return isAsyncGeneratorFunction(candidate.execute);
|
|
560
|
+
}
|
|
561
|
+
return isAsyncGeneratorFunction(candidate.execute) || isAsyncGeneratorFunction(candidate.func) || isAsyncGeneratorFunction(candidate._callLive);
|
|
562
|
+
}
|
|
563
|
+
__name(isStreamingTool, "isStreamingTool");
|
|
564
|
+
async function* callToolLive({
|
|
565
|
+
tool,
|
|
566
|
+
args,
|
|
567
|
+
toolContext,
|
|
568
|
+
invocationContext
|
|
569
|
+
}) {
|
|
570
|
+
const results = tool._callLive({
|
|
571
|
+
args,
|
|
572
|
+
toolContext,
|
|
573
|
+
invocationContext
|
|
574
|
+
});
|
|
575
|
+
for await (const result of results) {
|
|
576
|
+
yield result;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
__name(callToolLive, "callToolLive");
|
|
580
|
+
async function waitForTaskWithTimeout(task, timeoutMs) {
|
|
581
|
+
let timeoutId;
|
|
582
|
+
const timeoutPromise = new Promise((resolve) => {
|
|
583
|
+
timeoutId = setTimeout(resolve, timeoutMs);
|
|
584
|
+
});
|
|
585
|
+
try {
|
|
586
|
+
await Promise.race([
|
|
587
|
+
task.promise.then(
|
|
588
|
+
() => void 0,
|
|
589
|
+
() => void 0
|
|
590
|
+
),
|
|
591
|
+
timeoutPromise
|
|
592
|
+
]);
|
|
593
|
+
} finally {
|
|
594
|
+
if (timeoutId !== void 0) {
|
|
595
|
+
clearTimeout(timeoutId);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
__name(waitForTaskWithTimeout, "waitForTaskWithTimeout");
|
|
600
|
+
const STOP_STREAMING_FUNCTION_NAME = "stop_streaming";
|
|
601
|
+
const STOP_STREAMING_TIMEOUT_MS = 1e3;
|
|
602
|
+
async function processFunctionLiveHelper({
|
|
603
|
+
tool,
|
|
604
|
+
toolContext,
|
|
605
|
+
functionCall,
|
|
606
|
+
functionArgs,
|
|
607
|
+
invocationContext
|
|
608
|
+
}) {
|
|
609
|
+
var _a2, _b2;
|
|
610
|
+
let functionResponse = void 0;
|
|
611
|
+
const targetFunctionName = typeof functionArgs["function_name"] === "string" ? functionArgs["function_name"] : typeof functionArgs["functionName"] === "string" ? functionArgs["functionName"] : void 0;
|
|
612
|
+
if (functionCall.name === STOP_STREAMING_FUNCTION_NAME && targetFunctionName !== void 0) {
|
|
613
|
+
const activeTasks = invocationContext.activeStreamingTools;
|
|
614
|
+
const activeTask = (_a2 = activeTasks == null ? void 0 : activeTasks[targetFunctionName]) == null ? void 0 : _a2.task;
|
|
615
|
+
if (activeTasks && activeTask && !activeTask.done()) {
|
|
616
|
+
activeTask.cancel();
|
|
617
|
+
await waitForTaskWithTimeout(activeTask, STOP_STREAMING_TIMEOUT_MS);
|
|
618
|
+
if (!activeTask.done()) {
|
|
619
|
+
logger.warn(`The task for ${targetFunctionName} is not cancelled yet.`);
|
|
620
|
+
functionResponse = {
|
|
621
|
+
status: `The task is not cancelled yet for ${targetFunctionName}.`
|
|
622
|
+
};
|
|
623
|
+
} else {
|
|
624
|
+
activeTasks[targetFunctionName].task = void 0;
|
|
625
|
+
functionResponse = {
|
|
626
|
+
status: `Successfully stopped streaming function ${targetFunctionName}`
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
} else {
|
|
630
|
+
functionResponse = {
|
|
631
|
+
status: `No active streaming function named ${targetFunctionName} found`
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
} else if (isStreamingTool(tool)) {
|
|
635
|
+
const task = new Task(async (abortSignal) => {
|
|
636
|
+
var _a3, _b3;
|
|
637
|
+
const generator = callToolLive({
|
|
638
|
+
tool,
|
|
639
|
+
args: functionArgs,
|
|
640
|
+
toolContext,
|
|
641
|
+
invocationContext
|
|
642
|
+
});
|
|
643
|
+
let abortCleanup;
|
|
644
|
+
const abortPromise = new Promise(
|
|
645
|
+
(resolve) => {
|
|
646
|
+
if (abortSignal.aborted) {
|
|
647
|
+
resolve({ value: void 0, done: true });
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const onAbort = /* @__PURE__ */ __name(() => {
|
|
651
|
+
resolve({ value: void 0, done: true });
|
|
652
|
+
}, "onAbort");
|
|
653
|
+
abortSignal.addEventListener("abort", onAbort, { once: true });
|
|
654
|
+
abortCleanup = /* @__PURE__ */ __name(() => abortSignal.removeEventListener("abort", onAbort), "abortCleanup");
|
|
655
|
+
}
|
|
656
|
+
);
|
|
657
|
+
try {
|
|
658
|
+
while (!abortSignal.aborted) {
|
|
659
|
+
const nextResult = await Promise.race([
|
|
660
|
+
generator.next(),
|
|
661
|
+
abortPromise
|
|
662
|
+
]);
|
|
663
|
+
if (abortSignal.aborted || nextResult.done) {
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
const result = nextResult.value;
|
|
667
|
+
const updatedContent = createUserContent(
|
|
668
|
+
`Function ${tool.name} returned: ${String(result)}`
|
|
669
|
+
);
|
|
670
|
+
(_a3 = invocationContext.liveRequestQueue) == null ? void 0 : _a3.sendContent(updatedContent);
|
|
671
|
+
}
|
|
672
|
+
} finally {
|
|
673
|
+
abortCleanup == null ? void 0 : abortCleanup();
|
|
674
|
+
if (abortSignal.aborted) {
|
|
675
|
+
void ((_b3 = generator.return) == null ? void 0 : _b3.call(generator, void 0));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
(_b2 = invocationContext.activeStreamingTools) != null ? _b2 : invocationContext.activeStreamingTools = {};
|
|
680
|
+
if (tool.name in invocationContext.activeStreamingTools) {
|
|
681
|
+
invocationContext.activeStreamingTools[tool.name].task = task;
|
|
682
|
+
} else {
|
|
683
|
+
invocationContext.activeStreamingTools[tool.name] = new ActiveStreamingTool({ task });
|
|
684
|
+
}
|
|
685
|
+
functionResponse = {
|
|
686
|
+
status: "The function is running asynchronously and the results are pending."
|
|
687
|
+
};
|
|
688
|
+
} else {
|
|
689
|
+
functionResponse = await callToolAsync(tool, functionArgs, toolContext);
|
|
690
|
+
}
|
|
691
|
+
return functionResponse;
|
|
692
|
+
}
|
|
693
|
+
__name(processFunctionLiveHelper, "processFunctionLiveHelper");
|
|
494
694
|
function findEventByFunctionCallId(events, functionCallId, endIndex = events.length) {
|
|
495
695
|
for (let i = endIndex - 1; i >= 0; i--) {
|
|
496
696
|
const event = events[i];
|
|
@@ -559,6 +759,7 @@ export {
|
|
|
559
759
|
getLongRunningFunctionCalls,
|
|
560
760
|
handleFunctionCallList,
|
|
561
761
|
handleFunctionCallsAsync,
|
|
762
|
+
handleFunctionCallsLive,
|
|
562
763
|
isToolNotFound,
|
|
563
764
|
mergeParallelFunctionResponseEvents,
|
|
564
765
|
populateClientFunctionCallId,
|
|
@@ -8,6 +8,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
8
8
|
* SPDX-License-Identifier: Apache-2.0
|
|
9
9
|
*/
|
|
10
10
|
import { State } from "../sessions/state.js";
|
|
11
|
+
import { asSafePartForLlm } from "../tools/load_artifacts_tool.js";
|
|
11
12
|
const ARTIFACT_PREFIX = "artifact.";
|
|
12
13
|
const WORKFLOW_FIELD_KEY = /^[A-Za-z_]\w*\.[A-Za-z_]\w*$/;
|
|
13
14
|
const SOURCE_NODE_PLACEHOLDER = /<\s*[A-Za-z_]\w*\.([A-Za-z_]\w*)\s+from\s+([A-Za-z_]\w*)\s*>/g;
|
|
@@ -36,7 +37,7 @@ async function resolveKey(key, isOptional, rawMatch, readonlyContext) {
|
|
|
36
37
|
}
|
|
37
38
|
throw new Error(`Artifact ${fileName} not found.`);
|
|
38
39
|
}
|
|
39
|
-
return formatValue(artifact, true);
|
|
40
|
+
return formatValue(asSafePartForLlm(artifact, fileName), true);
|
|
40
41
|
}
|
|
41
42
|
if (isValidStateName(key)) {
|
|
42
43
|
if (key in invocationContext.session.state) {
|
|
@@ -91,7 +92,10 @@ async function injectSessionState(template, readonlyContext) {
|
|
|
91
92
|
resolved: resolveSourceNode(match[0], match[1], match[2], scope)
|
|
92
93
|
})) : [];
|
|
93
94
|
const pattern = /\{+[^{}]*}+/g;
|
|
94
|
-
const matches = Array.from(template.matchAll(pattern))
|
|
95
|
+
const matches = Array.from(template.matchAll(pattern)).filter((match) => {
|
|
96
|
+
const previous = template[match.index - 1];
|
|
97
|
+
return previous !== "$" && previous !== "\\";
|
|
98
|
+
});
|
|
95
99
|
if (matches.length === 0 && sourceMatches.length === 0) {
|
|
96
100
|
return template;
|
|
97
101
|
}
|
|
@@ -7,7 +7,25 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
|
|
|
7
7
|
* Copyright 2025 Google LLC
|
|
8
8
|
* SPDX-License-Identifier: Apache-2.0
|
|
9
9
|
*/
|
|
10
|
+
var _a, _b;
|
|
10
11
|
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
12
|
+
const LLM_CALLS_LIMIT_EXCEEDED_ERROR_SIGNATURE_SYMBOL = /* @__PURE__ */ Symbol.for(
|
|
13
|
+
"google.adk.llmCallsLimitExceededError"
|
|
14
|
+
);
|
|
15
|
+
const _LlmCallsLimitExceededError = class _LlmCallsLimitExceededError extends (_b = Error, _a = LLM_CALLS_LIMIT_EXCEEDED_ERROR_SIGNATURE_SYMBOL, _b) {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this[_a] = true;
|
|
19
|
+
this.name = "LlmCallsLimitExceededError";
|
|
20
|
+
Object.setPrototypeOf(this, _LlmCallsLimitExceededError.prototype);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
__name(_LlmCallsLimitExceededError, "LlmCallsLimitExceededError");
|
|
24
|
+
let LlmCallsLimitExceededError = _LlmCallsLimitExceededError;
|
|
25
|
+
function isLlmCallsLimitExceededError(obj) {
|
|
26
|
+
return typeof obj === "object" && obj !== null && LLM_CALLS_LIMIT_EXCEEDED_ERROR_SIGNATURE_SYMBOL in obj && obj[LLM_CALLS_LIMIT_EXCEEDED_ERROR_SIGNATURE_SYMBOL] === true;
|
|
27
|
+
}
|
|
28
|
+
__name(isLlmCallsLimitExceededError, "isLlmCallsLimitExceededError");
|
|
11
29
|
const _InvocationCostManager = class _InvocationCostManager {
|
|
12
30
|
constructor() {
|
|
13
31
|
this.numberOfLlmCalls = 0;
|
|
@@ -21,7 +39,7 @@ const _InvocationCostManager = class _InvocationCostManager {
|
|
|
21
39
|
incrementAndEnforceLlmCallsLimit(runConfig) {
|
|
22
40
|
this.numberOfLlmCalls++;
|
|
23
41
|
if (runConfig && runConfig.maxLlmCalls > 0 && this.numberOfLlmCalls > runConfig.maxLlmCalls) {
|
|
24
|
-
throw new
|
|
42
|
+
throw new LlmCallsLimitExceededError(
|
|
25
43
|
`Max number of llm calls limit of ${runConfig.maxLlmCalls} exceeded`
|
|
26
44
|
);
|
|
27
45
|
}
|
|
@@ -34,7 +52,7 @@ const _InvocationContext = class _InvocationContext {
|
|
|
34
52
|
* @param params The parameters for creating an invocation context.
|
|
35
53
|
*/
|
|
36
54
|
constructor(params) {
|
|
37
|
-
var
|
|
55
|
+
var _a2, _b2;
|
|
38
56
|
this.artifactService = params.artifactService;
|
|
39
57
|
this.sessionService = params.sessionService;
|
|
40
58
|
this.memoryService = params.memoryService;
|
|
@@ -51,9 +69,9 @@ const _InvocationContext = class _InvocationContext {
|
|
|
51
69
|
this.abortSignal = params.abortSignal;
|
|
52
70
|
this.workflowInstructionScope = params.workflowInstructionScope;
|
|
53
71
|
this.isolationScope = params.isolationScope;
|
|
54
|
-
this.nodeToolDepth = (
|
|
72
|
+
this.nodeToolDepth = (_a2 = params.nodeToolDepth) != null ? _a2 : 0;
|
|
55
73
|
this.a2aMetadata = params.a2aMetadata;
|
|
56
|
-
this.invocationCostManager = (
|
|
74
|
+
this.invocationCostManager = (_b2 = params.invocationCostManager) != null ? _b2 : new InvocationCostManager();
|
|
57
75
|
this.liveRequestQueue = params.liveRequestQueue;
|
|
58
76
|
this.liveSessionResumptionHandle = params.liveSessionResumptionHandle;
|
|
59
77
|
}
|
|
@@ -107,6 +125,8 @@ function requireAgent(ctx) {
|
|
|
107
125
|
__name(requireAgent, "requireAgent");
|
|
108
126
|
export {
|
|
109
127
|
InvocationContext,
|
|
128
|
+
LlmCallsLimitExceededError,
|
|
129
|
+
isLlmCallsLimitExceededError,
|
|
110
130
|
newInvocationContextId,
|
|
111
131
|
requireAgent
|
|
112
132
|
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {createRequire as topLevelCreateRequire} from 'module';
|
|
2
|
+
const require = topLevelCreateRequire(import.meta.url);
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
/**
|
|
6
|
+
* @license
|
|
7
|
+
* Copyright 2026 Google LLC
|
|
8
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
*/
|
|
10
|
+
var _a, _b;
|
|
11
|
+
import { createEvent } from "../events/event.js";
|
|
12
|
+
import { BaseAgent } from "./base_agent.js";
|
|
13
|
+
function getLastHumanMessages(events) {
|
|
14
|
+
var _a2, _b2, _c;
|
|
15
|
+
const messages = [];
|
|
16
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
17
|
+
const event = events[i];
|
|
18
|
+
if (messages.length > 0 && event.author !== "user") {
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
const text = (_c = (_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2[0]) == null ? void 0 : _c.text;
|
|
22
|
+
if (event.author === "user" && text !== void 0) {
|
|
23
|
+
messages.push({ type: "human", content: text });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return messages.reverse();
|
|
27
|
+
}
|
|
28
|
+
__name(getLastHumanMessages, "getLastHumanMessages");
|
|
29
|
+
function extractMessageText(content) {
|
|
30
|
+
if (typeof content === "string") {
|
|
31
|
+
return content;
|
|
32
|
+
}
|
|
33
|
+
return content.map((part) => {
|
|
34
|
+
var _a2;
|
|
35
|
+
return typeof part === "string" ? part : (_a2 = part.text) != null ? _a2 : "";
|
|
36
|
+
}).join("");
|
|
37
|
+
}
|
|
38
|
+
__name(extractMessageText, "extractMessageText");
|
|
39
|
+
const LANGGRAPH_AGENT_SIGNATURE_SYMBOL = /* @__PURE__ */ Symbol.for(
|
|
40
|
+
"google.adk.langGraphAgent"
|
|
41
|
+
);
|
|
42
|
+
function isLangGraphAgent(obj) {
|
|
43
|
+
return typeof obj === "object" && obj !== null && LANGGRAPH_AGENT_SIGNATURE_SYMBOL in obj && obj[LANGGRAPH_AGENT_SIGNATURE_SYMBOL] === true;
|
|
44
|
+
}
|
|
45
|
+
__name(isLangGraphAgent, "isLangGraphAgent");
|
|
46
|
+
const _LangGraphAgent = class _LangGraphAgent extends (_b = BaseAgent, _a = LANGGRAPH_AGENT_SIGNATURE_SYMBOL, _b) {
|
|
47
|
+
constructor(config) {
|
|
48
|
+
var _a2;
|
|
49
|
+
super(config);
|
|
50
|
+
this[_a] = true;
|
|
51
|
+
this.graph = config.graph;
|
|
52
|
+
this.instruction = (_a2 = config.instruction) != null ? _a2 : "";
|
|
53
|
+
}
|
|
54
|
+
async *runAsyncImpl(ctx) {
|
|
55
|
+
var _a2, _b2, _c;
|
|
56
|
+
const config = {
|
|
57
|
+
// LangGraph.js checkpointers read `configurable.thread_id`.
|
|
58
|
+
configurable: { thread_id: ctx.session.id }
|
|
59
|
+
};
|
|
60
|
+
let currentGraphState;
|
|
61
|
+
if (this.graph.checkpointer) {
|
|
62
|
+
if (typeof this.graph.getState !== "function") {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`LangGraphAgent '${this.name}' requires graph.getState() when graph.checkpointer is configured.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
currentGraphState = await this.graph.getState(config);
|
|
68
|
+
}
|
|
69
|
+
const graphMessages = (_b2 = (_a2 = currentGraphState == null ? void 0 : currentGraphState.values) == null ? void 0 : _a2.messages) != null ? _b2 : [];
|
|
70
|
+
const messages = this.instruction && graphMessages.length === 0 ? [{ type: "system", content: this.instruction }] : [];
|
|
71
|
+
messages.push(...this.getMessages(ctx.session.events));
|
|
72
|
+
const finalState = await this.graph.invoke({ messages }, config);
|
|
73
|
+
const finalMessages = (_c = finalState.messages) != null ? _c : [];
|
|
74
|
+
const lastMessage = finalMessages[finalMessages.length - 1];
|
|
75
|
+
if (!lastMessage) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`LangGraphAgent '${this.name}' received no messages from graph.invoke().`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
yield createEvent({
|
|
81
|
+
invocationId: ctx.invocationId,
|
|
82
|
+
author: this.name,
|
|
83
|
+
branch: ctx.branch,
|
|
84
|
+
content: {
|
|
85
|
+
role: "model",
|
|
86
|
+
parts: [{ text: extractMessageText(lastMessage.content) }]
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async *runLiveImpl(_ctx) {
|
|
91
|
+
yield* [];
|
|
92
|
+
throw new Error("This is not supported yet for LangGraphAgent.");
|
|
93
|
+
}
|
|
94
|
+
getMessages(events) {
|
|
95
|
+
if (this.graph.checkpointer) {
|
|
96
|
+
return getLastHumanMessages(events);
|
|
97
|
+
}
|
|
98
|
+
return this.getConversationWithAgent(events);
|
|
99
|
+
}
|
|
100
|
+
getConversationWithAgent(events) {
|
|
101
|
+
var _a2, _b2, _c;
|
|
102
|
+
const messages = [];
|
|
103
|
+
for (const event of events) {
|
|
104
|
+
const text = (_c = (_b2 = (_a2 = event.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2[0]) == null ? void 0 : _c.text;
|
|
105
|
+
if (text === void 0) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (event.author === "user") {
|
|
109
|
+
messages.push({ type: "human", content: text });
|
|
110
|
+
} else if (event.author === this.name) {
|
|
111
|
+
messages.push({ type: "ai", content: text });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return messages;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
__name(_LangGraphAgent, "LangGraphAgent");
|
|
118
|
+
let LangGraphAgent = _LangGraphAgent;
|
|
119
|
+
export {
|
|
120
|
+
LangGraphAgent,
|
|
121
|
+
getLastHumanMessages,
|
|
122
|
+
isLangGraphAgent
|
|
123
|
+
};
|