@google/adk 1.3.0 → 1.5.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 (248) hide show
  1. package/dist/cjs/a2a/agent_to_a2a.js +18 -2
  2. package/dist/cjs/a2a/event_converter_utils.js +8 -4
  3. package/dist/cjs/a2a/metadata_converter_utils.js +1 -1
  4. package/dist/cjs/agents/active_streaming_tool.js +6 -1
  5. package/dist/cjs/agents/base_agent.js +39 -0
  6. package/dist/cjs/agents/context.js +1 -11
  7. package/dist/cjs/agents/functions.js +39 -35
  8. package/dist/cjs/agents/instructions.js +23 -5
  9. package/dist/cjs/agents/invocation_context.js +2 -5
  10. package/dist/cjs/agents/llm_agent.js +10 -7
  11. package/dist/cjs/agents/loop_agent.js +15 -3
  12. package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
  13. package/dist/cjs/agents/processors/content_processor_utils.js +18 -4
  14. package/dist/cjs/agents/processors/context_compactor_request_processor.js +4 -2
  15. package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
  16. package/dist/cjs/agents/routed_agent.js +31 -0
  17. package/dist/cjs/agents/run_config.js +3 -2
  18. package/dist/cjs/apps/app.js +79 -0
  19. package/dist/cjs/apps/resumability_config.js +44 -0
  20. package/dist/cjs/artifacts/file_artifact_service.js +14 -1
  21. package/dist/cjs/artifacts/gcs_artifact_service.js +57 -15
  22. package/dist/cjs/artifacts/in_memory_artifact_service.js +12 -1
  23. package/dist/cjs/artifacts/scoped_artifact_service.js +101 -0
  24. package/dist/cjs/artifacts/session_artifact_service.js +27 -0
  25. package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
  26. package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
  27. package/dist/cjs/common.js +76 -0
  28. package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
  29. package/dist/cjs/context/anchored_context_compactor.js +127 -0
  30. package/dist/cjs/context/compaction_utils.js +52 -0
  31. package/dist/cjs/context/token_based_context_compactor.js +52 -33
  32. package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
  33. package/dist/cjs/events/compacted_event.js +9 -3
  34. package/dist/cjs/events/event.js +52 -0
  35. package/dist/cjs/index.js +4 -0
  36. package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  37. package/dist/cjs/models/google_llm.js +25 -10
  38. package/dist/cjs/models/interactions_utils.js +732 -0
  39. package/dist/cjs/plugins/base_plugin.js +1 -0
  40. package/dist/cjs/plugins/global_instruction_plugin.js +97 -0
  41. package/dist/cjs/runner/in_memory_runner.js +13 -2
  42. package/dist/cjs/runner/runner.js +143 -83
  43. package/dist/cjs/sessions/base_session_service.js +6 -1
  44. package/dist/cjs/sessions/database_session_service.js +24 -7
  45. package/dist/cjs/sessions/vertex_ai_session_service.js +19 -4
  46. package/dist/cjs/skills/loader.js +2 -2
  47. package/dist/cjs/tools/base_toolset.js +1 -1
  48. package/dist/cjs/tools/consolidate_context_tool.js +79 -0
  49. package/dist/cjs/tools/enterprise_web_search_tool.js +81 -0
  50. package/dist/cjs/tools/example_tool.js +68 -0
  51. package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
  52. package/dist/cjs/tools/load_web_page.js +250 -0
  53. package/dist/cjs/tools/mcp/load_mcp_resource_tool.js +140 -0
  54. package/dist/cjs/tools/mcp/mcp_toolset.js +63 -0
  55. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  56. package/dist/cjs/tools/skill/run_skill_inline_script_tool.js +54 -4
  57. package/dist/cjs/tools/skill/skill_toolset.js +4 -2
  58. package/dist/cjs/utils/async_hooks_shim.js +52 -0
  59. package/dist/cjs/utils/branch_trie.js +41 -0
  60. package/dist/cjs/utils/client_labels.js +41 -3
  61. package/dist/cjs/utils/env_aware_utils.js +10 -1
  62. package/dist/cjs/utils/streaming_utils.js +15 -9
  63. package/dist/cjs/utils/task.js +62 -0
  64. package/dist/cjs/version.js +1 -1
  65. package/dist/esm/a2a/agent_to_a2a.js +18 -2
  66. package/dist/esm/a2a/event_converter_utils.js +8 -4
  67. package/dist/esm/a2a/metadata_converter_utils.js +1 -1
  68. package/dist/esm/agents/active_streaming_tool.js +6 -1
  69. package/dist/esm/agents/base_agent.js +39 -0
  70. package/dist/esm/agents/context.js +1 -11
  71. package/dist/esm/agents/functions.js +43 -32
  72. package/dist/esm/agents/instructions.js +23 -5
  73. package/dist/esm/agents/invocation_context.js +2 -5
  74. package/dist/esm/agents/llm_agent.js +12 -9
  75. package/dist/esm/agents/loop_agent.js +15 -3
  76. package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
  77. package/dist/esm/agents/processors/content_processor_utils.js +16 -3
  78. package/dist/esm/agents/processors/context_compactor_request_processor.js +4 -2
  79. package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
  80. package/dist/esm/agents/routed_agent.js +31 -0
  81. package/dist/esm/agents/run_config.js +3 -2
  82. package/dist/esm/apps/app.js +49 -0
  83. package/dist/esm/apps/resumability_config.js +16 -0
  84. package/dist/esm/artifacts/file_artifact_service.js +14 -1
  85. package/dist/esm/artifacts/gcs_artifact_service.js +57 -15
  86. package/dist/esm/artifacts/in_memory_artifact_service.js +12 -1
  87. package/dist/esm/artifacts/scoped_artifact_service.js +73 -0
  88. package/dist/esm/artifacts/session_artifact_service.js +7 -0
  89. package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
  90. package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
  91. package/dist/esm/common.js +67 -3
  92. package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
  93. package/dist/esm/context/anchored_context_compactor.js +99 -0
  94. package/dist/esm/context/compaction_utils.js +27 -0
  95. package/dist/esm/context/token_based_context_compactor.js +52 -33
  96. package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
  97. package/dist/esm/events/compacted_event.js +7 -2
  98. package/dist/esm/events/event.js +46 -0
  99. package/dist/esm/index.js +6 -1
  100. package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  101. package/dist/esm/models/google_llm.js +23 -9
  102. package/dist/esm/models/interactions_utils.js +697 -0
  103. package/dist/esm/plugins/base_plugin.js +1 -0
  104. package/dist/esm/plugins/global_instruction_plugin.js +69 -0
  105. package/dist/esm/runner/in_memory_runner.js +13 -2
  106. package/dist/esm/runner/runner.js +141 -84
  107. package/dist/esm/sessions/base_session_service.js +6 -1
  108. package/dist/esm/sessions/database_session_service.js +24 -7
  109. package/dist/esm/sessions/vertex_ai_session_service.js +17 -3
  110. package/dist/esm/skills/loader.js +2 -2
  111. package/dist/esm/tools/base_toolset.js +1 -1
  112. package/dist/esm/tools/consolidate_context_tool.js +51 -0
  113. package/dist/esm/tools/enterprise_web_search_tool.js +55 -0
  114. package/dist/esm/tools/example_tool.js +42 -0
  115. package/dist/esm/tools/forwarding_artifact_service.js +16 -28
  116. package/dist/esm/tools/load_web_page.js +221 -0
  117. package/dist/esm/tools/mcp/load_mcp_resource_tool.js +114 -0
  118. package/dist/esm/tools/mcp/mcp_toolset.js +63 -0
  119. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  120. package/dist/esm/tools/skill/run_skill_inline_script_tool.js +53 -4
  121. package/dist/esm/tools/skill/skill_toolset.js +4 -2
  122. package/dist/esm/utils/async_hooks_shim.js +24 -0
  123. package/dist/esm/utils/branch_trie.js +13 -0
  124. package/dist/esm/utils/client_labels.js +38 -2
  125. package/dist/esm/utils/env_aware_utils.js +10 -1
  126. package/dist/esm/utils/streaming_utils.js +11 -5
  127. package/dist/esm/utils/task.js +34 -0
  128. package/dist/esm/version.js +1 -1
  129. package/dist/types/a2a/a2a_remote_agent.d.ts +5 -1
  130. package/dist/types/a2a/agent_to_a2a.d.ts +38 -0
  131. package/dist/types/agents/active_streaming_tool.d.ts +3 -4
  132. package/dist/types/agents/base_agent.d.ts +29 -1
  133. package/dist/types/agents/functions.d.ts +12 -17
  134. package/dist/types/agents/invocation_context.d.ts +7 -3
  135. package/dist/types/agents/llm_agent.d.ts +5 -1
  136. package/dist/types/agents/loop_agent.d.ts +2 -2
  137. package/dist/types/agents/processors/content_processor_utils.d.ts +8 -0
  138. package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
  139. package/dist/types/agents/routed_agent.d.ts +24 -1
  140. package/dist/types/agents/run_config.d.ts +1 -1
  141. package/dist/types/apps/app.d.ts +52 -0
  142. package/dist/types/apps/resumability_config.d.ts +27 -0
  143. package/dist/types/artifacts/base_artifact_service.d.ts +6 -36
  144. package/dist/types/artifacts/gcs_artifact_service.d.ts +2 -1
  145. package/dist/types/artifacts/scoped_artifact_service.d.ts +25 -0
  146. package/dist/types/artifacts/session_artifact_service.d.ts +25 -0
  147. package/dist/types/auth/oauth2/oauth2_discovery.d.ts +11 -0
  148. package/dist/types/common.d.ts +29 -5
  149. package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
  150. package/dist/types/context/anchored_context_compactor.d.ts +35 -0
  151. package/dist/types/context/base_context_compactor.d.ts +3 -0
  152. package/dist/types/context/compaction_utils.d.ts +15 -0
  153. package/dist/types/context/token_based_context_compactor.d.ts +6 -1
  154. package/dist/types/context/trajectory_thought_pruning_compactor.d.ts +27 -0
  155. package/dist/types/events/compacted_event.d.ts +10 -0
  156. package/dist/types/events/event.d.ts +21 -0
  157. package/dist/types/index.d.ts +3 -2
  158. package/dist/types/models/google_llm.d.ts +15 -1
  159. package/dist/types/models/interactions_utils.d.ts +92 -0
  160. package/dist/types/models/llm_request.d.ts +4 -0
  161. package/dist/types/models/llm_response.d.ts +4 -0
  162. package/dist/types/plugins/base_plugin.d.ts +2 -1
  163. package/dist/types/plugins/global_instruction_plugin.d.ts +38 -0
  164. package/dist/types/runner/in_memory_runner.d.ts +7 -1
  165. package/dist/types/runner/runner.d.ts +44 -10
  166. package/dist/types/sessions/base_session_service.d.ts +3 -16
  167. package/dist/types/sessions/session.d.ts +11 -0
  168. package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
  169. package/dist/types/tools/consolidate_context_tool.d.ts +19 -0
  170. package/dist/types/tools/enterprise_web_search_tool.d.ts +26 -0
  171. package/dist/types/tools/example_tool.d.ts +21 -0
  172. package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
  173. package/dist/types/tools/load_web_page.d.ts +33 -0
  174. package/dist/types/tools/mcp/load_mcp_resource_tool.d.ts +26 -0
  175. package/dist/types/tools/mcp/mcp_toolset.d.ts +27 -0
  176. package/dist/types/tools/skill/run_skill_inline_script_tool.d.ts +25 -0
  177. package/dist/types/tools/skill/skill_toolset.d.ts +9 -0
  178. package/dist/types/utils/async_hooks_shim.d.ts +10 -0
  179. package/dist/types/utils/branch_trie.d.ts +14 -0
  180. package/dist/types/utils/client_labels.d.ts +10 -0
  181. package/dist/types/utils/env_aware_utils.d.ts +3 -3
  182. package/dist/types/utils/task.d.ts +24 -0
  183. package/dist/types/version.d.ts +1 -1
  184. package/dist/web/a2a/agent_to_a2a.js +18 -2
  185. package/dist/web/a2a/event_converter_utils.js +8 -4
  186. package/dist/web/a2a/metadata_converter_utils.js +1 -1
  187. package/dist/web/agents/active_streaming_tool.js +6 -1
  188. package/dist/web/agents/base_agent.js +39 -0
  189. package/dist/web/agents/context.js +1 -11
  190. package/dist/web/agents/functions.js +43 -32
  191. package/dist/web/agents/instructions.js +23 -5
  192. package/dist/web/agents/invocation_context.js +2 -5
  193. package/dist/web/agents/llm_agent.js +12 -9
  194. package/dist/web/agents/loop_agent.js +27 -3
  195. package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
  196. package/dist/web/agents/processors/content_processor_utils.js +16 -3
  197. package/dist/web/agents/processors/context_compactor_request_processor.js +4 -2
  198. package/dist/web/agents/processors/interactions_request_processor.js +52 -0
  199. package/dist/web/agents/routed_agent.js +31 -0
  200. package/dist/web/agents/run_config.js +8 -3
  201. package/dist/web/apps/app.js +49 -0
  202. package/dist/web/apps/resumability_config.js +31 -0
  203. package/dist/web/artifacts/file_artifact_service.js +14 -1
  204. package/dist/web/artifacts/gcs_artifact_service.js +55 -15
  205. package/dist/web/artifacts/in_memory_artifact_service.js +12 -1
  206. package/dist/web/artifacts/scoped_artifact_service.js +86 -0
  207. package/dist/web/artifacts/session_artifact_service.js +7 -0
  208. package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
  209. package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
  210. package/dist/web/common.js +67 -3
  211. package/dist/web/context/agent_controlled_context_compactor.js +81 -0
  212. package/dist/web/context/anchored_context_compactor.js +117 -0
  213. package/dist/web/context/compaction_utils.js +27 -0
  214. package/dist/web/context/token_based_context_compactor.js +52 -33
  215. package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
  216. package/dist/web/events/compacted_event.js +7 -2
  217. package/dist/web/events/event.js +44 -0
  218. package/dist/web/index.js +6 -1
  219. package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  220. package/dist/web/models/google_llm.js +54 -9
  221. package/dist/web/models/interactions_utils.js +728 -0
  222. package/dist/web/plugins/base_plugin.js +1 -0
  223. package/dist/web/plugins/global_instruction_plugin.js +67 -0
  224. package/dist/web/runner/in_memory_runner.js +13 -2
  225. package/dist/web/runner/runner.js +157 -84
  226. package/dist/web/sessions/base_session_service.js +6 -1
  227. package/dist/web/sessions/database_session_service.js +24 -7
  228. package/dist/web/sessions/vertex_ai_session_service.js +17 -3
  229. package/dist/web/skills/loader.js +2 -2
  230. package/dist/web/tools/base_toolset.js +1 -1
  231. package/dist/web/tools/consolidate_context_tool.js +51 -0
  232. package/dist/web/tools/enterprise_web_search_tool.js +55 -0
  233. package/dist/web/tools/example_tool.js +42 -0
  234. package/dist/web/tools/forwarding_artifact_service.js +16 -28
  235. package/dist/web/tools/load_web_page.js +221 -0
  236. package/dist/web/tools/mcp/load_mcp_resource_tool.js +107 -0
  237. package/dist/web/tools/mcp/mcp_toolset.js +63 -0
  238. package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  239. package/dist/web/tools/skill/run_skill_inline_script_tool.js +51 -4
  240. package/dist/web/tools/skill/skill_toolset.js +4 -2
  241. package/dist/web/utils/async_hooks_shim.js +24 -0
  242. package/dist/web/utils/branch_trie.js +13 -0
  243. package/dist/web/utils/client_labels.js +38 -2
  244. package/dist/web/utils/env_aware_utils.js +10 -1
  245. package/dist/web/utils/streaming_utils.js +11 -5
  246. package/dist/web/utils/task.js +34 -0
  247. package/dist/web/version.js +1 -1
  248. package/package.json +2 -2
