solace-agent-mesh 0.2.4__py3-none-any.whl → 1.0.2__py3-none-any.whl

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.

Potentially problematic release.


This version of solace-agent-mesh might be problematic. Click here for more details.

Files changed (521) hide show
  1. solace_agent_mesh/__init__.py +5 -0
  2. solace_agent_mesh/agent/adk/adk_llm.txt +93 -0
  3. solace_agent_mesh/agent/adk/app_llm_agent.py +26 -0
  4. solace_agent_mesh/agent/adk/callbacks.py +1716 -0
  5. solace_agent_mesh/agent/adk/filesystem_artifact_service.py +381 -0
  6. solace_agent_mesh/agent/adk/invocation_monitor.py +295 -0
  7. solace_agent_mesh/agent/adk/models/lite_llm.py +872 -0
  8. solace_agent_mesh/agent/adk/models/models_llm.txt +94 -0
  9. solace_agent_mesh/agent/adk/runner.py +357 -0
  10. solace_agent_mesh/agent/adk/services.py +240 -0
  11. solace_agent_mesh/agent/adk/setup.py +751 -0
  12. solace_agent_mesh/agent/adk/stream_parser.py +214 -0
  13. solace_agent_mesh/agent/adk/tool_wrapper.py +139 -0
  14. solace_agent_mesh/agent/agent_llm.txt +41 -0
  15. solace_agent_mesh/agent/protocol/event_handlers.py +1444 -0
  16. solace_agent_mesh/agent/protocol/protocol_llm.txt +21 -0
  17. solace_agent_mesh/agent/sac/app.py +640 -0
  18. solace_agent_mesh/agent/sac/component.py +3496 -0
  19. solace_agent_mesh/agent/sac/patch_adk.py +111 -0
  20. solace_agent_mesh/agent/sac/sac_llm.txt +105 -0
  21. solace_agent_mesh/agent/sac/task_execution_context.py +185 -0
  22. solace_agent_mesh/agent/testing/__init__.py +3 -0
  23. solace_agent_mesh/agent/testing/debug_utils.py +135 -0
  24. solace_agent_mesh/agent/testing/testing_llm.txt +90 -0
  25. solace_agent_mesh/agent/tools/__init__.py +14 -0
  26. solace_agent_mesh/agent/tools/audio_tools.py +1622 -0
  27. solace_agent_mesh/agent/tools/builtin_artifact_tools.py +1954 -0
  28. solace_agent_mesh/agent/tools/builtin_data_analysis_tools.py +238 -0
  29. solace_agent_mesh/agent/tools/general_agent_tools.py +571 -0
  30. solace_agent_mesh/agent/tools/image_tools.py +1184 -0
  31. solace_agent_mesh/agent/tools/peer_agent_tool.py +290 -0
  32. solace_agent_mesh/agent/tools/registry.py +36 -0
  33. solace_agent_mesh/agent/tools/test_tools.py +135 -0
  34. solace_agent_mesh/agent/tools/tool_definition.py +45 -0
  35. solace_agent_mesh/agent/tools/tools_llm.txt +104 -0
  36. solace_agent_mesh/agent/tools/web_tools.py +381 -0
  37. solace_agent_mesh/agent/utils/artifact_helpers.py +927 -0
  38. solace_agent_mesh/agent/utils/config_parser.py +47 -0
  39. solace_agent_mesh/agent/utils/context_helpers.py +60 -0
  40. solace_agent_mesh/agent/utils/utils_llm.txt +153 -0
  41. solace_agent_mesh/assets/docs/404.html +16 -0
  42. solace_agent_mesh/assets/docs/assets/css/styles.906a1503.css +1 -0
  43. solace_agent_mesh/assets/docs/assets/images/Solace_AI_Framework_With_Broker-85f0a306a9bcdd20b390b7a949f6d862.png +0 -0
  44. solace_agent_mesh/assets/docs/assets/images/sac-flows-80d5b603c6aafd33e87945680ce0abf3.png +0 -0
  45. solace_agent_mesh/assets/docs/assets/images/sac_parts_of_a_component-cb3d0424b1d0c17734c5435cca6b4082.png +0 -0
  46. solace_agent_mesh/assets/docs/assets/js/04989206.674a8007.js +1 -0
  47. solace_agent_mesh/assets/docs/assets/js/0e682baa.79f0ab22.js +1 -0
  48. solace_agent_mesh/assets/docs/assets/js/1001.0182a8bd.js +1 -0
  49. solace_agent_mesh/assets/docs/assets/js/1023fc19.015679ca.js +1 -0
  50. solace_agent_mesh/assets/docs/assets/js/1039.0bd46aa1.js +1 -0
  51. solace_agent_mesh/assets/docs/assets/js/149.b797a808.js +1 -0
  52. solace_agent_mesh/assets/docs/assets/js/1523c6b4.91c7bc01.js +1 -0
  53. solace_agent_mesh/assets/docs/assets/js/165.6a39807d.js +2 -0
  54. solace_agent_mesh/assets/docs/assets/js/165.6a39807d.js.LICENSE.txt +9 -0
  55. solace_agent_mesh/assets/docs/assets/js/166ab619.7d97ccaf.js +1 -0
  56. solace_agent_mesh/assets/docs/assets/js/17896441.a5e82f9b.js +2 -0
  57. solace_agent_mesh/assets/docs/assets/js/17896441.a5e82f9b.js.LICENSE.txt +7 -0
  58. solace_agent_mesh/assets/docs/assets/js/1c6e87d2.a8c5ce5a.js +1 -0
  59. solace_agent_mesh/assets/docs/assets/js/2130.ab9fd314.js +1 -0
  60. solace_agent_mesh/assets/docs/assets/js/21ceee5f.614fa8dd.js +1 -0
  61. solace_agent_mesh/assets/docs/assets/js/2237.5e477fc6.js +1 -0
  62. solace_agent_mesh/assets/docs/assets/js/2334.622a6395.js +1 -0
  63. solace_agent_mesh/assets/docs/assets/js/2a9cab12.8909df92.js +1 -0
  64. solace_agent_mesh/assets/docs/assets/js/3219.adc1d663.js +1 -0
  65. solace_agent_mesh/assets/docs/assets/js/332e10b5.7a103f42.js +1 -0
  66. solace_agent_mesh/assets/docs/assets/js/3624.b524e433.js +1 -0
  67. solace_agent_mesh/assets/docs/assets/js/375.708d48db.js +1 -0
  68. solace_agent_mesh/assets/docs/assets/js/3834.b6cd790e.js +1 -0
  69. solace_agent_mesh/assets/docs/assets/js/3d406171.f722eaf5.js +1 -0
  70. solace_agent_mesh/assets/docs/assets/js/4250.95455b28.js +1 -0
  71. solace_agent_mesh/assets/docs/assets/js/42b3f8d8.36090198.js +1 -0
  72. solace_agent_mesh/assets/docs/assets/js/4356.d169ab5b.js +1 -0
  73. solace_agent_mesh/assets/docs/assets/js/442a8107.5ba94b65.js +1 -0
  74. solace_agent_mesh/assets/docs/assets/js/4458.518e66fa.js +1 -0
  75. solace_agent_mesh/assets/docs/assets/js/4488.c7cc3442.js +1 -0
  76. solace_agent_mesh/assets/docs/assets/js/4494.6ee23046.js +1 -0
  77. solace_agent_mesh/assets/docs/assets/js/4855.fc4444b6.js +1 -0
  78. solace_agent_mesh/assets/docs/assets/js/4866.22daefc0.js +1 -0
  79. solace_agent_mesh/assets/docs/assets/js/4950.ca4caeda.js +1 -0
  80. solace_agent_mesh/assets/docs/assets/js/4c2787c2.66ee00e9.js +1 -0
  81. solace_agent_mesh/assets/docs/assets/js/5388.7a136447.js +1 -0
  82. solace_agent_mesh/assets/docs/assets/js/55f47984.c484bf96.js +1 -0
  83. solace_agent_mesh/assets/docs/assets/js/5607.081356f8.js +1 -0
  84. solace_agent_mesh/assets/docs/assets/js/5864.b0d0e9de.js +1 -0
  85. solace_agent_mesh/assets/docs/assets/js/5b4258a4.bda20761.js +1 -0
  86. solace_agent_mesh/assets/docs/assets/js/5e95c892.558d5167.js +1 -0
  87. solace_agent_mesh/assets/docs/assets/js/6143.0a1464c9.js +1 -0
  88. solace_agent_mesh/assets/docs/assets/js/6395.e9c73649.js +1 -0
  89. solace_agent_mesh/assets/docs/assets/js/6796.51d2c9b7.js +1 -0
  90. solace_agent_mesh/assets/docs/assets/js/6976.379be23b.js +1 -0
  91. solace_agent_mesh/assets/docs/assets/js/6978.ee0b945c.js +1 -0
  92. solace_agent_mesh/assets/docs/assets/js/7040.cb436723.js +1 -0
  93. solace_agent_mesh/assets/docs/assets/js/7195.412f418a.js +1 -0
  94. solace_agent_mesh/assets/docs/assets/js/7280.3fb73bdb.js +1 -0
  95. solace_agent_mesh/assets/docs/assets/js/768e31b0.a12673db.js +1 -0
  96. solace_agent_mesh/assets/docs/assets/js/7845.e33e7c4c.js +1 -0
  97. solace_agent_mesh/assets/docs/assets/js/7900.69516146.js +1 -0
  98. solace_agent_mesh/assets/docs/assets/js/8356.8a379c04.js +1 -0
  99. solace_agent_mesh/assets/docs/assets/js/85387663.6bf41934.js +1 -0
  100. solace_agent_mesh/assets/docs/assets/js/8567.4732c6b7.js +1 -0
  101. solace_agent_mesh/assets/docs/assets/js/8573.cb04eda5.js +1 -0
  102. solace_agent_mesh/assets/docs/assets/js/8577.1d54e766.js +1 -0
  103. solace_agent_mesh/assets/docs/assets/js/8591.d7c16be6.js +2 -0
  104. solace_agent_mesh/assets/docs/assets/js/8591.d7c16be6.js.LICENSE.txt +61 -0
  105. solace_agent_mesh/assets/docs/assets/js/8709.7ecd4047.js +1 -0
  106. solace_agent_mesh/assets/docs/assets/js/8731.49e930c2.js +1 -0
  107. solace_agent_mesh/assets/docs/assets/js/8908.f9d1b506.js +1 -0
  108. solace_agent_mesh/assets/docs/assets/js/9157.b4093d07.js +1 -0
  109. solace_agent_mesh/assets/docs/assets/js/9278.a4fd875d.js +1 -0
  110. solace_agent_mesh/assets/docs/assets/js/945fb41e.74d728aa.js +1 -0
  111. solace_agent_mesh/assets/docs/assets/js/9616.b75c2f6d.js +1 -0
  112. solace_agent_mesh/assets/docs/assets/js/9793.c6d16376.js +1 -0
  113. solace_agent_mesh/assets/docs/assets/js/9eff14a2.1bf8f61c.js +1 -0
  114. solace_agent_mesh/assets/docs/assets/js/a3a92b25.26ca071f.js +1 -0
  115. solace_agent_mesh/assets/docs/assets/js/a7bd4aaa.2204d2f7.js +1 -0
  116. solace_agent_mesh/assets/docs/assets/js/a94703ab.0438dbc2.js +1 -0
  117. solace_agent_mesh/assets/docs/assets/js/aba21aa0.c42a534c.js +1 -0
  118. solace_agent_mesh/assets/docs/assets/js/aba87c2f.d3e2dcc3.js +1 -0
  119. solace_agent_mesh/assets/docs/assets/js/ae4415af.8e279b5d.js +1 -0
  120. solace_agent_mesh/assets/docs/assets/js/b7006a3a.40b10c9d.js +1 -0
  121. solace_agent_mesh/assets/docs/assets/js/bac0be12.f50d9bac.js +1 -0
  122. solace_agent_mesh/assets/docs/assets/js/bb2ef573.207e6990.js +1 -0
  123. solace_agent_mesh/assets/docs/assets/js/c2c06897.63b76e9e.js +1 -0
  124. solace_agent_mesh/assets/docs/assets/js/cc969b05.954186d4.js +1 -0
  125. solace_agent_mesh/assets/docs/assets/js/cd3d4052.ca6eed8c.js +1 -0
  126. solace_agent_mesh/assets/docs/assets/js/ced92a13.fb92e7ca.js +1 -0
  127. solace_agent_mesh/assets/docs/assets/js/cee5d587.f5b73ca1.js +1 -0
  128. solace_agent_mesh/assets/docs/assets/js/f284c35a.ecc3d195.js +1 -0
  129. solace_agent_mesh/assets/docs/assets/js/f897a61a.f8c53b0f.js +1 -0
  130. solace_agent_mesh/assets/docs/assets/js/fbfa3e75.aca209c9.js +1 -0
  131. solace_agent_mesh/assets/docs/assets/js/main.c6286d7c.js +2 -0
  132. solace_agent_mesh/assets/docs/assets/js/main.c6286d7c.js.LICENSE.txt +81 -0
  133. solace_agent_mesh/assets/docs/assets/js/runtime~main.d5133813.js +1 -0
  134. solace_agent_mesh/assets/docs/docs/documentation/concepts/agents/index.html +128 -0
  135. solace_agent_mesh/assets/docs/docs/documentation/concepts/architecture/index.html +91 -0
  136. solace_agent_mesh/assets/docs/docs/documentation/concepts/cli/index.html +201 -0
  137. solace_agent_mesh/assets/docs/docs/documentation/concepts/gateways/index.html +91 -0
  138. solace_agent_mesh/assets/docs/docs/documentation/concepts/orchestrator/index.html +55 -0
  139. solace_agent_mesh/assets/docs/docs/documentation/concepts/plugins/index.html +82 -0
  140. solace_agent_mesh/assets/docs/docs/documentation/deployment/debugging/index.html +77 -0
  141. solace_agent_mesh/assets/docs/docs/documentation/deployment/deploy/index.html +48 -0
  142. solace_agent_mesh/assets/docs/docs/documentation/deployment/observability/index.html +54 -0
  143. solace_agent_mesh/assets/docs/docs/documentation/enterprise/index.html +17 -0
  144. solace_agent_mesh/assets/docs/docs/documentation/getting-started/component-overview/index.html +45 -0
  145. solace_agent_mesh/assets/docs/docs/documentation/getting-started/installation/index.html +76 -0
  146. solace_agent_mesh/assets/docs/docs/documentation/getting-started/introduction/index.html +150 -0
  147. solace_agent_mesh/assets/docs/docs/documentation/getting-started/quick-start/index.html +54 -0
  148. solace_agent_mesh/assets/docs/docs/documentation/tutorials/bedrock-agents/index.html +267 -0
  149. solace_agent_mesh/assets/docs/docs/documentation/tutorials/custom-agent/index.html +136 -0
  150. solace_agent_mesh/assets/docs/docs/documentation/tutorials/event-mesh-gateway/index.html +116 -0
  151. solace_agent_mesh/assets/docs/docs/documentation/tutorials/mcp-integration/index.html +80 -0
  152. solace_agent_mesh/assets/docs/docs/documentation/tutorials/mongodb-integration/index.html +164 -0
  153. solace_agent_mesh/assets/docs/docs/documentation/tutorials/rest-gateway/index.html +57 -0
  154. solace_agent_mesh/assets/docs/docs/documentation/tutorials/slack-integration/index.html +72 -0
  155. solace_agent_mesh/assets/docs/docs/documentation/tutorials/sql-database/index.html +102 -0
  156. solace_agent_mesh/assets/docs/docs/documentation/user-guide/builtin-tools/artifact-management/index.html +99 -0
  157. solace_agent_mesh/assets/docs/docs/documentation/user-guide/builtin-tools/audio-tools/index.html +90 -0
  158. solace_agent_mesh/assets/docs/docs/documentation/user-guide/builtin-tools/data-analysis-tools/index.html +107 -0
  159. solace_agent_mesh/assets/docs/docs/documentation/user-guide/builtin-tools/embeds/index.html +152 -0
  160. solace_agent_mesh/assets/docs/docs/documentation/user-guide/builtin-tools/index.html +103 -0
  161. solace_agent_mesh/assets/docs/docs/documentation/user-guide/create-agents/index.html +170 -0
  162. solace_agent_mesh/assets/docs/docs/documentation/user-guide/create-gateways/index.html +200 -0
  163. solace_agent_mesh/assets/docs/docs/documentation/user-guide/creating-service-providers/index.html +54 -0
  164. solace_agent_mesh/assets/docs/docs/documentation/user-guide/solace-ai-connector/index.html +69 -0
  165. solace_agent_mesh/assets/docs/docs/documentation/user-guide/structure/index.html +59 -0
  166. solace_agent_mesh/assets/docs/img/Solace_AI_Framework_README.png +0 -0
  167. solace_agent_mesh/assets/docs/img/Solace_AI_Framework_With_Broker.png +0 -0
  168. solace_agent_mesh/assets/docs/img/logo.png +0 -0
  169. solace_agent_mesh/assets/docs/img/sac-flows.png +0 -0
  170. solace_agent_mesh/assets/docs/img/sac_parts_of_a_component.png +0 -0
  171. solace_agent_mesh/assets/docs/img/solace-logo.png +0 -0
  172. solace_agent_mesh/assets/docs/lunr-index-1754075282978.json +1 -0
  173. solace_agent_mesh/assets/docs/lunr-index.json +1 -0
  174. solace_agent_mesh/assets/docs/search-doc-1754075282978.json +1 -0
  175. solace_agent_mesh/assets/docs/search-doc.json +1 -0
  176. solace_agent_mesh/assets/docs/sitemap.xml +1 -0
  177. solace_agent_mesh/cli/__init__.py +1 -1
  178. solace_agent_mesh/cli/commands/add_cmd/__init__.py +15 -0
  179. solace_agent_mesh/cli/commands/add_cmd/add_cmd_llm.txt +250 -0
  180. solace_agent_mesh/cli/commands/add_cmd/agent_cmd.py +659 -0
  181. solace_agent_mesh/cli/commands/add_cmd/gateway_cmd.py +322 -0
  182. solace_agent_mesh/cli/commands/add_cmd/web_add_agent_step.py +93 -0
  183. solace_agent_mesh/cli/commands/add_cmd/web_add_gateway_step.py +118 -0
  184. solace_agent_mesh/cli/commands/docs_cmd.py +57 -0
  185. solace_agent_mesh/cli/commands/eval_cmd.py +64 -0
  186. solace_agent_mesh/cli/commands/init_cmd/__init__.py +404 -0
  187. solace_agent_mesh/cli/commands/init_cmd/broker_step.py +201 -0
  188. solace_agent_mesh/cli/commands/init_cmd/directory_step.py +28 -0
  189. solace_agent_mesh/cli/commands/init_cmd/env_step.py +205 -0
  190. solace_agent_mesh/cli/commands/init_cmd/init_cmd_llm.txt +365 -0
  191. solace_agent_mesh/cli/commands/init_cmd/orchestrator_step.py +407 -0
  192. solace_agent_mesh/cli/commands/init_cmd/project_files_step.py +38 -0
  193. solace_agent_mesh/cli/commands/init_cmd/web_init_step.py +110 -0
  194. solace_agent_mesh/cli/commands/init_cmd/webui_gateway_step.py +183 -0
  195. solace_agent_mesh/cli/commands/plugin_cmd/__init__.py +18 -0
  196. solace_agent_mesh/cli/commands/plugin_cmd/add_cmd.py +372 -0
  197. solace_agent_mesh/cli/commands/plugin_cmd/build_cmd.py +86 -0
  198. solace_agent_mesh/cli/commands/plugin_cmd/catalog_cmd.py +139 -0
  199. solace_agent_mesh/cli/commands/plugin_cmd/create_cmd.py +309 -0
  200. solace_agent_mesh/cli/commands/plugin_cmd/official_registry.py +175 -0
  201. solace_agent_mesh/cli/commands/plugin_cmd/plugin_cmd_llm.txt +305 -0
  202. solace_agent_mesh/cli/commands/run_cmd.py +158 -0
  203. solace_agent_mesh/cli/main.py +17 -294
  204. solace_agent_mesh/cli/utils.py +135 -204
  205. solace_agent_mesh/client/webui/frontend/static/assets/authCallback-DvlO62me.js +1 -0
  206. solace_agent_mesh/client/webui/frontend/static/assets/client-bp6u3qVZ.js +49 -0
  207. solace_agent_mesh/client/webui/frontend/static/assets/favicon-BLgzUch9.ico +0 -0
  208. solace_agent_mesh/client/webui/frontend/static/assets/main-D11Lmy9p.css +1 -0
  209. solace_agent_mesh/client/webui/frontend/static/assets/main-Gfk3BYn5.js +663 -0
  210. solace_agent_mesh/client/webui/frontend/static/auth-callback.html +14 -0
  211. solace_agent_mesh/client/webui/frontend/static/index.html +15 -0
  212. solace_agent_mesh/common/__init__.py +1 -0
  213. solace_agent_mesh/common/a2a_protocol.py +564 -0
  214. solace_agent_mesh/common/agent_registry.py +42 -0
  215. solace_agent_mesh/common/client/__init__.py +4 -0
  216. solace_agent_mesh/common/client/card_resolver.py +21 -0
  217. solace_agent_mesh/common/client/client.py +85 -0
  218. solace_agent_mesh/common/client/client_llm.txt +133 -0
  219. solace_agent_mesh/common/common_llm.txt +144 -0
  220. solace_agent_mesh/common/constants.py +1 -14
  221. solace_agent_mesh/common/middleware/__init__.py +12 -0
  222. solace_agent_mesh/common/middleware/config_resolver.py +130 -0
  223. solace_agent_mesh/common/middleware/middleware_llm.txt +174 -0
  224. solace_agent_mesh/common/middleware/registry.py +125 -0
  225. solace_agent_mesh/common/server/__init__.py +4 -0
  226. solace_agent_mesh/common/server/server.py +122 -0
  227. solace_agent_mesh/common/server/server_llm.txt +169 -0
  228. solace_agent_mesh/common/server/task_manager.py +291 -0
  229. solace_agent_mesh/common/server/utils.py +28 -0
  230. solace_agent_mesh/common/services/__init__.py +4 -0
  231. solace_agent_mesh/common/services/employee_service.py +162 -0
  232. solace_agent_mesh/common/services/identity_service.py +129 -0
  233. solace_agent_mesh/common/services/providers/__init__.py +4 -0
  234. solace_agent_mesh/common/services/providers/local_file_identity_service.py +148 -0
  235. solace_agent_mesh/common/services/providers/providers_llm.txt +113 -0
  236. solace_agent_mesh/common/services/services_llm.txt +132 -0
  237. solace_agent_mesh/common/types.py +411 -0
  238. solace_agent_mesh/common/utils/__init__.py +7 -0
  239. solace_agent_mesh/common/utils/asyncio_macos_fix.py +86 -0
  240. solace_agent_mesh/common/utils/embeds/__init__.py +33 -0
  241. solace_agent_mesh/common/utils/embeds/constants.py +55 -0
  242. solace_agent_mesh/common/utils/embeds/converter.py +452 -0
  243. solace_agent_mesh/common/utils/embeds/embeds_llm.txt +124 -0
  244. solace_agent_mesh/common/utils/embeds/evaluators.py +394 -0
  245. solace_agent_mesh/common/utils/embeds/modifiers.py +816 -0
  246. solace_agent_mesh/common/utils/embeds/resolver.py +865 -0
  247. solace_agent_mesh/common/utils/embeds/types.py +14 -0
  248. solace_agent_mesh/common/utils/in_memory_cache.py +108 -0
  249. solace_agent_mesh/common/utils/initializer.py +51 -0
  250. solace_agent_mesh/common/utils/log_formatters.py +44 -0
  251. solace_agent_mesh/common/utils/mime_helpers.py +106 -0
  252. solace_agent_mesh/common/utils/push_notification_auth.py +134 -0
  253. solace_agent_mesh/common/utils/utils_llm.txt +67 -0
  254. solace_agent_mesh/config_portal/backend/common.py +66 -24
  255. solace_agent_mesh/config_portal/backend/plugin_catalog/constants.py +24 -0
  256. solace_agent_mesh/config_portal/backend/plugin_catalog/models.py +49 -0
  257. solace_agent_mesh/config_portal/backend/plugin_catalog/registry_manager.py +164 -0
  258. solace_agent_mesh/config_portal/backend/plugin_catalog/scraper.py +521 -0
  259. solace_agent_mesh/config_portal/backend/plugin_catalog_server.py +217 -0
  260. solace_agent_mesh/config_portal/backend/server.py +551 -181
  261. solace_agent_mesh/config_portal/frontend/static/client/assets/_index-_7yox_eh.js +48 -0
  262. solace_agent_mesh/config_portal/frontend/static/client/assets/components-B7lKcHVY.js +140 -0
  263. solace_agent_mesh/config_portal/frontend/static/client/assets/{entry.client-DX1misIU.js → entry.client-CEumGClk.js} +3 -3
  264. solace_agent_mesh/config_portal/frontend/static/client/assets/index-DSo1AH_7.js +68 -0
  265. solace_agent_mesh/config_portal/frontend/static/client/assets/manifest-e5c3acfe.js +1 -0
  266. solace_agent_mesh/config_portal/frontend/static/client/assets/{root-BApq5dPK.js → root-C4XmHinv.js} +2 -2
  267. solace_agent_mesh/config_portal/frontend/static/client/assets/root-DxRwaWiE.css +1 -0
  268. solace_agent_mesh/config_portal/frontend/static/client/index.html +3 -3
  269. solace_agent_mesh/core_a2a/__init__.py +1 -0
  270. solace_agent_mesh/core_a2a/core_a2a_llm.txt +88 -0
  271. solace_agent_mesh/core_a2a/service.py +331 -0
  272. solace_agent_mesh/evaluation/config_loader.py +657 -0
  273. solace_agent_mesh/evaluation/evaluator.py +667 -0
  274. solace_agent_mesh/evaluation/message_organizer.py +568 -0
  275. solace_agent_mesh/evaluation/report/benchmark_info.html +35 -0
  276. solace_agent_mesh/evaluation/report/chart_section.html +141 -0
  277. solace_agent_mesh/evaluation/report/detailed_breakdown.html +28 -0
  278. solace_agent_mesh/evaluation/report/modal.html +59 -0
  279. solace_agent_mesh/evaluation/report/modal_chart_functions.js +411 -0
  280. solace_agent_mesh/evaluation/report/modal_script.js +296 -0
  281. solace_agent_mesh/evaluation/report/modal_styles.css +340 -0
  282. solace_agent_mesh/evaluation/report/performance_metrics_styles.css +93 -0
  283. solace_agent_mesh/evaluation/report/templates/footer.html +2 -0
  284. solace_agent_mesh/evaluation/report/templates/header.html +340 -0
  285. solace_agent_mesh/evaluation/report_data_processor.py +972 -0
  286. solace_agent_mesh/evaluation/report_generator.py +613 -0
  287. solace_agent_mesh/evaluation/run.py +613 -0
  288. solace_agent_mesh/evaluation/subscriber.py +872 -0
  289. solace_agent_mesh/evaluation/summary_builder.py +775 -0
  290. solace_agent_mesh/evaluation/test_case_loader.py +714 -0
  291. solace_agent_mesh/gateway/base/__init__.py +1 -0
  292. solace_agent_mesh/gateway/base/app.py +266 -0
  293. solace_agent_mesh/gateway/base/base_llm.txt +119 -0
  294. solace_agent_mesh/gateway/base/component.py +1542 -0
  295. solace_agent_mesh/gateway/base/task_context.py +74 -0
  296. solace_agent_mesh/gateway/gateway_llm.txt +125 -0
  297. solace_agent_mesh/gateway/http_sse/app.py +190 -0
  298. solace_agent_mesh/gateway/http_sse/component.py +1602 -0
  299. solace_agent_mesh/gateway/http_sse/components/__init__.py +7 -0
  300. solace_agent_mesh/gateway/http_sse/components/components_llm.txt +65 -0
  301. solace_agent_mesh/gateway/http_sse/components/visualization_forwarder_component.py +108 -0
  302. solace_agent_mesh/gateway/http_sse/dependencies.py +316 -0
  303. solace_agent_mesh/gateway/http_sse/http_sse_llm.txt +63 -0
  304. solace_agent_mesh/gateway/http_sse/main.py +442 -0
  305. solace_agent_mesh/gateway/http_sse/routers/__init__.py +4 -0
  306. solace_agent_mesh/gateway/http_sse/routers/agents.py +41 -0
  307. solace_agent_mesh/gateway/http_sse/routers/artifacts.py +827 -0
  308. solace_agent_mesh/gateway/http_sse/routers/auth.py +212 -0
  309. solace_agent_mesh/gateway/http_sse/routers/config.py +55 -0
  310. solace_agent_mesh/gateway/http_sse/routers/people.py +69 -0
  311. solace_agent_mesh/gateway/http_sse/routers/routers_llm.txt +37 -0
  312. solace_agent_mesh/gateway/http_sse/routers/sessions.py +80 -0
  313. solace_agent_mesh/gateway/http_sse/routers/sse.py +138 -0
  314. solace_agent_mesh/gateway/http_sse/routers/tasks.py +294 -0
  315. solace_agent_mesh/gateway/http_sse/routers/users.py +59 -0
  316. solace_agent_mesh/gateway/http_sse/routers/visualization.py +1131 -0
  317. solace_agent_mesh/gateway/http_sse/services/__init__.py +4 -0
  318. solace_agent_mesh/gateway/http_sse/services/agent_service.py +69 -0
  319. solace_agent_mesh/gateway/http_sse/services/people_service.py +158 -0
  320. solace_agent_mesh/gateway/http_sse/services/services_llm.txt +179 -0
  321. solace_agent_mesh/gateway/http_sse/services/task_service.py +121 -0
  322. solace_agent_mesh/gateway/http_sse/session_manager.py +187 -0
  323. solace_agent_mesh/gateway/http_sse/sse_manager.py +328 -0
  324. solace_agent_mesh/llm.txt +228 -0
  325. solace_agent_mesh/llm_detail.txt +2835 -0
  326. solace_agent_mesh/templates/agent_template.yaml +53 -0
  327. solace_agent_mesh/templates/eval_backend_template.yaml +54 -0
  328. solace_agent_mesh/templates/gateway_app_template.py +73 -0
  329. solace_agent_mesh/templates/gateway_component_template.py +431 -0
  330. solace_agent_mesh/templates/gateway_config_template.yaml +43 -0
  331. solace_agent_mesh/templates/logging_config_template.ini +64 -0
  332. solace_agent_mesh/templates/main_orchestrator.yaml +55 -0
  333. solace_agent_mesh/templates/plugin_agent_config_template.yaml +122 -0
  334. solace_agent_mesh/templates/plugin_custom_config_template.yaml +27 -0
  335. solace_agent_mesh/templates/plugin_custom_template.py +10 -0
  336. solace_agent_mesh/templates/plugin_gateway_config_template.yaml +63 -0
  337. solace_agent_mesh/templates/plugin_pyproject_template.toml +33 -0
  338. solace_agent_mesh/templates/plugin_readme_template.md +34 -0
  339. solace_agent_mesh/templates/plugin_tools_template.py +224 -0
  340. solace_agent_mesh/templates/shared_config.yaml +66 -0
  341. solace_agent_mesh/templates/templates_llm.txt +147 -0
  342. solace_agent_mesh/templates/webui.yaml +53 -0
  343. solace_agent_mesh-1.0.2.dist-info/METADATA +432 -0
  344. solace_agent_mesh-1.0.2.dist-info/RECORD +361 -0
  345. solace_agent_mesh-1.0.2.dist-info/entry_points.txt +3 -0
  346. {solace_agent_mesh-0.2.4.dist-info → solace_agent_mesh-1.0.2.dist-info}/licenses/LICENSE +1 -1
  347. solace_agent_mesh/agents/base_agent_component.py +0 -256
  348. solace_agent_mesh/agents/global/actions/agent_state_change.py +0 -54
  349. solace_agent_mesh/agents/global/actions/clear_history.py +0 -32
  350. solace_agent_mesh/agents/global/actions/convert_file_to_markdown.py +0 -160
  351. solace_agent_mesh/agents/global/actions/create_file.py +0 -70
  352. solace_agent_mesh/agents/global/actions/error_action.py +0 -45
  353. solace_agent_mesh/agents/global/actions/plantuml_diagram.py +0 -163
  354. solace_agent_mesh/agents/global/actions/plotly_graph.py +0 -152
  355. solace_agent_mesh/agents/global/actions/retrieve_file.py +0 -51
  356. solace_agent_mesh/agents/global/global_agent_component.py +0 -38
  357. solace_agent_mesh/agents/image_processing/actions/create_image.py +0 -75
  358. solace_agent_mesh/agents/image_processing/actions/describe_image.py +0 -115
  359. solace_agent_mesh/agents/image_processing/image_processing_agent_component.py +0 -23
  360. solace_agent_mesh/agents/slack/__init__.py +0 -1
  361. solace_agent_mesh/agents/slack/actions/__init__.py +0 -1
  362. solace_agent_mesh/agents/slack/actions/post_message.py +0 -177
  363. solace_agent_mesh/agents/slack/slack_agent_component.py +0 -59
  364. solace_agent_mesh/agents/web_request/actions/do_image_search.py +0 -84
  365. solace_agent_mesh/agents/web_request/actions/do_news_search.py +0 -47
  366. solace_agent_mesh/agents/web_request/actions/do_suggestion_search.py +0 -34
  367. solace_agent_mesh/agents/web_request/actions/do_web_request.py +0 -135
  368. solace_agent_mesh/agents/web_request/actions/download_file.py +0 -69
  369. solace_agent_mesh/agents/web_request/web_request_agent_component.py +0 -33
  370. solace_agent_mesh/assets/web-visualizer/assets/index-D0qORgkg.css +0 -1
  371. solace_agent_mesh/assets/web-visualizer/assets/index-DnDr1pnu.js +0 -109
  372. solace_agent_mesh/assets/web-visualizer/index.html +0 -14
  373. solace_agent_mesh/assets/web-visualizer/vite.svg +0 -1
  374. solace_agent_mesh/cli/commands/add/__init__.py +0 -3
  375. solace_agent_mesh/cli/commands/add/add.py +0 -88
  376. solace_agent_mesh/cli/commands/add/agent.py +0 -110
  377. solace_agent_mesh/cli/commands/add/copy_from_plugin.py +0 -92
  378. solace_agent_mesh/cli/commands/add/gateway.py +0 -374
  379. solace_agent_mesh/cli/commands/build.py +0 -670
  380. solace_agent_mesh/cli/commands/chat/__init__.py +0 -3
  381. solace_agent_mesh/cli/commands/chat/chat.py +0 -361
  382. solace_agent_mesh/cli/commands/config.py +0 -29
  383. solace_agent_mesh/cli/commands/init/__init__.py +0 -3
  384. solace_agent_mesh/cli/commands/init/ai_provider_step.py +0 -93
  385. solace_agent_mesh/cli/commands/init/broker_step.py +0 -99
  386. solace_agent_mesh/cli/commands/init/builtin_agent_step.py +0 -83
  387. solace_agent_mesh/cli/commands/init/check_if_already_done.py +0 -13
  388. solace_agent_mesh/cli/commands/init/create_config_file_step.py +0 -65
  389. solace_agent_mesh/cli/commands/init/create_other_project_files_step.py +0 -147
  390. solace_agent_mesh/cli/commands/init/file_service_step.py +0 -73
  391. solace_agent_mesh/cli/commands/init/init.py +0 -92
  392. solace_agent_mesh/cli/commands/init/project_structure_step.py +0 -16
  393. solace_agent_mesh/cli/commands/init/web_init_step.py +0 -32
  394. solace_agent_mesh/cli/commands/plugin/__init__.py +0 -3
  395. solace_agent_mesh/cli/commands/plugin/add.py +0 -100
  396. solace_agent_mesh/cli/commands/plugin/build.py +0 -268
  397. solace_agent_mesh/cli/commands/plugin/create.py +0 -117
  398. solace_agent_mesh/cli/commands/plugin/plugin.py +0 -124
  399. solace_agent_mesh/cli/commands/plugin/remove.py +0 -73
  400. solace_agent_mesh/cli/commands/run.py +0 -68
  401. solace_agent_mesh/cli/commands/visualizer.py +0 -138
  402. solace_agent_mesh/cli/config.py +0 -85
  403. solace_agent_mesh/common/action.py +0 -91
  404. solace_agent_mesh/common/action_list.py +0 -37
  405. solace_agent_mesh/common/action_response.py +0 -340
  406. solace_agent_mesh/common/mysql_database.py +0 -40
  407. solace_agent_mesh/common/postgres_database.py +0 -85
  408. solace_agent_mesh/common/prompt_templates.py +0 -28
  409. solace_agent_mesh/common/stimulus_utils.py +0 -152
  410. solace_agent_mesh/common/time.py +0 -24
  411. solace_agent_mesh/common/utils.py +0 -712
  412. solace_agent_mesh/config_portal/frontend/static/client/assets/_index-a-zJ6rLx.js +0 -46
  413. solace_agent_mesh/config_portal/frontend/static/client/assets/components-ZIfdTbrV.js +0 -191
  414. solace_agent_mesh/config_portal/frontend/static/client/assets/index-BJHAE5s4.js +0 -17
  415. solace_agent_mesh/config_portal/frontend/static/client/assets/manifest-44c41103.js +0 -1
  416. solace_agent_mesh/config_portal/frontend/static/client/assets/root-DX4gQ516.css +0 -1
  417. solace_agent_mesh/configs/agent_global.yaml +0 -74
  418. solace_agent_mesh/configs/agent_image_processing.yaml +0 -82
  419. solace_agent_mesh/configs/agent_slack.yaml +0 -64
  420. solace_agent_mesh/configs/agent_web_request.yaml +0 -75
  421. solace_agent_mesh/configs/conversation_to_file.yaml +0 -56
  422. solace_agent_mesh/configs/error_catcher.yaml +0 -56
  423. solace_agent_mesh/configs/monitor.yaml +0 -0
  424. solace_agent_mesh/configs/monitor_stim_and_errors_to_slack.yaml +0 -109
  425. solace_agent_mesh/configs/monitor_user_feedback.yaml +0 -58
  426. solace_agent_mesh/configs/orchestrator.yaml +0 -241
  427. solace_agent_mesh/configs/service_embedding.yaml +0 -81
  428. solace_agent_mesh/configs/service_llm.yaml +0 -265
  429. solace_agent_mesh/configs/visualize_websocket.yaml +0 -55
  430. solace_agent_mesh/gateway/components/gateway_base.py +0 -47
  431. solace_agent_mesh/gateway/components/gateway_input.py +0 -278
  432. solace_agent_mesh/gateway/components/gateway_output.py +0 -298
  433. solace_agent_mesh/gateway/identity/bamboohr_identity.py +0 -18
  434. solace_agent_mesh/gateway/identity/identity_base.py +0 -10
  435. solace_agent_mesh/gateway/identity/identity_provider.py +0 -60
  436. solace_agent_mesh/gateway/identity/no_identity.py +0 -9
  437. solace_agent_mesh/gateway/identity/passthru_identity.py +0 -9
  438. solace_agent_mesh/monitors/base_monitor_component.py +0 -26
  439. solace_agent_mesh/monitors/feedback/user_feedback_monitor.py +0 -75
  440. solace_agent_mesh/monitors/stim_and_errors/stim_and_error_monitor.py +0 -560
  441. solace_agent_mesh/orchestrator/__init__.py +0 -0
  442. solace_agent_mesh/orchestrator/action_manager.py +0 -237
  443. solace_agent_mesh/orchestrator/components/__init__.py +0 -0
  444. solace_agent_mesh/orchestrator/components/orchestrator_action_manager_timeout_component.py +0 -58
  445. solace_agent_mesh/orchestrator/components/orchestrator_action_response_component.py +0 -179
  446. solace_agent_mesh/orchestrator/components/orchestrator_register_component.py +0 -107
  447. solace_agent_mesh/orchestrator/components/orchestrator_stimulus_processor_component.py +0 -527
  448. solace_agent_mesh/orchestrator/components/orchestrator_streaming_output_component.py +0 -260
  449. solace_agent_mesh/orchestrator/orchestrator_main.py +0 -172
  450. solace_agent_mesh/orchestrator/orchestrator_prompt.py +0 -539
  451. solace_agent_mesh/services/__init__.py +0 -0
  452. solace_agent_mesh/services/authorization/providers/base_authorization_provider.py +0 -56
  453. solace_agent_mesh/services/bamboo_hr_service/__init__.py +0 -3
  454. solace_agent_mesh/services/bamboo_hr_service/bamboo_hr.py +0 -182
  455. solace_agent_mesh/services/common/__init__.py +0 -4
  456. solace_agent_mesh/services/common/auto_expiry.py +0 -45
  457. solace_agent_mesh/services/common/singleton.py +0 -18
  458. solace_agent_mesh/services/file_service/__init__.py +0 -14
  459. solace_agent_mesh/services/file_service/file_manager/__init__.py +0 -0
  460. solace_agent_mesh/services/file_service/file_manager/bucket_file_manager.py +0 -149
  461. solace_agent_mesh/services/file_service/file_manager/file_manager_base.py +0 -162
  462. solace_agent_mesh/services/file_service/file_manager/memory_file_manager.py +0 -64
  463. solace_agent_mesh/services/file_service/file_manager/volume_file_manager.py +0 -106
  464. solace_agent_mesh/services/file_service/file_service.py +0 -437
  465. solace_agent_mesh/services/file_service/file_service_constants.py +0 -54
  466. solace_agent_mesh/services/file_service/file_transformations.py +0 -141
  467. solace_agent_mesh/services/file_service/file_utils.py +0 -324
  468. solace_agent_mesh/services/file_service/transformers/__init__.py +0 -5
  469. solace_agent_mesh/services/history_service/__init__.py +0 -3
  470. solace_agent_mesh/services/history_service/history_providers/__init__.py +0 -0
  471. solace_agent_mesh/services/history_service/history_providers/base_history_provider.py +0 -54
  472. solace_agent_mesh/services/history_service/history_providers/file_history_provider.py +0 -74
  473. solace_agent_mesh/services/history_service/history_providers/index.py +0 -40
  474. solace_agent_mesh/services/history_service/history_providers/memory_history_provider.py +0 -33
  475. solace_agent_mesh/services/history_service/history_providers/mongodb_history_provider.py +0 -66
  476. solace_agent_mesh/services/history_service/history_providers/redis_history_provider.py +0 -66
  477. solace_agent_mesh/services/history_service/history_providers/sql_history_provider.py +0 -93
  478. solace_agent_mesh/services/history_service/history_service.py +0 -413
  479. solace_agent_mesh/services/history_service/long_term_memory/__init__.py +0 -0
  480. solace_agent_mesh/services/history_service/long_term_memory/long_term_memory.py +0 -399
  481. solace_agent_mesh/services/llm_service/components/llm_request_component.py +0 -340
  482. solace_agent_mesh/services/llm_service/components/llm_service_component_base.py +0 -152
  483. solace_agent_mesh/services/middleware_service/__init__.py +0 -0
  484. solace_agent_mesh/services/middleware_service/middleware_service.py +0 -20
  485. solace_agent_mesh/templates/action.py +0 -38
  486. solace_agent_mesh/templates/agent.py +0 -29
  487. solace_agent_mesh/templates/agent.yaml +0 -70
  488. solace_agent_mesh/templates/gateway-config-template.yaml +0 -6
  489. solace_agent_mesh/templates/gateway-default-config.yaml +0 -28
  490. solace_agent_mesh/templates/gateway-flows.yaml +0 -78
  491. solace_agent_mesh/templates/gateway-header.yaml +0 -16
  492. solace_agent_mesh/templates/gateway_base.py +0 -15
  493. solace_agent_mesh/templates/gateway_input.py +0 -98
  494. solace_agent_mesh/templates/gateway_output.py +0 -71
  495. solace_agent_mesh/templates/plugin-gateway-default-config.yaml +0 -29
  496. solace_agent_mesh/templates/plugin-pyproject.toml +0 -30
  497. solace_agent_mesh/templates/rest-api-default-config.yaml +0 -31
  498. solace_agent_mesh/templates/rest-api-flows.yaml +0 -81
  499. solace_agent_mesh/templates/slack-default-config.yaml +0 -16
  500. solace_agent_mesh/templates/slack-flows.yaml +0 -81
  501. solace_agent_mesh/templates/solace-agent-mesh-default.yaml +0 -86
  502. solace_agent_mesh/templates/solace-agent-mesh-plugin-default.yaml +0 -8
  503. solace_agent_mesh/templates/web-default-config.yaml +0 -10
  504. solace_agent_mesh/templates/web-flows.yaml +0 -76
  505. solace_agent_mesh/tools/__init__.py +0 -0
  506. solace_agent_mesh/tools/components/__init__.py +0 -0
  507. solace_agent_mesh/tools/components/conversation_formatter.py +0 -111
  508. solace_agent_mesh/tools/components/file_resolver_component.py +0 -58
  509. solace_agent_mesh/tools/config/runtime_config.py +0 -26
  510. solace_agent_mesh-0.2.4.dist-info/METADATA +0 -176
  511. solace_agent_mesh-0.2.4.dist-info/RECORD +0 -193
  512. solace_agent_mesh-0.2.4.dist-info/entry_points.txt +0 -3
  513. /solace_agent_mesh/{agents → agent}/__init__.py +0 -0
  514. /solace_agent_mesh/{agents/global → agent/adk}/__init__.py +0 -0
  515. /solace_agent_mesh/{agents/global/actions → agent/protocol}/__init__.py +0 -0
  516. /solace_agent_mesh/{agents/image_processing → agent/sac}/__init__.py +0 -0
  517. /solace_agent_mesh/{agents/image_processing/actions → agent/utils}/__init__.py +0 -0
  518. /solace_agent_mesh/{agents/web_request → config_portal/backend/plugin_catalog}/__init__.py +0 -0
  519. /solace_agent_mesh/{agents/web_request/actions → evaluation}/__init__.py +0 -0
  520. /solace_agent_mesh/gateway/{components → http_sse}/__init__.py +0 -0
  521. {solace_agent_mesh-0.2.4.dist-info → solace_agent_mesh-1.0.2.dist-info}/WHEEL +0 -0
