@google/adk 0.2.5 → 0.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/part_converter_utils.js +210 -0
- package/dist/cjs/agents/active_streaming_tool.js +1 -1
- package/dist/cjs/agents/base_agent.js +46 -24
- package/dist/cjs/agents/base_llm_processor.js +1 -1
- package/dist/cjs/agents/callback_context.js +5 -2
- package/dist/cjs/agents/content_processor_utils.js +16 -8
- package/dist/cjs/agents/functions.js +81 -30
- package/dist/cjs/agents/instructions.js +1 -1
- package/dist/cjs/agents/invocation_context.js +1 -1
- package/dist/cjs/agents/live_request_queue.js +1 -1
- package/dist/cjs/agents/llm_agent.js +106 -62
- package/dist/cjs/agents/loop_agent.js +3 -2
- package/dist/cjs/agents/parallel_agent.js +4 -5
- package/dist/cjs/agents/readonly_context.js +13 -1
- package/dist/cjs/agents/run_config.js +2 -1
- package/dist/cjs/agents/sequential_agent.js +1 -1
- package/dist/cjs/agents/transcription_entry.js +1 -1
- package/dist/cjs/artifacts/base_artifact_service.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +491 -0
- package/dist/cjs/artifacts/gcs_artifact_service.js +145 -58
- package/dist/cjs/artifacts/in_memory_artifact_service.js +72 -10
- package/dist/cjs/artifacts/registry.js +55 -0
- package/dist/cjs/auth/auth_credential.js +1 -1
- package/dist/cjs/auth/auth_handler.js +4 -2
- package/dist/cjs/auth/auth_schemes.js +1 -1
- package/dist/cjs/auth/auth_tool.js +1 -1
- package/dist/cjs/auth/credential_service/base_credential_service.js +1 -1
- package/dist/cjs/auth/credential_service/in_memory_credential_service.js +1 -1
- package/dist/cjs/auth/exchanger/base_credential_exchanger.js +1 -1
- package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +1 -1
- package/dist/cjs/code_executors/base_code_executor.js +4 -2
- package/dist/cjs/code_executors/built_in_code_executor.js +8 -4
- package/dist/cjs/code_executors/code_execution_utils.js +1 -1
- package/dist/cjs/code_executors/code_executor_context.js +6 -6
- package/dist/cjs/common.js +18 -1
- package/dist/cjs/events/event.js +34 -7
- package/dist/cjs/events/event_actions.js +2 -2
- package/dist/cjs/events/structured_events.js +105 -0
- package/dist/cjs/examples/base_example_provider.js +1 -1
- package/dist/cjs/examples/example.js +1 -1
- package/dist/cjs/examples/example_util.js +1 -1
- package/dist/cjs/index.js +54 -83
- package/dist/cjs/index_web.js +1 -1
- package/dist/cjs/memory/base_memory_service.js +1 -1
- package/dist/cjs/memory/in_memory_memory_service.js +4 -2
- package/dist/cjs/memory/memory_entry.js +1 -1
- package/dist/cjs/models/apigee_llm.js +182 -0
- package/dist/cjs/models/base_llm.js +9 -5
- package/dist/cjs/models/base_llm_connection.js +1 -1
- package/dist/cjs/models/gemini_llm_connection.js +2 -1
- package/dist/cjs/models/google_llm.js +73 -54
- package/dist/cjs/models/llm_request.js +1 -1
- package/dist/cjs/models/llm_response.js +1 -1
- package/dist/cjs/models/registry.js +3 -1
- package/dist/cjs/plugins/base_plugin.js +13 -1
- package/dist/cjs/plugins/logging_plugin.js +51 -14
- package/dist/cjs/plugins/plugin_manager.js +57 -25
- package/dist/cjs/plugins/security_plugin.js +2 -2
- package/dist/cjs/runner/in_memory_runner.js +1 -1
- package/dist/cjs/runner/runner.js +142 -96
- package/dist/cjs/sessions/base_session_service.js +53 -3
- package/dist/cjs/sessions/database_session_service.js +364 -0
- package/dist/cjs/sessions/db/operations.js +114 -0
- package/dist/cjs/sessions/db/schema.js +204 -0
- package/dist/cjs/sessions/in_memory_session_service.js +60 -34
- package/dist/cjs/sessions/registry.js +49 -0
- package/dist/cjs/sessions/session.js +1 -1
- package/dist/cjs/sessions/state.js +1 -1
- package/dist/cjs/telemetry/google_cloud.js +8 -10
- package/dist/cjs/telemetry/setup.js +16 -8
- package/dist/cjs/telemetry/tracing.js +38 -16
- package/dist/cjs/tools/agent_tool.js +9 -5
- package/dist/cjs/tools/base_tool.js +5 -3
- package/dist/cjs/tools/base_toolset.js +1 -1
- package/dist/cjs/tools/forwarding_artifact_service.js +18 -2
- package/dist/cjs/tools/function_tool.js +2 -3
- package/dist/cjs/tools/google_search_tool.js +2 -3
- package/dist/cjs/tools/long_running_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_session_manager.js +17 -11
- package/dist/cjs/tools/mcp/mcp_tool.js +2 -4
- package/dist/cjs/tools/mcp/mcp_toolset.js +2 -2
- package/dist/cjs/tools/tool_confirmation.js +1 -1
- package/dist/cjs/tools/tool_context.js +1 -1
- package/dist/cjs/utils/client_labels.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +11 -2
- package/dist/cjs/utils/gemini_schema_util.js +10 -5
- package/dist/cjs/utils/logger.js +48 -4
- package/dist/cjs/utils/model_name.js +1 -1
- package/dist/cjs/utils/object_notation_utils.js +78 -0
- package/dist/cjs/utils/simple_zod_to_json.js +101 -142
- package/dist/cjs/utils/variant_utils.js +3 -9
- package/dist/cjs/version.js +2 -2
- package/dist/esm/a2a/part_converter_utils.js +171 -0
- package/dist/esm/agents/base_agent.js +50 -24
- package/dist/esm/agents/callback_context.js +4 -1
- package/dist/esm/agents/content_processor_utils.js +25 -9
- package/dist/esm/agents/functions.js +84 -29
- package/dist/esm/agents/llm_agent.js +110 -62
- package/dist/esm/agents/loop_agent.js +2 -1
- package/dist/esm/agents/parallel_agent.js +3 -4
- package/dist/esm/agents/readonly_context.js +12 -0
- package/dist/esm/agents/run_config.js +1 -0
- package/dist/esm/artifacts/file_artifact_service.js +451 -0
- package/dist/esm/artifacts/gcs_artifact_service.js +144 -57
- package/dist/esm/artifacts/in_memory_artifact_service.js +69 -8
- package/dist/esm/artifacts/registry.js +28 -0
- package/dist/esm/auth/auth_handler.js +3 -1
- package/dist/esm/code_executors/base_code_executor.js +3 -1
- package/dist/esm/code_executors/built_in_code_executor.js +7 -3
- package/dist/esm/code_executors/code_executor_context.js +5 -5
- package/dist/esm/common.js +12 -2
- package/dist/esm/events/event.js +30 -5
- package/dist/esm/events/event_actions.js +1 -1
- package/dist/esm/events/structured_events.js +74 -0
- package/dist/esm/index.js +18 -88
- package/dist/esm/memory/in_memory_memory_service.js +3 -1
- package/dist/esm/models/apigee_llm.js +152 -0
- package/dist/esm/models/base_llm.js +8 -4
- package/dist/esm/models/gemini_llm_connection.js +1 -0
- package/dist/esm/models/google_llm.js +75 -53
- package/dist/esm/models/registry.js +2 -0
- package/dist/esm/plugins/base_plugin.js +12 -0
- package/dist/esm/plugins/logging_plugin.js +55 -14
- package/dist/esm/plugins/plugin_manager.js +56 -24
- package/dist/esm/plugins/security_plugin.js +1 -1
- package/dist/esm/runner/runner.js +145 -96
- package/dist/esm/sessions/base_session_service.js +49 -1
- package/dist/esm/sessions/database_session_service.js +350 -0
- package/dist/esm/sessions/db/operations.js +87 -0
- package/dist/esm/sessions/db/schema.js +172 -0
- package/dist/esm/sessions/in_memory_session_service.js +61 -33
- package/dist/esm/sessions/registry.js +25 -0
- package/dist/esm/telemetry/google_cloud.js +7 -9
- package/dist/esm/telemetry/setup.js +23 -9
- package/dist/esm/telemetry/tracing.js +37 -15
- package/dist/esm/tools/agent_tool.js +8 -4
- package/dist/esm/tools/base_tool.js +4 -2
- package/dist/esm/tools/forwarding_artifact_service.js +17 -1
- package/dist/esm/tools/function_tool.js +1 -2
- package/dist/esm/tools/google_search_tool.js +2 -5
- package/dist/esm/tools/long_running_tool.js +3 -1
- package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/esm/tools/mcp/mcp_tool.js +1 -3
- package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
- package/dist/esm/utils/env_aware_utils.js +9 -1
- package/dist/esm/utils/gemini_schema_util.js +9 -4
- package/dist/esm/utils/logger.js +43 -2
- package/dist/esm/utils/object_notation_utils.js +47 -0
- package/dist/esm/utils/simple_zod_to_json.js +102 -141
- package/dist/esm/utils/variant_utils.js +1 -7
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/part_converter_utils.d.ts +47 -0
- package/dist/types/agents/base_agent.d.ts +2 -1
- package/dist/types/agents/callback_context.d.ts +1 -1
- package/dist/types/agents/llm_agent.d.ts +12 -12
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/parallel_agent.d.ts +1 -1
- package/dist/types/agents/readonly_context.d.ts +8 -0
- package/dist/types/agents/run_config.d.ts +6 -0
- package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
- package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
- package/dist/types/artifacts/in_memory_artifact_service.d.ts +8 -5
- package/dist/types/artifacts/registry.d.ts +7 -0
- package/dist/types/code_executors/built_in_code_executor.d.ts +1 -1
- package/dist/types/code_executors/code_executor_context.d.ts +2 -4
- package/dist/types/common.d.ts +13 -3
- package/dist/types/events/event.d.ts +15 -1
- package/dist/types/events/event_actions.d.ts +1 -1
- package/dist/types/events/structured_events.d.ts +106 -0
- package/dist/types/index.d.ts +7 -3
- package/dist/types/models/apigee_llm.d.ts +59 -0
- package/dist/types/models/base_llm_connection.d.ts +1 -1
- package/dist/types/models/google_llm.d.ts +5 -2
- package/dist/types/models/llm_response.d.ts +1 -1
- package/dist/types/plugins/logging_plugin.d.ts +12 -12
- package/dist/types/plugins/plugin_manager.d.ts +12 -12
- package/dist/types/plugins/security_plugin.d.ts +1 -1
- package/dist/types/runner/runner.d.ts +16 -1
- package/dist/types/sessions/base_session_service.d.ts +20 -0
- package/dist/types/sessions/database_session_service.d.ts +31 -0
- package/dist/types/sessions/db/operations.d.ts +29 -0
- package/dist/types/sessions/db/schema.d.ts +45 -0
- package/dist/types/sessions/in_memory_session_service.d.ts +9 -6
- package/dist/types/sessions/registry.d.ts +7 -0
- package/dist/types/telemetry/setup.d.ts +1 -1
- package/dist/types/telemetry/tracing.d.ts +7 -6
- package/dist/types/tools/agent_tool.d.ts +1 -1
- package/dist/types/tools/base_tool.d.ts +1 -1
- package/dist/types/tools/base_toolset.d.ts +2 -1
- package/dist/types/tools/forwarding_artifact_service.d.ts +4 -2
- package/dist/types/tools/function_tool.d.ts +4 -3
- package/dist/types/tools/google_search_tool.d.ts +3 -3
- package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
- package/dist/types/utils/env_aware_utils.d.ts +7 -0
- package/dist/types/utils/gemini_schema_util.d.ts +4 -12
- package/dist/types/utils/logger.d.ts +11 -10
- package/dist/types/utils/object_notation_utils.d.ts +21 -0
- package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/part_converter_utils.js +171 -0
- package/dist/web/agents/base_agent.js +96 -35
- package/dist/web/agents/callback_context.js +4 -1
- package/dist/web/agents/content_processor_utils.js +25 -9
- package/dist/web/agents/functions.js +84 -29
- package/dist/web/agents/llm_agent.js +166 -83
- package/dist/web/agents/loop_agent.js +2 -1
- package/dist/web/agents/parallel_agent.js +3 -4
- package/dist/web/agents/readonly_context.js +12 -0
- package/dist/web/agents/run_config.js +2 -1
- package/dist/web/artifacts/file_artifact_service.js +506 -0
- package/dist/web/artifacts/gcs_artifact_service.js +139 -54
- package/dist/web/artifacts/in_memory_artifact_service.js +69 -8
- package/dist/web/artifacts/registry.js +28 -0
- package/dist/web/auth/auth_handler.js +3 -1
- package/dist/web/code_executors/base_code_executor.js +3 -1
- package/dist/web/code_executors/built_in_code_executor.js +7 -3
- package/dist/web/code_executors/code_executor_context.js +5 -5
- package/dist/web/common.js +12 -2
- package/dist/web/events/event.js +30 -5
- package/dist/web/events/event_actions.js +1 -1
- package/dist/web/events/structured_events.js +74 -0
- package/dist/web/index.js +18 -8
- package/dist/web/memory/in_memory_memory_service.js +3 -1
- package/dist/web/models/apigee_llm.js +219 -0
- package/dist/web/models/base_llm.js +8 -4
- package/dist/web/models/gemini_llm_connection.js +1 -0
- package/dist/web/models/google_llm.js +75 -50
- package/dist/web/models/registry.js +2 -0
- package/dist/web/plugins/base_plugin.js +12 -0
- package/dist/web/plugins/logging_plugin.js +55 -14
- package/dist/web/plugins/plugin_manager.js +56 -24
- package/dist/web/plugins/security_plugin.js +1 -1
- package/dist/web/runner/runner.js +192 -108
- package/dist/web/sessions/base_session_service.js +49 -1
- package/dist/web/sessions/database_session_service.js +368 -0
- package/dist/web/sessions/db/operations.js +87 -0
- package/dist/web/sessions/db/schema.js +172 -0
- package/dist/web/sessions/in_memory_session_service.js +61 -33
- package/dist/web/sessions/registry.js +25 -0
- package/dist/web/telemetry/google_cloud.js +7 -9
- package/dist/web/telemetry/setup.js +23 -9
- package/dist/web/telemetry/tracing.js +37 -15
- package/dist/web/tools/agent_tool.js +8 -4
- package/dist/web/tools/base_tool.js +4 -2
- package/dist/web/tools/forwarding_artifact_service.js +17 -1
- package/dist/web/tools/function_tool.js +1 -2
- package/dist/web/tools/google_search_tool.js +2 -5
- package/dist/web/tools/long_running_tool.js +3 -1
- package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
- package/dist/web/tools/mcp/mcp_tool.js +1 -3
- package/dist/web/tools/mcp/mcp_toolset.js +1 -1
- package/dist/web/utils/env_aware_utils.js +9 -1
- package/dist/web/utils/gemini_schema_util.js +9 -4
- package/dist/web/utils/logger.js +43 -2
- package/dist/web/utils/object_notation_utils.js +47 -0
- package/dist/web/utils/simple_zod_to_json.js +102 -155
- package/dist/web/utils/variant_utils.js +1 -7
- package/dist/web/version.js +1 -1
- package/package.json +16 -5
- package/dist/cjs/index.js.map +0 -7
- package/dist/esm/index.js.map +0 -7
- package/dist/web/index.js.map +0 -7
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
var MetadataKeys = /* @__PURE__ */ ((MetadataKeys2) => {
|
|
7
|
+
MetadataKeys2["TYPE"] = "adk_type";
|
|
8
|
+
MetadataKeys2["LONG_RUNNING"] = "adk_is_long_running";
|
|
9
|
+
MetadataKeys2["THOUGHT"] = "adk_thought";
|
|
10
|
+
return MetadataKeys2;
|
|
11
|
+
})(MetadataKeys || {});
|
|
12
|
+
var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
|
|
13
|
+
DataPartType2["FUNCTION_CALL"] = "function_call";
|
|
14
|
+
DataPartType2["FUNCTION_RESPONSE"] = "function_response";
|
|
15
|
+
DataPartType2["CODE_EXEC_RESULT"] = "code_execution_result";
|
|
16
|
+
DataPartType2["CODE_EXECUTABLE_CODE"] = "executable_code";
|
|
17
|
+
return DataPartType2;
|
|
18
|
+
})(DataPartType || {});
|
|
19
|
+
function toA2AParts(parts, longRunningToolIDs = []) {
|
|
20
|
+
return parts.map((part) => toA2APart(part, longRunningToolIDs));
|
|
21
|
+
}
|
|
22
|
+
function toA2APart(part, longRunningToolIDs) {
|
|
23
|
+
if (part.text !== void 0 && part.text !== null) {
|
|
24
|
+
return toA2ATextPart(part);
|
|
25
|
+
}
|
|
26
|
+
if (part.inlineData || part.fileData) {
|
|
27
|
+
return toA2AFilePart(part);
|
|
28
|
+
}
|
|
29
|
+
return toA2ADataPart(part, longRunningToolIDs);
|
|
30
|
+
}
|
|
31
|
+
function toA2ATextPart(part) {
|
|
32
|
+
const a2aPart = { kind: "text", text: part.text || "" };
|
|
33
|
+
if ("thought" in part && part["thought"]) {
|
|
34
|
+
a2aPart.metadata = {
|
|
35
|
+
["adk_thought" /* THOUGHT */]: true
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return a2aPart;
|
|
39
|
+
}
|
|
40
|
+
function toA2AFilePart(part) {
|
|
41
|
+
if (part.fileData) {
|
|
42
|
+
return {
|
|
43
|
+
kind: "file",
|
|
44
|
+
file: {
|
|
45
|
+
uri: part.fileData.fileUri || "",
|
|
46
|
+
mimeType: part.fileData.mimeType
|
|
47
|
+
},
|
|
48
|
+
metadata: {}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
if (part.inlineData) {
|
|
52
|
+
return {
|
|
53
|
+
kind: "file",
|
|
54
|
+
file: {
|
|
55
|
+
bytes: part.inlineData.data || "",
|
|
56
|
+
mimeType: part.inlineData.mimeType
|
|
57
|
+
},
|
|
58
|
+
metadata: {}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
throw new Error(`Not a file part: ${JSON.stringify(part)}`);
|
|
62
|
+
}
|
|
63
|
+
function toA2ADataPart(part, longRunningToolIDs = []) {
|
|
64
|
+
let type;
|
|
65
|
+
let data;
|
|
66
|
+
if (part.functionCall) {
|
|
67
|
+
type = "function_call" /* FUNCTION_CALL */;
|
|
68
|
+
data = part.functionCall;
|
|
69
|
+
} else if (part.functionResponse) {
|
|
70
|
+
type = "function_response" /* FUNCTION_RESPONSE */;
|
|
71
|
+
data = part.functionResponse;
|
|
72
|
+
} else if (part.executableCode) {
|
|
73
|
+
type = "executable_code" /* CODE_EXECUTABLE_CODE */;
|
|
74
|
+
data = part.executableCode;
|
|
75
|
+
} else if (part.codeExecutionResult) {
|
|
76
|
+
type = "code_execution_result" /* CODE_EXEC_RESULT */;
|
|
77
|
+
data = part.codeExecutionResult;
|
|
78
|
+
} else {
|
|
79
|
+
throw new Error(`Unknown part type: ${JSON.stringify(part)}`);
|
|
80
|
+
}
|
|
81
|
+
const metadata = {
|
|
82
|
+
["adk_type" /* TYPE */]: type
|
|
83
|
+
};
|
|
84
|
+
if (part.functionCall && part.functionCall.name && longRunningToolIDs.includes(part.functionCall.name)) {
|
|
85
|
+
metadata["adk_is_long_running" /* LONG_RUNNING */] = true;
|
|
86
|
+
}
|
|
87
|
+
if (part.functionResponse && part.functionResponse.name && longRunningToolIDs.includes(part.functionResponse.name)) {
|
|
88
|
+
metadata["adk_is_long_running" /* LONG_RUNNING */] = true;
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
kind: "data",
|
|
92
|
+
data,
|
|
93
|
+
metadata
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function toGenAIParts(a2aParts) {
|
|
97
|
+
return a2aParts.map((a2aPart) => toGenAIPart(a2aPart));
|
|
98
|
+
}
|
|
99
|
+
function toGenAIPart(a2aPart) {
|
|
100
|
+
if (a2aPart.kind === "text") {
|
|
101
|
+
return toGenAIPartText(a2aPart);
|
|
102
|
+
}
|
|
103
|
+
if (a2aPart.kind === "file") {
|
|
104
|
+
return toGenAIPartFile(a2aPart);
|
|
105
|
+
}
|
|
106
|
+
if (a2aPart.kind === "data") {
|
|
107
|
+
return toGenAIPartData(a2aPart);
|
|
108
|
+
}
|
|
109
|
+
throw new Error(`Unknown part kind: ${JSON.stringify(a2aPart)}`);
|
|
110
|
+
}
|
|
111
|
+
function toGenAIPartText(a2aPart) {
|
|
112
|
+
var _a;
|
|
113
|
+
return {
|
|
114
|
+
text: a2aPart.text,
|
|
115
|
+
thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a["adk_thought" /* THOUGHT */])
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function toGenAIPartFile(a2aPart) {
|
|
119
|
+
if ("bytes" in a2aPart.file) {
|
|
120
|
+
return {
|
|
121
|
+
inlineData: {
|
|
122
|
+
data: a2aPart.file.bytes,
|
|
123
|
+
mimeType: a2aPart.file.mimeType || ""
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if ("uri" in a2aPart.file) {
|
|
128
|
+
return {
|
|
129
|
+
fileData: {
|
|
130
|
+
fileUri: a2aPart.file.uri,
|
|
131
|
+
mimeType: a2aPart.file.mimeType || ""
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`Not a file part: ${JSON.stringify(a2aPart)}`);
|
|
136
|
+
}
|
|
137
|
+
function toGenAIPartData(a2aPart) {
|
|
138
|
+
var _a;
|
|
139
|
+
if (!a2aPart.data) {
|
|
140
|
+
throw new Error(`No data in part: ${JSON.stringify(a2aPart)}`);
|
|
141
|
+
}
|
|
142
|
+
const data = a2aPart.data;
|
|
143
|
+
const type = (_a = a2aPart.metadata) == null ? void 0 : _a["adk_type" /* TYPE */];
|
|
144
|
+
if (type === "function_call" /* FUNCTION_CALL */) {
|
|
145
|
+
return { functionCall: data };
|
|
146
|
+
}
|
|
147
|
+
if (type === "function_response" /* FUNCTION_RESPONSE */) {
|
|
148
|
+
return { functionResponse: data };
|
|
149
|
+
}
|
|
150
|
+
if (type === "executable_code" /* CODE_EXECUTABLE_CODE */) {
|
|
151
|
+
return { executableCode: data };
|
|
152
|
+
}
|
|
153
|
+
if (type === "code_execution_result" /* CODE_EXEC_RESULT */) {
|
|
154
|
+
return { codeExecutionResult: data };
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
text: JSON.stringify(a2aPart.data)
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export {
|
|
161
|
+
toA2ADataPart,
|
|
162
|
+
toA2AFilePart,
|
|
163
|
+
toA2APart,
|
|
164
|
+
toA2AParts,
|
|
165
|
+
toA2ATextPart,
|
|
166
|
+
toGenAIPart,
|
|
167
|
+
toGenAIPartData,
|
|
168
|
+
toGenAIPartFile,
|
|
169
|
+
toGenAIPartText,
|
|
170
|
+
toGenAIParts
|
|
171
|
+
};
|
|
@@ -4,8 +4,13 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
var _a;
|
|
7
|
-
import { trace } from "@opentelemetry/api";
|
|
7
|
+
import { context, trace } from "@opentelemetry/api";
|
|
8
8
|
import { createEvent } from "../events/event.js";
|
|
9
|
+
import {
|
|
10
|
+
runAsyncGeneratorWithOtelContext,
|
|
11
|
+
traceAgentInvocation,
|
|
12
|
+
tracer
|
|
13
|
+
} from "../telemetry/tracing.js";
|
|
9
14
|
import { CallbackContext } from "./callback_context.js";
|
|
10
15
|
import { InvocationContext } from "./invocation_context.js";
|
|
11
16
|
const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
|
|
@@ -23,13 +28,19 @@ class BaseAgent {
|
|
|
23
28
|
this.description = config.description;
|
|
24
29
|
this.parentAgent = config.parentAgent;
|
|
25
30
|
this.subAgents = config.subAgents || [];
|
|
26
|
-
this.rootAgent = getRootAgent(this);
|
|
27
31
|
this.beforeAgentCallback = getCannonicalCallback(
|
|
28
32
|
config.beforeAgentCallback
|
|
29
33
|
);
|
|
30
34
|
this.afterAgentCallback = getCannonicalCallback(config.afterAgentCallback);
|
|
31
35
|
this.setParentAgentForSubAgents();
|
|
32
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Root agent of this agent.
|
|
39
|
+
* Computed dynamically by traversing up the parent chain.
|
|
40
|
+
*/
|
|
41
|
+
get rootAgent() {
|
|
42
|
+
return getRootAgent(this);
|
|
43
|
+
}
|
|
33
44
|
/**
|
|
34
45
|
* Entry method to run an agent via text-based conversation.
|
|
35
46
|
*
|
|
@@ -38,26 +49,34 @@ class BaseAgent {
|
|
|
38
49
|
* @returns An AsyncGenerator that yields the events generated by the agent.
|
|
39
50
|
*/
|
|
40
51
|
async *runAsync(parentContext) {
|
|
41
|
-
const span =
|
|
52
|
+
const span = tracer.startSpan(`invoke_agent ${this.name}`);
|
|
53
|
+
const ctx = trace.setSpan(context.active(), span);
|
|
42
54
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
yield* runAsyncGeneratorWithOtelContext(
|
|
56
|
+
ctx,
|
|
57
|
+
this,
|
|
58
|
+
async function* () {
|
|
59
|
+
const context2 = this.createInvocationContext(parentContext);
|
|
60
|
+
const beforeAgentCallbackEvent = await this.handleBeforeAgentCallback(context2);
|
|
61
|
+
if (beforeAgentCallbackEvent) {
|
|
62
|
+
yield beforeAgentCallbackEvent;
|
|
63
|
+
}
|
|
64
|
+
if (context2.endInvocation) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
traceAgentInvocation({ agent: this, invocationContext: context2 });
|
|
68
|
+
for await (const event of this.runAsyncImpl(context2)) {
|
|
69
|
+
yield event;
|
|
70
|
+
}
|
|
71
|
+
if (context2.endInvocation) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const afterAgentCallbackEvent = await this.handleAfterAgentCallback(context2);
|
|
75
|
+
if (afterAgentCallbackEvent) {
|
|
76
|
+
yield afterAgentCallbackEvent;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
);
|
|
61
80
|
} finally {
|
|
62
81
|
span.end();
|
|
63
82
|
}
|
|
@@ -70,8 +89,15 @@ class BaseAgent {
|
|
|
70
89
|
* @returns An AsyncGenerator that yields the events generated by the agent.
|
|
71
90
|
*/
|
|
72
91
|
async *runLive(parentContext) {
|
|
73
|
-
const span =
|
|
92
|
+
const span = tracer.startSpan(`invoke_agent ${this.name}`);
|
|
93
|
+
const ctx = trace.setSpan(context.active(), span);
|
|
74
94
|
try {
|
|
95
|
+
yield* runAsyncGeneratorWithOtelContext(
|
|
96
|
+
ctx,
|
|
97
|
+
this,
|
|
98
|
+
async function* () {
|
|
99
|
+
}
|
|
100
|
+
);
|
|
75
101
|
throw new Error("Live mode is not implemented yet.");
|
|
76
102
|
} finally {
|
|
77
103
|
span.end();
|
|
@@ -199,7 +225,7 @@ class BaseAgent {
|
|
|
199
225
|
function validateAgentName(name) {
|
|
200
226
|
if (!isIdentifier(name)) {
|
|
201
227
|
throw new Error(
|
|
202
|
-
`Found invalid agent name: "${name}". Agent name must be a valid identifier. It should start with a letter (a-z, A-Z) or an underscore (_), and can only contain letters, digits (0-9), and
|
|
228
|
+
`Found invalid agent name: "${name}". Agent name must be a valid identifier. It should start with a letter (a-z, A-Z) or an underscore (_), and can only contain letters, digits (0-9), underscores, and hyphens.`
|
|
203
229
|
);
|
|
204
230
|
}
|
|
205
231
|
if (name === "user") {
|
|
@@ -210,7 +236,7 @@ function validateAgentName(name) {
|
|
|
210
236
|
return name;
|
|
211
237
|
}
|
|
212
238
|
function isIdentifier(str) {
|
|
213
|
-
return /^[\p{ID_Start}$_][\p{ID_Continue}$_]*$/u.test(str);
|
|
239
|
+
return /^[\p{ID_Start}$_][\p{ID_Continue}$_-]*$/u.test(str);
|
|
214
240
|
}
|
|
215
241
|
function getRootAgent(rootAgent) {
|
|
216
242
|
while (rootAgent.parentAgent) {
|
|
@@ -7,7 +7,10 @@ import { createEventActions } from "../events/event_actions.js";
|
|
|
7
7
|
import { State } from "../sessions/state.js";
|
|
8
8
|
import { ReadonlyContext } from "./readonly_context.js";
|
|
9
9
|
class CallbackContext extends ReadonlyContext {
|
|
10
|
-
constructor({
|
|
10
|
+
constructor({
|
|
11
|
+
invocationContext,
|
|
12
|
+
eventActions
|
|
13
|
+
}) {
|
|
11
14
|
super(invocationContext);
|
|
12
15
|
this.eventActions = eventActions || createEventActions();
|
|
13
16
|
this._state = new State(
|
|
@@ -4,8 +4,16 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { cloneDeep } from "lodash-es";
|
|
7
|
-
import {
|
|
8
|
-
|
|
7
|
+
import {
|
|
8
|
+
createEvent,
|
|
9
|
+
getFunctionCalls,
|
|
10
|
+
getFunctionResponses
|
|
11
|
+
} from "../events/event.js";
|
|
12
|
+
import {
|
|
13
|
+
removeClientFunctionCallId,
|
|
14
|
+
REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
|
|
15
|
+
REQUEST_EUC_FUNCTION_CALL_NAME
|
|
16
|
+
} from "./functions.js";
|
|
9
17
|
function getContents(events, agentName, currentBranch) {
|
|
10
18
|
var _a, _b, _c;
|
|
11
19
|
const filteredEvents = [];
|
|
@@ -79,9 +87,11 @@ function convertForeignEvent(event) {
|
|
|
79
87
|
}
|
|
80
88
|
const content = {
|
|
81
89
|
role: "user",
|
|
82
|
-
parts: [
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
parts: [
|
|
91
|
+
{
|
|
92
|
+
text: "For context:"
|
|
93
|
+
}
|
|
94
|
+
]
|
|
85
95
|
};
|
|
86
96
|
for (const part of event.content.parts) {
|
|
87
97
|
if (part.text && !part.thought) {
|
|
@@ -183,10 +193,14 @@ function rearrangeEventsForLatestFunctionResponse(events) {
|
|
|
183
193
|
const functionCallIds = new Set(
|
|
184
194
|
functionCalls.map((fc) => fc.id).filter((id) => !!id)
|
|
185
195
|
);
|
|
186
|
-
const isSubset = Array.from(functionResponsesIds).every(
|
|
196
|
+
const isSubset = Array.from(functionResponsesIds).every(
|
|
197
|
+
(id) => functionCallIds.has(id)
|
|
198
|
+
);
|
|
187
199
|
if (!isSubset) {
|
|
188
200
|
throw new Error(
|
|
189
|
-
`Last response event should only contain the responses for the function calls in the same function call event. Function call ids found : ${Array.from(functionCallIds).join(
|
|
201
|
+
`Last response event should only contain the responses for the function calls in the same function call event. Function call ids found : ${Array.from(functionCallIds).join(
|
|
202
|
+
", "
|
|
203
|
+
)}, function response ids provided: ${Array.from(functionResponsesIds).join(", ")}`
|
|
190
204
|
);
|
|
191
205
|
}
|
|
192
206
|
functionResponsesIds = functionCallIds;
|
|
@@ -254,7 +268,9 @@ function rearrangeEventsForAsyncFunctionResponsesInHistory(events) {
|
|
|
254
268
|
const [responseIndex] = [...functionResponseEventsIndices];
|
|
255
269
|
resultEvents.push(events[responseIndex]);
|
|
256
270
|
} else {
|
|
257
|
-
const indicesArray = Array.from(functionResponseEventsIndices).sort(
|
|
271
|
+
const indicesArray = Array.from(functionResponseEventsIndices).sort(
|
|
272
|
+
(a, b) => a - b
|
|
273
|
+
);
|
|
258
274
|
const eventsToMerge = indicesArray.map((index) => events[index]);
|
|
259
275
|
resultEvents.push(mergeFunctionResponseEvents(eventsToMerge));
|
|
260
276
|
}
|
|
@@ -270,7 +286,7 @@ function safeStringify(obj) {
|
|
|
270
286
|
}
|
|
271
287
|
try {
|
|
272
288
|
return JSON.stringify(obj);
|
|
273
|
-
} catch (
|
|
289
|
+
} catch (_e) {
|
|
274
290
|
return String(obj);
|
|
275
291
|
}
|
|
276
292
|
}
|
|
@@ -10,6 +10,11 @@ import { mergeEventActions } from "../events/event_actions.js";
|
|
|
10
10
|
import { ToolContext } from "../tools/tool_context.js";
|
|
11
11
|
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
12
12
|
import { logger } from "../utils/logger.js";
|
|
13
|
+
import {
|
|
14
|
+
traceMergedToolCalls,
|
|
15
|
+
tracer,
|
|
16
|
+
traceToolCall
|
|
17
|
+
} from "../telemetry/tracing.js";
|
|
13
18
|
const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
|
|
14
19
|
const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
|
|
15
20
|
const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
|
|
@@ -123,12 +128,56 @@ function generateRequestConfirmationEvent({
|
|
|
123
128
|
parts,
|
|
124
129
|
role: functionResponseEvent.content.role
|
|
125
130
|
},
|
|
131
|
+
actions: functionResponseEvent.actions,
|
|
126
132
|
longRunningToolIds: Array.from(longRunningToolIds)
|
|
127
133
|
});
|
|
128
134
|
}
|
|
129
135
|
async function callToolAsync(tool, args, toolContext) {
|
|
130
|
-
|
|
131
|
-
|
|
136
|
+
return tracer.startActiveSpan(`execute_tool ${tool.name}`, async (span) => {
|
|
137
|
+
try {
|
|
138
|
+
logger.debug(`callToolAsync ${tool.name}`);
|
|
139
|
+
const result = await tool.runAsync({ args, toolContext });
|
|
140
|
+
traceToolCall({
|
|
141
|
+
tool,
|
|
142
|
+
args,
|
|
143
|
+
functionResponseEvent: buildResponseEvent(
|
|
144
|
+
tool,
|
|
145
|
+
result,
|
|
146
|
+
toolContext,
|
|
147
|
+
toolContext.invocationContext
|
|
148
|
+
)
|
|
149
|
+
});
|
|
150
|
+
return result;
|
|
151
|
+
} finally {
|
|
152
|
+
span.end();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function buildResponseEvent(tool, functionResult, toolContext, invocationContext) {
|
|
157
|
+
let responseResult;
|
|
158
|
+
if (typeof functionResult !== "object" || functionResult == null) {
|
|
159
|
+
responseResult = { result: functionResult };
|
|
160
|
+
} else {
|
|
161
|
+
responseResult = functionResult;
|
|
162
|
+
}
|
|
163
|
+
const partFunctionResponse = {
|
|
164
|
+
functionResponse: {
|
|
165
|
+
name: tool.name,
|
|
166
|
+
response: responseResult,
|
|
167
|
+
id: toolContext.functionCallId
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const content = {
|
|
171
|
+
role: "user",
|
|
172
|
+
parts: [partFunctionResponse]
|
|
173
|
+
};
|
|
174
|
+
return createEvent({
|
|
175
|
+
invocationId: invocationContext.invocationId,
|
|
176
|
+
author: invocationContext.agent.name,
|
|
177
|
+
content,
|
|
178
|
+
actions: toolContext.actions,
|
|
179
|
+
branch: invocationContext.branch
|
|
180
|
+
});
|
|
132
181
|
}
|
|
133
182
|
async function handleFunctionCallsAsync({
|
|
134
183
|
invocationContext,
|
|
@@ -169,14 +218,12 @@ async function handleFunctionCallList({
|
|
|
169
218
|
if (toolConfirmationDict && functionCall.id) {
|
|
170
219
|
toolConfirmation = toolConfirmationDict[functionCall.id];
|
|
171
220
|
}
|
|
172
|
-
const { tool, toolContext } = getToolAndContext(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
);
|
|
221
|
+
const { tool, toolContext } = getToolAndContext({
|
|
222
|
+
invocationContext,
|
|
223
|
+
functionCall,
|
|
224
|
+
toolsDict,
|
|
225
|
+
toolConfirmation
|
|
226
|
+
});
|
|
180
227
|
logger.debug(`execute_tool ${tool.name}`);
|
|
181
228
|
const functionArgs = (_a = functionCall.args) != null ? _a : {};
|
|
182
229
|
let functionResponse = null;
|
|
@@ -200,21 +247,15 @@ async function handleFunctionCallList({
|
|
|
200
247
|
}
|
|
201
248
|
if (functionResponse == null) {
|
|
202
249
|
try {
|
|
203
|
-
functionResponse = await callToolAsync(
|
|
204
|
-
tool,
|
|
205
|
-
functionArgs,
|
|
206
|
-
toolContext
|
|
207
|
-
);
|
|
250
|
+
functionResponse = await callToolAsync(tool, functionArgs, toolContext);
|
|
208
251
|
} catch (e) {
|
|
209
252
|
if (e instanceof Error) {
|
|
210
|
-
const onToolErrorResponse = await invocationContext.pluginManager.runOnToolErrorCallback(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
);
|
|
253
|
+
const onToolErrorResponse = await invocationContext.pluginManager.runOnToolErrorCallback({
|
|
254
|
+
tool,
|
|
255
|
+
toolArgs: functionArgs,
|
|
256
|
+
toolContext,
|
|
257
|
+
error: e
|
|
258
|
+
});
|
|
218
259
|
if (onToolErrorResponse) {
|
|
219
260
|
functionResponse = onToolErrorResponse;
|
|
220
261
|
} else {
|
|
@@ -278,12 +319,24 @@ async function handleFunctionCallList({
|
|
|
278
319
|
if (!functionResponseEvents.length) {
|
|
279
320
|
return null;
|
|
280
321
|
}
|
|
281
|
-
const mergedEvent = mergeParallelFunctionResponseEvents(
|
|
322
|
+
const mergedEvent = mergeParallelFunctionResponseEvents(
|
|
323
|
+
functionResponseEvents
|
|
324
|
+
);
|
|
282
325
|
if (functionResponseEvents.length > 1) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
326
|
+
tracer.startActiveSpan("execute_tool (merged)", (span) => {
|
|
327
|
+
try {
|
|
328
|
+
logger.debug("execute_tool (merged)");
|
|
329
|
+
logger.debug("traceMergedToolCalls", {
|
|
330
|
+
responseEventId: mergedEvent.id,
|
|
331
|
+
functionResponseEvent: mergedEvent.id
|
|
332
|
+
});
|
|
333
|
+
traceMergedToolCalls({
|
|
334
|
+
responseEventId: mergedEvent.id,
|
|
335
|
+
functionResponseEvent: mergedEvent
|
|
336
|
+
});
|
|
337
|
+
} finally {
|
|
338
|
+
span.end();
|
|
339
|
+
}
|
|
287
340
|
});
|
|
288
341
|
}
|
|
289
342
|
return mergedEvent;
|
|
@@ -321,7 +374,9 @@ function mergeParallelFunctionResponseEvents(functionResponseEvents) {
|
|
|
321
374
|
}
|
|
322
375
|
}
|
|
323
376
|
const baseEvent = functionResponseEvents[0];
|
|
324
|
-
const actionsList = functionResponseEvents.map(
|
|
377
|
+
const actionsList = functionResponseEvents.map(
|
|
378
|
+
(event) => event.actions || {}
|
|
379
|
+
);
|
|
325
380
|
const mergedActions = mergeEventActions(actionsList);
|
|
326
381
|
return createEvent({
|
|
327
382
|
author: baseEvent.author,
|