Skip to content

Commit 2888b77

Browse files
committed
fix(media-use): address avatar-video PR review feedback
- heygenVideoGenerate short-circuits after the first discovery-call failure instead of always attempting both avatar list and voice list, so an unauthenticated caller gets one onboarding message and one provider-error telemetry ping instead of a double-fire. - runHeygenJson logs a diagnostic when a CLI call succeeds but returns unparseable JSON, instead of silently returning null. - dedupe the "avatar video is free" onboarding string into one constant (was duplicated across three call sites).
1 parent 9fb140e commit 2888b77

4 files changed

Lines changed: 31 additions & 16 deletions

File tree

‎skills-manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"files": 10
4747
},
4848
"media-use": {
49-
"hash": "60a3e05409c3ed40",
49+
"hash": "328408e847e3d5c6",
5050
"files": 139
5151
},
5252
"motion-graphics": {

‎skills/media-use/scripts/lib/heygen-cli.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export function runHeygenJson(bin, argv, label, onError) {
154154
try {
155155
return JSON.parse(out);
156156
} catch {
157+
console.error(`media-use: \`${bin} ${label}\` returned invalid JSON`);
157158
return null;
158159
}
159160
}

‎skills/media-use/scripts/lib/heygen-video-provider.mjs‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
runHeygenJson,
1111
} from "./heygen-cli.mjs";
1212

13+
const ONBOARDING_MESSAGE = `media-use: avatar video is free for new API users — sign in: ${HEYGEN_AUTH_COMMAND}`;
14+
1315
// Cache only a truthy id -- a transient discovery failure must not poison the
1416
// cache with `null` and permanently disable heygen.video for the rest of the
1517
// process. `onError` lets the caller distinguish not_authenticated (and nudge
@@ -45,14 +47,18 @@ export async function heygenVideoGenerate(intent, ctx) {
4547
const captureReason = (reason) => {
4648
discoveryFailureReason ??= reason;
4749
};
50+
51+
// Short-circuit: once one discovery call fails, the result is null either
52+
// way, so don't attempt the second -- that would double-fire the onboarding
53+
// message and the provider-error telemetry ping for what's really one failure.
4854
const avatarId = ctx?.avatarId || defaultAvatarId(captureReason);
55+
if (!avatarId) {
56+
if (discoveryFailureReason === "not_authenticated") console.error(ONBOARDING_MESSAGE);
57+
return null;
58+
}
4959
const voiceId = ctx?.voiceId || defaultStarfishVoiceId(captureReason);
50-
if (!avatarId || !voiceId) {
51-
if (discoveryFailureReason === "not_authenticated") {
52-
console.error(
53-
`media-use: avatar video is free for new API users — sign in: ${HEYGEN_AUTH_COMMAND}`,
54-
);
55-
}
60+
if (!voiceId) {
61+
if (discoveryFailureReason === "not_authenticated") console.error(ONBOARDING_MESSAGE);
5662
return null;
5763
}
5864

@@ -81,9 +87,7 @@ export async function heygenVideoGenerate(intent, ctx) {
8187
);
8288
} catch (err) {
8389
if (classifyHeygenErrorCode(err) === "not_authenticated") {
84-
console.error(
85-
`media-use: avatar video is free for new API users — sign in: ${HEYGEN_AUTH_COMMAND}`,
86-
);
90+
console.error(ONBOARDING_MESSAGE);
8791
}
8892
reportHeygenFailure(err, "heygen video create");
8993
return null;

‎skills/media-use/scripts/lib/heygen-video-provider.test.mjs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ case "$*" in
8080
*) printf '%s\\n' '{"data":[{"avatar_id":"avatar-public-1"}]}' ;;
8181
esac
8282
;;
83-
*"voice list"*) printf '%s\\n' '{"data":[{"voice_id":"voice-starfish-1"}]}' ;;
83+
*"voice list"*)
84+
case "$HEYGEN_DISCOVERY_MODE" in
85+
auth) printf '%s\\n' 'HTTP 401 Unauthorized' >&2; exit 1 ;;
86+
*) printf '%s\\n' '{"data":[{"voice_id":"voice-starfish-1"}]}' ;;
87+
esac
88+
;;
8489
*"video create"*)
8590
case "$HEYGEN_VIDEO_MODE" in
8691
auth) printf '%s\\n' 'HTTP 401 Unauthorized' >&2; exit 1 ;;
@@ -300,18 +305,23 @@ test("onboards and returns null when avatar/voice discovery itself is unauthenti
300305
const errors = [];
301306
t.mock.method(console, "error", (message) => errors.push(message));
302307

308+
// Both avatar list AND voice list would fail unauthenticated (discoveryMode
309+
// "auth" applies to both in the fake CLI) -- the short-circuit after the
310+
// first failure must mean only one is ever attempted, so the onboarding
311+
// message and the provider-error telemetry ping each fire exactly once
312+
// instead of double-firing for what's really one auth failure.
303313
await withFakeHeygen({ discoveryMode: "auth" }, async ({ invocations }) => {
304314
const heygenVideoGenerate = await freshGenerate();
305315
const result = await heygenVideoGenerate("Discovery auth failure", {});
306316

307317
assert.equal(result, null);
308-
assert.ok(errors.includes(ONBOARDING_MESSAGE));
309-
const calls = invocations();
310318
assert.equal(
311-
calls.filter((call) => call.includes("video create")).length,
312-
0,
313-
"must not attempt video create once discovery is unauthenticated",
319+
errors.filter((message) => message === ONBOARDING_MESSAGE).length,
320+
1,
321+
"onboarding message must fire exactly once, not once per failed discovery call",
314322
);
323+
const calls = invocations();
324+
assert.equal(calls.length, 1, "must short-circuit after the first discovery failure");
315325
assert.match(calls[0], /^avatar list /);
316326
});
317327
});

0 commit comments

Comments
 (0)