@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.
@@ -10,6 +10,11 @@ type CustomErrorOptions = {
10
10
  code?: number;
11
11
  message?: string;
12
12
  };
13
+ interface throwError {
14
+ throw(code?: number | string, message?: string): void;
15
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
16
+ throw(opts?: CustomErrorOptions): void;
17
+ }
13
18
 
14
19
  declare class MockProcess {
15
20
  emitter?: EventEmitter;
@@ -28,9 +33,20 @@ type RouterContextT = {
28
33
  code?: number;
29
34
  [key: string]: any;
30
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>;
31
45
  type RouteContext<T = {
32
46
  code?: number;
33
- }, S = any> = {
47
+ }, U extends SimpleObject = {}, S = {
48
+ [key: string]: any;
49
+ }> = {
34
50
  /**
35
51
  * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
36
52
  * 或者不需要登录的,直接调用
@@ -53,7 +69,14 @@ type RouteContext<T = {
53
69
  code?: number;
54
70
  /** return msg */
55
71
  message?: string;
72
+ /**
73
+ * 传递状态
74
+ */
56
75
  state?: S;
76
+ /**
77
+ * 当前routerId
78
+ */
79
+ currentId?: string;
57
80
  /**
58
81
  * 当前路径
59
82
  */
@@ -94,14 +117,12 @@ type RouteContext<T = {
94
117
  path: string;
95
118
  key?: string;
96
119
  payload?: any;
97
- }, ctx?: RouteContext & {
98
- [key: string]: any;
99
- }) => Promise<any>;
120
+ }, ctx?: RouteContext) => Promise<any>;
100
121
  index?: number;
101
- throw?: (code?: number | string, message?: string, tips?: string) => void;
122
+ throw?: throwError['throw'];
102
123
  /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
103
124
  needSerialize?: boolean;
104
- } & T;
125
+ } & T & U;
105
126
  type SimpleObject = Record<string, any>;
106
127
  type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
