Skip to content

fix: use inline config for AI proxy to avoid configId lookup failure (#166)#167

Merged
AmintaCCCP merged 3 commits into
mainfrom
fix/166-ai-config-not-found
May 28, 2026
Merged

fix: use inline config for AI proxy to avoid configId lookup failure (#166)#167
AmintaCCCP merged 3 commits into
mainfrom
fix/166-ai-config-not-found

Conversation

@AmintaCCCP
Copy link
Copy Markdown
Owner

@AmintaCCCP AmintaCCCP commented May 27, 2026

Summary

  • Fix "AI config not found" error when testing AI connection from the service list page or using AI to analyze repos in backend mode
  • Root cause: AIService.requestText() used configId lookup path when config.id was set, but the config might not yet be synced to the backend DB (auto-sync has a 2s debounce)
  • Fix: always use proxyAIRequestWithConfig (inline config path) to send the full config data directly to the backend, matching what the form test connection already does

Problem

In backend mode, there were two code paths for AI proxy requests:

Path Trigger How it works
proxyAIRequest(configId) Saved config (has id) Backend looks up config by ID in SQLite DB
proxyAIRequestWithConfig(config) Form test (no id) Backend uses config data directly from request

The configId path failed when auto-sync hadn't pushed the config yet (2s debounce window after saving), causing:

  • List page "Test Connection" → "AI config not found" 404
  • AI repo analysis → "AI config not found" 404

Test plan

  • Backend mode: create/edit AI config → test from list page → should succeed
  • Backend mode: use AI to analyze a repo → should succeed
  • Backend mode: test connection from edit form → should continue working
  • Desktop client (no backend): all AI features → should continue working
  • Pure web frontend (no backend): all AI features → should continue working

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Consolidated AI request routing to prefer a proxy lookup with an automatic fallback to an inline-config proxy to improve reliability of AI integrations (OpenAI-family, Claude, Gemini).
    • Enhanced backend error handling for more informative failures.
    • No visible user-facing changes.

Review Change Stack

When backend mode is enabled, AI service requests (test connection, repo
analysis, etc.) used configId to look up the config in the backend DB.
This failed when the auto-sync hadn't pushed the config yet (2s debounce
window), causing "AI config not found" 404 errors.

Now always sends the full config inline via proxyAIRequestWithConfig,
matching what the form test connection already does successfully.

Fixes #166

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b914000f-eefb-4663-bd12-f7d5187f795a

📥 Commits

Reviewing files that changed from the base of the PR and between 0d27649 and 41782d9.

📒 Files selected for processing (1)
  • src/services/backendAdapter.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/services/backendAdapter.ts

📝 Walkthrough

Walkthrough

Adds BackendAdapter.proxyAIRequestWithFallback which first tries a config-id based /proxy/ai call and falls back to an inline-config /proxy/ai call when the backend reports the AI config is missing; AIService.requestText now routes proxied OpenAI-family, Claude, and Gemini requests through this fallback helper.

Changes

Proxy Request Fallback

Layer / File(s) Summary
BackendAdapter fallback helper
src/services/backendAdapter.ts
Adds proxyAIRequestWithFallback(configId, aiConfig, body, signal) that attempts a config-id /proxy/ai call and falls back to proxyAIRequestWithConfig on 404 or AI_CONFIG_NOT_FOUND; throwTranslatedError now throws an Error including statusCode and parsed code.
AIService proxied calls use fallback
src/services/aiService.ts
OpenAI-family, Claude, and Gemini proxied request paths now call backend.proxyAIRequestWithFallback(this.config.id, this.config, requestBody, options.signal) replacing prior conditional selection between proxyAIRequest and proxyAIRequestWithConfig.
sequenceDiagram
  participant AIService
  participant BackendAdapter
  participant BackendAPI
  AIService->>BackendAdapter: proxyAIRequestWithFallback(configId, config, body, signal)
  BackendAdapter->>BackendAPI: POST /proxy/ai (with configId)
  alt Backend returns 200
    BackendAPI-->>BackendAdapter: JSON response
    BackendAdapter-->>AIService: return response
  else Backend returns 404 or AI_CONFIG_NOT_FOUND
    BackendAdapter->>BackendAPI: POST /proxy/ai (with inline config in body)
    BackendAPI-->>BackendAdapter: JSON response
    BackendAdapter-->>AIService: return response
  else other error
    BackendAPI-->>BackendAdapter: error
    BackendAdapter-->>AIService: throws translated error (Error with statusCode/code)
  end
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly Related PRs

Poem

🐰 I hopped a path with two bright doors,
Tried the key, then tried once more.
If one is gone, I gently swap—
A fallback hop, and then I stop.
Cozy code, no more detours.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: using inline config for AI proxy to avoid configId lookup failure, which matches the primary change of adding proxyAIRequestWithFallback method and routing requests through it.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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/166-ai-config-not-found

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.

@AmintaCCCP AmintaCCCP linked an issue May 27, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@src/services/aiService.ts`:
- Line 157: The code currently always sends this.config.apiKey inline when
calling backend.proxyAIRequestWithConfig (seen where data = await
backend.proxyAIRequestWithConfig(this.config, ...)), which exposes secrets for
saved configs; change the call sites (the calls at proxyAIRequestWithConfig
around the blocks handling requestBody/options) to first attempt using a minimal
payload containing only configId (this.config.id) and any non-secret metadata,
and only on a backend error indicating AI_CONFIG_NOT_FOUND or HTTP 404 retry
with the full inline config including this.config.apiKey; implement this retry
flow around backend.proxyAIRequestWithConfig and ensure errors are caught and
inspected for the specific not-found code before sending the fallback inline
config to avoid unnecessary secret transit.
🪄 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: aa1d5f0d-39a3-44cd-81af-6c752eb98bd2

📥 Commits

Reviewing files that changed from the base of the PR and between d93e18f and 9aa81b1.

📒 Files selected for processing (1)
  • src/services/aiService.ts

Comment thread src/services/aiService.ts Outdated
Address CodeRabbit review: avoid sending API key inline when the config
exists in the backend DB. Added proxyAIRequestWithFallback that first
tries configId lookup, and only falls back to inline config on 404.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@src/services/backendAdapter.ts`:
- Around line 269-274: The fallback check should not rely on localized message
text; change the error propagation so throwTranslatedError preserves a stable
machine-readable indicator (e.g., attach err.code = 'AI_CONFIG_NOT_FOUND' or set
err.cause?.status = 404) and then replace the message substring checks in the
catch block with a check against that property (e.g., if ((err as any).code !==
'AI_CONFIG_NOT_FOUND' && (err as any).cause?.status !== 404) throw err). Update
throwTranslatedError to copy the original error code/status into the thrown
Error and use that stable field here instead of checking for "AI config not
found".
🪄 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: 2ad7d540-01ea-4cf6-89e6-17ab02df7ff1

📥 Commits

Reviewing files that changed from the base of the PR and between 9aa81b1 and 0d27649.

📒 Files selected for processing (2)
  • src/services/aiService.ts
  • src/services/backendAdapter.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/services/aiService.ts

Comment thread src/services/backendAdapter.ts
Address CodeRabbit review: throwTranslatedError now attaches statusCode
and code to the Error object. proxyAIRequestWithFallback checks these
stable properties instead of matching localized message strings.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@AmintaCCCP AmintaCCCP merged commit 2889594 into main May 28, 2026
5 checks passed
@AmintaCCCP AmintaCCCP deleted the fix/166-ai-config-not-found branch May 28, 2026 08:00
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.

AI配置测试成功,实际分析不可用

1 participant