@midscene/shared 1.12.7-beta-20260914023554.0 → 1.12.7
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/es/cli/cli-args.mjs +24 -50
- package/dist/es/cli/cli-value.mjs +87 -0
- package/dist/es/env/parse-model-config.mjs +1 -1
- package/dist/es/zod-schema-utils.mjs +1 -52
- package/dist/lib/cli/cli-args.js +25 -51
- package/dist/lib/cli/cli-value.js +124 -0
- package/dist/lib/env/parse-model-config.js +1 -1
- package/dist/lib/zod-schema-utils.js +1 -55
- package/dist/types/cli/cli-args.d.ts +1 -1
- package/dist/types/cli/cli-value.d.ts +4 -0
- package/dist/types/zod-schema-utils.d.ts +1 -9
- package/package.json +1 -1
- package/src/cli/cli-args.ts +29 -72
- package/src/cli/cli-value.ts +137 -0
- package/src/zod-schema-utils.ts +1 -67
package/dist/es/cli/cli-args.mjs
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { getKeyAliases } from "../key-alias-utils.mjs";
|
|
3
|
-
import { getZodValueKinds } from "../zod-schema-utils.mjs";
|
|
4
3
|
import { CLIError } from "./cli-error.mjs";
|
|
5
|
-
|
|
6
|
-
function
|
|
7
|
-
const parsedJson = parseJsonValue(raw);
|
|
8
|
-
if (parsedJson !== raw) return parsedJson;
|
|
9
|
-
if (cliNumberPattern.test(raw)) return Number(raw);
|
|
10
|
-
return raw;
|
|
11
|
-
}
|
|
12
|
-
function walkCliArgs(args, setArgValue, fieldByCliName) {
|
|
4
|
+
import { parseCliValue, parseValue } from "./cli-value.mjs";
|
|
5
|
+
function walkCliArgs(args, setArgValue) {
|
|
13
6
|
for(let i = 0; i < args.length; i++){
|
|
14
7
|
const arg = args[i];
|
|
15
8
|
if (!arg.startsWith('--')) continue;
|
|
@@ -17,10 +10,10 @@ function walkCliArgs(args, setArgValue, fieldByCliName) {
|
|
|
17
10
|
const eqIdx = body.indexOf('=');
|
|
18
11
|
if (eqIdx >= 0) {
|
|
19
12
|
const key = body.slice(0, eqIdx);
|
|
20
|
-
setArgValue(key,
|
|
13
|
+
setArgValue(key, body.slice(eqIdx + 1));
|
|
21
14
|
} else if (args[i + 1] && !args[i + 1].startsWith('--')) {
|
|
22
15
|
i++;
|
|
23
|
-
setArgValue(body,
|
|
16
|
+
setArgValue(body, args[i]);
|
|
24
17
|
} else setArgValue(body, true);
|
|
25
18
|
}
|
|
26
19
|
}
|
|
@@ -29,48 +22,29 @@ function buildCliFieldIndex(def) {
|
|
|
29
22
|
for (const [schemaKey, field] of Object.entries(def.schema))for (const cliName of getAcceptedCliOptionNames(schemaKey, def.cli?.options?.[schemaKey]))fieldByCliName.set(cliName, field);
|
|
30
23
|
return fieldByCliName;
|
|
31
24
|
}
|
|
32
|
-
function parseJsonValue(raw) {
|
|
33
|
-
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
34
|
-
try {
|
|
35
|
-
return JSON.parse(raw);
|
|
36
|
-
} catch {
|
|
37
|
-
return raw;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
function parseCliValue(raw, field) {
|
|
41
|
-
if (!field) return parseValue(raw);
|
|
42
|
-
const kinds = getZodValueKinds(field);
|
|
43
|
-
if (kinds.has('object') || kinds.has('array')) {
|
|
44
|
-
const parsedJson = parseJsonValue(raw);
|
|
45
|
-
if (parsedJson !== raw) return parsedJson;
|
|
46
|
-
}
|
|
47
|
-
if (kinds.has('string')) return raw;
|
|
48
|
-
if (kinds.has('number') && cliNumberPattern.test(raw)) return Number(raw);
|
|
49
|
-
if (kinds.has('boolean')) {
|
|
50
|
-
if ('true' === raw) return true;
|
|
51
|
-
if ('false' === raw) return false;
|
|
52
|
-
}
|
|
53
|
-
return kinds.has('unknown') ? parseValue(raw) : raw;
|
|
54
|
-
}
|
|
55
25
|
function parseCliArgs(args, def) {
|
|
56
|
-
const result = {};
|
|
57
26
|
const fieldByCliName = def ? buildCliFieldIndex(def) : void 0;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
27
|
+
const tokensByName = new Map();
|
|
28
|
+
walkCliArgs(args, (key, raw)=>{
|
|
29
|
+
const tokens = tokensByName.get(key) ?? [];
|
|
30
|
+
tokens.push(raw);
|
|
31
|
+
tokensByName.set(key, tokens);
|
|
32
|
+
});
|
|
33
|
+
const result = {};
|
|
34
|
+
for (const [key, tokens] of tokensByName){
|
|
35
|
+
let elementIndex = 0;
|
|
36
|
+
for (const raw of tokens){
|
|
37
|
+
const existing = result[key];
|
|
38
|
+
const value = true === raw ? true : parseCliValue(raw, fieldByCliName?.get(key), tokens.length > 1 ? elementIndex : void 0);
|
|
39
|
+
if (void 0 === existing) result[key] = value;
|
|
40
|
+
else if (Array.isArray(existing)) existing.push(value);
|
|
41
|
+
else result[key] = [
|
|
42
|
+
existing,
|
|
43
|
+
value
|
|
44
|
+
];
|
|
45
|
+
elementIndex = Array.isArray(result[key]) ? result[key].length : 1;
|
|
68
46
|
}
|
|
69
|
-
|
|
70
|
-
existing,
|
|
71
|
-
value
|
|
72
|
-
];
|
|
73
|
-
}, fieldByCliName);
|
|
47
|
+
}
|
|
74
48
|
return result;
|
|
75
49
|
}
|
|
76
50
|
function formatCliOptionName(name) {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { unwrapZodField } from "../zod-schema-utils.mjs";
|
|
3
|
+
const cliNumberPattern = /^-?\d+(\.\d+)?$/;
|
|
4
|
+
function getFieldDef(field) {
|
|
5
|
+
return field._def;
|
|
6
|
+
}
|
|
7
|
+
function getFieldKind(field) {
|
|
8
|
+
return getFieldDef(field).typeName;
|
|
9
|
+
}
|
|
10
|
+
function parseJsonValue(raw) {
|
|
11
|
+
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(raw);
|
|
14
|
+
} catch {
|
|
15
|
+
return raw;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function parseValue(raw) {
|
|
19
|
+
const parsed = parseJsonValue(raw);
|
|
20
|
+
if (parsed !== raw) return parsed;
|
|
21
|
+
return cliNumberPattern.test(raw) ? Number(raw) : raw;
|
|
22
|
+
}
|
|
23
|
+
function getInputFields(field) {
|
|
24
|
+
const input = unwrapZodField(field);
|
|
25
|
+
const inputDef = getFieldDef(input);
|
|
26
|
+
if (inputDef.typeName === z.ZodFirstPartyTypeKind.ZodUnion) return (inputDef.options ?? []).flatMap(getInputFields);
|
|
27
|
+
return [
|
|
28
|
+
input
|
|
29
|
+
];
|
|
30
|
+
}
|
|
31
|
+
function getRepeatedInputFields(fields, index) {
|
|
32
|
+
const collections = fields.filter((field)=>{
|
|
33
|
+
const kind = getFieldKind(field);
|
|
34
|
+
return kind === z.ZodFirstPartyTypeKind.ZodArray || kind === z.ZodFirstPartyTypeKind.ZodTuple;
|
|
35
|
+
});
|
|
36
|
+
if (0 === collections.length) return fields;
|
|
37
|
+
return collections.flatMap((field)=>{
|
|
38
|
+
const fieldDef = getFieldDef(field);
|
|
39
|
+
const item = fieldDef.typeName === z.ZodFirstPartyTypeKind.ZodArray ? fieldDef.type : fieldDef.items?.[index] ?? fieldDef.rest;
|
|
40
|
+
return item ? getInputFields(item) : [];
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
function acceptsValue(field, value) {
|
|
44
|
+
switch(getFieldKind(field)){
|
|
45
|
+
case z.ZodFirstPartyTypeKind.ZodString:
|
|
46
|
+
return 'string' == typeof value && field.safeParse(value).success;
|
|
47
|
+
case z.ZodFirstPartyTypeKind.ZodNumber:
|
|
48
|
+
return 'number' == typeof value && field.safeParse(value).success;
|
|
49
|
+
case z.ZodFirstPartyTypeKind.ZodBoolean:
|
|
50
|
+
return 'boolean' == typeof value;
|
|
51
|
+
case z.ZodFirstPartyTypeKind.ZodEnum:
|
|
52
|
+
case z.ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
53
|
+
case z.ZodFirstPartyTypeKind.ZodLiteral:
|
|
54
|
+
return field.safeParse(value).success;
|
|
55
|
+
case z.ZodFirstPartyTypeKind.ZodArray:
|
|
56
|
+
case z.ZodFirstPartyTypeKind.ZodTuple:
|
|
57
|
+
return Array.isArray(value);
|
|
58
|
+
case z.ZodFirstPartyTypeKind.ZodObject:
|
|
59
|
+
case z.ZodFirstPartyTypeKind.ZodRecord:
|
|
60
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
61
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value);
|
|
62
|
+
default:
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function usesLegacyInference(field) {
|
|
67
|
+
const kind = getFieldKind(field);
|
|
68
|
+
return kind === z.ZodFirstPartyTypeKind.ZodAny || kind === z.ZodFirstPartyTypeKind.ZodUnknown;
|
|
69
|
+
}
|
|
70
|
+
function parseCliValue(raw, field, index) {
|
|
71
|
+
if (!field) return parseValue(raw);
|
|
72
|
+
const json = parseJsonValue(raw);
|
|
73
|
+
const wholeFields = getInputFields(field);
|
|
74
|
+
if (json !== raw && wholeFields.some((input)=>acceptsValue(input, json))) return json;
|
|
75
|
+
const fields = void 0 === index ? wholeFields : getRepeatedInputFields(wholeFields, index);
|
|
76
|
+
const candidates = json !== raw ? [
|
|
77
|
+
json,
|
|
78
|
+
raw
|
|
79
|
+
] : [
|
|
80
|
+
raw
|
|
81
|
+
];
|
|
82
|
+
if (cliNumberPattern.test(raw)) candidates.push(Number(raw));
|
|
83
|
+
if ('true' === raw || 'false' === raw) candidates.push('true' === raw);
|
|
84
|
+
for (const candidate of candidates)if (fields.some((input)=>acceptsValue(input, candidate))) return candidate;
|
|
85
|
+
return fields.some(usesLegacyInference) ? parseValue(raw) : raw;
|
|
86
|
+
}
|
|
87
|
+
export { parseCliValue, parseValue };
|
|
@@ -5,7 +5,7 @@ import { assert } from "../utils.mjs";
|
|
|
5
5
|
import { maskConfig, parseJson } from "./helper.mjs";
|
|
6
6
|
import { initDebugConfig } from "./init-debug.mjs";
|
|
7
7
|
const MODEL_CONFIG_DOC_URL = 'https://midscenejs.com/model-common-config.html';
|
|
8
|
-
const getCurrentVersion = ()=>"1.12.7
|
|
8
|
+
const getCurrentVersion = ()=>"1.12.7";
|
|
9
9
|
const getInvalidModelFamilyMessage = (modelFamily)=>`Invalid MIDSCENE_MODEL_FAMILY value: ${modelFamily}. Current version v${getCurrentVersion()} accepts the following model families: ${MODEL_FAMILY_VALUES.join(', ')}. You can also visit ${MODEL_CONFIG_DOC_URL} for the latest configuration information.`;
|
|
10
10
|
const KEYS_MAP = {
|
|
11
11
|
insight: INSIGHT_MODEL_CONFIG_KEYS,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
function unwrapZodField(field) {
|
|
3
2
|
const f = field;
|
|
4
3
|
if (!f._def) return f;
|
|
@@ -9,56 +8,6 @@ function unwrapZodField(field) {
|
|
|
9
8
|
}
|
|
10
9
|
return f;
|
|
11
10
|
}
|
|
12
|
-
function getLiteralValueKind(value) {
|
|
13
|
-
if ('string' == typeof value) return 'string';
|
|
14
|
-
if ('number' == typeof value) return 'number';
|
|
15
|
-
if ('boolean' == typeof value) return 'boolean';
|
|
16
|
-
return 'unknown';
|
|
17
|
-
}
|
|
18
|
-
function getZodValueKinds(field) {
|
|
19
|
-
const actualField = unwrapZodField(field);
|
|
20
|
-
const definition = actualField._def;
|
|
21
|
-
switch(definition?.typeName){
|
|
22
|
-
case 'ZodString':
|
|
23
|
-
case 'ZodEnum':
|
|
24
|
-
return new Set([
|
|
25
|
-
'string'
|
|
26
|
-
]);
|
|
27
|
-
case 'ZodNumber':
|
|
28
|
-
return new Set([
|
|
29
|
-
'number'
|
|
30
|
-
]);
|
|
31
|
-
case 'ZodBoolean':
|
|
32
|
-
return new Set([
|
|
33
|
-
'boolean'
|
|
34
|
-
]);
|
|
35
|
-
case 'ZodArray':
|
|
36
|
-
case 'ZodTuple':
|
|
37
|
-
return new Set([
|
|
38
|
-
'array'
|
|
39
|
-
]);
|
|
40
|
-
case 'ZodObject':
|
|
41
|
-
case 'ZodRecord':
|
|
42
|
-
case 'ZodDiscriminatedUnion':
|
|
43
|
-
return new Set([
|
|
44
|
-
'object'
|
|
45
|
-
]);
|
|
46
|
-
case 'ZodLiteral':
|
|
47
|
-
return new Set([
|
|
48
|
-
getLiteralValueKind(definition.value)
|
|
49
|
-
]);
|
|
50
|
-
case 'ZodNativeEnum':
|
|
51
|
-
return new Set(z.util.getValidEnumValues(definition.values ?? {}).map(getLiteralValueKind));
|
|
52
|
-
case 'ZodUnion':
|
|
53
|
-
return new Set((definition.options ?? []).flatMap((option)=>[
|
|
54
|
-
...getZodValueKinds(option)
|
|
55
|
-
]));
|
|
56
|
-
default:
|
|
57
|
-
return new Set([
|
|
58
|
-
'unknown'
|
|
59
|
-
]);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
11
|
function isMidsceneLocatorField(field) {
|
|
63
12
|
const actualField = unwrapZodField(field);
|
|
64
13
|
if (actualField._def?.typeName === 'ZodObject') {
|
|
@@ -98,4 +47,4 @@ function getZodDescription(field) {
|
|
|
98
47
|
if (isMidsceneLocatorField(actualField)) return 'Location information for the target element';
|
|
99
48
|
return null;
|
|
100
49
|
}
|
|
101
|
-
export { getZodDescription, getZodTypeName,
|
|
50
|
+
export { getZodDescription, getZodTypeName, isMidsceneLocatorField, unwrapZodField };
|
package/dist/lib/cli/cli-args.js
CHANGED
|
@@ -27,21 +27,14 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
27
27
|
canonicalizeCliArgKeys: ()=>canonicalizeCliArgKeys,
|
|
28
28
|
parseCliArgs: ()=>parseCliArgs,
|
|
29
29
|
formatCliValidationError: ()=>formatCliValidationError,
|
|
30
|
-
parseValue: ()=>parseValue,
|
|
30
|
+
parseValue: ()=>external_cli_value_js_namespaceObject.parseValue,
|
|
31
31
|
getCliOptionDisplay: ()=>getCliOptionDisplay
|
|
32
32
|
});
|
|
33
33
|
const external_zod_namespaceObject = require("zod");
|
|
34
34
|
const external_key_alias_utils_js_namespaceObject = require("../key-alias-utils.js");
|
|
35
|
-
const external_zod_schema_utils_js_namespaceObject = require("../zod-schema-utils.js");
|
|
36
35
|
const external_cli_error_js_namespaceObject = require("./cli-error.js");
|
|
37
|
-
const
|
|
38
|
-
function
|
|
39
|
-
const parsedJson = parseJsonValue(raw);
|
|
40
|
-
if (parsedJson !== raw) return parsedJson;
|
|
41
|
-
if (cliNumberPattern.test(raw)) return Number(raw);
|
|
42
|
-
return raw;
|
|
43
|
-
}
|
|
44
|
-
function walkCliArgs(args, setArgValue, fieldByCliName) {
|
|
36
|
+
const external_cli_value_js_namespaceObject = require("./cli-value.js");
|
|
37
|
+
function walkCliArgs(args, setArgValue) {
|
|
45
38
|
for(let i = 0; i < args.length; i++){
|
|
46
39
|
const arg = args[i];
|
|
47
40
|
if (!arg.startsWith('--')) continue;
|
|
@@ -49,10 +42,10 @@ function walkCliArgs(args, setArgValue, fieldByCliName) {
|
|
|
49
42
|
const eqIdx = body.indexOf('=');
|
|
50
43
|
if (eqIdx >= 0) {
|
|
51
44
|
const key = body.slice(0, eqIdx);
|
|
52
|
-
setArgValue(key,
|
|
45
|
+
setArgValue(key, body.slice(eqIdx + 1));
|
|
53
46
|
} else if (args[i + 1] && !args[i + 1].startsWith('--')) {
|
|
54
47
|
i++;
|
|
55
|
-
setArgValue(body,
|
|
48
|
+
setArgValue(body, args[i]);
|
|
56
49
|
} else setArgValue(body, true);
|
|
57
50
|
}
|
|
58
51
|
}
|
|
@@ -61,48 +54,29 @@ function buildCliFieldIndex(def) {
|
|
|
61
54
|
for (const [schemaKey, field] of Object.entries(def.schema))for (const cliName of getAcceptedCliOptionNames(schemaKey, def.cli?.options?.[schemaKey]))fieldByCliName.set(cliName, field);
|
|
62
55
|
return fieldByCliName;
|
|
63
56
|
}
|
|
64
|
-
function parseJsonValue(raw) {
|
|
65
|
-
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
66
|
-
try {
|
|
67
|
-
return JSON.parse(raw);
|
|
68
|
-
} catch {
|
|
69
|
-
return raw;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function parseCliValue(raw, field) {
|
|
73
|
-
if (!field) return parseValue(raw);
|
|
74
|
-
const kinds = (0, external_zod_schema_utils_js_namespaceObject.getZodValueKinds)(field);
|
|
75
|
-
if (kinds.has('object') || kinds.has('array')) {
|
|
76
|
-
const parsedJson = parseJsonValue(raw);
|
|
77
|
-
if (parsedJson !== raw) return parsedJson;
|
|
78
|
-
}
|
|
79
|
-
if (kinds.has('string')) return raw;
|
|
80
|
-
if (kinds.has('number') && cliNumberPattern.test(raw)) return Number(raw);
|
|
81
|
-
if (kinds.has('boolean')) {
|
|
82
|
-
if ('true' === raw) return true;
|
|
83
|
-
if ('false' === raw) return false;
|
|
84
|
-
}
|
|
85
|
-
return kinds.has('unknown') ? parseValue(raw) : raw;
|
|
86
|
-
}
|
|
87
57
|
function parseCliArgs(args, def) {
|
|
88
|
-
const result = {};
|
|
89
58
|
const fieldByCliName = def ? buildCliFieldIndex(def) : void 0;
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
59
|
+
const tokensByName = new Map();
|
|
60
|
+
walkCliArgs(args, (key, raw)=>{
|
|
61
|
+
const tokens = tokensByName.get(key) ?? [];
|
|
62
|
+
tokens.push(raw);
|
|
63
|
+
tokensByName.set(key, tokens);
|
|
64
|
+
});
|
|
65
|
+
const result = {};
|
|
66
|
+
for (const [key, tokens] of tokensByName){
|
|
67
|
+
let elementIndex = 0;
|
|
68
|
+
for (const raw of tokens){
|
|
69
|
+
const existing = result[key];
|
|
70
|
+
const value = true === raw ? true : (0, external_cli_value_js_namespaceObject.parseCliValue)(raw, fieldByCliName?.get(key), tokens.length > 1 ? elementIndex : void 0);
|
|
71
|
+
if (void 0 === existing) result[key] = value;
|
|
72
|
+
else if (Array.isArray(existing)) existing.push(value);
|
|
73
|
+
else result[key] = [
|
|
74
|
+
existing,
|
|
75
|
+
value
|
|
76
|
+
];
|
|
77
|
+
elementIndex = Array.isArray(result[key]) ? result[key].length : 1;
|
|
100
78
|
}
|
|
101
|
-
|
|
102
|
-
existing,
|
|
103
|
-
value
|
|
104
|
-
];
|
|
105
|
-
}, fieldByCliName);
|
|
79
|
+
}
|
|
106
80
|
return result;
|
|
107
81
|
}
|
|
108
82
|
function formatCliOptionName(name) {
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
parseValue: ()=>parseValue,
|
|
28
|
+
parseCliValue: ()=>parseCliValue
|
|
29
|
+
});
|
|
30
|
+
const external_zod_namespaceObject = require("zod");
|
|
31
|
+
const external_zod_schema_utils_js_namespaceObject = require("../zod-schema-utils.js");
|
|
32
|
+
const cliNumberPattern = /^-?\d+(\.\d+)?$/;
|
|
33
|
+
function getFieldDef(field) {
|
|
34
|
+
return field._def;
|
|
35
|
+
}
|
|
36
|
+
function getFieldKind(field) {
|
|
37
|
+
return getFieldDef(field).typeName;
|
|
38
|
+
}
|
|
39
|
+
function parseJsonValue(raw) {
|
|
40
|
+
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(raw);
|
|
43
|
+
} catch {
|
|
44
|
+
return raw;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function parseValue(raw) {
|
|
48
|
+
const parsed = parseJsonValue(raw);
|
|
49
|
+
if (parsed !== raw) return parsed;
|
|
50
|
+
return cliNumberPattern.test(raw) ? Number(raw) : raw;
|
|
51
|
+
}
|
|
52
|
+
function getInputFields(field) {
|
|
53
|
+
const input = (0, external_zod_schema_utils_js_namespaceObject.unwrapZodField)(field);
|
|
54
|
+
const inputDef = getFieldDef(input);
|
|
55
|
+
if (inputDef.typeName === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodUnion) return (inputDef.options ?? []).flatMap(getInputFields);
|
|
56
|
+
return [
|
|
57
|
+
input
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
function getRepeatedInputFields(fields, index) {
|
|
61
|
+
const collections = fields.filter((field)=>{
|
|
62
|
+
const kind = getFieldKind(field);
|
|
63
|
+
return kind === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodArray || kind === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodTuple;
|
|
64
|
+
});
|
|
65
|
+
if (0 === collections.length) return fields;
|
|
66
|
+
return collections.flatMap((field)=>{
|
|
67
|
+
const fieldDef = getFieldDef(field);
|
|
68
|
+
const item = fieldDef.typeName === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodArray ? fieldDef.type : fieldDef.items?.[index] ?? fieldDef.rest;
|
|
69
|
+
return item ? getInputFields(item) : [];
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function acceptsValue(field, value) {
|
|
73
|
+
switch(getFieldKind(field)){
|
|
74
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodString:
|
|
75
|
+
return 'string' == typeof value && field.safeParse(value).success;
|
|
76
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodNumber:
|
|
77
|
+
return 'number' == typeof value && field.safeParse(value).success;
|
|
78
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodBoolean:
|
|
79
|
+
return 'boolean' == typeof value;
|
|
80
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodEnum:
|
|
81
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
82
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodLiteral:
|
|
83
|
+
return field.safeParse(value).success;
|
|
84
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodArray:
|
|
85
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodTuple:
|
|
86
|
+
return Array.isArray(value);
|
|
87
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodObject:
|
|
88
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodRecord:
|
|
89
|
+
case external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
90
|
+
return 'object' == typeof value && null !== value && !Array.isArray(value);
|
|
91
|
+
default:
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function usesLegacyInference(field) {
|
|
96
|
+
const kind = getFieldKind(field);
|
|
97
|
+
return kind === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodAny || kind === external_zod_namespaceObject.z.ZodFirstPartyTypeKind.ZodUnknown;
|
|
98
|
+
}
|
|
99
|
+
function parseCliValue(raw, field, index) {
|
|
100
|
+
if (!field) return parseValue(raw);
|
|
101
|
+
const json = parseJsonValue(raw);
|
|
102
|
+
const wholeFields = getInputFields(field);
|
|
103
|
+
if (json !== raw && wholeFields.some((input)=>acceptsValue(input, json))) return json;
|
|
104
|
+
const fields = void 0 === index ? wholeFields : getRepeatedInputFields(wholeFields, index);
|
|
105
|
+
const candidates = json !== raw ? [
|
|
106
|
+
json,
|
|
107
|
+
raw
|
|
108
|
+
] : [
|
|
109
|
+
raw
|
|
110
|
+
];
|
|
111
|
+
if (cliNumberPattern.test(raw)) candidates.push(Number(raw));
|
|
112
|
+
if ('true' === raw || 'false' === raw) candidates.push('true' === raw);
|
|
113
|
+
for (const candidate of candidates)if (fields.some((input)=>acceptsValue(input, candidate))) return candidate;
|
|
114
|
+
return fields.some(usesLegacyInference) ? parseValue(raw) : raw;
|
|
115
|
+
}
|
|
116
|
+
exports.parseCliValue = __webpack_exports__.parseCliValue;
|
|
117
|
+
exports.parseValue = __webpack_exports__.parseValue;
|
|
118
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
119
|
+
"parseCliValue",
|
|
120
|
+
"parseValue"
|
|
121
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
122
|
+
Object.defineProperty(exports, '__esModule', {
|
|
123
|
+
value: true
|
|
124
|
+
});
|
|
@@ -37,7 +37,7 @@ const external_utils_js_namespaceObject = require("../utils.js");
|
|
|
37
37
|
const external_helper_js_namespaceObject = require("./helper.js");
|
|
38
38
|
const external_init_debug_js_namespaceObject = require("./init-debug.js");
|
|
39
39
|
const MODEL_CONFIG_DOC_URL = 'https://midscenejs.com/model-common-config.html';
|
|
40
|
-
const getCurrentVersion = ()=>"1.12.7
|
|
40
|
+
const getCurrentVersion = ()=>"1.12.7";
|
|
41
41
|
const getInvalidModelFamilyMessage = (modelFamily)=>`Invalid MIDSCENE_MODEL_FAMILY value: ${modelFamily}. Current version v${getCurrentVersion()} accepts the following model families: ${external_types_js_namespaceObject.MODEL_FAMILY_VALUES.join(', ')}. You can also visit ${MODEL_CONFIG_DOC_URL} for the latest configuration information.`;
|
|
42
42
|
const KEYS_MAP = {
|
|
43
43
|
insight: external_constants_js_namespaceObject.INSIGHT_MODEL_CONFIG_KEYS,
|
|
@@ -24,13 +24,11 @@ var __webpack_require__ = {};
|
|
|
24
24
|
var __webpack_exports__ = {};
|
|
25
25
|
__webpack_require__.r(__webpack_exports__);
|
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
-
getZodTypeName: ()=>getZodTypeName,
|
|
28
27
|
getZodDescription: ()=>getZodDescription,
|
|
29
|
-
|
|
28
|
+
getZodTypeName: ()=>getZodTypeName,
|
|
30
29
|
isMidsceneLocatorField: ()=>isMidsceneLocatorField,
|
|
31
30
|
unwrapZodField: ()=>unwrapZodField
|
|
32
31
|
});
|
|
33
|
-
const external_zod_namespaceObject = require("zod");
|
|
34
32
|
function unwrapZodField(field) {
|
|
35
33
|
const f = field;
|
|
36
34
|
if (!f._def) return f;
|
|
@@ -41,56 +39,6 @@ function unwrapZodField(field) {
|
|
|
41
39
|
}
|
|
42
40
|
return f;
|
|
43
41
|
}
|
|
44
|
-
function getLiteralValueKind(value) {
|
|
45
|
-
if ('string' == typeof value) return 'string';
|
|
46
|
-
if ('number' == typeof value) return 'number';
|
|
47
|
-
if ('boolean' == typeof value) return 'boolean';
|
|
48
|
-
return 'unknown';
|
|
49
|
-
}
|
|
50
|
-
function getZodValueKinds(field) {
|
|
51
|
-
const actualField = unwrapZodField(field);
|
|
52
|
-
const definition = actualField._def;
|
|
53
|
-
switch(definition?.typeName){
|
|
54
|
-
case 'ZodString':
|
|
55
|
-
case 'ZodEnum':
|
|
56
|
-
return new Set([
|
|
57
|
-
'string'
|
|
58
|
-
]);
|
|
59
|
-
case 'ZodNumber':
|
|
60
|
-
return new Set([
|
|
61
|
-
'number'
|
|
62
|
-
]);
|
|
63
|
-
case 'ZodBoolean':
|
|
64
|
-
return new Set([
|
|
65
|
-
'boolean'
|
|
66
|
-
]);
|
|
67
|
-
case 'ZodArray':
|
|
68
|
-
case 'ZodTuple':
|
|
69
|
-
return new Set([
|
|
70
|
-
'array'
|
|
71
|
-
]);
|
|
72
|
-
case 'ZodObject':
|
|
73
|
-
case 'ZodRecord':
|
|
74
|
-
case 'ZodDiscriminatedUnion':
|
|
75
|
-
return new Set([
|
|
76
|
-
'object'
|
|
77
|
-
]);
|
|
78
|
-
case 'ZodLiteral':
|
|
79
|
-
return new Set([
|
|
80
|
-
getLiteralValueKind(definition.value)
|
|
81
|
-
]);
|
|
82
|
-
case 'ZodNativeEnum':
|
|
83
|
-
return new Set(external_zod_namespaceObject.z.util.getValidEnumValues(definition.values ?? {}).map(getLiteralValueKind));
|
|
84
|
-
case 'ZodUnion':
|
|
85
|
-
return new Set((definition.options ?? []).flatMap((option)=>[
|
|
86
|
-
...getZodValueKinds(option)
|
|
87
|
-
]));
|
|
88
|
-
default:
|
|
89
|
-
return new Set([
|
|
90
|
-
'unknown'
|
|
91
|
-
]);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
42
|
function isMidsceneLocatorField(field) {
|
|
95
43
|
const actualField = unwrapZodField(field);
|
|
96
44
|
if (actualField._def?.typeName === 'ZodObject') {
|
|
@@ -132,13 +80,11 @@ function getZodDescription(field) {
|
|
|
132
80
|
}
|
|
133
81
|
exports.getZodDescription = __webpack_exports__.getZodDescription;
|
|
134
82
|
exports.getZodTypeName = __webpack_exports__.getZodTypeName;
|
|
135
|
-
exports.getZodValueKinds = __webpack_exports__.getZodValueKinds;
|
|
136
83
|
exports.isMidsceneLocatorField = __webpack_exports__.isMidsceneLocatorField;
|
|
137
84
|
exports.unwrapZodField = __webpack_exports__.unwrapZodField;
|
|
138
85
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
139
86
|
"getZodDescription",
|
|
140
87
|
"getZodTypeName",
|
|
141
|
-
"getZodValueKinds",
|
|
142
88
|
"isMidsceneLocatorField",
|
|
143
89
|
"unwrapZodField"
|
|
144
90
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ToolCliOption, ToolDefinition } from '../agent-tools/types';
|
|
2
|
-
export
|
|
2
|
+
export { parseValue } from './cli-value';
|
|
3
3
|
export declare function parseCliArgs(args: string[], def?: ToolDefinition): Record<string, unknown>;
|
|
4
4
|
export declare function getCliOptionDisplay(key: string, cliOption?: ToolCliOption): {
|
|
5
5
|
label: string;
|
|
@@ -1,17 +1,9 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
export type ZodValueKind = 'string' | 'number' | 'boolean' | 'array' | 'object' | 'unknown';
|
|
1
|
+
import type { z } from 'zod';
|
|
3
2
|
/**
|
|
4
3
|
* Recursively unwrap optional, nullable, default, and effects wrapper types
|
|
5
4
|
* to get the actual inner Zod type
|
|
6
5
|
*/
|
|
7
6
|
export declare function unwrapZodField(field: unknown): unknown;
|
|
8
|
-
/**
|
|
9
|
-
* Classify the supported top-level Zod input kinds without running validation.
|
|
10
|
-
* Unrecognized types return `unknown`; refinements are not evaluated. Unlike
|
|
11
|
-
* `getZodTypeName`, this normalizes enums, literals, and unions so consumers
|
|
12
|
-
* can make type-directed decisions without parsing its display label.
|
|
13
|
-
*/
|
|
14
|
-
export declare function getZodValueKinds(field: unknown): Set<ZodValueKind>;
|
|
15
7
|
/**
|
|
16
8
|
* Check if a field is a Midscene locator field
|
|
17
9
|
* Locator input schemas are identified by their prompt field.
|
package/package.json
CHANGED
package/src/cli/cli-args.ts
CHANGED
|
@@ -1,26 +1,13 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import type { ToolCliOption, ToolDefinition } from '../agent-tools/types';
|
|
3
3
|
import { getKeyAliases } from '../key-alias-utils';
|
|
4
|
-
import { getZodValueKinds } from '../zod-schema-utils';
|
|
5
4
|
import { CLIError } from './cli-error';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export function parseValue(raw: string): unknown {
|
|
10
|
-
const parsedJson = parseJsonValue(raw);
|
|
11
|
-
if (parsedJson !== raw) return parsedJson;
|
|
12
|
-
|
|
13
|
-
if (cliNumberPattern.test(raw)) {
|
|
14
|
-
return Number(raw);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return raw;
|
|
18
|
-
}
|
|
5
|
+
import { parseCliValue } from './cli-value';
|
|
6
|
+
export { parseValue } from './cli-value';
|
|
19
7
|
|
|
20
8
|
function walkCliArgs(
|
|
21
9
|
args: string[],
|
|
22
|
-
setArgValue: (key: string, value:
|
|
23
|
-
fieldByCliName?: ReadonlyMap<string, z.ZodTypeAny>,
|
|
10
|
+
setArgValue: (key: string, value: string | true) => void,
|
|
24
11
|
): void {
|
|
25
12
|
for (let i = 0; i < args.length; i++) {
|
|
26
13
|
const arg = args[i];
|
|
@@ -31,13 +18,10 @@ function walkCliArgs(
|
|
|
31
18
|
|
|
32
19
|
if (eqIdx >= 0) {
|
|
33
20
|
const key = body.slice(0, eqIdx);
|
|
34
|
-
setArgValue(
|
|
35
|
-
key,
|
|
36
|
-
parseCliValue(body.slice(eqIdx + 1), fieldByCliName?.get(key)),
|
|
37
|
-
);
|
|
21
|
+
setArgValue(key, body.slice(eqIdx + 1));
|
|
38
22
|
} else if (args[i + 1] && !args[i + 1].startsWith('--')) {
|
|
39
23
|
i++;
|
|
40
|
-
setArgValue(body,
|
|
24
|
+
setArgValue(body, args[i]);
|
|
41
25
|
} else {
|
|
42
26
|
setArgValue(body, true);
|
|
43
27
|
}
|
|
@@ -61,68 +45,41 @@ function buildCliFieldIndex(
|
|
|
61
45
|
return fieldByCliName;
|
|
62
46
|
}
|
|
63
47
|
|
|
64
|
-
function parseJsonValue(raw: string): unknown {
|
|
65
|
-
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
return JSON.parse(raw);
|
|
69
|
-
} catch {
|
|
70
|
-
// Preserve malformed JSON for string fields or the later schema error.
|
|
71
|
-
return raw;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function parseCliValue(raw: string, field?: z.ZodTypeAny): unknown {
|
|
76
|
-
if (!field) return parseValue(raw);
|
|
77
|
-
|
|
78
|
-
const kinds = getZodValueKinds(field);
|
|
79
|
-
if (kinds.has('object') || kinds.has('array')) {
|
|
80
|
-
const parsedJson = parseJsonValue(raw);
|
|
81
|
-
if (parsedJson !== raw) return parsedJson;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// CLI input cannot distinguish a numeric-looking identifier from a number.
|
|
85
|
-
// Prefer the lossless representation whenever the schema accepts strings.
|
|
86
|
-
if (kinds.has('string')) return raw;
|
|
87
|
-
|
|
88
|
-
if (kinds.has('number') && cliNumberPattern.test(raw)) {
|
|
89
|
-
return Number(raw);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (kinds.has('boolean')) {
|
|
93
|
-
if (raw === 'true') return true;
|
|
94
|
-
if (raw === 'false') return false;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return kinds.has('unknown') ? parseValue(raw) : raw;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
48
|
export function parseCliArgs(
|
|
101
49
|
args: string[],
|
|
102
50
|
def?: ToolDefinition,
|
|
103
51
|
): Record<string, unknown> {
|
|
104
|
-
const result: Record<string, unknown> = {};
|
|
105
52
|
const fieldByCliName = def ? buildCliFieldIndex(def) : undefined;
|
|
53
|
+
const tokensByName = new Map<string, Array<string | true>>();
|
|
54
|
+
walkCliArgs(args, (key, raw) => {
|
|
55
|
+
const tokens = tokensByName.get(key) ?? [];
|
|
56
|
+
tokens.push(raw);
|
|
57
|
+
tokensByName.set(key, tokens);
|
|
58
|
+
});
|
|
106
59
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
60
|
+
const result: Record<string, unknown> = {};
|
|
61
|
+
for (const [key, tokens] of tokensByName) {
|
|
62
|
+
let elementIndex = 0;
|
|
63
|
+
for (const raw of tokens) {
|
|
110
64
|
const existing = result[key];
|
|
65
|
+
const value =
|
|
66
|
+
raw === true
|
|
67
|
+
? true
|
|
68
|
+
: parseCliValue(
|
|
69
|
+
raw,
|
|
70
|
+
fieldByCliName?.get(key),
|
|
71
|
+
tokens.length > 1 ? elementIndex : undefined,
|
|
72
|
+
);
|
|
111
73
|
if (existing === undefined) {
|
|
112
74
|
result[key] = value;
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (Array.isArray(existing)) {
|
|
75
|
+
} else if (Array.isArray(existing)) {
|
|
117
76
|
existing.push(value);
|
|
118
|
-
|
|
119
|
-
|
|
77
|
+
} else {
|
|
78
|
+
result[key] = [existing, value];
|
|
120
79
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
fieldByCliName,
|
|
125
|
-
);
|
|
80
|
+
elementIndex = Array.isArray(result[key]) ? result[key].length : 1;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
126
83
|
|
|
127
84
|
return result;
|
|
128
85
|
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { unwrapZodField } from '../zod-schema-utils';
|
|
3
|
+
|
|
4
|
+
const cliNumberPattern = /^-?\d+(\.\d+)?$/;
|
|
5
|
+
|
|
6
|
+
interface CliZodDef {
|
|
7
|
+
typeName?: z.ZodFirstPartyTypeKind;
|
|
8
|
+
options?: z.ZodTypeAny[];
|
|
9
|
+
type?: z.ZodTypeAny;
|
|
10
|
+
items?: z.ZodTypeAny[];
|
|
11
|
+
rest?: z.ZodTypeAny | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getFieldDef(field: z.ZodTypeAny): CliZodDef {
|
|
15
|
+
return (field as z.ZodTypeAny & { _def: CliZodDef })._def;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getFieldKind(
|
|
19
|
+
field: z.ZodTypeAny,
|
|
20
|
+
): z.ZodFirstPartyTypeKind | undefined {
|
|
21
|
+
return getFieldDef(field).typeName;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parseJsonValue(raw: string): unknown {
|
|
25
|
+
if (!raw.startsWith('{') && !raw.startsWith('[')) return raw;
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(raw);
|
|
28
|
+
} catch {
|
|
29
|
+
// Preserve text for string inputs or the later schema validation error.
|
|
30
|
+
return raw;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function parseValue(raw: string): unknown {
|
|
35
|
+
const parsed = parseJsonValue(raw);
|
|
36
|
+
if (parsed !== raw) return parsed;
|
|
37
|
+
return cliNumberPattern.test(raw) ? Number(raw) : raw;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Keep the actual alternatives: a string literal is not an arbitrary string,
|
|
41
|
+
// and a repeated tuple argument must retain its position-specific schema.
|
|
42
|
+
function getInputFields(field: z.ZodTypeAny): z.ZodTypeAny[] {
|
|
43
|
+
const input = unwrapZodField(field) as z.ZodTypeAny;
|
|
44
|
+
const inputDef = getFieldDef(input);
|
|
45
|
+
if (inputDef.typeName === z.ZodFirstPartyTypeKind.ZodUnion) {
|
|
46
|
+
return (inputDef.options ?? []).flatMap(getInputFields);
|
|
47
|
+
}
|
|
48
|
+
return [input];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getRepeatedInputFields(
|
|
52
|
+
fields: z.ZodTypeAny[],
|
|
53
|
+
index: number,
|
|
54
|
+
): z.ZodTypeAny[] {
|
|
55
|
+
const collections = fields.filter((field) => {
|
|
56
|
+
const kind = getFieldKind(field);
|
|
57
|
+
return (
|
|
58
|
+
kind === z.ZodFirstPartyTypeKind.ZodArray ||
|
|
59
|
+
kind === z.ZodFirstPartyTypeKind.ZodTuple
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
if (collections.length === 0) return fields;
|
|
63
|
+
return collections.flatMap((field) => {
|
|
64
|
+
const fieldDef = getFieldDef(field);
|
|
65
|
+
const item =
|
|
66
|
+
fieldDef.typeName === z.ZodFirstPartyTypeKind.ZodArray
|
|
67
|
+
? fieldDef.type
|
|
68
|
+
: (fieldDef.items?.[index] ?? fieldDef.rest);
|
|
69
|
+
return item ? getInputFields(item) : [];
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Only inspect scalar constraints here. Effects have been unwrapped, and
|
|
74
|
+
// collection children are left to the existing full validation step.
|
|
75
|
+
function acceptsValue(
|
|
76
|
+
field: z.ZodTypeAny,
|
|
77
|
+
value: unknown,
|
|
78
|
+
): boolean | undefined {
|
|
79
|
+
switch (getFieldKind(field)) {
|
|
80
|
+
case z.ZodFirstPartyTypeKind.ZodString:
|
|
81
|
+
return typeof value === 'string' && field.safeParse(value).success;
|
|
82
|
+
case z.ZodFirstPartyTypeKind.ZodNumber:
|
|
83
|
+
return typeof value === 'number' && field.safeParse(value).success;
|
|
84
|
+
case z.ZodFirstPartyTypeKind.ZodBoolean:
|
|
85
|
+
return typeof value === 'boolean';
|
|
86
|
+
case z.ZodFirstPartyTypeKind.ZodEnum:
|
|
87
|
+
case z.ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
88
|
+
case z.ZodFirstPartyTypeKind.ZodLiteral:
|
|
89
|
+
return field.safeParse(value).success;
|
|
90
|
+
case z.ZodFirstPartyTypeKind.ZodArray:
|
|
91
|
+
case z.ZodFirstPartyTypeKind.ZodTuple:
|
|
92
|
+
return Array.isArray(value);
|
|
93
|
+
case z.ZodFirstPartyTypeKind.ZodObject:
|
|
94
|
+
case z.ZodFirstPartyTypeKind.ZodRecord:
|
|
95
|
+
case z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
96
|
+
return (
|
|
97
|
+
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
98
|
+
);
|
|
99
|
+
default:
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function usesLegacyInference(field: z.ZodTypeAny): boolean {
|
|
105
|
+
const kind = getFieldKind(field);
|
|
106
|
+
return (
|
|
107
|
+
kind === z.ZodFirstPartyTypeKind.ZodAny ||
|
|
108
|
+
kind === z.ZodFirstPartyTypeKind.ZodUnknown
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Decode one CLI token; index is supplied only for repeated options. */
|
|
113
|
+
export function parseCliValue(
|
|
114
|
+
raw: string,
|
|
115
|
+
field?: z.ZodTypeAny,
|
|
116
|
+
index?: number,
|
|
117
|
+
): unknown {
|
|
118
|
+
if (!field) return parseValue(raw);
|
|
119
|
+
const json = parseJsonValue(raw);
|
|
120
|
+
const wholeFields = getInputFields(field);
|
|
121
|
+
if (json !== raw && wholeFields.some((input) => acceptsValue(input, json))) {
|
|
122
|
+
return json;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const fields =
|
|
126
|
+
index === undefined
|
|
127
|
+
? wholeFields
|
|
128
|
+
: getRepeatedInputFields(wholeFields, index);
|
|
129
|
+
const candidates: unknown[] = json !== raw ? [json, raw] : [raw];
|
|
130
|
+
if (cliNumberPattern.test(raw)) candidates.push(Number(raw));
|
|
131
|
+
if (raw === 'true' || raw === 'false') candidates.push(raw === 'true');
|
|
132
|
+
for (const candidate of candidates) {
|
|
133
|
+
if (fields.some((input) => acceptsValue(input, candidate)))
|
|
134
|
+
return candidate;
|
|
135
|
+
}
|
|
136
|
+
return fields.some(usesLegacyInference) ? parseValue(raw) : raw;
|
|
137
|
+
}
|
package/src/zod-schema-utils.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
|
|
3
|
-
export type ZodValueKind =
|
|
4
|
-
| 'string'
|
|
5
|
-
| 'number'
|
|
6
|
-
| 'boolean'
|
|
7
|
-
| 'array'
|
|
8
|
-
| 'object'
|
|
9
|
-
| 'unknown';
|
|
1
|
+
import type { z } from 'zod';
|
|
10
2
|
|
|
11
3
|
/**
|
|
12
4
|
* Recursively unwrap optional, nullable, default, and effects wrapper types
|
|
@@ -39,64 +31,6 @@ export function unwrapZodField(field: unknown): unknown {
|
|
|
39
31
|
return f;
|
|
40
32
|
}
|
|
41
33
|
|
|
42
|
-
function getLiteralValueKind(value: unknown): ZodValueKind {
|
|
43
|
-
if (typeof value === 'string') return 'string';
|
|
44
|
-
if (typeof value === 'number') return 'number';
|
|
45
|
-
if (typeof value === 'boolean') return 'boolean';
|
|
46
|
-
return 'unknown';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Classify the supported top-level Zod input kinds without running validation.
|
|
51
|
-
* Unrecognized types return `unknown`; refinements are not evaluated. Unlike
|
|
52
|
-
* `getZodTypeName`, this normalizes enums, literals, and unions so consumers
|
|
53
|
-
* can make type-directed decisions without parsing its display label.
|
|
54
|
-
*/
|
|
55
|
-
export function getZodValueKinds(field: unknown): Set<ZodValueKind> {
|
|
56
|
-
const actualField = unwrapZodField(field) as {
|
|
57
|
-
_def?: {
|
|
58
|
-
typeName?: string;
|
|
59
|
-
options?: unknown[];
|
|
60
|
-
value?: unknown;
|
|
61
|
-
values?: Record<string, unknown>;
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
const definition = actualField._def;
|
|
65
|
-
|
|
66
|
-
switch (definition?.typeName) {
|
|
67
|
-
case 'ZodString':
|
|
68
|
-
case 'ZodEnum':
|
|
69
|
-
return new Set(['string']);
|
|
70
|
-
case 'ZodNumber':
|
|
71
|
-
return new Set(['number']);
|
|
72
|
-
case 'ZodBoolean':
|
|
73
|
-
return new Set(['boolean']);
|
|
74
|
-
case 'ZodArray':
|
|
75
|
-
case 'ZodTuple':
|
|
76
|
-
return new Set(['array']);
|
|
77
|
-
case 'ZodObject':
|
|
78
|
-
case 'ZodRecord':
|
|
79
|
-
case 'ZodDiscriminatedUnion':
|
|
80
|
-
return new Set(['object']);
|
|
81
|
-
case 'ZodLiteral':
|
|
82
|
-
return new Set([getLiteralValueKind(definition.value)]);
|
|
83
|
-
case 'ZodNativeEnum':
|
|
84
|
-
return new Set(
|
|
85
|
-
z.util
|
|
86
|
-
.getValidEnumValues(definition.values ?? {})
|
|
87
|
-
.map(getLiteralValueKind),
|
|
88
|
-
);
|
|
89
|
-
case 'ZodUnion':
|
|
90
|
-
return new Set(
|
|
91
|
-
(definition.options ?? []).flatMap((option) => [
|
|
92
|
-
...getZodValueKinds(option),
|
|
93
|
-
]),
|
|
94
|
-
);
|
|
95
|
-
default:
|
|
96
|
-
return new Set(['unknown']);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
34
|
/**
|
|
101
35
|
* Check if a field is a Midscene locator field
|
|
102
36
|
* Locator input schemas are identified by their prompt field.
|