| 1 | import { createSelector } from "@reduxjs/toolkit"; |
| 2 | import { ToolStatus } from "core"; |
| 3 | import { RootState } from "../store"; |
| 4 | import { |
| 5 | findAllCurToolCalls, |
| 6 | findAllCurToolCallsByStatus, |
| 7 | findToolCallById, |
| 8 | hasCurrentToolCalls, |
| 9 | } from "../util"; |
| 10 | |
| 11 | // Primary selectors for tool calls |
| 12 | export const selectCurrentToolCalls = createSelector( |
| 13 | (store: RootState) => store.session.history, |
| 14 | (history) => findAllCurToolCalls(history), |
| 15 | ); |
| 16 | |
| 17 | export const selectHasCurrentToolCalls = createSelector( |
| 18 | (store: RootState) => store.session.history, |
| 19 | (history) => hasCurrentToolCalls(history), |
| 20 | ); |
| 21 | |
| 22 | export const selectToolCallsByStatus = createSelector( |
| 23 | [ |
| 24 | (store: RootState) => store.session.history, |
| 25 | (_store: RootState, status: ToolStatus) => status, |
| 26 | ], |
| 27 | (history, status) => findAllCurToolCallsByStatus(history, status), |
| 28 | ); |
| 29 | |
| 30 | export const selectFirstPendingToolCall = createSelector( |
| 31 | (store: RootState) => store.session.history, |
| 32 | (history) => { |
| 33 | const pendingToolCalls = findAllCurToolCallsByStatus(history, "generated"); |
| 34 | return pendingToolCalls[0] || undefined; |
| 35 | }, |
| 36 | ); |
| 37 | |
| 38 | // ID-based selectors for specific tool calls |
| 39 | export const selectToolCallById = createSelector( |
| 40 | [ |
| 41 | (store: RootState) => store.session.history, |
| 42 | (_store: RootState, toolCallId: string) => toolCallId, |
| 43 | ], |
| 44 | (history, toolCallId) => findToolCallById(history, toolCallId), |
| 45 | ); |
| 46 | |
| 47 | export const selectApplyStateByToolCallId = createSelector( |
| 48 | [ |
| 49 | (store: RootState) => store.session.codeBlockApplyStates, |
| 50 | (_store: RootState, toolCallId: string) => toolCallId, |
| 51 | ], |
| 52 | (applyStates, toolCallId) => { |
| 53 | return applyStates.states.findLast( |
| 54 | (state) => state.toolCallId === toolCallId, |
| 55 | ); |
| 56 | }, |
| 57 | ); |
| 58 | |
| 59 | // Status-specific convenience selectors |
| 60 | export const selectPendingToolCalls = createSelector( |
| 61 | (store: RootState) => store.session.history, |
| 62 | (history) => findAllCurToolCallsByStatus(history, "generated"), |
| 63 | ); |
| 64 | |
| 65 | export const selectDoneApplyStates = createSelector( |
| 66 | (store: RootState) => store.session.codeBlockApplyStates.states, |
| 67 | (states) => states.filter((applyState) => applyState.status === "done"), |
| 68 | ); |