惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园 - 叶小钗
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
S
SegmentFault 最新的问题
D
DataBreaches.Net
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
F
Fortinet All Blogs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Martin Fowler
Martin Fowler
雷峰网
雷峰网
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
Refactor realtime voice turn context tracking (#86650) · ...
steipete · 2026-05-26 · via Recent Commits to openclaw:main

@@ -7,6 +7,7 @@ import {

77

controlRealtimeVoiceAgentRun,

88

createRealtimeVoiceAgentTalkbackQueue,

99

createRealtimeVoiceBridgeSession,

10+

createRealtimeVoiceTurnContextTracker,

1011

matchRealtimeVoiceActivationName,

1112

matchRealtimeVoiceConsultQuestions,

1213

normalizeSupportedRealtimeVoiceActivationName,

@@ -26,6 +27,8 @@ import {

2627

type RealtimeVoiceBridgeSession,

2728

type RealtimeVoiceProviderConfig,

2829

type RealtimeVoiceToolCallEvent,

30+

type RealtimeVoiceTurnContextHandle,

31+

type RealtimeVoiceTurnContextTracker,

2932

sortRealtimeVoiceActivationNames,

3033

type RealtimeVoiceActivationNameTranscriptResult,

3134

} from "openclaw/plugin-sdk/realtime-voice";

@@ -94,18 +97,18 @@ type DiscordRealtimeSpeakerContext = VoiceRealtimeSpeakerContext & { userId: str

94979598

type DiscordRealtimeVoiceConfig = NonNullable<DiscordAccountConfig["voice"]>["realtime"];

969997-

type PendingSpeakerTurn = {

98-

context: DiscordRealtimeSpeakerContext;

99-

hasAudio: boolean;

100+

type PendingSpeakerTurnStats = {

100101

inputDiscordBytes: number;

101102

inputRealtimeBytes: number;

102103

inputChunks: number;

103104

interruptedPlayback: boolean;

104-

closed: boolean;

105-

startedAt: number;

106-

lastAudioAt?: number;

107105

};

108106107+

type PendingSpeakerTurn = RealtimeVoiceTurnContextHandle<

108+

DiscordRealtimeSpeakerContext,

109+

PendingSpeakerTurnStats

110+

>;

111+109112

type PendingAgentProxyConsultContext = {

110113

context: DiscordRealtimeSpeakerContext;

111114

question: string;

@@ -126,11 +129,6 @@ type RecentAgentProxyConsultContext = {

126129

result?: RecentAgentProxyConsultResult;

127130

};

128131129-

type RecentIgnoredWakeNameSpeakerContext = {

130-

context: DiscordRealtimeSpeakerContext;

131-

createdAt: number;

132-

};

133-134132

function formatRealtimeLogPreview(text: string): string {

135133

const oneLine = text.replace(/\s+/g, " ").trim();

136134

if (oneLine.length <= DISCORD_REALTIME_LOG_PREVIEW_CHARS) {

@@ -366,8 +364,15 @@ export class DiscordRealtimeVoiceSession implements VoiceRealtimeSession {

366364

private wakeNames: string[] = [];

367365

private pendingAgentProxyConsultContexts: PendingAgentProxyConsultContext[] = [];

368366

private recentAgentProxyConsultContexts: RecentAgentProxyConsultContext[] = [];

369-

private recentIgnoredWakeNameSpeakerContext: RecentIgnoredWakeNameSpeakerContext | undefined;

370-

private readonly pendingSpeakerTurns: PendingSpeakerTurn[] = [];

367+

private readonly speakerTurns: RealtimeVoiceTurnContextTracker<

368+

DiscordRealtimeSpeakerContext,

369+

PendingSpeakerTurnStats

370+

> = createRealtimeVoiceTurnContextTracker<DiscordRealtimeSpeakerContext, PendingSpeakerTurnStats>(

371+

{

372+

limit: DISCORD_REALTIME_PENDING_SPEAKER_CONTEXT_LIMIT,

373+

ignoredContextTtlMs: DISCORD_REALTIME_IGNORED_WAKE_NAME_CONTEXT_TTL_MS,

374+

},

375+

);

371376

private outputAudioTimestampMs = 0;

372377

private outputAudioDiscordBytes = 0;

373378

private outputAudioRealtimeBytes = 0;

@@ -573,8 +578,7 @@ export class DiscordRealtimeVoiceSession implements VoiceRealtimeSession {

573578

this.clearForcedConsultTimers();

574579

this.pendingAgentProxyConsultContexts = [];

575580

this.recentAgentProxyConsultContexts = [];

576-

this.recentIgnoredWakeNameSpeakerContext = undefined;

577-

this.pendingSpeakerTurns.length = 0;

581+

this.speakerTurns.clear();

578582

this.queuedExactSpeechMessages = [];

579583

this.exactSpeechResponseActive = false;

580584

this.exactSpeechAudioStarted = false;

@@ -613,29 +617,25 @@ export class DiscordRealtimeVoiceSession implements VoiceRealtimeSession {

613617614618

beginSpeakerTurn(context: VoiceRealtimeSpeakerContext, userId: string): VoiceRealtimeSpeakerTurn {

615619

this.resetPartialWakeNameTracking();

616-

const turn: PendingSpeakerTurn = {

617-

context: { ...context, userId },

618-

hasAudio: false,

619-

inputDiscordBytes: 0,

620-

inputRealtimeBytes: 0,

621-

inputChunks: 0,

622-

interruptedPlayback: false,

623-

closed: false,

624-

startedAt: Date.now(),

625-

};

626-

this.pendingSpeakerTurns.push(turn);

620+

const turn = this.speakerTurns.open(

621+

{ ...context, userId },

622+

{

623+

inputDiscordBytes: 0,

624+

inputRealtimeBytes: 0,

625+

inputChunks: 0,

626+

interruptedPlayback: false,

627+

},

628+

);

627629

logger.info(

628-

`discord voice: realtime speaker turn opened guild=${this.params.entry.guildId} channel=${this.params.entry.channelId} user=${userId} speaker=${context.speakerLabel} owner=${context.senderIsOwner} pendingTurns=${this.pendingSpeakerTurns.length}`,

630+

`discord voice: realtime speaker turn opened guild=${this.params.entry.guildId} channel=${this.params.entry.channelId} user=${userId} speaker=${context.speakerLabel} owner=${context.senderIsOwner} pendingTurns=${this.speakerTurns.size()}`,

629631

);

630-

this.prunePendingSpeakerTurns();

631632

return {

632633

sendInputAudio: (discordPcm48kStereo) =>

633634

this.sendInputAudioForTurn(turn, discordPcm48kStereo),

634635

close: () => {

635636

this.sendRealtimeTrailingSilenceForTurn(turn);

636637

this.logSpeakerTurnClosed(turn);

637-

turn.closed = true;

638-

this.prunePendingSpeakerTurns();

638+

this.speakerTurns.close(turn);

639639

},

640640

};

641641

}

@@ -644,13 +644,12 @@ export class DiscordRealtimeVoiceSession implements VoiceRealtimeSession {

644644

if (!this.bridge || this.stopped) {

645645

return;

646646

}

647-

turn.hasAudio = true;

647+

this.speakerTurns.markAudio(turn);

648648

const realtimePcm = convertDiscordPcm48kStereoToRealtimePcm24kMono(discordPcm48kStereo);

649649

if (realtimePcm.length > 0) {

650650

turn.inputDiscordBytes += discordPcm48kStereo.length;

651651

turn.inputRealtimeBytes += realtimePcm.length;

652652

turn.inputChunks += 1;

653-

turn.lastAudioAt = Date.now();

654653

if (turn.inputChunks === 1) {

655654

logger.info(

656655

`discord voice: realtime input audio started guild=${this.params.entry.guildId} channel=${this.params.entry.channelId} user=${turn.context.userId} speaker=${turn.context.speakerLabel} discordBytes=${discordPcm48kStereo.length} realtimeBytes=${realtimePcm.length} outputAudioMs=${Math.floor(this.outputAudioTimestampMs)} outputActive=${this.isOutputAudioActive()}`,

@@ -1456,79 +1455,25 @@ export class DiscordRealtimeVoiceSession implements VoiceRealtimeSession {

14561455

}

1457145614581457

private consumePendingSpeakerContext(): DiscordRealtimeSpeakerContext | undefined {

1459-

this.prunePendingSpeakerTurns();

1460-

this.expireClosedSpeakerTurnsBeforeLaterAudio();

1461-

const index = this.pendingSpeakerTurns.findIndex((turn) => turn.hasAudio);

1462-

if (index < 0) {

1463-

return undefined;

1464-

}

1465-

const [turn] = this.pendingSpeakerTurns.splice(index, 1);

1466-

this.prunePendingSpeakerTurns();

1467-

return turn?.context;

1458+

return this.speakerTurns.consumeAudioContext();

14681459

}

1469146014701461

private rememberIgnoredWakeNameSpeakerContext(

14711462

context: DiscordRealtimeSpeakerContext | undefined,

14721463

): void {

1473-

if (!context) {

1474-

return;

1475-

}

1476-

this.recentIgnoredWakeNameSpeakerContext = {

1477-

context,

1478-

createdAt: Date.now(),

1479-

};

1464+

this.speakerTurns.rememberIgnoredContext(context);

14801465

}

1481146614821467

private consumeRecentIgnoredWakeNameSpeakerContext(): DiscordRealtimeSpeakerContext | undefined {

1483-

const recent = this.recentIgnoredWakeNameSpeakerContext;

1484-

this.recentIgnoredWakeNameSpeakerContext = undefined;

1485-

if (

1486-

!recent ||

1487-

Date.now() - recent.createdAt > DISCORD_REALTIME_IGNORED_WAKE_NAME_CONTEXT_TTL_MS

1488-

) {

1489-

return undefined;

1490-

}

1491-

return recent.context;

1468+

return this.speakerTurns.consumeIgnoredContext();

14921469

}

1493147014941471

private peekPendingSpeakerTurn(): PendingSpeakerTurn | undefined {

1495-

this.prunePendingSpeakerTurns();

1496-

this.expireClosedSpeakerTurnsBeforeLaterAudio();

1497-

return this.pendingSpeakerTurns.find((turn) => turn.hasAudio);

1472+

return this.speakerTurns.peekAudioTurn();

14981473

}

1499147415001475

private hasPendingSpeakerAudioContext(): boolean {

1501-

this.prunePendingSpeakerTurns();

1502-

this.expireClosedSpeakerTurnsBeforeLaterAudio();

1503-

return this.pendingSpeakerTurns.some((turn) => turn.hasAudio);

1504-

}

1505-1506-

private prunePendingSpeakerTurns(): void {

1507-

for (let index = this.pendingSpeakerTurns.length - 1; index >= 0; index -= 1) {

1508-

const turn = this.pendingSpeakerTurns[index];

1509-

if (turn?.closed && !turn.hasAudio) {

1510-

this.pendingSpeakerTurns.splice(index, 1);

1511-

}

1512-

}

1513-

while (this.pendingSpeakerTurns.length > DISCORD_REALTIME_PENDING_SPEAKER_CONTEXT_LIMIT) {

1514-

const completedIndex = this.pendingSpeakerTurns.findIndex((turn) => turn.closed);

1515-

this.pendingSpeakerTurns.splice(Math.max(completedIndex, 0), 1);

1516-

}

1517-

}

1518-1519-

private expireClosedSpeakerTurnsBeforeLaterAudio(): void {

1520-

let hasLaterAudio = false;

1521-

for (let index = this.pendingSpeakerTurns.length - 1; index >= 0; index -= 1) {

1522-

const turn = this.pendingSpeakerTurns[index];

1523-

if (!turn?.hasAudio) {

1524-

continue;

1525-

}

1526-

if (turn.closed && hasLaterAudio) {

1527-

this.pendingSpeakerTurns.splice(index, 1);

1528-

continue;

1529-

}

1530-

hasLaterAudio = true;

1531-

}

1476+

return this.speakerTurns.hasAudioContext();

15321477

}

1533147815341479

private rememberRecentAgentProxyConsultContext(