diff --git a/ui/src/components/voice/SpekoProviderSetup.test.tsx b/ui/src/components/voice/SpekoProviderSetup.test.tsx new file mode 100644 index 00000000000..b60e61a3955 --- /dev/null +++ b/ui/src/components/voice/SpekoProviderSetup.test.tsx @@ -0,0 +1,20 @@ +// @vitest-environment jsdom +import {act} from "react"; +import {createRoot} from "react-dom/client"; +import {expect, it, vi} from "vitest"; +import {SpekoProviderSetup} from "./SpekoProviderSetup"; +it("recovers interrupted setup with a signing secret, and preserves saved secrets on reconnect", async () => { + const onConnect = vi.fn(), onChange = vi.fn(); + const props = {agentName: "Agent", callbackUrl: "https://test.invalid/callback", repairing: true, + credentials: {apiKey: "synthetic", agentId: "agent_fixture"}, onConnect, onChange}; + const container = document.createElement("div"), root = createRoot(container); + document.body.append(container); + try { + await act(async () => root.render()); + await act(async () => (container.querySelector("button:last-child") as HTMLButtonElement).click()); + expect(onConnect).toHaveBeenLastCalledWith(expect.objectContaining({signingSecret: expect.stringMatching(/^whsec_/)})); + await act(async () => root.render()); + await act(async () => (container.querySelector("button:last-child") as HTMLButtonElement).click()); + expect(onConnect).toHaveBeenLastCalledWith(props.credentials); + } finally { await act(async () => root.unmount()); container.remove(); } +}); diff --git a/ui/src/components/voice/SpekoProviderSetup.tsx b/ui/src/components/voice/SpekoProviderSetup.tsx index a1aaacd8273..69d6828309a 100644 --- a/ui/src/components/voice/SpekoProviderSetup.tsx +++ b/ui/src/components/voice/SpekoProviderSetup.tsx @@ -11,14 +11,17 @@ export interface SpekoProviderSetupProps { callbackUrl?: string | null; pending?: boolean; repairing?: boolean; + /** Presence only; existing callback secrets never reach the browser. */ + signingSecretConfigured?: boolean; } -export function SpekoProviderSetup({ agentName, onSaveExit, credentials, onChange, onConnect, callbackUrl, pending = false, repairing = false }: SpekoProviderSetupProps) { - const ready = repairing || Boolean(credentials.apiKey?.trim() && credentials.agentId?.trim()); +export function SpekoProviderSetup({ agentName, onSaveExit, credentials, onChange, onConnect, callbackUrl, pending = false, repairing = false, signingSecretConfigured }: SpekoProviderSetupProps) { + const hasSavedSecret = signingSecretConfigured ?? repairing; + const ready = repairing && hasSavedSecret || Boolean(credentials.apiKey?.trim() && credentials.agentId?.trim()); const reachable = callbackUrl?.startsWith("https://") === true; function connect() { if (!ready || !reachable || pending) return; const values = Object.fromEntries(Object.entries(credentials).filter(([, value]) => value.trim())); - if (!repairing && !values.signingSecret) { + if (!hasSavedSecret && !values.signingSecret) { const bytes = crypto.getRandomValues(new Uint8Array(32)); values.signingSecret = `whsec_${btoa(String.fromCharCode(...bytes))}`; onChange(values); diff --git a/ui/src/pages/apps/chat/ChatEndpointSetup.tsx b/ui/src/pages/apps/chat/ChatEndpointSetup.tsx index 19e864d0bf6..47a6dc67701 100644 --- a/ui/src/pages/apps/chat/ChatEndpointSetup.tsx +++ b/ui/src/pages/apps/chat/ChatEndpointSetup.tsx @@ -898,7 +898,7 @@ function ProviderConnectStep({ null, 2, ); - if (provider === "speko") return navigate("/apps")} agentName={agentName} credentials={credentials} onChange={setCredentials} onConnect={(values) => onAction(repairing ? "reconnect" : "configure", values)} callbackUrl={endpoint.setup?.webhookUrl} pending={pending} repairing={repairing} />; + if (provider === "speko") return navigate("/apps")} agentName={agentName} credentials={credentials} onChange={setCredentials} onConnect={(values) => onAction(repairing ? "reconnect" : "configure", values)} callbackUrl={endpoint.setup?.webhookUrl} pending={pending} repairing={repairing} signingSecretConfigured={endpoint.setup?.webhookSecretConfigured} />; if (provider === "imessage-photon") return ; if (provider === "discord") { const applicationId = credentials.applicationId?.trim() ?? ""; diff --git a/ui/storybook/stories/speko-provider-setup.stories.tsx b/ui/storybook/stories/speko-provider-setup.stories.tsx index 1b1cfba4697..69a7022aed9 100644 --- a/ui/storybook/stories/speko-provider-setup.stories.tsx +++ b/ui/storybook/stories/speko-provider-setup.stories.tsx @@ -6,7 +6,7 @@ import { voiceStoryLifecycle } from "../fixtures/voiceStoryLifecycle"; function Fixture(props: Partial) { const [credentials, setCredentials] = useState(props.credentials ?? {}); const [connected, setConnected] = useState(false); - return connected ?

Credentials saved. Continue to the test conversation.

: {}} agentName="Company Phone Agent" callbackUrl="https://paperclip.example/api/voice-webhooks/endpoint/tools" {...props} credentials={credentials} onChange={setCredentials} onConnect={(values) => { if (!props.repairing && !values.signingSecret?.startsWith("whsec_")) throw new Error("Missing signing secret"); setConnected(true); }} />; + return connected ?

Credentials saved. Continue to the test conversation.

: {}} agentName="Company Phone Agent" callbackUrl="https://paperclip.example/api/voice-webhooks/endpoint/tools" {...props} credentials={credentials} onChange={setCredentials} onConnect={(values) => { if ((!props.repairing || props.signingSecretConfigured === false) && !values.signingSecret?.startsWith("whsec_")) throw new Error("Missing signing secret"); setConnected(true); }} />; } const meta: Meta = { ...voiceStoryLifecycle, title: "Connections/Speko/Provider setup", component: SpekoProviderSetup, parameters: { layout: "padded" } }; export default meta; @@ -37,3 +37,16 @@ export const KeyboardValidationAndConnect: Story = { await expect(canvas.getByRole("status")).toHaveTextContent("Credentials saved"); }, }; + +export const InterruptedBeforeCredentials: Story = { + render: () => , + play: async ({canvasElement}) => { + const canvas = within(canvasElement); + const connect = canvas.getByRole("button", {name: "Reconnect Speko"}); + await expect(connect).toBeDisabled(); + await userEvent.type(canvas.getByRole("textbox", {name: "Speko agent ID"}), "agent_fixture"); + await userEvent.type(canvas.getByLabelText("Speko API key"), "synthetic-not-a-key"); + await userEvent.click(connect); + await expect(canvas.getByRole("status")).toHaveTextContent("Credentials saved"); + }, +};