@kevisual/router 0.0.51 → 0.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.51",
4
+ "version": "0.0.53",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -21,22 +21,22 @@
21
21
  "keywords": [],
22
22
  "author": "abearxiong",
23
23
  "license": "MIT",
24
- "packageManager": "pnpm@10.26.2",
24
+ "packageManager": "pnpm@10.28.0",
25
25
  "devDependencies": {
26
26
  "@kevisual/local-proxy": "^0.0.8",
27
- "@kevisual/query": "^0.0.33",
27
+ "@kevisual/query": "^0.0.35",
28
28
  "@rollup/plugin-alias": "^6.0.0",
29
29
  "@rollup/plugin-commonjs": "29.0.0",
30
30
  "@rollup/plugin-node-resolve": "^16.0.3",
31
31
  "@rollup/plugin-typescript": "^12.3.0",
32
32
  "@types/bun": "^1.3.5",
33
- "@types/node": "^25.0.3",
33
+ "@types/node": "^25.0.7",
34
34
  "@types/send": "^1.2.1",
35
35
  "@types/ws": "^8.18.1",
36
36
  "@types/xml2js": "^0.4.14",
37
- "cookie": "^1.1.1",
37
+ "eventemitter3": "^5.0.1",
38
38
  "nanoid": "^5.1.6",
39
- "rollup": "^4.54.0",
39
+ "rollup": "^4.55.1",
40
40
  "rollup-plugin-dts": "^6.3.0",
41
41
  "ts-loader": "^9.5.4",
42
42
  "ts-node": "^10.9.2",
@@ -45,17 +45,16 @@
45
45
  "typescript": "^5.9.3",
46
46
  "ws": "npm:@kevisual/ws",
47
47
  "xml2js": "^0.6.2",
48
- "zod": "^4.2.1"
48
+ "zod": "^4.3.5",
49
+ "@kevisual/js-filter": "^0.0.4",
50
+ "path-to-regexp": "^8.3.0",
51
+ "send": "^1.2.1"
49
52
  },
50
53
  "repository": {
51
54
  "type": "git",
52
55
  "url": "git+https://github.com/abearxiong/kevisual-router.git"
53
56
  },
54
- "dependencies": {
55
- "path-to-regexp": "^8.3.0",
56
- "selfsigned": "^5.4.0",
57
- "send": "^1.2.1"
58
- },
57
+ "dependencies": {},
59
58
  "publishConfig": {
60
59
  "access": "public"
61
60
  },
@@ -70,11 +69,6 @@
70
69
  "require": "./dist/router-browser.js",
71
70
  "types": "./dist/router-browser.d.ts"
72
71
  },
73
- "./sign": {
74
- "import": "./dist/router-sign.js",
75
- "require": "./dist/router-sign.js",
76
- "types": "./dist/router-sign.d.ts"
77
- },
78
72
  "./simple": {
79
73
  "import": "./dist/router-simple.js",
80
74
  "require": "./dist/router-simple.js",
@@ -93,6 +87,10 @@
93
87
  "./src/*": {
94
88
  "import": "./src/*",
95
89
  "require": "./src/*"
90
+ },
91
+ "./modules/*": {
92
+ "import": "./src/modules/*",
93
+ "require": "./src/modules/*"
96
94
  }
97
95
  }
98
- }
96
+ }
package/src/app.ts CHANGED
@@ -7,6 +7,7 @@ import { handleServer } from './server/handle-server.ts';
7
7
  import { IncomingMessage, ServerResponse } from 'http';
8
8
  import { isBun } from './utils/is-engine.ts';
9
9
  import { BunServer } from './server/server-bun.ts';
10
+ import { nanoid } from 'nanoid';
10
11
 
11
12
  type RouterHandle = (msg: { path: string;[key: string]: any }) => { code: string; data?: any; message?: string;[key: string]: any };