@@ -0,0 +1,1716 @@
1
+ """
2
+ ADK Callbacks for the A2A Host Component.
3
+ Includes dynamic instruction injection, artifact metadata injection,
4
+ embed resolution, and logging.
5
+ """
6
+
7
+ import json
8
+ import asyncio
9
+ import uuid
10
+ from typing import Any, Dict, Optional, TYPE_CHECKING, List
11
+ from collections import defaultdict
12
+ from datetime import datetime, timezone
13
+
14
+ from google.adk.tools import BaseTool, ToolContext
15
+ from google.adk.artifacts import BaseArtifactService
16
+ from google.adk.agents.callback_context import CallbackContext
17
+ from google.adk.models.llm_request import LlmRequest
18
+ from google.adk.models.llm_response import LlmResponse
19
+ from ...common.a2a_protocol import (
20
+ A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY,
21
+ )
22
+ from google.genai import types as adk_types
23
+ from google.adk.tools.mcp_tool import MCPTool
24
+ from solace_ai_connector.common.log import log
25
+
26
+ from ...agent.utils.artifact_helpers import (
27
+ METADATA_SUFFIX,
28
+ format_metadata_for_llm,
29
+ )
30
+ from ...agent.utils.context_helpers import (
31
+ get_original_session_id,
32
+ get_session_from_callback_context,
33
+ )
34
+ from ..tools.tool_definition import BuiltinTool
35
+
36
+ from ...common.utils.embeds import (
37
+ EMBED_DELIMITER_OPEN,
38
+ EMBED_DELIMITER_CLOSE,
39
+ )
40
+
41
+ from ...common.utils.embeds import (
42
+ EMBED_CHAIN_DELIMITER,
43
+ )
44
+
45
+ from ...common.utils.embeds.modifiers import MODIFIER_IMPLEMENTATIONS
46
+
47
+ if TYPE_CHECKING:
48
+ from ..sac.component import SamAgentComponent
49
+
50
+ from ...common.types import (
51
+ TaskStatusUpdateEvent,
52
+ TaskStatus,
53
+ TaskState,
54
+ Message as A2AMessage,
55
+ )
56
+
57
+ from ...agent.utils.artifact_helpers import (
58
+ save_artifact_with_metadata,
59
+ DEFAULT_SCHEMA_MAX_KEYS,
60
+ )
61
+
62
+ METADATA_RESPONSE_KEY = "appended_artifact_metadata"
63
+ from ..tools.builtin_artifact_tools import _internal_create_artifact
64
+ from ...agent.adk.tool_wrapper import ADKToolWrapper
65
+
66
+ # Import the new parser and its events
67
+ from ...agent.adk.stream_parser import (
68
+ FencedBlockStreamParser,
69
+ BlockStartedEvent,
70
+ BlockProgressedEvent,
71
+ BlockCompletedEvent,
72
+ BlockInvalidatedEvent,
73
+ ARTIFACT_BLOCK_DELIMITER_OPEN,
74
+ ARTIFACT_BLOCK_DELIMITER_CLOSE,
75
+ )
76
+
77
+
78
+ async def process_artifact_blocks_callback(
79
+ callback_context: CallbackContext,
80
+ llm_response: LlmResponse,
81
+ host_component: "SamAgentComponent",
82
+ ) -> Optional[LlmResponse]:
83
+ """
84
+ Orchestrates the parsing of fenced artifact blocks from an LLM stream
85
+ by delegating to a FencedBlockStreamParser instance.
86
+ This callback is stateful across streaming chunks within a single turn.
87
+ """
88
+ log_identifier = "[Callback:ProcessArtifactBlocks]"
89
+ parser_state_key = "fenced_block_parser"
90
+ session = get_session_from_callback_context(callback_context)
91
+
92
+ parser: FencedBlockStreamParser = session.state.get(parser_state_key)
93
+ if parser is None:
94
+ log.debug("%s New turn. Creating new FencedBlockStreamParser.", log_identifier)
95
+ parser = FencedBlockStreamParser(progress_update_interval_bytes=250)
96
+ session.state[parser_state_key] = parser
97
+ session.state["completed_artifact_blocks_list"] = []
98
+
99
+ stream_chunks_were_processed = callback_context.state.get(
100
+ A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY, False
101
+ )
102
+ if llm_response.partial:
103
+ callback_context.state[A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY] = True
104
+
105
+ if llm_response.partial or not stream_chunks_were_processed:
106
+ processed_parts: List[adk_types.Part] = []
107
+ original_parts = llm_response.content.parts if llm_response.content else []
108
+ a2a_context = callback_context.state.get("a2a_context")
109
+
110
+ for part in original_parts:
111
+ if part.text is not None:
112
+ parser_result = parser.process_chunk(part.text)
113
+
114
+ if llm_response.partial:
115
+ if parser_result.user_facing_text:
116
+ processed_parts.append(
117
+ adk_types.Part(text=parser_result.user_facing_text)
118
+ )
119
+ else:
120
+ processed_parts.append(part)
121
+
122
+ for event in parser_result.events:
123
+ if isinstance(event, BlockStartedEvent):
124
+ log.info(
125
+ "%s Event: BlockStarted. Params: %s",
126
+ log_identifier,
127
+ event.params,
128
+ )
129
+ filename = event.params.get("filename", "unknown_artifact")
130
+ if a2a_context:
131
+ await host_component._publish_agent_status_signal_update(
132
+ f"Receiving artifact `{filename}`...", a2a_context
133
+ )
134
+ params_str = " ".join(
135
+ [f'{k}="{v}"' for k, v in event.params.items()]
136
+ )
137
+ original_text = f"«««save_artifact: {params_str}\n"
138
+ session.state["artifact_block_original_text"] = original_text
139
+
140
+ elif isinstance(event, BlockProgressedEvent):
141
+ log.debug(
142
+ "%s Event: BlockProgressed. Size: %d",
143
+ log_identifier,
144
+ event.buffered_size,
145
+ )
146
+ params = parser._block_params
147
+ filename = params.get("filename", "unknown_artifact")
148
+ status_message = f"Creating artifact `{filename}` ({event.buffered_size}B saved)..."
149
+ if a2a_context:
150
+ await host_component._publish_agent_status_signal_update(
151
+ status_message, a2a_context
152
+ )
153
+
154
+ elif isinstance(event, BlockCompletedEvent):
155
+ log.info(
156
+ "%s Event: BlockCompleted. Content length: %d",
157
+ log_identifier,
158
+ len(event.content),
159
+ )
160
+ original_text = session.state.get(
161
+ "artifact_block_original_text", ""
162
+ )
163
+ original_text += event.content
164
+ original_text += "»»»"
165
+
166
+ tool_context_for_call = ToolContext(
167
+ callback_context._invocation_context
168
+ )
169
+
170
+ params = event.params
171
+ filename = params.get("filename")
172
+ if not filename or not filename.strip():
173
+ log.warning(
174
+ "%s Fenced artifact block is missing a valid 'filename'. Failing operation.",
175
+ log_identifier,
176
+ )
177
+ session.state["completed_artifact_blocks_list"].append(
178
+ {
179
+ "filename": (
180
+ "unknown_artifact"
181
+ if filename is None
182
+ else filename
183
+ ),
184
+ "version": 0,
185
+ "status": "error",
186
+ "original_text": original_text,
187
+ }
188
+ )
189
+ continue
190
+
191
+ kwargs_for_call = {
192
+ "filename": filename,
193
+ "content": event.content,
194
+ "mime_type": params.get("mime_type"),
195
+ "description": params.get("description"),
196
+ "metadata_json": params.get("metadata"),
197
+ "tool_context": tool_context_for_call,
198
+ }
199
+ if "schema_max_keys" in params:
200
+ try:
201
+ kwargs_for_call["schema_max_keys"] = int(
202
+ params["schema_max_keys"]
203
+ )
204
+ except (ValueError, TypeError):
205
+ log.warning(
206
+ "%s Invalid 'schema_max_keys' value '%s'. Ignoring.",
207
+ log_identifier,
208
+ params["schema_max_keys"],
209
+ )
210
+
211
+ wrapped_creator = ADKToolWrapper(
212
+ original_func=_internal_create_artifact,
213
+ tool_config=None, # No specific config for this internal tool
214
+ tool_name="_internal_create_artifact",
215
+ )
216
+ save_result = await wrapped_creator(**kwargs_for_call)
217
+
218
+ if save_result.get("status") in ["success", "partial_success"]:
219
+ status_for_tool = "success"
220
+ version_for_tool = save_result.get("data_version", 1)
221
+ try:
222
+ logical_task_id = a2a_context.get("logical_task_id")
223
+ if logical_task_id:
224
+ with host_component.active_tasks_lock:
225
+ task_context = host_component.active_tasks.get(
226
+ logical_task_id
227
+ )
228
+ if task_context:
229
+ task_context.register_produced_artifact(
230
+ filename, version_for_tool
231
+ )
232
+ log.info(
233
+ "%s Registered inline artifact '%s' v%d for task %s.",
234
+ log_identifier,
235
+ filename,
236
+ version_for_tool,
237
+ logical_task_id,
238
+ )
239
+ else:
240
+ log.warning(
241
+ "%s No logical_task_id, cannot register inline artifact.",
242
+ log_identifier,
243
+ )
244
+ except Exception as e_track:
245
+ log.error(
246
+ "%s Failed to track inline artifact: %s",
247
+ log_identifier,
248
+ e_track,
249
+ )
250
+ else:
251
+ status_for_tool = "error"
252
+ version_for_tool = 0
253
+
254
+ session.state["completed_artifact_blocks_list"].append(
255
+ {
256
+ "filename": filename,
257
+ "version": version_for_tool,
258
+ "status": status_for_tool,
259
+ "original_text": original_text,
260
+ }
261
+ )
262
+
263
+ elif isinstance(event, BlockInvalidatedEvent):
264
+ log.debug(
265
+ "%s Event: BlockInvalidated. Rolled back: '%s'",
266
+ log_identifier,
267
+ event.rolled_back_text,
268
+ )
269
+ else:
270
+ processed_parts.append(part)
271
+
272
+ if llm_response.partial:
273
+ if llm_response.content:
274
+ llm_response.content.parts = processed_parts
275
+ elif processed_parts:
276
+ llm_response.content = adk_types.Content(parts=processed_parts)
277
+ else:
278
+ log.debug(
279
+ "%s Ignoring text content of final aggregated response because stream was already processed.",
280
+ log_identifier,
281
+ )
282
+
283
+ if not llm_response.partial and not llm_response.interrupted:
284
+ log.debug(
285
+ "%s Final, non-interrupted stream chunk received. Finalizing parser.",
286
+ log_identifier,
287
+ )
288
+ final_parser_result = parser.finalize()
289
+
290
+ for event in final_parser_result.events:
291
+ if isinstance(event, BlockCompletedEvent):
292
+ log.warning(
293
+ "%s Unterminated artifact block detected at end of turn.",
294
+ log_identifier,
295
+ )
296
+ params = event.params
297
+ filename = params.get("filename", "unknown_artifact")
298
+ if (
299
+ "completed_artifact_blocks_list" not in session.state
300
+ or session.state["completed_artifact_blocks_list"] is None
301
+ ):
302
+ session.state["completed_artifact_blocks_list"] = []
303
+ session.state["completed_artifact_blocks_list"].append(
304
+ {
305
+ "filename": filename,
306
+ "version": 0,
307
+ "status": "error",
308
+ "original_text": session.state.get(
309
+ "artifact_block_original_text", ""
310
+ )
311
+ + event.content,
312
+ }
313
+ )
314
+
315
+ # If there was any rolled-back text from finalization, append it
316
+ if final_parser_result.user_facing_text:
317
+ if (
318
+ llm_response.content
319
+ and llm_response.content.parts
320
+ and llm_response.content.parts[-1].text is not None
321
+ ):
322
+ llm_response.content.parts[
323
+ -1
324
+ ].text += final_parser_result.user_facing_text
325
+ else:
326
+ if llm_response.content is None:
327
+ llm_response.content = adk_types.Content(parts=[])
328
+ elif llm_response.content.parts is None:
329
+ llm_response.content.parts = []
330
+ llm_response.content.parts.append(
331
+ adk_types.Part(text=final_parser_result.user_facing_text)
332
+ )
333
+
334
+ # Check if any blocks were completed and need to be injected into the final response
335
+ completed_blocks_list = session.state.get("completed_artifact_blocks_list")
336
+ if completed_blocks_list:
337
+ log.info(
338
+ "%s Injecting info for %d saved artifact(s) into final LlmResponse.",
339
+ log_identifier,
340
+ len(completed_blocks_list),
341
+ )
342
+
343
+ tool_call_parts = []
344
+ for block_info in completed_blocks_list:
345
+ notify_tool_call = adk_types.FunctionCall(
346
+ name="_notify_artifact_save",
347
+ args={
348
+ "filename": block_info["filename"],
349
+ "version": block_info["version"],
350
+ "status": block_info["status"],
351
+ },
352
+ id=f"host-notify-{uuid.uuid4()}",
353
+ )
354
+ tool_call_parts.append(adk_types.Part(function_call=notify_tool_call))
355
+
356
+ existing_parts = llm_response.content.parts if llm_response.content else []
357
+ final_existing_parts = existing_parts
358
+
359
+ if llm_response.content is None:
360
+ llm_response.content = adk_types.Content(parts=[])
361
+
362
+ llm_response.content.parts = tool_call_parts + final_existing_parts
363
+
364
+ llm_response.turn_complete = True
365
+ llm_response.partial = False
366
+
367
+ session.state[parser_state_key] = None
368
+ session.state["completed_artifact_blocks_list"] = None
369
+ session.state["artifact_block_original_text"] = None
370
+ log.debug("%s Cleaned up parser session state.", log_identifier)
371
+
372
+ return None
373
+
374
+
375
+ def create_dangling_tool_call_repair_content(
376
+ dangling_calls: List[adk_types.FunctionCall], error_message: str
377
+ ) -> adk_types.Content:
378
+ """
379
+ Creates a synthetic ADK Content object to repair a dangling tool call.
380
+
381
+ Args:
382
+ dangling_calls: The list of FunctionCall objects that need a response.
383
+ error_message: The error message to include in the response.
384
+
385
+ Returns:
386
+ An ADK Content object with role='tool' containing the error response.
387
+ """
388
+ error_response_parts = []
389
+ for fc in dangling_calls:
390
+ error_response_part = adk_types.Part.from_function_response(
391
+ name=fc.name,
392
+ response={"status": "error", "message": error_message},
393
+ )
394
+ error_response_part.function_response.id = fc.id
395
+ error_response_parts.append(error_response_part)
396
+
397
+ return adk_types.Content(role="tool", parts=error_response_parts)
398
+
399
+
400
+ def repair_history_callback(
401
+ callback_context: CallbackContext, llm_request: LlmRequest
402
+ ) -> Optional[LlmResponse]:
403
+ """
404
+ ADK before_model_callback to proactively check for and repair dangling
405
+ tool calls in the conversation history before it's sent to the LLM.
406
+ This acts as a "suspender" to catch any history corruption.
407
+ """
408
+ log_identifier = "[Callback:RepairHistory]"
409
+ if not llm_request.contents:
410
+ return None
411
+
412
+ history_modified = False
413
+ i = 0
414
+ while i < len(llm_request.contents):
415
+ content = llm_request.contents[i]
416
+ function_calls = []
417
+ if content.role == "model" and content.parts:
418
+ function_calls = [p.function_call for p in content.parts if p.function_call]
419
+
420
+ if function_calls:
421
+ next_content_is_valid_response = False
422
+ if (i + 1) < len(llm_request.contents):
423
+ next_content = llm_request.contents[i + 1]
424
+ if (
425
+ next_content.role in ["user", "tool"]
426
+ and next_content.parts
427
+ and any(p.function_response for p in next_content.parts)
428
+ ):
429
+ next_content_is_valid_response = True
430
+
431
+ if not next_content_is_valid_response:
432
+ log.warning(
433
+ "%s Found dangling tool call in history for tool(s): %s. Repairing.",
434
+ log_identifier,
435
+ [fc.name for fc in function_calls],
436
+ )
437
+ repair_content = create_dangling_tool_call_repair_content(
438
+ dangling_calls=function_calls,
439
+ error_message="The previous tool call did not complete successfully and was automatically repaired.",
440
+ )
441
+ llm_request.contents.insert(i + 1, repair_content)
442
+ history_modified = True
443
+ i += 1
444
+ i += 1
445
+
446
+ if history_modified:
447
+ log.info(
448
+ "%s History was modified to repair dangling tool calls.", log_identifier
449
+ )
450
+
451
+ return None
452
+
453
+
454
+ async def _save_mcp_response_as_artifact(
455
+ tool: BaseTool,
456
+ tool_context: ToolContext,
457
+ host_component: "SamAgentComponent",
458
+ mcp_response_dict: Dict[str, Any],
459
+ original_tool_args: Dict[str, Any],
460
+ ) -> Dict[str, Any]:
461
+ """
462
+ Saves the full MCP tool response as a JSON artifact with associated metadata.
463
+
464
+ Args:
465
+ tool: The MCPTool instance that generated the response.
466
+ tool_context: The ADK ToolContext.
467
+ host_component: The A2A_ADK_HostComponent instance for accessing config and services.
468
+ mcp_response_dict: The raw MCP tool response dictionary.
469
+ original_tool_args: The original arguments passed to the MCP tool.
470
+
471
+ Returns:
472
+ A dictionary containing details of the saved artifact (filename, version, etc.),
473
+ as returned by `save_artifact_with_metadata`.
474
+ """
475
+ log_identifier = f"[CallbackHelper:{tool.name}]"
476
+ log.debug("%s Saving MCP response as artifact...", log_identifier)
477
+
478
+ try:
479
+ a2a_context = tool_context.state.get("a2a_context", {})
480
+ logical_task_id = a2a_context.get("logical_task_id", "unknownTask")
481
+ task_id_suffix = logical_task_id[-6:]
482
+ random_suffix = uuid.uuid4().hex[:6]
483
+ filename = f"{task_id_suffix}_{tool.name}_{random_suffix}.json"
484
+ log.debug("%s Generated artifact filename: %s", log_identifier, filename)
485
+
486
+ content_bytes = json.dumps(mcp_response_dict, indent=2).encode("utf-8")
487
+ mime_type = "application/json"
488
+ artifact_timestamp = datetime.now(timezone.utc)
489
+
490
+ metadata_for_saving = {
491
+ "description": f"Full JSON response from MCP tool {tool.name}.",
492
+ "source_tool_name": tool.name,
493
+ "source_tool_args": original_tool_args,
494
+ }
495
+ log.debug("%s Prepared content and metadata for saving.", log_identifier)
496
+
497
+ artifact_service = host_component.artifact_service
498
+ if not artifact_service:
499
+ raise ValueError("ArtifactService is not available on host_component.")
500
+
501
+ app_name = host_component.agent_name
502
+ user_id = tool_context._invocation_context.user_id
503
+ session_id = get_original_session_id(tool_context._invocation_context)
504
+ schema_max_keys = host_component.get_config(
505
+ "schema_max_keys", DEFAULT_SCHEMA_MAX_KEYS
506
+ )
507
+
508
+ log.debug(
509
+ "%s Calling save_artifact_with_metadata with: app_name=%s, user_id=%s, session_id=%s, filename=%s, schema_max_keys=%d",
510
+ log_identifier,
511
+ app_name,
512
+ user_id,
513
+ session_id,
514
+ filename,
515
+ schema_max_keys,
516
+ )
517
+
518
+ save_result = await save_artifact_with_metadata(
519
+ artifact_service=artifact_service,
520
+ app_name=app_name,
521
+ user_id=user_id,
522
+ session_id=session_id,
523
+ filename=filename,
524
+ content_bytes=content_bytes,
525
+ mime_type=mime_type,
526
+ metadata_dict=metadata_for_saving,
527
+ timestamp=artifact_timestamp,
528
+ schema_max_keys=schema_max_keys,
529
+ tool_context=tool_context,
530
+ )
531
+
532
+ log.info(
533
+ "%s MCP response saved as artifact '%s' (version %s). Result: %s",
534
+ log_identifier,
535
+ save_result.get("data_filename", filename),
536
+ save_result.get("data_version", "N/A"),
537
+ save_result.get("status"),
538
+ )
539
+ return save_result
540
+
541
+ except Exception as e:
542
+ log.exception(
543
+ "%s Error in _save_mcp_response_as_artifact: %s", log_identifier, e
544
+ )
545
+ return {
546
+ "status": "error",
547
+ "data_filename": filename if "filename" in locals() else "unknown_filename",
548
+ "message": f"Failed to save MCP response as artifact: {e}",
549
+ }
550
+
551
+
552
+ async def manage_large_mcp_tool_responses_callback(
553
+ tool: BaseTool,
554
+ args: Dict[str, Any],
555
+ tool_context: ToolContext,
556
+ tool_response: Any,
557
+ host_component: "SamAgentComponent",
558
+ ) -> Optional[Dict[str, Any]]:
559
+ """
560
+ Manages large responses from MCP tools by conditionally saving them as artifacts
561
+ and/or truncating them before returning to the LLM.
562
+ The 'tool_response' is the direct output from the tool's run_async method.
563
+ """
564
+ log_identifier = f"[Callback:ManageLargeMCPResponse:{tool.name}]"
565
+ log.info(
566
+ "%s Starting callback for tool response, type: %s",
567
+ log_identifier,
568
+ type(tool_response).__name__,
569
+ )
570
+
571
+ if tool_response is None:
572
+ return None
573
+
574
+ if not isinstance(tool, MCPTool):
575
+ log.debug(
576
+ "%s Tool is not an MCPTool. Skipping large response handling.",
577
+ log_identifier,
578
+ )
579
+ return (
580
+ tool_response
581
+ if isinstance(tool_response, dict)
582
+ else {"result": tool_response}
583
+ )
584
+
585
+ log.debug(
586
+ "%s Tool is an MCPTool. Proceeding with large response handling.",
587
+ log_identifier,
588
+ )
589
+
590
+ if hasattr(tool_response, "model_dump"):
591
+ mcp_response_dict = tool_response.model_dump(exclude_none=True)
592
+ log.debug("%s Converted MCPTool response object to dictionary.", log_identifier)
593
+ elif isinstance(tool_response, dict):
594
+ mcp_response_dict = tool_response
595
+ log.debug("%s MCPTool response is already a dictionary.", log_identifier)
596
+ else:
597
+ log.warning(
598
+ "%s MCPTool response is not a Pydantic model or dict (type: %s). Attempting to proceed, but serialization might fail.",
599
+ log_identifier,
600
+ type(tool_response),
601
+ )
602
+ mcp_response_dict = tool_response
603
+
604
+ try:
605
+ save_threshold = host_component.get_config(
606
+ "mcp_tool_response_save_threshold_bytes", 2048
607
+ )
608
+ llm_max_bytes = host_component.get_config("mcp_tool_llm_return_max_bytes", 4096)
609
+ log.debug(
610
+ "%s Config: save_threshold=%d bytes, llm_max_bytes=%d bytes.",
611
+ log_identifier,
612
+ save_threshold,
613
+ llm_max_bytes,
614
+ )
615
+ except Exception as e:
616
+ log.error(
617
+ "%s Error retrieving configuration: %s. Using defaults.", log_identifier, e
618
+ )
619
+ save_threshold = 2048
620
+ llm_max_bytes = 4096
621
+
622
+ try:
623
+ serialized_original_response_str = json.dumps(mcp_response_dict)
624
+ original_response_bytes = len(serialized_original_response_str.encode("utf-8"))
625
+ log.debug(
626
+ "%s Original response size: %d bytes.",
627
+ log_identifier,
628
+ original_response_bytes,
629
+ )
630
+ except TypeError as e:
631
+ log.error(
632
+ "%s Failed to serialize original MCP tool response dictionary: %s. Returning original response object.",
633
+ log_identifier,
634
+ e,
635
+ )
636
+ return tool_response
637
+
638
+ needs_truncation_for_llm = original_response_bytes > llm_max_bytes
639
+ needs_saving_as_artifact = (
640
+ original_response_bytes > save_threshold
641
+ ) or needs_truncation_for_llm
642
+ log.debug(
643
+ "%s Conditions: needs_truncation_for_llm=%s, needs_saving_as_artifact=%s",
644
+ log_identifier,
645
+ needs_truncation_for_llm,
646
+ needs_saving_as_artifact,
647
+ )
648
+
649
+ saved_artifact_details = None
650
+ if needs_saving_as_artifact:
651
+ log.info(
652
+ "%s Original response (%d bytes) requires saving (save_threshold=%d, llm_max_bytes=%d). Saving as artifact.",
653
+ log_identifier,
654
+ original_response_bytes,
655
+ save_threshold,
656
+ llm_max_bytes,
657
+ )
658
+ saved_artifact_details = await _save_mcp_response_as_artifact(
659
+ tool, tool_context, host_component, mcp_response_dict, args
660
+ )
661
+ if not (
662
+ saved_artifact_details.get("status") == "success"
663
+ or saved_artifact_details.get("status") == "partial_success"
664
+ ):
665
+ log.warning(
666
+ "%s Failed to save artifact: %s. Proceeding without saved artifact details.",
667
+ log_identifier,
668
+ saved_artifact_details.get("message"),
669
+ )
670
+
671
+ final_llm_response_dict: Dict[str, Any] = {}
672
+ message_parts_for_llm: list[str] = []
673
+
674
+ if needs_truncation_for_llm:
675
+ truncation_suffix = "... [Response truncated due to size limit.]"
676
+ adjusted_max_bytes = llm_max_bytes - len(truncation_suffix.encode("utf-8"))
677
+ if adjusted_max_bytes < 0:
678
+ adjusted_max_bytes = 0
679
+
680
+ truncated_bytes = serialized_original_response_str.encode("utf-8")[
681
+ :adjusted_max_bytes
682
+ ]
683
+ truncated_preview_str = (
684
+ truncated_bytes.decode("utf-8", "ignore") + truncation_suffix
685
+ )
686
+
687
+ final_llm_response_dict["mcp_tool_output"] = {
688
+ "type": "truncated_json_string",
689
+ "content": truncated_preview_str,
690
+ }
691
+ message_parts_for_llm.append(
692
+ f"The response from tool '{tool.name}' was too large ({original_response_bytes} bytes) for direct display and has been truncated."
693
+ )
694
+ log.debug("%s MCP tool output truncated for LLM.", log_identifier)
695
+ else:
696
+ final_llm_response_dict["mcp_tool_output"] = mcp_response_dict
697
+ log.debug(
698
+ "%s MCP tool output is the full original response for LLM.", log_identifier
699
+ )
700
+
701
+ if needs_saving_as_artifact:
702
+ if saved_artifact_details and (
703
+ saved_artifact_details.get("status") == "success"
704
+ or saved_artifact_details.get("status") == "partial_success"
705
+ ):
706
+ final_llm_response_dict["saved_mcp_response_artifact_details"] = (
707
+ saved_artifact_details
708
+ )
709
+ filename = saved_artifact_details.get(
710
+ "data_filename", "unknown_artifact.json"
711
+ )
712
+ version = saved_artifact_details.get("data_version", "N/A")
713
+ message_parts_for_llm.append(
714
+ f"The full response has been saved as artifact '{filename}' (version {version})."
715
+ )
716
+ log.debug(
717
+ "%s Added saved artifact details to LLM response.", log_identifier
718
+ )
719
+ else:
720
+ message_parts_for_llm.append(
721
+ "Saving the full response as an artifact failed."
722
+ )
723
+ final_llm_response_dict["saved_mcp_response_artifact_details"] = {
724
+ "status": "error",
725
+ "message": saved_artifact_details.get(
726
+ "message", "Artifact saving failed."
727
+ ),
728
+ "filename": saved_artifact_details.get("data_filename", "unknown"),
729
+ }
730
+ log.warning(
731
+ "%s Artifact save failed, error details included in LLM response.",
732
+ log_identifier,
733
+ )
734
+
735
+ if needs_saving_as_artifact and (
736
+ saved_artifact_details.get("status") == "success"
737
+ or saved_artifact_details.get("status") == "partial_success"
738
+ ):
739
+ if needs_truncation_for_llm:
740
+ final_llm_response_dict["status"] = "processed_saved_and_truncated"
741
+ else:
742
+ final_llm_response_dict["status"] = "processed_and_saved"
743
+ elif needs_saving_as_artifact:
744
+ if needs_truncation_for_llm:
745
+ final_llm_response_dict["status"] = "processed_truncated_save_failed"
746
+ else:
747
+ final_llm_response_dict["status"] = "processed_save_failed"
748
+ elif needs_truncation_for_llm:
749
+ final_llm_response_dict["status"] = "processed_truncated"
750
+ else:
751
+ final_llm_response_dict["status"] = "processed"
752
+
753
+ if not message_parts_for_llm:
754
+ message_parts_for_llm.append(f"Response from tool '{tool.name}' processed.")
755
+ final_llm_response_dict["message_to_llm"] = " ".join(message_parts_for_llm)
756
+
757
+ log.info(
758
+ "%s Returning processed response for LLM. Final status: %s",
759
+ log_identifier,
760
+ final_llm_response_dict.get("status", "unknown"),
761
+ )
762
+ return final_llm_response_dict
763
+
764
+
765
+ def _generate_fenced_artifact_instruction() -> str:
766
+ """Generates the instruction text for using fenced artifact blocks."""
767
+ open_delim = ARTIFACT_BLOCK_DELIMITER_OPEN
768
+ close_delim = ARTIFACT_BLOCK_DELIMITER_CLOSE
769
+ return f"""\
770
+ **Creating Text-Based Artifacts:**
771
+ To create an artifact from content you generate (like code, a report, or a document), you MUST use a special `save_artifact` block. This is the only reliable way to ensure your content is saved correctly.
772
+
773
+ **Syntax:**
774
+ ```
775
+ {open_delim}save_artifact: filename="your_filename.ext" mime_type="text/plain" description="A brief description."
776
+ The full content you want to save goes here.
777
+ It can span multiple lines.
778
+ {close_delim}
779
+ ```
780
+
781
+ - **Rules:**
782
+ - The parameters `filename` and `mime_type` are required. `description` is optional but recommended.
783
+ - All parameter values **MUST** be enclosed in double quotes.
784
+ - You **MUST NOT** use double quotes `"` inside the parameter values (e.g., within the description string). Use single quotes or rephrase instead.
785
+
786
+ The system will automatically save the content and give you a confirmation in the next turn."""
787
+
788
+ def _generate_artifact_creation_instruction() -> str:
789
+ return """
790
+ **Creating Text-Based Artifacts:**
791
+
792
+ **When to Create Text-based Artifacts:**
793
+ Create an artifact when the content provides value as a standalone file:
794
+ - Content with special formatting (HTML, Markdown, CSS, structured markup) that requires proper rendering
795
+ - Content explicitly intended for use outside this conversation (reports, emails, presentations, reference documents)
796
+ - Structured reference content users will save or follow (schedules, guides, templates)
797
+ - Content that will be edited, expanded, or reused
798
+ - Substantial text documents
799
+ - Technical documentation meant as reference material
800
+
801
+ **When NOT to Create Text-based Artifacts:**
802
+ - Simple answers, explanations, or conversational responses
803
+ - Brief advice, opinions, or quick information
804
+ - Short lists, summaries, or single paragraphs
805
+ - Temporary content only relevant to the immediate conversation
806
+ - Basic explanations that don't require reference material
807
+ """
808
+
809
+ def _generate_embed_instruction(
810
+ include_artifact_content: bool,
811
+ log_identifier: str,
812
+ ) -> Optional[str]:
813
+ """Generates the instruction text for using embeds."""
814
+ open_delim = EMBED_DELIMITER_OPEN
815
+ close_delim = EMBED_DELIMITER_CLOSE
816
+ chain_delim = EMBED_CHAIN_DELIMITER
817
+ early_types = "`math`, `datetime`, `uuid`, `artifact_meta`"
818
+ modifier_list = ", ".join(
819
+ [f"`{prefix}`" for prefix in MODIFIER_IMPLEMENTATIONS.keys()]
820
+ )
821
+
822
+ base_instruction = f"""\
823
+ You can use dynamic embeds in your text responses and tool parameters using the syntax {open_delim}type:expression {chain_delim} format{close_delim}. This allows you to
824
+ always have correct information in your output. Specifically, make sure you always use embeds for math, even if it is simple. You will make mistakes if you try to do math yourself.
825
+ Use HTML entities to escape the delimiters.
826
+ This host resolves the following embed types *early* (before sending to the LLM or tool): {early_types}. This means the embed is replaced with its resolved value.
827
+ - `{open_delim}math:expression | .2f{close_delim}`: Evaluates the math expression using asteval - this must just be plain math (plus random(), randint() and uniform()), don't import anything. Optional format specifier follows Python's format(). Use this for all math calculations rather than doing it yourself. Don't give approximations.
828
+ - `{open_delim}datetime:format_or_keyword{close_delim}`: Inserts current date/time. Use Python strftime format (e.g., `%Y-%m-%d`) or keywords (`iso`, `timestamp`, `date`, `time`, `now`).
829
+ - `{open_delim}uuid:{close_delim}`: Inserts a random UUID.
830
+ - `{open_delim}artifact_meta:filename[:version]{close_delim}`: Inserts a summary of the artifact's metadata (latest version if unspecified).
831
+ - `{open_delim}status_update:Your message here{close_delim}`: Generates an immediate, distinct status message event that is displayed to the user (e.g., 'Thinking...', 'Searching database...'). This message appears in a status area, not as part of the main chat conversation. Use this to provide interim feedback during processing."""
832
+
833
+ artifact_content_instruction = f"""
834
+ - `{open_delim}artifact_content:filename[:version] {chain_delim} modifier1:value1 {chain_delim} ... {chain_delim} format:output_format{close_delim}`: Embeds artifact content after applying a chain of modifiers. This is resolved *late* (typically by a gateway before final display).
835
+ - Use `{chain_delim}` to separate the artifact identifier from the modifier steps and the final format step.
836
+ - Available modifiers: {modifier_list}.
837
+ - The `format:output_format` step *must* be the last step in the chain. Supported formats include `text`, `datauri`, `json`, `json_pretty`, `csv`. Formatting as datauri, will include the data URI prefix, so do not add it yourself.
838
+ - Use `artifact_meta` first to check size; embedding large files may fail.
839
+ - **Using `apply_to_template` Modifier:**
840
+ - This modifier renders a Mustache template artifact using the data from the previous step.
841
+ - **Data Context:**
842
+ - If the input data's original MIME type was `text/csv` or `application/csv`, it's automatically parsed into an object with two keys: `headers` (a list of column name strings) and `data_rows` (a list of lists, where each inner list contains the string values for a row). Example template usage: `<thead><tr>{{{{#headers}}}}<th>{{{{.}}}}</th>{{{{/headers}}}}</tr></thead><tbody>{{{{#data_rows}}}}<tr>{{{{#.}}}}<td>{{{{.}}}}</td>{{{{/.}}}}</tr>{{{{/data_rows}}}}</tbody>`. If CSV parsing fails, the raw string content is available under `text`.
843
+ - If the input data is a **list** (e.g., from `jsonpath` or a JSON array), it's available under `items`.
844
+ - If the input data is a **dictionary** (e.g., from a JSON object), its keys are directly available (e.g., `{{{{key1}}}}`).
845
+ - If the input data is a **plain string** (and not auto-parsed as CSV), it's available under `text`.
846
+ - The template filename can include a version (e.g., `template.mustache:2`). Defaults to latest.
847
+ - The template itself can contain `«artifact_content:...»` embeds, which will be resolved before rendering.
848
+ - Examples:
849
+ - `<img src="{open_delim}artifact_content:image.png {chain_delim} format:datauri{close_delim}`"> (Embed image as data URI - NOTE that this includes the datauri prefix. Do not add it yourself.)
850
+ - `{open_delim}artifact_content:data.json {chain_delim} jsonpath:$.items[*] {chain_delim} select_fields:name,status {chain_delim} format:json_pretty{close_delim}` (Extract and format JSON fields)
851
+ - `{open_delim}artifact_content:logs.txt {chain_delim} grep:ERROR {chain_delim} head:10 {chain_delim} format:text{close_delim}` (Get first 10 error lines)
852
+ - `{open_delim}artifact_content:products.csv {chain_delim} apply_to_template:product_table.html.mustache {chain_delim} format:text{close_delim}` (CSV is auto-parsed to `headers` and `data_rows` for the HTML template)
853
+ - `{open_delim}artifact_content:config.json {chain_delim} jsonpath:$.userPreferences.theme {chain_delim} format:text{close_delim}` (Extract a single value from a JSON artifact)
854
+ - `{open_delim}artifact_content:sensor_readings.csv {chain_delim} filter_rows_eq:status:critical {chain_delim} select_cols:timestamp,sensor_id,value {chain_delim} format:csv{close_delim}` (Filter critical sensor readings and select specific columns, output as CSV)
855
+ - `{open_delim}artifact_content:server.log {chain_delim} tail:100 {chain_delim} grep:WARN {chain_delim} format:text{close_delim}` (Get warning lines from the last 100 lines of a log file)"""
856
+
857
+ final_instruction = base_instruction
858
+ if include_artifact_content:
859
+ final_instruction += artifact_content_instruction
860
+
861
+ final_instruction += f"""
862
+ Ensure the syntax is exactly `{open_delim}type:expression{close_delim}` or `{open_delim}type:expression {chain_delim} ... {chain_delim} format:output_format{close_delim}` with no extra spaces around delimiters (`{open_delim}`, `{close_delim}`, `{chain_delim}`, `:`, `|`). Malformed directives will be ignored."""
863
+
864
+ return final_instruction
865
+
866
+
867
+ def _generate_tool_instructions_from_registry(
868
+ active_tools: List[BuiltinTool],
869
+ log_identifier: str,
870
+ ) -> str:
871
+ """Generates instruction text from a list of BuiltinTool definitions."""
872
+ if not active_tools:
873
+ return ""
874
+
875
+ instructions_by_category = defaultdict(list)
876
+ for tool in sorted(active_tools, key=lambda t: (t.category, t.name)):
877
+ param_parts = []
878
+ if tool.parameters and tool.parameters.properties:
879
+ for name, schema in tool.parameters.properties.items():
880
+ is_optional = name not in (tool.parameters.required or [])
881
+ type_name = "any"
882
+ if schema and hasattr(schema, "type") and schema.type:
883
+ type_name = schema.type.name.lower()
884
+
885
+ param_str = f"{name}: {type_name}"
886
+ if is_optional:
887
+ param_str = f"Optional[{param_str}]"
888
+ param_parts.append(param_str)
889
+
890
+ signature = f"`{tool.name}({', '.join(param_parts)})`"
891
+ description = tool.description or "No description available."
892
+
893
+ instructions_by_category[tool.category].append(f"- {signature}: {description}")
894
+
895
+ full_instruction_list = []
896
+ for category, tool_instructions in sorted(instructions_by_category.items()):
897
+ category_display_name = category.replace("_", " ").title()
898
+ full_instruction_list.append(
899
+ f"You have access to the following '{category_display_name}' tools:"
900
+ )
901
+ full_instruction_list.extend(tool_instructions)
902
+
903
+ return "\n".join(full_instruction_list)
904
+
905
+
906
+ def inject_dynamic_instructions_callback(
907
+ callback_context: CallbackContext,
908
+ llm_request: LlmRequest,
909
+ host_component: "SamAgentComponent",
910
+ active_builtin_tools: List[BuiltinTool],
911
+ ) -> Optional[LlmResponse]:
912
+ """
913
+ ADK before_model_callback to inject instructions based on host config.
914
+ Modifies the llm_request directly.
915
+ """
916
+ log_identifier = "[Callback:InjectInstructions]"
917
+ log.debug("%s Running instruction injection callback...", log_identifier)
918
+
919
+ if not host_component:
920
+ log.error(
921
+ "%s Host component instance not provided. Cannot inject instructions.",
922
+ log_identifier,
923
+ )
924
+ return None
925
+
926
+ injected_instructions = []
927
+
928
+ planning_instruction = """\
929
+ Parallel Tool Calling:
930
+ The system is capable of calling multiple tools in parallel to speed up processing. Please try to run tools in parallel when they don't depend on each other. This saves money and time, providing faster results to the user.
931
+
932
+ Embeds in responses from agents:
933
+ To be efficient, agents may response with artifact_content embeds in their responses. These will not be resolved until they are sent back to a gateway. If it makes
934
+ sense, just carry that embed forward to your response to the user. For example, if you ask for an org chart from another agent and its response contains an embed like
935
+ `{open_delim}artifact_content:org_chart.md{close_delim}`, you can just include that embed in your response to the user. The gateway will resolve it and display the org chart.
936
+
937
+ When faced with a complex goal or request that involves multiple steps, data retrieval, or artifact summarization to produce a new report or document, you MUST first create a plan.
938
+ Simple, direct requests like 'create an image of a dog' or 'write an email to thank my boss' do not require a plan.
939
+
940
+ If a plan is created:
941
+ 1. It should be a terse, hierarchical list describing the steps needed, with each checkbox item on its own line.
942
+ 2. Use '☐' (empty checkbox emoji) for pending items and '☑' (checked checkbox emoji) for completed items.
943
+ 3. If the plan changes significantly during execution, restate the updated plan.
944
+ 4. As items are completed, update the plan to check them off.
945
+
946
+ """
947
+ injected_instructions.append(planning_instruction)
948
+ log.debug("%s Added hardcoded planning instructions.", log_identifier)
949
+ artifact_creation_instruction = _generate_artifact_creation_instruction()
950
+ injected_instructions.append(artifact_creation_instruction)
951
+ fenced_artifact_instruction = _generate_fenced_artifact_instruction()
952
+ injected_instructions.append(fenced_artifact_instruction)
953
+
954
+ agent_instruction_str: Optional[str] = None
955
+ if host_component._agent_system_instruction_callback:
956
+ log.debug(
957
+ "%s Calling agent-provided system instruction callback.", log_identifier
958
+ )
959
+ try:
960
+ agent_instruction_str = host_component._agent_system_instruction_callback(
961
+ callback_context, llm_request
962
+ )
963
+ if agent_instruction_str and isinstance(agent_instruction_str, str):
964
+ injected_instructions.append(agent_instruction_str)
965
+ log.info(
966
+ "%s Injected instructions from agent callback.", log_identifier
967
+ )
968
+ elif agent_instruction_str:
969
+ log.warning(
970
+ "%s Agent instruction callback returned non-string type: %s. Ignoring.",
971
+ log_identifier,
972
+ type(agent_instruction_str),
973
+ )
974
+ except Exception as e_cb:
975
+ log.error(
976
+ "%s Error in agent-provided system instruction callback: %s. Skipping.",
977
+ log_identifier,
978
+ e_cb,
979
+ )
980
+ if host_component._agent_system_instruction_string:
981
+ log.debug(
982
+ "%s Using agent-provided static system instruction string.", log_identifier
983
+ )
984
+ agent_instruction_str = host_component._agent_system_instruction_string
985
+ if agent_instruction_str and isinstance(agent_instruction_str, str):
986
+ injected_instructions.append(agent_instruction_str)
987
+ log.info("%s Injected static instructions from agent.", log_identifier)
988
+
989
+ contents = llm_request.contents
990
+ if contents:
991
+ log.debug("\n\n### LLM Request Contents ###")
992
+ for content in contents:
993
+ if content.parts:
994
+ for part in content.parts:
995
+ if part.text:
996
+ log.debug("Content part: %s", part.text)
997
+ elif part.function_call:
998
+ log.debug("Function call: %s", part.function_call.name)
999
+ elif part.function_response:
1000
+ log.debug("Function response: %s", part.function_response)
1001
+ else:
1002
+ log.debug("raw: %s", part)
1003
+ log.debug("### End LLM Request Contents ###\n\n")
1004
+
1005
+ if host_component.get_config("enable_embed_resolution", True):
1006
+ include_artifact_content_instr = host_component.get_config(
1007
+ "enable_artifact_content_instruction", True
1008
+ )
1009
+ instruction = _generate_embed_instruction(
1010
+ include_artifact_content_instr, log_identifier
1011
+ )
1012
+ if instruction:
1013
+ injected_instructions.append(instruction)
1014
+ log.debug(
1015
+ "%s Prepared embed instructions (artifact_content included: %s).",
1016
+ log_identifier,
1017
+ include_artifact_content_instr,
1018
+ )
1019
+
1020
+ if active_builtin_tools:
1021
+ instruction = _generate_tool_instructions_from_registry(
1022
+ active_builtin_tools, log_identifier
1023
+ )
1024
+ if instruction:
1025
+ injected_instructions.append(instruction)
1026
+ log.debug(
1027
+ "%s Prepared instructions for %d active built-in tools.",
1028
+ log_identifier,
1029
+ len(active_builtin_tools),
1030
+ )
1031
+
1032
+ peer_instructions = callback_context.state.get("peer_tool_instructions")
1033
+ if peer_instructions and isinstance(peer_instructions, str):
1034
+ injected_instructions.append(peer_instructions)
1035
+ log.debug(
1036
+ "%s Injected peer discovery instructions from callback state.",
1037
+ log_identifier,
1038
+ )
1039
+
1040
+ last_call_notification_message_added = False
1041
+ try:
1042
+ invocation_context = callback_context._invocation_context
1043
+ if invocation_context and invocation_context.run_config:
1044
+ current_llm_calls = (
1045
+ invocation_context._invocation_cost_manager._number_of_llm_calls
1046
+ )
1047
+ max_llm_calls = invocation_context.run_config.max_llm_calls
1048
+
1049
+ log.debug(
1050
+ "%s Checking for last LLM call: current_calls=%d, max_calls=%s",
1051
+ log_identifier,
1052
+ current_llm_calls,
1053
+ max_llm_calls,
1054
+ )
1055
+
1056
+ if (
1057
+ max_llm_calls
1058
+ and max_llm_calls > 0
1059
+ and current_llm_calls >= (max_llm_calls - 1)
1060
+ ):
1061
+ last_call_text = (
1062
+ "IMPORTANT: This is your final allowed interaction for the current request. "
1063
+ "Please inform the user that to continue this line of inquiry, they will need to "
1064
+ "make a new request or explicitly ask to continue if the interface supports it. "
1065
+ "Summarize your current findings and conclude your response."
1066
+ )
1067
+ if llm_request.contents is None:
1068
+ llm_request.contents = []
1069
+
1070
+ last_call_content = adk_types.Content(
1071
+ role="model",
1072
+ parts=[adk_types.Part(text=last_call_text)],
1073
+ )
1074
+ llm_request.contents.append(last_call_content)
1075
+ last_call_notification_message_added = True
1076
+ log.info(
1077
+ "%s Added 'last LLM call' notification as a 'model' message to llm_request.contents. Current calls (%d) reached max_llm_calls (%d).",
1078
+ log_identifier,
1079
+ current_llm_calls,
1080
+ max_llm_calls,
1081
+ )
1082
+ except Exception as e_last_call:
1083
+ log.error(
1084
+ "%s Error checking/injecting last LLM call notification message: %s",
1085
+ log_identifier,
1086
+ e_last_call,
1087
+ )
1088
+
1089
+ if host_component.get_config("inject_current_time", True):
1090
+ current_time = datetime.now(timezone.utc).strftime("%A, %d %b %Y %H:%M:%S UTC")
1091
+ instruction = f"Current time {current_time}."
1092
+ injected_instructions.append(instruction)
1093
+
1094
+ if injected_instructions:
1095
+ combined_instructions = "\n\n---\n\n".join(injected_instructions)
1096
+ if llm_request.config is None:
1097
+ log.warning(
1098
+ "%s llm_request.config is None, cannot append system instructions.",
1099
+ log_identifier,
1100
+ )
1101
+ else:
1102
+ if llm_request.config.system_instruction is None:
1103
+ llm_request.config.system_instruction = ""
1104
+
1105
+ if llm_request.config.system_instruction:
1106
+ llm_request.config.system_instruction += (
1107
+ "\n\n---\n\n" + combined_instructions
1108
+ )
1109
+ else:
1110
+ llm_request.config.system_instruction = combined_instructions
1111
+ log.info(
1112
+ "%s Injected %d dynamic instruction block(s) into llm_request.config.system_instruction.",
1113
+ log_identifier,
1114
+ len(injected_instructions),
1115
+ )
1116
+ elif not last_call_notification_message_added:
1117
+ log.debug(
1118
+ "%s No dynamic instructions (system or last_call message) were injected based on config.",
1119
+ log_identifier,
1120
+ )
1121
+
1122
+ return None
1123
+
1124
+
1125
+ async def after_tool_callback_inject_metadata(
1126
+ tool: BaseTool,
1127
+ args: Dict,
1128
+ tool_context: ToolContext,
1129
+ tool_response: Dict,
1130
+ host_component: "SamAgentComponent",
1131
+ ) -> Optional[Dict]:
1132
+ """
1133
+ ADK after_tool_callback to automatically load and inject metadata for
1134
+ newly created artifacts into the tool's response dictionary.
1135
+ """
1136
+ log_identifier = f"[Callback:InjectMetadata:{tool.name}]"
1137
+ log.info(
1138
+ "%s Starting metadata injection for tool response, type: %s",
1139
+ log_identifier,
1140
+ type(tool_response).__name__,
1141
+ )
1142
+
1143
+ if not host_component:
1144
+ log.error(
1145
+ "%s Host component instance not provided. Cannot proceed.",
1146
+ log_identifier,
1147
+ )
1148
+ return None
1149
+
1150
+ if not tool_context.actions.artifact_delta:
1151
+ log.debug(
1152
+ "%s No artifact delta found. Skipping metadata injection.", log_identifier
1153
+ )
1154
+ return None
1155
+
1156
+ artifact_service: Optional[BaseArtifactService] = (
1157
+ tool_context._invocation_context.artifact_service
1158
+ )
1159
+ if not artifact_service:
1160
+ log.error(
1161
+ "%s ArtifactService not available. Cannot load metadata.",
1162
+ log_identifier,
1163
+ )
1164
+ return None
1165
+
1166
+ app_name = tool_context._invocation_context.app_name
1167
+ user_id = tool_context._invocation_context.user_id
1168
+ session_id = get_original_session_id(tool_context._invocation_context)
1169
+
1170
+ metadata_texts = []
1171
+
1172
+ for filename, version in tool_context.actions.artifact_delta.items():
1173
+ if filename.endswith(METADATA_SUFFIX):
1174
+ log.debug(
1175
+ "%s Skipping metadata artifact '%s' itself.", log_identifier, filename
1176
+ )
1177
+ continue
1178
+
1179
+ metadata_filename = f"{filename}{METADATA_SUFFIX}"
1180
+ log.debug(
1181
+ "%s Found data artifact '%s' v%d. Attempting to load metadata '%s' v%d.",
1182
+ log_identifier,
1183
+ filename,
1184
+ version,
1185
+ metadata_filename,
1186
+ version,
1187
+ )
1188
+
1189
+ try:
1190
+ metadata_part = await artifact_service.load_artifact(
1191
+ app_name=app_name,
1192
+ user_id=user_id,
1193
+ session_id=session_id,
1194
+ filename=metadata_filename,
1195
+ version=version,
1196
+ )
1197
+
1198
+ if metadata_part and metadata_part.inline_data:
1199
+ try:
1200
+ metadata_dict = json.loads(
1201
+ metadata_part.inline_data.data.decode("utf-8")
1202
+ )
1203
+ metadata_dict["version"] = version
1204
+ formatted_text = format_metadata_for_llm(metadata_dict)
1205
+ metadata_texts.append(formatted_text)
1206
+ log.info(
1207
+ "%s Successfully loaded and formatted metadata for '%s' v%d.",
1208
+ log_identifier,
1209
+ filename,
1210
+ version,
1211
+ )
1212
+ except json.JSONDecodeError as json_err:
1213
+ log.warning(
1214
+ "%s Failed to parse metadata JSON for '%s' v%d: %s",
1215
+ log_identifier,
1216
+ metadata_filename,
1217
+ version,
1218
+ json_err,
1219
+ )
1220
+ except Exception as fmt_err:
1221
+ log.warning(
1222
+ "%s Failed to format metadata for '%s' v%d: %s",
1223
+ log_identifier,
1224
+ metadata_filename,
1225
+ version,
1226
+ fmt_err,
1227
+ )
1228
+ else:
1229
+ log.warning(
1230
+ "%s Companion metadata artifact '%s' v%d not found or empty.",
1231
+ log_identifier,
1232
+ metadata_filename,
1233
+ version,
1234
+ )
1235
+
1236
+ except Exception as load_err:
1237
+ log.error(
1238
+ "%s Error loading companion metadata artifact '%s' v%d: %s",
1239
+ log_identifier,
1240
+ metadata_filename,
1241
+ version,
1242
+ load_err,
1243
+ )
1244
+
1245
+ if metadata_texts:
1246
+ if not isinstance(tool_response, dict):
1247
+ log.error(
1248
+ "%s Tool response is not a dictionary. Cannot inject metadata. Type: %s",
1249
+ log_identifier,
1250
+ type(tool_response),
1251
+ )
1252
+ return None
1253
+
1254
+ combined_metadata_text = "\n\n".join(metadata_texts)
1255
+ tool_response[METADATA_RESPONSE_KEY] = combined_metadata_text
1256
+ log.info(
1257
+ "%s Injected metadata for %d artifact(s) into tool response key '%s'.",
1258
+ log_identifier,
1259
+ len(metadata_texts),
1260
+ METADATA_RESPONSE_KEY,
1261
+ )
1262
+ return tool_response
1263
+ else:
1264
+ log.debug(
1265
+ "%s No metadata loaded or formatted. Returning original tool response.",
1266
+ log_identifier,
1267
+ )
1268
+ return None
1269
+
1270
+
1271
+ async def track_produced_artifacts_callback(
1272
+ tool: BaseTool,
1273
+ args: Dict,
1274
+ tool_context: ToolContext,
1275
+ tool_response: Dict,
1276
+ host_component: "SamAgentComponent",
1277
+ ) -> Optional[Dict]:
1278
+ """
1279
+ ADK after_tool_callback to automatically track all artifacts created by a tool.
1280
+ It inspects the artifact_delta and registers the created artifacts in the
1281
+ TaskExecutionContext.
1282
+ """
1283
+ log_identifier = f"[Callback:TrackArtifacts:{tool.name}]"
1284
+ log.debug("%s Starting artifact tracking for tool response.", log_identifier)
1285
+
1286
+ if not tool_context.actions.artifact_delta:
1287
+ log.debug("%s No artifact delta found. Skipping tracking.", log_identifier)
1288
+ return None
1289
+
1290
+ if not host_component:
1291
+ log.error(
1292
+ "%s Host component instance not provided. Cannot proceed.", log_identifier
1293
+ )
1294
+ return None
1295
+
1296
+ try:
1297
+ a2a_context = tool_context.state.get("a2a_context", {})
1298
+ logical_task_id = a2a_context.get("logical_task_id")
1299
+ if not logical_task_id:
1300
+ log.warning(
1301
+ "%s Could not find logical_task_id in tool_context. Cannot track artifacts.",
1302
+ log_identifier,
1303
+ )
1304
+ return None
1305
+
1306
+ with host_component.active_tasks_lock:
1307
+ task_context = host_component.active_tasks.get(logical_task_id)
1308
+
1309
+ if not task_context:
1310
+ log.warning(
1311
+ "%s TaskExecutionContext not found for task %s. Cannot track artifacts.",
1312
+ log_identifier,
1313
+ logical_task_id,
1314
+ )
1315
+ return None
1316
+
1317
+ for filename, version in tool_context.actions.artifact_delta.items():
1318
+ if filename.endswith(METADATA_SUFFIX):
1319
+ continue
1320
+ log.info(
1321
+ "%s Registering produced artifact '%s' v%d for task %s.",
1322
+ log_identifier,
1323
+ filename,
1324
+ version,
1325
+ logical_task_id,
1326
+ )
1327
+ task_context.register_produced_artifact(filename, version)
1328
+
1329
+ except Exception as e:
1330
+ log.exception(
1331
+ "%s Error during artifact tracking callback: %s", log_identifier, e
1332
+ )
1333
+
1334
+ return None
1335
+
1336
+
1337
+ def log_streaming_chunk_callback(
1338
+ callback_context: CallbackContext,
1339
+ llm_response: LlmResponse,
1340
+ host_component: "SamAgentComponent",
1341
+ ) -> Optional[LlmResponse]:
1342
+ """
1343
+ ADK after_model_callback to log the content of each LLM response chunk
1344
+ *after* potential modification by other callbacks (like embed resolution).
1345
+ """
1346
+ log_identifier = "[Callback:LogChunk]"
1347
+ try:
1348
+ content_str = "None"
1349
+ is_partial = llm_response.partial
1350
+ is_final = llm_response.turn_complete
1351
+ if llm_response.content and llm_response.content.parts:
1352
+ texts = [p.text for p in llm_response.content.parts if p.text]
1353
+ content_str = '"' + "".join(texts) + '"' if texts else "[Non-text parts]"
1354
+ elif llm_response.error_message:
1355
+ content_str = f"[ERROR: {llm_response.error_message}]"
1356
+
1357
+ except Exception as e:
1358
+ log.error("%s Error logging LLM chunk: %s", log_identifier, e)
1359
+
1360
+ return None
1361
+
1362
+
1363
+ def solace_llm_invocation_callback(
1364
+ callback_context: CallbackContext,
1365
+ llm_request: LlmRequest,
1366
+ host_component: "SamAgentComponent",
1367
+ ) -> Optional[LlmResponse]:
1368
+ """
1369
+ ADK before_model_callback to send a Solace message when an LLM is invoked,
1370
+ using the host_component's process_and_publish_adk_event method.
1371
+ """
1372
+ log_identifier = "[Callback:SolaceLLMInvocation]"
1373
+ log.debug(
1374
+ "%s Running Solace LLM invocation notification callback...", log_identifier
1375
+ )
1376
+
1377
+ if not host_component:
1378
+ log.error(
1379
+ "%s Host component instance not provided. Cannot send Solace message.",
1380
+ log_identifier,
1381
+ )
1382
+ return None
1383
+
1384
+ try:
1385
+ from ...common.a2a_protocol import A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY
1386
+
1387
+ callback_context.state[A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY] = False
1388
+ log.debug(
1389
+ "%s Reset %s to False.", log_identifier, A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY
1390
+ )
1391
+ except Exception as e_flag_reset:
1392
+ log.error(
1393
+ "%s Error resetting %s: %s",
1394
+ log_identifier,
1395
+ A2A_LLM_STREAM_CHUNKS_PROCESSED_KEY,
1396
+ e_flag_reset,
1397
+ )
1398
+
1399
+ try:
1400
+ a2a_context = callback_context.state.get("a2a_context")
1401
+ if not a2a_context:
1402
+ log.error(
1403
+ "%s a2a_context not found in callback_context.state. Cannot send Solace message.",
1404
+ log_identifier,
1405
+ )
1406
+ return None
1407
+
1408
+ agent_name = host_component.get_config("agent_name", "unknown_agent")
1409
+ logical_task_id = a2a_context.get("logical_task_id")
1410
+
1411
+ llm_invocation_metadata = {
1412
+ "type": "llm_invocation",
1413
+ "data": llm_request.model_dump(exclude_none=True),
1414
+ }
1415
+ a2a_message = A2AMessage(
1416
+ role="agent",
1417
+ parts=[],
1418
+ metadata=llm_invocation_metadata,
1419
+ )
1420
+ task_status = TaskStatus(
1421
+ state=TaskState.WORKING,
1422
+ message=a2a_message,
1423
+ timestamp=datetime.now(timezone.utc),
1424
+ )
1425
+ status_update_event = TaskStatusUpdateEvent(
1426
+ id=logical_task_id,
1427
+ status=task_status,
1428
+ final=False,
1429
+ metadata={"agent_name": agent_name},
1430
+ )
1431
+
1432
+ loop = host_component.get_async_loop()
1433
+ if loop and loop.is_running():
1434
+ asyncio.run_coroutine_threadsafe(
1435
+ host_component._publish_status_update_with_buffer_flush(
1436
+ status_update_event,
1437
+ a2a_context,
1438
+ skip_buffer_flush=False,
1439
+ ),
1440
+ loop,
1441
+ )
1442
+ log.debug(
1443
+ "%s Scheduled LLM invocation status update with buffer flush.",
1444
+ log_identifier,
1445
+ )
1446
+ else:
1447
+ log.error(
1448
+ "%s Async loop not available. Cannot publish LLM invocation status update.",
1449
+ log_identifier,
1450
+ )
1451
+
1452
+ except Exception as e:
1453
+ log.error(
1454
+ "%s Error during Solace LLM invocation notification: %s", log_identifier, e
1455
+ )
1456
+
1457
+ return None
1458
+
1459
+
1460
+ def solace_llm_response_callback(
1461
+ callback_context: CallbackContext,
1462
+ llm_response: LlmResponse,
1463
+ host_component: "SamAgentComponent",
1464
+ ) -> Optional[LlmResponse]:
1465
+ """
1466
+ ADK after_model_callback to send a Solace message with the LLM's response,
1467
+ using the host_component's process_and_publish_adk_event method.
1468
+ """
1469
+ log_identifier = "[Callback:SolaceLLMResponse]"
1470
+ if llm_response.partial: # Don't send partial responses for this notification
1471
+ log.debug("%s Skipping partial response", log_identifier)
1472
+ return None
1473
+
1474
+ if not host_component:
1475
+ log.error(
1476
+ "%s Host component instance not provided. Cannot send Solace message.",
1477
+ log_identifier,
1478
+ )
1479
+ return None
1480
+
1481
+ try:
1482
+ a2a_context = callback_context.state.get("a2a_context")
1483
+ if not a2a_context:
1484
+ log.error(
1485
+ "%s a2a_context not found in callback_context.state. Cannot send Solace message.",
1486
+ log_identifier,
1487
+ )
1488
+ return None
1489
+
1490
+ agent_name = host_component.get_config("agent_name", "unknown_agent")
1491
+ logical_task_id = a2a_context.get("logical_task_id")
1492
+
1493
+ llm_response_metadata = {
1494
+ "type": "llm_response",
1495
+ "data": llm_response.model_dump(exclude_none=True),
1496
+ }
1497
+ a2a_message = A2AMessage(role="agent", parts=[], metadata=llm_response_metadata)
1498
+ task_status = TaskStatus(
1499
+ state=TaskState.WORKING,
1500
+ message=a2a_message,
1501
+ timestamp=datetime.now(timezone.utc),
1502
+ )
1503
+ status_update_event = TaskStatusUpdateEvent(
1504
+ id=logical_task_id,
1505
+ status=task_status,
1506
+ final=True,
1507
+ metadata={"agent_name": agent_name},
1508
+ )
1509
+ loop = host_component.get_async_loop()
1510
+ if loop and loop.is_running():
1511
+ asyncio.run_coroutine_threadsafe(
1512
+ host_component._publish_status_update_with_buffer_flush(
1513
+ status_update_event,
1514
+ a2a_context,
1515
+ skip_buffer_flush=False,
1516
+ ),
1517
+ loop,
1518
+ )
1519
+ log.debug(
1520
+ "%s Scheduled LLM response status update with buffer flush (final_chunk=%s).",
1521
+ log_identifier,
1522
+ llm_response.turn_complete,
1523
+ )
1524
+ else:
1525
+ log.error(
1526
+ "%s Async loop not available. Cannot publish LLM response status update.",
1527
+ log_identifier,
1528
+ )
1529
+
1530
+ except Exception as e:
1531
+ log.error(
1532
+ "%s Error during Solace LLM response notification: %s", log_identifier, e
1533
+ )
1534
+
1535
+ return None
1536
+
1537
+
1538
+ def notify_tool_invocation_start_callback(
1539
+ tool: BaseTool,
1540
+ args: Dict[str, Any],
1541
+ tool_context: ToolContext,
1542
+ host_component: "SamAgentComponent",
1543
+ ) -> None:
1544
+ """
1545
+ ADK before_tool_callback to send an A2A status message indicating
1546
+ that a tool is about to be invoked.
1547
+ """
1548
+ log_identifier = f"[Callback:NotifyToolInvocationStart:{tool.name}]"
1549
+ log.debug(
1550
+ "%s Triggered for tool '%s' with args: %s", log_identifier, tool.name, args
1551
+ )
1552
+
1553
+ if not host_component:
1554
+ log.error(
1555
+ "%s Host component instance not provided. Cannot send notification.",
1556
+ log_identifier,
1557
+ )
1558
+ return
1559
+
1560
+ a2a_context = tool_context.state.get("a2a_context")
1561
+ if not a2a_context:
1562
+ log.error(
1563
+ "%s a2a_context not found in tool_context.state. Cannot send notification.",
1564
+ log_identifier,
1565
+ )
1566
+ return
1567
+
1568
+ logical_task_id = a2a_context.get("logical_task_id")
1569
+
1570
+ try:
1571
+ serializable_args = {}
1572
+ for k, v in args.items():
1573
+ try:
1574
+ json.dumps(v)
1575
+ serializable_args[k] = v
1576
+ except TypeError:
1577
+ serializable_args[k] = str(v)
1578
+
1579
+ a2a_message_parts = []
1580
+ message_metadata = {
1581
+ "type": "tool_invocation_start",
1582
+ "data": {
1583
+ "tool_name": tool.name,
1584
+ "tool_args": serializable_args,
1585
+ "function_call_id": tool_context.function_call_id,
1586
+ },
1587
+ }
1588
+
1589
+ a2a_message = A2AMessage(
1590
+ role="agent", parts=a2a_message_parts, metadata=message_metadata
1591
+ )
1592
+
1593
+ task_status = TaskStatus(
1594
+ state=TaskState.WORKING,
1595
+ message=a2a_message,
1596
+ timestamp=datetime.now(timezone.utc),
1597
+ )
1598
+
1599
+ status_update_event = TaskStatusUpdateEvent(
1600
+ id=logical_task_id,
1601
+ status=task_status,
1602
+ final=False,
1603
+ metadata={"agent_name": host_component.get_config("agent_name")},
1604
+ )
1605
+
1606
+ loop = host_component.get_async_loop()
1607
+ if loop and loop.is_running():
1608
+
1609
+ asyncio.run_coroutine_threadsafe(
1610
+ host_component._publish_status_update_with_buffer_flush(
1611
+ status_update_event,
1612
+ a2a_context,
1613
+ skip_buffer_flush=False,
1614
+ ),
1615
+ loop,
1616
+ )
1617
+ log.debug(
1618
+ "%s Scheduled tool_invocation_start notification with buffer flush.",
1619
+ log_identifier,
1620
+ )
1621
+ else:
1622
+ log.error(
1623
+ "%s Async loop not available. Cannot publish tool_invocation_start notification.",
1624
+ log_identifier,
1625
+ )
1626
+
1627
+ except Exception as e:
1628
+ log.exception(
1629
+ "%s Error publishing tool_invocation_start status update: %s",
1630
+ log_identifier,
1631
+ e,
1632
+ )
1633
+
1634
+ return None
1635
+
1636
+
1637
+ def auto_continue_on_max_tokens_callback(
1638
+ callback_context: CallbackContext,
1639
+ llm_response: LlmResponse,
1640
+ host_component: "SamAgentComponent",
1641
+ ) -> Optional[LlmResponse]:
1642
+ """
1643
+ ADK after_model_callback to automatically continue an LLM response that
1644
+ was interrupted. This handles two interruption signals:
1645
+ 1. The explicit `llm_response.interrupted` flag from the ADK.
1646
+ 2. An implicit signal where the model itself calls a `_continue` tool.
1647
+ """
1648
+ log_identifier = "[Callback:AutoContinue]"
1649
+
1650
+ if not host_component.get_config("enable_auto_continuation", True):
1651
+ log.debug("%s Auto-continuation is disabled. Skipping.", log_identifier)
1652
+ return None
1653
+
1654
+ # An interruption is signaled by either the explicit flag or an implicit tool call.
1655
+ was_explicitly_interrupted = llm_response.interrupted
1656
+ was_implicitly_interrupted = False
1657
+ if llm_response.content and llm_response.content.parts:
1658
+ if any(
1659
+ p.function_call and p.function_call.name == "_continue"
1660
+ for p in llm_response.content.parts
1661
+ ):
1662
+ was_implicitly_interrupted = True
1663
+
1664
+ if not was_explicitly_interrupted and not was_implicitly_interrupted:
1665
+ return None
1666
+
1667
+ log.info(
1668
+ "%s Interruption signal detected (explicit: %s, implicit: %s). Triggering auto-continuation.",
1669
+ log_identifier,
1670
+ was_explicitly_interrupted,
1671
+ was_implicitly_interrupted,
1672
+ )
1673
+
1674
+ # Get existing parts from the response, but filter out any `_continue` calls
1675
+ # the model might have added.
1676
+ existing_parts = []
1677
+ if llm_response.content and llm_response.content.parts:
1678
+ existing_parts = [
1679
+ p
1680
+ for p in llm_response.content.parts
1681
+ if not (p.function_call and p.function_call.name == "_continue")
1682
+ ]
1683
+ if was_implicitly_interrupted:
1684
+ log.debug(
1685
+ "%s Removed implicit '_continue' tool call from response parts.",
1686
+ log_identifier,
1687
+ )
1688
+
1689
+ continue_tool_call = adk_types.FunctionCall(
1690
+ name="_continue_generation",
1691
+ args={},
1692
+ id=f"host-continue-{uuid.uuid4()}",
1693
+ )
1694
+ continue_part = adk_types.Part(function_call=continue_tool_call)
1695
+
1696
+ all_parts = existing_parts + [continue_part]
1697
+
1698
+ # If there was no text content in the interrupted part, add a space to ensure
1699
+ # the event is not filtered out by history processing logic.
1700
+ if not any(p.text for p in existing_parts):
1701
+ all_parts.insert(0, adk_types.Part(text=" "))
1702
+ log.debug(
1703
+ "%s Prepended empty text part to ensure event is preserved.", log_identifier
1704
+ )
1705
+
1706
+ # Create a new, non-interrupted LlmResponse containing all parts.
1707
+ # This ensures the partial text is saved to history and the tool call is executed.
1708
+ hijacked_response = LlmResponse(
1709
+ content=adk_types.Content(role="model", parts=all_parts),
1710
+ partial=False,
1711
+ custom_metadata={
1712
+ "was_interrupted": True,
1713
+ },
1714
+ )
1715
+
1716
+ return hijacked_response