| 96 | }; |
| 97 | |
| 98 | export const SessionProvider: React.FC<{ children: ReactNode }> = ({ children }) => { |
| 99 | const [projects, setProjects] = useState<Project[]>([]); |
| 100 | const [sessions, setSessions] = useState<Session[]>([]); |
| 101 | const [currentSession, setCurrentSession] = useState<Session | null>(null); |
| 102 | const [messages, setMessages] = useState<Message[]>([]); |
| 103 | const [logs, setLogs] = useState<LogEntry[]>([]); |
| 104 | const [streamingContent, setStreamingContent] = useState(''); |
| 105 | const [isStreaming, setIsStreaming] = useState(false); |
| 106 | const [isLoading, setIsLoading] = useState(false); |
| 107 | const [ws, setWs] = useState<WebSocket | null>(null); |
| 108 | const pendingQueryRef = useRef<string | null>(null); |
| 109 | const prefixHandlersRef = useRef<Map<string, Set<(data: Record<string, unknown>) => void>>>(new Map()); |
| 110 | const restoreAttemptedRef = useRef(false); |
| 111 | |
| 112 | // Load projects |
| 113 | const loadProjects = useCallback(async () => { |
| 114 | try { |
| 115 | const response = await fetch(`${API_BASE}/projects`); |
| 116 | if (response.ok) { |
| 117 | const data = await response.json(); |
| 118 | const projectsWithOverrides: Project[] = (Array.isArray(data) ? data : []).map((project: Project) => { |
| 119 | const overrideDescription = project?.id ? PROJECT_DESCRIPTION_OVERRIDES[project.id] : undefined; |
| 120 | if (overrideDescription) { |
| 121 | return { ...project, description: overrideDescription }; |
| 122 | } |
| 123 | return project; |
| 124 | }); |
| 125 | setProjects(projectsWithOverrides); |
| 126 | } |
| 127 | } catch (error) { |
| 128 | console.error('Failed to load projects:', error); |
| 129 | } |
| 130 | }, []); |
| 131 | |
| 132 | const loadSessions = useCallback(async () => { |
| 133 | try { |
| 134 | const response = await fetch(`${API_BASE}/sessions`); |
| 135 | if (response.ok) { |
| 136 | const data = await response.json(); |
| 137 | setSessions(Array.isArray(data) ? data : []); |
| 138 | } |
| 139 | } catch (error) { |
| 140 | console.error('Failed to load sessions:', error); |
| 141 | } |
| 142 | }, []); |
| 143 | |
| 144 | // Create session |
| 145 | const createSession = useCallback(async (projectId: string, workflowType: string = 'standard'): Promise<Session | null> => { |
| 146 | try { |
| 147 | const response = await fetch(`${API_BASE}/sessions`, { |
| 148 | method: 'POST', |
| 149 | headers: { 'Content-Type': 'application/json' }, |
| 150 | body: JSON.stringify({ |
| 151 | project_id: projectId, |
| 152 | workflow_type: workflowType, |
| 153 | session_type: 'project' |
| 154 | }), |
| 155 | }); |