(selectedGroupId: string | null = null)
| 132 | } |
| 133 | |
| 134 | export function getChatSidebarState(selectedGroupId: string | null = null): ChatSidebarState { |
| 135 | ensureChatStateSchema() |
| 136 | const db = getThreadStateDatabase() |
| 137 | const groups: ChatSidebarState['groups'] = ( |
| 138 | db |
| 139 | .prepare( |
| 140 | ` |
| 141 | SELECT id, name, order_index AS orderIndex, collapsed |
| 142 | FROM chat_groups |
| 143 | ORDER BY order_index ASC, name COLLATE NOCASE ASC |
| 144 | `, |
| 145 | ) |
| 146 | .all() as ChatGroupRow[] |
| 147 | ).map((group) => ({ |
| 148 | id: group.id, |
| 149 | name: group.name, |
| 150 | orderIndex: group.orderIndex, |
| 151 | collapsed: Boolean(group.collapsed), |
| 152 | threads: [], |
| 153 | })) |
| 154 | |
| 155 | const rows = db |
| 156 | .prepare( |
| 157 | ` |
| 158 | SELECT |
| 159 | threads.id AS id, |
| 160 | threads.cwd AS projectId, |
| 161 | threads.title AS title, |
| 162 | threads.session_path AS sessionPath, |
| 163 | COALESCE(inbox_items.last_assistant_preview, threads.last_assistant_preview) AS summary, |
| 164 | threads.running AS running, |
| 165 | COALESCE(inbox_items.unread, 0) AS unread, |
| 166 | threads.pinned AS pinned, |
| 167 | threads.last_modified_ms AS lastModifiedMs, |
| 168 | chat_threads.group_id AS groupId |
| 169 | FROM threads |
| 170 | LEFT JOIN chat_threads ON chat_threads.session_path = threads.session_path |
| 171 | LEFT JOIN inbox_items ON inbox_items.session_path = threads.session_path |
| 172 | WHERE threads.archived = 0 |
| 173 | ORDER BY threads.pinned DESC, COALESCE(chat_threads.order_index, threads.last_modified_ms) DESC, threads.title COLLATE NOCASE ASC |
| 174 | `, |
| 175 | ) |
| 176 | .all() as ChatThreadGroupRow[] |
| 177 | |
| 178 | const chatRows = rows.filter((row) => isChatSessionPath(row.sessionPath)) |
| 179 | |
| 180 | const groupsById = new Map(groups.map((group) => [group.id, group])) |
| 181 | const ungroupedThreads: ChatThread[] = [] |
| 182 | for (const row of chatRows) { |
| 183 | const thread = mapChatThreadRow(row) |
| 184 | const group = thread.groupId ? groupsById.get(thread.groupId) : null |
| 185 | if (group) group.threads.push(thread) |
| 186 | else ungroupedThreads.push(thread) |
| 187 | } |
| 188 | |
| 189 | return { groups, ungroupedThreads, selectedGroupId } |
| 190 | } |
no test coverage detected