@google/adk 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/a2a/a2a_event.js +317 -0
  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_converter_utils.js +201 -0
  10. package/dist/cjs/a2a/event_processor_utils.js +180 -0
  11. package/dist/cjs/a2a/executor_context.js +53 -0
  12. package/dist/cjs/a2a/metadata_converter_utils.js +126 -0
  13. package/dist/cjs/a2a/part_converter_utils.js +47 -34
  14. package/dist/cjs/agents/base_agent.js +3 -3
  15. package/dist/cjs/{tools/tool_context.js → agents/context.js} +70 -15
  16. package/dist/cjs/agents/functions.js +2 -2
  17. package/dist/cjs/agents/llm_agent.js +38 -674
  18. package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
  19. package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
  20. package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
  21. package/dist/cjs/agents/{content_processor_utils.js → processors/content_processor_utils.js} +21 -2
  22. package/dist/cjs/agents/processors/content_request_processor.js +87 -0
  23. package/dist/cjs/agents/processors/context_compactor_request_processor.js +61 -0
  24. package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
  25. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
  26. package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
  27. package/dist/cjs/artifacts/file_artifact_service.js +35 -4
  28. package/dist/cjs/common.js +48 -7
  29. package/dist/cjs/context/base_context_compactor.js +27 -0
  30. package/dist/cjs/context/summarizers/base_summarizer.js +27 -0
  31. package/dist/cjs/context/summarizers/llm_summarizer.js +93 -0
  32. package/dist/cjs/context/token_based_context_compactor.js +135 -0
  33. package/dist/cjs/context/truncating_context_compactor.js +58 -0
  34. package/dist/cjs/events/compacted_event.js +53 -0
  35. package/dist/cjs/index.js +113 -53
  36. package/dist/cjs/index.js.map +7 -0
  37. package/dist/cjs/memory/in_memory_memory_service.js +1 -1
  38. package/dist/cjs/models/llm_response.js +2 -0
  39. package/dist/cjs/plugins/base_plugin.js +1 -1
  40. package/dist/cjs/runner/runner.js +20 -11
  41. package/dist/cjs/sessions/database_session_service.js +4 -1
  42. package/dist/cjs/sessions/db/operations.js +25 -23
  43. package/dist/cjs/sessions/state.js +2 -2
  44. package/dist/cjs/tools/agent_tool.js +6 -5
  45. package/dist/cjs/tools/base_tool.js +3 -0
  46. package/dist/cjs/tools/base_toolset.js +13 -3
  47. package/dist/cjs/tools/exit_loop_tool.js +63 -0
  48. package/dist/cjs/tools/load_artifacts_tool.js +188 -0
  49. package/dist/cjs/tools/load_memory_tool.js +107 -0
  50. package/dist/cjs/tools/mcp/mcp_toolset.js +9 -5
  51. package/dist/cjs/tools/preload_memory_tool.js +109 -0
  52. package/dist/cjs/utils/logger.js +62 -54
  53. package/dist/cjs/version.js +1 -1
  54. package/dist/esm/a2a/a2a_event.js +269 -0
  55. package/dist/esm/a2a/a2a_remote_agent.js +148 -0
  56. package/dist/esm/a2a/a2a_remote_agent_run_processor.js +175 -0
  57. package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
  58. package/dist/esm/a2a/agent_card.js +340 -0
  59. package/dist/esm/a2a/agent_executor.js +202 -0
  60. package/dist/esm/a2a/agent_to_a2a.js +80 -0
  61. package/dist/esm/a2a/event_converter_utils.js +187 -0
  62. package/dist/esm/a2a/event_processor_utils.js +159 -0
  63. package/dist/esm/a2a/executor_context.js +23 -0
  64. package/dist/esm/a2a/metadata_converter_utils.js +91 -0
  65. package/dist/esm/a2a/part_converter_utils.js +49 -34
  66. package/dist/esm/agents/base_agent.js +3 -3
  67. package/dist/{web/tools/tool_context.js → esm/agents/context.js} +66 -11
  68. package/dist/esm/agents/functions.js +2 -2
  69. package/dist/esm/agents/llm_agent.js +31 -683
  70. package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
  71. package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
  72. package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
  73. package/dist/esm/agents/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
  74. package/dist/esm/agents/processors/content_request_processor.js +59 -0
  75. package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
  76. package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
  77. package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
  78. package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
  79. package/dist/esm/artifacts/file_artifact_service.js +30 -3
  80. package/dist/esm/common.js +44 -10
  81. package/dist/esm/context/base_context_compactor.js +5 -0
  82. package/dist/esm/context/summarizers/base_summarizer.js +5 -0
  83. package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
  84. package/dist/esm/context/token_based_context_compactor.js +105 -0
  85. package/dist/esm/context/truncating_context_compactor.js +28 -0
  86. package/dist/esm/events/compacted_event.js +22 -0
  87. package/dist/esm/index.js +119 -18
  88. package/dist/esm/index.js.map +7 -0
  89. package/dist/esm/memory/in_memory_memory_service.js +1 -1
  90. package/dist/esm/models/llm_response.js +2 -0
  91. package/dist/esm/plugins/base_plugin.js +1 -1
  92. package/dist/esm/runner/runner.js +18 -10
  93. package/dist/esm/sessions/database_session_service.js +4 -1
  94. package/dist/esm/sessions/db/operations.js +32 -18
  95. package/dist/esm/sessions/state.js +2 -2
  96. package/dist/esm/tools/agent_tool.js +6 -5
  97. package/dist/esm/tools/base_tool.js +3 -0
  98. package/dist/esm/tools/base_toolset.js +11 -2
  99. package/dist/esm/tools/exit_loop_tool.js +32 -0
  100. package/dist/esm/tools/load_artifacts_tool.js +159 -0
  101. package/dist/esm/tools/load_memory_tool.js +78 -0
  102. package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
  103. package/dist/esm/tools/preload_memory_tool.js +80 -0
  104. package/dist/esm/utils/logger.js +52 -54
  105. package/dist/esm/version.js +1 -1
  106. package/dist/types/a2a/a2a_event.d.ts +139 -0
  107. package/dist/types/a2a/a2a_remote_agent.d.ts +63 -0
  108. package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
  109. package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
  110. package/dist/types/a2a/agent_card.d.ts +23 -0
  111. package/dist/types/a2a/agent_executor.d.ts +52 -0
  112. package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
  113. package/dist/types/a2a/event_converter_utils.d.ts +20 -0
  114. package/dist/types/a2a/event_processor_utils.d.ts +24 -0
  115. package/dist/types/a2a/executor_context.d.ts +33 -0
  116. package/dist/types/a2a/metadata_converter_utils.d.ts +63 -0
  117. package/dist/types/a2a/part_converter_utils.d.ts +4 -3
  118. package/dist/types/agents/base_agent.d.ts +2 -2
  119. package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
  120. package/dist/types/agents/llm_agent.d.ts +14 -31
  121. package/dist/types/agents/loop_agent.d.ts +1 -1
  122. package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
  123. package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
  124. package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
  125. package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
  126. package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
  127. package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
  128. package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
  129. package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
  130. package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
  131. package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
  132. package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
  133. package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
  134. package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
  135. package/dist/types/common.d.ts +20 -5
  136. package/dist/types/context/base_context_compactor.d.ts +24 -0
  137. package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
  138. package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
  139. package/dist/types/context/token_based_context_compactor.d.ts +33 -0
  140. package/dist/types/context/truncating_context_compactor.d.ts +24 -0
  141. package/dist/types/events/compacted_event.d.ts +33 -0
  142. package/dist/types/index.d.ts +8 -0
  143. package/dist/types/models/llm_response.d.ts +5 -1
  144. package/dist/types/plugins/base_plugin.d.ts +12 -13
  145. package/dist/types/plugins/logging_plugin.d.ts +9 -10
  146. package/dist/types/plugins/plugin_manager.d.ts +9 -10
  147. package/dist/types/plugins/security_plugin.d.ts +2 -2
  148. package/dist/types/runner/runner.d.ts +13 -0
  149. package/dist/types/sessions/database_session_service.d.ts +2 -1
  150. package/dist/types/sessions/db/operations.d.ts +3 -4
  151. package/dist/types/tools/base_tool.d.ts +3 -3
  152. package/dist/types/tools/base_toolset.d.ts +12 -3
  153. package/dist/types/tools/exit_loop_tool.d.ts +24 -0
  154. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  155. package/dist/types/tools/function_tool.d.ts +2 -2
  156. package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
  157. package/dist/types/tools/load_memory_tool.d.ts +22 -0
  158. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
  159. package/dist/types/tools/preload_memory_tool.d.ts +23 -0
  160. package/dist/types/utils/logger.d.ts +5 -9
  161. package/dist/types/version.d.ts +1 -1
  162. package/dist/web/a2a/a2a_event.js +269 -0
  163. package/dist/web/a2a/a2a_remote_agent.js +193 -0
  164. package/dist/web/a2a/a2a_remote_agent_run_processor.js +175 -0
  165. package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
  166. package/dist/web/a2a/agent_card.js +340 -0
  167. package/dist/web/a2a/agent_executor.js +216 -0
  168. package/dist/web/a2a/agent_to_a2a.js +80 -0
  169. package/dist/web/a2a/event_converter_utils.js +201 -0
  170. package/dist/web/a2a/event_processor_utils.js +168 -0
  171. package/dist/web/a2a/executor_context.js +23 -0
  172. package/dist/web/a2a/metadata_converter_utils.js +108 -0
  173. package/dist/web/a2a/part_converter_utils.js +49 -34
  174. package/dist/web/agents/base_agent.js +3 -3
  175. package/dist/{esm/tools/tool_context.js → web/agents/context.js} +66 -11
  176. package/dist/web/agents/functions.js +2 -2
  177. package/dist/web/agents/llm_agent.js +31 -661
  178. package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
  179. package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
  180. package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
  181. package/dist/web/agents/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
  182. package/dist/web/agents/processors/content_request_processor.js +77 -0
  183. package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
  184. package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
  185. package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
  186. package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
  187. package/dist/web/artifacts/file_artifact_service.js +30 -3
  188. package/dist/web/common.js +44 -10
  189. package/dist/web/context/base_context_compactor.js +5 -0
  190. package/dist/web/context/summarizers/base_summarizer.js +5 -0
  191. package/dist/web/context/summarizers/llm_summarizer.js +74 -0
  192. package/dist/web/context/token_based_context_compactor.js +105 -0
  193. package/dist/web/context/truncating_context_compactor.js +28 -0
  194. package/dist/web/events/compacted_event.js +40 -0
  195. package/dist/web/index.js +13 -18
  196. package/dist/web/index.js.map +7 -0
  197. package/dist/web/memory/in_memory_memory_service.js +1 -1
  198. package/dist/web/models/llm_response.js +2 -0
  199. package/dist/web/plugins/base_plugin.js +1 -1
  200. package/dist/web/runner/runner.js +18 -10
  201. package/dist/web/sessions/database_session_service.js +4 -1
  202. package/dist/web/sessions/db/operations.js +32 -18
  203. package/dist/web/sessions/state.js +2 -2
  204. package/dist/web/tools/agent_tool.js +6 -5
  205. package/dist/web/tools/base_tool.js +3 -0
  206. package/dist/web/tools/base_toolset.js +11 -2
  207. package/dist/web/tools/exit_loop_tool.js +32 -0
  208. package/dist/web/tools/load_artifacts_tool.js +150 -0
  209. package/dist/web/tools/load_memory_tool.js +77 -0
  210. package/dist/web/tools/mcp/mcp_toolset.js +27 -5
  211. package/dist/web/tools/preload_memory_tool.js +75 -0
  212. package/dist/web/utils/logger.js +52 -54
  213. package/dist/web/version.js +1 -1
  214. package/package.json +7 -4
  215. package/dist/cjs/agents/callback_context.js +0 -101
  216. package/dist/esm/agents/callback_context.js +0 -71
  217. package/dist/types/agents/callback_context.d.ts +0 -42
  218. package/dist/web/agents/callback_context.js +0 -71
  219. /package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  220. /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  221. /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
