Skip to content

Commit 8a5b64d

Browse files
committed
Replace hardcoded fallback port with configurable forwarded ports for Remote-SSH setups
- in Settings > WEB, below Chatbots, add Ports group. It should allow users add to settings.json-held array of numbers, a port number. Don't limit the number of added ports. in `apps/editor/package.json`, name the key 'forwardedPorts'. - move the port forwarding ui to ui package - rename 'add port' to 'Forwarded ports', add missing translations - translate [Fragment], move - move ports below defaults - the field is red from the start, should not be when empty - whenever 'forwarded ports' change, send them to the connected browser, so that it knows what ports it should try to connect to - can multiple editors e.g. VS Code and VSCodium send their own sets of forwareded ports? they should - remove the + 1 port, just check the default 55155 and user-specified ones - in settings, rename WEB > Ports to WEB > Autofill. Place it as second, below Chatbots. - make 'forwarded ports' collapsible ├── apps │ ├── browser │ │ └── src │ │ └── background │ │ ├── keep-alive.ts │ │ ├── main.ts │ │ ├── message-handler.ts │ │ └── websocket.ts │ └── editor │ ├── package.json │ └── src │ ├── extension.ts │ ├── services │ │ ├── websocket-manager.ts │ │ └── websocket-server-process.ts │ ├── types │ │ └── web-configuration.ts │ ├── utils │ │ └── web-configuration-format-converters.ts │ └── views │ ├── settings │ │ ├── backend │ │ │ ├── message-handlers │ │ │ │ ├── handle-create-web-configuration.ts │ │ │ │ ├── handle-delete-web-configuration.ts │ │ │ │ ├── handle-get-forwarded-ports.ts │ │ │ │ ├── handle-get-web-configurations.ts │ │ │ │ ├── handle-pick-chatbot.ts │ │ │ │ ├── handle-pick-model.ts │ │ │ │ ├── handle-pick-reasoning-effort.ts │ │ │ │ ├── handle-reorder-web-configurations.ts │ │ │ │ ├── handle-select-default-web-configuration.ts │ │ │ │ ├── handle-set-default-web-configuration.ts │ │ │ │ ├── handle-update-forwarded-ports.ts │ │ │ │ ├── handle-update-web-configuration.ts │ │ │ │ └── index.ts │ │ │ └── settings-view-provider.ts │ │ ├── frontend │ │ │ ├── App.tsx │ │ │ ├── Home │ │ │ │ ├── Home.tsx │ │ │ │ ├── hooks │ │ │ │ │ └── use-scroll-to.ts │ │ │ │ ├── index.ts │ │ │ │ └── sections │ │ │ │ ├── ApiSection.tsx │ │ │ │ ├── CliSection.tsx │ │ │ │ ├── GeneralSection.tsx │ │ │ │ └── WebSection.tsx │ │ │ ├── Settings.tsx │ │ │ ├── forms │ │ │ │ ├── EditProviderForm │ │ │ │ │ ├── EditProviderForm.module.scss │ │ │ │ │ ├── EditProviderForm.tsx │ │ │ │ │ └── index.ts │ │ │ │ ├── EditTemplateForm │ │ │ │ │ ├── EditTemplateForm.module.scss │ │ │ │ │ ├── EditTemplateForm.tsx │ │ │ │ │ └── index.ts │ │ │ │ └── TemplatesForm │ │ │ │ ├── TemplatesForm.tsx │ │ │ │ └── index.ts │ │ │ ├── hooks │ │ │ │ ├── use-api-configuration-editing.ts │ │ │ │ ├── use-cli-configuration-editing.ts │ │ │ │ ├── use-provider-editing.ts │ │ │ │ ├── use-settings.ts │ │ │ │ ├── use-template-editing.ts │ │ │ │ └── use-web-configuration-editing.ts │ │ │ ├── i18n │ │ │ │ ├── translations │ │ │ │ │ ├── actions.ts │ │ │ │ │ ├── api.ts │ │ │ │ │ ├── cli.ts │ │ │ │ │ ├── common.ts │ │ │ │ │ ├── edit-provider-form.ts │ │ │ │ │ ├── edit-template-form.ts │ │ │ │ │ ├── general.ts │ │ │ │ │ ├── index.ts │ │ │ │ │ ├── sections.ts │ │ │ │ │ └── web.ts │ │ │ │ └── use-translation.tsx │ │ │ └── utils │ │ │ └── post-message.ts │ │ └── types │ │ └── messages.ts │ └── shared │ ├── actions │ │ └── web │ │ ├── create.ts │ │ ├── delete.ts │ │ ├── pick-chatbot.ts │ │ ├── pick-model.ts │ │ ├── reorder.ts │ │ ├── toggle-pinned.ts │ │ └── update.ts │ ├── forms │ │ └── EditWebConfigurationForm │ │ ├── EditWebConfigurationForm.module.scss │ │ ├── EditWebConfigurationForm.tsx │ │ └── index.ts │ ├── i18n │ │ ├── translations │ │ │ ├── edit-web-configuration-form.ts │ │ │ └── index.ts │ │ └── use-translation.tsx │ └── utils │ └── generate-unique-name.ts └── packages ├── shared │ └── src │ ├── constants │ │ ├── chatbots.ts │ │ └── websocket.ts │ └── types │ └── websocket-message.ts └── ui └── src └── components └── editor ├── common │ ├── Button │ │ ├── Button.module.scss │ │ ├── Button.tsx │ │ └── index.ts │ ├── Dropdown │ │ ├── Dropdown.module.scss │ │ ├── Dropdown.tsx │ │ └── index.ts │ ├── Field │ │ ├── Field.module.scss │ │ ├── Field.tsx │ │ └── index.ts │ ├── Icon │ │ ├── Icon.tsx │ │ └── index.ts │ ├── IconButton │ │ ├── IconButton.module.scss │ │ ├── IconButton.tsx │ │ └── index.ts │ ├── Input │ │ ├── Input.module.scss │ │ ├── Input.stories.tsx │ │ ├── Input.tsx │ │ └── index.ts │ ├── QuickPickButton │ │ ├── QuickPickButton.module.scss │ │ ├── QuickPickButton.tsx │ │ └── index.ts │ ├── Scrollable │ │ ├── Scrollable.module.scss │ │ ├── Scrollable.tsx │ │ └── index.ts │ ├── TextButton │ │ ├── TextButton.module.scss │ │ ├── TextButton.tsx │ │ └── index.ts │ ├── Textarea │ │ ├── Textarea.module.scss │ │ ├── Textarea.tsx │ │ └── index.ts │ └── Toggler │ ├── Toggler.module.scss │ ├── Toggler.tsx │ └── index.ts └── settings-view ├── DefaultConfigurationSelector │ ├── DefaultConfigurationSelector.module.scss │ ├── DefaultConfigurationSelector.tsx │ └── index.ts ├── ForwardedPorts │ ├── ForwardedPorts.module.scss │ ├── ForwardedPorts.tsx │ └── index.ts ├── Group │ ├── Group.module.scss │ └── Group.tsx ├── Item │ ├── Item.module.scss │ ├── Item.stories.tsx │ ├── Item.tsx │ └── index.ts ├── Layout │ ├── Layout.module.scss │ ├── Layout.tsx │ └── index.ts ├── Modal │ ├── Form │ │ ├── Form.module.scss │ │ ├── Form.tsx │ │ └── index.ts │ ├── Modal.module.scss │ ├── Modal.tsx │ └── index.ts ├── NavigationItemGroup │ ├── NavigationItemGroup.module.scss │ ├── NavigationItemGroup.tsx │ └── index.ts ├── NavigationItemSection │ ├── NavigationItemSection.module.scss │ ├── NavigationItemSection.tsx │ └── index.ts ├── NavigationSection │ ├── NavigationSection.module.scss │ ├── NavigationSection.tsx │ └── index.ts ├── Notices │ ├── Notices.module.scss │ ├── Notices.tsx │ └── index.ts ├── Section │ ├── Section.module.scss │ ├── Section.tsx │ └── index.ts ├── SortableList │ ├── SortableList.module.scss │ ├── SortableList.stories.tsx │ ├── SortableList.tsx │ └── index.ts └── Templates ├── Templates.module.scss ├── Templates.tsx └── index.ts
1 parent d81a77f commit 8a5b64d