107
128
  type RunMessage = {
@@ -112,7 +133,7 @@ type RunMessage = {
112
133
  };
113
134
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
114
135
  type RouteMiddleware = {
115
- path: string;
136
+ path?: string;
116
137
  key?: string;
117
138
  id?: string;
118
139
  } | string;
@@ -139,9 +160,11 @@ type RouteOpts<U = {}, T = SimpleObject> = {
139
160
  type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
140
161
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
141
162
  type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
142
- declare class Route<U = {
143
- [key: string]: any;
144
- }, T extends SimpleObject = SimpleObject> {
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 {
145
168
  /**
146
169
  * 一级路径
147
170
  */
@@ -151,10 +174,10 @@ declare class Route<U = {
151
174
  */
152
175
  key?: string;
153
176
  id?: string;
154
- run?: Run;
177
+ run?: Run<BuildRouteContext<M, U>>;
155
178
  nextRoute?: NextRoute;
156
179
  description?: string;
157
- metadata?: T;
180
+ metadata?: M;
158
181
  middleware?: RouteMiddleware[];
159
182
  type?: string;
160
183
  /**
@@ -169,19 +192,19 @@ declare class Route<U = {
169
192
  } = RouterContextT>(opts: DefineRouteOpts): this;
170
193
  define<T extends {
171
194
  [key: string]: any;
172
- } = RouterContextT>(fn: Run<T & U>): this;
195
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
173
196
  define<T extends {
174
197
  [key: string]: any;
175
- } = RouterContextT>(key: string, fn: Run<T & U>): this;
198
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
176
199
  define<T extends {
177
200
  [key: string]: any;
178
- } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
201
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
179
202
  update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
180
203
  addTo(router: QueryRouter | {
181
204
  add: (route: Route) => void;
182
205
  [key: string]: any;
183
206
  }, opts?: AddOpts): void;
184
- throw(code?: number | string, message?: string, tips?: string): void;
207
+ throw(...args: any[]): void;
185
208
  }
186
209
  /**
187
210
  * @parmas overwrite 是否覆盖已存在的route,默认true
@@ -189,11 +212,11 @@ declare class Route<U = {
189
212
  type AddOpts = {
190
213
  overwrite?: boolean;
191
214
  };
192
- declare class QueryRouter {
215
+ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
193
216
  appId: string;
194
217
  routes: Route[];
195
218
  maxNextRoute: number;
196
- context?: RouteContext;
219
+ context?: RouteContext<T>;
197
220
  constructor();
198
221
  /**
199
222
  * add route
@@ -221,7 +244,7 @@ declare class QueryRouter {
221
244
  * @param ctx
222
245
  * @returns
223
246
  */
224
- runRoute(path: string, key: string, ctx?: RouteContext): any;
247
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
225
248
  /**
226
249
  * 第一次执行
227
250
  * @param message
@@ -232,9 +255,11 @@ declare class QueryRouter {
232
255
  path: string;
233
256
  key?: string;
234
257
  payload?: any;
235
- }, ctx?: RouteContext & {
258
+ }, ctx?: RouteContext<T> & {
259
+ [key: string]: any;
260
+ }): Promise<RouteContext<T, {}, {
236
261
  [key: string]: any;
237
- }): Promise<any>;
262
+ }>>;
238
263
  /**
239
264
  * 返回的数据包含所有的context的请求返回的内容,可做其他处理
240
265
  * @param message
@@ -246,9 +271,15 @@ declare class QueryRouter {
246
271
  path?: string;
247
272
  key?: string;
248
273
  payload?: any;
249
- }, ctx?: RouteContext & {
274
+ }, ctx?: RouteContext<T> & {
275
+ [key: string]: any;
276
+ }): Promise<RouteContext<T, {}, {
250
277
  [key: string]: any;
251
- }): Promise<any>;
278
+ }> | {
279
+ code: number;
280
+ body: any;
281
+ message: string;
282
+ }>;
252
283
  /**
253
284
  * 请求 result 的数据
254
285
  * @param message
@@ -264,9 +295,9 @@ declare class QueryRouter {
264
295
  }, ctx?: RouteContext & {
265
296
  [key: string]: any;
266
297
  }): Promise<{
267
- code: any;
298
+ code: number;
268
299
  data: any;
269
- message: any;
300
+ message: string;
270
301
  }>;
271
302
  /**
272
303
  * Router Run获取数据
@@ -279,12 +310,12 @@ declare class QueryRouter {
279
310
  path?: string;
280
311
  key?: string;
281
312
  payload?: any;
282
- }, ctx?: RouteContext & {
313
+ }, ctx?: RouteContext<T> & {
283
314
  [key: string]: any;
284
315
  }): Promise<{
285
- code: any;
316
+ code: number;
286
317
  data: any;
287
- message: any;
318
+ message: string;
288
319
  }>;
289
320
  /**
290
321
  * 设置上下文
@@ -296,12 +327,12 @@ declare class QueryRouter {
296
327
  /**
297
328
  * 获取handle函数, 这里会去执行parse函数
298
329
  */
299
- getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
330
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
300
331
  id?: string;
301
332
  path?: string;
302
333
  key?: string;
303
334
  [key: string]: any;
304
- }, handleContext?: RouteContext) => Promise<{
335
+ }, handleContext?: RouteContext<T>) => Promise<{
305
336
  [key: string]: any;
306
337
  code: string;
307
338
  data?: any;
@@ -315,24 +346,16 @@ declare class QueryRouter {
315
346
  message: any;
316
347
  data?: undefined;
317
348
  }>;
318
- exportRoutes(): Route<{
319
- [key: string]: any;
320
- }, SimpleObject>[];
349
+ exportRoutes(): Route<SimpleObject, SimpleObject>[];
321
350
  importRoutes(routes: Route[]): void;
322
351
  importRouter(router: QueryRouter): void;
323
- throw(code?: number | string, message?: string): void;
324
- throw(code?: number | string, opts?: CustomErrorOptions): void;
325
- throw(opts?: CustomErrorOptions): void;
326
- hasRoute(path: string, key?: string): Route<{
327
- [key: string]: any;
328
- }, SimpleObject>;
352
+ throw(...args: any[]): void;
353
+ hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
329
354
  findRoute(opts?: {
330
355
  path?: string;
331
356
  key?: string;
332
357
  id?: string;
333
- }): Route<{
334
- [key: string]: any;
335
- }, SimpleObject>;
358
+ }): Route<SimpleObject, SimpleObject>;
336
359
  createRouteList(opts?: {
337
360
  force?: boolean;
338
361
  filter?: (route: Route) => boolean;
@@ -374,10 +397,11 @@ declare class QueryRouter {
374
397
  [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
375
398
  };
376
399
  }
377
- type QueryRouterServerOpts = {
400
+ type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
378
401
  handleFn?: HandleFn;
379
- context?: RouteContext;
402
+ context?: RouteContext<C>;
380
403
  appId?: string;
404
+ initHandle?: boolean;
381
405
  };
382
406
  interface HandleFn<T = any> {
383
407
  (msg: {
@@ -394,20 +418,27 @@ interface HandleFn<T = any> {
394
418
  /**
395
419
  * QueryRouterServer
396
420
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
421
+ * @template C 自定义 RouteContext 类型
397
422
  */
398
- declare class QueryRouterServer extends QueryRouter {
423
+ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
399
424
  appId: string;
400
425
  handle: any;
401
- constructor(opts?: QueryRouterServerOpts);
426
+ context: RouteContext<C>;
427
+ constructor(opts?: QueryRouterServerOpts<C>);
402
428
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
403
429
  addRoute(route: Route, opts?: AddOpts): void;
404
430
  Route: typeof Route;
405
- route(opts: RouteOpts): Route<Required<RouteContext>>;
406
- route(path: string, key?: string): Route<Required<RouteContext>>;
407
- route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
408
- route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
409
- prompt(description: string): Route<Required<RouteContext>>;
410
- 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>;
411
442
  /**
412
443
  * 调用了handle
413
444
  * @param param0
@@ -418,9 +449,7 @@ declare class QueryRouterServer extends QueryRouter {
418
449
  path?: string;
419
450
  key?: string;
420
451
  payload?: any;
421
- }, ctx?: RouteContext & {
422
- [key: string]: any;
423
- }): Promise<any>;
452
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
424
453
  }
425
454
 
426
455
  type Cors = {
@@ -671,7 +700,7 @@ type RouterHandle = (msg: {
671
700
  [key: string]: any;
672
701
  };
673
702
  type AppOptions<T = {}> = {
674
- router?: QueryRouter;
703
+ router?: QueryRouterServer;
675
704
  server?: ServerType;
676
705
  /** handle msg 关联 */
677
706
  routerHandle?: RouterHandle;
@@ -679,17 +708,18 @@ type AppOptions<T = {}> = {
679
708
  serverOptions?: ServerNodeOpts;
680
709
  appId?: string;
681
710
  };
682
- type AppRouteContext<T = {}> = HandleCtx & RouteContext<T> & {
711
+ type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
683
712
  app: App<T>;
684
713
  };
685
714
  /**
686
715
  * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
687
716
  * U - Route Context的扩展类型
688
717
  */
689
- declare class App<U = {}> extends QueryRouter {
718
+ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
690
719
  appId: string;
691
- router: QueryRouter;
720
+ router: QueryRouterServer;
692
721
  server: ServerType;
722
+ context: AppRouteContext<U>;
693
723
  constructor(opts?: AppOptions<U>);
694
724
  listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
695
725
  listen(port: number, hostname?: string, listeningListener?: () => void): void;
@@ -699,34 +729,7 @@ declare class App<U = {}> extends QueryRouter {
699
729
  listen(path: string, listeningListener?: () => void): void;
700
730
  listen(handle: any, backlog?: number, listeningListener?: () => void): void;
701
731
  listen(handle: any, listeningListener?: () => void): void;
702
- addRoute(route: Route, opts?: AddOpts): void;
703
732
  Route: typeof Route;
704
- route(opts: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
705
- route(path: string, key?: string): Route<AppRouteContext<U>>;
706
- route(path: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
707
- route(path: string, key?: string, opts?: RouteOpts<AppRouteContext<U>>): Route<AppRouteContext<U>>;
708
- prompt(description: string): Route<AppRouteContext<U>>;
709
- prompt(description: Function): Route<AppRouteContext<U>>;
710
- call(message: {
711
- id?: string;
712
- path?: string;
713
- key?: string;
714
- payload?: any;
715
- }, ctx?: AppRouteContext<U> & {
716
- [key: string]: any;
717
- }): Promise<any>;
718
- run(msg: {
719
- id?: string;
720
- path?: string;
721
- key?: string;
722
- payload?: any;
723
- }, ctx?: Partial<AppRouteContext<U>> & {
724
- [key: string]: any;
725
- }): Promise<{
726
- code: any;
727
- data: any;
728
- message: any;
729
- }>;
730
733
  static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
731
734
  cookies: Record<string, string>;
732
735
  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,