Files
sure/app/models/assistant/responder.rb
Guillem Arias 5ab9bb33d6 fix(ai): sanitize Langfuse warn logs, normalize tool_use.input, dedup history fetch
Addresses three open CodeRabbit findings on PR #1983.

- Provider::Anthropic Langfuse rescue branches no longer include
  `e.full_message` in `Rails.logger.warn`. `full_message` bundles the
  backtrace + cause chain and on some SDK error types includes the
  serialized request/response payload (prompt, model output). Logs
  now report `#{e.class}: #{e.message}` only. Three sites:
  create_langfuse_trace, log_langfuse_generation, upsert_langfuse_trace.
  Note: Provider::Openai has the same pattern (copy-pasted source) —
  harmonization deferred to a follow-up cleanup PR; this commit fixes
  only the Anthropic provider to keep PR scope tight.

- MessageFormatter#parse_arguments now coerces any non-Hash parsed
  result to `{}`. Anthropic's Messages API requires `tool_use.input`
  to be a JSON object (map); a stored ToolCall::Function record whose
  arguments parse to a scalar, bool, or array (corrupt row, legacy
  data, cross-provider bleed) would otherwise produce a payload the
  API rejects. Normal flow stores Hash arguments end-to-end so the
  fix is defensive — adds 2 tests covering scalar/array JSON strings
  and non-String non-Hash inputs.

- Assistant::Responder dedups the chat-history fetch. The previous
  layout fired two near-identical `chat.messages.where(...).includes(
  :tool_calls).ordered` queries per LLM turn (one for the OpenAI-shape
  payload, one for the raw-records kwarg). A new memoized
  `complete_chat_messages` fetches once; `chat_message_records` filters
  out the current message via `Array#reject`, `openai_messages_payload`
  iterates the cached array unchanged. One SQL query per turn instead
  of two. Memoization scope = single Responder instance (per LLM call),
  so cache invalidation is not a concern.

All 4370 tests pass (1 pre-existing libvips env error unrelated).
Rubocop + brakeman clean.
2026-05-27 10:42:12 +02:00

184 lines
5.4 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: openai_messages_payload,
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
# Memoized fetch — both `chat_message_records` and `openai_messages_payload`
# derive their shape from this one in-memory array so a single chat turn
# fires one history query instead of two.
def complete_chat_messages
return @complete_chat_messages if defined?(@complete_chat_messages)
@complete_chat_messages =
if chat&.messages
chat.messages
.where(type: [ "UserMessage", "AssistantMessage" ], status: "complete")
.includes(:tool_calls)
.ordered
.to_a
else
[]
end
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 payload below.
def chat_message_records
complete_chat_messages.reject { |m| m.id == message.id }
end
# Builds the OpenAI-shaped messages payload (role: "user" | "assistant" |
# "tool"; tool_call_id pairing) consumed by Provider::Openai's generic
# chat path. Anthropic uses chat_message_records instead.
def openai_messages_payload
messages = []
complete_chat_messages.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