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
@@ -0,0 +1,636 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module ActiveRecord
5
+ # Claims a persisted chat for the process that runs its conversation
6
+ # loop, and each tool call for the process that answers it. Claims are
7
+ # conditional updates, so no transaction stays open while a model or a
8
+ # tool runs.
9
+ #
10
+ # A claim is a lease of CLAIM_LEASE seconds that a heartbeat thread
11
+ # renews every CLAIM_RENEWAL seconds, or every quarter of a shorter
12
+ # Configuration#max_run_time, so the work of a process that dies
13
+ # is free again within a lease. Renewal stops at the claim's deadline:
14
+ # Configuration#max_run_time after the run last made progress, or for
15
+ # a tool call, its run time after it starts, so a run that hangs past
16
+ # it counts as crashed. A call waiting for the calls before it in its
17
+ # round keeps its claim as long as the chat's claim lasts. A run inside
18
+ # a transaction can't renew from another connection, so its claims
19
+ # last until their deadline instead.
20
+ module RunClaims # :nodoc:
21
+ class ClaimLost < StandardError; end # :nodoc:
22
+ private_constant :ClaimLost
23
+
24
+ # A tool call this process claimed: its row, when its claim ends, the
25
+ # expiries written to the row, the seconds it may run, whether it
26
+ # started, and whether it was interrupted: claimed from a process that
27
+ # had started it and stopped before it saved the result.
28
+ ToolCallClaim = Struct.new(:row, :deadline, :written, :seconds, :started, :interrupted)
29
+ private_constant :ToolCallClaim
30
+
31
+ CLAIM_LEASE = 60
32
+ CLAIM_RENEWAL = 15
33
+
34
+ RUN_STATE = %i[
35
+ @chat @message @last_message_id @last_cancellation_poll_at @unpersisted_instructions
36
+ @usage_records_by_entry @run_claim_token @run_claim_deadline @run_claim_renewed_at
37
+ @run_claim_lost_token @synced_run_claim_token @synced_usage_count @tool_call_claims
38
+ @claim_heartbeat @claim_lock @tool_call_renewal_lock @lost_tool_calls
39
+ ].freeze
40
+ private_constant :RUN_STATE
41
+
42
+ EXIT_LOCK = Mutex.new
43
+ private_constant :EXIT_LOCK
44
+
45
+ # Tells the runs of this process that it will exit by +time+ without
46
+ # unwinding, as a Solid Queue worker does once its shutdown timeout
47
+ # passes, so no run can expire its claims as it stops. Claims end by
48
+ # then from now on, and live heartbeats cut theirs back to it at once,
49
+ # so a chat still running at the exit counts as interrupted as the
50
+ # process ends rather than a lease later. A run that finishes first
51
+ # releases its claims as usual. The earliest time wins, to the whole
52
+ # second, which a column that rounds fractions up still holds.
53
+ def self.process_exits_by(time)
54
+ time = time.change(usec: 0)
55
+ EXIT_LOCK.synchronize { @exit_deadline = [@exit_deadline, time].compact.min }
56
+ ClaimHeartbeat.hurry_all
57
+ end
58
+
59
+ def self.exit_deadline # :nodoc:
60
+ @exit_deadline
61
+ end
62
+
63
+ # The chats whose run died holding its claim. Every run that ends
64
+ # releases its claim, so a token whose lease expired is left only by
65
+ # a process that died or shut down mid-run. A chat someone stopped
66
+ # stays stopped. It skips the query cache, since a job polls it for
67
+ # what other processes wrote.
68
+ def self.interrupted(relation)
69
+ chat = relation.klass
70
+ return relation.none unless (%w[claim_token claimed_until] - chat.column_names).empty?
71
+
72
+ died = relation.where.not(claim_token: nil).where(claimed_until: ..Time.current)
73
+ died = died.where(cancelled: false) if chat.column_names.include?('cancelled')
74
+ died.skip_query_cache!
75
+ end
76
+
77
+ def run_claim_lost?(error) # :nodoc:
78
+ error.is_a?(ClaimLost)
79
+ end
80
+
81
+ def claim_persisted_tool_calls(leases, executing:) # :nodoc:
82
+ return leases.keys.map(&:id) unless tool_call_claims?
83
+
84
+ start_claim_heartbeat
85
+ claimed = claim_tool_call_rows(leases, executing)
86
+ resync_messages if claimed.size < leases.size
87
+ claimed
88
+ rescue StandardError
89
+ claim_lock.synchronize { leases.each_key { |tool_call| tool_call_claims.delete(tool_call.id) } }
90
+ stop_idle_claim_heartbeat
91
+ raise
92
+ end
93
+
94
+ # A call that was running when its process shut down may have done
95
+ # its work, such as a message it already sent, so it keeps its claim,
96
+ # expired at once like the chat's: the expired claim is the only
97
+ # record that it started. So does an interrupted call this run didn't
98
+ # get to answer, which stays interrupted for the next one. Every other
99
+ # call is released. A round that
100
+ # stopped before it +finished+ writes its claims without yielding to a
101
+ # fiber scheduler, as #unyielding explains, while one that finished
102
+ # releases the calls that paused as usual.
103
+ def release_persisted_tool_calls(tool_call_ids, stopped_by = nil, finished: false) # :nodoc:
104
+ expired = Time.current.change(usec: 0)
105
+ shutting_down = shutting_down?(stopped_by)
106
+ tool_call_ids.each do |tool_call_id|
107
+ claim = claim_lock.synchronize { tool_call_claims.delete(tool_call_id) }
108
+ next unless claim
109
+
110
+ kept = claim.interrupted || (claim.started && shutting_down)
111
+ unyielding(!finished) do
112
+ RubyLLM::ActiveRecord::ToolCall.where(id: claim.row, result_id: nil, claimed_until: claim.written)
113
+ .update_all(claimed_until: (expired if kept))
114
+ end
115
+ end
116
+ ensure
117
+ stop_idle_claim_heartbeat
118
+ end
119
+
120
+ # Counts a claimed tool call's run time from now, when it starts, not
121
+ # from when its round was claimed, so waiting for the calls before it
122
+ # costs it nothing, and stretches the chat's claim to cover it. Until
123
+ # then the call keeps its claim as long as the chat does. Both claims
124
+ # are written before the call runs, so neither can lapse before the
125
+ # heartbeat's next renewal, and the call doesn't run once another
126
+ # process took either over.
127
+ def persisted_tool_call_started(tool_call_id) # :nodoc:
128
+ now = Time.current.change(usec: 0)
129
+ claim = claim_lock.synchronize do
130
+ started = tool_call_claims[tool_call_id]
131
+ raise ClaimLost if started.nil? && lost_tool_calls.include?(tool_call_id)
132
+ next unless started
133
+
134
+ started.started = true
135
+ started.deadline = now + started.seconds
136
+ @run_claim_deadline = [@run_claim_deadline, started.deadline].max if @run_claim_token
137
+ started
138
+ end
139
+ return unless claim
140
+
141
+ write_run_claim(now) if @run_claim_token
142
+ raise ClaimLost unless started_tool_call_claimed?(tool_call_id, claim, now)
143
+ end
144
+
145
+ # Whether this run claimed +tool_call_id+ from a process that had
146
+ # started it and stopped, by a shutdown or a crash, before it saved
147
+ # the result. Its claim is left expired, or lapses, while a call that
148
+ # never started is released.
149
+ def persisted_tool_call_interrupted?(tool_call_id) # :nodoc:
150
+ claim_lock.synchronize { tool_call_claims[tool_call_id]&.interrupted }
151
+ end
152
+
153
+ def tool_call_running_elsewhere?(tool_call) # :nodoc:
154
+ RubyLLM::ActiveRecord::ToolCall.uncached { find_tool_call(tool_call.id) }&.running? || false
155
+ end
156
+
157
+ private
158
+
159
+ def claim_tool_call_rows(leases, executing)
160
+ rows = RubyLLM::ActiveRecord::ToolCall.uncached { tool_call_rows(leases.keys.map(&:id)) }
161
+ RubyLLM::ActiveRecord::ToolCall.transaction do
162
+ ids = leases.filter_map do |tool_call, seconds|
163
+ row = rows[tool_call.id]
164
+ tool_call.id if row.nil? || tool_call_claimed?(tool_call.id, row, seconds, executing.include?(tool_call.id))
165
+ end
166
+ extend_run_claim_for_tool_calls
167
+ ids
168
+ end
169
+ end
170
+
171
+ def initialize_dup(*)
172
+ super
173
+ RUN_STATE.each { |name| instance_variable_set(name, nil) }
174
+ %i[claim_token claimed_until].each { |name| self[name] = nil if has_attribute?(name) }
175
+ self[:cancelled] = false if has_attribute?(:cancelled)
176
+ end
177
+
178
+ # Hold the same row a run claims without replacing its token, so a
179
+ # failed submission leaves an interrupted run discoverable.
180
+ def stage_while_idle
181
+ transaction(requires_new: true) do
182
+ next false unless unclaimed(chat_row, Time.current).update_all('claim_token = claim_token') == 1
183
+
184
+ resync_messages if @chat && run_finished_elsewhere?
185
+ yield
186
+ true
187
+ end
188
+ rescue StandardError
189
+ resync_messages if @chat
190
+ raise
191
+ end
192
+
193
+ def exclusively(busy, &)
194
+ return yield if @run_claim_token
195
+
196
+ holding_run_claim(busy, &)
197
+ end
198
+
199
+ # A run stopped because its process is shutting down didn't end, so
200
+ # it keeps its claim but expires it, and the chat counts as
201
+ # interrupted at once.
202
+ def holding_run_claim(busy)
203
+ if run_claims?
204
+ start_claim_heartbeat
205
+ return busy.call unless run_claim_acquired?
206
+ end
207
+
208
+ settle_cancellation_request
209
+ result = yield
210
+ completed = true
211
+ result
212
+ rescue ClaimLost
213
+ @message.destroy if blank_placeholder?
214
+ resync_messages if @chat
215
+ busy.call
216
+ rescue SignalException, SystemExit => e
217
+ stopped_by = e
218
+ raise
219
+ ensure
220
+ unyielding(!completed) do
221
+ stop_claim_heartbeat
222
+ ensure
223
+ shutting_down?(stopped_by) ? expire_run_claim : release_run_claim(completed)
224
+ end
225
+ end
226
+
227
+ # Under a fiber scheduler, as in Async or Solid Queue's fiber workers,
228
+ # a fiber whose thread is exiting or whose reactor is stopping is
229
+ # cancelled, or never resumed, once it waits on a socket, a file or a
230
+ # thread. So a run that +stopped+ before it finished ends its claims
231
+ # without yielding to the scheduler, and one that finished yields as
232
+ # usual.
233
+ def unyielding(stopped, &)
234
+ stopped ? Fiber.blocking(&) : yield
235
+ end
236
+
237
+ # Whether the process is shutting down: a thread killed at exit, or
238
+ # +stopped_by+ a signal such as TERM or an exit. Interrupt doesn't
239
+ # count: Ctrl-C in a console stops the run, and a job backend that
240
+ # interrupts a job on shutdown, as Sidekiq does, runs it again.
241
+ def shutting_down?(stopped_by)
242
+ return true if Thread.current.status == 'aborting'
243
+
244
+ (stopped_by.is_a?(SignalException) || stopped_by.is_a?(SystemExit)) && !stopped_by.is_a?(Interrupt)
245
+ end
246
+
247
+ def run_claim_acquired?
248
+ now = Time.current
249
+ deadline = now + run_claim_duration
250
+ claim = { claim_token: SecureRandom.uuid, claimed_until: leased_until(deadline, now) }
251
+ available = unclaimed(chat_row, now)
252
+ unless available.where(claim_token: @synced_run_claim_token).update_all(claim) == 1
253
+ return false unless available.update_all(claim) == 1
254
+
255
+ stale = true
256
+ end
257
+
258
+ @run_claim_token = claim[:claim_token]
259
+ @run_claim_deadline = deadline
260
+ @run_claim_renewed_at = now
261
+ resync_messages if @chat && (stale || run_finished_elsewhere?)
262
+ true
263
+ end
264
+
265
+ # A released claim leaves no token behind, so what another process's
266
+ # run wrote is the only sign of it. A failed attempt adds no message,
267
+ # but each model call adds a usage row. Continuing an unfinished reply
268
+ # keeps its id and may lose its usage row, so such a reply is always
269
+ # read again: repeating it would bill the model twice.
270
+ def run_finished_elsewhere?
271
+ @chat.messages.last&.continuation || latest_message_id != @last_message_id ||
272
+ usage_row_count != @synced_usage_count
273
+ end
274
+
275
+ def usage_row_count
276
+ ruby_llm_usages.klass.uncached { ruby_llm_usages.count }
277
+ end
278
+
279
+ # Whole seconds, so a column that rounds fractions up still holds a
280
+ # time that has passed.
281
+ def expire_run_claim
282
+ token = @run_claim_token
283
+ @synced_usage_count = @synced_run_claim_token = @run_claim_token = nil
284
+ chat_row.where(claim_token: token).update_all(claimed_until: Time.current.change(usec: 0)) if token
285
+ end
286
+
287
+ def release_run_claim(completed)
288
+ token = @run_claim_token
289
+ return unless token
290
+
291
+ @synced_usage_count = @synced_run_claim_token = @run_claim_token = nil
292
+ @synced_usage_count = usage_row_count if completed
293
+ ensure
294
+ free_chat_row(token) if token
295
+ end
296
+
297
+ def free_chat_row(token)
298
+ cancellable = has_attribute?(:cancelled)
299
+ released = { claim_token: nil, claimed_until: nil }
300
+ released[:cancelled] = false if cancellable
301
+ self[:cancelled] = false if chat_row.where(claim_token: token).update_all(released) == 1 && cancellable
302
+ end
303
+
304
+ def renew_run_claim(force: false)
305
+ return unless @run_claim_token
306
+ raise ClaimLost if @run_claim_lost_token == @run_claim_token
307
+
308
+ now = Time.current
309
+ claim_lock.synchronize { @run_claim_deadline = [@run_claim_deadline, now + run_claim_duration].max }
310
+ write_run_claim(now) if force || now >= @run_claim_renewed_at + claim_renewal_interval
311
+ end
312
+
313
+ def write_run_claim(now = Time.current)
314
+ claimed_until = leased_until(@run_claim_deadline, now)
315
+ raise ClaimLost unless chat_claim_extended?(@run_claim_token, claimed_until)
316
+
317
+ @run_claim_renewed_at = now
318
+ end
319
+
320
+ # Writes the chat's claim, moving its stored expiry forward and never
321
+ # back: the heartbeat and a starting tool call write it from separate
322
+ # threads, and the older of two writes can land last. The one way back
323
+ # is to the process's exit, past which no claim lasts. Returns whether
324
+ # the claim is still this run's.
325
+ def chat_claim_extended?(token, claimed_until)
326
+ exit_deadline = RunClaims.exit_deadline
327
+ cut = ' WHEN claimed_until > ? THEN ?' if exit_deadline
328
+ chat_row.where(claim_token: token).update_all(
329
+ ["claimed_until = CASE WHEN claimed_until IS NULL OR claimed_until < ? THEN ?#{cut} ELSE claimed_until END",
330
+ claimed_until, claimed_until, *([exit_deadline] * 2 if exit_deadline)]
331
+ ) == 1
332
+ end
333
+
334
+ # Runs on the heartbeat's thread. A chat claim it finds taken is lost,
335
+ # but only the run's own thread raises, at its next renewal.
336
+ def renew_claim_leases
337
+ now = Time.current
338
+ renew_run_claim_lease(now)
339
+ renew_tool_call_leases(now)
340
+ end
341
+
342
+ def renew_run_claim_lease(now)
343
+ token = @run_claim_token
344
+ deadline = @run_claim_deadline
345
+ return unless token && deadline > now
346
+
347
+ claimed_until = by_exit([now + CLAIM_LEASE, deadline].min)
348
+ if chat_claim_extended?(token, claimed_until)
349
+ @run_claim_renewed_at = now
350
+ else
351
+ @run_claim_lost_token = token
352
+ end
353
+ end
354
+
355
+ def renew_tool_call_leases(now)
356
+ tool_call_renewal_lock.synchronize do
357
+ renewals = claim_lock.synchronize do
358
+ tool_call_claims.filter_map do |tool_call_id, claim|
359
+ deadline = tool_call_deadline(claim)
360
+ next unless deadline
361
+
362
+ claimed_until = by_exit([now.change(usec: 0) + CLAIM_LEASE, deadline].min)
363
+ next if deadline <= now || claim.written.include?(claimed_until)
364
+
365
+ [tool_call_id, claim.row, claim.written.dup, claim.written.push(claimed_until).last]
366
+ end
367
+ end
368
+ renewals.each { |renewal| renew_tool_call_lease(*renewal) }
369
+ end
370
+ end
371
+
372
+ # Writes a starting call's claim to its row, unless another process
373
+ # took the row over, and returns whether this process still holds it.
374
+ def started_tool_call_claimed?(tool_call_id, claim, now)
375
+ tool_call_renewal_lock.synchronize do
376
+ claimed_until = leased_until(claim.deadline, now)
377
+ written = claim_lock.synchronize { claim.written.dup.tap { claim.written.push(claimed_until) } }
378
+ renew_tool_call_lease(tool_call_id, claim.row, written, claimed_until)
379
+ claim_lock.synchronize { tool_call_claims[tool_call_id].equal?(claim) }
380
+ end
381
+ end
382
+
383
+ # A call that hasn't started yet, waiting for the calls before it in
384
+ # its round, keeps its claim as long as the chat's claim lasts. Once
385
+ # another process takes the chat over, this run won't start it, so
386
+ # there is no deadline to renew to.
387
+ def tool_call_deadline(claim)
388
+ return claim.deadline if claim.started || !@run_claim_token
389
+ return if @run_claim_lost_token == @run_claim_token
390
+
391
+ [claim.deadline, @run_claim_deadline].max
392
+ end
393
+
394
+ def renew_tool_call_lease(tool_call_id, row, written, claimed_until)
395
+ renewed = RubyLLM::ActiveRecord::ToolCall.where(id: row, result_id: nil, claimed_until: written)
396
+ .update_all(claimed_until:) == 1
397
+ claim_lock.synchronize do
398
+ claim = tool_call_claims[tool_call_id]
399
+ next unless claim&.row == row
400
+ next claim.written = [claimed_until] if renewed
401
+
402
+ tool_call_claims.delete(tool_call_id)
403
+ lost_tool_calls << tool_call_id
404
+ end
405
+ end
406
+
407
+ def start_claim_heartbeat
408
+ return if @claim_heartbeat || claims_in_transaction?
409
+
410
+ claim_lock
411
+ @claim_heartbeat = ClaimHeartbeat.new([self.class, RubyLLM::ActiveRecord::ToolCall], claim_renewal_interval) do
412
+ renew_claim_leases
413
+ end
414
+ end
415
+
416
+ def stop_idle_claim_heartbeat
417
+ stop_claim_heartbeat unless @run_claim_token || claim_lock.synchronize { tool_call_claims.any? }
418
+ end
419
+
420
+ def stop_claim_heartbeat
421
+ heartbeat = @claim_heartbeat
422
+ @claim_heartbeat = nil
423
+ heartbeat&.stop
424
+ end
425
+
426
+ def claims_in_transaction?
427
+ self.class.connection_pool.with_connection(&:transaction_open?)
428
+ end
429
+
430
+ def leased_until(deadline, now)
431
+ by_exit(@claim_heartbeat ? [now + CLAIM_LEASE, deadline].min : deadline)
432
+ end
433
+
434
+ def by_exit(claimed_until)
435
+ exit_deadline = RunClaims.exit_deadline
436
+ exit_deadline && exit_deadline < claimed_until ? exit_deadline : claimed_until
437
+ end
438
+
439
+ # A max_run_time shorter than the lease is the claim's whole life, so
440
+ # a renewal has to land within it.
441
+ def claim_renewal_interval
442
+ [CLAIM_RENEWAL, [CLAIM_LEASE, run_claim_duration].min / 4.0].min
443
+ end
444
+
445
+ def running_elsewhere?
446
+ return false unless run_claims?
447
+
448
+ token, claimed_until = self.class.uncached { chat_row.pick(:claim_token, :claimed_until) }
449
+ (claimed_until&.future? && token != @run_claim_token) || false
450
+ end
451
+
452
+ def work_arrived_during_run?
453
+ return false if @run_claim_token || !run_claims?
454
+
455
+ resync_messages if latest_message_id != @last_message_id
456
+ !to_llm.complete? && !to_llm.waiting?
457
+ end
458
+
459
+ def latest_message_id
460
+ messages_association.klass.uncached do
461
+ messages_association.reorder(created_at: :desc, id: :desc).pick(:id)
462
+ end
463
+ end
464
+
465
+ def resync_messages
466
+ messages_association.reset
467
+ reload_usages
468
+ messages_association.klass.uncached { sync_messages }
469
+ end
470
+
471
+ def reload_usages
472
+ ruby_llm_usages.reset
473
+ ruby_llm_usages.klass.uncached { ruby_llm_usages.load }
474
+ end
475
+
476
+ def snapshot_run_claim
477
+ return unless run_claims?
478
+
479
+ # Counting first means a run that starts after the token is read
480
+ # still changes the count this process compares against.
481
+ @synced_usage_count = usage_row_count
482
+ @synced_run_claim_token = self.class.uncached { chat_row.pick(:claim_token) }
483
+ end
484
+
485
+ # Tool call rows carry no token, so this process recognizes its claim
486
+ # on a row by the whole-second expiries it wrote there.
487
+ def tool_call_claimed?(tool_call_id, row, seconds, executing)
488
+ now = Time.current.change(usec: 0)
489
+ seconds = seconds.to_f.ceil
490
+ deadline = now + seconds
491
+ claimed_until = leased_until(deadline, now)
492
+ scope = unclaimed(RubyLLM::ActiveRecord::ToolCall.where(id: row, result_id: nil), now)
493
+ if executing && row_supports_approval?
494
+ approval = RubyLLM::ActiveRecord::ToolCall.arel_table[:approval]
495
+ scope = scope.where(approval.eq(nil).or(approval.not_eq('denied')))
496
+ end
497
+ claimed, expired = claim_tool_call_row(scope, { claimed_until:, updated_at: now })
498
+ if claimed
499
+ claim = ToolCallClaim.new(row, deadline, [claimed_until], seconds, false, expired)
500
+ claim_lock.synchronize do
501
+ lost_tool_calls.delete(tool_call_id)
502
+ tool_call_claims[tool_call_id] = claim
503
+ end
504
+ end
505
+ claimed
506
+ end
507
+
508
+ # A released call has no expiry, while a call that started keeps its
509
+ # expired claim when its process shuts down, or lets it lapse when the
510
+ # process dies. Returns whether the claim was written, and whether it
511
+ # took over an expired one.
512
+ def claim_tool_call_row(scope, written)
513
+ return [true, false] if scope.where(claimed_until: nil).update_all(written) == 1
514
+
515
+ [scope.where.not(claimed_until: nil).update_all(written) == 1, true]
516
+ end
517
+
518
+ def extend_run_claim_for_tool_calls
519
+ extended = claim_lock.synchronize do
520
+ latest = tool_call_claims.values.map(&:deadline).max
521
+ @run_claim_deadline = [@run_claim_deadline, latest].max if @run_claim_token && latest
522
+ end
523
+ write_run_claim if extended
524
+ end
525
+
526
+ def tool_result_recorded?(tool_call, message)
527
+ attributes = { result_type: @message.class.polymorphic_name, result_id: @message.id, updated_at: Time.current }
528
+ attributes[:mcp_result] = message.mcp_result&.dump if tool_call.has_attribute?(:mcp_result)
529
+ attributes[:claimed_until] = nil if tool_call.has_attribute?(:claimed_until)
530
+ claim_lock.synchronize { tool_call_claims.delete(tool_call.tool_call_id) }
531
+ RubyLLM::ActiveRecord::ToolCall.where(id: tool_call.id, result_id: nil).update_all(attributes) == 1
532
+ end
533
+
534
+ def decide_persisted_tool_call(record, decision)
535
+ return if record.approval == decision || decision_recorded?(record, decision)
536
+ return unless decision == 'denied' && RubyLLM::ActiveRecord::ToolCall.uncached { record.reload }.running?
537
+
538
+ raise ToolCallRunningError, "The #{record.name} tool call is already running"
539
+ end
540
+
541
+ def decision_recorded?(record, decision)
542
+ now = Time.current
543
+ scope = RubyLLM::ActiveRecord::ToolCall.where(id: record.id, result_id: nil)
544
+ scope = unclaimed(scope, now) if record.has_attribute?(:claimed_until)
545
+ transaction do
546
+ forget_stale_cancellation
547
+ scope.update_all(approval: decision, updated_at: now) == 1 || raise(::ActiveRecord::Rollback)
548
+ end
549
+ end
550
+
551
+ def discard_superseded_message
552
+ @message.destroy
553
+ raise ClaimLost
554
+ end
555
+
556
+ # Renewing inside the transaction holds the chat row until the result
557
+ # commits, so no other process can take the chat over in between. A
558
+ # message that touches its chat touches it after the commit: on
559
+ # Postgres, a second write to the row the claim renewed rechecks every
560
+ # foreign key on it and locks each row the chat references until the
561
+ # commit, which can deadlock with the application's own locks.
562
+ def fenced_by_run_claim(&)
563
+ reflection, columns = message_chat_touch if @run_claim_token
564
+ committed = transaction do
565
+ renew_run_claim(force: true)
566
+ reflection ? reflection.klass.no_touching(&) : yield
567
+ end
568
+ @message.public_send(reflection.name)&.touch(*columns) if reflection && committed
569
+ committed
570
+ rescue ClaimLost
571
+ @message.destroy
572
+ raise
573
+ end
574
+
575
+ def message_chat_touch
576
+ reflection = @message.class.reflect_on_association(@message.class.chat_association_name)
577
+ touch = reflection&.options&.dig(:touch)
578
+ [reflection, touch == true ? [] : Array(touch)] if touch
579
+ end
580
+
581
+ def tool_call_rows(tool_call_ids)
582
+ RubyLLM::ActiveRecord::ToolCall.where(
583
+ tool_call_id: tool_call_ids,
584
+ message_type: messages_association.klass.polymorphic_name,
585
+ message_id: messages_association.select(:id)
586
+ ).pluck(:tool_call_id, :id).to_h
587
+ end
588
+
589
+ def unclaimed(scope, now)
590
+ claimed_until = scope.arel_table[:claimed_until]
591
+ scope.where(claimed_until.eq(nil).or(claimed_until.lteq(now)))
592
+ end
593
+
594
+ def chat_row
595
+ self.class.unscoped.where(self.class.primary_key => id)
596
+ end
597
+
598
+ def tool_call_claims
599
+ @tool_call_claims ||= {}
600
+ end
601
+
602
+ # Calls whose claim the heartbeat found taken, so their start fails
603
+ # rather than taking them for calls that were never claimed.
604
+ def lost_tool_calls
605
+ @lost_tool_calls ||= Set.new
606
+ end
607
+
608
+ def claim_lock
609
+ @claim_lock ||= Mutex.new
610
+ end
611
+
612
+ # Keeps the heartbeat and a starting call from writing a tool call's
613
+ # row at once, when one write would find the other's expiry there and
614
+ # take the claim for lost.
615
+ def tool_call_renewal_lock
616
+ claim_lock.synchronize { @tool_call_renewal_lock ||= Mutex.new }
617
+ end
618
+
619
+ def run_claims?
620
+ persisted? && has_attribute?(:claim_token) && has_attribute?(:claimed_until)
621
+ end
622
+
623
+ def tool_call_claims?
624
+ RubyLLM::ActiveRecord::ToolCall.column_names.include?('claimed_until')
625
+ end
626
+
627
+ def row_supports_approval?
628
+ RubyLLM::ActiveRecord::ToolCall.column_names.include?('approval')
629
+ end
630
+
631
+ def run_claim_duration
632
+ (context&.config || RubyLLM.config).max_run_time.to_f.ceil
633
+ end
634
+ end
635
+ end
636
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module ActiveRecord
5
+ # Solid Queue stops a worker on TERM and waits its shutdown timeout for
6
+ # the jobs to finish. Then it ends the process with exit!, which runs no
7
+ # ensure block, so a run still going can't expire its claims as it stops.
8
+ # Once a process that ends that way starts stopping, its claims end by
9
+ # the time it exits. A forked worker's process ends with the worker. In
10
+ # async mode the workers are threads of the supervisor's process, which
11
+ # ends when the supervisor stops, while a worker the supervisor replaces
12
+ # leaves the process running.
13
+ module SolidQueueShutdown # :nodoc:
14
+ module_function
15
+
16
+ def install(solid_queue)
17
+ solid_queue.on_worker_stop { |worker| exiting(solid_queue) if forked?(worker) }
18
+ solid_queue.on_stop { |supervisor| exiting(solid_queue) if supervisor.try(:mode).to_s == 'async' }
19
+ end
20
+
21
+ def forked?(worker)
22
+ supervisor = worker.supervisor
23
+ !supervisor.nil? && supervisor.pid != ::Process.pid
24
+ end
25
+
26
+ def exiting(solid_queue)
27
+ require 'ruby_llm/active_record/claim_heartbeat'
28
+ require 'ruby_llm/active_record/run_claims'
29
+ RunClaims.process_exits_by(Time.current + solid_queue.shutdown_timeout)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyLLM
4
+ module ActiveRecord
5
+ # Compares the system messages a chat record persisted with the
6
+ # instructions about to be applied to it.
7
+ module SystemInstructions # :nodoc:
8
+ # Returns whether the persisted system messages are exactly
9
+ # +instructions+, pairs of content and +cache_until_here+ in order.
10
+ # An agent applied to a record on every request asks before it
11
+ # rewrites them.
12
+ def system_instructions_persisted?(instructions)
13
+ records = messages_association.where(role: :system).order(:id).to_a
14
+ records.size == instructions.size && records.zip(instructions).all? do |record, (content, cache_until_here)|
15
+ system_instruction_attributes(record, content, cache_until_here).all? do |column, value|
16
+ record[column] == value
17
+ end
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def system_instruction_attributes(record, instructions, cache_until_here)
24
+ boundary = cache_boundary_attributes(cache_until_here)
25
+ boundary.delete(:cache_until_here) unless record.has_attribute?(:cache_until_here)
26
+ { content: instructions, **boundary }
27
+ end
28
+ end
29
+ end
30
+ end