import type { MessageSearchResponse } from "@letta-ai/letta-client/resources/messages"; import { Box, useInput } from "ink"; import { useCallback, useEffect, useRef, useState } from "react"; import { getBackend } from "@/backend"; import { searchMessagesForBackend, warmMessageSearchCacheForBackend, } from "@/backend/message-search"; import { truncateText } from "@/cli/helpers/truncate-text"; import { useTerminalWidth } from "@/cli/hooks/use-terminal-width"; import { colors } from "./colors"; import { Text } from "./Text"; // Horizontal line character (matches approval dialogs) const SOLID_LINE = "─"; interface MessageSearchProps { onClose: () => void; initialQuery?: string; /** Current agent ID for "current agent" filter */ agentId?: string; /** Current conversation ID for "current conv" filter */ conversationId?: string; /** Callback when user wants to open a conversation */ onOpenConversation?: ( agentId: string, conversationId?: string, searchContext?: { query: string; message: string }, ) => void; } const VISIBLE_ITEMS = 5; const SEARCH_LIMIT = 100; // Max results from API type SearchMode = "hybrid" | "vector" | "fts"; const SEARCH_MODES: SearchMode[] = ["fts", "vector", "hybrid"]; // Display order (hybrid is default) type SearchRange = "all" | "agent" | "conv"; const SEARCH_RANGES: SearchRange[] = ["all", "agent", "conv"]; type SearchTarget = { mode: SearchMode; range: SearchRange; }; type SearchCacheWarmRequest = { collection: "messages"; scope: Record; }; type SearchCacheWarmResponse = { collection: "messages"; status: string; warmed: boolean; }; export async function warmMessageSearchCache() { const body: SearchCacheWarmRequest = { collection: "messages", scope: {}, }; return warmMessageSearchCacheForBackend(body); } function isSearchRangeAvailable( range: SearchRange, options: { agentId?: string; conversationId?: string }, ): boolean { if (range === "agent") return Boolean(options.agentId); if (range === "conv") return Boolean(options.conversationId); return true; } export function buildSearchTargetPlan( mode: SearchMode, range: SearchRange, options: { agentId?: string; conversationId?: string; textOnlyModes?: boolean; }, ): { primary: SearchTarget; prefetch: SearchTarget[] } { const availableRanges = SEARCH_RANGES.filter((candidateRange) => isSearchRangeAvailable(candidateRange, options), ); const prefetch = [ ...(options.textOnlyModes ? [] : SEARCH_MODES.filter((candidateMode) => candidateMode !== mode).map( (candidateMode) => ({ mode: candidateMode, range }), )), ...availableRanges .filter((candidateRange) => candidateRange !== range) .map((candidateRange) => ({ mode, range: candidateRange })), ]; return { primary: { mode, range }, prefetch, }; } export function buildMessageSearchRequestBody( query: string, mode: SearchMode, range: SearchRange, options: { agentId?: string; conversationId?: string; limit?: number }, ): Record { const body: Record = { query: query.trim(), search_mode: mode, limit: options.limit ?? SEARCH_LIMIT, }; if (range === "agent" && options.agentId) { body.agent_id = options.agentId; } else if (range === "conv" && options.conversationId) { body.conversation_id = options.conversationId; if (options.agentId) { body.agent_id = options.agentId; } } return body; } /** * Format a timestamp in local timezone */ function formatLocalTime(dateStr: string | null | undefined): string { if (!dateStr) return ""; const date = new Date(dateStr); // Format: "Dec 15, 6:30 PM" or "Dec 15, 2024, 6:30 PM" depending on year const now = new Date(); const sameYear = date.getFullYear() === now.getFullYear(); const options: Intl.DateTimeFormatOptions = { month: "short", day: "numeric", ...(sameYear ? {} : { year: "numeric" }), hour: "numeric", minute: "2-digit", }; return date.toLocaleString(undefined, options); } /** * Truncate text to fit width, adding ellipsis if needed */ function escapeRegExp(text: string): string { return text.replace(/[.*+?^${}()|[\]\\]/g, (match) => `\\${match}`); } function stringifyMessageValue(value: unknown): string { if (typeof value === "string") return value; if (value === null || value === undefined) return ""; try { return JSON.stringify(value); } catch { return String(value); } } /** * Get display text from a message */ export function getMessageText(msg: MessageSearchResponse[number]): string { // Assistant message content if ("content" in msg) { const content = msg.content; if (typeof content === "string") return content; if (Array.isArray(content)) { const textPart = content.find( (c) => typeof c === "object" && c && "text" in c, ); if (textPart && typeof textPart === "object" && "text" in textPart) { return String(textPart.text); } } } // Text field (user messages, etc) if ("text" in msg && typeof msg.text === "string") { return msg.text; } // Reasoning messages if ("reasoning" in msg && typeof msg.reasoning === "string") { return msg.reasoning; } // Tool call messages if ("tool_call" in msg && msg.tool_call) { const tc = msg.tool_call as { name?: string; arguments?: string }; return `Tool: ${tc.name || "unknown"}`; } // Tool return messages - show tool name and preview of return if ("tool_return" in msg) { const toolName = "name" in msg ? (msg.name as string) : "tool"; const returnValue = stringifyMessageValue(msg.tool_return); // Truncate long return values const preview = returnValue.slice(0, 100); return `${toolName}: ${preview}`; } return `[${msg.message_type || "unknown"}]`; } /** * Highlight keywords in text */ function HighlightedText({ text, query }: { text: string; query: string }) { if (!query.trim()) return {text}; const highlightTerms = [ ...new Set(query.trim().split(/\s+/).filter(Boolean)), ].sort((a, b) => b.length - a.length); if (highlightTerms.length === 0) return {text}; const parts = text.split( new RegExp(`(${highlightTerms.map(escapeRegExp).join("|")})`, "gi"), ); let offset = 0; return ( {parts.map((part) => { const key = `${offset}-${part}`; offset += part.length; return highlightTerms.some( (term) => part.toLowerCase() === term.toLowerCase(), ) ? ( {part} ) : ( {part} ); })} ); } export function MessageSearch({ onClose, initialQuery, agentId, conversationId, onOpenConversation, }: MessageSearchProps) { const terminalWidth = useTerminalWidth(); const textOnlyModes = getBackend().capabilities.localModelCatalog; const [searchInput, setSearchInput] = useState(initialQuery ?? ""); const [activeQuery, setActiveQuery] = useState(initialQuery ?? ""); const [searchMode, setSearchMode] = useState( textOnlyModes ? "fts" : "hybrid", ); const [searchRange, setSearchRange] = useState("agent"); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [selectedIndex, setSelectedIndex] = useState(0); const [expandedMessage, setExpandedMessage] = useState< MessageSearchResponse[number] | null >(null); const searchRequestIdRef = useRef(0); // Cache results per query+mode+range combination to avoid re-fetching const resultsCache = useRef>(new Map()); const pendingResultsCache = useRef< Map> >(new Map()); // Warm tpuf cache on mount (fire-and-forget) useEffect(() => { void warmMessageSearchCache().catch(() => { // Silently ignore - cache warm is best-effort }); }, []); // Get cache key for a specific query+mode+range combination const getCacheKey = useCallback( (query: string, mode: SearchMode, range: SearchRange) => { const rangeKey = range === "agent" ? agentId || "no-agent" : range === "conv" ? conversationId || "no-conv" : "all"; return `${query.trim()}-${mode}-${rangeKey}`; }, [agentId, conversationId], ); // Execute search for a single mode (returns results, doesn't set state) const fetchSearchResults = useCallback( async (query: string, mode: SearchMode, range: SearchRange) => { const body = buildMessageSearchRequestBody(query, mode, range, { agentId, conversationId, limit: SEARCH_LIMIT, }); return searchMessagesForBackend(body); }, [agentId, conversationId], ); const fetchAndCacheSearchResults = useCallback( async (query: string, mode: SearchMode, range: SearchRange) => { const cacheKey = getCacheKey(query, mode, range); const cached = resultsCache.current.get(cacheKey); if (cached) { return cached; } const pending = pendingResultsCache.current.get(cacheKey); if (pending) { return pending; } if (!isSearchRangeAvailable(range, { agentId, conversationId })) { const emptyResults: MessageSearchResponse = []; resultsCache.current.set(cacheKey, emptyResults); return emptyResults; } const request = fetchSearchResults(query, mode, range) .then((searchResults) => { resultsCache.current.set(cacheKey, searchResults); return searchResults; }) .finally(() => { pendingResultsCache.current.delete(cacheKey); }); pendingResultsCache.current.set(cacheKey, request); return request; }, [agentId, conversationId, fetchSearchResults, getCacheKey], ); const prefetchSearchResults = useCallback( (query: string, mode: SearchMode, range: SearchRange) => { const { prefetch } = buildSearchTargetPlan(mode, range, { agentId, conversationId, textOnlyModes, }); if (prefetch.length === 0) { return; } void (async () => { try { await Promise.all( prefetch.map(({ mode: prefetchMode, range: prefetchRange }) => fetchAndCacheSearchResults( query, prefetchMode, prefetchRange, ).catch(() => []), ), ); } catch { // Best-effort only - prefetch should never block the active result } })(); }, [agentId, conversationId, fetchAndCacheSearchResults, textOnlyModes], ); // Execute the active search first, then prefetch adjacent tab/range results const executeSearch = useCallback( async (query: string, mode: SearchMode, range: SearchRange) => { if (!query.trim()) return; const cacheKey = getCacheKey(query, mode, range); const requestId = ++searchRequestIdRef.current; const cached = resultsCache.current.get(cacheKey); setError(null); if (cached) { setLoading(false); setResults(cached); setSelectedIndex(0); prefetchSearchResults(query, mode, range); return; } setLoading(true); try { const primaryResults = await fetchAndCacheSearchResults( query, mode, range, ); if (searchRequestIdRef.current !== requestId) { return; } setResults(primaryResults); setSelectedIndex(0); setLoading(false); prefetchSearchResults(query, mode, range); } catch (err) { if (searchRequestIdRef.current !== requestId) { return; } setError(err instanceof Error ? err.message : String(err)); setResults([]); setLoading(false); } }, [fetchAndCacheSearchResults, getCacheKey, prefetchSearchResults], ); // Submit search (only when query changes) const submitSearch = useCallback(() => { if (searchInput.trim() && searchInput !== activeQuery) { setActiveQuery(searchInput); executeSearch(searchInput, searchMode, searchRange); } }, [searchInput, activeQuery, searchMode, searchRange, executeSearch]); // Clear search const clearSearch = useCallback(() => { setSearchInput(""); setActiveQuery(""); setResults([]); setSelectedIndex(0); }, []); // Cycle search mode (Shift+Tab) const cycleSearchMode = useCallback((reverse = false) => { setSearchMode((current) => { const currentIndex = SEARCH_MODES.indexOf(current); const nextIndex = reverse ? (currentIndex - 1 + SEARCH_MODES.length) % SEARCH_MODES.length : (currentIndex + 1) % SEARCH_MODES.length; return SEARCH_MODES[nextIndex] as SearchMode; }); }, []); // Cycle search range (Tab) const cycleSearchRange = useCallback(() => { setSearchRange((current) => { const currentIndex = SEARCH_RANGES.indexOf(current); const nextIndex = (currentIndex + 1) % SEARCH_RANGES.length; return SEARCH_RANGES[nextIndex] as SearchRange; }); }, []); // Re-run search when mode or range changes (if there's an active query) - uses cache useEffect(() => { if (activeQuery) { executeSearch(activeQuery, searchMode, searchRange); } }, [searchMode, searchRange, activeQuery, executeSearch]); // Sliding window for visible items const startIndex = Math.max( 0, Math.min(selectedIndex - 2, results.length - VISIBLE_ITEMS), ); const visibleResults = results.slice(startIndex, startIndex + VISIBLE_ITEMS); useInput((input, key) => { // CTRL-C: immediately close (bypasses search clearing) if (key.ctrl && input === "c") { onClose(); return; } // Handle expanded message view if (expandedMessage) { if (key.escape) { setExpandedMessage(null); } else if (key.return && onOpenConversation) { const msgData = expandedMessage as { agent_id?: string; conversation_id?: string; }; if (msgData.agent_id) { const fullText = getMessageText(expandedMessage); onOpenConversation(msgData.agent_id, msgData.conversation_id, { query: activeQuery, message: fullText, }); } } return; } if (key.escape) { if (searchInput || activeQuery) { clearSearch(); } else { onClose(); } } else if (key.return) { // If user has typed a new query, search first if (searchInput.trim() && searchInput !== activeQuery) { submitSearch(); } else if (results.length > 0 && results[selectedIndex]) { // Otherwise expand the selected result setExpandedMessage(results[selectedIndex]); } } else if (key.backspace || key.delete) { setSearchInput((prev) => prev.slice(0, -1)); } else if (key.tab && key.shift) { // Shift+Tab cycles search mode cycleSearchMode(); } else if (key.tab) { // Tab cycles search range cycleSearchRange(); } else if (key.upArrow) { setSelectedIndex((prev) => Math.max(0, prev - 1)); } else if (key.downArrow) { setSelectedIndex((prev) => Math.min(results.length - 1, prev + 1)); } else if (input && !key.ctrl && !key.meta) { setSearchInput((prev) => prev + input); } }); const solidLine = SOLID_LINE.repeat(Math.max(terminalWidth, 10)); // Range label helper const getRangeLabel = (range: SearchRange) => { switch (range) { case "all": return "all agents"; case "agent": return "this agent"; case "conv": return "this conversation"; } }; return ( {/* Command header */} {"> /search"} {solidLine} {/* Expanded message view - hide title/controls */} {expandedMessage && (() => { const msgData = expandedMessage as { date?: string; created_at?: string; agent_id?: string; conversation_id?: string; }; const fullText = getMessageText(expandedMessage); const msgType = expandedMessage.message_type || "unknown"; const isAssistant = msgType === "assistant_message" || msgType === "reasoning_message"; const typeLabel = isAssistant ? "Agent message" : "User message"; const timestamp = formatLocalTime(msgData.created_at || msgData.date); return ( {/* Full message text with padding and HighlightedText */} {/* Metadata list */} {typeLabel}, sent {timestamp} Agent ID: {msgData.agent_id || "unknown"} {msgData.conversation_id && ( Conversation ID: {msgData.conversation_id} )} {/* Action prompt footer */} {onOpenConversation ? ( <> Enter to open conversation · Esc cancel ) : ( Esc cancel )} ); })()} {/* Title and search controls - hidden when expanded */} {!expandedMessage && ( Search messages across all agents {/* Search input */} Search: {searchInput ? ( <> {searchInput} {searchInput !== activeQuery && ( (press Enter to search) )} ) : ( (type to search) )} {/* Range tabs */} Range (tab): {SEARCH_RANGES.map((range, i) => { const isActive = range === searchRange; return ( {i > 0 && } {` ${getRangeLabel(range)} `} ); })} {/* Mode tabs */} Mode (shift-tab): {SEARCH_MODES.map((mode, i) => { const isActive = mode === searchMode; return ( {i > 0 && } {` ${mode} `} ); })} )} {/* Error state */} {!expandedMessage && error && ( Error: {error} )} {/* Loading state */} {!expandedMessage && loading && ( Searching... )} {/* No results */} {!expandedMessage && !loading && activeQuery && results.length === 0 && ( No results found for "{activeQuery}" )} {/* Results list */} {!expandedMessage && !loading && results.length > 0 && ( {visibleResults.map( (msg: MessageSearchResponse[number], visibleIndex: number) => { const actualIndex = startIndex + visibleIndex; const isSelected = actualIndex === selectedIndex; const messageText = getMessageText(msg); // All messages have a date field const msgWithDate = msg as { date?: string; created_at?: string; agent_id?: string; conversation_id?: string; }; const msgType = msg.message_type || "unknown"; const agentIdFromMsg = msgWithDate.agent_id || "unknown"; const conversationIdFromMsg = msgWithDate.conversation_id; const createdAt = formatLocalTime( msgWithDate.created_at || msgWithDate.date, ); // Determine emoji based on message type const isAssistant = msgType === "assistant_message" || msgType === "reasoning_message"; const emoji = isAssistant ? "👾" : "👤"; // Calculate available width for message text (account for emoji + spacing) const availableWidth = Math.max(20, terminalWidth - 8); const displayText = truncateText( messageText.replace(/\n/g, " "), availableWidth, ); // Show conversation_id if exists, otherwise agent_id const idToShow = conversationIdFromMsg || agentIdFromMsg; // Use message id + index for guaranteed uniqueness (search can return same message multiple times) const msgId = "message_id" in msg ? String(msg.message_id) : "result"; const uniqueKey = `${msgId}-${actualIndex}`; return ( {isSelected ? ">" : " "} {emoji} {createdAt} {idToShow && ` · ${idToShow}`} ); }, )} )} {/* Footer */} {!expandedMessage && ( {results.length > 0 && ( {selectedIndex + 1}/{results.length} results )} Enter expand · ↑↓ navigate · Esc close )} ); }