Skip to content

Commit ffcca75

Browse files
authored
fix(media): fall back when providers return empty output (#118660)
* fix(media): fall back when providers return empty output * test(media): type the synthetic attachment cache fixture * test(media): avoid object spread in fallback test cases
1 parent c547f16 commit ffcca75

2 files changed

Lines changed: 126 additions & 1 deletion

File tree

‎src/media-understanding/runner.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ async function runAttachmentEntries(params: {
782782
config: params.config,
783783
secretOwnerId: candidate.secretOwnerId,
784784
});
785-
if (result) {
785+
if (result?.text) {
786786
const decision = buildModelDecision({ entry, entryType, outcome: "success" });
787787
if (result.provider) {
788788
decision.provider = result.provider;

‎src/media-understanding/runner.video.test.ts‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Video runner tests cover provider request wiring, auth/config precedence, and
22
// provider output handling for video attachments.
33
import { describe, expect, it, vi } from "vitest";
4+
import {
5+
formatAudioTranscripts,
6+
formatMediaUnderstandingBody,
7+
} from "../../packages/media-understanding-common/src/format.js";
48
import type { OpenClawConfig } from "../config/types.js";
59
import { withTempDir } from "../test-helpers/temp-dir.js";
610
import { withEnvAsync } from "../test-utils/env.js";
@@ -465,3 +469,124 @@ describe("runCapability video provider wiring", () => {
465469
expect(firstCall?.modelApi).toBeUndefined();
466470
});
467471
});
472+
473+
describe("runCapability provider output decisions", () => {
474+
const outputs = [
475+
{ label: "empty", text: "" },
476+
{ label: "whitespace", text: " \t\n" },
477+
{ label: "usable", text: " usable primary output " },
478+
] as const;
479+
const cases = (["audio", "video", "image"] as const).flatMap((capability) =>
480+
outputs.flatMap((output) =>
481+
(output.text.trim() ? [true] : [true, false]).map((configureFallback) => ({
482+
capability,
483+
configureFallback,
484+
label: output.label,
485+
text: output.text,
486+
})),
487+
),
488+
);
489+
490+
it.each(cases)(
491+
"handles $label $capability provider output with fallback=$configureFallback",
492+
async ({ capability, configureFallback, text }) => {
493+
const extension = capability === "image" ? "png" : capability === "video" ? "mp4" : "wav";
494+
const mime = `${capability}/${extension}`;
495+
const buffer =
496+
capability === "image"
497+
? Buffer.from(
498+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAuMBg4n8tLwAAAAASUVORK5CYII=",
499+
"base64",
500+
)
501+
: Buffer.alloc(2048, 1);
502+
const primary = vi.fn(async () => ({ text, model: "primary-model" }));
503+
const fallback = vi.fn(async () => ({
504+
text: "usable fallback output",
505+
model: "fallback-model",
506+
}));
507+
const createProvider = (
508+
id: string,
509+
run: () => Promise<{ text: string; model: string }>,
510+
): MediaUnderstandingProvider => ({
511+
id,
512+
capabilities: [capability],
513+
...(capability === "audio"
514+
? { transcribeAudio: run }
515+
: capability === "video"
516+
? { describeVideo: run }
517+
: { describeImage: run }),
518+
});
519+
const providerIds = ["qa-primary", ...(configureFallback ? ["qa-fallback"] : [])];
520+
const cfg = {
521+
models: {
522+
providers: Object.fromEntries(
523+
providerIds.map((provider) => [provider, { apiKey: "test-key", models: [] }]),
524+
),
525+
},
526+
tools: {
527+
media: {
528+
models: providerIds.map((provider) => ({
529+
provider,
530+
model: provider === "qa-primary" ? "primary-model" : "fallback-model",
531+
capabilities: [capability],
532+
})),
533+
[capability]: { enabled: true },
534+
},
535+
},
536+
} as unknown as OpenClawConfig;
537+
538+
const result = await runCapability({
539+
capability,
540+
cfg,
541+
ctx: { Body: "" },
542+
attachments: {
543+
getBuffer: async () => ({
544+
buffer,
545+
mime,
546+
fileName: `fixture.${extension}`,
547+
size: buffer.length,
548+
}),
549+
} as unknown as Parameters<typeof runCapability>[0]["attachments"],
550+
media: [{ index: 0, kind: capability, mime }],
551+
agentDir: "/tmp/openclaw-media-provider-output-test",
552+
providerRegistry: new Map<string, MediaUnderstandingProvider>([
553+
["qa-primary", createProvider("qa-primary", primary)],
554+
["qa-fallback", createProvider("qa-fallback", fallback)],
555+
]),
556+
});
557+
558+
const usablePrimary = text.trim();
559+
const expectedText = usablePrimary || (configureFallback ? "usable fallback output" : "");
560+
const expectedFallbackCalls = !usablePrimary && configureFallback ? 1 : 0;
561+
expect(primary).toHaveBeenCalledOnce();
562+
expect(fallback).toHaveBeenCalledTimes(expectedFallbackCalls);
563+
expect(result.outputs.map((output) => output.text)).toEqual(
564+
expectedText ? [expectedText] : [],
565+
);
566+
expect(result.decision.outcome).toBe(expectedText ? "success" : "skipped");
567+
568+
const attempts = result.decision.attachments[0]?.attempts.map(
569+
({ provider, outcome, reason }) => ({
570+
provider,
571+
outcome,
572+
...(reason ? { reason } : {}),
573+
}),
574+
);
575+
expect(attempts).toEqual([
576+
usablePrimary
577+
? { provider: "qa-primary", outcome: "success" }
578+
: { provider: "qa-primary", outcome: "skipped", reason: "empty output" },
579+
...(expectedFallbackCalls ? [{ provider: "qa-fallback", outcome: "success" }] : []),
580+
]);
581+
582+
if (capability === "audio") {
583+
expect(formatMediaUnderstandingBody({ outputs: result.outputs })).toBe(
584+
expectedText ? `[Audio]\nTranscript:\n${expectedText}` : "",
585+
);
586+
if (expectedText) {
587+
expect(formatAudioTranscripts(result.outputs)).toBe(expectedText);
588+
}
589+
}
590+
},
591+
);
592+
});

0 commit comments

Comments
 (0)