Compare commits

..

51 Commits

Author SHA1 Message Date
14f2dad837 fix: 优化connect ws的模块 2026-01-26 18:51:59 +08:00
915f7aff6b 更新 @opencode-ai/sdk 版本至 1.1.36,调整类型定义以支持 AssistantConfig 2026-01-26 04:20:23 +08:00
15d81c4f68 更新依赖项版本,添加文件哈希功能,优化远程应用连接逻辑 2026-01-26 04:18:03 +08:00
48dafc6040 update pkgs fix lightcode 2026-01-26 01:13:49 +08:00
2ccda9f008 update 2026-01-26 01:13:10 +08:00
6cf3beae94 update 2026-01-25 02:56:56 +08:00
a583fdc211 update 2026-01-22 17:31:26 +08:00
59b3961ff9 temp 2026-01-22 12:51:44 +08:00
ddfdb63598 fix: 修复配置文档中的表格格式;添加路由配置示例 2026-01-22 01:53:51 +08:00
18a6fd4cfe fix doc 2026-01-21 23:25:45 +08:00
028a6ac726 feat: restructure command for Claude models and add new remote routes
- Deleted the old cc.ts command and created a new cc.ts under src/command/claude for better organization.
- Added support for a new model 'bailian' in the command.
- Implemented remote app connection status and connection routes in assistant/src/routes/remote/index.ts.
- Updated index.ts to reflect the new path for the cc command.
- Added a placeholder for future management of plugin operations in src/command/opencode/plugin.ts.
2026-01-21 23:22:58 +08:00
a911334459 fix: 调整重连逻辑以增加每次连接尝试的延迟时间;更新 mvFile 方法以支持 'app-file' 类型 2026-01-21 15:56:28 +08:00
3f899dbd5f update 2026-01-20 23:23:07 +08:00
bb2b129343 fix: 更新 pm2 依赖至最新版本 2026-01-20 21:12:47 +08:00
xiongxiao
2eeaf991b9 update 2026-01-20 20:05:45 +08:00
ddba845ce7 fix: 更新缓存文件路径的创建逻辑 2026-01-20 17:45:59 +08:00
a4e04e7afa chore: 更新版本号至 0.0.89 2026-01-20 17:35:28 +08:00
xiongxiao
398c41a512 update 2026-01-20 17:34:53 +08:00
xiongxiao
dbd044ec66 clear pnpmstore 2026-01-20 16:00:39 +08:00
2b55c2bd03 update 2026-01-20 15:57:21 +08:00
89470346be udpate 2026-01-20 15:39:46 +08:00
9f20e149a0 feat: 更新依赖项,添加 OpenCode 支持,重构代理和路由逻辑,新增 AGENTS 文档 2026-01-20 02:46:29 +08:00
26b4ffa3a2 feat: 移除fileProxy函数,更新ProxyInfo类型,修复文件代理逻辑 2026-01-19 03:52:16 +08:00
43992d896f feat: 重构代理功能,添加文件代理支持并优化相关逻辑 2026-01-18 13:41:47 +08:00
5e5f4f6543 update 2026-01-18 00:31:57 +08:00
fee5076e16 feat: 更新authFilter以允许对/root路径的公共访问并修改相关消息 2026-01-17 23:34:08 +08:00
91d4fed474 feat: update README with installation command and add debug logs in deploy command
- Added installation command for the CLI tool in README.
- Enhanced deploy command with debug logging for upload results and query app version.
- Integrated useKey for fetching KEVISUAL_TOKEN in get-config module.
- Added debug logging in queryAppVersion for better traceability.
- Updated temp.md with new dependency and example command for deployment.
2026-01-17 23:28:38 +08:00
5395449751 update 2026-01-17 20:19:07 +08:00
2cb12644ea update 2026-01-17 16:50:29 +08:00
e1b86aa809 udpate 2026-01-17 16:41:50 +08:00
2f79925e3d update 2026-01-17 16:19:49 +08:00
0b5a0557ee update 2026-01-17 14:48:49 +08:00
b9b4c993f4 add button 2026-01-17 01:15:04 +08:00
99f01e2b94 add cnb 2026-01-17 01:14:00 +08:00
cc043bfd7e update 2026-01-17 01:13:55 +08:00
327f115ef2 update 2026-01-16 01:39:01 +08:00
6a375db5a1 update 2026-01-15 18:59:06 +08:00
a5ce44ba70 更新依赖,增强ASR功能,优化代理路由处理,添加登录页面渲染 2026-01-15 12:42:39 +08:00
2011ea818c update 2026-01-15 10:48:41 +08:00
146fde274e fix: add login -e 登录通过env的环境变量登录 2026-01-15 09:26:20 +08:00
8549a4aa53 feat: migrate from inquirer to @inquirer/prompts for interactive prompts
- Replaced inquirer with @inquirer/prompts in various command files for improved prompt handling.
- Updated confirmation and input prompts in commands such as app, config, deploy, login, and others.
- Added a new command 'cc' for switching between Claude code models with appropriate configurations.
- Enhanced user experience by ensuring prompts are more streamlined and consistent across the application.
2026-01-15 09:17:07 +08:00
4f541748da fix 2026-01-14 00:14:25 +08:00
4723efbbd2 update 2026-01-11 23:16:49 +08:00
3de89b6985 update 2025-12-29 14:42:50 +08:00
a90f98ef3f 更新依赖并增强应用管理命令功能 2025-12-29 03:33:11 +08:00
9647fcf48f update 2025-12-29 01:54:45 +08:00
f1f2d81201 update add asr config 2025-12-29 01:53:07 +08:00
9f580a7709 update 2025-12-29 01:34:21 +08:00
91409861b9 update 2025-12-29 01:34:06 +08:00
ff7e6aa152 bump 2025-12-24 00:28:59 +08:00
a52ac525bf feat: 更新依赖项版本,调整 pnpm-lock.yaml 和 package.json 中的 @kevisual/ha-api 版本,优化 WebSocket 处理逻辑 2025-12-24 00:28:39 +08:00
98 changed files with 5380 additions and 3120 deletions

44
.cnb.yml Normal file
View File

@@ -0,0 +1,44 @@
# .cnb.yml
include:
- https://cnb.cool/kevisual/cnb/-/blob/main/.cnb/template.yml
.common_env: &common_env
env:
TO_REPO: kevisual/cli
TO_URL: git.xiongxiao.me
imports:
- https://cnb.cool/kevisual/env/-/blob/main/.env.development
$:
vscode:
- docker:
image: docker.cnb.cool/kevisual/dev-env:latest
services:
- vscode
- docker
imports: !reference [.common_env, imports]
# 开发环境启动后会执行的任务
# stages:
# - name: pnpm install
# script: pnpm install
stages: !reference [.dev_tempalte, stages]
.common_sync_to_gitea: &common_sync_to_gitea
- <<: *common_env
services: !reference [.common_sync_to_gitea_template, services]
stages: !reference [.common_sync_to_gitea_template, stages]
.common_sync_from_gitea: &common_sync_from_gitea
- <<: *common_env
services: !reference [.common_sync_from_gitea_template, services]
stages: !reference [.common_sync_from_gitea_template, stages]
main:
web_trigger_sync_to_gitea:
- <<: *common_sync_to_gitea
web_trigger_sync_from_gitea:
- <<: *common_sync_from_gitea
api_trigger_sync_to_gitea:
- <<: *common_sync_to_gitea
api_trigger_sync_from_gitea:
- <<: *common_sync_from_gitea

11
.cnb/web_trigger.yml Normal file
View File

@@ -0,0 +1,11 @@
# .cnb/web_trigger.yml
branch:
# 如下按钮在分支名以 release 开头的分支详情页面显示
- reg: "^main"
buttons:
- name: 同步代码到gitea
description: 同步代码到gitea
event: web_trigger_sync_to_gitea
- name: 同步gitea代码到当前仓库
description: 同步gitea代码到当前仓库
event: web_trigger_sync_from_gitea

3
.gitignore vendored
View File

@@ -8,3 +8,6 @@ pack-dist
assistant-app
build
.pnpm-store
jwt

1
.npmrc
View File

@@ -1,2 +1,3 @@
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
//npm.cnb.cool/kevisual/registry/-/packages/:_authToken=${CNB_API_KEY}
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

View File

@@ -0,0 +1 @@
export { AgentPlugin } from "../../assistant/src/main.ts";

2
apps/.gitignore vendored
View File

@@ -1,2 +0,0 @@
container
root

View File

@@ -1,13 +0,0 @@
{
"name": "apps",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

View File

@@ -10,4 +10,6 @@ assistant-app
.env*
!.env*example
libs
libs
cache-file

View File

@@ -0,0 +1 @@
export { AgentPlugin } from "../../src/main.ts";

View File

@@ -0,0 +1,35 @@
---
name: kill-opencode
description: 自动查找并杀死所有opencode相关的进程确保系统资源释放。
tags:
- opencode
- process-management
- automation
---
```bash
#!/bin/bash
# kill_opencode.sh - 自动杀死所有opencode进程
echo "正在查找opencode进程..."
ps aux | grep opencode | grep -v grep
if [ $? -eq 0 ]; then
echo "正在杀死所有opencode进程..."
pkill -f opencode
sleep 2
# 检查是否还有残留进程
remaining=$(ps aux | grep opencode | grep -v grep | wc -l)
if [ $remaining -gt 0 ]; then
echo "发现 $remaining 个顽固进程,使用强制杀死模式..."
pkill -9 -f opencode
fi
echo "opencode进程清理完成"
else
echo "未找到opencode进程"
fi
# 验证是否已完全清理
echo "当前opencode进程状态"
ps aux | grep opencode | grep -v grep || echo "没有运行中的opencode进程"
```

204
assistant/AGENTS.md Normal file
View File

@@ -0,0 +1,204 @@
# AGENTS.md
## 构建、代码检查和测试命令
### 核心命令
```bash
# 开发
bun run dev # 运行主入口 (src/run.ts)
bun run dev:server # 启动服务器(热重载)
bun run dev:cnb # 使用自定义配置目录启动服务器
bun run dev:share # 测试远程应用
# 构建
bun run build # 完整生产构建(先清理 dist
bun run build:lib # 构建库文件
bun run postbuild:lib # 生成 TypeScript 类型定义
# 包管理
pnpm install # 安装依赖(使用 pnpm v10.28.0
```
### 环境要求
- **Node.js**: >=22.0.0
- **运行时**: Bun开发环境/ Node.js生产环境
- **包管理器**: pnpm@10.28.0(强制要求)
### 环境变量
- `ASSISTANT_CONFIG_DIR`: 覆盖助手配置文件目录路径
- `BUN_PATH`: 覆盖 Bun 可执行文件路径
---
## 代码风格规范
### TypeScript
- **严格模式**: 通过扩展配置启用(`@kevisual/types/json/backend.json`
- **模块格式**: 仅 ESM`package.json` 中的 `"type": "module"`
- **目标**: NodeNext 模块解析
- **路径别名**: 使用 `@/*` 作为本地导入别名(如 `import { Foo } from '@/module/foo'`
### 导入规则
```typescript
// Node.js 内置模块必须使用 node: 协议
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
// 外部包(不使用路径别名)
import chalk from 'chalk';
import { program } from 'commander';
// 本地模块(使用 @ 别名)
import { AssistantInit } from '@/services/init/index.ts';
// 类型导入
import type { AssistantConfig } from '@/module/assistant/index.ts';
```
### 命名规范
| 类型 | 规范 | 示例 |
|------|------|------|
| 类 | PascalCase | `AssistantInit`, `AssistantApp`, `AssistantQuery` |
| 接口 | PascalCase | `ProxyInfo`, `AssistantInitOptions` |
| 类型 | PascalCase | `AssistantConfigData` |
| 函数 | camelCase | `getBunPath`, `checkFileExists`, `parseHomeArg` |
| 变量 | camelCase | `configDir`, `assistantConfig`, `isPortAvailable` |
| 私有字段 | `#` 前缀 + camelCase | `#query`, `#config` |
| 常量 | UPPER_SNAKE_CASE 或 camelCase | `commonPaths`, `randomId` |
| 文件名 | kebab-case | `proxy-page-index.ts`, `query-login.ts` |
| 目录名 | kebab-case | `query-login`, `hot-api` |
### 代码模式
#### 错误处理
```typescript
// 同步操作包装
try {
const result = fs.readFileSync(path);
return result;
} catch (error) {
console.error('读取文件错误:', error.message);
continue; // 或 return/throw
}
// 异步操作包装
try {
const result = await someAsyncOperation();
return result;
} catch (error) {
console.error('操作失败:', error.message);
process.exit(1); // 或 throw/return undefined
}
```
#### 上下文/依赖注入
使用 `@kevisual/use-config` 进行上下文管理:
```typescript
export const assistantConfig = useContextKey<AssistantInit>('assistantConfig', () => {
return new AssistantInit({
path: configDir,
init: isInit,
});
});
```
#### 路由模式
使用 `@kevisual/router` 的 App 和 SimpleRouter
```typescript
app.route({
path: 'router',
key: 'list',
description: '获取路由列表',
}).define(async (ctx) => {
ctx.body = { list };
}).addTo(app);
```
### JSDoc 文档
为公共 API、复杂函数和类型添加文档
```typescript
/**
* 助手初始化类
* @class AssistantInit
*/
export class AssistantInit extends AssistantConfig {}
/**
* @param {string} p - 要解析的路径
* @returns {string} 解析后的绝对路径
*/
export const w = (p: string) => path.join(__dirname, p);
```
### 文件组织
- **路由**: `src/routes/``src/routes-simple/`
- **模块**: `src/module/`(功能模块)
- **服务**: `src/services/`(公共服务)
- **命令**: `src/command/`CLI 命令)
- **查询**: `src/query/`API/查询逻辑)
- **入口文件**:
- `src/index.ts` - 主 CLI 入口
- `src/server.ts` - 服务器入口
- `src/run.ts` - 开发运行器
- `src/run-server.ts` - 服务器运行器
### 控制台输出
使用 `chalk` 实现彩色输出:
```typescript
console.log(chalk.blue('助手路径不存在,正在创建...'));
console.log(chalk.yellow('助手路径已存在'), chalk.green(assistantConfig.configDir));
console.error(chalk.red('启动服务器错误:'), error.message);
```
### 系统检测
```typescript
const isWindows = process.platform === 'win32';
const bunExecutableName = isWindows ? 'bun.exe' : 'bun';
```
### 端口处理
```typescript
import getPort, { portNumbers } from 'get-port';
const port = await getPort({ port: 51515 });
const availablePort = await getPort({ port: portNumbers(51515, 52000) });
```
### 关键依赖
- `@kevisual/router` - 应用路由
- `@kevisual/query` - API 查询
- `@kevisual/use-config` - 配置/上下文
- `commander` - CLI 参数解析
- `chalk` - 终端颜色
- `ws` - WebSocket通过 `@kevisual/ws`
- `pm2` - 进程管理(生产环境)
### 重要说明
1. 开发时始终使用 `bun`,但构建输出以 Node.js 为目标
2. 打包时外部包必须在 `bun.config.mjs``external` 数组中声明
3. 库构建排除 `pm2`
4.`ENVISION_*` 为前缀的环境变量在构建时可用
5. 项目在业务逻辑中广泛使用中文注释
6. 私有类成员应使用 `#` 前缀语法
7. 操作前始终使用 `checkFileExists` 检查文件是否存在

View File

@@ -6,7 +6,7 @@ import fs from 'node:fs';
// bun run src/index.ts --
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const external = ['pm2', '@kevisual/hot-api', '@nut-tree-fork/nut-js'];
const external = ['pm2', '@kevisual/hot-api', '@nut-tree-fork/nut-js', 'bun'];
/**
*
* @param {string} p
@@ -40,6 +40,28 @@ await Bun.build({
},
external,
});
await Bun.build({
target: 'node',
format: 'esm',
entrypoints: [w('./src/main.ts')],
outdir: w('./dist'),
naming: {
entry: 'assistant-opencode.js',
},
define: {
ENVISION_VERSION: JSON.stringify(pkg.version),
},
external,
});
const dts = 'dts -i src/main.ts -o assistant-opencode.d.ts';
Bun.spawnSync({
cmd: ['sh', '-c', dts],
cwd: __dirname,
stdout: 'inherit',
stderr: 'inherit',
});
// const copyDist = ['dist', 'bin'];
const copyDist = ['dist'];
export const copyFileToEnvision = async () => {

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/assistant-cli",
"version": "0.0.6",
"version": "0.0.8",
"description": "",
"main": "dist/assistant.mjs",
"keywords": [
@@ -10,7 +10,7 @@
],
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
"license": "MIT",
"packageManager": "pnpm@10.26.1",
"packageManager": "pnpm@10.28.1",
"type": "module",
"files": [
"dist",
@@ -25,8 +25,7 @@
"dev:share": "bun --watch src/test/remote-app.ts ",
"build:lib": "bun run bun-lib.config.mjs",
"postbuild:lib": "dts -i src/lib.ts -o assistant-lib.d.ts -d libs -t",
"build": "rimraf dist && bun run bun.config.mjs",
"postbuild": "pnpm run build:lib"
"build": "rimraf dist && bun run bun.config.mjs"
},
"bin": {
"ev-assistant": "bin/assistant.js",
@@ -42,18 +41,19 @@
}
},
"devDependencies": {
"@kevisual/ai": "^0.0.19",
"@kevisual/ai": "^0.0.22",
"@kevisual/api": "^0.0.26",
"@kevisual/load": "^0.0.6",
"@kevisual/local-app-manager": "^0.1.32",
"@kevisual/logger": "^0.0.4",
"@kevisual/query": "0.0.33",
"@kevisual/query": "0.0.38",
"@kevisual/query-login": "0.0.7",
"@kevisual/router": "^0.0.49",
"@kevisual/types": "^0.0.10",
"@kevisual/use-config": "^1.0.21",
"@types/bun": "^1.3.5",
"@types/lodash-es": "^4.17.12",
"@types/node": "^25.0.3",
"@kevisual/router": "^0.0.62",
"@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.28",
"@opencode-ai/plugin": "^1.1.36",
"@types/bun": "^1.3.6",
"@types/node": "^25.0.10",
"@types/send": "^1.2.1",
"@types/ws": "^8.18.1",
"chalk": "^5.6.2",
@@ -62,11 +62,11 @@
"dayjs": "^1.11.19",
"dotenv": "^17.2.3",
"get-port": "^7.1.0",
"inquirer": "^13.1.0",
"lodash-es": "^4.17.22",
"inquirer": "^13.2.1",
"nanoid": "^5.1.6",
"send": "^1.2.1",
"supports-color": "^10.2.2",
"table": "^6.9.0",
"ws": "npm:@kevisual/ws"
},
"engines": {
@@ -76,12 +76,17 @@
"access": "public"
},
"dependencies": {
"@kevisual/ha-api": "^0.0.1",
"@kevisual/video-tools": "^0.0.12",
"eventemitter3": "^5.0.1",
"@aws-sdk/client-s3": "^3.975.0",
"@kevisual/ha-api": "^0.0.6",
"@kevisual/js-filter": "^0.0.5",
"@kevisual/oss": "^0.0.16",
"@kevisual/video-tools": "^0.0.13",
"@opencode-ai/sdk": "^1.1.36",
"es-toolkit": "^1.44.0",
"eventemitter3": "^5.0.4",
"lowdb": "^7.0.1",
"lru-cache": "^11.2.4",
"pm2": "^6.0.14",
"unstorage": "^1.17.3"
"unstorage": "^1.17.4"
}
}

View File

@@ -1,7 +1,19 @@
# assistant cli
## 初始化路径
## 安装
## 启动服务
```bash
npm i -g @kevisual/cli --registry=https://npm.cnb.cool/kevisual/registry/-/packages/
```
## 环境变量配置项
## 配置
- ASSISTANT_CONFIG_DIR
- 说明:指定 assistant 的配置文件目录
- 示例:`export ASSISTANT_CONFIG_DIR=/path/to/your/config/dir`
## 启动命令
```bash
asst server -s -p 8686
```

View File

@@ -1,11 +1,13 @@
import { App } from '@kevisual/router';
import { SimpleRouter } from '@kevisual/router/simple'
// import { App } from '@kevisual/router/src/app.ts';
import { HttpsPem } from '@/module/assistant/https/sign.ts';
// import { AssistantConfig } from '@/module/assistant/index.ts';
import { AssistantInit, parseHomeArg } from '@/services/init/index.ts';
import { configDir as HomeConfigDir } from '@/module/assistant/config/index.ts';
import { useContextKey } from '@kevisual/use-config/context';
import { AssistantQuery } from '@/module/assistant/query/index.ts';
const manualParse = parseHomeArg(HomeConfigDir);
const _configDir = manualParse.configDir;
export const configDir = AssistantInit.detectConfigDir(_configDir);
@@ -14,6 +16,7 @@ export const assistantConfig = useContextKey<AssistantInit>('assistantConfig', (
return new AssistantInit({
path: configDir,
init: isInit,
initWorkspace: manualParse.isOpencode ? false : true,
});
});
@@ -21,8 +24,6 @@ export const assistantQuery = useContextKey('assistantQuery', () => {
return new AssistantQuery(assistantConfig);
});
const httpsPem = new HttpsPem(assistantConfig);
export const runtime = useContextKey('runtime', () => {
return {
type: 'client',
@@ -30,30 +31,10 @@ export const runtime = useContextKey('runtime', () => {
});
export const app: App = useContextKey<App>('app', () => {
const init = isInit;
if (init) {
const config = assistantConfig.getConfig();
if (config?.https?.type !== 'https') {
console.log('http模式', 'http');
return new App({
serverOptions: {
path: '/client/router',
httpType: 'http',
cors: {
origin: '*',
},
io: true
},
});
}
}
return new App({
serverOptions: {
path: '/client/router',
httpType: 'https',
httpsCert: httpsPem.cert,
httpsKey: httpsPem.key,
httpType: 'http',
cors: {
origin: '*',
},
@@ -61,3 +42,19 @@ export const app: App = useContextKey<App>('app', () => {
},
});
});
export const simpleRouter = useContextKey('simpleRouter', () => {
return new SimpleRouter();
});
app.route({
path: 'router',
key: 'list',
description: '获取路由列表',
}).define(async (ctx) => {
const list = ctx.app.getList((item) => {
if (item?.path?.includes('auth') || item?.id?.includes('auth')) return false;
return true;
})
ctx.body = { list }
}).addTo(app);

View File

@@ -1,6 +1,7 @@
import { AssistantApp } from '@/module/assistant/index.ts';
import { program, Command, assistantConfig } from '@/program.ts';
import { AppDownload } from '@/services/app/index.ts';
import { table } from 'table';
const appManagerCommand = new Command('app-manager').alias('am').description('Manage Assistant Apps 管理本地的应用模块');
program.addCommand(appManagerCommand);
@@ -8,11 +9,43 @@ program.addCommand(appManagerCommand);
appManagerCommand
.command('list')
.description('List all installed apps')
.action(async () => {
.option('-s, --status <status>', '列出状态信息, 可选值: running, stopped, inactive')
.option('-w, --wide', '显示更多信息')
.action(async (opts) => {
const manager = new AssistantApp(assistantConfig);
await manager.loadConfig();
const showInfos = manager.getAllAppShowInfo();
console.log('Installed Apps:', showInfos);
let showInfos = manager.getAllAppShowInfo();
const isWide = opts.wide ?? false;
let header = [];
if (!isWide) {
showInfos = showInfos.map((item) => {
return { key: item.key, status: item.status };
});
header = ['Key', 'Status'];
}
if (opts.status) {
const showList = showInfos.filter(info => info.status === opts.status);
if (showList.length === 0) {
console.log(`No apps with status: ${opts.status}`);
return;
}
const teables = showList.map(item => Object.values(item));
teables.unshift(header);
console.log('App Start Info:\n')
console.log(table(teables));
return
}
if (showInfos.length === 0) {
console.log('No installed apps found.');
return;
}
header = Object.keys(showInfos[0]);;
const teables = showInfos.map(item => Object.values(item));
teables.unshift(header);
console.log('Installed Apps:\n')
console.log(table(teables));
});
appManagerCommand

View File

@@ -6,19 +6,19 @@ import inquirer from 'inquirer';
import chalk from 'chalk';
type InitCommandOptions = {
path?: string;
workspace?: string;
};
const Init = new Command('init')
.description('初始化一个助手客户端,生成配置文件。')
.option('-p --path <path>', '助手路径,默认为执行命令的目录,如果助手路径不存在则创建。')
.option('-w --workspace <workspace>', '助手路径,默认为执行命令的目录,如果助手路径不存在则创建。')
.action((opts: InitCommandOptions) => {
// 如果path参数存在检测path是否是相对路径,如果是相对路径,则转换为绝对路径
if (opts.path && !opts.path.startsWith('/')) {
opts.path = path.join(process.cwd(), opts.path);
} else if (opts.path) {
opts.path = path.resolve(opts.path);
// 如果workspace参数存在检测workspace是否是相对路径,如果是相对路径,则转换为绝对路径
if (opts.workspace && !opts.workspace.startsWith('/')) {
opts.workspace = path.join(process.cwd(), opts.workspace);
} else if (opts.workspace) {
opts.workspace = path.resolve(opts.workspace);
}
const configDir = AssistantInit.detectConfigDir(opts.path);
const configDir = AssistantInit.detectConfigDir(opts.workspace);
console.log('configDir', configDir);
const assistantInit = new AssistantInit({
path: configDir,

View File

@@ -1,34 +1,97 @@
import { program, Command, assistantConfig } from '@/program.ts';
import { spawnSync } from 'node:child_process';
const runScriptsCommand = new Command('run-scripts')
.alias('run')
.arguments('<cmd> [env]')
.description('运行脚本在assistant.config.json中配置的脚本')
.action(async (cmd, env) => {
assistantConfig.checkMounted();
const configs = assistantConfig.getCacheAssistantConfig();
const scripts = configs?.scripts || {};
try {
const script = scripts[cmd];
if (!script) {
console.error(`Script "${cmd}" not found.`);
return;
import path from 'node:path';
import fs from 'node:fs';
import { AssistantApp, checkFileExists } from '@/lib.ts';
import { logger } from '@/module/logger.ts';
import { LoadApp, StopApp } from '@/module/local-apps/src/modules/manager.ts';
const createRandomApp = (opts: { app: any, package: any, pwd: string, status?: string }) => {
const { app, package: packageJson, pwd } = opts;
if (!app.status) {
app.status = opts.status || 'running'
}
if (!app.key) {
const randomSuffix = Math.random().toString(36).substring(2, 8);
app.key = packageJson.basename || `${'unknown-app'}-${randomSuffix}`;
}
app.path = pwd;
if (app.type === 'pm2-system-app' && !app.pm2Options) {
app.pm2Options = {
cwd: pwd,
}
}
return app;
}
const start = new Command('start')
.description('获取package.json中app参数并启动对应的app')
.option('-s --save', '保存应用信息到assistant配置中', false)
.action(async (opts) => {
// assistantConfig.checkMounted();
const pwd = process.cwd();
const packageJsonPath = path.join(pwd, 'package.json');
if (checkFileExists(packageJsonPath) === false) {
logger.error('package.json 在当前目录未找到,请在包含 package.json 的目录下运行此命令。');
return
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const appKey = packageJson.app;
if (!appKey) {
logger.error('package.json 中未找到 app 字段,请确保在 package.json 中正确配置 app 字段。');
return
}
const app = createRandomApp({ app: packageJson.app, package: packageJson, pwd });
if (app.type !== 'system-app') {
const load = await LoadApp(app, {});
if (!load) {
logger.error(`未能加载应用, 请确保应用名称正确且已安装。`, app.type);
return
}
// console.log(`Running script "${script}"...`);
const command = [script, ...(env ? [env] : [])].join(' ');
// console.log(`Command: ${command}`, env);
const res = spawnSync(command, { shell: true, stdio: 'inherit', cwd: assistantConfig.configDir });
if (res.error) {
console.error(`Error running script "${cmd}":`, res.error);
return;
}
if (res.status !== 0) {
console.error(`Script "${cmd}" exited with code ${res.status}`);
return;
}
console.log(`Script "${cmd}" run successfully.`);
} catch (error) {
console.error(`Failed to run script "${cmd}":`, error);
} else {
LoadApp(app, {}).then(() => {
logger.info(`系统应用已启动: ${app.key}`);
}).catch((err) => {
logger.error(`启动系统应用失败: ${app.key}`, err);
});
}
if (opts.save) {
assistantConfig.checkMounted();
const manager = new AssistantApp(assistantConfig, app);
await manager.loadConfig();
await manager.saveAppInfo(app);
}
});
program.addCommand(runScriptsCommand);
program.addCommand(start);
const stop = new Command('stop')
.description('获取package.json中app参数并停止对应的app')
.option('-t --todo <todo>', '停止应用,在pm2中如果为stop则停止,如果为remove则删除默认为stop', 'stop')
.option('-s --save', '保存应用信息到assistant配置中', false)
.action(async (opts) => {
// assistantConfig.checkMounted();
const pwd = process.cwd();
const packageJsonPath = path.join(pwd, 'package.json');
if (checkFileExists(packageJsonPath) === false) {
logger.error('package.json 在当前目录未找到,请在包含 package.json 的目录下运行此命令。');
return
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
const appKey = packageJson.app;
if (!appKey) {
logger.error('package.json 中未找到 app 字段,请确保在 package.json 中正确配置 app 字段。');
return
}
const app = createRandomApp({ app: packageJson.app, package: packageJson, pwd, status: 'stopped' });
await StopApp(app, { todo: opts.todo });
if (opts.save) {
assistantConfig.checkMounted();
const manager = new AssistantApp(assistantConfig, app);
await manager.loadConfig();
await manager.removeApp(app.key, { deleteFile: false });
}
});
program.addCommand(stop);

9
assistant/src/main.ts Normal file
View File

@@ -0,0 +1,9 @@
import { app } from './app.ts';
import { type Plugin } from "@opencode-ai/plugin"
import { createRouterAgentPluginFn } from '@kevisual/router/opencode';
import './routes/index.ts';
import './routes-simple/index.ts';
export const AgentPlugin: Plugin = createRouterAgentPluginFn({
router: app,
})

View File

@@ -4,12 +4,13 @@ import fs from 'fs';
import { checkFileExists, createDir } from '../file/index.ts';
import { ProxyInfo } from '../proxy/proxy.ts';
import dotenv from 'dotenv';
import { logger } from '@/module/logger.ts';
let kevisualDir = path.join(homedir(), 'kevisual');
const envKevisualDir = process.env.ASSISTANT_CONFIG_DIR
if (envKevisualDir) {
kevisualDir = envKevisualDir;
console.log('使用环境变量 ASSISTANT_CONFIG_DIR 作为 kevisual 目录:', kevisualDir);
logger.debug('使用环境变量 ASSISTANT_CONFIG_DIR 作为 kevisual 目录:', kevisualDir);
}
/**
* 助手配置文件路径, 全局配置文件目录
@@ -93,6 +94,17 @@ export type AssistantConfigData = {
* 例子: { path: '/root/home', target: 'https://kevisual.cn', pathname: '/root/home' }
*/
proxy?: ProxyInfo[];
/**
* Router代理, 会自动获取 {path: 'router', key: 'list'}的路由信息,然后注入到整个router应用当中.
* 例子: { proxy: [ { type: 'router', api: 'https://localhost:50002/api/router' } ] }
* base: 是否使用 /api/router的基础路径默认false
* lightcode: 是否启用lightcode路由默认false
*/
router?: {
proxy: ProxyInfo[];
base?: boolean;
lightcode?: boolean;
}
/**
* API 代理配置, 比如api开头的v1开头的等等
*/
@@ -130,7 +142,7 @@ export type AssistantConfigData = {
*/
home?: string;
/**
* 启用本地AI代理
* 启用AI代理
*/
ai?: {
enabled?: boolean;
@@ -138,28 +150,34 @@ export type AssistantConfigData = {
apiKey?: string;
model?: string;
};
/**
* 自定义脚本, asst 启动时会执行这些脚本
*/
scripts?: {
[key: string]: string;
};
/** 阿里云的语音识别服务,模型 qwen3-asr-flash-realtime */
asr?: {
enabled?: boolean;
token?: string;
}
/**
* 认证和权限配置
* share: protected 需要认证代理访问(默认) public 公开访问, private 私有访问
* share 是对外共享 pages 目录下的页面
*/
auth?: AuthPermission;
/**
* HTTPS 证书配置, 启用后,助手服务会启用 HTTPS 服务, 默认 HTTP
* 理论上也不需要https因为可以通过反向代理实现https
*/
https?: {
type?: 'https' | 'http';
keyPath?: string; // 证书私钥路径
certPath?: string; // 证书路径
};
storage?: AssistantStorage[];
};
type AssistantStorage = FileStorage | S3Storage;
type FileStorage = {
id: string;
path: string;
type: 'local'
}
type S3Storage = {
id: string;
bucket: string;
region: string;
accessKeyId: string;
secretAccessKey: string;
endpoint?: string;
type: 's3' | 'minio';
}
let assistantConfig: AssistantConfigData;
type AssistantConfigOptions = {
configDir?: string;
@@ -185,10 +203,11 @@ export class AssistantConfig {
}
return this.#configPath;
}
init() {
this.configPath = initConfig(this.configDir);
init(configDir?: string) {
this.configPath = initConfig(configDir || this.configDir);
this.isMountedConfig = true;
}
checkMounted() {
if (!this.isMountedConfig) {
this.init();
@@ -350,7 +369,7 @@ type AppConfig = {
list: any[];
};
export function parseArgs(args: string[]) {
const result: { root?: string; home?: boolean; help?: boolean } = {};
const result: { root?: string; home?: boolean; help?: boolean } = { home: true };
for (let i = 0; i < args.length; i++) {
const arg = args[i];
// 处理 root 参数
@@ -360,14 +379,13 @@ export function parseArgs(args: string[]) {
i++; // 跳过下一个参数,因为它是值
}
}
// 处理 home 参数
// if (arg === '--home') {
result.home = true;
// }
if (arg === '--help' || arg === '-h') {
result.help = true;
}
}
if (result.root) {
result.home = false;
}
return result;
}
/**
@@ -377,6 +395,7 @@ export function parseArgs(args: string[]) {
*/
export const parseHomeArg = (homedir?: string) => {
const args = process.argv.slice(2);
const execPath = process.execPath;
const options = parseArgs(args);
let _configDir = undefined;
if (options.home && homedir) {
@@ -384,7 +403,10 @@ export const parseHomeArg = (homedir?: string) => {
} else if (options.root) {
_configDir = options.root;
}
const checkUrl = ['.opencode', 'bin/opencode', 'opencode.exe']
const isOpencode = checkUrl.some((item) => execPath.includes(item))
return {
isOpencode,
options,
configDir: _configDir,
};

View File

@@ -35,7 +35,8 @@ export const checkFileDir = (filePath: string, create = true) => {
return exist;
};
export const createDir = (dirPath: string) => {
export const createDir = (dirPath: string, isCreate = true) => {
if (!isCreate) return dirPath;
if (!checkFileExists(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}

View File

@@ -0,0 +1,52 @@
export const renderNoAuthAndLogin = (text: string) => {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Required</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
text-align: center;
}
h1 {
color: #333;
}
p {
color: #666;
}
a {
display: inline-block;
margin-top: 15px;
padding: 10px 15px;
background-color: #007BFF;
color: white;
text-decoration: none;
border-radius: 5px;
}
a:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<p>${text}</p>
<a href="/root/home">转到首页</a>
</div>
</body>
</html>`;
}

View File

@@ -1,180 +0,0 @@
import { createCert } from '@kevisual/router/sign';
import path from 'node:path';
import fs from 'node:fs';
import { AssistantConfig } from '../config/index.ts';
import { checkFileExists } from '../file/index.ts';
import { chalk } from '@/module/chalk.ts';
import dayjs from 'dayjs';
type Attributes = {
name: string;
value: string;
};
type AltNames = {
type: number;
value?: string;
ip?: string;
};
export class HttpsPem {
assistantConfig: AssistantConfig;
key: string;
cert: string;
isHttps = false;
constructor(assistantConfig: AssistantConfig) {
this.assistantConfig = assistantConfig;
this.#initKeyCert();
}
#initKeyCert() {
this.assistantConfig.checkMounted();
const config = this.assistantConfig.getConfig();
if (config.https) {
const httpsType = config.https?.type || 'https';
if (httpsType !== 'https') {
// console.log(chalk.yellow('当前配置文件 https.type 不是 https, 不使用证书'));
return;
}
this.isHttps = true;
if (config.https.keyPath) {
const keyPath = config.https.keyPath;
const certPath = config.https.certPath;
if (checkFileExists(keyPath) && checkFileExists(certPath)) {
this.key = fs.readFileSync(keyPath, 'utf-8');
this.cert = fs.readFileSync(certPath, 'utf-8');
console.log(chalk.green('使用配置文件 https.keyPath 和 https.certPath 的证书'), keyPath, certPath);
return;
} else {
console.log(chalk.red('证书路径不存在,请检查配置文件 https.keyPath 和 https.certPath 是否正确'));
}
}
}
if(!this.isHttps) return;
const { key, cert } = this.getCert();
this.key = key;
this.cert = cert;
}
getPemDir() {
const configDir = this.assistantConfig.configPath?.configDir || process.cwd();
const pemDir = path.join(configDir, 'pem');
if (!checkFileExists(pemDir)) {
fs.mkdirSync(pemDir, { recursive: true });
}
return pemDir;
}
getCert() {
if (!this.assistantConfig.init) return;
const pemDir = this.getPemDir();
const pemPath = {
key: path.join(pemDir, 'https-private-key.pem'),
cert: path.join(pemDir, 'https-cert.pem'),
config: path.join(pemDir, 'https-config.json'),
};
const writeCreate = (opts: { key: string; cert: string; data: { createTime: number; expireTime: number } }) => {
fs.writeFileSync(pemPath.key, opts.key);
fs.writeFileSync(pemPath.cert, opts.cert);
fs.writeFileSync(pemPath.config, JSON.stringify(opts.data, null, 2));
};
if (!checkFileExists(pemPath.key) || !checkFileExists(pemPath.cert)) {
const { key, cert, data } = this.createCert();
writeCreate({ key, cert, data });
console.log(chalk.green('证书创建成功,浏览器需要导入当前证书'));
return {
key,
cert,
};
}
if (!checkFileExists(pemPath.config)) {
const data = this.createExpireData();
fs.writeFileSync(pemPath.config, JSON.stringify(data, null, 2));
}
const key = fs.readFileSync(pemPath.key, 'utf-8');
const cert = fs.readFileSync(pemPath.cert, 'utf-8');
const config = fs.readFileSync(pemPath.config, 'utf-8');
let expireTime = 0;
try {
const data = JSON.parse(config);
expireTime = data.expireTime;
if (typeof expireTime !== 'number') {
throw new Error('expireTime is not a number');
}
} catch (error) {
console.log(chalk.red('证书配置文件损坏,重新生成证书'));
}
const now = new Date().getTime();
if (now > expireTime) {
this.removeCert();
const { key, cert, data } = this.createCert();
writeCreate({ key, cert, data });
console.log(chalk.green('证书更新成功, 浏览器需要重新导入当前证书'));
return {
key,
cert,
};
}
return {
key,
cert,
};
}
createExpireData() {
const expireTime = new Date().getTime() + 365 * 24 * 60 * 60 * 1000;
const expireDate = dayjs(expireTime).format('YYYY-MM-DD HH:mm:ss');
return {
description: '手动导入证书到浏览器, https-cert.pem文件, 具体使用教程访问 https://kevisual.cn/root/pem-docs/',
createTime: new Date().getTime(),
expireDate,
expireTime,
};
}
/*
* 重新生成证书
*/
removeCert() {
const pemDir = this.getPemDir();
const pemPath = {
key: path.join(pemDir, 'https-private-key.pem'),
cert: path.join(pemDir, 'https-cert.pem'),
};
const oldPath = {
key: path.join(pemDir, 'https-private-key.pem.bak'),
cert: path.join(pemDir, 'https-cert.pem.bak'),
};
if (checkFileExists(pemPath.key)) {
fs.renameSync(pemPath.key, oldPath.key);
}
if (checkFileExists(pemPath.cert)) {
fs.renameSync(pemPath.cert, oldPath.cert);
}
}
/*
* 创建证书
* @param attrs 证书属性
* @param altNames 证书备用名称
*/
createCert(attrs?: Attributes[], altNames?: AltNames[]) {
const attributes = attrs || [];
const altNamesList = altNames || [];
const { key, cert } = createCert(
[
{
name: 'commonName',
value: 'localhost',
},
{
name: 'organizationName',
value: 'kevisual',
},
...attributes,
],
altNamesList,
);
const data = this.createExpireData();
return {
key,
cert,
data: {
...data,
},
};
}
}

View File

@@ -7,12 +7,17 @@ import glob from 'fast-glob';
import type { App } from '@kevisual/router';
import { RemoteApp } from '@/module/remote-app/remote-app.ts';
import { logger } from '@/module/logger.ts';
import { getEnvToken } from '@/module/http-token.ts';
import { initApi } from '@kevisual/api/proxy'
import { Query } from '@kevisual/query';
import { initLightCode } from '@/module/light-code/index.ts';
export class AssistantApp extends Manager {
config: AssistantConfig;
pagesPath: string;
remoteIsConnected = false;
attemptedConnectTimes = 0;
remoteApp: RemoteApp | null = null;
remoteUrl: string | null = null;
constructor(config: AssistantConfig, mainApp?: App) {
config.checkMounted();
const appsPath = config?.configPath?.appsDir || path.join(process.cwd(), 'apps');
@@ -71,11 +76,17 @@ export class AssistantApp extends Manager {
return pagesParse;
}
async initRemoteApp() {
async initRemoteApp(opts?: { token?: string, enabled?: boolean }) {
const config = this.config.getConfig();
const share = config?.share;
if (share && share.enabled !== false) {
const token = config?.token;
const enabled = opts?.enabled ?? share?.enabled ?? false;
if (share && enabled !== false) {
if (this.remoteApp) {
this.remoteApp.ws?.close();
this.remoteApp = null;
this.remoteIsConnected = false;
}
const token = config?.token || opts?.token || getEnvToken() as string;
const url = new URL(share.url || 'https://kevisual.cn/ws/proxy');
const id = config?.app?.id;
if (token && url && id) {
@@ -89,7 +100,9 @@ export class AssistantApp extends Manager {
if (isConnect) {
remoteApp.listenProxy();
this.remoteIsConnected = true;
remoteApp.emitter.once('close', () => {
// 清理已有的 close 事件监听器,防止多重绑定
remoteApp.emitter.removeAllListeners('close');
remoteApp.emitter.on('close', () => {
setTimeout(() => {
if (remoteApp.isError) {
console.error('远程应用发生错误,不重连');
@@ -99,12 +112,79 @@ export class AssistantApp extends Manager {
}, 5 * 1000); // 第一次断开5秒后重连
});
logger.debug('链接到了远程应用服务器');
const appId = id;
const username = config?.auth.username || 'unknown';
const url = new URL(`/${username}/v1/${appId}`, config?.registry || 'https://kevisual.cn/');
this.remoteUrl = url.toString();
console.log('远程地址', this.remoteUrl);
} else {
console.log('Not connected to remote app server');
}
this.remoteApp = remoteApp;
} else {
//
if (!token) {
logger.error('Token是远程应用连接必须的参数');
}
}
}
}
async initRouterApp() {
const config = this.config.getConfig();
const routerProxy = config?.router?.proxy || [];
const base = config.router?.base ?? false;
const lightcode = config.router?.lightcode ?? true;
if (base) {
routerProxy.push({
type: 'router',
router: {
url: `${this.config.getRegistry()}/api/router`,
}
})
}
if (lightcode) {
routerProxy.push({
type: 'lightcode',
lightcode: {
check: true,
}
})
}
if (routerProxy.length === 0) {
return
}
for (const proxyInfo of routerProxy) {
if (proxyInfo.type !== 'router' && proxyInfo.type !== 'lightcode') {
console.warn('路由的type必须是"router", 或者lightcode');
continue;
}
if (proxyInfo.type === 'lightcode') {
initLightCode({
router: this.mainApp,
config: this.config
});
continue;
}
const url = proxyInfo.router!.url;
if (!url) {
console.warn('路由的api地址不能为空', proxyInfo.router);
continue;
}
const query = new Query({ url });
try {
initApi({
router: this.mainApp,
item: {
type: 'api',
api: {
url,
query: query as any,
}
},
exclude: "WHERE path = 'auth' OR path = 'router' OR path = 'call'",
})
console.log('Router API 已初始化', url.toString());
} catch (err) {
console.error('Router API 初始化失败', url.toString(), err);
}
}
}
@@ -112,6 +192,10 @@ export class AssistantApp extends Manager {
console.log('重新连接到远程应用服务器...', this.attemptedConnectTimes);
const remoteApp = this.remoteApp;;
if (remoteApp) {
// 先关闭旧的 WebSocket防止竞态条件
if (remoteApp.ws) {
remoteApp.ws.close();
}
remoteApp.init();
this.attemptedConnectTimes += 1;
const isConnect = await remoteApp.isConnect();
@@ -120,9 +204,10 @@ export class AssistantApp extends Manager {
this.attemptedConnectTimes = 0;
console.log('重新连接到了远程应用服务器');
} else {
this.reconnectRemoteApp();
setTimeout(() => {
this.reconnectRemoteApp();
}, 30 * 1000); // 30秒后重连
this.initRouterApp()
}, 30 * 1000 + this.attemptedConnectTimes * 10 * 1000); // 30秒后重连 + 每次增加10秒
}
}
}

View File

@@ -5,49 +5,39 @@ import path from 'node:path';
import { ProxyInfo } from './proxy.ts';
import { checkFileExists } from '../file/index.ts';
import { log } from '@/module/logger.ts';
import { pipeFileStream } from './pipe.ts';
import { getContentType } from './module/mime.ts';
export const fileProxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
// url开头的文件
export const fileProxy2 = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
res.statusCode = 501;
const url = new URL(req.url, 'http://localhost');
const [user, key, _info] = url.pathname.split('/');
const pathname = url.pathname.slice(1);
const { indexPath = '', target = '', rootPath = process.cwd() } = proxyApi;
if (!indexPath) {
return res.end('Not Found indexPath');
}
try {
// 检测文件是否存在如果文件不存在则返回404
let filePath = '';
let exist = false;
if (_info) {
filePath = path.join(rootPath, target, pathname);
exist = checkFileExists(filePath, true);
}
if (!exist) {
filePath = path.join(rootPath, target, indexPath);
exist = checkFileExists(filePath, true);
}
log.debug('filePath', { filePath, exist });
if (!exist) {
res.statusCode = 404;
res.end('Not Found File');
return;
}
const ext = path.extname(filePath);
let maxAge = 24 * 60 * 60 * 1000; // 24小时
if (ext === '.html') {
maxAge = 0;
}
let sendFilePath = path.relative(rootPath, filePath);
const file = send(req, sendFilePath, {
root: rootPath,
maxAge,
});
file.pipe(res);
} catch (error) {
res.statusCode = 404;
res.end('Error:Not Found File');
const { rootPath, indexPath = '' } = proxyApi?.file || {}
if (!rootPath) {
res.end(`系统未配置根路径 rootPath id:[${proxyApi?.file?.id}]`);
return;
}
};
const pathname = url.pathname;
let targetFilepath = pathname.replace(proxyApi.path || '', '');
if (targetFilepath.endsWith('/')) {
// 没有指定文件访问index.html
targetFilepath += 'index.html';
}
const filePath = path.join(rootPath || process.cwd(), targetFilepath);
const indexTargetPath = path.join(rootPath || process.cwd(), indexPath);
let sendPath = filePath;
if (!checkFileExists(filePath)) {
res.setHeader('X-Proxy-File', 'false');
if (indexPath && checkFileExists(indexTargetPath)) {
sendPath = indexTargetPath;
} else {
res.statusCode = 404;
res.end(`文件不存在, 路径: ${filePath}`);
return;
}
} else {
res.setHeader('X-Proxy-File', 'true');
}
const contentType = getContentType(sendPath);
res.setHeader('Content-Type', contentType);
pipeFileStream(sendPath, res);
};

View File

@@ -23,7 +23,7 @@ export const defaultApiProxy = [
* @param paths ['/api/router', '/v1' ]
* @returns
*/
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1']) => {
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1']): ProxyInfo[] => {
const pathList = paths.map((item) => {
return {
path: item,
@@ -116,4 +116,6 @@ export const httpProxy = (req: http.IncomingMessage, res: http.ServerResponse, p
// 处理 POST 请求的请求体(传递数据到目标服务器),end:true 表示当请求体结束时,关闭请求
// req.pipe(proxyReq, { end: true });
pipeProxyReq(req, proxyReq, res);
}
}

View File

@@ -0,0 +1,50 @@
import path from 'path';
// 获取文件的 content-type
export const getContentType = (filePath: string) => {
const extname = path.extname(filePath);
const contentType = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.mjs': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.md': 'text/markdown; charset=utf-8', // utf-8配置
'.ico': 'image/x-icon', // Favicon 图标
'.webp': 'image/webp', // WebP 图像格式
'.webm': 'video/webm', // WebM 视频格式
'.ogg': 'audio/ogg', // Ogg 音频格式
'.mp3': 'audio/mpeg', // MP3 音频格式
'.m4a': 'audio/mp4', // M4A 音频格式
'.m3u8': 'application/vnd.apple.mpegurl', // HLS 播放列表
'.ts': 'video/mp2t', // MPEG Transport Stream
'.pdf': 'application/pdf', // PDF 文档
'.doc': 'application/msword', // Word 文档
'.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', // Word 文档 (新版)
'.ppt': 'application/vnd.ms-powerpoint', // PowerPoint 演示文稿
'.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', // PowerPoint (新版)
'.xls': 'application/vnd.ms-excel', // Excel 表格
'.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // Excel 表格 (新版)
'.csv': 'text/csv; charset=utf-8', // CSV 文件
'.xml': 'application/xml; charset=utf-8', // XML 文件
'.rtf': 'application/rtf', // RTF 文本文件
'.eot': 'application/vnd.ms-fontobject', // Embedded OpenType 字体
'.ttf': 'font/ttf', // TrueType 字体
'.woff': 'font/woff', // Web Open Font Format 1.0
'.woff2': 'font/woff2', // Web Open Font Format 2.0
'.otf': 'font/otf', // OpenType 字体
'.wasm': 'application/wasm', // WebAssembly 文件
'.pem': 'application/x-pem-file', // PEM 证书文件
'.crt': 'application/x-x509-ca-cert', // CRT 证书文件
'.yaml': 'application/x-yaml; charset=utf-8', // YAML 文件
'.yml': 'application/x-yaml; charset=utf-8', // YAML 文件(别名)
'.zip': 'application/octet-stream',
};
return contentType[extname] || 'application/octet-stream';
};

View File

@@ -1,7 +1,8 @@
import * as http from 'http';
import * as fs from 'fs';
import { isBun } from './utils.ts';
import { Readable } from 'stream';
import { logger } from '@/module/logger.ts';
/**
* 文件流管道传输函数
* 将指定文件的内容通过流的方式传输给客户端响应
@@ -25,7 +26,7 @@ export const pipeFileStream = (filePath: string, res: http.ServerResponse) => {
* @param readStream 可读流对象
* @param res HTTP服务器响应对象
*/
export const pipeStream = (readStream: fs.ReadStream, res: http.ServerResponse) => {
export const pipeStream = (readStream: fs.ReadStream | Readable, res: http.ServerResponse) => {
if (isBun) {
// Bun环境下的流处理方式
res.pipe(readStream as any);
@@ -99,7 +100,7 @@ export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.Cli
proxyReq.end();
return;
}
console.log('Bun pipeProxyReq content-type', contentType);
logger.debug('Bun pipeProxyReq content-type', contentType);
// @ts-ignore
const bodyString = req.body;
bodyString && proxyReq.write(bodyString);
@@ -111,4 +112,17 @@ export const pipeProxyReq = async (req: http.IncomingMessage, proxyReq: http.Cli
// Node.js标准环境下直接使用pipe进行流传输
req.pipe(proxyReq, { end: true });
}
}
export const pipeBusboy = async (req: http.IncomingMessage, res: http.ServerResponse, busboy: any) => {
if (isBun) {
// @ts-ignore
const bunRequest = req.bun.request;
const arrayBuffer = await bunRequest.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
busboy.end(buffer);
} else {
req.pipe(busboy);
}
}

View File

@@ -1,3 +1,7 @@
import http from 'node:http';
import { httpProxy } from './http-proxy.ts';
import { s3Proxy } from './s3.ts';
import { fileProxy2 } from './file-proxy.ts';
export type ProxyInfo = {
/**
* 代理路径, 比如/root/home, 匹配的路径
@@ -10,7 +14,7 @@ export type ProxyInfo = {
/**
* 类型
*/
type?: 'file' | 'dynamic' | 'minio' | 'http';
type?: 'file' | 'dynamic' | 'minio' | 'http' | 's3' | 'router' | 'lightcode';
/**
* 目标的 pathname 默认为请求的url.pathname, 设置了pathname则会使用pathname作为请求的url.pathname
* @default undefined
@@ -22,41 +26,44 @@ export type ProxyInfo = {
* @default false
*/
ws?: boolean;
/**
* type为file时有效
* 索引文件比如index.html type为fileProxy代理有用 设置了索引文件,如果文件不存在,则访问索引文件
*/
indexPath?: string;
/**
* type为file时有效
* 根路径, 默认是process.cwd(), type为fileProxy代理有用必须为绝对路径
*/
rootPath?: string;
s3?: {
/**
* 如何id存在使用assistan-config的storage配置
*/
id?: string;
bucket?: string;
region?: string;
accessKeyId?: string;
secretAccessKey?: string;
endpoint?: string;
},
file?: {
id?: string;
indexPath?: string;
rootPath?: string;
},
router?: {
id?: string;
url?: string;
},
lightcode?: {
id?: string;
/**
* 是否检测远程服务更新
*/
check?: boolean;
}
};
export type ApiList = {
path: string;
/**
* url或者相对路径
*/
target: string;
/**
* 目标地址
*/
ws?: boolean;
/**
* 类型
*/
type?: 'static' | 'dynamic' | 'minio';
}[];
/**
[
{
path: '/api/v1/user',
target: 'http://localhost:3000/api/v1/user',
type: 'dynamic',
},
]
*/
export const proxy = (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
if (proxyApi.type === 'http' || !proxyApi.type) {
return httpProxy(req, res, proxyApi);
}
console.log('proxyApi', proxyApi);
if (proxyApi.type === 's3') {
return s3Proxy(req, res, proxyApi);
}
if (proxyApi.type === 'file') {
return fileProxy2(req, res, proxyApi);
}
}

View File

@@ -0,0 +1,79 @@
import { S3Client } from '@aws-sdk/client-s3';
import { ProxyInfo } from './proxy.ts';
import http from 'http';
import { OssBase } from '@kevisual/oss/s3.ts';
import { pipeStream } from './pipe.ts';
import { Readable } from 'stream';
const mapS3 = new Map<string, S3Client>();
export const s3Proxy = async (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
const s3 = proxyApi.s3;
if (!s3) {
res.statusCode = 500;
res.end('S3 config not found');
return;
}
let findClient = mapS3.get(s3.accessKeyId);
let s3Client: S3Client;
if (findClient) {
s3Client = findClient;
} else {
s3Client = new S3Client({
credentials: {
accessKeyId: s3.accessKeyId,
secretAccessKey: s3.secretAccessKey,
},
region: s3.region,
endpoint: s3.endpoint,
});
mapS3.set(s3.accessKeyId, s3Client);
}
const oss = new OssBase({
client: s3Client!,
bucketName: s3.bucket,
});
const requestUrl = new URL(req.url, 'http://localhost');
const proxyPath = proxyApi.path || '';
let objectPath = requestUrl.pathname.replace(proxyPath + '/', '');
if (objectPath.startsWith(s3.bucket + '/')) {
objectPath = objectPath.replace(s3.bucket + '/', '');
}
if (objectPath.endsWith('/')) {
// 获取目录列表
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
const list = await oss.listObjects(objectPath);
res.end(JSON.stringify(list, null, 2));
return;
}
oss.getObject(objectPath).then((response) => {
if (!response.Body) {
res.statusCode = 404;
res.end('Object not found');
return;
}
// 设置响应头
if (response.ContentType) {
res.setHeader('Content-Type', response.ContentType);
}
if (response.ContentLength) {
res.setHeader('Content-Length', response.ContentLength);
}
if (response.LastModified) {
res.setHeader('Last-Modified', response.LastModified.toUTCString());
}
if (response.ETag) {
res.setHeader('ETag', response.ETag);
}
// response.Body 已经是 Readable 流,直接 pipe 到 res
// (response.Body as Readable).pipe(res);
pipeStream(response.Body as Readable, res);
}).catch((err) => {
console.error('S3 getObject error:', err);
res.statusCode = 500;
res.end(`S3 error: ${err.message}`);
});
return;
}

View File

@@ -0,0 +1,16 @@
import crypto from 'node:crypto';
import fs from 'node:fs';
export const getHash = (file: string) => {
if (!fs.existsSync(file)) return '';
const buffer = fs.readFileSync(file); // 不指定编码,返回 Buffer
return crypto.createHash('md5').update(buffer).digest('hex');
};
export const getBufferHash = (buffer: Buffer) => {
return crypto.createHash('md5').update(buffer).digest('hex');
};
export const getStringHash = (str: string) => {
return crypto.createHash('md5').update(str).digest('hex');
}

View File

@@ -0,0 +1,22 @@
import http from 'http';
import * as cookie from '@kevisual/router/src/server/cookie.ts';
export const getTokenFromRequest = (req: http.IncomingMessage) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost');
if (!token) {
token = url.searchParams.get('token') || '';
}
if (!token) {
const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (token) {
token = token.replace('Bearer ', '');
}
return token;
}
export const getTokenFromContext = (ctx: any) => {
return ctx.query.token;
}

View File

@@ -1,3 +1,4 @@
import { useKey } from '@kevisual/use-config';
import http from 'node:http';
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
@@ -32,3 +33,7 @@ export const getToken = async (req: http.IncomingMessage) => {
return { token };
};
export const getEnvToken = () => {
const envTokne = useKey('KEVISUAL_TOKEN') || '';
return envTokne;
}

View File

@@ -0,0 +1,177 @@
import { App, QueryRouterServer } from '@kevisual/router';
import { AssistantInit } from '../../services/init/index.ts';
import path from 'node:path';
import fs, { write } from 'node:fs';
import os from 'node:os';
import glob from 'fast-glob';
import { runCode } from './run.ts';
const codeDemoId = '0e700dc8-90dd-41b7-91dd-336ea51de3d2'
import { filter } from "@kevisual/js-filter";
import { getHash, getStringHash } from '../file-hash.ts';
import { AssistantConfig } from '@/lib.ts';
const codeDemo = `// 这是一个示例代码文件
import {App} from '@kevisual/router';
const app = new App();
app.route({
path: 'hello',
description: 'LightCode 示例路由',
metadata: {
tags: ['light-code', 'example'],
},
}).define(async (ctx) => {
console.log('tokenUser:', ctx.query?.tokenUser);
ctx.body = 'Hello from LightCode!';
}).addTo(app);
app.wait();
`;
const writeCodeDemo = async (appDir: string) => {
const lightcodeDir = path.join(appDir, 'light-code', 'code');
const demoPath = path.join(lightcodeDir, `${codeDemoId}.ts`);
fs.writeFileSync(demoPath, codeDemo, 'utf-8');
}
// writeCodeDemo(path.join(os.homedir(), 'kevisual', 'assistant-app', 'apps'));
type opts = {
router: QueryRouterServer | App
config: AssistantConfig | AssistantInit
sync?: boolean
}
type LightCodeFile = {
id?: string, code?: string, hash?: string, filepath: string
}
export const initLightCode = async (opts: opts) => {
// 注册 light-code 路由
console.log('初始化 light-code 路由');
const config = opts.config as AssistantInit;
const app = opts.router;
const token = config.getConfig()?.token || '';
const query = config.query;
const sync = opts.sync ?? true;
if (!config || !app) {
console.error('initLightCode 缺少必要参数, config 或 app');
return;
}
const appDir = config.configPath.appsDir;
const lightcodeDir = path.join(appDir, 'light-code', 'code');
if (!fs.existsSync(lightcodeDir)) {
fs.mkdirSync(lightcodeDir, { recursive: true });
}
let diffList: LightCodeFile[] = [];
const codeFiles = glob.sync(['**/*.ts', '**/*.js'], {
cwd: lightcodeDir,
onlyFiles: true,
}).map(file => {
return {
filepath: path.join(lightcodeDir, file),
// hash: getHash(path.join(lightcodeDir, file))
}
});
if (sync) {
const queryRes = await query.post({
path: 'light-code',
key: 'list',
token,
});
if (queryRes.code === 200) {
const lightQueryList = queryRes.data?.list || [];
for (const item of lightQueryList) {
const codeHash = getStringHash(item.code || '');
diffList.push({ id: item.id!, code: item.code || '', hash: codeHash, filepath: path.join(lightcodeDir, `${item.id}.ts`) });
}
const codeFileSet = new Set(codeFiles.map(f => f.filepath));
// 需要新增的文件 (在 diffList 中但不在 codeFiles 中)
const toAdd = diffList.filter(d => !codeFileSet.has(d.filepath));
// 需要删除的文件 (在 codeFiles 中但不在 diffList 中)
const toDelete = codeFiles.filter(f => !diffList.some(d => d.filepath === f.filepath));
// 需要更新的文件 (两边都有但 hash 不同)
const toUpdate = diffList.filter(d => codeFileSet.has(d.filepath) && d.hash !== getHash(d.filepath));
const unchanged = diffList.filter(d => codeFileSet.has(d.filepath) && d.hash === getHash(d.filepath));
// 执行新增
for (const item of toAdd) {
fs.writeFileSync(item.filepath, item.code, 'utf-8');
// console.log(`新增 light-code 文件: ${item.filepath}`);
}
// 执行删除
for (const filepath of toDelete) {
fs.unlinkSync(filepath.filepath);
// console.log(`删除 light-code 文件: ${filepath.filepath}`);
}
// 执行更新
for (const item of toUpdate) {
fs.writeFileSync(item.filepath, item.code, 'utf-8');
// console.log(`更新 light-code 文件: ${item.filepath}`);
}
// 记录未更新的文件
// const lightCodeList = [...toAdd, ...unchanged].map(d => ({
// filepath: d.filepath,
// hash: d.hash
// }));
} else {
console.error('light-code 同步失败', queryRes.message);
diffList = codeFiles;
}
} else {
diffList = codeFiles;
}
for (const file of diffList) {
const tsPath = file.filepath;
const runRes = await runCode(tsPath, { path: 'router', key: 'list' }, { timeout: 10000 });
if (runRes.success) {
const res = runRes.data;
if (res.code === 200) {
const list = res.data?.list || [];
for (const routerItem of list) {
if (routerItem.path?.includes('auth') || routerItem.path?.includes('router') || routerItem.path?.includes('call')) {
continue;
}
// console.log(`注册 light-code 路由: [${routerItem.path}] ${routerItem.id} 来自文件: ${file.filepath}`);
const metadata = routerItem.metadata || {};
if (metadata.tags && Array.isArray(metadata.tags)) {
metadata.tags.push('light-code');
} else {
metadata.tags = ['light-code'];
}
app.route({
id: routerItem.id,
path: `${routerItem.id}__${routerItem.path}`,
key: routerItem.key,
description: routerItem.description || '',
metadata,
middleware: ['auth'],
}).define(async (ctx) => {
const tokenUser = ctx.state?.tokenUser || {};
const query = { ...ctx.query, tokenUser }
const runRes2 = await runCode(tsPath, query, { timeout: 30000 });
if (runRes2.success) {
const res2 = runRes2.data;
if (res2.code === 200) {
ctx.body = res2.data;
} else {
ctx.throw(res2.code, res2.message || 'Lightcode 路由执行失败');
}
} else {
ctx.throw(runRes2.error || 'Lightcode 路由执行失败');
}
}).addTo(app);
}
}
} else {
console.error('light-code 路由执行失败', runRes.error);
}
}
console.log(`light-code 路由注册成功`, `注册${diffList.length}个路由`);
}

View File

@@ -32,7 +32,7 @@ type RunCode = {
};
error?: any
timestamp?: string
[key: string]: any
output?: string
}
export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?: RunCodeOptions): Promise<RunCode> => {
return new Promise((resolve, reject) => {
@@ -43,7 +43,7 @@ export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?:
})
return
}
let output = ''
const timeoutMs = opts?.timeout || 30000; // 默认30秒超时
let child
@@ -70,6 +70,7 @@ export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?:
if (!resolved) {
resolved = true
cleanup()
result.output = output
resolve(result)
}
}
@@ -77,7 +78,11 @@ export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?:
try {
// 使用 Bun 的 fork 模式启动子进程
child = fork(tsPath, [], {
silent: true // 启用 stdio 重定向
silent: true, // 启用 stdio 重定向
env: {
...process.env,
BUN_CHILD_PROCESS: 'true' // 标记为子进程
}
})
// 监听来自子进程的消息
child.on('message', (msg: RunCode) => {
@@ -94,6 +99,11 @@ export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?:
})
})
}
if (child.stdout) {
child.stdout.on('data', (data) => {
output += data.toString()
})
}
// 监听子进程退出事件
child.on('exit', (code, signal) => {

View File

@@ -40,7 +40,3 @@ if (DEV_SERVER) {
});
loadManager({ configFilename: 'b.json' });
}
export const loadApp = (mainApp: App, appInfo?: any) => {
//
};

View File

@@ -1,7 +1,7 @@
import type { App } from '@kevisual/router';
import { loadAppInfo, AppInfoConfig, saveAppInfo, getAppsPath } from './app-file.ts';
import { fork } from 'node:child_process';
import { merge } from 'lodash-es';
import { merge } from 'es-toolkit';
import { deleteFileAppInfo } from './app-file.ts';
import { fileIsExist } from '@kevisual/use-config/env';
import path from 'node:path';
@@ -53,6 +53,7 @@ export type AppInfo = {
path?: string; // 文件路径
env?: Record<string, any>; // 环境变量
engine?: string; // runtime, python node deno bun etc
init?: boolean; // 是否需要初始化安装npm等依赖
/**
* pm2 选项, 仅仅当是AppType.Pm2SystemApp的时候生效
* pm2 选项可以参考 https://pm2.keymetrics.io/docs/usage/application-declaration/
@@ -125,71 +126,7 @@ export class Manager<T extends AppInfo = AppInfo> {
async loadApp(app: T) {
const mainApp = this.mainApp;
this.apps.set(app.key, app);
if (app.status !== 'running') {
return;
}
if (!fileIsExist(app.path)) {
console.error('app is not found');
return;
}
const pathEntry = path.join(app.path, app.entry);
if (!fileIsExist(pathEntry)) {
console.error('file entry not found');
return;
}
const entry = app.entry + `?timestamp=${app?.timestamp}`;
// 注册路由
if (app.type === AppType.MicroApp) {
const childProcess = fork(app.entry, [], {
stdio: 'inherit', // 共享主进程的标准输入输出
cwd: app.path,
env: {
...process.env,
...app.env,
APP_KEY: app.key,
APP_PATH: app.path,
APP_ENTRY: entry
}
});
app.process = childProcess;
} else if (app.type === AppType.SystemApp) {
const pathEntryAndTimestamp = path.join(app.path, entry);
// Windows下需要使用file://协议,并将反斜杠转换为正斜杠
const importPath = process.platform === 'win32'
? 'file:///' + pathEntryAndTimestamp.replace(/\\/g, '/')
: pathEntryAndTimestamp;
const module = await import(importPath);
if (module.loadApp && mainApp) {
await module.loadApp?.(mainApp, app);
}
} else if (app.type === AppType.GatewayApp) {
console.log('gateway app not support');
} else if (app.type === AppType.Pm2SystemApp) {
const pathEntry = path.join(app.path, app.entry);
const pm2Manager = new Pm2Manager({
appName: app.key,
script: pathEntry,
pm2Connect: this.#pm2Connect
});
// const isInstall = await checkInstall(app);
// if (!isInstall) {
// console.log('install failed');
// return;
// }
const pm2Options: StartOptions = app.pm2Options || {};
if (app?.engine) {
pm2Options.interpreter = pm2Options.interpreter || app?.engine;
}
if (!pm2Options.cwd) {
pm2Options.cwd = path.join(app.path, '../..');
}
await pm2Manager.start(pm2Options);
} else {
console.error('app type not support', app.type);
}
console.log(`load ${app.type} success`, app.key);
return true;
return await LoadApp(app, { mainApp, pm2Connect: this.#pm2Connect });
}
/**
* create new app info
@@ -308,24 +245,7 @@ export class Manager<T extends AppInfo = AppInfo> {
if (!app) {
return;
}
if (app.status === 'stop' && app.type === AppType.SystemApp) {
console.log(`app ${key} is stopped`);
return;
}
app.status = 'stop';
if (app.type === AppType.MicroApp) {
if (app.process) {
app.process.kill();
}
}
if (app.type === AppType.Pm2SystemApp) {
const pm2Manager = new Pm2Manager({
appName: app.key,
script: app.entry,
pm2Connect: this.#pm2Connect
});
await pm2Manager.stop();
}
await StopApp(app, { pm2Connect: this.#pm2Connect, todo: 'stop' });
await this.saveAppInfo(app);
}
async restart(key: string) {
@@ -393,8 +313,9 @@ export class Manager<T extends AppInfo = AppInfo> {
* @param key
* @returns
*/
async removeApp(key: string) {
async removeApp(key: string, opts?: { deleteFile?: boolean }) {
const app = this.apps.get(key);
const deleteFile = opts?.deleteFile ?? true;
if (!app) {
return false;
}
@@ -414,6 +335,9 @@ export class Manager<T extends AppInfo = AppInfo> {
} catch (e) {
console.log('delete pm2 process error', e);
}
if (!deleteFile) {
return true;
}
try {
deleteFileAppInfo(key, this.appsPath);
} catch (e) {
@@ -485,7 +409,103 @@ export class Manager<T extends AppInfo = AppInfo> {
}
}
}
export const LoadApp = async (app: AppInfo, opts?: { mainApp?: any, pm2Connect?: any }) => {
const mainApp = opts?.mainApp;
const pm2Connect = opts?.pm2Connect;
if (app.status !== 'running') {
return false;
}
if (!fileIsExist(app.path)) {
console.error('app is not found');
return false;
}
const pathEntry = path.join(app.path, app.entry);
if (!fileIsExist(pathEntry)) {
console.error('file entry not found');
return false;
}
const entry = app.entry + `?timestamp=${app?.timestamp}`;
// 注册路由
if (app.type === AppType.MicroApp) {
const childProcess = fork(app.entry, [], {
stdio: 'inherit', // 共享主进程的标准输入输出
cwd: app.path,
env: {
...process.env,
...app.env,
APP_KEY: app.key,
APP_PATH: app.path,
APP_ENTRY: entry
}
});
app.process = childProcess;
} else if (app.type === AppType.SystemApp) {
const pathEntryAndTimestamp = path.join(app.path, entry);
// Windows下需要使用file://协议,并将反斜杠转换为正斜杠
const importPath = process.platform === 'win32'
? 'file:///' + pathEntryAndTimestamp.replace(/\\/g, '/')
: pathEntryAndTimestamp;
const module = await import(importPath);
if (module.loadApp && mainApp) {
await module.loadApp?.(mainApp, app);
}
} else if (app.type === AppType.GatewayApp) {
console.log('gateway app not support');
} else if (app.type === AppType.Pm2SystemApp) {
const pathEntry = path.join(app.path, app.entry);
const pm2Manager = new Pm2Manager({
appName: app.key,
script: pathEntry,
pm2Connect: pm2Connect
});
if (app?.init) {
const isInstall = await checkInstall(app);
if (!isInstall) {
console.log('install failed');
return false;
}
}
const pm2Options: StartOptions = app.pm2Options || {};
if (app?.engine) {
pm2Options.interpreter = pm2Options.interpreter || app?.engine;
}
if (!pm2Options.cwd) {
pm2Options.cwd = path.join(app.path, '../..');
}
await pm2Manager.start(pm2Options);
} else if (app.type === AppType.ScriptApp) {
// console.log('script app 直接运行,不需要启动');
return true;
} else {
console.error('app type not support', app.type);
}
console.log(`load ${app.type} success`, app.key);
return true;
}
export const StopApp = async (app: AppInfo, opts?: { pm2Connect?: Pm2Connect, todo?: 'stop' | 'remove' | 'restart' }) => {
const key = app.key;
const pm2Connect = opts?.pm2Connect;
const todo = opts?.todo || 'stop';
if (app.status === 'stop' && app.type === AppType.SystemApp) {
console.log(`app ${key} is stopped`);
return;
}
app.status = 'stop';
if (app.type === AppType.MicroApp) {
if (app.process) {
app.process.kill();
}
}
if (app.type === AppType.Pm2SystemApp) {
const pm2Manager = new Pm2Manager({
appName: app.key,
script: app.entry,
pm2Connect: pm2Connect
});
await pm2Manager[todo]?.();
}
}
/**
* 安装app通过key
* @param key

View File

@@ -108,6 +108,9 @@ export class Pm2Connect {
}
}
type RunOptions = {
/**
* 是否在操作完成后退出连接
*/
needExit?: boolean;
};
export class Pm2Manager {
@@ -226,6 +229,9 @@ export class Pm2Manager {
this.pm2Connect.checkDisconnect(runOpts);
}
}
async remove(runOpts?: RunOptions) {
this.deleteProcess(runOpts);
}
async deleteProcess(runOpts?: RunOptions) {
try {
await this.pm2Connect.checkConnect();

View File

@@ -17,8 +17,11 @@ type ProxyType = {
user: string;
key: string;
path: string;
indexPath: string;
absolutePath?: string;
type?: 'file';
file: {
indexPath: string;
absolutePath: string;
};
};
export type LocalProxyOpts = {
watch?: boolean; // 是否监听文件变化
@@ -79,8 +82,10 @@ export class LocalProxy {
user: user,
key: app,
path: `/${user}/${app}/`,
indexPath: `${user}/${app}/index.html`,
absolutePath: appPath,
file: {
indexPath: `${user}/${app}/index.html`,
absolutePath: appPath,
}
});
}
});

View File

@@ -64,6 +64,10 @@ export class RemoteApp {
throw new Error('No id provided for remote app');
}
this.isError = false;
// 关闭已有连接
if (this.ws) {
this.ws.close();
}
const ws = new WebSocket(this.getWsURL(this.url));
const that = this;
ws.onopen = function () {

View File

@@ -0,0 +1,34 @@
import type { AssistantConfig } from '@/module/assistant/index.ts';
import fs from 'node:fs';
import path from 'node:path';
export class UploadManager {
config: AssistantConfig;
constructor(config: AssistantConfig) {
this.config = config;
}
mvFile(opts: {
temppath: string;
type: 'file' | 's3' | 'app-file';
targetPath: string;
}) {
const { type = 'file', temppath, targetPath } = opts;
const pageDir = this.config.configPath?.pagesDir!;
const appsDir = this.config.configPath?.appsDir!;
let dir = type === 'app-file' ? appsDir : pageDir;
if (type === 'file' || type === 'app-file') {
const fullTargetPath = targetPath.startsWith('/')
? targetPath
: path.join(dir, targetPath);
const targetDir = fullTargetPath.substring(0, fullTargetPath.lastIndexOf('/'));
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
fs.renameSync(temppath, fullTargetPath);
return fullTargetPath;
}
}
}
export const uploadManager = (config: AssistantConfig) => {
return new UploadManager(config);
}

View File

@@ -0,0 +1,32 @@
import { simpleRouter, clients, getTaskId, writeEvents, deleteOldClients, error } from './router.ts';
simpleRouter.get('/client/events', async (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
const taskId = getTaskId(req);
if (!taskId) {
res.end(error('task-id is required'));
return;
}
// 将客户端连接推送到 clients 数组
clients.set(taskId, { client: res, createTime: Date.now() });
writeEvents(req, { progress: 0, message: 'start' })
// 移除客户端连接
req.on('close', () => {
clients.delete(taskId);
});
});
simpleRouter.get('/client/events/close', async (req, res) => {
const taskId = getTaskId(req);
if (!taskId) {
res.end(error('task-id is required'));
return;
}
deleteOldClients();
clients.delete(taskId);
res.end('ok');
});

View File

@@ -0,0 +1 @@
export * from './upload.ts'

View File

@@ -0,0 +1,95 @@
import { simpleRouter } from '@/app.ts';
import http from 'http';
import { useContextKey } from '@kevisual/context';
export { simpleRouter };
import os from 'node:os';
import path from 'node:path';
import fs from 'node:fs';
/**
* 缓存文件路径
*/
const defaultCachePath = path.join(os.homedir(), '.envision', 'cache-file');
if (!fs.existsSync(defaultCachePath)) {
fs.mkdirSync(defaultCachePath, { recursive: true });
}
export const cacheFilePath = defaultCachePath;
/**
* 事件客户端
*/
const eventClientsInit = () => {
const clients = new Map<string, { client?: http.ServerResponse; createTime?: number;[key: string]: any }>();
return clients;
};
export const clients = useContextKey('event-clients', () => eventClientsInit());
/**
* 获取 task-id
* @param req
* @returns
*/
export const getTaskId = (req: http.IncomingMessage) => {
const url = new URL(req.url || '', 'http://localhost');
const taskId = url.searchParams.get('taskId');
if (taskId) {
return taskId;
}
return req.headers['task-id'] as string;
};
type EventData = {
progress: number | string;
message: string;
};
/**
* 写入事件
* @param req
* @param data
*/
export const writeEvents = (req: http.IncomingMessage, data: EventData) => {
const taskId = getTaskId(req);
if (taskId) {
const client = clients.get(taskId)?.client;
if (client) {
client.write(`data: ${JSON.stringify(data)}\n\n`);
}
if (Number(data.progress) === 100) {
clients.delete(taskId);
}
} else {
console.log('taskId is remove.', taskId);
}
};
/**
* 查找超出2个小时的clients都删除了
*/
export const deleteOldClients = () => {
const now = Date.now();
for (const [taskId, client] of clients) {
// 如果创建时间超过2个小时则删除
if (now - client.createTime > 1000 * 60 * 60 * 2) {
clients.delete(taskId);
}
}
};
/**
* 解析表单数据, 如果表单数据是数组, 则取第一个appKey, version, username 等
* @param fields 表单数据
* @param parseKeys 需要解析的键
* @returns 解析后的数据
*/
export const getKey = (fields: Record<string, any>, parseKeys: string[]) => {
let value: Record<string, any> = {};
for (const key of parseKeys) {
const v = fields[key];
if (Array.isArray(v)) {
value[key] = v[0];
} else {
value[key] = v;
}
}
return value;
};
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};

View File

@@ -0,0 +1,179 @@
import Busboy from 'busboy';
import { assistantConfig, simpleRouter } from '../app.ts'
import http from 'http';
import path from 'path';
import fs from 'fs';
import { checkAuth } from '@/routes/index.ts';
import { getTokenFromRequest } from '@/module/get-header-token.ts';
import { pipeBusboy } from '@/module/assistant/proxy/pipe.ts';
import { logger } from '@/module/logger.ts';
import { cacheFilePath, getKey, writeEvents } from './router.ts';
import { getContentType } from '@kevisual/oss/services';
import { validateDirectory } from './utils.ts';
import { UploadManager } from '@/module/upload/mv.ts';
simpleRouter.get('/client/upload', async (req, res) => {
if (res.headersSent) return; // 如果响应已发送,不再处理
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ message: 'Upload endpoint reached' }));
})
export const error = (msg: string, code = 500) => {
return JSON.stringify({ code, message: msg });
};
export const parseIfJson = (data = '{}') => {
try {
const _data = JSON.parse(data);
if (typeof _data === 'object') return _data;
return {};
} catch (error) {
return {};
}
};
const uploadManager = new UploadManager(assistantConfig)
export const uploadResources = async (req: http.IncomingMessage, res: http.ServerResponse) => {
// const { tokenUser, token } = await checkAuth(req, res);
const token = getTokenFromRequest(req);
let tokenUser: any = null;
const authResult = await checkAuth({ query: { token } });
if (authResult.code === 200) {
tokenUser = authResult.data?.tokenUser;
} else {
res.end(error(authResult.message, authResult.code));
return;
}
if (!tokenUser) {
res.end(error('Token is invalid.'));
return;
}
const url = new URL(req.url || '', 'http://localhost');
const share = !!url.searchParams.get('public');
const meta = parseIfJson(url.searchParams.get('meta'));
// 使用 busboy 解析 multipart/form-data
const busboy = Busboy({ headers: req.headers, preservePath: true });
const fields: any = {};
const files: any[] = [];
const filePromises: Promise<void>[] = [];
let bytesReceived = 0;
let bytesExpected = parseInt(req.headers['content-length'] || '0');
busboy.on('field', (fieldname, value) => {
fields[fieldname] = value;
});
busboy.on('file', (fieldname, fileStream, info) => {
const { filename, encoding, mimeType } = info;
const tempPath = path.join(cacheFilePath, `${Date.now()}-${Math.random().toString(36).substring(7)}`);
const writeStream = fs.createWriteStream(tempPath);
const filePromise = new Promise<void>((resolve, reject) => {
fileStream.on('data', (chunk) => {
bytesReceived += chunk.length;
if (bytesExpected > 0) {
const progress = (bytesReceived / bytesExpected) * 100;
const data = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
console.log('progress-upload', JSON.stringify(data, null, 2));
writeEvents(req, data);
}
});
fileStream.pipe(writeStream);
writeStream.on('finish', () => {
files.push({
filepath: tempPath,
originalFilename: filename,
mimetype: mimeType,
});
resolve();
});
writeStream.on('error', (err) => {
reject(err);
});
});
filePromises.push(filePromise);
});
busboy.on('finish', async () => {
// 等待所有文件写入完成
try {
await Promise.all(filePromises);
} catch (err) {
logger.error(`File write error: ${err.message}`);
res.end(error(`File write error: ${err.message}`));
return;
}
const clearFiles = () => {
files.forEach((file) => {
if (file?.filepath && fs.existsSync(file.filepath)) {
fs.unlinkSync(file.filepath);
}
});
};
// 检查是否有文件上传
if (files.length === 0) {
res.end(error('files is required'));
return;
}
console.log('fields', fields);
let { appKey, version, username, directory } = getKey(fields, ['appKey', 'version', 'username', 'directory']);
if (!appKey || !version) {
res.end(error('appKey or version is not found, please check the upload config.'));
clearFiles();
return;
}
const { code, message } = validateDirectory(directory);
if (code !== 200) {
res.end(error(message));
clearFiles();
return;
}
// 逐个处理每个上传的文件
const uploadedFiles = files;
const uploadResults = [];
for (let i = 0; i < uploadedFiles.length; i++) {
const file = uploadedFiles[i];
// @ts-ignore
const tempPath = file.filepath; // 文件上传时的临时路径
const relativePath = file.originalFilename; // 保留表单中上传的文件名 (包含文件夹结构)
// 比如 child2/b.txt
const showVersion = false;
const _version = showVersion ? `${version ? '/' + version : ''}` : '';
const _directory = directory ? `/${directory}` : '';
const minioPath = `${username || tokenUser.username || 'unknown'}/${appKey}${_version}${_directory}/${relativePath}`;
uploadResults.push({
name: relativePath,
path: minioPath,
});
const type = 'file';
uploadManager.mvFile({
type: 'file',
temppath: tempPath,
targetPath: minioPath,
})
if (type !== 'file') {
fs.unlinkSync(tempPath); // 删除临时文件
}
}
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
code: 200,
data: {
detect: [],
upload: uploadResults,
},
}),
);
});
pipeBusboy(req, res, busboy);
}
simpleRouter.post('/client/upload', uploadResources);

View File

@@ -0,0 +1,30 @@
/**
* 校验directory是否合法, 合法返回200, 不合法返回500
*
* directory 不能以/开头,不能以/结尾。不能以.开头,不能以.结尾。
* 把directory的/替换掉后,只能包含数字、字母、下划线、中划线
* @param directory 目录
* @returns
*/
export const validateDirectory = (directory?: string) => {
// 对directory进行校验不能以/开头,不能以/结尾。不能以.开头,不能以.结尾。
if (directory && (directory.startsWith('/') || directory.endsWith('/') || directory.startsWith('..') || directory.endsWith('..'))) {
return {
code: 500,
message: 'directory is invalid',
};
}
// 把directory的/替换掉后,只能包含数字、字母、下划线、中划线
// 可以包含.
let _directory = directory?.replace(/\//g, '');
if (_directory && !/^[a-zA-Z0-9_.-]+$/.test(_directory)) {
return {
code: 500,
message: 'directory is invalid, only number, letter, underline and hyphen are allowed',
};
}
return {
code: 200,
message: 'directory is valid',
};
};

View File

@@ -0,0 +1,32 @@
import { app } from '../../app.ts';
import { createSkill, tool } from '@kevisual/router';
app.route({
path: 'call',
key: '',
description: '调用',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'call-app',
title: '调用app应用',
summary: '调用router的应用, 参数path, key, payload',
args: {
path: tool.schema.string().describe('应用路径,例如 cnb'),
key: tool.schema.string().optional().describe('应用key例如 list-repos'),
payload: tool.schema.object({}).optional().describe('调用参数'),
}
})
},
}).define(async (ctx) => {
const { path, key = '' } = ctx.query;
if (!path) {
ctx.throw('路径path不能为空');
}
const res = await ctx.run({ path, key, payload: ctx.query.payload || {} }, {
...ctx
});
ctx.forward(res);
}).addTo(app)

View File

@@ -1,2 +1,37 @@
import { LightHA } from "@kevisual/ha-api";
export const lightHA = new LightHA({ token: process.env.HAAS_TOKEN || '', homeassistantURL: process.env.HAAS_URL });
export const callText = async (text: string) => {
const command = text?.trim().slice(0, 20);
type ParseCommand = {
type?: '打开' | '关闭',
appName?: string,
command?: string,
}
let obj: ParseCommand = {};
if (command.startsWith('打开')) {
obj.appName = command.replace('打开', '').trim();
obj.type = '打开';
} else if (command.startsWith('关闭')) {
obj.appName = command.replace('关闭', '').trim();
obj.type = '关闭';
}
let endTime = Date.now();
if (obj.type) {
try {
const search = await lightHA.searchLight(obj.appName || '');
console.log('searchTime', Date.now() - endTime);
if (search.id) {
await lightHA.runService({ entity_id: search.id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
} else if (search.hasMore) {
const [first] = search.result;
await lightHA.runService({ entity_id: first.entity_id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
} else {
console.log('未找到对应设备:', obj.appName);
}
console.log('解析到控制指令', obj);
} catch (e) {
console.error('控制失败', e);
}
}
}

View File

@@ -5,12 +5,17 @@ import './ai/index.ts';
// TODO:
// import './light-code/index.ts';
import './user/index.ts';
import './call/index.ts'
// TODO: 移除
import './hot-api/key-sender/index.ts';
// import './hot-api/key-sender/index.ts';
import './opencode/index.ts';
import './remote/index.ts';
import os from 'node:os';
import { authCache } from '@/module/cache/auth.ts';
import { createSkill } from '@kevisual/router';
import { logger } from '@/module/logger.ts';
const getTokenUser = async (token: string) => {
const query = assistantConfig.query
const res = await query.post({
@@ -34,30 +39,38 @@ export const getTokenUserCache = async (token: string) => {
}
return res;
}
const checkAuth = async (ctx: any, isAdmin = false) => {
export const checkAuth = async (ctx: any, isAdmin = false) => {
const config = assistantConfig.getConfig();
const { auth = {} } = config;
const token = ctx.query.token;
console.log('checkAuth', ctx.query, { token });
logger.debug('checkAuth', ctx.query, { token });
if (!token) {
return ctx.throw(401, 'not login');
return {
code: 401,
message: '未登录',
}
}
// 鉴权代理
let tokenUser = await authCache.get(token);
if (!tokenUser) {
const tokenUserRes = await getTokenUser(token);
if (tokenUserRes.code !== 200) {
return ctx.throw(tokenUserRes.code, 'not login');
return {
code: tokenUserRes.code,
message: '验证失败' + tokenUserRes.message,
}
} else {
tokenUser = tokenUserRes.data;
}
authCache.set(token, tokenUser);
}
ctx.state = {
...ctx.state,
token,
tokenUser,
};
if (ctx.state) {
ctx.state = {
...ctx.state,
token,
tokenUser,
};
}
const { username } = tokenUser;
if (!auth.username) {
// 初始管理员账号
@@ -75,9 +88,16 @@ const checkAuth = async (ctx: any, isAdmin = false) => {
isCheckAdmin = true;
}
if (!isCheckAdmin) {
return ctx.throw(403, 'not admin user');
return {
code: 403,
message: '非管理员用户',
}
}
}
return {
code: 200,
data: { tokenUser, token }
}
};
app
.route({
@@ -86,7 +106,13 @@ app
description: '获取当前登录用户信息, 第一个登录的用户为管理员用户',
})
.define(async (ctx) => {
await checkAuth(ctx);
if (!ctx.query?.token && ctx.appId === app.appId) {
return;
}
const authResult = await checkAuth(ctx);
if (authResult.code !== 200) {
ctx.throw(authResult.code, authResult.message);
}
})
.addTo(app);
app
@@ -96,8 +122,14 @@ app
description: '管理员鉴权, 获取用户信息,并验证是否为管理员。',
})
.define(async (ctx) => {
console.log('query', ctx.query);
await checkAuth(ctx, true);
logger.debug('query', ctx.query);
if (!ctx.query?.token && ctx.appId === app.appId) {
return;
}
const authResult = await checkAuth(ctx, true);
if (authResult.code !== 200) {
ctx.throw(authResult.code, authResult.message);
}
})
.addTo(app);
@@ -131,6 +163,14 @@ app
path: 'client',
key: 'system',
description: '获取系统信息',
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'view-system-info',
title: '查看系统信息',
summary: '获取服务器操作系统平台、架构和版本信息',
})
}
})
.define(async (ctx) => {
const { platform, arch, release } = os;

View File

@@ -4,7 +4,8 @@ import { runCode } from '../../module/light-code/run.ts'
// http://localhost:4005/api/router?path=call
app.route({
path: 'call',
path: 'light-code',
key: 'call',
// middleware: ['auth']
}).define(async (ctx) => {
const filename = ctx.query?.filename || 'root/light-code-demo/demo-router.ts'

View File

@@ -0,0 +1 @@
import './ls.ts'

View File

@@ -0,0 +1,127 @@
import { app } from '@/app.ts'
import { createSkill, tool } from "@kevisual/router";
import { opencodeManager } from './module/open.ts'
import path from "node:path";
import { execSync } from "node:child_process";
import { useKey } from '@kevisual/use-config';
// 创建一个opencode 客户端
app.route({
path: 'opencode',
key: 'create',
middleware: ['auth'],
description: '创建 OpenCode 客户端',
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'create-opencode-client',
title: '创建 OpenCode 客户端',
summary: '创建 OpenCode 客户端,如果存在则复用',
args: {
}
})
},
}).define(async (ctx) => {
const client = await opencodeManager.getClient();
ctx.body = { content: `${opencodeManager.url} OpenCode 客户端已就绪` };
}).addTo(app);
// 关闭 opencode 客户端
app.route({
path: 'opencode',
key: 'close',
middleware: ['auth'],
description: '关闭 OpenCode 客户端',
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'close-opencode-client',
title: '关闭 OpenCode 客户端',
summary: '关闭 OpenCode 客户端',
args: {
}
})
},
}).define(async (ctx) => {
await opencodeManager.close();
ctx.body = { content: 'OpenCode 客户端已关闭' };
}).addTo(app);
// 调用 path: opencode key: getUrl
app.route({
path: 'opencode',
key: 'getUrl',
middleware: ['auth'],
description: '获取 OpenCode 服务 URL',
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'get-opencode-url',
title: '获取 OpenCode 服务 URL',
summary: '获取当前 OpenCode 服务的 URL 地址',
args: {
}
})
},
}).define(async (ctx) => {
const url = await opencodeManager.getUrl();
const cnbURL = useKey('CNB_VSCODE_PROXY_URI') as string | undefined;
let content = `本地访问地址: ${url}`
if (cnbURL) {
content += `\n云端访问地址: ${cnbURL.replace('{{port}}', '5000')}`;
}
ctx.body = { content };
}).addTo(app);
// 调用 path: opencode key: ls-projects
app.route({
path: 'opencode',
key: 'ls-projects'
}).define(async (ctx) => {
const client = await opencodeManager.getClient();
const projects = await client.project.list();
const currentProject = await client.project.current();
ctx.body = {
projects,
currentProject
};
}).addTo(app);
// 调用 path: opencode key: runProject 参数 /home/ubuntu/cli/assistant
app.route({
path: 'opencode',
key: 'runProject',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'run-opencode-project',
title: '运行 OpenCode 项目',
summary: '运行一个已有的 OpenCode 项目',
args: {
projectPath: tool.schema.string().optional().describe('OpenCode 项目的路径, 默认为 /workspace')
}
})
}
}).define(async (ctx) => {
const { projectPath = '/workspace' } = ctx.query;
try {
// const directory = path.resolve(projectPath);
// const runOpencodeCli = 'opencode run hello';
// execSync(runOpencodeCli, { cwd: directory, stdio: 'inherit' });
// ctx.body = { content: `OpenCode 项目已在路径 ${directory} 运行` };
const client = await opencodeManager.getClient();
const session = await client.session.create({
query: {
directory: projectPath
}
})
console.log('Created session:', session.data.id);
ctx.body = { content: `OpenCode 项目已在路径 ${projectPath} 运行` };
} catch (error) {
ctx.body = { content: `运行 OpenCode 项目失败, 请手动运行命令初始化: opencode run hello` };
}
}).addTo(app);

View File

@@ -0,0 +1,127 @@
import { createOpencode, createOpencodeClient, OpencodeClient, } from "@opencode-ai/sdk";
import { randomInt } from "es-toolkit";
import getPort from "get-port";
import os from "node:os";
import path from "node:path";
import fs from "node:fs";
import { execSync } from "node:child_process";
export class OpencodeManager {
private static instance: OpencodeManager | null = null;
private client: OpencodeClient | null = null;
private server: { url: string; close(): void } | null = null;
private isInitializing: boolean = false;
private currentPort: number | null = null;
public url: string = '';
private constructor() { }
static getInstance(): OpencodeManager {
if (!OpencodeManager.instance) {
OpencodeManager.instance = new OpencodeManager();
}
return OpencodeManager.instance;
}
async getClient(): Promise<OpencodeClient> {
// 如果已经有 client直接返回
if (this.client) {
return this.client;
}
// 如果正在初始化,等待初始化完成
if (this.isInitializing) {
await new Promise(resolve => setTimeout(resolve, 100));
return this.getClient();
}
// 开始初始化
this.isInitializing = true;
try {
const port = 5000;
const currentPort = await getPort({ port: port });
if (port === currentPort) {
const result = await createOpencode({
hostname: '0.0.0.0',
port: port
});
this.url = result.server.url;
this.client = result.client;
this.server = result.server;
return this.client;
} else {
this.client = await this.createOpencodeProject({ port });
this.url = `http://localhost:${port}`;
return this.client;
}
} finally {
this.isInitializing = false;
}
}
async createOpencodeProject({
directory,
port = 5000
}: { directory?: string, port?: number }): Promise<OpencodeClient> {
const client = createOpencodeClient({
baseUrl: `http://localhost:${port}`,
directory
});
return client;
}
async killPort(port: number): Promise<void> {
try {
// 尝试 使用命令行去关闭 port为5000的服务
if (os.platform() === 'win32') {
// Windows 平台
execSync(`netstat -ano | findstr :${port} | findstr LISTENING`).toString().split('\n').forEach(line => {
const parts = line.trim().split(/\s+/);
const pid = parts[parts.length - 1];
if (pid) {
execSync(`taskkill /PID ${pid} /F`);
console.log(`OpencodeManager: 已关闭占用端口 ${port} 的进程 PID ${pid}`);
}
});
} else {
// Unix-like 平台
const result = execSync(`lsof -i :${port} -t`).toString();
result.split('\n').forEach(pid => {
if (pid) {
execSync(`kill -9 ${pid}`);
console.log(`OpencodeManager: 已关闭占用端口 ${port} 的进程 PID ${pid}`);
}
});
}
} catch (error) {
console.error('Failed to close OpenCode server:', error);
}
}
async close(): Promise<void> {
if (this.server) {
this.server.close();
this.server = null;
return
}
const port = 5000;
const currentPort = await getPort({ port: port });
if (port === currentPort) {
this.client = null;
return;
} else {
await this.killPort(port);
}
this.client = null;
}
async getUrl(): Promise<string> {
if (this.url) {
return this.url;
}
if (!this.url) {
await this.getClient();
}
return 'http://localhost:5000';
}
}
export const opencodeManager = OpencodeManager.getInstance();

View File

@@ -0,0 +1,54 @@
import { useContextKey } from "@kevisual/context";
import { app } from "../../app.ts";
import { AssistantApp } from "@/lib.ts";
app.route({
path: 'remote',
key: 'status',
middleware: ['admin-auth'],
description: '获取远程app连接状态',
}).define(async (ctx) => {
const manager = useContextKey('manager') as AssistantApp;
if (manager?.remoteApp?.isConnect()) {
const url = manager.remoteUrl || ''
ctx.body = {
content: `远程app已经链接, 访问地址:${url}`,
}
} else {
ctx.body = {
content: '远程app未连接',
}
}
}).addTo(app);
app.route({
path: 'remote',
key: 'connect',
middleware: ['admin-auth'],
description: '连接远程app',
}).define(async (ctx) => {
const manager = useContextKey('manager') as AssistantApp;
if (!manager) {
ctx.body = {
content: '远程app管理器未初始化',
}
return;
}
if (manager?.remoteApp?.isConnect()) {
const url = manager.remoteUrl || ''
ctx.body = {
content: `远程app已经链接, 访问地址:${url}`,
}
return;
}
await manager.initRemoteApp({ enabled: true, token: ctx.query?.token }).then(() => {
ctx.body = {
content: '远程app连接成功',
}
}).catch((err) => {
ctx.body = {
content: `远程app连接失败: ${err.message}`,
}
});
}).addTo(app);

View File

@@ -47,7 +47,7 @@ app
}
})
.define(async (ctx) => {
// https://localhost:51015/client/router?path=shop&key=install
// https://localhost:51515/client/router?path=shop&key=install
const options = ctx.query?.data || {};
const { id, type, force, yes } = options;
assistantConfig.checkMounted();
@@ -74,7 +74,7 @@ app
}
})
.define(async (ctx) => {
// https://localhost:51015/client/router?path=shop&key=uninstall
// https://localhost:51515/client/router?path=shop&key=uninstall
const options = ctx.query?.data || {};
const { id, type, yes } = options;
const app = new AppDownload(assistantConfig);

View File

@@ -1,13 +1,15 @@
import { useContextKey } from '@kevisual/context';
import { app, assistantConfig } from './app.ts';
import { proxyRoute, proxyWs } from './services/proxy/proxy-page-index.ts';
import './routes/index.ts';
import './routes-simple/index.ts';
import getPort, { portNumbers } from 'get-port';
import { program } from 'commander';
import { spawnSync } from 'node:child_process';
import path from 'node:path'
import chalk from 'chalk';
import { AssistantApp } from './lib.ts';
import { AssistantApp, checkFileExists } from './lib.ts';
import { getBunPath } from './module/get-bun-path.ts';
import { qwenAsr } from './services/asr/qwen-asr.ts';
export const runServer = async (port: number = 51515, listenPath = '127.0.0.1') => {
@@ -35,8 +37,11 @@ export const runServer = async (port: number = 51515, listenPath = '127.0.0.1')
});
} else {
app.listen(_port, listenPath, () => {
const protocol = assistantConfig.getHttps().protocol;
console.log(`Server is running on ${protocol}://${listenPath}:${_port}`);
let showListenPath = listenPath;
if (listenPath === '::') {
showListenPath = 'localhost';
}
console.log(`Server is running on ${'http'}://${showListenPath}:${_port}`);
});
}
app.server.on([{
@@ -46,13 +51,15 @@ export const runServer = async (port: number = 51515, listenPath = '127.0.0.1')
...proxyWs(),
qwenAsr,
]);
const manager = new AssistantApp(assistantConfig, app);
const manager = useContextKey('manager', new AssistantApp(assistantConfig, app));
setTimeout(() => {
manager.load({ runtime: 'client' }).then(() => {
console.log('Assistant App Loaded');
});
manager.initRemoteApp()
manager.initRouterApp()
}, 1000);
return {
app,
port: _port,
@@ -64,6 +71,8 @@ program
.option('-n, --name <name>', '服务名称', 'assistant-server')
.option('-p, --port <port>', '服务端口')
.option('-s, --start', '是否启动服务')
.option('-r, --root <root>', 'assistant app的根目录路径')
.option('-i, --input <input>', '启动的输入文件,例如/workspace/src/main.ts')
.option('-e, --interpreter <interpreter>', '指定使用的解释器', 'bun')
.action(async (options) => {
// console.log('当前执行路径:', execPath, inte);
@@ -94,7 +103,12 @@ program
if (port) {
pm2Command += ` -p ${port}`;
}
if (options.root) {
pm2Command += ` --root ${options.root}`;
}
if (options.input) {
pm2Command += ` --input ${options.input}`;
}
console.log(chalk.gray('执行命令:'), pm2Command);
console.log(chalk.gray('脚本路径:'), runPath);
@@ -138,9 +152,15 @@ program
} else if (options.start) {
console.log('启动服务', chalk.green(assistantConfig.configDir));
const config = assistantConfig.getCacheAssistantConfig();
const listenPort = options.port || config?.server?.port;
const listenPort = parseInt(options.port || config?.server?.port);
const listenPath = config?.server?.path || '::';
const server = await runServer(listenPort, listenPath);
if (options.input) {
const _input = path.resolve(options.input);
if (checkFileExists(_input)) {
await import(_input);
}
}
} else {
console.log('请使用 -s 参数启动服务');
}

View File

@@ -0,0 +1,25 @@
import { QwenAsrRelatime } from "@kevisual/video-tools/src/asr/index.ts";
export class AsrManger {
map: Map<string, QwenAsrRelatime>;
constructor() {
this.map = new Map();
}
getAsr(id: string) {
const asr = this.map.get(id);
return asr;
}
setAsr(id: string, asr: QwenAsrRelatime) {
this.map.set(id, asr);
return asr;
}
deleteAsr(id: string) {
this.map.delete(id);
}
}
export const arrManager = new AsrManger();

View File

@@ -1,9 +1,10 @@
import { QwenAsrRelatime } from "@kevisual/video-tools/src/asr/index.ts";
import { Listener, WebSocketListenerFun, WebSocketReq } from "@kevisual/router";
import { lightHA } from "@/routes/ha-api/ha.ts";
import { callText } from "@/routes/ha-api/ha.ts";
import { assistantConfig } from "@/app.ts";
const func: WebSocketListenerFun = async (req: WebSocketReq<{ asr: QwenAsrRelatime, msgId: string }>, res) => {
const func: WebSocketListenerFun = async (req: WebSocketReq<{ asr: QwenAsrRelatime, msgId: string, startTime?: number, loading?: boolean }>, res) => {
const { ws, emitter, id, data } = req;
let asr = ws.data.asr;
@@ -13,89 +14,91 @@ const func: WebSocketListenerFun = async (req: WebSocketReq<{ asr: QwenAsrRelati
return;
}
if (!asr) {
const token = process.env.BAILIAN_API_KEY || ''
if (ws.data.loading) return;
ws.data.loading = true;
const confg = assistantConfig.getConfig();
const asrConfig = confg?.asr;
if (!asrConfig?.enabled) {
ws.send(JSON.stringify({ type: 'error', message: 'asr服务未启用' }));
ws.close();
return;
}
const token = asrConfig?.token;
if (!token) {
ws.send(JSON.stringify({ type: 'error', message: 'asr服务未配置token' }));
ws.close();
return;
}
const onConnect = () => {
const asr = ws.data.asr as QwenAsrRelatime;
ws.send(JSON.stringify({ type: 'asr', code: 200, message: 'asr服务已连接', time: Date.now() }));
if (!asr) return;
asr.emitter.on('message', (message) => {
// console.log('ASR message', message);
});
asr.emitter.on('partial', (message) => {
// console.log('ASR message', message);
let msgId = ws.data.msgId || Math.random().toString(36).substring(2, 10);
ws.data.msgId = msgId;
ws.send(JSON.stringify({
type: 'partial',
msgId: msgId,
time: Date.now(),
text: message?.text,
raw: message?.raw,
}));
});
asr.emitter.on('result', async ({ text, raw }) => {
let msgId = ws.data.msgId;
ws.data.msgId = Math.random().toString(36).substring(2, 10);
const endTime = Date.now();
console.log('cost time', ws.data.startTime ? (endTime - ws.data.startTime) : 0);
ws.send(JSON.stringify({
type: 'result',
msgId: msgId,
time: Date.now(),
text,
}));
if (!text) return;
await callText(text);
console.log('toogle light time', Date.now() - endTime);
});
asr.start();
}
// 第一次请求
asr = new QwenAsrRelatime({
token,
onConnect: () => {
const asr = ws.data.asr as QwenAsrRelatime;
ws.send(JSON.stringify({ type: 'asr', code: 200, message: 'asr服务已连接', time: Date.now() }));
if (!asr) return;
asr.emitter.on('message', (message) => {
console.log('ASR message', message);
});
asr.emitter.on('partial', (message) => {
// console.log('ASR message', message);
let msgId = ws.data.msgId || Math.random().toString(36).substring(2, 10);
ws.data.msgId = msgId;
ws.send(JSON.stringify({
type: 'partial',
msgId: msgId,
time: Date.now(),
text: message?.text,
raw: message?.raw,
}));
});
asr.emitter.on('result', async ({ text, raw }) => {
let msgId = ws.data.msgId;
ws.data.msgId = Math.random().toString(36).substring(2, 10);
ws.send(JSON.stringify({
type: 'result',
msgId: msgId,
time: Date.now(),
text,
}));
if (!text) return;
const command = text?.trim().slice(0, 20);
type ParseCommand = {
type?: '打开' | '关闭',
appName?: string,
command?: string,
}
let obj: ParseCommand = {};
if (command.startsWith('打开')) {
obj.appName = command.replace('打开', '').trim();
obj.type = '打开';
} else if (command.startsWith('关闭')) {
obj.appName = command.replace('关闭', '').trim();
obj.type = '关闭';
}
if (obj.type) {
try {
const search = await lightHA.searchLight(obj.appName || '');
if (search.id) {
lightHA.toggleLight({ entity_id: search.id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
} else if (search.hasMore) {
const [first] = search.result;
lightHA.toggleLight({ entity_id: first.entity_id, service: obj.type === '打开' ? 'turn_on' : 'turn_off' });
} else {
console.log('未找到对应设备:', obj.appName);
}
console.log('解析到控制指令', obj);
} catch (e) {
console.error('控制失败', e);
}
}
});
asr.start();
}
emitter,
onConnect,
})
ws.data.asr = asr;
ws.data.loading = false;
emitter.on('close--' + id, () => {
console.log('ASR websocket 关闭, 释放资源');
asr?.close?.();
});
}
const isConnected = await asr.checkConnected();
if (!isConnected) return;
console.log('ASR receive data', 'has voice', !!data?.voice);
if (data?.type === 'blankVoice') {
asr.sendBlank();
console.log('ASR receive data', 'blank voice');
} else if (data?.voice) {
console.log('ASR receive voice', data.voice.slice(0, 30));
if (!data?.isRelatime) {
console.log('ASR receive data', 'has voice', !!data?.voice, data?.isRelatime);
const time = data?.time || 0;
console.log('receiveDelay', Date.now() - time);
}
const isBrowserFormat = data.format === 'float32';
let voice: Buffer;
if (isBrowserFormat) {
voice = await asr.fixBrowerBuffer(data.voice);
} else {
voice = Buffer.from(data.voice, 'base64');
}
ws.data.startTime = Date.now();
asr.sendBuffer(voice);
}
}

View File

@@ -2,13 +2,13 @@ import fs from 'node:fs';
import path from 'node:path';
import { checkFileExists, AssistantConfig, AssistantConfigData, parseHomeArg, parseHelpArg } from '@/module/assistant/index.ts';
import { chalk } from '@/module/chalk.ts';
import { HttpsPem } from '@/module/assistant/https/sign.ts';
import { Query } from '@kevisual/query/query';
import { installDeps } from '@/module/npm-install.ts'
export { parseHomeArg, parseHelpArg };
export type AssistantInitOptions = {
path?: string;
init?: boolean;
initWorkspace?: boolean;
};
const randomId = () => Math.random().toString(36).substring(2, 8);
/**
@@ -17,24 +17,31 @@ const randomId = () => Math.random().toString(36).substring(2, 8);
*/
export class AssistantInit extends AssistantConfig {
#query: Query
initWorkspace: boolean = false;
constructor(opts?: AssistantInitOptions) {
const configDir = opts?.path || process.cwd();
super({
configDir,
init: false,
});
this.initWorkspace = opts?.initWorkspace ?? true;
if (opts?.init) {
this.init();
}
}
async init() {
async init(configDir?: string) {
if (configDir) {
this.configDir = configDir;
}
// 1. 检查助手路径是否存在
if (!this.checkConfigPath()) {
super.init(configDir);
if (!this.initWorkspace) { return }
console.log(chalk.blue('助手路径不存在,正在创建...'));
super.init();
} else {
super.init();
super.init(configDir);
if (!this.initWorkspace) { return }
const assistantConfig = this;
console.log(chalk.yellow('助手路径已存在'), chalk.green(assistantConfig.configDir));
}
@@ -77,14 +84,6 @@ export class AssistantInit extends AssistantConfig {
fs.writeFileSync(appsConfig, JSON.stringify({ description: 'apps manager.', list: [] }));
console.log(chalk.green('助手应用配置文件 apps.json 创建成功'));
}
// create pem dir //
const pemDir = path.join(this.configPath?.configDir, 'pem');
const httpsPem = new HttpsPem(this);
if (httpsPem.isHttps) {
if (!checkFileExists(pemDir)) {
console.log(chalk.green('助手证书目录创建成功'));
}
}
}
createAssistantConfig() {
const assistantPath = this.configPath?.configPath;
@@ -129,32 +128,38 @@ export class AssistantInit extends AssistantConfig {
"description": "assistant-app package pnpm, node pkgs projects",
"type": "module",
"scripts": {
"start": "pm2 start apps/root/code-center/app.mjs --name root/code-center",
"cnb": "ASSISTANT_CONFIG_DIR=/workspace asst server -s -p 7878"
"start": "pm2 start apps/code-center/dist/app.mjs --name code-center",
"proxy": "pm2 start apps/page-proxy/dist/app.mjs --name page-proxy",
"preview": "pnpm i && ASSISTANT_CONFIG_DIR=/workspace asst server -s -p 8686"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@kevisual/router": "latest",
"@kevisual/use-config": "latest",
"@kevisual/query": "latest",
"ioredis": "latest",
"minio": "latest",
"pg": "latest",
"pm2": "latest",
"sequelize": "latest",
"crypto-js": "latest",
"better-sqlite3": "latest",
"unstorage": "latest",
"dayjs": "latest",
"es-toolkit": "latest",
"node-cron": "latest",
"dotenv": "latest"
"@aws-sdk/client-s3": "^3.975.0",
"@kevisual/oss": "^0.0.16",
"@kevisual/query": "^0.0.38",
"eventemitter3": "^5.0.4",
"@kevisual/router": "^0.0.62",
"@kevisual/use-config": "^1.0.28",
"ioredis": "^5.9.2",
"minio": "^8.0.6",
"pg": "^8.17.2",
"pm2": "^6.0.14",
"sequelize": "^6.37.7",
"crypto-js": "^4.2.0",
"better-sqlite3": "^12.6.2",
"unstorage": "^1.17.4",
"dayjs": "^1.11.19",
"es-toolkit": "^1.44.0",
"node-cron": "^4.2.1",
"dotenv": "^17.2.3"
},
"devDependencies": {
"@types/node": "latest",
"@types/crypto-js": "latest"
"@kevisual/types": "^0.0.12",
"@types/bun": "^1.3.6",
"@types/crypto-js": "^4.2.2",
"@types/node": "^25.0.10"
}
}
`,
@@ -218,11 +223,4 @@ export class AssistantInit extends AssistantConfig {
},
} as AssistantConfigData;
}
getHttps() {
const https = this.getConfig()?.https || {};
return {
https,
protocol: https?.type === 'https' ? 'https' : 'http',
};
}
}

View File

@@ -4,31 +4,37 @@
"description": "assistant-app package pnpm, node pkgs projects",
"type": "module",
"scripts": {
"start": "pm2 start apps/root/code-center/app.mjs --name root/code-center",
"proxy": "pm2 start apps/root/page-proxy/app.mjs --name root/page-proxy"
"start": "pm2 start apps/code-center/dist/app.mjs --name code-center",
"proxy": "pm2 start apps/page-proxy/dist/app.mjs --name page-proxy",
"preview": "pnpm i && ASSISTANT_CONFIG_DIR=/workspace asst server -s -p 8686"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@kevisual/router": "latest",
"@kevisual/use-config": "latest",
"@kevisual/query": "latest",
"ioredis": "latest",
"minio": "latest",
"pg": "latest",
"pm2": "latest",
"sequelize": "latest",
"crypto-js": "latest",
"better-sqlite3": "latest",
"unstorage": "latest",
"dayjs": "latest",
"es-toolkit": "latest",
"node-cron": "latest",
"dotenv": "latest"
"@aws-sdk/client-s3": "^3.975.0",
"@kevisual/oss": "^0.0.16",
"@kevisual/query": "^0.0.38",
"eventemitter3": "^5.0.4",
"@kevisual/router": "^0.0.62",
"@kevisual/use-config": "^1.0.28",
"ioredis": "^5.9.2",
"minio": "^8.0.6",
"pg": "^8.17.2",
"pm2": "^6.0.14",
"sequelize": "^6.37.7",
"crypto-js": "^4.2.0",
"better-sqlite3": "^12.6.2",
"unstorage": "^1.17.4",
"dayjs": "^1.11.19",
"es-toolkit": "^1.44.0",
"node-cron": "^4.2.1",
"dotenv": "^17.2.3"
},
"devDependencies": {
"@types/node": "latest",
"@types/crypto-js": "latest"
"@kevisual/types": "^0.0.12",
"@types/bun": "^1.3.6",
"@types/crypto-js": "^4.2.2",
"@types/node": "^25.0.10"
}
}

View File

@@ -1,15 +1,25 @@
import { fileProxy, httpProxy, createApiProxy, ProxyInfo } from '@/module/assistant/index.ts';
import { createApiProxy, ProxyInfo, proxy } from '@/module/assistant/index.ts';
import http from 'node:http';
import { LocalProxy } from './local-proxy.ts';
import { assistantConfig, app } from '@/app.ts';
import { assistantConfig, simpleRouter } from '@/app.ts';
import { log, logger } from '@/module/logger.ts';
import { getToken } from '@/module/http-token.ts';
import { getTokenUserCache } from '@/routes/index.ts';
import type { WebSocketListenerFun } from "@kevisual/router";
import WebSocket from 'ws';
import { renderNoAuthAndLogin } from '@/module/assistant/html/login.ts';
const localProxy = new LocalProxy({});
localProxy.initFromAssistantConfig(assistantConfig);
const isOpenPath = (pathname: string): boolean => {
const openPaths = ['/root/home', '/root/cli', '/root/login'];
for (const openPath of openPaths) {
if (pathname.startsWith(openPath)) {
return true;
}
}
return false;
}
/**
* 过滤访问的资源,允许谁访问
* @param req
@@ -21,7 +31,7 @@ const authFilter = async (req: http.IncomingMessage, res: http.ServerResponse) =
const auth = _assistantConfig?.auth || {};
const share = auth.share || 'protected';
const noAdmin = !auth.username;
if (noAdmin) return false;
if (noAdmin) return { code: 200, message: '没有管理员, 直接放过, 让管理登录和自己设置' };
const admin = auth.username;
const admins = auth.admin || [];
if (admin) {
@@ -31,42 +41,42 @@ const authFilter = async (req: http.IncomingMessage, res: http.ServerResponse) =
const pathname = decodeURIComponent(url.pathname);
// 放开 /
if (pathname === '/' || pathname === '/favicon.ico') {
return false;
return { code: 200, message: '允许访问根路径' };
}
// 放开首页
if (pathname.startsWith('/root/home') || pathname === '/root/cli') {
return false;
if (pathname.startsWith('/root/home') || pathname === '/root/cli/docs/') {
return { code: 200, message: '允许访问首页' };
}
// 放开api 以 /api /v1, /client, /serve 开头的请求
const openApiPaths = ['/api', '/v1', '/client', '/serve'];
const openApiPaths = ['/api', '/v1', '/client', '/serve', '/proxy', '/root'];
for (const openPath of openApiPaths) {
if (pathname.startsWith(openPath)) {
return false;
return { code: 200, message: '允许公共访问模块' };
}
}
if (share === 'public') {
return false;
return { code: 200, message: '公开模式允许访问' };
}
const { token } = await getToken(req)
if (!token) {
// no token 转到登录页面
res.writeHead(302, { Location: `/root/home/` });
res.end();
return false;
return { code: 500, message: '未登录' };
}
const tokenUser = await getTokenUserCache(token);
if (share === 'protected' && tokenUser?.code === 200) {
return false;
return { code: 200, message: '受保护模式已登录允许访问' };
}
if (share === 'private') {
if (tokenUser?.code === 200) {
const username = tokenUser?.data?.username;
if (admins.includes(username)) {
return false;
return { code: 200, message: '私有模式管理员允许访问' };
}
}
}
return true;
return { code: 500, message: '没有权限访问' };
}
export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const _assistantConfig = assistantConfig.getCacheAssistantConfig();
@@ -93,21 +103,55 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
res.end('Not Found Favicon');
return;
}
if (pathname.startsWith('/client/upload')) {
simpleRouter.parse(req, res);
return;
}
if (pathname.startsWith('/client')) {
logger.debug('handle by router', { url: req.url });
return;
}
if (pathname.startsWith('/router') || pathname.startsWith('/opencode')) {
logger.debug('handle by router (opencode/router)', { url: req.url });
return;
}
// client, api, v1, serve 开头的拦截
const apiProxy = _assistantConfig?.api?.proxy || [];
const defaultApiProxy = createApiProxy(_assistantConfig?.app?.url || 'https://kevisual.cn');
const apiBackendProxy = [...apiProxy, ...defaultApiProxy].find((item) => pathname.startsWith(item.path));
if (apiBackendProxy) {
log.debug('apiBackendProxy', { apiBackendProxy, url: req.url });
return httpProxy(req, res, {
path: apiBackendProxy.path,
target: apiBackendProxy.target,
const allProxy = [...apiProxy, ...defaultApiProxy];
const apiBackendProxy = allProxy.find((item) => pathname.startsWith(item.path));
const proxyFn = async (req: http.IncomingMessage, res: http.ServerResponse, proxyApi: ProxyInfo) => {
log.debug('proxyApi', { proxyApi, url: req.url });
// 设置 CORS 头
// res.setHeader('Access-Control-Allow-Origin', '*');
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
// res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
if (proxyApi.s3?.id) {
const storage = _assistantConfig?.storage || []
const storageConfig = storage.find((item) => item.id === proxyApi.s3?.id);
proxyApi.s3 = {
...storageConfig,
...proxyApi.s3,
}
}
if (proxyApi.file?.id) {
const storage = _assistantConfig?.storage || []
const storageConfig = storage.find((item) => item.id === proxyApi.file?.id);
proxyApi.file = {
...storageConfig,
...proxyApi.file,
}
}
return proxy(req, res, {
path: proxyApi.path,
target: proxyApi.target,
...proxyApi,
});
}
if (apiBackendProxy) {
return proxyFn(req, res, apiBackendProxy);
}
logger.debug('proxyRoute handle by router', { url: req.url }, noAdmin);
const urls = pathname.split('/');
const [_, _user, _app] = urls;
if (!_app) {
@@ -115,7 +159,9 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
res.end('Not Found Proxy');
return;
}
if (noAdmin) {
const isOpen = isOpenPath(pathname)
logger.debug('proxyRoute', { _user, _app, pathname, noAdmin, isOpen });
if (noAdmin && !isOpen) {
return toSetting();
}
if (_app && urls.length === 3) {
@@ -125,52 +171,40 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
}
const proxyApiList = _assistantConfig?.proxy || [];
const proxyApi = proxyApiList.find((item) => pathname.startsWith(item.path));
if (proxyApi && proxyApi.type === 'file') {
log.debug('proxyApi', { proxyApi, pathname });
const _indexPath = proxyApi.indexPath || `${_user}/${_app}/index.html`;
const _rootPath = proxyApi.rootPath;
if (!_rootPath) {
log.error('Not Found rootPath', { proxyApi, pathname });
return res.end(`Not Found [${proxyApi.path}] rootPath`);
}
return fileProxy(req, res, {
path: proxyApi.path, // 代理路径, 比如/root/home
rootPath: proxyApi.rootPath,
...proxyApi,
indexPath: _indexPath, // 首页路径
});
} else if (proxyApi && proxyApi.type === 'http') {
log.debug('proxyApi http', { proxyApi, pathname });
return httpProxy(req, res, {
path: proxyApi.path,
target: proxyApi.target,
type: 'http',
});
if (proxyApi) {
logger.debug('proxyPage', { proxyApi, pathname });
return proxyFn(req, res, proxyApi);
}
const filter = await authFilter(req, res);
if (filter) {
return res.end('Not Authorized Proxy');
if (filter.code !== 200) {
logger.debug('auth filter deny', filter);
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
// TODO: 这里可以做成可配置的登录页面
return res.end(renderNoAuthAndLogin('Not Authorized Proxy'));
}
const localProxyProxyList = localProxy.getLocalProxyList();
const localProxyProxy = localProxyProxyList.find((item) => pathname.startsWith(item.path));
if (localProxyProxy) {
log.log('localProxyProxy', { localProxyProxy, url: req.url });
return fileProxy(req, res, {
logger.debug('localProxyProxy', { localProxyProxy, url: req.url });
return proxyFn(req, res, {
path: localProxyProxy.path,
rootPath: localProxy.pagesDir,
indexPath: localProxyProxy.indexPath,
"type": 'file',
file: {
rootPath: localProxy.pagesDir,
indexPath: localProxyProxy.file.indexPath,
}
});
}
const creatCenterProxy = createApiProxy(_assistantConfig?.app?.url || 'https://kevisual.cn', ['/root', '/' + _user]);
const centerProxy = creatCenterProxy.find((item) => pathname.startsWith(item.path));
if (centerProxy) {
return httpProxy(req, res, {
return proxyFn(req, res, {
path: centerProxy.path,
target: centerProxy.target,
type: 'http',
});
}
log.debug('handle by router 404', req.url);
logger.debug('handle by router 404', req.url);
res.statusCode = 404;
res.end('Not Found Proxy');

View File

@@ -0,0 +1,178 @@
// 使用 fetch 和 FormData 上传文件的示例代码
// 示例 1: 使用 File 对象上传
async function uploadFileUsingFileObject(file: File) {
const formData = new FormData();
formData.append('file', file);
// 可以添加其他字段
formData.append('filename', file.name);
formData.append('description', '文件描述');
formData.append('appKey', 'test');
formData.append('version', '1.0.0');
try {
const response = await fetch('http://localhost:51516/client/router', {
method: 'POST',
body: formData,
// 注意:使用 FormData 时不需要手动设置 Content-Type
// 浏览器会自动设置正确的 multipart/form-data 边界
});
if (!response.ok) {
throw new Error(`上传失败: ${response.status} ${response.statusText}`);
}
const result = await response.json();
console.log('上传成功:', result);
return result;
} catch (error) {
console.error('上传出错:', error);
throw error;
}
}
// 示例 2: 使用 FileList如 input[type="file"])上传
async function uploadFilesUsingFileList(files: FileList) {
const formData = new FormData();
// 多个文件使用相同的字段名
for (let i = 0; i < files.length; i++) {
formData.append('files', files[i]);
}
try {
const response = await fetch('http://localhost:51516/client/router', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`上传失败: ${response.status} ${response.statusText}`);
}
const result = await response.json();
console.log('上传成功:', result);
return result;
} catch (error) {
console.error('上传出错:', error);
throw error;
}
}
// 示例 3: 从路径读取文件上传Node.js 环境,使用 fs 和 Blob
import fs from 'fs';
async function uploadFileFromPath(fileList: string[]) {
const formData = new FormData();
for (let m of fileList) {
const buffer = fs.readFileSync(m);
const file = new File([buffer], m.split('/').pop()!, {
type: 'application/octet-stream',
});
formData.append('file', file);
}
formData.append('appKey', 'test');
formData.append('version', '1.0.0');
let token = 'st_n9ycynd4m7wdyw3lejb8plnkyi62uejd'; // 如果需要身份验证,添加令牌
try {
const response = await fetch('http://localhost:51516/client/upload' + `?token=${token}`, {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`上传失败: ${response.status} ${response.statusText}`);
}
const result = await response.json();
console.log('上传成功:', JSON.stringify(result, null, 2));
return result;
} catch (error) {
console.error('上传出错:', error);
throw error;
}
}
uploadFileFromPath(['./src/test/remote-app.ts', './src/test/upload-file.ts']);
// 示例 4: 完整的 HTML 使用示例(浏览器环境)
/*
// HTML
// <input type="file" id="fileInput" multiple>
// <button onclick="handleUpload()">上传</button>
async function handleUpload() {
const fileInput = document.getElementById('fileInput') as HTMLInputElement;
const files = fileInput.files;
if (!files || files.length === 0) {
alert('请选择文件');
return;
}
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append('files', files[i]);
}
// 添加额外数据
formData.append('userId', '12345');
formData.append('timestamp', Date.now().toString());
try {
const response = await fetch('http://localhost:51516/client/router', {
method: 'POST',
body: formData,
});
if (!response.ok) {
throw new Error(`上传失败: ${response.status}`);
}
const result = await response.json();
alert('上传成功: ' + JSON.stringify(result));
} catch (error) {
alert('上传出错: ' + error);
}
}
*/
// 示例 5: 带进度监控的上传(浏览器环境)
/*
async function uploadWithProgress(file: File, onProgress: (percent: number) => void) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
formData.append('file', file);
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const percent = (e.loaded / e.total) * 100;
onProgress(percent);
}
});
xhr.addEventListener('load', () => {
if (xhr.status === 200) {
resolve(JSON.parse(xhr.responseText));
} else {
reject(new Error(`上传失败: ${xhr.status}`));
}
});
xhr.addEventListener('error', () => reject(new Error('网络错误')));
xhr.addEventListener('abort', () => reject(new Error('上传被取消')));
xhr.open('POST', 'http://localhost:51516/client/router');
xhr.send(formData);
});
}
// 使用
// const file = fileInput.files[0];
// uploadWithProgress(file, (percent) => {
// console.log(`上传进度: ${percent.toFixed(1)}%`);
// }).then(result => console.log('完成', result));
*/

View File

@@ -8,8 +8,8 @@ const testRouter = () => {
// const ws = new WebSocket('ws://118.196.32.29:3005/api/router');
// const ws = new WebSocket('wss://kevisual.cn/api/router');
// const ws = new WebSocket('ws://localhost:51015/api/ws/demo?id=12345');
const ws = new WebSocket('ws://localhost:51015/api/router?id=12345');
// const ws = new WebSocket('ws://localhost:51515/api/ws/demo?id=12345');
const ws = new WebSocket('ws://localhost:51515/api/router?id=12345');
console.log('Connecting to WebSocket server...');
ws.on('open', () => {
console.log('WebSocket connection opened');

View File

@@ -14,5 +14,6 @@
},
"include": [
"src/**/*",
"agent/**/*"
],
}

View File

@@ -3,6 +3,7 @@
// @ts-ignore
import pkg from './package.json';
// bun run src/index.ts --
const external = ['bun'];
await Bun.build({
target: 'node',
format: 'esm',
@@ -11,9 +12,8 @@ await Bun.build({
naming: {
entry: 'envision.js',
},
external: external,
define: {
ENVISION_VERSION: JSON.stringify(pkg.version),
},
env: 'ENVISION_*',
});

View File

@@ -10,7 +10,7 @@ import dotenv from 'dotenv';
dotenv.config();
const isDev = process.env.NODE_ENV === 'development';
let target = process.env.VITE_API_URL || 'http://localhost:51015';
let target = process.env.VITE_API_URL || 'http://localhost:51515';
const apiProxy = { target: target, changeOrigin: true, ws: true, rewriteWsOrigin: true, secure: false, cookieDomainRewrite: 'localhost' };
let proxy = {
'/root/': apiProxy,
@@ -19,6 +19,7 @@ let proxy = {
};
const basename = isDev ? undefined : `${pkgs.basename}`;
console.log('Astro Config Basename:', basename);
export default defineConfig({
base: basename,
integrations: [

View File

@@ -8,10 +8,10 @@
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"pub": "envision deploy ./dist -k cli -v 0.0.3 -u -y y",
"pub": "envision deploy ./dist -k cli -v 0.0.4 -u -y y",
"slide:dev": "slidev --open slides/index.md",
"slide:build": "slidev build slides/index.md --base /root/cli-slide/",
"slide:pub": "envision deploy ./slides/dist -k cli-slide -v 0.0.3 -u",
"slide:pub": "envision deploy ./slides/dist -k cli-slide -v 0.0.4 -u",
"ui": "pnpm dlx shadcn@latest add "
},
"keywords": [],
@@ -21,23 +21,23 @@
"dependencies": {
"@astrojs/mdx": "^4.3.13",
"@astrojs/react": "^4.4.2",
"@astrojs/sitemap": "^3.6.0",
"@astrojs/vue": "^5.1.3",
"@kevisual/api": "^0.0.5",
"@astrojs/sitemap": "^3.7.0",
"@astrojs/vue": "^5.1.4",
"@kevisual/api": "^0.0.17",
"@kevisual/context": "^0.0.4",
"@kevisual/kv-code": "^0.0.4",
"@kevisual/query": "^0.0.33",
"@kevisual/query": "^0.0.35",
"@kevisual/query-login": "^0.0.7",
"@kevisual/registry": "^0.0.1",
"@radix-ui/react-slot": "^1.2.4",
"@tailwindcss/vite": "^4.1.18",
"@uiw/react-md-editor": "^4.0.11",
"antd": "^6.1.1",
"astro": "^5.16.6",
"antd": "^6.2.0",
"astro": "^5.16.11",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"dayjs": "^1.11.19",
"es-toolkit": "^1.43.0",
"es-toolkit": "^1.44.0",
"github-markdown-css": "^5.8.1",
"highlight.js": "^11.11.1",
"lucide-react": "^0.562.0",
@@ -49,20 +49,20 @@
"react-toastify": "^11.0.5",
"tailwind-merge": "^3.4.0",
"vue": "^3.5.26",
"zustand": "^5.0.9"
"zustand": "^5.0.10"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@kevisual/types": "^0.0.10",
"@types/react": "^19.2.7",
"@kevisual/types": "^0.0.11",
"@types/react": "^19.2.8",
"@types/react-dom": "^19.2.3",
"dotenv": "^17.2.3",
"tailwindcss": "^4.1.18",
"tw-animate-css": "^1.4.0"
},
"packageManager": "pnpm@10.26.1",
"packageManager": "pnpm@10.28.0",
"onlyBuiltDependencies": [
"@tailwindcss/oxide",
"esbuild",

View File

@@ -1,4 +1,4 @@
import { query, queryLogin } from '@/modules/query';
import { clientQuery, queryLogin } from '@/modules/query';
import { create } from 'zustand';
import { toast } from 'react-toastify';
type SettingState = {
@@ -12,7 +12,7 @@ export const useStore = create<SettingState>((set => ({
username: undefined,
config: undefined,
initAdmin: async () => {
const res = await query.post({
const res = await clientQuery.post({
path: 'config'
})
console.log('initAdmin', res);
@@ -25,7 +25,7 @@ export const useStore = create<SettingState>((set => ({
}
},
login: async (username: string, password: string) => {
const res = await query.post({
const res = await clientQuery.post({
path: 'admin',
key: 'login',
username,
@@ -40,7 +40,7 @@ export const useStore = create<SettingState>((set => ({
return res;
},
saveConfig: async (config: any) => {
const res = await query.post({
const res = await clientQuery.post({
path: 'config',
key: 'set',
data: config

View File

@@ -1,6 +1,6 @@
---
title: '配置项介绍'
description: 'Assistant 应用配置项完整说明文档包括应用信息、代理、服务器、认证、AI等各项配置详解'
description: 'Assistant 应用配置项完整说明文档包括应用信息、代理、服务器、认证、AI、存储等各项配置详解'
tags: ['config', 'configuration', 'settings', 'assistant']
createdAt: '2025-12-18'
---
@@ -22,28 +22,38 @@ createdAt: '2025-12-18'
}
```
- **id**: `string` - 应用唯一标识符,用于识别具体设备或应用实例
- **url**: `string` - 应用访问地址
| 字段 | 类型 | 说明 |
| ---- | -------- | ------------------------------------------ |
| id | `string` | 应用唯一标识符,用于识别具体设备或应用实例 |
| url | `string` | 应用访问地址 |
## token - 访问令牌
用于身份验证的访问令牌。
```json
{
"token": "your-access-token"
}
```
- **token**: `string` - 用于身份验证的访问令牌
| 字段 | 类型 | 说明 |
| ----- | -------- | -------- |
| token | `string` | 访问令牌 |
## registry - 注册中心
注册中心地址,默认为 `https://kevisual.cn`
```json
{
"registry": "https://kevisual.cn"
}
```
- **registry**: `string` - 注册中心地址,默认为 `https://kevisual.cn`
| 字段 | 类型 | 说明 |
| -------- | -------- | ------------ |
| registry | `string` | 注册中心地址 |
## proxy - 前端代理配置
@@ -61,16 +71,18 @@ createdAt: '2025-12-18'
}
```
- **proxy**: `ProxyInfo[]` - 代理配置数组
- **path**: `string` - 匹配的路径前缀
- **target**: `string` - 目标服务器地址
- **pathname**: `string` - 转发到目标服务器的路径
| 字段 | 类型 | 说明 |
| ---------------- | ------------- | ---------------------- |
| proxy | `ProxyInfo[]` | 代理配置数组 |
| proxy[].path | `string` | 匹配的路径前缀 |
| proxy[].target | `string` | 目标服务器地址 |
| proxy[].pathname | `string` | 转发到目标服务器的路径 |
示例:访问 `/root/home` 会被转发到 `https://kevisual.cn/root/home`
## api - API代理配置
专门用于API请求的代理配置。
专门用于 API 请求的代理配置,例如 `/api``/v1` 开头的请求
```json
{
@@ -89,17 +101,48 @@ createdAt: '2025-12-18'
}
```
- **api.proxy**: `ProxyInfo[]` - API代理配置数组配置方式同 `proxy`
| 字段 | 类型 | 说明 |
|------|------|{------|
| api.proxy | `ProxyInfo[]` | API代理配置数组 |
## router - 路由配置
配置应用的路由代理功能。
```json
{
"router": {
"proxy": [
{
"type": "router",
"router": {
"url": "https://kevisual.cn/api/router"
}
}
],
"base": true
}
}
```
| 字段 | 类型 | 说明 |
| ------------ | ------------- | ----------------------------------------------------------------- |
| router.proxy | `ProxyInfo[]` | 代理配置数组 |
| router.base | `boolean` | 是否注册基础路由,监听https://kevisual.cn/api/router默认 `false` |
## description - 应用描述
应用的描述信息。
```json
{
"description": "我的助手应用"
}
```
- **description**: `string` - 应用的描述信息
| 字段 | 类型 | 说明 |
| ----------- | -------- | ------------ |
| description | `string` | 应用描述信息 |
## server - 服务器配置
@@ -114,8 +157,10 @@ createdAt: '2025-12-18'
}
```
- **server.path**: `string` - 服务器监听地址,默认 `127.0.0.1`
- **server.port**: `number` - 服务器监听端口号
| 字段 | 类型 | 说明 |
| ----------- | --------- | -------------------------------- |
| server.path | `string`` | 服务器监听地址,默认 `127.0.0.1` |
| server.port | `number` | 服务器监听端口号 |
## share - 远程访问配置
@@ -130,8 +175,10 @@ createdAt: '2025-12-18'
}
```
- **share.url**: `string` - 远程应用代理地址
- **share.enabled**: `boolean` - 是否启用远程访问功能
| 字段 | 类型 | 说明 |
| ------------- | --------- | -------------------- |
| share.url | `string` | 远程应用代理地址 |
| share.enabled | `boolean` | 是否启用远程访问功能 |
## watch - 文件监听配置
@@ -145,21 +192,27 @@ createdAt: '2025-12-18'
}
```
- **watch.enabled**: `boolean` - 是否启用文件监听
| 字段 | 类型 | 说明 |
| ------------- | --------- | ---------------- |
| watch.enabled | `boolean` | 是否启用文件监听 |
## home - 首页路径
访问根路径 `/` 时自动重定向的首页地址。
```json
{
"home": "/root/home"
}
```
- **home**: `string` - 访问根路径 `/` 时自动重定向的首页地址
| 字段 | 类型 | 说明 |
| ---- | -------- | -------- |
| home | `string` | 首页路径 |
## ai - AI功能配置
启用和配置本地AI代理功能。
启用和配置本地 AI 代理功能。
```json
{
@@ -172,28 +225,32 @@ createdAt: '2025-12-18'
}
```
- **ai.enabled**: `boolean` - 是否启用AI功能
- **ai.provider**: `string` - AI提供商可选 `'DeepSeek'` | `'Custom'` 或其他自定义值
- **ai.apiKey**: `string` - API密钥
- **ai.model**: `string` - 使用的模型名称
| 字段 | 类型 | 说明 |
| ----------- | --------- | ---------------------------- | ---------- |
| ai.enabled | `boolean` | 是否启用 AI 功能 |
| ai.provider | `string` | AI 提供商,可选 `'DeepSeek'` | `'Custom'` |
| ai.apiKey | `string` | API 密钥 |
| ai.model | `string` | 使用的模型名称 |
## scripts - 自定义脚本
## asr - 语音识别配置
定义自定义脚本命令,在应用启动时执行
配置阿里云语音识别服务
```json
{
"scripts": {
"start": "node server.js",
"build": "npm run build",
"custom": "echo 'Hello World'"
"asr": {
"enabled": true,
"token": "your-asr-token"
}
}
```
- **scripts**: `Record<string, string>` - 键值对形式的脚本配置
- key: 脚本名称
- value: 要执行的命令
| 字段 | 类型 | 说明 |
| ----------- | --------- | -------------------- |
| asr.enabled | `boolean` | 是否启用语音识别功能 |
| asr.token | `string` | 阿里云 ASR 服务令牌 |
使用模型:`qwen3-asr-flash-realtime`
## auth - 认证和权限配置
@@ -207,33 +264,71 @@ createdAt: '2025-12-18'
}
```
- **auth**: `AuthPermission` - 认证权限配置对象
- **share**: 共享访问模式
- `"protected"` - 需要认证才能访问(默认)
- `"public"` - 公开访问,无需认证
- `"private"` - 私有访问,完全禁止外部访问
| 字段 | 类型 | 说明 |
| ---------- | -------- | ------------------------------------------------- |
| auth.share | `string` | 共享访问模式,影响 pages 目录下页面的对外共享权限 |
> **说明**: `share` 配置影响 pages 目录下页面的对外共享权限
**share 可选值:**
## https - HTTPS证书配置
- `"protected"` - 需要认证才能访问(默认)
- `"public"` - 公开访问,无需认证
- `"private"` - 私有访问,完全禁止外部访问
配置HTTPS服务和证书。
## storage - 存储配置
配置文件存储,支持本地文件系统和 S3/MinIO 两种存储方式。
### 本地文件存储 (FileStorage)
```json
{
"https": {
"type": "https",
"keyPath": "/path/to/private.key",
"certPath": "/path/to/certificate.crt"
}
"storage": [
{
"id": "local-storage",
"path": "./uploads",
"type": "local"
}
]
}
```
- **https.type**: `'https' | 'http'` - 服务协议类型,默认 `'http'`
- **https.keyPath**: `string` - SSL证书私钥文件路径
- **https.certPath**: `string` - SSL证书文件路径
### S3/MinIO 存储 (S3Storage)
> **注意**: 通常不需要配置HTTPS可以通过反向代理如Nginx实现HTTPS访问
```json
{
"storage": [
{
"id": "s3-storage",
"bucket": "my-bucket",
"region": "us-east-1",
"accessKeyId": "AKIAXXXXXXXXXXXXXXXX",
"secretAccessKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"endpoint": "https://s3.amazonaws.com",
"type": "s3"
},
{
"id": "minio-storage",
"bucket": "my-bucket",
"region": "us-east-1",
"accessKeyId": "minioadmin",
"secretAccessKey": "minioadmin",
"endpoint": "http://localhost:9000",
"type": "minio"
}
]
}
```
| 字段 | 类型 | 说明 |
| ------------------------- | ---------------------------- | ------------------------------------------------------- |
| storage[].id | `string` | 存储标识符,唯一标识一个存储配置 |
| storage[].type | `'local' \| 's3' \| 'minio'` | 存储类型 |
| storage[].path | `string` | 本地存储路径(仅 type 为 `local` 时有效) |
| storage[].bucket | `string` | 存储桶名称(仅 type 为 `s3` 或 `minio` 时有效) |
| storage[].region | `string` | 存储区域(仅 type 为 `s3` 或 `minio` 时有效) |
| storage[].accessKeyId | `string` | 访问密钥 ID仅 type 为 `s3` 或 `minio` 时有效) |
| storage[].secretAccessKey | `string` | 访问密钥(仅 type 为 `s3` 或 `minio` 时有效) |
| storage[].endpoint | `string` | 服务端点地址(仅 type 为 `s3` 或 `minio` 时有效,可选) |
## 完整配置示例
@@ -260,6 +355,17 @@ createdAt: '2025-12-18'
}
]
},
"router": {
"proxy": [
{
"type": "router",
"router": {
"url": "https://kevisual.cn/api/router"
}
}
],
"base": true
},
"description": "生产环境助手应用",
"server": {
"path": "0.0.0.0",
@@ -279,13 +385,29 @@ createdAt: '2025-12-18'
"apiKey": "sk-xxx",
"model": "deepseek-chat"
},
"scripts": {
"setup": "npm install",
"dev": "npm run dev"
"asr": {
"enabled": true,
"token": "your-asr-token"
},
"auth": {
"share": "protected"
}
},
"storage": [
{
"id": "local-storage",
"path": "./uploads",
"type": "local"
},
{
"id": "s3-storage",
"bucket": "my-bucket",
"region": "us-east-1",
"accessKeyId": "AKIAXXXXXXXXXXXXXXXX",
"secretAccessKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"endpoint": "https://s3.amazonaws.com",
"type": "s3"
}
]
}
```
@@ -295,8 +417,8 @@ createdAt: '2025-12-18'
## 最佳实践
1. **安全性**: 不要在配置文件中硬编码敏感信息(如 token、apiKey建议使用环境变量
1. **安全性**: 不要在配置文件中硬编码敏感信息(如 token、apiKey、secretAccessKey),建议使用环境变量
2. **端口选择**: 确保选择的端口未被占用
3. **代理配置**: 合理配置代理路径,避免路径冲突
4. **HTTPS**: 生产环境建议使用反向代理配置HTTPS而非直接在应用中配置
5. **权限控制**: 根据实际需求选择合适的 `auth.share` 模式
4. **权限控制**: 根据实际需求选择合适的 `auth.share` 模式
5. **存储配置**: 根据应用规模选择合适的存储方式本地存储适合开发和小型应用S3/MinIO 适合生产环境

View File

@@ -14,6 +14,10 @@ export const query = new QueryClient({
url: getUrl()
});
export const clientQuery = new QueryClient({
url: '/client/router'
});
export const remoteQuery = new Query({
url: '/api/router'
});

View File

@@ -10,13 +10,13 @@ import Html from '@/components/html.astro';
</div>
<div class="card-grid">
<a href="./docs/" class="card">
<a href={"/root/cli/docs/"} class="card">
<div class="card-icon">📚</div>
<h2>文档中心</h2>
<p>查看完整的使用文档和API参考</p>
</a>
<a href="./settings/" class="card">
<a href={"/root/cli/settings/"} class="card">
<div class="card-icon">⚙️</div>
<h2>设置中心</h2>
<p>配置和管理您的应用设置</p>

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/cli",
"version": "0.0.76",
"version": "0.0.94",
"description": "envision 命令行工具",
"type": "module",
"basename": "/root/cli",
@@ -20,6 +20,9 @@
"asst": "bin/assistant.js",
"asst-server": "bin/assistant-server.js"
},
"exports": {
".": "./dist/assistant-opencode.js"
},
"files": [
"dist",
"bin",
@@ -41,29 +44,34 @@
],
"author": "abearxiong",
"dependencies": {
"@inquirer/prompts": "^8.2.0",
"@kevisual/app": "^0.0.2",
"@kevisual/auth": "^2.0.3",
"@kevisual/context": "^0.0.4",
"@kevisual/hot-api": "^0.0.3",
"@nut-tree-fork/nut-js": "^4.2.6",
"eventemitter3": "^5.0.1",
"@kevisual/use-config": "^1.0.28",
"@opencode-ai/sdk": "^1.1.36",
"@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.4",
"micromatch": "^4.0.8",
"pm2": "^6.0.14",
"pm2": "latest",
"semver": "^7.7.3",
"unstorage": "^1.17.3"
"unstorage": "^1.17.4"
},
"devDependencies": {
"@kevisual/dts": "^0.0.3",
"@kevisual/load": "^0.0.6",
"@kevisual/logger": "^0.0.4",
"@kevisual/query": "0.0.33",
"@kevisual/query": "0.0.38",
"@kevisual/query-login": "0.0.7",
"@types/bun": "^1.3.5",
"@types/bun": "^1.3.6",
"@types/crypto-js": "^4.2.2",
"@types/jsonwebtoken": "^9.0.10",
"@types/micromatch": "^4.0.10",
"@types/node": "^25.0.3",
"@types/node": "^25.0.10",
"@types/semver": "^7.7.1",
"chalk": "^5.6.2",
"commander": "^14.0.2",
@@ -72,10 +80,10 @@
"filesize": "^11.0.13",
"form-data": "^4.0.5",
"ignore": "^7.0.5",
"inquirer": "^13.1.0",
"jsonwebtoken": "^9.0.3",
"tar": "^7.5.2",
"zustand": "^5.0.9"
"pm2": "^6.0.14",
"tar": "^7.5.6",
"zustand": "^5.0.10"
},
"engines": {
"node": ">=22.0.0"
@@ -83,4 +91,4 @@
"publishConfig": {
"access": "public"
}
}
}

4206
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,7 @@
packages:
- 'assistant'
- 'cli-center'
- assistant
- cli-center
onlyBuiltDependencies:
- esbuild
- sharp

View File

@@ -1,8 +1,5 @@
# 可视化控制台工具
## 1. 上传文件
## 2. 下载文件
## 3. 同步模板
# 一个简单的cli工具
```
npm i -g @kevisual/cli
```

View File

@@ -10,7 +10,7 @@ import { fileIsExist } from '@/uitls/file.ts';
import fs from 'fs';
import { getConfig } from '@/module/get-config.ts';
import path from 'path';
import inquirer from 'inquirer';
import { confirm } from '@inquirer/prompts';
import { baseURL, getUrl } from '@/module/query.ts';
export const appCommand = new Command('app').description('app 命令').action(() => {
console.log('app');
@@ -99,15 +99,11 @@ const uninstallAppCommand = new Command('uninstall')
if (!checkPath) {
console.error(chalk.red('path is error, 请输入正确的路径'));
} else {
const answer = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `确定要删除 ${_path} 吗?`,
default: false,
},
]);
if (answer.confirm) {
const confirmed = await confirm({
message: `确定要删除 ${_path} 吗?`,
default: false,
});
if (confirmed) {
fs.rmSync(_path, { recursive: true });
console.log(chalk.green('删除成功', _path));
}

150
src/command/claude/cc.ts Normal file
View File

@@ -0,0 +1,150 @@
import { program, Command } from '@/program.ts';
import { chalk } from '../../module/chalk.ts';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { useKey } from '@kevisual/use-config';
import os from 'node:os'
import fs from 'node:fs';
import { select } from '@inquirer/prompts';
const MODELS = ['minimax', 'glm', 'volcengine', 'bailian'] as const;
type Model = typeof MODELS[number];
const changeMinimax = (token?: string) => {
const auth_token = token || useKey("MINIMAX_API_KEY")
return {
"env": {
"ANTHROPIC_AUTH_TOKEN": auth_token,
"ANTHROPIC_BASE_URL": "https://api.minimaxi.com/anthropic",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "MiniMax-M2.1",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "MiniMax-M2.1",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "MiniMax-M2.1",
"ANTHROPIC_MODEL": "MiniMax-M2.1",
"API_TIMEOUT_MS": "3000000",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": 1
}
}
}
const changeGLM = (token?: string) => {
const auth_token = token || useKey('ZHIPU_API_KEY')
return {
"env": {
"ANTHROPIC_AUTH_TOKEN": auth_token,
"ANTHROPIC_BASE_URL": "https://open.bigmodel.cn/api/anthropic",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.7",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-4.7",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-4.7",
"ANTHROPIC_MODEL": "glm-4.7"
}
}
}
const changeVolcengine = (token?: string) => {
const auth_token = token || useKey('VOLCENGINE_API_KEY')
return {
"env": {
"ANTHROPIC_AUTH_TOKEN": auth_token,
"ANTHROPIC_BASE_URL": "https://ark.cn-beijing.volces.com/api/coding",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "ark-code-latest",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "ark-code-latest",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "ark-code-latest",
"ANTHROPIC_MODEL": "ark-code-latest",
"API_TIMEOUT_MS": "3000000"
}
}
}
const changeBailian = (token?: string) => {
const auth_token = token || useKey('BAILIAN_API_KEY')
return {
"env": {
"ANTHROPIC_AUTH_TOKEN": auth_token,
"ANTHROPIC_BASE_URL": "https://coding.dashscope.aliyuncs.com/apps/anthropic",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "qwen3-coder-plus",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "qwen3-coder-plus",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "qwen3-coder-plus",
"ANTHROPIC_MODEL": "qwen3-coder-plus"
},
"includeCoAuthoredBy": false
}
}
/**
* 跳过登录检查,在~/.claude.json的配置中添加字段 "hasCompletedOnboarding": true
*/
const changeNoCheck = () => {
const homeDir = os.homedir();
const claudeConfigPath = path.join(homeDir, '.claude.json');
let claudeConfig = {};
if (fs.existsSync(claudeConfigPath)) {
const content = fs.readFileSync(claudeConfigPath, 'utf-8');
try {
claudeConfig = JSON.parse(content);
} catch {
claudeConfig = {};
}
}
claudeConfig = {
...claudeConfig,
hasCompletedOnboarding: true
};
fs.writeFileSync(claudeConfigPath, JSON.stringify(claudeConfig, null, 2));
}
const modelConfig: Record<Model, (token?: string) => object> = {
minimax: changeMinimax,
glm: changeGLM,
volcengine: changeVolcengine,
bailian: changeBailian,
};
const readOrCreateConfig = (configPath: string): Record<string, unknown> => {
if (fs.existsSync(configPath)) {
const content = fs.readFileSync(configPath, 'utf-8');
try {
return JSON.parse(content);
} catch {
return {};
}
}
return {};
};
const saveConfig = (configPath: string, config: Record<string, unknown>) => {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
};
export const command = new Command('cc')
.description('切换claude code模型支持GLM4.7、Minimax和豆包')
.option('-m, --models <model:string>', `选择模型: ${MODELS.join(' | ')}`)
.action(async (options) => {
const configPath = path.join(os.homedir(), '.claude/settings.json');
// 读取或创建配置
const config = readOrCreateConfig(configPath);
// 如果没有指定模型,使用 inquire 选择
let selectedModel: Model;
if (options.models && MODELS.includes(options.models as Model)) {
selectedModel = options.models as Model;
} else {
selectedModel = await select({
message: '请选择模型:',
choices: MODELS,
});
}
// 更新配置
const updateConfig = modelConfig[selectedModel]();
Object.assign(config, updateConfig);
// 保存配置
saveConfig(configPath, config);
changeNoCheck();
console.log(`已切换到模型: ${chalk.green(selectedModel)}`);
console.log(`配置已保存到: ${configPath}`);
});
program.addCommand(command)

View File

@@ -3,7 +3,7 @@ import { checkFileExists, getConfig, writeConfig } from '@/module/index.ts';
import path from 'path';
import fs from 'fs';
import { chalk } from '@/module/chalk.ts';
import inquirer from 'inquirer';
import { confirm, input } from '@inquirer/prompts';
// 设置工作目录
const setWorkdir = async (options: { workdir: string }) => {
@@ -24,15 +24,11 @@ const setWorkdir = async (options: { workdir: string }) => {
console.log('路径不存在');
fs.mkdirSync(finalPath, { recursive: true });
}
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
default: false,
},
]);
if (answers.confirm) {
const confirmed = await confirm({
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
default: false,
});
if (confirmed) {
flag = true;
config.workdir = finalPath;
console.log(chalk.green(`set workdir success:`, finalPath));
@@ -81,15 +77,11 @@ const command = new Command('config')
console.log('路径不存在');
fs.mkdirSync(finalPath, { recursive: true });
}
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
default: false,
},
]);
if (answers.confirm) {
const confirmed = await confirm({
message: `Are you sure you want to set the workdir to: ${finalPath}?`,
default: false,
});
if (confirmed) {
flag = true;
config.workdir = finalPath;
console.log(chalk.green(`set workdir success:`, finalPath));
@@ -100,15 +92,11 @@ const command = new Command('config')
if (options.set) {
const key = options.set;
let value = options.value;
const answer = await inquirer.prompt([
{
type: 'input',
name: 'value',
if (!value) {
value = await input({
message: `Enter your ${key}:(current: ${config[key]})`,
when: () => !value,
},
]);
value = answer.value || value;
});
}
if (key && value) {
flag = true;
config[key] = value;
@@ -138,16 +126,10 @@ const setCommand = new Command('set')
return;
}
let flag = false;
const answer = await inquirer.prompt([
{
type: 'input',
name: 'value',
if (value === 'not_input') {
value = await input({
message: `Enter your ${key}:(current: ${config[key]})`,
when: () => value === 'not_input',
},
]);
if (value === 'not_input' && !answer.value) {
value = '';
});
}
if (key === 'workdir') {
await setWorkdir({ workdir: value });
@@ -192,15 +174,11 @@ const getCommand = new Command('get')
.action(async (key) => {
const config = getConfig();
const keys = Object.keys(config);
const answer = await inquirer.prompt([
{
type: 'input',
name: 'key',
if (!key) {
key = await input({
message: `Enter your key:(keys: ${JSON.stringify(keys)})`,
when: () => !key,
},
]);
key = answer.key || key;
});
}
if (config[key]) {
console.log(chalk.green(`get ${key}:`));

View File

@@ -4,7 +4,7 @@ import path from 'path';
import fs from 'fs';
import FormData from 'form-data';
import { getBaseURL, query, storage } from '@/module/query.ts';
import inquirer from 'inquirer';
import { input, confirm } from '@inquirer/prompts';
import chalk from 'chalk';
import { upload } from '@/module/download/upload.ts';
import { getHash } from '@/uitls/hash.ts';
@@ -60,23 +60,15 @@ const command = new Command('deploy')
key = pkgInfo?.appKey || '';
}
logger.debug('start deploy');
if (!version || !key) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'version',
message: 'Enter your version:',
when: () => !version,
},
{
type: 'input',
name: 'key',
message: 'Enter your key:',
when: () => !key,
},
]);
version = answers.version || version;
key = answers.key || key;
if (!version) {
version = await input({
message: 'Enter your version:',
});
}
if (!key) {
key = await input({
message: 'Enter your key:',
});
}
const pwd = process.cwd();
const directory = path.join(pwd, filePath);
@@ -110,19 +102,16 @@ const command = new Command('deploy')
logger.debug('upload Files Key', key, version);
if (!yes) {
// 确认是否上传
const confirm = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: 'Do you want to upload these files?',
},
]);
if (!confirm.confirm) {
const confirmed = await confirm({
message: 'Do you want to upload these files?',
});
if (!confirmed) {
return;
}
}
const uploadDirectory = isDirectory ? directory : path.dirname(directory);
const res = await uploadFiles(_relativeFiles, uploadDirectory, { key, version, username: org, noCheckAppFiles: !noCheck, directory: options.directory });
logger.debug('upload res', res);
if (res?.code === 200) {
res.data?.upload?.map?.((d) => {
console.log(chalk.green('uploaded file', d?.name, d?.path));
@@ -131,6 +120,7 @@ const command = new Command('deploy')
key: key,
version: version,
});
logger.debug('queryAppVersion res', res2);
if (res2.code !== 200) {
console.error(chalk.red('查询应用版本失败'), res2.message, key);
return;
@@ -181,7 +171,7 @@ const uploadFiles = async (files: string[], directory: string, opts: UploadFileO
logger.error('请检查文件是否存在');
}
data.files.push({ path: file, hash: hash });
if(filePath.includes('readme.md')) {
if (filePath.includes('readme.md')) {
description = fs.readFileSync(filePath, 'utf-8');
}
}
@@ -225,8 +215,10 @@ const uploadFiles = async (files: string[], directory: string, opts: UploadFileO
}
const filename = path.basename(filePath);
logger.debug('upload file', file, filename);
// 解决 busbox 文件名乱码: 将 UTF-8 编码的文件名转换为 binary 字符串
const encodedFilename = Buffer.from(filename, 'utf-8').toString('binary');
form.append('file', fs.createReadStream(filePath), {
filename: filename,
filename: encodedFilename,
filepath: file,
});
needUpload = true;

104
src/command/docker.ts Normal file
View File

@@ -0,0 +1,104 @@
import { program, Command } from '@/program.ts';
import { chalk } from '../module/chalk.ts';
import path from 'node:path';
import { spawn } from 'node:child_process';
import { useKey } from '@kevisual/use-config';
import os from 'node:os'
import fs from 'node:fs';
import { select } from '@inquirer/prompts';
// 执行 docker指令
// docker login --username=${DOCKER_USERNAME} ${DOCKER_REGISTRY} --password ${DOCKER_PASSWORD}
// docker login -u cnb docker.cnb.cool --password ${CNB_TOKEN}
// helm registry login -u cnb helm.cnb.cool --password ${CNB_TOKEN}
const dockerCommand = new Command('docker')
.description('Docker 相关指令')
.action(async () => {
console.log(chalk.green('Docker command executed'));
});
const login = new Command('login')
.description('登录 Docker 镜像仓库')
.option('-r , --registry <registry>', 'Docker 镜像仓库地址', 'default')
.action(async (options) => {
const { registry = 'default' } = options;
let DOCKER_USERNAME = useKey('DOCKER_USERNAME') as string;
let DOCKER_PASSWORD = useKey('DOCKER_PASSWORD') as string;
let DOCKER_REGISTRY = useKey('DOCKER_REGISTRY') as string;
if (registry !== 'default') {
DOCKER_USERNAME = 'cnb';
DOCKER_PASSWORD = useKey('CNB_TOKEN') as string;
DOCKER_REGISTRY = 'docker.cnb.cool';
}
if (!DOCKER_USERNAME || !DOCKER_PASSWORD) {
console.log(chalk.red('请先配置 DOCKER_USERNAME 和 DOCKER_PASSWORD'));
return;
}
const loginProcess = spawn('docker', [
'login',
'--username',
DOCKER_USERNAME,
DOCKER_REGISTRY,
'--password-stdin'
], {
stdio: ['pipe', 'inherit', 'inherit']
});
loginProcess.stdin.write(DOCKER_PASSWORD + '\n');
loginProcess.stdin.end();
loginProcess.on('close', (code) => {
if (code === 0) {
console.log(chalk.green('登录成功'));
} else {
console.log(chalk.red(`登录失败,退出码:${code}`));
}
});
});
dockerCommand.addCommand(login);
const helmLogin = new Command('helm')
.description('Helm 登录镜像仓库')
.action(async () => {
});
const helmLoginCommand = new Command('login')
.description('登录 Helm 镜像仓库')
.action(async () => {
let DOCKER_USERNAME = 'cnb';
let DOCKER_PASSWORD = useKey('CNB_TOKEN') as string;
if (!DOCKER_PASSWORD) {
console.log(chalk.red('请先配置 CNB_TOKEN'));
return;
}
const helmLoginProcess = spawn('helm', [
'registry',
'login',
'--username',
DOCKER_USERNAME,
'--password-stdin',
'helm.cnb.cool'
], {
stdio: ['pipe', 'inherit', 'inherit']
});
helmLoginProcess.stdin.write(DOCKER_PASSWORD + '\n');
helmLoginProcess.stdin.end();
helmLoginProcess.on('close', (code) => {
if (code === 0) {
console.log(chalk.green('Helm 登录成功'));
} else {
console.log(chalk.red(`Helm 登录失败,退出码:${code}`));
}
});
});
helmLogin.addCommand(helmLoginCommand);
program.addCommand(dockerCommand);
program.addCommand(helmLogin);

74
src/command/jwks.ts Normal file
View File

@@ -0,0 +1,74 @@
import { generate } from '@kevisual/auth'
import { program, Command } from '@/program.ts';
import fs from 'node:fs';
import path from 'node:path';
export const getPath = async (dir: string) => {
const JWKS_PATH = path.join(dir, 'jwks.json');
const PRIVATE_JWK_PATH = path.join(dir, 'privateKey.json');
const PRIVATE_KEY_PATH = path.join(dir, 'privateKey.txt');
const PUBLIC_KEY_PATH = path.join(dir, 'publicKey.txt');
return {
JWKS_PATH,
PRIVATE_JWK_PATH,
PRIVATE_KEY_PATH,
PUBLIC_KEY_PATH,
}
}
const jwksCmd = new Command('jwks')
.description('JWKS 相关命令')
.action(async (opts) => {
});
const jwksGenerate = new Command('generate')
.alias('gen')
.option('-d , --dir <dir>', '指定保存目录,默认当前目录下 jwt 文件夹', 'jwt')
.description('生成 JWKS 密钥对')
.action(async (opts) => {
const dir = path.isAbsolute(opts.dir) ? opts.dir : path.join(process.cwd(), opts.dir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const { JWKS_PATH, PRIVATE_JWK_PATH, PRIVATE_KEY_PATH, PUBLIC_KEY_PATH } = await getPath(dir);
const { jwks, privateJWK, privatePEM, publicPEM } = await generate();
fs.writeFileSync(PUBLIC_KEY_PATH, publicPEM);
fs.writeFileSync(PRIVATE_KEY_PATH, privatePEM);
fs.writeFileSync(PRIVATE_JWK_PATH, JSON.stringify(privateJWK, null, 2));
fs.writeFileSync(JWKS_PATH, JSON.stringify(jwks, null, 2));
console.log(`Keys have been saved to directory: ${dir}`);
});
jwksCmd.addCommand(jwksGenerate);
const getJWKS = new Command('get')
.description('获取 JWKS 内容')
.option('-d , --dir <dir>', '指定 JWKS 所在目录,默认当前目录下 jwt 文件夹', 'jwt')
.option('-t, --type <type>', '指定获取类型jwks 或 privateJWK', 'jwks')
.action(async (opts) => {
const dir = path.isAbsolute(opts.dir) ? opts.dir : path.join(process.cwd(), opts.dir);
const { JWKS_PATH, PRIVATE_JWK_PATH } = await getPath(dir);
const type = opts.type || 'jwks';
if (type !== 'jwks') {
if (!fs.existsSync(PRIVATE_JWK_PATH)) {
console.error(`Private JWK file not found in directory: ${dir}`);
return;
}
const privateJWKContent = fs.readFileSync(PRIVATE_JWK_PATH, 'utf-8');
console.log('Private JWK:\n');
console.log(privateJWKContent);
return;
}
if (!fs.existsSync(JWKS_PATH)) {
console.error(`JWKS file not found in directory: ${dir}`);
return;
}
const jwksContent = fs.readFileSync(JWKS_PATH, 'utf-8');
console.log('PublicJWKS:\n');
console.log(jwksContent);
});
jwksCmd.addCommand(getJWKS);
program.addCommand(jwksCmd);

View File

@@ -1,6 +1,6 @@
import { program, Command } from '@/program.ts';
import { getConfig } from '@/module/get-config.ts';
import inquirer from 'inquirer';
import { getConfig, getEnvToken } from '@/module/get-config.ts';
import { input, password } from '@inquirer/prompts';
import { loginInCommand } from '@/module/login/login-by-web.ts';
import { queryLogin, storage } from '@/module/query.ts';
import chalk from 'chalk';
@@ -21,31 +21,39 @@ const loginCommand = new Command('login')
.option('-p, --password <password>', 'Specify password')
.option('-f, --force', 'Force login')
.option('-w, --web', 'Login on the web')
.option('-e, --env', 'Login from KEVISUAL_TOKEN environment variable')
.action(async (options) => {
let { username, password } = options;
if (options.web) {
await loginInCommand();
return;
}
// 从环境变量登录
if (options.env) {
const envToken = getEnvToken();
if (!envToken) {
console.log('环境变量 KEVISUAL_TOKEN 未设置');
return;
}
const res = await showMe(false);
if (res.code === 200) {
console.log('Login success:', res.data?.username || res.data?.email);
} else {
console.log('Login failed:', res.message || 'Invalid token');
}
return;
}
// 如果没有传递参数,则通过交互式输入
if (!username || !password) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'username',
message: 'Enter your username:',
when: () => !username, // 当 username 为空时,提示用户输入
},
{
type: 'password',
name: 'password',
message: 'Enter your password:',
mask: '*', // 隐藏输入的字符
when: () => !password, // 当 password 为空时,提示用户输入
},
]);
username = answers.username || username;
password = answers.password || password;
if (!username) {
username = await input({
message: 'Enter your username:',
});
}
if (!password) {
password = await password({
message: 'Enter your password:',
});
}
const token = storage.getItem('token');
if (token) {
@@ -75,13 +83,13 @@ const loginCommand = new Command('login')
program.addCommand(loginCommand);
const showMe = async (show = true) => {
const token = process.env.KEVISUAL_TOKEN;
const token = getEnvToken();
const localToken = storage.getItem('token');
if (!token && !localToken) {
console.log('请先登录');
return { code: 40400, message: '请先登录' };
}
let me = await queryLogin.getMe(token);
let me = await queryLogin.getMe(localToken || token);
if (me?.code === 401) {
me = await queryLogin.getMe();
}

View File

@@ -1,7 +1,7 @@
import { program as app, Command } from '@/program.ts';
import { getConfig, writeConfig } from '@/module/index.ts';
import { getConfig, getEnvToken, writeConfig } from '@/module/index.ts';
import { queryLogin, storage } from '@/module/query.ts';
import inquirer from 'inquirer';
import { input } from '@inquirer/prompts';
import util from 'util';
function isNumeric(str: string) {
return /^-?\d+\.?\d*$/.test(str);
@@ -23,7 +23,7 @@ const token = new Command('token')
.action(async (opts) => {
const token = storage.getItem('token');
if (opts.env) {
console.log('token in env', process.env.KEVISUAL_TOKEN);
console.log('token in env', getEnvToken());
} else {
console.log('token', token);
}
@@ -136,14 +136,9 @@ const setBaseURL = new Command('set')
const config = getConfig();
let baseURL = opt.baseURL;
if (!baseURL) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'baseURL',
message: `Enter your baseURL:(current: ${config.baseURL})`,
},
]);
baseURL = answers.baseURL;
baseURL = await input({
message: `Enter your baseURL:(current: ${config.baseURL})`,
});
if (!baseURL) {
console.log('baseURL is required');
return;

View File

@@ -5,7 +5,7 @@ import { spawn } from 'child_process';
import { fileIsExist } from '@/uitls/file.ts';
import { getConfig } from '@/module/get-config.ts';
import fs from 'fs';
import inquirer from 'inquirer';
import { select, confirm } from '@inquirer/prompts';
import { checkPnpm } from '@/uitls/npm.ts';
const parseIfJson = (str: string) => {
try {
@@ -14,17 +14,15 @@ const parseIfJson = (str: string) => {
return {};
}
};
const command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => {});
const command = new Command('npm').description('npm command show publish and set .npmrc').action(async (options) => { });
const publish = new Command('publish')
.argument('[registry]')
.option('-p --proxy', 'proxy')
.option('-t, --tag', 'tag')
.description('publish npm')
.action(async (registry, options) => {
const answer = await inquirer.prompt([
{
type: 'list',
name: 'publish',
if (!registry) {
registry = await select({
message: 'Select the registry to publish',
choices: [
{
@@ -35,11 +33,13 @@ const publish = new Command('publish')
name: 'npm',
value: 'npm',
},
{
name: 'cnb',
value: 'cnb'
}
],
when: !registry,
},
]);
registry = registry || answer.publish;
});
}
const config = getConfig();
let cmd = '';
const execPath = process.cwd();
@@ -64,6 +64,9 @@ const publish = new Command('publish')
case 'npm':
cmd = 'npm publish --registry https://registry.npmjs.org';
break;
case 'cnb':
cmd = 'npm publish --registry https://npm.cnb.cool/kevisual/registry/-/packages/';
break;
default:
cmd = 'npm publish --registry https://npm.xiongxiao.me';
break;
@@ -140,6 +143,7 @@ const npmrc = new Command('set')
const npmrcContent =
config?.npmrc ||
`//npm.xiongxiao.me/:_authToken=\${ME_NPM_TOKEN}
//npm.cnb.cool/kevisual/registry/-/packages/:_authToken=\${CNB_API_KEY}
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
`;
const execPath = process.cwd();
@@ -149,15 +153,11 @@ const npmrc = new Command('set')
if (options.force) {
writeFlag = true;
} else {
const answer = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to overwrite the .npmrc file?`,
default: false,
},
]);
if (answer.confirm) {
const confirmed = await confirm({
message: `Are you sure you want to overwrite the .npmrc file?`,
default: false,
});
if (confirmed) {
writeFlag = true;
}
}

View File

@@ -0,0 +1,2 @@
// TODO: 对 .opencode/plugin/agent.ts 的内容进行管理
// 例如添加、删除、列出等操作

View File

@@ -1,6 +1,5 @@
import { program, Command } from '@/program.ts';
import { chalk } from '@/module/chalk.ts';
import inquirer from 'inquirer';
const command = new Command('proxy')
.description('执行代理相关的命令')

View File

@@ -6,8 +6,9 @@ import { getConfig, query } from '@/module/index.ts';
import { fileIsExist } from '@/uitls/file.ts';
import { chalk } from '@/module/chalk.ts';
import * as backServices from '@/query/services/index.ts';
import inquirer from 'inquirer';
import { input } from '@inquirer/prompts';
import { logger } from '@/module/logger.ts';
// 查找文件(忽略大小写)
async function findFileInsensitive(targetFile: string): Promise<string | null> {
const files = fs.readdirSync('.');
@@ -249,14 +250,9 @@ const packCommand = new Command('pack')
let appKey: string | undefined;
let version = packageInfo.version || '';
if (!version) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'version',
message: 'Enter your version:',
},
]);
version = answers.version || version;
version = await input({
message: 'Enter your version:',
});
}
if (basename) {
@@ -271,14 +267,9 @@ const packCommand = new Command('pack')
appKey = basenameArr[1] || '';
}
if (!appKey) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'appKey',
message: 'Enter your appKey:',
},
]);
appKey = answers.appKey || appKey;
appKey = await input({
message: 'Enter your appKey:',
});
}
let value = await pack({
packDist,

View File

@@ -1,5 +1,5 @@
import { program, Command } from '@/program.ts';
import inquirer from 'inquirer';
import { input } from '@inquirer/prompts';
import { query } from '../module/index.ts';
import chalk from 'chalk';
import util from 'util';
@@ -15,27 +15,14 @@ const command = new Command('service')
let { path, key } = options;
// 如果没有传递参数,则通过交互式输入
if (!path) {
const answers = await inquirer.prompt([
{
type: 'input',
name: 'path',
required: true,
message: 'Enter your path:',
when: () => !path, // 当 username 为空时,提示用户输入
},
]);
path = answers.path || path;
path = await input({
message: 'Enter your path:',
});
}
if (!key) {
const answers = await inquirer.prompt([
{
type: 'input',
required: false,
name: 'key',
message: 'Enter your key:',
},
]);
key = answers.key || key;
key = await input({
message: 'Enter your key:',
});
}
const res = await query.post({ path, key });
if (res?.code === 200) {

View File

@@ -18,12 +18,14 @@ const checkAuth = (value: string = '', baseURL: string = '') => {
}
return false;
};
const DEFAULT_IGNORE = ['node_modules/**', '.git/**', '.next/**', '.astro/**', '.pack-dist/**'];
export class SyncBase {
config: Config;
#filename: string;
#dir: string;
baseURL: string;
defaultIgnore: string[] = ['node_modules/**', '.git/**'];
defaultIgnore: string[] = DEFAULT_IGNORE;
constructor(opts?: SyncOptions) {
const filename = opts?.configFilename || 'kevisual.json';
const dir = opts?.dir || process.cwd();
@@ -71,6 +73,12 @@ export class SyncBase {
const realFilename = path.basename(filename);
return { relative: path.relative(dir, file), absolute: file, filename: realFilename };
}
/**
*
* @param syncType
* @param type
* @returns
*/
async canDone(syncType: SyncConfigType, type?: SyncConfigType) {
if (syncType === 'sync') return true;
return syncType === type;
@@ -103,6 +111,9 @@ export class SyncBase {
const syncList = syncKeys.map((key) => {
const value = sync[key];
const filepath = path.join(this.#dir, key); // 文件的路径
if (filepath.includes('node_modules') || filepath.includes('.git')) {
return null;
}
if (typeof value === 'string') {
const auth = checkAuth(value, baseURL);
const type = auth ? 'sync' : 'none';
@@ -123,11 +134,14 @@ export class SyncBase {
type: value?.type ?? type,
auth: checkAuth(value.url, baseURL),
};
});
}).filter((item) => item);
let resultSyncList: SyncList[] = []
if (opts?.getFile) {
return this.getSyncListFile(syncList);
resultSyncList = await this.getSyncListFile(syncList);
} else {
resultSyncList = syncList;
}
return syncList;
return resultSyncList;
}
async getCheckList() {
const checkDir = this.config?.clone || {};

View File

@@ -8,7 +8,7 @@ import { logger, printClickableLink } from '@/module/logger.ts';
import { chalk } from '@/module/chalk.ts';
import path from 'node:path';
import { fileIsExist } from '@/uitls/file.ts';
import { confirm } from '@inquirer/prompts'
const command = new Command('sync')
.option('-d --dir <dir>')
.description('同步项目')
@@ -33,7 +33,19 @@ const syncUpload = new Command('upload')
};
const filepath = sync.getRelativePath(opts.file);
const newInfos = [];
const uploadLength = syncList.length;
logger.info(`开始上传文件,总计 ${uploadLength} 个文件`);
if (uploadLength > 100) {
// 提示用户确认
const shouldContinue = await confirm({
message: `即将上传 ${uploadLength} 个文件,是否继续?`,
default: false,
});
if (!shouldContinue) {
logger.info('已取消上传');
return;
}
}
for (const item of syncList) {
if (!item.auth || !item.exist) {
nodonwArr.push(item);

View File

@@ -17,6 +17,10 @@ import './command/gist/index.ts';
import './command/config-remote.ts';
import './command/config-secret-remote.ts';
import './command/ai.ts';
import './command/claude/cc.ts'
import './command/docker.ts';
import './command/jwks.ts';
// program.parse(process.argv);
export const runParser = async (argv: string[]) => {

View File

@@ -5,7 +5,8 @@ import { chalk } from '../chalk.ts';
import { Result } from '@kevisual/query';
import { fileIsExist } from '@/uitls/file.ts';
import { glob } from 'fast-glob';
import inquirer from 'inquirer';
import { confirm } from '@inquirer/prompts';
import { getEnvToken } from '../get-config.ts';
type DownloadTask = {
downloadPath: string;
@@ -38,7 +39,7 @@ type Options = {
[key: string]: any;
};
export const fetchLink = async (url: string = '', opts?: Options) => {
const token = process.env.KEVISUAL_TOKEN || storage.getItem('token');
const token = storage.getItem('token') || getEnvToken();
const fetchURL = new URL(url);
const check = opts?.check ?? false;
const isKevisual = !!url.includes('kevisual');
@@ -79,15 +80,11 @@ const checkDelete = async (opts?: { force?: boolean; dir?: string; yes?: boolean
try {
if (fileIsExist(dir)) {
const files = await glob(`${dir}/**/*`, { onlyFiles: true });
const answers = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `是否你需要删除 【${opts?.dir}】 目录下的文件. [${files.length}] 个?`,
when: () => files.length > 0 && !yes, // 当 username 为空时,提示用户输入
},
]);
if (answers?.confirm || yes) {
const shouldConfirm = files.length > 0 && !yes;
const confirmed = shouldConfirm ? await confirm({
message: `是否你需要删除 【${opts?.dir}】 目录下的文件. [${files.length}] 个?`,
}) : false;
if (confirmed || yes) {
fs.rmSync(dir, { recursive: true });
console.log(chalk.green('删除成功', dir));
} else {
@@ -272,8 +269,14 @@ export type AiList = {
pathname?: string;
url?: string;
};
/**
* AI路径的资源下载地址
* @param url
* @param opts
* @returns
*/
export const fetchAiList = async (url: string, opts?: { recursive: boolean }): Promise<Result<AiList[]>> => {
const token = process.env.KEVISUAL_TOKEN || storage.getItem('token');
const token = storage.getItem('token') || getEnvToken();
const _url = new URL(url);
const dir = _url.searchParams.get('dir');
if (!dir) {

View File

@@ -1,6 +1,7 @@
import os from 'os';
import path from 'path';
import fs from 'fs';
import { useKey } from '@kevisual/use-config';
export const envisionPath = path.join(os.homedir(), '.config', 'envision');
const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json');
@@ -35,3 +36,8 @@ export const writeConfig = (config: Record<string, any>) => {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
return config;
};
export const getEnvToken = () => {
const envTokne = useKey('KEVISUAL_TOKEN') || '';
return envTokne;
}

View File

@@ -1,7 +1,7 @@
import MD5 from 'crypto-js/md5.js';
import jsonwebtoken from 'jsonwebtoken';
import { getBaseURL, queryLogin } from '../query.ts';
import { chalk } from '../chalk.ts';
import jsonwebtoken from 'jsonwebtoken';
import { BaseLoad } from '@kevisual/load';
type LoginWithWebOptions = {};

View File

@@ -1,5 +1,5 @@
import { Query } from '@kevisual/query/query';
import { getConfig } from './get-config.ts';
import { getConfig, getEnvToken } from './get-config.ts';
import { QueryLoginNode, storage } from '@kevisual/query-login/node';
const config = getConfig();
export const baseURL = config?.baseURL || 'https://kevisual.cn';
@@ -14,7 +14,7 @@ export const getHeader = async () => {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
let token = process.env.KEVISUAL_TOKEN;
let token = getEnvToken();
if (!token) {
token = await storage.getItem('token');
}
@@ -26,7 +26,7 @@ export const getHeader = async () => {
query.beforeRequest = async (config) => {
if (config.headers) {
let token = process.env.KEVISUAL_TOKEN;
let token = getEnvToken();
if (!token) {
token = await storage.getItem('token');
}

View File

@@ -1,6 +1,7 @@
import { program, Command } from 'commander';
import fs from 'fs';
import { useContextKey } from '@kevisual/context'
// 将多个子命令加入主程序中
const version = useContextKey('version', () => {
let version = '0.0.64';

View File

@@ -1,3 +1,4 @@
import { logger } from '@/module/logger.ts';
import { query } from '@/module/query.ts';
import { DataOpts } from '@kevisual/query';
@@ -26,6 +27,7 @@ export const queryApp = async (params: QueryAppParams, opts?: any) => {
};
export const queryAppVersion = async (params: { key?: string; version?: string; id?: string }, opts?: DataOpts) => {
logger.debug('queryAppVersion params', params, query.url);
return await query.post(
{
path: 'app',

4
temp.md Normal file
View File

@@ -0,0 +1,4 @@
"@nut-tree-fork/nut-js": "^4.2.6",
"@kevisual/hot-api": "^0.0.3",
KEVISUAL_TOKEN="" LOG_LEVEL=DEBUG pnpm dev deploy ./cli-center/dist -k cli -v 0.0.4 -u -y y