@google/adk 0.1.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/LICENSE +202 -0
- package/README.md +9 -0
- package/dist/cjs/agents/active_streaming_tool.js +44 -0
- package/dist/cjs/agents/base_agent.js +245 -0
- package/dist/cjs/agents/base_llm_processor.js +44 -0
- package/dist/cjs/agents/callback_context.js +98 -0
- package/dist/cjs/agents/content_processor_utils.js +299 -0
- package/dist/cjs/agents/functions.js +394 -0
- package/dist/cjs/agents/instructions.js +110 -0
- package/dist/cjs/agents/invocation_context.js +109 -0
- package/dist/cjs/agents/live_request_queue.js +136 -0
- package/dist/cjs/agents/llm_agent.js +859 -0
- package/dist/cjs/agents/loop_agent.js +68 -0
- package/dist/cjs/agents/parallel_agent.js +78 -0
- package/dist/cjs/agents/readonly_context.js +68 -0
- package/dist/cjs/agents/run_config.js +74 -0
- package/dist/cjs/agents/sequential_agent.js +84 -0
- package/dist/cjs/agents/transcription_entry.js +27 -0
- package/dist/cjs/artifacts/base_artifact_service.js +27 -0
- package/dist/cjs/artifacts/in_memory_artifact_service.js +119 -0
- package/dist/cjs/auth/auth_credential.js +46 -0
- package/dist/cjs/auth/auth_handler.js +92 -0
- package/dist/cjs/auth/auth_schemes.js +62 -0
- package/dist/cjs/auth/auth_tool.js +27 -0
- package/dist/cjs/auth/credential_service/base_credential_service.js +27 -0
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +63 -0
- package/dist/cjs/code_executors/base_code_executor.js +76 -0
- package/dist/cjs/code_executors/built_in_code_executor.js +58 -0
- package/dist/cjs/code_executors/code_execution_utils.js +142 -0
- package/dist/cjs/code_executors/code_executor_context.js +198 -0
- package/dist/cjs/common.js +161 -0
- package/dist/cjs/events/event.js +107 -0
- package/dist/cjs/events/event_actions.js +83 -0
- package/dist/cjs/examples/base_example_provider.js +40 -0
- package/dist/cjs/examples/example.js +27 -0
- package/dist/cjs/examples/example_util.js +107 -0
- package/dist/cjs/index.js +40 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/index_web.js +33 -0
- package/dist/cjs/memory/base_memory_service.js +27 -0
- package/dist/cjs/memory/in_memory_memory_service.js +97 -0
- package/dist/cjs/memory/memory_entry.js +27 -0
- package/dist/cjs/models/base_llm.js +77 -0
- package/dist/cjs/models/base_llm_connection.js +27 -0
- package/dist/cjs/models/gemini_llm_connection.js +132 -0
- package/dist/cjs/models/google_llm.js +321 -0
- package/dist/cjs/models/llm_request.js +82 -0
- package/dist/cjs/models/llm_response.js +71 -0
- package/dist/cjs/models/registry.js +121 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/plugins/base_plugin.js +236 -0
- package/dist/cjs/plugins/logging_plugin.js +222 -0
- package/dist/cjs/plugins/plugin_manager.js +239 -0
- package/dist/cjs/plugins/security_plugin.js +153 -0
- package/dist/cjs/runner/in_memory_runner.js +58 -0
- package/dist/cjs/runner/runner.js +276 -0
- package/dist/cjs/sessions/base_session_service.js +71 -0
- package/dist/cjs/sessions/in_memory_session_service.js +184 -0
- package/dist/cjs/sessions/session.js +48 -0
- package/dist/cjs/sessions/state.js +101 -0
- package/dist/cjs/tools/agent_tool.js +134 -0
- package/dist/cjs/tools/base_tool.js +107 -0
- package/dist/cjs/tools/base_toolset.js +76 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +71 -0
- package/dist/cjs/tools/function_tool.js +101 -0
- package/dist/cjs/tools/google_search_tool.js +76 -0
- package/dist/cjs/tools/long_running_tool.js +63 -0
- package/dist/cjs/tools/mcp/mcp_session_manager.js +65 -0
- package/dist/cjs/tools/mcp/mcp_tool.js +65 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +61 -0
- package/dist/cjs/tools/tool_confirmation.js +49 -0
- package/dist/cjs/tools/tool_context.js +129 -0
- package/dist/cjs/utils/deep_clone.js +44 -0
- package/dist/cjs/utils/env_aware_utils.js +83 -0
- package/dist/cjs/utils/gemini_schema_util.js +88 -0
- package/dist/cjs/utils/logger.js +121 -0
- package/dist/cjs/utils/model_name.js +64 -0
- package/dist/cjs/utils/simple_zod_to_json.js +191 -0
- package/dist/cjs/utils/variant_utils.js +55 -0
- package/dist/cjs/version.js +39 -0
- package/dist/esm/agents/active_streaming_tool.js +14 -0
- package/dist/esm/agents/base_agent.js +214 -0
- package/dist/esm/agents/base_llm_processor.js +13 -0
- package/dist/esm/agents/callback_context.js +68 -0
- package/dist/esm/agents/content_processor_utils.js +268 -0
- package/dist/esm/agents/functions.js +353 -0
- package/dist/esm/agents/instructions.js +80 -0
- package/dist/esm/agents/invocation_context.js +78 -0
- package/dist/esm/agents/live_request_queue.js +106 -0
- package/dist/esm/agents/llm_agent.js +828 -0
- package/dist/esm/agents/loop_agent.js +38 -0
- package/dist/esm/agents/parallel_agent.js +48 -0
- package/dist/esm/agents/readonly_context.js +38 -0
- package/dist/esm/agents/run_config.js +43 -0
- package/dist/esm/agents/sequential_agent.js +54 -0
- package/dist/esm/agents/transcription_entry.js +5 -0
- package/dist/esm/artifacts/base_artifact_service.js +5 -0
- package/dist/esm/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/esm/auth/auth_credential.js +16 -0
- package/dist/esm/auth/auth_handler.js +62 -0
- package/dist/esm/auth/auth_schemes.js +31 -0
- package/dist/esm/auth/auth_tool.js +5 -0
- package/dist/esm/auth/credential_service/base_credential_service.js +5 -0
- package/dist/esm/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/esm/code_executors/base_code_executor.js +46 -0
- package/dist/esm/code_executors/built_in_code_executor.js +28 -0
- package/dist/esm/code_executors/code_execution_utils.js +108 -0
- package/dist/esm/code_executors/code_executor_context.js +168 -0
- package/dist/esm/common.js +85 -0
- package/dist/esm/events/event.js +72 -0
- package/dist/esm/events/event_actions.js +52 -0
- package/dist/esm/examples/base_example_provider.js +10 -0
- package/dist/esm/examples/example.js +5 -0
- package/dist/esm/examples/example_util.js +76 -0
- package/dist/esm/index.js +40 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/index_web.js +6 -0
- package/dist/esm/memory/base_memory_service.js +5 -0
- package/dist/esm/memory/in_memory_memory_service.js +67 -0
- package/dist/esm/memory/memory_entry.js +5 -0
- package/dist/esm/models/base_llm.js +47 -0
- package/dist/esm/models/base_llm_connection.js +5 -0
- package/dist/esm/models/gemini_llm_connection.js +102 -0
- package/dist/esm/models/google_llm.js +291 -0
- package/dist/esm/models/llm_request.js +50 -0
- package/dist/esm/models/llm_response.js +41 -0
- package/dist/esm/models/registry.js +91 -0
- package/dist/esm/plugins/base_plugin.js +206 -0
- package/dist/esm/plugins/logging_plugin.js +192 -0
- package/dist/esm/plugins/plugin_manager.js +209 -0
- package/dist/esm/plugins/security_plugin.js +119 -0
- package/dist/esm/runner/in_memory_runner.js +28 -0
- package/dist/esm/runner/runner.js +246 -0
- package/dist/esm/sessions/base_session_service.js +41 -0
- package/dist/esm/sessions/in_memory_session_service.js +154 -0
- package/dist/esm/sessions/session.js +18 -0
- package/dist/esm/sessions/state.js +71 -0
- package/dist/esm/tools/agent_tool.js +104 -0
- package/dist/esm/tools/base_tool.js +77 -0
- package/dist/esm/tools/base_toolset.js +46 -0
- package/dist/esm/tools/forwarding_artifact_service.js +41 -0
- package/dist/esm/tools/function_tool.js +71 -0
- package/dist/esm/tools/google_search_tool.js +46 -0
- package/dist/esm/tools/long_running_tool.js +33 -0
- package/dist/esm/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/esm/tools/mcp/mcp_tool.js +35 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +31 -0
- package/dist/esm/tools/tool_confirmation.js +19 -0
- package/dist/esm/tools/tool_context.js +99 -0
- package/dist/esm/utils/deep_clone.js +14 -0
- package/dist/esm/utils/env_aware_utils.js +49 -0
- package/dist/esm/utils/gemini_schema_util.js +58 -0
- package/dist/esm/utils/logger.js +89 -0
- package/dist/esm/utils/model_name.js +31 -0
- package/dist/esm/utils/simple_zod_to_json.js +160 -0
- package/dist/esm/utils/variant_utils.js +24 -0
- package/dist/esm/version.js +9 -0
- package/dist/types/agents/active_streaming_tool.d.ts +29 -0
- package/dist/types/agents/base_agent.d.ts +167 -0
- package/dist/types/agents/base_llm_processor.d.ts +27 -0
- package/dist/types/agents/callback_context.d.ts +42 -0
- package/dist/types/agents/content_processor_utils.d.ts +36 -0
- package/dist/types/agents/functions.d.ts +90 -0
- package/dist/types/agents/instructions.d.ts +32 -0
- package/dist/types/agents/invocation_context.d.ts +155 -0
- package/dist/types/agents/live_request_queue.d.ts +67 -0
- package/dist/types/agents/llm_agent.d.ts +333 -0
- package/dist/types/agents/loop_agent.d.ts +31 -0
- package/dist/types/agents/parallel_agent.d.ts +21 -0
- package/dist/types/agents/readonly_context.d.ts +31 -0
- package/dist/types/agents/run_config.d.ts +76 -0
- package/dist/types/agents/sequential_agent.d.ts +26 -0
- package/dist/types/agents/transcription_entry.d.ts +17 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +127 -0
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +18 -0
- package/dist/types/auth/auth_credential.d.ts +227 -0
- package/dist/types/auth/auth_handler.d.ts +27 -0
- package/dist/types/auth/auth_schemes.d.ts +36 -0
- package/dist/types/auth/auth_tool.d.ts +51 -0
- package/dist/types/auth/credential_service/base_credential_service.d.ts +27 -0
- package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +19 -0
- package/dist/types/code_executors/base_code_executor.d.ts +60 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +13 -0
- package/dist/types/code_executors/code_execution_utils.d.ts +99 -0
- package/dist/types/code_executors/code_executor_context.d.ts +92 -0
- package/dist/types/common.d.ts +51 -0
- package/dist/types/events/event.d.ts +81 -0
- package/dist/types/events/event_actions.d.ts +74 -0
- package/dist/types/examples/base_example_provider.d.ts +20 -0
- package/dist/types/examples/example.d.ts +19 -0
- package/dist/types/examples/example_util.d.ts +13 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index_web.d.ts +6 -0
- package/dist/types/memory/base_memory_service.d.ts +47 -0
- package/dist/types/memory/in_memory_memory_service.d.ts +18 -0
- package/dist/types/memory/memory_entry.d.ts +24 -0
- package/dist/types/models/base_llm.d.ts +46 -0
- package/dist/types/models/base_llm_connection.d.ts +51 -0
- package/dist/types/models/gemini_llm_connection.d.ts +54 -0
- package/dist/types/models/google_llm.d.ts +88 -0
- package/dist/types/models/llm_request.d.ts +49 -0
- package/dist/types/models/llm_response.d.ts +79 -0
- package/dist/types/models/registry.d.ts +45 -0
- package/dist/types/plugins/base_plugin.d.ts +310 -0
- package/dist/types/plugins/logging_plugin.d.ts +104 -0
- package/dist/types/plugins/plugin_manager.d.ts +155 -0
- package/dist/types/plugins/security_plugin.d.ts +60 -0
- package/dist/types/runner/in_memory_runner.d.ts +15 -0
- package/dist/types/runner/runner.d.ts +80 -0
- package/dist/types/sessions/base_session_service.d.ts +129 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +32 -0
- package/dist/types/sessions/session.d.ts +46 -0
- package/dist/types/sessions/state.d.ts +57 -0
- package/dist/types/tools/agent_tool.d.ts +37 -0
- package/dist/types/tools/base_tool.d.ts +84 -0
- package/dist/types/tools/base_toolset.d.ts +64 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +21 -0
- package/dist/types/tools/function_tool.d.ts +48 -0
- package/dist/types/tools/google_search_tool.d.ts +18 -0
- package/dist/types/tools/long_running_tool.d.ts +18 -0
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +57 -0
- package/dist/types/tools/mcp/mcp_tool.d.ts +30 -0
- package/dist/types/tools/mcp/mcp_toolset.d.ts +39 -0
- package/dist/types/tools/tool_confirmation.d.ts +25 -0
- package/dist/types/tools/tool_context.d.ts +63 -0
- package/dist/types/utils/deep_clone.d.ts +1 -0
- package/dist/types/utils/env_aware_utils.d.ts +31 -0
- package/dist/types/utils/gemini_schema_util.d.ts +23 -0
- package/dist/types/utils/logger.d.ts +41 -0
- package/dist/types/utils/model_name.d.ts +34 -0
- package/dist/types/utils/simple_zod_to_json.d.ts +12 -0
- package/dist/types/utils/variant_utils.d.ts +24 -0
- package/dist/types/version.d.ts +6 -0
- package/dist/web/agents/active_streaming_tool.js +14 -0
- package/dist/web/agents/base_agent.js +265 -0
- package/dist/web/agents/base_llm_processor.js +13 -0
- package/dist/web/agents/callback_context.js +68 -0
- package/dist/web/agents/content_processor_utils.js +268 -0
- package/dist/web/agents/functions.js +353 -0
- package/dist/web/agents/instructions.js +80 -0
- package/dist/web/agents/invocation_context.js +78 -0
- package/dist/web/agents/live_request_queue.js +124 -0
- package/dist/web/agents/llm_agent.js +973 -0
- package/dist/web/agents/loop_agent.js +71 -0
- package/dist/web/agents/parallel_agent.js +83 -0
- package/dist/web/agents/readonly_context.js +38 -0
- package/dist/web/agents/run_config.js +43 -0
- package/dist/web/agents/sequential_agent.js +99 -0
- package/dist/web/agents/transcription_entry.js +5 -0
- package/dist/web/artifacts/base_artifact_service.js +5 -0
- package/dist/web/artifacts/in_memory_artifact_service.js +89 -0
- package/dist/web/auth/auth_credential.js +16 -0
- package/dist/web/auth/auth_handler.js +62 -0
- package/dist/web/auth/auth_schemes.js +31 -0
- package/dist/web/auth/auth_tool.js +5 -0
- package/dist/web/auth/credential_service/base_credential_service.js +5 -0
- package/dist/web/auth/credential_service/in_memory_credential_service.js +33 -0
- package/dist/web/code_executors/base_code_executor.js +46 -0
- package/dist/web/code_executors/built_in_code_executor.js +28 -0
- package/dist/web/code_executors/code_execution_utils.js +105 -0
- package/dist/web/code_executors/code_executor_context.js +168 -0
- package/dist/web/common.js +85 -0
- package/dist/web/events/event.js +90 -0
- package/dist/web/events/event_actions.js +67 -0
- package/dist/web/examples/base_example_provider.js +10 -0
- package/dist/web/examples/example.js +5 -0
- package/dist/web/examples/example_util.js +75 -0
- package/dist/web/index.js +13 -0
- package/dist/web/index.js.map +7 -0
- package/dist/web/index_web.js +6 -0
- package/dist/web/memory/base_memory_service.js +5 -0
- package/dist/web/memory/in_memory_memory_service.js +67 -0
- package/dist/web/memory/memory_entry.js +5 -0
- package/dist/web/models/base_llm.js +47 -0
- package/dist/web/models/base_llm_connection.js +5 -0
- package/dist/web/models/gemini_llm_connection.js +120 -0
- package/dist/web/models/google_llm.js +332 -0
- package/dist/web/models/llm_request.js +50 -0
- package/dist/web/models/llm_response.js +41 -0
- package/dist/web/models/registry.js +91 -0
- package/dist/web/plugins/base_plugin.js +206 -0
- package/dist/web/plugins/logging_plugin.js +192 -0
- package/dist/web/plugins/plugin_manager.js +209 -0
- package/dist/web/plugins/security_plugin.js +119 -0
- package/dist/web/runner/in_memory_runner.js +28 -0
- package/dist/web/runner/runner.js +277 -0
- package/dist/web/sessions/base_session_service.js +41 -0
- package/dist/web/sessions/in_memory_session_service.js +154 -0
- package/dist/web/sessions/session.js +18 -0
- package/dist/web/sessions/state.js +87 -0
- package/dist/web/tools/agent_tool.js +118 -0
- package/dist/web/tools/base_tool.js +77 -0
- package/dist/web/tools/base_toolset.js +46 -0
- package/dist/web/tools/forwarding_artifact_service.js +41 -0
- package/dist/web/tools/function_tool.js +71 -0
- package/dist/web/tools/google_search_tool.js +46 -0
- package/dist/web/tools/long_running_tool.js +50 -0
- package/dist/web/tools/mcp/mcp_session_manager.js +35 -0
- package/dist/web/tools/mcp/mcp_tool.js +35 -0
- package/dist/web/tools/mcp/mcp_toolset.js +31 -0
- package/dist/web/tools/tool_confirmation.js +19 -0
- package/dist/web/tools/tool_context.js +99 -0
- package/dist/web/utils/deep_clone.js +14 -0
- package/dist/web/utils/env_aware_utils.js +49 -0
- package/dist/web/utils/gemini_schema_util.js +58 -0
- package/dist/web/utils/logger.js +89 -0
- package/dist/web/utils/model_name.js +31 -0
- package/dist/web/utils/simple_zod_to_json.js +174 -0
- package/dist/web/utils/variant_utils.js +24 -0
- package/dist/web/version.js +9 -0
- package/package.json +61 -0
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { createEvent, createNewEventId, getFunctionCalls, getFunctionResponses, isFinalResponse } from "../events/event.js";
|
|
8
|
+
import { BaseLlm } from "../models/base_llm.js";
|
|
9
|
+
import { appendInstructions, setOutputSchema } from "../models/llm_request.js";
|
|
10
|
+
import { LLMRegistry } from "../models/registry.js";
|
|
11
|
+
import { BaseTool } from "../tools/base_tool.js";
|
|
12
|
+
import { FunctionTool } from "../tools/function_tool.js";
|
|
13
|
+
import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
14
|
+
import { ToolContext } from "../tools/tool_context.js";
|
|
15
|
+
import { logger } from "../utils/logger.js";
|
|
16
|
+
import { BaseAgent } from "./base_agent.js";
|
|
17
|
+
import { BaseLlmRequestProcessor } from "./base_llm_processor.js";
|
|
18
|
+
import { CallbackContext } from "./callback_context.js";
|
|
19
|
+
import { getContents, getCurrentTurnContents } from "./content_processor_utils.js";
|
|
20
|
+
import { generateAuthEvent, generateRequestConfirmationEvent, getLongRunningFunctionCalls, handleFunctionCallList, handleFunctionCallsAsync, populateClientFunctionCallId, REQUEST_CONFIRMATION_FUNCTION_CALL_NAME } from "./functions.js";
|
|
21
|
+
import { injectSessionState } from "./instructions.js";
|
|
22
|
+
import { ReadonlyContext } from "./readonly_context.js";
|
|
23
|
+
const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
|
|
24
|
+
async function convertToolUnionToTools(toolUnion, context) {
|
|
25
|
+
if (toolUnion instanceof BaseTool) {
|
|
26
|
+
return [toolUnion];
|
|
27
|
+
}
|
|
28
|
+
return await toolUnion.getTools(context);
|
|
29
|
+
}
|
|
30
|
+
class BasicLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
31
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
32
|
+
var _a;
|
|
33
|
+
const agent = invocationContext.agent;
|
|
34
|
+
if (!(agent instanceof LlmAgent)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
llmRequest.model = agent.canonicalModel.model;
|
|
38
|
+
llmRequest.config = { ...(_a = agent.generateContentConfig) != null ? _a : {} };
|
|
39
|
+
if (agent.outputSchema) {
|
|
40
|
+
setOutputSchema(llmRequest, agent.outputSchema);
|
|
41
|
+
}
|
|
42
|
+
if (invocationContext.runConfig) {
|
|
43
|
+
llmRequest.liveConnectConfig.responseModalities = invocationContext.runConfig.responseModalities;
|
|
44
|
+
llmRequest.liveConnectConfig.speechConfig = invocationContext.runConfig.speechConfig;
|
|
45
|
+
llmRequest.liveConnectConfig.outputAudioTranscription = invocationContext.runConfig.outputAudioTranscription;
|
|
46
|
+
llmRequest.liveConnectConfig.inputAudioTranscription = invocationContext.runConfig.inputAudioTranscription;
|
|
47
|
+
llmRequest.liveConnectConfig.realtimeInputConfig = invocationContext.runConfig.realtimeInputConfig;
|
|
48
|
+
llmRequest.liveConnectConfig.enableAffectiveDialog = invocationContext.runConfig.enableAffectiveDialog;
|
|
49
|
+
llmRequest.liveConnectConfig.proactivity = invocationContext.runConfig.proactivity;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const BASIC_LLM_REQUEST_PROCESSOR = new BasicLlmRequestProcessor();
|
|
54
|
+
class IdentityLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
55
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
56
|
+
const agent = invocationContext.agent;
|
|
57
|
+
const si = [`You are an agent. Your internal name is "${agent.name}".`];
|
|
58
|
+
if (agent.description) {
|
|
59
|
+
si.push(`The description about you is "${agent.description}"`);
|
|
60
|
+
}
|
|
61
|
+
appendInstructions(llmRequest, si);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const IDENTITY_LLM_REQUEST_PROCESSOR = new IdentityLlmRequestProcessor();
|
|
65
|
+
class InstructionsLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
66
|
+
/**
|
|
67
|
+
* Handles instructions and global instructions for LLM flow.
|
|
68
|
+
*/
|
|
69
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
70
|
+
const agent = invocationContext.agent;
|
|
71
|
+
if (!(agent instanceof LlmAgent) || !(agent.rootAgent instanceof LlmAgent)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const rootAgent = agent.rootAgent;
|
|
75
|
+
if (rootAgent instanceof LlmAgent && rootAgent.globalInstruction) {
|
|
76
|
+
const { instruction, requireStateInjection } = await rootAgent.canonicalGlobalInstruction(
|
|
77
|
+
new ReadonlyContext(invocationContext)
|
|
78
|
+
);
|
|
79
|
+
let instructionWithState = instruction;
|
|
80
|
+
if (requireStateInjection) {
|
|
81
|
+
instructionWithState = await injectSessionState(
|
|
82
|
+
instruction,
|
|
83
|
+
new ReadonlyContext(invocationContext)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
appendInstructions(llmRequest, [instructionWithState]);
|
|
87
|
+
}
|
|
88
|
+
if (agent.instruction) {
|
|
89
|
+
const { instruction, requireStateInjection } = await agent.canonicalInstruction(
|
|
90
|
+
new ReadonlyContext(invocationContext)
|
|
91
|
+
);
|
|
92
|
+
let instructionWithState = instruction;
|
|
93
|
+
if (requireStateInjection) {
|
|
94
|
+
instructionWithState = await injectSessionState(
|
|
95
|
+
instruction,
|
|
96
|
+
new ReadonlyContext(invocationContext)
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
appendInstructions(llmRequest, [instructionWithState]);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const INSTRUCTIONS_LLM_REQUEST_PROCESSOR = new InstructionsLlmRequestProcessor();
|
|
104
|
+
class ContentRequestProcessor {
|
|
105
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
106
|
+
const agent = invocationContext.agent;
|
|
107
|
+
if (!agent || !(agent instanceof LlmAgent)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (agent.includeContents === "default") {
|
|
111
|
+
llmRequest.contents = getContents(
|
|
112
|
+
invocationContext.session.events,
|
|
113
|
+
agent.name,
|
|
114
|
+
invocationContext.branch
|
|
115
|
+
);
|
|
116
|
+
} else {
|
|
117
|
+
llmRequest.contents = getCurrentTurnContents(
|
|
118
|
+
invocationContext.session.events,
|
|
119
|
+
agent.name,
|
|
120
|
+
invocationContext.branch
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const CONTENT_REQUEST_PROCESSOR = new ContentRequestProcessor();
|
|
127
|
+
class AgentTransferLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
128
|
+
constructor() {
|
|
129
|
+
super(...arguments);
|
|
130
|
+
this.toolName = "transfer_to_agent";
|
|
131
|
+
this.tool = new FunctionTool({
|
|
132
|
+
name: this.toolName,
|
|
133
|
+
description: "Transfer the question to another agent. This tool hands off control to another agent when it is more suitable to answer the user question according to the agent description.",
|
|
134
|
+
parameters: z.object({
|
|
135
|
+
agentName: z.string().describe("the agent name to transfer to.")
|
|
136
|
+
}),
|
|
137
|
+
execute: function(args, toolContext) {
|
|
138
|
+
if (!toolContext) {
|
|
139
|
+
throw new Error("toolContext is required.");
|
|
140
|
+
}
|
|
141
|
+
toolContext.actions.transferToAgent = args.agentName;
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
146
|
+
if (!(invocationContext.agent instanceof LlmAgent)) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const transferTargets = this.getTransferTargets(invocationContext.agent);
|
|
150
|
+
if (!transferTargets.length) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
appendInstructions(llmRequest, [
|
|
154
|
+
this.buildTargetAgentsInstructions(
|
|
155
|
+
invocationContext.agent,
|
|
156
|
+
transferTargets
|
|
157
|
+
)
|
|
158
|
+
]);
|
|
159
|
+
const toolContext = new ToolContext({ invocationContext });
|
|
160
|
+
await this.tool.processLlmRequest({ toolContext, llmRequest });
|
|
161
|
+
}
|
|
162
|
+
buildTargetAgentsInfo(targetAgent) {
|
|
163
|
+
return `
|
|
164
|
+
Agent name: ${targetAgent.name}
|
|
165
|
+
Agent description: ${targetAgent.description}
|
|
166
|
+
`;
|
|
167
|
+
}
|
|
168
|
+
buildTargetAgentsInstructions(agent, targetAgents) {
|
|
169
|
+
let instructions = `
|
|
170
|
+
You have a list of other agents to transfer to:
|
|
171
|
+
|
|
172
|
+
${targetAgents.map(this.buildTargetAgentsInfo).join("\n")}
|
|
173
|
+
|
|
174
|
+
If you are the best to answer the question according to your description, you
|
|
175
|
+
can answer it.
|
|
176
|
+
|
|
177
|
+
If another agent is better for answering the question according to its
|
|
178
|
+
description, call \`${this.toolName}\` function to transfer the
|
|
179
|
+
question to that agent. When transferring, do not generate any text other than
|
|
180
|
+
the function call.
|
|
181
|
+
`;
|
|
182
|
+
if (agent.parentAgent && !agent.disallowTransferToParent) {
|
|
183
|
+
instructions += `
|
|
184
|
+
Your parent agent is ${agent.parentAgent.name}. If neither the other agents nor
|
|
185
|
+
you are best for answering the question according to the descriptions, transfer
|
|
186
|
+
to your parent agent.
|
|
187
|
+
`;
|
|
188
|
+
}
|
|
189
|
+
return instructions;
|
|
190
|
+
}
|
|
191
|
+
getTransferTargets(agent) {
|
|
192
|
+
const targets = [];
|
|
193
|
+
targets.push(...agent.subAgents);
|
|
194
|
+
if (!agent.parentAgent || !(agent.parentAgent instanceof LlmAgent)) {
|
|
195
|
+
return targets;
|
|
196
|
+
}
|
|
197
|
+
if (!agent.disallowTransferToParent) {
|
|
198
|
+
targets.push(agent.parentAgent);
|
|
199
|
+
}
|
|
200
|
+
if (!agent.disallowTransferToPeers) {
|
|
201
|
+
targets.push(
|
|
202
|
+
...agent.parentAgent.subAgents.filter(
|
|
203
|
+
(peerAgent) => peerAgent.name !== agent.name
|
|
204
|
+
)
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return targets;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const AGENT_TRANSFER_LLM_REQUEST_PROCESSOR = new AgentTransferLlmRequestProcessor();
|
|
211
|
+
class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
|
|
212
|
+
/** Handles tool confirmation information to build the LLM request. */
|
|
213
|
+
async *runAsync(invocationContext, llmRequest) {
|
|
214
|
+
const agent = invocationContext.agent;
|
|
215
|
+
if (!(agent instanceof LlmAgent)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const events = invocationContext.session.events;
|
|
219
|
+
if (!events || events.length === 0) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const requestConfirmationFunctionResponses = {};
|
|
223
|
+
let confirmationEventIndex = -1;
|
|
224
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
225
|
+
const event = events[i];
|
|
226
|
+
if (event.author !== "user") {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
const responses = getFunctionResponses(event);
|
|
230
|
+
if (!responses) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
let foundConfirmation = false;
|
|
234
|
+
for (const functionResponse of responses) {
|
|
235
|
+
if (functionResponse.name !== REQUEST_CONFIRMATION_FUNCTION_CALL_NAME) {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
foundConfirmation = true;
|
|
239
|
+
let toolConfirmation = null;
|
|
240
|
+
if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
|
|
241
|
+
toolConfirmation = JSON.parse(functionResponse.response["response"]);
|
|
242
|
+
} else if (functionResponse.response) {
|
|
243
|
+
toolConfirmation = new ToolConfirmation({
|
|
244
|
+
hint: functionResponse.response["hint"],
|
|
245
|
+
payload: functionResponse.response["payload"],
|
|
246
|
+
confirmed: functionResponse.response["confirmed"]
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
if (functionResponse.id && toolConfirmation) {
|
|
250
|
+
requestConfirmationFunctionResponses[functionResponse.id] = toolConfirmation;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (foundConfirmation) {
|
|
254
|
+
confirmationEventIndex = i;
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (Object.keys(requestConfirmationFunctionResponses).length === 0) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
for (let i = confirmationEventIndex - 1; i >= 0; i--) {
|
|
262
|
+
const event = events[i];
|
|
263
|
+
const functionCalls = getFunctionCalls(event);
|
|
264
|
+
if (!functionCalls) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const toolsToResumeWithConfirmation = {};
|
|
268
|
+
const toolsToResumeWithArgs = {};
|
|
269
|
+
for (const functionCall of functionCalls) {
|
|
270
|
+
if (!functionCall.id || !(functionCall.id in requestConfirmationFunctionResponses)) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const args = functionCall.args;
|
|
274
|
+
if (!args || !("originalFunctionCall" in args)) {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const originalFunctionCall = args["originalFunctionCall"];
|
|
278
|
+
if (originalFunctionCall.id) {
|
|
279
|
+
toolsToResumeWithConfirmation[originalFunctionCall.id] = requestConfirmationFunctionResponses[functionCall.id];
|
|
280
|
+
toolsToResumeWithArgs[originalFunctionCall.id] = originalFunctionCall;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
for (let j = events.length - 1; j > confirmationEventIndex; j--) {
|
|
287
|
+
const eventToCheck = events[j];
|
|
288
|
+
const functionResponses = getFunctionResponses(eventToCheck);
|
|
289
|
+
if (!functionResponses) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
for (const fr of functionResponses) {
|
|
293
|
+
if (fr.id && fr.id in toolsToResumeWithConfirmation) {
|
|
294
|
+
delete toolsToResumeWithConfirmation[fr.id];
|
|
295
|
+
delete toolsToResumeWithArgs[fr.id];
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
const toolsList = await agent.canonicalTools(new ReadonlyContext(invocationContext));
|
|
306
|
+
const toolsDict = Object.fromEntries(toolsList.map((tool) => [tool.name, tool]));
|
|
307
|
+
const functionResponseEvent = await handleFunctionCallList({
|
|
308
|
+
invocationContext,
|
|
309
|
+
functionCalls: Object.values(toolsToResumeWithArgs),
|
|
310
|
+
toolsDict,
|
|
311
|
+
beforeToolCallbacks: agent.canonicalBeforeToolCallbacks,
|
|
312
|
+
afterToolCallbacks: agent.canonicalAfterToolCallbacks,
|
|
313
|
+
filters: new Set(Object.keys(toolsToResumeWithConfirmation)),
|
|
314
|
+
toolConfirmationDict: toolsToResumeWithConfirmation
|
|
315
|
+
});
|
|
316
|
+
if (functionResponseEvent) {
|
|
317
|
+
yield functionResponseEvent;
|
|
318
|
+
}
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
|
|
324
|
+
class LlmAgent extends BaseAgent {
|
|
325
|
+
constructor(config) {
|
|
326
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
327
|
+
super(config);
|
|
328
|
+
this.model = config.model;
|
|
329
|
+
this.instruction = (_a = config.instruction) != null ? _a : "";
|
|
330
|
+
this.globalInstruction = (_b = config.globalInstruction) != null ? _b : "";
|
|
331
|
+
this.tools = (_c = config.tools) != null ? _c : [];
|
|
332
|
+
this.generateContentConfig = config.generateContentConfig;
|
|
333
|
+
this.disallowTransferToParent = (_d = config.disallowTransferToParent) != null ? _d : false;
|
|
334
|
+
this.disallowTransferToPeers = (_e = config.disallowTransferToPeers) != null ? _e : false;
|
|
335
|
+
this.includeContents = (_f = config.includeContents) != null ? _f : "default";
|
|
336
|
+
this.inputSchema = config.inputSchema;
|
|
337
|
+
this.outputSchema = config.outputSchema;
|
|
338
|
+
this.outputKey = config.outputKey;
|
|
339
|
+
this.beforeModelCallback = config.beforeModelCallback;
|
|
340
|
+
this.afterModelCallback = config.afterModelCallback;
|
|
341
|
+
this.beforeToolCallback = config.beforeToolCallback;
|
|
342
|
+
this.afterToolCallback = config.afterToolCallback;
|
|
343
|
+
this.requestProcessors = (_g = config.requestProcessors) != null ? _g : [
|
|
344
|
+
BASIC_LLM_REQUEST_PROCESSOR,
|
|
345
|
+
IDENTITY_LLM_REQUEST_PROCESSOR,
|
|
346
|
+
INSTRUCTIONS_LLM_REQUEST_PROCESSOR,
|
|
347
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
|
|
348
|
+
CONTENT_REQUEST_PROCESSOR
|
|
349
|
+
];
|
|
350
|
+
this.responseProcessors = (_h = config.responseProcessors) != null ? _h : [];
|
|
351
|
+
const agentTransferDisabled = this.disallowTransferToParent && this.disallowTransferToPeers && !((_i = this.subAgents) == null ? void 0 : _i.length);
|
|
352
|
+
if (!agentTransferDisabled) {
|
|
353
|
+
this.requestProcessors.push(AGENT_TRANSFER_LLM_REQUEST_PROCESSOR);
|
|
354
|
+
}
|
|
355
|
+
if (config.generateContentConfig) {
|
|
356
|
+
if (config.generateContentConfig.tools) {
|
|
357
|
+
throw new Error("All tools must be set via LlmAgent.tools.");
|
|
358
|
+
}
|
|
359
|
+
if (config.generateContentConfig.systemInstruction) {
|
|
360
|
+
throw new Error(
|
|
361
|
+
"System instruction must be set via LlmAgent.instruction."
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
if (config.generateContentConfig.responseSchema) {
|
|
365
|
+
throw new Error(
|
|
366
|
+
"Response schema must be set via LlmAgent.output_schema."
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
370
|
+
this.generateContentConfig = {};
|
|
371
|
+
}
|
|
372
|
+
if (this.outputSchema) {
|
|
373
|
+
if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
|
|
374
|
+
logger.warn(
|
|
375
|
+
`Invalid config for agent ${this.name}: outputSchema cannot co-exist with agent transfer configurations. Setting disallowTransferToParent=true, disallowTransferToPeers=true`
|
|
376
|
+
);
|
|
377
|
+
this.disallowTransferToParent = true;
|
|
378
|
+
this.disallowTransferToPeers = true;
|
|
379
|
+
}
|
|
380
|
+
if (this.subAgents && this.subAgents.length > 0) {
|
|
381
|
+
throw new Error(
|
|
382
|
+
`Invalid config for agent ${this.name}: if outputSchema is set, subAgents must be empty to disable agent transfer.`
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
if (this.tools && this.tools.length > 0) {
|
|
386
|
+
throw new Error(
|
|
387
|
+
`Invalid config for agent ${this.name}: if outputSchema is set, tools must be empty`
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* The resolved BaseLlm instance.
|
|
394
|
+
*
|
|
395
|
+
* When not set, the agent will inherit the model from its ancestor.
|
|
396
|
+
*/
|
|
397
|
+
get canonicalModel() {
|
|
398
|
+
if (this.model instanceof BaseLlm) {
|
|
399
|
+
return this.model;
|
|
400
|
+
}
|
|
401
|
+
if (typeof this.model === "string" && this.model) {
|
|
402
|
+
return LLMRegistry.newLlm(this.model);
|
|
403
|
+
}
|
|
404
|
+
let ancestorAgent = this.parentAgent;
|
|
405
|
+
while (ancestorAgent) {
|
|
406
|
+
if (ancestorAgent instanceof LlmAgent) {
|
|
407
|
+
return ancestorAgent.canonicalModel;
|
|
408
|
+
}
|
|
409
|
+
ancestorAgent = ancestorAgent.parentAgent;
|
|
410
|
+
}
|
|
411
|
+
throw new Error(`No model found for ${this.name}.`);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* The resolved self.instruction field to construct instruction for this
|
|
415
|
+
* agent.
|
|
416
|
+
*
|
|
417
|
+
* This method is only for use by Agent Development Kit.
|
|
418
|
+
* @param context The context to retrieve the session state.
|
|
419
|
+
* @returns The resolved self.instruction field.
|
|
420
|
+
*/
|
|
421
|
+
async canonicalInstruction(context) {
|
|
422
|
+
if (typeof this.instruction === "string") {
|
|
423
|
+
return { instruction: this.instruction, requireStateInjection: true };
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
instruction: await this.instruction(context),
|
|
427
|
+
requireStateInjection: false
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* The resolved self.instruction field to construct global instruction.
|
|
432
|
+
*
|
|
433
|
+
* This method is only for use by Agent Development Kit.
|
|
434
|
+
* @param context The context to retrieve the session state.
|
|
435
|
+
* @returns The resolved self.global_instruction field.
|
|
436
|
+
*/
|
|
437
|
+
async canonicalGlobalInstruction(context) {
|
|
438
|
+
if (typeof this.globalInstruction === "string") {
|
|
439
|
+
return { instruction: this.globalInstruction, requireStateInjection: true };
|
|
440
|
+
}
|
|
441
|
+
return {
|
|
442
|
+
instruction: await this.globalInstruction(context),
|
|
443
|
+
requireStateInjection: false
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* The resolved self.tools field as a list of BaseTool based on the context.
|
|
448
|
+
*
|
|
449
|
+
* This method is only for use by Agent Development Kit.
|
|
450
|
+
*/
|
|
451
|
+
async canonicalTools(context) {
|
|
452
|
+
const resolvedTools = [];
|
|
453
|
+
for (const toolUnion of this.tools) {
|
|
454
|
+
const tools = await convertToolUnionToTools(toolUnion, context);
|
|
455
|
+
resolvedTools.push(...tools);
|
|
456
|
+
}
|
|
457
|
+
return resolvedTools;
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Normalizes a callback or an array of callbacks into an array of callbacks.
|
|
461
|
+
*
|
|
462
|
+
* @param callback The callback or an array of callbacks.
|
|
463
|
+
* @returns An array of callbacks.
|
|
464
|
+
*/
|
|
465
|
+
static normalizeCallbackArray(callback) {
|
|
466
|
+
if (!callback) {
|
|
467
|
+
return [];
|
|
468
|
+
}
|
|
469
|
+
if (Array.isArray(callback)) {
|
|
470
|
+
return callback;
|
|
471
|
+
}
|
|
472
|
+
return [callback];
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The resolved self.before_model_callback field as a list of
|
|
476
|
+
* SingleBeforeModelCallback.
|
|
477
|
+
*
|
|
478
|
+
* This method is only for use by Agent Development Kit.
|
|
479
|
+
*/
|
|
480
|
+
get canonicalBeforeModelCallbacks() {
|
|
481
|
+
return LlmAgent.normalizeCallbackArray(this.beforeModelCallback);
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* The resolved self.after_model_callback field as a list of
|
|
485
|
+
* SingleAfterModelCallback.
|
|
486
|
+
*
|
|
487
|
+
* This method is only for use by Agent Development Kit.
|
|
488
|
+
*/
|
|
489
|
+
get canonicalAfterModelCallbacks() {
|
|
490
|
+
return LlmAgent.normalizeCallbackArray(this.afterModelCallback);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* The resolved self.before_tool_callback field as a list of
|
|
494
|
+
* BeforeToolCallback.
|
|
495
|
+
*
|
|
496
|
+
* This method is only for use by Agent Development Kit.
|
|
497
|
+
*/
|
|
498
|
+
get canonicalBeforeToolCallbacks() {
|
|
499
|
+
return LlmAgent.normalizeCallbackArray(this.beforeToolCallback);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* The resolved self.after_tool_callback field as a list of AfterToolCallback.
|
|
503
|
+
*
|
|
504
|
+
* This method is only for use by Agent Development Kit.
|
|
505
|
+
*/
|
|
506
|
+
get canonicalAfterToolCallbacks() {
|
|
507
|
+
return LlmAgent.normalizeCallbackArray(this.afterToolCallback);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Saves the agent's final response to the session state if configured.
|
|
511
|
+
*
|
|
512
|
+
* It extracts the text content from the final response event, optionally
|
|
513
|
+
* parses it as JSON based on the output schema, and stores the result in the
|
|
514
|
+
* session state using the specified output key.
|
|
515
|
+
*
|
|
516
|
+
* @param event The event to process.
|
|
517
|
+
*/
|
|
518
|
+
maybeSaveOutputToState(event) {
|
|
519
|
+
var _a, _b;
|
|
520
|
+
if (event.author !== this.name) {
|
|
521
|
+
logger.debug(
|
|
522
|
+
`Skipping output save for agent ${this.name}: event authored by ${event.author}`
|
|
523
|
+
);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
if (!this.outputKey) {
|
|
527
|
+
logger.debug(
|
|
528
|
+
`Skipping output save for agent ${this.name}: outputKey is not set`
|
|
529
|
+
);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (!isFinalResponse(event)) {
|
|
533
|
+
logger.debug(
|
|
534
|
+
`Skipping output save for agent ${this.name}: event is not a final response`
|
|
535
|
+
);
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (!((_b = (_a = event.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
|
|
539
|
+
logger.debug(
|
|
540
|
+
`Skipping output save for agent ${this.name}: event content is empty`
|
|
541
|
+
);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const resultStr = event.content.parts.map((part) => part.text ? part.text : "").join("");
|
|
545
|
+
let result = resultStr;
|
|
546
|
+
if (this.outputSchema) {
|
|
547
|
+
if (!resultStr.trim()) {
|
|
548
|
+
return;
|
|
549
|
+
}
|
|
550
|
+
try {
|
|
551
|
+
result = JSON.parse(resultStr);
|
|
552
|
+
} catch (e) {
|
|
553
|
+
logger.error(`Error parsing output for agent ${this.name}`, e);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
event.actions.stateDelta[this.outputKey] = result;
|
|
557
|
+
}
|
|
558
|
+
async *runAsyncImpl(context) {
|
|
559
|
+
while (true) {
|
|
560
|
+
let lastEvent = void 0;
|
|
561
|
+
for await (const event of this.runOneStepAsync(context)) {
|
|
562
|
+
lastEvent = event;
|
|
563
|
+
this.maybeSaveOutputToState(event);
|
|
564
|
+
yield event;
|
|
565
|
+
}
|
|
566
|
+
if (!lastEvent || isFinalResponse(lastEvent)) {
|
|
567
|
+
break;
|
|
568
|
+
}
|
|
569
|
+
if (lastEvent.partial) {
|
|
570
|
+
logger.warn("The last event is partial, which is not expected.");
|
|
571
|
+
break;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
async *runLiveImpl(context) {
|
|
576
|
+
for await (const event of this.runLiveFlow(context)) {
|
|
577
|
+
this.maybeSaveOutputToState(event);
|
|
578
|
+
yield event;
|
|
579
|
+
}
|
|
580
|
+
if (context.endInvocation) {
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
// --------------------------------------------------------------------------
|
|
585
|
+
// #START LlmFlow Logic
|
|
586
|
+
// --------------------------------------------------------------------------
|
|
587
|
+
async *runLiveFlow(invocationContext) {
|
|
588
|
+
await Promise.resolve();
|
|
589
|
+
throw new Error("LlmAgent.runLiveFlow not implemented");
|
|
590
|
+
}
|
|
591
|
+
async *runOneStepAsync(invocationContext) {
|
|
592
|
+
const llmRequest = {
|
|
593
|
+
contents: [],
|
|
594
|
+
toolsDict: {},
|
|
595
|
+
liveConnectConfig: {}
|
|
596
|
+
};
|
|
597
|
+
for (const processor of this.requestProcessors) {
|
|
598
|
+
for await (const event of processor.runAsync(invocationContext, llmRequest)) {
|
|
599
|
+
yield event;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
for (const toolUnion of this.tools) {
|
|
603
|
+
const toolContext = new ToolContext({ invocationContext });
|
|
604
|
+
const tools = await convertToolUnionToTools(
|
|
605
|
+
toolUnion,
|
|
606
|
+
new ReadonlyContext(invocationContext)
|
|
607
|
+
);
|
|
608
|
+
for (const tool of tools) {
|
|
609
|
+
await tool.processLlmRequest({ toolContext, llmRequest });
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
if (invocationContext.endInvocation) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const modelResponseEvent = createEvent({
|
|
616
|
+
invocationId: invocationContext.invocationId,
|
|
617
|
+
author: this.name,
|
|
618
|
+
branch: invocationContext.branch
|
|
619
|
+
});
|
|
620
|
+
for await (const llmResponse of this.callLlmAsync(
|
|
621
|
+
invocationContext,
|
|
622
|
+
llmRequest,
|
|
623
|
+
modelResponseEvent
|
|
624
|
+
)) {
|
|
625
|
+
for await (const event of this.postprocess(
|
|
626
|
+
invocationContext,
|
|
627
|
+
llmRequest,
|
|
628
|
+
llmResponse,
|
|
629
|
+
modelResponseEvent
|
|
630
|
+
)) {
|
|
631
|
+
modelResponseEvent.id = createNewEventId();
|
|
632
|
+
modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
633
|
+
yield event;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
|
|
638
|
+
var _a;
|
|
639
|
+
for (const processor of this.responseProcessors) {
|
|
640
|
+
for await (const event of processor.runAsync(invocationContext, llmResponse)) {
|
|
641
|
+
yield event;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (!llmResponse.content && !llmResponse.errorCode && !llmResponse.interrupted) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
const mergedEvent = createEvent({
|
|
648
|
+
...modelResponseEvent,
|
|
649
|
+
...llmResponse
|
|
650
|
+
});
|
|
651
|
+
if (mergedEvent.content) {
|
|
652
|
+
const functionCalls = getFunctionCalls(mergedEvent);
|
|
653
|
+
if (functionCalls == null ? void 0 : functionCalls.length) {
|
|
654
|
+
populateClientFunctionCallId(mergedEvent);
|
|
655
|
+
mergedEvent.longRunningToolIds = Array.from(
|
|
656
|
+
getLongRunningFunctionCalls(functionCalls, llmRequest.toolsDict)
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
yield mergedEvent;
|
|
661
|
+
if (!((_a = getFunctionCalls(mergedEvent)) == null ? void 0 : _a.length)) {
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const functionResponseEvent = await handleFunctionCallsAsync({
|
|
665
|
+
invocationContext,
|
|
666
|
+
functionCallEvent: mergedEvent,
|
|
667
|
+
toolsDict: llmRequest.toolsDict,
|
|
668
|
+
beforeToolCallbacks: this.canonicalBeforeToolCallbacks,
|
|
669
|
+
afterToolCallbacks: this.canonicalAfterToolCallbacks
|
|
670
|
+
});
|
|
671
|
+
if (!functionResponseEvent) {
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const authEvent = generateAuthEvent(invocationContext, functionResponseEvent);
|
|
675
|
+
if (authEvent) {
|
|
676
|
+
yield authEvent;
|
|
677
|
+
}
|
|
678
|
+
const toolConfirmationEvent = generateRequestConfirmationEvent({
|
|
679
|
+
invocationContext,
|
|
680
|
+
functionCallEvent: mergedEvent,
|
|
681
|
+
functionResponseEvent
|
|
682
|
+
});
|
|
683
|
+
if (toolConfirmationEvent) {
|
|
684
|
+
yield toolConfirmationEvent;
|
|
685
|
+
}
|
|
686
|
+
yield functionResponseEvent;
|
|
687
|
+
const nextAgentName = functionResponseEvent.actions.transferToAgent;
|
|
688
|
+
if (nextAgentName) {
|
|
689
|
+
const nextAgent = this.getAgentByName(invocationContext, nextAgentName);
|
|
690
|
+
for await (const event of nextAgent.runAsync(invocationContext)) {
|
|
691
|
+
yield event;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Retrieves an agent from the agent tree by its name.
|
|
697
|
+
*
|
|
698
|
+
* Performing a depth-first search to locate the agent with the given name.
|
|
699
|
+
* - Starts searching from the root agent of the current invocation context.
|
|
700
|
+
* - Traverses down the agent tree to find the specified agent.
|
|
701
|
+
*
|
|
702
|
+
* @param invocationContext The current invocation context.
|
|
703
|
+
* @param agentName The name of the agent to retrieve.
|
|
704
|
+
* @returns The agent with the given name.
|
|
705
|
+
* @throws Error if the agent is not found.
|
|
706
|
+
*/
|
|
707
|
+
getAgentByName(invocationContext, agentName) {
|
|
708
|
+
const rootAgent = invocationContext.agent.rootAgent;
|
|
709
|
+
const agentToRun = rootAgent.findAgent(agentName);
|
|
710
|
+
if (!agentToRun) {
|
|
711
|
+
throw new Error(`Agent ${agentName} not found in the agent tree.`);
|
|
712
|
+
}
|
|
713
|
+
return agentToRun;
|
|
714
|
+
}
|
|
715
|
+
async *callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
|
|
716
|
+
var _a, _b, _c, _d;
|
|
717
|
+
const beforeModelResponse = await this.handleBeforeModelCallback(
|
|
718
|
+
invocationContext,
|
|
719
|
+
llmRequest,
|
|
720
|
+
modelResponseEvent
|
|
721
|
+
);
|
|
722
|
+
if (beforeModelResponse) {
|
|
723
|
+
yield beforeModelResponse;
|
|
724
|
+
return;
|
|
725
|
+
}
|
|
726
|
+
(_a = llmRequest.config) != null ? _a : llmRequest.config = {};
|
|
727
|
+
(_c = (_b = llmRequest.config).labels) != null ? _c : _b.labels = {};
|
|
728
|
+
if (!llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY]) {
|
|
729
|
+
llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
|
|
730
|
+
}
|
|
731
|
+
const llm = this.canonicalModel;
|
|
732
|
+
if ((_d = invocationContext.runConfig) == null ? void 0 : _d.supportCfc) {
|
|
733
|
+
throw new Error("CFC is not yet supported in callLlmAsync");
|
|
734
|
+
} else {
|
|
735
|
+
invocationContext.incrementLlmCallCount();
|
|
736
|
+
const responsesGenerator = llm.generateContentAsync(llmRequest);
|
|
737
|
+
for await (const llmResponse of this.runAndHandleError(
|
|
738
|
+
responsesGenerator,
|
|
739
|
+
invocationContext,
|
|
740
|
+
llmRequest,
|
|
741
|
+
modelResponseEvent
|
|
742
|
+
)) {
|
|
743
|
+
const alteredLlmResponse = await this.handleAfterModelCallback(
|
|
744
|
+
invocationContext,
|
|
745
|
+
llmResponse,
|
|
746
|
+
modelResponseEvent
|
|
747
|
+
);
|
|
748
|
+
yield alteredLlmResponse != null ? alteredLlmResponse : llmResponse;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
async handleBeforeModelCallback(invocationContext, llmRequest, modelResponseEvent) {
|
|
753
|
+
const callbackContext = new CallbackContext(
|
|
754
|
+
{ invocationContext, eventActions: modelResponseEvent.actions }
|
|
755
|
+
);
|
|
756
|
+
const beforeModelCallbackResponse = await invocationContext.pluginManager.runBeforeModelCallback(
|
|
757
|
+
{ callbackContext, llmRequest }
|
|
758
|
+
);
|
|
759
|
+
if (beforeModelCallbackResponse) {
|
|
760
|
+
return beforeModelCallbackResponse;
|
|
761
|
+
}
|
|
762
|
+
for (const callback of this.canonicalBeforeModelCallbacks) {
|
|
763
|
+
const callbackResponse = await callback({ context: callbackContext, request: llmRequest });
|
|
764
|
+
if (callbackResponse) {
|
|
765
|
+
return callbackResponse;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return void 0;
|
|
769
|
+
}
|
|
770
|
+
async handleAfterModelCallback(invocationContext, llmResponse, modelResponseEvent) {
|
|
771
|
+
const callbackContext = new CallbackContext(
|
|
772
|
+
{ invocationContext, eventActions: modelResponseEvent.actions }
|
|
773
|
+
);
|
|
774
|
+
const afterModelCallbackResponse = await invocationContext.pluginManager.runAfterModelCallback(
|
|
775
|
+
{ callbackContext, llmResponse }
|
|
776
|
+
);
|
|
777
|
+
if (afterModelCallbackResponse) {
|
|
778
|
+
return afterModelCallbackResponse;
|
|
779
|
+
}
|
|
780
|
+
for (const callback of this.canonicalAfterModelCallbacks) {
|
|
781
|
+
const callbackResponse = await callback({ context: callbackContext, response: llmResponse });
|
|
782
|
+
if (callbackResponse) {
|
|
783
|
+
return callbackResponse;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return void 0;
|
|
787
|
+
}
|
|
788
|
+
async *runAndHandleError(responseGenerator, invocationContext, llmRequest, modelResponseEvent) {
|
|
789
|
+
try {
|
|
790
|
+
for await (const response of responseGenerator) {
|
|
791
|
+
yield response;
|
|
792
|
+
}
|
|
793
|
+
} catch (modelError) {
|
|
794
|
+
const callbackContext = new CallbackContext(
|
|
795
|
+
{ invocationContext, eventActions: modelResponseEvent.actions }
|
|
796
|
+
);
|
|
797
|
+
if (modelError instanceof Error) {
|
|
798
|
+
const onModelErrorCallbackResponse = await invocationContext.pluginManager.runOnModelErrorCallback({
|
|
799
|
+
callbackContext,
|
|
800
|
+
llmRequest,
|
|
801
|
+
error: modelError
|
|
802
|
+
});
|
|
803
|
+
if (onModelErrorCallbackResponse) {
|
|
804
|
+
yield onModelErrorCallbackResponse;
|
|
805
|
+
} else {
|
|
806
|
+
const errorResponse = JSON.parse(modelError.message);
|
|
807
|
+
yield {
|
|
808
|
+
errorCode: String(errorResponse.error.code),
|
|
809
|
+
errorMessage: errorResponse.error.message
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
} else {
|
|
813
|
+
logger.error("Unknown error during response generation", modelError);
|
|
814
|
+
throw modelError;
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
// --------------------------------------------------------------------------
|
|
819
|
+
// #END LlmFlow Logic
|
|
820
|
+
// --------------------------------------------------------------------------
|
|
821
|
+
// TODO - b/425992518: omitted Py LlmAgent features.
|
|
822
|
+
// - code_executor
|
|
823
|
+
// - configurable agents by yaml config
|
|
824
|
+
}
|
|
825
|
+
export {
|
|
826
|
+
LlmAgent,
|
|
827
|
+
REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR
|
|
828
|
+
};
|