ruby_llm 2.1.1 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (523) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +160 -120
  3. data/app/assets/javascripts/ruby_llm/chat_message_controller.js +163 -0
  4. data/app/assets/javascripts/ruby_llm/dompurify.js +9 -0
  5. data/app/assets/javascripts/ruby_llm/marked.js +71 -0
  6. data/config/importmap.rb +5 -0
  7. data/lib/generators/ruby_llm/agent/agent_generator.rb +11 -3
  8. data/lib/generators/ruby_llm/agent/templates/agent.rb.tt +6 -3
  9. data/lib/generators/ruby_llm/chat_ui/chat_ui_generator.rb +201 -55
  10. data/lib/generators/ruby_llm/chat_ui/templates/agents/chat_agent.rb.tt +15 -0
  11. data/lib/generators/ruby_llm/chat_ui/templates/controllers/answers_controller.rb.tt +31 -0
  12. data/lib/generators/ruby_llm/chat_ui/templates/controllers/chats_controller.rb.tt +16 -5
  13. data/lib/generators/ruby_llm/chat_ui/templates/controllers/messages_controller.rb.tt +27 -9
  14. data/lib/generators/ruby_llm/chat_ui/templates/controllers/models_controller.rb.tt +1 -1
  15. data/lib/generators/ruby_llm/chat_ui/templates/controllers/queued_messages_controller.rb.tt +14 -0
  16. data/lib/generators/ruby_llm/chat_ui/templates/javascript/chat_message_controller.js +1 -0
  17. data/lib/generators/ruby_llm/chat_ui/templates/jobs/chat_response_job.rb.tt +5 -8
  18. data/lib/generators/ruby_llm/chat_ui/templates/jobs/resume_interrupted_chats_job.rb.tt +7 -0
  19. data/lib/generators/ruby_llm/chat_ui/templates/migrations/add_reply_error_migration.rb.tt +5 -0
  20. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/_files.html.erb.tt +15 -0
  21. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/_form.html.erb.tt +5 -7
  22. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/_model_select.html.erb.tt +7 -0
  23. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/chats/show.html.erb.tt +32 -3
  24. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_assistant.html.erb.tt +17 -2
  25. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_attachments.html.erb.tt +14 -0
  26. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_error.html.erb.tt +1 -1
  27. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_form.html.erb.tt +28 -18
  28. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_queued_message.html.erb.tt +18 -0
  29. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_system.html.erb.tt +1 -1
  30. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_tool_calls.html.erb.tt +4 -1
  31. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/_user.html.erb.tt +4 -2
  32. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/tool_calls/_default.html.erb.tt +1 -1
  33. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/messages/tool_results/_default.html.erb.tt +4 -2
  34. data/lib/generators/ruby_llm/chat_ui/templates/tailwind/views/questions/_question.html.erb.tt +102 -0
  35. data/lib/generators/ruby_llm/chat_ui/templates/test/agents/chat_agent_test.rb.tt +28 -0
  36. data/lib/generators/ruby_llm/chat_ui/templates/test/application_system_test_case.rb.tt +5 -0
  37. data/lib/generators/ruby_llm/chat_ui/templates/test/system/chats_test.rb.tt +240 -0
  38. data/lib/generators/ruby_llm/chat_ui/templates/views/answers/create.turbo_stream.erb.tt +6 -0
  39. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_files.html.erb.tt +15 -0
  40. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_form.html.erb.tt +5 -7
  41. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_model_select.html.erb.tt +7 -0
  42. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/_questions.html.erb.tt +13 -0
  43. data/lib/generators/ruby_llm/chat_ui/templates/views/chats/show.html.erb.tt +29 -3
  44. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_assistant.html.erb.tt +16 -2
  45. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_attachments.html.erb.tt +14 -0
  46. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_error.html.erb.tt +1 -1
  47. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_form.html.erb.tt +26 -16
  48. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_queued_message.html.erb.tt +8 -0
  49. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_system.html.erb.tt +1 -1
  50. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_tool_calls.html.erb.tt +4 -1
  51. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/_user.html.erb.tt +3 -2
  52. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/create.turbo_stream.erb.tt +1 -0
  53. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/tool_calls/_default.html.erb.tt +1 -1
  54. data/lib/generators/ruby_llm/chat_ui/templates/views/messages/tool_results/_default.html.erb.tt +3 -2
  55. data/lib/generators/ruby_llm/chat_ui/templates/views/questions/_question.html.erb.tt +79 -0
  56. data/lib/generators/ruby_llm/evaluation/evaluation_generator.rb +43 -0
  57. data/lib/generators/ruby_llm/evaluation/templates/evaluation.rb.tt +9 -0
  58. data/lib/generators/ruby_llm/evaluation/templates/evaluation.yml.tt +4 -0
  59. data/lib/generators/ruby_llm/generator_helpers.rb +116 -3
  60. data/lib/generators/ruby_llm/install/install_generator.rb +5 -47
  61. data/lib/generators/ruby_llm/install/templates/create_chats_migration.rb.tt +4 -0
  62. data/lib/generators/ruby_llm/install/templates/create_messages_migration.rb.tt +5 -3
  63. data/lib/generators/ruby_llm/install/templates/create_ruby_llm_records_migration.rb.tt +41 -1
  64. data/lib/generators/ruby_llm/install/templates/initializer.rb.tt +2 -1
  65. data/lib/generators/ruby_llm/install/templates/message_model.rb.tt +1 -2
  66. data/lib/generators/ruby_llm/judge/judge_generator.rb +35 -0
  67. data/lib/generators/ruby_llm/judge/templates/judge.rb.tt +2 -0
  68. data/lib/generators/ruby_llm/mcp/mcp_generator.rb +58 -0
  69. data/lib/generators/ruby_llm/mcp/templates/mcp.rb.tt +7 -0
  70. data/lib/generators/ruby_llm/provider/scaffold.rb +5 -12
  71. data/lib/generators/ruby_llm/provider/templates/core/provider.rb.erb +6 -0
  72. data/lib/generators/ruby_llm/provider/templates/gem/chat_spec.rb.erb +0 -1
  73. data/lib/generators/ruby_llm/provider/templates/gem/chat_streaming_spec.rb.erb +0 -1
  74. data/lib/generators/ruby_llm/provider/templates/gem/moderation_spec.rb.erb +1 -1
  75. data/lib/generators/ruby_llm/sandbox/sandbox_generator.rb +68 -0
  76. data/lib/generators/ruby_llm/sandbox/templates/sandbox.rb.tt +3 -0
  77. data/lib/generators/ruby_llm/skill/skill_generator.rb +48 -0
  78. data/lib/generators/ruby_llm/tool/templates/tailwind/tool_call.html.erb.tt +1 -1
  79. data/lib/generators/ruby_llm/tool/templates/tailwind/tool_result.html.erb.tt +1 -1
  80. data/lib/generators/ruby_llm/tool/templates/tool.rb.tt +0 -3
  81. data/lib/generators/ruby_llm/tool/templates/tool_call.html.erb.tt +1 -1
  82. data/lib/generators/ruby_llm/tool/templates/tool_result.html.erb.tt +1 -1
  83. data/lib/generators/ruby_llm/tool/tool_generator.rb +1 -5
  84. data/lib/generators/ruby_llm/upgrade/templates/upgrade_ruby_llm_to_2_2.rb.tt +164 -0
  85. data/lib/generators/ruby_llm/upgrade/upgrade_generator.rb +41 -3
  86. data/lib/ruby_llm/accounting/operation.rb +73 -0
  87. data/lib/ruby_llm/accounting/usage.rb +74 -24
  88. data/lib/ruby_llm/active_record/acts_as.rb +78 -11
  89. data/lib/ruby_llm/active_record/attachment_helpers.rb +10 -4
  90. data/lib/ruby_llm/active_record/batch.rb +67 -9
  91. data/lib/ruby_llm/active_record/chat_agent_hooks.rb +17 -0
  92. data/lib/ruby_llm/active_record/chat_approvals.rb +74 -0
  93. data/lib/ruby_llm/active_record/chat_cancellation.rb +106 -0
  94. data/lib/ruby_llm/active_record/chat_column_guard.rb +51 -0
  95. data/lib/ruby_llm/active_record/chat_file.rb +37 -0
  96. data/lib/ruby_llm/active_record/chat_files.rb +30 -0
  97. data/lib/ruby_llm/active_record/chat_helper.rb +73 -0
  98. data/lib/ruby_llm/active_record/chat_methods.rb +516 -297
  99. data/lib/ruby_llm/active_record/chat_placeholders.rb +67 -0
  100. data/lib/ruby_llm/active_record/chat_questions.rb +177 -0
  101. data/lib/ruby_llm/active_record/chat_queue.rb +133 -0
  102. data/lib/ruby_llm/active_record/chat_reply_error.rb +35 -0
  103. data/lib/ruby_llm/active_record/chat_run.rb +51 -0
  104. data/lib/ruby_llm/active_record/chat_tool_call_state.rb +77 -0
  105. data/lib/ruby_llm/active_record/chunk_methods.rb +49 -0
  106. data/lib/ruby_llm/active_record/claim_heartbeat.rb +149 -0
  107. data/lib/ruby_llm/active_record/file_store.rb +269 -0
  108. data/lib/ruby_llm/active_record/mcp_credential.rb +24 -9
  109. data/lib/ruby_llm/active_record/message_broadcasting.rb +85 -0
  110. data/lib/ruby_llm/active_record/message_methods.rb +83 -11
  111. data/lib/ruby_llm/active_record/model.rb +37 -9
  112. data/lib/ruby_llm/active_record/provider_file.rb +21 -4
  113. data/lib/ruby_llm/active_record/queued_message.rb +50 -0
  114. data/lib/ruby_llm/active_record/record.rb +3 -1
  115. data/lib/ruby_llm/active_record/run_claims.rb +636 -0
  116. data/lib/ruby_llm/active_record/solid_queue_shutdown.rb +33 -0
  117. data/lib/ruby_llm/active_record/system_instructions.rb +30 -0
  118. data/lib/ruby_llm/active_record/tool_call.rb +55 -4
  119. data/lib/ruby_llm/active_record/usage.rb +79 -9
  120. data/lib/ruby_llm/agent/callbacks.rb +98 -0
  121. data/lib/ruby_llm/agent/context_budget.rb +26 -0
  122. data/lib/ruby_llm/agent/join_callbacks.rb +28 -0
  123. data/lib/ruby_llm/agent/model_options.rb +56 -0
  124. data/lib/ruby_llm/agent/ownership.rb +22 -0
  125. data/lib/ruby_llm/agent/tool_conversion.rb +19 -0
  126. data/lib/ruby_llm/agent.rb +621 -236
  127. data/lib/ruby_llm/agent_tool.rb +91 -0
  128. data/lib/ruby_llm/aliases.json +93 -91
  129. data/lib/ruby_llm/attachment.rb +101 -20
  130. data/lib/ruby_llm/batch.rb +132 -46
  131. data/lib/ruby_llm/cached_content.rb +4 -4
  132. data/lib/ruby_llm/chat/agent_run.rb +22 -0
  133. data/lib/ruby_llm/chat/context_budget.rb +119 -0
  134. data/lib/ruby_llm/chat/file_references.rb +256 -0
  135. data/lib/ruby_llm/chat/message_queue.rb +56 -0
  136. data/lib/ruby_llm/chat/ownership.rb +18 -0
  137. data/lib/ruby_llm/chat/rescuable.rb +48 -0
  138. data/lib/ruby_llm/chat/run.rb +32 -0
  139. data/lib/ruby_llm/chat/tool_call_ids.rb +46 -0
  140. data/lib/ruby_llm/chat/tool_call_state.rb +126 -0
  141. data/lib/ruby_llm/chat/tool_concurrency.rb +90 -49
  142. data/lib/ruby_llm/chat/tool_search.rb +36 -1
  143. data/lib/ruby_llm/chat/tool_sequences.rb +84 -0
  144. data/lib/ruby_llm/chat.rb +1671 -334
  145. data/lib/ruby_llm/chunk.rb +27 -1
  146. data/lib/ruby_llm/citation.rb +2 -1
  147. data/lib/ruby_llm/code_execution/language.rb +28 -0
  148. data/lib/ruby_llm/code_execution/node_installation.rb +56 -0
  149. data/lib/ruby_llm/code_execution/packages.rb +241 -0
  150. data/lib/ruby_llm/code_execution/ruby_installation.rb +170 -0
  151. data/lib/ruby_llm/code_execution/workspace.rb +314 -0
  152. data/lib/ruby_llm/code_execution.rb +781 -0
  153. data/lib/ruby_llm/configuration.rb +152 -5
  154. data/lib/ruby_llm/content_filter.rb +68 -0
  155. data/lib/ruby_llm/cost.rb +68 -41
  156. data/lib/ruby_llm/downloaded_file.rb +10 -9
  157. data/lib/ruby_llm/embedding.rb +30 -45
  158. data/lib/ruby_llm/embedding_request.rb +1 -1
  159. data/lib/ruby_llm/error.rb +167 -14
  160. data/lib/ruby_llm/evaluation/dataset.rb +5 -4
  161. data/lib/ruby_llm/evaluation/evidence.rb +5 -3
  162. data/lib/ruby_llm/evaluation/report.rb +7 -3
  163. data/lib/ruby_llm/evaluation/result.rb +1 -1
  164. data/lib/ruby_llm/evaluation.rb +2 -1
  165. data/lib/ruby_llm/fallback.rb +32 -4
  166. data/lib/ruby_llm/file_store/converters/ocr.rb +28 -0
  167. data/lib/ruby_llm/file_store/converters/office/markup.rb +148 -0
  168. data/lib/ruby_llm/file_store/converters/office/outline.rb +193 -0
  169. data/lib/ruby_llm/file_store/converters/office/prose.rb +161 -0
  170. data/lib/ruby_llm/file_store/converters/office/workbook.rb +187 -0
  171. data/lib/ruby_llm/file_store/converters/office/zip.rb +149 -0
  172. data/lib/ruby_llm/file_store/converters/office.rb +106 -0
  173. data/lib/ruby_llm/file_store/converters/text.rb +27 -0
  174. data/lib/ruby_llm/file_store/converters.rb +84 -0
  175. data/lib/ruby_llm/file_store/directory.rb +190 -0
  176. data/lib/ruby_llm/file_store/memory.rb +75 -0
  177. data/lib/ruby_llm/file_store/tools/base.rb +99 -0
  178. data/lib/ruby_llm/file_store/tools/delete.rb +48 -0
  179. data/lib/ruby_llm/file_store/tools/edit.rb +155 -0
  180. data/lib/ruby_llm/file_store/tools/list.rb +57 -0
  181. data/lib/ruby_llm/file_store/tools/read.rb +126 -0
  182. data/lib/ruby_llm/file_store/tools/search.rb +208 -0
  183. data/lib/ruby_llm/file_store/tools/write.rb +40 -0
  184. data/lib/ruby_llm/file_store/tools.rb +39 -0
  185. data/lib/ruby_llm/file_store.rb +372 -0
  186. data/lib/ruby_llm/files/saving.rb +14 -0
  187. data/lib/ruby_llm/image.rb +67 -45
  188. data/lib/ruby_llm/input_required_error.rb +28 -0
  189. data/lib/ruby_llm/judge/builder.rb +6 -1
  190. data/lib/ruby_llm/judge.rb +1 -1
  191. data/lib/ruby_llm/judgment.rb +6 -24
  192. data/lib/ruby_llm/mcp/client.rb +50 -24
  193. data/lib/ruby_llm/mcp/collection.rb +39 -2
  194. data/lib/ruby_llm/mcp/content.rb +7 -2
  195. data/lib/ruby_llm/mcp/error.rb +8 -2
  196. data/lib/ruby_llm/mcp/http.rb +117 -37
  197. data/lib/ruby_llm/mcp/input_request.rb +3 -112
  198. data/lib/ruby_llm/mcp/input_required_error.rb +3 -21
  199. data/lib/ruby_llm/mcp/insufficient_scope_error.rb +27 -0
  200. data/lib/ruby_llm/mcp/listener.rb +7 -1
  201. data/lib/ruby_llm/mcp/oauth.rb +102 -39
  202. data/lib/ruby_llm/mcp/outages.rb +172 -0
  203. data/lib/ruby_llm/mcp/resource.rb +9 -6
  204. data/lib/ruby_llm/mcp/resource_template.rb +3 -2
  205. data/lib/ruby_llm/mcp/session_expired.rb +10 -2
  206. data/lib/ruby_llm/mcp/stdio.rb +66 -18
  207. data/lib/ruby_llm/mcp/task.rb +13 -40
  208. data/lib/ruby_llm/mcp/tool.rb +34 -11
  209. data/lib/ruby_llm/mcp.rb +123 -59
  210. data/lib/ruby_llm/message.rb +217 -23
  211. data/lib/ruby_llm/model/modalities.rb +1 -1
  212. data/lib/ruby_llm/model/pricing.rb +26 -4
  213. data/lib/ruby_llm/model/pricing_category.rb +38 -15
  214. data/lib/ruby_llm/model/pricing_tier.rb +3 -2
  215. data/lib/ruby_llm/model.rb +24 -6
  216. data/lib/ruby_llm/models/lookup.rb +37 -4
  217. data/lib/ruby_llm/models/registry.rb +4 -19
  218. data/lib/ruby_llm/models.json +49086 -39251
  219. data/lib/ruby_llm/models.rb +66 -38
  220. data/lib/ruby_llm/moderation.rb +10 -30
  221. data/lib/ruby_llm/ocr.rb +5 -22
  222. data/lib/ruby_llm/open_telemetry/attributes.rb +68 -19
  223. data/lib/ruby_llm/open_telemetry/content.rb +105 -0
  224. data/lib/ruby_llm/open_telemetry.rb +18 -4
  225. data/lib/ruby_llm/prompt/context.rb +2 -2
  226. data/lib/ruby_llm/prompt.rb +7 -5
  227. data/lib/ruby_llm/protocol/binary_streaming.rb +2 -8
  228. data/lib/ruby_llm/protocol/cache_lifetime.rb +22 -0
  229. data/lib/ruby_llm/protocol/native_content.rb +69 -0
  230. data/lib/ruby_llm/protocol/replayed_arguments.rb +28 -0
  231. data/lib/ruby_llm/protocol/stream_accumulator.rb +73 -33
  232. data/lib/ruby_llm/protocol/streaming.rb +105 -22
  233. data/lib/ruby_llm/protocol/tool_namespaces.rb +100 -0
  234. data/lib/ruby_llm/protocol/unsupported_operations.rb +42 -0
  235. data/lib/ruby_llm/protocol.rb +127 -106
  236. data/lib/ruby_llm/protocols/anthropic/batches.rb +41 -4
  237. data/lib/ruby_llm/protocols/anthropic/block_binding.rb +30 -0
  238. data/lib/ruby_llm/protocols/anthropic/chat.rb +135 -60
  239. data/lib/ruby_llm/protocols/anthropic/files.rb +9 -0
  240. data/lib/ruby_llm/protocols/anthropic/media.rb +14 -14
  241. data/lib/ruby_llm/protocols/anthropic/models.rb +16 -10
  242. data/lib/ruby_llm/protocols/anthropic/streaming.rb +43 -22
  243. data/lib/ruby_llm/protocols/anthropic/tools.rb +36 -24
  244. data/lib/ruby_llm/protocols/anthropic.rb +61 -6
  245. data/lib/ruby_llm/protocols/azure/speech/transcription.rb +125 -0
  246. data/lib/ruby_llm/protocols/azure/speech.rb +14 -0
  247. data/lib/ruby_llm/protocols/bedrock/batches.rb +26 -8
  248. data/lib/ruby_llm/protocols/bedrock/event_stream.rb +205 -0
  249. data/lib/ruby_llm/protocols/bedrock/files.rb +15 -6
  250. data/lib/ruby_llm/protocols/bedrock/guardrails.rb +1 -1
  251. data/lib/ruby_llm/protocols/bedrock/rerank.rb +2 -0
  252. data/lib/ruby_llm/protocols/chat_completions/chat.rb +158 -58
  253. data/lib/ruby_llm/protocols/chat_completions/content_filters.rb +45 -0
  254. data/lib/ruby_llm/protocols/chat_completions/embedding_batches.rb +15 -15
  255. data/lib/ruby_llm/protocols/chat_completions/embeddings.rb +26 -3
  256. data/lib/ruby_llm/protocols/chat_completions/images.rb +7 -9
  257. data/lib/ruby_llm/protocols/chat_completions/media.rb +20 -20
  258. data/lib/ruby_llm/protocols/chat_completions/moderation.rb +8 -5
  259. data/lib/ruby_llm/protocols/chat_completions/speech.rb +8 -2
  260. data/lib/ruby_llm/protocols/chat_completions/streaming.rb +38 -17
  261. data/lib/ruby_llm/protocols/chat_completions/think_tags.rb +148 -0
  262. data/lib/ruby_llm/protocols/chat_completions/tools.rb +15 -28
  263. data/lib/ruby_llm/protocols/chat_completions/transcription.rb +17 -2
  264. data/lib/ruby_llm/protocols/chat_completions.rb +4 -0
  265. data/lib/ruby_llm/protocols/cohere/batches.rb +15 -8
  266. data/lib/ruby_llm/protocols/cohere/chat.rb +47 -25
  267. data/lib/ruby_llm/protocols/cohere/datasets.rb +6 -2
  268. data/lib/ruby_llm/protocols/cohere/embeddings.rb +7 -5
  269. data/lib/ruby_llm/protocols/cohere/media.rb +7 -7
  270. data/lib/ruby_llm/protocols/cohere/rerank.rb +2 -1
  271. data/lib/ruby_llm/protocols/cohere/streaming.rb +26 -11
  272. data/lib/ruby_llm/protocols/cohere/tools.rb +17 -28
  273. data/lib/ruby_llm/protocols/cohere/transcription.rb +5 -4
  274. data/lib/ruby_llm/protocols/converse/batches.rb +2 -24
  275. data/lib/ruby_llm/protocols/converse/chat.rb +210 -75
  276. data/lib/ruby_llm/protocols/converse/media.rb +24 -22
  277. data/lib/ruby_llm/protocols/converse/reasoning.rb +23 -9
  278. data/lib/ruby_llm/protocols/converse/streaming.rb +68 -220
  279. data/lib/ruby_llm/protocols/converse.rb +17 -1
  280. data/lib/ruby_llm/protocols/deepgram/speech.rb +1 -2
  281. data/lib/ruby_llm/protocols/deepgram/streaming_transcription.rb +1 -3
  282. data/lib/ruby_llm/protocols/deepgram/transcription.rb +28 -19
  283. data/lib/ruby_llm/protocols/deepgram.rb +3 -2
  284. data/lib/ruby_llm/protocols/deepseek/files.rb +1 -1
  285. data/lib/ruby_llm/protocols/elevenlabs/assets.rb +6 -4
  286. data/lib/ruby_llm/protocols/elevenlabs/flows/images.rb +4 -0
  287. data/lib/ruby_llm/protocols/elevenlabs/speech.rb +3 -3
  288. data/lib/ruby_llm/protocols/elevenlabs/streaming_transcription.rb +5 -2
  289. data/lib/ruby_llm/protocols/elevenlabs/transcription.rb +6 -3
  290. data/lib/ruby_llm/protocols/files.rb +27 -12
  291. data/lib/ruby_llm/protocols/gemini/batches.rb +74 -5
  292. data/lib/ruby_llm/protocols/gemini/caches.rb +4 -4
  293. data/lib/ruby_llm/protocols/gemini/chat.rb +118 -51
  294. data/lib/ruby_llm/protocols/gemini/embedding_batches.rb +9 -5
  295. data/lib/ruby_llm/protocols/gemini/embeddings.rb +3 -3
  296. data/lib/ruby_llm/protocols/gemini/file_transcription.rb +4 -0
  297. data/lib/ruby_llm/protocols/gemini/files.rb +5 -4
  298. data/lib/ruby_llm/protocols/gemini/images.rb +11 -9
  299. data/lib/ruby_llm/protocols/gemini/live_transcription.rb +1 -1
  300. data/lib/ruby_llm/protocols/gemini/long_decode.rb +104 -0
  301. data/lib/ruby_llm/protocols/gemini/media.rb +31 -28
  302. data/lib/ruby_llm/protocols/gemini/retry_info.rb +29 -0
  303. data/lib/ruby_llm/protocols/gemini/schemas.rb +126 -0
  304. data/lib/ruby_llm/protocols/gemini/speech.rb +9 -3
  305. data/lib/ruby_llm/protocols/gemini/streaming.rb +37 -13
  306. data/lib/ruby_llm/protocols/gemini/tools.rb +18 -10
  307. data/lib/ruby_llm/protocols/gemini/transcription.rb +13 -9
  308. data/lib/ruby_llm/protocols/gemini/videos.rb +14 -3
  309. data/lib/ruby_llm/protocols/gemini.rb +16 -0
  310. data/lib/ruby_llm/protocols/gpustack/responses.rb +3 -3
  311. data/lib/ruby_llm/protocols/gpustack/tokenization.rb +14 -0
  312. data/lib/ruby_llm/protocols/interactions/chat.rb +29 -14
  313. data/lib/ruby_llm/protocols/interactions/content.rb +19 -14
  314. data/lib/ruby_llm/protocols/interactions/streaming.rb +1 -1
  315. data/lib/ruby_llm/protocols/interactions/tools.rb +10 -13
  316. data/lib/ruby_llm/protocols/interactions/videos.rb +99 -0
  317. data/lib/ruby_llm/protocols/interactions.rb +1 -0
  318. data/lib/ruby_llm/protocols/invoke_model/cohere_embeddings.rb +28 -10
  319. data/lib/ruby_llm/protocols/invoke_model/embedding_batches.rb +1 -1
  320. data/lib/ruby_llm/protocols/invoke_model/nova_embeddings.rb +45 -11
  321. data/lib/ruby_llm/protocols/invoke_model/stability_images.rb +5 -2
  322. data/lib/ruby_llm/protocols/invoke_model/titan_multimodal_embeddings.rb +11 -3
  323. data/lib/ruby_llm/protocols/invoke_model.rb +21 -2
  324. data/lib/ruby_llm/protocols/meta/transcription/stream.rb +108 -0
  325. data/lib/ruby_llm/protocols/meta/transcription.rb +76 -0
  326. data/lib/ruby_llm/protocols/minimax/files.rb +58 -0
  327. data/lib/ruby_llm/protocols/minimax/images.rb +54 -0
  328. data/lib/ruby_llm/protocols/minimax/speech.rb +53 -0
  329. data/lib/ruby_llm/protocols/minimax/transcription.rb +82 -0
  330. data/lib/ruby_llm/protocols/minimax/videos.rb +93 -0
  331. data/lib/ruby_llm/protocols/minimax.rb +21 -0
  332. data/lib/ruby_llm/protocols/mistral/conversations/chat.rb +44 -21
  333. data/lib/ruby_llm/protocols/mistral/files.rb +6 -4
  334. data/lib/ruby_llm/protocols/mistral/multi_completion.rb +15 -4
  335. data/lib/ruby_llm/protocols/moonshot/files.rb +48 -0
  336. data/lib/ruby_llm/protocols/openai/batches.rb +3 -2
  337. data/lib/ruby_llm/protocols/openai/decisions.rb +10 -6
  338. data/lib/ruby_llm/protocols/openai/files.rb +13 -6
  339. data/lib/ruby_llm/protocols/openrouter/batches.rb +13 -17
  340. data/lib/ruby_llm/protocols/openrouter/responses.rb +6 -10
  341. data/lib/ruby_llm/protocols/perplexity/agent.rb +19 -35
  342. data/lib/ruby_llm/protocols/perplexity/batches.rb +37 -0
  343. data/lib/ruby_llm/protocols/perplexity/files.rb +19 -9
  344. data/lib/ruby_llm/protocols/responses/approvals.rb +12 -11
  345. data/lib/ruby_llm/protocols/responses/chat.rb +165 -75
  346. data/lib/ruby_llm/protocols/responses/citation_markers.rb +167 -0
  347. data/lib/ruby_llm/protocols/responses/compaction.rb +2 -2
  348. data/lib/ruby_llm/protocols/responses/media.rb +17 -9
  349. data/lib/ruby_llm/protocols/responses/streaming.rb +71 -12
  350. data/lib/ruby_llm/protocols/responses/token_counting.rb +1 -1
  351. data/lib/ruby_llm/protocols/responses/tools.rb +48 -1
  352. data/lib/ruby_llm/protocols/responses.rb +21 -0
  353. data/lib/ruby_llm/protocols/system_one/responses.rb +12 -5
  354. data/lib/ruby_llm/protocols/vertexai/files.rb +4 -3
  355. data/lib/ruby_llm/protocols/vertexai/text_to_speech.rb +116 -0
  356. data/lib/ruby_llm/protocols/xai/files.rb +13 -6
  357. data/lib/ruby_llm/protocols/zai/files.rb +37 -0
  358. data/lib/ruby_llm/provider.rb +128 -31
  359. data/lib/ruby_llm/providers/anthropic.rb +5 -1
  360. data/lib/ruby_llm/providers/azure/chat.rb +17 -3
  361. data/lib/ruby_llm/providers/azure/chat_completions.rb +1 -0
  362. data/lib/ruby_llm/providers/azure/decisions.rb +17 -0
  363. data/lib/ruby_llm/providers/azure/embeddings.rb +1 -2
  364. data/lib/ruby_llm/providers/azure/media.rb +6 -6
  365. data/lib/ruby_llm/providers/azure/mistral.rb +17 -0
  366. data/lib/ruby_llm/providers/azure/models.rb +16 -2
  367. data/lib/ruby_llm/providers/azure/moderation.rb +16 -0
  368. data/lib/ruby_llm/providers/azure/responses.rb +9 -0
  369. data/lib/ruby_llm/providers/azure/videos.rb +2 -2
  370. data/lib/ruby_llm/providers/azure.rb +43 -1
  371. data/lib/ruby_llm/providers/bedrock/anthropic/batches.rb +32 -0
  372. data/lib/ruby_llm/providers/bedrock/anthropic.rb +134 -0
  373. data/lib/ruby_llm/providers/bedrock/auth.rb +12 -3
  374. data/lib/ruby_llm/providers/bedrock/connection.rb +19 -0
  375. data/lib/ruby_llm/providers/bedrock/mantle/anthropic.rb +5 -1
  376. data/lib/ruby_llm/providers/bedrock/mantle/chat_completions.rb +2 -2
  377. data/lib/ruby_llm/providers/bedrock/mantle/responses.rb +12 -2
  378. data/lib/ruby_llm/providers/bedrock/mantle/voxtral.rb +1 -1
  379. data/lib/ruby_llm/providers/bedrock/mantle.rb +7 -0
  380. data/lib/ruby_llm/providers/bedrock/models.rb +1 -7
  381. data/lib/ruby_llm/providers/bedrock.rb +51 -8
  382. data/lib/ruby_llm/providers/cohere.rb +5 -1
  383. data/lib/ruby_llm/providers/deepgram/chat.rb +25 -0
  384. data/lib/ruby_llm/providers/deepgram.rb +21 -2
  385. data/lib/ruby_llm/providers/deepseek/chat.rb +7 -16
  386. data/lib/ruby_llm/providers/deepseek/responses.rb +25 -8
  387. data/lib/ruby_llm/providers/deepseek.rb +5 -1
  388. data/lib/ruby_llm/providers/elevenlabs.rb +2 -1
  389. data/lib/ruby_llm/providers/gemini.rb +13 -1
  390. data/lib/ruby_llm/providers/gpustack/chat.rb +23 -3
  391. data/lib/ruby_llm/providers/gpustack/embeddings.rb +2 -2
  392. data/lib/ruby_llm/providers/gpustack/media.rb +8 -8
  393. data/lib/ruby_llm/providers/gpustack/transcription.rb +14 -6
  394. data/lib/ruby_llm/providers/gpustack.rb +1 -1
  395. data/lib/ruby_llm/providers/hetzner/chat.rb +23 -6
  396. data/lib/ruby_llm/providers/hetzner.rb +5 -1
  397. data/lib/ruby_llm/providers/meta/responses.rb +81 -0
  398. data/lib/ruby_llm/providers/meta.rb +37 -0
  399. data/lib/ruby_llm/providers/minimax/anthropic.rb +95 -0
  400. data/lib/ruby_llm/providers/minimax/chat_completions.rb +48 -0
  401. data/lib/ruby_llm/providers/minimax/responses.rb +52 -0
  402. data/lib/ruby_llm/providers/minimax.rb +104 -0
  403. data/lib/ruby_llm/providers/mistral/chat.rb +34 -4
  404. data/lib/ruby_llm/providers/mistral/chat_completions/batches.rb +5 -14
  405. data/lib/ruby_llm/providers/mistral/embeddings.rb +1 -12
  406. data/lib/ruby_llm/providers/mistral/input_tokens.rb +35 -0
  407. data/lib/ruby_llm/providers/mistral/media.rb +8 -8
  408. data/lib/ruby_llm/providers/mistral/transcription.rb +8 -6
  409. data/lib/ruby_llm/providers/mistral.rb +13 -1
  410. data/lib/ruby_llm/providers/moonshot/capabilities.rb +16 -0
  411. data/lib/ruby_llm/providers/moonshot/chat.rb +100 -0
  412. data/lib/ruby_llm/providers/moonshot/documents.rb +79 -0
  413. data/lib/ruby_llm/providers/moonshot/responses.rb +55 -0
  414. data/lib/ruby_llm/providers/moonshot.rb +54 -0
  415. data/lib/ruby_llm/providers/ollama/chat.rb +5 -6
  416. data/lib/ruby_llm/providers/ollama/embeddings.rb +41 -0
  417. data/lib/ruby_llm/providers/ollama/media.rb +6 -6
  418. data/lib/ruby_llm/providers/ollama/speech.rb +24 -0
  419. data/lib/ruby_llm/providers/ollama.rb +13 -1
  420. data/lib/ruby_llm/providers/ollama_cloud.rb +22 -1
  421. data/lib/ruby_llm/providers/openai/responses.rb +8 -0
  422. data/lib/ruby_llm/providers/openai.rb +7 -3
  423. data/lib/ruby_llm/providers/openrouter/chat.rb +15 -9
  424. data/lib/ruby_llm/providers/openrouter/embeddings.rb +12 -8
  425. data/lib/ruby_llm/providers/openrouter/images.rb +16 -10
  426. data/lib/ruby_llm/providers/openrouter/judgments.rb +47 -0
  427. data/lib/ruby_llm/providers/openrouter/media.rb +5 -5
  428. data/lib/ruby_llm/providers/openrouter/models.rb +19 -7
  429. data/lib/ruby_llm/providers/openrouter/streaming.rb +4 -4
  430. data/lib/ruby_llm/providers/openrouter/videos.rb +1 -1
  431. data/lib/ruby_llm/providers/openrouter.rb +32 -5
  432. data/lib/ruby_llm/providers/perplexity/chat.rb +3 -3
  433. data/lib/ruby_llm/providers/perplexity/embeddings.rb +1 -1
  434. data/lib/ruby_llm/providers/perplexity/media.rb +6 -6
  435. data/lib/ruby_llm/providers/perplexity.rb +13 -2
  436. data/lib/ruby_llm/providers/typesafe.rb +1 -1
  437. data/lib/ruby_llm/providers/vertexai/anthropic.rb +5 -1
  438. data/lib/ruby_llm/providers/vertexai/credentials.rb +27 -3
  439. data/lib/ruby_llm/providers/vertexai/embed_content.rb +1 -1
  440. data/lib/ruby_llm/providers/vertexai/embeddings.rb +2 -3
  441. data/lib/ruby_llm/providers/vertexai/gemini.rb +1 -1
  442. data/lib/ruby_llm/providers/vertexai/mistral.rb +5 -0
  443. data/lib/ruby_llm/providers/vertexai/models.rb +24 -2
  444. data/lib/ruby_llm/providers/vertexai/transcription.rb +2 -2
  445. data/lib/ruby_llm/providers/vertexai/videos.rb +1 -1
  446. data/lib/ruby_llm/providers/vertexai.rb +56 -9
  447. data/lib/ruby_llm/providers/xai/chat.rb +3 -3
  448. data/lib/ruby_llm/providers/xai/chat_completions/batches.rb +8 -3
  449. data/lib/ruby_llm/providers/xai/images.rb +9 -4
  450. data/lib/ruby_llm/providers/xai/models.rb +2 -2
  451. data/lib/ruby_llm/providers/xai/responses.rb +10 -0
  452. data/lib/ruby_llm/providers/xai/speech.rb +2 -2
  453. data/lib/ruby_llm/providers/xai/transcription.rb +14 -9
  454. data/lib/ruby_llm/providers/xai/videos.rb +1 -1
  455. data/lib/ruby_llm/providers/xai.rb +9 -1
  456. data/lib/ruby_llm/providers/zai/chat.rb +117 -0
  457. data/lib/ruby_llm/providers/zai/ocr.rb +65 -0
  458. data/lib/ruby_llm/providers/zai/token_counting.rb +25 -0
  459. data/lib/ruby_llm/providers/zai/videos.rb +67 -0
  460. data/lib/ruby_llm/providers/zai.rb +66 -0
  461. data/lib/ruby_llm/question.rb +390 -0
  462. data/lib/ruby_llm/queued_message.rb +74 -0
  463. data/lib/ruby_llm/railtie.rb +43 -0
  464. data/lib/ruby_llm/request_shape/part.rb +2 -2
  465. data/lib/ruby_llm/request_shape/problem.rb +2 -2
  466. data/lib/ruby_llm/request_shape/tool_round.rb +2 -2
  467. data/lib/ruby_llm/request_shape/turn.rb +2 -2
  468. data/lib/ruby_llm/request_shape.rb +1 -1
  469. data/lib/ruby_llm/rerank.rb +10 -37
  470. data/lib/ruby_llm/research_job.rb +26 -13
  471. data/lib/ruby_llm/sandbox/landlock.rb +78 -0
  472. data/lib/ruby_llm/sandbox/mxc.rb +137 -0
  473. data/lib/ruby_llm/sandbox.rb +141 -0
  474. data/lib/ruby_llm/search_results.rb +9 -4
  475. data/lib/ruby_llm/server_tool_call.rb +1 -1
  476. data/lib/ruby_llm/skill/loader.rb +131 -0
  477. data/lib/ruby_llm/skill.rb +263 -0
  478. data/lib/ruby_llm/speech.rb +25 -53
  479. data/lib/ruby_llm/stored_file.rb +94 -0
  480. data/lib/ruby_llm/support/cancellation.rb +4 -4
  481. data/lib/ruby_llm/support/deprecated_enumerable.rb +13 -0
  482. data/lib/ruby_llm/support/deprecated_job_predicates.rb +24 -0
  483. data/lib/ruby_llm/support/inspectable.rb +1 -1
  484. data/lib/ruby_llm/support/instrumentation.rb +6 -5
  485. data/lib/ruby_llm/support/rescuable.rb +84 -0
  486. data/lib/ruby_llm/support/utils.rb +26 -3
  487. data/lib/ruby_llm/task.rb +126 -0
  488. data/lib/ruby_llm/test_helper.rb +129 -0
  489. data/lib/ruby_llm/thinking.rb +3 -3
  490. data/lib/ruby_llm/tokens.rb +52 -12
  491. data/lib/ruby_llm/tool.rb +339 -5
  492. data/lib/ruby_llm/tool_call/collection.rb +132 -0
  493. data/lib/ruby_llm/tool_call.rb +68 -6
  494. data/lib/ruby_llm/tools/provider_tools.rb +23 -0
  495. data/lib/ruby_llm/tools/search.rb +118 -0
  496. data/lib/ruby_llm/transcription/segment.rb +67 -0
  497. data/lib/ruby_llm/transcription/wav_audio.rb +2 -1
  498. data/lib/ruby_llm/transcription.rb +25 -49
  499. data/lib/ruby_llm/transcription_chunk.rb +9 -9
  500. data/lib/ruby_llm/transport/connection.rb +146 -33
  501. data/lib/ruby_llm/transport/error_middleware.rb +94 -49
  502. data/lib/ruby_llm/transport/event_stream_parser.rb +18 -4
  503. data/lib/ruby_llm/transport/json_response.rb +9 -3
  504. data/lib/ruby_llm/transport/log_formatter.rb +20 -0
  505. data/lib/ruby_llm/transport/release_request.rb +35 -0
  506. data/lib/ruby_llm/transport/websocket_connection.rb +26 -30
  507. data/lib/ruby_llm/uploaded_file.rb +15 -6
  508. data/lib/ruby_llm/version.rb +1 -1
  509. data/lib/ruby_llm/video.rb +34 -10
  510. data/lib/ruby_llm/video_job.rb +55 -42
  511. data/lib/ruby_llm/web_source.rb +38 -0
  512. data/lib/ruby_llm/workflow.rb +2 -2
  513. data/lib/ruby_llm.rb +63 -25
  514. data/lib/tasks/ruby_llm/evaluations.rb +1 -1
  515. data/skills/rubyllm/SKILL.md +128 -54
  516. data/skills/rubyllm/references/cost-and-tracing.md +8 -0
  517. data/skills/rubyllm/references/files-code-and-context.md +6 -0
  518. data/skills/rubyllm/references/operations.md +8 -0
  519. data/skills/rubyllm/references/rails-chats.md +22 -0
  520. data/skills/rubyllm/references/tools-and-agents.md +11 -0
  521. metadata +213 -14
  522. data/lib/generators/ruby_llm/chat_ui/templates/helpers/messages_helper.rb.tt +0 -25
  523. data/lib/generators/ruby_llm/upgrade/templates/upgrade_ruby_llm_to_2_1.rb.tt +0 -78
