@google/adk 2.2.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) 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/agents/agent_engine_client.js +172 -0
  5. package/dist/cjs/agents/base_agent.js +20 -4
  6. package/dist/cjs/agents/functions.js +227 -25
  7. package/dist/cjs/agents/instructions.js +6 -2
  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 +194 -107
  11. package/dist/cjs/agents/processors/basic_llm_request_processor.js +1 -1
  12. package/dist/cjs/agents/processors/code_execution_request_processor.js +18 -6
  13. package/dist/cjs/agents/processors/content_processor_utils.js +38 -10
  14. package/dist/cjs/agents/processors/content_request_processor.js +34 -2
  15. package/dist/cjs/agents/processors/instructions_llm_request_processor.js +1 -1
  16. package/dist/cjs/agents/processors/nl_planning_processor.js +12 -3
  17. package/dist/cjs/artifacts/in_memory_artifact_service.js +14 -0
  18. package/dist/cjs/auth/auth_handler.js +37 -5
  19. package/dist/cjs/auth/auth_schemes.js +9 -0
  20. package/dist/cjs/cli/cli_eval.js +532 -0
  21. package/dist/cjs/cli/utils/envs.js +116 -0
  22. package/dist/cjs/cli/utils/evals.js +137 -0
  23. package/dist/cjs/cli/utils/logs.js +150 -0
  24. package/dist/cjs/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  25. package/dist/cjs/code_executors/code_execution_utils.js +48 -16
  26. package/dist/cjs/code_executors/code_executor_context.js +6 -2
  27. package/dist/cjs/code_executors/container_code_executor.js +27 -16
  28. package/dist/cjs/code_executors/unsafe_local_code_executor.js +6 -1
  29. package/dist/cjs/common.js +61 -0
  30. package/dist/cjs/context/summarizers/llm_summarizer.js +12 -10
  31. package/dist/cjs/context/token_based_context_compactor.js +2 -1
  32. package/dist/cjs/errors/input_validation_error.js +3 -2
  33. package/dist/cjs/events/event.js +4 -2
  34. package/dist/cjs/examples/example_util.js +23 -2
  35. package/dist/cjs/flows/llm_flows/audio_transcriber.js +170 -0
  36. package/dist/cjs/index.js +104 -1
  37. package/dist/cjs/integrations/agent_registry/helpers.js +1 -1
  38. package/dist/cjs/models/gemini_llm_connection.js +16 -8
  39. package/dist/cjs/models/google_llm.js +13 -16
  40. package/dist/cjs/models/llm_response.js +9 -1
  41. package/dist/cjs/models/registry.js +1 -1
  42. package/dist/cjs/models/routed_llm.js +9 -8
  43. package/dist/cjs/planners/plan_re_act_planner.js +1 -1
  44. package/dist/cjs/plugins/base_plugin.js +13 -23
  45. package/dist/cjs/runner/in_memory_runner.js +6 -11
  46. package/dist/cjs/runner/runner.js +105 -33
  47. package/dist/cjs/sessions/base_session_service.js +14 -8
  48. package/dist/cjs/sessions/database_session_service.js +50 -28
  49. package/dist/cjs/sessions/db/operations.js +1 -1
  50. package/dist/cjs/sessions/in_memory_session_service.js +3 -1
  51. package/dist/cjs/sessions/stale_session_error.js +59 -0
  52. package/dist/cjs/sessions/state.js +8 -4
  53. package/dist/cjs/sessions/vertex_ai_session_service.js +34 -15
  54. package/dist/cjs/skills/loader.js +52 -18
  55. package/dist/cjs/telemetry/setup.js +0 -1
  56. package/dist/cjs/telemetry/tracing.js +147 -73
  57. package/dist/cjs/tools/agent_tool.js +44 -0
  58. package/dist/cjs/tools/apihub_tool/apihub_toolset.js +137 -0
  59. package/dist/cjs/tools/apihub_tool/clients/apihub_client.js +272 -0
  60. package/dist/cjs/tools/apihub_tool/clients/secret_client.js +148 -0
  61. package/dist/cjs/tools/apihub_tool/clients/service_account.js +48 -0
  62. package/dist/cjs/tools/application_integration_tool/application_integration_toolset.js +263 -0
  63. package/dist/cjs/tools/application_integration_tool/clients/cloud_request.js +133 -0
  64. package/dist/cjs/tools/application_integration_tool/clients/connections_client.js +671 -0
  65. package/dist/cjs/tools/application_integration_tool/clients/integration_client.js +223 -0
  66. package/dist/cjs/tools/application_integration_tool/integration_connector_tool.js +184 -0
  67. package/dist/cjs/tools/example_tool.js +10 -0
  68. package/dist/cjs/tools/function_tool.js +319 -0
  69. package/dist/cjs/tools/google_api_tool/google_api_tool.js +100 -0
  70. package/dist/cjs/tools/google_api_tool/google_api_toolset.js +173 -0
  71. package/dist/cjs/tools/google_api_tool/google_api_toolsets.js +124 -0
  72. package/dist/cjs/tools/google_api_tool/googleapi_to_openapi_converter.js +440 -0
  73. package/dist/cjs/tools/google_api_tool/index.js +61 -0
  74. package/dist/cjs/tools/load_artifacts_tool.js +12 -6
  75. package/dist/cjs/tools/load_memory_tool.js +12 -10
  76. package/dist/cjs/tools/mcp/index.js +12 -0
  77. package/dist/cjs/tools/mcp/mcp_session_manager.js +25 -0
  78. package/dist/cjs/tools/mcp/mcp_toolset.js +1 -14
  79. package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +1 -2
  80. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +46 -2
  81. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +4 -3
  82. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  83. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +20 -6
  84. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/operation_parser.js +17 -6
  85. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +14 -2
  86. package/dist/cjs/tools/openapi_tool/openapi_toolset.js +7 -13
  87. package/dist/cjs/tools/openapi_tool/rest_api_tool.js +17 -1
  88. package/dist/cjs/tools/retrieval/base_retrieval_tool.js +18 -3
  89. package/dist/cjs/tools/toolbox_toolset.js +174 -0
  90. package/dist/cjs/tools/webmcp/webmcp_toolset.js +1 -14
  91. package/dist/cjs/utils/client_labels.js +19 -2
  92. package/dist/cjs/utils/env_aware_utils.js +6 -8
  93. package/dist/cjs/utils/error_utils.js +11 -11
  94. package/dist/cjs/utils/failover_utils.js +1 -1
  95. package/dist/cjs/utils/file_extension_utils.js +11 -0
  96. package/dist/cjs/utils/file_utils.js +2 -24
  97. package/dist/cjs/utils/gemini_schema_util.js +28 -7
  98. package/dist/cjs/utils/json_utils.js +49 -0
  99. package/dist/cjs/utils/live_connection_utils.js +4 -5
  100. package/dist/cjs/utils/logger_node.js +11 -1
  101. package/dist/cjs/utils/object_notation_utils.js +6 -3
  102. package/dist/cjs/utils/output_schema_utils.js +5 -2
  103. package/dist/cjs/utils/streaming_utils.js +12 -12
  104. package/dist/cjs/utils/task.js +1 -1
  105. package/dist/cjs/version.js +1 -1
  106. package/dist/esm/a2a/a2a_remote_agent.js +4 -1
  107. package/dist/esm/a2a/agent_card.js +103 -27
  108. package/dist/esm/a2a/agent_to_a2a.js +5 -2
  109. package/dist/esm/agents/agent_engine_client.js +144 -0
  110. package/dist/esm/agents/base_agent.js +20 -4
  111. package/dist/esm/agents/functions.js +226 -25
  112. package/dist/esm/agents/instructions.js +6 -2
  113. package/dist/esm/agents/invocation_context.js +24 -4
  114. package/dist/esm/agents/langgraph_agent.js +123 -0
  115. package/dist/esm/agents/llm_agent.js +204 -110
  116. package/dist/esm/agents/processors/basic_llm_request_processor.js +1 -1
  117. package/dist/esm/agents/processors/code_execution_request_processor.js +21 -8
  118. package/dist/esm/agents/processors/content_processor_utils.js +38 -10
  119. package/dist/esm/agents/processors/content_request_processor.js +34 -2
  120. package/dist/esm/agents/processors/instructions_llm_request_processor.js +1 -1
  121. package/dist/esm/agents/processors/nl_planning_processor.js +12 -3
  122. package/dist/esm/artifacts/in_memory_artifact_service.js +14 -0
  123. package/dist/esm/auth/auth_handler.js +37 -5
  124. package/dist/esm/auth/auth_schemes.js +8 -0
  125. package/dist/esm/cli/cli_eval.js +510 -0
  126. package/dist/esm/cli/utils/envs.js +78 -0
  127. package/dist/esm/cli/utils/evals.js +109 -0
  128. package/dist/esm/cli/utils/logs.js +111 -0
  129. package/dist/esm/code_executors/agent_engine_sandbox_code_executor.js +7 -2
  130. package/dist/esm/code_executors/code_execution_utils.js +46 -16
  131. package/dist/esm/code_executors/code_executor_context.js +5 -2
  132. package/dist/esm/code_executors/container_code_executor.js +27 -16
  133. package/dist/esm/code_executors/unsafe_local_code_executor.js +6 -1
  134. package/dist/esm/common.js +65 -4
  135. package/dist/esm/context/summarizers/llm_summarizer.js +12 -10
  136. package/dist/esm/context/token_based_context_compactor.js +2 -1
  137. package/dist/esm/errors/input_validation_error.js +3 -2
  138. package/dist/esm/events/event.js +4 -2
  139. package/dist/esm/examples/example_util.js +21 -1
  140. package/dist/esm/flows/llm_flows/audio_transcriber.js +142 -0
  141. package/dist/esm/index.js +114 -2
  142. package/dist/esm/integrations/agent_registry/helpers.js +1 -1
  143. package/dist/esm/models/gemini_llm_connection.js +16 -8
  144. package/dist/esm/models/google_llm.js +14 -17
  145. package/dist/esm/models/llm_response.js +9 -1
  146. package/dist/esm/models/registry.js +1 -1
  147. package/dist/esm/models/routed_llm.js +9 -8
  148. package/dist/esm/planners/plan_re_act_planner.js +1 -1
  149. package/dist/esm/plugins/base_plugin.js +13 -23
  150. package/dist/esm/runner/in_memory_runner.js +6 -11
  151. package/dist/esm/runner/runner.js +105 -33
  152. package/dist/esm/sessions/base_session_service.js +12 -7
  153. package/dist/esm/sessions/database_session_service.js +49 -28
  154. package/dist/esm/sessions/db/operations.js +1 -1
  155. package/dist/esm/sessions/in_memory_session_service.js +3 -1
  156. package/dist/esm/sessions/stale_session_error.js +31 -0
  157. package/dist/esm/sessions/state.js +8 -4
  158. package/dist/esm/sessions/vertex_ai_session_service.js +34 -15
  159. package/dist/esm/skills/loader.js +51 -17
  160. package/dist/esm/telemetry/setup.js +1 -2
  161. package/dist/esm/telemetry/tracing.js +151 -74
  162. package/dist/esm/tools/agent_tool.js +44 -0
  163. package/dist/esm/tools/apihub_tool/apihub_toolset.js +101 -0
  164. package/dist/esm/tools/apihub_tool/clients/apihub_client.js +244 -0
  165. package/dist/esm/tools/apihub_tool/clients/secret_client.js +132 -0
  166. package/dist/esm/tools/apihub_tool/clients/service_account.js +21 -0
  167. package/dist/esm/tools/application_integration_tool/application_integration_toolset.js +245 -0
  168. package/dist/esm/tools/application_integration_tool/clients/cloud_request.js +103 -0
  169. package/dist/esm/tools/application_integration_tool/clients/connections_client.js +629 -0
  170. package/dist/esm/tools/application_integration_tool/clients/integration_client.js +219 -0
  171. package/dist/esm/tools/application_integration_tool/integration_connector_tool.js +158 -0
  172. package/dist/esm/tools/example_tool.js +13 -1
  173. package/dist/esm/tools/function_tool.js +316 -0
  174. package/dist/esm/tools/google_api_tool/google_api_tool.js +73 -0
  175. package/dist/esm/tools/google_api_tool/google_api_toolset.js +146 -0
  176. package/dist/esm/tools/google_api_tool/google_api_toolsets.js +94 -0
  177. package/dist/esm/tools/google_api_tool/googleapi_to_openapi_converter.js +393 -0
  178. package/dist/esm/tools/google_api_tool/index.js +31 -0
  179. package/dist/esm/tools/load_artifacts_tool.js +10 -5
  180. package/dist/esm/tools/load_memory_tool.js +12 -10
  181. package/dist/esm/tools/mcp/index.js +6 -0
  182. package/dist/esm/tools/mcp/mcp_session_manager.js +25 -0
  183. package/dist/esm/tools/mcp/mcp_toolset.js +1 -14
  184. package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +1 -2
  185. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +50 -3
  186. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +4 -3
  187. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +94 -10
  188. package/dist/esm/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +20 -6
  189. package/dist/esm/tools/openapi_tool/openapi_spec_parser/operation_parser.js +17 -6
  190. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -2
  191. package/dist/esm/tools/openapi_tool/openapi_toolset.js +6 -12
  192. package/dist/esm/tools/openapi_tool/rest_api_tool.js +17 -1
  193. package/dist/esm/tools/retrieval/base_retrieval_tool.js +16 -2
  194. package/dist/esm/tools/toolbox_toolset.js +148 -0
  195. package/dist/esm/tools/webmcp/webmcp_toolset.js +1 -14
  196. package/dist/esm/utils/client_labels.js +19 -2
  197. package/dist/esm/utils/env_aware_utils.js +6 -8
  198. package/dist/esm/utils/error_utils.js +9 -10
  199. package/dist/esm/utils/failover_utils.js +1 -1
  200. package/dist/esm/utils/file_extension_utils.js +11 -0
  201. package/dist/esm/utils/file_utils.js +2 -23
  202. package/dist/esm/utils/gemini_schema_util.js +27 -7
  203. package/dist/esm/utils/json_utils.js +21 -0
  204. package/dist/esm/utils/live_connection_utils.js +4 -5
  205. package/dist/esm/utils/logger_node.js +11 -1
  206. package/dist/esm/utils/object_notation_utils.js +4 -2
  207. package/dist/esm/utils/output_schema_utils.js +5 -2
  208. package/dist/esm/utils/streaming_utils.js +11 -11
  209. package/dist/esm/utils/task.js +1 -1
  210. package/dist/esm/version.js +1 -1
  211. package/dist/types/a2a/a2a_remote_agent.d.ts +9 -0
  212. package/dist/types/a2a/agent_card.d.ts +44 -1
  213. package/dist/types/a2a/agent_to_a2a.d.ts +9 -0
  214. package/dist/types/a2a/index.d.ts +1 -0
  215. package/dist/types/agents/agent_engine_client.d.ts +37 -0
  216. package/dist/types/agents/base_agent.d.ts +12 -0
  217. package/dist/types/agents/functions.d.ts +33 -1
  218. package/dist/types/agents/invocation_context.d.ts +20 -1
  219. package/dist/types/agents/langgraph_agent.d.ts +153 -0
  220. package/dist/types/agents/llm_agent.d.ts +5 -1
  221. package/dist/types/agents/processors/content_processor_utils.d.ts +4 -2
  222. package/dist/types/agents/processors/nl_planning_processor.d.ts +2 -1
  223. package/dist/types/auth/auth_handler.d.ts +28 -1
  224. package/dist/types/auth/auth_schemes.d.ts +13 -0
  225. package/dist/types/cli/cli_eval.d.ts +173 -0
  226. package/dist/types/cli/utils/envs.d.ts +23 -0
  227. package/dist/types/cli/utils/evals.d.ts +47 -0
  228. package/dist/types/cli/utils/logs.d.ts +38 -0
  229. package/dist/types/code_executors/code_execution_utils.d.ts +39 -0
  230. package/dist/types/code_executors/code_executor_context.d.ts +1 -0
  231. package/dist/types/code_executors/container_code_executor.d.ts +17 -5
  232. package/dist/types/common.d.ts +24 -5
  233. package/dist/types/errors/input_validation_error.d.ts +2 -1
  234. package/dist/types/events/event.d.ts +4 -0
  235. package/dist/types/examples/example_util.d.ts +17 -0
  236. package/dist/types/flows/llm_flows/audio_transcriber.d.ts +85 -0
  237. package/dist/types/index.d.ts +19 -2
  238. package/dist/types/models/gemini_llm_connection.d.ts +6 -7
  239. package/dist/types/models/routed_llm.d.ts +1 -1
  240. package/dist/types/plugins/base_plugin.d.ts +0 -10
  241. package/dist/types/runner/in_memory_runner.d.ts +2 -1
  242. package/dist/types/runner/runner.d.ts +34 -0
  243. package/dist/types/sessions/base_session_service.d.ts +5 -0
  244. package/dist/types/sessions/database_session_service.d.ts +17 -1
  245. package/dist/types/sessions/stale_session_error.d.ts +21 -0
  246. package/dist/types/sessions/state.d.ts +5 -1
  247. package/dist/types/sessions/vertex_ai_session_service.d.ts +8 -0
  248. package/dist/types/skills/loader.d.ts +3 -2
  249. package/dist/types/skills/skill.d.ts +1 -1
  250. package/dist/types/telemetry/tracing.d.ts +20 -4
  251. package/dist/types/tools/apihub_tool/apihub_toolset.d.ts +115 -0
  252. package/dist/types/tools/apihub_tool/clients/apihub_client.d.ts +142 -0
  253. package/dist/types/tools/apihub_tool/clients/secret_client.d.ts +55 -0
  254. package/dist/types/tools/apihub_tool/clients/service_account.d.ts +11 -0
  255. package/dist/types/tools/application_integration_tool/application_integration_toolset.d.ts +109 -0
  256. package/dist/types/tools/application_integration_tool/clients/cloud_request.d.ts +53 -0
  257. package/dist/types/tools/application_integration_tool/clients/connections_client.d.ts +229 -0
  258. package/dist/types/tools/application_integration_tool/clients/integration_client.d.ts +72 -0
  259. package/dist/types/tools/application_integration_tool/integration_connector_tool.d.ts +70 -0
  260. package/dist/types/tools/base_toolset.d.ts +6 -5
  261. package/dist/types/tools/example_tool.d.ts +10 -0
  262. package/dist/types/tools/function_tool.d.ts +32 -0
  263. package/dist/types/tools/google_api_tool/google_api_tool.d.ts +37 -0
  264. package/dist/types/tools/google_api_tool/google_api_toolset.d.ts +55 -0
  265. package/dist/types/tools/google_api_tool/google_api_toolsets.d.ts +48 -0
  266. package/dist/types/tools/google_api_tool/googleapi_to_openapi_converter.d.ts +165 -0
  267. package/dist/types/tools/google_api_tool/index.d.ts +10 -0
  268. package/dist/types/tools/load_artifacts_tool.d.ts +14 -1
  269. package/dist/types/tools/load_memory_tool.d.ts +23 -1
  270. package/dist/types/tools/mcp/index.d.ts +3 -0
  271. package/dist/types/tools/mcp/mcp_session_manager.d.ts +34 -1
  272. package/dist/types/tools/mcp/mcp_toolset.d.ts +15 -0
  273. package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +19 -0
  274. package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +18 -1
  275. package/dist/types/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.d.ts +12 -0
  276. package/dist/types/tools/openapi_tool/openapi_spec_parser/operation_parser.d.ts +8 -0
  277. package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +2 -1
  278. package/dist/types/tools/openapi_tool/openapi_toolset.d.ts +5 -0
  279. package/dist/types/tools/openapi_tool/rest_api_tool.d.ts +10 -0
  280. package/dist/types/tools/retrieval/base_retrieval_tool.d.ts +15 -0
  281. package/dist/types/tools/toolbox_toolset.d.ts +159 -0
  282. package/dist/types/utils/client_labels.d.ts +2 -1
  283. package/dist/types/utils/env_aware_utils.d.ts +18 -2
  284. package/dist/types/utils/error_utils.d.ts +7 -0
  285. package/dist/types/utils/file_utils.d.ts +6 -1
  286. package/dist/types/utils/gemini_schema_util.d.ts +9 -2
  287. package/dist/types/utils/json_utils.d.ts +11 -0
  288. package/dist/types/utils/object_notation_utils.d.ts +7 -0
  289. package/dist/types/utils/output_schema_utils.d.ts +3 -2
  290. package/dist/types/utils/simple_zod_to_json.d.ts +1 -2
  291. package/dist/types/utils/streaming_utils.d.ts +11 -4
  292. package/dist/types/version.d.ts +1 -1
  293. package/dist/web/index_web.js +117 -110
  294. package/dist/web/index_web.js.map +4 -4
  295. package/package.json +38 -26
  296. package/dist/cjs/models/gemini_continuation.js +0 -232
  297. package/dist/esm/models/gemini_continuation.js +0 -205
  298. package/dist/types/models/gemini_continuation.d.ts +0 -59
