@google/adk 0.5.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.
Files changed (139) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/a2a/a2a_event.js +53 -26
  3. package/dist/cjs/a2a/a2a_remote_agent.js +174 -0
  4. package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +198 -0
  5. package/dist/cjs/a2a/a2a_remote_agent_utils.js +165 -0
  6. package/dist/cjs/a2a/agent_card.js +380 -0
  7. package/dist/cjs/a2a/agent_executor.js +221 -0
  8. package/dist/cjs/a2a/agent_to_a2a.js +115 -0
  9. package/dist/cjs/a2a/event_processor_utils.js +180 -0
  10. package/dist/cjs/a2a/executor_context.js +1 -1
  11. package/dist/cjs/a2a/metadata_converter_utils.js +1 -0
  12. package/dist/cjs/a2a/part_converter_utils.js +24 -13
  13. package/dist/cjs/agents/llm_agent.js +17 -0
  14. package/dist/cjs/agents/{content_processor_utils.js → processors/content_processor_utils.js} +21 -2
  15. package/dist/cjs/agents/processors/content_request_processor.js +24 -3
  16. package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -0
  17. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
  18. package/dist/cjs/artifacts/file_artifact_service.js +35 -4
  19. package/dist/cjs/common.js +37 -0
  20. package/dist/cjs/context/base_context_compactor.js +27 -0
  21. package/dist/cjs/context/summarizers/base_summarizer.js +27 -0
  22. package/dist/cjs/context/summarizers/llm_summarizer.js +93 -0
  23. package/dist/cjs/context/token_based_context_compactor.js +135 -0
  24. package/dist/cjs/context/truncating_context_compactor.js +58 -0
  25. package/dist/cjs/events/compacted_event.js +53 -0
  26. package/dist/cjs/index.js +38 -14
  27. package/dist/cjs/index.js.map +4 -4
  28. package/dist/cjs/memory/in_memory_memory_service.js +1 -1
  29. package/dist/cjs/runner/runner.js +19 -10
  30. package/dist/cjs/sessions/db/operations.js +4 -14
  31. package/dist/cjs/sessions/state.js +2 -2
  32. package/dist/cjs/tools/agent_tool.js +6 -5
  33. package/dist/cjs/tools/load_artifacts_tool.js +188 -0
  34. package/dist/cjs/tools/load_memory_tool.js +107 -0
  35. package/dist/cjs/tools/preload_memory_tool.js +109 -0
  36. package/dist/cjs/utils/logger.js +1 -0
  37. package/dist/cjs/version.js +1 -1
  38. package/dist/esm/a2a/a2a_event.js +52 -26
  39. package/dist/esm/a2a/a2a_remote_agent.js +148 -0
  40. package/dist/esm/a2a/a2a_remote_agent_run_processor.js +175 -0
  41. package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
  42. package/dist/esm/a2a/agent_card.js +340 -0
  43. package/dist/esm/a2a/agent_executor.js +202 -0
  44. package/dist/esm/a2a/agent_to_a2a.js +80 -0
  45. package/dist/esm/a2a/event_processor_utils.js +159 -0
  46. package/dist/esm/a2a/executor_context.js +1 -1
  47. package/dist/esm/a2a/metadata_converter_utils.js +1 -0
  48. package/dist/esm/a2a/part_converter_utils.js +24 -13
  49. package/dist/esm/agents/llm_agent.js +17 -0
  50. package/dist/esm/agents/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
  51. package/dist/esm/agents/processors/content_request_processor.js +25 -4
  52. package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
  53. package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
  54. package/dist/esm/artifacts/file_artifact_service.js +30 -3
  55. package/dist/esm/common.js +33 -1
  56. package/dist/esm/context/base_context_compactor.js +5 -0
  57. package/dist/esm/context/summarizers/base_summarizer.js +5 -0
  58. package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
  59. package/dist/esm/context/token_based_context_compactor.js +105 -0
  60. package/dist/esm/context/truncating_context_compactor.js +28 -0
  61. package/dist/esm/events/compacted_event.js +22 -0
  62. package/dist/esm/index.js +38 -14
  63. package/dist/esm/index.js.map +4 -4
  64. package/dist/esm/memory/in_memory_memory_service.js +1 -1
  65. package/dist/esm/runner/runner.js +17 -9
  66. package/dist/esm/sessions/db/operations.js +4 -14
  67. package/dist/esm/sessions/state.js +2 -2
  68. package/dist/esm/tools/agent_tool.js +6 -5
  69. package/dist/esm/tools/load_artifacts_tool.js +159 -0
  70. package/dist/esm/tools/load_memory_tool.js +78 -0
  71. package/dist/esm/tools/preload_memory_tool.js +80 -0
  72. package/dist/esm/utils/logger.js +1 -0
  73. package/dist/esm/version.js +1 -1
  74. package/dist/types/a2a/a2a_event.d.ts +23 -6
  75. package/dist/types/a2a/a2a_remote_agent.d.ts +63 -0
  76. package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
  77. package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
  78. package/dist/types/a2a/agent_card.d.ts +23 -0
  79. package/dist/types/a2a/agent_executor.d.ts +52 -0
  80. package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
  81. package/dist/types/a2a/event_processor_utils.d.ts +24 -0
  82. package/dist/types/a2a/executor_context.d.ts +1 -1
  83. package/dist/types/a2a/metadata_converter_utils.d.ts +2 -1
  84. package/dist/types/agents/llm_agent.d.ts +6 -0
  85. package/dist/types/agents/loop_agent.d.ts +1 -1
  86. package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
  87. package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
  88. package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
  89. package/dist/types/common.d.ts +16 -1
  90. package/dist/types/context/base_context_compactor.d.ts +24 -0
  91. package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
  92. package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
  93. package/dist/types/context/token_based_context_compactor.d.ts +33 -0
  94. package/dist/types/context/truncating_context_compactor.d.ts +24 -0
  95. package/dist/types/events/compacted_event.d.ts +33 -0
  96. package/dist/types/index.d.ts +8 -0
  97. package/dist/types/runner/runner.d.ts +13 -0
  98. package/dist/types/sessions/db/operations.d.ts +2 -3
  99. package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
  100. package/dist/types/tools/load_memory_tool.d.ts +22 -0
  101. package/dist/types/tools/preload_memory_tool.d.ts +23 -0
  102. package/dist/types/version.d.ts +1 -1
  103. package/dist/web/a2a/a2a_event.js +52 -26
  104. package/dist/web/a2a/a2a_remote_agent.js +193 -0
  105. package/dist/web/a2a/a2a_remote_agent_run_processor.js +175 -0
  106. package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
  107. package/dist/web/a2a/agent_card.js +340 -0
  108. package/dist/web/a2a/agent_executor.js +216 -0
  109. package/dist/web/a2a/agent_to_a2a.js +80 -0
  110. package/dist/web/a2a/event_processor_utils.js +168 -0
  111. package/dist/web/a2a/executor_context.js +1 -1
  112. package/dist/web/a2a/metadata_converter_utils.js +1 -0
  113. package/dist/web/a2a/part_converter_utils.js +24 -13
  114. package/dist/web/agents/llm_agent.js +17 -0
  115. package/dist/web/agents/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
  116. package/dist/web/agents/processors/content_request_processor.js +25 -4
  117. package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
  118. package/dist/web/agents/processors/instructions_llm_request_processor.js +1 -1
  119. package/dist/web/artifacts/file_artifact_service.js +30 -3
  120. package/dist/web/common.js +33 -1
  121. package/dist/web/context/base_context_compactor.js +5 -0
  122. package/dist/web/context/summarizers/base_summarizer.js +5 -0
  123. package/dist/web/context/summarizers/llm_summarizer.js +74 -0
  124. package/dist/web/context/token_based_context_compactor.js +105 -0
  125. package/dist/web/context/truncating_context_compactor.js +28 -0
  126. package/dist/web/events/compacted_event.js +40 -0
  127. package/dist/web/index.js +1 -1
  128. package/dist/web/index.js.map +4 -4
  129. package/dist/web/memory/in_memory_memory_service.js +1 -1
  130. package/dist/web/runner/runner.js +17 -9
  131. package/dist/web/sessions/db/operations.js +4 -14
  132. package/dist/web/sessions/state.js +2 -2
  133. package/dist/web/tools/agent_tool.js +6 -5
  134. package/dist/web/tools/load_artifacts_tool.js +150 -0
  135. package/dist/web/tools/load_memory_tool.js +77 -0
  136. package/dist/web/tools/preload_memory_tool.js +75 -0
  137. package/dist/web/utils/logger.js +1 -0
  138. package/dist/web/version.js +1 -1
  139. package/package.json +6 -4
