@kevisual/router 0.0.79 → 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 -87
- package/dist/opencode.d.ts +6 -1
- package/dist/router-browser.d.ts +12 -7
- package/dist/router-browser.js +93 -88
- package/dist/router-define.d.ts +6 -1
- package/dist/router.d.ts +12 -7
- package/dist/router.js +93 -88
- package/dist/ws.d.ts +6 -1
- package/package.json +1 -1
- package/src/route.ts +5 -103
- 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,82 +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
|
-
if (!args)
|
|
16950
|
-
return {};
|
|
16951
|
-
if (mergeObject) {
|
|
16952
|
-
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
16953
|
-
return args.toJSONSchema();
|
|
16954
|
-
}
|
|
16955
|
-
const schema = exports_external.object(args);
|
|
16956
|
-
return schema.toJSONSchema();
|
|
16957
|
-
}
|
|
16958
|
-
args = extractArgs(args);
|
|
16959
|
-
const keys = Object.keys(args);
|
|
16960
|
-
let newArgs = {};
|
|
16961
|
-
for (let key of keys) {
|
|
16962
|
-
const item = args[key];
|
|
16963
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16964
|
-
newArgs[key] = item.toJSONSchema();
|
|
16965
|
-
} else {
|
|
16966
|
-
newArgs[key] = args[key];
|
|
16967
|
-
}
|
|
16968
|
-
}
|
|
16969
|
-
return newArgs;
|
|
16970
|
-
};
|
|
16971
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16972
|
-
let resultArgs = null;
|
|
16973
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
16974
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16975
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16976
|
-
const extract = extractArgs(objectSchema);
|
|
16977
|
-
const keys = Object.keys(extract);
|
|
16978
|
-
const newArgs = {};
|
|
16979
|
-
for (let key of keys) {
|
|
16980
|
-
newArgs[key] = extract[key];
|
|
16981
|
-
}
|
|
16982
|
-
resultArgs = newArgs;
|
|
16983
|
-
}
|
|
16984
|
-
if (!resultArgs) {
|
|
16985
|
-
const keys = Object.keys(args);
|
|
16986
|
-
const newArgs = {};
|
|
16987
|
-
for (let key of keys) {
|
|
16988
|
-
const item = args[key];
|
|
16989
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
16990
|
-
}
|
|
16991
|
-
resultArgs = newArgs;
|
|
16992
|
-
}
|
|
16993
|
-
if (mergeObject) {
|
|
16994
|
-
resultArgs = exports_external.object(resultArgs);
|
|
16995
|
-
}
|
|
16996
|
-
return resultArgs;
|
|
16997
|
-
};
|
|
17015
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
17016
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
16998
17017
|
|
|
16999
17018
|
class QueryRouter {
|
|
17000
17019
|
appId = "";
|
|
@@ -17248,20 +17267,6 @@ class QueryRouter {
|
|
|
17248
17267
|
getList(filter) {
|
|
17249
17268
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
17250
17269
|
const pickValues = pick(r, pickValue);
|
|
17251
|
-
if (pickValues?.metadata?.args) {
|
|
17252
|
-
const args = pickValues.metadata.args;
|
|
17253
|
-
const keys = Object.keys(args);
|
|
17254
|
-
const newArgs = {};
|
|
17255
|
-
for (let key of keys) {
|
|
17256
|
-
const item = args[key];
|
|
17257
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
17258
|
-
newArgs[key] = item.toJSONSchema();
|
|
17259
|
-
} else {
|
|
17260
|
-
newArgs[key] = args[key];
|
|
17261
|
-
}
|
|
17262
|
-
}
|
|
17263
|
-
pickValues.metadata.args = newArgs;
|
|
17264
|
-
}
|
|
17265
17270
|
return pickValues;
|
|
17266
17271
|
});
|
|
17267
17272
|
}
|
|
@@ -17349,8 +17354,8 @@ class QueryRouter {
|
|
|
17349
17354
|
}
|
|
17350
17355
|
return listenProcess({ app: this, params, ...opts });
|
|
17351
17356
|
}
|
|
17352
|
-
toJSONSchema =
|
|
17353
|
-
fromJSONSchema =
|
|
17357
|
+
toJSONSchema = toJSONSchema3;
|
|
17358
|
+
fromJSONSchema = fromJSONSchema3;
|
|
17354
17359
|
}
|
|
17355
17360
|
|
|
17356
17361
|
// src/server/server.ts
|
|
@@ -19517,7 +19522,7 @@ app
|
|
|
19517
19522
|
10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
|
|
19518
19523
|
`;
|
|
19519
19524
|
// package.json
|
|
19520
|
-
var version2 = "0.0.
|
|
19525
|
+
var version2 = "0.0.80";
|
|
19521
19526
|
|
|
19522
19527
|
// agent/routes/route-create.ts
|
|
19523
19528
|
app.route({
|
package/dist/opencode.d.ts
CHANGED
|
@@ -350,10 +350,15 @@ declare class QueryRouter {
|
|
|
350
350
|
}): Promise<void>;
|
|
351
351
|
toJSONSchema: (args: any, opts?: {
|
|
352
352
|
mergeObject?: boolean;
|
|
353
|
+
override?: (opts: {
|
|
354
|
+
jsonSchema: any;
|
|
355
|
+
path: string[];
|
|
356
|
+
zodSchema: z.ZodTypeAny;
|
|
357
|
+
}) => void;
|
|
353
358
|
}) => {
|
|
354
359
|
[key: string]: any;
|
|
355
360
|
};
|
|
356
|
-
fromJSONSchema: <Merge extends boolean =
|
|
361
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
357
362
|
mergeObject?: boolean;
|
|
358
363
|
}) => Merge extends true ? z.ZodObject<{
|
|
359
364
|
[key: string]: any;
|
package/dist/router-browser.d.ts
CHANGED
|
@@ -204,17 +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
|
-
* 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
|
|
209
|
-
* @param args
|
|
210
|
-
* @returns
|
|
211
|
-
*/
|
|
212
207
|
declare const toJSONSchema: (args: any, opts?: {
|
|
213
208
|
mergeObject?: boolean;
|
|
209
|
+
override?: (opts: {
|
|
210
|
+
jsonSchema: any;
|
|
211
|
+
path: string[];
|
|
212
|
+
zodSchema: z.ZodTypeAny;
|
|
213
|
+
}) => void;
|
|
214
214
|
}) => {
|
|
215
215
|
[key: string]: any;
|
|
216
216
|
};
|
|
217
|
-
declare const fromJSONSchema: <Merge extends boolean =
|
|
217
|
+
declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
218
218
|
mergeObject?: boolean;
|
|
219
219
|
}) => Merge extends true ? z.ZodObject<{
|
|
220
220
|
[key: string]: any;
|
|
@@ -394,10 +394,15 @@ declare class QueryRouter {
|
|
|
394
394
|
}): Promise<void>;
|
|
395
395
|
toJSONSchema: (args: any, opts?: {
|
|
396
396
|
mergeObject?: boolean;
|
|
397
|
+
override?: (opts: {
|
|
398
|
+
jsonSchema: any;
|
|
399
|
+
path: string[];
|
|
400
|
+
zodSchema: z.ZodTypeAny;
|
|
401
|
+
}) => void;
|
|
397
402
|
}) => {
|
|
398
403
|
[key: string]: any;
|
|
399
404
|
};
|
|
400
|
-
fromJSONSchema: <Merge extends boolean =
|
|
405
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
401
406
|
mergeObject?: boolean;
|
|
402
407
|
}) => Merge extends true ? z.ZodObject<{
|
|
403
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,82 +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
|
-
if (!args)
|
|
14114
|
-
return {};
|
|
14115
|
-
if (mergeObject) {
|
|
14116
|
-
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
14117
|
-
return args.toJSONSchema();
|
|
14118
|
-
}
|
|
14119
|
-
const schema = exports_external.object(args);
|
|
14120
|
-
return schema.toJSONSchema();
|
|
14121
|
-
}
|
|
14122
|
-
args = extractArgs(args);
|
|
14123
|
-
const keys = Object.keys(args);
|
|
14124
|
-
let newArgs = {};
|
|
14125
|
-
for (let key of keys) {
|
|
14126
|
-
const item = args[key];
|
|
14127
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14128
|
-
newArgs[key] = item.toJSONSchema();
|
|
14129
|
-
} else {
|
|
14130
|
-
newArgs[key] = args[key];
|
|
14131
|
-
}
|
|
14132
|
-
}
|
|
14133
|
-
return newArgs;
|
|
14134
|
-
};
|
|
14135
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
14136
|
-
let resultArgs = null;
|
|
14137
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
14138
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
14139
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
14140
|
-
const extract = extractArgs(objectSchema);
|
|
14141
|
-
const keys = Object.keys(extract);
|
|
14142
|
-
const newArgs = {};
|
|
14143
|
-
for (let key of keys) {
|
|
14144
|
-
newArgs[key] = extract[key];
|
|
14145
|
-
}
|
|
14146
|
-
resultArgs = newArgs;
|
|
14147
|
-
}
|
|
14148
|
-
if (!resultArgs) {
|
|
14149
|
-
const keys = Object.keys(args);
|
|
14150
|
-
const newArgs = {};
|
|
14151
|
-
for (let key of keys) {
|
|
14152
|
-
const item = args[key];
|
|
14153
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
14154
|
-
}
|
|
14155
|
-
resultArgs = newArgs;
|
|
14156
|
-
}
|
|
14157
|
-
if (mergeObject) {
|
|
14158
|
-
resultArgs = exports_external.object(resultArgs);
|
|
14159
|
-
}
|
|
14160
|
-
return resultArgs;
|
|
14161
|
-
};
|
|
14179
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
14180
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
14162
14181
|
|
|
14163
14182
|
class QueryRouter {
|
|
14164
14183
|
appId = "";
|
|
@@ -14412,20 +14431,6 @@ class QueryRouter {
|
|
|
14412
14431
|
getList(filter) {
|
|
14413
14432
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
14414
14433
|
const pickValues = pick(r, pickValue);
|
|
14415
|
-
if (pickValues?.metadata?.args) {
|
|
14416
|
-
const args = pickValues.metadata.args;
|
|
14417
|
-
const keys = Object.keys(args);
|
|
14418
|
-
const newArgs = {};
|
|
14419
|
-
for (let key of keys) {
|
|
14420
|
-
const item = args[key];
|
|
14421
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
14422
|
-
newArgs[key] = item.toJSONSchema();
|
|
14423
|
-
} else {
|
|
14424
|
-
newArgs[key] = args[key];
|
|
14425
|
-
}
|
|
14426
|
-
}
|
|
14427
|
-
pickValues.metadata.args = newArgs;
|
|
14428
|
-
}
|
|
14429
14434
|
return pickValues;
|
|
14430
14435
|
});
|
|
14431
14436
|
}
|
|
@@ -14513,8 +14518,8 @@ class QueryRouter {
|
|
|
14513
14518
|
}
|
|
14514
14519
|
return listenProcess({ app: this, params, ...opts });
|
|
14515
14520
|
}
|
|
14516
|
-
toJSONSchema =
|
|
14517
|
-
fromJSONSchema =
|
|
14521
|
+
toJSONSchema = toJSONSchema3;
|
|
14522
|
+
fromJSONSchema = fromJSONSchema3;
|
|
14518
14523
|
}
|
|
14519
14524
|
|
|
14520
14525
|
class QueryRouterServer extends QueryRouter {
|
|
@@ -14753,8 +14758,8 @@ var App = QueryRouterServer;
|
|
|
14753
14758
|
export {
|
|
14754
14759
|
util,
|
|
14755
14760
|
tool,
|
|
14756
|
-
|
|
14757
|
-
|
|
14761
|
+
toJSONSchema3 as toJSONSchema,
|
|
14762
|
+
fromJSONSchema3 as fromJSONSchema,
|
|
14758
14763
|
define,
|
|
14759
14764
|
createSkill,
|
|
14760
14765
|
createSchema,
|
package/dist/router-define.d.ts
CHANGED
|
@@ -347,10 +347,15 @@ declare class QueryRouter {
|
|
|
347
347
|
}): Promise<void>;
|
|
348
348
|
toJSONSchema: (args: any, opts?: {
|
|
349
349
|
mergeObject?: boolean;
|
|
350
|
+
override?: (opts: {
|
|
351
|
+
jsonSchema: any;
|
|
352
|
+
path: string[];
|
|
353
|
+
zodSchema: z.ZodTypeAny;
|
|
354
|
+
}) => void;
|
|
350
355
|
}) => {
|
|
351
356
|
[key: string]: any;
|
|
352
357
|
};
|
|
353
|
-
fromJSONSchema: <Merge extends boolean =
|
|
358
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
354
359
|
mergeObject?: boolean;
|
|
355
360
|
}) => Merge extends true ? z.ZodObject<{
|
|
356
361
|
[key: string]: any;
|
package/dist/router.d.ts
CHANGED
|
@@ -210,17 +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
|
-
* 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
|
|
215
|
-
* @param args
|
|
216
|
-
* @returns
|
|
217
|
-
*/
|
|
218
213
|
declare const toJSONSchema: (args: any, opts?: {
|
|
219
214
|
mergeObject?: boolean;
|
|
215
|
+
override?: (opts: {
|
|
216
|
+
jsonSchema: any;
|
|
217
|
+
path: string[];
|
|
218
|
+
zodSchema: z.ZodTypeAny;
|
|
219
|
+
}) => void;
|
|
220
220
|
}) => {
|
|
221
221
|
[key: string]: any;
|
|
222
222
|
};
|
|
223
|
-
declare const fromJSONSchema: <Merge extends boolean =
|
|
223
|
+
declare const fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
224
224
|
mergeObject?: boolean;
|
|
225
225
|
}) => Merge extends true ? z.ZodObject<{
|
|
226
226
|
[key: string]: any;
|
|
@@ -400,10 +400,15 @@ declare class QueryRouter {
|
|
|
400
400
|
}): Promise<void>;
|
|
401
401
|
toJSONSchema: (args: any, opts?: {
|
|
402
402
|
mergeObject?: boolean;
|
|
403
|
+
override?: (opts: {
|
|
404
|
+
jsonSchema: any;
|
|
405
|
+
path: string[];
|
|
406
|
+
zodSchema: z.ZodTypeAny;
|
|
407
|
+
}) => void;
|
|
403
408
|
}) => {
|
|
404
409
|
[key: string]: any;
|
|
405
410
|
};
|
|
406
|
-
fromJSONSchema: <Merge extends boolean =
|
|
411
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
407
412
|
mergeObject?: boolean;
|
|
408
413
|
}) => Merge extends true ? z.ZodObject<{
|
|
409
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,82 +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
|
-
if (!args)
|
|
16947
|
-
return {};
|
|
16948
|
-
if (mergeObject) {
|
|
16949
|
-
if (typeof args === "object" && typeof args.toJSONSchema === "function") {
|
|
16950
|
-
return args.toJSONSchema();
|
|
16951
|
-
}
|
|
16952
|
-
const schema = exports_external.object(args);
|
|
16953
|
-
return schema.toJSONSchema();
|
|
16954
|
-
}
|
|
16955
|
-
args = extractArgs(args);
|
|
16956
|
-
const keys = Object.keys(args);
|
|
16957
|
-
let newArgs = {};
|
|
16958
|
-
for (let key of keys) {
|
|
16959
|
-
const item = args[key];
|
|
16960
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16961
|
-
newArgs[key] = item.toJSONSchema();
|
|
16962
|
-
} else {
|
|
16963
|
-
newArgs[key] = args[key];
|
|
16964
|
-
}
|
|
16965
|
-
}
|
|
16966
|
-
return newArgs;
|
|
16967
|
-
};
|
|
16968
|
-
var fromJSONSchema2 = (args = {}, opts) => {
|
|
16969
|
-
let resultArgs = null;
|
|
16970
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
16971
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16972
|
-
const objectSchema = exports_external.fromJSONSchema(args);
|
|
16973
|
-
const extract = extractArgs(objectSchema);
|
|
16974
|
-
const keys = Object.keys(extract);
|
|
16975
|
-
const newArgs = {};
|
|
16976
|
-
for (let key of keys) {
|
|
16977
|
-
newArgs[key] = extract[key];
|
|
16978
|
-
}
|
|
16979
|
-
resultArgs = newArgs;
|
|
16980
|
-
}
|
|
16981
|
-
if (!resultArgs) {
|
|
16982
|
-
const keys = Object.keys(args);
|
|
16983
|
-
const newArgs = {};
|
|
16984
|
-
for (let key of keys) {
|
|
16985
|
-
const item = args[key];
|
|
16986
|
-
newArgs[key] = exports_external.fromJSONSchema(item).optional();
|
|
16987
|
-
}
|
|
16988
|
-
resultArgs = newArgs;
|
|
16989
|
-
}
|
|
16990
|
-
if (mergeObject) {
|
|
16991
|
-
resultArgs = exports_external.object(resultArgs);
|
|
16992
|
-
}
|
|
16993
|
-
return resultArgs;
|
|
16994
|
-
};
|
|
17012
|
+
var toJSONSchema3 = toJSONSchema2;
|
|
17013
|
+
var fromJSONSchema3 = fromJSONSchema2;
|
|
16995
17014
|
|
|
16996
17015
|
class QueryRouter {
|
|
16997
17016
|
appId = "";
|
|
@@ -17245,20 +17264,6 @@ class QueryRouter {
|
|
|
17245
17264
|
getList(filter) {
|
|
17246
17265
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
17247
17266
|
const pickValues = pick(r, pickValue);
|
|
17248
|
-
if (pickValues?.metadata?.args) {
|
|
17249
|
-
const args = pickValues.metadata.args;
|
|
17250
|
-
const keys = Object.keys(args);
|
|
17251
|
-
const newArgs = {};
|
|
17252
|
-
for (let key of keys) {
|
|
17253
|
-
const item = args[key];
|
|
17254
|
-
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
17255
|
-
newArgs[key] = item.toJSONSchema();
|
|
17256
|
-
} else {
|
|
17257
|
-
newArgs[key] = args[key];
|
|
17258
|
-
}
|
|
17259
|
-
}
|
|
17260
|
-
pickValues.metadata.args = newArgs;
|
|
17261
|
-
}
|
|
17262
17267
|
return pickValues;
|
|
17263
17268
|
});
|
|
17264
17269
|
}
|
|
@@ -17346,8 +17351,8 @@ class QueryRouter {
|
|
|
17346
17351
|
}
|
|
17347
17352
|
return listenProcess({ app: this, params, ...opts });
|
|
17348
17353
|
}
|
|
17349
|
-
toJSONSchema =
|
|
17350
|
-
fromJSONSchema =
|
|
17354
|
+
toJSONSchema = toJSONSchema3;
|
|
17355
|
+
fromJSONSchema = fromJSONSchema3;
|
|
17351
17356
|
}
|
|
17352
17357
|
|
|
17353
17358
|
class QueryRouterServer extends QueryRouter {
|
|
@@ -18545,9 +18550,9 @@ class App extends QueryRouter {
|
|
|
18545
18550
|
export {
|
|
18546
18551
|
util,
|
|
18547
18552
|
tool,
|
|
18548
|
-
|
|
18553
|
+
toJSONSchema3 as toJSONSchema,
|
|
18549
18554
|
handleServer,
|
|
18550
|
-
|
|
18555
|
+
fromJSONSchema3 as fromJSONSchema,
|
|
18551
18556
|
define,
|
|
18552
18557
|
createSkill,
|
|
18553
18558
|
createSchema,
|
package/dist/ws.d.ts
CHANGED
|
@@ -397,10 +397,15 @@ declare class QueryRouter {
|
|
|
397
397
|
}): Promise<void>;
|
|
398
398
|
toJSONSchema: (args: any, opts?: {
|
|
399
399
|
mergeObject?: boolean;
|
|
400
|
+
override?: (opts: {
|
|
401
|
+
jsonSchema: any;
|
|
402
|
+
path: string[];
|
|
403
|
+
zodSchema: z.ZodTypeAny;
|
|
404
|
+
}) => void;
|
|
400
405
|
}) => {
|
|
401
406
|
[key: string]: any;
|
|
402
407
|
};
|
|
403
|
-
fromJSONSchema: <Merge extends boolean =
|
|
408
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
404
409
|
mergeObject?: boolean;
|
|
405
410
|
}) => Merge extends true ? z.ZodObject<{
|
|
406
411
|
[key: string]: any;
|
package/package.json
CHANGED
package/src/route.ts
CHANGED
|
@@ -3,7 +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
|
|
6
|
+
import * as schema from './validator/schema.ts';
|
|
7
|
+
|
|
7
8
|
export type RouterContextT = { code?: number;[key: string]: any };
|
|
8
9
|
export type RouteContext<T = { code?: number }, S = any> = {
|
|
9
10
|
/**
|
|
@@ -246,101 +247,17 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
|
|
246
247
|
throw new CustomError(...args);
|
|
247
248
|
}
|
|
248
249
|
}
|
|
249
|
-
const extractArgs = (args: any) => {
|
|
250
|
-
if (args && typeof args === 'object' && typeof args.shape === 'object') {
|
|
251
|
-
return args.shape as z.ZodRawShape;
|
|
252
|
-
}
|
|
253
|
-
return args || {};
|
|
254
|
-
};
|
|
255
250
|
|
|
256
251
|
const toJSONSchemaRoute = (route: RouteInfo) => {
|
|
257
252
|
const pickValues = pick(route, pickValue as any);
|
|
258
253
|
if (pickValues?.metadata?.args) {
|
|
259
|
-
|
|
260
|
-
// 如果 args 本身是一个 zod object schema,先提取 shape
|
|
261
|
-
args = extractArgs(args);
|
|
262
|
-
|
|
263
|
-
const keys = Object.keys(args);
|
|
264
|
-
const newArgs: { [key: string]: any } = {};
|
|
265
|
-
for (let key of keys) {
|
|
266
|
-
const item = args[key] as z.ZodAny;
|
|
267
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
268
|
-
newArgs[key] = item.toJSONSchema();
|
|
269
|
-
} else {
|
|
270
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
pickValues.metadata.args = newArgs;
|
|
254
|
+
pickValues.metadata.args = toJSONSchema(pickValues?.metadata?.args, { mergeObject: false });
|
|
274
255
|
}
|
|
275
256
|
return pickValues;
|
|
276
257
|
}
|
|
277
|
-
const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => {
|
|
278
|
-
const args = route?.metadata?.args;
|
|
279
|
-
if (!args) return route;
|
|
280
|
-
const newArgs = fromJSONSchema(args);
|
|
281
|
-
route.metadata.args = newArgs;
|
|
282
|
-
return route;
|
|
283
|
-
}
|
|
284
258
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
* @param args
|
|
288
|
-
* @returns
|
|
289
|
-
*/
|
|
290
|
-
export const toJSONSchema = (args: any, opts?: { mergeObject?: boolean }): { [key: string]: any } => {
|
|
291
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
292
|
-
if (!args) return {};
|
|
293
|
-
if (mergeObject) {
|
|
294
|
-
if (typeof args === 'object' && typeof args.toJSONSchema === 'function') {
|
|
295
|
-
return args.toJSONSchema();
|
|
296
|
-
}
|
|
297
|
-
const schema = z.object(args);
|
|
298
|
-
return schema.toJSONSchema();
|
|
299
|
-
}
|
|
300
|
-
// 如果 args 本身是一个 zod object schema,先提取 shape
|
|
301
|
-
args = extractArgs(args);
|
|
302
|
-
const keys = Object.keys(args);
|
|
303
|
-
let newArgs: { [key: string]: any } = {};
|
|
304
|
-
for (let key of keys) {
|
|
305
|
-
const item = args[key] as z.ZodAny;
|
|
306
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
307
|
-
newArgs[key] = item.toJSONSchema();
|
|
308
|
-
} else {
|
|
309
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
return newArgs;
|
|
313
|
-
}
|
|
314
|
-
export const fromJSONSchema = <Merge extends boolean = true>(args: any = {}, opts?: { mergeObject?: boolean }) => {
|
|
315
|
-
let resultArgs: any = null;
|
|
316
|
-
const mergeObject = opts?.mergeObject ?? true;
|
|
317
|
-
if (args["$schema"] || (args.type === 'object' && args.properties && typeof args.properties === 'object')) {
|
|
318
|
-
// 可能是整个schema
|
|
319
|
-
const objectSchema = z.fromJSONSchema(args);
|
|
320
|
-
const extract = extractArgs(objectSchema);
|
|
321
|
-
const keys = Object.keys(extract);
|
|
322
|
-
const newArgs: { [key: string]: any } = {};
|
|
323
|
-
for (let key of keys) {
|
|
324
|
-
newArgs[key] = extract[key];
|
|
325
|
-
}
|
|
326
|
-
resultArgs = newArgs;
|
|
327
|
-
}
|
|
328
|
-
if (!resultArgs) {
|
|
329
|
-
const keys = Object.keys(args);
|
|
330
|
-
const newArgs: { [key: string]: any } = {};
|
|
331
|
-
for (let key of keys) {
|
|
332
|
-
const item = args[key];
|
|
333
|
-
// fromJSONSchema 可能会失败,所以先 optional,等使用的时候再验证
|
|
334
|
-
newArgs[key] = z.fromJSONSchema(item).optional();
|
|
335
|
-
}
|
|
336
|
-
resultArgs = newArgs;
|
|
337
|
-
}
|
|
338
|
-
if (mergeObject) {
|
|
339
|
-
resultArgs = z.object(resultArgs);
|
|
340
|
-
}
|
|
341
|
-
type ResultArgs = Merge extends true ? z.ZodObject<{ [key: string]: any }> : { [key: string]: z.ZodTypeAny };
|
|
342
|
-
return resultArgs as unknown as ResultArgs;
|
|
343
|
-
}
|
|
259
|
+
export const toJSONSchema = schema.toJSONSchema;
|
|
260
|
+
export const fromJSONSchema = schema.fromJSONSchema;
|
|
344
261
|
|
|
345
262
|
/**
|
|
346
263
|
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
@@ -658,21 +575,6 @@ export class QueryRouter {
|
|
|
658
575
|
getList(filter?: (route: Route) => boolean): RouteInfo[] {
|
|
659
576
|
return this.routes.filter(filter || (() => true)).map((r) => {
|
|
660
577
|
const pickValues = pick(r, pickValue as any);
|
|
661
|
-
if (pickValues?.metadata?.args) {
|
|
662
|
-
// const demoArgs = { k: tool.schema.string().describe('示例参数') };
|
|
663
|
-
const args = pickValues.metadata.args;
|
|
664
|
-
const keys = Object.keys(args);
|
|
665
|
-
const newArgs: { [key: string]: any } = {};
|
|
666
|
-
for (let key of keys) {
|
|
667
|
-
const item = args[key] as z.ZodAny;
|
|
668
|
-
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
|
669
|
-
newArgs[key] = item.toJSONSchema();
|
|
670
|
-
} else {
|
|
671
|
-
newArgs[key] = args[key]; // 可能不是schema
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
pickValues.metadata.args = newArgs;
|
|
675
|
-
}
|
|
676
578
|
return pickValues;
|
|
677
579
|
});
|
|
678
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
|
+
}
|