Skip to content

Commit 0528755

Browse files
committed
feat(gpt-runner-vscode): add public ip support when open in browser
1 parent c01d99a commit 0528755

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

packages/gpt-runner-shared/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@
5858
"peerDependencies": {
5959
"debug": "*",
6060
"find-free-ports": "*",
61+
"ip": "*",
6162
"minimatch": "*",
6263
"zod": "*"
6364
},
6465
"dependencies": {
6566
"debug": "^4.3.4",
6667
"find-free-ports": "^3.1.1",
68+
"ip": "^1.1.8",
6769
"minimatch": "^9.0.1",
6870
"zod": "^3.21.4"
6971
},
7072
"devDependencies": {
7173
"@types/express": "^4.17.17",
74+
"@types/ip": "^1.1.0",
7275
"express": "^4.18.2"
7376
}
7477
}

packages/gpt-runner-shared/src/node/helpers/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import open from 'open'
22
import fp from 'find-free-ports'
3+
import ip from 'ip'
34

45
export interface OpenInBrowserProps {
56
url: string
@@ -35,3 +36,8 @@ export async function getPort(props: GetPortProps): Promise<number> {
3536

3637
return freePort
3738
}
39+
40+
// return 192.168.xxx.xxx or 127.0.0.1
41+
export function getLocalHostname() {
42+
return ip.address('public', 'ipv4')
43+
}

packages/gpt-runner-vscode/src/register/open-in-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function registerOpenInBrowser(
2121

2222
disposable = vscode.commands.registerCommand(Commands.OpenInBrowser, () => {
2323
openInBrowser({
24-
url: `${getServerBaseUrl()}/#/chat?rootPath=${cwd}`,
24+
url: `${getServerBaseUrl(true)}/#/chat?rootPath=${cwd}`,
2525
})
2626
})
2727

packages/gpt-runner-vscode/src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from 'vscode'
2+
import { getLocalHostname } from '@nicepkg/gpt-runner-shared/node'
23
import { EXT_NAME } from './constant'
34
import { state } from './state'
45

@@ -41,6 +42,7 @@ export function createHash() {
4142
return text
4243
}
4344

44-
export function getServerBaseUrl() {
45-
return `http://localhost:${state.serverPort || 3003}`
45+
export function getServerBaseUrl(localIp = false) {
46+
const hostname = localIp ? getLocalHostname() : 'localhost'
47+
return `http://${hostname}:${state.serverPort || 3003}`
4648
}

packages/gpt-runner-web/client/src/helpers/global-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface GlobalConfig {
1111
window.__DEFAULT_GLOBAL_CONFIG__ = {
1212
rootPath: getSearchParams('rootPath') || '/Users/yangxiaoming/Documents/codes/gpt-runner',
1313
initialRoutePath: '/',
14-
serverBaseUrl: 'http://localhost:3003',
14+
serverBaseUrl: '',
1515
showDiffCodesBtn: false,
1616
showInsertCodesBtn: false,
1717
}

pnpm-lock.yaml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)