@kevisual/router 0.0.67 → 0.0.68

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,9 +1,23 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
2
3
  import * as http from 'node:http';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
5
6
  import { PluginInput, Hooks, Plugin } from '@opencode-ai/plugin';
6
7
 
8
+ declare class MockProcess {
9
+ emitter?: EventEmitter;
10
+ process?: NodeJS.Process;
11
+ constructor(opts?: {
12
+ emitter?: EventEmitter;
13
+ isNode?: boolean;
14
+ });
15
+ send(data?: any, callback?: (err?: Error) => void): void;
16
+ exit(flag?: number): void;
17
+ on(fn: (msg?: any) => any): void;
18
+ desctroy(): void;
19
+ }
20
+
7
21
  type RouterContextT = {
8
22
  code?: number;
9
23
  [key: string]: any;
@@ -157,10 +171,10 @@ declare class Route<U = {
157
171
  throw(code?: number | string, message?: string, tips?: string): void;
158
172
  }
159
173
  /**
160
- * @parmas override 是否覆盖已存在的route,默认true
174
+ * @parmas overwrite 是否覆盖已存在的route,默认true
161
175
  */
162
176
  type AddOpts = {
163
- override?: boolean;
177
+ overwrite?: boolean;
164
178
  };
165
179
  declare class QueryRouter {
166
180
  appId: string;
@@ -318,12 +332,16 @@ declare class QueryRouter {
318
332
  key?: string;
319
333
  payload?: any;
320
334
  }, opts?: {
321
- emitter?: any;
335
+ mockProcess?: MockProcess;
322
336
  timeout?: number;
323
337
  getList?: boolean;
324
338
  force?: boolean;
325
339
  filter?: (route: Route) => boolean;
326
340
  }): Promise<void>;
341
+ static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
342
+ static fromJSONSchema: (route: RouteInfo) => {
343
+ [key: string]: z.ZodTypeAny;
344
+ };
327
345
  }
328
346
  type QueryRouterServerOpts = {
329
347
  handleFn?: HandleFn;
@@ -1,7 +1,20 @@
1
+ import { EventEmitter } from 'eventemitter3';
1
2
  import { z } from 'zod';
2
3
  import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
3
4
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
- import { EventEmitter } from 'eventemitter3';
5
+
6
+ declare class MockProcess {
7
+ emitter?: EventEmitter;
8
+ process?: NodeJS.Process;
9
+ constructor(opts?: {
10
+ emitter?: EventEmitter;
11
+ isNode?: boolean;
12
+ });
13
+ send(data?: any, callback?: (err?: Error) => void): void;
14
+ exit(flag?: number): void;
15
+ on(fn: (msg?: any) => any): void;
16
+ desctroy(): void;
17
+ }
5
18
 
6
19
  type RouterContextT = {
7
20
  code?: number;
@@ -168,11 +181,15 @@ declare class Route<U = {
168
181
  setData(data: any): this;
169
182
  throw(code?: number | string, message?: string, tips?: string): void;
170
183
  }
184
+ declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
185
+ declare const fromJSONSchema: (route: RouteInfo) => {
186
+ [key: string]: z.ZodTypeAny;
187
+ };
171
188
  /**
172
- * @parmas override 是否覆盖已存在的route,默认true
189
+ * @parmas overwrite 是否覆盖已存在的route,默认true
173
190
  */
174
191
  type AddOpts = {
175
- override?: boolean;
192
+ overwrite?: boolean;
176
193
  };
177
194
  declare class QueryRouter {
178
195
  appId: string;
@@ -330,12 +347,16 @@ declare class QueryRouter {
330
347
  key?: string;
331
348
  payload?: any;
332
349
  }, opts?: {
333
- emitter?: any;
350
+ mockProcess?: MockProcess;
334
351
  timeout?: number;
335
352
  getList?: boolean;
336
353
  force?: boolean;
337
354
  filter?: (route: Route) => boolean;
338
355
  }): Promise<void>;
356
+ static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
357
+ static fromJSONSchema: (route: RouteInfo) => {
358
+ [key: string]: z.ZodTypeAny;
359
+ };
339
360
  }
340
361
  type QueryRouterServerOpts = {
341
362
  handleFn?: HandleFn;
@@ -522,20 +543,7 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
522
543
  get routeObject(): T;
523
544
  }
524
545
 
525
- declare class MockProcess {
526
- emitter?: EventEmitter;
527
- process?: NodeJS.Process;
528
- constructor(opts?: {
529
- emitter?: EventEmitter;
530
- isNode?: boolean;
531
- });
532
- send(data?: any, callback?: (err?: Error) => void): void;
533
- exit(flag?: number): void;
534
- on(fn: (msg?: any) => any): void;
535
- desctroy(): void;
536
- }
537
-
538
546
  declare const App: typeof QueryRouterServer;
539
547
 
540
- export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, createSkill, define, tool, util };
548
+ export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, createSkill, define, fromJSONSchema, toJSONSchema, tool, util };
541
549
  export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema, Skill };
