A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(ui): keep an unsent message, and recheck voice after the model changes
Alan Buscagliaffbca1db3abc
diff --git a/packages/ui/src/components/Composer.tsx b/packages/ui/src/components/Composer.tsxindex 49b1e5b..33b1e02 100644--- a/packages/ui/src/components/Composer.tsx+++ b/packages/ui/src/components/Composer.tsx@@ -29,6 +29,8 @@ export function Composer({attachments,}: ComposerProps) {const [text, setText] = useState("");+ /** The last message could not go out; its text and files stay for another try (#21). */+ const [refused, setRefused] = useState(false);const box = useRef<HTMLTextAreaElement>(null);const recording = voice?.phase === "recording";const voiceStatus = voice?.phase === "transcribing" ? "Transcribing…" : voice?.note;@@ -62,7 +64,12 @@ export function Composer({// Files upload on send; a failed one keeps the message so sending again retries it.const refs = files.length > 0 ? await attachments?.upload() : undefined;if (files.length > 0 && !refs) return;- send({ type: "send", text: value, requestId: newRequestId(), ...(refs ? { attachments: refs } : {}) });+ const message = { type: "send", text: value, requestId: newRequestId() } as const;+ if (send(refs ? { ...message, attachments: refs } : message) === false) {+ setRefused(true);+ return;+ }+ setRefused(false);attachments?.clear();voice?.sent();setText("");@@ -86,6 +93,11 @@ export function Composer({return (<>+ {refused ? (+ <p className="voice-status" role="alert" aria-label="Not sent">+ Not connected, so the message was not sent. It is kept here to send again.+ </p>+ ) : null}{voiceStatus ? (<p className="voice-status" role="status" aria-label="Voice">{voiceStatus}diff --git a/packages/ui/src/components/types.ts b/packages/ui/src/components/types.tsindex 88ce358..93a78bc 100644--- a/packages/ui/src/components/types.ts+++ b/packages/ui/src/components/types.ts@@ -1,6 +1,8 @@import type { ClientMessage } from "@gentle-dot/protocol";-export type Send = (message: ClientMessage) => void;+/** Sends to the daemon; `false` when the message could not be handed to an open connection. */+// biome-ignore lint/suspicious/noConfusingVoidType: a sender that reports nothing (tests, wrappers) counts as sent.+export type Send = (message: ClientMessage) => boolean | void;export function newRequestId(): string {return crypto.randomUUID();diff --git a/packages/ui/src/useDot.ts b/packages/ui/src/useDot.tsindex be54fa6..a209de9 100644--- a/packages/ui/src/useDot.ts+++ b/packages/ui/src/useDot.ts@@ -19,18 +19,19 @@ export function useDot(info: ConnectionInfo | undefined) {return () => c.stop();}, [info]);- const send = useCallback((message: ClientMessage) => {- if (!client.current?.send(message)) {- dispatch({- type: "server",- message: {- type: "error",- code: "offline",- message: "Not connected yet. Try again in a moment.",- seq: 0,- },- });- }+ /** True once the message is handed to an open connection; false (with a notice) otherwise. */+ const send = useCallback((message: ClientMessage): boolean => {+ if (client.current?.send(message)) return true;+ dispatch({+ type: "server",+ message: {+ type: "error",+ code: "offline",+ message: "Not connected yet. Try again in a moment.",+ seq: 0,+ },+ });+ return false;}, []);const dismiss = useCallback((id: number) => dispatch({ type: "dismiss", id }), []);diff --git a/packages/ui/src/useVoice.ts b/packages/ui/src/useVoice.tsindex 7d9f777..516b7a9 100644--- a/packages/ui/src/useVoice.ts+++ b/packages/ui/src/useVoice.ts@@ -10,6 +10,7 @@ import {type NativeVoiceStatus,nativeVoiceStatus,onNativeVoice,+ onVoiceModel,startNativeVoice,startRefusal,stopNativeVoice,@@ -369,6 +370,22 @@ export function useVoice(state: DotState, send: Send): VoiceControls {};}, [desktop, finish]);+ // Installing or removing the local model changes what the recognizer can do (#25).+ useEffect(() => {+ if (!desktop) return;+ let live = true;+ const off = onVoiceModel((event) => {+ if (event.state !== "installed" && event.state !== "removed") return;+ void nativeVoiceStatus().then((status) => {+ if (live) setNative(status);+ });+ }).catch(() => () => {});+ return () => {+ live = false;+ void off.then((stopListening) => stopListening());+ };+ }, [desktop]);+// Answers to voice requests.useEffect(() => {for (const reply of state.voiceReplies) {diff --git a/packages/ui/src/voice-native.ts b/packages/ui/src/voice-native.tsindex 4e17c0b..8f28714 100644--- a/packages/ui/src/voice-native.ts+++ b/packages/ui/src/voice-native.ts@@ -37,6 +37,13 @@ export function cancelNativeVoice(): Promise<void> {return invoke<void>("voice_cancel");}+/** One shared load of the event module (under Vitest mocks, a second concurrent import got the real one). */+let events: Promise<typeof import("@tauri-apps/api/event")> | undefined;+const tauriEvents = () => {+ events ??= import("@tauri-apps/api/event");+ return events;+};+export interface NativeVoiceHandlers {partial: (text: string) => void;level: (level: number) => void;@@ -45,7 +52,7 @@ export interface NativeVoiceHandlers {/** Listens to the recognizer's events; resolves with a function that stops listening. */export async function onNativeVoice(handlers: NativeVoiceHandlers): Promise<() => void> {- const { listen } = await import("@tauri-apps/api/event");+ const { listen } = await tauriEvents();const stops = await Promise.all([listen<{ text?: unknown }>("voice://partial", (event) => {if (typeof event.payload?.text === "string") handlers.partial(event.payload.text);@@ -99,7 +106,7 @@ export const removeVoiceModel = () => invoke<void>("voice_model_remove");/** Listens to the model's download and install progress. */export async function onVoiceModel(handler: (event: VoiceModelEvent) => void): Promise<() => void> {- const { listen } = await import("@tauri-apps/api/event");+ const { listen } = await tauriEvents();return listen<VoiceModelEvent>("voice://model", (event) => {if (event.payload && typeof event.payload.state === "string") handler(event.payload);});diff --git a/packages/ui/test/attachments.test.tsx b/packages/ui/test/attachments.test.tsxindex 5f609d4..cc7fc0b 100644--- a/packages/ui/test/attachments.test.tsx+++ b/packages/ui/test/attachments.test.tsx@@ -5,6 +5,7 @@ import { describe, expect, it, vi } from "vitest";import { ChatSurface } from "../src/components/ChatSurface.tsx";import { Composer } from "../src/components/Composer.tsx";import { MessageList } from "../src/components/MessageList.tsx";+import type { Send } from "../src/components/types.ts";import { type DotState, initialState, reduce } from "../src/store.ts";import { createUploader, formatSize, type Uploader, uploadBase } from "../src/uploads.ts";import { useAttachments } from "../src/useAttachments.ts";@@ -33,7 +34,7 @@ function instantUploader() {});}-function ComposerWith({ upload, send }: { upload: Uploader; send: (m: ClientMessage) => void }) {+function ComposerWith({ upload, send }: { upload: Uploader; send: Send }) {const attachments = useAttachments(upload);return <Composer busy={false} disabled={false} send={send} attachments={attachments} />;}@@ -81,6 +82,41 @@ describe("attaching files in the composer", () => {expect(screen.getByRole("textbox", { name: "Message" })).toHaveValue("");});+ it("Composer keeps the draft and attachments when send is refused", async () => {+ const upload = instantUploader();+ const send = vi.fn<(m: ClientMessage) => boolean>(() => false);+ render(<ComposerWith upload={upload} send={send} />);+ await userEvent.upload(attachInput(), [file("a.txt")]);+ const box = screen.getByRole("textbox", { name: "Message" });+ await userEvent.type(box, "read this{Enter}");+ await waitFor(() => expect(send).toHaveBeenCalledTimes(1));+ expect(box).toHaveValue("read this");+ expect(chips().map((c) => c.textContent)).toEqual([expect.stringContaining("a.txt")]);+ expect(screen.getByRole("alert", { name: "Not sent" })).toHaveTextContent("Not connected");++ // Sending again reuses the uploaded file and clears once the message goes out.+ send.mockReturnValue(true);+ await userEvent.click(screen.getByRole("button", { name: "Send" }));+ await waitFor(() => expect(send).toHaveBeenCalledTimes(2));+ expect(upload).toHaveBeenCalledTimes(1);+ expect(send.mock.calls[1]?.[0]).toMatchObject({ text: "read this", attachments: [{ name: "a.txt" }] });+ expect(box).toHaveValue("");+ expect(screen.queryByRole("list", { name: "Files to send" })).toBeNull();+ expect(screen.queryByRole("alert", { name: "Not sent" })).toBeNull();+ });++ it("a sent message still clears the composer", async () => {+ const send = vi.fn<(m: ClientMessage) => boolean>(() => true);+ render(<ComposerWith upload={instantUploader()} send={send} />);+ await userEvent.upload(attachInput(), [file("a.txt")]);+ const box = screen.getByRole("textbox", { name: "Message" });+ await userEvent.type(box, "read this{Enter}");+ await waitFor(() => expect(send).toHaveBeenCalledTimes(1));+ expect(box).toHaveValue("");+ expect(screen.queryByRole("list", { name: "Files to send" })).toBeNull();+ expect(screen.queryByRole("alert", { name: "Not sent" })).toBeNull();+ });+it("sends files without text", async () => {const send = vi.fn<(m: ClientMessage) => void>();render(<ComposerWith upload={instantUploader()} send={send} />);diff --git a/packages/ui/test/client.test.ts b/packages/ui/test/client.test.tsindex da45116..248dcc1 100644--- a/packages/ui/test/client.test.ts+++ b/packages/ui/test/client.test.ts@@ -1,7 +1,9 @@import type { ServerMessage } from "@gentle-dot/protocol";+import { act, renderHook } from "@testing-library/react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { DotClient, tokenFromLocation } from "../src/client.ts";import type { ConnectionStatus } from "../src/store.ts";+import { useDot } from "../src/useDot.ts";class FakeSocket {static all: FakeSocket[] = [];@@ -124,3 +126,32 @@ describe("tokenFromLocation", () => {expect(again).toBe("abc");});});++describe("useDot.send", () => {+ afterEach(() => vi.unstubAllGlobals());++ it("reports whether the message was handed to an open connection", () => {+ vi.stubGlobal("WebSocket", FakeSocket);+ const info = { url: "ws://127.0.0.1:4317/ws", token: "secret" };+ const { result } = renderHook(() => useDot(info));+ const message = { type: "send", text: "hi", requestId: "r1" } as const;++ let handed: boolean | undefined;+ act(() => {+ handed = result.current.send(message);+ });+ expect(handed).toBe(false);+ expect(result.current.state.notices.at(-1)?.message).toBe("Not connected yet. Try again in a moment.");++ const socket = FakeSocket.all[0];+ act(() => {+ socket?.open();+ socket?.receive({ type: "ready", agentState: "idle" });+ });+ act(() => {+ handed = result.current.send(message);+ });+ expect(handed).toBe(true);+ expect(socket?.sent.at(-1)).toEqual(message);+ });+});diff --git a/packages/ui/test/voice.test.tsx b/packages/ui/test/voice.test.tsxindex 61d6e8a..81b72e7 100644--- a/packages/ui/test/voice.test.tsx+++ b/packages/ui/test/voice.test.tsx@@ -99,7 +99,9 @@ function Harness({ initial }: { initial: DotState }) {<ChatSurfacevariant="panel"state={state}- send={(m) => sent.push(m)}+ send={(m) => {+ sent.push(m);+ }}dismiss={() => {}}dispatch={dispatch}/>@@ -294,6 +296,30 @@ describe("talking on the desktop", () => {expect(invoked("voice_stop")).toHaveLength(1);});+ it("useVoice re-checks voice status when the local model is installed or removed", async () => {+ const noModel = { available: false, reason: "Download the local voice model in Connectors." };+ let status: unknown = noModel;+ h.tauri = true;+ h.invoke = vi.fn(async (command: string) => (command === "voice_status" ? status : undefined));+ render(<Harness initial={withVoice(false)} />);+ await waitFor(() => expect(invoked("voice_status")).toHaveLength(1));+ await waitFor(() => expect(h.listeners.has("voice://model")).toBe(true));+ expect(talk()).toBeDisabled();++ // Progress alone does not change what the engine can do.+ emit("voice://model", { state: "downloading", received: 1, total: 2 });+ expect(invoked("voice_status")).toHaveLength(1);++ status = { available: true };+ emit("voice://model", { state: "installed" });+ await waitFor(() => expect(talk()).toBeEnabled());++ status = noModel;+ emit("voice://model", { state: "removed" });+ await waitFor(() => expect(talk()).toBeDisabled());+ expect(invoked("voice_status")).toHaveLength(3);+ });+it("listens in the language chosen in Settings instead of the system's", async () => {localStorage.setItem("gentle-dot-voice-language", "es-ES");desktop({ available: true });