@@ -8,6 +8,7 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
8
8
  * SPDX-License-Identifier: Apache-2.0
9
9
  */
10
10
  var _a, _b;
11
+ import { Modality } from "@google/genai";
11
12
  import { context, trace } from "@opentelemetry/api";
12
13
  import { FinishTaskTool } from "../tools/finish_task_tool.js";
13
14
  import { FunctionTool } from "../tools/function_tool.js";
@@ -20,6 +21,8 @@ import {
20
21
  createNewEventId,
21
22
  getFunctionCalls,
22
23
  getFunctionResponses,
24
+ hasTrailingCodeExecutionResult,
25
+ isEvent,
23
26
  isFinalResponse,
24
27
  populateClientFunctionCallId
25
28
  } from "../events/event.js";
@@ -28,13 +31,15 @@ import { isBaseLlm } from "../models/base_llm.js";
28
31
  import { LLMRegistry } from "../models/registry.js";
29
32
  import { isBuiltInPlanner } from "../planners/built_in_planner.js";
30
33
  import { isBaseTool } from "../tools/base_tool.js";
34
+ import { isBaseToolset } from "../tools/base_toolset.js";
31
35
  import { logger } from "../utils/logger.js";
32
36
  import { canUseOutputSchemaWithTools } from "../utils/output_schema_utils.js";
