refactor: extract citation pipeline from api deps into src/citations#39
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Provenance/citation selection is a signature explainability feature of chat answers, yet it lived as ~350 lines of underscore-private functions inside
src/api/deps.py— a dependency-injection/shared-helpers module. Worse,src/evals/regression_gate.pycross-imported the underscore-private_select_chat_citations, coupling the eval gate to a private symbol of a DI module. This PR gives the pipeline its own module with a public API.What moved
New module
src/citations.py(pure code motion — function bodies unchanged, leading underscores dropped):build_rag_citations,filter_relevant_rag_chunksbuild_web_citations,build_workspace_fallback_citationsbuild_wikipedia_citations,build_open_library_citations,build_arxiv_tool_citationsmerge_citations,iter_nested_dicts,normalize_tool_resulthas_tool_or_web_citations,select_chat_citations,build_tool_citations_TOOL_SOURCE_TYPESconstantAll call sites updated to the public names (no backwards-compat aliases needed):
src/evals/regression_gate.py,src/api/routers/sessions.py,src/api/routers/rag_chat.py,src/api/routers/rag_agents.py,tests/test_citations.py.What stayed in deps.py
Request models, billing/quota helpers, RAG validation error mapping, and the agent tool-calling loop (
_run_agent_loop,AgentLoopResult,_invoke_tool_raw_result,_generate_suggestions, stream/trace helpers). The agent loop now importsbuild_tool_citations/merge_citations/normalize_tool_resultfromsrc.citations.Follow-ups
_run_agent_loopand its helpers tosrc/api/agent_loop.py. Not done here because ~40 test sites patchsrc.api.routers.*._run_agent_loop; moving it cleanly means renaming the import in four routers plus all patch targets — a mechanical but noisy change better done alone.src/db/supabase_store.pyandsrc/rag.py— both are oversized and are candidates for the same treatment.Verification
uv run ruff check src tests— All checks passeduv run mypy src— Success: no issues found in 70 source filesuv run python -m pytest tests/ -q— 575 passeduv run python -m src.evals.regression_gate— 20/20 (100%)Zero behavior change intended; diff is 381 insertions / 378 deletions, all code motion + renames.