|
1 | 1 | import vscode from 'vscode'; |
2 | 2 | import { CONFIG_SECTION } from './consts'; |
3 | 3 |
|
| 4 | +export type DebugMode = 'minimal' | 'metadata' | 'verbose'; |
| 5 | + |
4 | 6 | /** |
5 | 7 | * Get DeepSeek API base URL from settings. |
6 | 8 | * Falls back to the official endpoint when not configured. |
@@ -34,10 +36,97 @@ export function getMaxTokens(): number | undefined { |
34 | 36 | return value > 0 ? value : undefined; |
35 | 37 | } |
36 | 38 |
|
| 39 | +/** |
| 40 | + * Diagnostic mode. `verbose` also enables metadata logs. |
| 41 | + * |
| 42 | + * The legacy boolean `debug` setting is still read as a fallback so old |
| 43 | + * settings keep working even if migration cannot update every scope. |
| 44 | + */ |
| 45 | +export function getDebugMode(): DebugMode { |
| 46 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
| 47 | + const mode = getConfiguredDebugMode(config); |
| 48 | + if (mode) return mode; |
| 49 | + |
| 50 | + return config.get<boolean>('debug', false) ? 'metadata' : 'minimal'; |
| 51 | +} |
| 52 | + |
37 | 53 | /** |
38 | 54 | * Whether to log privacy-preserving diagnostic debug information. |
39 | 55 | */ |
40 | 56 | export function getDebugLoggingEnabled(): boolean { |
41 | | - const config = vscode.workspace.getConfiguration(CONFIG_SECTION); |
42 | | - return config.get<boolean>('debug', false); |
| 57 | + return getDebugMode() !== 'minimal'; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Whether to write full DeepSeek request payloads to disk. |
| 62 | + */ |
| 63 | +export function getRequestDumpEnabled(): boolean { |
| 64 | + return getDebugMode() === 'verbose'; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Migrate the legacy boolean `deepseek-copilot.debug` setting to `debugMode`. |
| 69 | + * |
| 70 | + * `debug: true` maps to `debugMode: metadata`; `debug: false` maps to the |
| 71 | + * default `minimal`, so it only needs cleanup. |
| 72 | + */ |
| 73 | +export async function migrateLegacyDebugSetting(): Promise<void> { |
| 74 | + await migrateLegacyDebugSettingAtScope(vscode.ConfigurationTarget.Global); |
| 75 | + if (vscode.workspace.workspaceFile || vscode.workspace.workspaceFolders?.length) { |
| 76 | + await migrateLegacyDebugSettingAtScope(vscode.ConfigurationTarget.Workspace); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function getConfiguredDebugMode(config: vscode.WorkspaceConfiguration): DebugMode | undefined { |
| 81 | + const mode = config.inspect<unknown>('debugMode'); |
| 82 | + return normalizeDebugMode(mode?.workspaceValue) ?? normalizeDebugMode(mode?.globalValue); |
| 83 | +} |
| 84 | + |
| 85 | +function normalizeDebugMode(value: unknown): DebugMode | undefined { |
| 86 | + if (value === 'minimal' || value === 'metadata' || value === 'verbose') { |
| 87 | + return value; |
| 88 | + } |
| 89 | + return undefined; |
| 90 | +} |
| 91 | + |
| 92 | +async function migrateLegacyDebugSettingAtScope( |
| 93 | + target: vscode.ConfigurationTarget, |
| 94 | + resource?: vscode.Uri, |
| 95 | +): Promise<void> { |
| 96 | + const config = vscode.workspace.getConfiguration(CONFIG_SECTION, resource); |
| 97 | + const legacy = config.inspect<boolean>('debug'); |
| 98 | + const mode = config.inspect<DebugMode>('debugMode'); |
| 99 | + const legacyValue = getScopedValue(legacy, target); |
| 100 | + |
| 101 | + if (legacyValue === undefined) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (legacyValue === true && getScopedValue(mode, target) === undefined) { |
| 106 | + await config.update('debugMode', 'metadata', target); |
| 107 | + } |
| 108 | + await config.update('debug', undefined, target); |
| 109 | +} |
| 110 | + |
| 111 | +function getScopedValue<T>( |
| 112 | + inspection: |
| 113 | + | { |
| 114 | + globalValue?: T; |
| 115 | + workspaceValue?: T; |
| 116 | + workspaceFolderValue?: T; |
| 117 | + } |
| 118 | + | undefined, |
| 119 | + target: vscode.ConfigurationTarget, |
| 120 | +): T | undefined { |
| 121 | + if (!inspection) { |
| 122 | + return undefined; |
| 123 | + } |
| 124 | + |
| 125 | + if (target === vscode.ConfigurationTarget.Global) { |
| 126 | + return inspection.globalValue; |
| 127 | + } |
| 128 | + if (target === vscode.ConfigurationTarget.Workspace) { |
| 129 | + return inspection.workspaceValue; |
| 130 | + } |
| 131 | + return undefined; |
43 | 132 | } |
0 commit comments