@google/adk 0.5.0 → 0.6.1
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 +53 -26
- package/dist/cjs/a2a/a2a_remote_agent.js +179 -0
- package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +205 -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_processor_utils.js +180 -0
- package/dist/cjs/a2a/executor_context.js +1 -1
- package/dist/cjs/a2a/metadata_converter_utils.js +1 -0
- package/dist/cjs/a2a/part_converter_utils.js +24 -13
- package/dist/cjs/agents/llm_agent.js +17 -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 +24 -3
- package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/cjs/artifacts/file_artifact_service.js +35 -4
- package/dist/cjs/common.js +37 -0
- 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 +38 -14
- package/dist/cjs/index.js.map +4 -4
- package/dist/cjs/memory/in_memory_memory_service.js +1 -1
- package/dist/cjs/runner/runner.js +19 -10
- package/dist/cjs/sessions/db/operations.js +4 -14
- package/dist/cjs/sessions/state.js +2 -2
- package/dist/cjs/tools/agent_tool.js +6 -5
- package/dist/cjs/tools/load_artifacts_tool.js +188 -0
- package/dist/cjs/tools/load_memory_tool.js +107 -0
- package/dist/cjs/tools/preload_memory_tool.js +109 -0
- package/dist/cjs/utils/gemini_schema_util.js +54 -12
- package/dist/cjs/utils/logger.js +1 -0
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_event.js +52 -26
- package/dist/esm/a2a/a2a_remote_agent.js +153 -0
- package/dist/esm/a2a/a2a_remote_agent_run_processor.js +182 -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_processor_utils.js +159 -0
- package/dist/esm/a2a/executor_context.js +1 -1
- package/dist/esm/a2a/metadata_converter_utils.js +1 -0
- package/dist/esm/a2a/part_converter_utils.js +24 -13
- package/dist/esm/agents/llm_agent.js +17 -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 +25 -4
- package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/esm/artifacts/file_artifact_service.js +30 -3
- package/dist/esm/common.js +33 -1
- 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 +38 -14
- package/dist/esm/index.js.map +4 -4
- package/dist/esm/memory/in_memory_memory_service.js +1 -1
- package/dist/esm/runner/runner.js +17 -9
- package/dist/esm/sessions/db/operations.js +4 -14
- package/dist/esm/sessions/state.js +2 -2
- package/dist/esm/tools/agent_tool.js +6 -5
- package/dist/esm/tools/load_artifacts_tool.js +159 -0
- package/dist/esm/tools/load_memory_tool.js +78 -0
- package/dist/esm/tools/preload_memory_tool.js +80 -0
- package/dist/esm/utils/gemini_schema_util.js +54 -12
- package/dist/esm/utils/logger.js +1 -0
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +23 -6
- package/dist/types/a2a/a2a_remote_agent.d.ts +67 -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_processor_utils.d.ts +24 -0
- package/dist/types/a2a/executor_context.d.ts +1 -1
- package/dist/types/a2a/metadata_converter_utils.d.ts +2 -1
- package/dist/types/agents/llm_agent.d.ts +6 -0
- package/dist/types/agents/loop_agent.d.ts +1 -1
- package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
- package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
- package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
- package/dist/types/common.d.ts +16 -1
- 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/runner/runner.d.ts +13 -0
- package/dist/types/sessions/db/operations.d.ts +2 -3
- 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/preload_memory_tool.d.ts +23 -0
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +52 -26
- package/dist/web/a2a/a2a_remote_agent.js +184 -0
- package/dist/web/a2a/a2a_remote_agent_run_processor.js +182 -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_processor_utils.js +168 -0
- package/dist/web/a2a/executor_context.js +1 -1
- package/dist/web/a2a/metadata_converter_utils.js +1 -0
- package/dist/web/a2a/part_converter_utils.js +24 -13
- package/dist/web/agents/llm_agent.js +17 -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 +25 -4
- package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +1 -1
- package/dist/web/artifacts/file_artifact_service.js +30 -3
- package/dist/web/common.js +33 -1
- 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 +1 -1
- package/dist/web/index.js.map +4 -4
- package/dist/web/memory/in_memory_memory_service.js +1 -1
- package/dist/web/runner/runner.js +17 -9
- package/dist/web/sessions/db/operations.js +4 -14
- package/dist/web/sessions/state.js +2 -2
- package/dist/web/tools/agent_tool.js +6 -5
- package/dist/web/tools/load_artifacts_tool.js +150 -0
- package/dist/web/tools/load_memory_tool.js +77 -0
- package/dist/web/tools/preload_memory_tool.js +75 -0
- package/dist/web/utils/gemini_schema_util.js +85 -12
- package/dist/web/utils/logger.js +1 -0
- package/dist/web/version.js +1 -1
- package/package.json +7 -5
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { FunctionDeclaration } from '@google/genai';
|
|
7
|
+
import { BaseTool, RunAsyncToolRequest, ToolProcessLlmRequest } from './base_tool.js';
|
|
8
|
+
/**
|
|
9
|
+
* A tool that loads the artifacts and adds them to the session.
|
|
10
|
+
*/
|
|
11
|
+
export declare class LoadArtifactsTool extends BaseTool {
|
|
12
|
+
constructor();
|
|
13
|
+
_getDeclaration(): FunctionDeclaration | undefined;
|
|
14
|
+
runAsync({ args }: RunAsyncToolRequest): Promise<unknown>;
|
|
15
|
+
processLlmRequest(request: ToolProcessLlmRequest): Promise<void>;
|
|
16
|
+
private appendArtifactsToLlmRequest;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A global instance of {@link LoadArtifactsTool}.
|
|
20
|
+
*/
|
|
21
|
+
export declare const LOAD_ARTIFACTS: LoadArtifactsTool;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { FunctionDeclaration } from '@google/genai';
|
|
7
|
+
import { BaseTool, RunAsyncToolRequest, ToolProcessLlmRequest } from './base_tool.js';
|
|
8
|
+
/**
|
|
9
|
+
* A tool that loads the memory for the current user.
|
|
10
|
+
*
|
|
11
|
+
* NOTE: Currently this tool only uses text part from the memory.
|
|
12
|
+
*/
|
|
13
|
+
export declare class LoadMemoryTool extends BaseTool {
|
|
14
|
+
constructor();
|
|
15
|
+
_getDeclaration(): FunctionDeclaration | undefined;
|
|
16
|
+
runAsync({ args, toolContext, }: RunAsyncToolRequest): Promise<unknown>;
|
|
17
|
+
processLlmRequest(request: ToolProcessLlmRequest): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A global instance of {@link LoadMemoryTool}.
|
|
21
|
+
*/
|
|
22
|
+
export declare const LOAD_MEMORY: LoadMemoryTool;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { BaseTool, RunAsyncToolRequest, ToolProcessLlmRequest } from './base_tool.js';
|
|
7
|
+
/**
|
|
8
|
+
* A tool that preloads the memory for the current user.
|
|
9
|
+
*
|
|
10
|
+
* This tool will be automatically executed for each llm_request, and it won't be
|
|
11
|
+
* called by the model.
|
|
12
|
+
*
|
|
13
|
+
* NOTE: Currently this tool only uses text part from the memory.
|
|
14
|
+
*/
|
|
15
|
+
export declare class PreloadMemoryTool extends BaseTool {
|
|
16
|
+
constructor();
|
|
17
|
+
runAsync({ args, toolContext, }: RunAsyncToolRequest): Promise<unknown>;
|
|
18
|
+
processLlmRequest(request: ToolProcessLlmRequest): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A global instance of {@link PreloadMemoryTool}.
|
|
22
|
+
*/
|
|
23
|
+
export declare const PRELOAD_MEMORY: PreloadMemoryTool;
|
package/dist/types/version.d.ts
CHANGED
|
@@ -9,6 +9,16 @@ var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
|
|
|
9
9
|
MessageRole2["AGENT"] = "agent";
|
|
10
10
|
return MessageRole2;
|
|
11
11
|
})(MessageRole || {});
|
|
12
|
+
var TaskState = /* @__PURE__ */ ((TaskState2) => {
|
|
13
|
+
TaskState2["SUBMITTED"] = "submitted";
|
|
14
|
+
TaskState2["WORKING"] = "working";
|
|
15
|
+
TaskState2["COMPLETED"] = "completed";
|
|
16
|
+
TaskState2["FAILED"] = "failed";
|
|
17
|
+
TaskState2["CANCELED"] = "canceled";
|
|
18
|
+
TaskState2["REJECTED"] = "rejected";
|
|
19
|
+
TaskState2["INPUT_REQUIRED"] = "input-required";
|
|
20
|
+
return TaskState2;
|
|
21
|
+
})(TaskState || {});
|
|
12
22
|
function isTaskStatusUpdateEvent(event) {
|
|
13
23
|
return (event == null ? void 0 : event.kind) === "status-update";
|
|
14
24
|
}
|
|
@@ -31,13 +41,18 @@ function getEventMetadata(event) {
|
|
|
31
41
|
return {};
|
|
32
42
|
}
|
|
33
43
|
function isFailedTaskStatusUpdateEvent(event) {
|
|
34
|
-
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "failed"
|
|
44
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "failed" /* FAILED */;
|
|
35
45
|
}
|
|
36
46
|
function isTerminalTaskStatusUpdateEvent(event) {
|
|
37
|
-
return (isTaskStatusUpdateEvent(event) || isTask(event)) && [
|
|
47
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && [
|
|
48
|
+
"completed" /* COMPLETED */,
|
|
49
|
+
"failed" /* FAILED */,
|
|
50
|
+
"canceled" /* CANCELED */,
|
|
51
|
+
"rejected" /* REJECTED */
|
|
52
|
+
].includes(event.status.state);
|
|
38
53
|
}
|
|
39
54
|
function isInputRequiredTaskStatusUpdateEvent(event) {
|
|
40
|
-
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "input-required"
|
|
55
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "input-required" /* INPUT_REQUIRED */;
|
|
41
56
|
}
|
|
42
57
|
function getFailedTaskStatusUpdateEventError(event) {
|
|
43
58
|
var _a;
|
|
@@ -56,7 +71,8 @@ function getFailedTaskStatusUpdateEventError(event) {
|
|
|
56
71
|
function createTaskSubmittedEvent({
|
|
57
72
|
taskId,
|
|
58
73
|
contextId,
|
|
59
|
-
message
|
|
74
|
+
message,
|
|
75
|
+
metadata
|
|
60
76
|
}) {
|
|
61
77
|
return {
|
|
62
78
|
kind: "status-update",
|
|
@@ -64,16 +80,18 @@ function createTaskSubmittedEvent({
|
|
|
64
80
|
contextId,
|
|
65
81
|
final: false,
|
|
66
82
|
status: {
|
|
67
|
-
state: "submitted"
|
|
83
|
+
state: "submitted" /* SUBMITTED */,
|
|
68
84
|
message,
|
|
69
85
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
70
|
-
}
|
|
86
|
+
},
|
|
87
|
+
metadata
|
|
71
88
|
};
|
|
72
89
|
}
|
|
73
90
|
function createTask({
|
|
74
91
|
contextId,
|
|
75
92
|
message,
|
|
76
|
-
taskId
|
|
93
|
+
taskId,
|
|
94
|
+
metadata
|
|
77
95
|
}) {
|
|
78
96
|
return {
|
|
79
97
|
kind: "task",
|
|
@@ -81,15 +99,17 @@ function createTask({
|
|
|
81
99
|
contextId,
|
|
82
100
|
history: [message],
|
|
83
101
|
status: {
|
|
84
|
-
state: "submitted"
|
|
102
|
+
state: "submitted" /* SUBMITTED */,
|
|
85
103
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
86
|
-
}
|
|
104
|
+
},
|
|
105
|
+
metadata
|
|
87
106
|
};
|
|
88
107
|
}
|
|
89
108
|
function createTaskWorkingEvent({
|
|
90
109
|
taskId,
|
|
91
110
|
contextId,
|
|
92
|
-
message
|
|
111
|
+
message,
|
|
112
|
+
metadata
|
|
93
113
|
}) {
|
|
94
114
|
return {
|
|
95
115
|
kind: "status-update",
|
|
@@ -97,16 +117,17 @@ function createTaskWorkingEvent({
|
|
|
97
117
|
contextId,
|
|
98
118
|
final: false,
|
|
99
119
|
status: {
|
|
100
|
-
state: "working"
|
|
120
|
+
state: "working" /* WORKING */,
|
|
101
121
|
message,
|
|
102
122
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
103
|
-
}
|
|
123
|
+
},
|
|
124
|
+
metadata
|
|
104
125
|
};
|
|
105
126
|
}
|
|
106
127
|
function createTaskCompletedEvent({
|
|
107
128
|
taskId,
|
|
108
129
|
contextId,
|
|
109
|
-
metadata
|
|
130
|
+
metadata
|
|
110
131
|
}) {
|
|
111
132
|
return {
|
|
112
133
|
kind: "status-update",
|
|
@@ -114,7 +135,7 @@ function createTaskCompletedEvent({
|
|
|
114
135
|
contextId,
|
|
115
136
|
final: true,
|
|
116
137
|
status: {
|
|
117
|
-
state: "completed"
|
|
138
|
+
state: "completed" /* COMPLETED */,
|
|
118
139
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
119
140
|
},
|
|
120
141
|
metadata
|
|
@@ -137,9 +158,9 @@ function createTaskArtifactUpdateEvent({
|
|
|
137
158
|
lastChunk,
|
|
138
159
|
artifact: {
|
|
139
160
|
artifactId: artifactId || randomUUID(),
|
|
140
|
-
parts
|
|
141
|
-
|
|
142
|
-
|
|
161
|
+
parts
|
|
162
|
+
},
|
|
163
|
+
metadata
|
|
143
164
|
};
|
|
144
165
|
}
|
|
145
166
|
function createTaskFailedEvent({
|
|
@@ -152,8 +173,9 @@ function createTaskFailedEvent({
|
|
|
152
173
|
kind: "status-update",
|
|
153
174
|
taskId,
|
|
154
175
|
contextId,
|
|
176
|
+
final: true,
|
|
155
177
|
status: {
|
|
156
|
-
state: "failed"
|
|
178
|
+
state: "failed" /* FAILED */,
|
|
157
179
|
message: {
|
|
158
180
|
kind: "message",
|
|
159
181
|
messageId: randomUUID(),
|
|
@@ -169,14 +191,14 @@ function createTaskFailedEvent({
|
|
|
169
191
|
},
|
|
170
192
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
171
193
|
},
|
|
172
|
-
metadata
|
|
173
|
-
final: true
|
|
194
|
+
metadata
|
|
174
195
|
};
|
|
175
196
|
}
|
|
176
197
|
function createTaskInputRequiredEvent({
|
|
177
198
|
taskId,
|
|
178
199
|
contextId,
|
|
179
|
-
parts
|
|
200
|
+
parts,
|
|
201
|
+
metadata
|
|
180
202
|
}) {
|
|
181
203
|
return {
|
|
182
204
|
kind: "status-update",
|
|
@@ -184,7 +206,7 @@ function createTaskInputRequiredEvent({
|
|
|
184
206
|
contextId,
|
|
185
207
|
final: true,
|
|
186
208
|
status: {
|
|
187
|
-
state: "input-required"
|
|
209
|
+
state: "input-required" /* INPUT_REQUIRED */,
|
|
188
210
|
message: {
|
|
189
211
|
kind: "message",
|
|
190
212
|
messageId: randomUUID(),
|
|
@@ -194,13 +216,15 @@ function createTaskInputRequiredEvent({
|
|
|
194
216
|
parts
|
|
195
217
|
},
|
|
196
218
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
197
|
-
}
|
|
219
|
+
},
|
|
220
|
+
metadata
|
|
198
221
|
};
|
|
199
222
|
}
|
|
200
223
|
function createInputMissingErrorEvent({
|
|
201
224
|
taskId,
|
|
202
225
|
contextId,
|
|
203
|
-
parts
|
|
226
|
+
parts,
|
|
227
|
+
metadata
|
|
204
228
|
}) {
|
|
205
229
|
return {
|
|
206
230
|
kind: "status-update",
|
|
@@ -208,7 +232,7 @@ function createInputMissingErrorEvent({
|
|
|
208
232
|
contextId,
|
|
209
233
|
final: true,
|
|
210
234
|
status: {
|
|
211
|
-
state: "input-required"
|
|
235
|
+
state: "input-required" /* INPUT_REQUIRED */,
|
|
212
236
|
message: {
|
|
213
237
|
kind: "message",
|
|
214
238
|
messageId: randomUUID(),
|
|
@@ -218,11 +242,13 @@ function createInputMissingErrorEvent({
|
|
|
218
242
|
parts
|
|
219
243
|
},
|
|
220
244
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
221
|
-
}
|
|
245
|
+
},
|
|
246
|
+
metadata
|
|
222
247
|
};
|
|
223
248
|
}
|
|
224
249
|
export {
|
|
225
250
|
MessageRole,
|
|
251
|
+
TaskState,
|
|
226
252
|
createInputMissingErrorEvent,
|
|
227
253
|
createTask,
|
|
228
254
|
createTaskArtifactUpdateEvent,
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
2
|
+
var __await = function(promise, isYieldStar) {
|
|
3
|
+
this[0] = promise;
|
|
4
|
+
this[1] = isYieldStar;
|
|
5
|
+
};
|
|
6
|
+
var __asyncGenerator = (__this, __arguments, generator) => {
|
|
7
|
+
var resume = (k, v, yes, no) => {
|
|
8
|
+
try {
|
|
9
|
+
var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
|
|
10
|
+
Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
|
|
11
|
+
} catch (e) {
|
|
12
|
+
no(e);
|
|
13
|
+
}
|
|
14
|
+
}, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
|
|
15
|
+
return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
|
|
16
|
+
};
|
|
17
|
+
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
18
|
+
/**
|
|
19
|
+
* @license
|
|
20
|
+
* Copyright 2026 Google LLC
|
|
21
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
22
|
+
*/
|
|
23
|
+
import {
|
|
24
|
+
AGENT_CARD_PATH
|
|
25
|
+
} from "@a2a-js/sdk";
|
|
26
|
+
import { ClientFactory } from "@a2a-js/sdk/client";
|
|
27
|
+
import { BaseAgent } from "../agents/base_agent.js";
|
|
28
|
+
import { createEvent } from "../events/event.js";
|
|
29
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
30
|
+
import { logger } from "../utils/logger.js";
|
|
31
|
+
import { MessageRole } from "./a2a_event.js";
|
|
32
|
+
import { A2ARemoteAgentRunProcessor } from "./a2a_remote_agent_run_processor.js";
|
|
33
|
+
import {
|
|
34
|
+
getUserFunctionCallAt,
|
|
35
|
+
toMissingRemoteSessionParts
|
|
36
|
+
} from "./a2a_remote_agent_utils.js";
|
|
37
|
+
import { resolveAgentCard } from "./agent_card.js";
|
|
38
|
+
import { toAdkEvent } from "./event_converter_utils.js";
|
|
39
|
+
import { getA2ASessionMetadata } from "./metadata_converter_utils.js";
|
|
40
|
+
import { toA2AParts } from "./part_converter_utils.js";
|
|
41
|
+
class RemoteA2AAgent extends BaseAgent {
|
|
42
|
+
constructor(a2aConfig) {
|
|
43
|
+
super(a2aConfig);
|
|
44
|
+
this.a2aConfig = a2aConfig;
|
|
45
|
+
this.isInitialized = false;
|
|
46
|
+
if (!a2aConfig.agentCard && !a2aConfig.client) {
|
|
47
|
+
throw new Error("Either AgentCard or Client must be provided");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async init() {
|
|
51
|
+
if (this.isInitialized) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (this.a2aConfig.client) {
|
|
55
|
+
this.client = this.a2aConfig.client;
|
|
56
|
+
}
|
|
57
|
+
if (this.a2aConfig.agentCard) {
|
|
58
|
+
this.card = await resolveAgentCard(this.a2aConfig.agentCard);
|
|
59
|
+
if (!this.client) {
|
|
60
|
+
const factory = this.a2aConfig.clientFactory || new ClientFactory();
|
|
61
|
+
this.client = await factory.createFromAgentCard(this.card);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
this.isInitialized = true;
|
|
65
|
+
}
|
|
66
|
+
runAsyncImpl(context) {
|
|
67
|
+
return __asyncGenerator(this, null, function* () {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
yield new __await(this.init());
|
|
70
|
+
try {
|
|
71
|
+
const events = context.session.events;
|
|
72
|
+
if (events.length === 0) {
|
|
73
|
+
throw new Error("No events in session to send");
|
|
74
|
+
}
|
|
75
|
+
const userFnCall = getUserFunctionCallAt(
|
|
76
|
+
context.session,
|
|
77
|
+
events.length - 1
|
|
78
|
+
);
|
|
79
|
+
let parts;
|
|
80
|
+
let taskId = void 0;
|
|
81
|
+
let contextId = void 0;
|
|
82
|
+
if (userFnCall) {
|
|
83
|
+
const event = userFnCall.response;
|
|
84
|
+
parts = toA2AParts(
|
|
85
|
+
((_a = event.content) == null ? void 0 : _a.parts) || [],
|
|
86
|
+
event.longRunningToolIds
|
|
87
|
+
);
|
|
88
|
+
taskId = userFnCall.taskId;
|
|
89
|
+
contextId = userFnCall.contextId;
|
|
90
|
+
} else {
|
|
91
|
+
const missing = toMissingRemoteSessionParts(context, context.session);
|
|
92
|
+
parts = missing.parts;
|
|
93
|
+
contextId = missing.contextId;
|
|
94
|
+
}
|
|
95
|
+
const message = {
|
|
96
|
+
kind: "message",
|
|
97
|
+
messageId: randomUUID(),
|
|
98
|
+
role: MessageRole.USER,
|
|
99
|
+
parts,
|
|
100
|
+
metadata: getA2ASessionMetadata({
|
|
101
|
+
appName: context.session.appName,
|
|
102
|
+
userId: context.session.userId,
|
|
103
|
+
sessionId: context.session.id
|
|
104
|
+
})
|
|
105
|
+
};
|
|
106
|
+
if (taskId) message.taskId = taskId;
|
|
107
|
+
if (contextId) message.contextId = contextId;
|
|
108
|
+
const params = {
|
|
109
|
+
message,
|
|
110
|
+
configuration: this.a2aConfig.messageSendConfig
|
|
111
|
+
};
|
|
112
|
+
const processor = new A2ARemoteAgentRunProcessor(params);
|
|
113
|
+
if (this.a2aConfig.beforeRequestCallbacks) {
|
|
114
|
+
for (const callback of this.a2aConfig.beforeRequestCallbacks) {
|
|
115
|
+
yield new __await(callback(context, params));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const useStreaming = this.card ? ((_b = this.card.capabilities) == null ? void 0 : _b.streaming) !== false : true;
|
|
119
|
+
if (useStreaming) {
|
|
120
|
+
try {
|
|
121
|
+
for (var iter = __forAwait(this.client.sendMessageStream(params)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
|
|
122
|
+
const chunk = temp.value;
|
|
123
|
+
if (this.a2aConfig.afterRequestCallbacks) {
|
|
124
|
+
for (const callback of this.a2aConfig.afterRequestCallbacks) {
|
|
125
|
+
yield new __await(callback(context, chunk));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const adkEvent = toAdkEvent(chunk, context.invocationId, this.name);
|
|
129
|
+
if (!adkEvent) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
processor.updateCustomMetadata(adkEvent, chunk);
|
|
133
|
+
const eventsToEmit = processor.aggregatePartial(
|
|
134
|
+
context,
|
|
135
|
+
chunk,
|
|
136
|
+
adkEvent
|
|
137
|
+
);
|
|
138
|
+
for (const ev of eventsToEmit) {
|
|
139
|
+
yield ev;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} catch (temp) {
|
|
143
|
+
error = [temp];
|
|
144
|
+
} finally {
|
|
145
|
+
try {
|
|
146
|
+
more && (temp = iter.return) && (yield new __await(temp.call(iter)));
|
|
147
|
+
} finally {
|
|
148
|
+
if (error)
|
|
149
|
+
throw error[0];
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
} else {
|
|
153
|
+
const result = yield new __await(this.client.sendMessage(params));
|
|
154
|
+
if (this.a2aConfig.afterRequestCallbacks) {
|
|
155
|
+
for (const callback of this.a2aConfig.afterRequestCallbacks) {
|
|
156
|
+
yield new __await(callback(context, result));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const adkEvent = toAdkEvent(result, context.invocationId, this.name);
|
|
160
|
+
if (adkEvent) {
|
|
161
|
+
processor.updateCustomMetadata(adkEvent, result);
|
|
162
|
+
yield adkEvent;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
} catch (e) {
|
|
166
|
+
const error2 = e;
|
|
167
|
+
logger.error("A2ARemoteAgent ".concat(this.name, " failed:"), error2);
|
|
168
|
+
yield createEvent({
|
|
169
|
+
author: this.name,
|
|
170
|
+
invocationId: context.invocationId,
|
|
171
|
+
errorMessage: error2.message,
|
|
172
|
+
turnComplete: true
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
runLiveImpl(_context) {
|
|
178
|
+
throw new Error("Live mode is not supported in A2ARemoteAgent yet.");
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
export {
|
|
182
|
+
AGENT_CARD_PATH,
|
|
183
|
+
RemoteA2AAgent
|
|
184
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
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
|
+
if (isTask(response)) {
|
|
161
|
+
if (response.id) toAdd["task_id"] = response.id;
|
|
162
|
+
if (response.contextId) toAdd["context_id"] = response.contextId;
|
|
163
|
+
} else if (response.taskId) {
|
|
164
|
+
toAdd["task_id"] = response.taskId;
|
|
165
|
+
if (response.contextId) toAdd["context_id"] = response.contextId;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (Object.keys(toAdd).length === 0) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (!event.customMetadata) {
|
|
172
|
+
event.customMetadata = {};
|
|
173
|
+
}
|
|
174
|
+
for (const [k, v] of Object.entries(toAdd)) {
|
|
175
|
+
if (v === void 0 || v === null) continue;
|
|
176
|
+
event.customMetadata["a2a:".concat(k)] = v;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
export {
|
|
181
|
+
A2ARemoteAgentRunProcessor
|
|
182
|
+
};
|