@kevisual/router 0.0.83 → 0.0.85

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.
@@ -1,14 +1,20 @@
1
1
  import { app } from '../app.ts'
2
2
  import './route-create.ts'
3
3
 
4
- if (!app.hasRoute('auth', '')) {
5
- app.route({
6
- path: 'auth',
7
- key: '',
8
- id: 'auth',
9
- description: '身份验证路由',
10
- }).define(async (ctx) => {
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
@@ -3091,7 +3091,7 @@ function pick(obj, keys) {
3091
3091
  // node_modules/.pnpm/eventemitter3@5.0.4/node_modules/eventemitter3/index.mjs
3092
3092
  var import__ = __toESM(require_eventemitter3(), 1);
3093
3093
 
3094
- // node_modules/.pnpm/es-toolkit@1.44.0/node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
3094
+ // node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/predicate/isPlainObject.mjs
3095
3095
  function isPlainObject(value) {
3096
3096
  if (!value || typeof value !== "object") {
3097
3097
  return false;
@@ -3104,12 +3104,12 @@ function isPlainObject(value) {
3104
3104
  return Object.prototype.toString.call(value) === "[object Object]";
3105
3105
  }
3106
3106
 
3107
- // node_modules/.pnpm/es-toolkit@1.44.0/node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
3107
+ // node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/_internal/isUnsafeProperty.mjs
3108
3108
  function isUnsafeProperty(key) {
3109
3109
  return key === "__proto__";
3110
3110
  }
3111
3111
 
3112
- // node_modules/.pnpm/es-toolkit@1.44.0/node_modules/es-toolkit/dist/object/merge.mjs
3112
+ // node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/object/merge.mjs
3113
3113
  function merge(target, source) {
3114
3114
  const sourceKeys = Object.keys(source);
3115
3115
  for (let i = 0;i < sourceKeys.length; i++) {
@@ -16932,7 +16932,7 @@ class Route {
16932
16932
  if (opts) {
16933
16933
  this.id = opts.id || randomId(12, "rand-");
16934
16934
  if (!opts.id && opts.idUsePath) {
16935
- const delimiter = opts.delimiter ?? "$#$";
16935
+ const delimiter = opts.delimiter ?? "$$";
16936
16936
  this.id = path + delimiter + key;
16937
16937
  }
16938
16938
  this.run = opts.run;
@@ -17055,6 +17055,7 @@ class QueryRouter {
17055
17055
  const maxNextRoute = this.maxNextRoute;
17056
17056
  ctx = ctx || {};
17057
17057
  ctx.currentPath = path;
17058
+ ctx.currentId = route?.id;
17058
17059
  ctx.currentKey = key;
17059
17060
  ctx.currentRoute = route;
17060
17061
  ctx.index = (ctx.index || 0) + 1;
@@ -17068,7 +17069,7 @@ class QueryRouter {
17068
17069
  ctx.code = 500;
17069
17070
  ctx.message = "Too many nextRoute";
17070
17071
  ctx.body = null;
17071
- return;
17072
+ return ctx;
17072
17073
  }
17073
17074
  if (route && route.middleware && route.middleware.length > 0) {
17074
17075
  const errorMiddleware = [];
@@ -17143,7 +17144,9 @@ class QueryRouter {
17143
17144
  }
17144
17145
  return ctx;
17145
17146
  }
17146
- if (ctx.end) {}
17147
+ if (ctx.end) {
17148
+ return ctx;
17149
+ }
17147
17150
  }
17148
17151
  }
17149
17152
  }
@@ -17337,7 +17340,7 @@ class QueryRouter {
17337
17340
  description: "列出当前应用下的所有的路由信息",
17338
17341
  middleware: opts?.middleware || [],
17339
17342
  run: async (ctx) => {
17340
- const tokenUser = ctx.state.tokenUser;
17343
+ const tokenUser = ctx.state;
17341
17344
  let isUser = !!tokenUser;
17342
17345
  const list = this.getList(opts?.filter).filter((item) => {
17343
17346
  if (item.id === "auth" || item.id === "auth-can" || item.id === "check-auth-admin" || item.id === "auth-admin") {
@@ -17368,6 +17371,56 @@ class QueryRouter {
17368
17371
  fromJSONSchema = fromJSONSchema3;
17369
17372
  }
17370
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
+
17371
17424
  // src/server/server.ts
17372
17425
  import http from "node:http";
17373
17426
  import https from "node:https";
@@ -17861,7 +17914,7 @@ class ServerBase {
17861
17914
  }
17862
17915
  }
17863
17916
  async onWsClose(ws) {
17864
- const id = ws?.data?.id || "";
17917
+ const id = ws?.wsId || "";
17865
17918
  if (id) {
17866
17919
  this.emitter.emit("close--" + id, { type: "close", ws, id });
17867
17920
  setTimeout(() => {
@@ -17874,6 +17927,9 @@ class ServerBase {
17874
17927
  if (this.showConnected)
17875
17928
  ws.send(JSON.stringify({ type: "connected" }));
17876
17929
  }
17930
+ createId() {
17931
+ return Math.random().toString(36).substring(2, 15);
17932
+ }
17877
17933
  }
17878
17934
 
17879
17935
  // node_modules/.pnpm/@kevisual+ws@8.0.0/node_modules/@kevisual/ws/wrapper.mjs
@@ -17914,6 +17970,9 @@ class WsServerBase {
17914
17970
  token,
17915
17971
  id
17916
17972
  };
17973
+ if (!ws.wsId) {
17974
+ ws.wsId = this.createId();
17975
+ }
17917
17976
  ws.on("message", async (message) => {
17918
17977
  await this.server.onWebSocket({ ws, message, pathname, token, id });
17919
17978
  });
@@ -17924,6 +17983,9 @@ class WsServerBase {
17924
17983
  });
17925
17984
  });
17926
17985
  }
17986
+ createId() {
17987
+ return Math.random().toString(36).substring(2, 15);
17988
+ }
17927
17989
  }
17928
17990
 
17929
17991
  class WsServer extends WsServerBase {
@@ -18239,10 +18301,14 @@ class BunServer extends ServerBase {
18239
18301
  open: (ws) => {
18240
18302
  this.sendConnected(ws);
18241
18303
  },
18242
- message: async (ws, message) => {
18304
+ message: async (bunWs, message) => {
18305
+ const ws = bunWs;
18243
18306
  const pathname = ws.data.pathname || "";
18244
18307
  const token = ws.data.token || "";
18245
18308
  const id = ws.data.id || "";
18309
+ if (!ws.wsId) {
18310
+ ws.wsId = this.createId();
18311
+ }
18246
18312
  await this.onWebSocket({ ws, message, pathname, token, id });
18247
18313
  },
18248
18314
  close: (ws) => {
@@ -18257,11 +18323,11 @@ class BunServer extends ServerBase {
18257
18323
  }
18258
18324
  }
18259
18325
  // src/app.ts
18260
- class App extends QueryRouter {
18326
+ class App extends QueryRouterServer {
18261
18327
  router;
18262
18328
  server;
18263
18329
  constructor(opts) {
18264
- super();
18330
+ super({ initHandle: false, context: { needSerialize: true, ...opts?.routerContext } });
18265
18331
  const router = this;
18266
18332
  let server = opts?.server;
18267
18333
  if (!server) {
@@ -18273,7 +18339,6 @@ class App extends QueryRouter {
18273
18339
  }
18274
18340
  }
18275
18341
  server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
18276
- router.setContext({ needSerialize: true, ...opts?.routerContext });
18277
18342
  this.router = router;
18278
18343
  this.server = server;
18279
18344
  if (opts?.appId) {
@@ -18286,42 +18351,7 @@ class App extends QueryRouter {
18286
18351
  listen(...args) {
18287
18352
  this.server.listen(...args);
18288
18353
  }
18289
- addRoute(route, opts) {
18290
- super.add(route, opts);
18291
- }
18292
18354
  Route = Route;
18293
- route(...args) {
18294
- const [path, key, opts] = args;
18295
- if (typeof path === "object") {
18296
- return new Route(path.path, path.key, path);
18297
- }
18298
- if (typeof path === "string") {
18299
- if (opts) {
18300
- return new Route(path, key, opts);
18301
- }
18302
- if (key && typeof key === "object") {
18303
- return new Route(path, key?.key || "", key);
18304
- }
18305
- return new Route(path, key);
18306
- }
18307
- return new Route(path, key, opts);
18308
- }
18309
- prompt(...args) {
18310
- const [desc] = args;
18311
- let description = "";
18312
- if (typeof desc === "string") {
18313
- description = desc;
18314
- } else if (typeof desc === "function") {
18315
- description = desc() || "";
18316
- }
18317
- return new Route("", "", { description });
18318
- }
18319
- async call(message, ctx) {
18320
- return await super.call(message, ctx);
18321
- }
18322
- async run(msg, ctx) {
18323
- return await super.run(msg, ctx);
18324
- }
18325
18355
  static handleRequest(req, res) {
18326
18356
  return handleServer(req, res);
18327
18357
  }
@@ -19242,7 +19272,7 @@ var addCallFn = (app2) => {
19242
19272
  path: "call",
19243
19273
  key: "",
19244
19274
  description: "调用",
19245
- middleware: ["auth"],
19275
+ middleware: ["auth-admin"],
19246
19276
  metadata: {
19247
19277
  tags: ["opencode"],
19248
19278
  ...createSkill({
@@ -19256,7 +19286,7 @@ var addCallFn = (app2) => {
19256
19286
  args: {
19257
19287
  path: tool.schema.string().describe("应用路径,例如 cnb"),
19258
19288
  key: tool.schema.string().optional().describe("应用key,例如 list-repos"),
19259
- payload: tool.schema.object({}).optional().describe("调用参数")
19289
+ payload: tool.schema.object({}).optional().describe('调用参数, 为对象, 例如 { "query": "javascript" }')
19260
19290
  }
19261
19291
  })
19262
19292
  }
@@ -19284,8 +19314,13 @@ var createRouterAgentPluginFn = (opts) => {
19284
19314
  if (!router.hasRoute("call", "")) {
19285
19315
  addCallFn(router);
19286
19316
  }
19287
- if (!router.hasRoute("auth", "")) {
19288
- router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router);
19317
+ if (router) {
19318
+ router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router, {
19319
+ overwrite: false
19320
+ });
19321
+ router.route({ path: "auth-admin", key: "", id: "auth-admin", description: "认证" }).define(async (ctx) => {}).addTo(router, {
19322
+ overwrite: false
19323
+ });
19289
19324
  }
19290
19325
  const _routes = filter(router.routes, opts?.query || "");
19291
19326
  const routes = _routes.filter((r) => {
@@ -19299,7 +19334,7 @@ var createRouterAgentPluginFn = (opts) => {
19299
19334
  return false;
19300
19335
  });
19301
19336
  const AgentPlugin = async (pluginInput) => {
19302
- useContextKey("plugin-input", () => pluginInput, true);
19337
+ useContextKey("plugin-input", () => pluginInput, { isNew: true });
19303
19338
  const hooks = opts?.hooks ? await opts.hooks(pluginInput) : {};
19304
19339
  return {
19305
19340
  ...hooks,
@@ -19540,14 +19575,14 @@ app
19540
19575
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19541
19576
  `;
19542
19577
  // package.json
19543
- var version2 = "0.0.82";
19578
+ var version2 = "0.0.85";
19544
19579
 
19545
19580
  // agent/routes/route-create.ts
19546
19581
  app.route({
19547
19582
  path: "router-skill",
19548
19583
  key: "create-route",
19549
19584
  description: "创建路由技能",
19550
- middleware: ["auth"],
19585
+ middleware: ["auth-admin"],
19551
19586
  metadata: {
19552
19587
  tags: ["opencode"],
19553
19588
  ...createSkill({
@@ -19581,7 +19616,7 @@ app.route({
19581
19616
  path: "router-skill",
19582
19617
  key: "version",
19583
19618
  description: "获取最新router版本号",
19584
- middleware: ["auth"],
19619
+ middleware: ["auth-admin"],
19585
19620
  metadata: {
19586
19621
  tags: ["opencode"],
19587
19622
  ...createSkill({
@@ -19600,7 +19635,7 @@ app.route({
19600
19635
  path: "route-skill",
19601
19636
  key: "test",
19602
19637
  description: "测试路由技能",
19603
- middleware: ["auth"],
19638
+ middleware: ["auth-admin"],
19604
19639
  metadata: {
19605
19640
  tags: ["opencode"],
19606
19641
  ...createSkill({
@@ -19620,14 +19655,18 @@ app.route({
19620
19655
  }).addTo(app);
19621
19656
 
19622
19657
  // agent/routes/index.ts
19623
- if (!app.hasRoute("auth", "")) {
19624
- app.route({
19625
- path: "auth",
19626
- key: "",
19627
- id: "auth",
19628
- description: "身份验证路由"
19629
- }).define(async (ctx) => {}).addTo(app);
19630
- }
19658
+ app.route({
19659
+ path: "auth",
19660
+ key: "",
19661
+ id: "auth",
19662
+ description: "身份验证路由"
19663
+ }).define(async (ctx) => {}).addTo(app, { overwrite: false });
19664
+ app.route({
19665
+ path: "auth-admin",
19666
+ key: "",
19667
+ id: "auth-admin",
19668
+ description: "管理员身份验证路由"
19669
+ }).define(async (ctx) => {}).addTo(app, { overwrite: false });
19631
19670
 
19632
19671
  // agent/main.ts
19633
19672
  var routerAgentPlugin = createRouterAgentPluginFn({ router: app });