Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat: add voice input button to chat using Web Speech API

Gener12d831b200a3
diff --git a/src/web/src/components/agent-chat/agent-chat-view.tsx b/src/web/src/components/agent-chat/agent-chat-view.tsx
index 7713e936b..b05ea837c 100644
--- a/src/web/src/components/agent-chat/agent-chat-view.tsx
+++ b/src/web/src/components/agent-chat/agent-chat-view.tsx
@@ -20,7 +20,8 @@ import { useAgentContext } from "@/contexts/agent-context";
import { toast } from "sonner";
import { cn } from "@/lib/utils";
import { Skeleton } from "@/components/ui/skeleton";
-import { ArrowUp, Box, FileText, Loader2, Paperclip, RotateCcw, X } from "lucide-react";
+import { ArrowUp, Box, FileText, Loader2, Mic, Paperclip, RotateCcw, X } from "lucide-react";
+import { useSpeechRecognition } from "@/hooks/use-speech-recognition";
import { ArtifactSheet, formatSize } from "@/components/agent-chat/artifact-sheet";
import { isPreviewable, getArtifactUrl } from "@/components/artifact-content-renderer";
import {
@@ -172,6 +173,11 @@ export function AgentChatView() {
const pendingFilesMapRef = useRef<Map<string, File[]>>(new Map());
+ const handleSpeechResult = useCallback((text: string) => {
+ setInput((prev) => (prev ? prev + " " + text : text));
+ }, []);
+ const { listening, supported: speechSupported, toggle: toggleSpeech } = useSpeechRecognition(handleSpeechResult);
+
const agentArtifacts = useMemo(() => artifacts.filter((a) => a.source === "agent"), [artifacts]);
const timeline = useMemo(() => buildTimeline(messages, agentArtifacts), [messages, agentArtifacts]);
@@ -881,6 +887,23 @@ export function AgentChatView() {
>
<Paperclip className="size-3.5" />
</Button>
+ {speechSupported && (
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ onClick={toggleSpeech}
+ disabled={sending || isTaskActive}
+ className={cn(
+ "rounded-lg transition-colors duration-200",
+ listening
+ ? "text-red-500 hover:text-red-600 bg-red-500/10"
+ : "text-muted-foreground/60 hover:text-foreground"
+ )}
+ title={listening ? "Stop recording" : "Voice input"}
+ >
+ <Mic className={cn("size-3.5", listening && "animate-pulse")} />
+ </Button>
+ )}
<Button
size="icon-sm"
onClick={handleSend}
diff --git a/src/web/src/hooks/use-speech-recognition.ts b/src/web/src/hooks/use-speech-recognition.ts
new file mode 100644
index 000000000..c340d39ff
--- /dev/null
+++ b/src/web/src/hooks/use-speech-recognition.ts
@@ -0,0 +1,100 @@
+"use client";
+
+import { useCallback, useEffect, useRef, useState } from "react";
+
+interface SpeechRecognitionResult {
+ readonly [index: number]: { transcript: string; confidence: number };
+ readonly length: number;
+ readonly isFinal: boolean;
+}
+
+interface SpeechRecognitionResultList {
+ readonly [index: number]: SpeechRecognitionResult;
+ readonly length: number;
+}
+
+interface SpeechRecognitionEvent {
+ readonly results: SpeechRecognitionResultList;
+}
+
+interface SpeechRecognitionInstance {
+ lang: string;
+ interimResults: boolean;
+ continuous: boolean;
+ onresult: ((event: SpeechRecognitionEvent) => void) | null;
+ onend: (() => void) | null;
+ onerror: (() => void) | null;
+ start(): void;
+ stop(): void;
+ abort(): void;
+}
+
+function getSR(): (new () => SpeechRecognitionInstance) | null {
+ if (typeof window === "undefined") return null;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ const w = window as any;
+ return w.SpeechRecognition ?? w.webkitSpeechRecognition ?? null;
+}
+
+export function useSpeechRecognition(onResult: (text: string) => void) {
+ const [listening, setListening] = useState(false);
+ const [supported, setSupported] = useState(false);
+ const recognitionRef = useRef<SpeechRecognitionInstance | null>(null);
+
+ useEffect(() => {
+ setSupported(!!getSR());
+ }, []);
+
+ const start = useCallback(() => {
+ const SR = getSR();
+ if (!SR) return;
+
+ const recognition = new SR();
+ recognition.lang = navigator.language || "zh-CN";
+ recognition.interimResults = false;
+ recognition.continuous = false;
+
+ recognition.onresult = (event) => {
+ const results = event.results;
+ let transcript = "";
+ for (let i = 0; i < results.length; i++) {
+ transcript += results[i][0].transcript;
+ }
+ if (transcript) onResult(transcript);
+ };
+
+ recognition.onend = () => {
+ setListening(false);
+ recognitionRef.current = null;
+ };
+
+ recognition.onerror = () => {
+ setListening(false);
+ recognitionRef.current = null;
+ };
+
+ recognitionRef.current = recognition;
+ recognition.start();
+ setListening(true);
+ }, [onResult]);
+
+ const stop = useCallback(() => {
+ recognitionRef.current?.stop();
+ }, []);
+
+ const toggle = useCallback(() => {
+ if (listening) {
+ stop();
+ } else {
+ start();
+ }
+ }, [listening, start, stop]);
+
+ useEffect(() => {
+ return () => {
+ recognitionRef.current?.abort();
+ };
+ }, []);
+
+ return { listening, supported, toggle };
+}