@@ -12172,7 +12172,7 @@ const allProcessors = {
12172
12172
  optional: optionalProcessor,
12173
12173
  lazy: lazyProcessor,
12174
12174
  };
12175
- function toJSONSchema(input, params) {
12175
+ function toJSONSchema$1(input, params) {
12176
12176
  if ("_idmap" in input) {
12177
12177
  // Registry case
12178
12178
  const registry = input;
@@ -12581,7 +12581,7 @@ var index = /*#__PURE__*/Object.freeze({
12581
12581
  safeParse: safeParse$1,
12582
12582
  safeParseAsync: safeParseAsync$1,
12583
12583
  toDotPath: toDotPath,
12584
- toJSONSchema: toJSONSchema,
12584
+ toJSONSchema: toJSONSchema$1,
12585
12585
  treeifyError: treeifyError,
12586
12586
  util: util$1,
12587
12587
  version: version
@@ -14626,7 +14626,7 @@ function convertSchema(schema, ctx) {
14626
14626
  }
14627
14627
  /**
14628
14628
  * Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change. */
14629
- function fromJSONSchema(schema, params) {
14629
+ function fromJSONSchema$1(schema, params) {
14630
14630
  // Handle boolean schemas
14631
14631
  if (typeof schema === "boolean") {
14632
14632
  return schema ? z$1.any() : z$1.never();
@@ -14793,7 +14793,7 @@ var z = /*#__PURE__*/Object.freeze({
14793
14793
  float32: float32,
14794
14794
  float64: float64,
14795
14795
  formatError: formatError,
14796
- fromJSONSchema: fromJSONSchema,
14796
+ fromJSONSchema: fromJSONSchema$1,
14797
14797
  function: _function,
14798
14798
  getErrorMap: getErrorMap,
14799
14799
  globalRegistry: globalRegistry,
@@ -14886,7 +14886,7 @@ var z = /*#__PURE__*/Object.freeze({
14886
14886
  superRefine: superRefine,
14887
14887
  symbol: symbol,
14888
14888
  templateLiteral: templateLiteral,
14889
- toJSONSchema: toJSONSchema,
14889
+ toJSONSchema: toJSONSchema$1,
14890
14890
  toLowerCase: _toLowerCase,
14891
14891
  toUpperCase: _toUpperCase,
14892
14892
  transform: transform,
@@ -15044,6 +15044,35 @@ class Route {
15044
15044
  throw new CustomError(...args);
15045
15045
  }
15046
15046
  }
15047
+ const toJSONSchema = (route) => {
15048
+ const pickValues = pick$1(route, pickValue);
15049
+ if (pickValues?.metadata?.args) {
15050
+ const args = pickValues.metadata.args;
15051
+ const keys = Object.keys(args);
15052
+ const newArgs = {};
15053
+ for (let key of keys) {
15054
+ const item = args[key];
15055
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
15056
+ newArgs[key] = item.toJSONSchema();
15057
+ }
15058
+ else {
15059
+ newArgs[key] = args[key]; // 可能不是schema
15060
+ }
15061
+ }
15062
+ pickValues.metadata.args = newArgs;
15063
+ }
15064
+ return pickValues;
15065
+ };
15066
+ const fromJSONSchema = (route) => {
15067
+ const args = route?.metadata?.args || {};
15068
+ const keys = Object.keys(args);
15069
+ const newArgs = {};
15070
+ for (let key of keys) {
15071
+ const item = args[key];
15072
+ newArgs[key] = fromJSONSchema$1(item);
15073
+ }
15074
+ return newArgs;
15075
+ };
15047
15076
  class QueryRouter {
15048
15077
  appId = '';
15049
15078
  routes;
@@ -15058,13 +15087,13 @@ class QueryRouter {
15058
15087
  * @param opts
15059
15088
  */
15060
15089
  add(route, opts) {
15061
- const override = opts?.override ?? true;
15090
+ const overwrite = opts?.overwrite ?? true;
15062
15091
  const has = this.routes.findIndex((r) => r.path === route.path && r.key === route.key);
15063
15092
  if (has !== -1) {
15064
- if (!override) {
15093
+ if (!overwrite) {
15065
15094
  return;
15066
15095
  }
15067
- // 如果存在,且override为true,则覆盖
15096
+ // 如果存在,且overwrite为true,则覆盖
15068
15097
  this.routes.splice(has, 1);
15069
15098
  }
15070
15099
  this.routes.push(route);
@@ -15363,7 +15392,24 @@ class QueryRouter {
15363
15392
  }
15364
15393
  getList(filter) {
15365
15394
  return this.routes.filter(filter || (() => true)).map((r) => {
15366
- return pick$1(r, pickValue);
15395
+ const pickValues = pick$1(r, pickValue);
15396
+ if (pickValues?.metadata?.args) {
15397
+ // const demoArgs = { k: tool.schema.string().describe('示例参数') };
15398
+ const args = pickValues.metadata.args;
15399
+ const keys = Object.keys(args);
15400
+ const newArgs = {};
15401
+ for (let key of keys) {
15402
+ const item = args[key];
15403
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
15404
+ newArgs[key] = item.toJSONSchema();
15405
+ }
15406
+ else {
15407
+ newArgs[key] = args[key]; // 可能不是schema
15408
+ }
15409
+ }
15410
+ pickValues.metadata.args = newArgs;
15411
+ }
15412
+ return pickValues;
15367
15413
  });
15368
15414
  }
15369
15415
  /**
@@ -15448,6 +15494,8 @@ class QueryRouter {
15448
15494
  }
15449
15495
  return listenProcess({ app: this, params, ...opts });
15450
15496
  }
15497
+ static toJSONSchema = toJSONSchema;
15498
+ static fromJSONSchema = fromJSONSchema;
15451
15499
  }
15452
15500
  /**
15453
15501
  * QueryRouterServer
@@ -15699,4 +15747,4 @@ class QueryUtil {
15699
15747
 
15700
15748
  const App = QueryRouterServer;
15701
15749
 
15702
- export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, createSkill, define, tool, util };
15750
+ export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, createSkill, define, fromJSONSchema, toJSONSchema, tool, util };
package/dist/router.d.ts CHANGED
@@ -1,7 +1,7 @@
1
+ import { EventEmitter } from 'eventemitter3';
1
2
  import { z } from 'zod';
2
3
  import { RouteOpts as RouteOpts$1, QueryRouterServer as QueryRouterServer$1, RouteMiddleware as RouteMiddleware$1, Run as Run$1 } from '@kevisual/router';
3
4
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
- import { EventEmitter } from 'eventemitter3';
5
5
  import * as http from 'node:http';
6
6
  import http__default, { IncomingMessage, ServerResponse } from 'node:http';
7
7
  import https from 'node:https';
@@ -9,6 +9,19 @@ import http2 from 'node:http2';
9
9
  import { WebSocketServer } from 'ws';
10
10
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
11
11
 
12
+ declare class MockProcess {
13
+ emitter?: EventEmitter;
14
+ process?: NodeJS.Process;
15
+ constructor(opts?: {
16
+ emitter?: EventEmitter;
17
+ isNode?: boolean;
18
+ });
19
+ send(data?: any, callback?: (err?: Error) => void): void;
20
+ exit(flag?: number): void;
21
+ on(fn: (msg?: any) => any): void;
22
+ desctroy(): void;
23
+ }
24
+
12
25
  type RouterContextT = {
13
26
  code?: number;
14
27
  [key: string]: any;
@@ -174,11 +187,15 @@ declare class Route<U = {
174
187
  setData(data: any): this;
175
188
  throw(code?: number | string, message?: string, tips?: string): void;
176
189
  }
190
+ declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
191
+ declare const fromJSONSchema: (route: RouteInfo) => {
192
+ [key: string]: z.ZodTypeAny;
193
+ };
177
194
  /**
178
- * @parmas override 是否覆盖已存在的route,默认true
195
+ * @parmas overwrite 是否覆盖已存在的route,默认true
179
196
  */
180
197
  type AddOpts = {
181
- override?: boolean;
198
+ overwrite?: boolean;
182
199
  };
183
200
  declare class QueryRouter {
184
201
  appId: string;
@@ -336,12 +353,16 @@ declare class QueryRouter {
336
353
  key?: string;
337
354
  payload?: any;
338
355
  }, opts?: {
339
- emitter?: any;
356
+ mockProcess?: MockProcess;
340
357
  timeout?: number;
341
358
  getList?: boolean;
342
359
  force?: boolean;
343
360
  filter?: (route: Route) => boolean;
344
361
  }): Promise<void>;
362
+ static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
363
+ static fromJSONSchema: (route: RouteInfo) => {
364
+ [key: string]: z.ZodTypeAny;
365
+ };
345
366
  }
346
367
  type QueryRouterServerOpts = {
347
368
  handleFn?: HandleFn;
@@ -528,19 +549,6 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
528
549
  get routeObject(): T;
529
550
  }
530
551
 
531
- declare class MockProcess {
532
- emitter?: EventEmitter;
533
- process?: NodeJS.Process;
534
- constructor(opts?: {
535
- emitter?: EventEmitter;
536
- isNode?: boolean;
537
- });
538
- send(data?: any, callback?: (err?: Error) => void): void;
539
- exit(flag?: number): void;
540
- on(fn: (msg?: any) => any): void;
541
- desctroy(): void;
542
- }
543
-
544
552
  type Cors$2 = {
545
553
  /**
546
554
  * @default '*''
@@ -956,5 +964,5 @@ type GlobOptions = {
956
964
  };
957
965
  declare const loadTS: (match?: string, { cwd, load }?: GlobOptions) => Promise<any[]>;
958
966
 
959
- export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, handleServer, loadTS, tool, util };
967
+ export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, fromJSONSchema, handleServer, loadTS, toJSONSchema, tool, util };
960
968
  export type { HttpListenerFun, Listener, OnListener, OnWebSocketFn, RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, RouterReq, RouterRes, Rule, Run, Schema, Skill, WS, WebSocketListenerFun, WebSocketReq, WebSocketRes };
package/dist/router.js CHANGED
@@ -12196,7 +12196,7 @@ const allProcessors = {
12196
12196
  optional: optionalProcessor,
12197
12197
  lazy: lazyProcessor,
12198
12198
  };
12199
- function toJSONSchema(input, params) {
12199
+ function toJSONSchema$1(input, params) {
12200
12200
  if ("_idmap" in input) {
12201
12201
  // Registry case
12202
12202
  const registry = input;
@@ -12605,7 +12605,7 @@ var index = /*#__PURE__*/Object.freeze({
12605
12605
  safeParse: safeParse$1,
12606
12606
  safeParseAsync: safeParseAsync$1,
12607
12607
  toDotPath: toDotPath,
12608
- toJSONSchema: toJSONSchema,
12608
+ toJSONSchema: toJSONSchema$1,
12609
12609
  treeifyError: treeifyError,
12610
12610
  util: util$1,
12611
12611
  version: version
@@ -14650,7 +14650,7 @@ function convertSchema(schema, ctx) {
14650
14650
  }
14651
14651
  /**
14652
14652
  * Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change. */
14653
- function fromJSONSchema(schema, params) {
14653
+ function fromJSONSchema$1(schema, params) {
14654
14654
  // Handle boolean schemas
14655
14655
  if (typeof schema === "boolean") {
14656
14656
  return schema ? z$1.any() : z$1.never();
@@ -14817,7 +14817,7 @@ var z = /*#__PURE__*/Object.freeze({
14817
14817
  float32: float32,
14818
14818
  float64: float64,
14819
14819
  formatError: formatError,
14820
- fromJSONSchema: fromJSONSchema,
14820
+ fromJSONSchema: fromJSONSchema$1,
14821
14821
  function: _function,
14822
14822
  getErrorMap: getErrorMap,
14823
14823
  globalRegistry: globalRegistry,
@@ -14910,7 +14910,7 @@ var z = /*#__PURE__*/Object.freeze({
14910
14910
  superRefine: superRefine,
14911
14911
  symbol: symbol,
14912
14912
  templateLiteral: templateLiteral,
14913
- toJSONSchema: toJSONSchema,
14913
+ toJSONSchema: toJSONSchema$1,
14914
14914
  toLowerCase: _toLowerCase,
14915
14915
  toUpperCase: _toUpperCase,
14916
14916
  transform: transform,
@@ -15068,6 +15068,35 @@ class Route {
15068
15068
  throw new CustomError(...args);
15069
15069
  }
15070
15070
  }
15071
+ const toJSONSchema = (route) => {
15072
+ const pickValues = pick$1(route, pickValue);
15073
+ if (pickValues?.metadata?.args) {
15074
+ const args = pickValues.metadata.args;
15075
+ const keys = Object.keys(args);
15076
+ const newArgs = {};
15077
+ for (let key of keys) {
15078
+ const item = args[key];
15079
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
15080
+ newArgs[key] = item.toJSONSchema();
15081
+ }
15082
+ else {
15083
+ newArgs[key] = args[key]; // 可能不是schema
15084
+ }
15085
+ }
15086
+ pickValues.metadata.args = newArgs;
15087
+ }
15088
+ return pickValues;
15089
+ };
15090
+ const fromJSONSchema = (route) => {
15091
+ const args = route?.metadata?.args || {};
15092
+ const keys = Object.keys(args);
15093
+ const newArgs = {};
15094
+ for (let key of keys) {
15095
+ const item = args[key];
15096
+ newArgs[key] = fromJSONSchema$1(item);
15097
+ }
15098
+ return newArgs;
15099
+ };
15071
15100
  class QueryRouter {
15072
15101
  appId = '';
15073
15102
  routes;
@@ -15082,13 +15111,13 @@ class QueryRouter {
15082
15111
  * @param opts
15083
15112
  */
15084
15113
  add(route, opts) {
15085
- const override = opts?.override ?? true;
15114
+ const overwrite = opts?.overwrite ?? true;
15086
15115
  const has = this.routes.findIndex((r) => r.path === route.path && r.key === route.key);
15087
15116
  if (has !== -1) {
15088
- if (!override) {
15117
+ if (!overwrite) {
15089
15118
  return;
15090
15119
  }
15091
- // 如果存在,且override为true,则覆盖
15120
+ // 如果存在,且overwrite为true,则覆盖
15092
15121
  this.routes.splice(has, 1);
15093
15122
  }
15094
15123
  this.routes.push(route);
@@ -15387,7 +15416,24 @@ class QueryRouter {
15387
15416
  }
15388
15417
  getList(filter) {
15389
15418
  return this.routes.filter(filter || (() => true)).map((r) => {
15390
- return pick$1(r, pickValue);
15419
+ const pickValues = pick$1(r, pickValue);
15420
+ if (pickValues?.metadata?.args) {
15421
+ // const demoArgs = { k: tool.schema.string().describe('示例参数') };
15422
+ const args = pickValues.metadata.args;
15423
+ const keys = Object.keys(args);
15424
+ const newArgs = {};
15425
+ for (let key of keys) {
15426
+ const item = args[key];
15427
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
15428
+ newArgs[key] = item.toJSONSchema();
15429
+ }
15430
+ else {
15431
+ newArgs[key] = args[key]; // 可能不是schema
15432
+ }
15433
+ }
15434
+ pickValues.metadata.args = newArgs;
15435
+ }
15436
+ return pickValues;
15391
15437
  });
15392
15438
  }
15393
15439
  /**
@@ -15472,6 +15518,8 @@ class QueryRouter {
15472
15518
  }
15473
15519
  return listenProcess({ app: this, params, ...opts });
15474
15520
  }
15521
+ static toJSONSchema = toJSONSchema;
15522
+ static fromJSONSchema = fromJSONSchema;
15475
15523
  }
15476
15524
  /**
15477
15525
  * QueryRouterServer
@@ -21884,4 +21932,4 @@ const loadTS = async (match = './*.ts', { cwd = process.cwd(), load } = {}) => {
21884
21932
  return Promise.all(files.map((file) => (load ? load(file) : import(file))));
21885
21933
  };
21886
21934
 
21887
- export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, handleServer, loadTS, tool, util };
21935
+ export { App, CustomError, Mini, MockProcess, QueryRouter, QueryRouterServer, QueryUtil, Route, ServerNode, createSchema, createSkill, define, fromJSONSchema, handleServer, loadTS, toJSONSchema, tool, util };
package/dist/ws.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
2
3
  import * as http from 'node:http';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -51,6 +52,19 @@ declare class ReconnectingWebSocket {
51
52
  getRetryCount(): number;
52
53
  }
53
54
 
55
+ declare class MockProcess {
56
+ emitter?: EventEmitter;
57
+ process?: NodeJS.Process;
58
+ constructor(opts?: {
59
+ emitter?: EventEmitter;
60
+ isNode?: boolean;
61
+ });
62
+ send(data?: any, callback?: (err?: Error) => void): void;
63
+ exit(flag?: number): void;
64
+ on(fn: (msg?: any) => any): void;
65
+ desctroy(): void;
66
+ }
67
+
54
68
  type RouterContextT = {
55
69
  code?: number;
56
70
  [key: string]: any;
@@ -204,10 +218,10 @@ declare class Route<U = {
204
218
  throw(code?: number | string, message?: string, tips?: string): void;
205
219
  }
206
220
  /**
207
- * @parmas override 是否覆盖已存在的route,默认true
221
+ * @parmas overwrite 是否覆盖已存在的route,默认true
208
222
  */
209
223
  type AddOpts = {
210
- override?: boolean;
224
+ overwrite?: boolean;
211
225
  };
212
226
  declare class QueryRouter {
213
227
  appId: string;
@@ -365,12 +379,16 @@ declare class QueryRouter {
365
379
  key?: string;
366
380
  payload?: any;
367
381
  }, opts?: {
368
- emitter?: any;
382
+ mockProcess?: MockProcess;
369
383
  timeout?: number;
370
384
  getList?: boolean;
371
385
  force?: boolean;
372
386
  filter?: (route: Route) => boolean;
373
387
  }): Promise<void>;
388
+ static toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "key" | "path" | "id" | "description" | "type" | "middleware" | "metadata">;
389
+ static fromJSONSchema: (route: RouteInfo) => {
390
+ [key: string]: z.ZodTypeAny;
391
+ };
374
392
  }
375
393
  interface HandleFn<T = any> {
376
394
  (msg: {
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.67",
4
+ "version": "0.0.68",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/browser.ts CHANGED
@@ -8,7 +8,7 @@ export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
8
8
 
9
9
  export type { Run, Skill } from './route.ts';
10
10
 
11
- export { createSkill, tool } from './route.ts';
11
+ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
12
12
 
13
13
  export { CustomError } from './result/error.ts';
14
14
 
package/src/index.ts CHANGED
@@ -8,7 +8,7 @@ export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
8
8
 
9
9
  export type { Run, Skill } from './route.ts';
10
10
 
11
- export { createSkill, tool } from './route.ts';
11
+ export { createSkill, tool, fromJSONSchema, toJSONSchema } from './route.ts';
12
12
 
13
13
  export { CustomError } from './result/error.ts';
14
14
 
package/src/route.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { nanoid } from 'nanoid';
2
2
  import { CustomError } from './result/error.ts';
3
3
  import { pick } from './utils/pick.ts';
4
- import { listenProcess } from './utils/listen-process.ts';
4
+ import { listenProcess, MockProcess } from './utils/listen-process.ts';
5
5
  import { z } from 'zod';
6
6
  import { filter } from '@kevisual/js-filter'
7
7
 
@@ -243,11 +243,42 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
243
243
  throw new CustomError(...args);
244
244
  }
245
245
  }
246
+ export const toJSONSchema = (route: RouteInfo) => {
247
+ const pickValues = pick(route, pickValue as any);
248
+ if (pickValues?.metadata?.args) {
249
+ const args = pickValues.metadata.args;
250
+ const keys = Object.keys(args);
251
+ const newArgs: { [key: string]: any } = {};
252
+ for (let key of keys) {
253
+ const item = args[key] as z.ZodAny;
254
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
255
+ newArgs[key] = item.toJSONSchema();
256
+ } else {
257
+ newArgs[key] = args[key]; // 可能不是schema
258
+ }
259
+ }
260
+ pickValues.metadata.args = newArgs;
261
+ }
262
+ return pickValues;
263
+ }
264
+
265
+ export const fromJSONSchema = (route: RouteInfo): {
266
+ [key: string]: z.ZodTypeAny
267
+ } => {
268
+ const args = route?.metadata?.args || {};
269
+ const keys = Object.keys(args);
270
+ const newArgs: { [key: string]: any } = {};
271
+ for (let key of keys) {
272
+ const item = args[key];
273
+ newArgs[key] = z.fromJSONSchema(item);
274
+ }
275
+ return newArgs;
276
+ }
246
277
 
247
278
  /**
248
- * @parmas override 是否覆盖已存在的route,默认true
279
+ * @parmas overwrite 是否覆盖已存在的route,默认true
249
280
  */
250
- export type AddOpts = { override?: boolean };
281
+ export type AddOpts = { overwrite?: boolean };
251
282
  export class QueryRouter {
252
283
  appId: string = '';
253
284
  routes: Route[];
@@ -262,14 +293,14 @@ export class QueryRouter {
262
293
  * @param opts
263
294
  */
264
295
  add(route: Route, opts?: AddOpts) {
265
- const override = opts?.override ?? true;
296
+ const overwrite = opts?.overwrite ?? true;
266
297
  const has = this.routes.findIndex((r) => r.path === route.path && r.key === route.key);
267
298
 
268
299
  if (has !== -1) {
269
- if (!override) {
300
+ if (!overwrite) {
270
301
  return;
271
302
  }
272
- // 如果存在,且override为true,则覆盖
303
+ // 如果存在,且overwrite为true,则覆盖
273
304
  this.routes.splice(has, 1);
274
305
  }
275
306
  this.routes.push(route);
@@ -555,7 +586,23 @@ export class QueryRouter {
555
586
  }
556
587
  getList(filter?: (route: Route) => boolean): RouteInfo[] {
557
588
  return this.routes.filter(filter || (() => true)).map((r) => {
558
- return pick(r, pickValue as any);
589
+ const pickValues = pick(r, pickValue as any);
590
+ if (pickValues?.metadata?.args) {
591
+ // const demoArgs = { k: tool.schema.string().describe('示例参数') };
592
+ const args = pickValues.metadata.args;
593
+ const keys = Object.keys(args);
594
+ const newArgs: { [key: string]: any } = {};
595
+ for (let key of keys) {
596
+ const item = args[key] as z.ZodAny;
597
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
598
+ newArgs[key] = item.toJSONSchema();
599
+ } else {
600
+ newArgs[key] = args[key]; // 可能不是schema
601
+ }
602
+ }
603
+ pickValues.metadata.args = newArgs;
604
+ }
605
+ return pickValues;
559
606
  });
560
607
  }
561
608
  /**
@@ -634,7 +681,7 @@ export class QueryRouter {
634
681
  * -- .send
635
682
  */
636
683
  wait(params?: { path?: string; key?: string; payload?: any }, opts?: {
637
- emitter?: any,
684
+ mockProcess?: MockProcess,
638
685
  timeout?: number,
639
686
  getList?: boolean
640
687
  force?: boolean
@@ -646,6 +693,8 @@ export class QueryRouter {
646
693
  }
647
694
  return listenProcess({ app: this as any, params, ...opts });
648
695
  }
696
+ static toJSONSchema = toJSONSchema;
697
+ static fromJSONSchema = fromJSONSchema;
649
698
  }
650
699
 
651
700
  type QueryRouterServerOpts = {
@@ -729,4 +778,5 @@ export class QueryRouterServer extends QueryRouter {
729
778
  }
730
779
 
731
780
 
732
- export class Mini extends QueryRouterServer { }
781
+ export class Mini extends QueryRouterServer { }
782
+