update pkgs fix lightcode

This commit is contained in:
2026-01-26 01:13:47 +08:00
parent 2ccda9f008
commit 48dafc6040
11 changed files with 470 additions and 433 deletions

View File

@@ -41,20 +41,19 @@
}
},
"devDependencies": {
"@kevisual/ai": "^0.0.21",
"@kevisual/api": "^0.0.22",
"@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.37",
"@kevisual/query": "0.0.38",
"@kevisual/query-login": "0.0.7",
"@kevisual/router": "^0.0.60",
"@kevisual/router": "^0.0.62",
"@kevisual/types": "^0.0.12",
"@kevisual/use-config": "^1.0.28",
"@opencode-ai/plugin": "^1.1.28",
"@opencode-ai/plugin": "^1.1.35",
"@types/bun": "^1.3.6",
"@types/lodash-es": "^4.17.12",
"@types/node": "^25.0.9",
"@types/node": "^25.0.10",
"@types/send": "^1.2.1",
"@types/ws": "^8.18.1",
"chalk": "^5.6.2",
@@ -64,7 +63,6 @@
"dotenv": "^17.2.3",
"get-port": "^7.1.0",
"inquirer": "^13.2.1",
"lodash-es": "^4.17.22",
"nanoid": "^5.1.6",
"send": "^1.2.1",
"supports-color": "^10.2.2",
@@ -78,11 +76,12 @@
"access": "public"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.972.0",
"@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.28",
"@opencode-ai/sdk": "^1.1.35",
"es-toolkit": "^1.44.0",
"eventemitter3": "^5.0.4",
"lowdb": "^7.0.1",

View File

@@ -53,7 +53,7 @@ app.route({
description: '获取路由列表',
}).define(async (ctx) => {
const list = ctx.app.getList((item) => {
if (item.id === 'auth') return false;
if (item?.path?.includes('auth') || item?.id?.includes('auth')) return false;
return true;
})
ctx.body = { list }

View File

@@ -5,5 +5,5 @@ import './routes/index.ts';
import './routes-simple/index.ts';
export const AgentPlugin: Plugin = createRouterAgentPluginFn({
router: app.router,
router: app,
})

View File

@@ -98,10 +98,12 @@ export type AssistantConfigData = {
* 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开头的等等

View File

@@ -10,6 +10,7 @@ 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;
@@ -129,6 +130,7 @@ export class AssistantApp extends Manager {
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',
@@ -137,12 +139,27 @@ export class AssistantApp extends Manager {
}
})
}
if (lightcode) {
routerProxy.push({
type: 'lightcode',
lightcode: {
check: true,
}
})
}
if (routerProxy.length === 0) {
return
}
for (const proxyInfo of routerProxy) {
if (proxyInfo.type !== 'router') {
console.warn('路由的type必须是"router"');
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;

View File

@@ -14,7 +14,7 @@ export type ProxyInfo = {
/**
* 类型
*/
type?: 'file' | 'dynamic' | 'minio' | 'http' | 's3' | 'router';
type?: 'file' | 'dynamic' | 'minio' | 'http' | 's3' | 'router' | 'lightcode';
/**
* 目标的 pathname 默认为请求的url.pathname, 设置了pathname则会使用pathname作为请求的url.pathname
* @default undefined
@@ -45,6 +45,13 @@ export type ProxyInfo = {
router?: {
id?: string;
url?: string;
},
lightcode?: {
id?: string;
/**
* 是否检测远程服务更新
*/
check?: boolean;
}
};

View File

@@ -0,0 +1,70 @@
import { App, QueryRouterServer } from '@kevisual/router';
import { AssistantConfig } from '../assistant/index.ts';
import path from 'node:path';
import fs from 'node:fs';
import glob from 'fast-glob';
import { runCode } from './run.ts';
import { pick } from 'es-toolkit';
const codeDemoId = '0e700dc8-90dd-41b7-91dd-336ea51de3d2'
import { filter } from "@kevisual/js-filter";
const codeDemo = `// 这是一个示例代码文件
import {Mini} from '@kevisual/router';
const app = new Mini();
app.route({
path: 'hello',
describetion: 'LightCode 示例路由',
}).define(async (ctx) => {
ctx.body = 'Hello from LightCode!';
}).addTo(app);
app.wait();
`;
type opts = {
router: QueryRouterServer | App
config: AssistantConfig
}
export const initLightCode = async (opts: opts) => {
// 注册 lightcode 路由
console.log('初始化 lightcode 路由');
const config = opts.config;
const appDir = config.configPath.appsDir;
const lightcodeDir = path.join(appDir, 'light-code', 'code');
if (!fs.existsSync(lightcodeDir)) {
fs.mkdirSync(lightcodeDir, { recursive: true });
}
const codeFiles = glob.sync(['**/*.ts', '**/*.js'], {
cwd: lightcodeDir,
onlyFiles: true,
});
if (codeFiles.length === 0) {
// 如果没有代码文件,创建一个示例文件
const demoPath = path.join(lightcodeDir, `${codeDemoId}.ts`);
fs.writeFileSync(demoPath, codeDemo, 'utf-8');
}
for (const file of codeFiles) {
const tsPath = path.join(lightcodeDir, file);
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) {
const pickValues = pick(routerItem, ['path', 'id', 'description', 'metadata']);
if (routerItem.path?.includes('auth') || routerItem.path?.includes('router') || routerItem.path?.includes('call')) {
continue;
}
console.log('lightcode 路由必须包含 path 和 id', pickValues);
console.log(`注册 lightcode 路由: ${routerItem.path} ${routerItem.id} 来自文件: ${file}`);
const runRes2 = await runCode(tsPath, { path: routerItem.path, key: routerItem.key, id: routerItem.id }, { timeout: 10000 });
console.log('lightcode 路由执行结果', runRes2);
}
}
} else {
console.error('lightcode 路由执行失败', runRes.error);
}
}
}

View File

@@ -32,7 +32,6 @@ type RunCode = {
};
error?: any
timestamp?: string
[key: string]: any
}
export const runCode = async (tsPath: string, params: RunCodeParams = {}, opts?: RunCodeOptions): Promise<RunCode> => {
return new Promise((resolve, reject) => {

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';

View File

@@ -49,7 +49,7 @@
"@kevisual/auth": "^2.0.3",
"@kevisual/context": "^0.0.4",
"@kevisual/use-config": "^1.0.28",
"@opencode-ai/sdk": "^1.1.34",
"@opencode-ai/sdk": "^1.1.35",
"@types/busboy": "^1.5.4",
"busboy": "^1.6.0",
"eventemitter3": "^5.0.4",

773
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff