@google/adk 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) 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 +179 -0
  4. package/dist/cjs/a2a/a2a_remote_agent_run_processor.js +205 -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/gemini_schema_util.js +54 -12
  37. package/dist/cjs/utils/logger.js +1 -0
  38. package/dist/cjs/version.js +1 -1
  39. package/dist/esm/a2a/a2a_event.js +52 -26
  40. package/dist/esm/a2a/a2a_remote_agent.js +153 -0
  41. package/dist/esm/a2a/a2a_remote_agent_run_processor.js +182 -0
  42. package/dist/esm/a2a/a2a_remote_agent_utils.js +131 -0
  43. package/dist/esm/a2a/agent_card.js +340 -0
  44. package/dist/esm/a2a/agent_executor.js +202 -0
  45. package/dist/esm/a2a/agent_to_a2a.js +80 -0
  46. package/dist/esm/a2a/event_processor_utils.js +159 -0
  47. package/dist/esm/a2a/executor_context.js +1 -1
  48. package/dist/esm/a2a/metadata_converter_utils.js +1 -0
  49. package/dist/esm/a2a/part_converter_utils.js +24 -13
  50. package/dist/esm/agents/llm_agent.js +17 -0
  51. package/dist/esm/agents/{content_processor_utils.js → processors/content_processor_utils.js} +23 -2
  52. package/dist/esm/agents/processors/content_request_processor.js +25 -4
  53. package/dist/esm/agents/processors/context_compactor_request_processor.js +31 -0
  54. package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
  55. package/dist/esm/artifacts/file_artifact_service.js +30 -3
  56. package/dist/esm/common.js +33 -1
  57. package/dist/esm/context/base_context_compactor.js +5 -0
  58. package/dist/esm/context/summarizers/base_summarizer.js +5 -0
  59. package/dist/esm/context/summarizers/llm_summarizer.js +65 -0
  60. package/dist/esm/context/token_based_context_compactor.js +105 -0
  61. package/dist/esm/context/truncating_context_compactor.js +28 -0
  62. package/dist/esm/events/compacted_event.js +22 -0
  63. package/dist/esm/index.js +38 -14
  64. package/dist/esm/index.js.map +4 -4
  65. package/dist/esm/memory/in_memory_memory_service.js +1 -1
  66. package/dist/esm/runner/runner.js +17 -9
  67. package/dist/esm/sessions/db/operations.js +4 -14
  68. package/dist/esm/sessions/state.js +2 -2
  69. package/dist/esm/tools/agent_tool.js +6 -5
  70. package/dist/esm/tools/load_artifacts_tool.js +159 -0
  71. package/dist/esm/tools/load_memory_tool.js +78 -0
  72. package/dist/esm/tools/preload_memory_tool.js +80 -0
  73. package/dist/esm/utils/gemini_schema_util.js +54 -12
  74. package/dist/esm/utils/logger.js +1 -0
  75. package/dist/esm/version.js +1 -1
  76. package/dist/types/a2a/a2a_event.d.ts +23 -6
  77. package/dist/types/a2a/a2a_remote_agent.d.ts +67 -0
  78. package/dist/types/a2a/a2a_remote_agent_run_processor.d.ts +31 -0
  79. package/dist/types/a2a/a2a_remote_agent_utils.d.ts +38 -0
  80. package/dist/types/a2a/agent_card.d.ts +23 -0
  81. package/dist/types/a2a/agent_executor.d.ts +52 -0
  82. package/dist/types/a2a/agent_to_a2a.d.ts +45 -0
  83. package/dist/types/a2a/event_processor_utils.d.ts +24 -0
  84. package/dist/types/a2a/executor_context.d.ts +1 -1
  85. package/dist/types/a2a/metadata_converter_utils.d.ts +2 -1
  86. package/dist/types/agents/llm_agent.d.ts +6 -0
  87. package/dist/types/agents/loop_agent.d.ts +1 -1
  88. package/dist/types/agents/{content_processor_utils.d.ts → processors/content_processor_utils.d.ts} +1 -1
  89. package/dist/types/agents/processors/context_compactor_request_processor.d.ts +22 -0
  90. package/dist/types/artifacts/file_artifact_service.d.ts +4 -0
  91. package/dist/types/common.d.ts +16 -1
  92. package/dist/types/context/base_context_compactor.d.ts +24 -0
  93. package/dist/types/context/summarizers/base_summarizer.d.ts +19 -0
  94. package/dist/types/context/summarizers/llm_summarizer.d.ts +23 -0
  95. package/dist/types/context/token_based_context_compactor.d.ts +33 -0
  96. package/dist/types/context/truncating_context_compactor.d.ts +24 -0
  97. package/dist/types/events/compacted_event.d.ts +33 -0
  98. package/dist/types/index.d.ts +8 -0
  99. package/dist/types/runner/runner.d.ts +13 -0
  100. package/dist/types/sessions/db/operations.d.ts +2 -3
  101. package/dist/types/tools/load_artifacts_tool.d.ts +21 -0
  102. package/dist/types/tools/load_memory_tool.d.ts +22 -0
  103. package/dist/types/tools/preload_memory_tool.d.ts +23 -0
  104. package/dist/types/version.d.ts +1 -1
  105. package/dist/web/a2a/a2a_event.js +52 -26
  106. package/dist/web/a2a/a2a_remote_agent.js +184 -0
  107. package/dist/web/a2a/a2a_remote_agent_run_processor.js +182 -0
  108. package/dist/web/a2a/a2a_remote_agent_utils.js +131 -0
  109. package/dist/web/a2a/agent_card.js +340 -0
  110. package/dist/web/a2a/agent_executor.js +216 -0
  111. package/dist/web/a2a/agent_to_a2a.js +80 -0
  112. package/dist/web/a2a/event_processor_utils.js +168 -0
  113. package/dist/web/a2a/executor_context.js +1 -1
  114. package/dist/web/a2a/metadata_converter_utils.js +1 -0
  115. package/dist/web/a2a/part_converter_utils.js +24 -13
  116. package/dist/web/agents/llm_agent.js +17 -0
  117. package/dist/web/agents/{content_processor_utils.js → processors/content_processor_utils.js} +22 -2
  118. package/dist/web/agents/processors/content_request_processor.js +25 -4
  119. package/dist/web/agents/processors/context_compactor_request_processor.js +49 -0
  120. package/dist/web/agents/processors/instructions_llm_request_processor.js +1 -1
  121. package/dist/web/artifacts/file_artifact_service.js +30 -3
  122. package/dist/web/common.js +33 -1
  123. package/dist/web/context/base_context_compactor.js +5 -0
  124. package/dist/web/context/summarizers/base_summarizer.js +5 -0
  125. package/dist/web/context/summarizers/llm_summarizer.js +74 -0
  126. package/dist/web/context/token_based_context_compactor.js +105 -0
  127. package/dist/web/context/truncating_context_compactor.js +28 -0
  128. package/dist/web/events/compacted_event.js +40 -0
  129. package/dist/web/index.js +1 -1
  130. package/dist/web/index.js.map +4 -4
  131. package/dist/web/memory/in_memory_memory_service.js +1 -1
  132. package/dist/web/runner/runner.js +17 -9
  133. package/dist/web/sessions/db/operations.js +4 -14
  134. package/dist/web/sessions/state.js +2 -2
  135. package/dist/web/tools/agent_tool.js +6 -5
  136. package/dist/web/tools/load_artifacts_tool.js +150 -0
  137. package/dist/web/tools/load_memory_tool.js +77 -0
  138. package/dist/web/tools/preload_memory_tool.js +75 -0
  139. package/dist/web/utils/gemini_schema_util.js +85 -12
  140. package/dist/web/utils/logger.js +1 -0
  141. package/dist/web/version.js +1 -1
  142. package/package.json +7 -5
