@google/adk 1.3.0 → 1.4.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 (165) hide show
  1. package/dist/cjs/agents/active_streaming_tool.js +6 -1
  2. package/dist/cjs/agents/context.js +1 -11
  3. package/dist/cjs/agents/functions.js +35 -16
  4. package/dist/cjs/agents/instructions.js +23 -5
  5. package/dist/cjs/agents/invocation_context.js +2 -5
  6. package/dist/cjs/agents/llm_agent.js +8 -6
  7. package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
  8. package/dist/cjs/agents/processors/content_processor_utils.js +16 -3
  9. package/dist/cjs/agents/processors/context_compactor_request_processor.js +4 -2
  10. package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
  11. package/dist/cjs/apps/app.js +79 -0
  12. package/dist/cjs/apps/resumability_config.js +44 -0
  13. package/dist/cjs/artifacts/file_artifact_service.js +14 -1
  14. package/dist/cjs/artifacts/gcs_artifact_service.js +57 -15
  15. package/dist/cjs/artifacts/in_memory_artifact_service.js +5 -1
  16. package/dist/cjs/artifacts/scoped_artifact_service.js +101 -0
  17. package/dist/cjs/artifacts/session_artifact_service.js +27 -0
  18. package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
  19. package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
  20. package/dist/cjs/common.js +47 -0
  21. package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
  22. package/dist/cjs/context/token_based_context_compactor.js +42 -10
  23. package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
  24. package/dist/cjs/events/event.js +24 -0
  25. package/dist/cjs/models/google_llm.js +25 -10
  26. package/dist/cjs/models/interactions_utils.js +732 -0
  27. package/dist/cjs/plugins/base_plugin.js +1 -0
  28. package/dist/cjs/runner/in_memory_runner.js +13 -2
  29. package/dist/cjs/runner/runner.js +93 -72
  30. package/dist/cjs/sessions/base_session_service.js +6 -1
  31. package/dist/cjs/sessions/database_session_service.js +24 -7
  32. package/dist/cjs/sessions/vertex_ai_session_service.js +19 -4
  33. package/dist/cjs/skills/loader.js +2 -2
  34. package/dist/cjs/tools/base_toolset.js +1 -1
  35. package/dist/cjs/tools/consolidate_context_tool.js +79 -0
  36. package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
  37. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
  38. package/dist/cjs/utils/async_hooks_shim.js +52 -0
  39. package/dist/cjs/utils/client_labels.js +41 -3
  40. package/dist/cjs/utils/env_aware_utils.js +10 -1
  41. package/dist/cjs/utils/streaming_utils.js +10 -4
  42. package/dist/cjs/utils/task.js +62 -0
  43. package/dist/cjs/version.js +1 -1
  44. package/dist/esm/agents/active_streaming_tool.js +6 -1
  45. package/dist/esm/agents/context.js +1 -11
  46. package/dist/esm/agents/functions.js +36 -15
  47. package/dist/esm/agents/instructions.js +23 -5
  48. package/dist/esm/agents/invocation_context.js +2 -5
  49. package/dist/esm/agents/llm_agent.js +8 -6
  50. package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
  51. package/dist/esm/agents/processors/content_processor_utils.js +14 -2
  52. package/dist/esm/agents/processors/context_compactor_request_processor.js +4 -2
  53. package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
  54. package/dist/esm/apps/app.js +49 -0
  55. package/dist/esm/apps/resumability_config.js +16 -0
  56. package/dist/esm/artifacts/file_artifact_service.js +14 -1
  57. package/dist/esm/artifacts/gcs_artifact_service.js +57 -15
  58. package/dist/esm/artifacts/in_memory_artifact_service.js +5 -1
  59. package/dist/esm/artifacts/scoped_artifact_service.js +73 -0
  60. package/dist/esm/artifacts/session_artifact_service.js +7 -0
  61. package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
  62. package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
  63. package/dist/esm/common.js +42 -2
  64. package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
  65. package/dist/esm/context/token_based_context_compactor.js +42 -10
  66. package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
  67. package/dist/esm/events/event.js +22 -0
  68. package/dist/esm/models/google_llm.js +23 -9
  69. package/dist/esm/models/interactions_utils.js +697 -0
  70. package/dist/esm/plugins/base_plugin.js +1 -0
  71. package/dist/esm/runner/in_memory_runner.js +13 -2
  72. package/dist/esm/runner/runner.js +91 -73
  73. package/dist/esm/sessions/base_session_service.js +6 -1
  74. package/dist/esm/sessions/database_session_service.js +24 -7
  75. package/dist/esm/sessions/vertex_ai_session_service.js +17 -3
  76. package/dist/esm/skills/loader.js +2 -2
  77. package/dist/esm/tools/base_toolset.js +1 -1
  78. package/dist/esm/tools/consolidate_context_tool.js +51 -0
  79. package/dist/esm/tools/forwarding_artifact_service.js +16 -28
  80. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
  81. package/dist/esm/utils/async_hooks_shim.js +24 -0
  82. package/dist/esm/utils/client_labels.js +38 -2
  83. package/dist/esm/utils/env_aware_utils.js +10 -1
  84. package/dist/esm/utils/streaming_utils.js +10 -4
  85. package/dist/esm/utils/task.js +34 -0
  86. package/dist/esm/version.js +1 -1
  87. package/dist/types/agents/active_streaming_tool.d.ts +3 -4
  88. package/dist/types/agents/functions.d.ts +12 -9
  89. package/dist/types/agents/invocation_context.d.ts +7 -3
  90. package/dist/types/agents/processors/content_processor_utils.d.ts +8 -0
  91. package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
  92. package/dist/types/apps/app.d.ts +52 -0
  93. package/dist/types/apps/resumability_config.d.ts +27 -0
  94. package/dist/types/artifacts/base_artifact_service.d.ts +6 -36
  95. package/dist/types/artifacts/gcs_artifact_service.d.ts +2 -1
  96. package/dist/types/artifacts/scoped_artifact_service.d.ts +25 -0
  97. package/dist/types/artifacts/session_artifact_service.d.ts +25 -0
  98. package/dist/types/auth/oauth2/oauth2_discovery.d.ts +11 -0
  99. package/dist/types/common.d.ts +20 -3
  100. package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
  101. package/dist/types/context/base_context_compactor.d.ts +3 -0
  102. package/dist/types/context/token_based_context_compactor.d.ts +6 -1
  103. package/dist/types/context/trajectory_thought_pruning_compactor.d.ts +27 -0
  104. package/dist/types/events/event.d.ts +8 -0
  105. package/dist/types/models/google_llm.d.ts +15 -1
  106. package/dist/types/models/interactions_utils.d.ts +92 -0
  107. package/dist/types/models/llm_request.d.ts +4 -0
  108. package/dist/types/models/llm_response.d.ts +4 -0
  109. package/dist/types/plugins/base_plugin.d.ts +2 -1
  110. package/dist/types/runner/in_memory_runner.d.ts +7 -1
  111. package/dist/types/runner/runner.d.ts +43 -9
  112. package/dist/types/sessions/base_session_service.d.ts +3 -16
  113. package/dist/types/sessions/session.d.ts +11 -0
  114. package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
  115. package/dist/types/tools/consolidate_context_tool.d.ts +19 -0
  116. package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
  117. package/dist/types/utils/async_hooks_shim.d.ts +10 -0
  118. package/dist/types/utils/client_labels.d.ts +10 -0
  119. package/dist/types/utils/env_aware_utils.d.ts +3 -3
  120. package/dist/types/utils/task.d.ts +24 -0
  121. package/dist/types/version.d.ts +1 -1
  122. package/dist/web/agents/active_streaming_tool.js +6 -1
  123. package/dist/web/agents/context.js +1 -11
  124. package/dist/web/agents/functions.js +36 -15
  125. package/dist/web/agents/instructions.js +23 -5
  126. package/dist/web/agents/invocation_context.js +2 -5
  127. package/dist/web/agents/llm_agent.js +8 -6
  128. package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
  129. package/dist/web/agents/processors/content_processor_utils.js +14 -2
  130. package/dist/web/agents/processors/context_compactor_request_processor.js +4 -2
  131. package/dist/web/agents/processors/interactions_request_processor.js +52 -0
  132. package/dist/web/apps/app.js +49 -0
  133. package/dist/web/apps/resumability_config.js +31 -0
  134. package/dist/web/artifacts/file_artifact_service.js +14 -1
  135. package/dist/web/artifacts/gcs_artifact_service.js +55 -15
  136. package/dist/web/artifacts/in_memory_artifact_service.js +5 -1
  137. package/dist/web/artifacts/scoped_artifact_service.js +86 -0
  138. package/dist/web/artifacts/session_artifact_service.js +7 -0
  139. package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
  140. package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
  141. package/dist/web/common.js +42 -2
  142. package/dist/web/context/agent_controlled_context_compactor.js +81 -0
  143. package/dist/web/context/token_based_context_compactor.js +42 -10
  144. package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
  145. package/dist/web/events/event.js +20 -0
  146. package/dist/web/models/google_llm.js +54 -9
  147. package/dist/web/models/interactions_utils.js +728 -0
  148. package/dist/web/plugins/base_plugin.js +1 -0
  149. package/dist/web/runner/in_memory_runner.js +13 -2
  150. package/dist/web/runner/runner.js +111 -75
  151. package/dist/web/sessions/base_session_service.js +6 -1
  152. package/dist/web/sessions/database_session_service.js +24 -7
  153. package/dist/web/sessions/vertex_ai_session_service.js +17 -3
  154. package/dist/web/skills/loader.js +2 -2
  155. package/dist/web/tools/base_toolset.js +1 -1
  156. package/dist/web/tools/consolidate_context_tool.js +51 -0
  157. package/dist/web/tools/forwarding_artifact_service.js +16 -28
  158. package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +2 -12
  159. package/dist/web/utils/async_hooks_shim.js +24 -0
  160. package/dist/web/utils/client_labels.js +38 -2
  161. package/dist/web/utils/env_aware_utils.js +10 -1
  162. package/dist/web/utils/streaming_utils.js +10 -4
  163. package/dist/web/utils/task.js +34 -0
  164. package/dist/web/version.js +1 -1
  165. package/package.json +2 -2