33
37
  import { Context } from "./context.js";
34
38
  import {
35
39
  runAsyncGeneratorWithOtelContext,
36
40
  traceCallLlm,
37
- tracer
41
+ tracer,
42
+ traceSendData
38
43
  } from "../telemetry/tracing.js";
39
44
  import { parseWithSchema } from "../utils/schema.js";
40
45
  import { isZodObject, zodObjectToSchema } from "../utils/simple_zod_to_json.js";
@@ -43,13 +48,18 @@ import {
43
48
  generateAuthEvent,
44
49
  generateRequestConfirmationEvent,
45
50
  getLongRunningFunctionCalls,
46
- handleFunctionCallsAsync
51
+ handleFunctionCallsAsync,
52
+ handleFunctionCallsLive
47
53
  } from "./functions.js";
48
54
  import { AUTH_PREPROCESSOR } from "../auth/auth_preprocessor.js";
49
55
  import { requireAgent } from "./invocation_context.js";
56
+ import { LiveRequestQueue } from "./live_request_queue.js";
50
57
  import { AGENT_TRANSFER_LLM_REQUEST_PROCESSOR } from "./processors/agent_transfer_llm_request_processor.js";
51
58
  import { BASIC_LLM_REQUEST_PROCESSOR } from "./processors/basic_llm_request_processor.js";
