51 lines
2.1 KiB
TypeScript
51 lines
2.1 KiB
TypeScript
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
|
import { describe, expect, it } from "vitest";
|
|
import { formatAssistantErrorText } from "./pi-embedded-helpers.js";
|
|
import { DEFAULT_AGENTS_FILENAME } from "./workspace.js";
|
|
|
|
const _makeFile = (overrides: Partial<WorkspaceBootstrapFile>): WorkspaceBootstrapFile => ({
|
|
name: DEFAULT_AGENTS_FILENAME,
|
|
path: "/tmp/AGENTS.md",
|
|
content: "",
|
|
missing: false,
|
|
...overrides,
|
|
});
|
|
describe("formatAssistantErrorText", () => {
|
|
const makeAssistantError = (errorMessage: string): AssistantMessage =>
|
|
({
|
|
stopReason: "error",
|
|
errorMessage,
|
|
}) as AssistantMessage;
|
|
|
|
it("returns a friendly message for context overflow", () => {
|
|
const msg = makeAssistantError("request_too_large");
|
|
expect(formatAssistantErrorText(msg)).toContain("Context overflow");
|
|
});
|
|
it("returns a friendly message for Anthropic role ordering", () => {
|
|
const msg = makeAssistantError('messages: roles must alternate between "user" and "assistant"');
|
|
expect(formatAssistantErrorText(msg)).toContain("Message ordering conflict");
|
|
});
|
|
it("returns a friendly message for Anthropic overload errors", () => {
|
|
const msg = makeAssistantError(
|
|
'{"type":"error","error":{"details":null,"type":"overloaded_error","message":"Overloaded"},"request_id":"req_123"}',
|
|
);
|
|
expect(formatAssistantErrorText(msg)).toBe(
|
|
"The AI service is temporarily overloaded. Please try again in a moment.",
|
|
);
|
|
});
|
|
it("handles JSON-wrapped role errors", () => {
|
|
const msg = makeAssistantError('{"error":{"message":"400 Incorrect role information"}}');
|
|
const result = formatAssistantErrorText(msg);
|
|
expect(result).toContain("Message ordering conflict");
|
|
expect(result).not.toContain("400");
|
|
});
|
|
it("suppresses raw error JSON payloads that are not otherwise classified", () => {
|
|
const msg = makeAssistantError(
|
|
'{"type":"error","error":{"message":"Something exploded","type":"server_error"}}',
|
|
);
|
|
expect(formatAssistantErrorText(msg)).toBe(
|
|
"The AI service returned an error. Please try again.",
|
|
);
|
|
});
|
|
});
|