@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,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:".concat(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: "[".concat(agentEvent.author, "] said: ").concat(part.text)
|
|
102
|
+
});
|
|
103
|
+
} else if (part.functionCall) {
|
|
104
|
+
const call = part.functionCall;
|
|
105
|
+
parts.push({
|
|
106
|
+
text: "[".concat(agentEvent.author, "] called tool ").concat(call.name, " with parameters: ").concat(JSON.stringify(call.args))
|
|
107
|
+
});
|
|
108
|
+
} else if (part.functionResponse) {
|
|
109
|
+
const resp = part.functionResponse;
|
|
110
|
+
parts.push({
|
|
111
|
+
text: "[".concat(agentEvent.author, "] ").concat(resp.name, " tool returned result: ").concat(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
|
+
};
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { DefaultAgentCardResolver } from "@a2a-js/sdk/client";
|
|
7
|
+
import * as fs from "node:fs/promises";
|
|
8
|
+
import {
|
|
9
|
+
InvocationContext
|
|
10
|
+
} from "../agents/invocation_context.js";
|
|
11
|
+
import { isLlmAgent } from "../agents/llm_agent.js";
|
|
12
|
+
import { isLoopAgent } from "../agents/loop_agent.js";
|
|
13
|
+
import { isParallelAgent } from "../agents/parallel_agent.js";
|
|
14
|
+
import { ReadonlyContext } from "../agents/readonly_context.js";
|
|
15
|
+
import { isSequentialAgent } from "../agents/sequential_agent.js";
|
|
16
|
+
import { isBaseTool } from "../tools/base_tool.js";
|
|
17
|
+
import { isBaseToolset } from "../tools/base_toolset.js";
|
|
18
|
+
import { logger } from "../utils/logger.js";
|
|
19
|
+
async function resolveAgentCard(agentCard) {
|
|
20
|
+
if (typeof agentCard === "object") {
|
|
21
|
+
return agentCard;
|
|
22
|
+
}
|
|
23
|
+
const source = agentCard;
|
|
24
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
25
|
+
const resolver = new DefaultAgentCardResolver();
|
|
26
|
+
return await resolver.resolve(source);
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const content = await fs.readFile(source, "utf-8");
|
|
30
|
+
return JSON.parse(content);
|
|
31
|
+
} catch (err) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
"Failed to read agent card from file ".concat(source, ": ").concat(err.message)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function getA2AAgentCard(agent, transports) {
|
|
38
|
+
return {
|
|
39
|
+
name: agent.name,
|
|
40
|
+
description: agent.description || "",
|
|
41
|
+
protocolVersion: "0.3.0",
|
|
42
|
+
version: "1.0.0",
|
|
43
|
+
skills: await buildAgentSkills(agent),
|
|
44
|
+
url: transports[0].url,
|
|
45
|
+
preferredTransport: transports[0].transport,
|
|
46
|
+
capabilities: {
|
|
47
|
+
extensions: [],
|
|
48
|
+
stateTransitionHistory: false,
|
|
49
|
+
pushNotifications: false,
|
|
50
|
+
streaming: true
|
|
51
|
+
},
|
|
52
|
+
defaultInputModes: ["text"],
|
|
53
|
+
defaultOutputModes: ["text"],
|
|
54
|
+
additionalInterfaces: transports
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async function buildAgentSkills(agent) {
|
|
58
|
+
const [primarySkills, subAgentSkills] = await Promise.all([
|
|
59
|
+
buildPrimarySkills(agent),
|
|
60
|
+
buildSubAgentSkills(agent)
|
|
61
|
+
]);
|
|
62
|
+
return [...primarySkills, ...subAgentSkills];
|
|
63
|
+
}
|
|
64
|
+
async function buildPrimarySkills(agent) {
|
|
65
|
+
if (isLlmAgent(agent)) {
|
|
66
|
+
return buildLLMAgentSkills(agent);
|
|
67
|
+
}
|
|
68
|
+
return buildNonLLMAgentSkills(agent);
|
|
69
|
+
}
|
|
70
|
+
async function buildSubAgentSkills(agent) {
|
|
71
|
+
const subAgents = agent.subAgents;
|
|
72
|
+
const result = [];
|
|
73
|
+
for (const sub of subAgents) {
|
|
74
|
+
const skills = await buildPrimarySkills(sub);
|
|
75
|
+
for (const subSkill of skills) {
|
|
76
|
+
const skill = {
|
|
77
|
+
id: "".concat(sub.name, "_").concat(subSkill.id),
|
|
78
|
+
name: "".concat(sub.name, ": ").concat(subSkill.name),
|
|
79
|
+
description: subSkill.description,
|
|
80
|
+
tags: ["sub_agent:".concat(sub.name), ...subSkill.tags]
|
|
81
|
+
};
|
|
82
|
+
result.push(skill);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
async function buildLLMAgentSkills(agent) {
|
|
88
|
+
const skills = [
|
|
89
|
+
{
|
|
90
|
+
id: agent.name,
|
|
91
|
+
name: "model",
|
|
92
|
+
description: await buildDescriptionFromInstructions(agent),
|
|
93
|
+
tags: ["llm"]
|
|
94
|
+
}
|
|
95
|
+
];
|
|
96
|
+
if (agent.tools && agent.tools.length > 0) {
|
|
97
|
+
for (const toolUnion of agent.tools) {
|
|
98
|
+
if (isBaseTool(toolUnion)) {
|
|
99
|
+
skills.push(toolToSkill(agent.name, toolUnion));
|
|
100
|
+
} else if (isBaseToolset(toolUnion)) {
|
|
101
|
+
const tools = await toolUnion.getTools();
|
|
102
|
+
for (const tool of tools) {
|
|
103
|
+
skills.push(toolToSkill(agent.name, tool));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return skills;
|
|
109
|
+
}
|
|
110
|
+
function toolToSkill(prefix, tool) {
|
|
111
|
+
let description = tool.description;
|
|
112
|
+
if (!description) {
|
|
113
|
+
description = "Tool: ".concat(tool.name);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
id: "".concat(prefix, "-").concat(tool.name),
|
|
117
|
+
name: tool.name,
|
|
118
|
+
description,
|
|
119
|
+
tags: ["llm", "tools"]
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function buildNonLLMAgentSkills(agent) {
|
|
123
|
+
const skills = [
|
|
124
|
+
{
|
|
125
|
+
id: agent.name,
|
|
126
|
+
name: getAgentSkillName(agent),
|
|
127
|
+
description: buildAgentDescription(agent),
|
|
128
|
+
tags: [getAgentTypeTag(agent)]
|
|
129
|
+
}
|
|
130
|
+
];
|
|
131
|
+
const subAgents = agent.subAgents;
|
|
132
|
+
if (subAgents.length > 0) {
|
|
133
|
+
const descriptions = subAgents.map(
|
|
134
|
+
(sub) => sub.description || "No description"
|
|
135
|
+
);
|
|
136
|
+
skills.push({
|
|
137
|
+
id: "".concat(agent.name, "-sub-agents"),
|
|
138
|
+
name: "sub-agents",
|
|
139
|
+
description: "Orchestrates: ".concat(descriptions.join("; ")),
|
|
140
|
+
tags: [getAgentTypeTag(agent), "orchestration"]
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return skills;
|
|
144
|
+
}
|
|
145
|
+
function buildAgentDescription(agent) {
|
|
146
|
+
const descriptionParts = [];
|
|
147
|
+
if (agent.description) {
|
|
148
|
+
descriptionParts.push(agent.description);
|
|
149
|
+
}
|
|
150
|
+
if (agent.subAgents.length > 0) {
|
|
151
|
+
if (isLoopAgent(agent)) {
|
|
152
|
+
descriptionParts.push(buildLoopAgentDescription(agent));
|
|
153
|
+
} else if (isParallelAgent(agent)) {
|
|
154
|
+
descriptionParts.push(buildParallelAgentDescription(agent));
|
|
155
|
+
} else if (isSequentialAgent(agent)) {
|
|
156
|
+
descriptionParts.push(buildSequentialAgentDescription(agent));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (descriptionParts.length > 0) {
|
|
160
|
+
return descriptionParts.join(" ");
|
|
161
|
+
} else {
|
|
162
|
+
return getDefaultAgentDescription(agent);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function buildSequentialAgentDescription(agent) {
|
|
166
|
+
const subAgents = agent.subAgents;
|
|
167
|
+
const descriptions = [];
|
|
168
|
+
subAgents.forEach((sub, i) => {
|
|
169
|
+
let subDescription = sub.description;
|
|
170
|
+
if (!subDescription) {
|
|
171
|
+
subDescription = "execute the ".concat(sub.name, " agent");
|
|
172
|
+
}
|
|
173
|
+
if (i === 0) {
|
|
174
|
+
descriptions.push("First, this agent will ".concat(subDescription, "."));
|
|
175
|
+
} else if (i === subAgents.length - 1) {
|
|
176
|
+
descriptions.push("Finally, this agent will ".concat(subDescription, "."));
|
|
177
|
+
} else {
|
|
178
|
+
descriptions.push("Then, this agent will ".concat(subDescription, "."));
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
return descriptions.join(" ");
|
|
182
|
+
}
|
|
183
|
+
function buildParallelAgentDescription(agent) {
|
|
184
|
+
const subAgents = agent.subAgents;
|
|
185
|
+
const descriptions = [];
|
|
186
|
+
subAgents.forEach((sub, i) => {
|
|
187
|
+
let subDescription = sub.description;
|
|
188
|
+
if (!subDescription) {
|
|
189
|
+
subDescription = "execute the ".concat(sub.name, " agent");
|
|
190
|
+
}
|
|
191
|
+
if (i === 0) {
|
|
192
|
+
descriptions.push("This agent will ".concat(subDescription));
|
|
193
|
+
} else if (i === subAgents.length - 1) {
|
|
194
|
+
descriptions.push("and ".concat(subDescription));
|
|
195
|
+
} else {
|
|
196
|
+
descriptions.push(", ".concat(subDescription));
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
return "".concat(descriptions.join(" "), " simultaneously.");
|
|
200
|
+
}
|
|
201
|
+
function buildLoopAgentDescription(agent) {
|
|
202
|
+
const maxIterationsVal = agent.maxIterations;
|
|
203
|
+
let maxIterations = "unlimited";
|
|
204
|
+
if (typeof maxIterationsVal === "number" && maxIterationsVal < Number.MAX_SAFE_INTEGER) {
|
|
205
|
+
maxIterations = maxIterationsVal.toString();
|
|
206
|
+
}
|
|
207
|
+
const subAgents = agent.subAgents;
|
|
208
|
+
const descriptions = [];
|
|
209
|
+
subAgents.forEach((sub, i) => {
|
|
210
|
+
let subDescription = sub.description;
|
|
211
|
+
if (!subDescription) {
|
|
212
|
+
subDescription = "execute the ".concat(sub.name, " agent");
|
|
213
|
+
}
|
|
214
|
+
if (i === 0) {
|
|
215
|
+
descriptions.push("This agent will ".concat(subDescription));
|
|
216
|
+
} else if (i === subAgents.length - 1) {
|
|
217
|
+
descriptions.push("and ".concat(subDescription));
|
|
218
|
+
} else {
|
|
219
|
+
descriptions.push(", ".concat(subDescription));
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
return "".concat(descriptions.join(" "), " in a loop (max ").concat(maxIterations, " iterations).");
|
|
223
|
+
}
|
|
224
|
+
async function buildDescriptionFromInstructions(agent) {
|
|
225
|
+
const descriptionParts = [];
|
|
226
|
+
if (agent.description) {
|
|
227
|
+
descriptionParts.push(agent.description);
|
|
228
|
+
}
|
|
229
|
+
if (agent.instruction) {
|
|
230
|
+
let instructionStr;
|
|
231
|
+
if (typeof agent.instruction === "function") {
|
|
232
|
+
const dummyContext = new ReadonlyContext(
|
|
233
|
+
new InvocationContext({
|
|
234
|
+
agent
|
|
235
|
+
})
|
|
236
|
+
);
|
|
237
|
+
try {
|
|
238
|
+
instructionStr = await agent.instruction(dummyContext);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
logger.warn("Failed to resolve dynamic instruction for AgentCard", e);
|
|
241
|
+
instructionStr = "";
|
|
242
|
+
}
|
|
243
|
+
} else {
|
|
244
|
+
instructionStr = agent.instruction;
|
|
245
|
+
}
|
|
246
|
+
if (instructionStr) {
|
|
247
|
+
descriptionParts.push(replacePronouns(instructionStr));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const root = agent.rootAgent;
|
|
251
|
+
if (isLlmAgent(root) && root.globalInstruction) {
|
|
252
|
+
let globalInstructionStr;
|
|
253
|
+
if (typeof root.globalInstruction === "function") {
|
|
254
|
+
const dummyContext = new ReadonlyContext(
|
|
255
|
+
new InvocationContext({
|
|
256
|
+
agent
|
|
257
|
+
})
|
|
258
|
+
);
|
|
259
|
+
try {
|
|
260
|
+
globalInstructionStr = await root.globalInstruction(dummyContext);
|
|
261
|
+
} catch (e) {
|
|
262
|
+
logger.warn(
|
|
263
|
+
"Failed to resolve dynamic global instruction for AgentCard",
|
|
264
|
+
e
|
|
265
|
+
);
|
|
266
|
+
globalInstructionStr = "";
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
globalInstructionStr = root.globalInstruction;
|
|
270
|
+
}
|
|
271
|
+
if (globalInstructionStr) {
|
|
272
|
+
descriptionParts.push(replacePronouns(globalInstructionStr));
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (descriptionParts.length > 0) {
|
|
276
|
+
return descriptionParts.join(" ");
|
|
277
|
+
} else {
|
|
278
|
+
return getDefaultAgentDescription(agent);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function replacePronouns(instruction) {
|
|
282
|
+
const substitutions = [
|
|
283
|
+
{ original: "you were", target: "I was" },
|
|
284
|
+
{ original: "you are", target: "I am" },
|
|
285
|
+
{ original: "you're", target: "I am" },
|
|
286
|
+
{ original: "you've", target: "I have" },
|
|
287
|
+
{ original: "yours", target: "mine" },
|
|
288
|
+
{ original: "your", target: "my" },
|
|
289
|
+
{ original: "you", target: "I" }
|
|
290
|
+
];
|
|
291
|
+
let result = instruction;
|
|
292
|
+
for (const sub of substitutions) {
|
|
293
|
+
const pattern = new RegExp("\\b".concat(sub.original, "\\b"), "gi");
|
|
294
|
+
result = result.replace(pattern, sub.target);
|
|
295
|
+
}
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
function getDefaultAgentDescription(agent) {
|
|
299
|
+
if (isLoopAgent(agent)) {
|
|
300
|
+
return "A loop workflow agent";
|
|
301
|
+
} else if (isSequentialAgent(agent)) {
|
|
302
|
+
return "A sequential workflow agent";
|
|
303
|
+
} else if (isParallelAgent(agent)) {
|
|
304
|
+
return "A parallel workflow agent";
|
|
305
|
+
} else if (isLlmAgent(agent)) {
|
|
306
|
+
return "An LLM-based agent";
|
|
307
|
+
} else {
|
|
308
|
+
return "A custom agent";
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
function getAgentTypeTag(agent) {
|
|
312
|
+
if (isLoopAgent(agent)) {
|
|
313
|
+
return "loop_workflow";
|
|
314
|
+
} else if (isSequentialAgent(agent)) {
|
|
315
|
+
return "sequential_workflow";
|
|
316
|
+
} else if (isParallelAgent(agent)) {
|
|
317
|
+
return "parallel_workflow";
|
|
318
|
+
} else if (isLlmAgent(agent)) {
|
|
319
|
+
return "llm_agent";
|
|
320
|
+
} else {
|
|
321
|
+
return "custom_agent";
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function getAgentSkillName(agent) {
|
|
325
|
+
if (isLlmAgent(agent)) {
|
|
326
|
+
return "model";
|
|
327
|
+
}
|
|
328
|
+
if (isWorkflowAgent(agent)) {
|
|
329
|
+
return "workflow";
|
|
330
|
+
}
|
|
331
|
+
return "custom";
|
|
332
|
+
}
|
|
333
|
+
function isWorkflowAgent(agent) {
|
|
334
|
+
return isLoopAgent(agent) || isSequentialAgent(agent) || isParallelAgent(agent);
|
|
335
|
+
}
|
|
336
|
+
export {
|
|
337
|
+
buildAgentSkills,
|
|
338
|
+
getA2AAgentCard,
|
|
339
|
+
resolveAgentCard
|
|
340
|
+
};
|