@insmind/cli 2.1.5 → 2.1.6

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.
@@ -0,0 +1,114 @@
1
+ export const mediaListParameterNames = new Set([
2
+ "image_urls",
3
+ "reference_images",
4
+ "file_urls",
5
+ "video_urls",
6
+ "audio_urls"
7
+ ]);
8
+ function record(value) {
9
+ return typeof value === "object" && value !== null && !Array.isArray(value)
10
+ ? value
11
+ : undefined;
12
+ }
13
+ function text(value) {
14
+ if (typeof value !== "string")
15
+ return undefined;
16
+ const trimmed = value.trim();
17
+ return trimmed || undefined;
18
+ }
19
+ function requiredText(value) {
20
+ const result = text(value);
21
+ if (result === undefined)
22
+ throw new Error("invalid string");
23
+ return result;
24
+ }
25
+ function machineText(value) {
26
+ if (typeof value !== "string" || !value.trim())
27
+ throw new Error("invalid machine value");
28
+ return value;
29
+ }
30
+ export function cleanCatalogText(value) {
31
+ const result = text(value);
32
+ if (result === undefined)
33
+ return undefined;
34
+ return text(result.replace(/\{icon:\{[^{}]*\}\}/g, ""));
35
+ }
36
+ export function normalizeParameter(value, optionalControls) {
37
+ const field = record(value);
38
+ if (field === undefined)
39
+ throw new Error("invalid parameter");
40
+ if (field.is_open !== true)
41
+ return undefined;
42
+ const name = machineText(field.key);
43
+ const sourceType = requiredText(field.type);
44
+ const type = sourceType === "SLIDER"
45
+ ? "number"
46
+ : mediaListParameterNames.has(name)
47
+ ? "uri[]"
48
+ : name === "start_frame" || name === "end_frame" || sourceType === "IMAGE" || sourceType === "VIDEO"
49
+ ? "uri"
50
+ : sourceType === "NUMBER"
51
+ ? "number"
52
+ : sourceType === "TEXT" || sourceType === "INPUT" || sourceType === "SELECT"
53
+ ? "string"
54
+ : undefined;
55
+ if (type === undefined) {
56
+ if (field.required === true)
57
+ throw new Error("unsupported required parameter");
58
+ const description = text(field.description) ?? text(field.label);
59
+ Object.defineProperty(optionalControls, name, { value: description ? { description } : {}, enumerable: true, configurable: true, writable: true });
60
+ return undefined;
61
+ }
62
+ let minimum;
63
+ let maximum;
64
+ let step;
65
+ if (sourceType === "SLIDER" || sourceType === "NUMBER") {
66
+ const declaredStep = sourceType === "SLIDER" ? field.step : undefined;
67
+ if ([field.min, field.max, declaredStep].some(value => value !== undefined
68
+ && (typeof value !== "number" || !Number.isFinite(value)))
69
+ || typeof field.min === "number" && typeof field.max === "number" && field.min > field.max
70
+ || typeof declaredStep === "number" && declaredStep <= 0) {
71
+ throw new Error("invalid numeric constraints");
72
+ }
73
+ minimum = field.min;
74
+ maximum = field.max;
75
+ step = declaredStep;
76
+ }
77
+ let options;
78
+ if (sourceType === "SELECT") {
79
+ if (!Array.isArray(field.options))
80
+ throw new Error("invalid options");
81
+ options = field.options.map((value) => {
82
+ const option = record(value);
83
+ if (option === undefined)
84
+ throw new Error("invalid option");
85
+ const label = text(option.label);
86
+ return {
87
+ value: machineText(option.value),
88
+ ...(label === undefined ? {} : { label })
89
+ };
90
+ });
91
+ if (options.length === 0)
92
+ throw new Error("empty options");
93
+ }
94
+ const rawDescription = text(field.description) ?? text(field.label);
95
+ // MNS documents the service's wire encoding. Public URI-list inputs are native arrays.
96
+ const description = type === "uri[]" ? rawDescription
97
+ ?.replace(/JSON 数组的字符串/gu, "原生 JSON 数组")
98
+ .replace(/(?:例如|如)\s*(?:\\*["“])?\[.*?\](?:\\*["”])*/gu, '例如 ["https://example.com/media.png"]') : rawDescription;
99
+ const defaultValue = field.default_value;
100
+ const hasDefault = defaultValue !== undefined;
101
+ return {
102
+ name,
103
+ type,
104
+ required: field.required === true,
105
+ ...(minimum === undefined ? {} : { minimum }),
106
+ ...(maximum === undefined ? {} : { maximum }),
107
+ ...(step === undefined ? {} : { step }),
108
+ ...(description === undefined ? {} : { description }),
109
+ ...(hasDefault
110
+ ? { default: defaultValue }
111
+ : {}),
112
+ ...(options === undefined ? {} : { options })
113
+ };
114
+ }
@@ -0,0 +1,3 @@
1
+ export { cleanCatalogText, normalizeParameter, mediaListParameterNames } from "./fields.js";
2
+ export { parameterSchema, buildParameterModelSchema, buildToolArgumentsSchema } from "./schema.js";
3
+ export { inlineSchemaReferences } from "./inline-schema.js";
@@ -0,0 +1,72 @@
1
+ const schemaMaps = new Set(["properties", "patternProperties", "dependentSchemas"]);
2
+ const schemaLists = new Set(["allOf", "anyOf", "oneOf"]);
3
+ const schemaValues = new Set(["additionalProperties", "unevaluatedProperties", "propertyNames", "items",
4
+ "contains", "not", "if", "then", "else", "unevaluatedItems", "contentSchema"]);
5
+ function schema(value) {
6
+ if (typeof value === "boolean")
7
+ return value;
8
+ if (value && typeof value === "object" && !Array.isArray(value))
9
+ return value;
10
+ throw new Error("Expected a JSON Schema object or boolean");
11
+ }
12
+ /** Expand local JSON pointers without relaxing validation or interpreting example data as schemas. */
13
+ export function inlineSchemaReferences(input) {
14
+ function resolve(ref) {
15
+ if (ref === "#")
16
+ return input;
17
+ if (!ref.startsWith("#/"))
18
+ throw new Error("Only local JSON pointer references can be expanded: " + ref);
19
+ let value = input;
20
+ for (const part of decodeURIComponent(ref.slice(2)).split("/")) {
21
+ const key = part.replace(/~1/g, "/").replace(/~0/g, "~");
22
+ if (!value || typeof value !== "object" || !Object.hasOwn(value, key))
23
+ throw new Error("Unresolved schema reference: " + ref);
24
+ value = value[key];
25
+ }
26
+ return schema(value);
27
+ }
28
+ function visit(value, stack) {
29
+ if (typeof value === "boolean")
30
+ return value;
31
+ if ("$dynamicRef" in value || "$recursiveRef" in value || "$recursiveAnchor" in value || "$anchor" in value || "$dynamicAnchor" in value)
32
+ throw new Error("Dynamic or anchored schema references require a separate adapter");
33
+ if ("prefixItems" in value || Array.isArray(value.items))
34
+ throw new Error("Tuple schemas cannot be represented by this host schema adapter");
35
+ const entries = [];
36
+ for (const [key, child] of Object.entries(value)) {
37
+ if (["$ref", "$defs", "definitions"].includes(key))
38
+ continue;
39
+ if (key === "$id" && value !== input)
40
+ throw new Error("Nested schema resource identifiers cannot be inlined");
41
+ if (schemaMaps.has(key)) {
42
+ const fields = schema(child);
43
+ if (typeof fields === "boolean")
44
+ throw new Error("Expected a schema map: " + key);
45
+ entries.push([key, Object.fromEntries(Object.entries(fields).map(([name, field]) => [name, visit(schema(field), stack)]))]);
46
+ }
47
+ else if (schemaLists.has(key)) {
48
+ if (!Array.isArray(child))
49
+ throw new Error("Expected a schema list: " + key);
50
+ entries.push([key, child.map(item => visit(schema(item), stack))]);
51
+ }
52
+ else if (schemaValues.has(key))
53
+ entries.push([key, visit(schema(child), stack)]);
54
+ else
55
+ entries.push([key, structuredClone(child)]);
56
+ }
57
+ const siblings = Object.fromEntries(entries);
58
+ if (value.$ref === undefined)
59
+ return siblings;
60
+ if (typeof value.$ref !== "string")
61
+ throw new Error("Expected a string schema reference");
62
+ if (stack.includes(value.$ref))
63
+ throw new Error("Circular schema reference: " + value.$ref);
64
+ const target = visit(resolve(value.$ref), [...stack, value.$ref]);
65
+ // A $ref and its sibling keywords both apply in 2020-12. Object spread alone
66
+ // would overwrite duplicate constraints, such as two different enum lists.
67
+ if (!Object.keys(siblings).length)
68
+ return target;
69
+ return { ...siblings, allOf: [...(siblings.allOf ?? []), target] };
70
+ }
71
+ return visit(input, []);
72
+ }
@@ -0,0 +1,80 @@
1
+ export function parameterSchema(parameter, includeBounds = false) {
2
+ const common = {
3
+ ...(parameter.description === undefined ? {} : { description: parameter.description }),
4
+ ...(parameter.default === undefined ? {} : { default: parameter.default })
5
+ };
6
+ if (parameter.type === "uri")
7
+ return { type: "string", format: "uri", ...common };
8
+ if (parameter.type === "uri[]") {
9
+ return { type: "array", items: { type: "string", format: "uri" }, ...common };
10
+ }
11
+ if (parameter.type === "number" || parameter.type === "integer") {
12
+ return {
13
+ type: parameter.type,
14
+ ...(includeBounds && parameter.minimum !== undefined
15
+ ? { minimum: parameter.minimum }
16
+ : {}),
17
+ ...(includeBounds && parameter.maximum !== undefined
18
+ ? { maximum: parameter.maximum }
19
+ : {}),
20
+ ...common
21
+ };
22
+ }
23
+ if (["boolean", "object", "array"].includes(parameter.type)) {
24
+ return { type: parameter.type, ...common };
25
+ }
26
+ return {
27
+ type: "string",
28
+ ...(parameter.options === undefined
29
+ ? {}
30
+ : { enum: parameter.options.map((option) => option.value) }),
31
+ ...common
32
+ };
33
+ }
34
+ export function buildToolArgumentsSchema(models, modelDescription = "要调用的模型机器标识;取值来自 model list。") {
35
+ const properties = {
36
+ model: {
37
+ type: "string",
38
+ enum: models.map(model => model.model),
39
+ description: modelDescription
40
+ }
41
+ };
42
+ const branches = models.map(model => model.schema);
43
+ const fieldNames = new Set(branches.flatMap(branch => Object.keys(branch.properties)));
44
+ for (const name of fieldNames) {
45
+ if (name === "model")
46
+ continue;
47
+ const fields = branches.flatMap(branch => Object.hasOwn(branch.properties, name) ? [branch.properties[name]] : []);
48
+ const field = fields[0];
49
+ // An open sibling may accept a completely different value at this key.
50
+ // Its presence in the index must not restrict the selected model.
51
+ if (branches.some(branch => !Object.hasOwn(branch.properties, name) && branch.additionalProperties)) {
52
+ Object.defineProperty(properties, name, { value: field.description ? { description: field.description } : {}, enumerable: true });
53
+ }
54
+ else {
55
+ const variants = [...new Map(fields.map(item => [JSON.stringify(item), item])).values()];
56
+ Object.defineProperty(properties, name, { value: variants.length === 1 ? field : { anyOf: variants }, enumerable: true });
57
+ }
58
+ }
59
+ return {
60
+ type: "object",
61
+ properties,
62
+ required: ["model"],
63
+ additionalProperties: true,
64
+ allOf: models.map((model, i) => ({ if: { properties: { model: { const: model.model } }, required: ["model"] },
65
+ then: branches[i] }))
66
+ };
67
+ }
68
+ export function buildParameterModelSchema(model, managed = ["content_id", "contentid"]) {
69
+ const properties = {
70
+ ...Object.fromEntries(Object.entries(model.optionalControls ?? {}).filter(([key]) => !managed.includes(key))),
71
+ model: { type: "string", const: model.model, description: "要调用的模型机器标识。" }
72
+ };
73
+ for (const parameter of model.parameters) {
74
+ Object.defineProperty(properties, parameter.name, { value: parameterSchema(parameter, true),
75
+ enumerable: true, writable: true, configurable: true });
76
+ }
77
+ return { type: "object", properties,
78
+ required: ["model", ...model.parameters.filter(parameter => parameter.required).map(parameter => parameter.name)],
79
+ additionalProperties: true, propertyNames: { not: { enum: [...managed] } } };
80
+ }
@@ -1,77 +1,14 @@
1
+ import { buildParameterModelSchema, buildToolArgumentsSchema as buildSharedToolSchema, parameterSchema } from "#model-catalog";
1
2
  import { lazySchemaCompiler } from "../../platform/schema-compiler.js";
