fix(cli): a refused reverse start reports whether HeyGen sent the refusal (#8053)

* fix(cli): a refused reverse start reports whether HeyGen sent the refusal

* fix(cli): a refusal body that is not JSON is never read

* refactor(cli): drop the comments the names already say

* refactor(cli): reading a refusal's source lives in one helper
This commit is contained in:
Miguel Ángel
2026-11-17 08:04:58 +00:00
committed by GitHub
parent b29e40d707
commit 53080581cb
3 changed files with 45 additions and 3 deletions
+26 -1
View File
@@ -162,7 +162,32 @@ describe("startReverseSession", () => {
])("reports status %i as %s", async (status, cause) => {
serve({ [START_URL]: () => new Response("no", { status }) });
expect(await startReverseSession()).toMatchObject({ recorded: false, cause });
expect(track).toHaveBeenCalledExactlyOnceWith({ cause, status });
expect(track).toHaveBeenCalledExactlyOnceWith({ cause, status, heygenBody: false });
});
it.each([
["HeyGen's v1 body", { code: 40102, message: "Unauthorized" }, true],
["HeyGen's error body", { data: null, error: { code: "unauthorized", message: "no" } }, true],
["a proxy's JSON", { error: "blocked by policy" }, false],
["a JSON array", [403], false],
])("says whether a refusal came from HeyGen: %s", async (_name, body, heygenBody) => {
serve({ [START_URL]: () => Response.json(body, { status: 403 }) });
await startReverseSession();
expect(track).toHaveBeenCalledExactlyOnceWith({ cause: "http_4xx", status: 403, heygenBody });
});
it("never reads a refusal body that is not JSON, however it looks", async () => {
const page = '{"code": 40102}';
serve({
[START_URL]: () =>
new Response(page, { status: 403, headers: { "content-type": "text/html" } }),
});
await startReverseSession();
expect(track).toHaveBeenCalledExactlyOnceWith({
cause: "http_4xx",
status: 403,
heygenBody: false,
});
});
it("reports a network failure", async () => {
+12 -1
View File
@@ -54,6 +54,15 @@ function parseSession(body: unknown): ReverseSession | undefined {
return { sessionId, plan, gate, ...upgradeUrl };
}
async function refusedByHeygen(res: Response): Promise<boolean> {
if (!res.headers.get("content-type")?.includes("json")) return false;
const body: unknown = await res.json().catch(() => null);
if (!isRecord(body)) return false;
return (
typeof body.code === "number" || (isRecord(body.error) && typeof body.error.code === "string")
);
}
/** The plan from the subscription route, for when the reverse route cannot answer. */
async function fallbackPlan(): Promise<"free" | "pro" | undefined> {
const plan = await readPlan();
@@ -87,6 +96,7 @@ export async function startReverseSession(
}
let cause: ReverseUnrecordedCause;
let status: number | undefined;
let heygenBody: boolean | undefined;
try {
const res = await fetch(`${api2BaseUrl()}/v1/hyperframes/reverse/start`, {
method: "POST",
@@ -104,11 +114,12 @@ export async function startReverseSession(
} else {
status = res.status;
cause = res.status >= 500 ? "http_5xx" : res.status >= 400 ? "http_4xx" : "http_other";
heygenBody = await refusedByHeygen(res);
}
} catch {
cause = "network";
}
trackReverseStartUnrecorded({ cause, status });
trackReverseStartUnrecorded({ cause, status, heygenBody });
const plan = await fallbackPlan().catch(() => undefined);
return { recorded: false, reason: "unavailable", cause, ...(plan ? { plan } : {}) };
}
+7 -1
View File
@@ -1023,8 +1023,14 @@ export type ReverseUnrecordedCause =
export function trackReverseStartUnrecorded(props: {
cause: ReverseUnrecordedCause;
status?: number;
/** An error status only: whether HeyGen's API wrote the body, or something between (a proxy, a firewall). */
heygenBody?: boolean;
}): void {
trackEvent("cli_reverse_start_unrecorded", { cause: props.cause, status: props.status });
trackEvent("cli_reverse_start_unrecorded", {
cause: props.cause,
status: props.status,
heygen_body: props.heygenBody,
});
}
export function trackSkillCommandRan(props: {