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).
This commit is contained in:
Guillem Arias
2026-05-25 19:49:25 +02:00
parent c1dbb51553
commit 714cf0bbb4
8 changed files with 144 additions and 6 deletions

View File

@@ -62,6 +62,29 @@ class ChatTest < ActiveSupport::TestCase
end
end
test "default_model returns claude when LLM_PROVIDER=anthropic and Anthropic is configured" do
Provider::Anthropic.stubs(:configured?).returns(true)
Setting.stubs(:llm_provider).returns("anthropic")
assert_equal Provider::Anthropic::DEFAULT_MODEL, Chat.default_model
end
test "default_model falls back to OpenAI when Anthropic is preferred but unconfigured" do
Provider::Anthropic.stubs(:configured?).returns(false)
Provider::Openai.stubs(:configured?).returns(true)
Setting.stubs(:llm_provider).returns("anthropic")
assert_equal Provider::Openai::DEFAULT_MODEL, Chat.default_model
end
test "default_model uses Anthropic when OpenAI is unconfigured" do
Provider::Anthropic.stubs(:configured?).returns(true)
Provider::Openai.stubs(:configured?).returns(false)
Setting.stubs(:llm_provider).returns("openai")
assert_equal Provider::Anthropic::DEFAULT_MODEL, Chat.default_model
end
test "creates with configured model when OPENAI_MODEL env is set" do
prompt = "Test prompt"

View File

@@ -65,4 +65,30 @@ class Provider::Anthropic::ChatConfigTest < ActiveSupport::TestCase
# Anthropic schemas must not carry the OpenAI-specific `strict` flag.
req[:tools].each { |t| assert_not t[:input_schema].key?(:strict) }
end
test "strips both symbol and string-keyed `strict` flags from input_schema" do
config = Provider::Anthropic::ChatConfig.new(
prompt: "hi",
functions: [
{
name: "fn_with_string_strict",
description: "schema arrived from JSON.parse with string keys",
params_schema: {
"type" => "object",
"properties" => {},
"required" => [],
"additionalProperties" => false,
"strict" => true
},
strict: true
}
]
)
req = config.build_request(model: "claude-sonnet-4-6")
schema = req[:tools].first[:input_schema]
assert_not schema.key?(:strict)
assert_not schema.key?("strict")
end
end

View File

@@ -31,6 +31,20 @@ class Provider::AnthropicTest < ActiveSupport::TestCase
assert_not @subject.supports_model?("gpt-4.1")
end
test "supports_model? bypasses the prefix gate for custom endpoints" do
custom = Provider::Anthropic.new(
"test-token",
base_url: "https://bedrock.example.com/anthropic",
model: "anthropic.claude-sonnet-4-5-20250929-v1:0"
)
# Bedrock-shaped IDs start with "anthropic", not "claude" — would fail the
# default prefix check, but custom endpoints must accept any model.
assert custom.supports_model?("anthropic.claude-sonnet-4-5-20250929-v1:0")
assert custom.supports_model?("claude-opus-4@20250514")
assert custom.supports_model?("any-string-the-endpoint-accepts")
end
test "supported_models_description returns prefixes for standard provider" do
assert_equal "models starting with: claude", @subject.supported_models_description
end
@@ -40,12 +54,28 @@ class Provider::AnthropicTest < ActiveSupport::TestCase
assert_not @subject.supports_pdf_processing?(model: "gpt-4o")
end
test "effective_model defers to ENV when set" do
test "effective_model defers to ENV when set without consulting Setting" do
ClimateControl.modify("ANTHROPIC_MODEL" => "claude-haiku-4-5") do
Setting.expects(:anthropic_model).never
assert_equal "claude-haiku-4-5", Provider::Anthropic.effective_model
end
end
test "configured? reflects ENV and Setting presence" do
ClimateControl.modify("ANTHROPIC_ACCESS_TOKEN" => nil, "ANTHROPIC_API_KEY" => nil) do
Setting.stubs(:anthropic_access_token).returns(nil)
assert_not Provider::Anthropic.configured?
Setting.stubs(:anthropic_access_token).returns("sk-ant-x")
assert Provider::Anthropic.configured?
end
ClimateControl.modify("ANTHROPIC_API_KEY" => "sk-ant-y") do
Setting.stubs(:anthropic_access_token).returns(nil)
assert Provider::Anthropic.configured?
end
end
test "effective_model falls back to default when nothing set" do
ClimateControl.modify("ANTHROPIC_MODEL" => nil) do
Setting.stubs(:anthropic_model).returns(nil)
@@ -67,6 +97,31 @@ class Provider::AnthropicTest < ActiveSupport::TestCase
assert_match(/rate limit/i, response.error.message)
end
test "chat_response accepts messages: kwarg passed by Responder without raising" do
# The OpenAI-shaped `messages:` array is passed alongside `conversation_history:`
# for cross-provider parity. Anthropic ignores it but must still accept it as
# a keyword argument — historical regression that broke the first chat turn.
fake_client = stub_anthropic_client_with(
build_anthropic_message(
id: "msg_kw",
model: @subject_model,
text_blocks: [ "ok" ],
tool_use_blocks: [],
usage: { input_tokens: 1, output_tokens: 1 }
)
)
@subject.instance_variable_set(:@client, fake_client)
response = @subject.chat_response(
"hi",
model: @subject_model,
messages: [ { role: "user", content: "hi" } ],
conversation_history: []
)
assert response.success?
end
test "chat_response returns parsed ChatResponse on success" do
fake_client = stub_anthropic_client_with(
build_anthropic_message(