@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.
@@ -33,9 +33,20 @@ type RouterContextT = {
33
33
  code?: number;
34
34
  [key: string]: any;
35
35
  };
36
+ type BuildRouteContext<M, U> = M extends {
37
+ args?: infer A;
38
+ } ? A extends z.ZodObject<any> ? RouteContext<{
39
+ args?: z.infer<A>;
40
+ }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
41
+ args?: {
42
+ [K in keyof A]: z.infer<A[K]>;
43
+ };
44
+ }, U> : RouteContext<U> : RouteContext<U>;
36
45
  type RouteContext<T = {
37
46
  code?: number;
38
- }, S = any> = {
47
+ }, U extends SimpleObject = {}, S = {
48
+ [key: string]: any;
49
+ }> = {
39
50
  /**
40
51
  * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
41
52
  * 或者不需要登录的,直接调用
@@ -58,7 +69,14 @@ type RouteContext<T = {
58
69
  code?: number;
59
70
  /** return msg */
60
71
  message?: string;
72
+ /**
73
+ * 传递状态
74
+ */
61
75
  state?: S;
76
+ /**
77
+ * 当前routerId
78
+ */
79
+ currentId?: string;
62
80
  /**
63
81
  * 当前路径
64
82
  */
@@ -99,14 +117,12 @@ type RouteContext<T = {
99
117
  path: string;
100
118
  key?: string;
101
119
  payload?: any;
102
- }, ctx?: RouteContext & {
103
- [key: string]: any;
104
- }) => Promise<any>;
120
+ }, ctx?: RouteContext) => Promise<any>;
105
121
  index?: number;
106
122
  throw?: throwError['throw'];
107
123
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
108
124
  needSerialize?: boolean;
109
- } & T;
125
+ } & T & U;
110
126
  type SimpleObject = Record<string, any>;
111
127
  type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
