Refactor CNB command to improve configuration handling and add default keep.json

- Changed import of 'path' module to use default import syntax.
- Updated 'live' command to accept a configuration file path with higher priority than JSON input.
- Removed stdin reading for JSON input; now reads directly from the specified config file.
- Simplified error handling for JSON parsing and configuration validation.
- Enhanced keepAlive creation by removing unnecessary callbacks and enabling debug mode.
- Added default keep.json file with example configuration.
This commit is contained in:
2026-02-15 23:52:32 +08:00
parent 786ee80839
commit 76aeb21a41
5 changed files with 390 additions and 518 deletions

View File

@@ -10,7 +10,7 @@
],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT",
"packageManager": "pnpm@10.28.2",
"packageManager": "pnpm@10.29.3",
"type": "module",
"files": [
"dist",
@@ -42,27 +42,27 @@
}
},
"devDependencies": {
"@inquirer/prompts": "^8.2.0",
"@inquirer/prompts": "^8.2.1",
"@kevisual/ai": "^0.0.24",
"@kevisual/api": "^0.0.44",
"@kevisual/api": "^0.0.50",
"@kevisual/load": "^0.0.6",
"@kevisual/local-app-manager": "^0.1.32",
"@kevisual/logger": "^0.0.4",
"@kevisual/query": "0.0.39",
"@kevisual/query": "0.0.40",
"@kevisual/query-login": "0.0.7",
"@kevisual/router": "^0.0.70",
"@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.30",
"@opencode-ai/plugin": "^1.1.49",
"@types/bun": "^1.3.8",
"@types/node": "^25.2.0",
"@opencode-ai/plugin": "^1.2.4",
"@types/bun": "^1.3.9",
"@types/node": "^25.2.3",
"@types/send": "^1.2.1",
"@types/ws": "^8.18.1",
"chalk": "^5.6.2",
"commander": "^14.0.3",
"cross-env": "^10.1.0",
"dayjs": "^1.11.19",
"dotenv": "^17.2.3",
"dotenv": "^17.3.1",
"get-port": "^7.1.0",
"nanoid": "^5.1.6",
"send": "^1.2.1",
@@ -77,15 +77,15 @@
"access": "public"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.981.0",
"@aws-sdk/client-s3": "^3.990.0",
"@kevisual/js-filter": "^0.0.5",
"@kevisual/oss": "^0.0.19",
"@kevisual/video-tools": "^0.0.13",
"@opencode-ai/sdk": "^1.1.49",
"@opencode-ai/sdk": "^1.2.4",
"es-toolkit": "^1.44.0",
"eventemitter3": "^5.0.4",
"lowdb": "^7.0.1",
"lru-cache": "^11.2.5",
"lru-cache": "^11.2.6",
"pm2": "^6.0.14",
"unstorage": "^1.17.4",
"zod": "^4.3.6"

5
keep.json Normal file
View File

@@ -0,0 +1,5 @@
{
"wss": "wss://cnb-pf8-1jhgvbrkm-001.cnb.space:443/stable-3c0b449c6e6e37b44a8a7938c0d8a3049926a64c?reconnectionToken=3d90027f-b2b1-4c60-a3b4-f061b75ec073&reconnection=false&skipWebSocketFrames=false",
"cookie": "orange:workspace:cookie-session:cnb-pf8-1jhgvbrkm-001=9cc870da-d3d5-44ee-afdc-7498e1111186",
"url": "https://cnb-pf8-1jhgvbrkm-001.cnb.space/?folder=/workspace"
}

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/cli",
"version": "0.1.2",
"version": "0.1.3",
"description": "envision 命令行工具",
"type": "module",
"basename": "/root/cli",
@@ -30,13 +30,10 @@
],
"scripts": {
"dev": "bun src/run.ts ",
"dev:tsx": "tsx src/run.ts ",
"dev:server": "cd assistant && bun --watch src/run-server.ts ",
"build": "rimraf dist && bun run bun.config.mjs",
"deploy": "ev pack -u -p -m no",
"pub:me": "npm publish --registry https://npm.xiongxiao.me --tag beta",
"postbuild": "cd assistant && pnpm build ",
"dts": "dts-bundle-generator --external-inlines=@types/jsonwebtoken src/index.ts -o dist/index.d.ts "
"postbuild": "cd assistant && pnpm build "
},
"keywords": [
"kevisual",
@@ -44,32 +41,32 @@
],
"author": "abearxiong",
"dependencies": {
"@inquirer/prompts": "^8.2.0",
"@inquirer/prompts": "^8.2.1",
"@kevisual/app": "^0.0.2",
"@kevisual/auth": "^2.0.3",
"@kevisual/context": "^0.0.4",
"@kevisual/context": "^0.0.6",
"@kevisual/use-config": "^1.0.30",
"@opencode-ai/sdk": "^1.1.53",
"@opencode-ai/sdk": "^1.2.4",
"@types/busboy": "^1.5.4",
"busboy": "^1.6.0",
"eventemitter3": "^5.0.4",
"jose": "^6.1.3",
"lowdb": "^7.0.1",
"lru-cache": "^11.2.5",
"lru-cache": "^11.2.6",
"micromatch": "^4.0.8",
"pm2": "latest",
"semver": "^7.7.4",
"unstorage": "^1.17.4"
},
"devDependencies": {
"@kevisual/api": "^0.0.47",
"@kevisual/cnb": "^0.0.22",
"@kevisual/api": "^0.0.50",
"@kevisual/cnb": "^0.0.26",
"@kevisual/dts": "^0.0.3",
"@kevisual/load": "^0.0.6",
"@kevisual/logger": "^0.0.4",
"@kevisual/query": "0.0.40",
"@kevisual/query-login": "0.0.7",
"@types/bun": "^1.3.8",
"@types/bun": "^1.3.9",
"@types/crypto-js": "^4.2.2",
"@types/jsonwebtoken": "^9.0.10",
"@types/micromatch": "^4.0.10",

827
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
import { createKeepAlive } from '@kevisual/cnb/keep'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import path from 'node:path'
import { program, Command } from '@/program.ts';
@@ -16,25 +16,17 @@ const cnbCmd = new Command('cnb')
const liveCmd = new Command('live')
.description('启动 CNB Keep Alive 服务')
.option('-j, --json <string>', 'JSON数据 (不提供则从 stdin 读取)')
.option('-j, --json <string>', 'JSON数据')
.option('-c, --config <string>', '配置文件路径 (优先级高于 JSON 参数), 默认keep.json')
.action(async (opts) => {
let config: { wss: string; cookie: string; url?: string };
const configPath = path.join(process.cwd(), opts.config || 'keep.json');
try {
let jsonString = opts.json;
// 如果没有提供 json 参数,从 stdin 读取
if (!jsonString) {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
jsonString = Buffer.concat(chunks).toString('utf-8').trim();
}
// 从文件读取
else if (jsonString.startsWith('@')) {
const filePath = resolve(jsonString.slice(1));
jsonString = readFileSync(filePath, 'utf-8').trim();
jsonString = readFileSync(configPath, 'utf-8').trim();
}
config = JSON.parse(jsonString);
@@ -42,30 +34,19 @@ const liveCmd = new Command('live')
console.error('JSON 解析错误: 请检查输入的 JSON 格式是否正确');
process.exit(1);
}
if (!config.wss || !config.cookie) {
console.error('配置错误: 必须包含 wss 和 cookie 字段');
process.exit(1);
}
const keepAlive = createKeepAlive({
createKeepAlive({
wsUrl: config.wss,
cookie: config.cookie,
onConnect: () => {
console.log('已连接到 CNB 服务器');
},
onDisconnect: (code) => {
console.log(`与 CNB 服务器断开连接,代码: ${code}`);
},
onError: (error) => {
console.error('CNB 连接错误:', error);
},
onMessage: (data) => {
console.log('收到 CNB 消息:', data.toString());
},
debug: true
});
keepAlive.connect();
});
cnbCmd.addCommand(liveCmd);