@kevisual/router 0.0.78 → 0.0.80
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/dist/app.js +92 -77
- package/dist/opencode.d.ts +9 -2
- package/dist/router-browser.d.ts +18 -9
- package/dist/router-browser.js +93 -78
- package/dist/router-define.d.ts +9 -2
- package/dist/router.d.ts +18 -9
- package/dist/router.js +93 -78
- package/dist/ws.d.ts +9 -2
- package/package.json +1 -1
- package/src/route.ts +5 -93
- package/src/validator/schema.ts +100 -0
package/dist/app.js
CHANGED
|
@@ -16796,6 +16796,92 @@ var randomId = (length = 8, affix = "") => {
|
|
|
16796
16796
|
return affix + nanoid3(length);
|
|
16797
16797
|
};
|
|
16798
16798
|
|
|
16799
|
+
// src/validator/schema.ts
|
|
16800
|
+
var extractArgs = (args) => {
|
|
16801
|
+
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
16802
|
+
return args.shape;
|
|
16803
|
+
}
|
|
16804
|
+
return args || {};
|
|
16805
|
+
};
|
|
16806
|
+
var toJSONSchema2 = (args, opts) => {
|
|
16807
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
16808
|
+
if (!args)
|
|
16809
|
+
return {};
|
|
16810
|
+
const _override = ({ jsonSchema, path, zodSchema }) => {
|
|
16811
|
+
if (Array.isArray(path) && path.length > 0) {
|
|
16812
|
+
return;
|
|
16813
|
+
}
|
|
16814
|
+
const isOptional = zodSchema.isOptional?.();
|
|
16815
|
+
if (isOptional) {
|
|
16816
|
+
jsonSchema.optional = true;
|
|
16817
|
+
}
|
|
16818
|
+
};
|
|
16819
|
+
const isError = (keys2) => {
|
|
16820
|
+
const errorKeys = ["toJSONSchema", "def", "type", "parse"];
|
|
16821
|
+
const hasErrorKeys = errorKeys.every((key) => keys2.includes(key));
|
|
16822
|
+
return hasErrorKeys;
|
|
16823
|
+
};
|
|
16824
|
+
const override = opts?.override || _override;
|
|
16825
|
+
if (mergeObject) {
|
|
16826
|
+
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
16827
|
+
return args.toJSONSchema();
|
|
16828
|
+
}
|
|
16829
|
+
if (isError(Object.keys(args))) {
|
|
16830
|
+
return {};
|
|
16831
|
+
}
|
|
16832
|
+
const schema = exports_external.object(args);
|
|
16833
|
+
return schema.toJSONSchema();
|
|
16834
|
+
}
|
|
16835
|
+
args = extractArgs(args);
|
|
16836
|
+
let keys = Object.keys(args);
|
|
16837
|
+
if (isError(keys)) {
|
|
16838
|
+
console.error(`[toJSONSchema error]: 解析到的 schema 可能不正确,包含了zod默认的value的schema. 请检查输入的 schema 是否正确。`);
|
|
16839
|
+
args = {};
|
|
16840
|
+
keys = [];
|
|
16841
|
+
}
|
|
16842
|
+
if (mergeObject) {}
|
|
16843
|
+
let newArgs = {};
|
|
16844
|
+
for (let key of keys) {
|
|
16845
|
+
const item = args[key];
|
|
16846
|
+
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16847
|
+
newArgs[key] = item.toJSONSchema({ override });
|
|
16848
|
+
} else {
|
|
16849
|
+
newArgs[key] = args[key];
|
|
16850
|
+
}
|
|
16851
|
+
}
|
|
16852
|
+
return newArgs;
|
|
16853
|
+
};
|
|
16854
|
+
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16855
|
+
let resultArgs = null;
|
|
16856
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
16857
|
+
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16858
|
+
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16859
|
+
const extract = extractArgs(objectSchema);
|
|
16860
|
+
const keys = Object.keys(extract);
|
|
16861
|
+
const newArgs = {};
|
|
16862
|
+
for (let key of keys) {
|
|
16863
|
+
newArgs[key] = extract[key];
|
|
16864
|
+
}
|
|
16865
|
+
resultArgs = newArgs;
|
|
16866
|
+
}
|
|
16867
|
+
if (!resultArgs) {
|
|
16868
|
+
const keys = Object.keys(args);
|
|
16869
|
+
const newArgs = {};
|
|
16870
|
+
for (let key of keys) {
|
|
16871
|
+
const item = args[key];
|
|
16872
|
+
newArgs[key] = exports_external.fromJSONSchema(item);
|
|
16873
|
+
if (item.optional) {
|
|
16874
|
+
newArgs[key] = newArgs[key].optional();
|
|
16875
|
+
}
|
|
16876
|
+
}
|
|
16877
|
+
resultArgs = newArgs;
|
|
16878
|
+
}
|
|
16879
|
+
if (mergeObject) {
|
|
16880
|
+
resultArgs = exports_external.object(resultArgs);
|
|
16881
|
+
}
|
|
16882
|
+
return resultArgs;
|
|
16883
|
+
};
|
|
16884
|
+
|
|
16799
16885
|
// src/route.ts
|
|
16800
16886
|
var pickValue = ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
16801
16887
|
var tool = {
|
|
@@ -16919,72 +17005,15 @@ class Route {
|
|
|
16919
17005
|
throw new CustomError(...args);
|
|
16920
17006
|
}
|
|
16921
17007
|
}
|
|
16922
|
-
var extractArgs = (args) => {
|
|
16923
|
-
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
16924
|
-
return args.shape;
|
|
16925
|
-
}
|
|
16926
|
-
return args || {};
|
|
16927
|
-
};
|
|
16928
17008
|
var toJSONSchemaRoute = (route) => {
|
|
16929
17009
|
const pickValues = pick(route, pickValue);
|
|
16930
17010
|
if (pickValues?.metadata?.args) {
|
|
16931
|
-
|
|
16932
|
-
args = extractArgs(args);
|
|
16933
|
-
const keys = Object.keys(args);
|
|
16934
|
-
const newArgs = {};
|
|
16935
|
-
for (let key of keys) {
|
|
16936
|
-
const item = args[key];
|
|
16937
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16938
|
-
newArgs[key] = item.toJSONSchema();
|
|
16939
|
-
} else {
|
|
16940
|
-
newArgs[key] = args[key];
|
|
16941
|
-
}
|
|
16942
|
-
}
|
|
16943
|
-
pickValues.metadata.args = newArgs;
|
|
17011
|
+
pickValues.metadata.args = toJSONSchema3(pickValues?.metadata?.args, { mergeObject: false });
|
|
16944
17012
|
}
|
|
16945
17013
|
return pickValues;
|
|
16946
17014
|
};
|
|
16947
|
-
var
|
|
16948
|
-
|
|
16949
|
-
const keys = Object.keys(args);
|
|
16950
|
-
const newArgs = {};
|
|
16951
|
-
for (let key of keys) {
|
|
16952
|
-
const item = args[key];
|
|
16953
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16954
|
-
newArgs[key] = item.toJSONSchema();
|
|
16955
|
-
} else {
|
|
16956
|
-
newArgs[key] = args[key];
|
|
16957
|
-
}
|
|
16958
|
-
}
|
|
16959
|
-
return newArgs;
|
|
16960
|
-
};
|
|
16961
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16962
|
-
let resultArgs = null;
|
|
16963
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
16964
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16965
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16966
|
-
const extract = extractArgs(objectSchema);
|
|
16967
|
-
const keys = Object.keys(extract);
|
|
16968
|
-
const newArgs = {};
|
|
16969
|
-
for (let key of keys) {
|
|
16970
|
-
newArgs[key] = extract[key];
|
|
16971
|
-
}
|
|
16972
|
-
resultArgs = newArgs;
|
|
16973
|
-
}
|
|
16974
|
-
if (!resultArgs) {
|
|
16975
|
-
const keys = Object.keys(args);
|
|
16976
|
-
const newArgs = {};
|
|
16977
|
-
for (let key of keys) {
|
|
16978
|
-
const item = args[key];
|
|
16979
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
16980
|
-
}
|
|
16981
|
-
resultArgs = newArgs;
|
|
16982
|
-
}
|
|
16983
|
-
if (mergeObject) {
|
|
16984
|
-
resultArgs = exports_external.object(resultArgs);
|
|
16985
|
-
}
|
|
16986
|
-
return resultArgs;
|
|
16987
|
-
};
|
|
17015
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
17016
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
16988
17017
|
|
|
16989
17018
|
class QueryRouter {
|
|
16990
17019
|
appId = "";
|
|
@@ -17238,20 +17267,6 @@ class QueryRouter {
|
|
|
17238
17267
|
getList(filter) {
|
|
17239
17268
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
17240
17269
|
const pickValues = pick(r, pickValue);
|
|
17241
|
-
if (pickValues?.metadata?.args) {
|
|
17242
|
-
const args = pickValues.metadata.args;
|
|
17243
|
-
const keys = Object.keys(args);
|
|
17244
|
-
const newArgs = {};
|
|
17245
|
-
for (let key of keys) {
|
|
17246
|
-
const item = args[key];
|
|
17247
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
17248
|
-
newArgs[key] = item.toJSONSchema();
|
|
17249
|
-
} else {
|
|
17250
|
-
newArgs[key] = args[key];
|
|
17251
|
-
}
|
|
17252
|
-
}
|
|
17253
|
-
pickValues.metadata.args = newArgs;
|
|
17254
|
-
}
|
|
17255
17270
|
return pickValues;
|
|
17256
17271
|
});
|
|
17257
17272
|
}
|
|
@@ -17339,8 +17354,8 @@ class QueryRouter {
|
|
|
17339
17354
|
}
|
|
17340
17355
|
return listenProcess({ app: this, params, ...opts });
|
|
17341
17356
|
}
|
|
17342
|
-
toJSONSchema =
|
|
17343
|
-
fromJSONSchema =
|
|
17357
|
+
toJSONSchema = toJSONSchema3;
|
|
17358
|
+
fromJSONSchema = fromJSONSchema3;
|
|
17344
17359
|
}
|
|
17345
17360
|
|
|
17346
17361
|
// src/server/server.ts
|
|
@@ -19507,7 +19522,7 @@ app
|
|
|
19507
19522
|
10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
|
|
19508
19523
|
`;
|
|
19509
19524
|
// package.json
|
|
19510
|
-
var version2 = "0.0.
|
|
19525
|
+
var version2 = "0.0.80";
|
|
19511
19526
|
|
|
19512
19527
|
// agent/routes/route-create.ts
|
|
19513
19528
|
app.route({
|
package/dist/opencode.d.ts
CHANGED
|
@@ -348,10 +348,17 @@ declare class QueryRouter {
|
|
|
348
348
|
filter?: (route: Route) => boolean;
|
|
349
349
|
routeListMiddleware?: string[];
|
|
350
350
|
}): Promise<void>;
|
|
351
|
-
toJSONSchema: (args: any
|
|
351
|
+
toJSONSchema: (args: any, opts?: {
|
|
352
|
+
mergeObject?: boolean;
|
|
353
|
+
override?: (opts: {
|
|
354
|
+
jsonSchema: any;
|
|
355
|
+
path: string[];
|
|
356
|
+
zodSchema: z.ZodTypeAny;
|
|
357
|
+
}) => void;
|
|
358
|
+
}) => {
|
|
352
359
|
[key: string]: any;
|
|
353
360
|
};
|
|
354
|
-
fromJSONSchema: <Merge extends boolean =
|
|
361
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
355
362
|
mergeObject?: boolean;
|
|
356
363
|
}) => Merge extends true ? z.ZodObject<{
|
|
357
364
|
[key: string]: any;
|
package/dist/router-browser.d.ts
CHANGED
|
@@ -204,15 +204,17 @@ declare class Route<U = {
|
|
|
204
204
|
}, opts?: AddOpts): void;
|
|
205
205
|
throw(code?: number | string, message?: string, tips?: string): void;
|
|
206
206
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
207
|
+
declare const toJSONSchema: (args: any, opts?: {
|
|
208
|
+
mergeObject?: boolean;
|
|
209
|
+
override?: (opts: {
|
|
210
|
+
jsonSchema: any;
|
|
211
|
+
path: string[];
|
|
212
|
+
zodSchema: z.ZodTypeAny;
|
|
213
|
+
}) => void;
|
|
214
|
+
}) => {
|
|
213
215
|
[key: string]: any;
|
|
214
216
|
};
|
|
215
|
-
declare const fromJSONSchema: <Merge extends boolean =
|
|
217
|
+
declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
216
218
|
mergeObject?: boolean;
|
|
217
219
|
}) => Merge extends true ? z.ZodObject<{
|
|
218
220
|
[key: string]: any;
|
|
@@ -390,10 +392,17 @@ declare class QueryRouter {
|
|
|
390
392
|
filter?: (route: Route) => boolean;
|
|
391
393
|
routeListMiddleware?: string[];
|
|
392
394
|
}): Promise<void>;
|
|
393
|
-
toJSONSchema: (args: any
|
|
395
|
+
toJSONSchema: (args: any, opts?: {
|
|
396
|
+
mergeObject?: boolean;
|
|
397
|
+
override?: (opts: {
|
|
398
|
+
jsonSchema: any;
|
|
399
|
+
path: string[];
|
|
400
|
+
zodSchema: z.ZodTypeAny;
|
|
401
|
+
}) => void;
|
|
402
|
+
}) => {
|
|
394
403
|
[key: string]: any;
|
|
395
404
|
};
|
|
396
|
-
fromJSONSchema: <Merge extends boolean =
|
|
405
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
397
406
|
mergeObject?: boolean;
|
|
398
407
|
}) => Merge extends true ? z.ZodObject<{
|
|
399
408
|
[key: string]: any;
|
package/dist/router-browser.js
CHANGED
|
@@ -13960,6 +13960,92 @@ var randomId = (length = 8, affix = "") => {
|
|
|
13960
13960
|
return affix + nanoid3(length);
|
|
13961
13961
|
};
|
|
13962
13962
|
|
|
13963
|
+
// src/validator/schema.ts
|
|
13964
|
+
var extractArgs = (args) => {
|
|
13965
|
+
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
13966
|
+
return args.shape;
|
|
13967
|
+
}
|
|
13968
|
+
return args || {};
|
|
13969
|
+
};
|
|
13970
|
+
var toJSONSchema2 = (args, opts) => {
|
|
13971
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
13972
|
+
if (!args)
|
|
13973
|
+
return {};
|
|
13974
|
+
const _override = ({ jsonSchema, path, zodSchema }) => {
|
|
13975
|
+
if (Array.isArray(path) && path.length > 0) {
|
|
13976
|
+
return;
|
|
13977
|
+
}
|
|
13978
|
+
const isOptional = zodSchema.isOptional?.();
|
|
13979
|
+
if (isOptional) {
|
|
13980
|
+
jsonSchema.optional = true;
|
|
13981
|
+
}
|
|
13982
|
+
};
|
|
13983
|
+
const isError = (keys2) => {
|
|
13984
|
+
const errorKeys = ["toJSONSchema", "def", "type", "parse"];
|
|
13985
|
+
const hasErrorKeys = errorKeys.every((key) => keys2.includes(key));
|
|
13986
|
+
return hasErrorKeys;
|
|
13987
|
+
};
|
|
13988
|
+
const override = opts?.override || _override;
|
|
13989
|
+
if (mergeObject) {
|
|
13990
|
+
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
13991
|
+
return args.toJSONSchema();
|
|
13992
|
+
}
|
|
13993
|
+
if (isError(Object.keys(args))) {
|
|
13994
|
+
return {};
|
|
13995
|
+
}
|
|
13996
|
+
const schema = exports_external.object(args);
|
|
13997
|
+
return schema.toJSONSchema();
|
|
13998
|
+
}
|
|
13999
|
+
args = extractArgs(args);
|
|
14000
|
+
let keys = Object.keys(args);
|
|
14001
|
+
if (isError(keys)) {
|
|
14002
|
+
console.error(`[toJSONSchema error]: 解析到的 schema 可能不正确,包含了zod默认的value的schema. 请检查输入的 schema 是否正确。`);
|
|
14003
|
+
args = {};
|
|
14004
|
+
keys = [];
|
|
14005
|
+
}
|
|
14006
|
+
if (mergeObject) {}
|
|
14007
|
+
let newArgs = {};
|
|
14008
|
+
for (let key of keys) {
|
|
14009
|
+
const item = args[key];
|
|
14010
|
+
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14011
|
+
newArgs[key] = item.toJSONSchema({ override });
|
|
14012
|
+
} else {
|
|
14013
|
+
newArgs[key] = args[key];
|
|
14014
|
+
}
|
|
14015
|
+
}
|
|
14016
|
+
return newArgs;
|
|
14017
|
+
};
|
|
14018
|
+
var fromJSONSchema2 = (args = {}, opts) => {
|
|
14019
|
+
let resultArgs = null;
|
|
14020
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
14021
|
+
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
14022
|
+
const objectSchema = exports_external.fromJSONSchema(args);
|
|
14023
|
+
const extract = extractArgs(objectSchema);
|
|
14024
|
+
const keys = Object.keys(extract);
|
|
14025
|
+
const newArgs = {};
|
|
14026
|
+
for (let key of keys) {
|
|
14027
|
+
newArgs[key] = extract[key];
|
|
14028
|
+
}
|
|
14029
|
+
resultArgs = newArgs;
|
|
14030
|
+
}
|
|
14031
|
+
if (!resultArgs) {
|
|
14032
|
+
const keys = Object.keys(args);
|
|
14033
|
+
const newArgs = {};
|
|
14034
|
+
for (let key of keys) {
|
|
14035
|
+
const item = args[key];
|
|
14036
|
+
newArgs[key] = exports_external.fromJSONSchema(item);
|
|
14037
|
+
if (item.optional) {
|
|
14038
|
+
newArgs[key] = newArgs[key].optional();
|
|
14039
|
+
}
|
|
14040
|
+
}
|
|
14041
|
+
resultArgs = newArgs;
|
|
14042
|
+
}
|
|
14043
|
+
if (mergeObject) {
|
|
14044
|
+
resultArgs = exports_external.object(resultArgs);
|
|
14045
|
+
}
|
|
14046
|
+
return resultArgs;
|
|
14047
|
+
};
|
|
14048
|
+
|
|
13963
14049
|
// src/route.ts
|
|
13964
14050
|
var pickValue = ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
13965
14051
|
var tool = {
|
|
@@ -14083,72 +14169,15 @@ class Route {
|
|
|
14083
14169
|
throw new CustomError(...args);
|
|
14084
14170
|
}
|
|
14085
14171
|
}
|
|
14086
|
-
var extractArgs = (args) => {
|
|
14087
|
-
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
14088
|
-
return args.shape;
|
|
14089
|
-
}
|
|
14090
|
-
return args || {};
|
|
14091
|
-
};
|
|
14092
14172
|
var toJSONSchemaRoute = (route) => {
|
|
14093
14173
|
const pickValues = pick(route, pickValue);
|
|
14094
14174
|
if (pickValues?.metadata?.args) {
|
|
14095
|
-
|
|
14096
|
-
args = extractArgs(args);
|
|
14097
|
-
const keys = Object.keys(args);
|
|
14098
|
-
const newArgs = {};
|
|
14099
|
-
for (let key of keys) {
|
|
14100
|
-
const item = args[key];
|
|
14101
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14102
|
-
newArgs[key] = item.toJSONSchema();
|
|
14103
|
-
} else {
|
|
14104
|
-
newArgs[key] = args[key];
|
|
14105
|
-
}
|
|
14106
|
-
}
|
|
14107
|
-
pickValues.metadata.args = newArgs;
|
|
14175
|
+
pickValues.metadata.args = toJSONSchema3(pickValues?.metadata?.args, { mergeObject: false });
|
|
14108
14176
|
}
|
|
14109
14177
|
return pickValues;
|
|
14110
14178
|
};
|
|
14111
|
-
var
|
|
14112
|
-
|
|
14113
|
-
const keys = Object.keys(args);
|
|
14114
|
-
const newArgs = {};
|
|
14115
|
-
for (let key of keys) {
|
|
14116
|
-
const item = args[key];
|
|
14117
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14118
|
-
newArgs[key] = item.toJSONSchema();
|
|
14119
|
-
} else {
|
|
14120
|
-
newArgs[key] = args[key];
|
|
14121
|
-
}
|
|
14122
|
-
}
|
|
14123
|
-
return newArgs;
|
|
14124
|
-
};
|
|
14125
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
14126
|
-
let resultArgs = null;
|
|
14127
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
14128
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
14129
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
14130
|
-
const extract = extractArgs(objectSchema);
|
|
14131
|
-
const keys = Object.keys(extract);
|
|
14132
|
-
const newArgs = {};
|
|
14133
|
-
for (let key of keys) {
|
|
14134
|
-
newArgs[key] = extract[key];
|
|
14135
|
-
}
|
|
14136
|
-
resultArgs = newArgs;
|
|
14137
|
-
}
|
|
14138
|
-
if (!resultArgs) {
|
|
14139
|
-
const keys = Object.keys(args);
|
|
14140
|
-
const newArgs = {};
|
|
14141
|
-
for (let key of keys) {
|
|
14142
|
-
const item = args[key];
|
|
14143
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
14144
|
-
}
|
|
14145
|
-
resultArgs = newArgs;
|
|
14146
|
-
}
|
|
14147
|
-
if (mergeObject) {
|
|
14148
|
-
resultArgs = exports_external.object(resultArgs);
|
|
14149
|
-
}
|
|
14150
|
-
return resultArgs;
|
|
14151
|
-
};
|
|
14179
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
14180
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
14152
14181
|
|
|
14153
14182
|
class QueryRouter {
|
|
14154
14183
|
appId = "";
|
|
@@ -14402,20 +14431,6 @@ class QueryRouter {
|
|
|
14402
14431
|
getList(filter) {
|
|
14403
14432
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
14404
14433
|
const pickValues = pick(r, pickValue);
|
|
14405
|
-
if (pickValues?.metadata?.args) {
|
|
14406
|
-
const args = pickValues.metadata.args;
|
|
14407
|
-
const keys = Object.keys(args);
|
|
14408
|
-
const newArgs = {};
|
|
14409
|
-
for (let key of keys) {
|
|
14410
|
-
const item = args[key];
|
|
14411
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14412
|
-
newArgs[key] = item.toJSONSchema();
|
|
14413
|
-
} else {
|
|
14414
|
-
newArgs[key] = args[key];
|
|
14415
|
-
}
|
|
14416
|
-
}
|
|
14417
|
-
pickValues.metadata.args = newArgs;
|
|
14418
|
-
}
|
|
14419
14434
|
return pickValues;
|
|
14420
14435
|
});
|
|
14421
14436
|
}
|
|
@@ -14503,8 +14518,8 @@ class QueryRouter {
|
|
|
14503
14518
|
}
|
|
14504
14519
|
return listenProcess({ app: this, params, ...opts });
|
|
14505
14520
|
}
|
|
14506
|
-
toJSONSchema =
|
|
14507
|
-
fromJSONSchema =
|
|
14521
|
+
toJSONSchema = toJSONSchema3;
|
|
14522
|
+
fromJSONSchema = fromJSONSchema3;
|
|
14508
14523
|
}
|
|
14509
14524
|
|
|
14510
14525
|
class QueryRouterServer extends QueryRouter {
|
|
@@ -14743,8 +14758,8 @@ var App = QueryRouterServer;
|
|
|
14743
14758
|
export {
|
|
14744
14759
|
util,
|
|
14745
14760
|
tool,
|
|
14746
|
-
|
|
14747
|
-
|
|
14761
|
+
toJSONSchema3 as toJSONSchema,
|
|
14762
|
+
fromJSONSchema3 as fromJSONSchema,
|
|
14748
14763
|
define,
|
|
14749
14764
|
createSkill,
|
|
14750
14765
|
createSchema,
|
package/dist/router-define.d.ts
CHANGED
|
@@ -345,10 +345,17 @@ declare class QueryRouter {
|
|
|
345
345
|
filter?: (route: Route) => boolean;
|
|
346
346
|
routeListMiddleware?: string[];
|
|
347
347
|
}): Promise<void>;
|
|
348
|
-
toJSONSchema: (args: any
|
|
348
|
+
toJSONSchema: (args: any, opts?: {
|
|
349
|
+
mergeObject?: boolean;
|
|
350
|
+
override?: (opts: {
|
|
351
|
+
jsonSchema: any;
|
|
352
|
+
path: string[];
|
|
353
|
+
zodSchema: z.ZodTypeAny;
|
|
354
|
+
}) => void;
|
|
355
|
+
}) => {
|
|
349
356
|
[key: string]: any;
|
|
350
357
|
};
|
|
351
|
-
fromJSONSchema: <Merge extends boolean =
|
|
358
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
352
359
|
mergeObject?: boolean;
|
|
353
360
|
}) => Merge extends true ? z.ZodObject<{
|
|
354
361
|
[key: string]: any;
|
package/dist/router.d.ts
CHANGED
|
@@ -210,15 +210,17 @@ declare class Route<U = {
|
|
|
210
210
|
}, opts?: AddOpts): void;
|
|
211
211
|
throw(code?: number | string, message?: string, tips?: string): void;
|
|
212
212
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
213
|
+
declare const toJSONSchema: (args: any, opts?: {
|
|
214
|
+
mergeObject?: boolean;
|
|
215
|
+
override?: (opts: {
|
|
216
|
+
jsonSchema: any;
|
|
217
|
+
path: string[];
|
|
218
|
+
zodSchema: z.ZodTypeAny;
|
|
219
|
+
}) => void;
|
|
220
|
+
}) => {
|
|
219
221
|
[key: string]: any;
|
|
220
222
|
};
|
|
221
|
-
declare const fromJSONSchema: <Merge extends boolean =
|
|
223
|
+
declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
222
224
|
mergeObject?: boolean;
|
|
223
225
|
}) => Merge extends true ? z.ZodObject<{
|
|
224
226
|
[key: string]: any;
|
|
@@ -396,10 +398,17 @@ declare class QueryRouter {
|
|
|
396
398
|
filter?: (route: Route) => boolean;
|
|
397
399
|
routeListMiddleware?: string[];
|
|
398
400
|
}): Promise<void>;
|
|
399
|
-
toJSONSchema: (args: any
|
|
401
|
+
toJSONSchema: (args: any, opts?: {
|
|
402
|
+
mergeObject?: boolean;
|
|
403
|
+
override?: (opts: {
|
|
404
|
+
jsonSchema: any;
|
|
405
|
+
path: string[];
|
|
406
|
+
zodSchema: z.ZodTypeAny;
|
|
407
|
+
}) => void;
|
|
408
|
+
}) => {
|
|
400
409
|
[key: string]: any;
|
|
401
410
|
};
|
|
402
|
-
fromJSONSchema: <Merge extends boolean =
|
|
411
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
403
412
|
mergeObject?: boolean;
|
|
404
413
|
}) => Merge extends true ? z.ZodObject<{
|
|
405
414
|
[key: string]: any;
|
package/dist/router.js
CHANGED
|
@@ -16793,6 +16793,92 @@ var randomId = (length = 8, affix = "") => {
|
|
|
16793
16793
|
return affix + nanoid3(length);
|
|
16794
16794
|
};
|
|
16795
16795
|
|
|
16796
|
+
// src/validator/schema.ts
|
|
16797
|
+
var extractArgs = (args) => {
|
|
16798
|
+
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
16799
|
+
return args.shape;
|
|
16800
|
+
}
|
|
16801
|
+
return args || {};
|
|
16802
|
+
};
|
|
16803
|
+
var toJSONSchema2 = (args, opts) => {
|
|
16804
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
16805
|
+
if (!args)
|
|
16806
|
+
return {};
|
|
16807
|
+
const _override = ({ jsonSchema, path, zodSchema }) => {
|
|
16808
|
+
if (Array.isArray(path) && path.length > 0) {
|
|
16809
|
+
return;
|
|
16810
|
+
}
|
|
16811
|
+
const isOptional = zodSchema.isOptional?.();
|
|
16812
|
+
if (isOptional) {
|
|
16813
|
+
jsonSchema.optional = true;
|
|
16814
|
+
}
|
|
16815
|
+
};
|
|
16816
|
+
const isError = (keys2) => {
|
|
16817
|
+
const errorKeys = ["toJSONSchema", "def", "type", "parse"];
|
|
16818
|
+
const hasErrorKeys = errorKeys.every((key) => keys2.includes(key));
|
|
16819
|
+
return hasErrorKeys;
|
|
16820
|
+
};
|
|
16821
|
+
const override = opts?.override || _override;
|
|
16822
|
+
if (mergeObject) {
|
|
16823
|
+
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
16824
|
+
return args.toJSONSchema();
|
|
16825
|
+
}
|
|
16826
|
+
if (isError(Object.keys(args))) {
|
|
16827
|
+
return {};
|
|
16828
|
+
}
|
|
16829
|
+
const schema = exports_external.object(args);
|
|
16830
|
+
return schema.toJSONSchema();
|
|
16831
|
+
}
|
|
16832
|
+
args = extractArgs(args);
|
|
16833
|
+
let keys = Object.keys(args);
|
|
16834
|
+
if (isError(keys)) {
|
|
16835
|
+
console.error(`[toJSONSchema error]: 解析到的 schema 可能不正确,包含了zod默认的value的schema. 请检查输入的 schema 是否正确。`);
|
|
16836
|
+
args = {};
|
|
16837
|
+
keys = [];
|
|
16838
|
+
}
|
|
16839
|
+
if (mergeObject) {}
|
|
16840
|
+
let newArgs = {};
|
|
16841
|
+
for (let key of keys) {
|
|
16842
|
+
const item = args[key];
|
|
16843
|
+
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16844
|
+
newArgs[key] = item.toJSONSchema({ override });
|
|
16845
|
+
} else {
|
|
16846
|
+
newArgs[key] = args[key];
|
|
16847
|
+
}
|
|
16848
|
+
}
|
|
16849
|
+
return newArgs;
|
|
16850
|
+
};
|
|
16851
|
+
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16852
|
+
let resultArgs = null;
|
|
16853
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
16854
|
+
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16855
|
+
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16856
|
+
const extract = extractArgs(objectSchema);
|
|
16857
|
+
const keys = Object.keys(extract);
|
|
16858
|
+
const newArgs = {};
|
|
16859
|
+
for (let key of keys) {
|
|
16860
|
+
newArgs[key] = extract[key];
|
|
16861
|
+
}
|
|
16862
|
+
resultArgs = newArgs;
|
|
16863
|
+
}
|
|
16864
|
+
if (!resultArgs) {
|
|
16865
|
+
const keys = Object.keys(args);
|
|
16866
|
+
const newArgs = {};
|
|
16867
|
+
for (let key of keys) {
|
|
16868
|
+
const item = args[key];
|
|
16869
|
+
newArgs[key] = exports_external.fromJSONSchema(item);
|
|
16870
|
+
if (item.optional) {
|
|
16871
|
+
newArgs[key] = newArgs[key].optional();
|
|
16872
|
+
}
|
|
16873
|
+
}
|
|
16874
|
+
resultArgs = newArgs;
|
|
16875
|
+
}
|
|
16876
|
+
if (mergeObject) {
|
|
16877
|
+
resultArgs = exports_external.object(resultArgs);
|
|
16878
|
+
}
|
|
16879
|
+
return resultArgs;
|
|
16880
|
+
};
|
|
16881
|
+
|
|
16796
16882
|
// src/route.ts
|
|
16797
16883
|
var pickValue = ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
16798
16884
|
var tool = {
|
|
@@ -16916,72 +17002,15 @@ class Route {
|
|
|
16916
17002
|
throw new CustomError(...args);
|
|
16917
17003
|
}
|
|
16918
17004
|
}
|
|
16919
|
-
var extractArgs = (args) => {
|
|
16920
|
-
if (args && typeof args === "object" && typeof args.shape === "object") {
|
|
16921
|
-
return args.shape;
|
|
16922
|
-
}
|
|
16923
|
-
return args || {};
|
|
16924
|
-
};
|
|
16925
17005
|
var toJSONSchemaRoute = (route) => {
|
|
16926
17006
|
const pickValues = pick(route, pickValue);
|
|
16927
17007
|
if (pickValues?.metadata?.args) {
|
|
16928
|
-
|
|
16929
|
-
args = extractArgs(args);
|
|
16930
|
-
const keys = Object.keys(args);
|
|
16931
|
-
const newArgs = {};
|
|
16932
|
-
for (let key of keys) {
|
|
16933
|
-
const item = args[key];
|
|
16934
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16935
|
-
newArgs[key] = item.toJSONSchema();
|
|
16936
|
-
} else {
|
|
16937
|
-
newArgs[key] = args[key];
|
|
16938
|
-
}
|
|
16939
|
-
}
|
|
16940
|
-
pickValues.metadata.args = newArgs;
|
|
17008
|
+
pickValues.metadata.args = toJSONSchema3(pickValues?.metadata?.args, { mergeObject: false });
|
|
16941
17009
|
}
|
|
16942
17010
|
return pickValues;
|
|
16943
17011
|
};
|
|
16944
|
-
var
|
|
16945
|
-
|
|
16946
|
-
const keys = Object.keys(args);
|
|
16947
|
-
const newArgs = {};
|
|
16948
|
-
for (let key of keys) {
|
|
16949
|
-
const item = args[key];
|
|
16950
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16951
|
-
newArgs[key] = item.toJSONSchema();
|
|
16952
|
-
} else {
|
|
16953
|
-
newArgs[key] = args[key];
|
|
16954
|
-
}
|
|
16955
|
-
}
|
|
16956
|
-
return newArgs;
|
|
16957
|
-
};
|
|
16958
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16959
|
-
let resultArgs = null;
|
|
16960
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
16961
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16962
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16963
|
-
const extract = extractArgs(objectSchema);
|
|
16964
|
-
const keys = Object.keys(extract);
|
|
16965
|
-
const newArgs = {};
|
|
16966
|
-
for (let key of keys) {
|
|
16967
|
-
newArgs[key] = extract[key];
|
|
16968
|
-
}
|
|
16969
|
-
resultArgs = newArgs;
|
|
16970
|
-
}
|
|
16971
|
-
if (!resultArgs) {
|
|
16972
|
-
const keys = Object.keys(args);
|
|
16973
|
-
const newArgs = {};
|
|
16974
|
-
for (let key of keys) {
|
|
16975
|
-
const item = args[key];
|
|
16976
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
16977
|
-
}
|
|
16978
|
-
resultArgs = newArgs;
|
|
16979
|
-
}
|
|
16980
|
-
if (mergeObject) {
|
|
16981
|
-
resultArgs = exports_external.object(resultArgs);
|
|
16982
|
-
}
|
|
16983
|
-
return resultArgs;
|
|
16984
|
-
};
|
|
17012
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
17013
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
16985
17014
|
|
|
16986
17015
|
class QueryRouter {
|
|
16987
17016
|
appId = "";
|
|
@@ -17235,20 +17264,6 @@ class QueryRouter {
|
|
|
17235
17264
|
getList(filter) {
|
|
17236
17265
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
17237
17266
|
const pickValues = pick(r, pickValue);
|
|
17238
|
-
if (pickValues?.metadata?.args) {
|
|
17239
|
-
const args = pickValues.metadata.args;
|
|
17240
|
-
const keys = Object.keys(args);
|
|
17241
|
-
const newArgs = {};
|
|
17242
|
-
for (let key of keys) {
|
|
17243
|
-
const item = args[key];
|
|
17244
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
17245
|
-
newArgs[key] = item.toJSONSchema();
|
|
17246
|
-
} else {
|
|
17247
|
-
newArgs[key] = args[key];
|
|
17248
|
-
}
|
|
17249
|
-
}
|
|
17250
|
-
pickValues.metadata.args = newArgs;
|
|
17251
|
-
}
|
|
17252
17267
|
return pickValues;
|
|
17253
17268
|
});
|
|
17254
17269
|
}
|
|
@@ -17336,8 +17351,8 @@ class QueryRouter {
|
|
|
17336
17351
|
}
|
|
17337
17352
|
return listenProcess({ app: this, params, ...opts });
|
|
17338
17353
|
}
|
|
17339
|
-
toJSONSchema =
|
|
17340
|
-
fromJSONSchema =
|
|
17354
|
+
toJSONSchema = toJSONSchema3;
|
|
17355
|
+
fromJSONSchema = fromJSONSchema3;
|
|
17341
17356
|
}
|
|
17342
17357
|
|
|
17343
17358
|
class QueryRouterServer extends QueryRouter {
|
|
@@ -18535,9 +18550,9 @@ class App extends QueryRouter {
|
|
|
18535
18550
|
export {
|
|
18536
18551
|
util,
|
|
18537
18552
|
tool,
|
|
18538
|
-
|
|
18553
|
+
toJSONSchema3 as toJSONSchema,
|
|
18539
18554
|
handleServer,
|
|
18540
|
-
|
|
18555
|
+
fromJSONSchema3 as fromJSONSchema,
|
|
18541
18556
|
define,
|
|
18542
18557
|
createSkill,
|
|
18543
18558
|
createSchema,
|
package/dist/ws.d.ts
CHANGED
|
@@ -395,10 +395,17 @@ declare class QueryRouter {
|
|
|
395
395
|
filter?: (route: Route) => boolean;
|
|
396
396
|
routeListMiddleware?: string[];
|
|
397
397
|
}): Promise<void>;
|
|
398
|
-
toJSONSchema: (args: any
|
|
398
|
+
toJSONSchema: (args: any, opts?: {
|
|
399
|
+
mergeObject?: boolean;
|
|
400
|
+
override?: (opts: {
|
|
401
|
+
jsonSchema: any;
|
|
402
|
+
path: string[];
|
|
403
|
+
zodSchema: z.ZodTypeAny;
|
|
404
|
+
}) => void;
|
|
405
|
+
}) => {
|
|
399
406
|
[key: string]: any;
|
|
400
407
|
};
|
|
401
|
-
fromJSONSchema: <Merge extends boolean =
|
|
408
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
402
409
|
mergeObject?: boolean;
|
|
403
410
|
}) => Merge extends true ? z.ZodObject<{
|
|
404
411
|
[key: string]: any;
|
package/package.json
CHANGED
package/src/route.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { pick } from './utils/pick.ts';
|
|
|
3
3
|
import { listenProcess, MockProcess } from './utils/listen-process.ts';
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { randomId } from './utils/random.ts';
|
|
6
|
+
import * as schema from './validator/schema.ts';
|
|
7
|
+
|
|
6
8
|
export type RouterContextT = { code?: number;[key: string]: any };
|
|
7
9
|
export type RouteContext<T = { code?: number }, S = any> = {
|
|
8
10
|
/**
|
|
@@ -245,92 +247,17 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
|
|
245
247
|
throw new CustomError(...args);
|
|
246
248
|
}
|
|
247
249
|
}
|
|
248
|
-
export const extractArgs = (args: any) => {
|
|
249
|
-
if (args && typeof args === 'object' && typeof args.shape === 'object') {
|
|
250
|
-
return args.shape as z.ZodRawShape;
|
|
251
|
-
}
|
|
252
|
-
return args || {};
|
|
253
|
-
};
|
|
254
250
|
|
|
255
251
|
const toJSONSchemaRoute = (route: RouteInfo) => {
|
|
256
252
|
const pickValues = pick(route, pickValue as any);
|
|
257
253
|
if (pickValues?.metadata?.args) {
|
|
258
|
-
|
|
259
|
-
// 如果 args 本身是一个 zod object schema,先提取 shape
|
|
260
|
-
args = extractArgs(args);
|
|
261
|
-
|
|
262
|
-
const keys = Object.keys(args);
|
|
263
|
-
const newArgs: { [key: string]: any } = {};
|
|
264
|
-
for (let key of keys) {
|
|
265
|
-
const item = args[key] as z.ZodAny;
|
|
266
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
267
|
-
newArgs[key] = item.toJSONSchema();
|
|
268
|
-
} else {
|
|
269
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
pickValues.metadata.args = newArgs;
|
|
254
|
+
pickValues.metadata.args = toJSONSchema(pickValues?.metadata?.args, { mergeObject: false });
|
|
273
255
|
}
|
|
274
256
|
return pickValues;
|
|
275
257
|
}
|
|
276
|
-
const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => {
|
|
277
|
-
const args = route?.metadata?.args;
|
|
278
|
-
if (!args) return route;
|
|
279
|
-
const newArgs = fromJSONSchema(args);
|
|
280
|
-
route.metadata.args = newArgs;
|
|
281
|
-
return route;
|
|
282
|
-
}
|
|
283
258
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
* @param args
|
|
287
|
-
* @returns
|
|
288
|
-
*/
|
|
289
|
-
export const toJSONSchema = (args: any): { [key: string]: any } => {
|
|
290
|
-
// 如果 args 本身是一个 zod object schema,先提取 shape
|
|
291
|
-
args = extractArgs(args);
|
|
292
|
-
const keys = Object.keys(args);
|
|
293
|
-
const newArgs: { [key: string]: any } = {};
|
|
294
|
-
for (let key of keys) {
|
|
295
|
-
const item = args[key] as z.ZodAny;
|
|
296
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
297
|
-
newArgs[key] = item.toJSONSchema();
|
|
298
|
-
} else {
|
|
299
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
return newArgs;
|
|
303
|
-
}
|
|
304
|
-
export const fromJSONSchema = <Merge extends boolean = true>(args: any = {}, opts?: { mergeObject?: boolean }) => {
|
|
305
|
-
let resultArgs: any = null;
|
|
306
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
307
|
-
if (args["$schema"] || (args.type === 'object' && args.properties && typeof args.properties === 'object')) {
|
|
308
|
-
// 可能是整个schema
|
|
309
|
-
const objectSchema = z.fromJSONSchema(args);
|
|
310
|
-
const extract = extractArgs(objectSchema);
|
|
311
|
-
const keys = Object.keys(extract);
|
|
312
|
-
const newArgs: { [key: string]: any } = {};
|
|
313
|
-
for (let key of keys) {
|
|
314
|
-
newArgs[key] = extract[key];
|
|
315
|
-
}
|
|
316
|
-
resultArgs = newArgs;
|
|
317
|
-
}
|
|
318
|
-
if (!resultArgs) {
|
|
319
|
-
const keys = Object.keys(args);
|
|
320
|
-
const newArgs: { [key: string]: any } = {};
|
|
321
|
-
for (let key of keys) {
|
|
322
|
-
const item = args[key];
|
|
323
|
-
// fromJSONSchema 可能会失败,所以先 optional,等使用的时候再验证
|
|
324
|
-
newArgs[key] = z.fromJSONSchema(item).optional();
|
|
325
|
-
}
|
|
326
|
-
resultArgs = newArgs;
|
|
327
|
-
}
|
|
328
|
-
if (mergeObject) {
|
|
329
|
-
resultArgs = z.object(resultArgs);
|
|
330
|
-
}
|
|
331
|
-
type ResultArgs = Merge extends true ? z.ZodObject<{ [key: string]: any }> : { [key: string]: z.ZodTypeAny };
|
|
332
|
-
return resultArgs as unknown as ResultArgs;
|
|
333
|
-
}
|
|
259
|
+
export const toJSONSchema = schema.toJSONSchema;
|
|
260
|
+
export const fromJSONSchema = schema.fromJSONSchema;
|
|
334
261
|
|
|
335
262
|
/**
|
|
336
263
|
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
@@ -648,21 +575,6 @@ export class QueryRouter {
|
|
|
648
575
|
getList(filter?: (route: Route) => boolean): RouteInfo[] {
|
|
649
576
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
650
577
|
const pickValues = pick(r, pickValue as any);
|
|
651
|
-
if (pickValues?.metadata?.args) {
|
|
652
|
-
// const demoArgs = { k: tool.schema.string().describe('示例参数') };
|
|
653
|
-
const args = pickValues.metadata.args;
|
|
654
|
-
const keys = Object.keys(args);
|
|
655
|
-
const newArgs: { [key: string]: any } = {};
|
|
656
|
-
for (let key of keys) {
|
|
657
|
-
const item = args[key] as z.ZodAny;
|
|
658
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
659
|
-
newArgs[key] = item.toJSONSchema();
|
|
660
|
-
} else {
|
|
661
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
pickValues.metadata.args = newArgs;
|
|
665
|
-
}
|
|
666
578
|
return pickValues;
|
|
667
579
|
});
|
|
668
580
|
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const extractArgs = (args: any) => {
|
|
3
|
+
if (args && typeof args === 'object' && typeof args.shape === 'object') {
|
|
4
|
+
return args.shape as z.ZodRawShape;
|
|
5
|
+
}
|
|
6
|
+
return args || {};
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type ZodOverride = (opts: { jsonSchema: any; path: string[]; zodSchema: z.ZodTypeAny }) => void;
|
|
10
|
+
/**
|
|
11
|
+
* 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
|
|
12
|
+
* @param args
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export const toJSONSchema = (args: any, opts?: { mergeObject?: boolean, override?: ZodOverride }): { [key: string]: any } => {
|
|
16
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
17
|
+
if (!args) return {};
|
|
18
|
+
const _override = ({ jsonSchema, path, zodSchema }) => {
|
|
19
|
+
if (Array.isArray(path) && path.length > 0) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
const isOptional = (zodSchema as any).isOptional?.();
|
|
23
|
+
if (isOptional) {
|
|
24
|
+
// 添加自定义属性
|
|
25
|
+
jsonSchema.optional = true;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const isError = (keys: string[]) => {
|
|
29
|
+
const errorKeys: string[] = ["toJSONSchema", "def", "type", "parse"]
|
|
30
|
+
const hasErrorKeys = errorKeys.every(key => keys.includes(key));
|
|
31
|
+
return hasErrorKeys;
|
|
32
|
+
}
|
|
33
|
+
const override: any = opts?.override || _override;
|
|
34
|
+
if (mergeObject) {
|
|
35
|
+
if (typeof args === 'object' && typeof args.toJSONSchema === 'function') {
|
|
36
|
+
return args.toJSONSchema();
|
|
37
|
+
}
|
|
38
|
+
if (isError(Object.keys(args))) {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
// 如果 mergeObject 为 true,直接将整个对象转换为 JSON Schema
|
|
42
|
+
// 先检测是否是一个错误的 schema
|
|
43
|
+
const schema = z.object(args);
|
|
44
|
+
return schema.toJSONSchema();
|
|
45
|
+
}
|
|
46
|
+
// 如果 args 本身是一个 zod object schema,先提取 shape
|
|
47
|
+
args = extractArgs(args);
|
|
48
|
+
let keys = Object.keys(args);
|
|
49
|
+
if (isError(keys)) {
|
|
50
|
+
console.error(`[toJSONSchema error]: 解析到的 schema 可能不正确,包含了zod默认的value的schema. 请检查输入的 schema 是否正确。`);
|
|
51
|
+
args = {};
|
|
52
|
+
keys = [];
|
|
53
|
+
}
|
|
54
|
+
if (mergeObject) {
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
let newArgs: { [key: string]: any } = {};
|
|
58
|
+
for (let key of keys) {
|
|
59
|
+
const item = args[key] as z.ZodAny;
|
|
60
|
+
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
61
|
+
newArgs[key] = item.toJSONSchema({ override });
|
|
62
|
+
} else {
|
|
63
|
+
newArgs[key] = args[key]; // 可能不是schema
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return newArgs;
|
|
67
|
+
}
|
|
68
|
+
export const fromJSONSchema = <Merge extends boolean = false>(args: any = {}, opts?: { mergeObject?: boolean }) => {
|
|
69
|
+
let resultArgs: any = null;
|
|
70
|
+
const mergeObject = opts?.mergeObject ?? false;
|
|
71
|
+
if (args["$schema"] || (args.type === 'object' && args.properties && typeof args.properties === 'object')) {
|
|
72
|
+
// 可能是整个schema
|
|
73
|
+
const objectSchema = z.fromJSONSchema(args);
|
|
74
|
+
const extract = extractArgs(objectSchema);
|
|
75
|
+
const keys = Object.keys(extract);
|
|
76
|
+
const newArgs: { [key: string]: any } = {};
|
|
77
|
+
for (let key of keys) {
|
|
78
|
+
newArgs[key] = extract[key];
|
|
79
|
+
}
|
|
80
|
+
resultArgs = newArgs;
|
|
81
|
+
}
|
|
82
|
+
if (!resultArgs) {
|
|
83
|
+
const keys = Object.keys(args);
|
|
84
|
+
const newArgs: { [key: string]: any } = {};
|
|
85
|
+
for (let key of keys) {
|
|
86
|
+
const item = args[key];
|
|
87
|
+
// fromJSONSchema 可能会失败,所以先 optional,等使用的时候再验证
|
|
88
|
+
newArgs[key] = z.fromJSONSchema(item)
|
|
89
|
+
if (item.optional) {
|
|
90
|
+
newArgs[key] = newArgs[key].optional();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
resultArgs = newArgs;
|
|
94
|
+
}
|
|
95
|
+
if (mergeObject) {
|
|
96
|
+
resultArgs = z.object(resultArgs);
|
|
97
|
+
}
|
|
98
|
+
type ResultArgs = Merge extends true ? z.ZodObject<{ [key: string]: any }> : { [key: string]: z.ZodTypeAny };
|
|
99
|
+
return resultArgs as unknown as ResultArgs;
|
|
100
|
+
}
|