Elffuss BraidHub

kikocis/continue public

d66b7a6a5649 · 3.7 KiBRawPermalink
1import fs from "node:fs";
2
3import { ModelRole } from "@continuedev/config-yaml";
4
5import { SiteIndexingConfig } from "..";
6import {
7 salvageSharedConfig,
8 sharedConfigSchema,
9 SharedConfigSchema,
10} from "../config/sharedConfig";
11
12import { getGlobalContextFilePath } from "./paths";
13
14export type GlobalContextModelSelections = Partial<
15 Record<ModelRole, string | null>
16>;
17
18export type GlobalContextType = {
19 indexingPaused: boolean;
20 lastSelectedProfileForWorkspace: {
21 [workspaceIdentifier: string]: string | null;
22 };
23 lastSelectedOrgIdForWorkspace: {
24 [workspaceIdentifier: string]: string | null;
25 };
26 selectedModelsByProfileId: {
27 [profileId: string]: GlobalContextModelSelections;
28 };
29
30 /**
31 * This is needed to handle the case where a JetBrains user has created
32 * docs embeddings using one provider, and then updates to a new provider.
33 *
34 * For VS Code users, it is unnecessary since we use transformers.js by default.
35 */
36 hasDismissedConfigTsNoticeJetBrains: boolean;
37 hasAlreadyCreatedAPromptFile: boolean;
38 showConfigUpdateToast: boolean;
39 isSupportedLanceDbCpuTargetForLinux: boolean;
40 sharedConfig: SharedConfigSchema;
41 failedDocs: SiteIndexingConfig[];
42};
43
44/**
45 * A way to persist global state
46 */
47export class GlobalContext {
48 update<T extends keyof GlobalContextType>(
49 key: T,
50 value: GlobalContextType[T],
51 ) {
52 const filepath = getGlobalContextFilePath();
53 if (!fs.existsSync(filepath)) {
54 fs.writeFileSync(
55 filepath,
56 JSON.stringify(
57 {
58 [key]: value,
59 },
60 null,
61 2,
62 ),
63 );
64 } else {
65 const data = fs.readFileSync(filepath, "utf-8");
66
67 let parsed;
68 try {
69 parsed = JSON.parse(data);
70 } catch (e: any) {
71 console.warn(`Error updating global context: ${e}`);
72 return;
73 }
74
75 parsed[key] = value;
76 fs.writeFileSync(filepath, JSON.stringify(parsed, null, 2));
77 }
78 }
79
80 get<T extends keyof GlobalContextType>(
81 key: T,
82 ): GlobalContextType[T] | undefined {
83 const filepath = getGlobalContextFilePath();
84 if (!fs.existsSync(filepath)) {
85 return undefined;
86 }
87
88 const data = fs.readFileSync(filepath, "utf-8");
89 try {
90 const parsed = JSON.parse(data);
91 return parsed[key];
92 } catch (e: any) {
93 console.warn(`Error parsing global context: ${e}`);
94 return undefined;
95 }
96 }
97
98 getSharedConfig(): SharedConfigSchema {
99 const sharedConfig = this.get("sharedConfig") ?? {};
100 const result = sharedConfigSchema.safeParse(sharedConfig);
101 if (result.success) {
102 return result.data;
103 } else {
104 // in case of damaged shared config, repair it
105 // Attempt to salvage any values that are security concerns
106 console.error("Failed to load shared config, salvaging...", result.error);
107 const salvagedConfig = salvageSharedConfig(sharedConfig);
108 this.update("sharedConfig", salvagedConfig);
109 return salvagedConfig;
110 }
111 }
112
113 updateSharedConfig(
114 newValues: Partial<SharedConfigSchema>,
115 ): SharedConfigSchema {
116 const currentSharedConfig = this.getSharedConfig();
117 const updatedSharedConfig = {
118 ...currentSharedConfig,
119 ...newValues,
120 };
121 this.update("sharedConfig", updatedSharedConfig);
122 return updatedSharedConfig;
123 }
124
125 updateSelectedModel(
126 profileId: string,
127 role: ModelRole,
128 title: string | null,
129 ): GlobalContextModelSelections {
130 const currentSelections = this.get("selectedModelsByProfileId") ?? {};
131 const forProfile = currentSelections[profileId] ?? {};
132 const newSelections = {
133 ...forProfile,
134 [role]: title,
135 };
136
137 this.update("selectedModelsByProfileId", {
138 ...currentSelections,
139 [profileId]: newSelections,
140 });
141 return newSelections;
142 }
143}