20 files changed

Lines changed: 511 additions & 9 deletions

File tree

‎apps/browser/src/background/websocket.ts‎

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { handle_messages } from './message-handler'
33
import { DEFAULT_PORT, CLIENT_TYPE } from '@shared/constants/websocket'
44
import browser from 'webextension-polyfill'
55

6-
// Remote setups (Remote-SSH, WSL, Dev Containers) may forward the server to
7-
// DEFAULT_PORT + 1 when the local DEFAULT_PORT is already occupied.
8-
const ports_to_check = (): number[] => {
9-
return [DEFAULT_PORT, DEFAULT_PORT + 1]
6+
const FORWARDED_PORTS_BY_CLIENT_KEY = 'forwarded-ports-by-client'
7+
8+
const ports_to_check = async (): Promise<number[]> => {
9+
const storage = await browser.storage.local.get('forwarded-ports')
10+
const forwarded_ports = (storage['forwarded-ports'] as number[]) || []
11+
return [DEFAULT_PORT, ...forwarded_ports]
1012
}
1113

1214
const STALE_AFTER_MS = 20000
@@ -23,6 +25,49 @@ const connections = new Map<number, Connection>()
2325

2426
const client_id_to_port = new Map<string, number>()
2527

28+
let forwarded_ports_queue: Promise<void> = Promise.resolve()
29+
30+
const update_forwarded_ports = (params: {
31+
server_port: number
32+
client_id: string | null
33+
ports: number[] | null
34+
}) => {
35+
forwarded_ports_queue = forwarded_ports_queue
36+
.then(async () => {
37+
const storage = await browser.storage.local.get(
38+
FORWARDED_PORTS_BY_CLIENT_KEY
39+
)
40+
const by_client: Record<string, number[]> = {
41+
...((storage[FORWARDED_PORTS_BY_CLIENT_KEY] as
42+
| Record<string, number[]>
43+
| undefined) ?? {})
44+
}
45+
46+
if (params.client_id === null) {
47+
const prefix = `${params.server_port}:`
48+
for (const key of Object.keys(by_client)) {
49+
if (key.startsWith(prefix)) delete by_client[key]
50+
}
51+
} else {
52+
const key = `${params.server_port}:${params.client_id}`
53+
if (params.ports === null) {
54+
delete by_client[key]
55+
} else {
56+
by_client[key] = params.ports
57+
}
58+
}
59+
60+
const union = [...new Set(Object.values(by_client).flat())]
61+
await browser.storage.local.set({
62+
[FORWARDED_PORTS_BY_CLIENT_KEY]: by_client,
63+
'forwarded-ports': union
64+
})
65+
})
66+
.catch((e) => {
67+
console.error('Failed to update forwarded ports', e)
68+
})
69+
}
70+
2671
const get_connection = (port: number): Connection => {
2772
let c = connections.get(port)
2873
if (!c) {
@@ -57,8 +102,9 @@ const abandon_socket = (c: Connection) => {
57102
} catch {}
58103
}
59104

60-
export const check_and_recover_connection = () => {
61-
for (const port of ports_to_check()) {
105+
export const check_and_recover_connection = async () => {
106+
const ports = await ports_to_check()
107+
for (const port of ports) {
62108
const c = get_connection(port)
63109

64110
if (c.ws?.readyState === WebSocket.OPEN) {
@@ -141,6 +187,33 @@ export const connect_websocket = async (port: number): Promise<void> => {
141187
}
142188
return
143189
}
190+
if (message.action === 'connected') {
191+
update_forwarded_ports({
192+
server_port: port,
193+
client_id: null,
194+
ports: null
195+
})
196+
}
197+
if (message.action === 'forwarded-ports') {
198+
if (message.client_id) {
199+
update_forwarded_ports({
200+
server_port: port,
201+
client_id: message.client_id,
202+
ports: Array.isArray(message.ports) ? message.ports : []
203+
})
204+
}
205+
return
206+
}
207+
if (message.action === 'vscode-client-disconnected') {
208+
if (message.client_id) {
209+
client_id_to_port.delete(message.client_id)
210+
update_forwarded_ports({
211+
server_port: port,
212+
client_id: message.client_id,
213+
ports: null
214+
})
215+
}
216+
}
144217
if (message.action === 'initialize-chat' && message.client_id) {
145218
client_id_to_port.set(message.client_id, port)
146219
}
@@ -164,8 +237,9 @@ export const connect_websocket = async (port: number): Promise<void> => {
164237
}
165238
}
166239

167-
const reset_all_connections = () => {
168-
for (const port of ports_to_check()) {
240+
const reset_all_connections = async () => {
241+
const ports = await ports_to_check()
242+
for (const port of ports) {
169243
const c = get_connection(port)
170244
if (c.retry_timer) {
171245
clearTimeout(c.retry_timer)
@@ -182,6 +256,29 @@ browser.storage.onChanged.addListener((changes, areaName) => {
182256
if (areaName === 'local' && changes['browser-profile-name']) {
183257
reset_all_connections()
184258
}
259+
if (areaName === 'local' && changes['forwarded-ports']) {
260+
const new_ports = (changes['forwarded-ports'].newValue as number[]) || []
261+
const old_ports = (changes['forwarded-ports'].oldValue as number[]) || []
262+
263+
for (const port of old_ports) {
264+
if (
265+
!new_ports.includes(port) &&
266+
port !== DEFAULT_PORT
267+
) {
268+
const c = connections.get(port)
269+
if (c) {
270+
abandon_socket(c)
271+
if (c.retry_timer) {
272+
clearTimeout(c.retry_timer)
273+
c.retry_timer = null
274+
}
275+
connections.delete(port)
276+
}
277+
}
278+
}
279+
280+
check_and_recover_connection()
281+
}
185282
})
186283

187284
export const send_message_to_server = (message: any): boolean => {

‎apps/editor/package.json‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,14 @@
11151115
]
11161116
}
11171117
},
1118+
"codeWebChat.forwardedPorts": {
1119+
"type": "array",
1120+
"scope": "application",
1121+
"items": {
1122+
"type": "number"
1123+
},
1124+
"default": []
1125+
},
11181126
"codeWebChat.chatbots": {
11191127
"type": "array",
11201128
"scope": "application",

‎apps/editor/src/extension.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,16 @@ export const activate = async (extension_context: vscode.ExtensionContext) => {
299299
)
300300
)
301301

302+
extension_context.subscriptions.push(
303+
vscode.workspace.onDidChangeConfiguration((e) => {
304+
if (e.affectsConfiguration('codeWebChat.forwardedPorts')) {
305+
const config = vscode.workspace.getConfiguration('codeWebChat')
306+
const forwarded_ports = config.get<number[]>('forwardedPorts') ?? []
307+
websocket_server_instance?.send_forwarded_ports(forwarded_ports)
308+
}
309+
})
310+
)
311+
302312
const commit_status_bar_item = vscode.window.createStatusBarItem(
303313
vscode.StatusBarAlignment.Left,
304314
1000

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ export class WebSocketManager {
3939
public readonly on_connection_status_change: vscode.Event<boolean> =
4040
this._on_connection_status_change.event
4141

42+
send_forwarded_ports(ports: number[]) {
43+
if (this.client?.readyState === WebSocket.WebSocket.OPEN) {
44+
this.client.send(
45+
JSON.stringify({
46+
action: 'forwarded-ports',
47+
ports
48+
})
49+
)
50+
}
51+
}
52+
4253
constructor(extension_context: vscode.ExtensionContext) {
4354
this.extension_context = extension_context
4455
this._initialize_server()
@@ -260,6 +271,10 @@ export class WebSocketManager {
260271
this.connected_browsers = message.connected_browsers
261272
this.has_connected_browsers = this.connected_browsers.length > 0
262273
this._on_connection_status_change.fire(this.has_connected_browsers)
274+
275+
const config = vscode.workspace.getConfiguration('codeWebChat')
276+
const forwarded_ports = config.get<number[]>('forwardedPorts') ?? []
277+
this.send_forwarded_ports(forwarded_ports)
263278
} else if (message.action === 'apply-response') {
264279
const apply_msg = message as ApplyResponseMessage
265280
vscode.commands.executeCommand('codeWebChat.applyResponse', {

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,25 @@ class WebSocketServer {
281281
if (target_client && target_client.ws.readyState === WebSocket.OPEN) {
282282
target_client.ws.send(msg_string)
283283
}
284+
} else if (msg_data.action === 'forwarded-ports') {
285+
let sender_client_id: string | null = null
286+
for (const [client_id, client] of this.vscode_clients.entries()) {
287+
if (client.ws === ws) {
288+
sender_client_id = client_id
289+
break
290+
}
291+
}
292+
if (sender_client_id === null) return
293+
294+
const tagged_message = JSON.stringify({
295+
...msg_data,
296+
client_id: sender_client_id
297+
})
298+
for (const client of this.browser_clients.values()) {
299+
if (client.ws.readyState === WebSocket.OPEN) {
300+
client.ws.send(tagged_message)
301+
}
302+
}
284303
}
285304
}
286305

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as vscode from 'vscode'
2+
import { SettingsViewProvider } from '@/views/settings/backend/settings-view-provider'
3+
4+
export const handle_get_forwarded_ports = async (
5+
settings_provider: SettingsViewProvider
6+
): Promise<void> => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
const forwarded_ports = config.get<number[]>('forwardedPorts', []) || []
9+
settings_provider.postMessage({
10+
command: 'FORWARDED_PORTS',
11+
forwarded_ports
12+
})
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as vscode from 'vscode'
2+
import { UpdateForwardedPortsMessage } from '@/views/settings/types/messages'
3+
4+
export const handle_update_forwarded_ports = async (
5+
message: UpdateForwardedPortsMessage
6+
): Promise<void> => {
7+
const config = vscode.workspace.getConfiguration('codeWebChat')
8+
await config.update(
9+
'forwardedPorts',
10+
message.forwarded_ports,
11+
vscode.ConfigurationTarget.Global
12+
)
13+
}

‎apps/editor/src/views/settings/backend/message-handlers/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export * from './handle-update-cli-configuration'
6161
export * from './handle-reorder-cli-configurations'
6262
export * from './handle-pick-agent'
6363
export * from './handle-pick-example-flags'
64+
export * from './handle-get-forwarded-ports'
65+
export * from './handle-update-forwarded-ports'
6466
export * from './handle-get-web-configurations'
6567
export * from './handle-get-cli-configurations'
6668
export * from './handle-get-is-modern-ui'

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import {
6565
handle_reorder_cli_configurations,
6666
handle_pick_agent,
6767
handle_pick_example_flags,
68+
handle_get_forwarded_ports,
69+
handle_update_forwarded_ports,
6870
handle_get_web_configurations,
6971
handle_get_cli_configurations,
7072
handle_get_is_modern_ui,
@@ -260,6 +262,10 @@ export class SettingsViewProvider {
260262
await handle_pick_agent(this, message)
261263
} else if (message.command === 'PICK_EXAMPLE_FLAGS') {
262264
await handle_pick_example_flags(this, message)
265+
} else if (message.command === 'GET_FORWARDED_PORTS') {
266+
await handle_get_forwarded_ports(this)
267+
} else if (message.command === 'UPDATE_FORWARDED_PORTS') {
268+
await handle_update_forwarded_ports(message)
263269
} else if (message.command === 'GET_WEB_CONFIGURATIONS') {
264270
await handle_get_web_configurations(this)
265271
} else if (message.command === 'REORDER_WEB_CONFIGURATIONS') {
@@ -324,6 +330,7 @@ export class SettingsViewProvider {
324330
void handle_get_ai_studio_user_id(this)
325331
void handle_get_send_with_shift_enter(this)
326332
void handle_get_templates(this)
333+
void handle_get_forwarded_ports(this)
327334
void handle_get_web_configurations(this)
328335
void handle_get_cli_configurations(this)
329336
}

‎apps/editor/src/views/settings/frontend/Home/Home.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type NavItem =
2626
| 'section:general:group:agentic-search'
2727
| 'section:web'
2828
| 'section:web:group:chatbots'
29+
| 'section:web:group:ports'
2930
| 'section:web:group:web-defaults'
3031
| 'section:api'
3132
| 'section:api:group:providers'
@@ -71,6 +72,10 @@ export const NAV_ITEMS_CONFIG: NavConfigItem[] = [
7172
id: 'section:web:group:chatbots',
7273
label: 'chatbots.configurations.title'
7374
},
75+
{
76+
id: 'section:web:group:ports',
77+
label: 'web.autofill.title'
78+
},
7479
{
7580
id: 'section:web:group:web-defaults',
7681
label: 'common.defaults'
@@ -112,6 +117,7 @@ export const NAV_ITEMS_CONFIG: NavConfigItem[] = [
112117
type Props = {
113118
providers: Provider[]
114119
api_configurations: ApiConfiguration[]
120+
forwarded_ports: number[]
115121
web_configurations: WebConfiguration[]
116122
cli_configurations: CliConfiguration[]
117123
defaults: Record<ApiFeature, string | null>
@@ -209,6 +215,7 @@ type Props = {
209215
on_edit_web_configuration: (id: string) => void
210216
on_delete_web_configuration: (name: string) => void
211217
on_toggle_pinned_web_configuration: (config: WebConfiguration) => void
218+
on_update_forwarded_ports: (ports: number[]) => void
212219
web_defaults: Record<string, string | null>
213220
on_set_default_web_configuration: (
214221
web_feature: string,
@@ -446,6 +453,8 @@ export const Home: React.FC<Props> = (props) => {
446453
<WebSection
447454
ref={(el) => set_section_ref('section:web', el)}
448455
set_section_ref={set_section_ref}
456+
forwarded_ports={props.forwarded_ports}
457+
on_update_forwarded_ports={props.on_update_forwarded_ports}
449458
web_configurations={props.web_configurations}
450459
set_web_configurations={props.set_web_configurations}
451460
on_reorder_web_configurations={props.on_reorder_web_configurations}

0 commit comments

Comments
 (0)