app-transcript-errors.ts 818 B

123456789101112131415161718
  1. import type { Message } from "./app-model.js";
  2. const ERROR_MESSAGE_PREFIX = "Error:";
  3. const UNKNOWN_ERROR_MESSAGE = "Unknown error";
  4. export function appendSystemErrorMessage(messages: Message[], message: string): Message[] {
  5. const content = formatSystemErrorMessage(message);
  6. const last = messages.at(-1);
  7. // runtime error events often arrive before the rejected prompt promise;
  8. // dedupe the catch-path render so the transcript shows one clear failure.
  9. if (last?.role === "system" && last.content === content) return messages;
  10. return [...messages, { content, role: "system" }];
  11. }
  12. function formatSystemErrorMessage(message: string): string {
  13. const trimmed = message.trim() || UNKNOWN_ERROR_MESSAGE;
  14. return trimmed.startsWith(ERROR_MESSAGE_PREFIX) ? trimmed : `${ERROR_MESSAGE_PREFIX} ${trimmed}`;
  15. }