| 1 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 2 | import { JSONContent } from "@tiptap/core"; |
| 3 | import { |
| 4 | ContextItemWithId, |
| 5 | InputModifiers, |
| 6 | MessageContent, |
| 7 | RangeInFile, |
| 8 | SlashCommandDescription, |
| 9 | } from "core"; |
| 10 | import * as URI from "uri-js"; |
| 11 | import { resolveEditorContent } from "../../components/mainInput/TipTapEditor"; |
| 12 | import { selectSelectedChatModel } from "../slices/configSlice"; |
| 13 | import { ThunkApiType } from "../store"; |
| 14 | |
| 15 | export const gatherContext = createAsyncThunk< |
| 16 | { |
| 17 | selectedContextItems: ContextItemWithId[]; |
| 18 | selectedCode: RangeInFile[]; |
| 19 | content: MessageContent; |
| 20 | slashCommandWithInput: |
| 21 | | { |
| 22 | command: SlashCommandDescription; |
| 23 | input: string; |
| 24 | } |
| 25 | | undefined; |
| 26 | }, |
| 27 | { |
| 28 | editorState: JSONContent; |
| 29 | modifiers: InputModifiers; |
| 30 | promptPreamble?: string; |
| 31 | }, |
| 32 | ThunkApiType |
| 33 | >( |
| 34 | "chat/gatherContext", |
| 35 | async ( |
| 36 | { modifiers, editorState, promptPreamble }, |
| 37 | { dispatch, extra, getState }, |
| 38 | ) => { |
| 39 | const state = getState(); |
| 40 | const selectedChatModel = selectSelectedChatModel(state); |
| 41 | |
| 42 | const defaultContextProviders = |
| 43 | state.config.config.experimental?.defaultContext ?? []; |
| 44 | |
| 45 | if (!selectedChatModel) { |
| 46 | console.error( |
| 47 | "gatherContext thunk: Cannot gather context, no model selected", |
| 48 | ); |
| 49 | throw new Error("No chat model selected"); |
| 50 | } |
| 51 | |
| 52 | // Resolve context providers and construct new history |
| 53 | let [selectedContextItems, selectedCode, content, slashCommandWithInput] = |
| 54 | await resolveEditorContent({ |
| 55 | editorState, |
| 56 | modifiers, |
| 57 | ideMessenger: extra.ideMessenger, |
| 58 | defaultContextProviders, |
| 59 | availableSlashCommands: state.config.config.slashCommands, |
| 60 | dispatch, |
| 61 | }); |
| 62 | |
| 63 | // Automatically use currently open file |
| 64 | if (!modifiers.noContext) { |
| 65 | const usingFreeTrial = false; // TODO no longer tracking free trial count, need to hook up to hub |
| 66 | |
| 67 | const currentFileResponse = await extra.ideMessenger.request( |
| 68 | "context/getContextItems", |
| 69 | { |
| 70 | name: "currentFile", |
| 71 | query: "non-mention-usage", |
| 72 | fullInput: "", |
| 73 | selectedCode: [], |
| 74 | }, |
| 75 | ); |
| 76 | if (currentFileResponse.status === "success") { |
| 77 | const items = currentFileResponse.content; |
| 78 | if (items.length > 0) { |
| 79 | const currentFile = items[0]; |
| 80 | const uri = currentFile.uri?.value; |
| 81 | |
| 82 | // don't add the file if it's already in the context items |
| 83 | if ( |
| 84 | uri && |
| 85 | !selectedContextItems.find( |
| 86 | (item) => item.uri?.value && URI.equal(item.uri.value, uri), |
| 87 | ) |
| 88 | ) { |
| 89 | // Limit to 1000 lines if using free trial |
| 90 | if (usingFreeTrial) { |
| 91 | currentFile.content = currentFile.content |
| 92 | .split("\n") |
| 93 | .slice(0, 1000) |
| 94 | .join("\n"); |
| 95 | if (!currentFile.content.endsWith("```")) { |
| 96 | currentFile.content += "\n```"; |
| 97 | } |
| 98 | } |
| 99 | currentFile.id = { |
| 100 | providerTitle: "file", |
| 101 | itemId: uri, |
| 102 | }; |
| 103 | selectedContextItems.unshift(currentFile); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (promptPreamble) { |
| 110 | if (typeof content === "string") { |
| 111 | content = promptPreamble + content; |
| 112 | } else if (content[0].type === "text") { |
| 113 | content[0].text = promptPreamble + content[0].text; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return { |
| 118 | selectedContextItems, |
| 119 | selectedCode, |
| 120 | content, |
| 121 | slashCommandWithInput, |
| 122 | }; |
| 123 | }, |
| 124 | ); |