diff --git a/assistant/src/routes/index.ts b/assistant/src/routes/index.ts index 7a9ac2d..21e443c 100644 --- a/assistant/src/routes/index.ts +++ b/assistant/src/routes/index.ts @@ -2,8 +2,6 @@ import { app, assistantConfig } from '../app.ts'; import './config/index.ts'; import './shop-install/index.ts'; import './ai/index.ts'; -// TODO: -// import './light-code/index.ts'; import './user/index.ts'; import './call/index.ts' @@ -64,18 +62,16 @@ export const checkAuth = async (ctx: any, isAdmin = false) => { } authCache.set(token, tokenUser); } - if (ctx.state) { - ctx.state = { - ...ctx.state, - token, - tokenUser, - }; - } + ctx.state = { + ...ctx.state, + token, + tokenUser, + }; const { username } = tokenUser; if (!auth.username) { // 初始管理员账号 auth.username = username; - assistantConfig.setConfig({ auth }); + assistantConfig.setConfig({ auth, token: token }); } if (isAdmin && auth.username) { const admins = config.auth?.admin || []; @@ -83,6 +79,12 @@ export const checkAuth = async (ctx: any, isAdmin = false) => { const admin = auth.username; if (admin === username) { isCheckAdmin = true; + const _token = config.token; + if (!_token) { + assistantConfig.setConfig({ token: token }); + } else if (_token && _token.startsWith('st-') && _token !== token) { + assistantConfig.setConfig({ token: token }); + } } if (!isCheckAdmin && admins.length > 0 && admins.includes(username)) { isCheckAdmin = true; @@ -106,7 +108,7 @@ app description: '获取当前登录用户信息, 第一个登录的用户为管理员用户', }) .define(async (ctx) => { - if (!ctx.query?.token && ctx.appId === app.appId) { + if (!ctx.query?.token && ctx.appId === app.appId) { return; } const authResult = await checkAuth(ctx); @@ -122,7 +124,7 @@ app description: '管理员鉴权, 获取用户信息,并验证是否为管理员。', }) .define(async (ctx) => { - logger.debug('query', ctx.query); + // logger.debug('query', ctx.query); if (!ctx.query?.token && ctx.appId === app.appId) { return; } diff --git a/assistant/src/routes/light-code/call.ts b/assistant/src/routes/light-code/call.ts deleted file mode 100644 index 716071e..0000000 --- a/assistant/src/routes/light-code/call.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { app, assistantConfig } from '../../app.ts' -import path from 'path' -import { runCode } from '../../module/light-code/run.ts' - -// http://localhost:4005/api/router?path=call -app.route({ - path: 'light-code', - key: 'call', - // middleware: ['auth'] -}).define(async (ctx) => { - const filename = ctx.query?.filename || 'root/light-code-demo/demo-router.ts' - const data = ctx.query?.data || {} - const appsConfigPath = assistantConfig.configPath?.appsDir || ''; - const testA = path.join(appsConfigPath, filename) - try { - const resulst = await runCode(testA, data); - if (resulst.success) { - const callResult = resulst.data; - if (callResult.code === 200) ctx.body = callResult.data - else { - const callError = `调用程序错误: ${callResult.message}` - ctx.throw(callResult.code, callError) - } - } else { - ctx.body = `执行脚本错误: ${resulst.error}` - } - } catch (error) { - ctx.body = `执行脚本异常: ${error?.message || error}` - } - -}).addTo(app) \ No newline at end of file diff --git a/assistant/src/routes/light-code/index.ts b/assistant/src/routes/light-code/index.ts deleted file mode 100644 index a769548..0000000 --- a/assistant/src/routes/light-code/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import './call.ts' - -import './upload.ts' \ No newline at end of file diff --git a/assistant/src/routes/light-code/upload.ts b/assistant/src/routes/light-code/upload.ts deleted file mode 100644 index 0ae634f..0000000 --- a/assistant/src/routes/light-code/upload.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { app } from '../../app.ts'; - -app.route({ - path: 'light-code', - key: 'upload', - middleware: ['auth'], - description: '上传轻代码应用代码', -}).define(async (ctx) => { - const files = ctx.query.files; -}).addTo(app); \ No newline at end of file diff --git a/assistant/src/routes/opencode/ls.ts b/assistant/src/routes/opencode/ls.ts index 7cb1592..23632b9 100644 --- a/assistant/src/routes/opencode/ls.ts +++ b/assistant/src/routes/opencode/ls.ts @@ -18,12 +18,12 @@ app.route({ title: '创建 OpenCode 客户端', summary: '创建 OpenCode 客户端,如果存在则复用', args: { - + port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 5000') } }) }, }).define(async (ctx) => { - const client = await opencodeManager.getClient(); + const client = await opencodeManager.getClient({ port: ctx.query.port }); ctx.body = { content: `${opencodeManager.url} OpenCode 客户端已就绪` }; }).addTo(app); @@ -62,12 +62,12 @@ app.route({ title: '获取 OpenCode 服务 URL', summary: '获取当前 OpenCode 服务的 URL 地址', args: { - + port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 5000') } }) }, }).define(async (ctx) => { - const url = await opencodeManager.getUrl(); + const url = await opencodeManager.getUrl({ port: ctx.query.port }); const cnbURL = useKey('CNB_VSCODE_PROXY_URI') as string | undefined; let content = `本地访问地址: ${url}` if (cnbURL) { @@ -75,6 +75,7 @@ app.route({ } ctx.body = { content }; }).addTo(app); + // 调用 path: opencode key: ls-projects app.route({ path: 'opencode', diff --git a/assistant/src/routes/opencode/module/open.ts b/assistant/src/routes/opencode/module/open.ts index 7ba3721..87da2ab 100644 --- a/assistant/src/routes/opencode/module/open.ts +++ b/assistant/src/routes/opencode/module/open.ts @@ -6,13 +6,13 @@ import path from "node:path"; import fs from "node:fs"; import { execSync } from "node:child_process"; +const DEFAULT_PORT = 5000; 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() { } @@ -24,23 +24,21 @@ export class OpencodeManager { return OpencodeManager.instance; } - async getClient(): Promise { - // 如果已经有 client,直接返回 + async getClient(opts?: { port?: number }): Promise { + const port = opts?.port ?? DEFAULT_PORT; + if (this.client) { return this.client; } - // 如果正在初始化,等待初始化完成 if (this.isInitializing) { await new Promise(resolve => setTimeout(resolve, 100)); - return this.getClient(); + return this.getClient(opts); } - // 开始初始化 this.isInitializing = true; try { - const port = 5000; - const currentPort = await getPort({ port: port }); + const currentPort = await getPort({ port }); if (port === currentPort) { const result = await createOpencode({ hostname: '0.0.0.0', @@ -59,6 +57,7 @@ export class OpencodeManager { this.isInitializing = false; } } + async createOpencodeProject({ directory, port = 5000 @@ -69,11 +68,10 @@ export class OpencodeManager { }); return client; } + async killPort(port: number): Promise { 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]; @@ -83,7 +81,6 @@ export class OpencodeManager { } }); } else { - // Unix-like 平台 const result = execSync(`lsof -i :${port} -t`).toString(); result.split('\n').forEach(pid => { if (pid) { @@ -96,14 +93,15 @@ export class OpencodeManager { console.error('Failed to close OpenCode server:', error); } } - async close(): Promise { + + async close(opts?: { port?: number }): Promise { if (this.server) { this.server.close(); this.server = null; return } - const port = 5000; - const currentPort = await getPort({ port: port }); + const port = opts?.port ?? DEFAULT_PORT; + const currentPort = await getPort({ port }); if (port === currentPort) { this.client = null; return; @@ -113,14 +111,16 @@ export class OpencodeManager { this.client = null; } - async getUrl(): Promise { + + async getUrl(opts?: { port?: number }): Promise { + const port = opts?.port ?? DEFAULT_PORT; if (this.url) { return this.url; } if (!this.url) { - await this.getClient(); + await this.getClient(opts); } - return 'http://localhost:5000'; + return `http://localhost:${port}`; } }