Skip to content

Commit 18f31f6

Browse files
harjothkharavincentkocPeter Steinberger
authored
fix(media-understanding): resolve inbound media:// refs carried as attachment url (#117268)
* fix(media-understanding): resolve inbound media:// refs carried as attachment url * fix(media-understanding): resolve stored inbound URL aliases Co-authored-by: harjoth <[email protected]> * test(qa): classify stored inbound media proof * test(qa): require exact inbound media completion * fix(media-understanding): preserve local media refs --------- Co-authored-by: Vincent Koc <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 30c87bd commit 18f31f6

9 files changed

Lines changed: 737 additions & 64 deletions

File tree

‎extensions/qa-channel/src/inbound.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Qa Channel tests cover inbound plugin behavior.
22
import path from "node:path";
33
import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers";
4+
import { saveMediaBuffer } from "openclaw/plugin-sdk/media-runtime";
45
import { loadOutboundMediaFromUrl } from "openclaw/plugin-sdk/outbound-media";
56
import { beforeEach, describe, expect, it, vi } from "vitest";
67
import { setQaChannelRuntime } from "../api.js";
@@ -33,6 +34,15 @@ vi.mock("openclaw/plugin-sdk/outbound-media", async (importOriginal) => {
3334
};
3435
});
3536

37+
vi.mock("openclaw/plugin-sdk/media-runtime", async (importOriginal) => ({
38+
...(await importOriginal<typeof import("openclaw/plugin-sdk/media-runtime")>()),
39+
saveMediaBuffer: vi.fn(async () => ({
40+
id: "stored-audio.ogg",
41+
path: "/tmp/openclaw-media/stored-audio.ogg",
42+
contentType: "audio/ogg",
43+
})),
44+
}));
45+
3646
type HandleQaInboundParams = Parameters<typeof handleQaInbound>[0];
3747

