| 1 | import { |
| 2 | AssistantUnrolledNonNullable, |
| 3 | ConfigValidationError, |
| 4 | } from "@continuedev/config-yaml"; |
| 5 | import { IContextProvider, IdeType } from ".."; |
| 6 | import { contextProviderClassFromName } from "../context/providers"; |
| 7 | import CurrentFileContextProvider from "../context/providers/CurrentFileContextProvider"; |
| 8 | import DiffContextProvider from "../context/providers/DiffContextProvider"; |
| 9 | import DocsContextProvider from "../context/providers/DocsContextProvider"; |
| 10 | import FileContextProvider from "../context/providers/FileContextProvider"; |
| 11 | import ProblemsContextProvider from "../context/providers/ProblemsContextProvider"; |
| 12 | import RulesContextProvider from "../context/providers/RulesContextProvider"; |
| 13 | import TerminalContextProvider from "../context/providers/TerminalContextProvider"; |
| 14 | |
| 15 | /* |
| 16 | Loads context providers based on configuration |
| 17 | - default providers will always be loaded, using config params if present |
| 18 | - other providers will be loaded if configured |
| 19 | |
| 20 | NOTE the MCPContextProvider is added in doLoadConfig if any resources are present |
| 21 | */ |
| 22 | export function loadConfigContextProviders( |
| 23 | configContext: AssistantUnrolledNonNullable["context"], |
| 24 | hasDocs: boolean, |
| 25 | ideType: IdeType, |
| 26 | ): { |
| 27 | providers: IContextProvider[]; |
| 28 | errors: ConfigValidationError[]; |
| 29 | } { |
| 30 | const providers: IContextProvider[] = []; |
| 31 | const errors: ConfigValidationError[] = []; |
| 32 | |
| 33 | const defaultProviders: IContextProvider[] = [ |
| 34 | new FileContextProvider({}), |
| 35 | new CurrentFileContextProvider({}), |
| 36 | new DiffContextProvider({}), |
| 37 | new TerminalContextProvider({}), |
| 38 | new ProblemsContextProvider({}), |
| 39 | new RulesContextProvider({}), |
| 40 | ]; |
| 41 | |
| 42 | // Add from config |
| 43 | if (configContext) { |
| 44 | for (const config of configContext) { |
| 45 | const cls = contextProviderClassFromName(config.provider) as any; |
| 46 | if ( |
| 47 | !cls && |
| 48 | !defaultProviders.find((p) => p.description.title === config.provider) |
| 49 | ) { |
| 50 | errors.push({ |
| 51 | fatal: false, |
| 52 | message: `Unknown context provider ${config.provider}`, |
| 53 | }); |
| 54 | continue; |
| 55 | } |
| 56 | providers.push( |
| 57 | new cls({ |
| 58 | name: config.name, |
| 59 | ...config.params, |
| 60 | }), |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Add from defaults if not found in config |
| 66 | for (const defaultProvider of defaultProviders) { |
| 67 | if ( |
| 68 | !providers.find( |
| 69 | (p) => p.description.title === defaultProvider.description.title, |
| 70 | ) |
| 71 | ) { |
| 72 | providers.push(defaultProvider); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (hasDocs && !providers?.some((cp) => cp.description.title === "docs")) { |
| 77 | providers.push(new DocsContextProvider({})); |
| 78 | } |
| 79 | |
| 80 | // @problems and @terminal are not supported in jetbrains |
| 81 | const filteredProviders = providers.filter((pv) => { |
| 82 | if (ideType === "jetbrains") { |
| 83 | return ( |
| 84 | pv.description.title !== TerminalContextProvider.description.title && |
| 85 | pv.description.title !== ProblemsContextProvider.description.title |
| 86 | ); |
| 87 | } |
| 88 | return true; |
| 89 | }); |
| 90 | |
| 91 | return { |
| 92 | providers: filteredProviders, |
| 93 | errors, |
| 94 | }; |
| 95 | } |