@arcticdata/dsh-datarc-skills 0.0.0-stage → 26.39.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.
Files changed (135) hide show
  1. package/README.md +20 -2
  2. package/cordis.patch.yml +4 -0
  3. package/icon.svg +4 -0
  4. package/index.js +35 -0
  5. package/locale/en.json +6 -0
  6. package/locale/zh.json +6 -0
  7. package/package.json +37 -4
  8. package/skills/datarc-access-permissions/SKILL.md +30 -0
  9. package/skills/datarc-access-permissions/references/api.md +31 -0
  10. package/skills/datarc-access-permissions/references/write-order.md +16 -0
  11. package/skills/datarc-analysis-report/SKILL.md +125 -0
  12. package/skills/datarc-analysis-report/agents/openai.yaml +6 -0
  13. package/skills/datarc-analysis-report/references/analyst-frameworks.md +215 -0
  14. package/skills/datarc-analysis-report/references/api-contracts.md +478 -0
  15. package/skills/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
  16. package/skills/datarc-analysis-report/references/human-in-the-loop.md +142 -0
  17. package/skills/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
  18. package/skills/datarc-analysis-report/references/manifest-example.json +779 -0
  19. package/skills/datarc-analysis-report/references/narrative-schema.md +160 -0
  20. package/skills/datarc-analysis-report/references/question-syntax.md +151 -0
  21. package/skills/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
  22. package/skills/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
  23. package/skills/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
  24. package/skills/datarc-data-search/SKILL.md +243 -0
  25. package/skills/datarc-data-search/scripts/calculate.py +116 -0
  26. package/skills/datarc-data-search/scripts/data-search.mjs +628 -0
  27. package/skills/datarc-data-search/scripts/datarc-client.mjs +464 -0
  28. package/skills/datarc-data-search/scripts/search-result.mjs +211 -0
  29. package/skills/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
  30. package/skills/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
  31. package/skills/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
  32. package/skills/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
  33. package/skills/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
  34. package/skills/datarc-dataseek-search/SKILL.md +96 -0
  35. package/skills/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
  36. package/skills/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
  37. package/skills/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
  38. package/skills/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
  39. package/skills/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
  40. package/skills/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
  41. package/skills/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
  42. package/skills/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
  43. package/skills/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
  44. package/skills/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
  45. package/skills/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
  46. package/skills/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
  47. package/skills/datarc-dataseek-testset-generation/SKILL.md +47 -0
  48. package/skills/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
  49. package/skills/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
  50. package/skills/datarc-datasource/SKILL.md +97 -0
  51. package/skills/datarc-oauth/SKILL.md +47 -0
  52. package/skills/datarc-oauth/scripts/auth.mjs +130 -0
  53. package/skills/datarc-oauth/scripts/credentials.mjs +237 -0
  54. package/skills/datarc-report-add-card/SKILL.md +90 -0
  55. package/skills/datarc-report-add-card/agents/openai.yaml +4 -0
  56. package/skills/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
  57. package/skills/datarc-report-add-card/scripts/report-card.mjs +439 -0
  58. package/skills/datarc-semantic-alias/SKILL.md +20 -0
  59. package/skills/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
  60. package/skills/datarc-semantic-alias/scripts/alias.mjs +11 -0
  61. package/skills/datarc-semantic-calibration/SKILL.md +20 -0
  62. package/skills/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
  63. package/skills/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
  64. package/skills/datarc-semantic-core/SKILL.md +12 -0
  65. package/skills/datarc-semantic-core/scripts/artifact-journal.mjs +830 -0
  66. package/skills/datarc-semantic-core/scripts/canonical.mjs +48 -0
  67. package/skills/datarc-semantic-core/scripts/case-model.mjs +608 -0
  68. package/skills/datarc-semantic-core/scripts/cli.mjs +180 -0
  69. package/skills/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
  70. package/skills/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
  71. package/skills/datarc-semantic-core/scripts/interventions.mjs +693 -0
  72. package/skills/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
  73. package/skills/datarc-semantic-core/scripts/model-scope.mjs +49 -0
  74. package/skills/datarc-semantic-core/scripts/reproduction.mjs +330 -0
  75. package/skills/datarc-semantic-core/scripts/semantic-cli.mjs +740 -0
  76. package/skills/datarc-semantic-core/scripts/shared.mjs +312 -0
  77. package/skills/datarc-semantic-core/scripts/verification.mjs +1014 -0
  78. package/skills/datarc-semantic-dataset/SKILL.md +20 -0
  79. package/skills/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
  80. package/skills/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
  81. package/skills/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
  82. package/skills/datarc-semantic-knowledge/SKILL.md +38 -0
  83. package/skills/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
  84. package/skills/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
  85. package/skills/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
  86. package/skills/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
  87. package/skills/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
  88. package/skills/datarc-semantic-prompt/SKILL.md +20 -0
  89. package/skills/datarc-semantic-prompt/scripts/prompt-plan.mjs +447 -0
  90. package/skills/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
  91. package/skills/datarc-semantic-synonym/SKILL.md +20 -0
  92. package/skills/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
  93. package/skills/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
  94. package/skills/datarc-viz-analysis-report/SKILL.md +108 -0
  95. package/skills/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
  96. package/skills/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
  97. package/skills/datarc-viz-analysis-report/references/api-contracts.md +163 -0
  98. package/skills/datarc-viz-analysis-report/references/derivation-log.md +157 -0
  99. package/skills/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
  100. package/skills/datarc-viz-analysis-report/references/html-template.md +92 -0
  101. package/skills/datarc-viz-analysis-report/references/provenance-format.md +56 -0
  102. package/skills/datarc-viz-analysis-report/references/question-syntax.md +151 -0
  103. package/skills/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
  104. package/skills/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
  105. package/skills/datarc-workspace-configuration/SKILL.md +59 -0
  106. package/skills/datarc-workspace-configuration/references/artifacts.md +75 -0
  107. package/skills/datarc-workspace-configuration/references/verification.md +20 -0
  108. package/skills/datarc-workspace-configuration/scripts/configuration.mjs +662 -0
  109. package/skills/datarc-workspace-context/SKILL.md +43 -0
  110. package/skills/datarc-workspace-context/agents/openai.yaml +4 -0
  111. package/skills/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
  112. package/skills/datarc-workspace-context/scripts/query-display.mjs +114 -0
  113. package/skills/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
  114. package/skills/datarc-workspace-custom-tables/SKILL.md +89 -0
  115. package/skills/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
  116. package/skills/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
  117. package/skills/datarc-workspace-keywords/SKILL.md +197 -0
  118. package/skills/datarc-workspace-keywords/agents/openai.yaml +4 -0
  119. package/skills/datarc-workspace-keywords/references/api-fields.md +79 -0
  120. package/skills/datarc-workspace-keywords/references/derived-measures.md +177 -0
  121. package/skills/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
  122. package/skills/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
  123. package/skills/datarc-workspace-membership/SKILL.md +29 -0
  124. package/skills/datarc-workspace-membership/references/api.md +19 -0
  125. package/skills/datarc-workspace-modeling/SKILL.md +209 -0
  126. package/skills/datarc-workspace-modeling/agents/openai.yaml +4 -0
  127. package/skills/datarc-workspace-modeling/references/join-preview.md +38 -0
  128. package/skills/datarc-workspace-modeling/references/model-plan.md +300 -0
  129. package/skills/datarc-workspace-modeling/references/model-tags.md +95 -0
  130. package/skills/datarc-workspace-modeling/references/modeling-contract.json +29 -0
  131. package/skills/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
  132. package/skills/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
  133. package/skills/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
  134. package/skills/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
  135. package/skills-index.json +194 -0
