Files
sure/app/models/chat.rb
Guillem Arias 714cf0bbb4 fix(ai): address PR review on Anthropic provider foundation
Surface fixes raised by Codex + CodeRabbit on PR 1/5:

- Provider::Anthropic#chat_response now accepts (and ignores) a
  `messages:` kwarg. Assistant::Responder passes both `messages:`
  (OpenAI-shape) and `conversation_history:` (raw Message records) for
  cross-provider parity, so the previous signature raised
  ArgumentError on the first chat turn through the Anthropic provider.
- Provider::Anthropic#supports_model? bypasses the `claude` prefix
  gate when a custom base_url is configured, mirroring the OpenAI
  provider. Bedrock-shaped IDs like
  `anthropic.claude-sonnet-4-5-20250929-v1:0` and
  `claude-opus-4@20250514` are otherwise rejected by
  Assistant::Provided#get_model_provider and the chat dies.
- Setting.anthropic_access_token is now in
  EncryptedSettingFields::ENCRYPTED_FIELDS so the Anthropic API key
  is encrypted at rest like every other provider secret. Previously
  plaintext while siblings (openai_access_token, twelve_data_api_key,
  external_assistant_token) were ciphertext.
- Chat.default_model falls back to whichever provider is actually
  configured. Previously, with LLM_PROVIDER=anthropic but no
  Anthropic credentials, the default model resolved to a Claude ID
  that no registered provider supported, so chats failed even when
  OpenAI was fully configured. Adds Provider::{Anthropic,Openai}#configured?
  class methods for the readable callsite.
- Provider::Anthropic.effective_model uses
  `ENV["ANTHROPIC_MODEL"].presence || Setting.anthropic_model` so the
  Setting lookup is only performed when the env var is absent — the
  previous `ENV.fetch(KEY, default)` evaluated the default arg
  eagerly on every call.
- Provider::Anthropic::ChatConfig#anthropic_input_schema strips both
  `:strict` and `"strict"` keys so JSON-decoded schemas with string
  keys cannot leak the OpenAI-only flag through to Anthropic.

Test coverage added: supports_model? bypass on custom endpoints,
chat_response messages: kwarg compatibility, default_model fallback
in the three credential combinations, configured? against ENV +
Setting, strict-flag stripping for both key types, and a
`Setting.expects(:anthropic_model).never` assertion proving the
ENV-precedence test now exercises the lazy path.

All 4365 tests pass (1 pre-existing libvips env error unrelated).
2026-05-25 19:49:25 +02:00

189 lines
5.2 KiB
Ruby

class Chat < ApplicationRecord
include Debuggable
RATE_LIMIT_PATTERNS = [
/\b429\b/i,
/rate limit/i,
/too many requests/i,
/quota exceeded/i
].freeze
TEMPORARY_PROVIDER_PATTERNS = [
/\b5\d\d\b/i,
/service unavailable/i,
/temporarily unavailable/i,
/gateway timeout/i,
/bad gateway/i,
/overloaded/i,
/time(?:out|d?\s*out)/i,
/connection reset/i
].freeze
AUTH_CONFIGURATION_PATTERNS = [
/unauthorized/i,
/authentication/i,
/invalid api key/i,
/incorrect api key/i,
/access token/i
].freeze
belongs_to :user
has_one :viewer, class_name: "User", foreign_key: :last_viewed_chat_id, dependent: :nullify # "Last chat user has viewed"
has_many :messages, dependent: :destroy
validates :title, presence: true
scope :ordered, -> { order(created_at: :desc) }
class << self
def start!(prompt, model:)
# Ensure we have a valid model by using the default if none provided
effective_model = model.presence || default_model
create!(
title: generate_title(prompt),
messages: [ UserMessage.new(content: prompt, ai_model: effective_model) ]
)
end
def generate_title(prompt)
prompt.first(80)
end
# Returns the default AI model to use for chats.
# Resolved from the configured llm_provider so installs that swap providers
# don't have to manually update every chat default. Falls through to a
# provider that actually has credentials configured, otherwise the chosen
# provider's classes would later raise "no LLM provider supports model …"
# even when the other provider is configured.
def default_model
prefers_anthropic = Setting.llm_provider == "anthropic"
if prefers_anthropic && Provider::Anthropic.configured?
Provider::Anthropic.effective_model.presence || Setting.anthropic_model
elsif Provider::Openai.configured?
Provider::Openai.effective_model.presence || Setting.openai_model
elsif Provider::Anthropic.configured?
Provider::Anthropic.effective_model.presence || Setting.anthropic_model
else
Provider::Openai.effective_model.presence || Setting.openai_model
end
end
end
def needs_assistant_response?
conversation_messages.ordered.last.role != "assistant"
end
def retry_last_message!
update!(error: nil)
last_message = conversation_messages.ordered.last
if last_message.present? && last_message.role == "user"
ask_assistant_later(last_message)
end
end
def update_latest_response!(provider_response_id)
update!(latest_assistant_response_id: provider_response_id)
end
def add_error(e)
update!(error: build_error_payload(e).to_json)
broadcast_append target: messages_target, partial: "chats/error", locals: { chat: self }
end
def presentable_error_message
return nil if error.blank?
parsed_error_payload["message"].presence || classify_error_message(error)
end
def technical_error_message
parsed_error_payload["technical_message"].presence || parsed_legacy_error_message || error
end
def clear_error
update! error: nil
broadcast_remove target: error_target
end
def conversation_messages
messages.where(type: [ "UserMessage", "AssistantMessage" ])
end
def messages_target
ActionView::RecordIdentifier.dom_id(self, :messages)
end
def error_target
ActionView::RecordIdentifier.dom_id(self, :chat_error)
end
def ask_assistant_later(message)
clear_error
pending = messages.create!(type: "AssistantMessage", content: "", ai_model: message.ai_model, status: :pending)
AssistantResponseJob.perform_later(message, pending)
end
def ask_assistant(message, assistant_message: nil)
assistant.respond_to(message, assistant_message: assistant_message)
end
private
def build_error_payload(error)
technical_message = error_message_for(error)
{
message: classify_error_message(technical_message),
technical_message: technical_message,
type: error.class.name
}
end
def classify_error_message(message)
normalized_message = message.to_s.strip
return I18n.t("chat.errors.default") if normalized_message.blank?
if RATE_LIMIT_PATTERNS.any? { |pattern| normalized_message.match?(pattern) }
I18n.t("chat.errors.rate_limited")
elsif TEMPORARY_PROVIDER_PATTERNS.any? { |pattern| normalized_message.match?(pattern) }
I18n.t("chat.errors.temporarily_unavailable")
elsif AUTH_CONFIGURATION_PATTERNS.any? { |pattern| normalized_message.match?(pattern) }
I18n.t("chat.errors.misconfigured")
else
I18n.t("chat.errors.default")
end
end
def parsed_error_payload
return {} if error.blank?
return error if error.is_a?(Hash)
parsed = JSON.parse(error)
parsed.is_a?(Hash) ? parsed : {}
rescue JSON::ParserError, TypeError
{}
end
def error_message_for(error)
error.respond_to?(:message) ? error.message.to_s : error.to_s
rescue StandardError
""
end
def parsed_legacy_error_message
parsed = JSON.parse(error)
parsed.is_a?(String) ? parsed : nil
rescue JSON::ParserError, TypeError
nil
end
def assistant
@assistant ||= Assistant.for_chat(self)
end
end