refactor(completion): extract provider dispatch into typed helpers so basedpyright can analyze it

completion packed a ~2,900-line per-provider if/elif dispatch into a single
body, pushing it past basedpyright's code-flow complexity ceiling. basedpyright
emitted "Code is too complex to analyze" and skipped the whole function, so
every type error on the hottest request path was invisible and unguarded.

This extracts each provider branch into its own helper that receives a single
frozen _CompletionDispatchContext carrying the shared locals the dispatch reads
(model, messages, optional_params, litellm_params, logging, headers, api_key,
api_base, client, timeout, ...). Each helper destructures only what it uses and
keeps its body verbatim, so the dispatch logic is unchanged; completion now sits
well under the ceiling and basedpyright type-checks it and all 61 helpers.

The context is built once after setup, right before the dispatch, and each arm
becomes `response = _complete_<provider>(ctx)` feeding the existing single
`return response`, preserving the original "dispatch sets response, return once"
shape and the early-return/streaming semantics. The three deprecated no-op arms
(clarifai, together_ai, palm) stay inline.

Building the context surfaced a shadowing trap: acompletion, client, api_version,
organization and text_completion are completion parameters that shadow
module-level names, so a naive free-variable pass would have dropped them from
the context and silently changed behavior; they are threaded through explicitly.

Restoring analysis also surfaced get_secret()'s bool-inclusive return type
broadening api_base in the anthropic and anthropic_text branches (it flows into
.endswith()/+=); api_base is narrowed back to Optional[str] at those two sites.
Genuinely dead assignments the monolith hid at function scope (an unused prompt
build, two unused data dicts, and two no-op custom_llm_provider reassignments)
are removed.

No behavior change. The basedpyright per-rule budget is unchanged; the gate is
confirmed by the CI run (the dev-only/local basedpyright env inflates
reportMissingTypeStubs on any main.py edit, independent of this change, as the
budget's own git history documents).
This commit is contained in:
mateo-berri 2026-06-18 21:49:39 -07:00
parent 7e5699c7ab
commit 1c2041177e

File diff suppressed because it is too large Load diff