@kevisual/router 0.0.30 → 0.0.32
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 +4 -2
- package/dist/router-browser.js +13 -2
- package/dist/router.d.ts +6 -2
- package/dist/router.js +24 -2
- package/package.json +8 -8
- package/src/app.ts +14 -1
- package/src/chat.ts +40 -0
- package/src/route.ts +32 -3
- package/src/test/chat.ts +17 -0
package/dist/router-browser.d.ts
CHANGED
|
@@ -157,7 +157,7 @@ type RouteOpts = {
|
|
|
157
157
|
isDebug?: boolean;
|
|
158
158
|
};
|
|
159
159
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
|
160
|
-
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
|
|
160
|
+
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware", "metadata"];
|
|
161
161
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
162
162
|
declare class Route<U = {
|
|
163
163
|
[key: string]: any;
|
|
@@ -193,7 +193,7 @@ declare class Route<U = {
|
|
|
193
193
|
* 是否开启debug,开启后会打印错误信息
|
|
194
194
|
*/
|
|
195
195
|
isDebug?: boolean;
|
|
196
|
-
constructor(path
|
|
196
|
+
constructor(path?: string, key?: string, opts?: RouteOpts);
|
|
197
197
|
private createSchema;
|
|
198
198
|
/**
|
|
199
199
|
* set validator and create schema
|
|
@@ -413,6 +413,8 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
413
413
|
route(path: string, key?: string): Route<Required<RouteContext>>;
|
|
414
414
|
route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
|
|
415
415
|
route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
|
|
416
|
+
prompt(description: string): Route<Required<RouteContext>>;
|
|
417
|
+
prompt(description: Function): Route<Required<RouteContext>>;
|
|
416
418
|
/**
|
|
417
419
|
* 等于queryRoute,但是调用了handle
|
|
418
420
|
* @param param0
|
package/dist/router-browser.js
CHANGED
|
@@ -4788,7 +4788,7 @@ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 10
|
|
|
4788
4788
|
}
|
|
4789
4789
|
};
|
|
4790
4790
|
|
|
4791
|
-
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
|
|
4791
|
+
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'];
|
|
4792
4792
|
class Route {
|
|
4793
4793
|
/**
|
|
4794
4794
|
* 一级路径
|
|
@@ -4817,7 +4817,7 @@ class Route {
|
|
|
4817
4817
|
* 是否开启debug,开启后会打印错误信息
|
|
4818
4818
|
*/
|
|
4819
4819
|
isDebug;
|
|
4820
|
-
constructor(path, key = '', opts) {
|
|
4820
|
+
constructor(path = '', key = '', opts) {
|
|
4821
4821
|
if (!path) {
|
|
4822
4822
|
path = nanoid$1(8);
|
|
4823
4823
|
}
|
|
@@ -5445,6 +5445,17 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5445
5445
|
}
|
|
5446
5446
|
return new Route(path, key, opts);
|
|
5447
5447
|
}
|
|
5448
|
+
prompt(...args) {
|
|
5449
|
+
const [desc] = args;
|
|
5450
|
+
let description = '';
|
|
5451
|
+
if (typeof desc === 'string') {
|
|
5452
|
+
description = desc;
|
|
5453
|
+
}
|
|
5454
|
+
else if (typeof desc === 'function') {
|
|
5455
|
+
description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
5456
|
+
}
|
|
5457
|
+
return new Route('', '', { description });
|
|
5458
|
+
}
|
|
5448
5459
|
/**
|
|
5449
5460
|
* 等于queryRoute,但是调用了handle
|
|
5450
5461
|
* @param param0
|
package/dist/router.d.ts
CHANGED
|
@@ -161,7 +161,7 @@ type RouteOpts = {
|
|
|
161
161
|
isDebug?: boolean;
|
|
162
162
|
};
|
|
163
163
|
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
|
164
|
-
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
|
|
164
|
+
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware", "metadata"];
|
|
165
165
|
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
166
166
|
declare class Route<U = {
|
|
167
167
|
[key: string]: any;
|
|
@@ -197,7 +197,7 @@ declare class Route<U = {
|
|
|
197
197
|
* 是否开启debug,开启后会打印错误信息
|
|
198
198
|
*/
|
|
199
199
|
isDebug?: boolean;
|
|
200
|
-
constructor(path
|
|
200
|
+
constructor(path?: string, key?: string, opts?: RouteOpts);
|
|
201
201
|
private createSchema;
|
|
202
202
|
/**
|
|
203
203
|
* set validator and create schema
|
|
@@ -417,6 +417,8 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
417
417
|
route(path: string, key?: string): Route<Required<RouteContext>>;
|
|
418
418
|
route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
|
|
419
419
|
route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
|
|
420
|
+
prompt(description: string): Route<Required<RouteContext>>;
|
|
421
|
+
prompt(description: Function): Route<Required<RouteContext>>;
|
|
420
422
|
/**
|
|
421
423
|
* 等于queryRoute,但是调用了handle
|
|
422
424
|
* @param param0
|
|
@@ -723,6 +725,8 @@ declare class App<T = {}, U = AppReqRes> {
|
|
|
723
725
|
route(path: string, key?: string): Route<U>;
|
|
724
726
|
route(path: string, opts?: RouteOpts): Route<U>;
|
|
725
727
|
route(path: string, key?: string, opts?: RouteOpts): Route<U>;
|
|
728
|
+
prompt(description: string): Route<Required<RouteContext>>;
|
|
729
|
+
prompt(description: Function): Route<Required<RouteContext>>;
|
|
726
730
|
call(message: {
|
|
727
731
|
id?: string;
|
|
728
732
|
path?: string;
|
package/dist/router.js
CHANGED
|
@@ -4810,7 +4810,7 @@ const listenProcess = async ({ app, emitter, params, timeout = 10 * 60 * 60 * 10
|
|
|
4810
4810
|
}
|
|
4811
4811
|
};
|
|
4812
4812
|
|
|
4813
|
-
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'];
|
|
4813
|
+
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'];
|
|
4814
4814
|
class Route {
|
|
4815
4815
|
/**
|
|
4816
4816
|
* 一级路径
|
|
@@ -4839,7 +4839,7 @@ class Route {
|
|
|
4839
4839
|
* 是否开启debug,开启后会打印错误信息
|
|
4840
4840
|
*/
|
|
4841
4841
|
isDebug;
|
|
4842
|
-
constructor(path, key = '', opts) {
|
|
4842
|
+
constructor(path = '', key = '', opts) {
|
|
4843
4843
|
if (!path) {
|
|
4844
4844
|
path = nanoid$1(8);
|
|
4845
4845
|
}
|
|
@@ -5467,6 +5467,17 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5467
5467
|
}
|
|
5468
5468
|
return new Route(path, key, opts);
|
|
5469
5469
|
}
|
|
5470
|
+
prompt(...args) {
|
|
5471
|
+
const [desc] = args;
|
|
5472
|
+
let description = '';
|
|
5473
|
+
if (typeof desc === 'string') {
|
|
5474
|
+
description = desc;
|
|
5475
|
+
}
|
|
5476
|
+
else if (typeof desc === 'function') {
|
|
5477
|
+
description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
5478
|
+
}
|
|
5479
|
+
return new Route('', '', { description });
|
|
5480
|
+
}
|
|
5470
5481
|
/**
|
|
5471
5482
|
* 等于queryRoute,但是调用了handle
|
|
5472
5483
|
* @param param0
|
|
@@ -11371,6 +11382,17 @@ class App {
|
|
|
11371
11382
|
}
|
|
11372
11383
|
return new Route(path, key, opts);
|
|
11373
11384
|
}
|
|
11385
|
+
prompt(...args) {
|
|
11386
|
+
const [desc] = args;
|
|
11387
|
+
let description = '';
|
|
11388
|
+
if (typeof desc === 'string') {
|
|
11389
|
+
description = desc;
|
|
11390
|
+
}
|
|
11391
|
+
else if (typeof desc === 'function') {
|
|
11392
|
+
description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
11393
|
+
}
|
|
11394
|
+
return new Route('', '', { description });
|
|
11395
|
+
}
|
|
11374
11396
|
async call(message, ctx) {
|
|
11375
11397
|
const router = this.router;
|
|
11376
11398
|
return await router.call(message, ctx);
|
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.32",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -23,19 +23,19 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@kevisual/local-proxy": "^0.0.6",
|
|
25
25
|
"@kevisual/query": "^0.0.29",
|
|
26
|
-
"@rollup/plugin-alias": "^
|
|
27
|
-
"@rollup/plugin-commonjs": "
|
|
26
|
+
"@rollup/plugin-alias": "^6.0.0",
|
|
27
|
+
"@rollup/plugin-commonjs": "29.0.0",
|
|
28
28
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
29
|
-
"@rollup/plugin-typescript": "^12.
|
|
29
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
30
30
|
"@types/lodash-es": "^4.17.12",
|
|
31
|
-
"@types/node": "^24.
|
|
32
|
-
"@types/send": "^1.2.
|
|
31
|
+
"@types/node": "^24.10.1",
|
|
32
|
+
"@types/send": "^1.2.1",
|
|
33
33
|
"@types/ws": "^8.18.1",
|
|
34
34
|
"@types/xml2js": "^0.4.14",
|
|
35
35
|
"cookie": "^1.0.2",
|
|
36
36
|
"lodash-es": "^4.17.21",
|
|
37
37
|
"nanoid": "^5.1.6",
|
|
38
|
-
"rollup": "^4.
|
|
38
|
+
"rollup": "^4.53.2",
|
|
39
39
|
"rollup-plugin-dts": "^6.2.3",
|
|
40
40
|
"ts-loader": "^9.5.4",
|
|
41
41
|
"ts-node": "^10.9.2",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"path-to-regexp": "^8.3.0",
|
|
54
|
-
"selfsigned": "^
|
|
54
|
+
"selfsigned": "^4.0.0",
|
|
55
55
|
"send": "^1.2.0"
|
|
56
56
|
},
|
|
57
57
|
"publishConfig": {
|
package/src/app.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { CustomError } from './result/error.ts';
|
|
|
5
5
|
import { handleServer } from './server/handle-server.ts';
|
|
6
6
|
import { IncomingMessage, ServerResponse } from 'http';
|
|
7
7
|
|
|
8
|
-
type RouterHandle = (msg: { path: string;
|
|
8
|
+
type RouterHandle = (msg: { path: string;[key: string]: any }) => { code: string; data?: any; message?: string;[key: string]: any };
|
|
9
9
|
type AppOptions<T = {}> = {
|
|
10
10
|
router?: QueryRouter;
|
|
11
11
|
server?: Server;
|
|
@@ -82,6 +82,19 @@ export class App<T = {}, U = AppReqRes> {
|
|
|
82
82
|
}
|
|
83
83
|
return new Route(path, key, opts);
|
|
84
84
|
}
|
|
85
|
+
prompt(description: string): Route<Required<RouteContext>>;
|
|
86
|
+
prompt(description: Function): Route<Required<RouteContext>>;
|
|
87
|
+
prompt(...args: any[]) {
|
|
88
|
+
const [desc] = args;
|
|
89
|
+
let description = ''
|
|
90
|
+
if (typeof desc === 'string') {
|
|
91
|
+
description = desc;
|
|
92
|
+
} else if (typeof desc === 'function') {
|
|
93
|
+
description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
94
|
+
}
|
|
95
|
+
return new Route('', '', { description });
|
|
96
|
+
}
|
|
97
|
+
|
|
85
98
|
async call(message: { id?: string, path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
86
99
|
const router = this.router;
|
|
87
100
|
return await router.call(message, ctx);
|
package/src/chat.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { QueryRouter } from "./route.ts";
|
|
2
|
+
|
|
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(wrapperFn?: (routes: any[]) => string) {
|
|
13
|
+
if (this.prompt) {
|
|
14
|
+
return this.prompt;
|
|
15
|
+
}
|
|
16
|
+
let _prompt = `你是一个调用函数工具的助手,当用户询问时,如果拥有工具,请返回 JSON 数据,数据的值的内容是 id 和 payload 。如果有参数,请放到 payload 当中。
|
|
17
|
+
|
|
18
|
+
下面是你可以使用的工具列表:
|
|
19
|
+
|
|
20
|
+
`;
|
|
21
|
+
if (!wrapperFn) {
|
|
22
|
+
_prompt += this.router.routes.map(r => `工具名称: ${r.id}\n描述: ${r.description}\n`).join('\n');
|
|
23
|
+
} else {
|
|
24
|
+
_prompt += wrapperFn(this.router.exportRoutes());
|
|
25
|
+
}
|
|
26
|
+
_prompt += `当你需要使用工具时,请严格按照以下格式返回:
|
|
27
|
+
{
|
|
28
|
+
"id": "工具名称",
|
|
29
|
+
"payload": {
|
|
30
|
+
// 参数列表
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
如果你不需要使用工具,直接返回用户想要的内容即可,不要返回任何多余的信息。`;
|
|
34
|
+
return _prompt;
|
|
35
|
+
}
|
|
36
|
+
chat() {
|
|
37
|
+
const prompt = this.prefix();
|
|
38
|
+
return prompt;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/route.ts
CHANGED
|
@@ -103,7 +103,7 @@ export type RouteOpts = {
|
|
|
103
103
|
isDebug?: boolean;
|
|
104
104
|
};
|
|
105
105
|
export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
|
106
|
-
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware'] as const;
|
|
106
|
+
const pickValue = ['path', 'key', 'id', 'description', 'type', 'validator', 'middleware', 'metadata'] as const;
|
|
107
107
|
export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
108
108
|
export class Route<U = { [key: string]: any }> {
|
|
109
109
|
/**
|
|
@@ -133,7 +133,7 @@ export class Route<U = { [key: string]: any }> {
|
|
|
133
133
|
* 是否开启debug,开启后会打印错误信息
|
|
134
134
|
*/
|
|
135
135
|
isDebug?: boolean;
|
|
136
|
-
constructor(path: string, key: string = '', opts?: RouteOpts) {
|
|
136
|
+
constructor(path: string = '', key: string = '', opts?: RouteOpts) {
|
|
137
137
|
if (!path) {
|
|
138
138
|
path = nanoid(8)
|
|
139
139
|
}
|
|
@@ -702,6 +702,19 @@ export class QueryRouter {
|
|
|
702
702
|
hasRoute(path: string, key: string = '') {
|
|
703
703
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
704
704
|
}
|
|
705
|
+
createRouteList(force: boolean = false) {
|
|
706
|
+
const hasListRoute = this.hasRoute('route', 'list');
|
|
707
|
+
if (!hasListRoute || force) {
|
|
708
|
+
const listRoute = new Route('route', 'list', {
|
|
709
|
+
description: '列出当前应用下的所有的路由信息',
|
|
710
|
+
run: async (ctx: RouteContext) => {
|
|
711
|
+
const list = this.getList();
|
|
712
|
+
ctx.body = list;
|
|
713
|
+
},
|
|
714
|
+
});
|
|
715
|
+
this.add(listRoute);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
705
718
|
/**
|
|
706
719
|
* 等待程序运行, 获取到message的数据,就执行
|
|
707
720
|
*
|
|
@@ -710,7 +723,11 @@ export class QueryRouter {
|
|
|
710
723
|
* -- .on
|
|
711
724
|
* -- .send
|
|
712
725
|
*/
|
|
713
|
-
wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number }) {
|
|
726
|
+
wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number, getList?: boolean }) {
|
|
727
|
+
const getList = opts?.getList ?? true;
|
|
728
|
+
if (getList) {
|
|
729
|
+
this.createRouteList();
|
|
730
|
+
}
|
|
714
731
|
return listenProcess({ app: this, params, ...opts });
|
|
715
732
|
}
|
|
716
733
|
}
|
|
@@ -767,6 +784,18 @@ export class QueryRouterServer extends QueryRouter {
|
|
|
767
784
|
}
|
|
768
785
|
return new Route(path, key, opts);
|
|
769
786
|
}
|
|
787
|
+
prompt(description: string): Route<Required<RouteContext>>;
|
|
788
|
+
prompt(description: Function): Route<Required<RouteContext>>;
|
|
789
|
+
prompt(...args: any[]) {
|
|
790
|
+
const [desc] = args;
|
|
791
|
+
let description = ''
|
|
792
|
+
if (typeof desc === 'string') {
|
|
793
|
+
description = desc;
|
|
794
|
+
} else if (typeof desc === 'function') {
|
|
795
|
+
description = desc() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
796
|
+
}
|
|
797
|
+
return new Route('', '', { description });
|
|
798
|
+
}
|
|
770
799
|
|
|
771
800
|
/**
|
|
772
801
|
* 等于queryRoute,但是调用了handle
|
package/src/test/chat.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { App } from '../app.ts'
|
|
2
|
+
import { RouterChat } from '@/chat.ts';
|
|
3
|
+
|
|
4
|
+
const app = new App();
|
|
5
|
+
|
|
6
|
+
app.prompt(`获取时间的工具`).define(async (ctx) => {
|
|
7
|
+
ctx.body = '123'
|
|
8
|
+
}).addTo(app);
|
|
9
|
+
|
|
10
|
+
app.prompt('获取天气的工具。\n参数是 city 为对应的城市').define(async (ctx) => {
|
|
11
|
+
ctx.body = '晴天'
|
|
12
|
+
}).addTo(app);
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
export const chat = new RouterChat({ router: app.router });
|
|
16
|
+
|
|
17
|
+
console.log(chat.chat());
|