zcode-api-retry-status.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import type { ZCodeApiRetryStatus } from "./zcode-task-types-core.js";
  2. export function normalizeZCodeApiRetryStatus(
  3. value: unknown,
  4. ): ZCodeApiRetryStatus | null | undefined {
  5. if (value === null) {
  6. return null;
  7. }
  8. const record = asRecord(value);
  9. if (Object.keys(record).length === 0) {
  10. return undefined;
  11. }
  12. const attempt =
  13. positiveIntegerValue(record.attempt) ??
  14. Math.max((positiveIntegerValue(record.nextAttempt) ?? 2) - 1, 1);
  15. const maxRetries = Math.max(
  16. nonNegativeIntegerValue(record.maxRetries) ??
  17. (positiveIntegerValue(record.maxAttempts) ?? attempt + 1) - 1,
  18. attempt,
  19. );
  20. return {
  21. kind: "api_retry",
  22. attempt,
  23. maxRetries,
  24. retryDelayMs:
  25. nonNegativeIntegerValue(record.retryDelayMs) ?? nonNegativeIntegerValue(record.delayMs) ?? 0,
  26. errorStatus:
  27. nonNegativeIntegerValue(record.errorStatus) ??
  28. nonNegativeIntegerValue(record.statusCode) ??
  29. null,
  30. error:
  31. stringValue(record.error) ??
  32. stringValue(record.message) ??
  33. stringValue(record.reason) ??
  34. "Model retry scheduled",
  35. };
  36. }
  37. export function zcodeApiRetryFromModelNetworkStatusPayload(
  38. payload: Record<string, unknown>,
  39. ): ZCodeApiRetryStatus | null | undefined {
  40. const type = stringValue(payload.type);
  41. if (type === "model_retry_scheduled") {
  42. return normalizeZCodeApiRetryStatus(payload);
  43. }
  44. if (type === "model_request_started") {
  45. const streamRecoveryRetry = zcodeApiRetryFromStreamRecoveryPayload(payload.streamRecovery);
  46. if (streamRecoveryRetry !== undefined) {
  47. return streamRecoveryRetry;
  48. }
  49. if ((positiveIntegerValue(payload.attempt) ?? 1) <= 1) {
  50. return null;
  51. }
  52. // 普通 adapter retry 的 request_started 只代表下一次请求开始,
  53. // 不代表已经恢复成功;这里保持 undefined,让投影层等首个有效模型进展再清理重试态。
  54. }
  55. if (type === "model_request_completed") {
  56. return null;
  57. }
  58. if (type === "model_request_failed" && payload.retryable !== true) {
  59. return null;
  60. }
  61. return undefined;
  62. }
  63. export function isZCodeModelRetryRecoveryProgressPayload(
  64. payload: Record<string, unknown>,
  65. ): boolean {
  66. const kind = stringValue(payload.kind);
  67. if (kind === "text_delta" || kind === "reasoning_delta") {
  68. return Boolean(stringValue(payload.delta));
  69. }
  70. const toolCallId = stringValue(payload.toolCallId);
  71. if (!toolCallId) {
  72. return false;
  73. }
  74. if (kind === "tool_input_start" || kind === "tool_input_end" || kind === "tool_call") {
  75. return true;
  76. }
  77. if (kind === "tool_input_delta") {
  78. return Boolean(stringValue(payload.delta));
  79. }
  80. return false;
  81. }
  82. export function zcodeApiRetryFromStreamRecoveryPayload(
  83. value: unknown,
  84. ): ZCodeApiRetryStatus | undefined {
  85. const record = asRecord(value);
  86. const attempt = positiveIntegerValue(record.retryNumber);
  87. if (attempt === undefined) {
  88. return undefined;
  89. }
  90. // core stream recovery 每次新请求都是 adapter attempt=1,
  91. // 旧 UI 会误清空重试状态;这里改用 streamRecovery.retryNumber 展示 1/10、2/10。
  92. return {
  93. kind: "api_retry",
  94. attempt,
  95. maxRetries: Math.max(nonNegativeIntegerValue(record.maxRetries) ?? attempt, attempt),
  96. retryDelayMs: 0,
  97. errorStatus: null,
  98. error: stringValue(record.message) ?? "Model stream recovery retry started",
  99. };
  100. }
  101. function asRecord(value: unknown): Record<string, unknown> {
  102. return typeof value === "object" && value !== null && !Array.isArray(value)
  103. ? (value as Record<string, unknown>)
  104. : {};
  105. }
  106. function stringValue(value: unknown): string | undefined {
  107. return typeof value === "string" ? value : undefined;
  108. }
  109. function nonNegativeIntegerValue(value: unknown): number | undefined {
  110. return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : undefined;
  111. }
  112. function positiveIntegerValue(value: unknown): number | undefined {
  113. return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
  114. }