2
3
  import { inputValidationSummary, validationIssues } from "../../platform/schema-issues.js";
3
4
  import { unavailableCanvasMediaRoles } from "./canvas-models.js";
4
5
  import { canonicalizeInputFileUris } from "../../platform/input-uri.js";
5
6
  import { ToolInputError } from "./catalog.js";
6
- function parameterSchema(parameter, includeBounds = false) {
7
- const common = {
8
- ...(parameter.description === undefined ? {} : { description: parameter.description }),
9
- ...(parameter.default === undefined ? {} : { default: parameter.default })
10
- };
11
- if (parameter.type === "uri")
12
- return { type: "string", format: "uri", ...common };
13
- if (parameter.type === "uri[]") {
14
- return { type: "array", items: { type: "string", format: "uri" }, ...common };
15
- }
16
- if (parameter.type === "number" || parameter.type === "integer") {
17
- return {
18
- type: parameter.type,
19
- ...(includeBounds && parameter.minimum !== undefined
20
- ? { minimum: parameter.minimum }
21
- : {}),
22
- ...(includeBounds && parameter.maximum !== undefined
23
- ? { maximum: parameter.maximum }
24
- : {}),
25
- ...common
26
- };
27
- }
28
- if (["boolean", "object", "array"].includes(parameter.type)) {
29
- return { type: parameter.type, ...common };
30
- }
31
- return {
32
- type: "string",
33
- ...(parameter.options === undefined
34
- ? {}
35
- : { enum: parameter.options.map((option) => option.value) }),
36
- ...common
37
- };
38
- }
39
7
  export function buildToolArgumentsSchema(catalog, tool, modelDescription = "要调用的模型机器标识;取值来自 model list。") {
40
- const models = catalog.models.filter((model) => model.tool === tool);
41
- if (models.length === 0)
8
+ const models = catalog.models.filter(model => model.tool === tool);
9
+ if (!models.length)
42
10
  throw new ToolInputError("tool_not_found", "/tool");
43
- const properties = {
44
- model: {
45
- type: "string",
46
- enum: models.map(model => model.model),
47
- description: modelDescription
48
- }
49
- };
50
- const branches = models.map(buildModelArgumentsSchema);
51
- const fieldNames = new Set(branches.flatMap(branch => Object.keys(branch.properties)));
52
- for (const name of fieldNames) {
53
- if (name === "model")
54
- continue;
55
- const fields = branches.flatMap(branch => branch.properties[name] ? [branch.properties[name]] : []);
56
- const field = fields[0];
57
- // An open sibling may accept a completely different value at this key.
58
- // Its presence in the index must not restrict the selected model.
59
- if (branches.some(branch => !Object.hasOwn(branch.properties, name) && branch.additionalProperties)) {
60
- properties[name] = field.description ? { description: field.description } : {};
61
- }
62
- else {
63
- const variants = [...new Map(fields.map(item => [JSON.stringify(item), item])).values()];
64
- properties[name] = variants.length === 1 ? field : { anyOf: variants };
65
- }
66
- }
67
- return {
68
- type: "object",
69
- properties,
70
- required: ["model"],
71
- additionalProperties: true,
72
- allOf: models.map((model, i) => ({ if: { properties: { model: { const: model.model } }, required: ["model"] },
73
- then: branches[i] }))
74
- };
11
+ return buildSharedToolSchema(models.map(model => ({ model: model.model, schema: buildModelArgumentsSchema(model) })), modelDescription);
75
12
  }
