@insmind/cli 1.3.0
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/LICENSE +21 -0
- package/README.md +11 -0
- package/contracts/operations/agent.send/input.schema.json +335 -0
- package/contracts/operations/agent.send/output.schema.json +248 -0
- package/contracts/operations/auth.status/input.schema.json +7 -0
- package/contracts/operations/auth.status/output.schema.json +119 -0
- package/contracts/operations/dam.copy/input.schema.json +42 -0
- package/contracts/operations/dam.copy/output.schema.json +43 -0
- package/contracts/operations/dam.delete/input.schema.json +24 -0
- package/contracts/operations/dam.delete/output.schema.json +5 -0
- package/contracts/operations/dam.download/input.schema.json +28 -0
- package/contracts/operations/dam.download/output.schema.json +51 -0
- package/contracts/operations/dam.folder.list/input.schema.json +36 -0
- package/contracts/operations/dam.folder.list/output.schema.json +104 -0
- package/contracts/operations/dam.get/input.schema.json +16 -0
- package/contracts/operations/dam.get/output.schema.json +107 -0
- package/contracts/operations/dam.list/input.schema.json +73 -0
- package/contracts/operations/dam.list/output.schema.json +145 -0
- package/contracts/operations/dam.move/input.schema.json +42 -0
- package/contracts/operations/dam.move/output.schema.json +40 -0
- package/contracts/operations/dam.rename/input.schema.json +42 -0
- package/contracts/operations/dam.rename/output.schema.json +43 -0
- package/contracts/operations/dam.repository.list/input.schema.json +7 -0
- package/contracts/operations/dam.repository.list/output.schema.json +65 -0
- package/contracts/operations/dam.search/input.schema.json +79 -0
- package/contracts/operations/dam.search/output.schema.json +145 -0
- package/contracts/operations/dam.tag.list/input.schema.json +21 -0
- package/contracts/operations/dam.tag.list/output.schema.json +52 -0
- package/contracts/operations/dam.upload/input.schema.json +43 -0
- package/contracts/operations/dam.upload/output.schema.json +107 -0
- package/contracts/operations/editor.apply/input.schema.json +177 -0
- package/contracts/operations/editor.apply/output.schema.json +15 -0
- package/contracts/operations/editor.connect/input.schema.json +13 -0
- package/contracts/operations/editor.connect/output.schema.json +17 -0
- package/contracts/operations/editor.disconnect/input.schema.json +7 -0
- package/contracts/operations/editor.disconnect/output.schema.json +14 -0
- package/contracts/operations/editor.save/input.schema.json +7 -0
- package/contracts/operations/editor.save/output.schema.json +23 -0
- package/contracts/operations/editor.screenshot/input.schema.json +7 -0
- package/contracts/operations/editor.screenshot/output.schema.json +16 -0
- package/contracts/operations/editor.snapshot/input.schema.json +7 -0
- package/contracts/operations/editor.snapshot/output.schema.json +148 -0
- package/contracts/operations/model.get/input.schema.json +16 -0
- package/contracts/operations/model.get/output.schema.json +167 -0
- package/contracts/operations/model.list/input.schema.json +13 -0
- package/contracts/operations/model.list/output.schema.json +97 -0
- package/contracts/operations/task.get/input.schema.json +14 -0
- package/contracts/operations/task.get/output.schema.json +103 -0
- package/contracts/operations/task.list/input.schema.json +18 -0
- package/contracts/operations/task.list/output.schema.json +53 -0
- package/contracts/operations/tool.call/input.schema.json +31 -0
- package/contracts/operations/tool.call/output.schema.json +115 -0
- package/contracts/operations/tool.list/input.schema.json +6 -0
- package/contracts/operations/tool.list/output.schema.json +40 -0
- package/dist/bin/agent-worker.js +41 -0
- package/dist/bin/insmind-cli.js +60 -0
- package/dist/bin/postinstall.js +69 -0
- package/dist/src/bootstrap/create-cli.js +124 -0
- package/dist/src/bootstrap/create-runtime.js +451 -0
- package/dist/src/bootstrap/run-agent-worker.js +37 -0
- package/dist/src/bootstrap/validators.js +169 -0
- package/dist/src/cli/action-binding.js +109 -0
- package/dist/src/cli/agent-commands.js +75 -0
- package/dist/src/cli/auth-commands.js +58 -0
- package/dist/src/cli/credits-command.js +23 -0
- package/dist/src/cli/dam-commands.js +470 -0
- package/dist/src/cli/editor-commands.js +74 -0
- package/dist/src/cli/errors.js +208 -0
- package/dist/src/cli/model-commands.js +62 -0
- package/dist/src/cli/org-commands.js +58 -0
- package/dist/src/cli/presenter.js +123 -0
- package/dist/src/cli/prompt.js +30 -0
- package/dist/src/cli/task-commands.js +48 -0
- package/dist/src/cli/tool-commands.js +101 -0
- package/dist/src/cli/update-command.js +15 -0
- package/dist/src/contracts/schema.js +4 -0
- package/dist/src/features/agent/creative-agent-adapter.js +42 -0
- package/dist/src/features/agent/creative-protocol.js +421 -0
- package/dist/src/features/agent/creative-stream.js +138 -0
- package/dist/src/features/agent/filems-resource-auth-key-provider.js +23 -0
- package/dist/src/features/agent/resource-authorizer.js +47 -0
- package/dist/src/features/agent/result-reader.js +30 -0
- package/dist/src/features/agent/use-cases.js +153 -0
- package/dist/src/features/auth/access-policy.js +101 -0
- package/dist/src/features/auth/credential-store.js +62 -0
- package/dist/src/features/auth/sso-service.js +196 -0
- package/dist/src/features/auth/state.js +129 -0
- package/dist/src/features/auth/use-cases.js +162 -0
- package/dist/src/features/credits/use-cases.js +52 -0
- package/dist/src/features/dam/asset-projection.js +278 -0
- package/dist/src/features/dam/dam-api-adapter.js +562 -0
- package/dist/src/features/dam/file-downloader.js +242 -0
- package/dist/src/features/dam/object-storage.js +193 -0
- package/dist/src/features/dam/operation-executor.js +72 -0
- package/dist/src/features/dam/registered-uploader.js +247 -0
- package/dist/src/features/dam/repository-catalog.js +127 -0
- package/dist/src/features/dam/storage-upload.js +141 -0
- package/dist/src/features/dam/transient-uploader.js +17 -0
- package/dist/src/features/dam/use-cases.js +470 -0
- package/dist/src/features/editor/bridge-client.js +109 -0
- package/dist/src/features/editor/bridge-process.js +229 -0
- package/dist/src/features/editor/bridge-server.js +337 -0
- package/dist/src/features/editor/protocol.js +1 -0
- package/dist/src/features/editor/public-url.js +36 -0
- package/dist/src/features/editor/session-state.js +78 -0
- package/dist/src/features/editor/session.js +213 -0
- package/dist/src/features/editor/use-cases.js +88 -0
- package/dist/src/features/org/org-service.js +76 -0
- package/dist/src/features/org/use-cases.js +163 -0
- package/dist/src/features/skill/bundled-skills.js +63 -0
- package/dist/src/features/skill/installer.js +271 -0
- package/dist/src/features/task/agent-background.js +63 -0
- package/dist/src/features/task/store.js +97 -0
- package/dist/src/features/task/types.js +1 -0
- package/dist/src/features/task/use-cases.js +139 -0
- package/dist/src/features/tool/catalog.js +83 -0
- package/dist/src/features/tool/dynamic-schema.js +128 -0
- package/dist/src/features/tool/mns-catalog-adapter.js +265 -0
- package/dist/src/features/tool/tool-api-adapter.js +369 -0
- package/dist/src/features/tool/use-cases.js +180 -0
- package/dist/src/features/update/latest-version.js +37 -0
- package/dist/src/features/update/update-notification.js +271 -0
- package/dist/src/features/update/update-service.js +226 -0
- package/dist/src/platform/installation-owner.js +81 -0
- package/dist/src/platform/json-input.js +67 -0
- package/dist/src/platform/local-json-file.js +59 -0
- package/dist/src/platform/open-browser.js +8 -0
- package/dist/src/platform/redact.js +80 -0
- package/dist/src/platform/remote-error-evidence.js +80 -0
- package/dist/src/platform/remote-protocol.js +10 -0
- package/dist/src/platform/resource-auth-key.js +1 -0
- package/dist/src/platform/safe-upload-file.js +146 -0
- package/dist/src/platform/signature.js +20 -0
- package/dist/src/platform/signed-http-transport.js +144 -0
- package/dist/src/platform/task-observer.js +1 -0
- package/dist/src/platform/url-safety.js +100 -0
- package/dist/src/product/billing.js +47 -0
- package/dist/src/product/config.js +85 -0
- package/dist/src/product/locale.js +38 -0
- package/dist/src/product/text.js +3 -0
- package/dist/src/telemetry/invocation.js +156 -0
- package/dist/src/telemetry/sls-sink.js +30 -0
- package/package.json +53 -0
- package/skills/insmind-cli/SKILL.md +15 -0
- package/skills/insmind-cli/references/agent.md +34 -0
- package/skills/insmind-cli/references/auth.md +8 -0
- package/skills/insmind-cli/references/creation.md +40 -0
- package/skills/insmind-cli/references/dam.md +51 -0
- package/skills/insmind-cli/references/editor.md +184 -0
- package/skills/insmind-cli/references/errors.md +29 -0
- package/skills/insmind-cli/references/generation-constraints.md +36 -0
- package/skills/insmind-cli/references/tool.md +31 -0
- package/skills/insmind-cli/references/update.md +11 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
2
|
+
import addFormatsImport, {} from "ajv-formats";
|
|
3
|
+
import { ToolInputError } from "./catalog.js";
|
|
4
|
+
function parameterSchema(parameter) {
|
|
5
|
+
const common = {
|
|
6
|
+
...(parameter.description === undefined ? {} : { description: parameter.description }),
|
|
7
|
+
...(parameter.default === undefined ? {} : { default: parameter.default })
|
|
8
|
+
};
|
|
9
|
+
if (parameter.type === "uri")
|
|
10
|
+
return { type: "string", format: "uri", ...common };
|
|
11
|
+
if (parameter.type === "uri[]") {
|
|
12
|
+
return { type: "array", items: { type: "string", format: "uri" }, ...common };
|
|
13
|
+
}
|
|
14
|
+
if (parameter.type === "number")
|
|
15
|
+
return { type: "number", ...common };
|
|
16
|
+
return {
|
|
17
|
+
type: "string",
|
|
18
|
+
...(parameter.options === undefined
|
|
19
|
+
? {}
|
|
20
|
+
: { enum: parameter.options.map((option) => option.value) }),
|
|
21
|
+
...common
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function sameShape(left, right) {
|
|
25
|
+
return left.type === right.type
|
|
26
|
+
&& left.format === right.format
|
|
27
|
+
&& left.items?.format === right.items?.format;
|
|
28
|
+
}
|
|
29
|
+
function mergeSchema(current, next) {
|
|
30
|
+
if (!sameShape(current, next)) {
|
|
31
|
+
const variants = current.anyOf ?? [current];
|
|
32
|
+
return variants.some((variant) => sameShape(variant, next))
|
|
33
|
+
? current
|
|
34
|
+
: { anyOf: [...variants, next] };
|
|
35
|
+
}
|
|
36
|
+
if (current.enum === undefined || next.enum === undefined) {
|
|
37
|
+
if (current.enum === undefined)
|
|
38
|
+
return current;
|
|
39
|
+
const { enum: ignored, ...unrestricted } = current;
|
|
40
|
+
void ignored;
|
|
41
|
+
return unrestricted;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
...current,
|
|
45
|
+
enum: [...new Set([...current.enum, ...next.enum])]
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function buildToolArgumentsSchema(catalog, tool) {
|
|
49
|
+
const models = catalog.models.filter((model) => model.tool === tool);
|
|
50
|
+
if (models.length === 0)
|
|
51
|
+
throw new ToolInputError();
|
|
52
|
+
const properties = {
|
|
53
|
+
model: {
|
|
54
|
+
type: "string",
|
|
55
|
+
description: "要调用的模型机器标识;取值来自 model list。"
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
for (const model of models) {
|
|
59
|
+
for (const parameter of model.parameters) {
|
|
60
|
+
const next = parameterSchema(parameter);
|
|
61
|
+
const current = properties[parameter.name];
|
|
62
|
+
properties[parameter.name] = current === undefined ? next : mergeSchema(current, next);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: "object",
|
|
67
|
+
properties,
|
|
68
|
+
required: ["model"],
|
|
69
|
+
additionalProperties: false
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
export function buildModelArgumentsSchema(model) {
|
|
73
|
+
const properties = {
|
|
74
|
+
model: {
|
|
75
|
+
type: "string",
|
|
76
|
+
const: model.model,
|
|
77
|
+
description: "要调用的模型机器标识。"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
for (const parameter of model.parameters) {
|
|
81
|
+
properties[parameter.name] = parameterSchema(parameter);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
type: "object",
|
|
85
|
+
properties,
|
|
86
|
+
required: [
|
|
87
|
+
"model",
|
|
88
|
+
...model.parameters
|
|
89
|
+
.filter((parameter) => parameter.required)
|
|
90
|
+
.map((parameter) => parameter.name)
|
|
91
|
+
],
|
|
92
|
+
additionalProperties: false
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const ajv = new Ajv2020({ allErrors: true, strict: true });
|
|
96
|
+
const addFormats = addFormatsImport;
|
|
97
|
+
addFormats(ajv);
|
|
98
|
+
function validator(model) {
|
|
99
|
+
return ajv.compile(buildModelArgumentsSchema(model));
|
|
100
|
+
}
|
|
101
|
+
function controlArgumentsValidator(model) {
|
|
102
|
+
const properties = {};
|
|
103
|
+
for (const parameter of model.parameters) {
|
|
104
|
+
if (parameter.name === "prompt"
|
|
105
|
+
|| parameter.type === "uri"
|
|
106
|
+
|| parameter.type === "uri[]")
|
|
107
|
+
continue;
|
|
108
|
+
properties[parameter.name] = parameterSchema(parameter);
|
|
109
|
+
}
|
|
110
|
+
return ajv.compile({
|
|
111
|
+
type: "object",
|
|
112
|
+
properties,
|
|
113
|
+
required: [],
|
|
114
|
+
additionalProperties: false
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
export function assertModelArguments(model, value) {
|
|
118
|
+
if (typeof value !== "object" || value === null
|
|
119
|
+
|| !validator(model)(value)) {
|
|
120
|
+
throw new ToolInputError();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export function modelControlArgumentsAreValid(model, value) {
|
|
124
|
+
return typeof value === "object"
|
|
125
|
+
&& value !== null
|
|
126
|
+
&& !Array.isArray(value)
|
|
127
|
+
&& controlArgumentsValidator(model)(value);
|
|
128
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { RemoteRequestError } from "../../platform/signed-http-transport.js";
|
|
2
|
+
export const ROOT_SCENE_CODES = [
|
|
3
|
+
"image-model-art",
|
|
4
|
+
"AI-image-generation-art",
|
|
5
|
+
"AI-video-generation",
|
|
6
|
+
"AI-text-generation"
|
|
7
|
+
];
|
|
8
|
+
const rootTools = {
|
|
9
|
+
"image-model-art": "image.generate",
|
|
10
|
+
"AI-image-generation-art": "image.generate",
|
|
11
|
+
"AI-video-generation": "video.generate",
|
|
12
|
+
"AI-text-generation": "text.generate"
|
|
13
|
+
};
|
|
14
|
+
const toolOrder = {
|
|
15
|
+
"image.generate": 0,
|
|
16
|
+
"video.generate": 1,
|
|
17
|
+
"text.generate": 2
|
|
18
|
+
};
|
|
19
|
+
const multiUriKeys = new Set([
|
|
20
|
+
"image_urls",
|
|
21
|
+
"reference_images",
|
|
22
|
+
"file_urls",
|
|
23
|
+
"video_urls",
|
|
24
|
+
"audio_urls"
|
|
25
|
+
]);
|
|
26
|
+
function record(value) {
|
|
27
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
28
|
+
? value
|
|
29
|
+
: undefined;
|
|
30
|
+
}
|
|
31
|
+
function text(value) {
|
|
32
|
+
if (typeof value !== "string")
|
|
33
|
+
return undefined;
|
|
34
|
+
const trimmed = value.trim();
|
|
35
|
+
return trimmed || undefined;
|
|
36
|
+
}
|
|
37
|
+
function requiredText(value) {
|
|
38
|
+
const result = text(value);
|
|
39
|
+
if (result === undefined)
|
|
40
|
+
throw new Error("invalid string");
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
function number(value, fallback = 0) {
|
|
44
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
45
|
+
}
|
|
46
|
+
function cleanText(value) {
|
|
47
|
+
const result = text(value);
|
|
48
|
+
if (result === undefined)
|
|
49
|
+
return undefined;
|
|
50
|
+
return text(result.replace(/\{icon:\{[^{}]*\}\}/g, ""));
|
|
51
|
+
}
|
|
52
|
+
function collectScenes(raw) {
|
|
53
|
+
const roots = new Map();
|
|
54
|
+
for (const value of raw) {
|
|
55
|
+
const item = record(value);
|
|
56
|
+
if (item === undefined)
|
|
57
|
+
throw new Error("invalid scene");
|
|
58
|
+
roots.set(requiredText(item.code), item);
|
|
59
|
+
}
|
|
60
|
+
const scenes = [];
|
|
61
|
+
const seen = new Set();
|
|
62
|
+
function visit(value, tool) {
|
|
63
|
+
const code = requiredText(value.code);
|
|
64
|
+
if (!seen.has(code)) {
|
|
65
|
+
seen.add(code);
|
|
66
|
+
scenes.push({ code, tool, sort: number(value.sort) });
|
|
67
|
+
}
|
|
68
|
+
if (value.children === undefined || value.children === null)
|
|
69
|
+
return;
|
|
70
|
+
if (!Array.isArray(value.children))
|
|
71
|
+
throw new Error("invalid children");
|
|
72
|
+
for (const child of value.children) {
|
|
73
|
+
const childRecord = record(child);
|
|
74
|
+
if (childRecord === undefined)
|
|
75
|
+
throw new Error("invalid child");
|
|
76
|
+
visit(childRecord, tool);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const rootCode of ROOT_SCENE_CODES) {
|
|
80
|
+
const root = roots.get(rootCode);
|
|
81
|
+
if (root !== undefined)
|
|
82
|
+
visit(root, rootTools[rootCode]);
|
|
83
|
+
}
|
|
84
|
+
if (scenes.length === 0)
|
|
85
|
+
throw new Error("empty scenes");
|
|
86
|
+
return scenes;
|
|
87
|
+
}
|
|
88
|
+
function parameter(value) {
|
|
89
|
+
const field = record(value);
|
|
90
|
+
if (field === undefined)
|
|
91
|
+
throw new Error("invalid parameter");
|
|
92
|
+
if (field.is_open !== true)
|
|
93
|
+
return undefined;
|
|
94
|
+
const name = requiredText(field.key);
|
|
95
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name))
|
|
96
|
+
return undefined;
|
|
97
|
+
const sourceType = requiredText(field.type);
|
|
98
|
+
const type = multiUriKeys.has(name)
|
|
99
|
+
? "uri[]"
|
|
100
|
+
: name === "start_frame" || name === "end_frame" || sourceType === "IMAGE"
|
|
101
|
+
? "uri"
|
|
102
|
+
: sourceType === "NUMBER"
|
|
103
|
+
? "number"
|
|
104
|
+
: sourceType === "TEXT" || sourceType === "INPUT" || sourceType === "SELECT"
|
|
105
|
+
? "string"
|
|
106
|
+
: undefined;
|
|
107
|
+
if (type === undefined)
|
|
108
|
+
throw new Error("unsupported parameter");
|
|
109
|
+
let options;
|
|
110
|
+
if (sourceType === "SELECT") {
|
|
111
|
+
if (!Array.isArray(field.options))
|
|
112
|
+
throw new Error("invalid options");
|
|
113
|
+
options = field.options.map((value) => {
|
|
114
|
+
const option = record(value);
|
|
115
|
+
if (option === undefined)
|
|
116
|
+
throw new Error("invalid option");
|
|
117
|
+
const label = text(option.label);
|
|
118
|
+
return {
|
|
119
|
+
value: requiredText(option.value),
|
|
120
|
+
...(label === undefined ? {} : { label })
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
if (options.length === 0)
|
|
124
|
+
throw new Error("empty options");
|
|
125
|
+
}
|
|
126
|
+
const description = text(field.description) ?? text(field.label);
|
|
127
|
+
const defaultValue = field.default_value;
|
|
128
|
+
const hasDefault = defaultValue !== undefined
|
|
129
|
+
&& defaultValue !== null
|
|
130
|
+
&& !(typeof defaultValue === "string" && defaultValue.trim() === "");
|
|
131
|
+
return {
|
|
132
|
+
name,
|
|
133
|
+
type,
|
|
134
|
+
required: field.required === true,
|
|
135
|
+
...(description === undefined ? {} : { description }),
|
|
136
|
+
...(hasDefault
|
|
137
|
+
? { default: defaultValue }
|
|
138
|
+
: {}),
|
|
139
|
+
...(options === undefined ? {} : { options })
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function range(values, unit) {
|
|
143
|
+
if (values.length === 0)
|
|
144
|
+
return undefined;
|
|
145
|
+
const bounds = { minimum: Math.min(...values), maximum: Math.max(...values) };
|
|
146
|
+
return unit === "gaodou" ? { unit, ...bounds } : { unit, ...bounds };
|
|
147
|
+
}
|
|
148
|
+
function normalizeModel(value, scenes) {
|
|
149
|
+
const item = record(value);
|
|
150
|
+
const model = record(item?.model);
|
|
151
|
+
if (item === undefined || model === undefined || !Array.isArray(item.skus)) {
|
|
152
|
+
throw new Error("invalid model");
|
|
153
|
+
}
|
|
154
|
+
const activeSkus = item.skus.map(record).filter((sku) => sku !== undefined && sku.status === 1);
|
|
155
|
+
if (activeSkus.length === 0)
|
|
156
|
+
return undefined;
|
|
157
|
+
const code = requiredText(model.code);
|
|
158
|
+
const title = requiredText(model.name);
|
|
159
|
+
if (!Array.isArray(model.scene_categories))
|
|
160
|
+
throw new Error("invalid categories");
|
|
161
|
+
const matches = model.scene_categories
|
|
162
|
+
.map((category) => scenes.get(requiredText(record(category)?.code)))
|
|
163
|
+
.filter((scene) => scene !== undefined)
|
|
164
|
+
.toSorted((left, right) => toolOrder[left.tool] - toolOrder[right.tool]
|
|
165
|
+
|| left.sort - right.sort);
|
|
166
|
+
const scene = matches[0];
|
|
167
|
+
if (scene === undefined)
|
|
168
|
+
throw new Error("unknown scene");
|
|
169
|
+
if (typeof model.input_field_settings !== "string")
|
|
170
|
+
throw new Error("missing settings");
|
|
171
|
+
const settings = JSON.parse(model.input_field_settings);
|
|
172
|
+
if (!Array.isArray(settings))
|
|
173
|
+
throw new Error("invalid settings");
|
|
174
|
+
const parameters = settings
|
|
175
|
+
.map(parameter)
|
|
176
|
+
.filter((item) => item !== undefined);
|
|
177
|
+
const description = cleanText(model.description);
|
|
178
|
+
const usageDescription = cleanText(model.tool_description);
|
|
179
|
+
const outputMediaTypes = Array.isArray(model.output_mime_types)
|
|
180
|
+
? model.output_mime_types.map(text).filter((item) => item !== undefined)
|
|
181
|
+
: [];
|
|
182
|
+
const costs = activeSkus
|
|
183
|
+
.map((sku) => sku.point_cost)
|
|
184
|
+
.filter((value) => typeof value === "number"
|
|
185
|
+
&& Number.isFinite(value) && value >= 0);
|
|
186
|
+
const times = activeSkus
|
|
187
|
+
.map((sku) => sku.estimated_time)
|
|
188
|
+
.filter((value) => typeof value === "number"
|
|
189
|
+
&& Number.isFinite(value) && value >= 0);
|
|
190
|
+
const cost = range(costs, "gaodou");
|
|
191
|
+
const estimatedTime = range(times, "second");
|
|
192
|
+
return {
|
|
193
|
+
tool: scene.tool,
|
|
194
|
+
model: code,
|
|
195
|
+
title,
|
|
196
|
+
...(description === undefined ? {} : { description }),
|
|
197
|
+
...(usageDescription === undefined ? {} : { usageDescription }),
|
|
198
|
+
...(outputMediaTypes.length === 0 ? {} : { outputMediaTypes }),
|
|
199
|
+
parameters,
|
|
200
|
+
...(cost?.unit === "gaodou" ? { cost } : {}),
|
|
201
|
+
...(estimatedTime?.unit === "second" ? { estimatedTime } : {}),
|
|
202
|
+
sceneSort: scene.sort,
|
|
203
|
+
modelSort: number(model.sort)
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export function createMnsCatalogAdapter(dependencies) {
|
|
207
|
+
return {
|
|
208
|
+
async load({ access, signal }) {
|
|
209
|
+
signal.throwIfAborted();
|
|
210
|
+
try {
|
|
211
|
+
const rawScenes = await dependencies.transport.getJson({
|
|
212
|
+
path: "/mns/scene-categories",
|
|
213
|
+
query: { codes: ROOT_SCENE_CODES.join(",") },
|
|
214
|
+
credential: access.credential,
|
|
215
|
+
organizationId: access.organizationId,
|
|
216
|
+
signal
|
|
217
|
+
});
|
|
218
|
+
if (!Array.isArray(rawScenes))
|
|
219
|
+
throw new Error("invalid scene response");
|
|
220
|
+
const scenes = collectScenes(rawScenes);
|
|
221
|
+
const rawModels = await dependencies.transport.getJson({
|
|
222
|
+
path: "/mns/models",
|
|
223
|
+
query: { codes: scenes.map((scene) => scene.code).join(",") },
|
|
224
|
+
credential: access.credential,
|
|
225
|
+
organizationId: access.organizationId,
|
|
226
|
+
signal
|
|
227
|
+
});
|
|
228
|
+
if (!Array.isArray(rawModels))
|
|
229
|
+
throw new Error("invalid model response");
|
|
230
|
+
const sceneMap = new Map(scenes.map((scene) => [scene.code, scene]));
|
|
231
|
+
const models = [];
|
|
232
|
+
const seen = new Set();
|
|
233
|
+
let invalidCount = 0;
|
|
234
|
+
for (const rawModel of rawModels) {
|
|
235
|
+
try {
|
|
236
|
+
const model = normalizeModel(rawModel, sceneMap);
|
|
237
|
+
if (model === undefined || seen.has(model.model))
|
|
238
|
+
continue;
|
|
239
|
+
seen.add(model.model);
|
|
240
|
+
models.push(model);
|
|
241
|
+
}
|
|
242
|
+
catch {
|
|
243
|
+
invalidCount += 1;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
models.sort((left, right) => toolOrder[left.tool] - toolOrder[right.tool]
|
|
247
|
+
|| left.sceneSort - right.sceneSort
|
|
248
|
+
|| left.modelSort - right.modelSort
|
|
249
|
+
|| (left.model < right.model ? -1 : left.model > right.model ? 1 : 0));
|
|
250
|
+
return {
|
|
251
|
+
models,
|
|
252
|
+
warnings: invalidCount === 0
|
|
253
|
+
? []
|
|
254
|
+
: [`已忽略 ${invalidCount} 个无效模型配置。`]
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
signal.throwIfAborted();
|
|
259
|
+
if (error instanceof RemoteRequestError)
|
|
260
|
+
throw error;
|
|
261
|
+
throw new RemoteRequestError();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
}
|