12
13
  type AppOptions<T = {}> = {
@@ -16,6 +17,7 @@ type AppOptions<T = {}> = {
16
17
  routerHandle?: RouterHandle;
17
18
  routerContext?: RouteContext<T>;
18
19
  serverOptions?: ServerNodeOpts;
20
+ appId?: string;
19
21
  };
20
22
 
21
23
  export type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & { app: App<T> };
@@ -25,6 +27,7 @@ export type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & { app: App<T
25
27
  * U - Route Context的扩展类型
26
28
  */
27
29
  export class App<U = {}> {
30
+ appId: string;
28
31
  router: QueryRouter;
29
32
  server: ServerType;
30
33
  constructor(opts?: AppOptions<U>) {
@@ -42,6 +45,12 @@ export class App<U = {}> {
42
45
  router.setContext({ needSerialize: true, ...opts?.routerContext });
43
46
  this.router = router;
44
47
  this.server = server;
48
+ if (opts?.appId) {
49
+ this.appId = opts.appId;
50
+ } else {
51
+ this.appId = nanoid(16);
52
+ }
53
+ router.appId = this.appId;
45
54
  }
46
55
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
47
56
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -0,0 +1,19 @@
1
+ import { loadTS, getMatchFiles } from './load-ts.ts';
2
+ import { listenSocket } from './listen-sock.ts';
3
+ import { Route, QueryRouter, QueryRouterServer } from '../route.ts';
4
+
5
+ export { Route, QueryRouter, QueryRouterServer };
6
+
7
+ export const App = QueryRouterServer;
8
+
9
+ export { createSchema } from './../index.ts';
10
+ export type { Rule } from '../validator/rule.ts';
11
+ export type { RouteContext, RouteOpts } from '../route.ts';
12
+
13
+ export type { Run } from '../route.ts';
14
+
15
+ export { CustomError } from '../result/error.ts';
16
+
17
+ export { listenSocket, loadTS, getMatchFiles };
18
+
19
+ export { autoCall } from './call-sock.ts';
package/src/index.ts CHANGED
@@ -20,7 +20,7 @@ export { App } from './app.ts';
20
20
  export * from './router-define.ts';
21
21
 
22
22
 
23
- export {
23
+ export type {
24
24
  RouterReq,
25
25
  RouterRes,
26
26
  OnWebSocketFn,
@@ -31,4 +31,6 @@ export {
31
31
  WebSocketListenerFun,
32
32
  HttpListenerFun,
33
33
  OnListener,
34
- } from './server/server-type.ts';
34
+ } from './server/server-type.ts';
35
+
36
+ export { loadTS } from './auto/load-ts.ts';
@@ -0,0 +1,57 @@
1
+ import { QueryRouter } from "../route.ts";
2
+ import { filter } from '@kevisual/js-filter'
3
+ type RouterChatOptions = {
4
+ router?: QueryRouter;
5
+ }
6
+ export class RouterChat {
7
+ router: QueryRouter;
8
+ prompt: string = '';
9
+ constructor(opts?: RouterChatOptions) {
10
+ this.router = opts?.router || new QueryRouter();
11
+ }
12
+ prefix(opts?: { query?: string }) {
13
+ if (this.prompt) {
14
+ return this.prompt;
15
+ }
16
+ let _routes = this.router.routes;
17
+ if (opts?.query) {
18
+ _routes = filter(this.router.routes, opts.query);
19
+ }
20
+ const toolsList = _routes.map((r, index) =>
21
+ `${index + 1}. 工具名称: ${r.id}\n 描述: ${r.description}`
22
+ ).join('\n\n');
23
+ const _prompt = `你是一个 AI 助手,你可以使用以下工具来帮助用户完成任务:
24
+
25
+ ${toolsList}
26
+
27
+ ## 回复规则
28
+ 1. 如果用户的请求可以使用上述工具完成,请返回 JSON 格式数据
29
+ 2. 如果没有合适的工具,请直接分析并回答用户问题
30
+
31
+ ## JSON 数据格式
32
+ \`\`\`json
33
+ {
34
+ "id": "工具的id",
35
+ "payload": {
36
+ // 工具所需的参数(如果需要)
37
+ // 例如: "id": "xxx", "name": "xxx"
38
+ }
39
+ }
40
+ \`\`\`
41
+
42
+ 注意:
43
+ - payload 中包含工具执行所需的所有参数
44
+ - 如果工具不需要参数,payload 可以为空对象 {}
45
+ - 确保返回的 id 与上述工具列表中的工具名称完全匹配`
46
+
47
+ this.prompt = _prompt;
48
+ return _prompt;
49
+ }
50
+ recreate() {
51
+ this.prompt = '';
52
+ }
53
+ getChatPrompt() {
54
+ const prompt = this.prefix();
55
+ return prompt;
56
+ }
57
+ }
package/src/route.ts CHANGED
@@ -5,6 +5,11 @@ import { listenProcess } from './utils/listen-process.ts';
5
5
 
6
6
  export type RouterContextT = { code?: number;[key: string]: any };
7
7
  export type RouteContext<T = { code?: number }, S = any> = {
8
+ /**
9
+ * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
10
+ * 或者不需要登录的,直接调用
11
+ */
12
+ appId?: string;
8
13
  // run first
9
14
  query?: { [key: string]: any };
10
15
  // response body
@@ -217,6 +222,7 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
217
222
  }
218
223
 
219
224
  export class QueryRouter {
225
+ appId: string = '';
220
226
  routes: Route[];
221
227
  maxNextRoute = 40;
222
228
  context?: RouteContext = {}; // default context for call
@@ -555,6 +561,21 @@ export class QueryRouter {
555
561
  hasRoute(path: string, key: string = '') {
556
562
  return this.routes.find((r) => r.path === path && r.key === key);
557
563
  }
564
+ findRoute(opts?: { path?: string; key?: string; id?: string }) {
565
+ const { path, key, id } = opts || {};
566
+ return this.routes.find((r) => {
567
+ if (id) {
568
+ return r.id === id;
569
+ }
570
+ if (path) {
571
+ if (key !== undefined) {
572
+ return r.path === path && r.key === key;
573
+ }
574
+ return r.path === path;
575
+ }
576
+ return false;
577
+ });
578
+ }
558
579
  createRouteList(force: boolean = false, filter?: (route: Route) => boolean) {
559
580
  const hasListRoute = this.hasRoute('router', 'list');
560
581
  if (!hasListRoute || force) {
@@ -562,7 +583,7 @@ export class QueryRouter {
562
583
  description: '列出当前应用下的所有的路由信息',
563
584
  run: async (ctx: RouteContext) => {
564
585
  const list = this.getList(filter);
565
- ctx.body = list;
586
+ ctx.body = { list };
566
587
  },
567
588
  });
568
589
  this.add(listRoute);
@@ -594,6 +615,7 @@ export class QueryRouter {
594
615
  type QueryRouterServerOpts = {
595
616
  handleFn?: HandleFn;
596
617
  context?: RouteContext;
618
+ appId?: string;
597
619
  };
598
620
  interface HandleFn<T = any> {
599
621
  (msg: { path: string;[key: string]: any }, ctx?: any): { code: string; data?: any; message?: string;[key: string]: any };
@@ -604,11 +626,17 @@ interface HandleFn<T = any> {
604
626
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
605
627
  */
606
628
  export class QueryRouterServer extends QueryRouter {
629
+ declare appId: string;
607
630
  handle: any;
608
631
  constructor(opts?: QueryRouterServerOpts) {
609
632
  super();
610
633
  this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
611
634
  this.setContext({ needSerialize: false, ...opts?.context });
635
+ if (opts?.appId) {
636
+ this.appId = opts.appId;
637
+ } else {
638
+ this.appId = nanoid(16);
639
+ }
612
640
  }
613
641
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
614
642
  this.handle = this.getHandle(this, wrapperFn, ctx);
@@ -3,7 +3,7 @@ import { handleServer } from './handle-server.ts';
3
3
  import * as cookie from './cookie.ts';
4
4
  import { ServerType, Listener, OnListener, ServerOpts, OnWebSocketOptions, OnWebSocketFn, WebSocketListenerFun, ListenerFun, HttpListenerFun, WS } from './server-type.ts';
5
5
  import { parseIfJson } from '../utils/parse.ts';
6
- import { EventEmitter } from 'events';
6
+ import { EventEmitter } from 'eventemitter3';
7
7
  type CookieFn = (name: string, value: string, options?: cookie.SerializeOptions, end?: boolean) => void;
8
8
 
9
9
  export type HandleCtx = {
@@ -273,6 +273,10 @@ export class ServerBase implements ServerType {
273
273
  end({ code: 500, message: `${type} server is error` });
274
274
  }
275
275
  }
276
+ /**
277
+ * 根据emitter提醒关闭ws连接
278
+ * @param ws
279
+ */
276
280
  async onWsClose(ws: WS) {
277
281
  const id = ws?.data?.id || '';
278
282
  if (id) {
@@ -253,6 +253,7 @@ export class BunServer extends ServerBase implements ServerType {
253
253
  },
254
254
  close: (ws: any) => {
255
255
  // WebSocket 连接关闭
256
+ ws?.data?.close?.();
256
257
  this.onWsClose(ws);
257
258
  },
258
259
  },
@@ -1,4 +1,4 @@
1
- import EventEmitter from 'node:events';
1
+ import { EventEmitter } from 'eventemitter3';
2
2
  import * as http from 'node:http';
3
3
 
4
4
 
@@ -2,7 +2,6 @@
2
2
  import { WebSocketServer } from 'ws';
3
3
  import type { WebSocket } from 'ws';
4
4
  import { ServerType } from './server-type.ts'
5
- import { parseIfJson } from '../utils/parse.ts';
6
5
  import { isBun } from '../utils/is-engine.ts';
7
6
 
8
7
 
@@ -62,6 +61,8 @@ export class WsServerBase {
62
61
  });
63
62
  this.server.sendConnected(ws);
64
63
  this.wss.on('close', () => {
64
+ // @ts-ignore
65
+ ws?.data?.close?.();
65
66
  this.server.onWsClose(ws);
66
67
  });
67
68
  });
package/src/test/chat.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { App } from '../app.ts'
2
- import { RouterChat } from '@/chat.ts';
2
+ import { RouterChat } from '@/modules/chat.ts';
3
3
 
4
4
  const app = new App();
5
5
 
package/auto.ts DELETED
@@ -1,19 +0,0 @@
1
- import { loadTS, getMatchFiles } from './src/auto/load-ts.ts';
2
- import { listenSocket } from './src/auto/listen-sock.ts';
3
- import { Route, QueryRouter, QueryRouterServer } from './src/route.ts';
4
-
5
- export { Route, QueryRouter, QueryRouterServer };
6
-
7
- export const App = QueryRouterServer;
8
-
9
- export { createSchema } from './src/validator/index.ts';
10
- export type { Rule } from './src/validator/rule.ts';
11
- export type { RouteContext, RouteOpts } from './src/route.ts';
12
-
13
- export type { Run } from './src/route.ts';
14
-
15
- export { CustomError } from './src/result/error.ts';
16
-
17
- export { listenSocket, loadTS, getMatchFiles };
18
-
19
- export { autoCall } from './src/auto/call-sock.ts';
@@ -1,16 +0,0 @@
1
- type Attributes = {
2
- name: string;
3
- value: string;
4
- };
5
- type AltNames = {
6
- type: number;
7
- value?: string;
8
- ip?: string;
9
- };
10
- declare const createCert: (attrs?: Attributes[], altNames?: AltNames[]) => {
11
- key: any;
12
- cert: any;
13
- };
14
-
15
- export { createCert };
16
- export type { AltNames, Attributes };