76
13
  export function buildModelArgumentsSchema(model) {
77
14
  const managed = model.execution?.kind === "canvas" ? ["content_id", "contentid", "async"] : ["content_id", "contentid"];
@@ -85,29 +22,7 @@ export function buildModelArgumentsSchema(model) {
85
22
  }
86
23
  return { ...schema, propertyNames: { not: { enum: [...managed, ...unavailable] } } };
87
24
  }
88
- const properties = {
89
- ...Object.fromEntries(Object.entries(model.optionalControls ?? {}).filter(([key]) => !managed.includes(key))),
90
- model: {
91
- type: "string",
92
- const: model.model,
93
- description: "要调用的模型机器标识。"
94
- }
95
- };
96
- for (const parameter of model.parameters) {
97
- properties[parameter.name] = parameterSchema(parameter, true);
98
- }
99
- return {
100
- type: "object",
101
- properties,
102
- required: [
103
- "model",
104
- ...model.parameters
105
- .filter((parameter) => parameter.required)
106
- .map((parameter) => parameter.name)
107
- ],
108
- additionalProperties: true,
109
- propertyNames: { not: { enum: managed } }
110
- };
25
+ return buildParameterModelSchema(model, managed);
111
26
  }
112
27
  const compiler = lazySchemaCompiler({ allErrors: true, strict: true });