3848
function createQaInboundParams(
@@ -510,6 +520,37 @@ describe("handleQaInbound", () => {
510520
expect(ctxPayload.media?.every((fact) => fact.path === undefined)).toBe(true);
511521
});
512522

523+
it("projects saved inline attachments through a media-store URL", async () => {
524+
const runtime = createPluginRuntimeMock();
525+
setQaChannelRuntime(runtime);
526+
527+
await handleQaInbound(
528+
createQaInboundParams({
529+
message: {
530+
attachments: [
531+
{
532+
id: "audio-1",
533+
kind: "audio",
534+
mimeType: "audio/ogg",
535+
fileName: "voice-note.ogg",
536+
contentBase64: Buffer.alloc(2048, 0x52).toString("base64"),
537+
mediaFactCarrier: "media-store-url",
538+
},
539+
],
540+
},
541+
}),
542+
);
543+
544+
expect(saveMediaBuffer).toHaveBeenCalledOnce();
545+
const media = firstRunAssembledParams(runtime).ctxPayload.media;
546+
expect(media).toHaveLength(1);
547+
expect(media?.[0]).toMatchObject({
548+
path: undefined,
549+
url: "media://inbound/stored-audio.ogg",
550+
contentType: "audio/ogg",
551+
});
552+
});
553+
513554
it("rejects non-http attachment URLs without dropping the message", async () => {
514555
const runtime = createPluginRuntimeMock();
515556
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);

‎extensions/qa-channel/src/inbound.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async function resolveQaInboundMediaFacts(attachments: QaBusMessage["attachments
5353
if (!Array.isArray(attachments) || attachments.length === 0) {
5454
return [];
5555
}
56-
const mediaList: Array<{ path: string; contentType?: string | null }> = [];
56+
const mediaList: Array<{ path?: string; url?: string; contentType?: string | null }> = [];
5757
for (const attachment of attachments) {
5858
if (!attachment?.mimeType) {
5959
continue;
@@ -71,10 +71,17 @@ async function resolveQaInboundMediaFacts(attachments: QaBusMessage["attachments
7171
undefined,
7272
attachment.fileName,
7373
);
74-
mediaList.push({
75-
path: saved.path,
76-
contentType: saved.contentType,
77-
});
74+
mediaList.push(
75+
attachment.mediaFactCarrier === "media-store-url"
76+
? {
77+
url: `media://inbound/${saved.id}`,
78+
contentType: saved.contentType,
79+
}
80+
: {
81+
path: saved.path,
82+
contentType: saved.contentType,
83+
},
84+
);
7885
continue;
7986
}
8087
if (typeof attachment.url === "string" && attachment.url.trim()) {

‎extensions/qa-lab/src/scenario-catalog-channels.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ describe("qa scenario catalog channel contracts", () => {
5959
expect(readQaScenarioById("memory-tools-channel-context").execution.channel).toBe("qa-channel");
6060
});
6161

62+
it("keeps stored inbound audio proof on the real QA Channel and Gateway flow", () => {
63+
const scenario = requireFlowScenario(
64+
readQaScenarioById("inbound-media-store-audio-transcription"),
65+
);
66+
const flow = JSON.stringify(scenario.execution.flow);
67+
68+
expect(scenario.coverage?.primary).toEqual(["media.inbound-media-store"]);
69+
expect(scenario.coverage?.secondary).toEqual(["channels.inbound-media-normalization"]);
70+
expect(scenario.execution.channel).toBe("qa-channel");
71+
expect(scenario.execution.providerMode).toBe("mock-openai");
72+
expect(flow).toContain('"sendInbound"');
73+
expect(flow).toContain('"contentBase64"');
74+
expect(flow).toContain('"mediaFactCarrier":"media-store-url"');
75+
expect(flow).toContain("String(candidate.text ?? '').trim() === config.expectedMarker");
76+
expect(flow).toContain("String(message.text ?? '').trim() === config.expectedMarker");
77+
expect(flow).toContain("conversationOutbound.length === 1");
78+
expect(flow).not.toContain(".includes(config.expectedMarker)");
79+
expect(scenario.gatewayConfigPatch).toMatchObject({
80+
tools: { media: { audio: { echoTranscript: false } } },
81+
});
82+
expect(flow).not.toContain('"call":"runAgentPrompt"');
83+
});
84+
6285
it("marks live transport modules as live-driver-only", () => {
6386
for (const scenarioId of [
6487
"matrix-approval-exec-metadata-single-event",
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
title: Inbound media-store audio transcription
2+
3+
scenario:
4+
id: inbound-media-store-audio-transcription
5+
surface: media-understanding
6+
coverage:
7+
primary:
8+
- media.inbound-media-store
9+
secondary:
10+
- channels.inbound-media-normalization
11+
objective: Verify a stored inbound audio attachment carried only by media:// reaches transcription and produces one visible reply.
12+
gatewayConfigPatch:
13+
tools:
14+
media:
15+
models:
16+
- provider: openai
17+
model: gpt-4o-transcribe
18+
capabilities: [audio]
19+
audio:
20+
enabled: true
21+
echoTranscript: false
22+
successCriteria:
23+
- QA Channel saves inline audio and emits only its media://inbound identity.
24+
- Media understanding resolves the local store reference without remote fetch failure.
25+
- The mock transcription instruction produces exactly one visible marker reply.
26+
codeRefs:
27+
- extensions/qa-channel/src/inbound.ts
28+
- src/media-understanding/attachments.cache.ts
29+
execution:
30+
kind: flow
31+
channel: qa-channel
32+
providerMode: mock-openai
33+
suiteIsolation: isolated
34+
timeoutMs: 90000
35+
retryCount: 0
36+
config:
37+
conversationId: qa-media-store-audio
38+
expectedMarker: WHATSAPP_QA_AUDIO_TRANSCRIPT_OK
39+
40+
flow:
41+
steps:
42+
- name: transcribes URL-only stored inbound audio
43+
actions:
44+
- assert:
45+
expr: env.providerMode === 'mock-openai'
46+
message: this seeded scenario is mock-openai only
47+
- call: waitForGatewayHealthy
48+
args:
49+
- ref: env
50+
- 60000
51+
- call: waitForQaChannelReady
52+
args:
53+
- ref: env
54+
- 60000
55+
- call: reset
56+
- set: logCursor
57+
value:
58+
expr: markGatewayLogCursor()
59+
- set: audioBase64
60+
value:
61+
expr: Buffer.alloc(2048, 0x52).toString('base64')
62+
- sendInbound:
63+
conversation:
64+
id:
65+
expr: config.conversationId
66+
kind: direct
67+
senderId: alice
68+
senderName: Alice
69+
text: Transcribe the attached voice note and follow its instruction.
70+
attachments:
71+
- id: stored-voice-note
72+
kind: audio
73+
mimeType: audio/wav
74+
fileName: stored-voice-note.wav
75+
mediaFactCarrier: media-store-url
76+
contentBase64:
77+
ref: audioBase64
78+
- call: waitForOutboundMessage
79+
saveAs: outbound
80+
args:
81+
- ref: state
82+
- lambda:
83+
params: [candidate]
84+
expr: "candidate.conversation.id === config.conversationId && String(candidate.text ?? '').trim() === config.expectedMarker"
85+
- expr: liveTurnTimeoutMs(env, 60000)
86+
- set: conversationOutbound
87+
value:
88+
expr: "state.getSnapshot().messages.filter((message) => message.direction === 'outbound' && message.conversation.id === config.conversationId)"
89+
- set: matchingOutbound
90+
value:
91+
expr: "conversationOutbound.filter((message) => String(message.text ?? '').trim() === config.expectedMarker)"
92+
- set: gatewayLog
93+
value:
94+
expr: "String(readGatewayLogs() ?? '').slice(logCursor)"
95+
- assert:
96+
expr: conversationOutbound.length === 1 && matchingOutbound.length === 1
97+
message:
98+
expr: "`expected exactly one outbound transcription marker reply, saw ${conversationOutbound.length} messages and ${matchingOutbound.length} marker replies`"
99+
- assert:
100+
expr: "!gatewayLog.includes('MediaFetchError') && !gatewayLog.includes('audio: failed')"
101+
message:
102+
expr: "`stored inbound audio failed before transcription: ${gatewayLog}`"
103+
detailsExpr: "`reply=${outbound.text}; markerReplies=${matchingOutbound.length}`"

‎src/media-understanding/apply.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { OpenClawConfig } from "../config/types.js";
1818
import { logVerbose, shouldLogVerbose } from "../globals.js";
1919
import { renderFileContextBlock } from "../media/file-context.js";
2020
import { extractFileContentFromSource, normalizeMimeType } from "../media/input-files.js";
21+
import { classifyMediaReferenceSource } from "../media/media-reference.js";
2122
import { wrapExternalContent } from "../security/external-content.js";
2223
import { runMediaCapability } from "./apply-capability.js";
2324
import { resolveAttachmentKind } from "./attachments.js";
@@ -412,7 +413,12 @@ async function extractFileContext(params: {
412413
if (!forcedTextMime && (kind === "image" || kind === "video" || kind === "audio")) {
413414
continue;
414415
}
415-
if (!limits.allowUrl && attachment.url && !attachment.path) {
416+
if (
417+
!limits.allowUrl &&
418+
attachment.url &&
419+
!attachment.path &&
420+
!classifyMediaReferenceSource(attachment.url).isMediaStoreUrl
421+
) {
416422
if (shouldLogVerbose()) {
417423
logVerbose(`media: file attachment skipped (url disabled) index=${attachment.index}`);
418424
}

0 commit comments

Comments
 (0)