@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
@@ -45,6 +45,7 @@ var import_experimental = require("../utils/experimental.js");
45
45
  var ContextCompactionTrigger = /* @__PURE__ */ ((ContextCompactionTrigger2) => {
46
46
  ContextCompactionTrigger2["Manual"] = "Manual";
47
47
  ContextCompactionTrigger2["Auto"] = "Auto";
48
+ ContextCompactionTrigger2["AgentControlled"] = "AgentControlled";
48
49
  return ContextCompactionTrigger2;
49
50
  })(ContextCompactionTrigger || {});
50
51
  class BasePlugin {
@@ -41,19 +41,30 @@ class InMemoryRunner extends import_runner.Runner {
41
41
  * Creates a new InMemoryRunner instance.
42
42
  *
43
43
  * @param params The configuration for the runner.
44
+ * @param params.app An optional application instance to run.
44
45
  * @param params.agent The root agent to run.
45
46
  * @param params.appName The application name. Defaults to `'InMemoryRunner'`.
46
47
  * @param params.plugins An optional list of plugins.
48
+ * @param params.resumabilityConfig An optional resumability configuration.
47
49
  */
48
50
  constructor(params) {
49
- const { agent, appName = "InMemoryRunner", plugins = [] } = params;
51
+ var _a;
52
+ const {
53
+ agent,
54
+ appName = "InMemoryRunner",
55
+ plugins = [],
56
+ app,
57
+ resumabilityConfig
58
+ } = params;
50
59
  super({
60
+ app,
51
61
  appName,
52
62
  agent,
53
63
  plugins,
54
64
  artifactService: new import_in_memory_artifact_service.InMemoryArtifactService(),
55
65
  sessionService: new import_in_memory_session_service.InMemorySessionService(),
56
- memoryService: new import_in_memory_memory_service.InMemoryMemoryService()
66
+ memoryService: new import_in_memory_memory_service.InMemoryMemoryService(),
67
+ resumabilityConfig: (_a = app == null ? void 0 : app.resumabilityConfig) != null ? _a : resumabilityConfig
57
68
  });
58
69
  }
59
70
  }
@@ -25,14 +25,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  var runner_exports = {};
26
26
  __export(runner_exports, {
27
27
  Runner: () => Runner,
28
+ determineAgentForResumption: () => determineAgentForResumption,
29
+ findEventByLastFunctionResponseId: () => findEventByLastFunctionResponseId,
30
+ isRoutableLlmAgent: () => isRoutableLlmAgent,
28
31
  isRunner: () => isRunner
29
32
  });
30
33
  module.exports = __toCommonJS(runner_exports);
31
34
  var import_genai = require("@google/genai");
32
35
  var import_api = require("@opentelemetry/api");
36
+ var import_functions = require("../agents/functions.js");
33
37
  var import_invocation_context = require("../agents/invocation_context.js");
34
38
  var import_llm_agent = require("../agents/llm_agent.js");
35
39
  var import_run_config = require("../agents/run_config.js");
40
+ var import_scoped_artifact_service = require("../artifacts/scoped_artifact_service.js");
36
41
  var import_built_in_code_executor = require("../code_executors/built_in_code_executor.js");
37
42
  var import_event = require("../events/event.js");
38
43
  var import_event_actions = require("../events/event_actions.js");
@@ -60,14 +65,24 @@ class Runner {
60
65
  */
61
66
  constructor(input) {
62
67
  this[_a] = true;
63
- var _a2;
64
- this.appName = input.appName;
65
- this.agent = input.agent;
66
- this.pluginManager = new import_plugin_manager.PluginManager((_a2 = input.plugins) != null ? _a2 : []);
68
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
69
+ const appName = (_b = (_a2 = input.app) == null ? void 0 : _a2.name) != null ? _b : input.appName;
70
+ const agent = (_d = (_c = input.app) == null ? void 0 : _c.rootAgent) != null ? _d : input.agent;
71
+ if (!agent) {
72
+ throw new Error(
73
+ "agent must be provided in runner constructor (or via app.rootAgent)"
74
+ );
75
+ }
76
+ this.appName = appName;
77
+ this.agent = agent;
78
+ const appPlugins = (_f = (_e = input.app) == null ? void 0 : _e.plugins) != null ? _f : [];
79
+ const configPlugins = (_g = input.plugins) != null ? _g : [];
80
+ this.pluginManager = new import_plugin_manager.PluginManager([...appPlugins, ...configPlugins]);
67
81
  this.artifactService = input.artifactService;
68
82
  this.sessionService = input.sessionService;
69
83
  this.memoryService = input.memoryService;
70
84
  this.credentialService = input.credentialService;
85
+ this.resumabilityConfig = (_i = (_h = input.app) == null ? void 0 : _h.resumabilityConfig) != null ? _i : input.resumabilityConfig;
71
86
  }
72
87
  /**
73
88
  * Runs the agent with a new, ephemeral session.
@@ -117,6 +132,9 @@ class Runner {
117
132
  const { userId, sessionId, stateDelta } = params;
118
133
  const runConfig = (0, import_run_config.createRunConfig)(params.runConfig);
119
134
  let newMessage = params.newMessage;
135
+ if (newMessage && !newMessage.role) {
136
+ newMessage.role = "user";
137
+ }
120
138
  const span = import_tracing.tracer.startSpan("invocation");
121
139
  const ctx = import_api.trace.setSpan(import_api.context.active(), span);
122
140
  try {
@@ -136,7 +154,7 @@ class Runner {
136
154
  if (!session) {
137
155
  if (!this.appName) {
138
156
  throw new Error(
139
- `Session lookup failed: appName must be provided in runner constructor`
157
+ `Session lookup failed: appName must be provided in runner constructor (or via app.name)`
140
158
  );
141
159
  }
142
160
  throw new Error(`Session not found: ${sessionId}`);
@@ -153,7 +171,12 @@ class Runner {
153
171
  }
154
172
  }
155
173
  const invocationContext = new import_invocation_context.InvocationContext({
156
- artifactService: this.artifactService,
174
+ artifactService: this.artifactService ? new import_scoped_artifact_service.ScopedArtifactService(
175
+ this.artifactService,
176
+ this.appName,
177
+ userId,
178
+ sessionId
179
+ ) : void 0,
157
180
  sessionService: this.sessionService,
158
181
  memoryService: this.memoryService,
159
182
  credentialService: this.credentialService,
@@ -280,6 +303,11 @@ class Runner {
280
303
  if (!this.artifactService || !((_a2 = message.parts) == null ? void 0 : _a2.length)) {
281
304
  return;
282
305
  }
306
+ const sessionKey = {
307
+ appName: this.appName,
308
+ userId,
309
+ sessionId
310
+ };
283
311
  for (let i = 0; i < message.parts.length; i++) {
284
312
  const part = message.parts[i];
285
313
  if (!part.inlineData) {
@@ -287,9 +315,7 @@ class Runner {
287
315
  }
288
316
  const fileName = `artifact_${invocationId}_${i}`;
289
317
  await this.artifactService.saveArtifact({
290
- appName: this.appName,
291
- userId,
292
- sessionId,
318
+ ...sessionKey,
293
319
  filename: fileName,
294
320
  artifact: part
295
321
  });
@@ -302,86 +328,78 @@ class Runner {
302
328
  * Determines the next agent to run to continue the session. This is primarily
303
329
  * used for session resumption.
304
330
  */
305
- // TODO - b/425992518: This is where LRO integration should happen.
306
- // Needs clean up before we can generalize it.
331
+ /**
332
+ * Determines the next agent to run to continue the session. This is primarily
333
+ * used for session resumption across tool and LRO boundaries.
334
+ */
307
335
  determineAgentForResumption(session, rootAgent) {
308
- const event = findEventByLastFunctionResponseId(session.events);
309
- if (event && event.author) {
310
- return rootAgent.findAgent(event.author) || rootAgent;
311
- }
312
- for (let i = session.events.length - 1; i >= 0; i--) {
313
- import_logger.logger.info("event:", JSON.stringify(session.events[i]));
314
- const event2 = session.events[i];
315
- if (event2.author === "user" || !event2.author) {
316
- continue;
317
- }
318
- if (event2.author === rootAgent.name) {
319
- return rootAgent;
320
- }
321
- const agent = rootAgent.findSubAgent(event2.author);
322
- if (!agent) {
323
- import_logger.logger.warn(
324
- `Event from an unknown agent: ${event2.author}, event id: ${event2.id}`
325
- );
326
- continue;
327
- }
328
- if (this.isRoutableLlmAgent(agent)) {
329
- return agent;
330
- }
331
- }
332
- return rootAgent;
336
+ return determineAgentForResumption(
337
+ session,
338
+ rootAgent,
339
+ this.resumabilityConfig
340
+ );
333
341
  }
334
342
  /**
335
343
  * Whether the agent to run can transfer to any other agent in the agent tree.
336
344
  *
337
- * An agent is transferable if:
338
- * - It is an instance of `LlmAgent`.
339
- * - All its ancestors are also transferable (i.e., they have
340
- * `disallowTransferToParent` set to false).
341
- *
342
345
  * @param agentToRun The agent to check for transferability.
343
346
  * @returns True if the agent can transfer, False otherwise.
344
347
  */
345
348
  isRoutableLlmAgent(agentToRun) {
346
- let agent = agentToRun;
347
- while (agent) {
348
- if (!(0, import_llm_agent.isLlmAgent)(agent)) {
349
- return false;
350
- }
351
- if (agent.disallowTransferToParent) {
352
- return false;
353
- }
354
- agent = agent.parentAgent;
355
- }
356
- return true;
349
+ return isRoutableLlmAgent(agentToRun);
357
350
  }
358
351
  // TODO - b/425992518: Implement runLive and related methods.
359
352
  }
360
- function findEventByLastFunctionResponseId(events) {
361
- var _a2, _b, _c, _d;
362
- if (!events.length) {
363
- return null;
364
- }
365
- const lastEvent = events[events.length - 1];
366
- const functionCallId = (_d = (_c = (_b = (_a2 = lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b.find(
367
- (part) => part.functionResponse
368
- )) == null ? void 0 : _c.functionResponse) == null ? void 0 : _d.id;
369
- if (!functionCallId) {
370
- return null;
353
+ function determineAgentForResumption(session, rootAgent, resumabilityConfig) {
354
+ const event = findEventByLastFunctionResponseId(session.events);
355
+ const isResumable = Boolean(resumabilityConfig == null ? void 0 : resumabilityConfig.isResumable);
356
+ if (event && event.author && isResumable) {
357
+ const resumedAgent = rootAgent.findAgent(event.author);
358
+ if (resumedAgent) {
359
+ return resumedAgent;
360
+ }
361
+ import_logger.logger.warn(
362
+ `Function response from an unknown agent: ${event.author}, event id: ${event.id}`
363
+ );
371
364
  }
372
- for (let i = events.length - 2; i >= 0; i--) {
373
- const event = events[i];
374
- const functionCalls = (0, import_event.getFunctionCalls)(event);
375
- if (!functionCalls) {
365
+ for (let i = session.events.length - 1; i >= 0; i--) {
366
+ import_logger.logger.debug("event:", JSON.stringify(session.events[i]));
367
+ const event2 = session.events[i];
368
+ if (event2.author === "user" || !event2.author) {
376
369
  continue;
377
370
  }
378
- for (const functionCall of functionCalls) {
379
- if (functionCall.id === functionCallId) {
380
- return event;
381
- }
371
+ if (event2.author === rootAgent.name) {
372
+ return rootAgent;
373
+ }
374
+ const agent = rootAgent.findSubAgent(event2.author);
375
+ if (!agent) {
376
+ import_logger.logger.warn(
377
+ `Event from an unknown agent: ${event2.author}, event id: ${event2.id}`
378
+ );
379
+ continue;
380
+ }
381
+ if (isRoutableLlmAgent(agent)) {
382
+ return agent;
382
383
  }
383
384
  }
384
- return null;
385
+ return rootAgent;
386
+ }
387
+ function isRoutableLlmAgent(agentToRun) {
388
+ let agent = agentToRun;
389
+ while (agent) {
390
+ if (!(0, import_llm_agent.isLlmAgent)(agent)) {
391
+ return false;
392
+ }
393
+ if (agent.disallowTransferToParent) {
394
+ return false;
395
+ }
396
+ agent = agent.parentAgent;
397
+ }
398
+ return true;
399
+ }
400
+ function findEventByLastFunctionResponseId(events) {
401
+ var _a2;
402
+ return (_a2 = (0, import_functions.findMatchingFunctionCall)(events)) != null ? _a2 : null;
385
403
  }
386
404
  function getAllToolsets(agent) {
387
405
  const toolsets = [];
@@ -406,5 +424,8 @@ function getAllToolsets(agent) {
406
424
  // Annotate the CommonJS export names for ESM import in node:
407
425
  0 && (module.exports = {
408
426
  Runner,
427
+ determineAgentForResumption,
428
+ findEventByLastFunctionResponseId,
429
+ isRoutableLlmAgent,
409
430
  isRunner
410
431
  });
@@ -70,7 +70,12 @@ class BaseSessionService {
70
70
  }
71
71
  event = trimTempDeltaState(event);
72
72
  this.updateSessionState({ session, event });
73
- session.events.push(event);
73
+ const index = session.events.findIndex((e) => e.id === event.id);
74
+ if (index >= 0) {
75
+ session.events[index] = event;
76
+ } else {
77
+ session.events.push(event);
78
+ }
74
79
  return event;
75
80
  }
76
81
  /**
@@ -382,16 +382,28 @@ class DatabaseSessionService extends import_base_session_service.BaseSessionServ
382
382
  storageSession.state = { ...storageSession.state, ...sessionDelta };
383
383
  }
384
384
  }
385
- const newStorageEvent = txEm.create(import_schema.StorageEvent, {
385
+ const existingStorageEvent = await txEm.findOne(import_schema.StorageEvent, {
386
386
  id: trimmedEvent.id,
387
387
  appName: session.appName,
388
388
  userId: session.userId,
389
- sessionId: session.id,
390
- invocationId: trimmedEvent.invocationId,
391
- timestamp: new Date(trimmedEvent.timestamp),
392
- eventData: trimmedEvent
389
+ sessionId: session.id
393
390
  });
394
- txEm.persist(newStorageEvent);
391
+ if (existingStorageEvent) {
392
+ existingStorageEvent.eventData = trimmedEvent;
393
+ existingStorageEvent.timestamp = new Date(trimmedEvent.timestamp);
394
+ txEm.persist(existingStorageEvent);
395
+ } else {
396
+ const newStorageEvent = txEm.create(import_schema.StorageEvent, {
397
+ id: trimmedEvent.id,
398
+ appName: session.appName,
399
+ userId: session.userId,
400
+ sessionId: session.id,
401
+ invocationId: trimmedEvent.invocationId,
402
+ timestamp: new Date(trimmedEvent.timestamp),
403
+ eventData: trimmedEvent
404
+ });
405
+ txEm.persist(newStorageEvent);
406
+ }
395
407
  await txEm.commit();
396
408
  storageSession.updateTime = new Date(event.timestamp);
397
409
  const newMergedState = (0, import_base_session_service.mergeStates)(
@@ -400,7 +412,12 @@ class DatabaseSessionService extends import_base_session_service.BaseSessionServ
400
412
  storageSession.state
401
413
  );
402
414
  session.state = newMergedState;
403
- session.events.push(event);
415
+ const index = session.events.findIndex((e) => e.id === event.id);
416
+ if (index >= 0) {
417
+ session.events[index] = event;
418
+ } else {
419
+ session.events.push(event);
420
+ }
404
421
  session.lastUpdateTime = storageSession.updateTime.getTime();
405
422
  });
406
423
  return event;
@@ -33,7 +33,8 @@ var __decorateClass = (decorators, target, key, kind) => {
33
33
  var vertex_ai_session_service_exports = {};
34
34
  __export(vertex_ai_session_service_exports, {
35
35
  VertexAiSessionService: () => VertexAiSessionService,
36
- isVertexAiConnectionString: () => isVertexAiConnectionString
36
+ isVertexAiConnectionString: () => isVertexAiConnectionString,
37
+ quoteFilterLiteral: () => quoteFilterLiteral
37
38
  });
38
39
  module.exports = __toCommonJS(vertex_ai_session_service_exports);
39
40
  var import_client = require("@google-cloud/vertexai/build/src/genai/client.js");
@@ -55,6 +56,10 @@ const HTTP_NOT_FOUND = 404;
55
56
  function isVertexAiConnectionString(uri) {
56
57
  return (uri == null ? void 0 : uri.startsWith("vertexai://")) || false;
57
58
  }
59
+ function quoteFilterLiteral(value) {
60
+ const escaped = value.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
61
+ return `"${escaped}"`;
62
+ }
58
63
  let VertexAiSessionService = class extends import_base_session_service.BaseSessionService {
59
64
  constructor(options) {
60
65
  super();
@@ -222,7 +227,7 @@ let VertexAiSessionService = class extends import_base_session_service.BaseSessi
222
227
  const response = await this.sessions.listInternal({
223
228
  name: `reasoningEngines/${reasoningEngineId}`,
224
229
  config: {
225
- ...userId ? { filter: `user_id="${userId}"` } : {},
230
+ ...userId ? { filter: `user_id=${quoteFilterLiteral(userId)}` } : {},
226
231
  ...pageToken ? { pageToken } : {}
227
232
  }
228
233
  });
@@ -283,10 +288,19 @@ let VertexAiSessionService = class extends import_base_session_service.BaseSessi
283
288
  }
284
289
  async deleteSession({
285
290
  appName,
286
- userId: _userId,
291
+ userId,
287
292
  sessionId
288
293
  }) {
289
294
  const reasoningEngineId = this.getReasoningEngineId(appName);
295
+ const session = await this.getSession({
296
+ appName,
297
+ userId,
298
+ sessionId,
299
+ config: { numRecentEvents: 0 }
300
+ });
301
+ if (!session) {
302
+ return;
303
+ }
290
304
  await this.sessions.delete({
291
305
  name: `reasoningEngines/${reasoningEngineId}/sessions/${sessionId}`
292
306
  });
@@ -425,5 +439,6 @@ function _fromApiEvent(apiEventObj) {
425
439
  // Annotate the CommonJS export names for ESM import in node:
426
440
  0 && (module.exports = {
427
441
  VertexAiSessionService,
428
- isVertexAiConnectionString
442
+ isVertexAiConnectionString,
443
+ quoteFilterLiteral
429
444
  });
@@ -123,12 +123,12 @@ function parseSkillMdContent(content) {
123
123
  if (!content.startsWith("---")) {
124
124
  throw new Error("SKILL.md must start with YAML frontmatter (---)");
125
125
  }
126
- const parts = content.split("---", 3);
126
+ const parts = content.split("---");
127
127
  if (parts.length < 3) {
128
128
  throw new Error("SKILL.md frontmatter not properly closed with ---");
129
129
  }
130
130
  const frontmatterStr = parts[1];
131
- const body = parts[2].trim();
131
+ const body = parts.filter((_, i) => i >= 2).join("---").trim();
132
132
  try {
133
133
  const parsed = import_js_yaml.default.load(frontmatterStr);
134
134
  if (typeof parsed !== "object" || parsed === null) {
@@ -53,7 +53,7 @@ class BaseToolset {
53
53
  * @return Whether the tool should be exposed to LLM.
54
54
  */
55
55
  isToolSelected(tool, context) {
56
- if (!this.toolFilter) {
56
+ if (!this.toolFilter || Array.isArray(this.toolFilter) && this.toolFilter.length === 0) {
57
57
  return true;
58
58
  }
59
59
  if (typeof this.toolFilter === "function") {
@@ -0,0 +1,79 @@
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 consolidate_context_tool_exports = {};
26
+ __export(consolidate_context_tool_exports, {
27
+ ConsolidateContextTool: () => ConsolidateContextTool
28
+ });
29
+ module.exports = __toCommonJS(consolidate_context_tool_exports);
30
+ var import_genai = require("@google/genai");
31
+ var import_base_tool = require("./base_tool.js");
32
+ /**
33
+ * @license
34
+ * Copyright 2026 Google LLC
35
+ * SPDX-License-Identifier: Apache-2.0
36
+ */
37
+ class ConsolidateContextTool extends import_base_tool.BaseTool {
38
+ constructor() {
39
+ super({
40
+ name: "consolidate_context",
41
+ 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."
42
+ });
43
+ }
44
+ _getDeclaration() {
45
+ return {
46
+ name: this.name,
47
+ description: this.description,
48
+ parameters: {
49
+ type: import_genai.Type.OBJECT,
50
+ properties: {
51
+ detail: {
52
+ type: import_genai.Type.STRING,
53
+ description: "Optional description of what has been accomplished so far. This detail will be used to guide the summarization of the history."
54
+ }
55
+ }
56
+ }
57
+ };
58
+ }
59
+ async runAsync({
60
+ args,
61
+ toolContext
62
+ }) {
63
+ toolContext.state.set("temp:consolidate_context", true);
64
+ if (args["detail"] !== void 0) {
65
+ toolContext.state.set(
66
+ "temp:consolidate_context_detail",
67
+ args["detail"]
68
+ );
69
+ }
70
+ return {
71
+ status: "success",
72
+ message: "Context consolidation requested."
73
+ };
74
+ }
75
+ }
76
+ // Annotate the CommonJS export names for ESM import in node:
77
+ 0 && (module.exports = {
78
+ ConsolidateContextTool
79
+ });
@@ -37,48 +37,36 @@ class ForwardingArtifactService {
37
37
  this.toolContext = toolContext;
38
38
  this.invocationContext = toolContext.invocationContext;
39
39
  }
40
- // TODO - b/425992518: Remove unnecessary parameters. We should rethink the
41
- // abstraction layer to make it more clear.
42
40
  async saveArtifact(request) {
43
41
  return this.toolContext.saveArtifact(request.filename, request.artifact);
44
42
  }
45
43
  async loadArtifact(request) {
46
44
  return this.toolContext.loadArtifact(request.filename, request.version);
47
45
  }
48
- async listArtifactKeys() {
46
+ async listArtifactKeys(_request) {
49
47
  return this.toolContext.listArtifacts();
50
48
  }
51
- async deleteArtifact(request) {
52
- if (!this.toolContext.invocationContext.artifactService) {
49
+ getArtifactService() {
50
+ const service = this.invocationContext.artifactService;
51
+ if (!service) {
53
52
  throw new Error("Artifact service is not initialized.");
54
53
  }
55
- return this.toolContext.invocationContext.artifactService.deleteArtifact(
56
- request
57
- );
54
+ return service;
55
+ }
56
+ async deleteArtifact(request) {
57
+ return this.getArtifactService().deleteArtifact(request.filename);
58
58
  }
59
59
  async listVersions(request) {
60
- if (!this.toolContext.invocationContext.artifactService) {
61
- throw new Error("Artifact service is not initialized.");
62
- }
63
- return this.toolContext.invocationContext.artifactService.listVersions(
64
- request
65
- );
60
+ return this.getArtifactService().listVersions(request.filename);
66
61
  }
67
- listArtifactVersions(request) {
68
- if (!this.toolContext.invocationContext.artifactService) {
69
- throw new Error("Artifact service is not initialized.");
70
- }
71
- return this.toolContext.invocationContext.artifactService.listArtifactVersions(
72
- request
73
- );
62
+ async listArtifactVersions(request) {
63
+ return this.getArtifactService().listArtifactVersions(request.filename);
74
64
  }
75
- getArtifactVersion(request) {
76
- if (!this.toolContext.invocationContext.artifactService) {
77
- throw new Error("Artifact service is not initialized.");
78
- }
79
- return this.toolContext.invocationContext.artifactService.getArtifactVersion(
80
- request
81
- );
65
+ async getArtifactVersion(request) {
66
+ return this.getArtifactService().getArtifactVersion({
67
+ filename: request.filename,
68
+ version: request.version
69
+ });
82
70
  }
83
71
  }
84
72
  // Annotate the CommonJS export names for ESM import in node:
@@ -52,20 +52,10 @@ class ToolContextCredentialStore {
52
52
  }
53
53
  getCredential(authScheme) {
54
54
  const key = this.getCredentialKey(authScheme);
55
- const state = this.context.state;
56
- if (state) {
57
- const serialized = state[key];
58
- if (serialized) {
59
- return serialized;
60
- }
61
- }
62
- return void 0;
55
+ return this.context.state.get(key);
63
56
  }
64
57
  storeCredential(key, credential) {
65
- const state = this.context.state;
66
- if (state) {
67
- state[key] = credential;
68
- }
58
+ this.context.state.set(key, credential);
69
59
  }
70
60
  }
71
61
  let ToolAuthHandler = class {
@@ -0,0 +1,52 @@
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 async_hooks_shim_exports = {};
26
+ __export(async_hooks_shim_exports, {
27
+ AsyncLocalStorage: () => AsyncLocalStorage
28
+ });
29
+ module.exports = __toCommonJS(async_hooks_shim_exports);
30
+ /**
31
+ * @license
32
+ * Copyright 2026 Google LLC
33
+ * SPDX-License-Identifier: Apache-2.0
34
+ */
35
+ class AsyncLocalStorage {
36
+ run(store, callback) {
37
+ const previous = this.store;
38
+ this.store = store;
39
+ try {
40
+ return callback();
41
+ } finally {
42
+ this.store = previous;
43
+ }
44
+ }
45
+ getStore() {
46
+ return this.store;
47
+ }
48
+ }
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ AsyncLocalStorage
52
+ });