Files
sure/app/models/assistant/responder.rb
Guillem Arias c1dbb51553 feat(ai): add Anthropic provider with chat parity (1/5)
Introduces Provider::Anthropic alongside Provider::Openai, implementing
the LlmConcept chat_response contract over the official anthropic Ruby
SDK. Batch ops, PDF, and RAG land in follow-up PRs.

- Provider::Anthropic uses Messages API for sync and streaming responses
- ChatConfig builds requests with ephemeral prompt-cache markers on the
  system prompt and the last tool definition
- MessageFormatter reconstructs multi-turn history (text + tool_use +
  tool_result blocks) from raw Message records, including the paired
  user-role tool_result turn Anthropic requires after every tool_use
- ChatParser maps Anthropic Message into the shared ChatResponse Data
- Registry, Setting, User, Chat default model wired for ANTHROPIC_*
  envs and Setting.anthropic_*; LLM_PROVIDER selects between providers
- Responder forwards raw conversation_history (Array<Message>) so
  providers without hosted conversation state can rebuild context
- OpenAI provider accepts and ignores the new kwarg (no behavior change)

Tests cover provider init, model gating, MessageFormatter for all turn
shapes, ChatConfig request building (max_tokens, system cache, tool
conversion), ChatParser for text / tool_use / mixed blocks, Registry
discovery, and mocked chat_response success / error / function_request
paths. Live VCR cassettes recorded in a follow-up with a real key.

Stacked PRs: 2/5 batch ops + cost ledger, 3/5 PDF, 4/5 pgvector RAG,
5/5 settings UI + disclosure.
2026-05-25 16:29:53 +02:00

176 lines
5.0 KiB
Ruby

class Assistant::Responder
def initialize(message:, instructions:, function_tool_caller:, llm:)
@message = message
@instructions = instructions
@function_tool_caller = function_tool_caller
@llm = llm
end
def on(event_name, &block)
listeners[event_name.to_sym] << block
end
def respond(previous_response_id: nil)
# Track whether response was handled by streamer
response_handled = false
# For the first response
streamer = proc do |chunk|
case chunk.type
when "output_text"
emit(:output_text, chunk.data)
when "response"
response = chunk.data
response_handled = true
if response.function_requests.any?
handle_follow_up_response(response)
else
emit(:response, { id: response.id })
end
end
end
response = get_llm_response(streamer: streamer, previous_response_id: previous_response_id)
# For synchronous (non-streaming) responses, handle function requests if not already handled by streamer
unless response_handled
if response && response.function_requests.any?
handle_follow_up_response(response)
elsif response
emit(:response, { id: response.id })
end
end
end
private
attr_reader :message, :instructions, :function_tool_caller, :llm
def handle_follow_up_response(response)
streamer = proc do |chunk|
case chunk.type
when "output_text"
emit(:output_text, chunk.data)
when "response"
# We do not currently support function executions for a follow-up response (avoid recursive LLM calls that could lead to high spend)
emit(:response, { id: chunk.data.id })
end
end
function_tool_calls = function_tool_caller.fulfill_requests(response.function_requests)
emit(:response, {
id: response.id,
function_tool_calls: function_tool_calls
})
# Get follow-up response with tool call results
get_llm_response(
streamer: streamer,
function_results: function_tool_calls.map(&:to_result),
previous_response_id: response.id
)
end
def get_llm_response(streamer:, function_results: [], previous_response_id: nil)
response = llm.chat_response(
message.content,
model: message.ai_model,
instructions: instructions,
functions: function_tool_caller.function_definitions,
function_results: function_results,
messages: conversation_history,
conversation_history: chat_message_records,
streamer: streamer,
previous_response_id: previous_response_id,
session_id: chat_session_id,
user_identifier: chat_user_identifier,
family: message.chat&.user&.family
)
unless response.success?
raise response.error
end
response.data
end
def emit(event_name, payload = nil)
listeners[event_name.to_sym].each { |block| block.call(payload) }
end
def listeners
@listeners ||= Hash.new { |h, k| h[k] = [] }
end
def chat_session_id
chat&.id&.to_s
end
def chat_user_identifier
return unless chat&.user_id
::Digest::SHA256.hexdigest(chat.user_id.to_s)
end
def chat
@chat ||= message.chat
end
# Raw Message records preceding the current turn — providers that build
# their own native message shape (Anthropic) consume this directly so they
# do not have to round-trip through the OpenAI-shaped `conversation_history`.
def chat_message_records
return [] unless chat&.messages
chat.messages
.where(type: [ "UserMessage", "AssistantMessage" ], status: "complete")
.where.not(id: message.id)
.includes(:tool_calls)
.ordered
.to_a
end
def conversation_history
messages = []
return messages unless chat&.messages
chat.messages
.where(type: [ "UserMessage", "AssistantMessage" ], status: "complete")
.includes(:tool_calls)
.ordered
.each do |chat_message|
if chat_message.tool_calls.any?
messages << {
role: chat_message.role,
content: chat_message.content || "",
tool_calls: chat_message.tool_calls.map(&:to_tool_call)
}
chat_message.tool_calls.map(&:to_result).each do |fn_result|
# Handle nil explicitly to avoid serializing to "null"
output = fn_result[:output]
content = if output.nil?
""
elsif output.is_a?(String)
output
else
output.to_json
end
messages << {
role: "tool",
tool_call_id: fn_result[:call_id],
name: fn_result[:name],
content: content
}
end
elsif !chat_message.content.blank?
messages << { role: chat_message.role, content: chat_message.content || "" }
end
end
messages
end
end