@@ -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
+ };
@@ -0,0 +1,216 @@
1
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
2
+ 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);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ import { isRunner, Runner } from "../runner/runner.js";
9
+ import { randomUUID } from "../utils/env_aware_utils.js";
10
+ import { logger } from "../utils/logger.js";
11
+ import {
12
+ createTask,
13
+ createTaskArtifactUpdateEvent,
14
+ createTaskFailedEvent,
15
+ createTaskWorkingEvent
16
+ } from "./a2a_event.js";
17
+ import {
18
+ getFinalTaskStatusUpdate,
19
+ getTaskInputRequiredEvent
20
+ } from "./event_processor_utils.js";
21
+ import { createExecutorContext } from "./executor_context.js";
22
+ import {
23
+ getA2AEventMetadata,
24
+ getA2ASessionMetadata
25
+ } from "./metadata_converter_utils.js";
26
+ import { toA2AParts, toGenAIContent } from "./part_converter_utils.js";
27
+ class A2AAgentExecutor {
28
+ constructor(config) {
29
+ this.config = config;
30
+ this.agentPartialArtifactIdsMap = {};
31
+ }
32
+ async execute(ctx, eventBus) {
33
+ var _a, _b;
34
+ const a2aUserMessage = ctx.userMessage;
35
+ if (!a2aUserMessage) {
36
+ throw new Error("message not provided");
37
+ }
38
+ const userId = "A2A_USER_".concat(ctx.contextId);
39
+ const sessionId = ctx.contextId;
40
+ const genAIUserMessage = toGenAIContent(a2aUserMessage);
41
+ const adkRunner = await getAdkRunner(this.config.runner);
42
+ const session = await getAdkSession(
43
+ userId,
44
+ sessionId,
45
+ adkRunner.sessionService,
46
+ adkRunner.appName
47
+ );
48
+ const executorContext = createExecutorContext({
49
+ session,
50
+ userContent: genAIUserMessage,
51
+ requestContext: ctx
52
+ });
53
+ try {
54
+ if (this.config.beforeExecuteCallback) {
55
+ await this.config.beforeExecuteCallback(ctx);
56
+ }
57
+ if (ctx.task) {
58
+ const inputRequiredEvent = getTaskInputRequiredEvent(
59
+ ctx.task,
60
+ genAIUserMessage
61
+ );
62
+ if (inputRequiredEvent) {
63
+ await this.publishFinalTaskStatus({
64
+ executorContext,
65
+ eventBus,
66
+ event: inputRequiredEvent
67
+ });
68
+ return;
69
+ }
70
+ }
71
+ if (!ctx.task) {
72
+ eventBus.publish(
73
+ createTask({
74
+ taskId: ctx.taskId,
75
+ contextId: ctx.contextId,
76
+ message: a2aUserMessage
77
+ })
78
+ );
79
+ }
80
+ eventBus.publish(
81
+ createTaskWorkingEvent({
82
+ taskId: ctx.taskId,
83
+ contextId: ctx.contextId
84
+ })
85
+ );
86
+ const adkEvents = [];
87
+ try {
88
+ for (var iter = __forAwait(adkRunner.runAsync({
89
+ userId,
90
+ sessionId,
91
+ newMessage: genAIUserMessage,
92
+ runConfig: this.config.runConfig
93
+ })), more, temp, error; more = !(temp = await iter.next()).done; more = false) {
94
+ const adkEvent = temp.value;
95
+ adkEvents.push(adkEvent);
96
+ const a2aEvent = this.convertAdkEventToA2AEvent(
97
+ adkEvent,
98
+ executorContext
99
+ );
100
+ if (!a2aEvent) {
101
+ continue;
102
+ }
103
+ await ((_b = (_a = this.config).afterEventCallback) == null ? void 0 : _b.call(
104
+ _a,
105
+ executorContext,
106
+ adkEvent,
107
+ a2aEvent
108
+ ));
109
+ eventBus.publish(a2aEvent);
110
+ }
111
+ } catch (temp) {
112
+ error = [temp];
113
+ } finally {
114
+ try {
115
+ more && (temp = iter.return) && await temp.call(iter);
116
+ } finally {
117
+ if (error)
118
+ throw error[0];
119
+ }
120
+ }
121
+ await this.publishFinalTaskStatus({
122
+ executorContext,
123
+ eventBus,
124
+ event: getFinalTaskStatusUpdate(adkEvents, executorContext)
125
+ });
126
+ } catch (e) {
127
+ const error2 = e;
128
+ await this.publishFinalTaskStatus({
129
+ executorContext,
130
+ eventBus,
131
+ error: error2,
132
+ event: createTaskFailedEvent({
133
+ taskId: ctx.taskId,
134
+ contextId: ctx.contextId,
135
+ error: new Error("Agent run failed: ".concat(error2.message)),
136
+ metadata: getA2ASessionMetadata(executorContext)
137
+ })
138
+ });
139
+ }
140
+ }
141
+ // Task cancellation is not supported in this implementation yet.
142
+ async cancelTask(_taskId) {
143
+ throw new Error("Task cancellation is not supported yet.");
144
+ }
145
+ convertAdkEventToA2AEvent(adkEvent, executorContext) {
146
+ var _a;
147
+ const a2aParts = toA2AParts(
148
+ (_a = adkEvent.content) == null ? void 0 : _a.parts,
149
+ adkEvent.longRunningToolIds
150
+ );
151
+ if (a2aParts.length === 0) {
152
+ return void 0;
153
+ }
154
+ const artifactId = this.agentPartialArtifactIdsMap[adkEvent.author] || randomUUID();
155
+ const a2aEvent = createTaskArtifactUpdateEvent({
156
+ taskId: executorContext.requestContext.taskId,
157
+ contextId: executorContext.requestContext.contextId,
158
+ artifactId,
159
+ parts: a2aParts,
160
+ metadata: getA2AEventMetadata(adkEvent, executorContext),
161
+ append: adkEvent.partial,
162
+ lastChunk: !adkEvent.partial
163
+ });
164
+ if (adkEvent.partial) {
165
+ this.agentPartialArtifactIdsMap[adkEvent.author] = artifactId;
166
+ } else {
167
+ delete this.agentPartialArtifactIdsMap[adkEvent.author];
168
+ }
169
+ return a2aEvent;
170
+ }
171
+ /**
172
+ * Writes the final status event to the queue.
173
+ */
174
+ async publishFinalTaskStatus({
175
+ executorContext,
176
+ eventBus,
177
+ event,
178
+ error
179
+ }) {
180
+ var _a, _b;
181
+ try {
182
+ await ((_b = (_a = this.config).afterExecuteCallback) == null ? void 0 : _b.call(_a, executorContext, event, error));
183
+ } catch (e) {
184
+ logger.error("Error in afterExecuteCallback:", e);
185
+ }
186
+ eventBus.publish(event);
187
+ }
188
+ }
189
+ async function getAdkSession(userId, sessionId, sessionService, appName) {
190
+ const session = await sessionService.getSession({
191
+ appName,
192
+ userId,
193
+ sessionId
194
+ });
195
+ if (session) {
196
+ return session;
197
+ }
198
+ return sessionService.createSession({
199
+ appName,
200
+ userId,
201
+ sessionId
202
+ });
203
+ }
204
+ async function getAdkRunner(runnerOrConfig) {
205
+ if (typeof runnerOrConfig === "function") {
206
+ const result = await runnerOrConfig();
207
+ return getAdkRunner(result);
208
+ }
209
+ if (isRunner(runnerOrConfig)) {
210
+ return runnerOrConfig;
211
+ }
212
+ return new Runner(runnerOrConfig);
213
+ }
214
+ export {
215
+ A2AAgentExecutor
216
+ };
@@ -0,0 +1,80 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { AGENT_CARD_PATH } from "@a2a-js/sdk";
7
+ import { DefaultRequestHandler, InMemoryTaskStore } from "@a2a-js/sdk/server";
8
+ import {
9
+ agentCardHandler,
10
+ jsonRpcHandler,
11
+ restHandler,
12
+ UserBuilder
13
+ } from "@a2a-js/sdk/server/express";
14
+ import express from "express";
15
+ import { StreamingMode } from "../agents/run_config.js";
16
+ import { InMemorySessionService } from "../sessions/in_memory_session_service.js";
17
+ import { getA2AAgentCard, resolveAgentCard } from "./agent_card.js";
18
+ import { A2AAgentExecutor } from "./agent_executor.js";
19
+ async function toA2a(agent, options = {}) {
20
+ var _a, _b, _c, _d;
21
+ const host = (_a = options.host) != null ? _a : "localhost";
22
+ const port = (_b = options.port) != null ? _b : 8e3;
23
+ const protocol = (_c = options.protocol) != null ? _c : "http";
24
+ const basePath = options.basePath || "";
25
+ const rpcUrl = "".concat(protocol, "://").concat(host, ":").concat(port).concat(basePath);
26
+ const agentCard = options.agentCard ? await resolveAgentCard(options.agentCard) : await getA2AAgentCard(agent, [
27
+ {
28
+ url: "".concat(rpcUrl, "/jsonrpc"),
29
+ transport: "JSONRPC"
30
+ },
31
+ {
32
+ url: "".concat(rpcUrl, "/rest"),
33
+ transport: "HTTP+JSON"
34
+ }
35
+ ]);
36
+ const agentExecutor = new A2AAgentExecutor({
37
+ runner: options.runner || {
38
+ agent,
39
+ appName: agent.name,
40
+ sessionService: options.sessionService || new InMemorySessionService(),
41
+ memoryService: options.memoryService,
42
+ artifactService: options.artifactService
43
+ },
44
+ runConfig: {
45
+ streamingMode: StreamingMode.SSE
46
+ }
47
+ });
48
+ const requestHandler = new DefaultRequestHandler(
49
+ agentCard,
50
+ new InMemoryTaskStore(),
51
+ agentExecutor
52
+ );
53
+ const app = (_d = options.app) != null ? _d : express();
54
+ if (!options.app) {
55
+ app.use(express.urlencoded({ limit: "50mb", extended: true }));
56
+ app.use(express.json({ limit: "50mb" }));
57
+ }
58
+ app.use(
59
+ "".concat(basePath, "/").concat(AGENT_CARD_PATH),
60
+ agentCardHandler({ agentCardProvider: requestHandler })
61
+ );
62
+ app.use(
63
+ "".concat(basePath, "/rest"),
64
+ restHandler({
65
+ requestHandler,
66
+ userBuilder: UserBuilder.noAuthentication
67
+ })
68
+ );
69
+ app.use(
70
+ "".concat(basePath, "/jsonrpc"),
71
+ jsonRpcHandler({
72
+ requestHandler,
73
+ userBuilder: UserBuilder.noAuthentication
74
+ })
75
+ );
76
+ return app;
77
+ }
78
+ export {
79
+ toA2a
80
+ };