| 1 | import { fetchwithRequestOptions } from "@continuedev/fetch"; |
| 2 | import { ContinueConfig, IDE } from "core"; |
| 3 | import { QuickPickItem, window } from "vscode"; |
| 4 | |
| 5 | export async function getContextProviderItems({ |
| 6 | contextProviders, |
| 7 | }: ContinueConfig): Promise<QuickPickItem[]> { |
| 8 | if (!contextProviders) { |
| 9 | return []; |
| 10 | } |
| 11 | |
| 12 | const quickPickItems = contextProviders |
| 13 | .filter((provider) => provider.description.type === "normal") |
| 14 | .map((provider) => { |
| 15 | return { |
| 16 | label: provider.description.displayTitle, |
| 17 | detail: provider.description.description, |
| 18 | }; |
| 19 | }); |
| 20 | |
| 21 | return quickPickItems; |
| 22 | } |
| 23 | |
| 24 | export async function getContextProvidersString( |
| 25 | selectedProviders: QuickPickItem[] | undefined, |
| 26 | config: ContinueConfig, |
| 27 | ide: IDE, |
| 28 | ): Promise<string> { |
| 29 | const contextItems = ( |
| 30 | await Promise.all( |
| 31 | selectedProviders?.map((selectedProvider) => { |
| 32 | const provider = config.contextProviders?.find( |
| 33 | (provider) => |
| 34 | provider.description.displayTitle === selectedProvider.label, |
| 35 | ); |
| 36 | |
| 37 | if (!provider || !config.selectedModelByRole.chat) { |
| 38 | return []; |
| 39 | } |
| 40 | |
| 41 | return provider.getContextItems("", { |
| 42 | config, |
| 43 | ide, |
| 44 | embeddingsProvider: config.selectedModelByRole.embed, |
| 45 | reranker: config.selectedModelByRole.rerank, |
| 46 | llm: config.selectedModelByRole.chat, |
| 47 | fullInput: "", |
| 48 | selectedCode: [], |
| 49 | fetch: (url, init) => |
| 50 | fetchwithRequestOptions(url, init, config.requestOptions), |
| 51 | isInAgentMode: false, |
| 52 | }); |
| 53 | }) || [], |
| 54 | ) |
| 55 | ).flat(); |
| 56 | |
| 57 | return contextItems.map((item) => item.content).join("\n\n") + "\n\n---\n\n"; |
| 58 | } |
| 59 | |
| 60 | export async function getContextProviderQuickPickVal( |
| 61 | config: ContinueConfig, |
| 62 | ide: IDE, |
| 63 | ) { |
| 64 | const contextProviderItems = await getContextProviderItems(config); |
| 65 | |
| 66 | const quickPick = window.createQuickPick(); |
| 67 | |
| 68 | quickPick.items = contextProviderItems; |
| 69 | quickPick.title = "Context providers"; |
| 70 | quickPick.placeholder = "Select a context provider to add to your prompt"; |
| 71 | quickPick.canSelectMany = true; |
| 72 | |
| 73 | quickPick.show(); |
| 74 | |
| 75 | const val = await new Promise<string>((resolve) => { |
| 76 | quickPick.onDidAccept(async () => { |
| 77 | const selectedItems = Array.from(quickPick.selectedItems); |
| 78 | getContextProvidersString(selectedItems, config, ide) |
| 79 | .then(resolve) |
| 80 | .catch((e) => { |
| 81 | console.warn(`Fail to get context providers: ${e}`); |
| 82 | resolve(""); |
| 83 | }); |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | quickPick.dispose(); |
| 88 | |
| 89 | return val; |
| 90 | } |