113
28
  /** Enforce structure that the CLI actually reads or transforms. Business controls go to the service. */
@@ -1,3 +1,4 @@
1
+ import { cleanCatalogText as cleanText, normalizeParameter as parameter } from "#model-catalog";
1
2
  import { RemoteRequestError } from "../../platform/signed-http-transport.js";
2
3
  import { defaultToolDefinitions } from "./catalog.js";
3
4
  import { canvasModel, canvasModelBindings, canvasSceneCodes } from "./canvas-models.js";
@@ -12,13 +13,6 @@ const rootTools = {
12
13
  "AI-video-generation": "video.generate"
13
14
  };
14
15
  const toolOrder = Object.fromEntries(defaultToolDefinitions.map((tool, index) => [tool.name, index]));
15
- const multiUriKeys = new Set([
16
- "image_urls",
17
- "reference_images",
18
- "file_urls",
19
- "video_urls",
20
- "audio_urls"
21
- ]);
22
16
  function record(value) {
23
17
  return typeof value === "object" && value !== null && !Array.isArray(value)
24
18
  ? value
@@ -39,12 +33,6 @@ function requiredText(value) {
39
33
  function number(value, fallback = 0) {
40
34
  return typeof value === "number" && Number.isFinite(value) ? value : fallback;
41
35
  }
42
- function cleanText(value) {
43
- const result = text(value);
44
- if (result === undefined)
45
- return undefined;
46
- return text(result.replace(/\{icon:\{[^{}]*\}\}/g, ""));
47
- }
48
36
  function collectScenes(raw) {
49
37
  const roots = new Map();
50
38
  for (const value of raw) {
@@ -77,88 +65,6 @@ function collectScenes(raw) {
77
65
  }
78
66
  return scenes;
79
67
  }
80
- function parameter(value, optionalControls) {
81
- const field = record(value);
82
- if (field === undefined)
83
- throw new Error("invalid parameter");
84
- if (field.is_open !== true)
85
- return undefined;
86
- const name = requiredText(field.key);
87
- if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name))
88
- return undefined;
89
- const sourceType = requiredText(field.type);
90
- const type = sourceType === "SLIDER"
91
- ? "number"
92
- : multiUriKeys.has(name)
93
- ? "uri[]"
94
- : name === "start_frame" || name === "end_frame" || sourceType === "IMAGE" || sourceType === "VIDEO"
95
- ? "uri"
96
- : sourceType === "NUMBER"
97
- ? "number"
98
- : sourceType === "TEXT" || sourceType === "INPUT" || sourceType === "SELECT"
99
- ? "string"
100
- : undefined;
101
- if (type === undefined) {
102
- if (field.required === true)
103
- throw new Error("unsupported required parameter");
104
- const description = text(field.description) ?? text(field.label);
105
- optionalControls[name] = description ? { description } : {};
106
- return undefined;
107
- }
108
- let minimum;
109
- let maximum;
110
- let step;
111
- if (sourceType === "SLIDER") {
112
- if (typeof field.min !== "number" || !Number.isFinite(field.min)
113
- || typeof field.max !== "number" || !Number.isFinite(field.max)
114
- || typeof field.step !== "number" || !Number.isFinite(field.step)
115
- || field.min > field.max || field.step <= 0) {
116
- throw new Error("invalid slider constraints");
117
- }
118
- minimum = field.min;
119
- maximum = field.max;
120
- step = field.step;
121
- }
122
- let options;
123
- if (sourceType === "SELECT") {
124
- if (!Array.isArray(field.options))
125
- throw new Error("invalid options");
126
- options = field.options.map((value) => {
127
- const option = record(value);
128
- if (option === undefined)
129
- throw new Error("invalid option");
130
- const label = text(option.label);
131
- return {
132
- value: requiredText(option.value),
133
- ...(label === undefined ? {} : { label })
134
- };
135
- });
136
- if (options.length === 0)
137
- throw new Error("empty options");
138
- }
139
- const rawDescription = text(field.description) ?? text(field.label);
140
- // MNS documents the service's wire encoding. Public URI-list inputs are native arrays.
141
- const description = type === "uri[]" ? rawDescription
142
- ?.replace(/JSON 数组的字符串/gu, "原生 JSON 数组")
143
- .replace(/(?:例如|如)\s*(?:\\*["“])?\[.*?\](?:\\*["”])*/gu, '例如 ["https://example.com/media.png"]') : rawDescription;
144
- const defaultValue = field.default_value;
145
- const hasDefault = defaultValue !== undefined
146
- && defaultValue !== null
147
- && !(typeof defaultValue === "string" && defaultValue.trim() === "");
148
- return {
149
- name,
150
- type,
151
- required: field.required === true,
152
- ...(minimum === undefined ? {} : { minimum }),
153
- ...(maximum === undefined ? {} : { maximum }),
154
- ...(step === undefined ? {} : { step }),
155
- ...(description === undefined ? {} : { description }),
156
- ...(hasDefault
157
- ? { default: defaultValue }
158
- : {}),
159
- ...(options === undefined ? {} : { options })
160
- };
161
- }
162
68
  function range(values, unit) {
163
69
  if (values.length === 0)
164
70
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@insmind/cli",
3
- "version": "2.1.5",
3
+ "version": "2.1.6",
4
4
  "description": "insMind command-line interface for agents and people.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -46,9 +46,12 @@
46
46
  "devDependencies": {
47
47
  "@gaoding/cli": "workspace:*"
48
48
  },
49
+ "imports": {
50
+ "#model-catalog": "./dist/shared/model-catalog/index.js"
51
+ },
49
52
  "gaodingRelease": {
50
- "sourceCommit": "dd07bc1432494f88eaa2d3a0976e6415cda8bc1b",
51
- "pipelineId": "259288",
52
- "pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/259288"
53
+ "sourceCommit": "20f11c2bd7335c4cad222fd90ebf5e5d6bafe353",
54
+ "pipelineId": "262872",
55
+ "pipelineUrl": "https://git.intra.gaoding.com/gtt/gaoding-cli/-/pipelines/262872"
53
56
  }
54
57
  }