52
- import { CODE_EXECUTION_REQUEST_PROCESSOR } from "./processors/code_execution_request_processor.js";
59
+ import {
60
+ CODE_EXECUTION_REQUEST_PROCESSOR,
61
+ responseProcessor as CODE_EXECUTION_RESPONSE_PROCESSOR
62
+ } from "./processors/code_execution_request_processor.js";
53
63
  import { CONTENT_REQUEST_PROCESSOR } from "./processors/content_request_processor.js";
54
64
  import { ContextCompactorRequestProcessor } from "./processors/context_compactor_request_processor.js";
55
65
  import { IDENTITY_LLM_REQUEST_PROCESSOR } from "./processors/identity_llm_request_processor.js";
@@ -112,6 +122,9 @@ function applyLiveRunConfig(runConfig, llmRequest) {
112
122
  liveConfig[k] = runConfig[k];
113
123
  }
114
124
  }
125
+ if (runConfig.supportCfc) {
126
+ liveConfig.responseModalities = [Modality.TEXT];
127
+ }
115
128
  }
116
129
  __name(applyLiveRunConfig, "applyLiveRunConfig");
117
130
  const ADK_AGENT_NAME_LABEL_KEY = "adk_agent_name";
@@ -194,7 +207,9 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
194
207
  }
195
208
  }
196
209
  this.responseProcessors = (_j = config.responseProcessors) != null ? _j : [
197
- NL_PLANNING_RESPONSE_PROCESSOR
210
+ NL_PLANNING_RESPONSE_PROCESSOR,
211
+ // Runs the code blocks that a client-side code executor receives.
212
+ CODE_EXECUTION_RESPONSE_PROCESSOR
198
213
  ];
199
214
  const agentTransferDisabled = this.disallowTransferToParent && this.disallowTransferToPeers && !((_k = this.subAgents) == null ? void 0 : _k.length);
