@google/adk 0.4.0 → 0.6.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/README.md +1 -1
- package/dist/cjs/a2a/a2a_event.js +317 -0
- package/dist/cjs/a2a/a2a_remote_agent.js +174 -0
- package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +198 -0
- package/dist/cjs/a2a/a2a_remote_agent_utils.js +165 -0
- package/dist/cjs/a2a/agent_card.js +380 -0
- package/dist/cjs/a2a/agent_executor.js +221 -0
- package/dist/cjs/a2a/agent_to_a2a.js +115 -0
- package/dist/cjs/a2a/event_converter_utils.js +201 -0
- package/dist/cjs/a2a/event_processor_utils.js +180 -0
- package/dist/cjs/a2a/executor_context.js +53 -0
- package/dist/cjs/a2a/metadata_converter_utils.js +126 -0
- package/dist/cjs/a2a/part_converter_utils.js +47 -34
- package/dist/cjs/agents/base_agent.js +3 -3
- package/dist/cjs/{tools/tool_context.js → agents/context.js} +70 -15
- package/dist/cjs/agents/functions.js +2 -2
- package/dist/cjs/agents/llm_agent.js +38 -674
- package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
- 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/{content_processor_utils.js → processors/content_processor_utils.js} +21 -2
- package/dist/cjs/agents/processors/content_request_processor.js +87 -0
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -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/artifacts/file_artifact_service.js +35 -4
- package/dist/cjs/common.js +48 -7
- package/dist/cjs/context/base_context_compactor.js +27 -0
- package/dist/cjs/context/summarizers/base_summarizer.js +27 -0
- package/dist/cjs/context/summarizers/llm_summarizer.js +93 -0
- package/dist/cjs/context/token_based_context_compactor.js +135 -0
- package/dist/cjs/context/truncating_context_compactor.js +58 -0
- package/dist/cjs/events/compacted_event.js +53 -0
- package/dist/cjs/index.js +113 -53
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/memory/in_memory_memory_service.js +1 -1
- package/dist/cjs/models/llm_response.js +2 -0
- package/dist/cjs/plugins/base_plugin.js +1 -1
- package/dist/cjs/runner/runner.js +20 -11
- package/dist/cjs/sessions/database_session_service.js +4 -1
- package/dist/cjs/sessions/db/operations.js +25 -23
- package/dist/cjs/sessions/state.js +2 -2
- package/dist/cjs/tools/agent_tool.js +6 -5
- package/dist/cjs/tools/base_tool.js +3 -0
- package/dist/cjs/tools/base_toolset.js +13 -3
- package/dist/cjs/tools/exit_loop_tool.js +63 -0
- package/dist/cjs/tools/load_artifacts_tool.js +188 -0
- package/dist/cjs/tools/load_memory_tool.js +107 -0
- package/dist/cjs/tools/mcp/mcp_toolset.js +9 -5
- package/dist/cjs/tools/preload_memory_tool.js +109 -0
- package/dist/cjs/utils/logger.js +62 -54
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_event.js +269 -0
- package/dist/esm/a2a/a2a_remote_agent.js +148 -0
- package/dist/esm/a2a/a2a_remote_agent_run_processor.js +175 -0
- package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/esm/a2a/agent_card.js +340 -0
- package/dist/esm/a2a/agent_executor.js +202 -0
- package/dist/esm/a2a/agent_to_a2a.js +80 -0
- package/dist/esm/a2a/event_converter_utils.js +187 -0
- package/dist/esm/a2a/event_processor_utils.js +159 -0
- package/dist/esm/a2a/executor_context.js +23 -0
- package/dist/esm/a2a/metadata_converter_utils.js +91 -0
- package/dist/esm/a2a/part_converter_utils.js +49 -34
- package/dist/esm/agents/base_agent.js +3 -3
- package/dist/{web/tools/tool_context.js → esm/agents/context.js} +66 -11
- package/dist/esm/agents/functions.js +2 -2
- package/dist/esm/agents/llm_agent.js +31 -683
- 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/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
- package/dist/esm/agents/processors/content_request_processor.js +59 -0
- package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -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/artifacts/file_artifact_service.js +30 -3
- package/dist/esm/common.js +44 -10
- package/dist/esm/context/base_context_compactor.js +5 -0
- package/dist/esm/context/summarizers/base_summarizer.js +5 -0
- package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
- package/dist/esm/context/token_based_context_compactor.js +105 -0
- package/dist/esm/context/truncating_context_compactor.js +28 -0
- package/dist/esm/events/compacted_event.js +22 -0
- package/dist/esm/index.js +119 -18
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/memory/in_memory_memory_service.js +1 -1
- package/dist/esm/models/llm_response.js +2 -0
- package/dist/esm/plugins/base_plugin.js +1 -1
- package/dist/esm/runner/runner.js +18 -10
- package/dist/esm/sessions/database_session_service.js +4 -1
- package/dist/esm/sessions/db/operations.js +32 -18
- package/dist/esm/sessions/state.js +2 -2
- package/dist/esm/tools/agent_tool.js +6 -5
- 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/load_artifacts_tool.js +159 -0
- package/dist/esm/tools/load_memory_tool.js +78 -0
- package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
- package/dist/esm/tools/preload_memory_tool.js +80 -0
- package/dist/esm/utils/logger.js +52 -54
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +139 -0
- package/dist/types/a2a/a2a_remote_agent.d.ts +63 -0
- package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
- package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
- package/dist/types/a2a/agent_card.d.ts +23 -0
- package/dist/types/a2a/agent_executor.d.ts +52 -0
- package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
- package/dist/types/a2a/event_converter_utils.d.ts +20 -0
- package/dist/types/a2a/event_processor_utils.d.ts +24 -0
- package/dist/types/a2a/executor_context.d.ts +33 -0
- package/dist/types/a2a/metadata_converter_utils.d.ts +63 -0
- package/dist/types/a2a/part_converter_utils.d.ts +4 -3
- 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 +14 -31
- package/dist/types/agents/loop_agent.d.ts +1 -1
- 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/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
- package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -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/artifacts/file_artifact_service.d.ts +4 -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 +20 -5
- package/dist/types/context/base_context_compactor.d.ts +24 -0
- package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
- package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
- package/dist/types/context/token_based_context_compactor.d.ts +33 -0
- package/dist/types/context/truncating_context_compactor.d.ts +24 -0
- package/dist/types/events/compacted_event.d.ts +33 -0
- package/dist/types/index.d.ts +8 -0
- 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 +13 -0
- package/dist/types/sessions/database_session_service.d.ts +2 -1
- package/dist/types/sessions/db/operations.d.ts +3 -4
- 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 +2 -2
- package/dist/types/tools/function_tool.d.ts +2 -2
- package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
- package/dist/types/tools/load_memory_tool.d.ts +22 -0
- package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
- package/dist/types/tools/preload_memory_tool.d.ts +23 -0
- package/dist/types/utils/logger.d.ts +5 -9
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +269 -0
- package/dist/web/a2a/a2a_remote_agent.js +193 -0
- package/dist/web/a2a/a2a_remote_agent_run_processor.js +175 -0
- package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
- package/dist/web/a2a/agent_card.js +340 -0
- package/dist/web/a2a/agent_executor.js +216 -0
- package/dist/web/a2a/agent_to_a2a.js +80 -0
- package/dist/web/a2a/event_converter_utils.js +201 -0
- package/dist/web/a2a/event_processor_utils.js +168 -0
- package/dist/web/a2a/executor_context.js +23 -0
- package/dist/web/a2a/metadata_converter_utils.js +108 -0
- package/dist/web/a2a/part_converter_utils.js +49 -34
- package/dist/web/agents/base_agent.js +3 -3
- package/dist/{esm/tools/tool_context.js → web/agents/context.js} +66 -11
- package/dist/web/agents/functions.js +2 -2
- package/dist/web/agents/llm_agent.js +31 -661
- 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/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
- package/dist/web/agents/processors/content_request_processor.js +77 -0
- package/dist/web/agents/processors/context_compactor_request_processor.js +49 -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/artifacts/file_artifact_service.js +30 -3
- package/dist/web/common.js +44 -10
- package/dist/web/context/base_context_compactor.js +5 -0
- package/dist/web/context/summarizers/base_summarizer.js +5 -0
- package/dist/web/context/summarizers/llm_summarizer.js +74 -0
- package/dist/web/context/token_based_context_compactor.js +105 -0
- package/dist/web/context/truncating_context_compactor.js +28 -0
- package/dist/web/events/compacted_event.js +40 -0
- package/dist/web/index.js +13 -18
- package/dist/web/index.js.map +7 -0
- package/dist/web/memory/in_memory_memory_service.js +1 -1
- package/dist/web/models/llm_response.js +2 -0
- package/dist/web/plugins/base_plugin.js +1 -1
- package/dist/web/runner/runner.js +18 -10
- package/dist/web/sessions/database_session_service.js +4 -1
- package/dist/web/sessions/db/operations.js +32 -18
- package/dist/web/sessions/state.js +2 -2
- package/dist/web/tools/agent_tool.js +6 -5
- 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/load_artifacts_tool.js +150 -0
- package/dist/web/tools/load_memory_tool.js +77 -0
- package/dist/web/tools/mcp/mcp_toolset.js +27 -5
- package/dist/web/tools/preload_memory_tool.js +75 -0
- package/dist/web/utils/logger.js +52 -54
- package/dist/web/version.js +1 -1
- package/package.json +7 -4
- 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/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
- /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,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
AGENT_CARD_PATH
|
|
8
|
+
} from "@a2a-js/sdk";
|
|
9
|
+
import { ClientFactory } from "@a2a-js/sdk/client";
|
|
10
|
+
import { BaseAgent } from "../agents/base_agent.js";
|
|
11
|
+
import { createEvent } from "../events/event.js";
|
|
12
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
13
|
+
import { logger } from "../utils/logger.js";
|
|
14
|
+
import { MessageRole } from "./a2a_event.js";
|
|
15
|
+
import { A2ARemoteAgentRunProcessor } from "./a2a_remote_agent_run_processor.js";
|
|
16
|
+
import {
|
|
17
|
+
getUserFunctionCallAt,
|
|
18
|
+
toMissingRemoteSessionParts
|
|
19
|
+
} from "./a2a_remote_agent_utils.js";
|
|
20
|
+
import { resolveAgentCard } from "./agent_card.js";
|
|
21
|
+
import { toAdkEvent } from "./event_converter_utils.js";
|
|
22
|
+
import { getA2ASessionMetadata } from "./metadata_converter_utils.js";
|
|
23
|
+
import { toA2AParts } from "./part_converter_utils.js";
|
|
24
|
+
class RemoteA2AAgent extends BaseAgent {
|
|
25
|
+
constructor(a2aConfig) {
|
|
26
|
+
super(a2aConfig);
|
|
27
|
+
this.a2aConfig = a2aConfig;
|
|
28
|
+
this.isInitialized = false;
|
|
29
|
+
if (!a2aConfig.agentCard) {
|
|
30
|
+
throw new Error("AgentCard must be provided");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async init() {
|
|
34
|
+
if (this.isInitialized) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.card = await resolveAgentCard(this.a2aConfig.agentCard);
|
|
38
|
+
const factory = this.a2aConfig.clientFactory || new ClientFactory();
|
|
39
|
+
this.client = await factory.createFromAgentCard(this.card);
|
|
40
|
+
this.isInitialized = true;
|
|
41
|
+
}
|
|
42
|
+
async *runAsyncImpl(context) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
await this.init();
|
|
45
|
+
try {
|
|
46
|
+
const events = context.session.events;
|
|
47
|
+
if (events.length === 0) {
|
|
48
|
+
throw new Error("No events in session to send");
|
|
49
|
+
}
|
|
50
|
+
const userFnCall = getUserFunctionCallAt(
|
|
51
|
+
context.session,
|
|
52
|
+
events.length - 1
|
|
53
|
+
);
|
|
54
|
+
let parts;
|
|
55
|
+
let taskId = void 0;
|
|
56
|
+
let contextId = void 0;
|
|
57
|
+
if (userFnCall) {
|
|
58
|
+
const event = userFnCall.response;
|
|
59
|
+
parts = toA2AParts(
|
|
60
|
+
((_a = event.content) == null ? void 0 : _a.parts) || [],
|
|
61
|
+
event.longRunningToolIds
|
|
62
|
+
);
|
|
63
|
+
taskId = userFnCall.taskId;
|
|
64
|
+
contextId = userFnCall.contextId;
|
|
65
|
+
} else {
|
|
66
|
+
const missing = toMissingRemoteSessionParts(context, context.session);
|
|
67
|
+
parts = missing.parts;
|
|
68
|
+
contextId = missing.contextId;
|
|
69
|
+
}
|
|
70
|
+
const message = {
|
|
71
|
+
kind: "message",
|
|
72
|
+
messageId: randomUUID(),
|
|
73
|
+
role: MessageRole.USER,
|
|
74
|
+
parts,
|
|
75
|
+
metadata: {
|
|
76
|
+
...getA2ASessionMetadata({
|
|
77
|
+
appName: context.session.appName,
|
|
78
|
+
userId: context.session.userId,
|
|
79
|
+
sessionId: context.session.id
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
if (taskId) message.taskId = taskId;
|
|
84
|
+
if (contextId) message.contextId = contextId;
|
|
85
|
+
const params = {
|
|
86
|
+
message,
|
|
87
|
+
configuration: this.a2aConfig.messageSendConfig
|
|
88
|
+
};
|
|
89
|
+
const processor = new A2ARemoteAgentRunProcessor(params);
|
|
90
|
+
if (this.a2aConfig.beforeRequestCallbacks) {
|
|
91
|
+
for (const callback of this.a2aConfig.beforeRequestCallbacks) {
|
|
92
|
+
await callback(context, params);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const useStreaming = ((_b = this.card.capabilities) == null ? void 0 : _b.streaming) !== false;
|
|
96
|
+
if (useStreaming) {
|
|
97
|
+
for await (const chunk of this.client.sendMessageStream(params)) {
|
|
98
|
+
if (this.a2aConfig.afterRequestCallbacks) {
|
|
99
|
+
for (const callback of this.a2aConfig.afterRequestCallbacks) {
|
|
100
|
+
await callback(context, chunk);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const adkEvent = toAdkEvent(chunk, context.invocationId, this.name);
|
|
104
|
+
if (!adkEvent) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
processor.updateCustomMetadata(adkEvent, chunk);
|
|
108
|
+
const eventsToEmit = processor.aggregatePartial(
|
|
109
|
+
context,
|
|
110
|
+
chunk,
|
|
111
|
+
adkEvent
|
|
112
|
+
);
|
|
113
|
+
for (const ev of eventsToEmit) {
|
|
114
|
+
yield ev;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
const result = await this.client.sendMessage(params);
|
|
119
|
+
if (this.a2aConfig.afterRequestCallbacks) {
|
|
120
|
+
for (const callback of this.a2aConfig.afterRequestCallbacks) {
|
|
121
|
+
await callback(context, result);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
const adkEvent = toAdkEvent(result, context.invocationId, this.name);
|
|
125
|
+
if (adkEvent) {
|
|
126
|
+
processor.updateCustomMetadata(adkEvent, result);
|
|
127
|
+
yield adkEvent;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
} catch (e) {
|
|
131
|
+
const error = e;
|
|
132
|
+
logger.error(`A2ARemoteAgent ${this.name} failed:`, error);
|
|
133
|
+
yield createEvent({
|
|
134
|
+
author: this.name,
|
|
135
|
+
invocationId: context.invocationId,
|
|
136
|
+
errorMessage: error.message,
|
|
137
|
+
turnComplete: true
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
runLiveImpl(_context) {
|
|
142
|
+
throw new Error("Live mode is not supported in A2ARemoteAgent yet.");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
export {
|
|
146
|
+
AGENT_CARD_PATH,
|
|
147
|
+
RemoteA2AAgent
|
|
148
|
+
};
|
|
@@ -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
|
+
} from "@google/genai";
|
|
9
|
+
import { createEvent } from "../events/event.js";
|
|
10
|
+
import {
|
|
11
|
+
getEventMetadata,
|
|
12
|
+
isTask,
|
|
13
|
+
isTaskArtifactUpdateEvent,
|
|
14
|
+
isTaskStatusUpdateEvent
|
|
15
|
+
} from "./a2a_event.js";
|
|
16
|
+
import { A2AMetadataKeys } from "./metadata_converter_utils.js";
|
|
17
|
+
class A2ARemoteAgentRunProcessor {
|
|
18
|
+
constructor(request) {
|
|
19
|
+
this.request = request;
|
|
20
|
+
this.aggregations = /* @__PURE__ */ new Map();
|
|
21
|
+
this.aggregationOrder = [];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* aggregatePartial stores contents of partial events to emit them with the terminal event.
|
|
25
|
+
* It can return multiple events to emit instead of just the provided one.
|
|
26
|
+
*/
|
|
27
|
+
aggregatePartial(context, a2aEvent, adkEvent) {
|
|
28
|
+
const metadata = getEventMetadata(a2aEvent);
|
|
29
|
+
if (metadata[A2AMetadataKeys.PARTIAL]) {
|
|
30
|
+
return [adkEvent];
|
|
31
|
+
}
|
|
32
|
+
if (isTaskStatusUpdateEvent(a2aEvent) && a2aEvent.final) {
|
|
33
|
+
const events = [];
|
|
34
|
+
for (const aid of this.aggregationOrder) {
|
|
35
|
+
const agg = this.aggregations.get(aid);
|
|
36
|
+
if (agg) {
|
|
37
|
+
events.push(this.buildNonPartialAggregation(context, agg));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
this.aggregations.clear();
|
|
41
|
+
this.aggregationOrder = [];
|
|
42
|
+
return [...events, adkEvent];
|
|
43
|
+
}
|
|
44
|
+
if (isTask(a2aEvent)) {
|
|
45
|
+
this.aggregations.clear();
|
|
46
|
+
this.aggregationOrder = [];
|
|
47
|
+
return [adkEvent];
|
|
48
|
+
}
|
|
49
|
+
if (!isTaskArtifactUpdateEvent(a2aEvent)) {
|
|
50
|
+
return [adkEvent];
|
|
51
|
+
}
|
|
52
|
+
const artifactId = a2aEvent.artifact.artifactId;
|
|
53
|
+
if (!a2aEvent.append) {
|
|
54
|
+
this.removeAggregation(artifactId);
|
|
55
|
+
if (a2aEvent.lastChunk) {
|
|
56
|
+
adkEvent.partial = false;
|
|
57
|
+
return [adkEvent];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
let aggregation = this.aggregations.get(artifactId);
|
|
61
|
+
if (!aggregation) {
|
|
62
|
+
aggregation = {
|
|
63
|
+
aggregatedText: "",
|
|
64
|
+
aggregatedThoughts: "",
|
|
65
|
+
parts: []
|
|
66
|
+
};
|
|
67
|
+
this.aggregations.set(artifactId, aggregation);
|
|
68
|
+
this.aggregationOrder.push(artifactId);
|
|
69
|
+
} else {
|
|
70
|
+
this.aggregationOrder = this.aggregationOrder.filter(
|
|
71
|
+
(id) => id !== artifactId
|
|
72
|
+
);
|
|
73
|
+
this.aggregationOrder.push(artifactId);
|
|
74
|
+
}
|
|
75
|
+
this.updateAggregation(aggregation, adkEvent);
|
|
76
|
+
if (!a2aEvent.lastChunk) {
|
|
77
|
+
return [adkEvent];
|
|
78
|
+
}
|
|
79
|
+
this.removeAggregation(artifactId);
|
|
80
|
+
return [adkEvent, this.buildNonPartialAggregation(context, aggregation)];
|
|
81
|
+
}
|
|
82
|
+
removeAggregation(artifactId) {
|
|
83
|
+
this.aggregations.delete(artifactId);
|
|
84
|
+
this.aggregationOrder = this.aggregationOrder.filter(
|
|
85
|
+
(id) => id !== artifactId
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
updateAggregation(agg, event) {
|
|
89
|
+
var _a;
|
|
90
|
+
const parts = ((_a = event.content) == null ? void 0 : _a.parts) || [];
|
|
91
|
+
for (const part of parts) {
|
|
92
|
+
if (part.text && part.text !== "") {
|
|
93
|
+
if (part.thought) {
|
|
94
|
+
agg.aggregatedThoughts += part.text;
|
|
95
|
+
} else {
|
|
96
|
+
agg.aggregatedText += part.text;
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
this.promoteTextBlocksToParts(agg);
|
|
100
|
+
agg.parts.push(part);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (event.citationMetadata) {
|
|
104
|
+
if (!agg.citations) {
|
|
105
|
+
agg.citations = { citations: [] };
|
|
106
|
+
}
|
|
107
|
+
if (!agg.citations.citations) {
|
|
108
|
+
agg.citations.citations = [];
|
|
109
|
+
}
|
|
110
|
+
agg.citations.citations.push(...event.citationMetadata.citations || []);
|
|
111
|
+
}
|
|
112
|
+
if (event.customMetadata) {
|
|
113
|
+
if (!agg.customMeta) {
|
|
114
|
+
agg.customMeta = {};
|
|
115
|
+
}
|
|
116
|
+
Object.assign(agg.customMeta, event.customMetadata);
|
|
117
|
+
}
|
|
118
|
+
if (event.groundingMetadata) {
|
|
119
|
+
agg.grounding = event.groundingMetadata;
|
|
120
|
+
}
|
|
121
|
+
if (event.usageMetadata) {
|
|
122
|
+
agg.usage = event.usageMetadata;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
buildNonPartialAggregation(context, agg) {
|
|
126
|
+
this.promoteTextBlocksToParts(agg);
|
|
127
|
+
const result = createEvent({
|
|
128
|
+
author: context.agent.name,
|
|
129
|
+
invocationId: context.invocationId,
|
|
130
|
+
content: agg.parts.length > 0 ? createModelContent([...agg.parts]) : void 0,
|
|
131
|
+
customMetadata: agg.customMeta,
|
|
132
|
+
groundingMetadata: agg.grounding,
|
|
133
|
+
citationMetadata: agg.citations,
|
|
134
|
+
usageMetadata: agg.usage,
|
|
135
|
+
turnComplete: false,
|
|
136
|
+
partial: false
|
|
137
|
+
});
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
promoteTextBlocksToParts(agg) {
|
|
141
|
+
if (agg.aggregatedThoughts !== "") {
|
|
142
|
+
agg.parts.push({ thought: true, text: agg.aggregatedThoughts });
|
|
143
|
+
agg.aggregatedThoughts = "";
|
|
144
|
+
}
|
|
145
|
+
if (agg.aggregatedText !== "") {
|
|
146
|
+
agg.parts.push({ text: agg.aggregatedText });
|
|
147
|
+
agg.aggregatedText = "";
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Adds request and response metadata to the event.
|
|
152
|
+
*/
|
|
153
|
+
updateCustomMetadata(event, response) {
|
|
154
|
+
const toAdd = {};
|
|
155
|
+
if (this.request && event.turnComplete) {
|
|
156
|
+
toAdd["request"] = this.request;
|
|
157
|
+
}
|
|
158
|
+
if (response) {
|
|
159
|
+
toAdd["response"] = response;
|
|
160
|
+
}
|
|
161
|
+
if (Object.keys(toAdd).length === 0) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (!event.customMetadata) {
|
|
165
|
+
event.customMetadata = {};
|
|
166
|
+
}
|
|
167
|
+
for (const [k, v] of Object.entries(toAdd)) {
|
|
168
|
+
if (v === void 0 || v === null) continue;
|
|
169
|
+
event.customMetadata[`a2a:${k}`] = v;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
export {
|
|
174
|
+
A2ARemoteAgentRunProcessor
|
|
175
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { createEvent } from "../events/event.js";
|
|
7
|
+
import { AdkMetadataKeys } from "./metadata_converter_utils.js";
|
|
8
|
+
import { toA2AParts } from "./part_converter_utils.js";
|
|
9
|
+
function getUserFunctionCallAt(session, index) {
|
|
10
|
+
const events = session.events;
|
|
11
|
+
if (index < 0 || index >= events.length) {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
const candidate = events[index];
|
|
15
|
+
if (candidate.author !== "user") {
|
|
16
|
+
return void 0;
|
|
17
|
+
}
|
|
18
|
+
const fnCallId = getFunctionResponseCallId(candidate);
|
|
19
|
+
if (!fnCallId) {
|
|
20
|
+
return void 0;
|
|
21
|
+
}
|
|
22
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
23
|
+
const request = events[i];
|
|
24
|
+
if (!isFunctionCallEvent(request, fnCallId)) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const metadata = request.customMetadata || {};
|
|
28
|
+
const taskId = metadata[AdkMetadataKeys.TASK_ID] || "";
|
|
29
|
+
const contextId = metadata[AdkMetadataKeys.CONTEXT_ID] || "";
|
|
30
|
+
return {
|
|
31
|
+
response: candidate,
|
|
32
|
+
taskId,
|
|
33
|
+
contextId
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return void 0;
|
|
37
|
+
}
|
|
38
|
+
function isFunctionCallEvent(event, callId) {
|
|
39
|
+
if (!event || !event.content || !event.content.parts) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return event.content.parts.some(
|
|
43
|
+
(part) => part.functionCall && part.functionCall.id === callId
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
function getFunctionResponseCallId(event) {
|
|
47
|
+
var _a;
|
|
48
|
+
if (!event || !event.content || !event.content.parts) {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
const responsePart = event.content.parts.find(
|
|
52
|
+
(part) => part.functionResponse
|
|
53
|
+
);
|
|
54
|
+
return (_a = responsePart == null ? void 0 : responsePart.functionResponse) == null ? void 0 : _a.id;
|
|
55
|
+
}
|
|
56
|
+
function toMissingRemoteSessionParts(ctx, session) {
|
|
57
|
+
const events = session.events;
|
|
58
|
+
let contextId = void 0;
|
|
59
|
+
let lastRemoteResponseIndex = -1;
|
|
60
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
61
|
+
const event = events[i];
|
|
62
|
+
if (event.author === ctx.agent.name) {
|
|
63
|
+
lastRemoteResponseIndex = i;
|
|
64
|
+
const metadata = event.customMetadata || {};
|
|
65
|
+
contextId = metadata[AdkMetadataKeys.CONTEXT_ID];
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const missingParts = [];
|
|
70
|
+
for (let i = lastRemoteResponseIndex + 1; i < events.length; i++) {
|
|
71
|
+
let event = events[i];
|
|
72
|
+
if (event.author !== "user" && event.author !== ctx.agent.name) {
|
|
73
|
+
event = presentAsUserMessage(ctx, event);
|
|
74
|
+
}
|
|
75
|
+
if (!event.content || !event.content.parts || event.content.parts.length === 0) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const parts = toA2AParts(event.content.parts, event.longRunningToolIds);
|
|
79
|
+
missingParts.push(...parts);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
parts: missingParts,
|
|
83
|
+
contextId
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function presentAsUserMessage(ctx, agentEvent) {
|
|
87
|
+
const event = createEvent({
|
|
88
|
+
author: "user",
|
|
89
|
+
invocationId: ctx.invocationId
|
|
90
|
+
});
|
|
91
|
+
if (!agentEvent.content || !agentEvent.content.parts) {
|
|
92
|
+
return event;
|
|
93
|
+
}
|
|
94
|
+
const parts = [{ text: "For context:" }];
|
|
95
|
+
for (const part of agentEvent.content.parts) {
|
|
96
|
+
if (part.thought) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (part.text) {
|
|
100
|
+
parts.push({
|
|
101
|
+
text: `[${agentEvent.author}] said: ${part.text}`
|
|
102
|
+
});
|
|
103
|
+
} else if (part.functionCall) {
|
|
104
|
+
const call = part.functionCall;
|
|
105
|
+
parts.push({
|
|
106
|
+
text: `[${agentEvent.author}] called tool ${call.name} with parameters: ${JSON.stringify(call.args)}`
|
|
107
|
+
});
|
|
108
|
+
} else if (part.functionResponse) {
|
|
109
|
+
const resp = part.functionResponse;
|
|
110
|
+
parts.push({
|
|
111
|
+
text: `[${agentEvent.author}] ${resp.name} tool returned result: ${JSON.stringify(resp.response)}`
|
|
112
|
+
});
|
|
113
|
+
} else {
|
|
114
|
+
parts.push(part);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (parts.length > 1) {
|
|
118
|
+
event.content = {
|
|
119
|
+
role: "user",
|
|
120
|
+
parts
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return event;
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
getFunctionResponseCallId,
|
|
127
|
+
getUserFunctionCallAt,
|
|
128
|
+
isFunctionCallEvent,
|
|
129
|
+
presentAsUserMessage,
|
|
130
|
+
toMissingRemoteSessionParts
|
|
131
|
+
};
|