@@ -19,6 +19,7 @@ import { experimental } from "../utils/experimental.js";
19
19
  var ContextCompactionTrigger = /* @__PURE__ */ ((ContextCompactionTrigger2) => {
20
20
  ContextCompactionTrigger2["Manual"] = "Manual";
21
21
  ContextCompactionTrigger2["Auto"] = "Auto";
22
+ ContextCompactionTrigger2["AgentControlled"] = "AgentControlled";
22
23
  return ContextCompactionTrigger2;
23
24
  })(ContextCompactionTrigger || {});
24
25
  class BasePlugin {
@@ -14,19 +14,30 @@ class InMemoryRunner extends Runner {
14
14
  * Creates a new InMemoryRunner instance.
15
15
  *
16
16
  * @param params The configuration for the runner.
17
+ * @param params.app An optional application instance to run.
17
18
  * @param params.agent The root agent to run.
18
19
  * @param params.appName The application name. Defaults to `'InMemoryRunner'`.
19
20
  * @param params.plugins An optional list of plugins.
21
+ * @param params.resumabilityConfig An optional resumability configuration.
20
22
  */
21
23
  constructor(params) {
22
- const { agent, appName = "InMemoryRunner", plugins = [] } = params;
24
+ var _a;
25
+ const {
26
+ agent,
27
+ appName = "InMemoryRunner",
28
+ plugins = [],
29
+ app,
30
+ resumabilityConfig
31
+ } = params;
23
32
  super({
33
+ app,
24
34
  appName,
25
35
  agent,
26
36
  plugins,
27
37
  artifactService: new InMemoryArtifactService(),
28
38
  sessionService: new InMemorySessionService(),
29
- memoryService: new InMemoryMemoryService()
39
+ memoryService: new InMemoryMemoryService(),
40
+ resumabilityConfig: (_a = app == null ? void 0 : app.resumabilityConfig) != null ? _a : resumabilityConfig
30
41
  });
31
42
  }
32
43
  }
@@ -1,9 +1,28 @@
1
1
  import {createRequire as topLevelCreateRequire} from 'module';
2
2
  const require = topLevelCreateRequire(import.meta.url);
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
3
9
  var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
4
10
  var __typeError = (msg) => {
5
11
  throw TypeError(msg);
6
12
  };
13
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
+ var __spreadValues = (a, b) => {
15
+ for (var prop in b || (b = {}))
16
+ if (__hasOwnProp.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ if (__getOwnPropSymbols)
19
+ for (var prop of __getOwnPropSymbols(b)) {
20
+ if (__propIsEnum.call(b, prop))
21
+ __defNormalProp(a, prop, b[prop]);
22
+ }
23
+ return a;
24
+ };
25
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
7
26
  var __await = function(promise, isYieldStar) {
8
27
  this[0] = promise;
9
28
  this[1] = isYieldStar;
@@ -56,17 +75,19 @@ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")])
56
75
  var _a;
57
76
  import { createPartFromText } from "@google/genai";
58
77
  import { context, trace } from "@opentelemetry/api";
78
+ import { findMatchingFunctionCall } from "../agents/functions.js";
59
79
  import {
60
80
  InvocationContext,
61
81
  newInvocationContextId
62
82
  } from "../agents/invocation_context.js";
63
83
  import { isLlmAgent } from "../agents/llm_agent.js";
64
84
  import { createRunConfig } from "../agents/run_config.js";
85
+ import { ScopedArtifactService } from "../artifacts/scoped_artifact_service.js";
65
86
  import {
66
87
  BuiltInCodeExecutor,
67
88
  isBuiltInCodeExecutor
68
89
  } from "../code_executors/built_in_code_executor.js";
69
- import { createEvent, getFunctionCalls } from "../events/event.js";
90
+ import { createEvent } from "../events/event.js";
70
91
  import { createEventActions } from "../events/event_actions.js";
71
92
  import { PluginManager } from "../plugins/plugin_manager.js";
72
93
  import {
@@ -89,14 +110,24 @@ class Runner {
89
110
  */
90
111
  constructor(input) {
91
112
  this[_a] = true;
92
- var _a2;
93
- this.appName = input.appName;
94
- this.agent = input.agent;
95
- this.pluginManager = new PluginManager((_a2 = input.plugins) != null ? _a2 : []);
113
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
114
+ const appName = (_b = (_a2 = input.app) == null ? void 0 : _a2.name) != null ? _b : input.appName;
115
+ const agent = (_d = (_c = input.app) == null ? void 0 : _c.rootAgent) != null ? _d : input.agent;
116
+ if (!agent) {
117
+ throw new Error(
118
+ "agent must be provided in runner constructor (or via app.rootAgent)"
119
+ );
120
+ }
121
+ this.appName = appName;
122
+ this.agent = agent;
123
+ const appPlugins = (_f = (_e = input.app) == null ? void 0 : _e.plugins) != null ? _f : [];
124
+ const configPlugins = (_g = input.plugins) != null ? _g : [];
125
+ this.pluginManager = new PluginManager([...appPlugins, ...configPlugins]);
96
126
  this.artifactService = input.artifactService;
97
127
  this.sessionService = input.sessionService;
98
128
  this.memoryService = input.memoryService;
99
129
  this.credentialService = input.credentialService;
130
+ this.resumabilityConfig = (_i = (_h = input.app) == null ? void 0 : _h.resumabilityConfig) != null ? _i : input.resumabilityConfig;
100
131
  }
101
132
  /**
102
133
  * Runs the agent with a new, ephemeral session.
@@ -149,6 +180,9 @@ class Runner {
149
180
  const { userId, sessionId, stateDelta } = params;
150
181
  const runConfig = createRunConfig(params.runConfig);
151
182
  let newMessage = params.newMessage;
183
+ if (newMessage && !newMessage.role) {
184
+ newMessage.role = "user";
185
+ }
152
186
  const span = tracer.startSpan("invocation");
153
187
  const ctx = trace.setSpan(context.active(), span);
154
188
  try {
@@ -169,7 +203,7 @@ class Runner {
169
203
  if (!session) {
170
204
  if (!this.appName) {
171
205
  throw new Error(
172
- "Session lookup failed: appName must be provided in runner constructor"
206
+ "Session lookup failed: appName must be provided in runner constructor (or via app.name)"
173
207
  );
174
208
  }
175
209
  throw new Error("Session not found: ".concat(sessionId));
@@ -186,7 +220,12 @@ class Runner {
186
220
  }
187
221
  }
188
222
  const invocationContext = new InvocationContext({
189
- artifactService: this.artifactService,
223
+ artifactService: this.artifactService ? new ScopedArtifactService(
224
+ this.artifactService,
225
+ this.appName,
226
+ userId,
227
+ sessionId
228
+ ) : void 0,
190
229
  sessionService: this.sessionService,
191
230
  memoryService: this.memoryService,
192
231
  credentialService: this.credentialService,
@@ -327,19 +366,21 @@ class Runner {
327
366
  if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
328
367
  return;
329
368
  }
369
+ const sessionKey = {
370
+ appName: this.appName,
371
+ userId,
372
+ sessionId
373
+ };
330
374
  for (let i = 0; i < message.parts.length; i++) {
331
375
  const part = message.parts[i];
332
376
  if (!part.inlineData) {
333
377
  continue;
334
378
  }
335
379
  const fileName = "artifact_".concat(invocationId, "_").concat(i);
336
- await this.artifactService.saveArtifact({
337
- appName: this.appName,
338
- userId,
339
- sessionId,
380
+ await this.artifactService.saveArtifact(__spreadProps(__spreadValues({}, sessionKey), {
340
381
  filename: fileName,
341
382
  artifact: part
342
- });
383
+ }));
343
384
  message.parts[i] = createPartFromText(
344
385
  "Uploaded file: ".concat(fileName, ". It is saved into artifacts")
345
386
  );
@@ -349,86 +390,78 @@ class Runner {
349
390
  * Determines the next agent to run to continue the session. This is primarily
350
391
  * used for session resumption.
351
392
  */
352
- // TODO - b/425992518: This is where LRO integration should happen.
353
- // Needs clean up before we can generalize it.
393
+ /**
394
+ * Determines the next agent to run to continue the session. This is primarily
395
+ * used for session resumption across tool and LRO boundaries.
396
+ */
354
397
  determineAgentForResumption(session, rootAgent) {
355
- const event = findEventByLastFunctionResponseId(session.events);
356
- if (event && event.author) {
357
- return rootAgent.findAgent(event.author) || rootAgent;
358
- }
359
- for (let i = session.events.length - 1; i >= 0; i--) {
360
- logger.info("event:", JSON.stringify(session.events[i]));
361
- const event2 = session.events[i];
362
- if (event2.author === "user" || !event2.author) {
363
- continue;
364
- }
365
- if (event2.author === rootAgent.name) {
366
- return rootAgent;
367
- }
368
- const agent = rootAgent.findSubAgent(event2.author);
369
- if (!agent) {
370
- logger.warn(
371
- "Event from an unknown agent: ".concat(event2.author, ", event id: ").concat(event2.id)
372
- );
373
- continue;
374
- }
375
- if (this.isRoutableLlmAgent(agent)) {
376
- return agent;
377
- }
378
- }
379
- return rootAgent;
398
+ return determineAgentForResumption(
399
+ session,
400
+ rootAgent,
401
+ this.resumabilityConfig
402
+ );
380
403
  }
381
404
  /**
382
405
  * Whether the agent to run can transfer to any other agent in the agent tree.
383
406
  *
384
- * An agent is transferable if:
385
- * - It is an instance of `LlmAgent`.
386
- * - All its ancestors are also transferable (i.e., they have
387
- * `disallowTransferToParent` set to false).
388
- *
389
407
  * @param agentToRun The agent to check for transferability.
390
408
  * @returns True if the agent can transfer, False otherwise.
391
409
  */
392
410
  isRoutableLlmAgent(agentToRun) {
393
- let agent = agentToRun;
394
- while (agent) {
395
- if (!isLlmAgent(agent)) {
396
- return false;
397
- }
398
- if (agent.disallowTransferToParent) {
399
- return false;
400
- }
401
- agent = agent.parentAgent;
402
- }
403
- return true;
411
+ return isRoutableLlmAgent(agentToRun);
404
412
  }
405
413
  // TODO - b/425992518: Implement runLive and related methods.
406
414
  }
407
- function findEventByLastFunctionResponseId(events) {
408
- var _a2, _b, _c, _d;
409
- if (!events.length) {
410
- return null;
411
- }
412
- const lastEvent = events[events.length - 1];
413
- const functionCallId = (_d = (_c = (_b = (_a2 = lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b.find(
414
- (part) => part.functionResponse
415
- )) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
416
- if (!functionCallId) {
417
- return null;
415
+ function determineAgentForResumption(session, rootAgent, resumabilityConfig) {
416
+ const event = findEventByLastFunctionResponseId(session.events);
417
+ const isResumable = Boolean(resumabilityConfig == null ? void 0 : resumabilityConfig.isResumable);
418
+ if (event && event.author && isResumable) {
419
+ const resumedAgent = rootAgent.findAgent(event.author);
420
+ if (resumedAgent) {
421
+ return resumedAgent;
422
+ }
423
+ logger.warn(
424
+ "Function response from an unknown agent: ".concat(event.author, ", event id: ").concat(event.id)
425
+ );
418
426
  }
419
- for (let i = events.length - 2; i >= 0; i--) {
420
- const event = events[i];
421
- const functionCalls = getFunctionCalls(event);
422
- if (!functionCalls) {
427
+ for (let i = session.events.length - 1; i >= 0; i--) {
428
+ logger.debug("event:", JSON.stringify(session.events[i]));
429
+ const event2 = session.events[i];
430
+ if (event2.author === "user" || !event2.author) {
423
431
  continue;
424
432
  }
425
- for (const functionCall of functionCalls) {
426
- if (functionCall.id === functionCallId) {
427
- return event;
428
- }
433
+ if (event2.author === rootAgent.name) {
434
+ return rootAgent;
435
+ }
436
+ const agent = rootAgent.findSubAgent(event2.author);
437
+ if (!agent) {
438
+ logger.warn(
439
+ "Event from an unknown agent: ".concat(event2.author, ", event id: ").concat(event2.id)
440
+ );
441
+ continue;
442
+ }
443
+ if (isRoutableLlmAgent(agent)) {
444
+ return agent;
429
445
  }
430
446
  }
431
- return null;
447
+ return rootAgent;
448
+ }
449
+ function isRoutableLlmAgent(agentToRun) {
450
+ let agent = agentToRun;
451
+ while (agent) {
452
+ if (!isLlmAgent(agent)) {
453
+ return false;
454
+ }
455
+ if (agent.disallowTransferToParent) {
456
+ return false;
457
+ }
458
+ agent = agent.parentAgent;
459
+ }
460
+ return true;
461
+ }
462
+ function findEventByLastFunctionResponseId(events) {
463
+ var _a2;
464
+ return (_a2 = findMatchingFunctionCall(events)) != null ? _a2 : null;
432
465
  }
433
466
  function getAllToolsets(agent) {
434
467
  const toolsets = [];
@@ -452,5 +485,8 @@ function getAllToolsets(agent) {
452
485
  }
453
486
  export {
454
487
  Runner,
488
+ determineAgentForResumption,
489
+ findEventByLastFunctionResponseId,
490
+ isRoutableLlmAgent,
455
491
  isRunner
456
492
  };
@@ -40,7 +40,12 @@ class BaseSessionService {
40
40
  }
41
41
  event = trimTempDeltaState(event);
42
42
  this.updateSessionState({ session, event });
43
- session.events.push(event);
43
+ const index = session.events.findIndex((e) => e.id === event.id);
44
+ if (index >= 0) {
45
+ session.events[index] = event;
46
+ } else {
47
+ session.events.push(event);
48
+ }
44
49
  return event;
45
50
  }
46
51
  /**
@@ -389,16 +389,28 @@ class DatabaseSessionService extends BaseSessionService {
389
389
  storageSession.state = __spreadValues(__spreadValues({}, storageSession.state), sessionDelta);
390
390
  }
391
391
  }
392
- const newStorageEvent = txEm.create(StorageEvent, {
392
+ const existingStorageEvent = await txEm.findOne(StorageEvent, {
393
393
  id: trimmedEvent.id,
394
394
  appName: session.appName,
395
395
  userId: session.userId,
396
- sessionId: session.id,
397
- invocationId: trimmedEvent.invocationId,
398
- timestamp: new Date(trimmedEvent.timestamp),
399
- eventData: trimmedEvent
396
+ sessionId: session.id
400
397
  });
401
- txEm.persist(newStorageEvent);
398
+ if (existingStorageEvent) {
399
+ existingStorageEvent.eventData = trimmedEvent;
400
+ existingStorageEvent.timestamp = new Date(trimmedEvent.timestamp);
401
+ txEm.persist(existingStorageEvent);
402
+ } else {
403
+ const newStorageEvent = txEm.create(StorageEvent, {
404
+ id: trimmedEvent.id,
405
+ appName: session.appName,
406
+ userId: session.userId,
407
+ sessionId: session.id,
408
+ invocationId: trimmedEvent.invocationId,
409
+ timestamp: new Date(trimmedEvent.timestamp),
410
+ eventData: trimmedEvent
411
+ });
412
+ txEm.persist(newStorageEvent);
413
+ }
402
414
  await txEm.commit();
403
415
  storageSession.updateTime = new Date(event.timestamp);
404
416
  const newMergedState = mergeStates(
@@ -407,7 +419,12 @@ class DatabaseSessionService extends BaseSessionService {
407
419
  storageSession.state
408
420
  );
409
421
  session.state = newMergedState;
410
- session.events.push(event);
422
+ const index = session.events.findIndex((e) => e.id === event.id);
423
+ if (index >= 0) {
424
+ session.events[index] = event;
425
+ } else {
426
+ session.events.push(event);
427
+ }
411
428
  session.lastUpdateTime = storageSession.updateTime.getTime();
412
429
  });
413
430
  return event;
@@ -50,6 +50,10 @@ const HTTP_NOT_FOUND = 404;
50
50
  function isVertexAiConnectionString(uri) {
51
51
  return (uri == null ? void 0 : uri.startsWith("vertexai://")) || false;
52
52
  }
53
+ function quoteFilterLiteral(value) {
54
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
55
+ return '"'.concat(escaped, '"');
56
+ }
53
57
  let VertexAiSessionService = class extends BaseSessionService {
54
58
  constructor(options) {
55
59
  super();
@@ -213,7 +217,7 @@ let VertexAiSessionService = class extends BaseSessionService {
213
217
  do {
214
218
  const response = await this.sessions.listInternal({
215
219
  name: "reasoningEngines/".concat(reasoningEngineId),
216
- config: __spreadValues(__spreadValues({}, userId ? { filter: 'user_id="'.concat(userId, '"') } : {}), pageToken ? { pageToken } : {})
220
+ config: __spreadValues(__spreadValues({}, userId ? { filter: "user_id=".concat(quoteFilterLiteral(userId)) } : {}), pageToken ? { pageToken } : {})
217
221
  });
218
222
  const sessions = response.sessions || [];
219
223
  for (const sessionObj of sessions) {
@@ -272,10 +276,19 @@ let VertexAiSessionService = class extends BaseSessionService {
272
276
  }
273
277
  async deleteSession({
274
278
  appName,
275
- userId: _userId,
279
+ userId,
276
280
  sessionId
277
281
  }) {
278
282
  const reasoningEngineId = this.getReasoningEngineId(appName);
283
+ const session = await this.getSession({
284
+ appName,
285
+ userId,
286
+ sessionId,
287
+ config: { numRecentEvents: 0 }
288
+ });
289
+ if (!session) {
290
+ return;
291
+ }
279
292
  await this.sessions.delete({
280
293
  name: "reasoningEngines/".concat(reasoningEngineId, "/sessions/").concat(sessionId)
281
294
  });
@@ -412,5 +425,6 @@ function _fromApiEvent(apiEventObj) {
412
425
  }
413
426
  export {
414
427
  VertexAiSessionService,
415
- isVertexAiConnectionString
428
+ isVertexAiConnectionString,
429
+ quoteFilterLiteral
416
430
  };
@@ -103,12 +103,12 @@ function parseSkillMdContent(content) {
103
103
  if (!content.startsWith("---")) {
104
104
  throw new Error("SKILL.md must start with YAML frontmatter (---)");
105
105
  }
106
- const parts = content.split("---", 3);
106
+ const parts = content.split("---");
107
107
  if (parts.length < 3) {
108
108
  throw new Error("SKILL.md frontmatter not properly closed with ---");
109
109
  }
110
110
  const frontmatterStr = parts[1];
111
- const body = parts[2].trim();
111
+ const body = parts.filter((_, i) => i >= 2).join("---").trim();
112
112
  try {
113
113
  const parsed = yaml.load(frontmatterStr);
114
114
  if (typeof parsed !== "object" || parsed === null) {
@@ -25,7 +25,7 @@ class BaseToolset {
25
25
  * @return Whether the tool should be exposed to LLM.
26
26
  */
27
27
  isToolSelected(tool, context) {
28
- if (!this.toolFilter) {
28
+ if (!this.toolFilter || Array.isArray(this.toolFilter) && this.toolFilter.length === 0) {
29
29
  return true;
30
30
  }
31
31
  if (typeof this.toolFilter === "function") {
@@ -0,0 +1,51 @@
1
+ import {createRequire as topLevelCreateRequire} from 'module';
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ import { Type } from "@google/genai";
9
+ import { BaseTool } from "./base_tool.js";
10
+ class ConsolidateContextTool extends BaseTool {
11
+ constructor() {
12
+ super({
13
+ name: "consolidate_context",
14
+ description: "Requests context consolidation (compaction) to manage history size. Use this when a subtask is complete and you want to summarize progress and clear detailed history."
15
+ });
16
+ }
17
+ _getDeclaration() {
18
+ return {
19
+ name: this.name,
20
+ description: this.description,
21
+ parameters: {
22
+ type: Type.OBJECT,
23
+ properties: {
24
+ detail: {
25
+ type: Type.STRING,
26
+ description: "Optional description of what has been accomplished so far. This detail will be used to guide the summarization of the history."
27
+ }
28
+ }
29
+ }
30
+ };
31
+ }
32
+ async runAsync({
33
+ args,
34
+ toolContext
35
+ }) {
36
+ toolContext.state.set("temp:consolidate_context", true);
37
+ if (args["detail"] !== void 0) {
38
+ toolContext.state.set(
39
+ "temp:consolidate_context_detail",
40
+ args["detail"]
41
+ );
42
+ }
43
+ return {
44
+ status: "success",
45
+ message: "Context consolidation requested."
46
+ };
47
+ }
48
+ }
49
+ export {
50
+ ConsolidateContextTool
51
+ };
@@ -10,48 +10,36 @@ class ForwardingArtifactService {
10
10
  this.toolContext = toolContext;
11
11
  this.invocationContext = toolContext.invocationContext;
12
12
  }
13
- // TODO - b/425992518: Remove unnecessary parameters. We should rethink the
14
- // abstraction layer to make it more clear.
15
13
  async saveArtifact(request) {
16
14
  return this.toolContext.saveArtifact(request.filename, request.artifact);
17
15
  }
18
16
  async loadArtifact(request) {
19
17
  return this.toolContext.loadArtifact(request.filename, request.version);
20
18
  }
21
- async listArtifactKeys() {
19
+ async listArtifactKeys(_request) {
22
20
  return this.toolContext.listArtifacts();
23
21
  }
24
- async deleteArtifact(request) {
25
- if (!this.toolContext.invocationContext.artifactService) {
22
+ getArtifactService() {
23
+ const service = this.invocationContext.artifactService;
24
+ if (!service) {
26
25
  throw new Error("Artifact service is not initialized.");
27
26
  }
28
- return this.toolContext.invocationContext.artifactService.deleteArtifact(
29
- request
30
- );
27
+ return service;
28
+ }
29
+ async deleteArtifact(request) {
30
+ return this.getArtifactService().deleteArtifact(request.filename);
31
31
  }
32
32
  async listVersions(request) {
33
- if (!this.toolContext.invocationContext.artifactService) {
34
- throw new Error("Artifact service is not initialized.");
35
- }
36
- return this.toolContext.invocationContext.artifactService.listVersions(
37
- request
38
- );
33
+ return this.getArtifactService().listVersions(request.filename);
39
34
  }
40
- listArtifactVersions(request) {
41
- if (!this.toolContext.invocationContext.artifactService) {
42
- throw new Error("Artifact service is not initialized.");
43
- }
44
- return this.toolContext.invocationContext.artifactService.listArtifactVersions(
45
- request
46
- );
35
+ async listArtifactVersions(request) {
36
+ return this.getArtifactService().listArtifactVersions(request.filename);
47
37
  }
48
- getArtifactVersion(request) {
49
- if (!this.toolContext.invocationContext.artifactService) {
50
- throw new Error("Artifact service is not initialized.");
51
- }
52
- return this.toolContext.invocationContext.artifactService.getArtifactVersion(
53
- request
54
- );
38
+ async getArtifactVersion(request) {
39
+ return this.getArtifactService().getArtifactVersion({
40
+ filename: request.filename,
41
+ version: request.version
42
+ });
55
43
  }
56
44
  }
57
45
  export {
@@ -27,20 +27,10 @@ class ToolContextCredentialStore {
27
27
  }
28
28
  getCredential(authScheme) {
29
29
  const key = this.getCredentialKey(authScheme);
30
- const state = this.context.state;
31
- if (state) {
32
- const serialized = state[key];
33
- if (serialized) {
34
- return serialized;
35
- }
36
- }
37
- return void 0;
30
+ return this.context.state.get(key);
38
31
  }
39
32
  storeCredential(key, credential) {
40
- const state = this.context.state;
41
- if (state) {
42
- state[key] = credential;
43
- }
33
+ this.context.state.set(key, credential);
44
34
  }
45
35
  }
46
36
  let ToolAuthHandler = class {
@@ -0,0 +1,24 @@
1
+ import {createRequire as topLevelCreateRequire} from 'module';
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ class AsyncLocalStorage {
9
+ run(store, callback) {
10
+ const previous = this.store;
11
+ this.store = store;
12
+ try {
13
+ return callback();
14
+ } finally {
15
+ this.store = previous;
16
+ }
17
+ }
18
+ getStore() {
19
+ return this.store;
20
+ }
21
+ }
22
+ export {
23
+ AsyncLocalStorage
24
+ };