@@ -9,14 +9,14 @@ module RubyLLM
9
9
  # configured chats wherever they are needed.
10
10
  #
11
11
  # class SupportAgent < RubyLLM::Agent
12
- # model "gpt-5.6-luna"
12
+ # model "gpt-6-luna"
13
13
  # instructions "You are a concise support assistant."
14
14
  # tools SearchDocs, LookupAccount
15
15
  # end
16
16
  #
17
17
  # SupportAgent.new.ask "How do I reset my API key?"
18
18
  #
19
- # ::chat returns a configured Chat. When ::chat_model names an ActiveRecord
19
+ # ::chat returns a configured Chat. When ::chat_class names an ActiveRecord
20
20
  # chat class, ::create, ::create!, and ::find return configured records of
21
21
  # that class instead.
22
22
  #
@@ -37,11 +37,14 @@ module RubyLLM
37
37
  # Agent instances delegate Chat's conversation API (#ask, #complete,
38
38
  # #with_tools, and so on) to the wrapped chat, which is available via
39
39
  # #chat. Direct transcript replacement stays on the wrapped chat because
40
- # Rails-backed chat models own their message association. Agents are
41
- # enumerable over their messages.
40
+ # Rails-backed chat models own their message association. Read the
41
+ # conversation through #messages.
42
42
  class Agent
