@google/adk 0.3.0 → 0.5.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_event.js +290 -0
- package/dist/cjs/a2a/event_converter_utils.js +201 -0
- package/dist/cjs/a2a/executor_context.js +53 -0
- package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
- package/dist/cjs/a2a/part_converter_utils.js +212 -0
- package/dist/cjs/agents/active_streaming_tool.js +1 -1
- package/dist/cjs/agents/base_agent.js +6 -6
- package/dist/cjs/agents/content_processor_utils.js +1 -1
- package/dist/cjs/{tools/tool_context.js → agents/context.js} +71 -16
- package/dist/cjs/agents/functions.js +4 -3
- 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 +76 -711
- package/dist/cjs/agents/loop_agent.js +1 -1
- package/dist/cjs/agents/parallel_agent.js +1 -1
- package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
- package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +1 -1
- package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
- package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
- package/dist/cjs/agents/processors/content_request_processor.js +66 -0
- package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
- package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
- 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 +127 -48
- package/dist/cjs/artifacts/in_memory_artifact_service.js +54 -6
- package/dist/cjs/artifacts/registry.js +55 -0
- package/dist/cjs/auth/auth_credential.js +1 -1
- package/dist/cjs/auth/auth_handler.js +1 -1
- 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 +1 -1
- package/dist/cjs/code_executors/built_in_code_executor.js +1 -1
- package/dist/cjs/code_executors/code_execution_utils.js +1 -1
- package/dist/cjs/code_executors/code_executor_context.js +1 -1
- package/dist/cjs/common.js +25 -8
- package/dist/cjs/events/event.js +33 -4
- 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 +24 -17
- package/dist/cjs/index.js.map +4 -4
- 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 +1 -1
- 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 +1 -1
- package/dist/cjs/models/base_llm_connection.js +1 -1
- package/dist/cjs/models/gemini_llm_connection.js +1 -1
- package/dist/cjs/models/google_llm.js +70 -51
- package/dist/cjs/models/llm_request.js +1 -1
- package/dist/cjs/models/llm_response.js +3 -1
- package/dist/cjs/models/registry.js +3 -1
- package/dist/cjs/plugins/base_plugin.js +2 -2
- package/dist/cjs/plugins/logging_plugin.js +1 -1
- package/dist/cjs/plugins/plugin_manager.js +1 -1
- package/dist/cjs/plugins/security_plugin.js +1 -1
- package/dist/cjs/runner/in_memory_runner.js +1 -1
- package/dist/cjs/runner/runner.js +33 -2
- package/dist/cjs/sessions/base_session_service.js +53 -3
- package/dist/cjs/sessions/database_session_service.js +367 -0
- package/dist/cjs/sessions/db/operations.js +126 -0
- package/dist/cjs/sessions/db/schema.js +204 -0
- package/dist/cjs/sessions/in_memory_session_service.js +24 -22
- 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 +1 -1
- package/dist/cjs/telemetry/setup.js +1 -1
- package/dist/cjs/telemetry/tracing.js +1 -1
- package/dist/cjs/tools/agent_tool.js +1 -1
- package/dist/cjs/tools/base_tool.js +4 -1
- package/dist/cjs/tools/base_toolset.js +14 -4
- package/dist/cjs/tools/exit_loop_tool.js +63 -0
- package/dist/cjs/tools/forwarding_artifact_service.js +17 -1
- package/dist/cjs/tools/function_tool.js +1 -1
- package/dist/cjs/tools/google_search_tool.js +1 -1
- package/dist/cjs/tools/long_running_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_session_manager.js +1 -1
- package/dist/cjs/tools/mcp/mcp_tool.js +1 -1
- package/dist/cjs/tools/mcp/mcp_toolset.js +10 -6
- package/dist/cjs/tools/tool_confirmation.js +1 -1
- package/dist/cjs/utils/client_labels.js +1 -1
- package/dist/cjs/utils/env_aware_utils.js +10 -1
- package/dist/cjs/utils/gemini_schema_util.js +1 -1
- package/dist/cjs/utils/logger.js +62 -55
- 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 +1 -1
- package/dist/cjs/utils/variant_utils.js +3 -9
- package/dist/cjs/version.js +2 -2
- package/dist/esm/a2a/a2a_event.js +243 -0
- package/dist/esm/a2a/event_converter_utils.js +187 -0
- package/dist/esm/a2a/executor_context.js +23 -0
- package/dist/esm/a2a/metadata_converter_utils.js +90 -0
- package/dist/esm/a2a/part_converter_utils.js +175 -0
- package/dist/esm/agents/base_agent.js +5 -5
- package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/esm/agents/functions.js +3 -2
- package/dist/esm/agents/llm_agent.js +69 -720
- package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
- package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
- package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
- package/dist/esm/agents/processors/content_request_processor.js +38 -0
- package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
- package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
- 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 +126 -47
- package/dist/esm/artifacts/in_memory_artifact_service.js +51 -4
- package/dist/esm/artifacts/registry.js +28 -0
- package/dist/esm/common.js +20 -10
- package/dist/esm/events/event.js +29 -2
- package/dist/esm/events/event_actions.js +1 -1
- package/dist/esm/events/structured_events.js +74 -0
- package/dist/esm/index.js +24 -17
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/models/apigee_llm.js +152 -0
- package/dist/esm/models/google_llm.js +67 -49
- package/dist/esm/models/llm_response.js +2 -0
- package/dist/esm/models/registry.js +2 -0
- package/dist/esm/plugins/base_plugin.js +1 -1
- package/dist/esm/runner/runner.js +32 -1
- package/dist/esm/sessions/base_session_service.js +49 -1
- package/dist/esm/sessions/database_session_service.js +353 -0
- package/dist/esm/sessions/db/operations.js +111 -0
- package/dist/esm/sessions/db/schema.js +172 -0
- package/dist/esm/sessions/in_memory_session_service.js +23 -21
- package/dist/esm/sessions/registry.js +25 -0
- package/dist/esm/tools/base_tool.js +3 -0
- package/dist/esm/tools/base_toolset.js +11 -2
- package/dist/esm/tools/exit_loop_tool.js +32 -0
- package/dist/esm/tools/forwarding_artifact_service.js +16 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
- package/dist/esm/utils/env_aware_utils.js +8 -0
- package/dist/esm/utils/logger.js +51 -54
- package/dist/esm/utils/object_notation_utils.js +47 -0
- package/dist/esm/utils/variant_utils.js +1 -7
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +122 -0
- package/dist/types/a2a/event_converter_utils.d.ts +20 -0
- package/dist/types/a2a/executor_context.d.ts +33 -0
- package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
- package/dist/types/a2a/part_converter_utils.d.ts +48 -0
- package/dist/types/agents/base_agent.d.ts +2 -2
- package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
- package/dist/types/agents/llm_agent.d.ts +19 -42
- package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
- package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
- package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
- 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 +5 -2
- package/dist/types/artifacts/registry.d.ts +7 -0
- package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
- package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
- package/dist/types/common.d.ts +15 -6
- 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 +5 -1
- package/dist/types/models/apigee_llm.d.ts +59 -0
- package/dist/types/models/google_llm.d.ts +5 -2
- package/dist/types/models/llm_response.d.ts +5 -1
- package/dist/types/plugins/base_plugin.d.ts +12 -13
- package/dist/types/plugins/logging_plugin.d.ts +9 -10
- package/dist/types/plugins/plugin_manager.d.ts +9 -10
- package/dist/types/plugins/security_plugin.d.ts +2 -2
- package/dist/types/runner/runner.d.ts +15 -0
- package/dist/types/sessions/base_session_service.d.ts +20 -0
- package/dist/types/sessions/database_session_service.d.ts +32 -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 +4 -1
- package/dist/types/sessions/registry.d.ts +7 -0
- package/dist/types/tools/base_tool.d.ts +3 -3
- package/dist/types/tools/base_toolset.d.ts +12 -3
- package/dist/types/tools/exit_loop_tool.d.ts +24 -0
- package/dist/types/tools/forwarding_artifact_service.d.ts +5 -3
- package/dist/types/tools/function_tool.d.ts +2 -2
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
- package/dist/types/utils/env_aware_utils.d.ts +7 -0
- package/dist/types/utils/logger.d.ts +5 -9
- package/dist/types/utils/object_notation_utils.d.ts +21 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +243 -0
- package/dist/web/a2a/event_converter_utils.js +201 -0
- package/dist/web/a2a/executor_context.js +23 -0
- package/dist/web/a2a/metadata_converter_utils.js +107 -0
- package/dist/web/a2a/part_converter_utils.js +175 -0
- package/dist/web/agents/base_agent.js +5 -5
- package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/web/agents/functions.js +3 -2
- package/dist/web/agents/llm_agent.js +90 -717
- package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
- package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
- package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
- package/dist/web/agents/processors/content_request_processor.js +56 -0
- package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
- package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
- 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 +123 -46
- package/dist/web/artifacts/in_memory_artifact_service.js +51 -4
- package/dist/web/artifacts/registry.js +28 -0
- package/dist/web/common.js +20 -10
- package/dist/web/events/event.js +29 -2
- package/dist/web/events/event_actions.js +1 -1
- package/dist/web/events/structured_events.js +74 -0
- package/dist/web/index.js +7 -2
- package/dist/web/index.js.map +4 -4
- package/dist/web/models/apigee_llm.js +219 -0
- package/dist/web/models/google_llm.js +67 -46
- package/dist/web/models/llm_response.js +2 -0
- package/dist/web/models/registry.js +2 -0
- package/dist/web/plugins/base_plugin.js +1 -1
- package/dist/web/runner/runner.js +34 -1
- package/dist/web/sessions/base_session_service.js +49 -1
- package/dist/web/sessions/database_session_service.js +371 -0
- package/dist/web/sessions/db/operations.js +111 -0
- package/dist/web/sessions/db/schema.js +172 -0
- package/dist/web/sessions/in_memory_session_service.js +23 -21
- package/dist/web/sessions/registry.js +25 -0
- package/dist/web/tools/base_tool.js +3 -0
- package/dist/web/tools/base_toolset.js +11 -2
- package/dist/web/tools/exit_loop_tool.js +32 -0
- package/dist/web/tools/forwarding_artifact_service.js +16 -0
- package/dist/web/tools/mcp/mcp_toolset.js +27 -5
- package/dist/web/utils/env_aware_utils.js +8 -0
- package/dist/web/utils/logger.js +51 -54
- package/dist/web/utils/object_notation_utils.js +47 -0
- package/dist/web/utils/variant_utils.js +1 -7
- package/dist/web/version.js +1 -1
- package/package.json +13 -3
- package/dist/cjs/agents/callback_context.js +0 -101
- package/dist/esm/agents/callback_context.js +0 -71
- package/dist/types/agents/callback_context.d.ts +0 -42
- package/dist/web/agents/callback_context.js +0 -71
- /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
- /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
createModelContent,
|
|
8
|
+
createUserContent
|
|
9
|
+
} from "@google/genai";
|
|
10
|
+
import { createEvent } from "../events/event.js";
|
|
11
|
+
import { createEventActions } from "../events/event_actions.js";
|
|
12
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
13
|
+
import {
|
|
14
|
+
getEventMetadata,
|
|
15
|
+
getFailedTaskStatusUpdateEventError,
|
|
16
|
+
isFailedTaskStatusUpdateEvent,
|
|
17
|
+
isInputRequiredTaskStatusUpdateEvent,
|
|
18
|
+
isMessage,
|
|
19
|
+
isTask,
|
|
20
|
+
isTaskArtifactUpdateEvent,
|
|
21
|
+
isTaskStatusUpdateEvent,
|
|
22
|
+
isTerminalTaskStatusUpdateEvent,
|
|
23
|
+
MessageRole
|
|
24
|
+
} from "./a2a_event.js";
|
|
25
|
+
import {
|
|
26
|
+
A2AMetadataKeys,
|
|
27
|
+
getA2AEventMetadata
|
|
28
|
+
} from "./metadata_converter_utils.js";
|
|
29
|
+
import { toA2AParts, toGenAIPart, toGenAIParts } from "./part_converter_utils.js";
|
|
30
|
+
function toA2AMessage(event, {
|
|
31
|
+
appName,
|
|
32
|
+
userId,
|
|
33
|
+
sessionId
|
|
34
|
+
}) {
|
|
35
|
+
var _a;
|
|
36
|
+
return {
|
|
37
|
+
kind: "message",
|
|
38
|
+
messageId: randomUUID(),
|
|
39
|
+
role: event.author === MessageRole.USER ? MessageRole.USER : MessageRole.AGENT,
|
|
40
|
+
parts: toA2AParts(((_a = event.content) == null ? void 0 : _a.parts) || [], event.longRunningToolIds),
|
|
41
|
+
metadata: getA2AEventMetadata(event, { appName, userId, sessionId })
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function toAdkEvent(event, invocationId, agentName) {
|
|
45
|
+
if (isMessage(event)) {
|
|
46
|
+
return messageToAdkEvent(event, invocationId, agentName);
|
|
47
|
+
}
|
|
48
|
+
if (isTask(event)) {
|
|
49
|
+
return taskToAdkEvent(event, invocationId, agentName);
|
|
50
|
+
}
|
|
51
|
+
if (isTaskArtifactUpdateEvent(event)) {
|
|
52
|
+
return artifactUpdateToAdkEvent(event, invocationId, agentName);
|
|
53
|
+
}
|
|
54
|
+
if (isTaskStatusUpdateEvent(event)) {
|
|
55
|
+
return event.final ? finalTaskStatusUpdateToAdkEvent(event, invocationId, agentName) : taskStatusUpdateToAdkEvent(event, invocationId, agentName);
|
|
56
|
+
}
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
function messageToAdkEvent(msg, invocationId, agentName, parentEvent) {
|
|
60
|
+
const parts = toGenAIParts(msg.parts);
|
|
61
|
+
return {
|
|
62
|
+
...createAdkEventFromMetadata(parentEvent || msg),
|
|
63
|
+
invocationId,
|
|
64
|
+
author: msg.role === MessageRole.USER ? MessageRole.USER : agentName,
|
|
65
|
+
content: msg.role === MessageRole.USER ? createUserContent(parts) : createModelContent(parts),
|
|
66
|
+
turnComplete: true,
|
|
67
|
+
partial: false
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function artifactUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
71
|
+
var _a;
|
|
72
|
+
const partsToConvert = ((_a = a2aEvent.artifact) == null ? void 0 : _a.parts) || [];
|
|
73
|
+
if (partsToConvert.length === 0) {
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
const partial = !!getEventMetadata(a2aEvent)[A2AMetadataKeys.PARTIAL] || a2aEvent.append || !a2aEvent.lastChunk;
|
|
77
|
+
return {
|
|
78
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
79
|
+
invocationId,
|
|
80
|
+
author: agentName,
|
|
81
|
+
content: createModelContent(toGenAIParts(partsToConvert)),
|
|
82
|
+
longRunningToolIds: getLongRunningToolIDs(partsToConvert),
|
|
83
|
+
partial
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function finalTaskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
87
|
+
var _a;
|
|
88
|
+
const partsToConvert = ((_a = a2aEvent.status.message) == null ? void 0 : _a.parts) || [];
|
|
89
|
+
if (partsToConvert.length === 0) {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
const parts = toGenAIParts(partsToConvert);
|
|
93
|
+
const isFailedTask = isFailedTaskStatusUpdateEvent(a2aEvent);
|
|
94
|
+
const hasContent = !isFailedTask && parts.length > 0;
|
|
95
|
+
return {
|
|
96
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
97
|
+
invocationId,
|
|
98
|
+
author: agentName,
|
|
99
|
+
errorMessage: isFailedTask ? getFailedTaskStatusUpdateEventError(a2aEvent) : void 0,
|
|
100
|
+
content: hasContent ? createModelContent(parts) : void 0,
|
|
101
|
+
longRunningToolIds: getLongRunningToolIDs(partsToConvert),
|
|
102
|
+
turnComplete: true
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function taskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
106
|
+
const msg = a2aEvent.status.message;
|
|
107
|
+
if (!msg) {
|
|
108
|
+
return void 0;
|
|
109
|
+
}
|
|
110
|
+
const parts = toGenAIParts(msg.parts);
|
|
111
|
+
return {
|
|
112
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
113
|
+
invocationId,
|
|
114
|
+
author: agentName,
|
|
115
|
+
content: createModelContent(parts),
|
|
116
|
+
turnComplete: false,
|
|
117
|
+
partial: true
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function taskToAdkEvent(a2aTask, invocationId, agentName) {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
const parts = [];
|
|
123
|
+
const longRunningToolIds = [];
|
|
124
|
+
if (a2aTask.artifacts) {
|
|
125
|
+
for (const artifact of a2aTask.artifacts) {
|
|
126
|
+
if (((_a = artifact.parts) == null ? void 0 : _a.length) > 0) {
|
|
127
|
+
const artifactParts = toGenAIParts(artifact.parts);
|
|
128
|
+
parts.push(...artifactParts);
|
|
129
|
+
longRunningToolIds.push(...getLongRunningToolIDs(artifact.parts));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if ((_b = a2aTask.status) == null ? void 0 : _b.message) {
|
|
134
|
+
const a2aParts = a2aTask.status.message.parts;
|
|
135
|
+
const genAIParts = toGenAIParts(a2aParts);
|
|
136
|
+
parts.push(...genAIParts);
|
|
137
|
+
longRunningToolIds.push(...getLongRunningToolIDs(a2aParts));
|
|
138
|
+
}
|
|
139
|
+
const isTerminal = isTerminalTaskStatusUpdateEvent(a2aTask) || isInputRequiredTaskStatusUpdateEvent(a2aTask);
|
|
140
|
+
const isFailed = isFailedTaskStatusUpdateEvent(a2aTask);
|
|
141
|
+
if (parts.length === 0 && !isTerminal) {
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
...createAdkEventFromMetadata(a2aTask),
|
|
146
|
+
invocationId,
|
|
147
|
+
author: agentName,
|
|
148
|
+
content: isFailed ? void 0 : createModelContent(parts),
|
|
149
|
+
errorMessage: isFailed ? getFailedTaskStatusUpdateEventError(a2aTask) : void 0,
|
|
150
|
+
longRunningToolIds,
|
|
151
|
+
turnComplete: isTerminal
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function createAdkEventFromMetadata(a2aEvent) {
|
|
155
|
+
const metadata = a2aEvent.metadata || {};
|
|
156
|
+
return createEvent({
|
|
157
|
+
branch: metadata[A2AMetadataKeys.BRANCH],
|
|
158
|
+
author: metadata[A2AMetadataKeys.AUTHOR],
|
|
159
|
+
partial: metadata[A2AMetadataKeys.PARTIAL],
|
|
160
|
+
errorCode: metadata[A2AMetadataKeys.ERROR_CODE],
|
|
161
|
+
errorMessage: metadata[A2AMetadataKeys.ERROR_MESSAGE],
|
|
162
|
+
citationMetadata: metadata[A2AMetadataKeys.CITATION_METADATA],
|
|
163
|
+
groundingMetadata: metadata[A2AMetadataKeys.GROUNDING_METADATA],
|
|
164
|
+
usageMetadata: metadata[A2AMetadataKeys.USAGE_METADATA],
|
|
165
|
+
customMetadata: metadata[A2AMetadataKeys.CUSTOM_METADATA],
|
|
166
|
+
actions: createEventActions({
|
|
167
|
+
escalate: !!metadata[A2AMetadataKeys.ESCALATE],
|
|
168
|
+
transferToAgent: metadata[A2AMetadataKeys.TRANSFER_TO_AGENT]
|
|
169
|
+
})
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function getLongRunningToolIDs(parts) {
|
|
173
|
+
const ids = [];
|
|
174
|
+
for (const a2aPart of parts) {
|
|
175
|
+
if (a2aPart.metadata && a2aPart.metadata[A2AMetadataKeys.IS_LONG_RUNNING]) {
|
|
176
|
+
const genAIPart = toGenAIPart(a2aPart);
|
|
177
|
+
if (genAIPart.functionCall && genAIPart.functionCall.id) {
|
|
178
|
+
ids.push(genAIPart.functionCall.id);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return ids;
|
|
183
|
+
}
|
|
184
|
+
export {
|
|
185
|
+
toA2AMessage,
|
|
186
|
+
toAdkEvent
|
|
187
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
function createExecutorContext({
|
|
7
|
+
session,
|
|
8
|
+
userContent,
|
|
9
|
+
requestContext
|
|
10
|
+
}) {
|
|
11
|
+
return {
|
|
12
|
+
userId: session.userId,
|
|
13
|
+
sessionId: session.id,
|
|
14
|
+
agentName: session.appName,
|
|
15
|
+
readonlyState: session.state,
|
|
16
|
+
events: session.events,
|
|
17
|
+
userContent,
|
|
18
|
+
requestContext
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
createExecutorContext
|
|
23
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { isTask } from "./a2a_event.js";
|
|
7
|
+
const ADK_METADATA_KEY_PREFIX = "adk_";
|
|
8
|
+
const A2A_METADATA_KEY_PREFIX = "a2a:";
|
|
9
|
+
var A2AMetadataKeys = ((A2AMetadataKeys2) => {
|
|
10
|
+
A2AMetadataKeys2["APP_NAME"] = `${ADK_METADATA_KEY_PREFIX}app_name`;
|
|
11
|
+
A2AMetadataKeys2["USER_ID"] = `${ADK_METADATA_KEY_PREFIX}user_id`;
|
|
12
|
+
A2AMetadataKeys2["SESSION_ID"] = `${ADK_METADATA_KEY_PREFIX}session_id`;
|
|
13
|
+
A2AMetadataKeys2["INVOCATION_ID"] = `${ADK_METADATA_KEY_PREFIX}invocation_id`;
|
|
14
|
+
A2AMetadataKeys2["AUTHOR"] = `${ADK_METADATA_KEY_PREFIX}author`;
|
|
15
|
+
A2AMetadataKeys2["BRANCH"] = `${ADK_METADATA_KEY_PREFIX}branch`;
|
|
16
|
+
A2AMetadataKeys2["DATA_PART_TYPE"] = `${ADK_METADATA_KEY_PREFIX}type`;
|
|
17
|
+
A2AMetadataKeys2["PARTIAL"] = `${ADK_METADATA_KEY_PREFIX}partial`;
|
|
18
|
+
A2AMetadataKeys2["ESCALATE"] = `${ADK_METADATA_KEY_PREFIX}escalate`;
|
|
19
|
+
A2AMetadataKeys2["TRANSFER_TO_AGENT"] = `${ADK_METADATA_KEY_PREFIX}transfer_to_agent`;
|
|
20
|
+
A2AMetadataKeys2["IS_LONG_RUNNING"] = `${ADK_METADATA_KEY_PREFIX}is_long_running`;
|
|
21
|
+
A2AMetadataKeys2["THOUGHT"] = `${ADK_METADATA_KEY_PREFIX}thought`;
|
|
22
|
+
A2AMetadataKeys2["ERROR_CODE"] = `${ADK_METADATA_KEY_PREFIX}error_code`;
|
|
23
|
+
A2AMetadataKeys2["ERROR_MESSAGE"] = `${ADK_METADATA_KEY_PREFIX}error_message`;
|
|
24
|
+
A2AMetadataKeys2["CITATION_METADATA"] = `${ADK_METADATA_KEY_PREFIX}citation_metadata`;
|
|
25
|
+
A2AMetadataKeys2["GROUNDING_METADATA"] = `${ADK_METADATA_KEY_PREFIX}grounding_metadata`;
|
|
26
|
+
A2AMetadataKeys2["USAGE_METADATA"] = `${ADK_METADATA_KEY_PREFIX}usage_metadata`;
|
|
27
|
+
A2AMetadataKeys2["CUSTOM_METADATA"] = `${ADK_METADATA_KEY_PREFIX}custom_metadata`;
|
|
28
|
+
return A2AMetadataKeys2;
|
|
29
|
+
})(A2AMetadataKeys || {});
|
|
30
|
+
var AdkMetadataKeys = ((AdkMetadataKeys2) => {
|
|
31
|
+
AdkMetadataKeys2["TASK_ID"] = `${A2A_METADATA_KEY_PREFIX}task_id`;
|
|
32
|
+
AdkMetadataKeys2["CONTEXT_ID"] = `${A2A_METADATA_KEY_PREFIX}context_id`;
|
|
33
|
+
return AdkMetadataKeys2;
|
|
34
|
+
})(AdkMetadataKeys || {});
|
|
35
|
+
function getAdkEventMetadata(a2aEvent) {
|
|
36
|
+
return {
|
|
37
|
+
[AdkMetadataKeys.TASK_ID]: isTask(a2aEvent) ? a2aEvent.id : a2aEvent.taskId,
|
|
38
|
+
[AdkMetadataKeys.CONTEXT_ID]: a2aEvent.contextId
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function getA2AEventMetadata(adkEvent, {
|
|
42
|
+
appName,
|
|
43
|
+
userId,
|
|
44
|
+
sessionId
|
|
45
|
+
}) {
|
|
46
|
+
return {
|
|
47
|
+
...getA2AEventMetadataFromActions(adkEvent.actions),
|
|
48
|
+
...getA2ASessionMetadata({
|
|
49
|
+
appName,
|
|
50
|
+
userId,
|
|
51
|
+
sessionId
|
|
52
|
+
}),
|
|
53
|
+
[A2AMetadataKeys.INVOCATION_ID]: adkEvent.invocationId,
|
|
54
|
+
[A2AMetadataKeys.AUTHOR]: adkEvent.author,
|
|
55
|
+
[A2AMetadataKeys.BRANCH]: adkEvent.branch,
|
|
56
|
+
[A2AMetadataKeys.ERROR_CODE]: adkEvent.errorMessage,
|
|
57
|
+
[A2AMetadataKeys.ERROR_MESSAGE]: adkEvent.errorMessage,
|
|
58
|
+
[A2AMetadataKeys.CITATION_METADATA]: adkEvent.citationMetadata,
|
|
59
|
+
[A2AMetadataKeys.GROUNDING_METADATA]: adkEvent.groundingMetadata,
|
|
60
|
+
[A2AMetadataKeys.USAGE_METADATA]: adkEvent.usageMetadata,
|
|
61
|
+
[A2AMetadataKeys.CUSTOM_METADATA]: adkEvent.customMetadata,
|
|
62
|
+
[A2AMetadataKeys.PARTIAL]: adkEvent.partial,
|
|
63
|
+
[A2AMetadataKeys.IS_LONG_RUNNING]: (adkEvent.longRunningToolIds || []).length > 0
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function getA2ASessionMetadata({
|
|
67
|
+
appName,
|
|
68
|
+
userId,
|
|
69
|
+
sessionId
|
|
70
|
+
}) {
|
|
71
|
+
return {
|
|
72
|
+
[A2AMetadataKeys.APP_NAME]: appName,
|
|
73
|
+
[A2AMetadataKeys.USER_ID]: userId,
|
|
74
|
+
[A2AMetadataKeys.SESSION_ID]: sessionId
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function getA2AEventMetadataFromActions(actions) {
|
|
78
|
+
return {
|
|
79
|
+
[A2AMetadataKeys.ESCALATE]: actions.escalate,
|
|
80
|
+
[A2AMetadataKeys.TRANSFER_TO_AGENT]: actions.transferToAgent
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
A2AMetadataKeys,
|
|
85
|
+
AdkMetadataKeys,
|
|
86
|
+
getA2AEventMetadata,
|
|
87
|
+
getA2AEventMetadataFromActions,
|
|
88
|
+
getA2ASessionMetadata,
|
|
89
|
+
getAdkEventMetadata
|
|
90
|
+
};
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
createModelContent,
|
|
8
|
+
createUserContent
|
|
9
|
+
} from "@google/genai";
|
|
10
|
+
import { A2AMetadataKeys } from "./metadata_converter_utils.js";
|
|
11
|
+
var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
|
|
12
|
+
DataPartType2["FUNCTION_CALL"] = "function_call";
|
|
13
|
+
DataPartType2["FUNCTION_RESPONSE"] = "function_response";
|
|
14
|
+
DataPartType2["CODE_EXEC_RESULT"] = "code_execution_result";
|
|
15
|
+
DataPartType2["CODE_EXECUTABLE_CODE"] = "executable_code";
|
|
16
|
+
return DataPartType2;
|
|
17
|
+
})(DataPartType || {});
|
|
18
|
+
function toA2AParts(parts = [], longRunningToolIDs = []) {
|
|
19
|
+
return parts.map((part) => toA2APart(part, longRunningToolIDs));
|
|
20
|
+
}
|
|
21
|
+
function toA2APart(part, longRunningToolIDs) {
|
|
22
|
+
if (part.text !== void 0 && part.text !== null) {
|
|
23
|
+
return toA2ATextPart(part);
|
|
24
|
+
}
|
|
25
|
+
if (part.inlineData || part.fileData) {
|
|
26
|
+
return toA2AFilePart(part);
|
|
27
|
+
}
|
|
28
|
+
return toA2ADataPart(part, longRunningToolIDs);
|
|
29
|
+
}
|
|
30
|
+
function toA2ATextPart(part) {
|
|
31
|
+
const a2aPart = { kind: "text", text: part.text || "" };
|
|
32
|
+
if (part.thought) {
|
|
33
|
+
a2aPart.metadata = {
|
|
34
|
+
[A2AMetadataKeys.THOUGHT]: true
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return a2aPart;
|
|
38
|
+
}
|
|
39
|
+
function toA2AFilePart(part) {
|
|
40
|
+
if (part.fileData) {
|
|
41
|
+
return {
|
|
42
|
+
kind: "file",
|
|
43
|
+
file: {
|
|
44
|
+
uri: part.fileData.fileUri || "",
|
|
45
|
+
mimeType: part.fileData.mimeType
|
|
46
|
+
},
|
|
47
|
+
metadata: {}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (part.inlineData) {
|
|
51
|
+
return {
|
|
52
|
+
kind: "file",
|
|
53
|
+
file: {
|
|
54
|
+
bytes: part.inlineData.data || "",
|
|
55
|
+
mimeType: part.inlineData.mimeType
|
|
56
|
+
},
|
|
57
|
+
metadata: {}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
throw new Error(`Not a file part: ${JSON.stringify(part)}`);
|
|
61
|
+
}
|
|
62
|
+
function toA2ADataPart(part, longRunningToolIDs = []) {
|
|
63
|
+
let dataPartType;
|
|
64
|
+
let data;
|
|
65
|
+
if (part.functionCall) {
|
|
66
|
+
dataPartType = "function_call" /* FUNCTION_CALL */;
|
|
67
|
+
data = part.functionCall;
|
|
68
|
+
} else if (part.functionResponse) {
|
|
69
|
+
dataPartType = "function_response" /* FUNCTION_RESPONSE */;
|
|
70
|
+
data = part.functionResponse;
|
|
71
|
+
} else if (part.executableCode) {
|
|
72
|
+
dataPartType = "executable_code" /* CODE_EXECUTABLE_CODE */;
|
|
73
|
+
data = part.executableCode;
|
|
74
|
+
} else if (part.codeExecutionResult) {
|
|
75
|
+
dataPartType = "code_execution_result" /* CODE_EXEC_RESULT */;
|
|
76
|
+
data = part.codeExecutionResult;
|
|
77
|
+
} else {
|
|
78
|
+
throw new Error(`Unknown part type: ${JSON.stringify(part)}`);
|
|
79
|
+
}
|
|
80
|
+
const metadata = {
|
|
81
|
+
[A2AMetadataKeys.DATA_PART_TYPE]: dataPartType
|
|
82
|
+
};
|
|
83
|
+
if (part.functionCall && part.functionCall.id && longRunningToolIDs.includes(part.functionCall.id)) {
|
|
84
|
+
metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
|
|
85
|
+
}
|
|
86
|
+
if (part.functionResponse && part.functionResponse.id && longRunningToolIDs.includes(part.functionResponse.id)) {
|
|
87
|
+
metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
kind: "data",
|
|
91
|
+
data,
|
|
92
|
+
metadata
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function toGenAIContent(a2aMessage) {
|
|
96
|
+
const parts = toGenAIParts(a2aMessage.parts);
|
|
97
|
+
return a2aMessage.role === "user" ? createUserContent(parts) : createModelContent(parts);
|
|
98
|
+
}
|
|
99
|
+
function toGenAIParts(a2aParts) {
|
|
100
|
+
return a2aParts.map((a2aPart) => toGenAIPart(a2aPart));
|
|
101
|
+
}
|
|
102
|
+
function toGenAIPart(a2aPart) {
|
|
103
|
+
if (a2aPart.kind === "text") {
|
|
104
|
+
return toGenAIPartText(a2aPart);
|
|
105
|
+
}
|
|
106
|
+
if (a2aPart.kind === "file") {
|
|
107
|
+
return toGenAIPartFile(a2aPart);
|
|
108
|
+
}
|
|
109
|
+
if (a2aPart.kind === "data") {
|
|
110
|
+
return toGenAIPartData(a2aPart);
|
|
111
|
+
}
|
|
112
|
+
throw new Error(`Unknown part kind: ${JSON.stringify(a2aPart)}`);
|
|
113
|
+
}
|
|
114
|
+
function toGenAIPartText(a2aPart) {
|
|
115
|
+
var _a;
|
|
116
|
+
return {
|
|
117
|
+
text: a2aPart.text,
|
|
118
|
+
thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.THOUGHT])
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function toGenAIPartFile(a2aPart) {
|
|
122
|
+
if ("bytes" in a2aPart.file) {
|
|
123
|
+
return {
|
|
124
|
+
inlineData: {
|
|
125
|
+
data: a2aPart.file.bytes,
|
|
126
|
+
mimeType: a2aPart.file.mimeType || ""
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
if ("uri" in a2aPart.file) {
|
|
131
|
+
return {
|
|
132
|
+
fileData: {
|
|
133
|
+
fileUri: a2aPart.file.uri,
|
|
134
|
+
mimeType: a2aPart.file.mimeType || ""
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
throw new Error(`Not a file part: ${JSON.stringify(a2aPart)}`);
|
|
139
|
+
}
|
|
140
|
+
function toGenAIPartData(a2aPart) {
|
|
141
|
+
var _a;
|
|
142
|
+
if (!a2aPart.data) {
|
|
143
|
+
throw new Error(`No data in part: ${JSON.stringify(a2aPart)}`);
|
|
144
|
+
}
|
|
145
|
+
const data = a2aPart.data;
|
|
146
|
+
const type = (_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.DATA_PART_TYPE];
|
|
147
|
+
if (type === "function_call" /* FUNCTION_CALL */) {
|
|
148
|
+
return { functionCall: data };
|
|
149
|
+
}
|
|
150
|
+
if (type === "function_response" /* FUNCTION_RESPONSE */) {
|
|
151
|
+
return { functionResponse: data };
|
|
152
|
+
}
|
|
153
|
+
if (type === "executable_code" /* CODE_EXECUTABLE_CODE */) {
|
|
154
|
+
return { executableCode: data };
|
|
155
|
+
}
|
|
156
|
+
if (type === "code_execution_result" /* CODE_EXEC_RESULT */) {
|
|
157
|
+
return { codeExecutionResult: data };
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
text: JSON.stringify(a2aPart.data)
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
toA2ADataPart,
|
|
165
|
+
toA2AFilePart,
|
|
166
|
+
toA2APart,
|
|
167
|
+
toA2AParts,
|
|
168
|
+
toA2ATextPart,
|
|
169
|
+
toGenAIContent,
|
|
170
|
+
toGenAIPart,
|
|
171
|
+
toGenAIPartData,
|
|
172
|
+
toGenAIPartFile,
|
|
173
|
+
toGenAIPartText,
|
|
174
|
+
toGenAIParts
|
|
175
|
+
};
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
traceAgentInvocation,
|
|
12
12
|
tracer
|
|
13
13
|
} from "../telemetry/tracing.js";
|
|
14
|
-
import {
|
|
14
|
+
import { Context } from "./context.js";
|
|
15
15
|
import { InvocationContext } from "./invocation_context.js";
|
|
16
16
|
const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
|
|
17
17
|
function isBaseAgent(obj) {
|
|
@@ -153,7 +153,7 @@ class BaseAgent {
|
|
|
153
153
|
if (this.beforeAgentCallback.length === 0) {
|
|
154
154
|
return void 0;
|
|
155
155
|
}
|
|
156
|
-
const callbackContext = new
|
|
156
|
+
const callbackContext = new Context({ invocationContext });
|
|
157
157
|
for (const callback of this.beforeAgentCallback) {
|
|
158
158
|
const content = await callback(callbackContext);
|
|
159
159
|
if (content) {
|
|
@@ -188,7 +188,7 @@ class BaseAgent {
|
|
|
188
188
|
if (this.afterAgentCallback.length === 0) {
|
|
189
189
|
return void 0;
|
|
190
190
|
}
|
|
191
|
-
const callbackContext = new
|
|
191
|
+
const callbackContext = new Context({ invocationContext });
|
|
192
192
|
for (const callback of this.afterAgentCallback) {
|
|
193
193
|
const content = await callback(callbackContext);
|
|
194
194
|
if (content) {
|
|
@@ -225,7 +225,7 @@ class BaseAgent {
|
|
|
225
225
|
function validateAgentName(name) {
|
|
226
226
|
if (!isIdentifier(name)) {
|
|
227
227
|
throw new Error(
|
|
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), 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.`
|
|
229
229
|
);
|
|
230
230
|
}
|
|
231
231
|
if (name === "user") {
|
|
@@ -236,7 +236,7 @@ function validateAgentName(name) {
|
|
|
236
236
|
return name;
|
|
237
237
|
}
|
|
238
238
|
function isIdentifier(str) {
|
|
239
|
-
return /^[\p{ID_Start}$_][\p{ID_Continue}$_]*$/u.test(str);
|
|
239
|
+
return /^[\p{ID_Start}$_][\p{ID_Continue}$_-]*$/u.test(str);
|
|
240
240
|
}
|
|
241
241
|
function getRootAgent(rootAgent) {
|
|
242
242
|
while (rootAgent.parentAgent) {
|
|
@@ -3,29 +3,84 @@
|
|
|
3
3
|
* Copyright 2025 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
import { CallbackContext } from "../agents/callback_context.js";
|
|
7
6
|
import { AuthHandler } from "../auth/auth_handler.js";
|
|
7
|
+
import { createEventActions } from "../events/event_actions.js";
|
|
8
|
+
import { State } from "../sessions/state.js";
|
|
8
9
|
import { ToolConfirmation } from "../tools/tool_confirmation.js";
|
|
9
|
-
|
|
10
|
+
import { ReadonlyContext } from "./readonly_context.js";
|
|
11
|
+
class Context extends ReadonlyContext {
|
|
10
12
|
/**
|
|
11
|
-
* @param
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
13
|
+
* @param options The configuration options for the Context.
|
|
14
|
+
* @param options.invocationContext The invocation context.
|
|
15
|
+
* @param options.eventActions The event actions of the current call.
|
|
16
|
+
* @param options.functionCallId The function call id of the current tool call.
|
|
14
17
|
* This id was returned in the function call event from LLM to identify a
|
|
15
18
|
* function call. If LLM didn't return this id, ADK will assign one to it.
|
|
16
19
|
* This id is used to map function call response to the original function
|
|
17
20
|
* call.
|
|
18
|
-
* @param
|
|
21
|
+
* @param options.toolConfirmation The tool confirmation of the current tool
|
|
19
22
|
* call.
|
|
20
23
|
*/
|
|
21
|
-
constructor(
|
|
22
|
-
super(
|
|
23
|
-
this.
|
|
24
|
-
this.
|
|
24
|
+
constructor(options) {
|
|
25
|
+
super(options.invocationContext);
|
|
26
|
+
this.eventActions = options.eventActions || createEventActions();
|
|
27
|
+
this._state = new State(
|
|
28
|
+
options.invocationContext.session.state,
|
|
29
|
+
this.eventActions.stateDelta
|
|
30
|
+
);
|
|
31
|
+
this.functionCallId = options.functionCallId;
|
|
32
|
+
this.toolConfirmation = options.toolConfirmation;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The delta-aware state of the current session.
|
|
36
|
+
*/
|
|
37
|
+
get state() {
|
|
38
|
+
return this._state;
|
|
25
39
|
}
|
|
26
40
|
get actions() {
|
|
27
41
|
return this.eventActions;
|
|
28
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Loads an artifact attached to the current session.
|
|
45
|
+
*
|
|
46
|
+
* @param filename The filename of the artifact.
|
|
47
|
+
* @param version The version of the artifact. If not provided, the latest
|
|
48
|
+
* version will be used.
|
|
49
|
+
* @return A promise that resolves to the loaded artifact.
|
|
50
|
+
*/
|
|
51
|
+
loadArtifact(filename, version) {
|
|
52
|
+
if (!this.invocationContext.artifactService) {
|
|
53
|
+
throw new Error("Artifact service is not initialized.");
|
|
54
|
+
}
|
|
55
|
+
return this.invocationContext.artifactService.loadArtifact({
|
|
56
|
+
appName: this.invocationContext.appName,
|
|
57
|
+
userId: this.invocationContext.userId,
|
|
58
|
+
sessionId: this.invocationContext.session.id,
|
|
59
|
+
filename,
|
|
60
|
+
version
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Saves an artifact attached to the current session.
|
|
65
|
+
*
|
|
66
|
+
* @param filename The filename of the artifact.
|
|
67
|
+
* @param artifact The artifact to save.
|
|
68
|
+
* @return A promise that resolves to the version of the saved artifact.
|
|
69
|
+
*/
|
|
70
|
+
async saveArtifact(filename, artifact) {
|
|
71
|
+
if (!this.invocationContext.artifactService) {
|
|
72
|
+
throw new Error("Artifact service is not initialized.");
|
|
73
|
+
}
|
|
74
|
+
const version = await this.invocationContext.artifactService.saveArtifact({
|
|
75
|
+
appName: this.invocationContext.appName,
|
|
76
|
+
userId: this.invocationContext.userId,
|
|
77
|
+
sessionId: this.invocationContext.session.id,
|
|
78
|
+
filename,
|
|
79
|
+
artifact
|
|
80
|
+
});
|
|
81
|
+
this.eventActions.artifactDelta[filename] = version;
|
|
82
|
+
return version;
|
|
83
|
+
}
|
|
29
84
|
requestCredential(authConfig) {
|
|
30
85
|
if (!this.functionCallId) {
|
|
31
86
|
throw new Error("functionCallId is not set.");
|
|
@@ -90,5 +145,5 @@ class ToolContext extends CallbackContext {
|
|
|
90
145
|
}
|
|
91
146
|
}
|
|
92
147
|
export {
|
|
93
|
-
|
|
148
|
+
Context
|
|
94
149
|
};
|
|
@@ -7,9 +7,9 @@ import { createUserContent } from "@google/genai";
|
|
|
7
7
|
import { isEmpty } from "lodash-es";
|
|
8
8
|
import { createEvent, getFunctionCalls } from "../events/event.js";
|
|
9
9
|
import { mergeEventActions } from "../events/event_actions.js";
|
|
10
|
-
import { ToolContext } from "../tools/tool_context.js";
|
|
11
10
|
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
12
11
|
import { logger } from "../utils/logger.js";
|
|
12
|
+
import { Context } from "./context.js";
|
|
13
13
|
import {
|
|
14
14
|
traceMergedToolCalls,
|
|
15
15
|
tracer,
|
|
@@ -128,6 +128,7 @@ function generateRequestConfirmationEvent({
|
|
|
128
128
|
parts,
|
|
129
129
|
role: functionResponseEvent.content.role
|
|
130
130
|
},
|
|
131
|
+
actions: functionResponseEvent.actions,
|
|
131
132
|
longRunningToolIds: Array.from(longRunningToolIds)
|
|
132
133
|
});
|
|
133
134
|
}
|
|
@@ -351,7 +352,7 @@ function getToolAndContext({
|
|
|
351
352
|
`Function ${functionCall.name} is not found in the toolsDict.`
|
|
352
353
|
);
|
|
353
354
|
}
|
|
354
|
-
const toolContext = new
|
|
355
|
+
const toolContext = new Context({
|
|
355
356
|
invocationContext,
|
|
356
357
|
functionCallId: functionCall.id || void 0,
|
|
357
358
|
toolConfirmation
|