Elffuss BraidHub

kikocis/continue public

main · 5.5 KiBRawPermalink
1import fs from "fs";
2import os from "os";
3
4import { ModelConfig } from "@continuedev/config-yaml";
5import {
6 ContinueConfig,
7 ExperimentalModelRoles,
8 IDE,
9 ILLM,
10 JSONModelDescription,
11 PromptTemplate,
12} from "../";
13import { GlobalContext } from "../util/GlobalContext";
14import { editConfigFile } from "../util/paths";
15
16function stringify(obj: any, indentation?: number): string {
17 return JSON.stringify(
18 obj,
19 (key, value) => {
20 return value === null ? undefined : value;
21 },
22 indentation,
23 );
24}
25
26export function addModel(
27 model: JSONModelDescription,
28 role?: keyof ExperimentalModelRoles,
29) {
30 editConfigFile(
31 (config) => {
32 if (config.models?.some((m) => stringify(m) === stringify(model))) {
33 return config;
34 }
35
36 const numMatches = config.models?.reduce(
37 (prev, curr) => (curr.title.startsWith(model.title) ? prev + 1 : prev),
38 0,
39 );
40 if (numMatches !== undefined && numMatches > 0) {
41 model.title = `${model.title} (${numMatches})`;
42 }
43
44 config.models.push(model);
45
46 // Set the role for the model
47 if (role) {
48 if (!config.experimental) {
49 config.experimental = {};
50 }
51 if (!config.experimental.modelRoles) {
52 config.experimental.modelRoles = {};
53 }
54 config.experimental.modelRoles[role] = model.title;
55 }
56
57 return config;
58 },
59 (config) => {
60 const numMatches = config.models?.reduce(
61 (prev, curr) =>
62 "name" in curr && curr.name.startsWith(model.title) ? prev + 1 : prev,
63 0,
64 );
65 if (numMatches !== undefined && numMatches > 0) {
66 model.title = `${model.title} (${numMatches})`;
67 }
68
69 if (!config.models) {
70 config.models = [];
71 }
72
73 const desc: ModelConfig = {
74 name: model.title,
75 provider: model.provider,
76 model: model.model,
77 apiKey: model.apiKey,
78 apiBase: model.apiBase,
79 maxStopWords: model.maxStopWords,
80 defaultCompletionOptions: model.completionOptions,
81 };
82 config.models.push(desc);
83 return config;
84 },
85 );
86}
87
88export function deleteModel(title: string) {
89 editConfigFile(
90 (config) => {
91 config.models = config.models.filter((m: any) => m.title !== title);
92 return config;
93 },
94 (config) => {
95 config.models = config.models?.filter((m: any) => m.name !== title);
96 return config;
97 },
98 );
99}
100
101export function getModelByRole<T extends keyof ExperimentalModelRoles>(
102 config: ContinueConfig,
103 role: T,
104): ILLM | undefined {
105 const roleTitle = config.experimental?.modelRoles?.[role];
106
107 if (!roleTitle) {
108 return undefined;
109 }
110
111 const matchingModel = config.modelsByRole.chat.find(
112 (model) => model.title === roleTitle,
113 );
114
115 return matchingModel;
116}
117
118/**
119 * This check is to determine if the user is on an unsupported CPU
120 * target for our Lance DB binaries.
121 *
122 * See here for details: https://github.com/continuedev/continue/issues/940
123 */
124export function isSupportedLanceDbCpuTargetForLinux(ide?: IDE) {
125 const CPU_FEATURES_TO_CHECK = ["avx2", "fma"] as const;
126
127 const globalContext = new GlobalContext();
128 const globalContextVal = globalContext.get(
129 "isSupportedLanceDbCpuTargetForLinux",
130 );
131
132 // If we've already checked the CPU target, return the cached value
133 if (globalContextVal !== undefined) {
134 return globalContextVal;
135 }
136
137 const arch = os.arch();
138
139 // This check only applies to x64
140 //https://github.com/lancedb/lance/issues/2195#issuecomment-2057841311
141 if (arch !== "x64") {
142 globalContext.update("isSupportedLanceDbCpuTargetForLinux", true);
143 return true;
144 }
145
146 try {
147 const cpuFlags = fs.readFileSync("/proc/cpuinfo", "utf-8").toLowerCase();
148
149 const isSupportedLanceDbCpuTargetForLinux = cpuFlags
150 ? CPU_FEATURES_TO_CHECK.every((feature) => cpuFlags.includes(feature))
151 : true;
152
153 // If it's not a supported CPU target, and it's the first time we are checking,
154 // show a toast to inform the user that we are going to disable indexing.
155 if (!isSupportedLanceDbCpuTargetForLinux && ide) {
156 // We offload our async toast to `showUnsupportedCpuToast` to prevent making
157 // our config loading async upstream of `isSupportedLanceDbCpuTargetForLinux`
158 void showUnsupportedCpuToast(ide);
159 }
160
161 globalContext.update(
162 "isSupportedLanceDbCpuTargetForLinux",
163 isSupportedLanceDbCpuTargetForLinux,
164 );
165
166 return isSupportedLanceDbCpuTargetForLinux;
167 } catch (error) {
168 // If we can't determine CPU features, default to true
169 return true;
170 }
171}
172
173async function showUnsupportedCpuToast(ide: IDE) {
174 const shouldOpenLink = await ide.showToast(
175 "warning",
176 "Codebase indexing disabled - Your Linux system lacks required CPU features (AVX2, FMA)",
177 "Learn more",
178 );
179
180 if (shouldOpenLink) {
181 void ide.openUrl(
182 "https://docs.continue.dev/troubleshooting#i-received-a-codebase-indexing-disabled---your-linux-system-lacks-required-cpu-features-avx2-fma-notification",
183 );
184 }
185}
186
187/**
188 * This is required because users are only able to define prompt templates as a
189 * string, while internally we also allow prompt templates to be functions
190 * @param templates
191 * @returns
192 */
193export function serializePromptTemplates(
194 templates: Record<string, PromptTemplate> | undefined,
195): Record<string, string> | undefined {
196 if (!templates) return undefined;
197
198 return Object.fromEntries(
199 Object.entries(templates).map(([key, template]) => {
200 const serialized = typeof template === "function" ? "" : template;
201 return [key, serialized];
202 }),
203 );
204}