43
43
  extend Forwardable
44
- include Enumerable
44
+ extend ModelOptions
45
+ singleton_class.prepend JoinCallbacks, Ownership, Callbacks, ToolConversion, ContextBudget
46
+ include Support::DeprecatedEnumerable
47
+ include Support::Rescuable
45
48
 
46
49
  DUPED_INHERITED_CONFIG = {
47
50
  :@chat_kwargs => {},
@@ -55,8 +58,7 @@ module RubyLLM
55
58
  :@headers => {},
56
59
  :@input_names => [],
57
60
  :@fallbacks => [],
58
- :@fallback_options => {},
59
- :@rescue_handlers => []
61
+ :@fallback_options => {}
60
62
  }.freeze
61
63
  # Simple value options: a class-level getter/setter macro whose value the
62
64
  # agent forwards to the matching Chat#with_* when it builds its chat.
@@ -65,42 +67,43 @@ module RubyLLM
65
67
  UNRESOLVED_CONTEXT = Object.new.freeze
66
68
  private_constant :UNRESOLVED_CONTEXT
67
69
 
68
- # The chat operations an agent instance runs through its ::rescue_from
69
- # handlers. Remaining delegated methods pass through untouched.
70
- GUARDED_OPERATIONS = %i[ask say ask_later complete generate run_tools step count_tokens compact].freeze
71
-
72
70
  # Chat methods that return the wrapped chat so calls can be chained there.
