Skip to content

Commit 4a20d37

Browse files
committed
Migrate legacy chat preset affixes to prompt templates
1 parent c34a6d3 commit 4a20d37

3 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎apps/editor/src/extension.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
migrate_checkpoints_title_to_trigger,
1111
migrate_model_providers_type,
1212
migrate_prune_context_to_find_relevant_files,
13-
migrate_merge_configurations
13+
migrate_merge_configurations,
14+
migrate_preset_affix_to_prompt_template
1415
} from './migrations'
1516
import {
1617
apply_chat_response_command,
@@ -66,6 +67,8 @@ export const activate = async (context: vscode.ExtensionContext) => {
6667
await migrate_prune_context_to_find_relevant_files(context)
6768
// 25 March 2026
6869
await migrate_merge_configurations(context)
70+
// 8 June 2026
71+
await migrate_preset_affix_to_prompt_template(context)
6972
}
7073

7174
await migrations()

‎apps/editor/src/migrations/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export * from './migrate-checkpoints-title-to-trigger'
55
export * from './migrate-model-providers-type'
66
export * from './migrate-prune-context-to-find-relevant-files'
77
export * from './migrate-merge-configurations'
8+
export * from './migrate-preset-affix-to-prompt-template'
9+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)