Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix: handle keyless litellm proxy and improve error messages

RheagalFire797980a144ed
diff --git a/src/providers/openai-compat.ts b/src/providers/openai-compat.ts
index 225a1c7f..6ffa3d0a 100644
--- a/src/providers/openai-compat.ts
+++ b/src/providers/openai-compat.ts
@@ -25,37 +25,67 @@ export class OpenAICompatProvider extends BaseProvider {
: this.client(config.model);
}
+ private formatApiError(err: unknown): Error {
+ if (err instanceof Error) {
+ const msg = err.message || '';
+ // Surface common HTTP error codes with actionable messages
+ if (msg.includes('401') || msg.toLowerCase().includes('unauthorized')) {
+ return new Error(`[${this.name}] Authentication failed - check your API key or proxy credentials.`);
+ }
+ if (msg.includes('404') || msg.toLowerCase().includes('not found')) {
+ return new Error(`[${this.name}] Model "${this.model}" not found - verify the model name and base URL.`);
+ }
+ if (msg.includes('429') || msg.toLowerCase().includes('rate limit') || msg.toLowerCase().includes('too many')) {
+ return new Error(`[${this.name}] Rate limited - wait a moment before retrying.`);
+ }
+ return new Error(`[${this.name}] ${msg}`);
+ }
+ return new Error(`[${this.name}] Unknown error during API call.`);
+ }
+
async generateText(prompt: string, systemPrompt: string): Promise<LLMResponse> {
- const result = await generateText({
- model: this.modelInstance,
- system: systemPrompt,
- prompt,
- });
+ try {
+ const result = await generateText({
+ model: this.modelInstance,
+ system: systemPrompt,
+ prompt,
+ });
- return {
- text: result.text,
- inputTokens: result.usage?.inputTokens ?? 0,
- outputTokens: result.usage?.outputTokens ?? 0,
- totalTokens: (result.usage?.inputTokens ?? 0) + (result.usage?.outputTokens ?? 0),
- model: this.model,
- provider: this.name,
- };
+ return {
+ text: result.text,
+ inputTokens: result.usage?.inputTokens ?? 0,
+ outputTokens: result.usage?.outputTokens ?? 0,
+ totalTokens: (result.usage?.inputTokens ?? 0) + (result.usage?.outputTokens ?? 0),
+ model: this.model,
+ provider: this.name,
+ };
+ } catch (err) {
+ throw this.formatApiError(err);
+ }
}
async *streamText(prompt: string, systemPrompt: string): AsyncIterable<LLMStreamChunk> {
- const result = streamText({
- model: this.modelInstance,
- system: systemPrompt,
- prompt,
- });
+ try {
+ const result = streamText({
+ model: this.modelInstance,
+ system: systemPrompt,
+ prompt,
+ });
- for await (const chunk of (await result).textStream) {
- yield { text: chunk, done: false };
+ for await (const chunk of (await result).textStream) {
+ yield { text: chunk, done: false };
+ }
+ yield { text: '', done: true };
+ } catch (err) {
+ throw this.formatApiError(err);
}
- yield { text: '', done: true };
}
isAvailable(): boolean {
+ // LiteLLM proxies and local Ollama can run without an API key
+ if (this.config.name === 'litellm' || this.config.name === 'ollamaLocal') {
+ return this.config.baseUrl.length > 0;
+ }
return this.config.apiKey.length > 0;
}
diff --git a/src/utils/provider-models.ts b/src/utils/provider-models.ts
index 7e5d9871..1e60242d 100644
--- a/src/utils/provider-models.ts
+++ b/src/utils/provider-models.ts
@@ -264,6 +264,8 @@ async function fetchOpenAICompatModels(provider: ProviderName, config: ProviderC
errorMessage = 'Mercury could not fetch models for this DeepSeek key. Please re-enter it.';
} else if (provider === 'openaiCompat') {
errorMessage = 'Mercury could not fetch models from this server. Please check the base URL and try again.';
+ } else if (provider === 'litellm') {
+ errorMessage = 'Mercury could not fetch models from the LiteLLM proxy. Please check the base URL and ensure the proxy is running.';
} else {
errorMessage = 'Mercury could not fetch models for this OpenAI key. Please re-enter it.';
}
@@ -280,7 +282,7 @@ async function fetchOpenAICompatModels(provider: ProviderName, config: ProviderC
if (provider === 'deepseek') {
return id.startsWith('deepseek-');
}
- if (provider === 'openaiCompat') {
+ if (provider === 'openaiCompat' || provider === 'litellm') {
return id.length > 0;
}
return isOpenAIChatModel(id);
@@ -421,7 +423,7 @@ export async function fetchProviderModelCatalog(
return fetchOllamaLocalModels(config);
}
- if (provider === 'openaiCompat') {
+ if (provider === 'openaiCompat' || provider === 'litellm') {
return fetchOpenAICompatModels(provider, config);
}
diff --git a/src/web/api/providers.ts b/src/web/api/providers.ts
index c81d7033..c3444526 100644
--- a/src/web/api/providers.ts
+++ b/src/web/api/providers.ts
@@ -46,9 +46,18 @@ providers.post('/api/providers/:name/test', async (c) => {
const config = loadConfig();
const p = config.providers[providerName];
- if (!p || !p.apiKey) {
+ if (!p) {
+ return c.json({ error: 'Provider not found' }, 400);
+ }
+
+ // LiteLLM proxies can be keyless - only require baseUrl
+ const needsKey = providerName !== 'litellm' && providerName !== 'ollamaLocal';
+ if (needsKey && !p.apiKey) {
return c.json({ error: 'No API key configured' }, 400);
}
+ if (!needsKey && !p.baseUrl) {
+ return c.json({ error: 'No base URL configured' }, 400);
+ }
try {
const { fetchProviderModelCatalog } = await import('../../utils/provider-models.js');