mirror of
https://github.com/we-promise/sure.git
synced 2026-12-03 10:49:07 +08:00
Check the vector store, the probe's API choice and its cache key
From the review on #4774: - A document upload says no store is configured when the embed route is set but the store can't be built (pgvector without its extension), instead of a NoMethodError, as main did. - A health probe keeps the configured endpoint's Responses API choice unless the caller picks one, so a chat probe checks the API chat uses. - The probe cache key includes the configured endpoint's static headers, so a changed gateway credential header isn't answered from the cache. - The hosting docs say chat and the buttons that open it keep their own availability check; the setups file drops PR history. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016zAWUV2Sxn9CgsmcUqojAZ
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
1e137e6def
commit
8c0a651f31
@@ -256,7 +256,12 @@ class ImportsController < ApplicationController
|
||||
redirect_to new_import_path, alert: t("imports.create.document_provider_not_configured")
|
||||
return
|
||||
end
|
||||
# The route can be configured while the store can't be built (pgvector without its extension).
|
||||
adapter = VectorStore.adapter
|
||||
unless adapter
|
||||
redirect_to new_import_path, alert: t("imports.create.document_provider_not_configured")
|
||||
return
|
||||
end
|
||||
|
||||
if file.size > Import::MAX_PDF_SIZE
|
||||
redirect_to new_import_path, alert: t("imports.create.document_too_large", max_size: Import::MAX_PDF_SIZE / 1.megabyte)
|
||||
|
||||
@@ -262,9 +262,11 @@ class AiHealth
|
||||
# The endpoint the feature's own calls use, from Ai::Config, with the URL and key AiHealth
|
||||
# resolved and the probe's timeout. Only a probe of a server Ai::Config doesn't know (an
|
||||
# admin's ad-hoc check, or a test) builds one from those values alone.
|
||||
def config_endpoint(provider:, base_url:, api_key:, responses_api: false, extra_headers: nil)
|
||||
# The endpoint's own API choice stands unless the caller picks one (the tool-call probe).
|
||||
def config_endpoint(provider:, base_url:, api_key:, responses_api: nil, extra_headers: nil)
|
||||
configured = configured_endpoint(provider.to_sym)
|
||||
overrides = { base_url:, api_key:, timeout_seconds: timeout, max_retries: 0, responses_api: }
|
||||
overrides = { base_url:, api_key:, timeout_seconds: timeout, max_retries: 0 }
|
||||
overrides[:responses_api] = responses_api unless responses_api.nil?
|
||||
overrides[:extra_headers] = extra_headers if extra_headers
|
||||
return configured.with(**overrides) if configured
|
||||
|
||||
@@ -351,9 +353,12 @@ class AiHealth
|
||||
@synthetic_pdf = pdf.freeze
|
||||
end
|
||||
|
||||
# The configured endpoint's static headers count too, so a changed gateway credential
|
||||
# header isn't answered from the cache.
|
||||
def cache_key(component:, provider_key:, endpoint:, model:, credential:, dimensions:, verification:)
|
||||
headers = configured_endpoint(provider_key.to_s.to_sym)&.extra_headers.to_h.sort.to_json
|
||||
fingerprint = Digest::SHA256.hexdigest(
|
||||
[ component, provider_key, endpoint, model, credential, dimensions, verification ].join("\0")
|
||||
[ component, provider_key, endpoint, model, credential, dimensions, verification, headers ].join("\0")
|
||||
)
|
||||
"#{CACHE_NAMESPACE}/#{fingerprint}"
|
||||
end
|
||||
|
||||
+9
-4
@@ -475,10 +475,15 @@ environment: the web container decides which AI buttons and actions are offered,
|
||||
runs the AI jobs. When their AI variables differ, a feature can be offered that the worker can't
|
||||
run, or hidden although it would work. Settings saved in the UI are shared by both.
|
||||
|
||||
**Which AI features are available** is decided in one place for every feature, from the same
|
||||
settings: a button, rule action or MCP tool appears only when a configured model can do the work.
|
||||
For example, **Enhance merchants** appears on an install with only an Anthropic key, as merchant
|
||||
enhancement runs on Anthropic too.
|
||||
**Which AI features are available** is decided in one place for every feature except chat, from
|
||||
the same settings: a button, rule action or MCP tool appears only when a configured model can do
|
||||
the work. For example, **Enhance merchants** appears on an install with only an Anthropic key, as
|
||||
merchant enhancement runs on Anthropic too.
|
||||
|
||||
Chat, and the buttons that open it (the Bills AI review and prompt chips, **Review with AI** and
|
||||
**Dig in with AI**), keep their own check: the user's AI switch and a configured provider. They can
|
||||
appear when a custom endpoint has no model, or when chat doesn't accept the configured model, and
|
||||
then every chat turn fails with an error.
|
||||
|
||||
## External AI Assistant
|
||||
|
||||
|
||||
@@ -464,6 +464,18 @@ class ImportsControllerTest < ActionDispatch::IntegrationTest
|
||||
assert_equal I18n.t("accounts.not_authorized"), flash[:alert]
|
||||
end
|
||||
|
||||
test "a document upload says no store is configured when the embed route is set but the store can't be built" do
|
||||
VectorStore::Registry.stubs(:adapter).returns(nil)
|
||||
use_ai_routes
|
||||
|
||||
assert_no_difference "Import.count" do
|
||||
post imports_url, params: { import: { type: "DocumentImport", import_file: file_fixture_upload("imports/valid.csv", "text/csv") } }
|
||||
end
|
||||
|
||||
assert_redirected_to new_import_url
|
||||
assert_equal I18n.t("imports.create.document_provider_not_configured"), flash[:alert]
|
||||
end
|
||||
|
||||
test "rejects unsupported document type for DocumentImport option" do
|
||||
adapter = mock("vector_store_adapter")
|
||||
adapter.stubs(:supported_extensions).returns(%w[.pdf .txt])
|
||||
|
||||
@@ -400,6 +400,24 @@ class AiHealth::ProbeTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "a probe keeps the configured endpoint's API choice unless the caller picks one" do
|
||||
@probe.stubs(:configured_endpoint).returns(ai_endpoint(name: "openai", responses_api: true, probe_check: :chat))
|
||||
build = ->(**choice) { @probe.send(:config_endpoint, provider: :openai, base_url: "https://llm.example.test/v1", api_key: "t", **choice) }
|
||||
|
||||
assert build.().responses_api, "the endpoint's Responses API stands"
|
||||
assert_not build.(responses_api: false).responses_api, "the tool-call probe picks Chat Completions"
|
||||
end
|
||||
|
||||
test "a changed static header isn't answered from the cache" do
|
||||
key = lambda do |headers|
|
||||
@probe.stubs(:configured_endpoint).returns(ai_endpoint(name: "openai", extra_headers: headers))
|
||||
@probe.send(:cache_key, component: "llm", provider_key: :openai, endpoint: "https://llm.example.test/v1", model: "m",
|
||||
credential: "t", dimensions: nil, verification: :models)
|
||||
end
|
||||
|
||||
assert_not_equal key.({ "X-Gateway-Key" => "old" }), key.({ "X-Gateway-Key" => "new" })
|
||||
end
|
||||
|
||||
test "forced probe bypasses the cached result" do
|
||||
models = mock("models")
|
||||
models.expects(:list).twice.returns({ "data" => [ { "id" => "gpt-4.1" } ] })
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# The AI setups today's code supports, for the tests that compare Ai::LegacyEnvConfig and the
|
||||
# router with today's resolution (test/architecture/ai_config_test, ai_agreement_test). C1-C9 are
|
||||
# PR #4440's E2 experiment; M1-M5 mix them.
|
||||
# The supported AI setups, for the tests that compare Ai::LegacyEnvConfig and the router with the
|
||||
# resolution of the code not yet on the router (test/architecture/ai_config_test,
|
||||
# ai_agreement_test). C1-C9 are one setup each; M1-M5 mix them.
|
||||
module AiLegacySetups
|
||||
# Cleared in every setup, so the host's environment can't leak in.
|
||||
CONTROLLED = %w[
|
||||
|
||||
Reference in New Issue
Block a user