-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsettings.ts
More file actions
275 lines (241 loc) · 8.01 KB
/
settings.ts
File metadata and controls
275 lines (241 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { defaultsToThinkingMode } from "./common/model-capabilities";
export type DeepcodingEnv = Record<string, string | undefined> & {
MODEL?: string;
BASE_URL?: string;
API_KEY?: string;
THINKING_ENABLED?: string;
REASONING_EFFORT?: string;
DEBUG_LOG_ENABLED?: string;
};
export type ReasoningEffort = "high" | "max";
export type McpServerConfig = {
command: string;
args?: string[];
env?: Record<string, string>;
};
export type DeepcodingSettings = {
env?: DeepcodingEnv;
model?: string;
thinkingEnabled?: boolean;
reasoningEffort?: ReasoningEffort;
debugLogEnabled?: boolean;
notify?: string;
webSearchTool?: string;
mcpServers?: Record<string, McpServerConfig>;
};
export type ResolvedDeepcodingSettings = {
env: Record<string, string>;
apiKey?: string;
baseURL: string;
model: string;
thinkingEnabled: boolean;
reasoningEffort: ReasoningEffort;
debugLogEnabled: boolean;
notify?: string;
webSearchTool?: string;
mcpServers?: Record<string, McpServerConfig>;
};
export type ModelConfigSelection = {
model: string;
thinkingEnabled: boolean;
reasoningEffort: ReasoningEffort;
};
export type SettingsProcessEnv = Record<string, string | undefined>;
function resolveReasoningEffort(value: unknown): ReasoningEffort | undefined {
return value === "high" || value === "max" ? value : undefined;
}
function parseBoolean(value: unknown): boolean | undefined {
if (typeof value === "boolean") {
return value;
}
if (typeof value !== "string") {
return undefined;
}
const normalized = value.trim().toLowerCase();
if (["1", "true", "enabled", "yes", "on"].includes(normalized)) {
return true;
}
if (["0", "false", "disabled", "no", "off"].includes(normalized)) {
return false;
}
return undefined;
}
function trimString(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function normalizeEnv(env: DeepcodingSettings["env"]): Record<string, string> {
const result: Record<string, string> = {};
if (!env) {
return result;
}
for (const [key, value] of Object.entries(env)) {
if (typeof value === "string") {
result[key] = value;
}
}
return result;
}
export function collectDeepcodeEnv(processEnv: SettingsProcessEnv = process.env): Record<string, string> {
const result: Record<string, string> = {};
for (const [key, value] of Object.entries(processEnv)) {
if (!key.startsWith("DEEPCODE_") || typeof value !== "string") {
continue;
}
const strippedKey = key.slice("DEEPCODE_".length);
if (strippedKey) {
result[strippedKey] = value;
}
}
return result;
}
function extractMcpEnv(env: Record<string, string>): Record<string, string> {
const result: Record<string, string> = {};
for (const [key, value] of Object.entries(env)) {
if (!key.startsWith("MCP_")) {
continue;
}
const strippedKey = key.slice("MCP_".length);
if (strippedKey) {
result[strippedKey] = value;
}
}
return result;
}
function mergeMcpServers(
userSettings: DeepcodingSettings | null | undefined,
projectSettings: DeepcodingSettings | null | undefined,
userEnv: Record<string, string>,
projectEnv: Record<string, string>,
systemEnv: Record<string, string>
): Record<string, McpServerConfig> | undefined {
const userServers = userSettings?.mcpServers ?? {};
const projectServers = projectSettings?.mcpServers ?? {};
const serverNames = new Set([...Object.keys(userServers), ...Object.keys(projectServers)]);
if (serverNames.size === 0) {
return undefined;
}
const userMcpEnv = extractMcpEnv(userEnv);
const projectMcpEnv = extractMcpEnv(projectEnv);
const systemMcpEnv = extractMcpEnv(systemEnv);
const merged: Record<string, McpServerConfig> = {};
for (const name of serverNames) {
const userConfig = userServers[name];
const projectConfig = projectServers[name];
const command = projectConfig?.command ?? userConfig?.command;
if (!command) {
continue;
}
const env = {
...userEnv,
...(userConfig?.env ?? {}),
...userMcpEnv,
...projectEnv,
...(projectConfig?.env ?? {}),
...projectMcpEnv,
...systemEnv,
...systemMcpEnv,
};
const config: McpServerConfig = {
command,
args: projectConfig?.args ?? userConfig?.args,
};
if (Object.keys(env).length > 0) {
config.env = env;
}
merged[name] = config;
}
return Object.keys(merged).length > 0 ? merged : undefined;
}
export function resolveSettingsSources(
userSettings: DeepcodingSettings | null | undefined,
projectSettings: DeepcodingSettings | null | undefined,
defaults: { model: string; baseURL: string },
processEnv: SettingsProcessEnv = process.env
): ResolvedDeepcodingSettings {
const userEnv = normalizeEnv(userSettings?.env);
const projectEnv = normalizeEnv(projectSettings?.env);
const systemEnv = collectDeepcodeEnv(processEnv);
const env = {
...userEnv,
...projectEnv,
...systemEnv,
};
const model =
trimString(systemEnv.MODEL) ||
trimString(projectSettings?.model) ||
trimString(projectEnv.MODEL) ||
trimString(userSettings?.model) ||
trimString(userEnv.MODEL) ||
defaults.model;
const thinkingEnabled =
parseBoolean(systemEnv.THINKING_ENABLED) ??
parseBoolean(projectSettings?.thinkingEnabled) ??
parseBoolean(projectEnv.THINKING_ENABLED) ??
parseBoolean(userSettings?.thinkingEnabled) ??
parseBoolean(userEnv.THINKING_ENABLED) ??
defaultsToThinkingMode(model);
const reasoningEffort =
resolveReasoningEffort(systemEnv.REASONING_EFFORT) ??
resolveReasoningEffort(projectSettings?.reasoningEffort) ??
resolveReasoningEffort(projectEnv.REASONING_EFFORT) ??
resolveReasoningEffort(userSettings?.reasoningEffort) ??
resolveReasoningEffort(userEnv.REASONING_EFFORT) ??
"max";
const debugLogEnabled =
parseBoolean(systemEnv.DEBUG_LOG_ENABLED) ??
parseBoolean(projectSettings?.debugLogEnabled) ??
parseBoolean(projectEnv.DEBUG_LOG_ENABLED) ??
parseBoolean(userSettings?.debugLogEnabled) ??
parseBoolean(userEnv.DEBUG_LOG_ENABLED) ??
false;
const notify =
trimString(systemEnv.NOTIFY) || trimString(projectSettings?.notify) || trimString(userSettings?.notify) || "";
const webSearchTool =
trimString(systemEnv.WEB_SEARCH_TOOL) ||
trimString(projectSettings?.webSearchTool) ||
trimString(userSettings?.webSearchTool) ||
"";
return {
env,
apiKey: trimString(env.API_KEY) || undefined,
baseURL: trimString(env.BASE_URL) || defaults.baseURL,
model,
thinkingEnabled,
reasoningEffort,
debugLogEnabled,
notify: notify || undefined,
webSearchTool: webSearchTool || undefined,
mcpServers: mergeMcpServers(userSettings, projectSettings, userEnv, projectEnv, systemEnv),
};
}
export function resolveSettings(
settings: DeepcodingSettings | null | undefined,
defaults: { model: string; baseURL: string },
processEnv: SettingsProcessEnv = process.env
): ResolvedDeepcodingSettings {
return resolveSettingsSources(settings, null, defaults, processEnv);
}
export function modelConfigKey(config: Pick<ModelConfigSelection, "thinkingEnabled" | "reasoningEffort">): string {
return config.thinkingEnabled ? `thinking:${config.reasoningEffort}` : "thinking:none";
}
export function applyModelConfigSelection(
settings: DeepcodingSettings | null | undefined,
current: ModelConfigSelection,
selected: ModelConfigSelection
): { settings: DeepcodingSettings; changed: boolean } {
const changed = selected.model !== current.model || modelConfigKey(selected) !== modelConfigKey(current);
const next: DeepcodingSettings = { ...(settings ?? {}) };
if (!changed) {
return { settings: next, changed: false };
}
if (selected.model !== current.model || Object.prototype.hasOwnProperty.call(next, "model")) {
next.model = selected.model;
} else {
delete next.model;
}
next.thinkingEnabled = selected.thinkingEnabled;
if (selected.thinkingEnabled) {
next.reasoningEffort = selected.reasoningEffort;
}
return { settings: next, changed: true };
}