@@ -0,0 +1,137 @@
1
+ import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
2
+ import { readFile } from "node:fs/promises";
3
+
4
+ export class DatarcError extends Error {
5
+ constructor(message, code = "DATARC_ERROR", details) {
6
+ super(message);
7
+ this.name = "DatarcError";
8
+ this.code = code;
9
+ this.details = details;
10
+ }
11
+ }
12
+
13
+ export function parseArgs(argv) {
14
+ const result = { _: [] };
15
+ for (let index = 0; index < argv.length; index += 1) {
16
+ const value = argv[index];
17
+ if (!value.startsWith("--")) {
18
+ result._.push(value);
19
+ continue;
20
+ }
21
+ const key = value.slice(2);
22
+ const next = argv[index + 1];
23
+ if (next === undefined || next.startsWith("--")) result[key] = true;
24
+ else {
25
+ result[key] =
26
+ result[key] === undefined ? next : [].concat(result[key], next);
27
+ index += 1;
28
+ }
29
+ }
30
+ return result;
31
+ }
32
+
33
+ export function required(args, key) {
34
+ if (args[key] === undefined || args[key] === true || args[key] === "")
35
+ throw new DatarcError(`缺少参数 --${key}`, "INVALID_ARGUMENT");
36
+ return args[key];
37
+ }
38
+
39
+ export async function readJson(path) {
40
+ const text = path === "-" ? await readStdin() : await readFile(path, "utf8");
41
+ try {
42
+ return JSON.parse(text);
43
+ } catch {
44
+ throw new DatarcError("输入不是有效 JSON", "INVALID_JSON");
45
+ }
46
+ }
47
+
48
+ export function createClient(options = {}) {
49
+ const { endpoint, token } = resolveCredentials(options);
50
+ const fetchImpl = options.fetchImpl ?? globalThis.fetch;
51
+ if (!endpoint)
52
+ throw new DatarcError("未配置 DATARC_ENDPOINT", "MISSING_ENDPOINT");
53
+ if (!token) throw new DatarcError("未配置 DATARC_TOKEN", "MISSING_TOKEN");
54
+ async function request(method, path, { query, body } = {}) {
55
+ const url = new URL(`${endpoint}${path}`);
56
+ for (const [key, value] of Object.entries(query ?? {}))
57
+ for (const item of [].concat(value ?? []))
58
+ if (item !== "") url.searchParams.append(key, item);
59
+ let response;
60
+ try {
61
+ response = await fetchImpl(url, {
62
+ method,
63
+ headers: {
64
+ Authorization: token,
65
+ ...(body === undefined ? {} : { "Content-Type": "application/json" }),
66
+ },
67
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
68
+ });
69
+ } catch (error) {
70
+ throw new DatarcError(`网络请求失败:${error.message}`, "NETWORK_ERROR");
71
+ }
72
+ const text = await response.text();
73
+ let payload;
74
+ try {
75
+ payload = text ? JSON.parse(text) : {};
76
+ } catch {
77
+ throw new DatarcError(
78
+ `服务返回非 JSON(HTTP ${response.status})`,
79
+ "INVALID_RESPONSE",
80
+ );
81
+ }
82
+ if (!response.ok || (payload.status && payload.status !== "SUCCESS"))
83
+ throw new DatarcError(
84
+ payload.message || `请求失败(HTTP ${response.status})`,
85
+ "API_ERROR",
86
+ {
87
+ httpStatus: response.status,
88
+ status: payload.status,
89
+ data: payload.data,
90
+ },
91
+ );
92
+ return payload.data;
93
+ }
94
+ return { request };
95
+ }
96
+
97
+ export function encryptSql(value) {
98
+ const first = Buffer.from(String(value), "utf8").toString("base64url");
99
+ const second = Buffer.from([...first].reverse().join(""), "utf8").toString(
100
+ "base64url",
101
+ );
102
+ return [...second].reverse().join("");
103
+ }
104
+
105
+ export function decryptSql(value) {
106
+ const second = [...String(value)].reverse().join("");
107
+ const firstReversed = Buffer.from(second, "base64url").toString("utf8");
108
+ const first = [...firstReversed].reverse().join("");
109
+ return Buffer.from(first, "base64url").toString("utf8");
110
+ }
111
+
112
+ export function errorJson(error) {
113
+ return {
114
+ code: error?.code ?? "UNEXPECTED_ERROR",
115
+ message: error?.message ?? String(error),
116
+ ...(error?.details ? { details: error.details } : {}),
117
+ };
118
+ }
119
+
120
+ export async function runCli(main) {
121
+ try {
122
+ process.stdout.write(
123
+ `${JSON.stringify({ ok: true, data: await main() }, null, 2)}\n`,
124
+ );
125
+ } catch (error) {
126
+ process.stdout.write(
127
+ `${JSON.stringify({ ok: false, error: errorJson(error) }, null, 2)}\n`,
128
+ );
129
+ process.exitCode = 1;
130
+ }
131
+ }
132
+
133
+ async function readStdin() {
134
+ const chunks = [];
135
+ for await (const chunk of process.stdin) chunks.push(chunk);
136
+ return Buffer.concat(chunks).toString("utf8");
137
+ }
@@ -0,0 +1,238 @@
1
+ import { DatarcError, readJson, required } from "./datarc-client.mjs";
2
+
3
+ export const MODEL_TAG_COMMANDS = [
4
+ "tags",
5
+ "create-tag",
6
+ "update-tag",
7
+ "delete-tag",
8
+ "tag-models",
9
+ ];
10
+
11
+ // Kept behind explicit CLI commands so ordinary modeling never reads or writes the tag directory.
12
+ export async function executeModelTags(args, client) {
13
+ const command = args._[0];
14
+ const allowed = new Set([
15
+ "_",
16
+ "workspace-id",
17
+ ...(command === "tags" ? [] : ["revision", "dry-run"]),
18
+ ...(["update-tag", "delete-tag", "tag-models"].includes(command)
19
+ ? ["tag-id"]
20
+ : []),
21
+ ...(command === "delete-tag"
22
+ ? ["confirm-delete"]
23
+ : command === "tags"
24
+ ? []
25
+ : ["input"]),
26
+ ...(command === "tag-models" ? ["confirm-replace"] : []),
27
+ ]);
28
+ if (args._.length !== 1 || Object.keys(args).some((key) => !allowed.has(key)))
29
+ fail("存在未知参数,请按标签操作文档重新输入");
30
+ for (const key of ["dry-run", "confirm-delete", "confirm-replace"])
31
+ if (args[key] !== undefined && args[key] !== true)
32
+ fail(`--${key} 不接受值,请使用独立开关`);
33
+ const workspaceId = required(args, "workspace-id");
34
+ if (typeof workspaceId !== "string" || !/^[1-9]\d*$/.test(workspaceId))
35
+ fail("工作区编号无效,请从可访问工作区列表重新选择");
36
+ const path = `/api/v2/admin-projects/${workspaceId}/model-tags/`;
37
+ if (command === "tags") return readSnapshot(client, path);
38
+ const revision = required(args, "revision");
39
+ if (typeof revision !== "string")
40
+ fail("revision 必须是单个版本号,请重新读取标签目录");
41
+ const snapshot = await readSnapshot(client, path);
42
+ if (snapshot.revision !== revision)
43
+ throw new DatarcError(
44
+ "标签或关联已变化,请重新读取目录并核对差异后再执行",
45
+ "MODEL_TAGS_CONFLICT",
46
+ );
47
+ let tag;
48
+ if (command !== "create-tag") {
49
+ const uid = required(args, "tag-id");
50
+ tag = snapshot.tags.find((item) => item.uid === uid);
51
+ if (!tag) fail("标签不存在,请重新读取目录并选择唯一标签");
52
+ }
53
+ let next;
54
+ if (command !== "delete-tag") {
55
+ const input = await readJson(required(args, "input"));
56
+ const keys =
57
+ command === "tag-models"
58
+ ? ["operation", "modelIds"]
59
+ : command === "create-tag"
60
+ ? ["name", "description", "modelIds"]
61
+ : ["name", "description"];
62
+ if (
63
+ !input ||
64
+ Array.isArray(input) ||
65
+ typeof input !== "object" ||
66
+ Object.keys(input).some((key) => !keys.includes(key))
67
+ )
68
+ fail(
69
+ "标签输入字段无效,请按文档填写;修改说明请显式传空字符串,调整关联请用 tag-models",
70
+ );
71
+ next = {
72
+ name: tag?.name,
73
+ description: tag?.description ?? "",
74
+ modelIds: tag?.modelIds ?? [],
75
+ ...input,
76
+ };
77
+ if (command === "tag-models") {
78
+ const requested = validateModelIds(input.modelIds, snapshot);
79
+ if (!["add", "remove", "replace"].includes(input.operation))
80
+ fail("operation 需为 add、remove 或 replace,请明确关联调整方式");
81
+ if (
82
+ input.operation === "replace" &&
83
+ !args["dry-run"] &&
84
+ !args["confirm-replace"]
85
+ )
86
+ fail(
87
+ "替换会覆盖此标签的全部关联,请先检查 dry-run 并取得用户确认,再传 --confirm-replace",
88
+ );
89
+ next.modelIds =
90
+ input.operation === "add"
91
+ ? [...new Set([...tag.modelIds, ...requested])]
92
+ : input.operation === "remove"
93
+ ? tag.modelIds.filter((id) => !requested.includes(id))
94
+ : requested;
95
+ delete next.operation;
96
+ }
97
+ next = validateTag(next, snapshot, tag?.uid);
98
+ }
99
+ const previousIds = tag?.modelIds ?? [];
100
+ const nextIds = next?.modelIds ?? [];
101
+ const changes = {
102
+ addedModelIds: nextIds.filter((id) => !previousIds.includes(id)),
103
+ removedModelIds: previousIds.filter((id) => !nextIds.includes(id)),
104
+ };
105
+ const method =
106
+ command === "create-tag"
107
+ ? "POST"
108
+ : command === "delete-tag"
109
+ ? "DELETE"
110
+ : "PUT";
111
+ const target = tag ? `${path}${tag.uid}/` : path;
112
+ const body = { revision, ...next };
113
+ if (args["dry-run"])
114
+ return { method, path: target, body, previous: tag ?? null, changes };
115
+ if (command === "delete-tag" && !args["confirm-delete"])
116
+ fail(
117
+ "删除会解除所有关联,请先检查 dry-run 并取得用户确认,再传 --confirm-delete",
118
+ );
119
+ try {
120
+ await client.request(method, target, { body });
121
+ } catch (error) {
122
+ if (error?.details?.httpStatus === 409)
123
+ throw new DatarcError(
124
+ "标签或关联已变化,请重新读取目录并核对差异后再执行",
125
+ "MODEL_TAGS_CONFLICT",
126
+ );
127
+ if ([400, 401, 403, 404].includes(error?.details?.httpStatus)) throw error;
128
+ throw new DatarcError(
129
+ "标签写入结果尚未确认,请运行 tags 核对名称、说明与关联;不要直接重复创建或写入",
130
+ "WRITE_RESULT_UNKNOWN",
131
+ { command, tagId: tag?.uid, attempted: next ?? null, changes },
132
+ );
133
+ }
134
+ try {
135
+ const verified = await readSnapshot(client, path);
136
+ const actual = tag
137
+ ? verified.tags.find((item) => item.uid === tag.uid)
138
+ : verified.tags.find((item) => item.name === next.name);
139
+ if (
140
+ command === "delete-tag"
141
+ ? !!actual
142
+ : !actual ||
143
+ actual.name !== next.name ||
144
+ actual.description !== next.description ||
145
+ !sameIds(actual.modelIds, next.modelIds)
146
+ )
147
+ throw new Error("回读内容不符合预期");
148
+ return {
149
+ status: "complete",
150
+ verified: true,
151
+ tag: actual ?? null,
152
+ changes,
153
+ snapshot: verified,
154
+ };
155
+ } catch {
156
+ throw new DatarcError(
157
+ "写入已返回成功,但回读尚未确认,请运行 tags 检查实际结果后再继续",
158
+ "WRITE_VERIFICATION_FAILED",
159
+ { writeAccepted: true, tagId: tag?.uid, expected: next ?? null, changes },
160
+ );
161
+ }
162
+ }
163
+
164
+ async function readSnapshot(client, path) {
165
+ const data = await client.request("GET", path);
166
+ if (
167
+ !data ||
168
+ typeof data.revision !== "string" ||
169
+ !Array.isArray(data.tags) ||
170
+ !Array.isArray(data.models) ||
171
+ data.tags.some(
172
+ (tag) =>
173
+ !tag ||
174
+ typeof tag.uid !== "string" ||
175
+ !/^[a-f0-9]{32}$/.test(tag.uid) ||
176
+ typeof tag.name !== "string" ||
177
+ typeof tag.description !== "string" ||
178
+ !Array.isArray(tag.modelIds) ||
179
+ tag.modelIds.some((id) => typeof id !== "string"),
180
+ ) ||
181
+ data.models.some((model) => !model || typeof model.uid !== "string")
182
+ )
183
+ throw new DatarcError(
184
+ "标签目录返回不完整,请刷新重试;持续失败请联系管理员检查服务版本",
185
+ "INVALID_RESPONSE",
186
+ );
187
+ return data;
188
+ }
189
+
190
+ function validateTag(value, snapshot, uid) {
191
+ if (
192
+ typeof value.name !== "string" ||
193
+ /[\r\n\t]/.test(value.name) ||
194
+ !/^[\u3400-\u9fffA-Za-z0-9 _()()-]{1,20}$/.test(value.name.trim())
195
+ )
196
+ fail(
197
+ "标签名称需为 1–20 字,仅支持中文、英文、数字、空格、横线、下划线和圆括号,请修改后重试",
198
+ );
199
+ const name = value.name.trim();
200
+ if (
201
+ snapshot.tags.some(
202
+ (tag) => tag.uid !== uid && tag.name.toLowerCase() === name.toLowerCase(),
203
+ )
204
+ )
205
+ fail("标签名称已存在,请复用已有标签或修改名称");
206
+ const description = value.description ?? "";
207
+ if (
208
+ typeof description !== "string" ||
209
+ Array.from(description.trim()).length > 1000
210
+ )
211
+ fail("标签说明选填,填写时请使用不超过 1000 字的文本");
212
+ return {
213
+ name,
214
+ description: description.trim(),
215
+ modelIds: validateModelIds(value.modelIds, snapshot),
216
+ };
217
+ }
218
+
219
+ function validateModelIds(ids, snapshot) {
220
+ if (
221
+ !Array.isArray(ids) ||
222
+ ids.some(
223
+ (id) =>
224
+ typeof id !== "string" ||
225
+ !snapshot.models.some((model) => model.uid === id),
226
+ )
227
+ )
228
+ fail("关联模型无效,请从标签目录返回的模型列表中重新选择");
229
+ return [...new Set(ids)];
230
+ }
231
+
232
+ function sameIds(a, b) {
233
+ return a.length === b.length && a.every((id) => b.includes(id));
234
+ }
235
+
236
+ function fail(message) {
237
+ throw new DatarcError(message, "INVALID_ARGUMENT");
238
+ }