|
| 1 | +import { create } from "zustand"; |
| 2 | +import { persist } from "zustand/middleware"; |
| 3 | +import { ChunkingConfig, ChunkingStrategy } from "../services/chunking"; |
| 4 | + |
| 5 | +export interface WebSource { |
| 6 | + id: string; |
| 7 | + url: string; |
| 8 | + enabled: boolean; |
| 9 | +} |
| 10 | + |
| 11 | +export interface KBDocumentSource { |
| 12 | + id: string; |
| 13 | + enabled: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +interface RAGPipelineState { |
| 17 | + // Active section and document selection |
| 18 | + activeSection: "sources" | "chunking" | "embedding" | "storage"; |
| 19 | + selectedDocumentId: string | undefined; |
| 20 | + |
| 21 | + // Knowledge sources |
| 22 | + webSources: WebSource[]; |
| 23 | + kbDocumentSources: KBDocumentSource[]; |
| 24 | + |
| 25 | + // Chunking configuration |
| 26 | + chunkingConfig: ChunkingConfig; |
| 27 | + |
| 28 | + // UI feedback state |
| 29 | + isChunking: boolean; |
| 30 | + chunkingProgress: number; // 0-100 |
| 31 | + chunkingError: string | null; |
| 32 | + chunkingCompleted: boolean; |
| 33 | + |
| 34 | + // Actions |
| 35 | + setActiveSection: (section: "sources" | "chunking" | "embedding" | "storage") => void; |
| 36 | + setSelectedDocumentId: (id: string | undefined) => void; |
| 37 | + |
| 38 | + // Web sources actions |
| 39 | + addWebSource: (url: string) => void; |
| 40 | + removeWebSource: (id: string) => void; |
| 41 | + toggleWebSource: (id: string) => void; |
| 42 | + updateWebSources: (sources: WebSource[]) => void; |
| 43 | + |
| 44 | + // KB document sources actions |
| 45 | + updateKBDocumentSources: (sources: KBDocumentSource[]) => void; |
| 46 | + toggleKBDocumentSource: (id: string) => void; |
| 47 | + |
| 48 | + // Chunking configuration actions |
| 49 | + updateChunkingConfig: (config: Partial<ChunkingConfig>) => void; |
| 50 | + setChunkingStrategy: (strategy: ChunkingStrategy) => void; |
| 51 | + setChunkSize: (size: number) => void; |
| 52 | + setChunkOverlap: (overlap: number) => void; |
| 53 | + setSeparators: (separators: string[]) => void; |
| 54 | + |
| 55 | + // UI feedback actions |
| 56 | + setChunkingState: (isChunking: boolean, progress?: number, error?: string | null) => void; |
| 57 | + setChunkingProgress: (progress: number) => void; |
| 58 | + setChunkingError: (error: string | null) => void; |
| 59 | + setChunkingCompleted: (completed: boolean) => void; |
| 60 | +} |
| 61 | + |
| 62 | +export const useRAGPipelineStore = create<RAGPipelineState>()( |
| 63 | + persist( |
| 64 | + (set, get) => ({ |
| 65 | + // Initial state |
| 66 | + activeSection: "sources", |
| 67 | + selectedDocumentId: undefined, |
| 68 | + webSources: [], |
| 69 | + kbDocumentSources: [], |
| 70 | + chunkingConfig: { |
| 71 | + strategy: "fixed-size", |
| 72 | + chunkSize: 1000, |
| 73 | + overlap: 100, |
| 74 | + separators: ["\n\n", "\n", ". ", " ", ""], |
| 75 | + }, |
| 76 | + isChunking: false, |
| 77 | + chunkingProgress: 0, |
| 78 | + chunkingError: null, |
| 79 | + chunkingCompleted: false, |
| 80 | + |
| 81 | + // Section and document actions |
| 82 | + setActiveSection: (section) => set({ activeSection: section }), |
| 83 | + setSelectedDocumentId: (id) => set({ selectedDocumentId: id }), |
| 84 | + |
| 85 | + // Web sources actions |
| 86 | + addWebSource: (url) => { |
| 87 | + const source: WebSource = { |
| 88 | + id: Date.now().toString(), |
| 89 | + url: url.trim(), |
| 90 | + enabled: true, |
| 91 | + }; |
| 92 | + set((state) => ({ |
| 93 | + webSources: [...state.webSources, source], |
| 94 | + })); |
| 95 | + }, |
| 96 | + |
| 97 | + removeWebSource: (id) => { |
| 98 | + set((state) => ({ |
| 99 | + webSources: state.webSources.filter(s => s.id !== id), |
| 100 | + })); |
| 101 | + }, |
| 102 | + |
| 103 | + toggleWebSource: (id) => { |
| 104 | + set((state) => ({ |
| 105 | + webSources: state.webSources.map(s => |
| 106 | + s.id === id ? { ...s, enabled: !s.enabled } : s |
| 107 | + ), |
| 108 | + })); |
| 109 | + }, |
| 110 | + |
| 111 | + updateWebSources: (sources) => set({ webSources: sources }), |
| 112 | + |
| 113 | + // KB document sources actions |
| 114 | + updateKBDocumentSources: (sources) => set({ kbDocumentSources: sources }), |
| 115 | + |
| 116 | + toggleKBDocumentSource: (id) => { |
| 117 | + set((state) => { |
| 118 | + const currentSource = state.kbDocumentSources.find(s => s.id === id); |
| 119 | + const willBeEnabled = !currentSource?.enabled; |
| 120 | + |
| 121 | + return { |
| 122 | + kbDocumentSources: state.kbDocumentSources.map(s => |
| 123 | + s.id === id ? { ...s, enabled: willBeEnabled } : { ...s, enabled: false } |
| 124 | + ), |
| 125 | + selectedDocumentId: willBeEnabled ? id : undefined, |
| 126 | + }; |
| 127 | + }); |
| 128 | + }, |
| 129 | + |
| 130 | + // Chunking configuration actions |
| 131 | + updateChunkingConfig: (config) => { |
| 132 | + set((state) => ({ |
| 133 | + chunkingConfig: { ...state.chunkingConfig, ...config }, |
| 134 | + })); |
| 135 | + }, |
| 136 | + |
| 137 | + setChunkingStrategy: (strategy) => { |
| 138 | + set((state) => ({ |
| 139 | + chunkingConfig: { ...state.chunkingConfig, strategy }, |
| 140 | + })); |
| 141 | + }, |
| 142 | + |
| 143 | + setChunkSize: (chunkSize) => { |
| 144 | + set((state) => ({ |
| 145 | + chunkingConfig: { ...state.chunkingConfig, chunkSize }, |
| 146 | + })); |
| 147 | + }, |
| 148 | + |
| 149 | + setChunkOverlap: (overlap) => { |
| 150 | + set((state) => ({ |
| 151 | + chunkingConfig: { ...state.chunkingConfig, overlap }, |
| 152 | + })); |
| 153 | + }, |
| 154 | + |
| 155 | + setSeparators: (separators) => { |
| 156 | + set((state) => ({ |
| 157 | + chunkingConfig: { ...state.chunkingConfig, separators }, |
| 158 | + })); |
| 159 | + }, |
| 160 | + |
| 161 | + // UI feedback actions |
| 162 | + setChunkingState: (isChunking, progress = 0, error = null) => { |
| 163 | + set({ isChunking, chunkingProgress: progress, chunkingError: error, chunkingCompleted: false }); |
| 164 | + }, |
| 165 | + |
| 166 | + setChunkingProgress: (progress) => { |
| 167 | + set({ chunkingProgress: Math.min(100, Math.max(0, progress)) }); |
| 168 | + }, |
| 169 | + |
| 170 | + setChunkingError: (error) => { |
| 171 | + set({ chunkingError: error, isChunking: false }); |
| 172 | + }, |
| 173 | + |
| 174 | + setChunkingCompleted: (completed) => { |
| 175 | + set({ chunkingCompleted: completed }); |
| 176 | + }, |
| 177 | + }), |
| 178 | + { |
| 179 | + name: "rag-pipeline-storage", |
| 180 | + partialize: (state) => ({ |
| 181 | + // Persist configuration and sources, but not UI state |
| 182 | + webSources: state.webSources, |
| 183 | + kbDocumentSources: state.kbDocumentSources, |
| 184 | + chunkingConfig: state.chunkingConfig, |
| 185 | + activeSection: state.activeSection, |
| 186 | + selectedDocumentId: state.selectedDocumentId, |
| 187 | + }), |
| 188 | + } |
| 189 | + ) |
| 190 | +); |
0 commit comments