The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
// Resolves the error card's title/body for a failed turn from the i18n tables
// (`assistant.thread.errorCodes` / `errorAuthKinds` / `errorLayers`), so the
// inline card and the global gateway-error toast read the same words for the
// same failure. Sibling of error-surface.ts because i18n/types.ts imports the
// code list from there — importing Translations back would be a cycle.
import type { ErrorCardCopy, Translations } from '@/i18n/types'
import {
errorCardKey,
type ErrorSurface,
formatResetClock,
isFreeTierSurface,
scheduledRetryDelayMs
} from './error-surface'
export interface ErrorCardText {
title: string
body: string
}
const render = (value: ErrorCardCopy['title'], provider: string, model = '') =>
typeof value === 'function' ? value(provider, model) : value
/** Model name for copy: the descriptor's model without its vendor prefix
* ("anthropic/claude-sonnet-5" → "claude-sonnet-5"). */
export function errorModelName(surface?: ErrorSurface | null): string {
const model = surface?.model?.trim() || ''
return model.includes('/') ? model.slice(model.lastIndexOf('/') + 1) : model
}
/** The failing provider's display name for copy — the descriptor's label,
* else its id, else the generic "the AI service". */
export function errorProviderName(thread: Translations['assistant']['thread'], surface?: ErrorSurface | null): string {
return surface?.providerLabel || surface?.provider || thread.errorGenericProvider
}
export function errorCardText(
thread: Translations['assistant']['thread'],
surface: ErrorSurface | null | undefined
): ErrorCardText {
const provider = errorProviderName(thread, surface)
const model = errorModelName(surface)
// A credential rejection is worded by HOW the provider is credentialed
// (key vs sign-in), which the code alone (`auth`) cannot tell.
if (surface?.layer === 'auth' && surface.authKind === 'oauth') {
return { body: thread.errorOauthExpired(provider), title: render(thread.errorAuthKinds.oauth.title, provider) }
}
if (surface?.layer === 'auth' && surface.authKind === 'api_key') {
const copy = thread.errorAuthKinds.api_key
return { body: render(copy.body, provider, model), title: render(copy.title, provider, model) }
}
// A rate limit whose reset moment is known names it instead of "wait a moment".
if (
(surface?.code === 'rate_limit' || surface?.code === 'upstream_rate_limit') &&
surface.resetsAt !== undefined &&
scheduledRetryDelayMs(surface.resetsAt) !== null
) {
const copy = thread.errorCodes[surface.code]
return {
body: thread.errorRateLimitResetBody(provider, formatResetClock(surface.resetsAt)),
title: render(copy.title, provider, model)
}
}
const key = errorCardKey(surface)
if ('code' in key) {
const copy = thread.errorCodes[key.code]
// Free-tier and usage-window refusals arrive with the backend's own sentence (the
// wait, the model, the way forward); the table body is only the fallback for an older backend.
const ownSentence = isFreeTierSurface(surface) || surface?.code === 'usage_window'
const body = surface?.retryable === false && copy.bodyNoRetry ? copy.bodyNoRetry : copy.body
return {
body: (ownSentence && surface?.message) || render(body, provider, model),
title: render(copy.title, provider, model)
}
}
// Pre-turn gateway refusals carry the server's own actionable sentence; it
// is the body, not a generic gloss with the useful text buried in a toast.
return { body: surface?.message || thread.errorLayerBodies[key.layer], title: thread.errorLayers[key.layer] }
}