Skip to content

Commit fcd2a78

Browse files
authored
fix(auto-reply): transcribe locked harness audio (#108714)
1 parent ccda544 commit fcd2a78

4 files changed

Lines changed: 165 additions & 20 deletions

File tree

‎src/auto-reply/reply/get-reply.message-hooks.test.ts‎

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ describe("getReplyFromConfig message hooks", () => {
249249
);
250250
});
251251

252-
it("skips utility media understanding for a model-locked harness session", async () => {
253-
const sessionKey = "agent:main:harness:codex:supervision:locked-media";
252+
it("runs configured audio transcription for a model-locked harness voice note", async () => {
253+
const sessionKey = "agent:main:harness:claude-cli:locked-audio";
254254
const sessionEntry = {
255255
sessionId: "locked-session",
256256
updatedAt: 1,
257-
agentHarnessId: "codex",
257+
agentHarnessId: "claude-cli",
258258
modelSelectionLocked: true,
259259
};
260260
mocks.resolveReplySessionPreprocessingState.mockReturnValueOnce({
@@ -276,26 +276,100 @@ describe("getReplyFromConfig message hooks", () => {
276276
await getReplyFromConfig(
277277
buildCtx({ SessionKey: sessionKey }),
278278
undefined,
279-
withFastReplyConfig({}),
279+
withFastReplyConfig({
280+
tools: {
281+
media: {
282+
audio: {
283+
enabled: true,
284+
models: [
285+
{
286+
type: "cli",
287+
command: "/usr/local/bin/stt-transcribe",
288+
args: ["{{MediaPath}}"],
289+
},
290+
],
291+
},
292+
},
293+
},
294+
}),
280295
);
281296

282297
expect(mocks.resolveReplySessionPreprocessingState).toHaveBeenCalledOnce();
283298
expect(mocks.initSessionState).toHaveBeenCalledOnce();
284-
expect(mocks.applyMediaUnderstanding).not.toHaveBeenCalled();
299+
expect(mocks.applyMediaUnderstanding).toHaveBeenCalledOnce();
300+
expect(mocks.applyMediaUnderstanding.mock.calls[0]?.[0]).toEqual(
301+
expect.objectContaining({ processingMode: "audio-only" }),
302+
);
285303
expect(mocks.resolveReplyDirectives.mock.calls[0]?.[0]).toEqual(
286304
expect.objectContaining({
287305
sessionEntry: expect.objectContaining({
288-
agentHarnessId: "codex",
306+
agentHarnessId: "claude-cli",
289307
modelSelectionLocked: true,
290308
}),
291-
sessionCtx: expect.objectContaining({
292-
BodyForAgent: "<media:audio>",
309+
ctx: expect.objectContaining({
310+
BodyForAgent: "[Audio]\nTranscript:\nvoice transcript",
293311
SessionKey: sessionKey,
294312
}),
295313
}),
296314
);
297315
});
298316

317+
it("runs normal media understanding for an unlocked voice note", async () => {
318+
await getReplyFromConfig(
319+
buildCtx(),
320+
undefined,
321+
withFastReplyConfig({
322+
tools: {
323+
media: {
324+
audio: {
325+
enabled: true,
326+
models: [
327+
{
328+
type: "cli",
329+
command: "/usr/local/bin/stt-transcribe",
330+
args: ["{{MediaPath}}"],
331+
},
332+
],
333+
},
334+
},
335+
},
336+
}),
337+
);
338+
339+
expect(mocks.applyMediaUnderstanding).toHaveBeenCalledOnce();
340+
expect(mocks.applyMediaUnderstanding.mock.calls[0]?.[0]).not.toHaveProperty("processingMode");
341+
expect(mocks.resolveReplyDirectives.mock.calls[0]?.[0]).toEqual(
342+
expect.objectContaining({
343+
ctx: expect.objectContaining({
344+
BodyForAgent: "[Audio]\nTranscript:\nvoice transcript",
345+
}),
346+
}),
347+
);
348+
});
349+
350+
it("keeps unconfigured audio with a model-locked harness", async () => {
351+
const sessionKey = "agent:main:harness:claude-cli:locked-unconfigured-audio";
352+
const sessionEntry = {
353+
sessionId: "locked-unconfigured-session",
354+
updatedAt: 1,
355+
agentHarnessId: "claude-cli",
356+
modelSelectionLocked: true,
357+
};
358+
mocks.resolveReplySessionPreprocessingState.mockReturnValueOnce({
359+
sessionEntry,
360+
sessionKey,
361+
storePath: "/tmp/sessions.json",
362+
});
363+
364+
await getReplyFromConfig(
365+
buildCtx({ SessionKey: sessionKey }),
366+
undefined,
367+
withFastReplyConfig({}),
368+
);
369+
370+
expect(mocks.applyMediaUnderstanding).not.toHaveBeenCalled();
371+
});
372+
299373
it("skips utility link understanding for a model-locked harness session", async () => {
300374
const sessionKey = "agent:main:harness:codex:supervision:locked-link";
301375
const body = "read https://example.test/page";

‎src/auto-reply/reply/get-reply.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ import type {
5959
ReplySessionBinding,
6060
} from "./get-reply.types.js";
6161
import { finalizeInboundContext } from "./inbound-context.js";
62-
import { hasInboundMedia, hasInboundMediaForUnderstanding } from "./inbound-media.js";
62+
import {
63+
hasInboundAudio,
64+
hasInboundMedia,
65+
hasInboundMediaForUnderstanding,
66+
} from "./inbound-media.js";
6367
import { emitPreAgentMessageHooks } from "./message-preprocess-hooks.js";
6468
import { createFastTestModelSelectionState, createModelSelectionState } from "./model-selection.js";
6569
import { sanitizePendingFinalDeliveryText } from "./pending-final-delivery.js";
@@ -168,6 +172,7 @@ async function applyMediaUnderstandingIfNeeded(params: {
168172
agentDir?: string;
169173
workspaceDir?: string;
170174
activeModel: { provider: string; model: string };
175+
processingMode?: "audio-only";
171176
}): Promise<ApplyMediaUnderstandingResult | undefined> {
172177
if (!hasInboundMediaForUnderstanding(params.ctx)) {
173178
return undefined;
@@ -184,6 +189,11 @@ async function applyMediaUnderstandingIfNeeded(params: {
184189
}
185190
}
186191

192+
function hasExplicitAudioUnderstandingConfig(cfg: OpenClawConfig): boolean {
193+
const audio = cfg.tools?.media?.audio;
194+
return audio !== undefined && audio.enabled !== false;
195+
}
196+
187197
function withExtractedFileImages(
188198
opts: RuntimeInternalGetReplyOptions | undefined,
189199
extractedFileImages: ExtractedFileImage[] | undefined,
@@ -434,9 +444,13 @@ export async function getReplyFromConfig(
434444
const utilityModelSelectionLocked = isModelSelectionLocked(preprocessingState?.sessionEntry);
435445

436446
if (mediaUnderstandingRequested) {
437-
// A durable native-harness lock owns attachment and link interpretation. The
438-
// harness receives raw inputs, so unrelated utility models stay outside the turn.
439-
if (!utilityModelSelectionLocked) {
447+
const shouldApplyLockedAudio =
448+
utilityModelSelectionLocked &&
449+
hasInboundAudio(finalized) &&
450+
hasExplicitAudioUnderstandingConfig(cfg);
451+
// Native harnesses own image, video, and file interpretation. They cannot
452+
// transcribe audio, so an explicitly configured STT pipeline still runs alone.
453+
if (!utilityModelSelectionLocked || shouldApplyLockedAudio) {
440454
const mediaResult = await traceGetReplyPhase("reply.apply_media_understanding", () =>
441455
applyMediaUnderstandingIfNeeded({
442456
ctx: finalized,
@@ -445,6 +459,7 @@ export async function getReplyFromConfig(
445459
agentDir,
446460
workspaceDir,
447461
activeModel: { provider, model },
462+
...(shouldApplyLockedAudio ? { processingMode: "audio-only" as const } : {}),
448463
}),
449464
);
450465
if (mediaResult?.extractedFileImages.length) {

‎src/media-understanding/apply.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,55 @@ describe("applyMediaUnderstanding", () => {
15011501
expect(ctx.BodyForCommands).toBe("audio ok");
15021502
});
15031503

1504+
it("limits native-harness preprocessing to audio", async () => {
1505+
const dir = await createTempMediaDir();
1506+
const imagePath = path.join(dir, "photo.jpg");
1507+
const audioPath = path.join(dir, "note.ogg");
1508+
const filePath = path.join(dir, "notes.txt");
1509+
await fs.writeFile(imagePath, "image-bytes");
1510+
await fs.writeFile(audioPath, createSafeAudioFixtureBuffer(2048));
1511+
await fs.writeFile(filePath, "file text");
1512+
1513+
const describeImage = vi.fn(async () => ({ text: "image ok" }));
1514+
const transcribeAudio = vi.fn(async () => ({ text: "audio ok" }));
1515+
const ctx: MsgContext = {
1516+
Body: "<media:mixed>",
1517+
MediaPaths: [imagePath, audioPath, filePath],
1518+
MediaTypes: ["image/jpeg", "audio/ogg", "text/plain"],
1519+
};
1520+
const cfg: OpenClawConfig = {
1521+
tools: {
1522+
media: {
1523+
image: { enabled: true, models: [{ provider: "openai", model: "gpt-5.4" }] },
1524+
audio: { enabled: true, models: [{ provider: "groq" }] },
1525+
},
1526+
},
1527+
};
1528+
1529+
const result = await applyMediaUnderstanding({
1530+
ctx,
1531+
cfg,
1532+
processingMode: "audio-only",
1533+
providers: {
1534+
openai: { id: "openai", describeImage },
1535+
groq: { id: "groq", transcribeAudio },
1536+
},
1537+
});
1538+
1539+
expect(describeImage).not.toHaveBeenCalled();
1540+
expect(transcribeAudio).toHaveBeenCalledOnce();
1541+
expect(result).toEqual(
1542+
expect.objectContaining({
1543+
appliedImage: false,
1544+
appliedAudio: true,
1545+
appliedVideo: false,
1546+
appliedFile: false,
1547+
extractedFileImages: [],
1548+
}),
1549+
);
1550+
expect(ctx.Body).toBe("[Audio]\nTranscript:\naudio ok");
1551+
});
1552+
15041553
it("orders synthetic too-small audio output between image and video", async () => {
15051554
const dir = await createTempMediaDir();
15061555
const imagePath = path.join(dir, "photo.jpg");

‎src/media-understanding/apply.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type ApplyMediaUnderstandingResult = {
5353
};
5454

5555
const CAPABILITY_ORDER: MediaUnderstandingCapability[] = ["image", "audio", "video"];
56+
const AUDIO_ONLY_CAPABILITY_ORDER: MediaUnderstandingCapability[] = ["audio"];
5657
const EMPTY_VOICE_NOTE_PLACEHOLDER =
5758
"[Voice note could not be transcribed because the audio attachment was too small]";
5859
const EXTRA_TEXT_MIMES = [
@@ -538,6 +539,8 @@ export async function applyMediaUnderstanding(params: {
538539
workspaceDir?: string;
539540
providers?: Record<string, MediaUnderstandingProvider>;
540541
activeModel?: ActiveMediaModel;
542+
/** Preserve native-harness ownership of image, video, and file inputs while applying STT. */
543+
processingMode?: "audio-only";
541544
}): Promise<ApplyMediaUnderstandingResult> {
542545
const { ctx, cfg } = params;
543546
const mediaWorkspaceDir = ctx.MediaWorkspaceDir ?? params.workspaceDir;
@@ -561,7 +564,7 @@ export async function applyMediaUnderstanding(params: {
561564

562565
try {
563566
const results = await pMap(
564-
CAPABILITY_ORDER,
567+
params.processingMode === "audio-only" ? AUDIO_ONLY_CAPABILITY_ORDER : CAPABILITY_ORDER,
565568
async (capability) =>
566569
await runMediaCapability({
567570
capability,
@@ -686,13 +689,17 @@ export async function applyMediaUnderstanding(params: {
686689
)
687690
.map((output) => output.attachmentIndex),
688691
);
689-
const fileContext = await extractFileContext({
690-
attachments,
691-
cache,
692-
cfg,
693-
limits: resolveFileExtractionLimits(cfg),
694-
skipAttachmentIndexes: audioAttachmentIndexes.size > 0 ? audioAttachmentIndexes : undefined,
695-
});
692+
const fileContext =
693+
params.processingMode === "audio-only"
694+
? { blocks: [], images: [] }
695+
: await extractFileContext({
696+
attachments,
697+
cache,
698+
cfg,
699+
limits: resolveFileExtractionLimits(cfg),
700+
skipAttachmentIndexes:
701+
audioAttachmentIndexes.size > 0 ? audioAttachmentIndexes : undefined,
702+
});
696703
if (fileContext.blocks.length > 0) {
697704
ctx.Body = appendFileBlocks(ctx.Body, fileContext.blocks);
698705
}

0 commit comments

Comments
 (0)