Skip to content

Commit 901b622

Browse files
fix(meeting-bot): keep local audio diagnostics readable across chunks (#110703)
* fix(meeting-bot): preserve split UTF-8 diagnostics * fix(meeting-bot): stream decoded stderr diagnostics * test(meeting-bot): type diagnostics monitor callback
1 parent 629e007 commit 901b622

2 files changed

Lines changed: 98 additions & 8 deletions

File tree

‎src/meeting-bot/realtime-local-audio-transport.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EventEmitter } from "node:events";
2+
import { PassThrough } from "node:stream";
23
import { afterEach, describe, expect, it, vi } from "vitest";
34
import { createLocalMeetingRealtimeAudioTransport } from "./realtime-local-audio-transport.js";
45

@@ -34,7 +35,7 @@ function createProcess(params: { stdin?: TestStdin | null; stdout?: EventEmitter
3435
signalCode: null as NodeJS.Signals | null,
3536
stdin: params.stdin ?? null,
3637
stdout: params.stdout ?? null,
37-
stderr: new EventEmitter(),
38+
stderr: new PassThrough(),
3839
kill: vi.fn((signal: NodeJS.Signals = "SIGTERM") => {
3940
proc.signalCode = signal;
4041
queueMicrotask(() => {
@@ -110,4 +111,50 @@ describe("local meeting realtime audio transport", () => {
110111
expect(replacementOutput.stdin?.write).not.toHaveBeenCalled();
111112
await transport.stop();
112113
});
114+
115+
it("preserves split UTF-8 diagnostics and logs complete fragments immediately", async () => {
116+
const processes = new Map<string, ReturnType<typeof createProcess>>();
117+
const debug = vi.fn();
118+
const spawn = vi.fn((command: string) => {
119+
const proc = createProcess({});
120+
processes.set(command, proc);
121+
return proc;
122+
});
123+
const transport = createLocalMeetingRealtimeAudioTransport({
124+
bargeInCooldownMs: 0,
125+
bargeInPeakThreshold: 0,
126+
bargeInRmsThreshold: 0,
127+
inputCommand: ["capture"],
128+
logger: { debug, warn: vi.fn() } as never,
129+
logScope: "[meeting]",
130+
outputCommand: ["play"],
131+
bargeInInputCommand: ["barge"],
132+
spawn: spawn as never,
133+
});
134+
transport.startBargeInMonitor?.(() => false);
135+
136+
for (const [command, label] of [
137+
["play", "audio output"],
138+
["capture", "audio input"],
139+
["barge", "barge-in input"],
140+
] as const) {
141+
const process = processes.get(command);
142+
if (!process) {
143+
throw new Error(`Expected ${command} process`);
144+
}
145+
const diagnostic = `诊断-${command}`;
146+
const line = Buffer.from(`${diagnostic}\n`, "utf8");
147+
const before = debug.mock.calls.length;
148+
process.stderr.write(line.subarray(0, 2));
149+
expect(debug).toHaveBeenCalledTimes(before);
150+
process.stderr.write(line.subarray(2, -1));
151+
expect(debug).toHaveBeenNthCalledWith(before + 1, `[meeting] ${label}: ${diagnostic}`);
152+
process.stderr.write(line.subarray(-1));
153+
154+
process.stderr.write(Buffer.from(`未换行-${command}`, "utf8"));
155+
expect(debug).toHaveBeenNthCalledWith(before + 2, `[meeting] ${label}: 未换行-${command}`);
156+
}
157+
158+
await transport.stop();
159+
});
113160
});

‎src/meeting-bot/realtime-local-audio-transport.ts‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { spawn } from "node:child_process";
2-
import type { Writable } from "node:stream";
2+
import { Readable, type Writable } from "node:stream";
33
import { formatErrorMessage } from "../infra/errors.js";
44
import type { RuntimeLogger } from "../plugins/runtime/types.js";
5+
import { onDecodedOutput } from "../process/decoded-output.js";
56
import { createSpeechThresholdGate, readPcm16AudioStats } from "../talk/audio-energy.js";
7+
import { truncateUtf8Suffix } from "../utils/utf8-truncate.js";
68
import { terminateMeetingBridgeProcess } from "./bridge-process.js";
79
import { createMeetingOutputLoopbackVerifier } from "./output-loopback-verifier.js";
810
import type { MeetingRealtimeAudioFormat } from "./realtime-audio-format.js";
@@ -46,6 +48,9 @@ type MeetingRealtimeAudioSpawn = (
4648
options: { stdio: ["pipe" | "ignore", "pipe" | "ignore", "pipe" | "ignore"] },
4749
) => BridgeProcess;
4850

51+
const STDERR_LINE_TRUNCATED_PREFIX = "[stderr line truncated] ";
52+
const MAX_STDERR_CHUNK_BYTES = 8 * 1024;
53+
4954
type OutputWriteWaiter = {
5055
proc: BridgeProcess;
5156
release: () => void;
@@ -59,6 +64,38 @@ function splitCommand(argv: string[]): { command: string; args: string[] } {
5964
return { command, args };
6065
}
6166

67+
function attachStderrLineLogger(params: {
68+
stderr: BridgeProcess["stderr"];
69+
logger: RuntimeLogger;
70+
prefix: string;
71+
}): void {
72+
if (!params.stderr) {
73+
return;
74+
}
75+
if (!params.logger.debug) {
76+
params.stderr.on("data", () => {});
77+
return;
78+
}
79+
const debug = (message: string) => params.logger.debug?.(message);
80+
if (!(params.stderr instanceof Readable)) {
81+
// Injected adapters do not promise stream completion; retain their
82+
// per-chunk behavior so diagnostics after child exit stay visible.
83+
params.stderr.on("data", (chunk) => {
84+
debug(`${params.prefix}: ${String(chunk).trim()}`);
85+
});
86+
return;
87+
}
88+
onDecodedOutput(params.stderr, (chunk) => {
89+
const trimmed = chunk.trim();
90+
if (!trimmed) {
91+
return;
92+
}
93+
const truncated = Buffer.byteLength(trimmed, "utf8") > MAX_STDERR_CHUNK_BYTES;
94+
const value = truncated ? truncateUtf8Suffix(trimmed, MAX_STDERR_CHUNK_BYTES) : trimmed;
95+
debug(`${params.prefix}: ${truncated ? STDERR_LINE_TRUNCATED_PREFIX : ""}${value}`);
96+
});
97+
}
98+
6299
export function createLocalMeetingRealtimeAudioTransport(params: {
63100
inputCommand: string[];
64101
outputCommand: string[];
@@ -122,8 +159,10 @@ export function createLocalMeetingRealtimeAudioTransport(params: {
122159
signalFatal();
123160
}
124161
});
125-
proc.stderr?.on("data", (chunk) => {
126-
params.logger.debug?.(`${params.logScope} audio output: ${String(chunk).trim()}`);
162+
attachStderrLineLogger({
163+
stderr: proc.stderr,
164+
logger: params.logger,
165+
prefix: `${params.logScope} audio output`,
127166
});
128167
proc.stderr?.on("error", (error: Error) => {
129168
if (proc === outputProcess) {
@@ -175,8 +214,10 @@ export function createLocalMeetingRealtimeAudioTransport(params: {
175214
signalFatal();
176215
}
177216
});
178-
inputProcess.stderr?.on("data", (chunk) => {
179-
params.logger.debug?.(`${params.logScope} audio input: ${String(chunk).trim()}`);
217+
attachStderrLineLogger({
218+
stderr: inputProcess.stderr,
219+
logger: params.logger,
220+
prefix: `${params.logScope} audio input`,
180221
});
181222
inputProcess.stdout?.on("error", fail("audio input command stdout"));
182223
inputProcess.stderr?.on("error", fail("audio input command stderr"));
@@ -308,8 +349,10 @@ export function createLocalMeetingRealtimeAudioTransport(params: {
308349
`${params.logScope} human barge-in input stdout failed: ${formatErrorMessage(error)}`,
309350
);
310351
});
311-
bargeInInputProcess.stderr?.on("data", (chunk) => {
312-
params.logger.debug?.(`${params.logScope} barge-in input: ${String(chunk).trim()}`);
352+
attachStderrLineLogger({
353+
stderr: bargeInInputProcess.stderr,
354+
logger: params.logger,
355+
prefix: `${params.logScope} barge-in input`,
313356
});
314357
bargeInInputProcess.stderr?.on("error", (error: Error) => {
315358
params.logger.warn(

0 commit comments

Comments
 (0)