Skip to content

fix: AI config form test fails with CORS network error#161

Merged
AmintaCCCP merged 2 commits into
mainfrom
fix/ai-config-form-test-cors
May 26, 2026
Merged

fix: AI config form test fails with CORS network error#161
AmintaCCCP merged 2 commits into
mainfrom
fix/ai-config-form-test-cors

Conversation

@AmintaCCCP

@AmintaCCCP AmintaCCCP commented May 26, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes #156 — AI config "Test Connection" button fails with "网络连接失败" (Network connection failed) for openai-compatible endpoints.

Root cause: handleTestForm() creates a temp config with id: '', and requestText() only routes through the backend proxy when config.id is 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 }. Extracted normalizeReasoningEffort() helper, added input validation, added HTTPS warning.
  • src/services/backendAdapter.ts: Added proxyAIRequestWithConfig() 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

  • Create/edit an AI config with openai-compatible type
  • Click "测试连接" in the form — should succeed through backend proxy
  • Test saved config — should still work
  • Test with backend unavailable — should fall back to direct fetch

Summary by CodeRabbit

  • New Features

    • Support for using inline AI configuration as an alternative to stored configurations.
  • Improvements

    • Clearer validation and distinct error responses for missing or invalid AI config.
    • Normalized reasoning effort values for consistent behavior.
    • Request cancellation/timeout now honored for AI requests routed through the backend proxy.
    • Console warning emitted when a non-HTTPS base URL is supplied.

Review Change Stack

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>
@coderabbitai

coderabbitai Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 48cda2b2-ae65-41c8-86fe-8d3ace38cdad

📥 Commits

Reviewing files that changed from the base of the PR and between f129a3f and 4d42c9c.

📒 Files selected for processing (3)
  • server/src/routes/proxy.ts
  • src/services/aiService.ts
  • src/services/backendAdapter.ts

📝 Walkthrough

Walkthrough

Server 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.

Changes

Inline Config Proxy Support

Layer / File(s) Summary
BackendAdapter: timeout and inline proxy method
src/services/backendAdapter.ts
fetchWithTimeout forwards caller options.signal; adds proxyAIRequestWithConfig(aiConfig, body, signal?); proxyAIRequest now accepts optional signal and forwards it to /proxy/ai.
Server route for inline and DB configs
server/src/routes/proxy.ts
Adds normalizeReasoningEffort; /api/proxy/ai accepts { config, body } inline or { configId } DB-backed, validates inline fields (baseUrl, apiKey, model), warns on non-HTTPS baseUrl, decrypts stored API keys for DB path, normalizes reasoningEffort, and returns distinct 400/404 errors.
AIService: prefer backend proxy (stored or inline)
src/services/aiService.ts
OpenAI, Claude, and Gemini request paths use backend proxy when backend.isAvailable, choosing proxyAIRequest(config.id, body, signal) if config.id exists or proxyAIRequestWithConfig(config, body, signal) otherwise; options.signal is forwarded.

Sequence Diagram

sequenceDiagram
  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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰 I tunneled through code with a hop and a twitch,
Proxying configs both stored and which—
Inline or DB, the requests now align,
Signals and reasons normalized in a line.
Hop, hop — the AI responses return just in time!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main change: routing AI config form tests through the backend proxy to fix CORS failures.
Linked Issues check ✅ Passed The PR directly addresses issue #156 by enabling backend proxy routing for form tests, adding inline config support, and including HTTPS warnings.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the AI config form test CORS issue: proxy route enhancement, AbortSignal forwarding, and service layer routing logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ai-config-form-test-cors

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3adb092 and f129a3f.

📒 Files selected for processing (3)
  • server/src/routes/proxy.ts
  • src/services/aiService.ts
  • src/services/backendAdapter.ts

Comment thread server/src/routes/proxy.ts
Comment thread src/services/aiService.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>
@AmintaCCCP AmintaCCCP merged commit 93f52f8 into main May 26, 2026
5 checks passed
@AmintaCCCP AmintaCCCP deleted the fix/ai-config-form-test-cors branch May 26, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

确认API key和端点都没问题,AI测试失败

1 participant