Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

2.3 KB
Raw Blame
import { EndpointServiceError } from "@hypit/hypit/endpoint";
import { EndpointHttpError, retryAfterMs } from "@hypit/hypit/endpoint/http";

/** Pollo's `{ errorCode, message, code, requestId }` envelope and generation `failMsg`, kept at the service boundary. */
function record(value: unknown): Record<string, unknown> | undefined {
  return value !== null && typeof value === "object" && !Array.isArray(value)
    ? value as Record<string, unknown> : undefined;
}
function text(value: unknown): string | undefined {
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
}

// Responses may mention a signed asset URL. Keep the reason, not its access capability.
export function safePolloReason(value: string): string {
  return value.replace(/https?:\/\/\S+/giu, "[redacted-url]");
}

export class PolloServiceError extends EndpointServiceError {}

export class PolloHttpError extends EndpointHttpError {
  constructor(status: number, response: { readonly headers: Headers }, bodyText: string,
    request: { readonly method: string; readonly path: string }) {
    let body: Record<string, unknown> | undefined;
    try { body = record(JSON.parse(bodyText)); } catch { /* Non-JSON gateway failures still have HTTP evidence. */ }
    const code = text(body?.errorCode) ?? "POLLO_HTTP_ERROR";
    const reason = text(body?.message) ?? (body === undefined ? text(bodyText.slice(0, 2000)) : undefined);
    const requestId = text(body?.requestId);
    const facts = [
      `Pollo HTTP ${status}`, code, `${request.method} ${request.path}`,
      ...(requestId === undefined ? [] : [`request=${requestId}`]),
    ];
    super(code, `${facts.join("; ")}${reason === undefined ? "" : `: ${safePolloReason(reason)}`}`,
      status, retryAfterMs(response.headers));
  }
}

/** The first failed generation of a task; `undefined` when none failed. */
export function polloTaskFailure(generations: readonly Record<string, unknown>[], id: string): PolloServiceError | undefined {
  const failed = generations.find((generation) => generation.status === "failed");
  if (failed === undefined) return undefined;
  const reason = text(failed.failMsg);
  return new PolloServiceError("POLLO_TASK_FAILED",
    `Pollo task ${id} failed${reason === undefined ? "" : `: ${safePolloReason(reason)}`}`);
}