import { KeyRound, MessageCircleQuestion, SendHorizontal, Trash2, X } from 'lucide-react' import { useEffect, useRef, useState } from 'react' import type { ReactNode } from 'react' import { ApiError, pcbApi } from '../services/pcbApi' import type { ChatMessage, Design, Health } from '../types/pcb' import { Spinner } from './ui' interface Props { projectId: string projectName: string design: Design | null health: Health | null focus: { ref?: string | null; net?: string | null; issue?: string | null } onClose: () => void } const store = new Map() // conversation per project for this browser session export function AssistantPanel({ projectId, projectName, design, health, focus, onClose }: Props) { const [messages, setMessages] = useState(() => store.get(projectId) ?? []) const [text, setText] = useState('') const [sending, setSending] = useState(false) const [error, setError] = useState<{ code: string; message: string } | null>(null) const [model, setModel] = useState(null) const list = useRef(null) const configured = health?.ai.configured ?? false useEffect(() => { store.set(projectId, messages) }, [projectId, messages]) useEffect(() => { list.current?.scrollTo({ top: list.current.scrollHeight, behavior: 'smooth' }) }, [messages, sending]) const send = async (q: string) => { const question = q.trim() if (!question || sending) return const next: ChatMessage[] = [...messages, { role: 'user', content: question }] setMessages(next) setText('') setError(null) setSending(true) try { const res = await pcbApi.chat(projectId, next, { ref: focus.ref ?? undefined, net: focus.net ?? undefined, issue: focus.issue ?? undefined, }) setModel(res.model) setMessages([...next, { role: 'assistant', content: res.reply }]) } catch (e) { const err = e instanceof ApiError ? e : new ApiError('ERROR', (e as Error).message) setError({ code: err.code, message: err.message }) // the question was not answered: take it out of the conversation and give it back for a retry setMessages(messages) setText(question) } finally { setSending(false) } } const v = design?.validation const m = design?.metrics const suggestions = [ 'Give me a short summary of this board', ...(m && m.routing_percent < 100 ? ['Why are some connections still unrouted, and what can I do?'] : []), ...(v && v.errors > 0 ? ['Explain the DRC errors and how to fix them'] : []), ...(focus.ref ? [`What does ${focus.ref} do, and is it placed well?`] : []), ...(focus.net ? [`Explain the net ${focus.net}`] : []), 'How do I get this board into KiCad?', ].slice(0, 5) return (
AI guide
{projectName}{model ? ` · ${model}` : configured ? ` · ${health?.ai.provider}` : ''}
{messages.length > 0 && ( )}
{!configured && } {messages.length === 0 && configured && (
Ask anything about this board: why a net is unrouted, what a DRC error means, what a part does, or how to use the app. {focus.ref || focus.net ? 'Your current selection is included.' : 'Select a part or net to ask about it.'}
)} {messages.map((msg, i) => (
{msg.role === 'user' ? msg.content : }
))} {sending &&
Thinking…
} {error && (error.code === 'AI_NOT_CONFIGURED' ? : (
{error.code}: {error.message}
Your question is back in the box below — press Enter to try again.
))}
{messages.length === 0 && configured && (
{suggestions.map((s) => ( ))}
)}
{ e.preventDefault(); void send(text) }}>