Skip to content

Commit 6186fc2

Browse files
fix(desktop): keep agent speech live-only
Signed-off-by: John Tennant <[email protected]>
1 parent 8a25584 commit 6186fc2

9 files changed

Lines changed: 52 additions & 95 deletions

File tree

‎desktop/src/features/huddle/lib/ttsLiveMessages.test.mjs‎

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import assert from "node:assert/strict";
22
import test from "node:test";
33

44
import {
5+
classifySpeakableAgentText,
56
createInitialMembershipGate,
67
createLatestStateGate,
78
createOrderedSpeaker,
89
routeLiveAgentText,
9-
speakableAgentText,
1010
} from "./ttsLiveMessages.ts";
1111

1212
const agents = new Set(["agent"]);
@@ -18,59 +18,48 @@ const base = {
1818
content: "Hello there",
1919
tags: [["h", CHANNEL]],
2020
};
21+
const speakableText = (event, selfPubkey = "human") =>
22+
classifySpeakableAgentText(event, agents, selfPubkey, CHANNEL).text;
2123

2224
test("speaks only new agent-authored text message events", () => {
25+
assert.equal(speakableText(base), "Hello there");
2326
assert.equal(
24-
speakableAgentText(base, agents, "human", CHANNEL),
25-
"Hello there",
26-
);
27-
assert.equal(
28-
speakableAgentText({ ...base, kind: 40002 }, agents, "human", CHANNEL),
27+
speakableText({ ...base, kind: 40002 }),
2928
"Hello there",
3029
"managed stream-message-v2 replies are spoken",
3130
);
3231
assert.equal(
33-
speakableAgentText({ ...base, kind: 7 }, agents, "human", CHANNEL),
32+
speakableText({ ...base, kind: 7 }),
3433
null,
3534
"reactions and other event kinds are excluded",
3635
);
3736
assert.equal(
38-
speakableAgentText({ ...base, kind: 10 }, agents, "human", CHANNEL),
37+
speakableText({ ...base, kind: 10 }),
3938
null,
4039
"edits and status events are excluded",
4140
);
4241
assert.equal(
43-
speakableAgentText({ ...base, pubkey: "human" }, agents, "human", CHANNEL),
42+
speakableText({ ...base, pubkey: "human" }),
4443
null,
4544
"human-authored messages are excluded",
4645
);
4746
assert.equal(
48-
speakableAgentText({ ...base, content: " " }, agents, "human", CHANNEL),
47+
speakableText({ ...base, content: " " }),
4948
null,
5049
"empty and non-text content are excluded",
5150
);
5251
assert.equal(
53-
speakableAgentText({ ...base, content: "K" }, agents, "human", CHANNEL),
52+
speakableText({ ...base, content: "K" }),
5453
"K",
5554
"one-character agent text remains speakable",
5655
);
5756
assert.equal(
58-
speakableAgentText(
59-
{ ...base, content: "[System] tool started" },
60-
agents,
61-
"human",
62-
CHANNEL,
63-
),
57+
speakableText({ ...base, content: "[System] tool started" }),
6458
null,
6559
"legacy system rows are excluded",
6660
);
6761
assert.equal(
68-
speakableAgentText(
69-
{ ...base, tags: [["h", "another-huddle"]] },
70-
agents,
71-
"human",
72-
CHANNEL,
73-
),
62+
speakableText({ ...base, tags: [["h", "another-huddle"]] }),
7463
null,
7564
"messages for another huddle are excluded",
7665
);
@@ -134,30 +123,19 @@ test("strips attachment markup and skips attachment-only events", () => {
134123
const url = "https://cdn.example/voice.png";
135124
const tags = [...base.tags, ["imeta", `url ${url}`, "m image/png"]];
136125
assert.equal(
137-
speakableAgentText(
138-
{ ...base, content: `![image](${url})`, tags },
139-
agents,
140-
"human",
141-
CHANNEL,
142-
),
126+
speakableText({ ...base, content: `![image](${url})`, tags }),
143127
null,
144128
);
145129
assert.equal(
146-
speakableAgentText(
147-
{ ...base, content: `Here is the diagram.\n\n![image](${url})`, tags },
148-
agents,
149-
"human",
150-
CHANNEL,
151-
),
130+
speakableText({
131+
...base,
132+
content: `Here is the diagram.\n\n![image](${url})`,
133+
tags,
134+
}),
152135
"Here is the diagram.",
153136
);
154137
assert.equal(
155-
speakableAgentText(
156-
{ ...base, content: `||\n![image](${url})\n||`, tags },
157-
agents,
158-
"human",
159-
CHANNEL,
160-
),
138+
speakableText({ ...base, content: `||\n![image](${url})\n||`, tags }),
161139
null,
162140
);
163141
});

‎desktop/src/features/huddle/lib/ttsLiveMessages.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ export function routeLiveAgentText(
9494
return enqueue(eligibility.text, routeId);
9595
}
9696

97-
export function speakableAgentText(
98-
event: LiveTtsEvent,
99-
agentPubkeys: ReadonlySet<string>,
100-
selfPubkey: string | null,
101-
channelId: string,
102-
): string | null {
103-
return classifySpeakableAgentText(event, agentPubkeys, selfPubkey, channelId)
104-
.text;
105-
}
106-
10797
/**
10898
* Serialize native speak calls so live messages enter the bounded Pocket queue
10999
* in thread arrival order even when the bridge resolves calls asynchronously.

‎desktop/src/features/huddle/lib/useTtsSubscription.ts‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,31 +193,27 @@ export function useTtsSubscription(
193193
const seenOrder: string[] = [];
194194
const MAX_SEEN_EVENTS = 5000;
195195
relayClient
196-
.subscribeLive(
197-
buildHuddleTtsLiveFilter(ephemeralChannelId),
198-
(event) => {
199-
if (disposed) return;
200-
// Dedup by event ID (covers reconnect replay).
201-
if (seenEventIds.has(event.id)) return;
202-
seenEventIds.add(event.id);
203-
seenOrder.push(event.id);
204-
if (seenOrder.length > MAX_SEEN_EVENTS) {
205-
const oldest = seenOrder.shift();
206-
if (oldest !== undefined) seenEventIds.delete(oldest);
207-
}
196+
.subscribeLive(buildHuddleTtsLiveFilter(ephemeralChannelId), (event) => {
197+
if (disposed) return;
198+
// Dedup by event ID if a relay repeats live fan-out.
199+
if (seenEventIds.has(event.id)) return;
200+
seenEventIds.add(event.id);
201+
seenOrder.push(event.id);
202+
if (seenOrder.length > MAX_SEEN_EVENTS) {
203+
const oldest = seenOrder.shift();
204+
if (oldest !== undefined) seenEventIds.delete(oldest);
205+
}
208206

209-
// Preserve arrival order while the initial authoritative membership
210-
// lookup is pending. A failed lookup clears this buffer fail-closed.
211-
const routeId = allocateTtsRouteId();
212-
if (!agentsLoaded) {
213-
console.debug(
214-
`[huddle] tts stage=eligibility status=deferred reason=membership_unavailable route_id=${routeId}`,
215-
);
216-
}
217-
initialMembershipGate.push({ event, routeId });
218-
},
219-
{ replayMissedHistory: true },
220-
)
207+
// Preserve arrival order while the initial authoritative membership
208+
// lookup is pending. A failed lookup clears this buffer fail-closed.
209+
const routeId = allocateTtsRouteId();
210+
if (!agentsLoaded) {
211+
console.debug(
212+
`[huddle] tts stage=eligibility status=deferred reason=membership_unavailable route_id=${routeId}`,
213+
);
214+
}
215+
initialMembershipGate.push({ event, routeId });
216+
})
221217
.then((dispose) => {
222218
if (disposed) {
223219
void dispose();

‎desktop/src/features/settings/ui/VoiceSettingsCard.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function VoiceSettingsCard() {
163163
<SettingsOptionGroup>
164164
<SettingsOptionRow>
165165
<div className="min-w-0">
166-
<p className="text-sm font-medium">Voice</p>
166+
<p className="text-sm font-medium">Pocket TTS voice</p>
167167
<p className="text-sm text-muted-foreground">
168168
Voice files stay private on this device.
169169
</p>
@@ -173,6 +173,7 @@ export function VoiceSettingsCard() {
173173
<DropdownMenu>
174174
<DropdownMenuTrigger asChild>
175175
<Button
176+
aria-label={`Pocket TTS voice: ${selectedVoice?.displayName ?? "Mary"}`}
176177
className="min-w-32 justify-between"
177178
data-testid="pocket-voice-selector"
178179
disabled={controlsDisabled}

‎desktop/src/shared/api/relayClientSession.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,8 @@ export class RelayClient {
430430
async subscribeLive(
431431
filter: RelaySubscriptionFilter,
432432
onEvent: (event: RelayEvent) => void,
433-
options?: { replayMissedHistory?: boolean },
434433
) {
435-
return this.subscribe(filter, onEvent, options);
434+
return this.subscribe(filter, onEvent);
436435
}
437436

438437
async subscribeToChannelMentionEvents(
@@ -586,7 +585,6 @@ export class RelayClient {
586585
private async subscribe(
587586
filter: RelaySubscriptionFilter,
588587
onEvent: (event: RelayEvent) => void,
589-
options?: { replayMissedHistory?: boolean },
590588
) {
591589
await this.ensureConnected();
592590

@@ -608,10 +606,6 @@ export class RelayClient {
608606
mode: "live",
609607
filter,
610608
onEvent,
611-
replayMissedHistory: options?.replayMissedHistory,
612-
lastSeenCreatedAt: options?.replayMissedHistory
613-
? Math.floor(Date.now() / 1_000)
614-
: undefined,
615609
resolveReady,
616610
});
617611

‎desktop/src/shared/api/relayClientShared.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ type LiveSubscription = {
5858
mode: "live";
5959
filter: RelaySubscriptionFilter;
6060
onEvent: (event: RelayEvent) => void;
61-
replayMissedHistory?: boolean;
6261
resolveReady?: () => void;
6362
lastSeenCreatedAt?: number;
6463
closedRetryAttempt?: number;

‎desktop/src/shared/api/relayReconnectReplay.test.mjs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,14 @@ test("reconnect replay preserves the live-only zero-history contract", () => {
129129
});
130130
});
131131

132-
test("missed-history replay is explicit for live-only subscriptions", () => {
132+
test("live-only subscriptions do not page reconnect history", () => {
133133
const filter = {
134134
kinds: [9],
135135
"#h": ["channel-1"],
136136
limit: 0,
137137
};
138138

139139
assert.equal(shouldPageReconnectReplay(filter), false);
140-
assert.equal(shouldPageReconnectReplay(filter, true), true);
141140
});
142141

143142
test("reconnect replay keeps the stricter existing since window", () => {

‎desktop/src/shared/api/relayReconnectReplay.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ export function buildReconnectReplayFilter(
6969
return replayFilter;
7070
}
7171

72-
export function shouldPageReconnectReplay(
73-
filter: RelaySubscriptionFilter,
74-
replayMissedHistory = false,
75-
) {
76-
if (replayMissedHistory) return true;
72+
export function shouldPageReconnectReplay(filter: RelaySubscriptionFilter) {
7773
return (
7874
filter.limit > 0 &&
7975
Array.isArray(filter["#h"]) &&
@@ -180,10 +176,7 @@ export async function replayLiveSubscriptions({
180176
);
181177
const shouldPageReplay =
182178
replaySince !== undefined &&
183-
shouldPageReconnectReplay(
184-
subscription.filter,
185-
subscription.replayMissedHistory,
186-
);
179+
shouldPageReconnectReplay(subscription.filter);
187180

188181
return { subId, subscription, replaySince, shouldPageReplay };
189182
});

‎desktop/tests/e2e/voice-settings.spec.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@ test.describe("Pocket voice settings", () => {
2121
await expect(
2222
page.getByText("Agent text to speech", { exact: true }),
2323
).toBeVisible();
24+
await expect(
25+
page.getByText("Pocket TTS voice", { exact: true }),
26+
).toBeVisible();
27+
await expect(card).not.toContainText("April INT8");
2428

2529
await page.getByTestId("pocket-voice-selector").click();
2630
await expect(page.getByRole("menuitemradio")).toHaveCount(12);
2731
await page.getByRole("menuitemradio", { name: "Eve" }).click();
2832
await expect(page.getByTestId("pocket-voice-selector")).toContainText(
2933
"Eve",
3034
);
35+
await expect(
36+
page.getByRole("button", { name: "Pocket TTS voice: Eve" }),
37+
).toBeVisible();
3138

3239
await page.getByTestId("agent-text-to-speech-toggle").click();
3340
await expect(

0 commit comments

Comments
 (0)