import { useRef, useState } from "react"; import { Link } from "@tanstack/react-router"; import { useServerFn } from "@tanstack/react-start"; import { ArrowUpRight, AudioLines, Loader2, Mic, MicOff, Phone, PhoneOff, ShieldCheck } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useGeminiLive } from "@/hooks/use-gemini-live"; import { useAuth } from "@/hooks/use-auth"; import { securityOverview, scanSecuritySite, startAegisLive } from "@/lib/security.functions"; import { publicHostname } from "@/lib/security-policy"; export function AegisVoice({ onRefresh }: { onRefresh: () => Promise }) { const startSession = useServerFn(startAegisLive); const overview = useServerFn(securityOverview); const scan = useServerFn(scanSecuritySite); const { user } = useAuth(); const [transcript, setTranscript] = useState<{ role: string; text: string }[]>([]); const [pendingScan, setPendingScan] = useState(null); const [handoff, setHandoff] = useState(false); const [scanning, setScanning] = useState(false); const [notice, setNotice] = useState(null); const callVersion = useRef(0); const voice = useGeminiLive({ assistantName: "Aegis", saveCall: false, startSession: name => startSession({ data: { name } }), onEvent: event => { if (event.type === "app.started") { setTranscript([]); setNotice(null); setHandoff(false); setPendingScan(null); } if (event.type === "app.closed") { setPendingScan(null); setHandoff(false); } if (typeof event["delta"] !== "string") return; const delta = event["delta"]; const role = event.type === "session.input_transcript.delta" ? "You" : event.type === "session.output_transcript.delta" ? "Aegis" : null; if (!role) return; setTranscript(previous => { const last = previous[previous.length - 1]; return last?.role === role ? [...previous.slice(0, -1), { role, text: (last.text + delta).slice(-6000) }] : [...previous.slice(-11), { role, text: delta }]; }); }, onToolCall: async call => { if (call.name === "get_security_overview") { const data = await overview(); return { cloud: data.cloud, external: data.external.map(site => ({ hostname: site.hostname, verified: Boolean(site.verified_at) })), reports: data.reports.slice(0, 3), edge: data.edge, enforcement: "Read-only monitoring, no protection changes performed." }; } if (call.name === "request_security_scan") { if (typeof call.args?.["hostname"] !== "string") return { error: "A public domain is required." }; const hostname = publicHostname(call.args["hostname"]); setPendingScan(hostname); return { awaitingApproval: true, hostname, message: "The caller must approve this domain on screen before a public Observatory scan runs." }; } if (call.name === "request_echo_handoff") { setHandoff(true); return { awaitingApproval: true, message: "Echo has NOT opened. Ask the caller to approve on screen." }; } return { error: "Aegis cannot perform that action." }; }, }); const active = voice.status === "connecting" || voice.status === "connected"; const end = () => { callVersion.current += 1; setPendingScan(null); setHandoff(false); voice.stop(); }; const approveScan = async () => { if (!pendingScan || scanning || !active) return; const hostname = pendingScan, version = callVersion.current; setPendingScan(null); setScanning(true); setNotice(null); try { const report = await scan({ data: { hostname, publicScanConsent: true } }); await onRefresh(); if (callVersion.current !== version) return; const summary = `${report.hostname}: header grade ${report.grade}, ${report.failed} failed checks. Observed ${report.scannedAt}. This is not a guarantee of safety.`; setNotice(summary); voice.reportTest(`Approved security scan result: ${summary} Findings: ${report.findings.filter(f => !f.pass).map(f => `${f.name}: ${f.advice}`).join("; ")}`); } catch (error) { if (callVersion.current !== version) return; const message = error instanceof Error ? error.message : "The scan could not complete."; setNotice(message); voice.reportTest(`The approved scan failed: ${message}. Do not claim success.`); } finally { setScanning(false); } }; return
{voice.status === "connecting" ? "Connecting…" : voice.status === "connected" ? voice.muted ? "Microphone muted" : voice.phase === "speaking" ? "Aegis speaking" : "Listening" : voice.status === "stopping" ? "Ending call…" : "Voice with Aegis"}
{active ? <> : }
{voice.error &&

{voice.error}

} {transcript.length > 0 &&
{transcript.map((message, index) =>

{message.role}

{message.text}

)}
} {pendingScan && active &&

Scan {pendingScan} with Mozilla Observatory? The domain and scan results will be public and may be cached.

} {scanning &&

Scanning approved domain…

} {notice &&

{notice}

} {handoff && active &&

Continue with Echo? Your Aegis call will end.

}
; }