@google/adk 2.2.0 → 2.3.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 (241) hide show
  1. package/dist/cjs/a2a/a2a_remote_agent.js +4 -1
  2. package/dist/cjs/a2a/agent_card.js +102 -26
  3. package/dist/cjs/a2a/agent_to_a2a.js +1 -1
  4. package/dist/cjs/a2a/event_converter_utils.js +5 -1
  5. package/dist/cjs/a2a/metadata_converter_utils.js +2 -1
  6. package/dist/cjs/agents/functions.js +199 -25
  7. package/dist/cjs/agents/instructions.js +4 -1
  8. package/dist/cjs/agents/invocation_context.js +26 -4
  9. package/dist/cjs/agents/langgraph_agent.js +152 -0
  10. package/dist/cjs/agents/llm_agent.js +175 -101
  11. package/dist/cjs/agents/processors/code_execution_request_processor.js +10 -6
  12. package/dist/cjs/agents/remote_agent.js +187 -0
  13. package/dist/cjs/artifacts/in_memory_artifact_service.js +14 -0
  14. package/dist/cjs/auth/auth_handler.js +37 -5
  15. package/dist/cjs/auth/auth_schemes.js +9 -0
  16. package/dist/cjs/cli/cli_eval.js +532 -0
  17. package/dist/cjs/cli/utils/envs.js +116 -0
  18. package/dist/cjs/cli/utils/evals.js +137 -0
  19. package/dist/cjs/cli/utils/logs.js +150 -0
  20. package/dist/cjs/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  21. package/dist/cjs/code_executors/code_execution_utils.js +48 -16
  22. package/dist/cjs/code_executors/code_executor_context.js +6 -2
  23. package/dist/cjs/common.js +90 -0
  24. package/dist/cjs/context/summarizers/llm_summarizer.js +12 -10
  25. package/dist/cjs/context/token_based_context_compactor.js +2 -1
  26. package/dist/cjs/events/abort_events.js +135 -0
  27. package/dist/cjs/events/event.js +4 -2
  28. package/dist/cjs/events/internal_metadata.js +131 -0
  29. package/dist/cjs/examples/example_util.js +23 -2
  30. package/dist/cjs/flows/llm_flows/audio_transcriber.js +170 -0
  31. package/dist/cjs/index.js +97 -1
  32. package/dist/cjs/integrations/agent_registry/helpers.js +1 -1
  33. package/dist/cjs/models/gemini_llm_connection.js +16 -8
  34. package/dist/cjs/models/google_llm.js +13 -16
  35. package/dist/cjs/models/llm_response.js +9 -1
  36. package/dist/cjs/models/registry.js +1 -1
  37. package/dist/cjs/models/routed_llm.js +9 -8
  38. package/dist/cjs/planners/plan_re_act_planner.js +1 -1
  39. package/dist/cjs/plugins/debug_logging_plugin.js +653 -0
  40. package/dist/cjs/plugins/plugin_manager.js +1 -1
  41. package/dist/cjs/plugins/tool_call_integrity_plugin.js +364 -0
  42. package/dist/cjs/runner/in_memory_runner.js +6 -11
  43. package/dist/cjs/runner/runner.js +121 -35
  44. package/dist/cjs/sessions/db/operations.js +1 -1
  45. package/dist/cjs/sessions/in_memory_session_service.js +3 -1
  46. package/dist/cjs/sessions/state.js +8 -4
  47. package/dist/cjs/sessions/vertex_ai_session_service.js +34 -15
  48. package/dist/cjs/telemetry/tracing.js +97 -66
  49. package/dist/cjs/tools/agent_tool.js +44 -0
  50. package/dist/cjs/tools/apihub_tool/apihub_toolset.js +142 -0
  51. package/dist/cjs/tools/apihub_tool/clients/apihub_client.js +272 -0
  52. package/dist/cjs/tools/apihub_tool/clients/secret_client.js +148 -0
  53. package/dist/cjs/tools/apihub_tool/clients/service_account.js +48 -0
  54. package/dist/cjs/tools/application_integration_tool/application_integration_toolset.js +185 -0
  55. package/dist/cjs/tools/application_integration_tool/clients/cloud_request.js +133 -0
  56. package/dist/cjs/tools/application_integration_tool/clients/connections_client.js +603 -0
  57. package/dist/cjs/tools/application_integration_tool/clients/integration_client.js +219 -0
  58. package/dist/cjs/tools/crewai_tool.js +132 -0
  59. package/dist/cjs/tools/example_tool.js +10 -0
  60. package/dist/cjs/tools/function_tool.js +319 -0
  61. package/dist/cjs/tools/google_api_tool/google_api_tool.js +97 -0
  62. package/dist/cjs/tools/google_api_tool/google_api_tool_set.js +226 -0
  63. package/dist/cjs/tools/google_api_tool/google_api_tool_sets.js +58 -0
  64. package/dist/cjs/tools/google_api_tool/googleapi_to_openapi_converter.js +440 -0
  65. package/dist/cjs/tools/google_api_tool/index.js +64 -0
  66. package/dist/cjs/tools/langchain_tool.js +172 -0
  67. package/dist/cjs/tools/mcp/index.js +12 -0
  68. package/dist/cjs/tools/mcp/mcp_session_manager.js +25 -0
  69. package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +1 -2
  70. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +46 -2
  71. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +2 -1
  72. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  73. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +14 -2
  74. package/dist/cjs/tools/retrieval/base_retrieval_tool.js +18 -3
  75. package/dist/cjs/tools/toolbox_tool.js +108 -0
  76. package/dist/cjs/utils/env_aware_utils.js +6 -8
  77. package/dist/cjs/utils/error_utils.js +11 -11
  78. package/dist/cjs/utils/file_extension_utils.js +11 -0
  79. package/dist/cjs/utils/file_utils.js +2 -24
  80. package/dist/cjs/utils/gemini_schema_util.js +4 -1
  81. package/dist/cjs/utils/json_utils.js +49 -0
  82. package/dist/cjs/utils/live_connection_utils.js +4 -5
  83. package/dist/cjs/utils/logger_node.js +7 -1
  84. package/dist/cjs/utils/object_notation_utils.js +6 -3
  85. package/dist/cjs/utils/streaming_utils.js +12 -12
  86. package/dist/cjs/version.js +1 -1
  87. package/dist/esm/a2a/a2a_remote_agent.js +4 -1
  88. package/dist/esm/a2a/agent_card.js +103 -27
  89. package/dist/esm/a2a/agent_to_a2a.js +5 -2
  90. package/dist/esm/a2a/event_converter_utils.js +5 -1
  91. package/dist/esm/a2a/metadata_converter_utils.js +2 -1
  92. package/dist/esm/agents/functions.js +198 -25
  93. package/dist/esm/agents/instructions.js +4 -1
  94. package/dist/esm/agents/invocation_context.js +24 -4
  95. package/dist/esm/agents/langgraph_agent.js +123 -0
  96. package/dist/esm/agents/llm_agent.js +185 -104
  97. package/dist/esm/agents/processors/code_execution_request_processor.js +13 -8
  98. package/dist/esm/agents/remote_agent.js +164 -0
  99. package/dist/esm/artifacts/in_memory_artifact_service.js +14 -0
  100. package/dist/esm/auth/auth_handler.js +37 -5
  101. package/dist/esm/auth/auth_schemes.js +8 -0
  102. package/dist/esm/cli/cli_eval.js +510 -0
  103. package/dist/esm/cli/utils/envs.js +78 -0
  104. package/dist/esm/cli/utils/evals.js +109 -0
  105. package/dist/esm/cli/utils/logs.js +111 -0
  106. package/dist/esm/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  107. package/dist/esm/code_executors/code_execution_utils.js +46 -16
  108. package/dist/esm/code_executors/code_executor_context.js +5 -2
  109. package/dist/esm/common.js +87 -3
  110. package/dist/esm/context/summarizers/llm_summarizer.js +12 -10
  111. package/dist/esm/context/token_based_context_compactor.js +2 -1
  112. package/dist/esm/events/abort_events.js +110 -0
  113. package/dist/esm/events/event.js +4 -2
  114. package/dist/esm/events/internal_metadata.js +97 -0
  115. package/dist/esm/examples/example_util.js +21 -1
  116. package/dist/esm/flows/llm_flows/audio_transcriber.js +142 -0
  117. package/dist/esm/index.js +99 -1
  118. package/dist/esm/integrations/agent_registry/helpers.js +1 -1
  119. package/dist/esm/models/gemini_llm_connection.js +16 -8
  120. package/dist/esm/models/google_llm.js +14 -17
  121. package/dist/esm/models/llm_response.js +9 -1
  122. package/dist/esm/models/registry.js +1 -1
  123. package/dist/esm/models/routed_llm.js +9 -8
  124. package/dist/esm/planners/plan_re_act_planner.js +1 -1
  125. package/dist/esm/plugins/debug_logging_plugin.js +615 -0
  126. package/dist/esm/plugins/plugin_manager.js +1 -1
  127. package/dist/esm/plugins/tool_call_integrity_plugin.js +339 -0
  128. package/dist/esm/runner/in_memory_runner.js +6 -11
  129. package/dist/esm/runner/runner.js +124 -35
  130. package/dist/esm/sessions/db/operations.js +1 -1
  131. package/dist/esm/sessions/in_memory_session_service.js +3 -1
  132. package/dist/esm/sessions/state.js +8 -4
  133. package/dist/esm/sessions/vertex_ai_session_service.js +34 -15
  134. package/dist/esm/telemetry/tracing.js +97 -66
  135. package/dist/esm/tools/agent_tool.js +44 -0
  136. package/dist/esm/tools/apihub_tool/apihub_toolset.js +106 -0
  137. package/dist/esm/tools/apihub_tool/clients/apihub_client.js +244 -0
  138. package/dist/esm/tools/apihub_tool/clients/secret_client.js +132 -0
  139. package/dist/esm/tools/apihub_tool/clients/service_account.js +21 -0
  140. package/dist/esm/tools/application_integration_tool/application_integration_toolset.js +163 -0
  141. package/dist/esm/tools/application_integration_tool/clients/cloud_request.js +103 -0
  142. package/dist/esm/tools/application_integration_tool/clients/connections_client.js +561 -0
  143. package/dist/esm/tools/application_integration_tool/clients/integration_client.js +215 -0
  144. package/dist/esm/tools/crewai_tool.js +107 -0
  145. package/dist/esm/tools/example_tool.js +13 -1
  146. package/dist/esm/tools/function_tool.js +316 -0
  147. package/dist/esm/tools/google_api_tool/google_api_tool.js +70 -0
  148. package/dist/esm/tools/google_api_tool/google_api_tool_set.js +188 -0
  149. package/dist/esm/tools/google_api_tool/google_api_tool_sets.js +24 -0
  150. package/dist/esm/tools/google_api_tool/googleapi_to_openapi_converter.js +393 -0
  151. package/dist/esm/tools/google_api_tool/index.js +33 -0
  152. package/dist/esm/tools/langchain_tool.js +148 -0
  153. package/dist/esm/tools/mcp/index.js +6 -0
  154. package/dist/esm/tools/mcp/mcp_session_manager.js +25 -0
  155. package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +1 -2
  156. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +50 -3
  157. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +2 -1
  158. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  159. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -2
  160. package/dist/esm/tools/retrieval/base_retrieval_tool.js +16 -2
  161. package/dist/esm/tools/toolbox_tool.js +80 -0
  162. package/dist/esm/utils/env_aware_utils.js +6 -8
  163. package/dist/esm/utils/error_utils.js +9 -10
  164. package/dist/esm/utils/file_extension_utils.js +11 -0
  165. package/dist/esm/utils/file_utils.js +2 -23
  166. package/dist/esm/utils/gemini_schema_util.js +4 -1
  167. package/dist/esm/utils/json_utils.js +21 -0
  168. package/dist/esm/utils/live_connection_utils.js +4 -5
  169. package/dist/esm/utils/logger_node.js +7 -1
  170. package/dist/esm/utils/object_notation_utils.js +4 -2
  171. package/dist/esm/utils/streaming_utils.js +11 -11
  172. package/dist/esm/version.js +1 -1
  173. package/dist/types/a2a/a2a_remote_agent.d.ts +9 -0
  174. package/dist/types/a2a/agent_card.d.ts +44 -1
  175. package/dist/types/a2a/agent_to_a2a.d.ts +9 -0
  176. package/dist/types/a2a/index.d.ts +1 -0
  177. package/dist/types/agents/functions.d.ts +33 -1
  178. package/dist/types/agents/invocation_context.d.ts +20 -1
  179. package/dist/types/agents/langgraph_agent.d.ts +153 -0
  180. package/dist/types/agents/remote_agent.d.ts +68 -0
  181. package/dist/types/auth/auth_handler.d.ts +28 -1
  182. package/dist/types/auth/auth_schemes.d.ts +13 -0
  183. package/dist/types/cli/cli_eval.d.ts +173 -0
  184. package/dist/types/cli/utils/envs.d.ts +23 -0
  185. package/dist/types/cli/utils/evals.d.ts +47 -0
  186. package/dist/types/cli/utils/logs.d.ts +38 -0
  187. package/dist/types/code_executors/code_execution_utils.d.ts +39 -0
  188. package/dist/types/code_executors/code_executor_context.d.ts +1 -0
  189. package/dist/types/common.d.ts +30 -4
  190. package/dist/types/events/abort_events.d.ts +43 -0
  191. package/dist/types/events/event.d.ts +4 -0
  192. package/dist/types/events/internal_metadata.d.ts +78 -0
  193. package/dist/types/examples/example_util.d.ts +17 -0
  194. package/dist/types/flows/llm_flows/audio_transcriber.d.ts +85 -0
  195. package/dist/types/index.d.ts +16 -1
  196. package/dist/types/models/gemini_llm_connection.d.ts +6 -7
  197. package/dist/types/models/routed_llm.d.ts +1 -1
  198. package/dist/types/plugins/debug_logging_plugin.d.ts +157 -0
  199. package/dist/types/plugins/tool_call_integrity_plugin.d.ts +116 -0
  200. package/dist/types/runner/in_memory_runner.d.ts +2 -1
  201. package/dist/types/runner/runner.d.ts +34 -0
  202. package/dist/types/sessions/state.d.ts +5 -1
  203. package/dist/types/sessions/vertex_ai_session_service.d.ts +8 -0
  204. package/dist/types/skills/skill.d.ts +1 -1
  205. package/dist/types/telemetry/tracing.d.ts +4 -2
  206. package/dist/types/tools/apihub_tool/apihub_toolset.d.ts +122 -0
  207. package/dist/types/tools/apihub_tool/clients/apihub_client.d.ts +142 -0
  208. package/dist/types/tools/apihub_tool/clients/secret_client.d.ts +55 -0
  209. package/dist/types/tools/apihub_tool/clients/service_account.d.ts +11 -0
  210. package/dist/types/tools/application_integration_tool/application_integration_toolset.d.ts +86 -0
  211. package/dist/types/tools/application_integration_tool/clients/cloud_request.d.ts +53 -0
  212. package/dist/types/tools/application_integration_tool/clients/connections_client.d.ts +215 -0
  213. package/dist/types/tools/application_integration_tool/clients/integration_client.d.ts +72 -0
  214. package/dist/types/tools/crewai_tool.d.ts +85 -0
  215. package/dist/types/tools/example_tool.d.ts +10 -0
  216. package/dist/types/tools/function_tool.d.ts +32 -0
  217. package/dist/types/tools/google_api_tool/google_api_tool.d.ts +30 -0
  218. package/dist/types/tools/google_api_tool/google_api_tool_set.d.ts +78 -0
  219. package/dist/types/tools/google_api_tool/google_api_tool_sets.d.ts +33 -0
  220. package/dist/types/tools/google_api_tool/googleapi_to_openapi_converter.d.ts +164 -0
  221. package/dist/types/tools/google_api_tool/index.d.ts +11 -0
  222. package/dist/types/tools/langchain_tool.d.ts +103 -0
  223. package/dist/types/tools/mcp/index.d.ts +3 -0
  224. package/dist/types/tools/mcp/mcp_session_manager.d.ts +34 -1
  225. package/dist/types/tools/mcp/mcp_toolset.d.ts +15 -0
  226. package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +19 -0
  227. package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +18 -1
  228. package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +2 -1
  229. package/dist/types/tools/retrieval/base_retrieval_tool.d.ts +15 -0
  230. package/dist/types/tools/toolbox_tool.d.ts +73 -0
  231. package/dist/types/utils/env_aware_utils.d.ts +18 -2
  232. package/dist/types/utils/error_utils.d.ts +7 -0
  233. package/dist/types/utils/file_utils.d.ts +6 -1
  234. package/dist/types/utils/gemini_schema_util.d.ts +1 -2
  235. package/dist/types/utils/json_utils.d.ts +11 -0
  236. package/dist/types/utils/object_notation_utils.d.ts +7 -0
  237. package/dist/types/utils/streaming_utils.d.ts +11 -4
  238. package/dist/types/version.d.ts +1 -1
  239. package/dist/web/index_web.js +105 -106
  240. package/dist/web/index_web.js.map +4 -4
  241. package/package.json +8 -3