73
71
  CHAINABLE_CHAT_DELEGATES = %i[
74
72
  with_instructions with_tools with_mcp with_provider_tools with_tool_options with_model
75
- with_temperature with_max_output_tokens with_thinking with_citations
76
- with_end_user with_compaction with_caching with_context with_provider_options
77
- with_headers with_schema with_fallbacks
78
- before_request before_message after_message before_tool_call after_tool_result after_tool_progress
79
- before_fallback after_fallback
73
+ with_temperature with_max_output_tokens with_thinking with_citations with_context_budget
74
+ with_end_user with_owner with_compaction with_caching with_code_execution with_skills with_file_store with_context
75
+ with_provider_options with_headers with_schema with_fallbacks
76
+ before_request before_message after_message before_tool_call around_tool_call after_tool_result
77
+ after_tool_progress before_fallback after_fallback around_join after_file_write
80
78
  convert_unsupported_attachments
81
- cancel approve deny answer decline cache_until_here
79
+ cancel approve answer decline cache_until_here
82
80
  ].freeze
83
81
 
84
82
  # Chat values and operations whose return values pass through unchanged.
85
83
  PASSTHROUGH_CHAT_DELEGATES = %i[
86
- model provider messages tools deferred_tools mcp provider_tools tool_options provider_options headers schema
87
- concurrency caching citations compaction context end_user fallbacks thinking temperature max_output_tokens
88
- each complete? cancelled? waiting? awaiting_approval? pending_approvals awaiting_input? pending_inputs
89
- awaiting_tasks? pending_tasks add_message add_completion tokens cost render
84
+ model model_id provider messages tools deferred_tools mcp provider_tools tool_options provider_options headers
85
+ schema concurrency caching citations code_execution compaction context_budget context end_user fallbacks thinking
86
+ temperature max_output_tokens skills file_store
87
+ each complete? cancelled? waiting? pending_questions pending_tasks add_message add_completion tokens cost render
88
+ queue queued_messages withdraw join_queued_messages
90
89
  ].freeze
91
90
 
92
91
  COPIED_INHERITED_CONFIG = (%i[
93
92
  @thinking
94
93
  @citations
94
+ @code_execution
95
+ @skills
96
+ @file_store
95
97
  @end_user
96
98
  @schema
97
99
  @context
98
- @chat_model
100
+ @context_budget
101
+ @chat_class
99
102
  @tools_defer
100
103
  @mcp_defer
101
104
  ] + PASSTHROUGH_OPTIONS.map { |option| :"@#{option}" }).freeze
102
105
  private_constant :DUPED_INHERITED_CONFIG, :COPIED_INHERITED_CONFIG,
103
- :PASSTHROUGH_OPTIONS, :THINKING_OPTIONS, :GUARDED_OPERATIONS,
106
+ :PASSTHROUGH_OPTIONS, :THINKING_OPTIONS,
104
107
  :CHAINABLE_CHAT_DELEGATES, :PASSTHROUGH_CHAT_DELEGATES
105
108
 
106
109
  class << self
@@ -116,9 +119,17 @@ module RubyLLM
116
119
  # available as methods. Called with no arguments, returns the
117
120
  # configured chat keywords.
118
121
  #
119
- # model "gpt-5.6-luna"
120
- # model "gpt-5.6", provider: :openai, protocol: :responses
121
- # model { quality == :high ? "gpt-5.6" : "gpt-5.6-luna" }
122
+ # model "gpt-6-luna"
123
+ # model "gpt-6.1-sol", provider: :openai, protocol: :responses
124
+ # model { quality == :high ? "gpt-6.1-sol" : "gpt-6-luna" }
125
+ #
126
+ # A +model:+ passed to ::chat, ::new, ::create, or ::create! replaces
127
+ # the declared model and its options, so the caller's model resolves
128
+ # on its own provider unless the caller also passes +provider:+. Options
129
+ # declared without a model stay. Later, ::find, ::new with +chat:+, and
130
+ # ::sync_instructions apply the options only to a record on the declared
131
+ # model, and always when a block declares it, since the block's choice
132
+ # is unknown until it runs.
122
133
  #
123
134
  # The block runs before the chat exists, so it can read inputs but not
124
135
  # +chat+.
@@ -138,12 +149,19 @@ module RubyLLM
138
149
  # tools SearchDocs, LookupAccount
139
150
  # tools { [TodoTool.new(chat: chat)] }
140
151
  #
141
- # Pass +defer:+ as Chat#with_tools takes it:
152
+ # Pass +defer:+ as Chat#with_tools takes it, or a lambda that decides
153
+ # when the chat is built:
142
154
  #
143
155
  # tools SearchDocs, LookupAccount, defer: true
156
+ # tools(defer: -> { many_tools? }) { connected_tools }
157
+ #
158
+ # With +defer:+ alone it applies to the tools a parent agent declared.
144
159
  #
145
160
  def tools(*tools, defer: nil, &block)
146
- return @tools || [] if tools.empty? && !block_given?
161
+ if tools.empty? && !block_given?
162
+ @tools_defer = defer unless defer.nil?
163
+ return @tools || []
164
+ end
147
165
 
148
166
  @tools_defer = defer
149
167
  @tools = block_given? ? block : tools.flatten
@@ -157,23 +175,30 @@ module RubyLLM
157
175
  # mcp Files
158
176
  # mcp { [Linear.new(user: user), Files] }
159
177
  #
160
- # Pass +defer:+ as Chat#with_mcp takes it:
178
+ # Pass +defer:+ as Chat#with_mcp takes it, or a lambda that decides
179
+ # when the chat is built:
161
180
  #
162
181
  # mcp GitHub, defer: true
163
182
  #
183
+ # With +defer:+ alone it applies to the servers a parent agent declared.
184
+ #
164
185
  def mcp(*servers, defer: nil, &block)
165
- return @mcp || [] if servers.empty? && !block_given?
186
+ if servers.empty? && !block_given?
187
+ @mcp_defer = defer unless defer.nil?
188
+ return @mcp || []
189
+ end
166
190
 
167
191
  @mcp_defer = defer
168
192
  @mcp = block_given? ? block : servers.flatten
169
193
  end
170
194
 
171
195
  # Sets how chats this agent builds use their tools, applied via
172
- # Chat#with_tool_options. Accepts +choice:+, +calls:+, and
173
- # +concurrency:+. A block defers evaluation until the chat is built.
196
+ # Chat#with_tool_options. Accepts +choice:+, +calls:+, +concurrency:+,
197
+ # and +rounds:+. A block defers evaluation until the chat is built.
174
198
  # Called with no arguments, returns the configured options.
175
199
  #
176
200
  # tool_options choice: :required, calls: :one
201
+ # tool_options rounds: 25, calls: 50
177
202
  #
178
203
  def tool_options(**options, &block)
179
204
  return @tool_options || {} if options.empty? && !block_given?
@@ -183,11 +208,13 @@ module RubyLLM
183
208
 
184
209
  # Enables provider-executed tools for chats this agent builds, applied
185
210
  # via Chat#with_provider_tools. Accepts the same aliases, options, and
