| 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | import { ChatHistoryItem, ContextItemWithId } from "core"; |
| 3 | import { CodeBlock } from "../../components/mainInput/TipTapEditor/extensions"; |
| 4 | import { updateFileSymbols } from "../slices/sessionSlice"; |
| 5 | import { ThunkApiType } from "../store"; |
| 6 | |
| 7 | export function getContextItemsFromHistory( |
| 8 | historyItems: ChatHistoryItem[], |
| 9 | priorToIndex?: number, |
| 10 | ) { |
| 11 | const pastHistoryItems = historyItems.filter( |
| 12 | (_, i) => i <= (priorToIndex ?? historyItems.length - 1), |
| 13 | ); |
| 14 | |
| 15 | const pastNormalContextItems = pastHistoryItems.flatMap((item) => { |
| 16 | return ( |
| 17 | item.contextItems?.filter( |
| 18 | (item) => item.uri?.type === "file" && item.uri?.value, |
| 19 | ) ?? [] |
| 20 | ); |
| 21 | }); |
| 22 | const pastToolbarContextItems: ContextItemWithId[] = pastHistoryItems |
| 23 | .filter( |
| 24 | (item) => item.editorState && Array.isArray(item.editorState.content), |
| 25 | ) |
| 26 | .flatMap((item) => item.editorState.content) |
| 27 | .filter( |
| 28 | (content) => |
| 29 | content?.type === CodeBlock.name && |
| 30 | content?.attrs?.item?.uri?.value && |
| 31 | content.attrs.item.uri?.type === "file", |
| 32 | ) |
| 33 | .map((content) => content.attrs!.item!); |
| 34 | |
| 35 | return [...pastNormalContextItems, ...pastToolbarContextItems]; |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | Get file symbols for given context items |
| 40 | Overwrite symbols for existing file matches |
| 41 | */ |
| 42 | export const updateFileSymbolsFromFiles = createAsyncThunk< |
| 43 | void, |
| 44 | string[], |
| 45 | ThunkApiType |
| 46 | >( |
| 47 | "symbols/updateFromContextItems", |
| 48 | async (filepaths, { dispatch, extra, getState }) => { |
| 49 | try { |
| 50 | // Get unique file uris from context items |
| 51 | const uniqueUris = Array.from(new Set(filepaths)); |
| 52 | |
| 53 | // Update symbols for those files |
| 54 | if (uniqueUris.length > 0) { |
| 55 | const result = await extra.ideMessenger.request( |
| 56 | "context/getSymbolsForFiles", |
| 57 | { uris: uniqueUris }, |
| 58 | ); |
| 59 | if (result.status === "error") { |
| 60 | throw new Error(result.error); |
| 61 | } |
| 62 | dispatch(updateFileSymbols(result.content)); |
| 63 | } |
| 64 | } catch (e) { |
| 65 | console.error("Error updating file symbols from filepaths", e, filepaths); |
| 66 | } |
| 67 | }, |
| 68 | ); |
| 69 | |
| 70 | /* |
| 71 | - Update file symbols for all context items in history |
| 72 | - That don't already have symbols |
| 73 | */ |
| 74 | export const updateFileSymbolsFromHistory = createAsyncThunk< |
| 75 | void, |
| 76 | undefined, |
| 77 | ThunkApiType |
| 78 | >("symbols/updateFromHistory", async (_, { dispatch, extra, getState }) => { |
| 79 | try { |
| 80 | const state = getState(); |
| 81 | |
| 82 | // Get unique context item file uris from all history |
| 83 | const contextItems = getContextItemsFromHistory(state.session.history); |
| 84 | |
| 85 | const uniqueUris = new Set( |
| 86 | contextItems |
| 87 | .filter((item) => item?.uri?.type === "file" && item?.uri?.value) |
| 88 | .map((item) => item.uri!.value), |
| 89 | ); |
| 90 | |
| 91 | // Remove if already in symbols |
| 92 | Object.keys(state.session.symbols).forEach((key) => { |
| 93 | uniqueUris.delete(key); |
| 94 | }); |
| 95 | |
| 96 | const uriArray = Array.from(uniqueUris); |
| 97 | |
| 98 | // And then update symbols for those files |
| 99 | if (uriArray.length > 0) { |
| 100 | const result = await extra.ideMessenger.request( |
| 101 | "context/getSymbolsForFiles", |
| 102 | { uris: uriArray }, |
| 103 | ); |
| 104 | if (result.status === "error") { |
| 105 | throw new Error(result.error); |
| 106 | } |
| 107 | dispatch(updateFileSymbols(result.content)); |
| 108 | } |
| 109 | } catch (e) { |
| 110 | // Catch all - don't want file symbols to break the chat experience for now |
| 111 | console.error("Error updating file symbols from context items", e); |
| 112 | } |
| 113 | }); |