@@ -58,7 +58,7 @@ function getUserKey(appName, userId) {
58
58
  }
59
59
  function extractWordsLower(text) {
60
60
  return new Set(
61
- [...text.matchAll(/[A-Za-z]+/)].map((match) => match[0].toLowerCase())
61
+ [...text.matchAll(/[A-Za-z]+/g)].map((match) => match[0].toLowerCase())
62
62
  );
63
63
  }
64
64
  function formatTimestamp(timestamp) {
@@ -12,6 +12,7 @@ function createLlmResponse(response) {
12
12
  return {
13
13
  content: candidate.content,
14
14
  groundingMetadata: candidate.groundingMetadata,
15
+ citationMetadata: candidate.citationMetadata,
15
16
  usageMetadata,
16
17
  finishReason: candidate.finishReason
17
18
  };
@@ -20,6 +21,7 @@ function createLlmResponse(response) {
20
21
  errorCode: candidate.finishReason,
21
22
  errorMessage: candidate.finishMessage,
22
23
  usageMetadata,
24
+ citationMetadata: candidate.citationMetadata,
23
25
  finishReason: candidate.finishReason
24
26
  };
25
27
  }
@@ -198,7 +198,7 @@ class BasePlugin {
198
198
  * Callback executed when a tool call encounters an error.
199
199
  tool: BaseTool;
200
200
  toolArgs: Record<string, unknown>;
201
- toolContext: ToolContext;
201
+ toolContext: Context;
202
202
  result: Record<string, unknown>;
203
203
  }): Promise<Record<string, unknown> | undefined> {
204
204
  return;
@@ -3,6 +3,7 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ var _a;
6
7
  import { createPartFromText } from "@google/genai";
7
8
  import { context, trace } from "@opentelemetry/api";
8
9
  import {
@@ -24,12 +25,18 @@ import {
24
25
  } from "../telemetry/tracing.js";
25
26
  import { logger } from "../utils/logger.js";
26
27
  import { isGemini2OrAbove } from "../utils/model_name.js";
28
+ const RUNNER_SIGNATURE_SYMBOL = Symbol.for("google.adk.runner");
29
+ function isRunner(obj) {
30
+ return typeof obj === "object" && obj !== null && RUNNER_SIGNATURE_SYMBOL in obj && obj[RUNNER_SIGNATURE_SYMBOL] === true;
31
+ }
32
+ _a = RUNNER_SIGNATURE_SYMBOL;
27
33
  class Runner {
28
34
  constructor(input) {
29
- var _a;
35
+ this[_a] = true;
36
+ var _a2;
30
37
  this.appName = input.appName;
31
38
  this.agent = input.agent;
32
- this.pluginManager = new PluginManager((_a = input.plugins) != null ? _a : []);
39
+ this.pluginManager = new PluginManager((_a2 = input.plugins) != null ? _a2 : []);
33
40
  this.artifactService = input.artifactService;
34
41
  this.sessionService = input.sessionService;
35
42
  this.memoryService = input.memoryService;
@@ -89,7 +96,7 @@ class Runner {
89
96
  ctx,
90
97
  this,
91
98
  async function* () {
92
- var _a;
99
+ var _a2;
93
100
  const session = await this.sessionService.getSession({
94
101
  appName: this.appName,
95
102
  userId,
@@ -134,7 +141,7 @@ class Runner {
134
141
  newMessage = pluginUserMessage;
135
142
  }
136
143
  if (newMessage) {
137
- if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
144
+ if (!((_a2 = newMessage.parts) == null ? void 0 : _a2.length)) {
138
145
  throw new Error("No parts in the newMessage.");
139
146
  }
140
147
  if (runConfig.saveInputBlobsAsArtifacts) {
@@ -210,8 +217,8 @@ class Runner {
210
217
  * @param message The message containing parts to process.
211
218
  */
212
219
  async saveArtifacts(invocationId, userId, sessionId, message) {
213
- var _a;
214
- if (!this.artifactService || !((_a = message.parts) == null ? void 0 : _a.length)) {
220
+ var _a2;
221
+ if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
215
222
  return;
216
223
  }
217
224
  for (let i = 0; i < message.parts.length; i++) {
@@ -244,7 +251,7 @@ class Runner {
244
251
  return rootAgent.findAgent(event.author) || rootAgent;
245
252
  }
246
253
  for (let i = session.events.length - 1; i >= 0; i--) {
247
- logger.info("event: ", JSON.stringify(session.events[i]));
254
+ logger.info("event:", JSON.stringify(session.events[i]));
248
255
  const event2 = session.events[i];
249
256
  if (event2.author === "user" || !event2.author) {
250
257
  continue;
@@ -292,12 +299,12 @@ class Runner {
292
299
  // TODO - b/425992518: Implement runLive and related methods.
293
300
  }
294
301
  function findEventByLastFunctionResponseId(events) {
295
- var _a, _b, _c, _d;
302
+ var _a2, _b, _c, _d;
296
303
  if (!events.length) {
297
304
  return null;
298
305
  }
299
306
  const lastEvent = events[events.length - 1];
300
- const functionCallId = (_d = (_c = (_b = (_a = lastEvent.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.find(
307
+ const functionCallId = (_d = (_c = (_b = (_a2 = lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b.find(
301
308
  (part) => part.functionResponse
302
309
  )) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
303
310
  if (!functionCallId) {
@@ -318,5 +325,6 @@ function findEventByLastFunctionResponseId(events) {
318
325
  return null;
319
326
  }
320
327
  export {
321
- Runner
328
+ Runner,
329
+ isRunner
322
330
  };
@@ -38,7 +38,7 @@ class DatabaseSessionService extends BaseSessionService {
38
38
  super();
39
39
  this.initialized = false;
40
40
  if (typeof connectionStringOrOptions === "string") {
41
- this.options = getConnectionOptionsFromUri(connectionStringOrOptions);
41
+ this.connectionString = connectionStringOrOptions;
42
42
  } else {
43
43
  if (!connectionStringOrOptions.driver) {
44
44
  throw new Error("Driver is required when passing options object.");
@@ -53,6 +53,9 @@ class DatabaseSessionService extends BaseSessionService {
53
53
  if (this.initialized) {
54
54
  return;
55
55
  }
56
+ if (this.connectionString && (!this.options || !this.options.driver)) {
57
+ this.options = await getConnectionOptionsFromUri(this.connectionString);
58
+ }
56
59
  this.orm = await MikroORM.init(this.options);
57
60
  await ensureDatabaseCreated(this.orm);
58
61
  await validateDatabaseSchemaVersion(this.orm);
@@ -1,31 +1,52 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from))
10
+ if (!__hasOwnProp.call(to, key) && key !== except)
11
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
+ }
13
+ return to;
14
+ };
15
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
1
23
  /**
2
24
  * @license
3
25
  * Copyright 2026 Google LLC
4
26
  * SPDX-License-Identifier: Apache-2.0
5
27
  */
6
- import { MikroORM } from "@mikro-orm/core";
7
- import { MariaDbDriver } from "@mikro-orm/mariadb";
8
- import { MsSqlDriver } from "@mikro-orm/mssql";
9
- import { MySqlDriver } from "@mikro-orm/mysql";
10
- import { PostgreSqlDriver } from "@mikro-orm/postgresql";
11
- import { SqliteDriver } from "@mikro-orm/sqlite";
12
28
  import {
13
29
  ENTITIES,
14
30
  SCHEMA_VERSION_1_JSON,
15
31
  SCHEMA_VERSION_KEY,
16
32
  StorageMetadata
17
33
  } from "./schema.js";
18
- function getConnectionOptionsFromUri(uri) {
34
+ async function getConnectionOptionsFromUri(uri) {
19
35
  let driver;
20
36
  if (uri.startsWith("postgres://") || uri.startsWith("postgresql://")) {
37
+ const { PostgreSqlDriver } = await Promise.resolve().then(() => __toESM(require("@mikro-orm/postgresql"), 1));
21
38
  driver = PostgreSqlDriver;
22
39
  } else if (uri.startsWith("mysql://")) {
40
+ const { MySqlDriver } = await Promise.resolve().then(() => __toESM(require("@mikro-orm/mysql"), 1));
23
41
  driver = MySqlDriver;
24
42
  } else if (uri.startsWith("mariadb://")) {
43
+ const { MariaDbDriver } = await Promise.resolve().then(() => __toESM(require("@mikro-orm/mariadb"), 1));
25
44
  driver = MariaDbDriver;
26
45
  } else if (uri.startsWith("sqlite://")) {
46
+ const { SqliteDriver } = await Promise.resolve().then(() => __toESM(require("@mikro-orm/sqlite"), 1));
27
47
  driver = SqliteDriver;
28
48
  } else if (uri.startsWith("mssql://")) {
49
+ const { MsSqlDriver } = await Promise.resolve().then(() => __toESM(require("@mikro-orm/mssql"), 1));
29
50
  driver = MsSqlDriver;
30
51
  } else {
31
52
  throw new Error(`Unsupported database URI: ${uri}`);
@@ -39,9 +60,10 @@ function getConnectionOptionsFromUri(uri) {
39
60
  }
40
61
  const { host, port, username, password, pathname } = new URL(uri);
41
62
  const hostName = host.split(":")[0];
63
+ const dbName = uri.startsWith("sqlite://") ? uri.substring("sqlite://".length) : pathname.slice(1);
42
64
  return {
43
65
  entities: ENTITIES,
44
- dbName: pathname.slice(1),
66
+ dbName,
45
67
  host: hostName,
46
68
  port: port ? parseInt(port) : void 0,
47
69
  user: username,
@@ -49,17 +71,9 @@ function getConnectionOptionsFromUri(uri) {
49
71
  driver
50
72
  };
51
73
  }
52
- async function ensureDatabaseCreated(ormOrUrlOrOptions) {
53
- let orm;
54
- if (ormOrUrlOrOptions instanceof MikroORM) {
55
- orm = ormOrUrlOrOptions;
56
- } else if (typeof ormOrUrlOrOptions === "string") {
57
- orm = await MikroORM.init(getConnectionOptionsFromUri(ormOrUrlOrOptions));
58
- } else {
59
- orm = await MikroORM.init(ormOrUrlOrOptions);
60
- }
74
+ async function ensureDatabaseCreated(orm) {
61
75
  await orm.schema.ensureDatabase();
62
- await orm.schema.updateSchema();
76
+ await orm.schema.updateSchema({ safe: true });
63
77
  }
64
78
  async function validateDatabaseSchemaVersion(orm) {
65
79
  const em = orm.em.fork();
@@ -53,8 +53,8 @@ class State {
53
53
  * @param delta The delta to update the state with.
54
54
  */
55
55
  update(delta) {
56
- this.delta = { ...this.delta, ...delta };
57
- this.value = { ...this.value, ...delta };
56
+ Object.assign(this.delta, delta);
57
+ Object.assign(this.value, delta);
58
58
  }
59
59
  /**
60
60
  * Returns the state as a plain JSON object.
@@ -63,7 +63,7 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
63
63
  args,
64
64
  toolContext
65
65
  }) {
66
- var _a2, _b2;
66
+ var _a2, _b2, _c, _d;
67
67
  if (this.skipSummarization) {
68
68
  toolContext.actions.skipSummarization = true;
69
69
  }
@@ -82,13 +82,14 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
82
82
  appName: this.agent.name,
83
83
  agent: this.agent,
84
84
  artifactService: new ForwardingArtifactService(toolContext),
85
- sessionService: new InMemorySessionService(),
86
- memoryService: new InMemoryMemoryService(),
85
+ sessionService: (_a2 = toolContext.invocationContext.sessionService) != null ? _a2 : new InMemorySessionService(),
86
+ memoryService: (_b2 = toolContext.invocationContext.memoryService) != null ? _b2 : new InMemoryMemoryService(),
87
87
  credentialService: toolContext.invocationContext.credentialService
88
88
  });
89
89
  const session = await runner.sessionService.createSession({
90
90
  appName: this.agent.name,
91
- userId: "tmp_user",
91
+ userId: toolContext.invocationContext.userId,
92
+ sessionId: toolContext.invocationContext.session.id,
92
93
  state: toolContext.state.toRecord()
93
94
  });
94
95
  let lastEvent;
@@ -102,7 +103,7 @@ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
102
103
  }
103
104
  lastEvent = event;
104
105
  }
105
- if (!((_b2 = (_a2 = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
106
+ if (!((_d = (_c = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _c.parts) == null ? void 0 : _d.length)) {
106
107
  return "";
107
108
  }
108
109
  const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
@@ -54,6 +54,9 @@ class BaseTool {
54
54
  if (!functionDeclaration) {
55
55
  return;
56
56
  }
57
+ if (this.name in llmRequest.toolsDict) {
58
+ throw new Error(`Duplicate tool name: ${this.name}`);
59
+ }
57
60
  llmRequest.toolsDict[this.name] = this;
58
61
  const tool = findToolWithFunctionDeclarations(llmRequest);
59
62
  if (tool) {
@@ -3,9 +3,17 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ var _a;
7
+ const BASE_TOOLSET_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseToolset");
8
+ function isBaseToolset(obj) {
9
+ return typeof obj === "object" && obj !== null && BASE_TOOLSET_SIGNATURE_SYMBOL in obj && obj[BASE_TOOLSET_SIGNATURE_SYMBOL] === true;
10
+ }
11
+ _a = BASE_TOOLSET_SIGNATURE_SYMBOL;
6
12
  class BaseToolset {
7
- constructor(toolFilter) {
13
+ constructor(toolFilter, prefix) {
8
14
  this.toolFilter = toolFilter;
15
+ this.prefix = prefix;
16
+ this[_a] = true;
9
17
  }
10
18
  /**
11
19
  * Returns whether the tool should be exposed to LLM.
@@ -42,5 +50,6 @@ class BaseToolset {
42
50
  }
43
51
  }
44
52
  export {
45
- BaseToolset
53
+ BaseToolset,
54
+ isBaseToolset
46
55
  };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { BaseTool } from "./base_tool.js";
7
+ class ExitLoopTool extends BaseTool {
8
+ constructor() {
9
+ super({
10
+ name: "exit_loop",
11
+ description: "Exits the loop.\n\nCall this function only when you are instructed to do so."
12
+ });
13
+ }
14
+ _getDeclaration() {
15
+ return {
16
+ name: this.name,
17
+ description: this.description
18
+ };
19
+ }
20
+ async runAsync({
21
+ toolContext
22
+ }) {
23
+ toolContext.actions.escalate = true;
24
+ toolContext.actions.skipSummarization = true;
25
+ return "";
26
+ }
27
+ }
28
+ const EXIT_LOOP = new ExitLoopTool();
29
+ export {
30
+ EXIT_LOOP,
31
+ ExitLoopTool
32
+ };
@@ -0,0 +1,159 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { Type } from "@google/genai";
7
+ import { appendInstructions } from "../models/llm_request.js";
8
+ import { getLogger } from "../utils/logger.js";
9
+ import {
10
+ BaseTool
11
+ } from "./base_tool.js";
12
+ const logger = getLogger();
13
+ const GEMINI_SUPPORTED_INLINE_MIME_PREFIXES = ["image/", "audio/", "video/"];
14
+ const GEMINI_SUPPORTED_INLINE_MIME_TYPES = /* @__PURE__ */ new Set(["application/pdf"]);
15
+ const TEXT_LIKE_MIME_TYPES = /* @__PURE__ */ new Set([
16
+ "application/csv",
17
+ "application/json",
18
+ "application/xml"
19
+ ]);
20
+ function normalizeMimeType(mimeType) {
21
+ if (!mimeType) {
22
+ return void 0;
23
+ }
24
+ return mimeType.split(";")[0].trim();
25
+ }
26
+ function isInlineMimeTypeSupported(mimeType) {
27
+ const normalized = normalizeMimeType(mimeType);
28
+ if (!normalized) {
29
+ return false;
30
+ }
31
+ return GEMINI_SUPPORTED_INLINE_MIME_PREFIXES.some(
32
+ (prefix) => normalized.startsWith(prefix)
33
+ ) || GEMINI_SUPPORTED_INLINE_MIME_TYPES.has(normalized);
34
+ }
35
+ function asSafePartForLlm(artifact, artifactName) {
36
+ const inlineData = artifact.inlineData;
37
+ if (!inlineData) {
38
+ return artifact;
39
+ }
40
+ if (isInlineMimeTypeSupported(inlineData.mimeType)) {
41
+ return artifact;
42
+ }
43
+ const mimeType = normalizeMimeType(inlineData.mimeType) || "application/octet-stream";
44
+ const data = inlineData.data;
45
+ if (!data) {
46
+ return {
47
+ text: `[Artifact: ${artifactName}, type: ${mimeType}. No inline data was provided.]`
48
+ };
49
+ }
50
+ const isTextLike = mimeType.startsWith("text/") || TEXT_LIKE_MIME_TYPES.has(mimeType);
51
+ const decodedBuffer = Buffer.from(data, "base64");
52
+ if (isTextLike) {
53
+ try {
54
+ const decoded = decodedBuffer.toString("utf8");
55
+ return { text: decoded };
56
+ } catch {
57
+ }
58
+ }
59
+ const sizeKb = decodedBuffer.length / 1024;
60
+ return {
61
+ text: `[Binary artifact: ${artifactName}, type: ${mimeType}, size: ${sizeKb.toFixed(1)} KB. Content cannot be displayed inline.]`
62
+ };
63
+ }
64
+ class LoadArtifactsTool extends BaseTool {
65
+ constructor() {
66
+ super({
67
+ name: "load_artifacts",
68
+ description: `Loads artifacts into the session for this request.
69
+
70
+ NOTE: Call when you need access to artifacts (for example, uploads saved by the web UI).`
71
+ });
72
+ }
73
+ _getDeclaration() {
74
+ return {
75
+ name: this.name,
76
+ description: this.description,
77
+ parameters: {
78
+ type: Type.OBJECT,
79
+ properties: {
80
+ artifact_names: {
81
+ type: Type.ARRAY,
82
+ items: {
83
+ type: Type.STRING
84
+ },
85
+ description: "The names of the artifacts to load."
86
+ }
87
+ }
88
+ }
89
+ };
90
+ }
91
+ async runAsync({ args }) {
92
+ const artifactNames = args["artifact_names"] || [];
93
+ return {
94
+ artifact_names: artifactNames,
95
+ status: "artifact contents temporarily inserted and removed. to access these artifacts, call load_artifacts tool again."
96
+ };
97
+ }
98
+ async processLlmRequest(request) {
99
+ await super.processLlmRequest(request);
100
+ await this.appendArtifactsToLlmRequest(
101
+ request.toolContext,
102
+ request.llmRequest
103
+ );
104
+ }
105
+ async appendArtifactsToLlmRequest(toolContext, llmRequest) {
106
+ if (!toolContext.invocationContext.artifactService) {
107
+ return;
108
+ }
109
+ const artifactNames = await toolContext.listArtifacts();
110
+ if (!artifactNames || artifactNames.length === 0) {
111
+ return;
112
+ }
113
+ appendInstructions(llmRequest, [
114
+ `You have a list of artifacts:
115
+ ${JSON.stringify(
116
+ artifactNames
117
+ )}
118
+
119
+ When the user asks questions about any of the artifacts, you should call the
120
+ \`load_artifacts\` function to load the artifact. Always call load_artifacts
121
+ before answering questions related to the artifacts, regardless of whether the
122
+ artifacts have been loaded before. Do not depend on prior answers about the
123
+ artifacts.`
124
+ ]);
125
+ const contents = llmRequest.contents;
126
+ if (contents && contents.length > 0) {
127
+ const lastContent = contents[contents.length - 1];
128
+ if (lastContent.role === "user" && lastContent.parts && lastContent.parts.length > 0) {
129
+ const functionResponsePart = lastContent.parts[0];
130
+ const functionResponse = functionResponsePart.functionResponse;
131
+ if (functionResponse && functionResponse.name === "load_artifacts") {
132
+ const response = functionResponse.response || {};
133
+ const namesToLoad = response["artifact_names"] || [];
134
+ for (const artifactName of namesToLoad) {
135
+ let artifact = await toolContext.loadArtifact(artifactName);
136
+ if (!artifact && !artifactName.startsWith("user:")) {
137
+ const prefixedName = `user:${artifactName}`;
138
+ artifact = await toolContext.loadArtifact(prefixedName);
139
+ }
140
+ if (!artifact) {
141
+ logger.warn(`Artifact "${artifactName}" not found, skipping`);
142
+ continue;
143
+ }
144
+ const artifactPart = asSafePartForLlm(artifact, artifactName);
145
+ llmRequest.contents.push({
146
+ role: "user",
147
+ parts: [{ text: `Artifact ${artifactName} is:` }, artifactPart]
148
+ });
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }
154
+ }
155
+ const LOAD_ARTIFACTS = new LoadArtifactsTool();
156
+ export {
157
+ LOAD_ARTIFACTS,
158
+ LoadArtifactsTool
159
+ };
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { Type } from "@google/genai";
7
+ import { appendInstructions } from "../models/llm_request.js";
8
+ import {
9
+ BaseTool
10
+ } from "./base_tool.js";
11
+ class LoadMemoryTool extends BaseTool {
12
+ constructor() {
13
+ super({
14
+ name: "load_memory",
15
+ description: "Loads the memory for the current user.\n\nNOTE: Currently this tool only uses text part from the memory."
16
+ });
17
+ }
18
+ _getDeclaration() {
19
+ return {
20
+ name: this.name,
21
+ description: this.description,
22
+ parameters: {
23
+ type: Type.OBJECT,
24
+ properties: {
25
+ query: {
26
+ type: Type.STRING,
27
+ description: "The query to load the memory for."
28
+ }
29
+ },
30
+ required: ["query"]
31
+ }
32
+ };
33
+ }
34
+ async runAsync({
35
+ args,
36
+ toolContext
37
+ }) {
38
+ try {
39
+ const query = args["query"];
40
+ if (!toolContext.invocationContext.memoryService) {
41
+ throw new Error("Memory service is not initialized.");
42
+ }
43
+ const searchMemoryResponse = await toolContext.searchMemory(query);
44
+ return {
45
+ memories: searchMemoryResponse.memories.map((m) => {
46
+ var _a, _b;
47
+ return {
48
+ // Join all text parts by a space, or empty string if no text parts
49
+ content: (_b = (_a = m.content.parts) == null ? void 0 : _a.map((p) => {
50
+ var _a2;
51
+ return (_a2 = p.text) != null ? _a2 : "";
52
+ }).join(" ")) != null ? _b : "",
53
+ author: m.author,
54
+ timestamp: m.timestamp
55
+ };
56
+ })
57
+ };
58
+ } catch (e) {
59
+ console.error("ERROR in LoadMemoryTool runAsync:", e);
60
+ throw e;
61
+ }
62
+ }
63
+ async processLlmRequest(request) {
64
+ await super.processLlmRequest(request);
65
+ if (!request.toolContext.invocationContext.memoryService) {
66
+ return;
67
+ }
68
+ appendInstructions(request.llmRequest, [
69
+ `You have memory. You can use it to answer questions. If any questions need
70
+ you to look up the memory, you should call load_memory function with a query.`
71
+ ]);
72
+ }
73
+ }
74
+ const LOAD_MEMORY = new LoadMemoryTool();
75
+ export {
76
+ LOAD_MEMORY,
77
+ LoadMemoryTool
78
+ };
@@ -8,8 +8,8 @@ import { BaseToolset } from "../base_toolset.js";
8
8
  import { MCPSessionManager } from "./mcp_session_manager.js";
9
9
  import { MCPTool } from "./mcp_tool.js";
10
10
  class MCPToolset extends BaseToolset {
11
- constructor(connectionParams, toolFilter = []) {
12
- super(toolFilter);
11
+ constructor(connectionParams, toolFilter = [], prefix) {
12
+ super(toolFilter, prefix);
13
13
  this.mcpSessionManager = new MCPSessionManager(connectionParams);
14
14
  }
15
15
  async getTools() {
@@ -19,9 +19,13 @@ class MCPToolset extends BaseToolset {
19
19
  for (const tool of listResult.tools) {
20
20
  logger.debug(`tool: ${tool.name}`);
21
21
  }
22
- return listResult.tools.map(
23
- (tool) => new MCPTool(tool, this.mcpSessionManager)
24
- );
22
+ return listResult.tools.map((tool) => {
23
+ const toolWithPrefix = {
24
+ ...tool,
25
+ name: this.prefix ? `${this.prefix}_${tool.name}` : tool.name
26
+ };
27
+ return new MCPTool(toolWithPrefix, this.mcpSessionManager);
28
+ });
25
29
  }
26
30
  async close() {
27
31
  }