Skip to content

Commit b0c8266

Browse files
committed
Add browser profile selection when launching the browser and delete removed profile directories
- when there are 'browser profiles', show the quick pick. First one is always 'Default'. Selection substitutes [Fragment]. - highlight last selection - in settings, removing browser profile should also remove its folder on disk [Image] - use logger for [Fragment]
1 parent c0ea85b commit b0c8266

5 files changed

Lines changed: 187 additions & 3 deletions

File tree

‎apps/editor/src/constants/state-keys.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export const get_last_used_template_key = (prompt_type: string) =>
9292
`last-used-template-${prompt_type}`
9393

9494
export const LAST_SELECTED_BROWSER_ID_STATE_KEY = 'last-selected-browser-id'
95+
export const LAST_SELECTED_BROWSER_PROFILE_STATE_KEY =
96+
'last-selected-browser-profile'
9597

9698
export const LAST_SEARCH_FILES_PHRASE_QUERY_STATE_KEY =
9799
'last-search-files-phrase-query'

‎apps/editor/src/i18n/translations/views/prompt/handlers/handle-run-browser.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,41 @@ export const translations = {
5252
cs: 'Stahování prohlížeče',
5353
hu: 'Böngésző letöltése',
5454
bg: 'Изтегляне на браузър'
55+
},
56+
'views.prompt.handlers.handle-run-browser.select-profile.title': {
57+
en: 'Browser Profiles',
58+
pl: 'Profile przeglądarki',
59+
'zh-cn': '浏览器配置文件',
60+
ja: 'ブラウザプロファイル',
61+
'zh-tw': '瀏覽器設定檔',
62+
de: 'Browser-Profile',
63+
es: 'Perfiles del navegador',
64+
fr: 'Profils du navigateur',
65+
'pt-br': 'Perfis do navegador',
66+
ru: 'Профили браузера',
67+
ko: '브라우저 프로필',
68+
it: 'Profili del browser',
69+
tr: 'Tarayıcı Profilleri',
70+
cs: 'Profily prohlížeče',
71+
hu: 'Böngészőprofilok',
72+
bg: 'Профили на браузъра'
73+
},
74+
'views.prompt.handlers.handle-run-browser.select-profile.placeholder': {
75+
en: 'Select a browser profile',
76+
pl: 'Wybierz profil przeglądarki',
77+
'zh-cn': '选择浏览器配置文件',
78+
ja: 'ブラウザプロファイルを選択',
79+
'zh-tw': '選擇瀏覽器設定檔',
80+
de: 'Browser-Profil auswählen',
81+
es: 'Seleccione un perfil del navegador',
82+
fr: 'Sélectionner un profil du navigateur',
83+
'pt-br': 'Selecione um perfil do navegador',
84+
ru: 'Выберите профиль браузера',
85+
ko: '브라우저 프로필 선택',
86+
it: 'Seleziona un profilo del browser',
87+
tr: 'Bir tarayıcı profili seçin',
88+
cs: 'Vyberte profil prohlížeče',
89+
hu: 'Válasszon böngészőprofilt',
90+
bg: 'Изберете профил на браузъра'
5591
}
5692
} as const

‎apps/editor/src/views/prompt/backend/message-handlers/handle-run-browser.ts‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
resolveBuildId
1111
} from '@puppeteer/browsers'
1212
import { t } from '@/i18n'
13+
import { PromptViewProvider } from '../prompt-view-provider'
14+
import { LAST_SELECTED_BROWSER_PROFILE_STATE_KEY } from '@/constants/state-keys'
1315

1416
const launch_browser = (params: {
1517
executable_path: string
@@ -36,7 +38,9 @@ const launch_browser = (params: {
3638
child.unref()
3739
}
3840

39-
export const handle_run_browser = async () => {
41+
export const handle_run_browser = async (
42+
prompt_view_provider: PromptViewProvider
43+
) => {
4044
if (vscode.env.remoteName) {
4145
const action = t('views.prompt.handlers.handle-run-browser.forward-port')
4246
const actions = vscode.env.remoteName === 'ssh-remote' ? [action] : []
@@ -56,6 +60,92 @@ export const handle_run_browser = async () => {
5660
return
5761
}
5862

63+
const browser_profiles = Array.from(
64+
new Set(
65+
vscode.workspace
66+
.getConfiguration('codeWebChat')
67+
.get<string[]>('browserProfiles', [])
68+
.map((p) => p.trim())
69+
.filter((p) => p.length > 0 && p !== 'Default')
70+
)
71+
)
72+
73+
let selected_profile: string | undefined = 'Default'
74+
75+
if (browser_profiles.length > 0) {
76+
const items = ['Default', ...browser_profiles].map((profile) => ({
77+
label: profile
78+
}))
79+
const last_selected_profile =
80+
prompt_view_provider.extension_context.globalState.get<string>(
81+
LAST_SELECTED_BROWSER_PROFILE_STATE_KEY
82+
)
83+
84+
selected_profile = await new Promise<string | undefined>((resolve) => {
85+
const quick_pick = vscode.window.createQuickPick<vscode.QuickPickItem>()
86+
quick_pick.items = items
87+
if (last_selected_profile) {
88+
const active_item = items.find(
89+
(item) => item.label === last_selected_profile
90+
)
91+
if (active_item) {
92+
quick_pick.activeItems = [active_item]
93+
}
94+
}
95+
quick_pick.title = t(
96+
'views.prompt.handlers.handle-run-browser.select-profile.title'
97+
)
98+
quick_pick.placeholder = t(
99+
'views.prompt.handlers.handle-run-browser.select-profile.placeholder'
100+
)
101+
102+
const close_button: vscode.QuickInputButton = {
103+
iconPath: new vscode.ThemeIcon('close'),
104+
tooltip: t('common.close')
105+
}
106+
quick_pick.buttons = [close_button]
107+
108+
let is_accepted = false
109+
const disposables: vscode.Disposable[] = []
110+
const cleanup = () => {
111+
disposables.forEach((d) => d.dispose())
112+
quick_pick.dispose()
113+
}
114+
115+
disposables.push(
116+
quick_pick.onDidAccept(() => {
117+
is_accepted = true
118+
const selected = quick_pick.selectedItems[0]
119+
if (selected) {
120+
prompt_view_provider.extension_context.globalState.update(
121+
LAST_SELECTED_BROWSER_PROFILE_STATE_KEY,
122+
selected.label
123+
)
124+
}
125+
resolve(selected?.label)
126+
quick_pick.hide()
127+
}),
128+
quick_pick.onDidTriggerButton((button) => {
129+
if (button === close_button) {
130+
quick_pick.hide()
131+
}
132+
}),
133+
quick_pick.onDidHide(() => {
134+
if (!is_accepted) {
135+
resolve(undefined)
136+
}
137+
cleanup()
138+
})
139+
)
140+
141+
quick_pick.show()
142+
})
143+
144+
if (!selected_profile) {
145+
return
146+
}
147+
}
148+
59149
const cwc_dir = path.join(os.homedir(), '.cwc')
60150
const browser_dir = path.join(cwc_dir, 'browser')
61151

@@ -71,7 +161,7 @@ export const handle_run_browser = async () => {
71161
'browser',
72162
'out'
73163
)
74-
const user_data_dir = path.join(browser_dir, 'profiles', 'Default')
164+
const user_data_dir = path.join(browser_dir, 'profiles', selected_profile)
75165
const cached_path_file = path.join(browser_dir, '.chrome-157-path')
76166

77167
if (fs.existsSync(cached_path_file)) {

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
10021002
} else if (message.command === 'AGENTIC_SEARCH') {
10031003
await handle_agentic_search(this)
10041004
} else if (message.command === 'RUN_BROWSER') {
1005-
await handlers.handle_run_browser()
1005+
await handlers.handle_run_browser(this)
10061006
} else if (message.command === 'SHOW_ACCURACY_BOOST_INFO') {
10071007
await handlers.handle_show_accuracy_boost_info()
10081008
} else if (message.command === 'GET_CLI_CONFIGURATIONS') {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
1+
import * as fs from 'fs'
2+
import * as os from 'os'
3+
import * as path from 'path'
14
import * as vscode from 'vscode'
25
import { UpdateBrowserProfilesMessage } from '@/views/settings/types/messages'
6+
import { Logger } from '@shared/utils/logger'
37

48
export const handle_update_browser_profiles = async (
59
message: UpdateBrowserProfilesMessage
610
): Promise<void> => {
711
const config = vscode.workspace.getConfiguration('codeWebChat')
12+
const existing_profiles = config.get<string[]>('browserProfiles', []) || []
13+
814
await config.update(
915
'browserProfiles',
1016
message.browser_profiles,
1117
vscode.ConfigurationTarget.Global
1218
)
19+
20+
const updated_profiles_set = new Set(
21+
message.browser_profiles.map((p) => p.trim())
22+
)
23+
const removed_profiles = Array.from(
24+
new Set(
25+
existing_profiles
26+
.map((p) => p.trim())
27+
.filter(
28+
(p) =>
29+
p.length > 0 &&
30+
p.toLowerCase() !== 'default' &&
31+
!updated_profiles_set.has(p)
32+
)
33+
)
34+
)
35+
36+
if (removed_profiles.length === 0) {
37+
return
38+
}
39+
40+
const profiles_dir = path.join(os.homedir(), '.cwc', 'browser', 'profiles')
41+
42+
for (const profile of removed_profiles) {
43+
const profile_dir = path.resolve(profiles_dir, profile)
44+
const relative = path.relative(profiles_dir, profile_dir)
45+
if (
46+
!relative ||
47+
relative === '..' ||
48+
relative.startsWith(`..${path.sep}`) ||
49+
path.isAbsolute(relative)
50+
) {
51+
continue
52+
}
53+
54+
if (fs.existsSync(profile_dir)) {
55+
try {
56+
await fs.promises.rm(profile_dir, {
57+
recursive: true,
58+
force: true
59+
})
60+
} catch (error) {
61+
Logger.error({
62+
function_name: 'handle_update_browser_profiles',
63+
message: `Failed to remove browser profile folder "${profile}"`,
64+
data: error
65+
})
66+
}
67+
}
68+
}
1369
}

0 commit comments

Comments
 (0)