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: hide provider details from chat errors

Salman Qureshi76277dff9a6b
diff --git a/src/channels/web.test.ts b/src/channels/web.test.ts
index 78aca636..1ac65abd 100644
--- a/src/channels/web.test.ts
+++ b/src/channels/web.test.ts
@@ -46,12 +46,26 @@ describe('WebChannel durable streaming', () => {
return true;
});
- channel.sendHeartbeat('Provider mercuryCloud failed. Trying fallback...', 'request-fallback');
+ channel.sendHeartbeat('A model attempt failed. Trying another option...', 'request-fallback');
expect(events).toContainEqual(expect.objectContaining({ type: 'heartbeat' }));
expect(channel.hasCloudEventHandler('request-fallback')).toBe(true);
});
+ it('keeps provider diagnostics out of terminal errors', () => {
+ const channel = new WebChannel('Mercury');
+ const events: Array<{ type: string; data?: Record<string, unknown> }> = [];
+ channel.emitCloudMessage('work', 'request-error', 'session-2', 'conversation-2', undefined, (event) => {
+ events.push(event);
+ return true;
+ });
+
+ channel.sendError('OpenRouter error 401: invalid API key sk-secret', 'request-error');
+
+ expect(events[0]?.data?.message).toContain('Mercury could not connect');
+ expect(JSON.stringify(events)).not.toMatch(/openrouter|sk-secret/i);
+ });
+
it('requires an explicit continuation decision even when permissions are bypassed', async () => {
vi.useFakeTimers();
const channel = new WebChannel('Mercury');
diff --git a/src/channels/web.ts b/src/channels/web.ts
index 8e4e3e32..b192bbd9 100644
--- a/src/channels/web.ts
+++ b/src/channels/web.ts
@@ -2,6 +2,7 @@ import { BaseChannel } from './base.js';
import type { ChannelMessage } from '../types/channel.js';
import { logger } from '../utils/logger.js';
import { randomUUID } from 'node:crypto';
+import { userFacingAiError } from '../utils/redact.js';
type CloudEventHandler = (event: ChatEvent) => boolean;
@@ -305,7 +306,7 @@ export class WebChannel extends BaseChannel {
sendError(message: string, targetId?: string): boolean {
return this.broadcast({
type: 'error',
- data: { message, targetId },
+ data: { message: userFacingAiError(message), targetId },
});
}
diff --git a/src/core/agent.ts b/src/core/agent.ts
index b42abc1c..0dcea805 100644
--- a/src/core/agent.ts
+++ b/src/core/agent.ts
@@ -2355,7 +2355,7 @@ export class Agent {
break;
}
logger.warn({ provider: provider.name, err: err.message }, 'Provider failed, trying fallback');
- await this.sendProgressNotice(msg, `Provider ${provider.name} failed. Trying fallback...`)
+ await this.sendProgressNotice(msg, 'A model attempt failed. Trying another option...')
.catch((e) => logger.warn({ e }, 'channel send failed'));
}
}
diff --git a/src/utils/redact.ts b/src/utils/redact.ts
index 6783acbe..2fbc240b 100644
--- a/src/utils/redact.ts
+++ b/src/utils/redact.ts
@@ -16,4 +16,28 @@ export function redactIdentity(phone: string, uuid?: string): string {
return `${phonePart} (${redactUuid(uuid)})`;
}
return phonePart;
-}
\ No newline at end of file
+}
+
+export function userFacingAiError(error: unknown): string {
+ const message = error instanceof Error ? error.message : typeof error === 'string' ? error : '';
+ const normalized = message.toLowerCase();
+ if (/context(?:_|\s|-)?length|too many tokens|maximum context|request too large|payload too large/.test(normalized)) {
+ return 'This request is too large for the selected model. Shorten the conversation or choose another model.';
+ }
+ if (/rate.?limit|too many requests|quota|capacity|overloaded|\b429\b/.test(normalized)) {
+ return 'The selected model is temporarily busy. Please try again shortly.';
+ }
+ if (/timeout|timed out|deadline|stalled/.test(normalized)) {
+ return 'The request timed out before Mercury could finish. Please try again.';
+ }
+ if (/api.?key|authenticat|unauthori[sz]ed|forbidden|credential|\b401\b|\b403\b/.test(normalized)) {
+ return 'Mercury could not connect to the selected model. Please try another model or contact support.';
+ }
+ if (/model.+(?:not found|unavailable|unsupported|invalid)|no provider available|provider mismatch|\b404\b/.test(normalized)) {
+ return 'The selected model is currently unavailable. Please choose another model or try again later.';
+ }
+ if (/content.?policy|safety|moderation|blocked|refused/.test(normalized)) {
+ return 'The selected model could not process this request. Revise it and try again.';
+ }
+ return 'Mercury could not complete this request. Please try again shortly.';
+}