Skip to content

🙌 OpenHands: AI-Driven Development

feat(acp): show a code-specific header + re-auth action on ACP error banners (#1387)

simonrosenberg61ed008f8906
diff --git a/__tests__/components/chat/error-message-banner.test.tsx b/__tests__/components/chat/error-message-banner.test.tsx
index 82d73628e7..251be62daa 100644
--- a/__tests__/components/chat/error-message-banner.test.tsx
+++ b/__tests__/components/chat/error-message-banner.test.tsx
@@ -63,4 +63,52 @@ describe("ErrorMessageBanner", () => {
await user.click(toggle);
expect(toggle).toHaveTextContent("COMMON$VIEW_LESS");
});
+
+ it("renders a code-specific header for a known ACP error code", () => {
+ render(
+ <ErrorMessageBanner
+ message="[-32603] Internal error: 401 invalid x-api-key"
+ code="ACPAuthRequired"
+ />,
+ );
+
+ const header = screen.getByTestId("error-message-banner-header");
+ expect(header).toHaveTextContent("ERROR$ACP_AUTH_REQUIRED_TITLE");
+ // The detail is still shown verbatim below the header.
+ expect(screen.getByTestId("error-message-banner-content")).toHaveTextContent(
+ "invalid x-api-key",
+ );
+ });
+
+ it("renders no header for an unknown or absent code", () => {
+ render(<ErrorMessageBanner message="boom" code={null} />);
+ expect(
+ screen.queryByTestId("error-message-banner-header"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("shows a re-auth action and calls onReauth when provided", async () => {
+ const user = userEvent.setup();
+ const onReauth = vi.fn();
+
+ render(
+ <ErrorMessageBanner
+ message="Authentication failed"
+ code="ACPAuthRequired"
+ onReauth={onReauth}
+ />,
+ );
+
+ const reauth = screen.getByTestId("error-message-banner-reauth");
+ expect(reauth).toHaveTextContent("ERROR$ACP_UPDATE_CREDENTIALS");
+ await user.click(reauth);
+ expect(onReauth).toHaveBeenCalledTimes(1);
+ });
+
+ it("omits the re-auth action when onReauth is not provided", () => {
+ render(<ErrorMessageBanner message="boom" code="ACPPromptError" />);
+ expect(
+ screen.queryByTestId("error-message-banner-reauth"),
+ ).not.toBeInTheDocument();
+ });
});
diff --git a/__tests__/stores/error-message-store.test.ts b/__tests__/stores/error-message-store.test.ts
index 071329b88a..f768424275 100644
--- a/__tests__/stores/error-message-store.test.ts
+++ b/__tests__/stores/error-message-store.test.ts
@@ -5,7 +5,11 @@ const getState = () => useErrorMessageStore.getState();
describe("error message store", () => {
beforeEach(() => {
- useErrorMessageStore.setState({ errorMessage: null, errorType: null });
+ useErrorMessageStore.setState({
+ errorMessage: null,
+ errorType: null,
+ errorCode: null,
+ });
});
it("defaults to a sticky conversation error", () => {
@@ -45,4 +49,24 @@ describe("error message store", () => {
expect(getState().errorMessage).toBeNull();
expect(getState().errorType).toBeNull();
});
+
+ it("stores the optional error code and defaults it to null", () => {
+ getState().setErrorMessage("boom");
+ expect(getState().errorCode).toBeNull();
+
+ getState().setErrorMessage("auth failed", "conversation", "ACPAuthRequired");
+ expect(getState().errorCode).toBe("ACPAuthRequired");
+ });
+
+ it("removeErrorMessage clears the error code too", () => {
+ getState().setErrorMessage("auth failed", "conversation", "ACPAuthRequired");
+ getState().removeErrorMessage();
+ expect(getState().errorCode).toBeNull();
+ });
+
+ it("clearConnectionError clears the code for a connection error", () => {
+ getState().setErrorMessage("offline", "connection", "SomeCode");
+ getState().clearConnectionError();
+ expect(getState().errorCode).toBeNull();
+ });
});
diff --git a/__tests__/utils/acp-error-codes.test.ts b/__tests__/utils/acp-error-codes.test.ts
new file mode 100644
index 0000000000..86c54cad08
--- /dev/null
+++ b/__tests__/utils/acp-error-codes.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest";
+import { I18nKey } from "#/i18n/declaration";
+import {
+ getAcpErrorHeaderKey,
+ isAcpAuthErrorCode,
+} from "#/utils/acp-error-codes";
+
+describe("acp-error-codes", () => {
+ it("maps the auth code to the auth header key", () => {
+ expect(getAcpErrorHeaderKey("ACPAuthRequired")).toBe(
+ I18nKey.ERROR$ACP_AUTH_REQUIRED_TITLE,
+ );
+ });
+
+ it("maps other ACP codes to the generic agent-error header", () => {
+ for (const code of [
+ "ACPSpawnError",
+ "ACPInitError",
+ "ACPPromptError",
+ "UsagePolicyRefusal",
+ ]) {
+ expect(getAcpErrorHeaderKey(code)).toBe(
+ I18nKey.CHAT_INTERFACE$AGENT_ERROR_MESSAGE,
+ );
+ }
+ });
+
+ it("returns null for unknown, empty, or missing codes", () => {
+ expect(getAcpErrorHeaderKey(null)).toBeNull();
+ expect(getAcpErrorHeaderKey(undefined)).toBeNull();
+ expect(getAcpErrorHeaderKey("")).toBeNull();
+ expect(getAcpErrorHeaderKey("RequestError")).toBeNull();
+ });
+
+ it("flags only the auth code for re-authentication", () => {
+ expect(isAcpAuthErrorCode("ACPAuthRequired")).toBe(true);
+ expect(isAcpAuthErrorCode("ACPPromptError")).toBe(false);
+ expect(isAcpAuthErrorCode(null)).toBe(false);
+ expect(isAcpAuthErrorCode(undefined)).toBe(false);
+ });
+});
diff --git a/src/components/features/chat/chat-interface.tsx b/src/components/features/chat/chat-interface.tsx
index 131c089c08..345ed5012b 100644
--- a/src/components/features/chat/chat-interface.tsx
+++ b/src/components/features/chat/chat-interface.tsx
@@ -1,6 +1,8 @@
import React from "react";
+import { useNavigate } from "react-router";
import { useTracking } from "#/hooks/use-tracking";
import { useTranslation } from "react-i18next";
+import { isAcpAuthErrorCode } from "#/utils/acp-error-codes";
import { convertImageToBase64 } from "#/utils/convert-image-to-base-64";
import { createChatMessage } from "#/services/chat-service";
import { BtwMessages } from "./btw-messages";
@@ -58,8 +60,9 @@ function getEntryPoint(
export function ChatInterface() {
const { trackInitialQuerySubmitted, trackUserMessageSent } = useTracking();
const { setMessageToSend } = useConversationStore();
- const { errorMessage, removeErrorMessage, setErrorMessage } =
+ const { errorMessage, errorCode, removeErrorMessage, setErrorMessage } =
useErrorMessageStore();
+ const navigate = useNavigate();
const { isTask, taskStatus, taskDetail } = useTaskPolling();
// Hide empty-state chrome for the entire `/conversations/task-{uuid}` route,
// including the brief READY window before redirect completes.
@@ -538,12 +541,18 @@ export function ChatInterface() {
{errorMessage && (
<ErrorMessageBanner
message={errorMessage}
+ code={errorCode}
onDismiss={removeErrorMessage}
onRetry={
errorMessage === SERVER_CONNECTION_ERROR_MESSAGE
? () => conversationWebSocket?.reconnect()
: undefined
}
+ onReauth={
+ isAcpAuthErrorCode(errorCode)
+ ? () => navigate("/settings/agent")
+ : undefined
+ }
/>
)}
diff --git a/src/components/features/chat/error-message-banner.tsx b/src/components/features/chat/error-message-banner.tsx
index 63dd1f158f..6f2e7ba8bd 100644
--- a/src/components/features/chat/error-message-banner.tsx
+++ b/src/components/features/chat/error-message-banner.tsx
@@ -3,22 +3,30 @@ import { Trans, useTranslation } from "react-i18next";
import { CircleX, X } from "lucide-react";
import { OH_STATUS_ERROR_COLOR } from "#/constants/status-colors";
import { I18nKey } from "#/i18n/declaration";
+import { getAcpErrorHeaderKey } from "#/utils/acp-error-codes";
import { cn } from "#/utils/utils";
interface ErrorMessageBannerProps {
message: string;
+ /** Structured error code (e.g. "ACPAuthRequired") used to pick a header. */
+ code?: string | null;
onDismiss?: () => void;
onRetry?: () => void;
+ /** Recovery action (e.g. re-authenticate) shown for credential failures. */
+ onReauth?: () => void;
}
const DEFAULT_MAX_COLLAPSED_CHARS = 220;
export function ErrorMessageBanner({
message,
+ code,
onDismiss,
onRetry,
+ onReauth,
}: ErrorMessageBannerProps) {
const { t, i18n } = useTranslation("openhands");
+ const headerKey = getAcpErrorHeaderKey(code);
const [isExpanded, setIsExpanded] = React.useState(false);
const [isMultiLine, setIsMultiLine] = React.useState(false);
const contentRef = React.useRef<HTMLDivElement>(null);
@@ -72,6 +80,14 @@ export function ErrorMessageBanner({
data-testid="error-message-banner-icon"
/>
<div className="min-w-0 flex-1">
+ {headerKey && (
+ <div
+ className="text-sm font-medium text-[var(--oh-foreground)]"
+ data-testid="error-message-banner-header"
+ >
+ {t(headerKey)}
+ </div>
+ )}
<div
ref={contentRef}
className={cn(
@@ -83,6 +99,17 @@ export function ErrorMessageBanner({
{isI18nKey ? <Trans ns="openhands" i18nKey={message} /> : message}
</div>
+ {onReauth && (
+ <button
+ type="button"
+ onClick={onReauth}
+ className="mt-2 cursor-pointer rounded-md border border-[var(--oh-border)] px-2 py-1 text-xs font-normal text-[var(--oh-foreground)] hover:bg-[var(--oh-interactive-hover)]"
+ data-testid="error-message-banner-reauth"
+ >
+ {t(I18nKey.ERROR$ACP_UPDATE_CREDENTIALS)}
+ </button>
+ )}
+
{shouldShowToggle && (
<button
type="button"
diff --git a/src/contexts/conversation-websocket-context.tsx b/src/contexts/conversation-websocket-context.tsx
index 9e69e89902..3e6b314c77 100644
--- a/src/contexts/conversation-websocket-context.tsx
+++ b/src/contexts/conversation-websocket-context.tsx
@@ -489,7 +489,7 @@ export function ConversationWebSocketProvider({
},
posthog,
});
- setErrorMessage(errorEvent.detail);
+ setErrorMessage(errorEvent.detail, "conversation", errorEvent.code);
} else {
handleNonErrorEvent();
}
@@ -683,7 +683,7 @@ export function ConversationWebSocketProvider({
},
posthog,
});
- setErrorMessage(errorEvent.detail);
+ setErrorMessage(errorEvent.detail, "conversation", errorEvent.code);
} else {
handleNonErrorEvent();
}
diff --git a/src/i18n/translation.json b/src/i18n/translation.json
index c4c85ccdd1..294b00669a 100644
--- a/src/i18n/translation.json
+++ b/src/i18n/translation.json
@@ -7037,6 +7037,40 @@
"uk": "Сталася помилка",
"ca": "S'ha produït un error"
},
+ "ERROR$ACP_AUTH_REQUIRED_TITLE": {
+ "en": "Authentication required",
+ "ja": "認証が必要です",
+ "zh-CN": "需要身份验证",
+ "zh-TW": "需要驗證",
+ "ko-KR": "인증이 필요합니다",
+ "de": "Authentifizierung erforderlich",
+ "no": "Autentisering kreves",
+ "it": "Autenticazione richiesta",
+ "pt": "Autenticação necessária",
+ "es": "Autenticación requerida",
+ "ar": "المصادقة مطلوبة",
+ "fr": "Authentification requise",
+ "tr": "Kimlik doğrulaması gerekli",
+ "uk": "Потрібна автентифікація",
+ "ca": "Cal autenticació"
+ },
+ "ERROR$ACP_UPDATE_CREDENTIALS": {
+ "en": "Update credentials",
+ "ja": "認証情報を更新",
+ "zh-CN": "更新凭据",
+ "zh-TW": "更新憑證",
+ "ko-KR": "자격 증명 업데이트",
+ "de": "Anmeldedaten aktualisieren",
+ "no": "Oppdater legitimasjon",
+ "it": "Aggiorna le credenziali",
+ "pt": "Atualizar credenciais",
+ "es": "Actualizar credenciales",
+ "ar": "تحديث بيانات الاعتماد",
+ "fr": "Mettre à jour les identifiants",
+ "tr": "Kimlik bilgilerini güncelle",
+ "uk": "Оновити облікові дані",
+ "ca": "Actualitza les credencials"
+ },
"ERROR$CORS_OR_NETWORK": {
"en": "Disconnected (check URL or network). Check that the backend URL is correct and the backend server is reachable. If the backend is on another origin, check that it allows this frontend origin.",
"ja": "切断されています(URL またはネットワークを確認)。バックエンド URL が正しく、バックエンドサーバーに到達できることを確認してください。バックエンドが別のオリジンにある場合は、このフロントエンドのオリジンを許可していることも確認してください。",
diff --git a/src/stores/error-message-store.ts b/src/stores/error-message-store.ts
index 7c030d89fe..8235846f2a 100644
--- a/src/stores/error-message-store.ts
+++ b/src/stores/error-message-store.ts
@@ -10,10 +10,20 @@ export type ErrorMessageType = "connection" | "conversation";
interface ErrorMessageState {
errorMessage: string | null;
errorType: ErrorMessageType | null;
+ /**
+ * Structured error code from a ConversationErrorEvent/ServerErrorEvent (e.g.
+ * "ACPAuthRequired"), used to render a code-specific header + recovery action.
+ * null for errors that carry no code (connection errors, plain strings).
+ */
+ errorCode: string | null;
}
interface ErrorMessageActions {
- setErrorMessage: (message: string, type?: ErrorMessageType) => void;
+ setErrorMessage: (
+ message: string,
+ type?: ErrorMessageType,
+ code?: string | null,
+ ) => void;
removeErrorMessage: () => void;
/** Clears the error only when it is a transient connection error. */
clearConnectionError: () => void;
@@ -24,27 +34,27 @@ type ErrorMessageStore = ErrorMessageState & ErrorMessageActions;
const initialState: ErrorMessageState = {
errorMessage: null,
errorType: null,
+ errorCode: null,
};
export const useErrorMessageStore = create<ErrorMessageStore>((set) => ({
...initialState,
- setErrorMessage: (message: string, type: ErrorMessageType = "conversation") =>
+ setErrorMessage: (
+ message: string,
+ type: ErrorMessageType = "conversation",
+ code: string | null = null,
+ ) =>
set(() => ({
errorMessage: message,
errorType: type,
+ errorCode: code,
})),
- removeErrorMessage: () =>
- set(() => ({
- errorMessage: null,
- errorType: null,
- })),
+ removeErrorMessage: () => set(() => ({ ...initialState })),
clearConnectionError: () =>
set((state) =>
- state.errorType === "connection"
- ? { errorMessage: null, errorType: null }
- : state,
+ state.errorType === "connection" ? { ...initialState } : state,
),
}));
diff --git a/src/utils/acp-error-codes.ts b/src/utils/acp-error-codes.ts
new file mode 100644
index 0000000000..4af44228f4
--- /dev/null
+++ b/src/utils/acp-error-codes.ts
@@ -0,0 +1,29 @@
+import { I18nKey } from "#/i18n/declaration";
+
+/**
+ * Structured `code` values the SDK's ACPAgent puts on a ConversationErrorEvent
+ * (see software-agent-sdk `acp_agent.py`). The banner uses them to show a
+ * code-specific header and, for credential failures, a recovery action.
+ */
+export const ACP_AUTH_REQUIRED_CODE = "ACPAuthRequired";
+
+const ACP_ERROR_HEADER_KEYS: Record<string, I18nKey> = {
+ ACPAuthRequired: I18nKey.ERROR$ACP_AUTH_REQUIRED_TITLE,
+ // Spawn/init/prompt/usage-policy failures share the generic "Agent error"
+ // header; their detail already carries the specific cause.
+ ACPSpawnError: I18nKey.CHAT_INTERFACE$AGENT_ERROR_MESSAGE,
+ ACPInitError: I18nKey.CHAT_INTERFACE$AGENT_ERROR_MESSAGE,
+ ACPPromptError: I18nKey.CHAT_INTERFACE$AGENT_ERROR_MESSAGE,
+ UsagePolicyRefusal: I18nKey.CHAT_INTERFACE$AGENT_ERROR_MESSAGE,
+};
+
+/** Localized header key for an error code, or null when the code is unknown. */
+export function getAcpErrorHeaderKey(code?: string | null): I18nKey | null {
+ if (!code) return null;
+ return ACP_ERROR_HEADER_KEYS[code] ?? null;
+}
+
+/** Whether the error is a credential failure that warrants a re-auth action. */
+export function isAcpAuthErrorCode(code?: string | null): boolean {
+ return code === ACP_AUTH_REQUIRED_CODE;
+}