mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 23:04:49 +00:00
* Add SearchFamilyImportedFiles assistant function with vector store support Implement per-Family document search using OpenAI vector stores, allowing the AI assistant to search through uploaded financial documents (tax returns, statements, contracts, etc.). The architecture is modular with a provider- agnostic VectorStoreConcept interface so other RAG backends can be added. Key components: - Assistant::Function::SearchFamilyImportedFiles - tool callable from any LLM - Provider::VectorStoreConcept - abstract vector store interface - Provider::Openai vector store methods (create, upload, search, delete) - Family::VectorSearchable concern with document management - FamilyDocument model for tracking uploaded files - Migration adding vector_store_id to families and family_documents table https://claude.ai/code/session_01TSkKc7a9Yu2ugm1RvSf4dh * Extract VectorStore adapter layer for swappable backends Replace the Provider::VectorStoreConcept mixin with a standalone adapter architecture under VectorStore::. This cleanly separates vector store concerns from the LLM provider and makes it trivial to swap backends. Components: - VectorStore::Base — abstract interface (create/delete/upload/remove/search) - VectorStore::Openai — uses ruby-openai gem's native vector_stores.search - VectorStore::Pgvector — skeleton for local pgvector + embedding model - VectorStore::Qdrant — skeleton for Qdrant vector DB - VectorStore::Registry — resolves adapter from VECTOR_STORE_PROVIDER env - VectorStore::Response — success/failure wrapper (like Provider::Response) Consumers updated to go through VectorStore.adapter: - Family::VectorSearchable - Assistant::Function::SearchFamilyImportedFiles - FamilyDocument Removed: Provider::VectorStoreConcept, vector store methods from Provider::Openai https://claude.ai/code/session_01TSkKc7a9Yu2ugm1RvSf4dh * Add Vector Store configuration docs to ai.md Documents how to configure the document search feature, covering all three supported backends (OpenAI, pgvector, Qdrant), environment variables, Docker Compose examples, supported file types, and privacy considerations. https://claude.ai/code/session_01TSkKc7a9Yu2ugm1RvSf4dh * No need to specify `imported` in code * Missed a couple more places * Tiny reordering for the human OCD * Update app/models/assistant/function/search_family_files.rb Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Juan José Mata <jjmata@jjmata.com> * PR comments * More PR comments --------- Signed-off-by: Juan José Mata <jjmata@jjmata.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
130 lines
3.9 KiB
Ruby
130 lines
3.9 KiB
Ruby
require "test_helper"
|
|
|
|
class Assistant::Function::SearchFamilyFilesTest < ActiveSupport::TestCase
|
|
setup do
|
|
@user = users(:family_admin)
|
|
@function = Assistant::Function::SearchFamilyFiles.new(@user)
|
|
end
|
|
|
|
test "has correct name" do
|
|
assert_equal "search_family_files", @function.name
|
|
end
|
|
|
|
test "has a description" do
|
|
assert_not_empty @function.description
|
|
end
|
|
|
|
test "is not in strict mode" do
|
|
assert_not @function.strict_mode?
|
|
end
|
|
|
|
test "params_schema requires query" do
|
|
schema = @function.params_schema
|
|
assert_includes schema[:required], "query"
|
|
assert schema[:properties].key?(:query)
|
|
end
|
|
|
|
test "generates valid tool definition" do
|
|
definition = @function.to_definition
|
|
assert_equal "search_family_files", definition[:name]
|
|
assert_not_nil definition[:description]
|
|
assert_not_nil definition[:params_schema]
|
|
assert_equal false, definition[:strict]
|
|
end
|
|
|
|
test "returns no_documents error when family has no vector store" do
|
|
@user.family.update!(vector_store_id: nil)
|
|
|
|
result = @function.call("query" => "tax return")
|
|
|
|
assert_equal false, result[:success]
|
|
assert_equal "no_documents", result[:error]
|
|
end
|
|
|
|
test "returns provider_not_configured when no adapter is available" do
|
|
@user.family.update!(vector_store_id: "vs_test123")
|
|
VectorStore::Registry.stubs(:adapter).returns(nil)
|
|
|
|
result = @function.call("query" => "tax return")
|
|
|
|
assert_equal false, result[:success]
|
|
assert_equal "provider_not_configured", result[:error]
|
|
end
|
|
|
|
test "returns search results on success" do
|
|
@user.family.update!(vector_store_id: "vs_test123")
|
|
|
|
mock_adapter = mock("vector_store_adapter")
|
|
mock_adapter.stubs(:search).returns(
|
|
VectorStore::Response.new(
|
|
success?: true,
|
|
data: [
|
|
{ content: "Total income: $85,000", filename: "2024_tax_return.pdf", score: 0.95, file_id: "file-abc" },
|
|
{ content: "W-2 wages: $80,000", filename: "2024_tax_return.pdf", score: 0.87, file_id: "file-abc" }
|
|
],
|
|
error: nil
|
|
)
|
|
)
|
|
|
|
VectorStore::Registry.stubs(:adapter).returns(mock_adapter)
|
|
|
|
result = @function.call("query" => "What was my total income?")
|
|
|
|
assert_equal true, result[:success]
|
|
assert_equal 2, result[:result_count]
|
|
assert_equal "Total income: $85,000", result[:results].first[:content]
|
|
assert_equal "2024_tax_return.pdf", result[:results].first[:filename]
|
|
end
|
|
|
|
test "returns empty results message when no matches found" do
|
|
@user.family.update!(vector_store_id: "vs_test123")
|
|
|
|
mock_adapter = mock("vector_store_adapter")
|
|
mock_adapter.stubs(:search).returns(
|
|
VectorStore::Response.new(success?: true, data: [], error: nil)
|
|
)
|
|
|
|
VectorStore::Registry.stubs(:adapter).returns(mock_adapter)
|
|
|
|
result = @function.call("query" => "nonexistent document")
|
|
|
|
assert_equal true, result[:success]
|
|
assert_empty result[:results]
|
|
end
|
|
|
|
test "handles search failure gracefully" do
|
|
@user.family.update!(vector_store_id: "vs_test123")
|
|
|
|
mock_adapter = mock("vector_store_adapter")
|
|
mock_adapter.stubs(:search).returns(
|
|
VectorStore::Response.new(
|
|
success?: false,
|
|
data: nil,
|
|
error: VectorStore::Error.new("API rate limit exceeded")
|
|
)
|
|
)
|
|
|
|
VectorStore::Registry.stubs(:adapter).returns(mock_adapter)
|
|
|
|
result = @function.call("query" => "tax return")
|
|
|
|
assert_equal false, result[:success]
|
|
assert_equal "search_failed", result[:error]
|
|
end
|
|
|
|
test "caps max_results at 20" do
|
|
@user.family.update!(vector_store_id: "vs_test123")
|
|
|
|
mock_adapter = mock("vector_store_adapter")
|
|
mock_adapter.expects(:search).with(
|
|
store_id: "vs_test123",
|
|
query: "test",
|
|
max_results: 20
|
|
).returns(VectorStore::Response.new(success?: true, data: [], error: nil))
|
|
|
|
VectorStore::Registry.stubs(:adapter).returns(mock_adapter)
|
|
|
|
@function.call("query" => "test", "max_results" => 50)
|
|
end
|
|
end
|