186
- # raw Hashes; a block defers evaluation until the chat is built.
211
+ # raw Hashes; a block defers evaluation until the chat is built and
212
+ # returns a list, or a Hash of aliases and options like the keywords.
187
213
  # Called with no arguments, returns the declared entries.
188
214
  #
189
215
  # provider_tools :web_search
190
216
  # provider_tools web_search: { allowed_domains: ["ruby-lang.org"] }
217
+ # provider_tools { { web_search: { max_uses: max_searches } } }
191
218
  #
192
219
  def provider_tools(*tools, **tools_with_options, &block)
193
220
  return @provider_tools || [] if tools.empty? && tools_with_options.empty? && !block_given?
@@ -286,6 +313,89 @@ module RubyLLM
286
313
  @compaction = options
287
314
  end
288
315
 
316
+ # Keeps tool results within shares of the model's context window in
317
+ # chats this agent builds, applied via Chat#with_context_budget, in
318
+ # place of Configuration#context_budget. Pass +nil+ for no budget.
319
+ #
320
+ # context_budget per_result: 0.25, per_request: 0.7
321
+ # context_budget window: 128_000, per_request: 0.7
322
+ # context_budget nil
323
+ #
324
+ # A block is evaluated for each chat, with the agent's inputs and
325
+ # +chat+ in scope, and returns the options, or +nil+ for no budget.
326
+ #
327
+ # context_budget { { window: model_window, per_request: 0.7 } if model_window }
328
+ #
329
+ def context_budget(budget = {}, **, &) = (@context_budget = context_budget_option(budget, **, &))
330
+
331
+ # Lets chats this agent builds run code in the configured sandbox,
332
+ # applied via Chat#with_code_execution. Accepts the same options as
333
+ # keywords or a Hash. Pass +false+ to disable it. Passing +nil+ or a
334
+ # block raises ArgumentError.
335
+ #
336
+ # code_execution
337
+ # code_execution false
338
+ # code_execution languages: [:ruby, :python], gems: %w[prawn]
339
+ # code_execution sandbox: ReadOnlySandbox, escalate_to: [WorkspaceSandbox]
340
+ # code_execution mounts: -> { { "templates" => chat.templates_dir } }
341
+ #
342
+ # A +mounts+ lambda runs before each run, with +chat+ and the
343
+ # agent's inputs in scope, like the agent's other lambdas.
344
+ def code_execution(enabled = true, **options) # rubocop:disable Style/OptionalBooleanParameter
345
+ raise ArgumentError, 'code_execution takes no block. Pass a lambda as mounts: for each run' if block_given?
346
+
347
+ @code_execution = CodeExecution.options(enabled, options)
348
+ end
349
+
350
+ # Attaches a file store to chats this agent builds, with the tools to
351
+ # work with it, applied via Chat#with_file_store, which takes the
352
+ # same arguments. A +store+ lambda runs once for each chat, with
353
+ # +chat+ and the agent's inputs in scope, like the agent's other
354
+ # lambdas. Pass +nil+ to remove the store a parent agent declared. A
355
+ # block raises ArgumentError: pass the lambda as the store instead.
356
+ #
357
+ # file_store
358
+ # file_store inline_limit: 20_000, tools: :read
359
+ # file_store :memory
360
+ # file_store -> { "tmp/chats/#{chat.id}" }
361
+ # file_store nil
362
+ #
363
+ # Each chat gets a store of its own: without +store+, a new
364
+ # FileStore::Memory, or for a persisted chat, its files in Active
365
+ # Storage, kept for the chat's owner (see ::owner). A folder path on
366
+ # its own is shared by every chat, so use a lambda to give each chat
367
+ # its own folder.
368
+ #
369
+ # :call-seq: file_store(store = default, inline_limit: 10_000, tools: %i[read write edit])
370
+ def file_store(store = Chat::FileReferences::DEFAULT_STORE, inline_limit: Chat::FileReferences::INLINE_LIMIT,
371
+ tools: FileStore::Tools::DEFAULT)
372
+ raise ArgumentError, 'file_store takes no block. Pass the store as a lambda instead' if block_given?
373
+ return @file_store = false if store.nil?
374
+
375
+ Chat::FileReferences.validate(store, inline_limit:, tools:, store_lambda: true)
376
+ @file_store = { store:, inline_limit:, tools: }.freeze
377
+ end
378
+
379
+ # Gives chats this agent builds the skills named, applied via
380
+ # Chat#with_skills when the chat is built. Symbols name folders under
381
+ # Skill.roots, Strings and Pathnames are paths to skill folders,
382
+ # +true+ is every skill under the roots, and +false+ removes the
383
+ # skills a parent agent declared. An agent without a +skills+
384
+ # declaration has no skills. Called with no arguments, returns the
385
+ # declaration: the names, +true+, +false+, or +nil+. A block raises
386
+ # ArgumentError.
387
+ #
388
+ # skills :invoices, :refunds
389
+ # skills true
390
+ # skills false
391
+ #
392
+ def skills(*skills)
393
+ raise ArgumentError, 'skills takes no block. Name the skills, or pass true for every skill' if block_given?
394
+ return @skills if skills.empty?
395
+
396
+ @skills = Skill.selection(skills)
397
+ end
398
+
289
399
  # Sets the safety identifier for chats this agent builds, applied via
290
400
  # Chat#with_end_user. A block defers evaluation until the
291
401
  # chat is built, so the id can come from the agent's inputs. Called
@@ -294,11 +404,21 @@ module RubyLLM
294
404
  # end_user "tenant-42"
295
405
  # end_user { workspace.public_id }
296
406
  #
297
- def end_user(value = nil, &block)
298
- return @end_user if value.nil? && !block_given?
299
-
300
- @end_user = block || value
301
- end
407
+ def end_user(value = nil, &block) = value.nil? && !block ? @end_user : (@end_user = block || value)
408
+
409
+ # Sets whoever the usage of chats this agent builds or finds is
410
+ # attributed to, applied via Chat#with_owner. A block defers
411
+ # evaluation until the chat is built, so the owner can come from the
412
+ # agent's inputs or, for a persisted chat, from +chat+. A persisted
413
+ # chat keeps the files of its Active Storage store for the owner
414
+ # too, and the owner wins over the association its
415
+ # <tt>acts_as_chat owner:</tt> names. Subclasses inherit it. Called
416
+ # with no arguments, returns the configured value.
417
+ #
418
+ # owner { account }
419
+ # owner { chat.user }
420
+ #
421
+ def owner(value = nil, &block) = value.nil? && !block ? @owner : (@owner = block || value)
302
422
 
303
423
  # Enables citations for chats this agent builds, applied via
304
424
  # Chat#with_citations. Pass +false+ to disable them.
@@ -320,13 +440,18 @@ module RubyLLM
320
440
  #
321
441
  # caching
322
442
  # caching false
443
+ # caching cache
323
444
  # caching ttl: "1h"
324
445
  # caching { { ttl: workspace.cache_ttl } }
325
446
  #
326
447
  def caching(enabled = true, **options, &block) # rubocop:disable Metrics/PerceivedComplexity, Style/OptionalBooleanParameter
327
448
  return caching(**enabled.transform_keys(&:to_sym), **options, &block) if enabled.is_a?(Hash)
449
+ return caching(id: enabled, **options, &block) if enabled.is_a?(CachedContent)
450
+
451
+ unless [true, false].include?(enabled)
452
+ raise ArgumentError, 'caching accepts false, a CachedContent, or caching options'
453
+ end
328
454
 
329
- raise ArgumentError, 'caching accepts false or caching options' unless [true, false].include?(enabled)
330
455
  raise ArgumentError, 'caching accepts options or a block, not both' if options.any? && block
331
456
  raise ArgumentError, 'caching false does not accept options or a block' if !enabled && (options.any? || block)
332
457
 
@@ -375,17 +500,19 @@ module RubyLLM
375
500
 
376
501
  # Sets fallback models for chats this agent builds, applied via
377
502
  # Chat#with_fallbacks. Called with no arguments, returns the
378
- # configured models.
503
+ # configured models. Keep the default +on:+: providers signal
504
+ # overload as HTTP 529 or 503, and the default covers both.
379
505
  #
380
506
  # fallbacks "gpt-4.1-mini", "claude-haiku-4-5"
381
- # fallbacks "gpt-4.1-mini", on: [RubyLLM::RateLimitError]
507
+ # fallbacks "gpt-4.1-mini", on: [*RubyLLM::Fallback::DEFAULT_ERRORS, RubyLLM::ContextLengthExceededError]
508
+ # fallbacks drop_unsupported: true
382
509
  #
383
- def fallbacks(*models, **options)
384
- return @fallbacks || [] if models.empty? && options.empty?
385
- raise ArgumentError, 'To set fallback options, provide at least one fallback model' if models.empty?
510
+ def fallbacks(*models, on: Fallback::DEFAULT_ERRORS, drop_unsupported: false)
511
+ return @fallbacks || [] if (bare = models.empty? && !drop_unsupported) && on.equal?(Fallback::DEFAULT_ERRORS)
512
+ raise ArgumentError, 'To set fallback options, provide at least one fallback model' if bare
386
513
 
387
514
  @fallbacks = models.flatten.compact
388
- @fallback_options = options
515
+ @fallback_options = { on:, drop_unsupported: }
389
516
  end
390
517
 
391
518
  def fallback_options
@@ -425,13 +552,18 @@ module RubyLLM
425
552
  # ::sync_instructions). Accepts the class or its name as a string.
426
553
  # Called with no argument, returns the configured value.
427
554
  #
428
- # chat_model Chat
555
+ # chat_class Chat
429
556
  #
430
- def chat_model(value = nil)
431
- return @chat_model if value.nil?
557
+ def chat_class(value = nil)
558
+ return @chat_class if value.nil?
559
+
560
+ @chat_class = value
561
+ remove_instance_variable(:@resolved_chat_class) if instance_variable_defined?(:@resolved_chat_class)
562
+ end
432
563
 
433
- @chat_model = value
434
- remove_instance_variable(:@resolved_chat_model) if instance_variable_defined?(:@resolved_chat_model)
564
+ def chat_model(value = nil) # :nodoc:
565
+ RubyLLM.deprecator.deprecate('Agent.chat_model', replacement: 'Agent.chat_class', removal: '3.0')
566
+ chat_class(value)
435
567
  end
436
568
 
437
569
  # Declares named runtime inputs. Matching keyword arguments passed to
@@ -447,57 +579,6 @@ module RubyLLM
447
579
  @input_names = names.flatten.map(&:to_sym)
448
580
  end
449
581
 
450
- # Registers a handler for exceptions raised by the chat operations of
451
- # this agent's instances: #ask, #say, #ask_later, #complete,
452
- # #generate, #run_tools, #step, #count_tokens, and #compact. Name the
453
- # handler with +with:+ or pass a block; either runs on the agent instance,
454
- # so the agent's #chat, inputs, and class name are available for instrumentation.
455
- #
456
- # class ApplicationAgent < RubyLLM::Agent
457
- # rescue_from RubyLLM::RateLimitError, Faraday::TimeoutError, with: :handle_transient
458
- # rescue_from RubyLLM::BadRequestError do |error|
459
- # error_tracker.notify(error)
460
- # raise
461
- # end
462
- #
463
- # private
464
- #
465
- # def handle_transient(error)
466
- # metrics.increment("llm.api_error", type: "transient")
467
- # raise
468
- # end
469
- # end
470
- #
471
- # Handlers are searched in reverse declaration order, so the last
472
- # matching one wins. Re-raise inside a handler to let the caller see
473
- # the exception; otherwise the handler's return value becomes the
474
- # operation's return value. Exceptions no handler matches are
475
- # re-raised. Subclasses inherit the handlers declared when they are
476
- # defined.
477
- #
478
- # Exception classes may be named as Strings, which defers constant
479
- # lookup until an exception is raised.
480
- def rescue_from(*exception_classes, with: nil, &block)
481
- raise ArgumentError, 'rescue_from needs a handler: pass with: or a block' unless with || block
482
- raise ArgumentError, 'rescue_from takes with: or a block, not both' if with && block
483
-
484
- exception_classes.flatten.each do |exception_class|
485
- rescue_handlers << [rescue_handler_key(exception_class), with || block]
486
- end
487
- end
488
-
489
- def rescue_handlers # :nodoc:
490
- @rescue_handlers ||= []
491
- end
492
-
493
- def rescue_handler_for(exception) # :nodoc:
494
- rescue_handlers.reverse_each do |class_name, handler|
495
- exception_class = rescue_handler_class(class_name)
496
- return handler if exception_class && exception.is_a?(exception_class)
497
- end
498
- nil
499
- end
500
-
501
582
  def chat_kwargs # :nodoc:
502
583
  @chat_kwargs || {}
503
584
  end
@@ -511,16 +592,9 @@ module RubyLLM
511
592
  evaluate(value, runtime_context(chat: nil, inputs:))
512
593
  end
513
594
 
