@google/adk 0.3.0 → 0.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 (265) hide show
  1. package/dist/cjs/a2a/a2a_event.js +290 -0
  2. package/dist/cjs/a2a/event_converter_utils.js +201 -0
  3. package/dist/cjs/a2a/executor_context.js +53 -0
  4. package/dist/cjs/a2a/metadata_converter_utils.js +125 -0
  5. package/dist/cjs/a2a/part_converter_utils.js +212 -0
  6. package/dist/cjs/agents/active_streaming_tool.js +1 -1
  7. package/dist/cjs/agents/base_agent.js +6 -6
  8. package/dist/cjs/agents/content_processor_utils.js +1 -1
  9. package/dist/cjs/{tools/tool_context.js → agents/context.js} +71 -16
  10. package/dist/cjs/agents/functions.js +4 -3
  11. package/dist/cjs/agents/instructions.js +1 -1
  12. package/dist/cjs/agents/invocation_context.js +1 -1
  13. package/dist/cjs/agents/live_request_queue.js +1 -1
  14. package/dist/cjs/agents/llm_agent.js +76 -711
  15. package/dist/cjs/agents/loop_agent.js +1 -1
  16. package/dist/cjs/agents/parallel_agent.js +1 -1
  17. package/dist/cjs/agents/processors/agent_transfer_llm_request_processor.js +132 -0
  18. package/dist/cjs/agents/{base_llm_processor.js → processors/base_llm_processor.js} +1 -1
  19. package/dist/cjs/agents/processors/basic_llm_request_processor.js +68 -0
  20. package/dist/cjs/agents/processors/code_execution_request_processor.js +389 -0
  21. package/dist/cjs/agents/processors/content_request_processor.js +66 -0
  22. package/dist/cjs/agents/processors/identity_llm_request_processor.js +54 -0
  23. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +85 -0
  24. package/dist/cjs/agents/processors/request_confirmation_llm_request_processor.js +165 -0
  25. package/dist/cjs/agents/readonly_context.js +13 -1
  26. package/dist/cjs/agents/run_config.js +2 -1
  27. package/dist/cjs/agents/sequential_agent.js +1 -1
  28. package/dist/cjs/agents/transcription_entry.js +1 -1
  29. package/dist/cjs/artifacts/base_artifact_service.js +1 -1
  30. package/dist/cjs/artifacts/file_artifact_service.js +491 -0
  31. package/dist/cjs/artifacts/gcs_artifact_service.js +127 -48
  32. package/dist/cjs/artifacts/in_memory_artifact_service.js +54 -6
  33. package/dist/cjs/artifacts/registry.js +55 -0
  34. package/dist/cjs/auth/auth_credential.js +1 -1
  35. package/dist/cjs/auth/auth_handler.js +1 -1
  36. package/dist/cjs/auth/auth_schemes.js +1 -1
  37. package/dist/cjs/auth/auth_tool.js +1 -1
  38. package/dist/cjs/auth/credential_service/base_credential_service.js +1 -1
  39. package/dist/cjs/auth/credential_service/in_memory_credential_service.js +1 -1
  40. package/dist/cjs/auth/exchanger/base_credential_exchanger.js +1 -1
  41. package/dist/cjs/auth/exchanger/credential_exchanger_registry.js +1 -1
  42. package/dist/cjs/code_executors/base_code_executor.js +1 -1
  43. package/dist/cjs/code_executors/built_in_code_executor.js +1 -1
  44. package/dist/cjs/code_executors/code_execution_utils.js +1 -1
  45. package/dist/cjs/code_executors/code_executor_context.js +1 -1
  46. package/dist/cjs/common.js +25 -8
  47. package/dist/cjs/events/event.js +33 -4
  48. package/dist/cjs/events/event_actions.js +2 -2
  49. package/dist/cjs/events/structured_events.js +105 -0
  50. package/dist/cjs/examples/base_example_provider.js +1 -1
  51. package/dist/cjs/examples/example.js +1 -1
  52. package/dist/cjs/examples/example_util.js +1 -1
  53. package/dist/cjs/index.js +24 -17
  54. package/dist/cjs/index.js.map +4 -4
  55. package/dist/cjs/index_web.js +1 -1
  56. package/dist/cjs/memory/base_memory_service.js +1 -1
  57. package/dist/cjs/memory/in_memory_memory_service.js +1 -1
  58. package/dist/cjs/memory/memory_entry.js +1 -1
  59. package/dist/cjs/models/apigee_llm.js +182 -0
  60. package/dist/cjs/models/base_llm.js +1 -1
  61. package/dist/cjs/models/base_llm_connection.js +1 -1
  62. package/dist/cjs/models/gemini_llm_connection.js +1 -1
  63. package/dist/cjs/models/google_llm.js +70 -51
  64. package/dist/cjs/models/llm_request.js +1 -1
  65. package/dist/cjs/models/llm_response.js +3 -1
  66. package/dist/cjs/models/registry.js +3 -1
  67. package/dist/cjs/plugins/base_plugin.js +2 -2
  68. package/dist/cjs/plugins/logging_plugin.js +1 -1
  69. package/dist/cjs/plugins/plugin_manager.js +1 -1
  70. package/dist/cjs/plugins/security_plugin.js +1 -1
  71. package/dist/cjs/runner/in_memory_runner.js +1 -1
  72. package/dist/cjs/runner/runner.js +33 -2
  73. package/dist/cjs/sessions/base_session_service.js +53 -3
  74. package/dist/cjs/sessions/database_session_service.js +367 -0
  75. package/dist/cjs/sessions/db/operations.js +126 -0
  76. package/dist/cjs/sessions/db/schema.js +204 -0
  77. package/dist/cjs/sessions/in_memory_session_service.js +24 -22
  78. package/dist/cjs/sessions/registry.js +49 -0
  79. package/dist/cjs/sessions/session.js +1 -1
  80. package/dist/cjs/sessions/state.js +1 -1
  81. package/dist/cjs/telemetry/google_cloud.js +1 -1
  82. package/dist/cjs/telemetry/setup.js +1 -1
  83. package/dist/cjs/telemetry/tracing.js +1 -1
  84. package/dist/cjs/tools/agent_tool.js +1 -1
  85. package/dist/cjs/tools/base_tool.js +4 -1
  86. package/dist/cjs/tools/base_toolset.js +14 -4
  87. package/dist/cjs/tools/exit_loop_tool.js +63 -0
  88. package/dist/cjs/tools/forwarding_artifact_service.js +17 -1
  89. package/dist/cjs/tools/function_tool.js +1 -1
  90. package/dist/cjs/tools/google_search_tool.js +1 -1
  91. package/dist/cjs/tools/long_running_tool.js +1 -1
  92. package/dist/cjs/tools/mcp/mcp_session_manager.js +1 -1
  93. package/dist/cjs/tools/mcp/mcp_tool.js +1 -1
  94. package/dist/cjs/tools/mcp/mcp_toolset.js +10 -6
  95. package/dist/cjs/tools/tool_confirmation.js +1 -1
  96. package/dist/cjs/utils/client_labels.js +1 -1
  97. package/dist/cjs/utils/env_aware_utils.js +10 -1
  98. package/dist/cjs/utils/gemini_schema_util.js +1 -1
  99. package/dist/cjs/utils/logger.js +62 -55
  100. package/dist/cjs/utils/model_name.js +1 -1
  101. package/dist/cjs/utils/object_notation_utils.js +78 -0
  102. package/dist/cjs/utils/simple_zod_to_json.js +1 -1
  103. package/dist/cjs/utils/variant_utils.js +3 -9
  104. package/dist/cjs/version.js +2 -2
  105. package/dist/esm/a2a/a2a_event.js +243 -0
  106. package/dist/esm/a2a/event_converter_utils.js +187 -0
  107. package/dist/esm/a2a/executor_context.js +23 -0
  108. package/dist/esm/a2a/metadata_converter_utils.js +90 -0
  109. package/dist/esm/a2a/part_converter_utils.js +175 -0
  110. package/dist/esm/agents/base_agent.js +5 -5
  111. package/dist/esm/{tools/tool_context.js → agents/context.js} +66 -11
  112. package/dist/esm/agents/functions.js +3 -2
  113. package/dist/esm/agents/llm_agent.js +69 -720
  114. package/dist/esm/agents/processors/agent_transfer_llm_request_processor.js +101 -0
  115. package/dist/esm/agents/processors/basic_llm_request_processor.js +37 -0
  116. package/dist/esm/agents/processors/code_execution_request_processor.js +363 -0
  117. package/dist/esm/agents/processors/content_request_processor.js +38 -0
  118. package/dist/esm/agents/processors/identity_llm_request_processor.js +23 -0
  119. package/dist/esm/agents/processors/instructions_llm_request_processor.js +54 -0
  120. package/dist/esm/agents/processors/request_confirmation_llm_request_processor.js +140 -0
  121. package/dist/esm/agents/readonly_context.js +12 -0
  122. package/dist/esm/agents/run_config.js +1 -0
  123. package/dist/esm/artifacts/file_artifact_service.js +451 -0
  124. package/dist/esm/artifacts/gcs_artifact_service.js +126 -47
  125. package/dist/esm/artifacts/in_memory_artifact_service.js +51 -4
  126. package/dist/esm/artifacts/registry.js +28 -0
  127. package/dist/esm/common.js +20 -10
  128. package/dist/esm/events/event.js +29 -2
  129. package/dist/esm/events/event_actions.js +1 -1
  130. package/dist/esm/events/structured_events.js +74 -0
  131. package/dist/esm/index.js +24 -17
  132. package/dist/esm/index.js.map +4 -4
  133. package/dist/esm/models/apigee_llm.js +152 -0
  134. package/dist/esm/models/google_llm.js +67 -49
  135. package/dist/esm/models/llm_response.js +2 -0
  136. package/dist/esm/models/registry.js +2 -0
  137. package/dist/esm/plugins/base_plugin.js +1 -1
  138. package/dist/esm/runner/runner.js +32 -1
  139. package/dist/esm/sessions/base_session_service.js +49 -1
  140. package/dist/esm/sessions/database_session_service.js +353 -0
  141. package/dist/esm/sessions/db/operations.js +111 -0
  142. package/dist/esm/sessions/db/schema.js +172 -0
  143. package/dist/esm/sessions/in_memory_session_service.js +23 -21
  144. package/dist/esm/sessions/registry.js +25 -0
  145. package/dist/esm/tools/base_tool.js +3 -0
  146. package/dist/esm/tools/base_toolset.js +11 -2
  147. package/dist/esm/tools/exit_loop_tool.js +32 -0
  148. package/dist/esm/tools/forwarding_artifact_service.js +16 -0
  149. package/dist/esm/tools/mcp/mcp_toolset.js +9 -5
  150. package/dist/esm/utils/env_aware_utils.js +8 -0
  151. package/dist/esm/utils/logger.js +51 -54
  152. package/dist/esm/utils/object_notation_utils.js +47 -0
  153. package/dist/esm/utils/variant_utils.js +1 -7
  154. package/dist/esm/version.js +1 -1
  155. package/dist/types/a2a/a2a_event.d.ts +122 -0
  156. package/dist/types/a2a/event_converter_utils.d.ts +20 -0
  157. package/dist/types/a2a/executor_context.d.ts +33 -0
  158. package/dist/types/a2a/metadata_converter_utils.d.ts +62 -0
  159. package/dist/types/a2a/part_converter_utils.d.ts +48 -0
  160. package/dist/types/agents/base_agent.d.ts +2 -2
  161. package/dist/types/{tools/tool_context.d.ts → agents/context.d.ts} +43 -8
  162. package/dist/types/agents/llm_agent.d.ts +19 -42
  163. package/dist/types/agents/processors/agent_transfer_llm_request_processor.d.ts +18 -0
  164. package/dist/types/agents/{base_llm_processor.d.ts → processors/base_llm_processor.d.ts} +4 -4
  165. package/dist/types/agents/processors/basic_llm_request_processor.d.ts +13 -0
  166. package/dist/types/agents/processors/code_execution_request_processor.d.ts +34 -0
  167. package/dist/types/agents/processors/content_request_processor.d.ts +13 -0
  168. package/dist/types/agents/processors/identity_llm_request_processor.d.ts +13 -0
  169. package/dist/types/agents/processors/instructions_llm_request_processor.d.ts +16 -0
  170. package/dist/types/agents/processors/request_confirmation_llm_request_processor.d.ts +13 -0
  171. package/dist/types/agents/readonly_context.d.ts +8 -0
  172. package/dist/types/agents/run_config.d.ts +6 -0
  173. package/dist/types/artifacts/base_artifact_service.d.ts +31 -0
  174. package/dist/types/artifacts/file_artifact_service.d.ts +43 -0
  175. package/dist/types/artifacts/gcs_artifact_service.d.ts +3 -1
  176. package/dist/types/artifacts/in_memory_artifact_service.d.ts +5 -2
  177. package/dist/types/artifacts/registry.d.ts +7 -0
  178. package/dist/types/auth/credential_service/base_credential_service.d.ts +3 -3
  179. package/dist/types/auth/credential_service/in_memory_credential_service.d.ts +3 -3
  180. package/dist/types/common.d.ts +15 -6
  181. package/dist/types/events/event.d.ts +15 -1
  182. package/dist/types/events/event_actions.d.ts +1 -1
  183. package/dist/types/events/structured_events.d.ts +106 -0
  184. package/dist/types/index.d.ts +5 -1
  185. package/dist/types/models/apigee_llm.d.ts +59 -0
  186. package/dist/types/models/google_llm.d.ts +5 -2
  187. package/dist/types/models/llm_response.d.ts +5 -1
  188. package/dist/types/plugins/base_plugin.d.ts +12 -13
  189. package/dist/types/plugins/logging_plugin.d.ts +9 -10
  190. package/dist/types/plugins/plugin_manager.d.ts +9 -10
  191. package/dist/types/plugins/security_plugin.d.ts +2 -2
  192. package/dist/types/runner/runner.d.ts +15 -0
  193. package/dist/types/sessions/base_session_service.d.ts +20 -0
  194. package/dist/types/sessions/database_session_service.d.ts +32 -0
  195. package/dist/types/sessions/db/operations.d.ts +29 -0
  196. package/dist/types/sessions/db/schema.d.ts +45 -0
  197. package/dist/types/sessions/in_memory_session_service.d.ts +4 -1
  198. package/dist/types/sessions/registry.d.ts +7 -0
  199. package/dist/types/tools/base_tool.d.ts +3 -3
  200. package/dist/types/tools/base_toolset.d.ts +12 -3
  201. package/dist/types/tools/exit_loop_tool.d.ts +24 -0
  202. package/dist/types/tools/forwarding_artifact_service.d.ts +5 -3
  203. package/dist/types/tools/function_tool.d.ts +2 -2
  204. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -1
  205. package/dist/types/utils/env_aware_utils.d.ts +7 -0
  206. package/dist/types/utils/logger.d.ts +5 -9
  207. package/dist/types/utils/object_notation_utils.d.ts +21 -0
  208. package/dist/types/version.d.ts +1 -1
  209. package/dist/web/a2a/a2a_event.js +243 -0
  210. package/dist/web/a2a/event_converter_utils.js +201 -0
  211. package/dist/web/a2a/executor_context.js +23 -0
  212. package/dist/web/a2a/metadata_converter_utils.js +107 -0
  213. package/dist/web/a2a/part_converter_utils.js +175 -0
  214. package/dist/web/agents/base_agent.js +5 -5
  215. package/dist/web/{tools/tool_context.js → agents/context.js} +66 -11
  216. package/dist/web/agents/functions.js +3 -2
  217. package/dist/web/agents/llm_agent.js +90 -717
  218. package/dist/web/agents/processors/agent_transfer_llm_request_processor.js +100 -0
  219. package/dist/web/agents/processors/basic_llm_request_processor.js +71 -0
  220. package/dist/web/agents/processors/code_execution_request_processor.js +365 -0
  221. package/dist/web/agents/processors/content_request_processor.js +56 -0
  222. package/dist/web/agents/processors/identity_llm_request_processor.js +41 -0
  223. package/dist/web/agents/processors/instructions_llm_request_processor.js +72 -0
  224. package/dist/web/agents/processors/request_confirmation_llm_request_processor.js +158 -0
  225. package/dist/web/agents/readonly_context.js +12 -0
  226. package/dist/web/agents/run_config.js +2 -1
  227. package/dist/web/artifacts/file_artifact_service.js +506 -0
  228. package/dist/web/artifacts/gcs_artifact_service.js +123 -46
  229. package/dist/web/artifacts/in_memory_artifact_service.js +51 -4
  230. package/dist/web/artifacts/registry.js +28 -0
  231. package/dist/web/common.js +20 -10
  232. package/dist/web/events/event.js +29 -2
  233. package/dist/web/events/event_actions.js +1 -1
  234. package/dist/web/events/structured_events.js +74 -0
  235. package/dist/web/index.js +7 -2
  236. package/dist/web/index.js.map +4 -4
  237. package/dist/web/models/apigee_llm.js +219 -0
  238. package/dist/web/models/google_llm.js +67 -46
  239. package/dist/web/models/llm_response.js +2 -0
  240. package/dist/web/models/registry.js +2 -0
  241. package/dist/web/plugins/base_plugin.js +1 -1
  242. package/dist/web/runner/runner.js +34 -1
  243. package/dist/web/sessions/base_session_service.js +49 -1
  244. package/dist/web/sessions/database_session_service.js +371 -0
  245. package/dist/web/sessions/db/operations.js +111 -0
  246. package/dist/web/sessions/db/schema.js +172 -0
  247. package/dist/web/sessions/in_memory_session_service.js +23 -21
  248. package/dist/web/sessions/registry.js +25 -0
  249. package/dist/web/tools/base_tool.js +3 -0
  250. package/dist/web/tools/base_toolset.js +11 -2
  251. package/dist/web/tools/exit_loop_tool.js +32 -0
  252. package/dist/web/tools/forwarding_artifact_service.js +16 -0
  253. package/dist/web/tools/mcp/mcp_toolset.js +27 -5
  254. package/dist/web/utils/env_aware_utils.js +8 -0
  255. package/dist/web/utils/logger.js +51 -54
  256. package/dist/web/utils/object_notation_utils.js +47 -0
  257. package/dist/web/utils/variant_utils.js +1 -7
  258. package/dist/web/version.js +1 -1
  259. package/package.json +13 -3
  260. package/dist/cjs/agents/callback_context.js +0 -101
  261. package/dist/esm/agents/callback_context.js +0 -71
  262. package/dist/types/agents/callback_context.d.ts +0 -42
  263. package/dist/web/agents/callback_context.js +0 -71
  264. /package/dist/esm/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
  265. /package/dist/web/agents/{base_llm_processor.js → processors/base_llm_processor.js} +0 -0
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import {
7
+ getFunctionCalls,
8
+ getFunctionResponses
9
+ } from "../../events/event.js";
10
+ import { ToolConfirmation } from "../../tools/tool_confirmation.js";
11
+ import {
12
+ REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
13
+ handleFunctionCallList
14
+ } from "../functions.js";
15
+ import { isLlmAgent } from "../llm_agent.js";
16
+ import { ReadonlyContext } from "../readonly_context.js";
17
+ import { BaseLlmRequestProcessor } from "./base_llm_processor.js";
18
+ class RequestConfirmationLlmRequestProcessor extends BaseLlmRequestProcessor {
19
+ /** Handles tool confirmation information to build the LLM request. */
20
+ async *runAsync(invocationContext) {
21
+ const agent = invocationContext.agent;
22
+ if (!isLlmAgent(agent)) {
23
+ return;
24
+ }
25
+ const events = invocationContext.session.events;
26
+ if (!events || events.length === 0) {
27
+ return;
28
+ }
29
+ const requestConfirmationFunctionResponses = {};
30
+ let confirmationEventIndex = -1;
31
+ for (let i = events.length - 1; i >= 0; i--) {
32
+ const event = events[i];
33
+ if (event.author !== "user") {
34
+ continue;
35
+ }
36
+ const responses = getFunctionResponses(event);
37
+ if (!responses) {
38
+ continue;
39
+ }
40
+ let foundConfirmation = false;
41
+ for (const functionResponse of responses) {
42
+ if (functionResponse.name !== REQUEST_CONFIRMATION_FUNCTION_CALL_NAME) {
43
+ continue;
44
+ }
45
+ foundConfirmation = true;
46
+ let toolConfirmation = null;
47
+ if (functionResponse.response && Object.keys(functionResponse.response).length === 1 && "response" in functionResponse.response) {
48
+ toolConfirmation = JSON.parse(
49
+ functionResponse.response["response"]
50
+ );
51
+ } else if (functionResponse.response) {
52
+ toolConfirmation = new ToolConfirmation({
53
+ hint: functionResponse.response["hint"],
54
+ payload: functionResponse.response["payload"],
55
+ confirmed: functionResponse.response["confirmed"]
56
+ });
57
+ }
58
+ if (functionResponse.id && toolConfirmation) {
59
+ requestConfirmationFunctionResponses[functionResponse.id] = toolConfirmation;
60
+ }
61
+ }
62
+ if (foundConfirmation) {
63
+ confirmationEventIndex = i;
64
+ break;
65
+ }
66
+ }
67
+ if (Object.keys(requestConfirmationFunctionResponses).length === 0) {
68
+ return;
69
+ }
70
+ for (let i = confirmationEventIndex - 1; i >= 0; i--) {
71
+ const event = events[i];
72
+ const functionCalls = getFunctionCalls(event);
73
+ if (!functionCalls) {
74
+ continue;
75
+ }
76
+ const toolsToResumeWithConfirmation = {};
77
+ const toolsToResumeWithArgs = {};
78
+ for (const functionCall of functionCalls) {
79
+ if (!functionCall.id || !(functionCall.id in requestConfirmationFunctionResponses)) {
80
+ continue;
81
+ }
82
+ const args = functionCall.args;
83
+ if (!args || !("originalFunctionCall" in args)) {
84
+ continue;
85
+ }
86
+ const originalFunctionCall = args["originalFunctionCall"];
87
+ if (originalFunctionCall.id) {
88
+ toolsToResumeWithConfirmation[originalFunctionCall.id] = requestConfirmationFunctionResponses[functionCall.id];
89
+ toolsToResumeWithArgs[originalFunctionCall.id] = originalFunctionCall;
90
+ }
91
+ }
92
+ if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
93
+ continue;
94
+ }
95
+ for (let j = events.length - 1; j > confirmationEventIndex; j--) {
96
+ const eventToCheck = events[j];
97
+ const functionResponses = getFunctionResponses(eventToCheck);
98
+ if (!functionResponses) {
99
+ continue;
100
+ }
101
+ for (const fr of functionResponses) {
102
+ if (fr.id && fr.id in toolsToResumeWithConfirmation) {
103
+ delete toolsToResumeWithConfirmation[fr.id];
104
+ delete toolsToResumeWithArgs[fr.id];
105
+ }
106
+ }
107
+ if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
108
+ break;
109
+ }
110
+ }
111
+ if (Object.keys(toolsToResumeWithConfirmation).length === 0) {
112
+ continue;
113
+ }
114
+ const toolsList = await agent.canonicalTools(
115
+ new ReadonlyContext(invocationContext)
116
+ );
117
+ const toolsDict = Object.fromEntries(
118
+ toolsList.map((tool) => [tool.name, tool])
119
+ );
120
+ const functionResponseEvent = await handleFunctionCallList({
121
+ invocationContext,
122
+ functionCalls: Object.values(toolsToResumeWithArgs),
123
+ toolsDict,
124
+ beforeToolCallbacks: agent.canonicalBeforeToolCallbacks,
125
+ afterToolCallbacks: agent.canonicalAfterToolCallbacks,
126
+ filters: new Set(Object.keys(toolsToResumeWithConfirmation)),
127
+ toolConfirmationDict: toolsToResumeWithConfirmation
128
+ });
129
+ if (functionResponseEvent) {
130
+ yield functionResponseEvent;
131
+ }
132
+ return;
133
+ }
134
+ }
135
+ }
136
+ const REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR = new RequestConfirmationLlmRequestProcessor();
137
+ export {
138
+ REQUEST_CONFIRMATION_LLM_REQUEST_PROCESSOR,
139
+ RequestConfirmationLlmRequestProcessor
140
+ };
@@ -20,6 +20,18 @@ class ReadonlyContext {
20
20
  get invocationId() {
21
21
  return this.invocationContext.invocationId;
22
22
  }
23
+ /**
24
+ * The user ID of the current session.
25
+ */
26
+ get userId() {
27
+ return this.invocationContext.userId;
28
+ }
29
+ /**
30
+ * The ID of the current session.
31
+ */
32
+ get sessionId() {
33
+ return this.invocationContext.session.id;
34
+ }
23
35
  /**
24
36
  * The current agent name.
25
37
  */
@@ -17,6 +17,7 @@ function createRunConfig(params = {}) {
17
17
  enableAffectiveDialog: false,
18
18
  streamingMode: "none" /* NONE */,
19
19
  maxLlmCalls: validateMaxLlmCalls(params.maxLlmCalls || 500),
20
+ pauseOnToolCalls: false,
20
21
  ...params
21
22
  };
22
23
  }
