Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions src/services/aiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ export class AIService {

let data: Record<string, unknown>;
if (backend.isAvailable) {
if (this.config.id) {
data = await backend.proxyAIRequest(this.config.id, requestBody, options.signal) as Record<string, unknown>;
} else {
data = await backend.proxyAIRequestWithConfig(this.config, requestBody, options.signal) as Record<string, unknown>;
}
data = await backend.proxyAIRequestWithFallback(this.config.id, this.config, requestBody, options.signal) as Record<string, unknown>;
} else {
const url = buildFinalApiUrl(this.config.baseUrl, apiType);
const response = await fetch(url, {
Expand Down Expand Up @@ -216,11 +212,7 @@ export class AIService {

let data: unknown;
if (backend.isAvailable) {
if (this.config.id) {
data = await backend.proxyAIRequest(this.config.id, requestBody, options.signal);
} else {
data = await backend.proxyAIRequestWithConfig(this.config, requestBody, options.signal);
}
data = await backend.proxyAIRequestWithFallback(this.config.id, this.config, requestBody, options.signal);
} else {
const url = buildApiUrl(this.config.baseUrl, 'v1/messages');
const response = await fetch(url, {
Expand Down Expand Up @@ -276,11 +268,7 @@ ${options.user}` : options.user;

let data: unknown;
if (backend.isAvailable) {
if (this.config.id) {
data = await backend.proxyAIRequest(this.config.id, requestBody, options.signal);
} else {
data = await backend.proxyAIRequestWithConfig(this.config, requestBody, options.signal);
}
data = await backend.proxyAIRequestWithFallback(this.config.id, this.config, requestBody, options.signal);
} else {
const path = `v1beta/models/${encodeURIComponent(model)}:generateContent`;
const urlObj = new URL(buildApiUrl(this.config.baseUrl, path));
Expand Down
26 changes: 26 additions & 0 deletions src/services/backendAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,32 @@ class BackendAdapter {
return res.json();
}

async proxyAIRequestWithFallback(configId: string, aiConfig: { apiType?: string; baseUrl: string; apiKey: string; model: string; reasoningEffort?: string }, body: object, signal?: AbortSignal): Promise<unknown> {
if (!this._backendUrl) throw new Error('Backend not available');

// Try configId lookup first to avoid sending API key inline
if (configId) {
try {
const res = await this.fetchWithTimeout(`${this._backendUrl}/proxy/ai`, {
method: 'POST',
headers: this.getAuthHeaders(),
body: JSON.stringify({ configId, body }),
signal,
}, 120000);
if (res.ok) return res.json();
// Fall through to inline config on 404 (config not synced yet)
if (res.status !== 404) await this.throwTranslatedError(res, 'AI proxy error');
} catch (err) {
// If it's a non-404 error, rethrow; otherwise fall through
const msg = err instanceof Error ? err.message : '';
if (!msg.includes('AI config not found') && !msg.includes('AI_CONFIG_NOT_FOUND')) throw err;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

// Fallback: send full config inline
return this.proxyAIRequestWithConfig(aiConfig, body, signal);
}

// === WebDAV Proxy ===

async proxyWebDAV(configId: string, method: string, path: string, body?: string, headers?: Record<string, string>): Promise<Response> {
Expand Down
Loading