fix(speko): restore persona selection and retire interrupted speech

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
DottaandPaperclip committed 2026-10-13 15:10:19 -05:00
1 parent 7f2f5aa262
commit f43733bc75
8 files changed
+60 -8

No files matched your search

@@ -3,6 +3,7 @@ import { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { VoiceCallContext } from "./VoiceCallContext";
import { TaskVoiceLauncher } from "./TaskVoiceLauncher";
import { spekoEndpoint } from "../../../storybook/support/speko/spekoNative";
const mocks = vi.hoisted(() => ({ list: vi.fn() }));
@@ -17,8 +18,8 @@ describe("task voice connection failures", () => {
root = createRoot(container); client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
});
afterEach(async () => { await act(async () => root.unmount()); client.clear(); container.remove(); });
async function render(bound = false) {
await act(async () => root.render(<QueryClientProvider client={client}><TaskVoiceLauncher companyId="company" issueId="issue" agentId={spekoEndpoint.assignedAgentId} boundEndpointId={bound ? spekoEndpoint.id : undefined} /></QueryClientProvider>));
async function render(bound = false, active?: boolean) {
await act(async () => root.render(<QueryClientProvider client={client}><VoiceCallContext.Provider value={active === undefined ? null : { active, open: vi.fn() }}><TaskVoiceLauncher companyId="company" issueId="issue" agentId={spekoEndpoint.assignedAgentId} boundEndpointId={bound ? spekoEndpoint.id : undefined} /></VoiceCallContext.Provider></QueryClientProvider>));
}
it.each([false, true])("shows a retryable request failure for bound=%s instead of a missing connection", async bound => {
mocks.list.mockRejectedValueOnce(new Error("Offline")).mockResolvedValue([spekoEndpoint]); await render(bound);
@@ -30,6 +31,22 @@ describe("task voice connection failures", () => {
expect(container.querySelector('[role="alert"]')).toBeNull();
expect(document.activeElement).toBe(container.querySelector('button'));
});
it("restores persona selection and keyboard focus after the hosted panel closes", async () => {
mocks.list.mockResolvedValue([spekoEndpoint, {...spekoEndpoint, id: "second", botLabel: "Second persona"}]);
await render(false, false);
await vi.waitFor(() => expect(container.querySelector('select')).not.toBeNull());
await act(async () => container.querySelector('button')!.click());
expect(container.querySelector('select')).toBeNull();
await render(false, true);
await render(false, false);
await vi.waitFor(() => expect(container.querySelector('select')).not.toBeNull());
expect(document.activeElement).toBe(container.querySelector('button'));
await act(async () => {
const select = container.querySelector('select')!; select.value = "second";
select.dispatchEvent(new Event("change", {bubbles: true}));
});
expect(container.querySelector('select')?.value).toBe("second");
});
it("keeps active controls mounted when a background connection refresh fails", async () => {
mocks.list.mockResolvedValue([spekoEndpoint]); await render();
await vi.waitFor(() => expect(container.querySelector('button')).not.toBeNull());
@@ -1,20 +1,26 @@
import { useEffect, useRef, useState } from "react";
import { useContext, useEffect, useRef, useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { chatEndpointsApi } from "@/api/chatEndpoints";
import { useChatConnectorsEnabled } from "@/hooks/useChatConnectorsEnabled";
import { Button } from "@/components/ui/button";
import { VoiceCallContext } from "./VoiceCallContext";
import { NativeVoiceConversation } from "./NativeVoiceConversation";
export function TaskVoiceLauncher({ companyId, issueId, agentId, boundEndpointId }: { companyId: string; issueId: string; agentId?: string | null; boundEndpointId?: string }) {
const launcher = useRef<HTMLButtonElement>(null), restoreFocus = useRef(false);
const host = useContext(VoiceCallContext), previouslyActive = useRef(false);
const { enabled } = useChatConnectorsEnabled();
const [open, setOpen] = useState(false), [selected, setSelected] = useState("");
const connections = useQuery({ queryKey: ["task-voice-endpoints", companyId], queryFn: () => chatEndpointsApi.list(companyId), enabled: enabled && Boolean(agentId || boundEndpointId) });
useEffect(() => {
if (previouslyActive.current && !host?.active && open) { setOpen(false); restoreFocus.current = true; }
previouslyActive.current = Boolean(host?.active);
}, [host?.active, open]);
useEffect(() => {
if (restoreFocus.current && !connections.isFetching && !connections.isError && launcher.current) {
restoreFocus.current = false; launcher.current.focus();
}
}, [connections.data, connections.isFetching, connections.isError]);
}, [connections.data, connections.isFetching, connections.isError, open]);
if (!enabled) return null;
const candidates = (connections.data ?? []).filter((endpoint) => endpoint.provider === "speko" && endpoint.status === "active" && (boundEndpointId ? endpoint.id === boundEndpointId : endpoint.assignedAgentId === agentId));
const endpoint = candidates.find((entry) => entry.id === selected) ?? candidates[0];
+1 -1
View File
@@ -16,7 +16,7 @@ export function VoiceTranscript({ entries, agentName, unavailable = false }: Voi
<li key={entry.id} className="flex min-w-0 flex-col gap-1">
<span className="text-xs font-medium text-muted-foreground break-words">
{entry.source === "user" ? "You" : entry.source === "application" ? "Paperclip" : agentName}
{!entry.final && <span> · Speaking</span>}
{!entry.final && !entry.interrupted && <span> · Speaking</span>}
{entry.interrupted && <span> · Interrupted</span>}
</span>
<p className="whitespace-pre-wrap break-words text-sm">{entry.text}</p>