@google/adk 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/a2a/a2a_event.js +290 -0
- package/dist/cjs/a2a/event_converter_utils.js +201 -0
- package/dist/cjs/a2a/executor_context.js +53 -0
- package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
- package/dist/cjs/a2a/part_converter_utils.js +23 -21
- 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 +21 -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/processors/content_request_processor.js +66 -0
- package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
- package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
- package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
- package/dist/cjs/common.js +11 -7
- package/dist/cjs/index.js +89 -53
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/models/llm_response.js +2 -0
- package/dist/cjs/plugins/base_plugin.js +1 -1
- package/dist/cjs/runner/runner.js +1 -1
- package/dist/cjs/sessions/database_session_service.js +4 -1
- package/dist/cjs/sessions/db/operations.js +24 -12
- 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/mcp/mcp_toolset.js +9 -5
- package/dist/cjs/utils/logger.js +61 -54
- package/dist/cjs/version.js +1 -1
- package/dist/esm/a2a/a2a_event.js +243 -0
- package/dist/esm/a2a/event_converter_utils.js +187 -0
- package/dist/esm/a2a/executor_context.js +23 -0
- package/dist/esm/a2a/metadata_converter_utils.js +90 -0
- package/dist/esm/a2a/part_converter_utils.js +25 -21
- package/dist/esm/agents/base_agent.js +3 -3
- package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/esm/agents/functions.js +2 -2
- package/dist/esm/agents/llm_agent.js +14 -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/processors/content_request_processor.js +38 -0
- package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
- package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
- package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
- package/dist/esm/common.js +11 -9
- package/dist/esm/index.js +95 -18
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/models/llm_response.js +2 -0
- package/dist/esm/plugins/base_plugin.js +1 -1
- package/dist/esm/runner/runner.js +1 -1
- package/dist/esm/sessions/database_session_service.js +4 -1
- package/dist/esm/sessions/db/operations.js +31 -7
- 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/mcp/mcp_toolset.js +9 -5
- package/dist/esm/utils/logger.js +51 -54
- package/dist/esm/version.js +1 -1
- package/dist/types/a2a/a2a_event.d.ts +122 -0
- package/dist/types/a2a/event_converter_utils.d.ts +20 -0
- package/dist/types/a2a/executor_context.d.ts +33 -0
- package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
- package/dist/types/a2a/part_converter_utils.d.ts +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 +8 -31
- package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
- package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
- package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
- package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
- package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
- package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
- package/dist/types/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 +4 -4
- 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/sessions/database_session_service.d.ts +2 -1
- package/dist/types/sessions/db/operations.d.ts +1 -1
- 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/mcp/mcp_toolset.d.ts +1 -1
- package/dist/types/utils/logger.d.ts +5 -9
- package/dist/types/version.d.ts +1 -1
- package/dist/web/a2a/a2a_event.js +243 -0
- package/dist/web/a2a/event_converter_utils.js +201 -0
- package/dist/web/a2a/executor_context.js +23 -0
- package/dist/web/a2a/metadata_converter_utils.js +107 -0
- package/dist/web/a2a/part_converter_utils.js +25 -21
- package/dist/web/agents/base_agent.js +3 -3
- package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
- package/dist/web/agents/functions.js +2 -2
- package/dist/web/agents/llm_agent.js +14 -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/processors/content_request_processor.js +56 -0
- package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
- package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
- package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
- package/dist/web/common.js +11 -9
- package/dist/web/index.js +13 -18
- package/dist/web/index.js.map +7 -0
- package/dist/web/models/llm_response.js +2 -0
- package/dist/web/plugins/base_plugin.js +1 -1
- package/dist/web/runner/runner.js +1 -1
- package/dist/web/sessions/database_session_service.js +4 -1
- package/dist/web/sessions/db/operations.js +31 -7
- 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/mcp/mcp_toolset.js +27 -5
- package/dist/web/utils/logger.js +51 -54
- package/dist/web/version.js +1 -1
- package/package.json +3 -2
- 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,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
7
|
+
var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
|
|
8
|
+
MessageRole2["USER"] = "user";
|
|
9
|
+
MessageRole2["AGENT"] = "agent";
|
|
10
|
+
return MessageRole2;
|
|
11
|
+
})(MessageRole || {});
|
|
12
|
+
function isTaskStatusUpdateEvent(event) {
|
|
13
|
+
return (event == null ? void 0 : event.kind) === "status-update";
|
|
14
|
+
}
|
|
15
|
+
function isTaskArtifactUpdateEvent(event) {
|
|
16
|
+
return (event == null ? void 0 : event.kind) === "artifact-update";
|
|
17
|
+
}
|
|
18
|
+
function isMessage(event) {
|
|
19
|
+
return (event == null ? void 0 : event.kind) === "message";
|
|
20
|
+
}
|
|
21
|
+
function isTask(event) {
|
|
22
|
+
return (event == null ? void 0 : event.kind) === "task";
|
|
23
|
+
}
|
|
24
|
+
function getEventMetadata(event) {
|
|
25
|
+
if (isTaskArtifactUpdateEvent(event)) {
|
|
26
|
+
return event.artifact.metadata || {};
|
|
27
|
+
}
|
|
28
|
+
if (isTaskStatusUpdateEvent(event) || isTask(event) || isMessage(event)) {
|
|
29
|
+
return event.metadata || {};
|
|
30
|
+
}
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
function isFailedTaskStatusUpdateEvent(event) {
|
|
34
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "failed";
|
|
35
|
+
}
|
|
36
|
+
function isTerminalTaskStatusUpdateEvent(event) {
|
|
37
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && ["completed", "failed", "canceled", "rejected"].includes(event.status.state);
|
|
38
|
+
}
|
|
39
|
+
function isInputRequiredTaskStatusUpdateEvent(event) {
|
|
40
|
+
return (isTaskStatusUpdateEvent(event) || isTask(event)) && event.status.state === "input-required";
|
|
41
|
+
}
|
|
42
|
+
function getFailedTaskStatusUpdateEventError(event) {
|
|
43
|
+
var _a;
|
|
44
|
+
if (!isFailedTaskStatusUpdateEvent(event)) {
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
const parts = ((_a = event.status.message) == null ? void 0 : _a.parts) || [];
|
|
48
|
+
if (parts.length === 0) {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
if (parts[0].kind !== "text") {
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
return parts[0].text;
|
|
55
|
+
}
|
|
56
|
+
function createTaskSubmittedEvent({
|
|
57
|
+
taskId,
|
|
58
|
+
contextId,
|
|
59
|
+
message
|
|
60
|
+
}) {
|
|
61
|
+
return {
|
|
62
|
+
kind: "status-update",
|
|
63
|
+
taskId,
|
|
64
|
+
contextId,
|
|
65
|
+
final: false,
|
|
66
|
+
status: {
|
|
67
|
+
state: "submitted",
|
|
68
|
+
message,
|
|
69
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function createTask({
|
|
74
|
+
contextId,
|
|
75
|
+
message,
|
|
76
|
+
taskId
|
|
77
|
+
}) {
|
|
78
|
+
return {
|
|
79
|
+
kind: "task",
|
|
80
|
+
id: taskId || randomUUID(),
|
|
81
|
+
contextId,
|
|
82
|
+
history: [message],
|
|
83
|
+
status: {
|
|
84
|
+
state: "submitted",
|
|
85
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function createTaskWorkingEvent({
|
|
90
|
+
taskId,
|
|
91
|
+
contextId,
|
|
92
|
+
message
|
|
93
|
+
}) {
|
|
94
|
+
return {
|
|
95
|
+
kind: "status-update",
|
|
96
|
+
taskId,
|
|
97
|
+
contextId,
|
|
98
|
+
final: false,
|
|
99
|
+
status: {
|
|
100
|
+
state: "working",
|
|
101
|
+
message,
|
|
102
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function createTaskCompletedEvent({
|
|
107
|
+
taskId,
|
|
108
|
+
contextId,
|
|
109
|
+
metadata = {}
|
|
110
|
+
}) {
|
|
111
|
+
return {
|
|
112
|
+
kind: "status-update",
|
|
113
|
+
taskId,
|
|
114
|
+
contextId,
|
|
115
|
+
final: true,
|
|
116
|
+
status: {
|
|
117
|
+
state: "completed",
|
|
118
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
119
|
+
},
|
|
120
|
+
metadata
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function createTaskArtifactUpdateEvent({
|
|
124
|
+
taskId,
|
|
125
|
+
contextId,
|
|
126
|
+
artifactId,
|
|
127
|
+
parts = [],
|
|
128
|
+
metadata,
|
|
129
|
+
append,
|
|
130
|
+
lastChunk
|
|
131
|
+
}) {
|
|
132
|
+
return {
|
|
133
|
+
kind: "artifact-update",
|
|
134
|
+
taskId,
|
|
135
|
+
contextId,
|
|
136
|
+
append,
|
|
137
|
+
lastChunk,
|
|
138
|
+
artifact: {
|
|
139
|
+
artifactId: artifactId || randomUUID(),
|
|
140
|
+
parts,
|
|
141
|
+
metadata
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
function createTaskFailedEvent({
|
|
146
|
+
taskId,
|
|
147
|
+
contextId,
|
|
148
|
+
error,
|
|
149
|
+
metadata
|
|
150
|
+
}) {
|
|
151
|
+
return {
|
|
152
|
+
kind: "status-update",
|
|
153
|
+
taskId,
|
|
154
|
+
contextId,
|
|
155
|
+
status: {
|
|
156
|
+
state: "failed",
|
|
157
|
+
message: {
|
|
158
|
+
kind: "message",
|
|
159
|
+
messageId: randomUUID(),
|
|
160
|
+
role: "agent",
|
|
161
|
+
taskId,
|
|
162
|
+
contextId,
|
|
163
|
+
parts: [
|
|
164
|
+
{
|
|
165
|
+
kind: "text",
|
|
166
|
+
text: error.message
|
|
167
|
+
}
|
|
168
|
+
]
|
|
169
|
+
},
|
|
170
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
171
|
+
},
|
|
172
|
+
metadata,
|
|
173
|
+
final: true
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function createTaskInputRequiredEvent({
|
|
177
|
+
taskId,
|
|
178
|
+
contextId,
|
|
179
|
+
parts
|
|
180
|
+
}) {
|
|
181
|
+
return {
|
|
182
|
+
kind: "status-update",
|
|
183
|
+
taskId,
|
|
184
|
+
contextId,
|
|
185
|
+
final: true,
|
|
186
|
+
status: {
|
|
187
|
+
state: "input-required",
|
|
188
|
+
message: {
|
|
189
|
+
kind: "message",
|
|
190
|
+
messageId: randomUUID(),
|
|
191
|
+
role: "agent",
|
|
192
|
+
taskId,
|
|
193
|
+
contextId,
|
|
194
|
+
parts
|
|
195
|
+
},
|
|
196
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function createInputMissingErrorEvent({
|
|
201
|
+
taskId,
|
|
202
|
+
contextId,
|
|
203
|
+
parts
|
|
204
|
+
}) {
|
|
205
|
+
return {
|
|
206
|
+
kind: "status-update",
|
|
207
|
+
taskId,
|
|
208
|
+
contextId,
|
|
209
|
+
final: true,
|
|
210
|
+
status: {
|
|
211
|
+
state: "input-required",
|
|
212
|
+
message: {
|
|
213
|
+
kind: "message",
|
|
214
|
+
messageId: randomUUID(),
|
|
215
|
+
role: "agent",
|
|
216
|
+
taskId,
|
|
217
|
+
contextId,
|
|
218
|
+
parts
|
|
219
|
+
},
|
|
220
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
export {
|
|
225
|
+
MessageRole,
|
|
226
|
+
createInputMissingErrorEvent,
|
|
227
|
+
createTask,
|
|
228
|
+
createTaskArtifactUpdateEvent,
|
|
229
|
+
createTaskCompletedEvent,
|
|
230
|
+
createTaskFailedEvent,
|
|
231
|
+
createTaskInputRequiredEvent,
|
|
232
|
+
createTaskSubmittedEvent,
|
|
233
|
+
createTaskWorkingEvent,
|
|
234
|
+
getEventMetadata,
|
|
235
|
+
getFailedTaskStatusUpdateEventError,
|
|
236
|
+
isFailedTaskStatusUpdateEvent,
|
|
237
|
+
isInputRequiredTaskStatusUpdateEvent,
|
|
238
|
+
isMessage,
|
|
239
|
+
isTask,
|
|
240
|
+
isTaskArtifactUpdateEvent,
|
|
241
|
+
isTaskStatusUpdateEvent,
|
|
242
|
+
isTerminalTaskStatusUpdateEvent
|
|
243
|
+
};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
createModelContent,
|
|
8
|
+
createUserContent
|
|
9
|
+
} from "@google/genai";
|
|
10
|
+
import { createEvent } from "../events/event.js";
|
|
11
|
+
import { createEventActions } from "../events/event_actions.js";
|
|
12
|
+
import { randomUUID } from "../utils/env_aware_utils.js";
|
|
13
|
+
import {
|
|
14
|
+
getEventMetadata,
|
|
15
|
+
getFailedTaskStatusUpdateEventError,
|
|
16
|
+
isFailedTaskStatusUpdateEvent,
|
|
17
|
+
isInputRequiredTaskStatusUpdateEvent,
|
|
18
|
+
isMessage,
|
|
19
|
+
isTask,
|
|
20
|
+
isTaskArtifactUpdateEvent,
|
|
21
|
+
isTaskStatusUpdateEvent,
|
|
22
|
+
isTerminalTaskStatusUpdateEvent,
|
|
23
|
+
MessageRole
|
|
24
|
+
} from "./a2a_event.js";
|
|
25
|
+
import {
|
|
26
|
+
A2AMetadataKeys,
|
|
27
|
+
getA2AEventMetadata
|
|
28
|
+
} from "./metadata_converter_utils.js";
|
|
29
|
+
import { toA2AParts, toGenAIPart, toGenAIParts } from "./part_converter_utils.js";
|
|
30
|
+
function toA2AMessage(event, {
|
|
31
|
+
appName,
|
|
32
|
+
userId,
|
|
33
|
+
sessionId
|
|
34
|
+
}) {
|
|
35
|
+
var _a;
|
|
36
|
+
return {
|
|
37
|
+
kind: "message",
|
|
38
|
+
messageId: randomUUID(),
|
|
39
|
+
role: event.author === MessageRole.USER ? MessageRole.USER : MessageRole.AGENT,
|
|
40
|
+
parts: toA2AParts(((_a = event.content) == null ? void 0 : _a.parts) || [], event.longRunningToolIds),
|
|
41
|
+
metadata: getA2AEventMetadata(event, { appName, userId, sessionId })
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function toAdkEvent(event, invocationId, agentName) {
|
|
45
|
+
if (isMessage(event)) {
|
|
46
|
+
return messageToAdkEvent(event, invocationId, agentName);
|
|
47
|
+
}
|
|
48
|
+
if (isTask(event)) {
|
|
49
|
+
return taskToAdkEvent(event, invocationId, agentName);
|
|
50
|
+
}
|
|
51
|
+
if (isTaskArtifactUpdateEvent(event)) {
|
|
52
|
+
return artifactUpdateToAdkEvent(event, invocationId, agentName);
|
|
53
|
+
}
|
|
54
|
+
if (isTaskStatusUpdateEvent(event)) {
|
|
55
|
+
return event.final ? finalTaskStatusUpdateToAdkEvent(event, invocationId, agentName) : taskStatusUpdateToAdkEvent(event, invocationId, agentName);
|
|
56
|
+
}
|
|
57
|
+
return void 0;
|
|
58
|
+
}
|
|
59
|
+
function messageToAdkEvent(msg, invocationId, agentName, parentEvent) {
|
|
60
|
+
const parts = toGenAIParts(msg.parts);
|
|
61
|
+
return {
|
|
62
|
+
...createAdkEventFromMetadata(parentEvent || msg),
|
|
63
|
+
invocationId,
|
|
64
|
+
author: msg.role === MessageRole.USER ? MessageRole.USER : agentName,
|
|
65
|
+
content: msg.role === MessageRole.USER ? createUserContent(parts) : createModelContent(parts),
|
|
66
|
+
turnComplete: true,
|
|
67
|
+
partial: false
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function artifactUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
71
|
+
var _a;
|
|
72
|
+
const partsToConvert = ((_a = a2aEvent.artifact) == null ? void 0 : _a.parts) || [];
|
|
73
|
+
if (partsToConvert.length === 0) {
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
const partial = !!getEventMetadata(a2aEvent)[A2AMetadataKeys.PARTIAL] || a2aEvent.append || !a2aEvent.lastChunk;
|
|
77
|
+
return {
|
|
78
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
79
|
+
invocationId,
|
|
80
|
+
author: agentName,
|
|
81
|
+
content: createModelContent(toGenAIParts(partsToConvert)),
|
|
82
|
+
longRunningToolIds: getLongRunningToolIDs(partsToConvert),
|
|
83
|
+
partial
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function finalTaskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
87
|
+
var _a;
|
|
88
|
+
const partsToConvert = ((_a = a2aEvent.status.message) == null ? void 0 : _a.parts) || [];
|
|
89
|
+
if (partsToConvert.length === 0) {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
const parts = toGenAIParts(partsToConvert);
|
|
93
|
+
const isFailedTask = isFailedTaskStatusUpdateEvent(a2aEvent);
|
|
94
|
+
const hasContent = !isFailedTask && parts.length > 0;
|
|
95
|
+
return {
|
|
96
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
97
|
+
invocationId,
|
|
98
|
+
author: agentName,
|
|
99
|
+
errorMessage: isFailedTask ? getFailedTaskStatusUpdateEventError(a2aEvent) : void 0,
|
|
100
|
+
content: hasContent ? createModelContent(parts) : void 0,
|
|
101
|
+
longRunningToolIds: getLongRunningToolIDs(partsToConvert),
|
|
102
|
+
turnComplete: true
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function taskStatusUpdateToAdkEvent(a2aEvent, invocationId, agentName) {
|
|
106
|
+
const msg = a2aEvent.status.message;
|
|
107
|
+
if (!msg) {
|
|
108
|
+
return void 0;
|
|
109
|
+
}
|
|
110
|
+
const parts = toGenAIParts(msg.parts);
|
|
111
|
+
return {
|
|
112
|
+
...createAdkEventFromMetadata(a2aEvent),
|
|
113
|
+
invocationId,
|
|
114
|
+
author: agentName,
|
|
115
|
+
content: createModelContent(parts),
|
|
116
|
+
turnComplete: false,
|
|
117
|
+
partial: true
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function taskToAdkEvent(a2aTask, invocationId, agentName) {
|
|
121
|
+
var _a, _b;
|
|
122
|
+
const parts = [];
|
|
123
|
+
const longRunningToolIds = [];
|
|
124
|
+
if (a2aTask.artifacts) {
|
|
125
|
+
for (const artifact of a2aTask.artifacts) {
|
|
126
|
+
if (((_a = artifact.parts) == null ? void 0 : _a.length) > 0) {
|
|
127
|
+
const artifactParts = toGenAIParts(artifact.parts);
|
|
128
|
+
parts.push(...artifactParts);
|
|
129
|
+
longRunningToolIds.push(...getLongRunningToolIDs(artifact.parts));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if ((_b = a2aTask.status) == null ? void 0 : _b.message) {
|
|
134
|
+
const a2aParts = a2aTask.status.message.parts;
|
|
135
|
+
const genAIParts = toGenAIParts(a2aParts);
|
|
136
|
+
parts.push(...genAIParts);
|
|
137
|
+
longRunningToolIds.push(...getLongRunningToolIDs(a2aParts));
|
|
138
|
+
}
|
|
139
|
+
const isTerminal = isTerminalTaskStatusUpdateEvent(a2aTask) || isInputRequiredTaskStatusUpdateEvent(a2aTask);
|
|
140
|
+
const isFailed = isFailedTaskStatusUpdateEvent(a2aTask);
|
|
141
|
+
if (parts.length === 0 && !isTerminal) {
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
...createAdkEventFromMetadata(a2aTask),
|
|
146
|
+
invocationId,
|
|
147
|
+
author: agentName,
|
|
148
|
+
content: isFailed ? void 0 : createModelContent(parts),
|
|
149
|
+
errorMessage: isFailed ? getFailedTaskStatusUpdateEventError(a2aTask) : void 0,
|
|
150
|
+
longRunningToolIds,
|
|
151
|
+
turnComplete: isTerminal
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function createAdkEventFromMetadata(a2aEvent) {
|
|
155
|
+
const metadata = a2aEvent.metadata || {};
|
|
156
|
+
return createEvent({
|
|
157
|
+
branch: metadata[A2AMetadataKeys.BRANCH],
|
|
158
|
+
author: metadata[A2AMetadataKeys.AUTHOR],
|
|
159
|
+
partial: metadata[A2AMetadataKeys.PARTIAL],
|
|
160
|
+
errorCode: metadata[A2AMetadataKeys.ERROR_CODE],
|
|
161
|
+
errorMessage: metadata[A2AMetadataKeys.ERROR_MESSAGE],
|
|
162
|
+
citationMetadata: metadata[A2AMetadataKeys.CITATION_METADATA],
|
|
163
|
+
groundingMetadata: metadata[A2AMetadataKeys.GROUNDING_METADATA],
|
|
164
|
+
usageMetadata: metadata[A2AMetadataKeys.USAGE_METADATA],
|
|
165
|
+
customMetadata: metadata[A2AMetadataKeys.CUSTOM_METADATA],
|
|
166
|
+
actions: createEventActions({
|
|
167
|
+
escalate: !!metadata[A2AMetadataKeys.ESCALATE],
|
|
168
|
+
transferToAgent: metadata[A2AMetadataKeys.TRANSFER_TO_AGENT]
|
|
169
|
+
})
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
function getLongRunningToolIDs(parts) {
|
|
173
|
+
const ids = [];
|
|
174
|
+
for (const a2aPart of parts) {
|
|
175
|
+
if (a2aPart.metadata && a2aPart.metadata[A2AMetadataKeys.IS_LONG_RUNNING]) {
|
|
176
|
+
const genAIPart = toGenAIPart(a2aPart);
|
|
177
|
+
if (genAIPart.functionCall && genAIPart.functionCall.id) {
|
|
178
|
+
ids.push(genAIPart.functionCall.id);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return ids;
|
|
183
|
+
}
|
|
184
|
+
export {
|
|
185
|
+
toA2AMessage,
|
|
186
|
+
toAdkEvent
|
|
187
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
function createExecutorContext({
|
|
7
|
+
session,
|
|
8
|
+
userContent,
|
|
9
|
+
requestContext
|
|
10
|
+
}) {
|
|
11
|
+
return {
|
|
12
|
+
userId: session.userId,
|
|
13
|
+
sessionId: session.id,
|
|
14
|
+
agentName: session.appName,
|
|
15
|
+
readonlyState: session.state,
|
|
16
|
+
events: session.events,
|
|
17
|
+
userContent,
|
|
18
|
+
requestContext
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
createExecutorContext
|
|
23
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { isTask } from "./a2a_event.js";
|
|
7
|
+
const ADK_METADATA_KEY_PREFIX = "adk_";
|
|
8
|
+
const A2A_METADATA_KEY_PREFIX = "a2a:";
|
|
9
|
+
var A2AMetadataKeys = ((A2AMetadataKeys2) => {
|
|
10
|
+
A2AMetadataKeys2["APP_NAME"] = `${ADK_METADATA_KEY_PREFIX}app_name`;
|
|
11
|
+
A2AMetadataKeys2["USER_ID"] = `${ADK_METADATA_KEY_PREFIX}user_id`;
|
|
12
|
+
A2AMetadataKeys2["SESSION_ID"] = `${ADK_METADATA_KEY_PREFIX}session_id`;
|
|
13
|
+
A2AMetadataKeys2["INVOCATION_ID"] = `${ADK_METADATA_KEY_PREFIX}invocation_id`;
|
|
14
|
+
A2AMetadataKeys2["AUTHOR"] = `${ADK_METADATA_KEY_PREFIX}author`;
|
|
15
|
+
A2AMetadataKeys2["BRANCH"] = `${ADK_METADATA_KEY_PREFIX}branch`;
|
|
16
|
+
A2AMetadataKeys2["DATA_PART_TYPE"] = `${ADK_METADATA_KEY_PREFIX}type`;
|
|
17
|
+
A2AMetadataKeys2["PARTIAL"] = `${ADK_METADATA_KEY_PREFIX}partial`;
|
|
18
|
+
A2AMetadataKeys2["ESCALATE"] = `${ADK_METADATA_KEY_PREFIX}escalate`;
|
|
19
|
+
A2AMetadataKeys2["TRANSFER_TO_AGENT"] = `${ADK_METADATA_KEY_PREFIX}transfer_to_agent`;
|
|
20
|
+
A2AMetadataKeys2["IS_LONG_RUNNING"] = `${ADK_METADATA_KEY_PREFIX}is_long_running`;
|
|
21
|
+
A2AMetadataKeys2["THOUGHT"] = `${ADK_METADATA_KEY_PREFIX}thought`;
|
|
22
|
+
A2AMetadataKeys2["ERROR_CODE"] = `${ADK_METADATA_KEY_PREFIX}error_code`;
|
|
23
|
+
A2AMetadataKeys2["ERROR_MESSAGE"] = `${ADK_METADATA_KEY_PREFIX}error_message`;
|
|
24
|
+
A2AMetadataKeys2["CITATION_METADATA"] = `${ADK_METADATA_KEY_PREFIX}citation_metadata`;
|
|
25
|
+
A2AMetadataKeys2["GROUNDING_METADATA"] = `${ADK_METADATA_KEY_PREFIX}grounding_metadata`;
|
|
26
|
+
A2AMetadataKeys2["USAGE_METADATA"] = `${ADK_METADATA_KEY_PREFIX}usage_metadata`;
|
|
27
|
+
A2AMetadataKeys2["CUSTOM_METADATA"] = `${ADK_METADATA_KEY_PREFIX}custom_metadata`;
|
|
28
|
+
return A2AMetadataKeys2;
|
|
29
|
+
})(A2AMetadataKeys || {});
|
|
30
|
+
var AdkMetadataKeys = ((AdkMetadataKeys2) => {
|
|
31
|
+
AdkMetadataKeys2["TASK_ID"] = `${A2A_METADATA_KEY_PREFIX}task_id`;
|
|
32
|
+
AdkMetadataKeys2["CONTEXT_ID"] = `${A2A_METADATA_KEY_PREFIX}context_id`;
|
|
33
|
+
return AdkMetadataKeys2;
|
|
34
|
+
})(AdkMetadataKeys || {});
|
|
35
|
+
function getAdkEventMetadata(a2aEvent) {
|
|
36
|
+
return {
|
|
37
|
+
[AdkMetadataKeys.TASK_ID]: isTask(a2aEvent) ? a2aEvent.id : a2aEvent.taskId,
|
|
38
|
+
[AdkMetadataKeys.CONTEXT_ID]: a2aEvent.contextId
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function getA2AEventMetadata(adkEvent, {
|
|
42
|
+
appName,
|
|
43
|
+
userId,
|
|
44
|
+
sessionId
|
|
45
|
+
}) {
|
|
46
|
+
return {
|
|
47
|
+
...getA2AEventMetadataFromActions(adkEvent.actions),
|
|
48
|
+
...getA2ASessionMetadata({
|
|
49
|
+
appName,
|
|
50
|
+
userId,
|
|
51
|
+
sessionId
|
|
52
|
+
}),
|
|
53
|
+
[A2AMetadataKeys.INVOCATION_ID]: adkEvent.invocationId,
|
|
54
|
+
[A2AMetadataKeys.AUTHOR]: adkEvent.author,
|
|
55
|
+
[A2AMetadataKeys.BRANCH]: adkEvent.branch,
|
|
56
|
+
[A2AMetadataKeys.ERROR_CODE]: adkEvent.errorMessage,
|
|
57
|
+
[A2AMetadataKeys.ERROR_MESSAGE]: adkEvent.errorMessage,
|
|
58
|
+
[A2AMetadataKeys.CITATION_METADATA]: adkEvent.citationMetadata,
|
|
59
|
+
[A2AMetadataKeys.GROUNDING_METADATA]: adkEvent.groundingMetadata,
|
|
60
|
+
[A2AMetadataKeys.USAGE_METADATA]: adkEvent.usageMetadata,
|
|
61
|
+
[A2AMetadataKeys.CUSTOM_METADATA]: adkEvent.customMetadata,
|
|
62
|
+
[A2AMetadataKeys.PARTIAL]: adkEvent.partial,
|
|
63
|
+
[A2AMetadataKeys.IS_LONG_RUNNING]: (adkEvent.longRunningToolIds || []).length > 0
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function getA2ASessionMetadata({
|
|
67
|
+
appName,
|
|
68
|
+
userId,
|
|
69
|
+
sessionId
|
|
70
|
+
}) {
|
|
71
|
+
return {
|
|
72
|
+
[A2AMetadataKeys.APP_NAME]: appName,
|
|
73
|
+
[A2AMetadataKeys.USER_ID]: userId,
|
|
74
|
+
[A2AMetadataKeys.SESSION_ID]: sessionId
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function getA2AEventMetadataFromActions(actions) {
|
|
78
|
+
return {
|
|
79
|
+
[A2AMetadataKeys.ESCALATE]: actions.escalate,
|
|
80
|
+
[A2AMetadataKeys.TRANSFER_TO_AGENT]: actions.transferToAgent
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
A2AMetadataKeys,
|
|
85
|
+
AdkMetadataKeys,
|
|
86
|
+
getA2AEventMetadata,
|
|
87
|
+
getA2AEventMetadataFromActions,
|
|
88
|
+
getA2ASessionMetadata,
|
|
89
|
+
getAdkEventMetadata
|
|
90
|
+
};
|
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
* Copyright 2026 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})(MetadataKeys || {});
|
|
6
|
+
import {
|
|
7
|
+
createModelContent,
|
|
8
|
+
createUserContent
|
|
9
|
+
} from "@google/genai";
|
|
10
|
+
import { A2AMetadataKeys } from "./metadata_converter_utils.js";
|
|
12
11
|
var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
|
|
13
12
|
DataPartType2["FUNCTION_CALL"] = "function_call";
|
|
14
13
|
DataPartType2["FUNCTION_RESPONSE"] = "function_response";
|
|
@@ -16,7 +15,7 @@ var DataPartType = /* @__PURE__ */ ((DataPartType2) => {
|
|
|
16
15
|
DataPartType2["CODE_EXECUTABLE_CODE"] = "executable_code";
|
|
17
16
|
return DataPartType2;
|
|
18
17
|
})(DataPartType || {});
|
|
19
|
-
function toA2AParts(parts, longRunningToolIDs = []) {
|
|
18
|
+
function toA2AParts(parts = [], longRunningToolIDs = []) {
|
|
20
19
|
return parts.map((part) => toA2APart(part, longRunningToolIDs));
|
|
21
20
|
}
|
|
22
21
|
function toA2APart(part, longRunningToolIDs) {
|
|
@@ -30,9 +29,9 @@ function toA2APart(part, longRunningToolIDs) {
|
|
|
30
29
|
}
|
|
31
30
|
function toA2ATextPart(part) {
|
|
32
31
|
const a2aPart = { kind: "text", text: part.text || "" };
|
|
33
|
-
if (
|
|
32
|
+
if (part.thought) {
|
|
34
33
|
a2aPart.metadata = {
|
|
35
|
-
[
|
|
34
|
+
[A2AMetadataKeys.THOUGHT]: true
|
|
36
35
|
};
|
|
37
36
|
}
|
|
38
37
|
return a2aPart;
|
|
@@ -61,31 +60,31 @@ function toA2AFilePart(part) {
|
|
|
61
60
|
throw new Error(`Not a file part: ${JSON.stringify(part)}`);
|
|
62
61
|
}
|
|
63
62
|
function toA2ADataPart(part, longRunningToolIDs = []) {
|
|
64
|
-
let
|
|
63
|
+
let dataPartType;
|
|
65
64
|
let data;
|
|
66
65
|
if (part.functionCall) {
|
|
67
|
-
|
|
66
|
+
dataPartType = "function_call" /* FUNCTION_CALL */;
|
|
68
67
|
data = part.functionCall;
|
|
69
68
|
} else if (part.functionResponse) {
|
|
70
|
-
|
|
69
|
+
dataPartType = "function_response" /* FUNCTION_RESPONSE */;
|
|
71
70
|
data = part.functionResponse;
|
|
72
71
|
} else if (part.executableCode) {
|
|
73
|
-
|
|
72
|
+
dataPartType = "executable_code" /* CODE_EXECUTABLE_CODE */;
|
|
74
73
|
data = part.executableCode;
|
|
75
74
|
} else if (part.codeExecutionResult) {
|
|
76
|
-
|
|
75
|
+
dataPartType = "code_execution_result" /* CODE_EXEC_RESULT */;
|
|
77
76
|
data = part.codeExecutionResult;
|
|
78
77
|
} else {
|
|
79
78
|
throw new Error(`Unknown part type: ${JSON.stringify(part)}`);
|
|
80
79
|
}
|
|
81
80
|
const metadata = {
|
|
82
|
-
[
|
|
81
|
+
[A2AMetadataKeys.DATA_PART_TYPE]: dataPartType
|
|
83
82
|
};
|
|
84
|
-
if (part.functionCall && part.functionCall.
|
|
85
|
-
metadata[
|
|
83
|
+
if (part.functionCall && part.functionCall.id && longRunningToolIDs.includes(part.functionCall.id)) {
|
|
84
|
+
metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
|
|
86
85
|
}
|
|
87
|
-
if (part.functionResponse && part.functionResponse.
|
|
88
|
-
metadata[
|
|
86
|
+
if (part.functionResponse && part.functionResponse.id && longRunningToolIDs.includes(part.functionResponse.id)) {
|
|
87
|
+
metadata[A2AMetadataKeys.IS_LONG_RUNNING] = true;
|
|
89
88
|
}
|
|
90
89
|
return {
|
|
91
90
|
kind: "data",
|
|
@@ -93,6 +92,10 @@ function toA2ADataPart(part, longRunningToolIDs = []) {
|
|
|
93
92
|
metadata
|
|
94
93
|
};
|
|
95
94
|
}
|
|
95
|
+
function toGenAIContent(a2aMessage) {
|
|
96
|
+
const parts = toGenAIParts(a2aMessage.parts);
|
|
97
|
+
return a2aMessage.role === "user" ? createUserContent(parts) : createModelContent(parts);
|
|
98
|
+
}
|
|
96
99
|
function toGenAIParts(a2aParts) {
|
|
97
100
|
return a2aParts.map((a2aPart) => toGenAIPart(a2aPart));
|
|
98
101
|
}
|
|
@@ -112,7 +115,7 @@ function toGenAIPartText(a2aPart) {
|
|
|
112
115
|
var _a;
|
|
113
116
|
return {
|
|
114
117
|
text: a2aPart.text,
|
|
115
|
-
thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a[
|
|
118
|
+
thought: !!((_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.THOUGHT])
|
|
116
119
|
};
|
|
117
120
|
}
|
|
118
121
|
function toGenAIPartFile(a2aPart) {
|
|
@@ -140,7 +143,7 @@ function toGenAIPartData(a2aPart) {
|
|
|
140
143
|
throw new Error(`No data in part: ${JSON.stringify(a2aPart)}`);
|
|
141
144
|
}
|
|
142
145
|
const data = a2aPart.data;
|
|
143
|
-
const type = (_a = a2aPart.metadata) == null ? void 0 : _a[
|
|
146
|
+
const type = (_a = a2aPart.metadata) == null ? void 0 : _a[A2AMetadataKeys.DATA_PART_TYPE];
|
|
144
147
|
if (type === "function_call" /* FUNCTION_CALL */) {
|
|
145
148
|
return { functionCall: data };
|
|
146
149
|
}
|
|
@@ -163,6 +166,7 @@ export {
|
|
|
163
166
|
toA2APart,
|
|
164
167
|
toA2AParts,
|
|
165
168
|
toA2ATextPart,
|
|
169
|
+
toGenAIContent,
|
|
166
170
|
toGenAIPart,
|
|
167
171
|
toGenAIPartData,
|
|
168
172
|
toGenAIPartFile,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
traceAgentInvocation,
|
|
12
12
|
tracer
|
|
13
13
|
} from "../telemetry/tracing.js";
|
|
14
|
-
import {
|
|
14
|
+
import { Context } from "./context.js";
|
|
15
15
|
import { InvocationContext } from "./invocation_context.js";
|
|
16
16
|
const BASE_AGENT_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseAgent");
|
|
17
17
|
function isBaseAgent(obj) {
|
|
@@ -153,7 +153,7 @@ class BaseAgent {
|
|
|
153
153
|
if (this.beforeAgentCallback.length === 0) {
|
|
154
154
|
return void 0;
|
|
155
155
|
}
|
|
156
|
-
const callbackContext = new
|
|
156
|
+
const callbackContext = new Context({ invocationContext });
|
|
157
157
|
for (const callback of this.beforeAgentCallback) {
|
|
158
158
|
const content = await callback(callbackContext);
|
|
159
159
|
if (content) {
|
|
@@ -188,7 +188,7 @@ class BaseAgent {
|
|
|
188
188
|
if (this.afterAgentCallback.length === 0) {
|
|
189
189
|
return void 0;
|
|
190
190
|
}
|
|
191
|
-
const callbackContext = new
|
|
191
|
+
const callbackContext = new Context({ invocationContext });
|
|
192
192
|
for (const callback of this.afterAgentCallback) {
|
|
193
193
|
const content = await callback(callbackContext);
|
|
194
194
|
if (content) {
|