ca143a4e08
- Added a persistent error banner so stream errors survive transcript scroll. - Cleared the banner when the next turn starts. - Skipped pinning for aborts and normal stops.
138 lines
4.9 KiB
TypeScript
138 lines
4.9 KiB
TypeScript
/**
|
|
* EventController error-banner wiring.
|
|
*
|
|
* A turn that ends on a provider error (e.g. Anthropic's "Output blocked by
|
|
* content filtering policy") must pin a persistent banner above the editor via
|
|
* `ctx.showPinnedError`, and the banner must be cleared at the next turn's
|
|
* `agent_start` via `ctx.clearPinnedError`. Aborts and normal stops must NOT
|
|
* pin a banner.
|
|
*/
|
|
import { beforeAll, describe, expect, it, vi } from "bun:test";
|
|
import type { AssistantMessage } from "@oh-my-pi/pi-ai";
|
|
import { ErrorBannerComponent } from "@oh-my-pi/pi-coding-agent/modes/components/error-banner";
|
|
import { EventController } from "@oh-my-pi/pi-coding-agent/modes/controllers/event-controller";
|
|
import { initTheme } from "@oh-my-pi/pi-coding-agent/modes/theme/theme";
|
|
import type { InteractiveModeContext } from "@oh-my-pi/pi-coding-agent/modes/types";
|
|
import type { AgentSessionEvent } from "@oh-my-pi/pi-coding-agent/session/agent-session";
|
|
|
|
function makeAssistantMessage(overrides: Partial<AssistantMessage> = {}): AssistantMessage {
|
|
return {
|
|
role: "assistant",
|
|
content: [{ type: "text", text: "draft" }],
|
|
api: "anthropic-messages",
|
|
provider: "anthropic",
|
|
model: "claude-sonnet-4-5",
|
|
stopReason: "stop",
|
|
usage: {
|
|
input: 0,
|
|
output: 0,
|
|
cacheRead: 0,
|
|
cacheWrite: 0,
|
|
totalTokens: 0,
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
},
|
|
timestamp: Date.now(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
await initTheme(false);
|
|
});
|
|
|
|
function createFixture(streamingMessage?: AssistantMessage) {
|
|
const streamingComponent = {
|
|
updateContent: vi.fn(),
|
|
setUsageInfo: vi.fn(),
|
|
setComplete: vi.fn(),
|
|
markTranscriptBlockFinalized: vi.fn(),
|
|
};
|
|
const showPinnedError = vi.fn();
|
|
const clearPinnedError = vi.fn();
|
|
|
|
const ctx = {
|
|
isInitialized: true,
|
|
init: vi.fn(async () => {}),
|
|
ui: { requestRender: vi.fn(), setEagerNativeScrollbackRebuild: vi.fn() },
|
|
statusLine: { invalidate: vi.fn() },
|
|
updateEditorTopBorder: vi.fn(),
|
|
ensureLoadingAnimation: vi.fn(),
|
|
editor: {},
|
|
streamingComponent: streamingMessage ? streamingComponent : undefined,
|
|
streamingMessage,
|
|
pendingTools: new Map(),
|
|
showPinnedError,
|
|
clearPinnedError,
|
|
session: { isTtsrAbortPending: false, retryAttempt: 0 },
|
|
} as unknown as InteractiveModeContext;
|
|
|
|
const controller = new EventController(ctx);
|
|
return { controller, ctx, showPinnedError, clearPinnedError };
|
|
}
|
|
|
|
describe("EventController error banner", () => {
|
|
it("pins the provider error above the editor when an assistant turn ends on stopReason error", async () => {
|
|
const errorMessage = "Output blocked by content filtering policy";
|
|
const message = makeAssistantMessage({ stopReason: "error", errorMessage });
|
|
const { controller, showPinnedError } = createFixture(message);
|
|
|
|
await controller.handleEvent({ type: "message_end", message } as Extract<
|
|
AgentSessionEvent,
|
|
{ type: "message_end" }
|
|
>);
|
|
|
|
expect(showPinnedError).toHaveBeenCalledTimes(1);
|
|
expect(showPinnedError).toHaveBeenCalledWith(errorMessage);
|
|
});
|
|
|
|
it("does not pin a banner for a normal assistant stop", async () => {
|
|
const message = makeAssistantMessage({ stopReason: "stop" });
|
|
const { controller, showPinnedError } = createFixture(message);
|
|
|
|
await controller.handleEvent({ type: "message_end", message } as Extract<
|
|
AgentSessionEvent,
|
|
{ type: "message_end" }
|
|
>);
|
|
|
|
expect(showPinnedError).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not pin a banner for an aborted assistant turn", async () => {
|
|
const message = makeAssistantMessage({ stopReason: "aborted", errorMessage: "Operation aborted" });
|
|
const { controller, showPinnedError } = createFixture(message);
|
|
|
|
await controller.handleEvent({ type: "message_end", message } as Extract<
|
|
AgentSessionEvent,
|
|
{ type: "message_end" }
|
|
>);
|
|
|
|
expect(showPinnedError).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("clears the pinned banner when the next turn starts", async () => {
|
|
const { controller, clearPinnedError } = createFixture();
|
|
|
|
await controller.handleEvent({ type: "agent_start" } as Extract<AgentSessionEvent, { type: "agent_start" }>);
|
|
|
|
expect(clearPinnedError).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe("ErrorBannerComponent", () => {
|
|
it("renders the provider error message", () => {
|
|
const banner = new ErrorBannerComponent("Output blocked by content filtering policy");
|
|
const rendered = Bun.stripANSI(banner.render(120).join("\n"));
|
|
expect(rendered).toContain("Output blocked by content filtering policy");
|
|
expect(rendered).toContain("Dismissed when you send your next message.");
|
|
});
|
|
|
|
it("caps an oversized multi-line error to a few lines", () => {
|
|
const huge = Array.from({ length: 50 }, (_, i) => `error detail line ${i}`).join("\n");
|
|
const banner = new ErrorBannerComponent(huge);
|
|
const lines = Bun.stripANSI(banner.render(120).join("\n")).split("\n");
|
|
const detailLines = lines.filter(line => line.includes("error detail line"));
|
|
expect(detailLines.length).toBeLessThanOrEqual(3);
|
|
expect(detailLines.length).toBeGreaterThan(0);
|
|
});
|
|
});
|