@@ -87,7 +87,7 @@ function getUserKey(appName, userId) {
87
87
  }
88
88
  function extractWordsLower(text) {
89
89
  return new Set(
90
- [...text.matchAll(/[A-Za-z]+/)].map((match) => match[0].toLowerCase())
90
+ [...text.matchAll(/[A-Za-z]+/g)].map((match) => match[0].toLowerCase())
91
91
  );
92
92
  }
93
93
  function formatTimestamp(timestamp) {
@@ -24,7 +24,8 @@ var __copyProps = (to, from, except, desc) => {
24
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
25
  var runner_exports = {};
26
26
  __export(runner_exports, {
27
- Runner: () => Runner
27
+ Runner: () => Runner,
28
+ isRunner: () => isRunner
28
29
  });
29
30
  module.exports = __toCommonJS(runner_exports);
30
31
  var import_genai = require("@google/genai");
@@ -44,12 +45,19 @@ var import_model_name = require("../utils/model_name.js");
44
45
  * Copyright 2025 Google LLC
45
46
  * SPDX-License-Identifier: Apache-2.0
46
47
  */
48
+ var _a;
49
+ const RUNNER_SIGNATURE_SYMBOL = Symbol.for("google.adk.runner");
50
+ function isRunner(obj) {
51
+ return typeof obj === "object" && obj !== null && RUNNER_SIGNATURE_SYMBOL in obj && obj[RUNNER_SIGNATURE_SYMBOL] === true;
52
+ }
53
+ _a = RUNNER_SIGNATURE_SYMBOL;
47
54
  class Runner {
48
55
  constructor(input) {
49
- var _a;
56
+ this[_a] = true;
57
+ var _a2;
50
58
  this.appName = input.appName;
51
59
  this.agent = input.agent;
52
- this.pluginManager = new import_plugin_manager.PluginManager((_a = input.plugins) != null ? _a : []);
60
+ this.pluginManager = new import_plugin_manager.PluginManager((_a2 = input.plugins) != null ? _a2 : []);
53
61
  this.artifactService = input.artifactService;
54
62
  this.sessionService = input.sessionService;
55
63
  this.memoryService = input.memoryService;
@@ -109,7 +117,7 @@ class Runner {
109
117
  ctx,
110
118
  this,
111
119
  async function* () {
112
- var _a;
120
+ var _a2;
113
121
  const session = await this.sessionService.getSession({
114
122
  appName: this.appName,
115
123
  userId,
@@ -154,7 +162,7 @@ class Runner {
154
162
  newMessage = pluginUserMessage;
155
163
  }
156
164
  if (newMessage) {
157
- if (!((_a = newMessage.parts) == null ? void 0 : _a.length)) {
165
+ if (!((_a2 = newMessage.parts) == null ? void 0 : _a2.length)) {
158
166
  throw new Error("No parts in the newMessage.");
159
167
  }
160
168
  if (runConfig.saveInputBlobsAsArtifacts) {
@@ -230,8 +238,8 @@ class Runner {
230
238
  * @param message The message containing parts to process.
231
239
  */
232
240
  async saveArtifacts(invocationId, userId, sessionId, message) {
233
- var _a;
234
- if (!this.artifactService || !((_a = message.parts) == null ? void 0 : _a.length)) {
241
+ var _a2;
242
+ if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
235
243
  return;
236
244
  }
237
245
  for (let i = 0; i < message.parts.length; i++) {
@@ -312,12 +320,12 @@ class Runner {
312
320
  // TODO - b/425992518: Implement runLive and related methods.
313
321
  }
314
322
  function findEventByLastFunctionResponseId(events) {
315
- var _a, _b, _c, _d;
323
+ var _a2, _b, _c, _d;
316
324
  if (!events.length) {
317
325
  return null;
318
326
  }
319
327
  const lastEvent = events[events.length - 1];
320
- const functionCallId = (_d = (_c = (_b = (_a = lastEvent.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.find(
328
+ const functionCallId = (_d = (_c = (_b = (_a2 = lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b.find(
321
329
  (part) => part.functionResponse
322
330
  )) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
323
331
  if (!functionCallId) {
@@ -339,5 +347,6 @@ function findEventByLastFunctionResponseId(events) {
339
347
  }
340
348
  // Annotate the CommonJS export names for ESM import in node:
341
349
  0 && (module.exports = {
342
- Runner
350
+ Runner,
351
+ isRunner
343
352
  });
@@ -39,7 +39,6 @@ __export(operations_exports, {
39
39
  validateDatabaseSchemaVersion: () => validateDatabaseSchemaVersion
40
40
  });
41
41
  module.exports = __toCommonJS(operations_exports);
42
- var import_core = require("@mikro-orm/core");
43
42
  var import_schema = require("./schema.js");
44
43
  /**
45
44
  * @license
@@ -75,9 +74,10 @@ async function getConnectionOptionsFromUri(uri) {
75
74
  }
76
75
  const { host, port, username, password, pathname } = new URL(uri);
77
76
  const hostName = host.split(":")[0];
77
+ const dbName = uri.startsWith("sqlite://") ? uri.substring("sqlite://".length) : pathname.slice(1);
78
78
  return {
79
79
  entities: import_schema.ENTITIES,
80
- dbName: pathname.slice(1),
80
+ dbName,
81
81
  host: hostName,
82
82
  port: port ? parseInt(port) : void 0,
83
83
  user: username,
@@ -85,19 +85,9 @@ async function getConnectionOptionsFromUri(uri) {
85
85
  driver
86
86
  };
87
87
  }
88
- async function ensureDatabaseCreated(ormOrUrlOrOptions) {
89
- let orm;
90
- if (ormOrUrlOrOptions instanceof import_core.MikroORM) {
91
- orm = ormOrUrlOrOptions;
92
- } else if (typeof ormOrUrlOrOptions === "string") {
93
- orm = await import_core.MikroORM.init(
94
- await getConnectionOptionsFromUri(ormOrUrlOrOptions)
95
- );
96
- } else {
97
- orm = await import_core.MikroORM.init(ormOrUrlOrOptions);
98
- }
88
+ async function ensureDatabaseCreated(orm) {
99
89
  await orm.schema.ensureDatabase();
100
- await orm.schema.updateSchema();
90
+ await orm.schema.updateSchema({ safe: true });
101
91
  }
102
92
  async function validateDatabaseSchemaVersion(orm) {
103
93
  const em = orm.em.fork();
@@ -82,8 +82,8 @@ class State {
82
82
  * @param delta The delta to update the state with.
83
83
  */
84
84
  update(delta) {
85
- this.delta = { ...this.delta, ...delta };
86
- this.value = { ...this.value, ...delta };
85
+ Object.assign(this.delta, delta);
86
+ Object.assign(this.value, delta);
87
87
  }
88
88
  /**
89
89
  * Returns the state as a plain JSON object.
@@ -93,7 +93,7 @@ class AgentTool extends (_b = import_base_tool.BaseTool, _a = AGENT_TOOL_SIGNATU
93
93
  args,
94
94
  toolContext
95
95
  }) {
96
- var _a2, _b2;
96
+ var _a2, _b2, _c, _d;
97
97
  if (this.skipSummarization) {
98
98
  toolContext.actions.skipSummarization = true;
99
99
  }
@@ -112,13 +112,14 @@ class AgentTool extends (_b = import_base_tool.BaseTool, _a = AGENT_TOOL_SIGNATU
112
112
  appName: this.agent.name,
113
113
  agent: this.agent,
114
114
  artifactService: new import_forwarding_artifact_service.ForwardingArtifactService(toolContext),
115
- sessionService: new import_in_memory_session_service.InMemorySessionService(),
116
- memoryService: new import_in_memory_memory_service.InMemoryMemoryService(),
115
+ sessionService: (_a2 = toolContext.invocationContext.sessionService) != null ? _a2 : new import_in_memory_session_service.InMemorySessionService(),
116
+ memoryService: (_b2 = toolContext.invocationContext.memoryService) != null ? _b2 : new import_in_memory_memory_service.InMemoryMemoryService(),
117
117
  credentialService: toolContext.invocationContext.credentialService
118
118
  });
119
119
  const session = await runner.sessionService.createSession({
120
120
  appName: this.agent.name,
121
- userId: "tmp_user",
121
+ userId: toolContext.invocationContext.userId,
122
+ sessionId: toolContext.invocationContext.session.id,
122
123
  state: toolContext.state.toRecord()
123
124
  });
124
125
  let lastEvent;
@@ -132,7 +133,7 @@ class AgentTool extends (_b = import_base_tool.BaseTool, _a = AGENT_TOOL_SIGNATU
132
133
  }
133
134
  lastEvent = event;
134
135
  }
135
- if (!((_b2 = (_a2 = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
136
+ if (!((_d = (_c = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _c.parts) == null ? void 0 : _d.length)) {
136
137
  return "";
137
138
  }
138
139
  const hasOutputSchema = (0, import_llm_agent.isLlmAgent)(this.agent) && this.agent.outputSchema;
@@ -0,0 +1,188 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var load_artifacts_tool_exports = {};
26
+ __export(load_artifacts_tool_exports, {
27
+ LOAD_ARTIFACTS: () => LOAD_ARTIFACTS,
28
+ LoadArtifactsTool: () => LoadArtifactsTool
29
+ });
30
+ module.exports = __toCommonJS(load_artifacts_tool_exports);
31
+ var import_genai = require("@google/genai");
32
+ var import_llm_request = require("../models/llm_request.js");
33
+ var import_logger = require("../utils/logger.js");
34
+ var import_base_tool = require("./base_tool.js");
35
+ /**
36
+ * @license
37
+ * Copyright 2026 Google LLC
38
+ * SPDX-License-Identifier: Apache-2.0
39
+ */
40
+ const logger = (0, import_logger.getLogger)();
41
+ const GEMINI_SUPPORTED_INLINE_MIME_PREFIXES = ["image/", "audio/", "video/"];
42
+ const GEMINI_SUPPORTED_INLINE_MIME_TYPES = /* @__PURE__ */ new Set(["application/pdf"]);
43
+ const TEXT_LIKE_MIME_TYPES = /* @__PURE__ */ new Set([
44
+ "application/csv",
45
+ "application/json",
46
+ "application/xml"
47
+ ]);
48
+ function normalizeMimeType(mimeType) {
49
+ if (!mimeType) {
50
+ return void 0;
51
+ }
52
+ return mimeType.split(";")[0].trim();
53
+ }
54
+ function isInlineMimeTypeSupported(mimeType) {
55
+ const normalized = normalizeMimeType(mimeType);
56
+ if (!normalized) {
57
+ return false;
58
+ }
59
+ return GEMINI_SUPPORTED_INLINE_MIME_PREFIXES.some(
60
+ (prefix) => normalized.startsWith(prefix)
61
+ ) || GEMINI_SUPPORTED_INLINE_MIME_TYPES.has(normalized);
62
+ }
63
+ function asSafePartForLlm(artifact, artifactName) {
64
+ const inlineData = artifact.inlineData;
65
+ if (!inlineData) {
66
+ return artifact;
67
+ }
68
+ if (isInlineMimeTypeSupported(inlineData.mimeType)) {
69
+ return artifact;
70
+ }
71
+ const mimeType = normalizeMimeType(inlineData.mimeType) || "application/octet-stream";
72
+ const data = inlineData.data;
73
+ if (!data) {
74
+ return {
75
+ text: `[Artifact: ${artifactName}, type: ${mimeType}. No inline data was provided.]`
76
+ };
77
+ }
78
+ const isTextLike = mimeType.startsWith("text/") || TEXT_LIKE_MIME_TYPES.has(mimeType);
79
+ const decodedBuffer = Buffer.from(data, "base64");
80
+ if (isTextLike) {
81
+ try {
82
+ const decoded = decodedBuffer.toString("utf8");
83
+ return { text: decoded };
84
+ } catch {
85
+ }
86
+ }
87
+ const sizeKb = decodedBuffer.length / 1024;
88
+ return {
89
+ text: `[Binary artifact: ${artifactName}, type: ${mimeType}, size: ${sizeKb.toFixed(1)} KB. Content cannot be displayed inline.]`
90
+ };
91
+ }
92
+ class LoadArtifactsTool extends import_base_tool.BaseTool {
93
+ constructor() {
94
+ super({
95
+ name: "load_artifacts",
96
+ description: `Loads artifacts into the session for this request.
97
+
98
+ NOTE: Call when you need access to artifacts (for example, uploads saved by the web UI).`
99
+ });
100
+ }
101
+ _getDeclaration() {
102
+ return {
103
+ name: this.name,
104
+ description: this.description,
105
+ parameters: {
106
+ type: import_genai.Type.OBJECT,
107
+ properties: {
108
+ artifact_names: {
109
+ type: import_genai.Type.ARRAY,
110
+ items: {
111
+ type: import_genai.Type.STRING
112
+ },
113
+ description: "The names of the artifacts to load."
114
+ }
115
+ }
116
+ }
117
+ };
118
+ }
119
+ async runAsync({ args }) {
120
+ const artifactNames = args["artifact_names"] || [];
121
+ return {
122
+ artifact_names: artifactNames,
123
+ status: "artifact contents temporarily inserted and removed. to access these artifacts, call load_artifacts tool again."
124
+ };
125
+ }
126
+ async processLlmRequest(request) {
127
+ await super.processLlmRequest(request);
128
+ await this.appendArtifactsToLlmRequest(
129
+ request.toolContext,
130
+ request.llmRequest
131
+ );
132
+ }
133
+ async appendArtifactsToLlmRequest(toolContext, llmRequest) {
134
+ if (!toolContext.invocationContext.artifactService) {
135
+ return;
136
+ }
137
+ const artifactNames = await toolContext.listArtifacts();
138
+ if (!artifactNames || artifactNames.length === 0) {
139
+ return;
140
+ }
141
+ (0, import_llm_request.appendInstructions)(llmRequest, [
142
+ `You have a list of artifacts:
143
+ ${JSON.stringify(
144
+ artifactNames
145
+ )}
146
+
147
+ When the user asks questions about any of the artifacts, you should call the
148
+ \`load_artifacts\` function to load the artifact. Always call load_artifacts
149
+ before answering questions related to the artifacts, regardless of whether the
150
+ artifacts have been loaded before. Do not depend on prior answers about the
151
+ artifacts.`
152
+ ]);
153
+ const contents = llmRequest.contents;
154
+ if (contents && contents.length > 0) {
155
+ const lastContent = contents[contents.length - 1];
156
+ if (lastContent.role === "user" && lastContent.parts && lastContent.parts.length > 0) {
157
+ const functionResponsePart = lastContent.parts[0];
158
+ const functionResponse = functionResponsePart.functionResponse;
159
+ if (functionResponse && functionResponse.name === "load_artifacts") {
160
+ const response = functionResponse.response || {};
161
+ const namesToLoad = response["artifact_names"] || [];
162
+ for (const artifactName of namesToLoad) {
163
+ let artifact = await toolContext.loadArtifact(artifactName);
164
+ if (!artifact && !artifactName.startsWith("user:")) {
165
+ const prefixedName = `user:${artifactName}`;
166
+ artifact = await toolContext.loadArtifact(prefixedName);
167
+ }
168
+ if (!artifact) {
169
+ logger.warn(`Artifact "${artifactName}" not found, skipping`);
170
+ continue;
171
+ }
172
+ const artifactPart = asSafePartForLlm(artifact, artifactName);
173
+ llmRequest.contents.push({
174
+ role: "user",
175
+ parts: [{ text: `Artifact ${artifactName} is:` }, artifactPart]
176
+ });
177
+ }
178
+ }
179
+ }
180
+ }
181
+ }
182
+ }
183
+ const LOAD_ARTIFACTS = new LoadArtifactsTool();
184
+ // Annotate the CommonJS export names for ESM import in node:
185
+ 0 && (module.exports = {
186
+ LOAD_ARTIFACTS,
187
+ LoadArtifactsTool
188
+ });
@@ -0,0 +1,107 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var load_memory_tool_exports = {};
26
+ __export(load_memory_tool_exports, {
27
+ LOAD_MEMORY: () => LOAD_MEMORY,
28
+ LoadMemoryTool: () => LoadMemoryTool
29
+ });
30
+ module.exports = __toCommonJS(load_memory_tool_exports);
31
+ var import_genai = require("@google/genai");
32
+ var import_llm_request = require("../models/llm_request.js");
33
+ var import_base_tool = require("./base_tool.js");
34
+ /**
35
+ * @license
36
+ * Copyright 2026 Google LLC
37
+ * SPDX-License-Identifier: Apache-2.0
38
+ */
39
+ class LoadMemoryTool extends import_base_tool.BaseTool {
40
+ constructor() {
41
+ super({
42
+ name: "load_memory",
43
+ description: "Loads the memory for the current user.\n\nNOTE: Currently this tool only uses text part from the memory."
44
+ });
45
+ }
46
+ _getDeclaration() {
47
+ return {
48
+ name: this.name,
49
+ description: this.description,
50
+ parameters: {
51
+ type: import_genai.Type.OBJECT,
52
+ properties: {
53
+ query: {
54
+ type: import_genai.Type.STRING,
55
+ description: "The query to load the memory for."
56
+ }
57
+ },
58
+ required: ["query"]
59
+ }
60
+ };
61
+ }
62
+ async runAsync({
63
+ args,
64
+ toolContext
65
+ }) {
66
+ try {
67
+ const query = args["query"];
68
+ if (!toolContext.invocationContext.memoryService) {
69
+ throw new Error("Memory service is not initialized.");
70
+ }
71
+ const searchMemoryResponse = await toolContext.searchMemory(query);
72
+ return {
73
+ memories: searchMemoryResponse.memories.map((m) => {
74
+ var _a, _b;
75
+ return {
76
+ // Join all text parts by a space, or empty string if no text parts
77
+ content: (_b = (_a = m.content.parts) == null ? void 0 : _a.map((p) => {
78
+ var _a2;
79
+ return (_a2 = p.text) != null ? _a2 : "";
80
+ }).join(" ")) != null ? _b : "",
81
+ author: m.author,
82
+ timestamp: m.timestamp
83
+ };
84
+ })
85
+ };
86
+ } catch (e) {
87
+ console.error("ERROR in LoadMemoryTool runAsync:", e);
88
+ throw e;
89
+ }
90
+ }
91
+ async processLlmRequest(request) {
92
+ await super.processLlmRequest(request);
93
+ if (!request.toolContext.invocationContext.memoryService) {
94
+ return;
95
+ }
96
+ (0, import_llm_request.appendInstructions)(request.llmRequest, [
97
+ `You have memory. You can use it to answer questions. If any questions need
98
+ you to look up the memory, you should call load_memory function with a query.`
99
+ ]);
100
+ }
101
+ }
102
+ const LOAD_MEMORY = new LoadMemoryTool();
103
+ // Annotate the CommonJS export names for ESM import in node:
104
+ 0 && (module.exports = {
105
+ LOAD_MEMORY,
106
+ LoadMemoryTool
107
+ });
@@ -0,0 +1,109 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __export = (target, all) => {
13
+ for (var name in all)
14
+ __defProp(target, name, { get: all[name], enumerable: true });
15
+ };
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
+ var preload_memory_tool_exports = {};
26
+ __export(preload_memory_tool_exports, {
27
+ PRELOAD_MEMORY: () => PRELOAD_MEMORY,
28
+ PreloadMemoryTool: () => PreloadMemoryTool
29
+ });
30
+ module.exports = __toCommonJS(preload_memory_tool_exports);
31
+ var import_llm_request = require("../models/llm_request.js");
32
+ var import_logger = require("../utils/logger.js");
33
+ var import_base_tool = require("./base_tool.js");
34
+ /**
35
+ * @license
36
+ * Copyright 2026 Google LLC
37
+ * SPDX-License-Identifier: Apache-2.0
38
+ */
39
+ class PreloadMemoryTool extends import_base_tool.BaseTool {
40
+ constructor() {
41
+ super({
42
+ // Name and description are not used because this tool only
43
+ // changes llm_request.
44
+ name: "preload_memory",
45
+ description: "preload_memory"
46
+ });
47
+ }
48
+ async runAsync({
49
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
50
+ args,
51
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
52
+ toolContext
53
+ }) {
54
+ throw new Error("PreloadMemoryTool should not be called by model");
55
+ }
56
+ async processLlmRequest(request) {
57
+ var _a, _b, _c;
58
+ await super.processLlmRequest(request);
59
+ const userContent = request.toolContext.userContent;
60
+ if (!userContent || !userContent.parts || !((_a = userContent.parts[0]) == null ? void 0 : _a.text)) {
61
+ return;
62
+ }
63
+ const userQuery = userContent.parts[0].text;
64
+ let response;
65
+ try {
66
+ if (!request.toolContext.invocationContext.memoryService) {
67
+ return;
68
+ }
69
+ response = await request.toolContext.searchMemory(userQuery);
70
+ } catch (_) {
71
+ import_logger.logger.warn(`Failed to preload memory for query: ${userQuery}`);
72
+ return;
73
+ }
74
+ if (!response.memories || response.memories.length === 0) {
75
+ return;
76
+ }
77
+ const memoryTextLines = [];
78
+ for (const memory of response.memories) {
79
+ const timeStr = memory.timestamp ? `Time: ${memory.timestamp}` : "";
80
+ if (timeStr) memoryTextLines.push(timeStr);
81
+ const memoryText = (_c = (_b = memory.content.parts) == null ? void 0 : _b.map((p) => {
82
+ var _a2;
83
+ return (_a2 = p.text) != null ? _a2 : "";
84
+ }).join(" ")) != null ? _c : "";
85
+ if (memoryText) {
86
+ memoryTextLines.push(
87
+ memory.author ? `${memory.author}: ${memoryText}` : memoryText
88
+ );
89
+ }
90
+ }
91
+ if (memoryTextLines.length === 0) {
92
+ return;
93
+ }
94
+ const fullMemoryText = memoryTextLines.join("\n");
95
+ const si = `The following content is from your previous conversations with the user.
96
+ They may be useful for answering the user's current query.
97
+ <PAST_CONVERSATIONS>
98
+ ${fullMemoryText}
99
+ </PAST_CONVERSATIONS>
100
+ `;
101
+ (0, import_llm_request.appendInstructions)(request.llmRequest, [si]);
102
+ }
103
+ }
104
+ const PRELOAD_MEMORY = new PreloadMemoryTool();
105
+ // Annotate the CommonJS export names for ESM import in node:
106
+ 0 && (module.exports = {
107
+ PRELOAD_MEMORY,
108
+ PreloadMemoryTool
109
+ });
@@ -55,22 +55,50 @@ function toGeminiType(mcpType) {
55
55
  return import_genai.Type.ARRAY;
56
56
  case "object":
57
57
  return import_genai.Type.OBJECT;
58
+ case "null":
59
+ return import_genai.Type.NULL;
58
60
  default:
59
61
  return import_genai.Type.TYPE_UNSPECIFIED;
60
62
  }
61
63
  }
64
+ const getTypeFromArrayItem = (mcpType) => {
65
+ var _a, _b;
66
+ if (typeof mcpType === "string") {
67
+ return mcpType.toLowerCase();
68
+ }
69
+ return (_b = (_a = mcpType == null ? void 0 : mcpType.type) == null ? void 0 : _a.toLowerCase) == null ? void 0 : _b.call(_a);
70
+ };
62
71
  function toGeminiSchema(mcpSchema) {
63
72
  if (!mcpSchema) {
64
73
  return void 0;
65
74
  }
66
75
  function recursiveConvert(mcp) {
67
- if (!mcp.type && mcp.anyOf && Array.isArray(mcp.anyOf)) {
68
- const nonNullOption = mcp.anyOf.find((opt) => {
69
- const t = opt.type;
70
- return t !== "null" && t !== "NULL";
71
- });
72
- if (nonNullOption) {
73
- mcp = nonNullOption;
76
+ var _a;
77
+ const sourceType = (_a = mcp.anyOf) != null ? _a : mcp.type;
78
+ let isNullable = false;
79
+ let nonNullTypes;
80
+ if (Array.isArray(sourceType)) {
81
+ nonNullTypes = sourceType.filter(
82
+ (t) => getTypeFromArrayItem(t) !== "null"
83
+ );
84
+ isNullable = sourceType.some(
85
+ (t) => getTypeFromArrayItem(t) === "null"
86
+ );
87
+ if (nonNullTypes.length === 1) {
88
+ const nonNullType = nonNullTypes[0];
89
+ if (typeof nonNullType === "object") {
90
+ mcp = nonNullType;
91
+ } else {
92
+ const { type: _removed, anyOf: _removedAnyOf, ...rest } = mcp;
93
+ mcp = { ...rest, type: nonNullType };
94
+ }
95
+ } else if (nonNullTypes.length === 0 && isNullable) {
96
+ const { type: _removed, anyOf: _removedAnyOf, ...rest } = mcp;
97
+ mcp = { ...rest, type: "null" };
98
+ } else if (typeof mcp.anyOf === "undefined") {
99
+ const anyOfItems = mcp.type.map((t) => ({ type: t }));
100
+ const { type: _removed, ...rest } = mcp;
101
+ mcp = { ...rest, anyOf: anyOfItems };
74
102
  }
75
103
  }
76
104
  if (!mcp.type) {
@@ -78,13 +106,25 @@ function toGeminiSchema(mcpSchema) {
78
106
  mcp.type = "object";
79
107
  } else if (mcp.items) {
80
108
  mcp.type = "array";
109
+ } else if (isNullable) {
110
+ mcp.type = "null";
81
111
  }
82
112
  }
83
113
  const geminiType = toGeminiType(mcp.type);
84
- const geminiSchema = {
85
- type: geminiType,
86
- description: mcp.description
87
- };
114
+ const geminiSchema = {};
115
+ if (mcp.anyOf) {
116
+ geminiSchema.anyOf = mcp.anyOf.map(
117
+ (item) => recursiveConvert(item)
118
+ );
119
+ } else {
120
+ geminiSchema.type = geminiType;
121
+ }
122
+ if (mcp.description) {
123
+ geminiSchema.description = mcp.description;
124
+ }
125
+ if (isNullable && mcp.type !== "null") {
126
+ geminiSchema.nullable = true;
127
+ }
88
128
  if (geminiType === import_genai.Type.OBJECT) {
89
129
  geminiSchema.properties = {};
90
130
  if (mcp.properties) {
@@ -94,7 +134,9 @@ function toGeminiSchema(mcpSchema) {
94
134
  );
95
135
  }
96
136
  }
97
- geminiSchema.required = mcp.required;
137
+ if (mcp.required) {
138
+ geminiSchema.required = mcp.required;
139
+ }
98
140
  } else if (geminiType === import_genai.Type.ARRAY) {
99
141
  if (mcp.items) {
100
142
  geminiSchema.items = recursiveConvert(mcp.items);