Elffuss BraidHub

kikocis/continue public

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