@kevisual/router 0.0.52 → 0.0.54
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/dist/router-browser.d.ts +25 -14
- package/dist/router-browser.js +330 -166
- package/dist/router.d.ts +167 -166
- package/dist/router.js +10680 -10369
- package/package.json +18 -20
- package/src/app.ts +9 -0
- package/src/auto/index.ts +19 -0
- package/src/browser.ts +3 -3
- package/src/index.ts +13 -13
- package/src/modules/chat.ts +57 -0
- package/src/{connect.ts → modules/connect.ts} +1 -1
- package/src/route.ts +46 -0
- package/src/test/chat.ts +1 -1
- package/auto.ts +0 -19
- package/dist/router-sign.d.ts +0 -16
- package/dist/router-sign.js +0 -14052
- package/src/chat.ts +0 -40
- package/src/io.ts +0 -6
- package/src/router-simple-lib.ts +0 -3
- package/src/sign.ts +0 -59
- package/src/static.ts +0 -97
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.
|
|
4
|
+
"version": "0.0.54",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -21,41 +21,40 @@
|
|
|
21
21
|
"keywords": [],
|
|
22
22
|
"author": "abearxiong",
|
|
23
23
|
"license": "MIT",
|
|
24
|
-
"packageManager": "pnpm@10.
|
|
24
|
+
"packageManager": "pnpm@10.28.0",
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@kevisual/local-proxy": "^0.0.8",
|
|
27
|
-
"@kevisual/query": "^0.0.
|
|
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
|
-
"@types/bun": "^1.3.
|
|
33
|
-
"@types/node": "^25.0.
|
|
32
|
+
"@types/bun": "^1.3.6",
|
|
33
|
+
"@types/node": "^25.0.8",
|
|
34
34
|
"@types/send": "^1.2.1",
|
|
35
|
-
"@types/xml2js": "^0.4.14",
|
|
36
35
|
"@types/ws": "^8.18.1",
|
|
36
|
+
"@types/xml2js": "^0.4.14",
|
|
37
|
+
"eventemitter3": "^5.0.1",
|
|
37
38
|
"nanoid": "^5.1.6",
|
|
38
|
-
"rollup": "^4.
|
|
39
|
+
"rollup": "^4.55.1",
|
|
39
40
|
"rollup-plugin-dts": "^6.3.0",
|
|
40
41
|
"ts-loader": "^9.5.4",
|
|
41
42
|
"ts-node": "^10.9.2",
|
|
42
43
|
"tslib": "^2.8.1",
|
|
43
44
|
"tsx": "^4.21.0",
|
|
44
45
|
"typescript": "^5.9.3",
|
|
46
|
+
"ws": "npm:@kevisual/ws",
|
|
45
47
|
"xml2js": "^0.6.2",
|
|
46
|
-
"zod": "^4.
|
|
47
|
-
"
|
|
48
|
+
"zod": "^4.3.5",
|
|
49
|
+
"@kevisual/js-filter": "^0.0.4",
|
|
50
|
+
"path-to-regexp": "^8.3.0",
|
|
51
|
+
"send": "^1.2.1"
|
|
48
52
|
},
|
|
49
53
|
"repository": {
|
|
50
54
|
"type": "git",
|
|
51
55
|
"url": "git+https://github.com/abearxiong/kevisual-router.git"
|
|
52
56
|
},
|
|
53
|
-
"dependencies": {
|
|
54
|
-
"eventemitter3": "^5.0.1",
|
|
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/browser.ts
CHANGED
|
@@ -6,10 +6,10 @@ export { createSchema } from './validator/index.ts';
|
|
|
6
6
|
|
|
7
7
|
export type { RouteContext, RouteOpts } from './route.ts';
|
|
8
8
|
|
|
9
|
-
export type { Run } from './route.ts';
|
|
9
|
+
export type { Run, Skill } from './route.ts';
|
|
10
10
|
|
|
11
|
-
export {
|
|
11
|
+
export { createSkill } from './route.ts';
|
|
12
12
|
|
|
13
|
-
export
|
|
13
|
+
export { CustomError } from './result/error.ts';
|
|
14
14
|
|
|
15
15
|
export * from './router-define.ts';
|
package/src/index.ts
CHANGED
|
@@ -1,26 +1,24 @@
|
|
|
1
1
|
export { Route, QueryRouter, QueryRouterServer, Mini } from './route.ts';
|
|
2
|
-
export { Connect, QueryConnect } from './connect.ts';
|
|
3
2
|
|
|
4
|
-
export type {
|
|
3
|
+
export type { Rule, Schema, } from './validator/index.ts';
|
|
5
4
|
|
|
6
|
-
export
|
|
5
|
+
export { createSchema } from './validator/index.ts';
|
|
7
6
|
|
|
8
|
-
export {
|
|
9
|
-
/**
|
|
10
|
-
* 自定义错误
|
|
11
|
-
*/
|
|
12
|
-
export { CustomError } from './result/error.ts';
|
|
7
|
+
export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
|
|
13
8
|
|
|
14
|
-
export {
|
|
9
|
+
export type { Run, Skill } from './route.ts';
|
|
15
10
|
|
|
16
|
-
export
|
|
11
|
+
export { createSkill } from './route.ts';
|
|
17
12
|
|
|
18
|
-
export {
|
|
13
|
+
export { CustomError } from './result/error.ts';
|
|
19
14
|
|
|
20
15
|
export * from './router-define.ts';
|
|
21
16
|
|
|
17
|
+
export { ServerNode, handleServer } from './server/index.ts';
|
|
22
18
|
|
|
23
|
-
export {
|
|
19
|
+
export { App } from './app.ts';
|
|
20
|
+
|
|
21
|
+
export type {
|
|
24
22
|
RouterReq,
|
|
25
23
|
RouterRes,
|
|
26
24
|
OnWebSocketFn,
|
|
@@ -31,4 +29,6 @@ export {
|
|
|
31
29
|
WebSocketListenerFun,
|
|
32
30
|
HttpListenerFun,
|
|
33
31
|
OnListener,
|
|
34
|
-
} from './server/server-type.ts';
|
|
32
|
+
} from './server/server-type.ts';
|
|
33
|
+
|
|
34
|
+
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
|
@@ -2,9 +2,16 @@ import { nanoid } from 'nanoid';
|
|
|
2
2
|
import { CustomError } from './result/error.ts';
|
|
3
3
|
import { pick } from './utils/pick.ts';
|
|
4
4
|
import { listenProcess } from './utils/listen-process.ts';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { filter } from '@kevisual/js-filter'
|
|
5
7
|
|
|
6
8
|
export type RouterContextT = { code?: number;[key: string]: any };
|
|
7
9
|
export type RouteContext<T = { code?: number }, S = any> = {
|
|
10
|
+
/**
|
|
11
|
+
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
12
|
+
* 或者不需要登录的,直接调用
|
|
13
|
+
*/
|
|
14
|
+
appId?: string;
|
|
8
15
|
// run first
|
|
9
16
|
query?: { [key: string]: any };
|
|
10
17
|
// response body
|
|
@@ -85,6 +92,22 @@ export type RouteOpts<U = {}, T = SimpleObject> = {
|
|
|
85
92
|
};
|
|
86
93
|
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|
|
87
94
|
const pickValue = ['path', 'key', 'id', 'description', 'type', 'middleware', 'metadata'] as const;
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
export type Skill<T = SimpleObject> = {
|
|
98
|
+
skill: string;
|
|
99
|
+
title: string;
|
|
100
|
+
summary?: string;
|
|
101
|
+
args?: z.ZodTypeAny;
|
|
102
|
+
} & T
|
|
103
|
+
/** */
|
|
104
|
+
export const createSkill = <T = SimpleObject>(skill: Skill<T>): Skill<T> => {
|
|
105
|
+
return {
|
|
106
|
+
args: {},
|
|
107
|
+
...skill
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
88
111
|
export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
89
112
|
export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleObject> {
|
|
90
113
|
/**
|
|
@@ -217,6 +240,7 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
|
|
217
240
|
}
|
|
218
241
|
|
|
219
242
|
export class QueryRouter {
|
|
243
|
+
appId: string = '';
|
|
220
244
|
routes: Route[];
|
|
221
245
|
maxNextRoute = 40;
|
|
222
246
|
context?: RouteContext = {}; // default context for call
|
|
@@ -555,6 +579,21 @@ export class QueryRouter {
|
|
|
555
579
|
hasRoute(path: string, key: string = '') {
|
|
556
580
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
557
581
|
}
|
|
582
|
+
findRoute(opts?: { path?: string; key?: string; id?: string }) {
|
|
583
|
+
const { path, key, id } = opts || {};
|
|
584
|
+
return this.routes.find((r) => {
|
|
585
|
+
if (id) {
|
|
586
|
+
return r.id === id;
|
|
587
|
+
}
|
|
588
|
+
if (path) {
|
|
589
|
+
if (key !== undefined) {
|
|
590
|
+
return r.path === path && r.key === key;
|
|
591
|
+
}
|
|
592
|
+
return r.path === path;
|
|
593
|
+
}
|
|
594
|
+
return false;
|
|
595
|
+
});
|
|
596
|
+
}
|
|
558
597
|
createRouteList(force: boolean = false, filter?: (route: Route) => boolean) {
|
|
559
598
|
const hasListRoute = this.hasRoute('router', 'list');
|
|
560
599
|
if (!hasListRoute || force) {
|
|
@@ -594,6 +633,7 @@ export class QueryRouter {
|
|
|
594
633
|
type QueryRouterServerOpts = {
|
|
595
634
|
handleFn?: HandleFn;
|
|
596
635
|
context?: RouteContext;
|
|
636
|
+
appId?: string;
|
|
597
637
|
};
|
|
598
638
|
interface HandleFn<T = any> {
|
|
599
639
|
(msg: { path: string;[key: string]: any }, ctx?: any): { code: string; data?: any; message?: string;[key: string]: any };
|
|
@@ -604,11 +644,17 @@ interface HandleFn<T = any> {
|
|
|
604
644
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
605
645
|
*/
|
|
606
646
|
export class QueryRouterServer extends QueryRouter {
|
|
647
|
+
declare appId: string;
|
|
607
648
|
handle: any;
|
|
608
649
|
constructor(opts?: QueryRouterServerOpts) {
|
|
609
650
|
super();
|
|
610
651
|
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
611
652
|
this.setContext({ needSerialize: false, ...opts?.context });
|
|
653
|
+
if (opts?.appId) {
|
|
654
|
+
this.appId = opts.appId;
|
|
655
|
+
} else {
|
|
656
|
+
this.appId = nanoid(16);
|
|
657
|
+
}
|
|
612
658
|
}
|
|
613
659
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
|
|
614
660
|
this.handle = this.getHandle(this, wrapperFn, ctx);
|
package/src/test/chat.ts
CHANGED
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';
|
package/dist/router-sign.d.ts
DELETED
|
@@ -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 };
|