refactor(ai): extract connection-diagnostics and local-model-discovery - #761
Conversation
Continues the Strangler-pattern decomposition of services/aiProviderService.ts that PR #759 started for providers/lifecycle/contracts, this time for the discovery layer: testAIConnection and local-model discovery/scanning. Behavior-preserving extraction, verified against current main (not the stale #747 reference branch, which predates #759 and would have dropped several QNBS-v3/ADR comments added since): - services/ai/discovery/connectionTests.ts: testAIConnection (all seven provider branches unchanged) plus its TestConnectionResult/ TestConnectionErrorKind types. Multi-line historical QNBS-v3/ADR comments condensed to one physical line each per this repo's own comment policy — git sees every line of a newly-created file as added, so the diff-aware historical-debt exemption does not apply to a moved comment the way it would to an untouched one. - services/ai/discovery/localModelDiscovery.ts: listOllamaModels, listLocalBackendModels, testOpenAiCompatibleLocalConnection, scanLocalOpenAiCompatibleEndpoints, and the LocalServerDiagnostic/ LocalEndpointScanResult/LocalEndpointScanState types, plus the localServerFailure/localServerTransport helpers. - aiProviderService.ts re-exports both modules' public surface unchanged, so every existing import site (Settings AI provider cards, WebLLM panel, ProForge orchestration, etc.) keeps working without a source-level change. Explicitly out of scope for this slice (per current owner map): #713, #714, #729, #731, #732, provider/model routing policy, any new discovery semantics, and #743 qualification work. The orchestration/ layer (generateText, streamText, generateImage, generateJson, providerDispatch, fallbackState) is a separate, larger residual slice, not touched here. Test plan: full suite (206 tests) across tests/unit/aiProviderService.test.ts, tests/unit/aiProviderService.fallbackChain.test.ts, tests/unit/settings/WebLlmPanel.test.tsx, tests/unit/settings/AiProviderCard.test.tsx, tests/unit/ollamaService.test.ts — all pass unchanged. Single-checker typecheck clean. ci:prepush PASS.
🤖 CodeAnt AI — Review Status
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Thanks for using CodeAnt! 🎉We're free for open-source projects. if you're enjoying it, help us grow by sharing. Share on X · |
Reviewer's GuideThis PR continues the Strangler-pattern decomposition by moving connection diagnostics and local-model discovery from aiProviderService.ts into focused discovery modules, while preserving behavior and re-exporting the same public surface for existing callers. Sequence diagram for local OpenAI-compatible connection diagnosticssequenceDiagram
participant Caller as Settings caller
participant Test as testOpenAiCompatibleLocalConnection
participant HTTP as localServerFetch
participant Server as Local model server
Caller->>Test: testOpenAiCompatibleLocalConnection(baseUrl)
Test->>Test: normalizeOpenAiCompatibleBaseUrl(baseUrl)
Test->>HTTP: localServerFetch(/models)
HTTP->>Server: GET /models
Server-->>HTTP: Response
HTTP-->>Test: Response
alt HTTP error
Test-->>Caller: TestConnectionResult httpError
else Invalid or empty model list
Test-->>Caller: TestConnectionResult invalidResponse or noModels
else Models available
Test-->>Caller: TestConnectionResult with LocalServerDiagnostic
end
Sequence diagram for local endpoint scanningsequenceDiagram
participant Caller as Settings caller
participant Scan as scanLocalOpenAiCompatibleEndpoints
participant HTTP as localServerFetch
participant Ollama as Ollama endpoint
participant Local as LMStudio or vLLM endpoint
Caller->>Scan: scanLocalOpenAiCompatibleEndpoints()
par Scan Ollama
Scan->>HTTP: localServerFetch(/api/tags)
HTTP->>Ollama: GET /api/tags
Ollama-->>HTTP: Response or failure
opt Native Ollama request fails without timeout
Scan->>HTTP: localServerFetch(/v1/models)
HTTP->>Ollama: GET /v1/models
Ollama-->>HTTP: Response
end
and Scan LMStudio and vLLM
Scan->>HTTP: localServerFetch(/v1/models)
HTTP->>Local: GET /v1/models
Local-->>HTTP: Response or failure
end
Scan-->>Caller: LocalEndpointScanResult[]
Sequence diagram for provider connection testingsequenceDiagram
participant Caller as Settings caller
participant Test as testAIConnection
participant KeyStore as storageService
participant Provider as Provider endpoint
Caller->>Test: testAIConnection(provider, opts)
Test->>KeyStore: getApiKey(provider)
KeyStore-->>Test: API key or missing key
alt API key missing
Test-->>Caller: TestConnectionResult noApiKey
else Provider request
Test->>Provider: Connection request
Provider-->>Test: HTTP response
alt Request succeeds
Test-->>Caller: TestConnectionResult ok
else Request fails
Test-->>Caller: TestConnectionResult httpError or unexpected
end
end
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
🏁 CodeAnt Quality Gate ResultsCommit: ✅ Overall Status: PASSEDQuality Gate Details
|
There was a problem hiding this comment.
Code Health Improved
(1 files improve in Code Health)
Gates Failed
New code is healthy
(2 new files with code health below 10.00)
Our agent can fix these. Install it.
Gates Passed
2 Quality Gates Passed
Reason for failure
| New code is healthy | Violations | Code Health Impact | |
|---|---|---|---|
| connectionTests.ts | 2 rules | 8.76 | Suppress |
| localModelDiscovery.ts | 1 rule | 9.31 | Suppress |
View Improvements
| File | Code Health Impact | Categories Improved |
|---|---|---|
| aiProviderService.ts | 6.07 → 7.37 | Complex Method, Complex Conditional, Overall Code Complexity |
Quality Gate Profile: The Bare Minimum
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.
|
[check-pr-size] PR size is over the target tier (normal profile): 3 files, 849 meaningful lines, 1 commits — limit ≤8 files / ≤400 lines / ≤6 commits. Consider splitting into smaller, independently reviewable PRs. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Essentials Run ID: 📒 Files selected for processing (3)
Included review availability: 0 reviews are currently available. Your included PR review attempts over the past 7 days set your current allowance at 1 review per hour. 📝 WalkthroughWalkthroughAdded dedicated modules for AI provider connection testing and local-model discovery. The AI provider service now re-exports these APIs and removes their previous in-file implementations. ChangesAI discovery APIs
Priority: ⬇️ Low Estimated code review effort: 4 (Complex) | ~45 minutes Change: Refactor Sequence Diagram(s)sequenceDiagram
participant testAIConnection
participant RuntimeChecks
participant ProviderEndpoint
testAIConnection->>RuntimeChecks: validate provider key and runtime
testAIConnection->>ProviderEndpoint: send bounded connectivity request
ProviderEndpoint-->>testAIConnection: return response or classified failure
testAIConnection-->>RuntimeChecks: return TestConnectionResult
Merge Risk: ⚪ Minimal · up to No verified merge-blocking behavior remains in the extracted discovery modules or their re-exports. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
User description
Summary
Continues the Strangler-pattern decomposition of
services/aiProviderService.tsthat PR #759 started for providers/lifecycle/contracts, this time for the discovery layer:testAIConnectionand local-model discovery/scanning.Scope
Behavior-preserving extraction, reconstructed fresh against current
main(not the stale#747reference branch, which predates #759 and would have silently dropped several QNBS-v3/ADR comments added since):services/ai/discovery/connectionTests.ts:testAIConnection(all seven provider branches unchanged) plus itsTestConnectionResult/TestConnectionErrorKindtypes.services/ai/discovery/localModelDiscovery.ts:listOllamaModels,listLocalBackendModels,testOpenAiCompatibleLocalConnection,scanLocalOpenAiCompatibleEndpoints, and theLocalServerDiagnostic/LocalEndpointScanResult/LocalEndpointScanStatetypes, plus thelocalServerFailure/localServerTransporthelpers.aiProviderService.tsre-exports both modules' public surface unchanged, so every existing import site (Settings AI provider cards, WebLLM panel, ProForge orchestration, etc.) keeps working without a source-level change.Explicitly out of scope for this slice (per current owner map): #713, #714, #729, #731, #732, provider/model routing policy, any new discovery semantics, and #743 qualification work. The
orchestration/layer (generateText,streamText,generateImage,generateJson,providerDispatch,fallbackState) is a separate, larger residual slice, not touched here.Relation to PR #747
#747(refactor(ai): decompose ai provider service) remains frozen/reference-only per this repo's convention and is not merged or cherry-picked. This PR reconstructs the still-missing discovery-layer piece of that decomposition fresh from currentmain.Test plan
tests/unit/aiProviderService.test.ts,tests/unit/aiProviderService.fallbackChain.test.ts,tests/unit/settings/WebLlmPanel.test.tsx,tests/unit/settings/AiProviderCard.test.tsx,tests/unit/ollamaService.test.ts— all pass unchanged.tsgotypecheck clean.pnpm run ci:prepushPASS.Summary by Sourcery
Enhancements:
Summary by cubic
Splits connection diagnostics and local-model discovery out of
services/aiProviderService.tsinto two new modules. This is a behavior-preserving refactor, so existing functionality and call sites remain unchanged.testAIConnectionand its result/error types toservices/ai/discovery/connectionTests.ts.services/ai/discovery/localModelDiscovery.ts.aiProviderService.tsnow re-exports the moved functions and types, so no consumer changes are needed.services/aiProviderService.ts.Written for commit d22ad5f. Summary will update on new commits.
CodeAnt-AI Description
Reorganize AI connection checks and local model discovery without changing how users access them
What Changed
Impact
✅ Clearer local-server connection errors✅ Reliable Ollama detection across versions✅ Existing AI provider settings continue to work💡 Usage Guide
Checking Your Pull Request
Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.
Talking to CodeAnt AI
Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
Preserve Org Learnings with CodeAnt
You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
Check Your Repository Health
To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.
Summary by CodeRabbit