mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
fix(web): align dev server node runtime
This commit is contained in:
1 parent
eebb428dd3
commit
e3370b52c2
6 files changed
+44
-28
No files matched your search
+3
-3
@@ -8,7 +8,7 @@ import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { execSync } from 'child_process'
|
||||
import { Command } from 'commander'
|
||||
import { loadConfig, getConfigPath } from '@openchatlab/config'
|
||||
import { DEFAULT_API_PORT, loadConfig, getConfigPath } from '@openchatlab/config'
|
||||
import {
|
||||
NodePathProvider,
|
||||
DatabaseManager,
|
||||
@@ -276,7 +276,7 @@ program
|
||||
program
|
||||
.command('serve')
|
||||
.description('Start the HTTP API server')
|
||||
.option('--port <port>', 'Server port', '3110')
|
||||
.option('--port <port>', 'Server port', String(DEFAULT_API_PORT))
|
||||
.option('--host <host>', 'Listen address', '127.0.0.1')
|
||||
.option('--token <token>', 'Custom Bearer Token (reads from config or auto-generates if omitted)')
|
||||
.option('--web [dir]', 'Serve Web frontend static assets (defaults to dist-web/)')
|
||||
@@ -335,7 +335,7 @@ program
|
||||
program
|
||||
.command('web')
|
||||
.description('Launch Web UI with one click (HTTP API + browser auto-open)')
|
||||
.option('--port <port>', 'Server port', '3110')
|
||||
.option('--port <port>', 'Server port', String(DEFAULT_API_PORT))
|
||||
.option('--host <host>', 'Listen address', '127.0.0.1')
|
||||
.option('--no-open', 'Do not auto-open browser')
|
||||
.action(async (options) => {
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
export { loadConfig, getConfigPath, getConfigDir, writeConfigField } from './loader'
|
||||
export { configSchema } from './schema'
|
||||
export { configSchema, DEFAULT_API_PORT } from './schema'
|
||||
export type { ChatLabConfig, LlmConfig, DataConfig, ApiConfig, LocaleConfig, UiConfig } from './schema'
|
||||
export {
|
||||
loadAuthProfiles,
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
import { z } from 'zod'
|
||||
|
||||
export const DEFAULT_API_PORT = 3110
|
||||
|
||||
export const llmConfigSchema = z.object({
|
||||
provider: z.string().default(''),
|
||||
model: z.string().default(''),
|
||||
@@ -17,7 +19,7 @@ export const dataConfigSchema = z.object({
|
||||
})
|
||||
|
||||
export const apiConfigSchema = z.object({
|
||||
port: z.number().int().min(1).max(65535).default(3210),
|
||||
port: z.number().int().min(1).max(65535).default(DEFAULT_API_PORT),
|
||||
host: z.string().default('127.0.0.1'),
|
||||
token: z.string().default(''),
|
||||
})
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
export function createChatlabServeCommand({ serverDir, backendPort, nodeExecutable = process.execPath }) {
|
||||
return {
|
||||
command: nodeExecutable,
|
||||
args: ['--watch', '--import', 'tsx', 'src/cli.ts', 'serve', '--port', String(backendPort)],
|
||||
options: {
|
||||
cwd: serverDir,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: { ...process.env },
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import { createChatlabServeCommand } from './dev-server-command.mjs'
|
||||
|
||||
test('web dev backend runs through the current Node executable with the tsx loader', () => {
|
||||
const command = createChatlabServeCommand({
|
||||
rootDir: '/repo',
|
||||
serverDir: '/repo/apps/cli',
|
||||
coreDir: '/repo/packages/core/src',
|
||||
runtimeDir: '/repo/packages/node-runtime/src',
|
||||
backendPort: 3110,
|
||||
nodeExecutable: '/custom/node',
|
||||
})
|
||||
|
||||
assert.equal(command.command, '/custom/node')
|
||||
assert.deepEqual(command.args, ['--watch', '--import', 'tsx', 'src/cli.ts', 'serve', '--port', '3110'])
|
||||
})
|
||||
+8
-23
@@ -17,8 +17,10 @@ import * as net from 'net'
|
||||
import { defineConfig, type Plugin } from 'vite'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import ui from '@nuxt/ui/vite'
|
||||
import { DEFAULT_API_PORT } from './packages/config/src/schema'
|
||||
import { createChatlabServeCommand } from './scripts/dev-server-command.mjs'
|
||||
|
||||
const BACKEND_PORT = 3110
|
||||
const BACKEND_PORT = DEFAULT_API_PORT
|
||||
|
||||
function isPortInUse(port: number): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
@@ -51,28 +53,11 @@ function chatlabServePlugin(): Plugin {
|
||||
}
|
||||
|
||||
const serverDir = resolve(__dirname, 'apps/cli')
|
||||
const coreDir = resolve(__dirname, 'packages/core/src')
|
||||
const runtimeDir = resolve(__dirname, 'packages/node-runtime/src')
|
||||
serverProcess = spawn(
|
||||
'pnpm',
|
||||
[
|
||||
'tsx',
|
||||
'watch',
|
||||
'--include',
|
||||
`${coreDir}/**`,
|
||||
'--include',
|
||||
`${runtimeDir}/**`,
|
||||
'src/cli.ts',
|
||||
'serve',
|
||||
'--port',
|
||||
String(BACKEND_PORT),
|
||||
],
|
||||
{
|
||||
cwd: serverDir,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: { ...process.env },
|
||||
}
|
||||
)
|
||||
const serveCommand = createChatlabServeCommand({
|
||||
serverDir,
|
||||
backendPort: BACKEND_PORT,
|
||||
})
|
||||
serverProcess = spawn(serveCommand.command, serveCommand.args, serveCommand.options)
|
||||
|
||||
serverProcess.stdout?.on('data', (data: Buffer) => {
|
||||
const line = data.toString().trim()
|
||||
|
||||
Reference in new issue
Block a user