1+ import * as vscode from 'vscode'
2+ import { Logger } from '@shared/utils/logger'
3+
4+ const MIGRATION_ID = 'preset-affix-to-prompt-template-migration-202606082'
5+
6+ export async function migrate_preset_affix_to_prompt_template (
7+ context : vscode . ExtensionContext
8+ ) : Promise < void > {
9+ try {
10+ if ( context . globalState . get ( MIGRATION_ID ) ) {
11+ return
12+ }
13+
14+ const MAPPINGS = [
15+ {
16+ preset_key : 'chatPresetsForAskAboutContext' ,
17+ template_key : 'promptTemplatesForAskAboutContext'
18+ } ,
19+ {
20+ preset_key : 'chatPresetsForEditContext' ,
21+ template_key : 'promptTemplatesForEditContext'
22+ } ,
23+ {
24+ preset_key : 'chatPresetsForCodeAtCursor' ,
25+ template_key : 'promptTemplatesForCodeAtCursor'
26+ } ,
27+ {
28+ preset_key : 'chatPresetsForFindRelevantFiles' ,
29+ template_key : 'promptTemplatesForFindRelevantFiles'
30+ } ,
31+ {
32+ preset_key : 'chatPresetsForNoContext' ,
33+ template_key : 'promptTemplatesForNoContext'
34+ }
35+ ]
36+
37+ for ( const mapping of MAPPINGS ) {
38+ // Fetch fresh configuration inside the loop to ensure we don't have stale data
39+ const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
40+ const presetsInspect = config . inspect < any [ ] > ( mapping . preset_key )
41+
42+ if (
43+ presetsInspect ?. globalValue &&
44+ Array . isArray ( presetsInspect . globalValue )
45+ ) {
46+ // Deep copy the array so we don't mutate the internal cached objects of VS Code.
47+ // Mutating them can cause config.update to fail silently because VS Code
48+ // thinks the new value is identical to the current value.
49+ const presets = JSON . parse ( JSON . stringify ( presetsInspect . globalValue ) )
50+ const newTemplates : any [ ] = [ ]
51+
52+ for ( const preset of presets ) {
53+ if ( preset . promptPrefix || preset . promptSuffix ) {
54+ const prefix = preset . promptPrefix ? `${ preset . promptPrefix } \n` : ''
55+ const suffix = preset . promptSuffix ? `\n${ preset . promptSuffix } ` : ''
56+ const templateText = `${ prefix } {text}${ suffix } `
57+
58+ const template : any = {
59+ template : templateText
60+ }
61+
62+ if ( preset . name && ! / ^ \( \d + \) $ / . test ( preset . name . trim ( ) ) ) {
63+ template . name = preset . name
64+ }
65+
66+ newTemplates . push ( template )
67+ }
68+ }
69+
70+ if ( newTemplates . length > 0 ) {
71+ const templatesInspect = config . inspect < any [ ] > ( mapping . template_key )
72+ const currentTemplates = Array . isArray (
73+ templatesInspect ?. globalValue
74+ )
75+ ? JSON . parse ( JSON . stringify ( templatesInspect . globalValue ) )
76+ : [ ]
77+
78+ const updatedTemplates = [ ...newTemplates , ...currentTemplates ]
79+ await config . update (
80+ mapping . template_key ,
81+ updatedTemplates ,
82+ vscode . ConfigurationTarget . Global
83+ )
84+ }
85+ }
86+ }
87+
88+ await context . globalState . update ( MIGRATION_ID , true )
89+ Logger . info ( {
90+ function_name : 'migrate_preset_affix_to_prompt_template' ,
91+ message : 'Successfully migrated preset affixes to prompt templates'
92+ } )
93+ } catch ( error ) {
94+ Logger . error ( {
95+ function_name : 'migrate_preset_affix_to_prompt_template' ,
96+ message : 'Error migrating preset affixes to prompt templates' ,
97+ data : error instanceof Error ? error . message : String ( error )
98+ } )
99+ }
100+ }
0 commit comments