200
215
  if (!agentTransferDisabled) {
@@ -457,14 +472,18 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
457
472
  while (true) {
458
473
  let lastEvent = void 0;
459
474
  let stepHadToolCalls = false;
475
+ let stepTransferred = false;
460
476
  for await (const event of this.runOneStepAsync(context2)) {
461
477
  if ((_a2 = context2.abortSignal) == null ? void 0 : _a2.aborted) {
462
478
  return;
463
479
  }
464
480
  lastEvent = event;
465
- if (getFunctionCalls(event).length > 0 || getFunctionResponses(event).length > 0) {
481
+ if (getFunctionCalls(event).length > 0 || getFunctionResponses(event).length > 0 || !event.partial && hasTrailingCodeExecutionResult(event)) {
466
482
  stepHadToolCalls = true;
467
483
  }
484
+ if (event.author === this.name && event.actions.transferToAgent) {
485
+ stepTransferred = true;
486
+ }
468
487
  this.maybeSaveOutputToState(event);
469
488
  yield event;
470
489
  }
@@ -472,7 +491,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
472
491
  break;
473
492
  }
474
493
  const isEmptyMetadataEvent = lastEvent.author === this.name && !lastEvent.partial && (!((_b2 = lastEvent.content) == null ? void 0 : _b2.parts) || lastEvent.content.parts.length === 0) && isDefaultEventActions(lastEvent.actions);
475
- if (isFinalResponse(lastEvent) && !(isEmptyMetadataEvent && stepHadToolCalls)) {
494
+ if (isFinalResponse(lastEvent) && !(isEmptyMetadataEvent && stepHadToolCalls && !stepTransferred)) {
476
495
  break;
477
496
  }
478
497
  if (lastEvent.partial) {
@@ -512,7 +531,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
512
531
  * already holds the conversation state associated with the handle.
513
532
  */
514
533
  async *runLiveFlow(invocationContext) {
515
- var _a2, _b2, _c, _d;
534
+ var _a2, _b2, _c, _d, _e;
516
535
  if (!invocationContext.liveRequestQueue) {
517
536
  throw new Error("liveRequestQueue is required for LlmAgent.runLiveFlow.");
518
537
  }
@@ -567,16 +586,29 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
567
586
  throw err;
568
587
  }
569
588
  if (llmRequest.contents.length > 0 && !invocationContext.liveSessionResumptionHandle) {
570
- await connection.sendHistory(llmRequest.contents);
589
+ await tracer.startActiveSpan("send_data", async (span) => {
590
+ try {
591
+ await connection.sendHistory(llmRequest.contents);
592
+ traceSendData({
593
+ invocationContext,
594
+ eventId: createNewEventId(),
595
+ data: llmRequest.contents
596
+ });
597
+ } finally {
598
+ span.end();
599
+ }
600
+ });
571
601
  }
572
602
  let sendError;
573
603
  const sendAbort = new AbortController();
574
604
  const combinedAbort = invocationContext.abortSignal ? AbortSignal.any([invocationContext.abortSignal, sendAbort.signal]) : sendAbort.signal;
605
+ (_d = invocationContext.activeStreamingTools) != null ? _d : invocationContext.activeStreamingTools = {};
575
606
  const sendTask = this.runSendLoop(
576
607
  connection,
577
608
  invocationContext.liveRequestQueue,
578
609
  combinedAbort,
579
- invocationContext.abortSignal
610
+ invocationContext.abortSignal,
611
+ invocationContext.activeStreamingTools
580
612
  );
581
613
  sendTask.catch((error) => {
582
614
  logger.error("Error in live send loop:", error);
@@ -608,7 +640,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
608
640
  }
609
641
  }
610
642
  await this.teardownLiveConnection(sendAbort, connection, sendTask);
611
- if ((_d = invocationContext.abortSignal) == null ? void 0 : _d.aborted) {
643
+ if ((_e = invocationContext.abortSignal) == null ? void 0 : _e.aborted) {
612
644
  return;
613
645
  }
614
646
  if (sendError) {
@@ -626,7 +658,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
626
658
  }
627
659
  }
628
660
  async *runLivePreprocess(invocationContext, llmRequest) {
629
- var _a2, _b2;
661
+ var _a2, _b2, _c;
630
662
  for (const processor of this.requestProcessors) {
631
663
  for await (const event of processor.runAsync(
632
664
  invocationContext,
@@ -640,6 +672,12 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
640
672
  }
641
673
  for (const toolUnion of this.tools) {
642
674
  const toolContext = new Context({ invocationContext });
675
+ if (isBaseToolset(toolUnion)) {
676
+ await toolUnion.processLlmRequest(toolContext, llmRequest);
677
+ if ((_b2 = invocationContext.abortSignal) == null ? void 0 : _b2.aborted) {
678
+ return;
679
+ }
680
+ }
643
681
  const tools = (await convertToolUnionToTools(
644
682
  toolUnion,
645
683
  new ReadonlyContext(invocationContext)
@@ -648,13 +686,13 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
648
686
  );
649
687
  for (const tool of tools) {
650
688
  await tool.processLlmRequest({ toolContext, llmRequest });
651
- if ((_b2 = invocationContext.abortSignal) == null ? void 0 : _b2.aborted) {
689
+ if ((_c = invocationContext.abortSignal) == null ? void 0 : _c.aborted) {
652
690
  return;
653
691
  }
654
692
  }
655
693
  }
656
694
  }
657
- async runSendLoop(connection, liveRequestQueue, sendAbortSignal, invocationAbortSignal) {
695
+ async runSendLoop(connection, liveRequestQueue, sendAbortSignal, invocationAbortSignal, activeStreamingTools) {
658
696
  while (true) {
659
697
  if ((sendAbortSignal == null ? void 0 : sendAbortSignal.aborted) || (invocationAbortSignal == null ? void 0 : invocationAbortSignal.aborted)) {
660
698
  return;
@@ -668,6 +706,13 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
668
706
  }
669
707
  throw error;
670
708
  }
709
+ if (activeStreamingTools) {
710
+ for (const activeStreamingTool of Object.values(activeStreamingTools)) {
711
+ if (activeStreamingTool.stream) {
712
+ activeStreamingTool.stream.send(liveRequest);
713
+ }
714
+ }
715
+ }
671
716
  try {
672
717
  await this.dispatchLiveRequest(connection, liveRequest);
673
718
  } catch (error) {
@@ -722,67 +767,74 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
722
767
  }
723
768
  async *runReceiveLoop(invocationContext, connection, llmRequest, sendAbort) {
724
769
  var _a2, _b2, _c, _d;
725
- for await (const llmResponse of connection.receive()) {
726
- if ((_a2 = invocationContext.abortSignal) == null ? void 0 : _a2.aborted) {
727
- return;
728
- }
729
- if (sendAbort.signal.aborted) {
730
- return;
731
- }
732
- if ((_b2 = llmResponse.liveSessionResumptionUpdate) == null ? void 0 : _b2.newHandle) {
733
- invocationContext.liveSessionResumptionHandle = llmResponse.liveSessionResumptionUpdate.newHandle;
734
- }
735
- if (llmResponse.goAway) {
736
- logger.info("Received goAway from live server; triggering reconnect.");
737
- throw new LiveReconnectSignal("goAway");
738
- }
739
- const author = llmResponse.inputTranscription || ((_c = llmResponse.content) == null ? void 0 : _c.role) === "user" ? "user" : this.name;
740
- const modelResponseEvent = createEvent({
741
- invocationId: invocationContext.invocationId,
742
- author,
743
- branch: invocationContext.branch
744
- });
745
- for await (const event of this.postprocessLive(
746
- invocationContext,
747
- llmRequest,
748
- llmResponse,
749
- modelResponseEvent
750
- )) {
751
- yield event;
752
- if (event.content && getFunctionResponses(event).length > 0) {
753
- await connection.sendContent(event.content);
770
+ let receivedAny = true;
771
+ while (receivedAny) {
772
+ receivedAny = false;
773
+ for await (const llmResponse of connection.receive()) {
774
+ receivedAny = true;
775
+ if ((_a2 = invocationContext.abortSignal) == null ? void 0 : _a2.aborted) {
776
+ return;
754
777
  }
755
- const taskCompleted = getFunctionResponses(event).some(
756
- (r) => r.name === "task_completed"
757
- );
758
- if (taskCompleted) {
759
- await sleep(TRANSFER_AGENT_DELAY_MS);
778
+ if (sendAbort.signal.aborted) {
760
779
  return;
761
780
  }
762
- const transferTo = (_d = event.actions) == null ? void 0 : _d.transferToAgent;
763
- if (transferTo) {
764
- await sleep(TRANSFER_AGENT_DELAY_MS);
765
- sendAbort.abort();
766
- await connection.close();
767
- const agent = requireAgent(invocationContext);
768
- const subAgent = agent.rootAgent.findAgent(transferTo);
769
- if (subAgent) {
770
- const previousAgent = invocationContext.agent;
771
- invocationContext.agent = subAgent;
772
- const previousHandle = invocationContext.liveSessionResumptionHandle;
773
- invocationContext.liveSessionResumptionHandle = void 0;
774
- try {
775
- for await (const subEvent of subAgent.runLive(
776
- invocationContext
777
- )) {
778
- yield subEvent;
781
+ if ((_b2 = llmResponse.liveSessionResumptionUpdate) == null ? void 0 : _b2.newHandle) {
782
+ invocationContext.liveSessionResumptionHandle = llmResponse.liveSessionResumptionUpdate.newHandle;
783
+ }
784
+ if (llmResponse.goAway) {
785
+ logger.info(
786
+ "Received goAway from live server; triggering reconnect."
787
+ );
788
+ throw new LiveReconnectSignal("goAway");
789
+ }
790
+ const author = llmResponse.inputTranscription || ((_c = llmResponse.content) == null ? void 0 : _c.role) === "user" ? "user" : this.name;
791
+ const modelResponseEvent = createEvent({
792
+ invocationId: invocationContext.invocationId,
793
+ author,
794
+ branch: invocationContext.branch
795
+ });
796
+ for await (const event of this.postprocessLive(
797
+ invocationContext,
798
+ llmRequest,
799
+ llmResponse,
800
+ modelResponseEvent
801
+ )) {
802
+ yield event;
803
+ if (event.content && getFunctionResponses(event).length > 0) {
804
+ await connection.sendContent(event.content);
805
+ }
806
+ const taskCompleted = getFunctionResponses(event).some(
807
+ (r) => r.name === "task_completed"
808
+ );
809
+ if (taskCompleted) {
810
+ await sleep(TRANSFER_AGENT_DELAY_MS);
811
+ return;
812
+ }
813
+ const transferTo = (_d = event.actions) == null ? void 0 : _d.transferToAgent;
814
+ if (transferTo) {
815
+ await sleep(TRANSFER_AGENT_DELAY_MS);
816
+ sendAbort.abort();
817
+ await connection.close();
818
+ const agent = requireAgent(invocationContext);
819
+ const subAgent = agent.rootAgent.findAgent(transferTo);
820
+ if (subAgent) {
821
+ const previousAgent = invocationContext.agent;
822
+ invocationContext.agent = subAgent;
823
+ const previousHandle = invocationContext.liveSessionResumptionHandle;
824
+ invocationContext.liveSessionResumptionHandle = void 0;
825
+ try {
826
+ for await (const subEvent of subAgent.runLive(
827
+ invocationContext
828
+ )) {
829
+ yield subEvent;
830
+ }
831
+ } finally {
832
+ invocationContext.agent = previousAgent;
833
+ invocationContext.liveSessionResumptionHandle = previousHandle;
779
834
  }
780
- } finally {
781
- invocationContext.agent = previousAgent;
782
- invocationContext.liveSessionResumptionHandle = previousHandle;
783
835
  }
836
+ return;
784
837
  }
785
- return;
786
838
  }
787
839
  }
788
840
  }
@@ -837,7 +889,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
837
889
  if (!functionCalls.length) {
838
890
  return;
839
891
  }
840
- const functionResponseEvent = await handleFunctionCallsAsync({
892
+ const functionResponseEvent = await handleFunctionCallsLive({
841
893
  invocationContext,
842
894
  functionCallEvent: mergedEvent,
843
895
  toolsDict: llmRequest.toolsDict,
@@ -857,7 +909,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
857
909
  yield functionResponseEvent;
858
910
  }
859
911
  async *runOneStepAsync(invocationContext) {
860
- var _a2, _b2, _c;
912
+ var _a2, _b2, _c, _d;
861
913
  const llmRequest = {
862
914
  contents: [],
863
915
  toolsDict: {},
@@ -877,7 +929,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
877
929
  const allTools = [...this.tools];
878
930
  if (this.mode === "task") {
879
931
  allTools.push(this.finishTaskTool);
880
- } else if (this.outputSchema && allTools.length > 0 && !canUseOutputSchemaWithTools(this.canonicalModel.model)) {
932
+ } else if (this.outputSchema && allTools.length > 0 && !canUseOutputSchemaWithTools(this.canonicalModel)) {
881
933
  const setModelResponseTool = new FunctionTool({
882
934
  name: "set_model_response",
883
935
  description: "Call this tool to submit your final response conforming to the output schema. Use this tool only when you have collected all the information and are ready to return the final answer.",
@@ -901,6 +953,12 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
901
953
  invocationContext,
902
954
  eventActions: modelResponseEvent.actions
903
955
  });
956
+ if (isBaseToolset(toolUnion)) {
957
+ await toolUnion.processLlmRequest(toolContext, llmRequest);
958
+ if ((_b2 = invocationContext.abortSignal) == null ? void 0 : _b2.aborted) {
959
+ return;
960
+ }
961
+ }
904
962
  const tools = (await convertToolUnionToTools(
905
963
  toolUnion,
906
964
  new ReadonlyContext(invocationContext)
@@ -909,54 +967,65 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
909
967
  });
910
968
  for (const tool of tools) {
911
969
  await tool.processLlmRequest({ toolContext, llmRequest });
912
- if ((_b2 = invocationContext.abortSignal) == null ? void 0 : _b2.aborted) {
970
+ if ((_c = invocationContext.abortSignal) == null ? void 0 : _c.aborted) {
913
971
  return;
914
972
  }
915
973
  }
916
974
  }
917
- if (invocationContext.endInvocation || ((_c = invocationContext.abortSignal) == null ? void 0 : _c.aborted)) {
975
+ if (invocationContext.endInvocation || ((_d = invocationContext.abortSignal) == null ? void 0 : _d.aborted)) {
918
976
  return;
919
977
  }
920
978
  const span = tracer.startSpan("call_llm");
921
979
  const ctx = trace.setSpan(context.active(), span);
922
- yield* runAsyncGeneratorWithOtelContext(
923
- ctx,
924
- this,
925
- async function* () {
926
- const responsesGenerator = /* @__PURE__ */ __name(async function* () {
927
- var _a3, _b3;
928
- for await (const llmResponse of this.callLlmAsync(
929
- invocationContext,
930
- llmRequest,
931
- modelResponseEvent
932
- )) {
933
- if ((_a3 = invocationContext.abortSignal) == null ? void 0 : _a3.aborted) {
934
- return;
935
- }
936
- for await (const event of this.postprocess(
980
+ try {
981
+ yield* runAsyncGeneratorWithOtelContext(
982
+ ctx,
983
+ this,
984
+ async function* () {
985
+ const responsesGenerator = /* @__PURE__ */ __name(async function* () {
986
+ var _a3, _b3, _c2;
987
+ for await (const llmResponse of this.callLlmAsync(
937
988
  invocationContext,
938
989
  llmRequest,
939
- llmResponse,
940
990
  modelResponseEvent
941
991
  )) {
942
- if ((_b3 = invocationContext.abortSignal) == null ? void 0 : _b3.aborted) {
992
+ if ((_a3 = invocationContext.abortSignal) == null ? void 0 : _a3.aborted) {
943
993
  return;
944
994
  }
945
- modelResponseEvent.id = createNewEventId();
946
- modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
947
- yield event;
995
+ if (((_b3 = invocationContext.runConfig) == null ? void 0 : _b3.supportCfc) && isEvent(llmResponse)) {
996
+ modelResponseEvent.id = createNewEventId();
997
+ modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
998
+ yield llmResponse;
999
+ continue;
1000
+ }
1001
+ for await (const event of this.postprocess(
1002
+ invocationContext,
1003
+ llmRequest,
1004
+ llmResponse,
1005
+ modelResponseEvent
1006
+ )) {
1007
+ if ((_c2 = invocationContext.abortSignal) == null ? void 0 : _c2.aborted) {
1008
+ return;
1009
+ }
1010
+ if (!event.partial) {
1011
+ modelResponseEvent.id = createNewEventId();
1012
+ modelResponseEvent.timestamp = (/* @__PURE__ */ new Date()).getTime();
1013
+ }
1014
+ yield event;
1015
+ }
948
1016
  }
949
- }
950
- }, "responsesGenerator");
951
- yield* this.runAndHandleError(
952
- responsesGenerator.call(this),
953
- invocationContext,
954
- llmRequest,
955
- modelResponseEvent
956
- );
957
- }
958
- );
959
- span.end();
1017
+ }, "responsesGenerator");
1018
+ yield* this.runAndHandleError(
1019
+ responsesGenerator.call(this),
1020
+ invocationContext,
1021
+ llmRequest,
1022
+ modelResponseEvent
1023
+ );
1024
+ }
1025
+ );
1026
+ } finally {
1027
+ span.end();
1028
+ }
960
1029
  }
961
1030
  async *postprocess(invocationContext, llmRequest, llmResponse, modelResponseEvent) {
962
1031
  var _a2, _b2, _c, _d, _e, _f;
@@ -1082,7 +1151,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
1082
1151
  return agentToRun;
1083
1152
  }
1084
1153
  async *callLlmAsync(invocationContext, llmRequest, modelResponseEvent) {
1085
- var _a2, _b2, _c, _d, _e, _f, _g, _h;
1154
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1086
1155
  const beforeModelResponse = await this.handleBeforeModelCallback(
1087
1156
  invocationContext,
1088
1157
  llmRequest,
@@ -1101,14 +1170,39 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
1101
1170
  llmRequest.config.labels[ADK_AGENT_NAME_LABEL_KEY] = this.name;
1102
1171
  }
1103
1172
  const llm = this.canonicalModel;
1173
+ invocationContext.incrementLlmCallCount();
1104
1174
  if ((_e = invocationContext.runConfig) == null ? void 0 : _e.supportCfc) {
1105
- throw new Error("CFC is not yet supported in callLlmAsync");
1175
+ (_f = invocationContext.liveRequestQueue) != null ? _f : invocationContext.liveRequestQueue = new LiveRequestQueue();
1176
+ const liveRequestQueue = invocationContext.liveRequestQueue;
1177
+ try {
1178
+ for await (const llmResponse of this.runLiveFlow(invocationContext)) {
1179
+ if ((_g = invocationContext.abortSignal) == null ? void 0 : _g.aborted) {
1180
+ return;
1181
+ }
1182
+ const alteredLlmResponse = await this.handleAfterModelCallback(
1183
+ invocationContext,
1184
+ llmResponse,
1185
+ modelResponseEvent
1186
+ );
1187
+ if ((_h = invocationContext.abortSignal) == null ? void 0 : _h.aborted) {
1188
+ return;
1189
+ }
1190
+ const effectiveResponse = alteredLlmResponse ? createEvent({ ...llmResponse, ...alteredLlmResponse }) : llmResponse;
1191
+ if (invocationContext.runConfig.streamingMode === StreamingMode.SSE || !effectiveResponse.partial) {
1192
+ yield effectiveResponse;
1193
+ }
1194
+ if (effectiveResponse.turnComplete) {
1195
+ liveRequestQueue.close();
1196
+ }
1197
+ }
1198
+ } finally {
1199
+ liveRequestQueue.close();
1200
+ }
1106
1201
  } else {
1107
- invocationContext.incrementLlmCallCount();
1108
1202
  const responsesGenerator = llm.generateContentAsync(
1109
1203
  llmRequest,
1110
1204
  /* stream= */
1111
- ((_f = invocationContext.runConfig) == null ? void 0 : _f.streamingMode) === StreamingMode.SSE,
1205
+ ((_i = invocationContext.runConfig) == null ? void 0 : _i.streamingMode) === StreamingMode.SSE,
1112
1206
  invocationContext.abortSignal
1113
1207
  );
1114
1208
  for await (const llmResponse of responsesGenerator) {
@@ -1118,7 +1212,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
1118
1212
  llmRequest,
1119
1213
  llmResponse
1120
1214
  });
1121
- if ((_g = invocationContext.abortSignal) == null ? void 0 : _g.aborted) {
1215
+ if ((_j = invocationContext.abortSignal) == null ? void 0 : _j.aborted) {
1122
1216
  return;
1123
1217
  }
1124
1218
  const alteredLlmResponse = await this.handleAfterModelCallback(
@@ -1126,7 +1220,7 @@ const _LlmAgent = class _LlmAgent extends (_b = BaseAgent, _a = LLM_AGENT_SIGNAT
1126
1220
  llmResponse,
1127
1221
  modelResponseEvent
1128
1222
  );
1129
- if ((_h = invocationContext.abortSignal) == null ? void 0 : _h.aborted) {
1223
+ if ((_k = invocationContext.abortSignal) == null ? void 0 : _k.aborted) {
1130
1224
  return;
1131
1225
  }
1132
1226
  yield alteredLlmResponse != null ? alteredLlmResponse : llmResponse;
@@ -28,7 +28,7 @@ const _BasicLlmRequestProcessor = class _BasicLlmRequestProcessor extends BaseLl
28
28
  }
29
29
  llmRequest.model = agent.canonicalModel.model;
30
30
  llmRequest.config = { ...(_a = agent.generateContentConfig) != null ? _a : {} };
31
- if (agent.outputSchema && agent.mode !== "task" && (!((_b = agent.tools) == null ? void 0 : _b.length) || canUseOutputSchemaWithTools(agent.canonicalModel.model))) {
31
+ if (agent.outputSchema && agent.mode !== "task" && (!((_b = agent.tools) == null ? void 0 : _b.length) || canUseOutputSchemaWithTools(agent.canonicalModel))) {
32
32
  setOutputSchema(llmRequest, agent.outputSchema);
33
33
  }
34
34
  if (invocationContext.runConfig) {
@@ -15,13 +15,14 @@ import {
15
15
  buildExecutableCodePart,
16
16
  CodeExecutionLanguage,
17
17
  convertCodeExecutionParts,
18
- extractCodeAndTruncateContent
18
+ extractCodeBlockAndTruncateContent,
19
+ FileContentEncoding
19
20
  } from "../../code_executors/code_execution_utils.js";
20
21
  import { CodeExecutorContext } from "../../code_executors/code_executor_context.js";
21
22
  import { createEvent } from "../../events/event.js";
22
23
  import { createEventActions } from "../../events/event_actions.js";
23
24
  import { State } from "../../sessions/state.js";
24
- import { base64Decode } from "../../utils/env_aware_utils.js";
25
+ import { base64Decode, base64Encode } from "../../utils/env_aware_utils.js";
25
26
  import { requireAgent } from "../invocation_context.js";
26
27
  import { isLlmAgent } from "../llm_agent.js";
27
28
  import {
@@ -62,6 +63,14 @@ const DATA_FILE_UTIL_MAP = {
62
63
  const DATA_FILE_HELPER_LIB = `
63
64
  import pandas as pd
64
65
 
66
+ def crop(s: str, max_chars: int = 64) -> str:
67
+ """Crops a string to max_chars characters."""
68
+ if len(s) <= max_chars:
69
+ return s
70
+ if max_chars >= 3:
71
+ return s[:max_chars - 3] + '...'
72
+ return s[:max_chars]
73
+
65
74
  def explore_df(df: pd.DataFrame) -> None:
66
75
  """Prints some information about a pandas DataFrame."""
67
76
 
@@ -223,7 +232,7 @@ async function* runPostProcessor(invocationContext, llmResponse) {
223
232
  return;
224
233
  }
225
234
  const responseContent = llmResponse.content;
226
- const codeStr = extractCodeAndTruncateContent(
235
+ const { code: codeStr, language } = extractCodeBlockAndTruncateContent(
227
236
  responseContent,
228
237
  codeExecutor.codeBlockDelimiters
229
238
  );
@@ -244,7 +253,7 @@ async function* runPostProcessor(invocationContext, llmResponse) {
244
253
  invocationContext,
245
254
  codeExecutionInput: {
246
255
  code: codeStr,
247
- language: CodeExecutionLanguage.PYTHON,
256
+ language,
248
257
  inputFiles: codeExecutorContext.getInputFiles(),
249
258
  executionId
250
259
  }
@@ -279,14 +288,15 @@ function extractAndReplaceInlineFiles(codeExecutorContext, llmRequest) {
279
288
  continue;
280
289
  }
281
290
  const fileName = `data_${i + 1}_${j + 1}${DATA_FILE_UTIL_MAP[mimeType].extension}`;
282
- part.text = `
283
- Available file: \`${fileName}\`
284
- `;
285
291
  const file = {
286
292
  name: fileName,
287
293
  content: base64Decode(part.inlineData.data),
288
294
  mimeType
289
295
  };
296
+ part.text = `
297
+ Available file: \`${fileName}\`
298
+ `;
299
+ delete part.inlineData;
290
300
  if (!savedFileNames.has(fileName)) {
291
301
  codeExecutorContext.addInputFiles([file]);
292
302
  allInputFiles.push(file);
@@ -330,7 +340,10 @@ async function postProcessCodeExecutionResult(invocationContext, codeExecutorCon
330
340
  const version = await invocationContext.artifactService.saveArtifact({
331
341
  filename: outputFile.name,
332
342
  artifact: {
333
- inlineData: { data: outputFile.content, mimeType: outputFile.mimeType }
343
+ inlineData: {
344
+ data: outputFile.contentEncoding === FileContentEncoding.UTF8 ? base64Encode(outputFile.content) : outputFile.content,
345
+ mimeType: outputFile.mimeType
346
+ }
334
347
  }
335
348
  });
336
349
  eventActions.artifactDelta[outputFile.name] = version;