feat(search): add PostgreSQL FTS on AI sessions with Command Palette integration

- Migration: add generated tsvector column + GIN index on ai_sessions (problem_summary, resolution_summary, escalation_reason, problem_domain)
- Backend: wire FTS into list_sessions q param; add GET /ai-sessions/search endpoint returning AISessionSearchResult (registered before /{session_id} to avoid UUID routing conflict)
- Frontend: add AISessionSearchResult type, aiSessionsApi.search() method, and Command Palette group "FlowPilot Sessions" using Zap icon navigating to /pilot/:id

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-20 03:42:01 +00:00
parent c3afc7a059
commit ce68fa84ca
6 changed files with 148 additions and 7 deletions

View File

@@ -2,12 +2,14 @@ import { useState, useEffect, useRef, useCallback, useMemo } from 'react'
import { useNavigate } from 'react-router-dom'
import {
Search, Loader2, ArrowRight, FileText, Clock,
Sparkles, LayoutDashboard, Tag, Plus, BookOpen, Terminal,
Sparkles, LayoutDashboard, Tag, Plus, BookOpen, Terminal, Zap,
} from 'lucide-react'
import { treesApi } from '@/api/trees'
import { sessionsApi } from '@/api/sessions'
import { aiSessionsApi } from '@/api/aiSessions'
import type { TreeListItem } from '@/types'
import type { Session } from '@/types/session'
import type { AISessionSearchResult } from '@/types/ai-session'
import { getTreeNavigatePath } from '@/lib/routing'
import { cn } from '@/lib/utils'
import { detectIntent } from '@/lib/paletteIntent'
@@ -19,7 +21,7 @@ interface CommandPaletteProps {
onClose: () => void
}
type GroupType = 'flowpilot' | 'pages' | 'flows' | 'sessions' | 'tags' | 'quick-actions' | 'recent-flows'
type GroupType = 'flowpilot' | 'pages' | 'flows' | 'sessions' | 'ai-sessions' | 'tags' | 'quick-actions' | 'recent-flows'
interface PaletteItem {
id: string
@@ -27,7 +29,7 @@ interface PaletteItem {
title: string
subtitle?: string
path: string
icon: 'sparkles' | 'tree' | 'session' | 'page' | 'tag' | 'action' | 'recent'
icon: 'sparkles' | 'tree' | 'session' | 'ai-session' | 'page' | 'tag' | 'action' | 'recent'
}
interface Group {
@@ -63,6 +65,7 @@ function ItemIcon({ icon, className }: { icon: PaletteItem['icon'], className?:
case 'sparkles': return <Sparkles size={16} className={cls} />
case 'tree': return <FileText size={16} className={cls} />
case 'session': return <Clock size={16} className={cls} />
case 'ai-session': return <Zap size={16} className={cls} />
case 'page': return <LayoutDashboard size={16} className={cls} />
case 'tag': return <Tag size={16} className={cls} />
case 'action': return <Plus size={16} className={cls} />
@@ -80,6 +83,7 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
const [selectedIndex, setSelectedIndex] = useState(0)
const [searchFlows, setSearchFlows] = useState<TreeListItem[]>([])
const [searchSessions, setSearchSessions] = useState<Session[]>([])
const [searchAISessions, setSearchAISessions] = useState<AISessionSearchResult[]>([])
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null)
// Focus input when opened
@@ -88,6 +92,7 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
setQuery('')
setSearchFlows([])
setSearchSessions([])
setSearchAISessions([])
setSelectedIndex(0)
setTimeout(() => inputRef.current?.focus(), 50)
}
@@ -109,15 +114,17 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
if (query.trim().length < 2) {
setSearchFlows([])
setSearchSessions([])
setSearchAISessions([])
setIsSearching(false)
return
}
setIsSearching(true)
debounceRef.current = setTimeout(async () => {
try {
const [flows, sessions] = await Promise.all([
const [flows, sessions, aiSessions] = await Promise.all([
treesApi.search(query, 6),
sessionsApi.list({ size: 5 }).catch(() => [] as Session[]),
aiSessionsApi.search(query, 5).catch(() => [] as AISessionSearchResult[]),
])
setSearchFlows(flows)
// Filter sessions by tree name
@@ -125,9 +132,11 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
s.tree_snapshot?.name?.toLowerCase().includes(query.toLowerCase())
).slice(0, 3)
setSearchSessions(filtered)
setSearchAISessions(aiSessions)
} catch {
setSearchFlows([])
setSearchSessions([])
setSearchAISessions([])
} finally {
setIsSearching(false)
}
@@ -216,6 +225,23 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
icon: 'session' as const,
}))
// Build AI session items
const aiSessionItems: PaletteItem[] = searchAISessions.map(s => {
const title = s.problem_summary
? s.problem_summary.slice(0, 60) + (s.problem_summary.length > 60 ? '…' : '')
: 'FlowPilot Session'
const statusLabel = s.status === 'resolved' ? 'Resolved' : s.status === 'escalated' ? 'Escalated' : 'Active'
const subtitle = [s.problem_domain, statusLabel].filter(Boolean).join(' · ')
return {
id: `ai-session-${s.id}`,
group: 'ai-sessions' as GroupType,
title,
subtitle: subtitle || undefined,
path: `/pilot/${s.id}`,
icon: 'ai-session' as const,
}
})
const result: Group[] = []
if (intent === 'question') {
@@ -223,12 +249,14 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
result.push({ type: 'flowpilot', label: 'AI Assistant', items: [flowPilotItem] })
if (flowItems.length > 0) result.push({ type: 'flows', label: 'Flows', items: flowItems })
if (sessionItems.length > 0) result.push({ type: 'sessions', label: 'Sessions', items: sessionItems })
if (aiSessionItems.length > 0) result.push({ type: 'ai-sessions', label: 'FlowPilot Sessions', items: aiSessionItems })
if (tagItems.length > 0) result.push({ type: 'tags', label: 'Tags', items: tagItems })
} else if (intent === 'page') {
// Pages first, FlowPilot at bottom
if (filteredPages.length > 0) result.push({ type: 'pages', label: 'Pages', items: filteredPages })
if (flowItems.length > 0) result.push({ type: 'flows', label: 'Flows', items: flowItems })
if (sessionItems.length > 0) result.push({ type: 'sessions', label: 'Sessions', items: sessionItems })
if (aiSessionItems.length > 0) result.push({ type: 'ai-sessions', label: 'FlowPilot Sessions', items: aiSessionItems })
if (tagItems.length > 0) result.push({ type: 'tags', label: 'Tags', items: tagItems })
result.push({ type: 'flowpilot', label: 'AI Assistant', items: [flowPilotItem] })
} else {
@@ -236,12 +264,13 @@ export function CommandPalette({ open, onClose }: CommandPaletteProps) {
result.push({ type: 'flowpilot', label: 'AI Assistant', items: [flowPilotItem] })
if (flowItems.length > 0) result.push({ type: 'flows', label: 'Flows', items: flowItems })
if (sessionItems.length > 0) result.push({ type: 'sessions', label: 'Sessions', items: sessionItems })
if (aiSessionItems.length > 0) result.push({ type: 'ai-sessions', label: 'FlowPilot Sessions', items: aiSessionItems })
if (tagItems.length > 0) result.push({ type: 'tags', label: 'Tags', items: tagItems })
if (filteredPages.length > 0) result.push({ type: 'pages', label: 'Pages', items: filteredPages })
}
return result
}, [query, searchFlows, searchSessions, user])
}, [query, searchFlows, searchSessions, searchAISessions, user])
// Flatten all items for keyboard navigation
const flatItems: PaletteItem[] = builtGroups.flatMap(g => g.items)