|
1 | 1 | import { defaultsToThinkingMode } from "./common/model-capabilities"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
2 | 5 |
|
3 | 6 | export type DeepcodingEnv = Record<string, string | undefined> & { |
4 | 7 | MODEL?: string; |
@@ -370,3 +373,88 @@ export function applyModelConfigSelection( |
370 | 373 |
|
371 | 374 | return { settings: next, changed: true }; |
372 | 375 | } |
| 376 | + |
| 377 | +// --------------------------------------------------------------------------- |
| 378 | +// Default constants |
| 379 | +// --------------------------------------------------------------------------- |
| 380 | + |
| 381 | +export const DEFAULT_MODEL = "deepseek-v4-pro"; |
| 382 | +export const DEFAULT_BASE_URL = "https://api.deepseek.com"; |
| 383 | + |
| 384 | +// --------------------------------------------------------------------------- |
| 385 | +// Settings file I/O |
| 386 | +// --------------------------------------------------------------------------- |
| 387 | + |
| 388 | +export function getUserSettingsPath(): string { |
| 389 | + return path.join(os.homedir(), ".deepcode", "settings.json"); |
| 390 | +} |
| 391 | + |
| 392 | +export function getProjectSettingsPath(projectRoot: string): string { |
| 393 | + return path.join(projectRoot, ".deepcode", "settings.json"); |
| 394 | +} |
| 395 | + |
| 396 | +export function readSettingsFile(settingsPath: string): DeepcodingSettings | null { |
| 397 | + try { |
| 398 | + if (!fs.existsSync(settingsPath)) { |
| 399 | + return null; |
| 400 | + } |
| 401 | + const raw = fs.readFileSync(settingsPath, "utf8"); |
| 402 | + return JSON.parse(raw) as DeepcodingSettings; |
| 403 | + } catch { |
| 404 | + return null; |
| 405 | + } |
| 406 | +} |
| 407 | + |
| 408 | +export function readSettings(): DeepcodingSettings | null { |
| 409 | + return readSettingsFile(getUserSettingsPath()); |
| 410 | +} |
| 411 | + |
| 412 | +export function readProjectSettings(projectRoot: string = process.cwd()): DeepcodingSettings | null { |
| 413 | + return readSettingsFile(getProjectSettingsPath(projectRoot)); |
| 414 | +} |
| 415 | + |
| 416 | +function writeSettingsFile(settingsPath: string, settings: DeepcodingSettings): void { |
| 417 | + fs.mkdirSync(path.dirname(settingsPath), { recursive: true }); |
| 418 | + fs.writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}\n`, "utf8"); |
| 419 | +} |
| 420 | + |
| 421 | +export function writeSettings(settings: DeepcodingSettings): void { |
| 422 | + const settingsPath = getUserSettingsPath(); |
| 423 | + writeSettingsFile(settingsPath, settings); |
| 424 | +} |
| 425 | + |
| 426 | +export function writeProjectSettings(settings: DeepcodingSettings, projectRoot: string = process.cwd()): void { |
| 427 | + const settingsPath = getProjectSettingsPath(projectRoot); |
| 428 | + writeSettingsFile(settingsPath, settings); |
| 429 | +} |
| 430 | + |
| 431 | +export function writeModelConfigSelection( |
| 432 | + selection: ModelConfigSelection, |
| 433 | + current: ModelConfigSelection = resolveCurrentSettings(), |
| 434 | + projectRoot: string = process.cwd() |
| 435 | +): { changed: boolean; settings: DeepcodingSettings } { |
| 436 | + const projectSettingsPath = getProjectSettingsPath(projectRoot); |
| 437 | + const shouldWriteProjectSettings = fs.existsSync(projectSettingsPath); |
| 438 | + const rawSettings = shouldWriteProjectSettings ? readProjectSettings(projectRoot) : readSettings(); |
| 439 | + const result = applyModelConfigSelection(rawSettings, current, selection); |
| 440 | + if (result.changed) { |
| 441 | + if (shouldWriteProjectSettings) { |
| 442 | + writeProjectSettings(result.settings, projectRoot); |
| 443 | + } else { |
| 444 | + writeSettings(result.settings); |
| 445 | + } |
| 446 | + } |
| 447 | + return result; |
| 448 | +} |
| 449 | + |
| 450 | +export function resolveCurrentSettings(projectRoot: string = process.cwd()): ResolvedDeepcodingSettings { |
| 451 | + return resolveSettingsSources( |
| 452 | + readSettings(), |
| 453 | + readProjectSettings(projectRoot), |
| 454 | + { |
| 455 | + model: DEFAULT_MODEL, |
| 456 | + baseURL: DEFAULT_BASE_URL, |
| 457 | + }, |
| 458 | + process.env |
| 459 | + ); |
| 460 | +} |
0 commit comments