514
- def resolved_chat_kwargs(inputs: {}) # :nodoc:
515
- kwargs = chat_kwargs
516
- return kwargs unless kwargs[:model].is_a?(Proc)
517
-
518
- kwargs.merge(model: evaluate(kwargs[:model], runtime_context(chat: nil, inputs: inputs)))
519
- end
520
-
521
595
  def build_chat(inputs:, options:, resolved_context: UNRESOLVED_CONTEXT) # :nodoc:
522
596
  context_value = resolved_context.equal?(UNRESOLVED_CONTEXT) ? self.resolved_context(inputs:) : resolved_context
523
- (context_value || RubyLLM).chat(**resolved_chat_kwargs(inputs:), **options)
597
+ (context_value || RubyLLM).chat(**resolved_chat_kwargs(inputs:, options:))
524
598
  end
525
599
 
526
600
  # Builds a Chat configured with this agent's declarations and returns
@@ -538,13 +612,13 @@ module RubyLLM
538
612
  chat
539
613
  end
540
614
 
541
- # Creates a ::chat_model record, applies this agent's configuration to
615
+ # Creates a ::chat_class record, applies this agent's configuration to
542
616
  # it, and returns it. Keywords matching declared ::inputs become
543
617
  # runtime inputs; the rest are forwarded to the model's +create+.
544
618
  #
545
619
  # chat = WorkAssistant.create(user: current_user)
546
620
  #
547
- # Raises ArgumentError if ::chat_model is not configured.
621
+ # Raises ArgumentError if ::chat_class is not configured.
548
622
  def create(**)
549
623
  with_rails_chat_record(:create, **)
550
624
  end
@@ -558,38 +632,37 @@ module RubyLLM
558
632
  with_rails_chat_record(:create!, **)
559
633
  end
560
634
 
561
- # Finds the ::chat_model record with +id+ and applies this agent's
635
+ # Finds the ::chat_class record with +id+ and applies this agent's
562
636
  # configuration at runtime, without persisting instructions. Returns
563
637
  # the record.
564
638
  #
565
639
  # chat = WorkAssistant.find(params[:id])
566
640
  #
567
- # Raises ArgumentError if ::chat_model is not configured.
641
+ # Raises ArgumentError if ::chat_class is not configured.
568
642
  def find(id, **kwargs)
569
- raise ArgumentError, 'chat_model must be configured to use find' unless resolved_chat_model
643
+ raise ArgumentError, 'chat_class must be configured to use find' unless resolved_chat_class
570
644
 
571
645
  input_values, = partition_inputs(kwargs)
572
- record = resolved_chat_model.find(id)
646
+ record = resolved_chat_class.find(id)
573
647
  apply_configuration(record, input_values:, persist_instructions: false)
574
648
 
575
649
  record
576
650
  end
577
651
 
578
652
  # Re-renders this agent's instructions and persists them on the given
579
- # ::chat_model record (or the record found by that id). Keywords
653
+ # ::chat_class record (or the record found by that id). Keywords
580
654
  # matching declared ::inputs become runtime inputs. Returns the
581
655
  # record.
582
656
  #
583
657
  # WorkAssistant.sync_instructions(chat)
584
658
  #
585
- # Raises ArgumentError if ::chat_model is not configured.
659
+ # Raises ArgumentError if ::chat_class is not configured.
586
660
  def sync_instructions(chat_or_id, **kwargs)
587
- raise ArgumentError, 'chat_model must be configured to use sync_instructions' unless resolved_chat_model
661
+ raise ArgumentError, 'chat_class must be configured to use sync_instructions' unless resolved_chat_class
588
662
 
589
663
  input_values, = partition_inputs(kwargs)
590
- record = chat_or_id.is_a?(resolved_chat_model) ? chat_or_id : resolved_chat_model.find(chat_or_id)
591
- apply_assume_model_exists(record)
592
- apply_protocol(record)
664
+ record = chat_or_id.is_a?(resolved_chat_class) ? chat_or_id : resolved_chat_class.find(chat_or_id)
665
+ apply_chat_options(record)
593
666
  runtime = runtime_context(chat: record, inputs: input_values)
594
667
  apply_context(record, runtime)
595
668
  apply_instructions(
@@ -627,10 +700,12 @@ module RubyLLM
627
700
  chat,
628
701
  input_values:,
629
702
  persist_instructions:,
630
- resolved_context: UNRESOLVED_CONTEXT
703
+ resolved_context: UNRESOLVED_CONTEXT,
704
+ agent: nil,
705
+ chat_options: {}
631
706
  )
632
707
  runtime = runtime_context(chat:, inputs: input_values)
633
- apply_chat_options(chat)
708
+ apply_chat_options(chat, chat_options)
634
709
  apply_context(chat, runtime, resolved_context:)
635
710
  apply_instructions(chat, runtime, inputs: input_values, persist: persist_instructions)
636
711
  apply_tools(chat, runtime)
@@ -640,10 +715,17 @@ module RubyLLM
640
715
  apply_end_user(chat, runtime)
641
716
  apply_caching(chat, runtime)
642
717
  apply_compaction(chat)
718
+ apply_code_execution(chat, runtime)
719
+ apply_skills(chat)
720
+ apply_file_store(chat, runtime)
721
+ apply_tool_options(chat, runtime)
643
722
  apply_provider_options(chat, runtime)
644
723
  apply_headers(chat, runtime)
645
724
  apply_schema(chat, runtime)
646
725
  apply_fallbacks(chat)
726
+ apply_rescue_handlers(chat, agent, input_values)
727
+ target = chat.respond_to?(:to_llm) ? chat.to_llm : chat
728
+ target.agent_class = self if target.respond_to?(:agent_class=)
647
729
  end
648
730
 
649
731
  private
@@ -666,28 +748,12 @@ module RubyLLM
666
748
  raise ArgumentError, "thinking accepts #{THINKING_OPTIONS.join(', ')}, got #{unsupported.join(', ')}"
667
749
  end
668
750
 
669
- def rescue_handler_key(exception_class)
670
- case exception_class
671
- when Module then exception_class.name || exception_class
672
- when String then exception_class
673
- else raise ArgumentError, "#{exception_class.inspect} is not an exception class or its name"
674
- end
675
- end
676
-
677
- def rescue_handler_class(class_name)
678
- return class_name if class_name.is_a?(Module)
679
-
680
- Object.const_get(class_name)
681
- rescue NameError
682
- nil
683
- end
684
-
685
751
  def copy_inherited_config_to(subclass)
686
752
  subclass.instance_variable_set(:@inherited_instruction_declarations, instruction_declarations.dup)
687
753
 
688
754
  DUPED_INHERITED_CONFIG.each do |ivar, default|
689
755
  value = instance_variable_defined?(ivar) ? instance_variable_get(ivar) : default
690
- subclass.instance_variable_set(ivar, value.respond_to?(:dup) ? value.dup : value)
756
+ subclass.instance_variable_set(ivar, value.dup)
691
757
  end
692
758
 
693
759
  COPIED_INHERITED_CONFIG.each do |ivar|
@@ -696,13 +762,13 @@ module RubyLLM
696
762
  end
697
763
 
698
764
  def with_rails_chat_record(method_name, **kwargs)
699
- raise ArgumentError, 'chat_model must be configured to use create/create!' unless resolved_chat_model
765
+ raise ArgumentError, 'chat_class must be configured to use create/create!' unless resolved_chat_class
700
766
 
701
767
  input_values, chat_options = partition_inputs(kwargs)
702
- record = resolved_chat_model.public_send(
703
- method_name, **resolved_chat_kwargs(inputs: input_values), **chat_options
768
+ record = resolved_chat_class.public_send(
769
+ method_name, **resolved_chat_kwargs(inputs: input_values, options: chat_options)
704
770
  )
705
- apply_configuration(record, input_values:, persist_instructions: true) if record
771
+ apply_configuration(record, input_values:, persist_instructions: true, chat_options:) if record&.persisted?
706
772
  record
707
773
  end
708
774
 
@@ -712,7 +778,13 @@ module RubyLLM
712
778
  end
713
779
 
714
780
  def apply_instructions(chat, runtime, inputs:, persist:, persistent_only: false)
715
- instructions_config.each do |declaration|
781
+ instructions = resolved_instructions(chat, runtime, inputs:, persist:, persistent_only:)
782
+ instructions.reject! { |_, options| options[:persist] } if instructions_persisted?(chat, instructions)
783
+ instructions.each { |value, options| chat.with_instructions(value, **options) }
784
+ end
785
+
786
+ def resolved_instructions(chat, runtime, inputs:, persist:, persistent_only:)
787
+ instructions_config.filter_map do |declaration|
716
788
  next if persistent_only && !declaration[:persist]
717
789
 
718
790
  value = resolved_instruction_value(declaration, chat, runtime, inputs:)
@@ -723,10 +795,22 @@ module RubyLLM
723
795
  cache_until_here: declaration[:cache_until_here]
724
796
  }
725
797
  options[:persist] = persist && declaration[:persist] if rails_chat_record?(chat)
726
- chat.with_instructions(value, **options)
798
+ [value, options]
727
799
  end
728
800
  end
729
801
 
802
+ # Applying an agent to a record on every request would otherwise
803
+ # rewrite the same system rows each time, behind the conversation.
804
+ def instructions_persisted?(chat, instructions)
805
+ persistent = instructions.select { |_, options| options[:persist] }
806
+ replacement = persistent.rindex { |_, options| !options[:append] }
807
+ return false unless replacement
808
+
809
+ chat.system_instructions_persisted?(
810
+ persistent.drop(replacement).map { |value, options| [value, options[:cache_until_here]] }
811
+ )
812
+ end
813
+
730
814
  # An empty prompt file, such as the one the agent generator writes,
731
815
  # means no instructions rather than an empty system message.
732
816
  def blank_instruction?(value)
@@ -735,16 +819,18 @@ module RubyLLM
735
819
 
736
820
  def apply_tools(chat, runtime)
737
821
  tools_to_apply = Array(evaluate(tools, runtime)).compact
738
- chat.with_tools(*tools_to_apply, defer: @tools_defer) if tools_to_apply.any?
822
+ chat.with_tools(*tools_to_apply, defer: evaluate(@tools_defer, runtime)) if tools_to_apply.any?
739
823
 
740
824
  servers = Array(evaluate(mcp, runtime)).compact
741
- chat.with_mcp(*servers, defer: @mcp_defer) if servers.any?
825
+ chat.with_mcp(*servers, defer: evaluate(@mcp_defer, runtime)) if servers.any?
826
+
827
+ provider_tools_to_apply = RubyLLM::Tools::ProviderTools.entries(evaluate(provider_tools, runtime))
828
+ chat.with_provider_tools(*provider_tools_to_apply) if provider_tools_to_apply.any?
829
+ end
742
830
 
831
+ def apply_tool_options(chat, runtime)
743
832
  options = evaluate(tool_options, runtime)
744
833
  chat.with_tool_options(**options) if options && !options.empty?
745
-
746
- provider_tools_to_apply = Array(evaluate(provider_tools, runtime)).compact
747
- chat.with_provider_tools(*provider_tools_to_apply) if provider_tools_to_apply.any?
748
834
  end
749
835
 
750
836
  def apply_passthrough_options(chat)
@@ -782,6 +868,33 @@ module RubyLLM
782
868
  @compaction == false ? chat.with_compaction(false) : chat.with_compaction(**@compaction)
783
869
  end
784
870
 
871
+ def apply_code_execution(chat, runtime)
872
+ return if @code_execution.nil?
873
+ return chat.with_code_execution(false) if @code_execution == false
874
+
875
+ options = @code_execution
876
+ mounts = options[:mounts]
877
+ options = options.merge(mounts: -> { evaluate(mounts, runtime) }) if mounts.is_a?(Proc)
878
+ chat.with_code_execution(**options)
879
+ end
880
+
881
+ def apply_file_store(chat, runtime)
882
+ return if @file_store.nil?
883
+ return chat.with_file_store(nil) if @file_store == false
884
+
885
+ store = @file_store[:store]
886
+ options = @file_store.except(:store).transform_values { |value| evaluate(value, runtime) }
887
+ return chat.with_file_store(**options) if store.equal?(Chat::FileReferences::DEFAULT_STORE)
888
+
889
+ chat.with_file_store(evaluate(store, runtime), **options)
890
+ end
891
+
892
+ def apply_skills(chat)
893
+ return if @skills.nil?
894
+
895
+ chat.with_skills(*Array(@skills))
896
+ end
897
+
785
898
  def apply_provider_options(chat, runtime)
786
899
  value = evaluate(provider_options, runtime)
787
900
  chat.with_provider_options(**value) if value && !value.empty?
@@ -798,7 +911,13 @@ module RubyLLM
798
911
  end
799
912
 
800
913
  def apply_fallbacks(chat)
801
- chat.with_fallbacks(*fallbacks, **fallback_options) if fallbacks.any?
914
+ chat.with_fallbacks(*fallbacks, **fallback_options) if fallbacks.any? || fallback_options[:drop_unsupported]
915
+ end
916
+
917
+ def apply_rescue_handlers(chat, agent, input_values)
918
+ return unless chat.is_a?(Chat::Rescuable)
919
+
920
+ chat.rescuer = rescue_handlers? ? agent || allocate.bind(chat, input_values) : nil
802
921
  end
