Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
feat: capture Brain thoughts from command palette (#5427)
[._.]/ Adam Eivy920d71cc500c
diff --git a/client/src/components/CmdKSearch.jsx b/client/src/components/CmdKSearch.jsxindex aab56c89bd..a4666f40bd 100644--- a/client/src/components/CmdKSearch.jsx+++ b/client/src/components/CmdKSearch.jsx@@ -1,7 +1,7 @@import { useState, useEffect, useRef, useCallback, useMemo } from 'react';import { createPortal } from 'react-dom';import { useLocation, useNavigate } from 'react-router';-import { Brain, Cpu, Package, History, HeartPulse, Search, Loader2, Navigation, Play, LayoutGrid, BookMarked } from 'lucide-react';+import { Brain, Cpu, Package, History, HeartPulse, Search, Loader2, Navigation, Play, LayoutGrid, BookMarked, Send } from 'lucide-react';import { useCmdKSearch } from '../hooks/useCmdKSearch';import useFocusTrap from '../hooks/useFocusTrap.js';import { useInstanceFeatures } from '../hooks/useInstanceFeatures.js';@@ -80,6 +80,11 @@ export default function CmdKSearch() {const [focusedIndex, setFocusedIndex] = useState(0);const [expandedSources, setExpandedSources] = useState(new Set());const [recentPaths, setRecentPaths] = useState([]);+ const [captureMode, setCaptureMode] = useState(false);+ const [captureDraft, setCaptureDraft] = useState('');+ const [captureSubmitting, setCaptureSubmitting] = useState(false);+ const captureSubmittingRef = useRef(false);+ const captureAttemptRef = useRef(0);const resultRefs = useRef([]);useFocusTrap(open, modalRef, { initialFocusRef: inputRef });@@ -95,6 +100,11 @@ export default function CmdKSearch() {useEffect(() => {if (!open) {setQuery('');+ setCaptureMode(false);+ setCaptureDraft('');+ setCaptureSubmitting(false);+ captureSubmittingRef.current = false;+ captureAttemptRef.current += 1;setSearchResults([]);setCatalogResults([]);setFocusedIndex(0);@@ -269,7 +279,58 @@ export default function CmdKSearch() {if (el) el.scrollIntoView({ block: 'nearest' });}, [focusedIndex]);- const close = useCallback(() => setOpen(false), [setOpen]);+ useEffect(() => {+ if (open && captureMode) inputRef.current?.focus();+ }, [open, captureMode]);++ const resetCapture = useCallback(() => {+ captureAttemptRef.current += 1;+ captureSubmittingRef.current = false;+ setCaptureMode(false);+ setCaptureDraft('');+ setCaptureSubmitting(false);+ }, []);++ const close = useCallback(() => {+ resetCapture();+ setOpen(false);+ }, [resetCapture, setOpen]);++ const enterCaptureMode = useCallback(() => {+ setCaptureMode(true);+ }, []);++ const submitCapture = useCallback(async () => {+ const text = captureDraft.trim();+ if (!text || captureSubmittingRef.current) return;++ captureSubmittingRef.current = true;+ setCaptureSubmitting(true);+ const attempt = ++captureAttemptRef.current;+ const res = await runPaletteAction('brain_capture', { text }).then(+ (result) => result,+ () => null,+ );+ if (attempt !== captureAttemptRef.current) return;++ captureSubmittingRef.current = false;+ setCaptureSubmitting(false);+ if (!res) {+ inputRef.current?.focus();+ return;+ }++ const summary = res?.result?.summary || 'Captured to Brain.';+ if (res?.ok === false) {+ toast.error(summary);+ inputRef.current?.focus();+ return;+ }++ toast.success(summary);+ setCaptureDraft('');+ close();+ }, [captureDraft, close]);const DISPATCH = useMemo(() => ({nav: (item) => { navigate(item.path); close(); },@@ -291,6 +352,10 @@ export default function CmdKSearch() {close();},action: async (item) => {+ if (item.id === 'brain_capture') {+ enterCaptureMode();+ return;+ }const required = item.parameters?.required || [];if (required.length > 0) {// Keep palette open so the user can pick another command.@@ -303,11 +368,22 @@ export default function CmdKSearch() {else toast.success(summary);close();},- }), [navigate, close]);+ }), [navigate, close, enterCaptureMode]);const dispatchCommand = useCallback((item) => DISPATCH[item.kind]?.(item), [DISPATCH]);const handleKeyDown = (e) => {+ if (captureMode) {+ if (e.key === 'Enter') {+ e.preventDefault();+ submitCapture();+ } else if (e.key === 'Escape') {+ e.preventDefault();+ setCaptureMode(false);+ }+ return;+ }+if (e.key === 'ArrowDown') {e.preventDefault();if (focusable.length > 0) setFocusedIndex((i) => Math.min(i + 1, focusable.length - 1));@@ -385,22 +461,55 @@ export default function CmdKSearch() {className="relative w-full max-w-3xl mx-4 bg-port-card rounded-xl border border-port-border shadow-2xl overflow-hidden"><div className="flex items-center gap-3 px-4 py-4 border-b border-port-border">- <Search size={18} className="text-gray-400 shrink-0" />- <input- ref={inputRef}- type="text"- value={query}- onChange={(e) => setQuery(e.target.value)}- placeholder="Go to page, run an action, or search Brain / Memory / Apps…"- className="flex-1 bg-transparent text-white placeholder-gray-500 outline-hidden text-sm"- aria-label="Command palette"- />- <span className="text-xs text-gray-500 border border-port-border rounded px-1.5 py-0.5 shrink-0">- {`${modKey}+K`}- </span>+ {captureMode ? <Brain size={18} className="text-port-accent shrink-0" /> : <Search size={18} className="text-gray-400 shrink-0" />}+ <div className="flex-1 min-w-0">+ {captureMode ? (+ <>+ <label htmlFor="command-palette-capture" className="block text-xs font-medium text-port-accent mb-1">+ Capture to Brain+ </label>+ <input+ id="command-palette-capture"+ ref={inputRef}+ type="text"+ value={captureDraft}+ onChange={(e) => setCaptureDraft(e.target.value)}+ placeholder="Thought or URL…"+ className="w-full bg-transparent text-white placeholder-gray-500 outline-hidden text-sm"+ readOnly={captureSubmitting}+ />+ </>+ ) : (+ <input+ ref={inputRef}+ type="text"+ value={query}+ onChange={(e) => setQuery(e.target.value)}+ placeholder="Go to page, run an action, or search Brain / Memory / Apps…"+ className="w-full bg-transparent text-white placeholder-gray-500 outline-hidden text-sm"+ aria-label="Command palette"+ />+ )}+ </div>+ {captureMode ? (+ <button+ type="button"+ onClick={submitCapture}+ disabled={!captureDraft.trim() || captureSubmitting}+ aria-label="Capture thought"+ className="flex items-center gap-1.5 px-3 py-2 rounded-lg bg-port-accent/20 text-port-accent hover:bg-port-accent/30 disabled:opacity-40 disabled:cursor-not-allowed transition-colors text-sm"+ >+ {captureSubmitting ? <Loader2 size={14} className="animate-spin" /> : <Send size={14} />}+ Capture+ </button>+ ) : (+ <span className="text-xs text-gray-500 border border-port-border rounded px-1.5 py-0.5 shrink-0">+ {`${modKey}+K`}+ </span>+ )}</div>- <div className="max-h-96 overflow-y-auto p-2">+ {!captureMode && <div className="max-h-96 overflow-y-auto p-2">{renderGroup(<History size={14} />,'Recent destinations',@@ -490,10 +599,16 @@ export default function CmdKSearch() {</div>);})}- </div>+ </div>}++ {captureMode && (+ <div className="px-4 py-5 text-sm text-gray-400">+ Capture one unstructured thought or URL without leaving the command palette.+ </div>+ )}<div className="flex items-center justify-between gap-2 px-4 py-2 border-t border-port-border text-[11px] text-gray-500">- <span>↑↓ navigate · ↵ run · Esc close</span>+ <span>{captureMode ? '↵ capture · Esc back' : '↑↓ navigate · ↵ run · Esc close'}</span><span>Shared backbone with voice agent</span></div></div>diff --git a/client/src/components/CmdKSearch.test.jsx b/client/src/components/CmdKSearch.test.jsxindex cdf946f0c3..5d370e03af 100644--- a/client/src/components/CmdKSearch.test.jsx+++ b/client/src/components/CmdKSearch.test.jsx@@ -7,17 +7,26 @@ const getPaletteManifest = vi.fn();const getInstanceFeatures = vi.fn();const getDashboardLayouts = vi.fn();const search = vi.fn(() => Promise.resolve({ sources: [] }));+const runPaletteAction = vi.fn(() => Promise.resolve({ ok: true }));+const toast = vi.hoisted(() => {+ const mock = vi.fn();+ mock.success = vi.fn();+ mock.error = vi.fn();+ return mock;+});vi.mock('../services/api', () => ({search: (...args) => search(...args),getPaletteManifest: (...args) => getPaletteManifest(...args),getInstanceFeatures: (...args) => getInstanceFeatures(...args),- runPaletteAction: vi.fn(() => Promise.resolve({ ok: true })),+ runPaletteAction: (...args) => runPaletteAction(...args),getDashboardLayouts: (...args) => getDashboardLayouts(...args),setActiveDashboardLayout: vi.fn(() => Promise.resolve()),listCatalogIngredients: vi.fn(() => Promise.resolve({ items: [] })),}));+vi.mock('./ui/Toast', () => ({ default: toast }));+import { __resetInstanceFeatureCache } from '../hooks/useInstanceFeatures.js';import CmdKSearch from './CmdKSearch.jsx';@@ -48,9 +57,149 @@ beforeEach(() => {getDashboardLayouts.mockResolvedValue({ layouts: [] });search.mockReset();search.mockResolvedValue({ sources: [] });+ runPaletteAction.mockReset();+ runPaletteAction.mockResolvedValue({ ok: true });+ toast.mockReset();+ toast.success.mockReset();+ toast.error.mockReset();HTMLElement.prototype.scrollIntoView = vi.fn();});+const BRAIN_CAPTURE = {+ id: 'brain_capture',+ label: 'Capture to Brain',+ section: 'Brain',+ description: 'Capture a thought',+ aliases: [],+ keywords: [],+ parameters: {+ type: 'object',+ required: ['text'],+ properties: { text: { type: 'string' } },+ },+};++const renderBrainCapturePalette = async () => {+ getPaletteManifest.mockResolvedValue({ nav: [], actions: [BRAIN_CAPTURE] });+ render(+ <MemoryRouter>+ <CmdKSearch />+ </MemoryRouter>,+ );+ fireEvent.keyDown(document, { key: 'k', metaKey: true });+ return screen.findByRole('dialog', { name: 'Command palette' });+};++describe('CmdKSearch inline Brain capture', () => {+ it('enters capture mode by mouse with a focused dedicated input', async () => {+ await renderBrainCapturePalette();++ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));++ const input = screen.getByRole('textbox', { name: 'Capture to Brain' });+ expect(input).toHaveFocus();+ expect(input).toHaveAttribute('placeholder', 'Thought or URL…');+ expect(toast).not.toHaveBeenCalled();+ });++ it('enters capture mode from the keyboard', async () => {+ await renderBrainCapturePalette();+ const input = screen.getByRole('textbox', { name: 'Command palette' });++ fireEvent.keyDown(input, { key: 'Enter' });++ expect(screen.getByRole('textbox', { name: 'Capture to Brain' })).toHaveFocus();+ });++ it('blocks blank drafts and submits trimmed text before closing on success', async () => {+ runPaletteAction.mockResolvedValue({ ok: true, result: { summary: 'Thought captured.' } });+ await renderBrainCapturePalette();+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ const input = screen.getByRole('textbox', { name: 'Capture to Brain' });++ fireEvent.change(input, { target: { value: ' ' } });+ fireEvent.keyDown(input, { key: 'Enter' });+ fireEvent.click(screen.getByRole('button', { name: 'Capture thought' }));+ expect(runPaletteAction).not.toHaveBeenCalled();++ fireEvent.change(input, { target: { value: ' Remember this idea ' } });+ fireEvent.keyDown(input, { key: 'Enter' });++ await waitFor(() => expect(runPaletteAction).toHaveBeenCalledWith('brain_capture', { text: 'Remember this idea' }));+ expect(runPaletteAction).toHaveBeenCalledTimes(1);+ expect(toast.success).toHaveBeenCalledWith('Thought captured.');+ await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());++ fireEvent.keyDown(document, { key: 'k', metaKey: true });+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ expect(screen.getByRole('textbox', { name: 'Capture to Brain' })).toHaveValue('');+ });++ it('prevents duplicate submissions while capture is pending', async () => {+ let resolveCapture;+ runPaletteAction.mockImplementation(() => new Promise((resolve) => { resolveCapture = resolve; }));+ await renderBrainCapturePalette();+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ const input = screen.getByRole('textbox', { name: 'Capture to Brain' });+ fireEvent.change(input, { target: { value: 'One thought' } });++ fireEvent.keyDown(input, { key: 'Enter' });+ fireEvent.keyDown(input, { key: 'Enter' });+ fireEvent.click(screen.getByRole('button', { name: 'Capture thought' }));++ expect(runPaletteAction).toHaveBeenCalledTimes(1);+ await act(async () => { resolveCapture({ ok: true, result: { summary: 'Captured.' } }); });+ });++ it('keeps the full draft open after a failed request without a second error toast', async () => {+ runPaletteAction.mockRejectedValue(new Error('Capture failed'));+ await renderBrainCapturePalette();+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ const input = screen.getByRole('textbox', { name: 'Capture to Brain' });+ fireEvent.change(input, { target: { value: ' Keep my spacing ' } });++ fireEvent.keyDown(input, { key: 'Enter' });++ await waitFor(() => expect(runPaletteAction).toHaveBeenCalledTimes(1));+ expect(screen.getByRole('dialog', { name: 'Command palette' })).toBeInTheDocument();+ expect(input).toHaveValue(' Keep my spacing ');+ expect(input).toHaveFocus();+ expect(toast.error).not.toHaveBeenCalled();+ });++ it('uses Escape to return to the prior query before closing the palette', async () => {+ await renderBrainCapturePalette();+ const searchInput = screen.getByRole('textbox', { name: 'Command palette' });+ fireEvent.change(searchInput, { target: { value: 'capture' } });+ fireEvent.keyDown(searchInput, { key: 'Enter' });+ const captureInput = screen.getByRole('textbox', { name: 'Capture to Brain' });+ fireEvent.change(captureInput, { target: { value: 'Draft thought' } });++ fireEvent.keyDown(captureInput, { key: 'Escape' });++ expect(screen.getByRole('dialog', { name: 'Command palette' })).toBeInTheDocument();+ const restoredSearch = screen.getByRole('textbox', { name: 'Command palette' });+ expect(restoredSearch).toHaveValue('capture');++ fireEvent.keyDown(restoredSearch, { key: 'Escape' });+ await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());+ });++ it('resets capture state when dismissed through the backdrop', async () => {+ const dialog = await renderBrainCapturePalette();+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ fireEvent.change(screen.getByRole('textbox', { name: 'Capture to Brain' }), { target: { value: 'Discard me' } });++ fireEvent.click(dialog.parentElement.querySelector('[aria-hidden="true"]'));+ await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());++ fireEvent.keyDown(document, { key: 'k', metaKey: true });+ expect(await screen.findByRole('textbox', { name: 'Command palette' })).toHaveValue('');+ fireEvent.click(await screen.findByRole('option', { name: /Capture to Brain/ }));+ expect(screen.getByRole('textbox', { name: 'Capture to Brain' })).toHaveValue('');+ });+});+describe('CmdKSearch dialog accessibility', () => {it('traps focus inside the modal and restores it to the opener on close', async () => {render(