@@ -26,6 +26,7 @@ class BaseAgent {
26
26
  * A unique symbol to identify ADK agent classes.
27
27
  */
28
28
  this[_a] = true;
29
+ this.config = { ...config };
29
30
  this.name = validateAgentName(config.name);
30
31
  this.description = config.description;
31
32
  this.parentAgent = config.parentAgent;
@@ -43,6 +44,44 @@ class BaseAgent {
43
44
  get rootAgent() {
44
45
  return getRootAgent(this);
45
46
  }
47
+ /**
48
+ * Creates a copy of this agent with the given config fields overridden.
49
+ *
50
+ * Mirrors adk-python's `BaseAgent.clone(update=...)`. The clone is a detached
51
+ * root: its `parentAgent` is always `undefined`. Sub-agents are recursively
52
+ * cloned (and re-parented to the clone) unless `subAgents` is overridden.
53
+ * Rebuilding via the concrete constructor re-derives all state, so a cloned
54
+ * `LlmAgent` gets a fresh `requestProcessors` array rather than sharing the
55
+ * original's. See google/adk-js#534.
56
+ *
57
+ * @param overrides Config fields to override on the clone. Overriding
58
+ * `parentAgent` is rejected, matching adk-python.
59
+ * @returns A new detached agent instance of the same concrete class.
60
+ */
61
+ clone(overrides) {
62
+ if (overrides && "parentAgent" in overrides) {
63
+ throw new Error(
64
+ "Cannot update `parentAgent` field in clone. Parent agent is set only when the parent agent is instantiated with the sub-agents."
65
+ );
66
+ }
67
+ const merged = { ...this.config, ...overrides };
68
+ merged.parentAgent = void 0;
69
+ const mergedRecord = merged;
70
+ for (const key of Object.keys(mergedRecord)) {
71
+ if (key === "subAgents" || overrides && key in overrides) {
72
+ continue;
73
+ }
74
+ const value = mergedRecord[key];
75
+ if (Array.isArray(value)) {
76
+ mergedRecord[key] = value.slice();
77
+ }
78
+ }
79
+ if (!overrides || !("subAgents" in overrides)) {
80
+ merged.subAgents = this.subAgents.map((subAgent) => subAgent.clone());
81
+ }
82
+ const ctor = this.constructor;
83
+ return new ctor(merged);
84
+ }
46
85
  /**
47
86
  * Entry method to run an agent via text-based conversation.
48
87
  *
@@ -56,9 +56,6 @@ class Context extends ReadonlyContext {
56
56
  throw new Error("Artifact service is not initialized.");
57
57
  }
58
58
  return this.invocationContext.artifactService.loadArtifact({
59
- appName: this.invocationContext.appName,
60
- userId: this.invocationContext.userId,
61
- sessionId: this.invocationContext.session.id,
62
59
  filename,
63
60
  version
64
61
  });
@@ -75,9 +72,6 @@ class Context extends ReadonlyContext {
75
72
  throw new Error("Artifact service is not initialized.");
76
73
  }
77
74
  const version = await this.invocationContext.artifactService.saveArtifact({
78
- appName: this.invocationContext.appName,
79
- userId: this.invocationContext.userId,
80
- sessionId: this.invocationContext.session.id,
81
75
  filename,
82
76
  artifact
83
77
  });
@@ -110,11 +104,7 @@ class Context extends ReadonlyContext {
110
104
  if (!this.invocationContext.artifactService) {
111
105
  throw new Error("Artifact service is not initialized.");
112
106
  }
113
- return this.invocationContext.artifactService.listArtifactKeys({
114
- appName: this.invocationContext.session.appName,
115
- userId: this.invocationContext.session.userId,
116
- sessionId: this.invocationContext.session.id
117
- });
107
+ return this.invocationContext.artifactService.listArtifactKeys();
118
108
  }
119
109
  /**
120
110
  * Searches the memory of the current user.
@@ -7,9 +7,13 @@ const require = topLevelCreateRequire(import.meta.url);
7
7
  */
8
8
  import { createUserContent } from "@google/genai";
9
9
  import { isEmpty } from "lodash-es";
10
- import { createEvent, getFunctionCalls } from "../events/event.js";
10
+ import {
11
+ createEvent,
12
+ generateClientFunctionCallId,
13
+ getFunctionCalls,
14
+ getFunctionResponses
15
+ } from "../events/event.js";
11
16
  import { mergeEventActions } from "../events/event_actions.js";
12
- import { randomUUID } from "../utils/env_aware_utils.js";
13
17
  import { logger } from "../utils/logger.js";
14
18
  import { Context } from "./context.js";
15
19
  import {
@@ -17,7 +21,11 @@ import {
17
21
  tracer,
18
22
  traceToolCall
19
23
  } from "../telemetry/tracing.js";
20
- const AF_FUNCTION_CALL_ID_PREFIX = "adk-";
24
+ import {
25
+ AF_FUNCTION_CALL_ID_PREFIX,
26
+ generateClientFunctionCallId as generateClientFunctionCallId2,
27
+ populateClientFunctionCallId
28
+ } from "../events/event.js";
21
29
  const REQUEST_EUC_FUNCTION_CALL_NAME = "adk_request_credential";
22
30
  const REQUEST_CONFIRMATION_FUNCTION_CALL_NAME = "adk_request_confirmation";
23
31
  const functionsExportedForTestingOnly = {
@@ -25,32 +33,6 @@ const functionsExportedForTestingOnly = {
25
33
  generateAuthEvent,
26
34
  generateRequestConfirmationEvent
27
35
  };
28
- function generateClientFunctionCallId() {
29
- return `${AF_FUNCTION_CALL_ID_PREFIX}${randomUUID()}`;
30
- }
31
- function populateClientFunctionCallId(modelResponseEvent) {
32
- const functionCalls = getFunctionCalls(modelResponseEvent);
33
- if (!functionCalls) {
34
- return;
35
- }
36
- for (const functionCall of functionCalls) {
37
- if (!functionCall.id) {
38
- functionCall.id = generateClientFunctionCallId();
39
- }
40
- }
41
- }
42
- function removeClientFunctionCallId(content) {
43
- if (content && content.parts) {
44
- for (const part of content.parts) {
45
- if (part.functionCall && part.functionCall.id && part.functionCall.id.startsWith(AF_FUNCTION_CALL_ID_PREFIX)) {
46
- part.functionCall.id = void 0;
47
- }
48
- if (part.functionResponse && part.functionResponse.id && part.functionResponse.id.startsWith(AF_FUNCTION_CALL_ID_PREFIX)) {
49
- part.functionResponse.id = void 0;
50
- }
51
- }
52
- }
53
- }
54
36
  function getLongRunningFunctionCalls(functionCalls, toolsDict) {
55
37
  const longRunningToolIds = /* @__PURE__ */ new Set();
56
38
  for (const functionCall of functionCalls) {
@@ -393,17 +375,46 @@ function mergeParallelFunctionResponseEvents(functionResponseEvents) {
393
375
  timestamp: baseEvent.timestamp
394
376
  });
395
377
  }
378
+ function findEventByFunctionCallId(events, functionCallId, endIndex = events.length) {
379
+ for (let i = endIndex - 1; i >= 0; i--) {
380
+ const event = events[i];
381
+ const functionCalls = getFunctionCalls(event);
382
+ for (const functionCall of functionCalls) {
383
+ if (functionCall.id === functionCallId) {
384
+ return event;
385
+ }
386
+ }
387
+ }
388
+ return void 0;
389
+ }
390
+ function findMatchingFunctionCall(events) {
391
+ if (!events.length) {
392
+ return void 0;
393
+ }
394
+ const lastEvent = events[events.length - 1];
395
+ const functionResponses = getFunctionResponses(lastEvent);
396
+ if (!functionResponses.length || !functionResponses[0].id) {
397
+ return void 0;
398
+ }
399
+ return findEventByFunctionCallId(
400
+ events,
401
+ functionResponses[0].id,
402
+ events.length - 1
403
+ );
404
+ }
396
405
  export {
406
+ AF_FUNCTION_CALL_ID_PREFIX,
397
407
  REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
398
408
  REQUEST_EUC_FUNCTION_CALL_NAME,
409
+ findEventByFunctionCallId,
410
+ findMatchingFunctionCall,
399
411
  functionsExportedForTestingOnly,
400
412
  generateAuthEvent,
401
- generateClientFunctionCallId,
413
+ generateClientFunctionCallId2 as generateClientFunctionCallId,
402
414
  generateRequestConfirmationEvent,
403
415
  getLongRunningFunctionCalls,
404
416
  handleFunctionCallList,
405
417
  handleFunctionCallsAsync,
406
418
  mergeParallelFunctionResponseEvents,
407
- populateClientFunctionCallId,
408
- removeClientFunctionCallId
419
+ populateClientFunctionCallId
409
420
  };
@@ -15,9 +15,6 @@ async function resolveKey(key, isOptional, rawMatch, readonlyContext) {
15
15
  throw new Error("Artifact service is not initialized.");
16
16
  }
17
17
  const artifact = await invocationContext.artifactService.loadArtifact({
18
- appName: invocationContext.session.appName,
19
- userId: invocationContext.session.userId,
20
- sessionId: invocationContext.session.id,
21
18
  filename: fileName
22
19
  });
23
20
  if (!artifact) {
@@ -26,19 +23,40 @@ async function resolveKey(key, isOptional, rawMatch, readonlyContext) {
26
23
  }
27
24
  throw new Error(`Artifact ${fileName} not found.`);
28
25
  }
29
- return String(artifact);
26
+ return formatValue(artifact, true);
30
27
  }
31
28
  if (!isValidStateName(key)) {
32
29
  return rawMatch;
33
30
  }
34
31
  if (key in invocationContext.session.state) {
35
- return String(invocationContext.session.state[key]);
32
+ return formatValue(invocationContext.session.state[key], false);
36
33
  }
37
34
  if (isOptional) {
38
35
  return "";
39
36
  }
40
37
  throw new Error(`Context variable not found: \`${key}\`.`);
41
38
  }
39
+ function formatValue(value, isArtifact = false) {
40
+ if (typeof value === "string") {
41
+ return value;
42
+ }
43
+ if (typeof value === "object" && value !== null) {
44
+ if (isArtifact && "text" in value && typeof value.text === "string") {
45
+ return value.text;
46
+ }
47
+ try {
48
+ const json = JSON.stringify(value);
49
+ if (json !== void 0) {
50
+ return json;
51
+ }
52
+ } catch (e) {
53
+ throw new Error(
54
+ `Failed to serialize value for instruction template: ${e instanceof Error ? e.message : String(e)}`
55
+ );
56
+ }
57
+ }
58
+ return String(value);
59
+ }
42
60
  async function injectSessionState(template, readonlyContext) {
43
61
  const pattern = /\{+[^{}]*}+/g;
44
62
  const matches = Array.from(template.matchAll(pattern));
@@ -30,11 +30,7 @@ class InvocationContext {
30
30
  * @param params The parameters for creating an invocation context.
31
31
  */
32
32
  constructor(params) {
33
- /**
34
- * A container to keep track of different kinds of costs incurred as a part of
35
- * this invocation.
36
- */
37
- this.invocationCostManager = new InvocationCostManager();
33
+ var _a;
38
34
  this.artifactService = params.artifactService;
39
35
  this.sessionService = params.sessionService;
40
36
  this.memoryService = params.memoryService;
@@ -49,6 +45,7 @@ class InvocationContext {
49
45
  this.activeStreamingTools = params.activeStreamingTools;
50
46
  this.pluginManager = params.pluginManager;
51
47
  this.abortSignal = params.abortSignal;
48
+ this.invocationCostManager = (_a = params.invocationCostManager) != null ? _a : new InvocationCostManager();
52
49
  }
53
50
  /**
54
51
  * The app name of the current session.
@@ -13,7 +13,8 @@ import {
13
13
  createNewEventId,
14
14
  getFunctionCalls,
15
15
  getFunctionResponses,
16
- isFinalResponse
16
+ isFinalResponse,
17
+ populateClientFunctionCallId
17
18
  } from "../events/event.js";
18
19
  import { isBaseLlm } from "../models/base_llm.js";
19
20
  import { LLMRegistry } from "../models/registry.js";
@@ -31,8 +32,7 @@ import {
31
32
  generateAuthEvent,
32
33
  generateRequestConfirmationEvent,
33
34
  getLongRunningFunctionCalls,
34
- handleFunctionCallsAsync,
35
- populateClientFunctionCallId
35
+ handleFunctionCallsAsync
36
36
  } from "./functions.js";
37
37
  import { AUTH_PREPROCESSOR } from "../auth/auth_preprocessor.js";
38
38
  import { AGENT_TRANSFER_LLM_REQUEST_PROCESSOR } from "./processors/agent_transfer_llm_request_processor.js";
@@ -42,6 +42,7 @@ import { CONTENT_REQUEST_PROCESSOR } from "./processors/content_request_processo
42
42
  import { ContextCompactorRequestProcessor } from "./processors/context_compactor_request_processor.js";
43
43
  import { IDENTITY_LLM_REQUEST_PROCESSOR } from "./processors/identity_llm_request_processor.js";
44
44
  import { INSTRUCTIONS_LLM_REQUEST_PROCESSOR } from "./processors/instructions_llm_request_processor.js";
45
+ import { INTERACTIONS_REQUEST_PROCESSOR } from "./processors/interactions_request_processor.js";
45
46
  import { REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR } from "./processors/request_confirmation_llm_request_processor.js";
46
47
  import { TOOL_FILTER_REQUEST_PROCESSOR } from "./processors/tool_filter_request_processor.js";
47
48
  import { ReadonlyContext } from "./readonly_context.js";
@@ -86,6 +87,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
86
87
  INSTRUCTIONS_LLM_REQUEST_PROCESSOR,
87
88
  REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
88
89
  CONTENT_REQUEST_PROCESSOR,
90
+ INTERACTIONS_REQUEST_PROCESSOR,
89
91
  CODE_EXECUTION_REQUEST_PROCESSOR,
90
92
  TOOL_FILTER_REQUEST_PROCESSOR
91
93
  ];
@@ -181,6 +183,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
181
183
  * This method is only for use by Agent Development Kit.
182
184
  * @param context The context to retrieve the session state.
183
185
  * @returns The resolved globalInstruction field.
186
+ * @deprecated Use GlobalInstructionPlugin instead.
184
187
  */
185
188
  async canonicalGlobalInstruction(context2) {
186
189
  if (typeof this.globalInstruction === "string") {
@@ -468,7 +471,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
468
471
  span.end();
469
472
  }
470
473
  async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
471
- var _a2, _b2, _c, _d, _e;
474
+ var _a2, _b2, _c, _d, _e, _f;
472
475
  for (const processor of this.responseProcessors) {
473
476
  for await (const event of processor.runAsync(
474
477
  invocationContext,
@@ -480,7 +483,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
480
483
  yield event;
481
484
  }
482
485
  }
483
- if (!llmResponse.content && !llmResponse.errorCode && !llmResponse.interrupted) {
486
+ if ((!llmResponse.content || ((_b2 = llmResponse.content.parts) == null ? void 0 : _b2.length) === 0) && !llmResponse.errorCode && !llmResponse.interrupted) {
484
487
  return;
485
488
  }
486
489
  const mergedEvent = createEvent({
@@ -504,10 +507,10 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
504
507
  }
505
508
  }
506
509
  yield mergedEvent;
507
- if (!((_b2 = getFunctionCalls(mergedEvent)) == null ? void 0 : _b2.length)) {
510
+ if (!((_c = getFunctionCalls(mergedEvent)) == null ? void 0 : _c.length)) {
508
511
  return;
509
512
  }
510
- if ((_c = invocationContext.runConfig) == null ? void 0 : _c.pauseOnToolCalls) {
513
+ if ((_d = invocationContext.runConfig) == null ? void 0 : _d.pauseOnToolCalls) {
511
514
  invocationContext.endInvocation = true;
512
515
  return;
513
516
  }
@@ -518,7 +521,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
518
521
  beforeToolCallbacks: this.canonicalBeforeToolCallbacks,
519
522
  afterToolCallbacks: this.canonicalAfterToolCallbacks
520
523
  });
521
- if (!functionResponseEvent || ((_d = invocationContext.abortSignal) == null ? void 0 : _d.aborted)) {
524
+ if (!functionResponseEvent || ((_e = invocationContext.abortSignal) == null ? void 0 : _e.aborted)) {
522
525
  return;
523
526
  }
524
527
  const authEvent = generateAuthEvent(
@@ -543,7 +546,7 @@ class LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNATURE_SYMBOL, _b) {
543
546
  if (nextAgentName) {
544
547
  const nextAgent = this.getAgentByName(invocationContext, nextAgentName);
545
548
  for await (const event of nextAgent.runAsync(invocationContext)) {
546
- if ((_e = invocationContext.abortSignal) == null ? void 0 : _e.aborted) {
549
+ if ((_f = invocationContext.abortSignal) == null ? void 0 : _f.aborted) {
547
550
  return;
548
551
  }
549
552
  yield event;
@@ -44,9 +44,21 @@ class LoopAgent extends (_b = BaseAgent, _a = LOOP_AGENT_SIGNATURE_SYMBOL, _b) {
44
44
  }
45
45
  return;
46
46
  }
47
- // eslint-disable-next-line require-yield
48
- async *runLiveImpl(_context) {
49
- throw new Error("This is not supported yet for LoopAgent.");
47
+ async *runLiveImpl(context) {
48
+ var _a2, _b2;
49
+ for (let iteration = 0; iteration < this.maxIterations; iteration++) {
50
+ for (const subAgent of this.subAgents) {
51
+ for await (const event of subAgent.runLive(context)) {
52
+ if ((_a2 = context.abortSignal) == null ? void 0 : _a2.aborted) {
53
+ return;
54
+ }
55
+ yield event;
56
+ if ((_b2 = event.actions) == null ? void 0 : _b2.escalate) {
57
+ return;
58
+ }
59
+ }
60
+ }
61
+ }
50
62
  }
51
63
  }
52
64
  export {
@@ -317,9 +317,6 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
317
317
  }
318
318
  for (const outputFile of codeExecutionResult.outputFiles) {
319
319
  const version = await invocationContext.artifactService.saveArtifact({
320
- appName: invocationContext.appName || "",
321
- userId: invocationContext.userId || "",
322
- sessionId: invocationContext.session.id,
323
320
  filename: outputFile.name,
324
321
  artifact: {
325
322
  inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
@@ -15,11 +15,23 @@ import {
15
15
  getFunctionCalls,
16
16
  getFunctionResponses
17
17
  } from "../../events/event.js";
18
+ import { isSegmentPrefix } from "../../utils/branch_trie.js";
18
19
  import {
19
- removeClientFunctionCallId,
20
+ AF_FUNCTION_CALL_ID_PREFIX,
20
21
  REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
21
22
  REQUEST_EUC_FUNCTION_CALL_NAME
22
23
  } from "../functions.js";
24
+ function removeClientFunctionCallId(content) {
25
+ var _a, _b, _c, _d, _e;
26
+ for (const part of (_a = content == null ? void 0 : content.parts) != null ? _a : []) {
27
+ if ((_c = (_b = part.functionCall) == null ? void 0 : _b.id) == null ? void 0 : _c.startsWith(AF_FUNCTION_CALL_ID_PREFIX)) {
28
+ part.functionCall.id = void 0;
29
+ }
30
+ if ((_e = (_d = part.functionResponse) == null ? void 0 : _d.id) == null ? void 0 : _e.startsWith(AF_FUNCTION_CALL_ID_PREFIX)) {
31
+ part.functionResponse.id = void 0;
32
+ }
33
+ }
34
+ }
23
35
  function getContents(events, agentName, currentBranch) {
24
36
  var _a, _b, _c;
25
37
  const filteredEvents = [];
@@ -31,7 +43,7 @@ function getContents(events, agentName, currentBranch) {
31
43
  if (!((_a = event.content) == null ? void 0 : _a.role) || ((_c = (_b = event.content.parts) == null ? void 0 : _b[0]) == null ? void 0 : _c.text) === "") {
32
44
  continue;
33
45
  }
34
- if (currentBranch && event.branch && !currentBranch.startsWith(event.branch)) {
46
+ if (currentBranch && event.branch && !isSegmentPrefix(currentBranch, event.branch)) {
35
47
  continue;
36
48
  }
37
49
  if (isAuthEvent(event)) {
@@ -327,5 +339,6 @@ ${event.compactedContent}`
327
339
  export {
328
340
  getContents,
329
341
  getCurrentTurnContents,
330
- mergeFunctionResponseEvents
342
+ mergeFunctionResponseEvents,
343
+ removeClientFunctionCallId
331
344
  };
@@ -23,20 +23,22 @@ class ContextCompactorRequestProcessor {
23
23
  * @param _llmRequest - Unused; present to satisfy the {@link BaseLlmRequestProcessor} interface.
24
24
  */
25
25
  async *runAsync(invocationContext, _llmRequest) {
26
+ var _a;
26
27
  for (const compactor of this.compactors) {
27
28
  const shouldCompact = await Promise.resolve(
28
29
  compactor.shouldCompact(invocationContext)
29
30
  );
30
31
  if (shouldCompact) {
32
+ const trigger = (_a = compactor.trigger) != null ? _a : ContextCompactionTrigger.Auto;
31
33
  await invocationContext.pluginManager.runBeforeContextCompaction({
32
34
  invocationContext,
33
- trigger: ContextCompactionTrigger.Auto
35
+ trigger
34
36
  });
35
37
  const oldEvents = new Set(invocationContext.session.events);
36
38
  await Promise.resolve(compactor.compact(invocationContext));
37
39
  await invocationContext.pluginManager.runAfterContextCompaction({
38
40
  invocationContext,
39
- trigger: ContextCompactionTrigger.Auto
41
+ trigger
40
42
  });
41
43
  const newEvents = invocationContext.session.events.filter(
42
44
  (e) => !oldEvents.has(e)
@@ -0,0 +1,34 @@
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 { isGemini } from "../../models/google_llm.js";
9
+ import { isLlmAgent } from "../llm_agent.js";
10
+ class InteractionsRequestProcessor {
11
+ // eslint-disable-next-line require-yield
12
+ async *runAsync(invocationContext, llmRequest) {
13
+ const agent = invocationContext.agent;
14
+ if (!agent || !isLlmAgent(agent)) {
15
+ return;
16
+ }
17
+ const model = agent.canonicalModel;
18
+ if (isGemini(model) && model.useInteractionsApi) {
19
+ const events = invocationContext.session.events;
20
+ for (let i = events.length - 1; i >= 0; i--) {
21
+ const event = events[i];
22
+ if (event.branch === invocationContext.branch && event.author === agent.name && event.interactionId) {
23
+ llmRequest.previousInteractionId = event.interactionId;
24
+ break;
25
+ }
26
+ }
27
+ }
28
+ }
29
+ }
30
+ const INTERACTIONS_REQUEST_PROCESSOR = new InteractionsRequestProcessor();
31
+ export {
32
+ INTERACTIONS_REQUEST_PROCESSOR,
33
+ InteractionsRequestProcessor
34
+ };
@@ -38,6 +38,29 @@ let RoutedAgent = class extends (_b = BaseAgent, _a = ROUTED_AGENT_SIGNATURE_SYM
38
38
  }
39
39
  this.router = config.router;
40
40
  }
41
+ /**
42
+ * Creates a detached copy of this routed agent.
43
+ *
44
+ * The inherited {@link BaseAgent.clone} only deep-clones `subAgents`, but a
45
+ * `RoutedAgent` derives its routing targets from `config.agents`. Rebuilding
46
+ * through the base clone alone would re-read the already-parented original
47
+ * agents and throw "already has a parent agent". This override deep-clones the
48
+ * routing targets (unless the caller overrides `agents`) so the rebuilt
49
+ * constructor re-parents fresh, detached copies. The array-vs-record shape
50
+ * and, for records, the keys are preserved so the router keeps selecting the
51
+ * same targets.
52
+ *
53
+ * @param overrides Config fields to override on the clone. Overriding
54
+ * `parentAgent` is rejected by the base implementation.
55
+ * @returns A new detached `RoutedAgent` of the same concrete class.
56
+ */
57
+ clone(overrides) {
58
+ const nextOverrides = { ...overrides };
59
+ if (!("agents" in nextOverrides)) {
60
+ nextOverrides.agents = cloneRoutingTargets(this.config.agents);
61
+ }
62
+ return super.clone(nextOverrides);
63
+ }
41
64
  /**
42
65
  * Runs the selected agent via text-based conversation.
43
66
  */
@@ -64,6 +87,14 @@ let RoutedAgent = class extends (_b = BaseAgent, _a = ROUTED_AGENT_SIGNATURE_SYM
64
87
  RoutedAgent = __decorateClass([
65
88
  experimental
66
89
  ], RoutedAgent);
90
+ function cloneRoutingTargets(agents) {
91
+ if (Array.isArray(agents)) {
92
+ return agents.map((agent) => agent.clone());
93
+ }
94
+ return Object.fromEntries(
95
+ Object.entries(agents).map(([key, agent]) => [key, agent.clone()])
96
+ );
97
+ }
67
98
  export {
68
99
  RoutedAgent,
69
100
  isRoutedAgent
@@ -13,14 +13,15 @@ var StreamingMode = /* @__PURE__ */ ((StreamingMode2) => {
13
13
  return StreamingMode2;
14
14
  })(StreamingMode || {});
15
15
  function createRunConfig(params = {}) {
16
+ var _a;
16
17
  return {
17
18
  saveInputBlobsAsArtifacts: false,
18
19
  supportCfc: false,
19
20
  enableAffectiveDialog: false,
20
21
  streamingMode: "none" /* NONE */,
21
- maxLlmCalls: validateMaxLlmCalls(params.maxLlmCalls || 500),
22
22
  pauseOnToolCalls: false,
23
- ...params
23
+ ...params,
24
+ maxLlmCalls: validateMaxLlmCalls((_a = params.maxLlmCalls) != null ? _a : 500)
24
25
  };
25
26
  }
26
27
  function validateMaxLlmCalls(value) {
@@ -0,0 +1,49 @@
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
+ var _a;
9
+ import { isBaseAgent } from "../agents/base_agent.js";
10
+ const VALID_APP_NAME_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
11
+ function validateAppName(name) {
12
+ if (!VALID_APP_NAME_RE.test(name)) {
13
+ throw new Error(
14
+ `Invalid app name '${name}': must start with a letter and can only consist of letters, digits, underscores, and hyphens.`
15
+ );
16
+ }
17
+ if (name === "user") {
18
+ throw new Error("App name cannot be 'user'; reserved for end-user input.");
19
+ }
20
+ }
21
+ const APP_SIGNATURE_SYMBOL = Symbol.for("google.adk.app");
22
+ function isApp(obj) {
23
+ return typeof obj === "object" && obj !== null && APP_SIGNATURE_SYMBOL in obj && obj[APP_SIGNATURE_SYMBOL] === true;
24
+ }
25
+ _a = APP_SIGNATURE_SYMBOL;
26
+ class App {
27
+ constructor(options) {
28
+ this[_a] = true;
29
+ var _a2, _b, _c, _d;
30
+ validateAppName(options.name);
31
+ if (options.rootAgent === void 0 || options.rootAgent === null) {
32
+ throw new Error("rootAgent must be provided.");
33
+ }
34
+ if (!isBaseAgent(options.rootAgent)) {
35
+ throw new TypeError(
36
+ `rootAgent must be a BaseAgent instance, got ${(_c = (_b = (_a2 = options.rootAgent) == null ? void 0 : _a2.constructor) == null ? void 0 : _b.name) != null ? _c : typeof options.rootAgent}`
37
+ );
38
+ }
39
+ this.name = options.name;
40
+ this.rootAgent = options.rootAgent;
41
+ this.plugins = (_d = options.plugins) != null ? _d : [];
42
+ this.resumabilityConfig = options.resumabilityConfig;
43
+ }
44
+ }
45
+ export {
46
+ App,
47
+ isApp,
48
+ validateAppName
49
+ };
@@ -0,0 +1,16 @@
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
+ function createResumabilityConfig(params = {}) {
9
+ return {
10
+ isResumable: false,
11
+ ...params
12
+ };
13
+ }
14
+ export {
15
+ createResumabilityConfig
16
+ };