errors.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. function isRecord(value: unknown): value is Record<string, unknown> {
  2. return typeof value === "object" && value !== null;
  3. }
  4. function normalizeErrorCode(value: unknown): string | undefined {
  5. if (typeof value === "string" || typeof value === "number" || typeof value === "bigint") {
  6. return String(value);
  7. }
  8. return undefined;
  9. }
  10. function getErrorCandidate(error: unknown): unknown {
  11. if (!isRecord(error) || !("error" in error) || !isRecord(error.error)) {
  12. return error;
  13. }
  14. if ("message" in error.error || "code" in error.error) {
  15. return error.error;
  16. }
  17. return error;
  18. }
  19. export interface NormalizedUnknownError {
  20. message: string;
  21. code?: string;
  22. }
  23. export const ZCODE_FILE_LOCK_TIMEOUT_ERROR_CODE = "ZCODE_FILE_LOCK_TIMEOUT" as const;
  24. export function stringifyUnknownValue(value: unknown): string {
  25. if (typeof value === "string") {
  26. return value;
  27. }
  28. if (value === null) {
  29. return "null";
  30. }
  31. if (value === undefined) {
  32. return "undefined";
  33. }
  34. if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
  35. return String(value);
  36. }
  37. try {
  38. const serialized = JSON.stringify(value);
  39. if (serialized !== undefined) {
  40. return serialized;
  41. }
  42. } catch {
  43. // 某些协议错误对象会带循环引用。
  44. // 这里吞掉 JSON 序列化异常,避免在展示原始错误时再制造第二个错误。
  45. }
  46. return String(value);
  47. }
  48. export function normalizeUnknownError(error: unknown): NormalizedUnknownError {
  49. const candidate = getErrorCandidate(error);
  50. if (candidate instanceof Error) {
  51. const errorWithCode = candidate as Error & { code?: unknown };
  52. return {
  53. // 有些运行时 Error.message 可能为空字符串。
  54. // 这里按 message -> name -> String 的顺序兜底,确保前端始终能拿到可展示的文本。
  55. message: candidate.message || candidate.name || String(candidate),
  56. code: normalizeErrorCode(errorWithCode.code),
  57. };
  58. }
  59. if (isRecord(candidate)) {
  60. const code = "code" in candidate ? normalizeErrorCode(candidate.code) : undefined;
  61. const message =
  62. "message" in candidate
  63. ? stringifyUnknownValue(candidate.message)
  64. : stringifyUnknownValue(candidate);
  65. return {
  66. message:
  67. message !== "undefined" && message.length > 0 ? message : stringifyUnknownValue(candidate),
  68. code,
  69. };
  70. }
  71. return {
  72. message: stringifyUnknownValue(candidate),
  73. };
  74. }
  75. export function isZCodeFileLockTimeoutError(error: unknown): boolean {
  76. return normalizeUnknownError(error).code === ZCODE_FILE_LOCK_TIMEOUT_ERROR_CODE;
  77. }
  78. /**
  79. * 不可自动重试的 workspace ZCode Agent 预热错误模式列表。
  80. *
  81. * 这些错误代表确定性的配置/安装/鉴权问题,继续重试不会变好。
  82. * 同时用于 MCP-fallback 降级判断:命中这些模式时不做 MCP 降级,因为错误与 MCP 无关。
  83. */
  84. export const NON_RETRYABLE_WORKSPACE_PREPARE_ERROR_PATTERNS = [
  85. "binary 未找到",
  86. "未正确安装",
  87. "binary not found",
  88. "not installed",
  89. "initialize 前进程已退出",
  90. "initialize 超时",
  91. "进程启动失败",
  92. "optional dependency was not installed",
  93. "Cannot find package '@openai/codex-",
  94. "Missing optional dependency @openai/codex-",
  95. "Missing Codex runtime files",
  96. "Gemini API key is missing or not configured",
  97. "Please fix the configuration file(s)",
  98. "Expected property name or '}' in JSON",
  99. "已暂停自动重试",
  100. ] as const;
  101. /**
  102. * 额外的不可 MCP-fallback 降级的错误模式。
  103. *
  104. * session/new 超时属于 agent 自身启动/登录卡住,和 MCP 无关,降级也没用。
  105. */
  106. export const NON_MCP_FALLBACK_EXTRA_ERROR_PATTERNS = ["session/new 超时"] as const;
  107. const OPENCODE_RUNTIME_CRASH_ERROR_PATTERNS = [
  108. "opencode runtime 崩溃",
  109. "opencode runtime crashed",
  110. "bun has crashed",
  111. "segmentation fault",
  112. "panic(main thread)",
  113. "code=3221226505",
  114. ] as const;
  115. /**
  116. * 判断错误消息是否代表确定性失败,不应自动重试 workspace ZCode Agent 预热。
  117. */
  118. export function isNonRetryableWorkspacePrepareError(errorMessage: string): boolean {
  119. const normalizedMessage = errorMessage.toLowerCase();
  120. return NON_RETRYABLE_WORKSPACE_PREPARE_ERROR_PATTERNS.some((pattern) =>
  121. normalizedMessage.includes(pattern.toLowerCase()),
  122. );
  123. }
  124. /**
  125. * 判断 OpenCode 是否命中 Bun/运行时崩溃。
  126. *
  127. * 这类错误不同于配置缺失:短时间内重建进程可能恢复,但连续出现时应停止自动重试,
  128. * 避免 UI 只显示泛化的 “connection closed” 并把用户拖进 5/5 的无意义等待。
  129. */
  130. export function isOpenCodeRuntimeCrashError(errorMessage: string): boolean {
  131. const normalizedMessage = errorMessage.toLowerCase();
  132. return OPENCODE_RUNTIME_CRASH_ERROR_PATTERNS.some((pattern) =>
  133. normalizedMessage.includes(pattern.toLowerCase()),
  134. );
  135. }