feat(gpt-runner-vscode): support without nodejs need

This commit is contained in:
JinmingYang
2023-07-04 11:35:32 +08:00
parent 2cacd7a8ff
commit 98bc3ebd56
9 changed files with 52 additions and 62 deletions

View File

@@ -4,9 +4,10 @@
"unbuild",
"msw",
"open",
"commander"
"commander",
"axios"
],
"packageMode": {
"react": "minor"
}
}
}

View File

@@ -51,6 +51,7 @@
"OPENAI",
"OVSE",
"ovsx",
"ponyfill",
"pycache",
"rehype",
"rubberband",

View File

@@ -65,7 +65,8 @@
},
"pnpm": {
"overrides": {
"magic-string": "^0.30.0"
"magic-string": "^0.30.0",
"axios": "1.3.4"
}
},
"simple-git-hooks": {

View File

@@ -89,7 +89,7 @@
"dependencies": {
"@kvs/node-localstorage": "^2.1.3",
"@kvs/storage": "^2.1.3",
"axios": "^1.4.0",
"axios": "1.3.4",
"cachedir": "^2.3.0",
"debug": "^4.3.4",
"find-free-ports": "^3.1.1",

View File

@@ -26,10 +26,14 @@ export function checkNodeVersion() {
return `You are using Node ${currentNodeVersion}, but GPT-Runner requires Node ${MIN_NODE_VERSION}.\nPlease upgrade your Node version in https://nodejs.org/en/download`
}
export function getRunServerEnv() {
export function canUseNodeFetchWithoutCliFlag() {
const currentNodeVersion = process.version
if (compareVersion(currentNodeVersion, '19.0.0') <= 0) {
return compareVersion(currentNodeVersion, '18.0.0') > 0
}
export function getRunServerEnv() {
if (!canUseNodeFetchWithoutCliFlag()) {
return {
NODE_OPTIONS: '--experimental-fetch',
NODE_NO_WARNINGS: '1',

View File

@@ -19,10 +19,11 @@ export function openInBrowser(props: OpenInBrowserProps) {
export interface GetPortProps {
defaultPort?: number
autoFreePort?: boolean
excludePorts?: number[]
}
export async function getPort(props: GetPortProps): Promise<number> {
const { defaultPort, autoFreePort } = props
const { defaultPort, autoFreePort, excludePorts } = props
if (defaultPort) {
if (!autoFreePort)
@@ -34,12 +35,18 @@ export async function getPort(props: GetPortProps): Promise<number> {
return defaultPort
}
const [freePort] = await fp.findFreePorts(1, {
const freePorts = await fp.findFreePorts(1, {
startPort: 3001,
endPort: 9999,
isFree: async (port) => {
if (excludePorts?.includes(port))
return false
return fp.isFreePort(port)
},
})
return freePort
return freePorts[0]
}
// return 192.168.xxx.xxx or 127.0.0.1

View File

@@ -1,11 +1,10 @@
import child_process from 'child_process'
import { getPort, getRunServerEnv } from '@nicepkg/gpt-runner-shared/node'
import type child_process from 'child_process'
import { getPort } from '@nicepkg/gpt-runner-shared/node'
import type { Disposable, ExtensionContext } from 'vscode'
import * as vscode from 'vscode'
import waitPort from 'wait-port'
import type { ContextLoader } from '../contextLoader'
import { Commands } from '../constant'
import { log } from '../log'
import { state } from '../state'
export async function registerServer(
@@ -28,25 +27,20 @@ export async function registerServer(
serverProcess?.kill?.()
const { extensionUri } = ext
const serverUri = vscode.Uri.joinPath(extensionUri, './dist/web/start-server.cjs')
const serverUri = vscode.Uri.joinPath(extensionUri, './dist/web/server.cjs')
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { startServer } = require(serverUri.fsPath)
// always get a random free port
const finalPort = await getPort({
autoFreePort: true,
excludePorts: [3003, 3006],
})
serverProcess = child_process.spawn('node', [
serverUri.fsPath,
'--port',
String(finalPort),
'--client-dist-path',
vscode.Uri.joinPath(extensionUri, './dist/web/browser').fsPath,
], {
env: {
...process.env,
...getRunServerEnv(),
DEBUG: 'enabled',
},
await startServer({
port: finalPort,
autoFreePort: false,
clientDistPath: vscode.Uri.joinPath(extensionUri, './dist/web/browser').fsPath,
})
await waitPort({
@@ -55,18 +49,6 @@ export async function registerServer(
})
state.serverPort = finalPort
serverProcess.stdout.on('data', (data: string) => {
log.appendLine(`stdout: ${data}`)
})
serverProcess.stderr.on('data', (data: string) => {
log.appendLine(`stderr: ${data}`)
})
serverProcess.on('close', (code: string) => {
log.appendLine(`child process exited with code ${code}`)
})
})
return serverDisposable

View File

@@ -1,6 +1,15 @@
import { getDefaultProxyUrl } from '@nicepkg/gpt-runner-shared/node'
import { canUseNodeFetchWithoutCliFlag, getDefaultProxyUrl } from '@nicepkg/gpt-runner-shared/node'
import { bootstrap } from 'global-agent'
import { ProxyAgent, setGlobalDispatcher } from 'undici'
import { Headers, ProxyAgent, Request, Response, fetch, setGlobalDispatcher } from 'undici'
if (!canUseNodeFetchWithoutCliFlag()) {
console.log('GPT Runner: add polyfill for fetch', process.version)
// polyfill for nodejs < 18.0.0
globalThis.fetch = fetch as any
globalThis.Headers = Headers as any
globalThis.Request = Request as any
globalThis.Response = Response as any
}
// global proxy
async function startProxy() {

31
pnpm-lock.yaml generated
View File

@@ -2,6 +2,7 @@ lockfileVersion: '6.0'
overrides:
magic-string: ^0.30.0
axios: 1.3.4
importers:
@@ -179,8 +180,8 @@ importers:
specifier: ^2.1.3
version: 2.1.3
axios:
specifier: ^1.4.0
version: 1.4.0(debug@4.3.4)
specifier: 1.3.4
version: 1.3.4(debug@4.3.4)
cachedir:
specifier: ^2.3.0
version: 2.3.0
@@ -3079,7 +3080,7 @@ packages:
resolution: {integrity: sha512-FARJariPFUZ6SCrBEMNQf1vQHTi3xvSWayP2qWtbAWc+MSzzWeaChBNaq2r9nDljZhjryTlTMmLYL0fGCY1T/A==}
engines: {node: '>=12.9.0'}
dependencies:
axios: 1.4.0(debug@4.3.4)
axios: 1.3.4(debug@4.3.4)
transitivePeerDependencies:
- debug
dev: false
@@ -6738,24 +6739,8 @@ packages:
resolution: {integrity: sha512-DMD0KiN46eipeziST1LPP/STfDU0sufISXmjSgvVsoU2tqxctQeASejWcfNtxYKqETM1UxQ8sp2OrSBWpHY6sw==}
engines: {node: '>= 0.4'}
/axios@0.25.0:
resolution: {integrity: sha512-cD8FOb0tRH3uuEe6+evtAbgJtfxr7ly3fQjYcMcuPlgkwVS9xboaVIpcDV+cYQe+yGykgwZCs1pzjntcGa6l5g==}
dependencies:
follow-redirects: 1.15.2(debug@4.3.4)
transitivePeerDependencies:
- debug
dev: false
/axios@0.26.1:
resolution: {integrity: sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==}
dependencies:
follow-redirects: 1.15.2(debug@4.3.4)
transitivePeerDependencies:
- debug
dev: false
/axios@1.4.0(debug@4.3.4):
resolution: {integrity: sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==}
/axios@1.3.4(debug@4.3.4):
resolution: {integrity: sha512-toYm+Bsyl6VC5wSkfkbbNB6ROv7KY93PEBBL6xyDczaIHasAiv4wPqQ/c4RjoQzipxRD2W5g21cOqQulZ7rHwQ==}
dependencies:
follow-redirects: 1.15.2(debug@4.3.4)
form-data: 4.0.0
@@ -13662,7 +13647,7 @@ packages:
/openai@3.3.0:
resolution: {integrity: sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==}
dependencies:
axios: 0.26.1
axios: 1.3.4(debug@4.3.4)
form-data: 4.0.0
transitivePeerDependencies:
- debug
@@ -17912,7 +17897,7 @@ packages:
engines: {node: '>=10.0.0'}
hasBin: true
dependencies:
axios: 0.25.0
axios: 1.3.4(debug@4.3.4)
joi: 17.9.2
lodash: 4.17.21
minimist: 1.2.8