803
922
 
804
923
  def resolved_schema_value(runtime)
@@ -809,27 +928,6 @@ module RubyLLM
809
928
  Schematist::Schema.create(&value)
810
929
  end
811
930
 
812
- def apply_chat_options(chat)
813
- apply_assume_model_exists(chat)
814
- apply_protocol(chat)
815
- end
816
-
817
- def apply_assume_model_exists(chat_object)
818
- return unless chat_kwargs.key?(:assume_model_exists) &&
819
- resolved_chat_model &&
820
- chat_object.is_a?(resolved_chat_model)
821
-
822
- chat_object.assume_model_exists = chat_kwargs[:assume_model_exists]
823
- end
824
-
825
- def apply_protocol(chat_object)
826
- return unless chat_kwargs.key?(:protocol) &&
827
- resolved_chat_model &&
828
- chat_object.is_a?(resolved_chat_model)
829
-
830
- chat_object.protocol = chat_kwargs[:protocol]
831
- end
832
-
833
931
  def evaluate(value, runtime)
834
932
  value.is_a?(Proc) ? runtime.instance_exec(&value) : value
835
933
  end
@@ -860,7 +958,7 @@ module RubyLLM
860
958
  end
861
959
 
862
960
  def rails_chat_record?(chat)
863
- resolved_chat_model && chat.is_a?(resolved_chat_model)
961
+ resolved_chat_class && chat.is_a?(resolved_chat_class)
864
962
  end
865
963
 
866
964
  def default_instructions_prompt_exists?
@@ -898,12 +996,12 @@ module RubyLLM
898
996
  Support::Utils.underscore(class_name.gsub('::', '/')).tr('-', '_')
899
997
  end
900
998
 
901
- def resolved_chat_model
902
- return @resolved_chat_model if defined?(@resolved_chat_model)
999
+ def resolved_chat_class
1000
+ return @resolved_chat_class if defined?(@resolved_chat_class)
903
1001
 
904
- @resolved_chat_model = case @chat_model
905
- when String then Object.const_get(@chat_model)
906
- else @chat_model
1002
+ @resolved_chat_class = case @chat_class
1003
+ when String then Object.const_get(@chat_class)
1004
+ else @chat_class
907
1005
  end
908
1006
  end
909
1007
  end
@@ -927,22 +1025,20 @@ module RubyLLM
927
1025
  input_values = input_values.merge(inputs || {})
928
1026
 
929
1027
  if chat
930
- @chat = chat
931
- self.class.apply_configuration(@chat, input_values:, persist_instructions:)
1028
+ bind(chat, input_values)
1029
+ self.class.apply_configuration(@chat, input_values:, persist_instructions:, agent: self)
932
1030
  return
933
1031
  end
934
1032
 
935
1033
  context_value = self.class.resolved_context(inputs: input_values)
936
- @chat = self.class.build_chat(
937
- inputs: input_values,
938
- options: chat_options,
939
- resolved_context: context_value
940
- )
1034
+ bind(self.class.build_chat(inputs: input_values, options: chat_options, resolved_context: context_value),
1035
+ input_values)
941
1036
  self.class.apply_configuration(
942
1037
  @chat,
943
1038
  input_values:,
944
1039
  persist_instructions:,
945
- resolved_context: context_value
1040
+ resolved_context: context_value,
1041
+ agent: self
946
1042
  )
947
1043
  end
948
1044
 
@@ -953,6 +1049,207 @@ module RubyLLM
953
1049
  # #chat without adapting its return values.
954
1050
  def_delegators :chat, *CHAINABLE_CHAT_DELEGATES, *PASSTHROUGH_CHAT_DELEGATES
955
1051
 
1052
+ ##
1053
+ # :singleton-method: rescue_from
1054
+ # :call-seq: rescue_from(*exception_classes, with: nil, &block)
1055
+ #
1056
+ # Registers a handler for exceptions raised by the chat operations of
1057
+ # this agent's instances and of the chats and records ::chat, ::create,
1058
+ # ::create!, and ::find return: #ask, #say, #ask_later, #complete,
1059
+ # #generate, #run_tools, #step, #count_tokens, and #compact. Name the
1060
+ # handler with +with:+ or pass a block; either runs on the agent instance,
1061
+ # so the agent's #chat, inputs, and class name are available for instrumentation.
1062
+ #
1063
+ # class ApplicationAgent < RubyLLM::Agent
1064
+ # rescue_from RubyLLM::RateLimitError, RubyLLM::ConnectionError, with: :handle_transient
1065
+ # rescue_from RubyLLM::BadRequestError do |error|
1066
+ # error_tracker.notify(error)
1067
+ # raise
1068
+ # end
1069
+ #
1070
+ # private
1071
+ #
1072
+ # def handle_transient(error)
1073
+ # metrics.increment("llm.api_error", type: "transient")
1074
+ # raise
1075
+ # end
1076
+ # end
1077
+ #
1078
+ # Handlers are searched in reverse declaration order, so the last
1079
+ # matching one wins. Re-raise inside a handler to let the caller see
1080
+ # the exception; otherwise the handler's return value becomes the
1081
+ # operation's return value. Exceptions no handler matches are
1082
+ # re-raised. Subclasses use their parents' handlers, including ones
1083
+ # declared after the subclass, and their own come first.
1084
+ #
1085
+ # Exception classes may be named as Strings, which defers constant
1086
+ # lookup until an exception is raised. A class that can never match a
1087
+ # StandardError, such as Interrupt, raises ArgumentError.
1088
+
1089
+ ##
1090
+ # :method: rescue_with_handler
1091
+ # :call-seq: rescue_with_handler(exception)
1092
+ #
1093
+ # Runs the ::rescue_from handler matching +exception+ and returns its
1094
+ # value, or re-raises when no handler matches. The chat operations call
1095
+ # this for you.
1096
+
1097
+ ##
1098
+ # :singleton-method: description
1099
+ # :call-seq: description(text = nil)
1100
+ #
1101
+ # Sets what the agent does, which a model reads when the agent is one
1102
+ # of its tools (see ::to_tool), or returns it when called without an
1103
+ # argument.
1104
+ #
1105
+ # class ResearchAgent < RubyLLM::Agent
1106
+ # description "Researches a question across the web and reports back"
1107
+ # end
1108
+
1109
+ ##
1110
+ # :singleton-method: to_tool
1111
+ # :call-seq: to_tool(**inputs)
1112
+ #
1113
+ # Returns an AgentTool that runs this agent as a subagent: a model
1114
+ # hands it a task and gets its answer back. Keywords matching ::inputs
1115
+ # are given to every run; the agent's other inputs become parameters
1116
+ # the model fills. Chat#with_tools and ::tools take an Agent class too,
1117
+ # and convert it with no inputs.
1118
+ #
1119
+ # chat.with_tools(ResearchAgent.to_tool(workspace: current_workspace))
1120
+ # chat.with_tools(ResearchAgent)
1121
+ #
1122
+ # Raises ArgumentError for an agent class without a name.
1123
+
1124
+ ##
1125
+ # :singleton-method: after_message
1126
+ # :call-seq: after_message(method_name = nil, if: nil, unless: nil, &block)
1127
+ #
1128
+ # Registers a Chat#after_message callback on every chat the agent builds
1129
+ # or finds, Agent::find records included. Name a method or pass a block;
1130
+ # either runs on the agent instance, so its inputs, +chat+, and private
1131
+ # methods are available, and receives the callback's arguments when it
1132
+ # takes them. +if:+ and +unless:+ take a method name, a lambda, or an
1133
+ # Array of them, like Rails callbacks. Subclasses inherit the callbacks
1134
+ # declared when they are defined. Returns the agent class.
1135
+ #
1136
+ # For a chat from ::chat or ::find, the agent is bound to the chat
1137
+ # without running +initialize+, as for ::rescue_from handlers, so read
1138
+ # inputs and +chat+ rather than instance variables +initialize+ sets.
1139
+ # Wrapping a configured chat in a new agent, as
1140
+ # <tt>SupportAgent.new(chat:)</tt> does, moves its callbacks to that
1141
+ # agent instead of registering them twice.
1142
+ #
1143
+ # class SupportAgent < RubyLLM::Agent
1144
+ # inputs :ticket
1145
+ #
1146
+ # after_message :log_reply, if: -> { ticket.urgent? }
1147
+ # before_tool_call { |tool_call| Rails.logger.info "Calling #{tool_call.name}" }
1148
+ #
1149
+ # private
1150
+ #
1151
+ # def log_reply(message) = ticket.notes.create!(body: message.content)
1152
+ # end
1153
+ #
1154
+ # The other chat callbacks have macros of the same name and shape:
1155
+ # ::before_request, ::before_message, ::before_tool_call,
1156
+ # ::around_tool_call, ::after_tool_result, ::after_tool_progress, ::before_fallback,
1157
+ # ::after_fallback, and ::after_file_write. Raises ArgumentError for
1158
+ # both a method name and a block, neither, or another option.
1159
+
1160
+ ##
1161
+ # :singleton-method: before_request
1162
+ # :call-seq: before_request(method_name = nil, if: nil, unless: nil, &block)
1163
+ #
1164
+ # Registers a Chat#before_request callback on every chat the agent builds or
1165
+ # finds. See ::after_message.
1166
+
1167
+ ##
1168
+ # :singleton-method: before_message
1169
+ # :call-seq: before_message(method_name = nil, if: nil, unless: nil, &block)
1170
+ #
1171
+ # Registers a Chat#before_message callback on every chat the agent builds or
1172
+ # finds. See ::after_message.
1173
+
1174
+ ##
1175
+ # :singleton-method: before_tool_call
1176
+ # :call-seq: before_tool_call(method_name = nil, if: nil, unless: nil, &block)
1177
+ #
1178
+ # Registers a Chat#before_tool_call callback on every chat the agent builds or
1179
+ # finds. See ::after_message.
1180
+
1181
+ ##
1182
+ # :singleton-method: around_tool_call
1183
+ # :call-seq: around_tool_call(method_name = nil, if: nil, unless: nil, &block)
1184
+ #
1185
+ # Registers a Chat#around_tool_call callback on every chat the agent builds
1186
+ # or finds. The method or block receives the ToolCall and a block that
1187
+ # runs the tool. A method yields to it; a block takes it as +&run+:
1188
+ #
1189
+ # class SupportAgent < RubyLLM::Agent
1190
+ # inputs :account
1191
+ #
1192
+ # around_tool_call :in_tenant
1193
+ #
1194
+ # private
1195
+ #
1196
+ # def in_tenant(tool_call, &) = ActsAsTenant.with_tenant(account, &)
1197
+ # end
1198
+ #
1199
+ # When +if:+ or +unless:+ skips the callback, the tool runs without it.
1200
+ # See ::after_message.
1201
+
1202
+ ##
1203
+ # :singleton-method: after_tool_result
1204
+ # :call-seq: after_tool_result(method_name = nil, if: nil, unless: nil, &block)
1205
+ #
1206
+ # Registers a Chat#after_tool_result callback on every chat the agent builds or
1207
+ # finds. See ::after_message.
1208
+
1209
+ ##
1210
+ # :singleton-method: after_tool_progress
1211
+ # :call-seq: after_tool_progress(method_name = nil, if: nil, unless: nil, &block)
1212
+ #
1213
+ # Registers a Chat#after_tool_progress callback on every chat the agent builds or
1214
+ # finds. See ::after_message.
1215
+
1216
+ ##
1217
+ # :singleton-method: before_fallback
1218
+ # :call-seq: before_fallback(method_name = nil, if: nil, unless: nil, &block)
1219
+ #
1220
+ # Registers a Chat#before_fallback callback on every chat the agent builds or
1221
+ # finds. See ::after_message.
1222
+
1223
+ ##
1224
+ # :singleton-method: after_fallback
1225
+ # :call-seq: after_fallback(method_name = nil, if: nil, unless: nil, &block)
1226
+ #
1227
+ # Registers a Chat#after_fallback callback on every chat the agent builds or
1228
+ # finds. See ::after_message.
1229
+
1230
+ ##
1231
+ # :singleton-method: after_file_write
1232
+ # :call-seq: after_file_write(method_name = nil, if: nil, unless: nil, &block)
1233
+ #
1234
+ # Registers a Chat#after_file_write callback on every chat the agent builds or
1235
+ # finds. See ::after_message.
1236
+
1237
+ ##
1238
+ # :singleton-method: around_join
1239
+ # :call-seq: around_join(&block)
1240
+ #
1241
+ # Registers +block+ as a Chat#around_join callback on every chat the
1242
+ # agent builds or finds. Subclasses inherit the callbacks declared
1243
+ # when they are defined. Returns the list of callbacks.
1244
+ #
1245
+ # class SupportAgent < RubyLLM::Agent
1246
+ # around_join do |queued, join|
1247
+ # join.call(sender: queued.sender)
1248
+ # end
1249
+ # end
1250
+ #
1251
+ # Raises ArgumentError without a block.
1252
+
956
1253
  ##