112
128
  type RunMessage = {
@@ -117,7 +133,7 @@ type RunMessage = {
117
133
  };
118
134
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
119
135
  type RouteMiddleware = {
120
- path: string;
136
+ path?: string;
121
137
  key?: string;
122
138
  id?: string;
123
139
  } | string;
@@ -130,7 +146,7 @@ type RouteOpts<U = {}, T = SimpleObject> = {
130
146
  description?: string;
131
147
  metadata?: T;
132
148
  middleware?: RouteMiddleware[];
133
- type?: 'route' | 'middleware';
149
+ type?: 'route' | 'middleware' | 'compound';
134
150
  /**
135
151
  * $#$ will be used to split path and key
136
152
  */
@@ -144,9 +160,11 @@ type RouteOpts<U = {}, T = SimpleObject> = {
144
160
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
145
161
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
146
162
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
147
- declare class Route<U = {
148
- [key: string]: any;
149
- }, T extends SimpleObject = SimpleObject> implements throwError {
163
+ /**
164
+ * @M 是 route的 metadate的类型,默认是 SimpleObject
165
+ * @U RouteContext state的类型
166
+ */
167
+ declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject> implements throwError {
150
168
  /**
151
169
  * 一级路径
152
170
  */
@@ -156,10 +174,10 @@ declare class Route<U = {
156
174
  */
157
175
  key?: string;
158
176
  id?: string;
159
- run?: Run;
177
+ run?: Run<BuildRouteContext<M, U>>;
160
178
  nextRoute?: NextRoute;
161
179
  description?: string;
162
- metadata?: T;
180
+ metadata?: M;
163
181
  middleware?: RouteMiddleware[];
164
182
  type?: string;
165
183
  /**
@@ -174,13 +192,13 @@ declare class Route<U = {
174
192
  } = RouterContextT>(opts: DefineRouteOpts): this;
175
193
  define<T extends {
176
194
  [key: string]: any;
177
- } = RouterContextT>(fn: Run<T & U>): this;
195
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
178
196
  define<T extends {
179
197
  [key: string]: any;
180
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
198
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
181
199
  define<T extends {
182
200
  [key: string]: any;
183
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
201
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
184
202
  update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
185
203
  addTo(router: QueryRouter | {
186
204
  add: (route: Route) => void;
@@ -194,11 +212,11 @@ declare class Route<U = {
194
212
  type AddOpts = {
195
213
  overwrite?: boolean;
196
214
  };
197
- declare class QueryRouter implements throwError {
215
+ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
198
216
  appId: string;
199
217
  routes: Route[];
200
218
  maxNextRoute: number;
201
- context?: RouteContext;
219
+ context?: RouteContext<T>;
202
220
  constructor();
203
221
  /**
204
222
  * add route
@@ -226,7 +244,7 @@ declare class QueryRouter implements throwError {
226
244
  * @param ctx
227
245
  * @returns
228
246
  */
229
- runRoute(path: string, key: string, ctx?: RouteContext): any;
247
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
230
248
  /**
231
249
  * 第一次执行
232
250
  * @param message
@@ -237,9 +255,11 @@ declare class QueryRouter implements throwError {
237
255
  path: string;
238
256
  key?: string;
239
257
  payload?: any;
240
- }, ctx?: RouteContext & {
258
+ }, ctx?: RouteContext<T> & {
241
259
  [key: string]: any;
242
- }): Promise<any>;
260
+ }): Promise<RouteContext<T, {}, {
261
+ [key: string]: any;
262
+ }>>;
243
263
  /**
244
264
  * 返回的数据包含所有的context的请求返回的内容,可做其他处理
245
265
  * @param message
@@ -251,9 +271,15 @@ declare class QueryRouter implements throwError {
251
271
  path?: string;
252
272
  key?: string;
253
273
  payload?: any;
254
- }, ctx?: RouteContext & {
274
+ }, ctx?: RouteContext<T> & {
255
275
  [key: string]: any;
256
- }): Promise<any>;
276
+ }): Promise<RouteContext<T, {}, {
277
+ [key: string]: any;
278
+ }> | {
279
+ code: number;
280
+ body: any;
281
+ message: string;
282
+ }>;
257
283
  /**
258
284
  * 请求 result 的数据
259
285
  * @param message
@@ -269,9 +295,9 @@ declare class QueryRouter implements throwError {
269
295
  }, ctx?: RouteContext & {
270
296
  [key: string]: any;
271
297
  }): Promise<{
272
- code: any;
298
+ code: number;
273
299
  data: any;
274
- message: any;
300
+ message: string;
275
301
  }>;
276
302
  /**
277
303
  * Router Run获取数据
@@ -284,12 +310,12 @@ declare class QueryRouter implements throwError {
284
310
  path?: string;
285
311
  key?: string;
286
312
  payload?: any;
287
- }, ctx?: RouteContext & {
313
+ }, ctx?: RouteContext<T> & {
288
314
  [key: string]: any;
289
315
  }): Promise<{
290
- code: any;
316
+ code: number;
291
317
  data: any;
292
- message: any;
318
+ message: string;
293
319
  }>;
294
320
  /**
295
321
  * 设置上下文
@@ -301,12 +327,12 @@ declare class QueryRouter implements throwError {
301
327
  /**
302
328
  * 获取handle函数, 这里会去执行parse函数
303
329
  */
304
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
330
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
305
331
  id?: string;
306
332
  path?: string;
307
333
  key?: string;
308
334
  [key: string]: any;
309
- }, handleContext?: RouteContext) => Promise<{
335
+ }, handleContext?: RouteContext<T>) => Promise<{
310
336
  [key: string]: any;
311
337
  code: string;
312
338
  data?: any;
@@ -320,22 +346,16 @@ declare class QueryRouter implements throwError {
320
346
  message: any;
321
347
  data?: undefined;
322
348
  }>;
323
- exportRoutes(): Route<{
324
- [key: string]: any;
325
- }, SimpleObject>[];
349
+ exportRoutes(): Route<SimpleObject, SimpleObject>[];
326
350
  importRoutes(routes: Route[]): void;
327
351
  importRouter(router: QueryRouter): void;
328
352
  throw(...args: any[]): void;
329
- hasRoute(path: string, key?: string): Route<{
330
- [key: string]: any;
331
- }, SimpleObject>;
353
+ hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
332
354
  findRoute(opts?: {
333
355
  path?: string;
334
356
  key?: string;
335
357
  id?: string;
336
- }): Route<{
337
- [key: string]: any;
338
- }, SimpleObject>;
358
+ }): Route<SimpleObject, SimpleObject>;
339
359
  createRouteList(opts?: {
340
360
  force?: boolean;
341
361
  filter?: (route: Route) => boolean;
@@ -377,10 +397,11 @@ declare class QueryRouter implements throwError {
377
397
  [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
378
398
  };
379
399
  }
380
- type QueryRouterServerOpts = {
400
+ type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
381
401
  handleFn?: HandleFn;
382
- context?: RouteContext;
402
+ context?: RouteContext<C>;
383
403
  appId?: string;
404
+ initHandle?: boolean;
384
405
  };
385
406
  interface HandleFn<T = any> {
386
407
  (msg: {
@@ -397,20 +418,27 @@ interface HandleFn<T = any> {
397
418
  /**
398
419
  * QueryRouterServer
399
420
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
421
+ * @template C 自定义 RouteContext 类型
400
422
  */
401
- declare class QueryRouterServer extends QueryRouter {
423
+ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
402
424
  appId: string;
403
425
  handle: any;
404
- constructor(opts?: QueryRouterServerOpts);
426
+ context: RouteContext<C>;
427
+ constructor(opts?: QueryRouterServerOpts<C>);
405
428
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
406
429
  addRoute(route: Route, opts?: AddOpts): void;
407
430
  Route: typeof Route;
408
- route(opts: RouteOpts): Route<Required<RouteContext>>;
409
- route(path: string, key?: string): Route<Required<RouteContext>>;
410
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
411
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
412
- prompt(description: string): Route<Required<RouteContext>>;
413
- prompt(description: Function): Route<Required<RouteContext>>;
431
+ route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & {
432
+ metadata?: M;
433
+ }): Route<M, Required<RouteContext<C>>>;
434
+ route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & {
435
+ metadata?: M;
436
+ }): Route<M, Required<RouteContext<C>>>;
437
+ route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
438
+ route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & {
439
+ metadata?: M;
440
+ }): Route<M, Required<RouteContext<C>>>;
441
+ prompt(description: string): Route<SimpleObject, SimpleObject>;
414
442
  /**
415
443
  * 调用了handle
416
444
  * @param param0
@@ -421,9 +449,7 @@ declare class QueryRouterServer extends QueryRouter {
421
449
  path?: string;
422
450
  key?: string;
423
451
  payload?: any;
424
- }, ctx?: RouteContext & {
425
- [key: string]: any;
426
- }): Promise<any>;
452
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
427
453
  }
428
454
 
429
455
  type Cors = {
@@ -477,15 +503,32 @@ type OnWebSocketOptions<T = {}> = {
477
503
  message: string | Buffer;
478
504
  pathname: string;
479
505
  token?: string;
506
+ /** data 的id提取出来 */
480
507
  id?: string;
481
508
  };
482
509
  type WS<T = {}> = {
483
510
  send: (data: any) => void;
484
511
  close: (code?: number, reason?: string) => void;
512
+ /**
513
+ * ws 自己生成的一个id,主要是为了区分不同的ws连接,方便在onWebSocket中使用
514
+ */
515
+ wsId?: string;
485
516
  data?: {
517
+ /**
518
+ * ws连接时的url,包含pathname和searchParams
519
+ */
486
520
  url: URL;
521
+ /**
522
+ * ws连接时的pathname
523
+ */
487
524
  pathname: string;
525
+ /**
526
+ * ws连接时的url中的token参数
527
+ */
488
528
  token?: string;
529
+ /**
530
+ * ws连接时的url中的id参数.
531
+ */
489
532
  id?: string;
490
533
  /**
491
534
  * 鉴权后的获取的信息
@@ -674,7 +717,7 @@ type RouterHandle = (msg: {
674
717
  [key: string]: any;
675
718
  };
676
719
  type AppOptions<T = {}> = {
677
- router?: QueryRouter;
720
+ router?: QueryRouterServer;
678
721
  server?: ServerType;
679
722
  /** handle msg 关联 */
680
723
  routerHandle?: RouterHandle;
@@ -682,17 +725,18 @@ type AppOptions<T = {}> = {
682
725
  serverOptions?: ServerNodeOpts;
683
726
  appId?: string;
684
727
  };
685
- type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
728
+ type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
686
729
  app: App<T>;
687
730
  };
688
731
  /**
689
732
  * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
690
733
  * U - Route Context的扩展类型
691
734
  */
692
- declare class App<U = {}> extends QueryRouter {
735
+ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
693
736
  appId: string;
694
- router: QueryRouter;
737
+ router: QueryRouterServer;
695
738
  server: ServerType;
739
+ context: AppRouteContext<U>;
696
740
  constructor(opts?: AppOptions<U>);
697
741
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
698
742
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -702,34 +746,7 @@ declare class App<U = {}> extends QueryRouter {
702
746
  listen(path: string, listeningListener?: () => void): void;
703
747
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
704
748
  listen(handle: any, listeningListener?: () => void): void;
705
- addRoute(route: Route, opts?: AddOpts): void;
706
749
  Route: typeof Route;
707
- route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
708
- route(path: string, key?: string): Route<AppRouteContext<U>>;
709
- route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
710
- route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
711
- prompt(description: string): Route<AppRouteContext<U>>;
712
- prompt(description: Function): Route<AppRouteContext<U>>;
713
- call(message: {
714
- id?: string;
715
- path?: string;
716
- key?: string;
717
- payload?: any;
718
- }, ctx?: AppRouteContext<U> & {
719
- [key: string]: any;
720
- }): Promise<any>;
721
- run(msg: {
722
- id?: string;
723
- path?: string;
724
- key?: string;
725
- payload?: any;
726
- }, ctx?: Partial<AppRouteContext<U>> & {
727
- [key: string]: any;
728
- }): Promise<{
729
- code: any;
730
- data: any;
731
- message: any;
732
- }>;
733
750
  static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
734
751
  cookies: Record<string, string>;
735
752
  token: string;
package/dist/opencode.js CHANGED
@@ -14703,7 +14703,7 @@ var addCallFn = (app) => {
14703
14703
  path: "call",
14704
14704
  key: "",
14705
14705
  description: "调用",
14706
- middleware: ["auth"],
14706
+ middleware: ["auth-admin"],
14707
14707
  metadata: {
14708
14708
  tags: ["opencode"],
14709
14709
  ...createSkill({
@@ -14717,7 +14717,7 @@ var addCallFn = (app) => {
14717
14717
  args: {
14718
14718
  path: tool.schema.string().describe("应用路径,例如 cnb"),
14719
14719
  key: tool.schema.string().optional().describe("应用key,例如 list-repos"),
14720
- payload: tool.schema.object({}).optional().describe("调用参数")
14720
+ payload: tool.schema.object({}).optional().describe('调用参数, 为对象, 例如 { "query": "javascript" }')
14721
14721
  }
14722
14722
  })
14723
14723
  }
@@ -14745,8 +14745,13 @@ var createRouterAgentPluginFn = (opts) => {
14745
14745
  if (!router.hasRoute("call", "")) {
14746
14746
  addCallFn(router);
14747
14747
  }
14748
- if (!router.hasRoute("auth", "")) {
14749
- router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router);
14748
+ if (router) {
14749
+ router.route({ path: "auth", key: "", id: "auth", description: "认证" }).define(async (ctx) => {}).addTo(router, {
14750
+ overwrite: false
14751
+ });
14752
+ router.route({ path: "auth-admin", key: "", id: "auth-admin", description: "认证" }).define(async (ctx) => {}).addTo(router, {
14753
+ overwrite: false
14754
+ });
14750
14755
  }
14751
14756
  const _routes = filter(router.routes, opts?.query || "");
14752
14757
  const routes = _routes.filter((r) => {
@@ -14760,7 +14765,7 @@ var createRouterAgentPluginFn = (opts) => {
14760
14765
  return false;
14761
14766
  });
14762
14767
  const AgentPlugin = async (pluginInput) => {
14763
- useContextKey("plugin-input", () => pluginInput, true);
14768
+ useContextKey("plugin-input", () => pluginInput, { isNew: true });
14764
14769
  const hooks = opts?.hooks ? await opts.hooks(pluginInput) : {};
14765
14770
  return {
14766
14771
  ...hooks,