Elffuss BraidHub

kikocis/continue public

5fda066adb4e · 2.8 KiBRawPermalink
1import {
2 AssistantUnrolledNonNullable,
3 ConfigValidationError,
4} from "@continuedev/config-yaml";
5import { IContextProvider, IdeType } from "..";
6import { contextProviderClassFromName } from "../context/providers";
7import CurrentFileContextProvider from "../context/providers/CurrentFileContextProvider";
8import DiffContextProvider from "../context/providers/DiffContextProvider";
9import DocsContextProvider from "../context/providers/DocsContextProvider";
10import FileContextProvider from "../context/providers/FileContextProvider";
11import ProblemsContextProvider from "../context/providers/ProblemsContextProvider";
12import RulesContextProvider from "../context/providers/RulesContextProvider";
13import 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*/
22export 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 // Add from config
33 if (configContext) {
34 for (const config of configContext) {
35 const cls = contextProviderClassFromName(config.provider) as any;
36 if (!cls) {
37 errors.push({
38 fatal: false,
39 message: `Unknown context provider ${config.provider}`,
40 });
41 continue;
42 }
43 providers.push(
44 new cls({
45 name: config.name,
46 ...config.params,
47 }),
48 );
49 }
50 }
51
52 // Add from defaults if not found in config
53 const defaultProviders: IContextProvider[] = [
54 new FileContextProvider({}),
55 new CurrentFileContextProvider({}),
56 new DiffContextProvider({}),
57 new TerminalContextProvider({}),
58 new ProblemsContextProvider({}),
59 new RulesContextProvider({}),
60 ];
61
62 for (const defaultProvider of defaultProviders) {
63 if (
64 !providers.find(
65 (p) => p.description.title === defaultProvider.description.title,
66 )
67 ) {
68 providers.push(defaultProvider);
69 }
70 }
71
72 if (hasDocs && !providers?.some((cp) => cp.description.title === "docs")) {
73 providers.push(new DocsContextProvider({}));
74 }
75
76 // @problems and @terminal are not supported in jetbrains
77 const filteredProviders = providers.filter((pv) => {
78 if (ideType === "jetbrains") {
79 return (
80 pv.description.title !== TerminalContextProvider.description.title &&
81 pv.description.title !== ProblemsContextProvider.description.title
82 );
83 }
84 return true;
85 });
86
87 return {
88 providers: filteredProviders,
89 errors,
90 };
91}