@@ -0,0 +1,451 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import * as fs from "fs/promises";
7
+ import * as path from "path";
8
+ import { fileURLToPath, pathToFileURL } from "url";
9
+ import { logger } from "../utils/logger.js";
10
+ const USER_NAMESPACE_PREFIX = "user:";
11
+ class FileArtifactService {
12
+ constructor(rootDirOrUri) {
13
+ try {
14
+ const rootDir = rootDirOrUri.startsWith("file://") ? fileURLToPath(rootDirOrUri) : rootDirOrUri;
15
+ this.rootDir = path.resolve(rootDir);
16
+ } catch (e) {
17
+ throw new Error(`Invalid root directory: ${rootDirOrUri}`, { cause: e });
18
+ }
19
+ }
20
+ async saveArtifact({
21
+ userId,
22
+ sessionId,
23
+ filename,
24
+ artifact,
25
+ customMetadata
26
+ }) {
27
+ if (!artifact.inlineData && !artifact.text) {
28
+ throw new Error("Artifact must have either inlineData or text content.");
29
+ }
30
+ const artifactDir = getArtifactDir(
31
+ this.rootDir,
32
+ userId,
33
+ sessionId,
34
+ filename
35
+ );
36
+ await fs.mkdir(artifactDir, { recursive: true });
37
+ const versions = await getArtifactVersionsFromDir(artifactDir);
38
+ const nextVersion = versions.length > 0 ? versions[versions.length - 1] + 1 : 0;
39
+ const versionsDir = getVersionsDir(artifactDir);
40
+ const versionDir = path.join(versionsDir, nextVersion.toString());
41
+ await fs.mkdir(versionDir, { recursive: true });
42
+ const storedFilename = path.basename(artifactDir);
43
+ const contentPath = path.join(versionDir, storedFilename);
44
+ let mimeType;
45
+ if (artifact.inlineData) {
46
+ const data = artifact.inlineData.data || "";
47
+ await fs.writeFile(contentPath, Buffer.from(data, "base64"));
48
+ mimeType = artifact.inlineData.mimeType || "application/octet-stream";
49
+ } else if (artifact.text !== void 0) {
50
+ await fs.writeFile(contentPath, artifact.text, "utf-8");
51
+ }
52
+ const canonicalUri = await getCanonicalUri(
53
+ this.rootDir,
54
+ userId,
55
+ sessionId,
56
+ filename,
57
+ nextVersion
58
+ );
59
+ const metadata = {
60
+ fileName: filename,
61
+ mimeType,
62
+ version: nextVersion,
63
+ canonicalUri,
64
+ customMetadata
65
+ };
66
+ await writeMetadata(path.join(versionDir, "metadata.json"), metadata);
67
+ return nextVersion;
68
+ }
69
+ async loadArtifact({
70
+ userId,
71
+ sessionId,
72
+ filename,
73
+ version
74
+ }) {
75
+ try {
76
+ const artifactDir = getArtifactDir(
77
+ this.rootDir,
78
+ userId,
79
+ sessionId,
80
+ filename
81
+ );
82
+ try {
83
+ await fs.access(artifactDir);
84
+ } catch (e) {
85
+ logger.warn(
86
+ `[FileArtifactService] loadArtifact: Artifact ${filename} not found`,
87
+ e
88
+ );
89
+ return void 0;
90
+ }
91
+ const versions = await getArtifactVersionsFromDir(artifactDir);
92
+ if (versions.length === 0) {
93
+ return void 0;
94
+ }
95
+ let versionToLoad;
96
+ if (version === void 0) {
97
+ versionToLoad = versions[versions.length - 1];
98
+ } else {
99
+ if (!versions.includes(version)) {
100
+ logger.warn(
101
+ `[FileArtifactService] loadArtifact: Artifact ${filename} version ${version} not found`
102
+ );
103
+ return void 0;
104
+ }
105
+ versionToLoad = version;
106
+ }
107
+ const versionDir = path.join(
108
+ getVersionsDir(artifactDir),
109
+ versionToLoad.toString()
110
+ );
111
+ const metadataPath = path.join(versionDir, "metadata.json");
112
+ const metadata = await readMetadata(metadataPath);
113
+ const storedFilename = path.basename(artifactDir);
114
+ let contentPath = path.join(versionDir, storedFilename);
115
+ if (metadata.canonicalUri) {
116
+ const uriPath = fileUriToPath(metadata.canonicalUri);
117
+ if (uriPath) {
118
+ try {
119
+ await fs.access(uriPath);
120
+ contentPath = uriPath;
121
+ } catch {
122
+ logger.warn(
123
+ `[FileArtifactService] loadArtifact: Artifact ${filename} missing at ${uriPath}, falling back to content path ${contentPath}`
124
+ );
125
+ }
126
+ }
127
+ }
128
+ if (metadata.mimeType) {
129
+ try {
130
+ const data = await fs.readFile(contentPath);
131
+ return {
132
+ inlineData: {
133
+ mimeType: metadata.mimeType,
134
+ data: data.toString("base64")
135
+ }
136
+ };
137
+ } catch {
138
+ logger.warn(
139
+ `[FileArtifactService] loadArtifact: Artifact ${filename} missing at ${contentPath}`
140
+ );
141
+ return void 0;
142
+ }
143
+ }
144
+ try {
145
+ const text = await fs.readFile(contentPath, "utf-8");
146
+ return { text };
147
+ } catch {
148
+ logger.warn(
149
+ `[FileArtifactService] loadArtifact: Text artifact ${filename} missing at ${contentPath}`
150
+ );
151
+ return void 0;
152
+ }
153
+ } catch (e) {
154
+ logger.error(
155
+ `[FileArtifactService] loadArtifact: Error loading artifact ${filename}`,
156
+ e
157
+ );
158
+ return void 0;
159
+ }
160
+ }
161
+ async listArtifactKeys({
162
+ userId,
163
+ sessionId
164
+ }) {
165
+ const filenames = /* @__PURE__ */ new Set();
166
+ const userRoot = getUserRoot(this.rootDir, userId);
167
+ const sessionRoot = getSessionArtifactsDir(userRoot, sessionId);
168
+ for await (const artifactDir of iterateArtifactDirs(sessionRoot)) {
169
+ const metadata = await getLatestMetadata(artifactDir);
170
+ if (metadata == null ? void 0 : metadata.fileName) {
171
+ filenames.add(metadata.fileName);
172
+ } else {
173
+ const rel = path.relative(sessionRoot, artifactDir);
174
+ filenames.add(asPosixPath(rel));
175
+ }
176
+ }
177
+ const artifactsRoot = getUserArtifactsDir(userRoot);
178
+ for await (const artifactDir of iterateArtifactDirs(artifactsRoot)) {
179
+ const metadata = await getLatestMetadata(artifactDir);
180
+ if (metadata == null ? void 0 : metadata.fileName) {
181
+ filenames.add(metadata.fileName);
182
+ } else {
183
+ const rel = path.relative(artifactsRoot, artifactDir);
184
+ filenames.add(`${USER_NAMESPACE_PREFIX}${asPosixPath(rel)}`);
185
+ }
186
+ }
187
+ return Array.from(filenames).sort();
188
+ }
189
+ async deleteArtifact({
190
+ userId,
191
+ sessionId,
192
+ filename
193
+ }) {
194
+ try {
195
+ const artifactDir = getArtifactDir(
196
+ this.rootDir,
197
+ userId,
198
+ sessionId,
199
+ filename
200
+ );
201
+ await fs.rm(artifactDir, { recursive: true, force: true });
202
+ } catch (e) {
203
+ logger.warn(
204
+ `[FileArtifactService] deleteArtifact: Failed to delete artifact ${filename}`,
205
+ e
206
+ );
207
+ }
208
+ }
209
+ async listVersions({
210
+ userId,
211
+ sessionId,
212
+ filename
213
+ }) {
214
+ try {
215
+ const artifactDir = getArtifactDir(
216
+ this.rootDir,
217
+ userId,
218
+ sessionId,
219
+ filename
220
+ );
221
+ return await getArtifactVersionsFromDir(artifactDir);
222
+ } catch (e) {
223
+ logger.warn(
224
+ `[FileArtifactService] listVersions: Failed to list versions for artifact ${filename}`,
225
+ e
226
+ );
227
+ return [];
228
+ }
229
+ }
230
+ async listArtifactVersions({
231
+ userId,
232
+ sessionId,
233
+ filename
234
+ }) {
235
+ try {
236
+ const artifactDir = getArtifactDir(
237
+ this.rootDir,
238
+ userId,
239
+ sessionId,
240
+ filename
241
+ );
242
+ const versions = await getArtifactVersionsFromDir(artifactDir);
243
+ const artifactVersions = [];
244
+ for (const version of versions) {
245
+ const metadataPath = path.join(
246
+ getVersionsDir(artifactDir),
247
+ version.toString(),
248
+ "metadata.json"
249
+ );
250
+ try {
251
+ const metadata = await readMetadata(metadataPath);
252
+ artifactVersions.push(metadata);
253
+ } catch (e) {
254
+ logger.warn(
255
+ `[FileArtifactService] listArtifactVersions: Failed to read artifact version ${version} at ${artifactDir}`,
256
+ e
257
+ );
258
+ }
259
+ }
260
+ return artifactVersions;
261
+ } catch (e) {
262
+ logger.warn(
263
+ `[FileArtifactService] listArtifactVersions: Failed to list artifact versions for userId: ${userId} sessionId: ${sessionId} filename: ${filename}`,
264
+ e
265
+ );
266
+ return [];
267
+ }
268
+ }
269
+ async getArtifactVersion({
270
+ userId,
271
+ sessionId,
272
+ filename,
273
+ version
274
+ }) {
275
+ try {
276
+ const artifactDir = getArtifactDir(
277
+ this.rootDir,
278
+ userId,
279
+ sessionId,
280
+ filename
281
+ );
282
+ const versions = await getArtifactVersionsFromDir(artifactDir);
283
+ if (versions.length === 0) {
284
+ return void 0;
285
+ }
286
+ let versionToRead;
287
+ if (version === void 0) {
288
+ versionToRead = versions[versions.length - 1];
289
+ } else {
290
+ if (!versions.includes(version)) {
291
+ return void 0;
292
+ }
293
+ versionToRead = version;
294
+ }
295
+ const metadataPath = path.join(
296
+ getVersionsDir(artifactDir),
297
+ versionToRead.toString(),
298
+ "metadata.json"
299
+ );
300
+ return await readMetadata(metadataPath);
301
+ } catch (e) {
302
+ logger.warn(
303
+ `[FileArtifactService] getArtifactVersion: Failed to get artifact version for userId: ${userId} sessionId: ${sessionId} filename: ${filename} version: ${version}`,
304
+ e
305
+ );
306
+ return void 0;
307
+ }
308
+ }
309
+ }
310
+ function getUserRoot(rootDir, userId) {
311
+ return path.join(rootDir, "users", userId);
312
+ }
313
+ function isUserScoped(sessionId, filename) {
314
+ return !sessionId || filename.startsWith(USER_NAMESPACE_PREFIX);
315
+ }
316
+ function getUserArtifactsDir(userRoot) {
317
+ return path.join(userRoot, "artifacts");
318
+ }
319
+ function getSessionArtifactsDir(baseRoot, sessionId) {
320
+ return path.join(baseRoot, "sessions", sessionId, "artifacts");
321
+ }
322
+ function getVersionsDir(artifactDir) {
323
+ return path.join(artifactDir, "versions");
324
+ }
325
+ function getArtifactDir(rootDir, userId, sessionId, filename) {
326
+ const userRoot = getUserRoot(rootDir, userId);
327
+ let scopeRoot;
328
+ if (isUserScoped(sessionId, filename)) {
329
+ scopeRoot = getUserArtifactsDir(userRoot);
330
+ } else {
331
+ if (!sessionId) {
332
+ throw new Error(
333
+ "Session ID must be provided for session-scoped artifacts."
334
+ );
335
+ }
336
+ scopeRoot = getSessionArtifactsDir(userRoot, sessionId);
337
+ }
338
+ let cleanFilename = filename;
339
+ if (cleanFilename.startsWith(USER_NAMESPACE_PREFIX)) {
340
+ cleanFilename = cleanFilename.substring(USER_NAMESPACE_PREFIX.length);
341
+ }
342
+ cleanFilename = cleanFilename.trim();
343
+ if (path.isAbsolute(cleanFilename)) {
344
+ throw new Error(`Absolute artifact filename ${filename} is not permitted.`);
345
+ }
346
+ const artifactDir = path.resolve(scopeRoot, cleanFilename);
347
+ const relative = path.relative(scopeRoot, artifactDir);
348
+ if (relative.startsWith("..") || path.isAbsolute(relative)) {
349
+ throw new Error(`Artifact filename ${filename} escapes storage directory.`);
350
+ }
351
+ if (relative === "" || relative === ".") {
352
+ return path.join(scopeRoot, "artifact");
353
+ }
354
+ return artifactDir;
355
+ }
356
+ async function getArtifactVersionsFromDir(artifactDir) {
357
+ const versionsDir = getVersionsDir(artifactDir);
358
+ try {
359
+ const files = await fs.readdir(versionsDir, { withFileTypes: true });
360
+ const versions = files.filter((dirent) => dirent.isDirectory()).map((dirent) => parseInt(dirent.name, 10)).filter((v) => !isNaN(v));
361
+ return versions.sort((a, b) => a - b);
362
+ } catch (e) {
363
+ logger.warn(
364
+ `[FileArtifactService] getArtifactVersionsFromDir: Failed to list artifact versions from ${artifactDir}`,
365
+ e
366
+ );
367
+ return [];
368
+ }
369
+ }
370
+ async function getCanonicalUri(rootDir, userId, sessionId, filename, version) {
371
+ const artifactDir = await getArtifactDir(
372
+ rootDir,
373
+ userId,
374
+ sessionId,
375
+ filename
376
+ );
377
+ const storedFilename = path.basename(artifactDir);
378
+ const versionsDir = getVersionsDir(artifactDir);
379
+ const payloadPath = path.join(
380
+ versionsDir,
381
+ version.toString(),
382
+ storedFilename
383
+ );
384
+ return pathToFileURL(payloadPath).toString();
385
+ }
386
+ async function writeMetadata(metadataPath, metadata) {
387
+ await fs.writeFile(metadataPath, JSON.stringify(metadata, null, 2), "utf-8");
388
+ }
389
+ async function readMetadata(metadataPath) {
390
+ const content = await fs.readFile(metadataPath, "utf-8");
391
+ return JSON.parse(content);
392
+ }
393
+ async function getLatestMetadata(artifactDir) {
394
+ const versions = await getArtifactVersionsFromDir(artifactDir);
395
+ if (versions.length === 0) {
396
+ return void 0;
397
+ }
398
+ const latestVersion = versions[versions.length - 1];
399
+ const metadataPath = path.join(
400
+ getVersionsDir(artifactDir),
401
+ latestVersion.toString(),
402
+ "metadata.json"
403
+ );
404
+ try {
405
+ return await readMetadata(metadataPath);
406
+ } catch (e) {
407
+ logger.warn(
408
+ `[FileArtifactService] getLatestMetadata: Failed to read metadata from ${metadataPath}`,
409
+ e
410
+ );
411
+ return void 0;
412
+ }
413
+ }
414
+ async function* iterateArtifactDirs(dir) {
415
+ try {
416
+ const entries = await fs.readdir(dir, { withFileTypes: true });
417
+ const hasVersions = entries.some(
418
+ (e) => e.isDirectory() && e.name === "versions"
419
+ );
420
+ if (hasVersions) {
421
+ yield dir;
422
+ return;
423
+ }
424
+ for (const entry of entries) {
425
+ if (entry.isDirectory()) {
426
+ const subdir = path.join(dir, entry.name);
427
+ for await (const foundDir of iterateArtifactDirs(subdir)) {
428
+ yield foundDir;
429
+ }
430
+ }
431
+ }
432
+ } catch (_e) {
433
+ }
434
+ }
435
+ function fileUriToPath(uri) {
436
+ try {
437
+ return fileURLToPath(uri);
438
+ } catch (e) {
439
+ logger.warn(
440
+ `[FileArtifactService] fileUriToPath: Failed to convert file URI to path: ${uri}`,
441
+ e
442
+ );
443
+ return void 0;
444
+ }
445
+ }
446
+ function asPosixPath(p) {
447
+ return p.split(path.sep).join("/");
448
+ }
449
+ export {
450
+ FileArtifactService
451
+ };