@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,162 @@
|
|
|
1
|
+
export class DeviceAuthorizationExpiredError extends Error {
|
|
2
|
+
constructor(commandName = "gd-cli") {
|
|
3
|
+
super(`Authorization expired. Run ${commandName} auth login again.`);
|
|
4
|
+
this.name = "DeviceAuthorizationExpiredError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class AuthorizedAccountContextMissingError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super("The authorized insMind account did not provide its account context.");
|
|
10
|
+
this.name = "AuthorizedAccountContextMissingError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function createAuthUseCases(dependencies) {
|
|
14
|
+
const insmind = dependencies.commandName === "insmind-cli";
|
|
15
|
+
return {
|
|
16
|
+
async status(input) {
|
|
17
|
+
const state = await dependencies.telemetry.stage("state", () => dependencies.store.read());
|
|
18
|
+
if (!state)
|
|
19
|
+
return {
|
|
20
|
+
logged_in: false,
|
|
21
|
+
next_steps: [`${dependencies.commandName ?? "gd-cli"} auth login`]
|
|
22
|
+
};
|
|
23
|
+
dependencies.telemetry.annotate({
|
|
24
|
+
account_id: state.account?.id,
|
|
25
|
+
organization_id: state.organization?.id
|
|
26
|
+
});
|
|
27
|
+
const valid = Date.parse(state.credential.expiresAt) > input.now.getTime();
|
|
28
|
+
return {
|
|
29
|
+
logged_in: true,
|
|
30
|
+
account: {
|
|
31
|
+
user_id: state.account?.id ?? null,
|
|
32
|
+
name: state.account?.name ?? null
|
|
33
|
+
},
|
|
34
|
+
organization: !dependencies.requireOrganization && state.organization
|
|
35
|
+
? { id: state.organization.id, name: state.organization.name }
|
|
36
|
+
: null,
|
|
37
|
+
credential: {
|
|
38
|
+
expires_at: state.credential.expiresAt,
|
|
39
|
+
valid,
|
|
40
|
+
scope: [...state.credential.scope]
|
|
41
|
+
},
|
|
42
|
+
next_steps: !valid
|
|
43
|
+
? [`${dependencies.commandName ?? "gd-cli"} auth logout`, `${dependencies.commandName ?? "gd-cli"} auth login`]
|
|
44
|
+
: dependencies.requireOrganization
|
|
45
|
+
? []
|
|
46
|
+
: state.organization
|
|
47
|
+
? []
|
|
48
|
+
: ["gd-cli org list", "gd-cli org switch"]
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
async logout() {
|
|
52
|
+
await dependencies.telemetry.stage("state", () => dependencies.store.remove());
|
|
53
|
+
},
|
|
54
|
+
async login(input) {
|
|
55
|
+
input.signal.throwIfAborted();
|
|
56
|
+
const existing = await dependencies.telemetry.stage("state", () => dependencies.store.read());
|
|
57
|
+
if (existing
|
|
58
|
+
&& Date.parse(existing.credential.expiresAt) > dependencies.now().getTime()
|
|
59
|
+
&& (!dependencies.requireOrganization || existing.organization !== undefined)) {
|
|
60
|
+
dependencies.telemetry.annotate({
|
|
61
|
+
account_id: existing.account?.id,
|
|
62
|
+
organization_id: existing.organization?.id
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
status: "already_logged_in",
|
|
66
|
+
organizationSelected: existing.organization !== undefined
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const authorization = await dependencies.telemetry.stage("authorize", async () => {
|
|
70
|
+
const authorization = await dependencies.sso.startDeviceAuthorization(input.signal);
|
|
71
|
+
const authorizationUrl = new URL("/cli/device-authorization", dependencies.authPageOrigin);
|
|
72
|
+
authorizationUrl.searchParams.set("code", authorization.authorizeCode);
|
|
73
|
+
await input.interaction.showAuthorization(authorizationUrl);
|
|
74
|
+
if (!input.noBrowser) {
|
|
75
|
+
try {
|
|
76
|
+
await input.interaction.openBrowser(authorizationUrl);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
input.interaction.warn(insmind
|
|
80
|
+
? "Could not open the browser. Open the authorization URL manually."
|
|
81
|
+
: "无法自动打开浏览器,请手动访问授权链接。");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return authorization;
|
|
85
|
+
});
|
|
86
|
+
const deadline = dependencies.now().getTime() + authorization.expiresInSeconds * 1000;
|
|
87
|
+
let interval = authorization.intervalSeconds * 1000;
|
|
88
|
+
const token = await dependencies.telemetry.stage("wait", async () => {
|
|
89
|
+
while (true) {
|
|
90
|
+
if (dependencies.now().getTime() + interval > deadline) {
|
|
91
|
+
throw new DeviceAuthorizationExpiredError(dependencies.commandName);
|
|
92
|
+
}
|
|
93
|
+
await dependencies.sleep(interval, input.signal);
|
|
94
|
+
input.signal.throwIfAborted();
|
|
95
|
+
const result = await dependencies.sso.pollDeviceToken(authorization.deviceCode, input.signal);
|
|
96
|
+
if (result.status === "pending")
|
|
97
|
+
continue;
|
|
98
|
+
if (result.status === "slow_down") {
|
|
99
|
+
interval += 5_000;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
let state = {
|
|
106
|
+
version: 1,
|
|
107
|
+
credential: token.credential,
|
|
108
|
+
...(token.account ? { account: token.account } : {})
|
|
109
|
+
};
|
|
110
|
+
if (!dependencies.requireOrganization) {
|
|
111
|
+
await dependencies.telemetry.stage("state", () => dependencies.store.write(state));
|
|
112
|
+
}
|
|
113
|
+
dependencies.telemetry.annotate({ account_id: state.account?.id });
|
|
114
|
+
let organizationId = token.organizationId;
|
|
115
|
+
try {
|
|
116
|
+
const identity = await dependencies.telemetry.stage("identity", () => (dependencies.sso.getIdentity(token.credential.accessKey, input.signal)));
|
|
117
|
+
organizationId = identity.organizationId ?? organizationId;
|
|
118
|
+
if (!state.account && identity.account) {
|
|
119
|
+
state = { ...state, account: identity.account };
|
|
120
|
+
if (!dependencies.requireOrganization) {
|
|
121
|
+
await dependencies.telemetry.stage("state", () => dependencies.store.write(state));
|
|
122
|
+
}
|
|
123
|
+
dependencies.telemetry.annotate({ account_id: identity.account.id });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
if (input.signal.aborted)
|
|
128
|
+
throw input.signal.reason;
|
|
129
|
+
}
|
|
130
|
+
if (!organizationId) {
|
|
131
|
+
if (dependencies.requireOrganization)
|
|
132
|
+
throw new AuthorizedAccountContextMissingError();
|
|
133
|
+
input.interaction.warn("登录成功,请执行 gd-cli org switch 选择组织。");
|
|
134
|
+
return { status: "logged_in", organizationSelected: false };
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const organization = await dependencies.telemetry.stage("bind", () => (dependencies.organizationBinder.bindAuthorizedOrganization({
|
|
138
|
+
credential: token.credential,
|
|
139
|
+
organizationId,
|
|
140
|
+
signal: input.signal
|
|
141
|
+
})));
|
|
142
|
+
if (!organization) {
|
|
143
|
+
if (dependencies.requireOrganization)
|
|
144
|
+
throw new AuthorizedAccountContextMissingError();
|
|
145
|
+
input.interaction.warn("登录成功,但未能选择组织;请执行 gd-cli org switch。");
|
|
146
|
+
return { status: "logged_in", organizationSelected: false };
|
|
147
|
+
}
|
|
148
|
+
await dependencies.telemetry.stage("state", () => dependencies.store.write({ ...state, organization }));
|
|
149
|
+
dependencies.telemetry.annotate({ organization_id: organization.id });
|
|
150
|
+
return { status: "logged_in", organizationSelected: true };
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
if (input.signal.aborted)
|
|
154
|
+
throw input.signal.reason;
|
|
155
|
+
if (dependencies.requireOrganization)
|
|
156
|
+
throw new AuthorizedAccountContextMissingError();
|
|
157
|
+
input.interaction.warn("登录成功,但组织绑定失败;请执行 gd-cli org switch。");
|
|
158
|
+
return { status: "logged_in", organizationSelected: false };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { RemoteProtocolError } from "../../platform/remote-protocol.js";
|
|
2
|
+
export function createCreditsUseCases(options) {
|
|
3
|
+
return {
|
|
4
|
+
async get(input) {
|
|
5
|
+
const body = await options.transport.getJson({
|
|
6
|
+
path: "/trade/rights-overview",
|
|
7
|
+
query: { goods_type_enums: "GAODOU" },
|
|
8
|
+
credential: input.credential,
|
|
9
|
+
organizationId: input.organizationId,
|
|
10
|
+
signal: input.signal
|
|
11
|
+
});
|
|
12
|
+
const source = findCreditsRecord(body);
|
|
13
|
+
const total = number(source.total);
|
|
14
|
+
const used = number(source.used);
|
|
15
|
+
const pending = number(source.wait);
|
|
16
|
+
if (total === undefined || used === undefined || pending === undefined) {
|
|
17
|
+
throw new RemoteProtocolError();
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
credits: {
|
|
21
|
+
unit: options.billingUnit,
|
|
22
|
+
total,
|
|
23
|
+
used,
|
|
24
|
+
pending,
|
|
25
|
+
available: Math.max(0, total - used)
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function findCreditsRecord(value) {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
const record = value.find((candidate) => (isRecord(candidate) && candidate.goods_type_enum === "GAODOU"));
|
|
34
|
+
if (!record)
|
|
35
|
+
throw new RemoteProtocolError();
|
|
36
|
+
return record;
|
|
37
|
+
}
|
|
38
|
+
if (!isRecord(value))
|
|
39
|
+
throw new RemoteProtocolError();
|
|
40
|
+
const candidates = [value.GAODOU, value.data, isRecord(value.data) ? value.data.GAODOU : undefined];
|
|
41
|
+
const record = candidates.find(isRecord);
|
|
42
|
+
if (!record)
|
|
43
|
+
throw new RemoteProtocolError();
|
|
44
|
+
return record;
|
|
45
|
+
}
|
|
46
|
+
function number(value) {
|
|
47
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
48
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
49
|
+
}
|
|
50
|
+
function isRecord(value) {
|
|
51
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
52
|
+
}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { isSensitiveKey } from "../../platform/redact.js";
|
|
2
|
+
import { parseSafeRemoteAssetUrl } from "../../platform/url-safety.js";
|
|
3
|
+
const publicAssetTypes = new Set([
|
|
4
|
+
"image",
|
|
5
|
+
"video",
|
|
6
|
+
"audio",
|
|
7
|
+
"font",
|
|
8
|
+
"template",
|
|
9
|
+
"file"
|
|
10
|
+
]);
|
|
11
|
+
const internalAssetTypes = new Map([
|
|
12
|
+
["gdpic", "image"],
|
|
13
|
+
["gdimage", "image"],
|
|
14
|
+
["gdvideo", "video"],
|
|
15
|
+
["gdaudio", "audio"],
|
|
16
|
+
["gdfont", "font"],
|
|
17
|
+
["gdtemplate", "template"],
|
|
18
|
+
["gdfile", "file"],
|
|
19
|
+
["gdslide", "file"]
|
|
20
|
+
]);
|
|
21
|
+
const recognizableAssetKeys = new Set([
|
|
22
|
+
"asset_id",
|
|
23
|
+
"id",
|
|
24
|
+
"content_id",
|
|
25
|
+
"title",
|
|
26
|
+
"name",
|
|
27
|
+
"description",
|
|
28
|
+
"format",
|
|
29
|
+
"type",
|
|
30
|
+
"content_format",
|
|
31
|
+
"storage_format",
|
|
32
|
+
"origin_info",
|
|
33
|
+
"originInfo",
|
|
34
|
+
"origin_url",
|
|
35
|
+
"url",
|
|
36
|
+
"preview",
|
|
37
|
+
"preview_info",
|
|
38
|
+
"previewInfo",
|
|
39
|
+
"cover",
|
|
40
|
+
"cover_url",
|
|
41
|
+
"analysis_result",
|
|
42
|
+
"analysis_status"
|
|
43
|
+
]);
|
|
44
|
+
function record(value) {
|
|
45
|
+
return value !== null && typeof value === "object" && !Array.isArray(value)
|
|
46
|
+
? value
|
|
47
|
+
: undefined;
|
|
48
|
+
}
|
|
49
|
+
function nonblank(value) {
|
|
50
|
+
return typeof value === "string" && value.trim() !== "" ? value : undefined;
|
|
51
|
+
}
|
|
52
|
+
function publicSourceFormat(value) {
|
|
53
|
+
const format = nonblank(value);
|
|
54
|
+
return format !== undefined && !internalAssetTypes.has(format.toLowerCase())
|
|
55
|
+
? format
|
|
56
|
+
: undefined;
|
|
57
|
+
}
|
|
58
|
+
function finiteNumber(value) {
|
|
59
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
60
|
+
}
|
|
61
|
+
function findAssetRecord(value) {
|
|
62
|
+
const queue = [value];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
while (queue.length > 0) {
|
|
65
|
+
const candidate = queue.shift();
|
|
66
|
+
if (Array.isArray(candidate)) {
|
|
67
|
+
queue.push(...candidate);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const item = record(candidate);
|
|
71
|
+
if (item === undefined || seen.has(item))
|
|
72
|
+
continue;
|
|
73
|
+
seen.add(item);
|
|
74
|
+
if (Object.keys(item).some((key) => recognizableAssetKeys.has(key)))
|
|
75
|
+
return item;
|
|
76
|
+
for (const key of ["data", "asset", "asset_info", "item", "result"]) {
|
|
77
|
+
if (item[key] !== undefined)
|
|
78
|
+
queue.push(item[key]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
function safePublicUrl(value) {
|
|
84
|
+
const raw = nonblank(value);
|
|
85
|
+
if (raw === undefined)
|
|
86
|
+
return undefined;
|
|
87
|
+
try {
|
|
88
|
+
const url = parseSafeRemoteAssetUrl(raw);
|
|
89
|
+
for (const key of [...url.searchParams.keys()]) {
|
|
90
|
+
if (isSensitiveKey(key))
|
|
91
|
+
url.searchParams.delete(key);
|
|
92
|
+
}
|
|
93
|
+
return url.href;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function timestamp(value) {
|
|
100
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
101
|
+
const milliseconds = Math.abs(value) < 1_000_000_000_000 ? value * 1000 : value;
|
|
102
|
+
const date = new Date(milliseconds);
|
|
103
|
+
return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
|
|
104
|
+
}
|
|
105
|
+
const text = nonblank(value);
|
|
106
|
+
if (text === undefined)
|
|
107
|
+
return undefined;
|
|
108
|
+
const date = new Date(text);
|
|
109
|
+
return Number.isNaN(date.getTime()) ? undefined : date.toISOString();
|
|
110
|
+
}
|
|
111
|
+
function tags(value) {
|
|
112
|
+
if (!Array.isArray(value))
|
|
113
|
+
return undefined;
|
|
114
|
+
const result = value
|
|
115
|
+
.map((item) => typeof item === "string" ? nonblank(item) : nonblank(record(item)?.name))
|
|
116
|
+
.filter((item) => item !== undefined);
|
|
117
|
+
return result.length === 0 ? undefined : result;
|
|
118
|
+
}
|
|
119
|
+
function tagIds(tagIdsValue, tagsValue) {
|
|
120
|
+
const candidates = Array.isArray(tagIdsValue)
|
|
121
|
+
? tagIdsValue
|
|
122
|
+
: Array.isArray(tagsValue)
|
|
123
|
+
? tagsValue.map((item) => record(item)?.id)
|
|
124
|
+
: [];
|
|
125
|
+
const result = candidates.filter((item) => (typeof item === "string" && item.trim() !== "")
|
|
126
|
+
|| (typeof item === "number" && Number.isFinite(item)));
|
|
127
|
+
return result.length === 0 ? undefined : result;
|
|
128
|
+
}
|
|
129
|
+
function assetType(item, format, mediaType) {
|
|
130
|
+
const explicit = nonblank(item.type)?.toLowerCase();
|
|
131
|
+
if (explicit !== undefined && publicAssetTypes.has(explicit))
|
|
132
|
+
return explicit;
|
|
133
|
+
for (const candidate of [item.content_format, item.storage_format, item.format]) {
|
|
134
|
+
const internalType = internalAssetTypes.get(nonblank(candidate)?.toLowerCase() ?? "");
|
|
135
|
+
if (internalType !== undefined)
|
|
136
|
+
return internalType;
|
|
137
|
+
}
|
|
138
|
+
const mediaCategory = mediaType?.split("/", 1)[0]?.toLowerCase();
|
|
139
|
+
if (mediaCategory !== undefined && publicAssetTypes.has(mediaCategory))
|
|
140
|
+
return mediaCategory;
|
|
141
|
+
const normalizedFormat = format?.toLowerCase();
|
|
142
|
+
if (normalizedFormat !== undefined
|
|
143
|
+
&& ["png", "jpg", "jpeg", "gif", "webp", "svg", "avif", "heic"].includes(normalizedFormat)) {
|
|
144
|
+
return "image";
|
|
145
|
+
}
|
|
146
|
+
if (normalizedFormat !== undefined
|
|
147
|
+
&& ["mp4", "mov", "webm", "mkv"].includes(normalizedFormat))
|
|
148
|
+
return "video";
|
|
149
|
+
if (normalizedFormat !== undefined
|
|
150
|
+
&& ["mp3", "wav", "m4a", "aac", "ogg", "flac"].includes(normalizedFormat)) {
|
|
151
|
+
return "audio";
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
function compact(value) {
|
|
156
|
+
return Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined));
|
|
157
|
+
}
|
|
158
|
+
function normalizeAsset(value, fallback) {
|
|
159
|
+
const item = findAssetRecord(value);
|
|
160
|
+
if (item === undefined)
|
|
161
|
+
return undefined;
|
|
162
|
+
const fallbackAsset = typeof fallback === "object" ? fallback : undefined;
|
|
163
|
+
const origin = record(item.origin_info) ?? record(item.originInfo);
|
|
164
|
+
const design = record(item.design_info) ?? record(item.designInfo);
|
|
165
|
+
const preview = record(item.preview)
|
|
166
|
+
?? record(item.preview_info)
|
|
167
|
+
?? record(item.previewInfo)
|
|
168
|
+
?? record(item.cover);
|
|
169
|
+
const repository = record(item.repository_info) ?? record(item.repositoryInfo);
|
|
170
|
+
const folder = record(item.folder_info) ?? record(item.folderInfo);
|
|
171
|
+
const format = publicSourceFormat(origin?.format) ?? publicSourceFormat(item.format);
|
|
172
|
+
const mediaType = nonblank(origin?.mime_type)
|
|
173
|
+
?? nonblank(item.mime_type)
|
|
174
|
+
?? nonblank(item.mimeType);
|
|
175
|
+
const assetId = nonblank(item.asset_id)
|
|
176
|
+
?? nonblank(item.id)
|
|
177
|
+
?? nonblank(item.content_id)
|
|
178
|
+
?? (typeof fallback === "string" ? fallback : fallbackAsset?.asset_id);
|
|
179
|
+
if (assetId === undefined)
|
|
180
|
+
return undefined;
|
|
181
|
+
return compact({
|
|
182
|
+
...fallbackAsset,
|
|
183
|
+
asset_id: assetId,
|
|
184
|
+
title: nonblank(item.title) ?? nonblank(item.name) ?? fallbackAsset?.title,
|
|
185
|
+
description: nonblank(item.description) ?? fallbackAsset?.description,
|
|
186
|
+
format: format ?? fallbackAsset?.format,
|
|
187
|
+
type: assetType(item, format, mediaType) ?? fallbackAsset?.type,
|
|
188
|
+
size: finiteNumber(origin?.size) ?? finiteNumber(item.size) ?? fallbackAsset?.size,
|
|
189
|
+
url: safePublicUrl(origin?.url ?? item.origin_url ?? item.url) ?? fallbackAsset?.url,
|
|
190
|
+
cover_url: safePublicUrl(preview?.url ?? item.cover_url ?? item.preview_url) ?? fallbackAsset?.cover_url,
|
|
191
|
+
width: finiteNumber(design?.width)
|
|
192
|
+
?? finiteNumber(preview?.width)
|
|
193
|
+
?? finiteNumber(item.width)
|
|
194
|
+
?? fallbackAsset?.width,
|
|
195
|
+
height: finiteNumber(design?.height)
|
|
196
|
+
?? finiteNumber(preview?.height)
|
|
197
|
+
?? finiteNumber(item.height)
|
|
198
|
+
?? fallbackAsset?.height,
|
|
199
|
+
duration: finiteNumber(design?.duration)
|
|
200
|
+
?? finiteNumber(preview?.duration)
|
|
201
|
+
?? finiteNumber(item.duration)
|
|
202
|
+
?? fallbackAsset?.duration,
|
|
203
|
+
repository_id: nonblank(item.repository_id)
|
|
204
|
+
?? nonblank(repository?.id)
|
|
205
|
+
?? nonblank(repository?.repository_id)
|
|
206
|
+
?? fallbackAsset?.repository_id,
|
|
207
|
+
repository_name: nonblank(item.repository_name)
|
|
208
|
+
?? nonblank(repository?.name)
|
|
209
|
+
?? nonblank(repository?.title)
|
|
210
|
+
?? fallbackAsset?.repository_name,
|
|
211
|
+
folder_id: nonblank(item.folder_id)
|
|
212
|
+
?? nonblank(folder?.id)
|
|
213
|
+
?? fallbackAsset?.folder_id,
|
|
214
|
+
folder_name: nonblank(item.folder_name)
|
|
215
|
+
?? nonblank(folder?.name)
|
|
216
|
+
?? nonblank(folder?.title)
|
|
217
|
+
?? fallbackAsset?.folder_name,
|
|
218
|
+
tags: tags(item.tags),
|
|
219
|
+
tag_ids: tagIds(item.tag_ids, item.tags) ?? fallbackAsset?.tag_ids,
|
|
220
|
+
analysis_status: finiteNumber(item.analysis_result)
|
|
221
|
+
?? finiteNumber(item.analysis_status)
|
|
222
|
+
?? nonblank(item.analysis_result)
|
|
223
|
+
?? nonblank(item.analysis_status)
|
|
224
|
+
?? fallbackAsset?.analysis_status,
|
|
225
|
+
created_at: timestamp(item.created_at) ?? fallbackAsset?.created_at,
|
|
226
|
+
updated_at: timestamp(item.updated_at) ?? fallbackAsset?.updated_at
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
export function projectDamAssetDetail(value, fallback) {
|
|
230
|
+
const asset = normalizeAsset(value, fallback);
|
|
231
|
+
if (asset === undefined)
|
|
232
|
+
return undefined;
|
|
233
|
+
return compact({
|
|
234
|
+
asset_id: asset.asset_id,
|
|
235
|
+
title: asset.title,
|
|
236
|
+
description: asset.description,
|
|
237
|
+
format: asset.format,
|
|
238
|
+
type: asset.type,
|
|
239
|
+
size: asset.size,
|
|
240
|
+
url: asset.url,
|
|
241
|
+
cover_url: asset.cover_url,
|
|
242
|
+
width: asset.width,
|
|
243
|
+
height: asset.height,
|
|
244
|
+
duration: asset.duration,
|
|
245
|
+
repository_id: asset.repository_id,
|
|
246
|
+
repository_name: asset.repository_name,
|
|
247
|
+
folder_id: asset.folder_id,
|
|
248
|
+
folder_name: asset.folder_name,
|
|
249
|
+
tag_ids: asset.tag_ids,
|
|
250
|
+
analysis_status: asset.analysis_status,
|
|
251
|
+
created_at: asset.created_at,
|
|
252
|
+
updated_at: asset.updated_at
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
export function projectDamAssetSummary(value) {
|
|
256
|
+
const asset = normalizeAsset(value);
|
|
257
|
+
if (asset === undefined)
|
|
258
|
+
return undefined;
|
|
259
|
+
return compact({
|
|
260
|
+
asset_id: asset.asset_id,
|
|
261
|
+
title: asset.title,
|
|
262
|
+
format: asset.format,
|
|
263
|
+
type: asset.type,
|
|
264
|
+
size: asset.size,
|
|
265
|
+
url: asset.url,
|
|
266
|
+
cover_url: asset.cover_url,
|
|
267
|
+
width: asset.width,
|
|
268
|
+
height: asset.height,
|
|
269
|
+
repository_id: asset.repository_id,
|
|
270
|
+
folder_id: asset.folder_id,
|
|
271
|
+
folder_name: asset.folder_name,
|
|
272
|
+
tags: asset.tags,
|
|
273
|
+
tag_ids: asset.tag_ids,
|
|
274
|
+
analysis_status: asset.analysis_status,
|
|
275
|
+
created_at: asset.created_at,
|
|
276
|
+
updated_at: asset.updated_at
|
|
277
|
+
});
|
|
278
|
+
}
|