import { html, useState, useRef, useCallback } from '../lib/preact.js'; import { getStatusMeta } from '../utils/status.js'; export function SimpleInput({ sessionId, status, onRespond, autocompleteConfig = null }) { const [text, setText] = useState(''); const [focused, setFocused] = useState(false); const [sending, setSending] = useState(false); const [error, setError] = useState(null); const textareaRef = useRef(null); const meta = getStatusMeta(status); // Detect if cursor is at a trigger position for autocomplete const getTriggerInfo = useCallback((value, cursorPos) => { // No config means no autocomplete if (!autocompleteConfig) return null; const { trigger } = autocompleteConfig; // Find the start of the current "word" (after last whitespace before cursor) let wordStart = cursorPos; while (wordStart > 0 && !/\s/.test(value[wordStart - 1])) { wordStart--; } // Check if word starts with this agent's trigger character if (value[wordStart] === trigger) { return { trigger, filterText: value.slice(wordStart + 1, cursorPos).toLowerCase(), replaceStart: wordStart, replaceEnd: cursorPos, }; } return null; }, [autocompleteConfig]); const handleSubmit = async (e) => { e.preventDefault(); e.stopPropagation(); if (text.trim() && !sending) { setSending(true); setError(null); try { await onRespond(sessionId, text.trim(), true, 0); setText(''); } catch (err) { setError('Failed to send message'); console.error('SimpleInput send error:', err); } finally { setSending(false); // Refocus the textarea after submission // Use setTimeout to ensure React has re-rendered with disabled=false setTimeout(() => { textareaRef.current?.focus(); }, 0); } } }; return html`
e.stopPropagation()}> ${error && html`
${error}
`}