@kevisual/router 0.0.82 → 0.0.84
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/agent/routes/index.ts +16 -10
- package/agent/routes/route-create.ts +3 -3
- package/dist/app.js +98 -69
- package/dist/opencode.d.ts +89 -86
- package/dist/opencode.js +10 -5
- package/dist/router-browser.d.ts +87 -55
- package/dist/router-browser.js +26 -24
- package/dist/router-define.d.ts +84 -55
- package/dist/router.d.ts +92 -86
- package/dist/router.js +28 -62
- package/dist/ws.d.ts +112 -73
- package/package.json +2 -2
- package/readme.md +78 -63
- package/src/app.ts +7 -50
- package/src/opencode.ts +12 -5
- package/src/result/error.ts +22 -0
- package/src/route.ts +92 -78
- package/src/test/app-type.ts +66 -10
- package/src/test/chat.ts +1 -1
- package/src/test/route-ts.ts +15 -0
package/agent/routes/index.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { app } from '../app.ts'
|
|
2
2
|
import './route-create.ts'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}).addTo(app);
|
|
13
|
-
}
|
|
4
|
+
app.route({
|
|
5
|
+
path: 'auth',
|
|
6
|
+
key: '',
|
|
7
|
+
id: 'auth',
|
|
8
|
+
description: '身份验证路由',
|
|
9
|
+
}).define(async (ctx) => {
|
|
10
|
+
//
|
|
11
|
+
}).addTo(app, { overwrite: false });
|
|
14
12
|
|
|
13
|
+
app.route({
|
|
14
|
+
path: 'auth-admin',
|
|
15
|
+
key: '',
|
|
16
|
+
id: 'auth-admin',
|
|
17
|
+
description: '管理员身份验证路由',
|
|
18
|
+
}).define(async (ctx) => {
|
|
19
|
+
//
|
|
20
|
+
}).addTo(app, { overwrite: false });
|
|
@@ -6,7 +6,7 @@ app.route({
|
|
|
6
6
|
path: 'router-skill',
|
|
7
7
|
key: 'create-route',
|
|
8
8
|
description: '创建路由技能',
|
|
9
|
-
middleware: ['auth'],
|
|
9
|
+
middleware: ['auth-admin'],
|
|
10
10
|
metadata: {
|
|
11
11
|
tags: ['opencode'],
|
|
12
12
|
...createSkill({
|
|
@@ -38,7 +38,7 @@ app.route({
|
|
|
38
38
|
path: 'router-skill',
|
|
39
39
|
key: 'version',
|
|
40
40
|
description: '获取最新router版本号',
|
|
41
|
-
middleware: ['auth'],
|
|
41
|
+
middleware: ['auth-admin'],
|
|
42
42
|
metadata: {
|
|
43
43
|
tags: ['opencode'],
|
|
44
44
|
...createSkill({
|
|
@@ -59,7 +59,7 @@ app.route({
|
|
|
59
59
|
path: 'route-skill',
|
|
60
60
|
key: 'test',
|
|
61
61
|
description: '测试路由技能',
|
|
62
|
-
middleware: ['auth'],
|
|
62
|
+
middleware: ['auth-admin'],
|
|
63
63
|
metadata: {
|
|
64
64
|
tags: ['opencode'],
|
|
65
65
|
...createSkill({
|
package/dist/app.js
CHANGED
|
@@ -3051,6 +3051,18 @@ class CustomError extends Error {
|
|
|
3051
3051
|
static isError(error) {
|
|
3052
3052
|
return error instanceof CustomError || typeof error === "object" && error !== null && "code" in error;
|
|
3053
3053
|
}
|
|
3054
|
+
static throw(...args) {
|
|
3055
|
+
const [args0, args1] = args;
|
|
3056
|
+
if (args0 && typeof args0 === "object") {
|
|
3057
|
+
throw new CustomError(args0);
|
|
3058
|
+
}
|
|
3059
|
+
if (args1 && typeof args1 === "object") {
|
|
3060
|
+
throw new CustomError(args0, args1);
|
|
3061
|
+
} else if (args1) {
|
|
3062
|
+
throw new CustomError(args0, { message: args1 });
|
|
3063
|
+
}
|
|
3064
|
+
throw new CustomError(args0);
|
|
3065
|
+
}
|
|
3054
3066
|
parse(e) {
|
|
3055
3067
|
if (e) {
|
|
3056
3068
|
return CustomError.parseError(e);
|
|
@@ -17000,7 +17012,7 @@ class Route {
|
|
|
17000
17012
|
router.add(this, opts);
|
|
17001
17013
|
}
|
|
17002
17014
|
throw(...args) {
|
|
17003
|
-
throw
|
|
17015
|
+
CustomError.throw(...args);
|
|
17004
17016
|
}
|
|
17005
17017
|
}
|
|
17006
17018
|
var toJSONSchemaRoute = (route) => {
|
|
@@ -17043,6 +17055,7 @@ class QueryRouter {
|
|
|
17043
17055
|
const maxNextRoute = this.maxNextRoute;
|
|
17044
17056
|
ctx = ctx || {};
|
|
17045
17057
|
ctx.currentPath = path;
|
|
17058
|
+
ctx.currentId = route?.id;
|
|
17046
17059
|
ctx.currentKey = key;
|
|
17047
17060
|
ctx.currentRoute = route;
|
|
17048
17061
|
ctx.index = (ctx.index || 0) + 1;
|
|
@@ -17056,7 +17069,7 @@ class QueryRouter {
|
|
|
17056
17069
|
ctx.code = 500;
|
|
17057
17070
|
ctx.message = "Too many nextRoute";
|
|
17058
17071
|
ctx.body = null;
|
|
17059
|
-
return;
|
|
17072
|
+
return ctx;
|
|
17060
17073
|
}
|
|
17061
17074
|
if (route && route.middleware && route.middleware.length > 0) {
|
|
17062
17075
|
const errorMiddleware = [];
|
|
@@ -17131,7 +17144,9 @@ class QueryRouter {
|
|
|
17131
17144
|
}
|
|
17132
17145
|
return ctx;
|
|
17133
17146
|
}
|
|
17134
|
-
if (ctx.end) {
|
|
17147
|
+
if (ctx.end) {
|
|
17148
|
+
return ctx;
|
|
17149
|
+
}
|
|
17135
17150
|
}
|
|
17136
17151
|
}
|
|
17137
17152
|
}
|
|
@@ -17298,16 +17313,7 @@ class QueryRouter {
|
|
|
17298
17313
|
this.importRoutes(router.routes);
|
|
17299
17314
|
}
|
|
17300
17315
|
throw(...args) {
|
|
17301
|
-
|
|
17302
|
-
if (args0 && typeof args0 === "object") {
|
|
17303
|
-
throw new CustomError(args0);
|
|
17304
|
-
}
|
|
17305
|
-
if (args1 && typeof args1 === "object") {
|
|
17306
|
-
throw new CustomError(args0, args1);
|
|
17307
|
-
} else if (args1) {
|
|
17308
|
-
throw new CustomError(args0, { message: args1 });
|
|
17309
|
-
}
|
|
17310
|
-
throw new CustomError(args0);
|
|
17316
|
+
CustomError.throw(...args);
|
|
17311
17317
|
}
|
|
17312
17318
|
hasRoute(path, key = "") {
|
|
17313
17319
|
return this.routes.find((r) => r.path === path && r.key === key);
|
|
@@ -17334,7 +17340,7 @@ class QueryRouter {
|
|
|
17334
17340
|
description: "列出当前应用下的所有的路由信息",
|
|
17335
17341
|
middleware: opts?.middleware || [],
|
|
17336
17342
|
run: async (ctx) => {
|
|
17337
|
-
const tokenUser = ctx.state
|
|
17343
|
+
const tokenUser = ctx.state;
|
|
17338
17344
|
let isUser = !!tokenUser;
|
|
17339
17345
|
const list = this.getList(opts?.filter).filter((item) => {
|
|
17340
17346
|
if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
|
|
@@ -17365,6 +17371,56 @@ class QueryRouter {
|
|
|
17365
17371
|
fromJSONSchema = fromJSONSchema3;
|
|
17366
17372
|
}
|
|
17367
17373
|
|
|
17374
|
+
class QueryRouterServer extends QueryRouter {
|
|
17375
|
+
handle;
|
|
17376
|
+
constructor(opts) {
|
|
17377
|
+
super();
|
|
17378
|
+
const initHandle = opts?.initHandle ?? true;
|
|
17379
|
+
if (initHandle || opts?.handleFn) {
|
|
17380
|
+
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
|
17381
|
+
}
|
|
17382
|
+
this.setContext({ needSerialize: false, ...opts?.context });
|
|
17383
|
+
if (opts?.appId) {
|
|
17384
|
+
this.appId = opts.appId;
|
|
17385
|
+
} else {
|
|
17386
|
+
this.appId = randomId(16);
|
|
17387
|
+
}
|
|
17388
|
+
}
|
|
17389
|
+
setHandle(wrapperFn, ctx) {
|
|
17390
|
+
this.handle = this.getHandle(this, wrapperFn, ctx);
|
|
17391
|
+
}
|
|
17392
|
+
addRoute(route, opts) {
|
|
17393
|
+
this.add(route, opts);
|
|
17394
|
+
}
|
|
17395
|
+
Route = Route;
|
|
17396
|
+
route(...args) {
|
|
17397
|
+
const [path, key, opts] = args;
|
|
17398
|
+
if (typeof path === "object") {
|
|
17399
|
+
return new Route(path.path, path.key, path);
|
|
17400
|
+
}
|
|
17401
|
+
if (typeof path === "string") {
|
|
17402
|
+
if (opts) {
|
|
17403
|
+
return new Route(path, key, opts);
|
|
17404
|
+
}
|
|
17405
|
+
if (key && typeof key === "object") {
|
|
17406
|
+
return new Route(path, key?.key || "", key);
|
|
17407
|
+
}
|
|
17408
|
+
return new Route(path, key);
|
|
17409
|
+
}
|
|
17410
|
+
return new Route(path, key, opts);
|
|
17411
|
+
}
|
|
17412
|
+
prompt(description) {
|
|
17413
|
+
return new Route(undefined, undefined, { description });
|
|
17414
|
+
}
|
|
17415
|
+
async run(msg, ctx) {
|
|
17416
|
+
const handle = this.handle;
|
|
17417
|
+
if (handle) {
|
|
17418
|
+
return handle(msg, ctx);
|
|
17419
|
+
}
|
|
17420
|
+
return super.run(msg, ctx);
|
|
17421
|
+
}
|
|
17422
|
+
}
|
|
17423
|
+
|
|
17368
17424
|
// src/server/server.ts
|
|
17369
17425
|
import http from "node:http";
|
|
17370
17426
|
import https from "node:https";
|
|
@@ -18254,11 +18310,11 @@ class BunServer extends ServerBase {
|
|
|
18254
18310
|
}
|
|
18255
18311
|
}
|
|
18256
18312
|
// src/app.ts
|
|
18257
|
-
class App extends
|
|
18313
|
+
class App extends QueryRouterServer {
|
|
18258
18314
|
router;
|
|
18259
18315
|
server;
|
|
18260
18316
|
constructor(opts) {
|
|
18261
|
-
super();
|
|
18317
|
+
super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
|
|
18262
18318
|
const router = this;
|
|
18263
18319
|
let server = opts?.server;
|
|
18264
18320
|
if (!server) {
|
|
@@ -18270,7 +18326,6 @@ class App extends QueryRouter {
|
|
|
18270
18326
|
}
|
|
18271
18327
|
}
|
|
18272
18328
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
|
18273
|
-
router.setContext({ needSerialize: true, ...opts?.routerContext });
|
|
18274
18329
|
this.router = router;
|
|
18275
18330
|
this.server = server;
|
|
18276
18331
|
if (opts?.appId) {
|
|
@@ -18283,42 +18338,7 @@ class App extends QueryRouter {
|
|
|
18283
18338
|
listen(...args) {
|
|
18284
18339
|
this.server.listen(...args);
|
|
18285
18340
|
}
|
|
18286
|
-
addRoute(route, opts) {
|
|
18287
|
-
super.add(route, opts);
|
|
18288
|
-
}
|
|
18289
18341
|
Route = Route;
|
|
18290
|
-
route(...args) {
|
|
18291
|
-
const [path, key, opts] = args;
|
|
18292
|
-
if (typeof path === "object") {
|
|
18293
|
-
return new Route(path.path, path.key, path);
|
|
18294
|
-
}
|
|
18295
|
-
if (typeof path === "string") {
|
|
18296
|
-
if (opts) {
|
|
18297
|
-
return new Route(path, key, opts);
|
|
18298
|
-
}
|
|
18299
|
-
if (key && typeof key === "object") {
|
|
18300
|
-
return new Route(path, key?.key || "", key);
|
|
18301
|
-
}
|
|
18302
|
-
return new Route(path, key);
|
|
18303
|
-
}
|
|
18304
|
-
return new Route(path, key, opts);
|
|
18305
|
-
}
|
|
18306
|
-
prompt(...args) {
|
|
18307
|
-
const [desc] = args;
|
|
18308
|
-
let description = "";
|
|
18309
|
-
if (typeof desc === "string") {
|
|
18310
|
-
description = desc;
|
|
18311
|
-
} else if (typeof desc === "function") {
|
|
18312
|
-
description = desc() || "";
|
|
18313
|
-
}
|
|
18314
|
-
return new Route("", "", { description });
|
|
18315
|
-
}
|
|
18316
|
-
async call(message, ctx) {
|
|
18317
|
-
return await super.call(message, ctx);
|
|
18318
|
-
}
|
|
18319
|
-
async run(msg, ctx) {
|
|
18320
|
-
return await super.run(msg, ctx);
|
|
18321
|
-
}
|
|
18322
18342
|
static handleRequest(req, res) {
|
|
18323
18343
|
return handleServer(req, res);
|
|
18324
18344
|
}
|
|
@@ -19239,7 +19259,7 @@ var addCallFn = (app2) => {
|
|
|
19239
19259
|
path: "call",
|
|
19240
19260
|
key: "",
|
|
19241
19261
|
description: "调用",
|
|
19242
|
-
middleware: ["auth"],
|
|
19262
|
+
middleware: ["auth-admin"],
|
|
19243
19263
|
metadata: {
|
|
19244
19264
|
tags: ["opencode"],
|
|
19245
19265
|
...createSkill({
|
|
@@ -19253,7 +19273,7 @@ var addCallFn = (app2) => {
|
|
|
19253
19273
|
args: {
|
|
19254
19274
|
path: tool.schema.string().describe("应用路径,例如 cnb"),
|
|
19255
19275
|
key: tool.schema.string().optional().describe("应用key,例如 list-repos"),
|
|
19256
|
-
payload: tool.schema.object({}).optional().describe("
|
|
19276
|
+
payload: tool.schema.object({}).optional().describe('调用参数, 为对象, 例如 { "query": "javascript" }')
|
|
19257
19277
|
}
|
|
19258
19278
|
})
|
|
19259
19279
|
}
|
|
@@ -19281,8 +19301,13 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
19281
19301
|
if (!router.hasRoute("call", "")) {
|
|
19282
19302
|
addCallFn(router);
|
|
19283
19303
|
}
|
|
19284
|
-
if (
|
|
19285
|
-
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router
|
|
19304
|
+
if (router) {
|
|
19305
|
+
router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
19306
|
+
overwrite: false
|
|
19307
|
+
});
|
|
19308
|
+
router.route({ path: "auth-admin", key: "", id: "auth-admin", description: "认证" }).define(async (ctx) => {}).addTo(router, {
|
|
19309
|
+
overwrite: false
|
|
19310
|
+
});
|
|
19286
19311
|
}
|
|
19287
19312
|
const _routes = filter(router.routes, opts?.query || "");
|
|
19288
19313
|
const routes = _routes.filter((r) => {
|
|
@@ -19296,7 +19321,7 @@ var createRouterAgentPluginFn = (opts) => {
|
|
|
19296
19321
|
return false;
|
|
19297
19322
|
});
|
|
19298
19323
|
const AgentPlugin = async (pluginInput) => {
|
|
19299
|
-
useContextKey("plugin-input", () => pluginInput, true);
|
|
19324
|
+
useContextKey("plugin-input", () => pluginInput, { isNew: true });
|
|
19300
19325
|
const hooks = opts?.hooks ? await opts.hooks(pluginInput) : {};
|
|
19301
19326
|
return {
|
|
19302
19327
|
...hooks,
|
|
@@ -19537,14 +19562,14 @@ app
|
|
|
19537
19562
|
10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
|
|
19538
19563
|
`;
|
|
19539
19564
|
// package.json
|
|
19540
|
-
var version2 = "0.0.
|
|
19565
|
+
var version2 = "0.0.84";
|
|
19541
19566
|
|
|
19542
19567
|
// agent/routes/route-create.ts
|
|
19543
19568
|
app.route({
|
|
19544
19569
|
path: "router-skill",
|
|
19545
19570
|
key: "create-route",
|
|
19546
19571
|
description: "创建路由技能",
|
|
19547
|
-
middleware: ["auth"],
|
|
19572
|
+
middleware: ["auth-admin"],
|
|
19548
19573
|
metadata: {
|
|
19549
19574
|
tags: ["opencode"],
|
|
19550
19575
|
...createSkill({
|
|
@@ -19578,7 +19603,7 @@ app.route({
|
|
|
19578
19603
|
path: "router-skill",
|
|
19579
19604
|
key: "version",
|
|
19580
19605
|
description: "获取最新router版本号",
|
|
19581
|
-
middleware: ["auth"],
|
|
19606
|
+
middleware: ["auth-admin"],
|
|
19582
19607
|
metadata: {
|
|
19583
19608
|
tags: ["opencode"],
|
|
19584
19609
|
...createSkill({
|
|
@@ -19597,7 +19622,7 @@ app.route({
|
|
|
19597
19622
|
path: "route-skill",
|
|
19598
19623
|
key: "test",
|
|
19599
19624
|
description: "测试路由技能",
|
|
19600
|
-
middleware: ["auth"],
|
|
19625
|
+
middleware: ["auth-admin"],
|
|
19601
19626
|
metadata: {
|
|
19602
19627
|
tags: ["opencode"],
|
|
19603
19628
|
...createSkill({
|
|
@@ -19617,14 +19642,18 @@ app.route({
|
|
|
19617
19642
|
}).addTo(app);
|
|
19618
19643
|
|
|
19619
19644
|
// agent/routes/index.ts
|
|
19620
|
-
|
|
19621
|
-
|
|
19622
|
-
|
|
19623
|
-
|
|
19624
|
-
|
|
19625
|
-
|
|
19626
|
-
|
|
19627
|
-
|
|
19645
|
+
app.route({
|
|
19646
|
+
path: "auth",
|
|
19647
|
+
key: "",
|
|
19648
|
+
id: "auth",
|
|
19649
|
+
description: "身份验证路由"
|
|
19650
|
+
}).define(async (ctx) => {}).addTo(app, { overwrite: false });
|
|
19651
|
+
app.route({
|
|
19652
|
+
path: "auth-admin",
|
|
19653
|
+
key: "",
|
|
19654
|
+
id: "auth-admin",
|
|
19655
|
+
description: "管理员身份验证路由"
|
|
19656
|
+
}).define(async (ctx) => {}).addTo(app, { overwrite: false });
|
|
19628
19657
|
|
|
19629
19658
|
// agent/main.ts
|
|
19630
19659
|
var routerAgentPlugin = createRouterAgentPluginFn({ router: app });
|