@@ -0,0 +1,364 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
+ var __export = (target, all) => {
14
+ for (var name in all)
15
+ __defProp(target, name, { get: all[name], enumerable: true });
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
+ var tool_call_integrity_plugin_exports = {};
27
+ __export(tool_call_integrity_plugin_exports, {
28
+ TOOL_CALL_HMAC_METADATA_KEY: () => TOOL_CALL_HMAC_METADATA_KEY,
29
+ ToolCallIntegrityError: () => ToolCallIntegrityError,
30
+ ToolCallIntegrityPlugin: () => ToolCallIntegrityPlugin
31
+ });
32
+ module.exports = __toCommonJS(tool_call_integrity_plugin_exports);
33
+ var import_event = require("../events/event.js");
34
+ var import_internal_metadata = require("../events/internal_metadata.js");
35
+ var import_logger = require("../utils/logger.js");
36
+ var import_base_plugin = require("./base_plugin.js");
37
+ /**
38
+ * @license
39
+ * Copyright 2026 Google LLC
40
+ * SPDX-License-Identifier: Apache-2.0
41
+ */
42
+ const TOOL_CALL_HMAC_METADATA_KEY = `${import_internal_metadata.INTERNAL_METADATA_PREFIX}fc_hmac`;
43
+ const STAMP_PREFIX = "v1:";
44
+ const _ToolCallIntegrityError = class _ToolCallIntegrityError extends Error {
45
+ constructor(message) {
46
+ super(message);
47
+ this.name = "ToolCallIntegrityError";
48
+ }
49
+ };
50
+ __name(_ToolCallIntegrityError, "ToolCallIntegrityError");
51
+ let ToolCallIntegrityError = _ToolCallIntegrityError;
52
+ function jsonForm(value) {
53
+ const json = JSON.stringify(value);
54
+ return json === void 0 ? null : JSON.parse(json);
55
+ }
56
+ __name(jsonForm, "jsonForm");
57
+ function canonicalJson(value) {
58
+ if (Array.isArray(value)) {
59
+ return `[${value.map((item) => canonicalJson(item)).join(",")}]`;
60
+ }
61
+ if (value !== null && typeof value === "object") {
62
+ const object = value;
63
+ const entries = Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(object[key])}`);
64
+ return `{${entries.join(",")}}`;
65
+ }
66
+ return JSON.stringify(value);
67
+ }
68
+ __name(canonicalJson, "canonicalJson");
69
+ function jsonArgs(args) {
70
+ const json = jsonForm(args != null ? args : {});
71
+ return json != null ? json : {};
72
+ }
73
+ __name(jsonArgs, "jsonArgs");
74
+ function canonicalPayload(session, event, functionCall) {
75
+ var _a, _b, _c, _d, _e;
76
+ return new TextEncoder().encode(
77
+ canonicalJson({
78
+ app_name: session.appName,
79
+ user_id: session.userId,
80
+ session_id: session.id,
81
+ invocation_id: (_a = event.invocationId) != null ? _a : null,
82
+ branch: (_b = event.branch) != null ? _b : null,
83
+ author: (_c = event.author) != null ? _c : null,
84
+ name: (_d = functionCall.name) != null ? _d : null,
85
+ id: (_e = functionCall.id) != null ? _e : null,
86
+ args: jsonArgs(functionCall.args)
87
+ })
88
+ );
89
+ }
90
+ __name(canonicalPayload, "canonicalPayload");
91
+ function isStampMap(value) {
92
+ return value !== null && typeof value === "object" && !Array.isArray(value) && Object.values(value).every(
93
+ // eslint-disable-next-line no-control-regex
94
+ (stamp) => typeof stamp === "string" && /^[\x00-\x7f]*$/.test(stamp)
95
+ );
96
+ }
97
+ __name(isStampMap, "isStampMap");
98
+ function isRestored(event) {
99
+ var _a;
100
+ return Boolean((_a = event.customMetadata) == null ? void 0 : _a[import_internal_metadata.RESTORED_EVENT_KEY]);
101
+ }
102
+ __name(isRestored, "isRestored");
103
+ function getStamps(event) {
104
+ var _a;
105
+ const stamps = (_a = event.customMetadata) == null ? void 0 : _a[TOOL_CALL_HMAC_METADATA_KEY];
106
+ if (stamps === void 0 || stamps === null) {
107
+ return {};
108
+ }
109
+ if (!isStampMap(stamps)) {
110
+ throw new ToolCallIntegrityError(
111
+ `Event '${event.id}' has a malformed integrity stamp.`
112
+ );
113
+ }
114
+ return stamps;
115
+ }
116
+ __name(getStamps, "getStamps");
117
+ function hasStamp(stamps, callId) {
118
+ return Object.hasOwn(stamps, callId);
119
+ }
120
+ __name(hasStamp, "hasStamp");
121
+ function toHex(bytes) {
122
+ return Array.from(
123
+ new Uint8Array(bytes),
124
+ (b) => b.toString(16).padStart(2, "0")
125
+ ).join("");
126
+ }
127
+ __name(toHex, "toHex");
128
+ function fromHex(hex) {
129
+ if (!/^[0-9a-f]{64}$/.test(hex)) {
130
+ return void 0;
131
+ }
132
+ const bytes = new Uint8Array(hex.length / 2);
133
+ for (let i = 0; i < bytes.length; i++) {
134
+ bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
135
+ }
136
+ return bytes;
137
+ }
138
+ __name(fromHex, "fromHex");
139
+ function getSubtleCrypto() {
140
+ var _a;
141
+ const subtle = (_a = globalThis.crypto) == null ? void 0 : _a.subtle;
142
+ if (!subtle) {
143
+ throw new Error(
144
+ "ToolCallIntegrityPlugin requires the Web Crypto API (globalThis.crypto.subtle), which is not available in this environment."
145
+ );
146
+ }
147
+ return subtle;
148
+ }
149
+ __name(getSubtleCrypto, "getSubtleCrypto");
150
+ const _ToolCallIntegrityPlugin = class _ToolCallIntegrityPlugin extends import_base_plugin.BasePlugin {
151
+ /**
152
+ * @throws {Error} If `secretKey` is empty or contains a key that is not
153
+ * non-empty bytes.
154
+ */
155
+ constructor(options) {
156
+ var _a, _b;
157
+ super((_a = options.name) != null ? _a : "tool_call_integrity");
158
+ const secretKey = options.secretKey;
159
+ const rawKeys = secretKey instanceof Uint8Array ? [secretKey] : Array.isArray(secretKey) ? [...secretKey] : [];
160
+ if (rawKeys.length === 0 || !rawKeys.every((key) => key instanceof Uint8Array && key.length > 0)) {
161
+ throw new Error(
162
+ "secretKey must be non-empty bytes or a non-empty list of them."
163
+ );
164
+ }
165
+ this.keys = rawKeys.map((key) => new Uint8Array(key));
166
+ this.allowUnstampedCalls = (_b = options.allowUnstampedCalls) != null ? _b : false;
167
+ }
168
+ /**
169
+ * Returns a copy with internal metadata removed and marked as restored.
170
+ *
171
+ * Use it on events that your code copies into another session, so that the
172
+ * history passes the integrity check. The copy only keeps the history: its
173
+ * function calls are never executed.
174
+ *
175
+ * @param event The event to copy. It is not modified.
176
+ * @returns The marked copy, ready to append to the new session.
177
+ */
178
+ static prepareRestoredEvent(event) {
179
+ return (0, import_internal_metadata.markRestored)((0, import_event.createEvent)(structuredClone(event)));
180
+ }
181
+ getCryptoKeys() {
182
+ if (!this.cryptoKeys) {
183
+ const subtle = getSubtleCrypto();
184
+ this.cryptoKeys = Promise.all(
185
+ this.keys.map(
186
+ (key) => subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, [
187
+ "sign",
188
+ "verify"
189
+ ])
190
+ )
191
+ );
192
+ }
193
+ return this.cryptoKeys;
194
+ }
195
+ async sign(payload) {
196
+ const [primary] = await this.getCryptoKeys();
197
+ const digest = await getSubtleCrypto().sign("HMAC", primary, payload);
198
+ return STAMP_PREFIX + toHex(digest);
199
+ }
200
+ /** `subtle.verify` compares in constant time. */
201
+ async verify(payload, stamp) {
202
+ if (!stamp.startsWith(STAMP_PREFIX)) {
203
+ return false;
204
+ }
205
+ const signature = fromHex(stamp.slice(STAMP_PREFIX.length));
206
+ if (!signature) {
207
+ return false;
208
+ }
209
+ const subtle = getSubtleCrypto();
210
+ for (const key of await this.getCryptoKeys()) {
211
+ if (await subtle.verify("HMAC", key, signature, payload)) {
212
+ return true;
213
+ }
214
+ }
215
+ return false;
216
+ }
217
+ unstamped(name, callId) {
218
+ const message = `${name} call '${callId}' has no integrity stamp.`;
219
+ if (!this.allowUnstampedCalls) {
220
+ throw new ToolCallIntegrityError(message);
221
+ }
222
+ import_logger.logger.warn(message);
223
+ }
224
+ async onEventCallback({
225
+ invocationContext,
226
+ event
227
+ }) {
228
+ var _a;
229
+ const session = invocationContext.session;
230
+ const calls = (0, import_event.getFunctionCalls)(event).filter((fc) => fc.id);
231
+ if (!event.partial) {
232
+ const seen = /* @__PURE__ */ new Set();
233
+ for (const fc of calls) {
234
+ if (seen.has(fc.id)) {
235
+ throw new ToolCallIntegrityError(
236
+ `Event '${event.id}' repeats function call ID '${fc.id}'.`
237
+ );
238
+ }
239
+ seen.add(fc.id);
240
+ }
241
+ }
242
+ const stamps = {};
243
+ for (const fc of calls) {
244
+ stamps[fc.id] = await this.sign(canonicalPayload(session, event, fc));
245
+ }
246
+ if (Object.keys(stamps).length > 0) {
247
+ event.customMetadata = {
248
+ ...(_a = event.customMetadata) != null ? _a : {},
249
+ [TOOL_CALL_HMAC_METADATA_KEY]: stamps
250
+ };
251
+ }
252
+ return void 0;
253
+ }
254
+ async beforeRunCallback({
255
+ invocationContext
256
+ }) {
257
+ const session = invocationContext.session;
258
+ for (const event of session.events) {
259
+ const calls = (0, import_event.getFunctionCalls)(event);
260
+ if (calls.length === 0 || isRestored(event)) {
261
+ continue;
262
+ }
263
+ const stamps = getStamps(event);
264
+ const verified = /* @__PURE__ */ new Set();
265
+ for (const fc of calls) {
266
+ if (!fc.id) {
267
+ continue;
268
+ }
269
+ if (!hasStamp(stamps, fc.id)) {
270
+ this.unstamped(fc.name, fc.id);
271
+ continue;
272
+ }
273
+ if (verified.has(fc.id)) {
274
+ throw new ToolCallIntegrityError(
275
+ `Event '${event.id}' has duplicate function call ID '${fc.id}'.`
276
+ );
277
+ }
278
+ const payload = canonicalPayload(session, event, fc);
279
+ if (!await this.verify(payload, stamps[fc.id])) {
280
+ throw new ToolCallIntegrityError(
281
+ `${fc.name} call '${fc.id}' does not match its integrity stamp.`
282
+ );
283
+ }
284
+ verified.add(fc.id);
285
+ }
286
+ const unmatched = Object.keys(stamps).filter((id) => !verified.has(id)).sort();
287
+ if (unmatched.length > 0) {
288
+ throw new ToolCallIntegrityError(
289
+ `Event '${event.id}' has integrity stamps with no matching call: ${JSON.stringify(unmatched)}.`
290
+ );
291
+ }
292
+ }
293
+ return void 0;
294
+ }
295
+ async beforeToolCallback({
296
+ tool,
297
+ toolArgs,
298
+ toolContext
299
+ }) {
300
+ const callId = toolContext.functionCallId;
301
+ if (!callId) {
302
+ throw new ToolCallIntegrityError(
303
+ `${tool.name} call has no ID, so its integrity cannot be checked.`
304
+ );
305
+ }
306
+ const session = toolContext.invocationContext.session;
307
+ const stored = [];
308
+ let restored = false;
309
+ for (const event of session.events) {
310
+ for (const fc of (0, import_event.getFunctionCalls)(event)) {
311
+ if (fc.id !== callId) {
312
+ continue;
313
+ }
314
+ if (isRestored(event)) {
315
+ restored = true;
316
+ } else {
317
+ stored.push([event, fc]);
318
+ }
319
+ }
320
+ }
321
+ if (stored.length === 0) {
322
+ if (restored) {
323
+ throw new ToolCallIntegrityError(
324
+ `${tool.name} call '${callId}' comes from restored history and cannot be executed.`
325
+ );
326
+ }
327
+ this.unstamped(tool.name, callId);
328
+ return void 0;
329
+ }
330
+ const trusted = [];
331
+ for (const [event, fc] of stored) {
332
+ const stamps = getStamps(event);
333
+ if (!hasStamp(stamps, callId)) {
334
+ this.unstamped(tool.name, callId);
335
+ } else if (!await this.verify(
336
+ canonicalPayload(session, event, fc),
337
+ stamps[callId]
338
+ )) {
339
+ throw new ToolCallIntegrityError(
340
+ `${tool.name} call '${callId}' does not match its integrity stamp.`
341
+ );
342
+ }
343
+ trusted.push(fc);
344
+ }
345
+ const executed = canonicalJson(jsonArgs(toolArgs));
346
+ const matches = trusted.some(
347
+ (fc) => fc.name === tool.name && canonicalJson(jsonArgs(fc.args)) === executed
348
+ );
349
+ if (!matches) {
350
+ throw new ToolCallIntegrityError(
351
+ `${tool.name} call '${callId}' arguments do not match its stored call.`
352
+ );
353
+ }
354
+ return void 0;
355
+ }
356
+ };
357
+ __name(_ToolCallIntegrityPlugin, "ToolCallIntegrityPlugin");
358
+ let ToolCallIntegrityPlugin = _ToolCallIntegrityPlugin;
359
+ // Annotate the CommonJS export names for ESM import in node:
360
+ 0 && (module.exports = {
361
+ TOOL_CALL_HMAC_METADATA_KEY,
362
+ ToolCallIntegrityError,
363
+ ToolCallIntegrityPlugin
364
+ });
@@ -44,28 +44,23 @@ const _InMemoryRunner = class _InMemoryRunner extends import_runner.Runner {
44
44
  * @param params The configuration for the runner.
45
45
  * @param params.app An optional application instance to run.
46
46
  * @param params.agent The root agent to run.
47
- * @param params.appName The application name. Defaults to `'InMemoryRunner'`.
47
+ * @param params.appName The application name. Defaults to `app.name` when
48
+ * `app` is provided, or `'InMemoryRunner'` otherwise.
48
49
  * @param params.plugins An optional list of plugins.
49
50
  * @param params.resumabilityConfig An optional resumability configuration.
50
51
  */
51
52
  constructor(params) {
52
- var _a;
53
- const {
54
- agent,
55
- appName = "InMemoryRunner",
56
- plugins = [],
57
- app,
58
- resumabilityConfig
59
- } = params;
53
+ var _a, _b;
54
+ const { agent, appName, plugins = [], app, resumabilityConfig } = params;
60
55
  super({
61
56
  app,
62
- appName,
57
+ appName: (_a = appName != null ? appName : app == null ? void 0 : app.name) != null ? _a : "InMemoryRunner",
63
58
  agent,
64
59
  plugins,
65
60
  artifactService: new import_in_memory_artifact_service.InMemoryArtifactService(),
66
61
  sessionService: new import_in_memory_session_service.InMemorySessionService(),
67
62
  memoryService: new import_in_memory_memory_service.InMemoryMemoryService(),
68
- resumabilityConfig: (_a = app == null ? void 0 : app.resumabilityConfig) != null ? _a : resumabilityConfig
63
+ resumabilityConfig: (_b = app == null ? void 0 : app.resumabilityConfig) != null ? _b : resumabilityConfig
69
64
  });
70
65
  }
71
66
  };
@@ -43,8 +43,10 @@ var import_run_config = require("../agents/run_config.js");
43
43
  var import_scoped_artifact_service = require("../artifacts/scoped_artifact_service.js");
44
44
  var import_session_artifact_service = require("../artifacts/session_artifact_service.js");
45
45
  var import_built_in_code_executor = require("../code_executors/built_in_code_executor.js");
46
+ var import_abort_events = require("../events/abort_events.js");
46
47
  var import_event = require("../events/event.js");
47
48
  var import_event_actions = require("../events/event_actions.js");
49
+ var import_internal_metadata = require("../events/internal_metadata.js");
48
50
  var import_plugin_manager = require("../plugins/plugin_manager.js");
49
51
  var import_tracing = require("../telemetry/tracing.js");
50
52
  var import_base_toolset = require("../tools/base_toolset.js");
@@ -72,8 +74,8 @@ const _Runner = class _Runner {
72
74
  */
73
75
  constructor(input) {
74
76
  this[_a] = true;
75
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
76
- const appName = (_b = (_a2 = input.app) == null ? void 0 : _a2.name) != null ? _b : input.appName;
77
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
78
+ const appName = (_b = input.appName) != null ? _b : (_a2 = input.app) == null ? void 0 : _a2.name;
77
79
  const agent = (_d = (_c = input.app) == null ? void 0 : _c.rootAgent) != null ? _d : input.agent;
78
80
  if (!agent) {
79
81
  throw new Error(
@@ -90,6 +92,20 @@ const _Runner = class _Runner {
90
92
  this.memoryService = input.memoryService;
91
93
  this.credentialService = input.credentialService;
92
94
  this.resumabilityConfig = (_i = (_h = input.app) == null ? void 0 : _h.resumabilityConfig) != null ? _i : input.resumabilityConfig;
95
+ this.autoCreateSession = (_j = input.autoCreateSession) != null ? _j : false;
96
+ }
97
+ /**
98
+ * Looks the session up, creating it first when `autoCreateSession` is set.
99
+ *
100
+ * A runner with no `appName` cannot address a session, so it falls through to
101
+ * the plain lookup rather than creating one under an undefined app name.
102
+ */
103
+ async resolveSession(userId, sessionId) {
104
+ const key = { appName: this.appName, userId, sessionId };
105
+ if (this.autoCreateSession && this.appName) {
106
+ return this.sessionService.getOrCreateSession(key);
107
+ }
108
+ return this.sessionService.getSession(key);
93
109
  }
94
110
  /**
95
111
  * Runs the agent with a new, ephemeral session.
@@ -150,12 +166,8 @@ const _Runner = class _Runner {
150
166
  ctx,
151
167
  this,
152
168
  async function* () {
153
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
154
- const session = await this.sessionService.getSession({
155
- appName: this.appName,
156
- userId,
157
- sessionId
158
- });
169
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
170
+ const session = await this.resolveSession(userId, sessionId);
159
171
  if ((_a2 = params.abortSignal) == null ? void 0 : _a2.aborted) {
160
172
  return;
161
173
  }
@@ -230,7 +242,8 @@ const _Runner = class _Runner {
230
242
  author: "user",
231
243
  actions: stateDelta ? (0, import_event_actions.createEventActions)({ stateDelta }) : void 0,
232
244
  content: newMessage,
233
- customMetadata: params.customMetadata
245
+ // Callers cannot set ADK-internal keys.
246
+ customMetadata: (0, import_internal_metadata.withoutInternalMetadata)(params.customMetadata)
234
247
  })
235
248
  });
236
249
  if ((_e = params.abortSignal) == null ? void 0 : _e.aborted) {
@@ -265,35 +278,45 @@ const _Runner = class _Runner {
265
278
  }
266
279
  yield earlyExitEvent;
267
280
  } else {
268
- for await (const event of this.runRoot(invocationContext)) {
269
- if ((_h = params.abortSignal) == null ? void 0 : _h.aborted) {
270
- return;
271
- }
272
- const modifiedEvent = await this.pluginManager.runOnEventCallback({
273
- invocationContext,
274
- event
275
- });
276
- const outputEvent = modifiedEvent ? {
277
- ...modifiedEvent,
278
- id: event.id,
279
- invocationId: event.invocationId,
280
- timestamp: event.timestamp,
281
- author: modifiedEvent.author || event.author,
282
- branch: (_i = modifiedEvent.branch) != null ? _i : event.branch
283
- } : event;
284
- if (!event.partial) {
285
- await this.sessionService.appendEvent({
286
- session,
287
- event: outputEvent
288
- });
281
+ let abortSealed = false;
282
+ const sealAbortedInvocation = /* @__PURE__ */ __name(() => {
283
+ abortSealed = true;
284
+ return this.synthesizeAbortEvents(invocationContext);
285
+ }, "sealAbortedInvocation");
286
+ try {
287
+ for await (const event of this.runRoot(invocationContext)) {
288
+ if ((_h = params.abortSignal) == null ? void 0 : _h.aborted) {
289
+ break;
290
+ }
291
+ const outputEvent = await this.processAndAppendEvent(
292
+ invocationContext,
293
+ event
294
+ );
295
+ if ((_i = params.abortSignal) == null ? void 0 : _i.aborted) {
296
+ break;
297
+ }
298
+ yield outputEvent;
289
299
  }
290
300
  if ((_j = params.abortSignal) == null ? void 0 : _j.aborted) {
301
+ for (const abortEvent of await sealAbortedInvocation()) {
302
+ yield abortEvent;
303
+ }
291
304
  return;
292
305
  }
293
- yield outputEvent;
306
+ } finally {
307
+ if (((_k = params.abortSignal) == null ? void 0 : _k.aborted) && !abortSealed) {
308
+ try {
309
+ await sealAbortedInvocation();
310
+ } catch (e) {
311
+ import_logger.logger.error(
312
+ `Failed to seal aborted invocation ${invocationContext.invocationId}.`,
313
+ e
314
+ );
315
+ }
316
+ }
294
317
  }
295
318
  await this.pluginManager.runAfterRunCallback({ invocationContext });
296
- if ((_k = params.abortSignal) == null ? void 0 : _k.aborted) {
319
+ if ((_l = params.abortSignal) == null ? void 0 : _l.aborted) {
297
320
  return;
298
321
  }
299
322
  }
@@ -325,6 +348,56 @@ const _Runner = class _Runner {
325
348
  }
326
349
  yield* (0, import_run_node_as_invocation.runNodeAsInvocation)(this.agent, invocationContext);
327
350
  }
351
+ /**
352
+ * Runs the on_event plugin callbacks on an event and appends the result to
353
+ * the session unless it is partial.
354
+ *
355
+ * @returns The event as streamed to the caller.
356
+ */
357
+ async processAndAppendEvent(invocationContext, event) {
358
+ var _a2;
359
+ const modifiedEvent = await this.pluginManager.runOnEventCallback({
360
+ invocationContext,
361
+ event
362
+ });
363
+ const outputEvent = modifiedEvent ? {
364
+ ...withInternalMetadataOf(event, modifiedEvent),
365
+ id: event.id,
366
+ invocationId: event.invocationId,
367
+ timestamp: event.timestamp,
368
+ author: modifiedEvent.author || event.author,
369
+ branch: (_a2 = modifiedEvent.branch) != null ? _a2 : event.branch
370
+ } : event;
371
+ if (!event.partial) {
372
+ await this.sessionService.appendEvent({
373
+ session: invocationContext.session,
374
+ event: outputEvent
375
+ });
376
+ }
377
+ return outputEvent;
378
+ }
379
+ /**
380
+ * Seals an aborted invocation in session history: answers its dangling
381
+ * function calls with error responses, or records a plain abort event when
382
+ * there is nothing to answer.
383
+ *
384
+ * @returns The synthetic events after plugin processing and persistence.
385
+ */
386
+ async synthesizeAbortEvents(invocationContext) {
387
+ const abortEvents = (0, import_abort_events.buildAbortEvents)({
388
+ events: invocationContext.session.events,
389
+ invocationId: invocationContext.invocationId,
390
+ rootAgentName: this.agent.name,
391
+ branch: invocationContext.branch
392
+ });
393
+ const outputEvents = [];
394
+ for (const event of abortEvents) {
395
+ outputEvents.push(
396
+ await this.processAndAppendEvent(invocationContext, event)
397
+ );
398
+ }
399
+ return outputEvents;
400
+ }
328
401
  /**
329
402
  * Saves artifacts from the message parts and replaces the inline data with
330
403
  * a file name placeholder and optional file reference.
@@ -526,7 +599,7 @@ const _Runner = class _Runner {
526
599
  if ((_d2 = params.abortSignal) == null ? void 0 : _d2.aborted) {
527
600
  return;
528
601
  }
529
- const eventToProcess = modifiedEvent != null ? modifiedEvent : event;
602
+ const eventToProcess = modifiedEvent ? withInternalMetadataOf(event, modifiedEvent) : event;
530
603
  if (!eventToProcess.partial && !isLiveModelMediaEventWithInlineData(eventToProcess)) {
531
604
  await this.sessionService.appendEvent({
532
605
  session,
@@ -545,6 +618,18 @@ const _Runner = class _Runner {
545
618
  };
546
619
  __name(_Runner, "Runner");
547
620
  let Runner = _Runner;
621
+ function withInternalMetadataOf(original, replacement) {
622
+ var _a2;
623
+ const internal = (0, import_internal_metadata.internalMetadata)(original.customMetadata);
624
+ if (Object.keys(internal).length === 0) {
625
+ return replacement;
626
+ }
627
+ return {
628
+ ...replacement,
629
+ customMetadata: { ...(_a2 = replacement.customMetadata) != null ? _a2 : {}, ...internal }
630
+ };
631
+ }
632
+ __name(withInternalMetadataOf, "withInternalMetadataOf");
548
633
  function isLiveModelMediaEventWithInlineData(event) {
549
634
  var _a2;
550
635
  const parts = (_a2 = event.content) == null ? void 0 : _a2.parts;
@@ -559,7 +644,8 @@ function isLiveModelMediaEventWithInlineData(event) {
559
644
  }
560
645
  __name(isLiveModelMediaEventWithInlineData, "isLiveModelMediaEventWithInlineData");
561
646
  function determineAgentForResumption(session, rootAgent, resumabilityConfig) {
562
- const event = findEventByLastFunctionResponseId(session.events);
647
+ const lastEvent = session.events[session.events.length - 1];
648
+ const event = lastEvent && !(0, import_abort_events.isAbortEvent)(lastEvent) ? findEventByLastFunctionResponseId(session.events) : null;
563
649
  const isResumable = Boolean(resumabilityConfig == null ? void 0 : resumabilityConfig.isResumable);
564
650
  if (event && event.author && isResumable) {
565
651
  const conflictingAuthors = (0, import_functions.getConflictingFunctionResponseAuthors)(
@@ -583,7 +669,7 @@ function determineAgentForResumption(session, rootAgent, resumabilityConfig) {
583
669
  for (let i = session.events.length - 1; i >= 0; i--) {
584
670
  import_logger.logger.debug("event:", (0, import_redact_inline_data.stringifyWithRedactedInlineData)(session.events[i]));
585
671
  const event2 = session.events[i];
586
- if (event2.author === "user" || !event2.author) {
672
+ if (event2.author === "user" || !event2.author || (0, import_abort_events.isAbortEvent)(event2)) {
587
673
  continue;
588
674
  }
589
675
  if (event2.author === rootAgent.name) {
@@ -293,7 +293,7 @@ async function getConnectionOptionsFromUri(uri) {
293
293
  );
294
294
  return {
295
295
  entities: import_schema.ENTITIES,
296
- dbName: uri === "sqlite://:memory:" ? ":memory:" : uri.substring("sqlite://".length),
296
+ dbName: uri === "sqlite://:memory:" || uri === "sqlite:///:memory:" ? ":memory:" : uri.substring("sqlite://".length),
297
297
  driver: SqliteDriver
298
298
  };
299
299
  }
@@ -70,7 +70,9 @@ const _InMemorySessionService = class _InMemorySessionService extends import_bas
70
70
  var _a;
71
71
  const filteredState = state ? (0, import_base_session_service.trimTempState)(state) : void 0;
72
72
  const session = (0, import_session.createSession)({
73
- id: sessionId || (0, import_env_aware_utils.randomUUID)(),
73
+ // Surrounding whitespace is dropped so that a later lookup by the bare
74
+ // id finds the session, and a blank id gets a generated one.
75
+ id: (sessionId == null ? void 0 : sessionId.trim()) || (0, import_env_aware_utils.randomUUID)(),
74
76
  appName,
75
77
  userId,
76
78
  state: filteredState,