Elffuss BraidHub

kikocis/continue public

9d8db5fc402e · 5.7 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/constructMessages";
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 defaultCompletionOptions: model.completionOptions,
81 };
82 if (model.systemMessage) {
83 desc.chatOptions = {
84 baseSystemMessage:
85 DEFAULT_CHAT_SYSTEM_MESSAGE + "\n\n" + model.systemMessage,
86 };
87 }
88 config.models.push(desc);
89 return config;
90 },
91 );
92}
93
94export function deleteModel(title: string) {
95 editConfigFile(
96 (config) => {
97 config.models = config.models.filter((m: any) => m.title !== title);
98 return config;
99 },
100 (config) => {
101 config.models = config.models?.filter((m: any) => m.name !== title);
102 return config;
103 },
104 );
105}
106
107export function getModelByRole<T extends keyof ExperimentalModelRoles>(
108 config: ContinueConfig,
109 role: T,
110): ILLM | undefined {
111 const roleTitle = config.experimental?.modelRoles?.[role];
112
113 if (!roleTitle) {
114 return undefined;
115 }
116
117 const matchingModel = config.modelsByRole.chat.find(
118 (model) => model.title === roleTitle,
119 );
120
121 return matchingModel;
122}
123
124/**
125 * This check is to determine if the user is on an unsupported CPU
126 * target for our Lance DB binaries.
127 *
128 * See here for details: https://github.com/continuedev/continue/issues/940
129 */
130export function isSupportedLanceDbCpuTargetForLinux(ide?: IDE) {
131 const CPU_FEATURES_TO_CHECK = ["avx2", "fma"] as const;
132
133 const globalContext = new GlobalContext();
134 const globalContextVal = globalContext.get(
135 "isSupportedLanceDbCpuTargetForLinux",
136 );
137
138 // If we've already checked the CPU target, return the cached value
139 if (globalContextVal !== undefined) {
140 return globalContextVal;
141 }
142
143 const arch = os.arch();
144
145 // This check only applies to x64
146 //https://github.com/lancedb/lance/issues/2195#issuecomment-2057841311
147 if (arch !== "x64") {
148 globalContext.update("isSupportedLanceDbCpuTargetForLinux", true);
149 return true;
150 }
151
152 try {
153 const cpuFlags = fs.readFileSync("/proc/cpuinfo", "utf-8").toLowerCase();
154
155 const isSupportedLanceDbCpuTargetForLinux = cpuFlags
156 ? CPU_FEATURES_TO_CHECK.every((feature) => cpuFlags.includes(feature))
157 : true;
158
159 // If it's not a supported CPU target, and it's the first time we are checking,
160 // show a toast to inform the user that we are going to disable indexing.
161 if (!isSupportedLanceDbCpuTargetForLinux && ide) {
162 // We offload our async toast to `showUnsupportedCpuToast` to prevent making
163 // our config loading async upstream of `isSupportedLanceDbCpuTargetForLinux`
164 void showUnsupportedCpuToast(ide);
165 }
166
167 globalContext.update(
168 "isSupportedLanceDbCpuTargetForLinux",
169 isSupportedLanceDbCpuTargetForLinux,
170 );
171
172 return isSupportedLanceDbCpuTargetForLinux;
173 } catch (error) {
174 // If we can't determine CPU features, default to true
175 return true;
176 }
177}
178
179async function showUnsupportedCpuToast(ide: IDE) {
180 const shouldOpenLink = await ide.showToast(
181 "warning",
182 "Codebase indexing disabled - Your Linux system lacks required CPU features (AVX2, FMA)",
183 "Learn more",
184 );
185
186 if (shouldOpenLink) {
187 void ide.openUrl(
188 "https://docs.continue.dev/troubleshooting#i-received-a-codebase-indexing-disabled---your-linux-system-lacks-required-cpu-features-avx2-fma-notification",
189 );
190 }
191}
192
193/**
194 * This is required because users are only able to define prompt templates as a
195 * string, while internally we also allow prompt templates to be functions
196 * @param templates
197 * @returns
198 */
199export function serializePromptTemplates(
200 templates: Record<string, PromptTemplate> | undefined,
201): Record<string, string> | undefined {
202 if (!templates) return undefined;
203
204 return Object.fromEntries(
205 Object.entries(templates).map(([key, template]) => {
206 const serialized = typeof template === "function" ? "" : template;
207 return [key, serialized];
208 }),
209 );
210}