957
1254
  # :method: with_instructions
958
1255
  # :call-seq: with_instructions(instructions, append: false, cache_until_here: false)
@@ -1019,6 +1316,18 @@ module RubyLLM
1019
1316
  #
1020
1317
  # Delegates to Chat#with_end_user. See that method for arguments and return values.
1021
1318
 
1319
+ ##
1320
+ # :method: with_owner
1321
+ # :call-seq: with_owner(owner)
1322
+ #
1323
+ # Delegates to Chat#with_owner. See that method for arguments and return values.
1324
+
1325
+ ##
1326
+ # :method: with_context_budget
1327
+ # :call-seq: with_context_budget(budget = {}, per_result: nil, per_request: nil, window: nil)
1328
+ #
1329
+ # Delegates to Chat#with_context_budget. See that method for arguments and return values.
1330
+
1022
1331
  ##
1023
1332
  # :method: with_compaction
1024
1333
  # :call-seq: with_compaction(options = {})
@@ -1031,6 +1340,24 @@ module RubyLLM
1031
1340
  #
1032
1341
  # Delegates to Chat#with_caching. See that method for arguments and return values.
1033
1342
 
1343
+ ##
1344
+ # :method: with_code_execution
1345
+ # :call-seq: with_code_execution(enabled = true, **options)
1346
+ #
1347
+ # Delegates to Chat#with_code_execution. See that method for arguments and return values.
1348
+
1349
+ ##
1350
+ # :method: with_skills
1351
+ # :call-seq: with_skills(*skills)
1352
+ #
1353
+ # Delegates to Chat#with_skills. See that method for arguments and return values.
1354
+
1355
+ ##
1356
+ # :method: with_file_store
1357
+ # :call-seq: with_file_store(store = :memory, inline_limit: 10_000, tools: %i[read write edit])
1358
+ #
1359
+ # Delegates to Chat#with_file_store. See that method for arguments and return values.
1360
+
1034
1361
  ##
1035
1362
  # :method: with_context
1036
1363
  # :call-seq: with_context(context)
@@ -1057,7 +1384,7 @@ module RubyLLM
1057
1384
 
1058
1385
  ##
1059
1386
  # :method: with_fallbacks
1060
- # :call-seq: with_fallbacks(*models, on: Fallback::DEFAULT_ERRORS)
1387
+ # :call-seq: with_fallbacks(*models, on: Fallback::DEFAULT_ERRORS, drop_unsupported: false)
1061
1388
  #
1062
1389
  # Delegates to Chat#with_fallbacks. See that method for arguments and return values.
1063
1390
 
@@ -1097,6 +1424,12 @@ module RubyLLM
1097
1424
  #
1098
1425
  # Delegates to Chat#after_tool_result. See that method for arguments and return values.
1099
1426
 
1427
+ ##
1428
+ # :method: around_tool_call
1429
+ # :call-seq: around_tool_call(&block)
1430
+ #
1431
+ # Delegates to Chat#around_tool_call. See that method for arguments and return values.
1432
+
1100
1433
  ##
1101
1434
  # :method: after_tool_progress
1102
1435
  # :call-seq: after_tool_progress(&block)
@@ -1115,6 +1448,24 @@ module RubyLLM
1115
1448
  #
1116
1449
  # Delegates to Chat#after_fallback. See that method for arguments and return values.
1117
1450
 
1451
+ ##
1452
+ # :method: around_join
1453
+ # :call-seq: around_join(&block)
1454
+ #
1455
+ # Delegates to Chat#around_join. See that method for arguments and return values.
1456
+
1457
+ ##
1458
+ # :method: join_queued_messages
1459
+ # :call-seq: join_queued_messages
1460
+ #
1461
+ # Delegates to Chat#join_queued_messages. See that method for arguments and return values.
1462
+
1463
+ ##
1464
+ # :method: after_file_write
1465
+ # :call-seq: after_file_write(&block)
1466
+ #
1467
+ # Delegates to Chat#after_file_write. See that method for arguments and return values.
1468
+
1118
1469
  ##
1119
1470
  # :method: cancel
1120
1471
  # :call-seq: cancel()
@@ -1123,25 +1474,19 @@ module RubyLLM
1123
1474
 
1124
1475
  ##
1125
1476
  # :method: approve
1126
- # :call-seq: approve(tool_call)
1477
+ # :call-seq: approve(tool_call, remember: false)
1127
1478
  #
1128
1479
  # Delegates to Chat#approve. See that method for arguments and return values.
1129
1480
 
1130
- ##
1131
- # :method: deny
1132
- # :call-seq: deny(tool_call)
1133
- #
1134
- # Delegates to Chat#deny. See that method for arguments and return values.
1135
-
1136
1481
  ##
1137
1482
  # :method: answer
1138
- # :call-seq: answer(request, **values)
1483
+ # :call-seq: answer(question, value = nil, **values)
1139
1484
  #
1140
1485
  # Delegates to Chat#answer. See that method for arguments and return values.
1141
1486
 
1142
1487
  ##
1143
1488
  # :method: decline
1144
- # :call-seq: decline(request)
1489
+ # :call-seq: decline(question, note = nil)
1145
1490
  #
1146
1491
  # Delegates to Chat#decline. See that method for arguments and return values.
1147
1492
 
@@ -1149,7 +1494,9 @@ module RubyLLM
1149
1494
  # :method: cache_until_here
1150
1495
  # :call-seq: cache_until_here(ttl: nil)
1151
1496
  #
1152
- # Delegates to Chat#cache_until_here. See that method for arguments and return values.
1497
+ # Deprecated: use Message#cache_until_here or
1498
+ # <tt>with_instructions(cache_until_here:)</tt>. Delegates to
1499
+ # Chat#cache_until_here, which warns. RubyLLM 3.0 removes it.
1153
1500
 
1154
1501
  ##
1155
1502
  # :method: model
@@ -1235,12 +1582,36 @@ module RubyLLM
1235
1582
  #
1236
1583
  # Delegates to Chat#citations. See that method for arguments and return values.
1237
1584
 
1585
+ ##
1586
+ # :method: code_execution
1587
+ # :call-seq: code_execution
1588
+ #
1589
+ # Delegates to Chat#code_execution. See that method for arguments and return values.
1590
+
1591
+ ##
1592
+ # :method: skills
1593
+ # :call-seq: skills
1594
+ #
1595
+ # Delegates to Chat#skills. See that method for arguments and return values.
1596
+
1597
+ ##
1598
+ # :method: file_store
1599
+ # :call-seq: file_store
1600
+ #
1601
+ # Delegates to Chat#file_store. See that method for arguments and return values.
1602
+
1238
1603
  ##
1239
1604
  # :method: compaction
1240
1605
  # :call-seq: compaction
1241
1606
  #
1242
1607
  # Delegates to Chat#compaction. See that method for arguments and return values.
1243
1608
 
1609
+ ##
1610
+ # :method: context_budget
1611
+ # :call-seq: context_budget
1612
+ #
1613
+ # Delegates to Chat#context_budget. See that method for arguments and return values.
1614
+
1244
1615
  ##
1245
1616
  # :method: context
1246
1617
  # :call-seq: context
@@ -1281,7 +1652,15 @@ module RubyLLM
1281
1652
  # :method: each
1282
1653
  # :call-seq: each(&block)
1283
1654
  #
1284
- # Delegates to Chat#each. See that method for arguments and return values.
1655
+ # Deprecated: iterate #messages instead. Delegates to Chat#each, which
1656
+ # warns, as do the Enumerable methods built on it. RubyLLM 3.0 removes
1657
+ # them.
1658
+
1659
+ ##
1660
+ # :method: model_id
1661
+ # :call-seq: model_id()
1662
+ #
1663
+ # Delegates to Chat#model_id. See that method for arguments and return values.
1285
1664
 
1286
1665
  ##
1287
1666
  # :method: complete?
@@ -1302,46 +1681,40 @@ module RubyLLM
1302
1681
  # Delegates to Chat#waiting?. See that method for arguments and return values.
1303
1682
 
1304
1683
  ##
1305
- # :method: awaiting_approval?
1306
- # :call-seq: awaiting_approval?()
1684
+ # :method: pending_questions
1685
+ # :call-seq: pending_questions()
1307
1686
  #
1308
- # Delegates to Chat#awaiting_approval?. See that method for arguments and return values.
1687
+ # Delegates to Chat#pending_questions. See that method for arguments and return values.
1309
1688
 
1310
1689
  ##
1311
- # :method: pending_approvals
1312
- # :call-seq: pending_approvals()
1313
- #
1314
- # Delegates to Chat#pending_approvals. See that method for arguments and return values.
1315
-
1316
- ##
1317
- # :method: awaiting_input?
1318
- # :call-seq: awaiting_input?()
1690
+ # :method: pending_tasks
1691
+ # :call-seq: pending_tasks()
1319
1692
  #
1320
- # Delegates to Chat#awaiting_input?. See that method for arguments and return values.
1693
+ # Delegates to Chat#pending_tasks. See that method for arguments and return values.
1321
1694
 
1322
1695
  ##
1323
- # :method: pending_inputs
1324
- # :call-seq: pending_inputs()
1696
+ # :method: add_message
1697
+ # :call-seq: add_message(message_or_attributes)
1325
1698
  #
1326
- # Delegates to Chat#pending_inputs. See that method for arguments and return values.
1699
+ # Delegates to Chat#add_message. See that method for arguments and return values.
1327
1700
 
1328
1701
  ##
1329
- # :method: awaiting_tasks?
1330
- # :call-seq: awaiting_tasks?()
1702
+ # :method: queue
1703
+ # :call-seq: queue(message = nil, with: nil, steer: true, sender: nil)
1331
1704
  #
1332
- # Delegates to Chat#awaiting_tasks?. See that method for arguments and return values.
1705
+ # Delegates to Chat#queue. See that method for arguments and return values.
1333
1706
 
1334
1707
  ##
1335
- # :method: pending_tasks
1336
- # :call-seq: pending_tasks()
1708
+ # :method: queued_messages
1709
+ # :call-seq: queued_messages()
1337
1710
  #
1338
- # Delegates to Chat#pending_tasks. See that method for arguments and return values.
1711
+ # Delegates to Chat#queued_messages. See that method for arguments and return values.
1339
1712
 
1340
1713
  ##
1341
- # :method: add_message
1342
- # :call-seq: add_message(message_or_attributes)
1714
+ # :method: withdraw
1715
+ # :call-seq: withdraw(queued)
1343
1716
  #
1344
- # Delegates to Chat#add_message. See that method for arguments and return values.
1717
+ # Delegates to Chat#withdraw. See that method for arguments and return values.
1345
1718
 
1346
1719
  ##
1347
1720
  # :method: tokens
@@ -1423,25 +1796,37 @@ module RubyLLM
1423
1796
  # Compacts the model context through Chat#compact and returns its Message.
1424
1797
  # Exceptions use ::rescue_from handlers.
1425
1798
 
1426
- GUARDED_OPERATIONS.each do |operation|
1799
+ Chat::Rescuable::OPERATIONS.each do |operation|
1427
1800
  define_method(operation) do |*args, **kwargs, &block|
1428
- chat.public_send(operation, *args, **kwargs, &block)
1429
- rescue StandardError => e
1430
- rescue_with_handler(e)
1801
+ rescuing { chat.public_send(operation, *args, **kwargs, &block) }
1431
1802
  end
1432
1803
  end
1433
1804
 
1434
- # Runs the ::rescue_from handler matching +exception+ and returns its
1435
- # value, or re-raises when no handler matches. The chat operations call
1436
- # this for you.
1437
- def rescue_with_handler(exception)
1438
- handler = self.class.rescue_handler_for(exception)
1439
- raise exception unless handler
1805
+ # Returns whoever the usage of the agent's chat is attributed to, as
1806
+ # Chat#owner describes. For a persisted chat, reads it from the
1807
+ # record's chat, since the record leaves +owner+ to the application.
1808
+ def owner
1809
+ (chat.respond_to?(:to_llm) ? chat.to_llm : chat).owner
1810
+ end
1440
1811
 
1441
- return instance_exec(exception, &handler) unless handler.is_a?(Symbol)
1812
+ def bind(chat, input_values) # :nodoc:
1813
+ @chat = chat
1814
+ input_values.each do |name, value|
1815
+ define_singleton_method(name) { value } unless respond_to?(name)
1816
+ end
1817
+ self
1818
+ end
1819
+
1820
+ private
1821
+
1822
+ def rescuing(&)
1823
+ return chat.rescuing_with(self, &) if chat.is_a?(Chat::Rescuable)
1442
1824
 
1443
- handler_method = method(handler)
1444
- handler_method.arity.zero? ? handler_method.call : handler_method.call(exception)
1825
+ begin
1826
+ yield
1827
+ rescue StandardError => e
1828
+ rescue_with_handler(e)
1829
+ end
1445
1830
  end
1446
1831
  end
1447
1832
  end