fix: AI config form test fails with CORS network error#161
Conversation
The "Test Connection" button in the AI config form always failed with "Network connection failed" for openai-compatible endpoints because: 1. handleTestForm() creates a temp config with id: '' 2. requestText() only routes through backend proxy when config.id is truthy 3. Empty string is falsy, so form tests fell through to direct browser fetch 4. Browser CORS policy blocks cross-origin requests to the API endpoint Fix by accepting inline config (without stored ID) in the backend proxy route and routing form tests through it when the backend is available. Also extracted normalizeReasoningEffort() helper, added input validation for inline config, and added HTTPS warning for non-encrypted connections. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughServer proxy endpoint now accepts inline AI configs or a stored configId; BackendAdapter gained proxyAIRequestWithConfig and signal-forwarding; AIService routes OpenAI/Claude/Gemini calls through the backend proxy for both inline and stored configs, passing abort signals and normalizing reasoningEffort. ChangesInline Config Proxy Support
Sequence DiagramsequenceDiagram
participant AIService
participant BackendAdapter
participant ServerProxy
participant ExternalAI as AIProvider
AIService->>BackendAdapter: proxyAIRequest(configId, body, signal) / proxyAIRequestWithConfig(config, body, signal)
BackendAdapter->>ServerProxy: POST /proxy/ai { config? , body } (with auth, timeout)
ServerProxy->>AIProvider: proxied request to baseUrl with API key and normalized reasoningEffort
AIProvider-->>ServerProxy: response
ServerProxy-->>BackendAdapter: proxied response
BackendAdapter-->>AIService: parsed JSON response
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@server/src/routes/proxy.ts`:
- Around line 110-140: The non-HTTPS warning is only issued in the inlineConfig
branch; update the DB-backed path so it issues the same warning after resolving
baseUrl from aiConfig (i.e., after apiKey = decrypt(...), apiType = ..., baseUrl
= aiConfig.base_url, model = aiConfig.model, reasoningEffort =
normalizeReasoningEffort(...)). Add the same URL parse + protocol check (using
new URL(baseUrl)) and console.warn when protocol !== 'https:' inside a try/catch
so invalid URLs are still handled by the existing validation flow; ensure the
warning message matches the inline branch for consistency.
In `@src/services/aiService.ts`:
- Around line 156-161: The proxy AI request path ignores caller AbortSignals
causing connection tests to wait full proxy timeouts; update requestText to pass
the incoming options.signal into backend.proxyAIRequest and
proxyAIRequestWithConfig, and modify backendAdapter functions proxyAIRequest and
proxyAIRequestWithConfig to accept an optional signal?: AbortSignal and forward
it into fetchWithTimeout; change fetchWithTimeout to combine the caller signal
with its internal timeout (race the caller signal with the timeout instead of
replacing it) so testConnection's AbortController can cancel in-flight proxy
requests.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: de2ac862-cbfa-4797-aa68-f52708ba2498
📒 Files selected for processing (3)
server/src/routes/proxy.tssrc/services/aiService.tssrc/services/backendAdapter.ts
…rd AbortSignal - Add HTTPS protocol warning to DB-backed AI config path in proxy.ts, matching the existing warning in the inline config branch - Forward caller AbortSignal through backend proxy methods so testConnection's AbortController can cancel in-flight requests - Pass options.signal to all proxyAIRequest/proxyAIRequestWithConfig calls in aiService.ts (OpenAI, Claude, Gemini branches) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Summary
Fixes #156 — AI config "Test Connection" button fails with "网络连接失败" (Network connection failed) for
openai-compatibleendpoints.Root cause:
handleTestForm()creates a temp config withid: '', andrequestText()only routes through the backend proxy whenconfig.idis truthy. Empty string is falsy, so form tests always fell through to direct browser fetch, which fails due to CORS policy.Changes
server/src/routes/proxy.ts: Accept inline config{ config, body }in addition to{ configId, body }. ExtractednormalizeReasoningEffort()helper, added input validation, added HTTPS warning.src/services/backendAdapter.ts: AddedproxyAIRequestWithConfig()for one-time proxy requests without a stored config ID.src/services/aiService.ts: Route form tests through backend proxy when available, even without a saved config ID.Test plan
openai-compatibletypeSummary by CodeRabbit
New Features
Improvements