@arcticdata/dsh-datarc-assistant 26.39.0 → 26.39.1

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 (149) hide show
  1. package/README.md +45 -10
  2. package/api-operations.js +404 -0
  3. package/client.js +452 -87
  4. package/cordis.patch.yml +4 -4
  5. package/entry/SKILL.md +34 -0
  6. package/icon.png +0 -0
  7. package/index.js +35 -47
  8. package/locale/en.json +6 -6
  9. package/locale/zh.json +6 -6
  10. package/native-api.mjs +326 -0
  11. package/native-contracts.js +195 -0
  12. package/native-execution.js +184 -0
  13. package/native-runner.mjs +107 -0
  14. package/native-state.js +67 -0
  15. package/native-tools.js +241 -0
  16. package/operations.js +1260 -0
  17. package/package.json +65 -51
  18. package/{skills → runtime}/datarc-access-permissions/SKILL.md +30 -30
  19. package/{skills → runtime}/datarc-access-permissions/references/api.md +31 -31
  20. package/{skills → runtime}/datarc-access-permissions/references/write-order.md +16 -16
  21. package/{skills → runtime}/datarc-analysis-report/SKILL.md +125 -125
  22. package/{skills → runtime}/datarc-analysis-report/agents/openai.yaml +6 -6
  23. package/{skills → runtime}/datarc-analysis-report/references/analyst-frameworks.md +215 -215
  24. package/{skills → runtime}/datarc-analysis-report/references/api-contracts.md +478 -478
  25. package/{skills → runtime}/datarc-analysis-report/references/direct-card-fallback-design.md +66 -66
  26. package/{skills → runtime}/datarc-analysis-report/references/human-in-the-loop.md +142 -142
  27. package/{skills → runtime}/datarc-analysis-report/references/loop-personalization-policy.md +138 -138
  28. package/{skills → runtime}/datarc-analysis-report/references/manifest-example.json +779 -779
  29. package/{skills → runtime}/datarc-analysis-report/references/narrative-schema.md +160 -160
  30. package/{skills → runtime}/datarc-analysis-report/references/question-syntax.md +151 -151
  31. package/{skills → runtime}/datarc-analysis-report/scripts/run_analysis_report.py +2225 -2225
  32. package/{skills → runtime}/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -1914
  33. package/{skills → runtime}/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -5295
  34. package/{skills → runtime}/datarc-data-search/SKILL.md +243 -243
  35. package/{skills → runtime}/datarc-data-search/scripts/calculate.py +116 -116
  36. package/{skills → runtime}/datarc-data-search/scripts/data-search.mjs +628 -628
  37. package/{skills/datarc-dataseek-search → runtime/datarc-data-search}/scripts/datarc-client.mjs +464 -464
  38. package/{skills → runtime}/datarc-data-search/scripts/search-result.mjs +211 -211
  39. package/{skills → runtime}/datarc-dataseek-batch-evaluation/SKILL.md +59 -59
  40. package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -36
  41. package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -30
  42. package/{skills/datarc-data-search → runtime/datarc-dataseek-batch-evaluation}/scripts/datarc-client.mjs +464 -464
  43. package/{skills → runtime}/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -1020
  44. package/{skills → runtime}/datarc-dataseek-search/SKILL.md +96 -96
  45. package/{skills/datarc-dataseek-batch-evaluation → runtime/datarc-dataseek-search}/scripts/datarc-client.mjs +464 -464
  46. package/{skills → runtime}/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -257
  47. package/{skills → runtime}/datarc-dataseek-semantic-debugging/SKILL.md +44 -44
  48. package/{skills → runtime}/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -4
  49. package/{skills → runtime}/datarc-dataseek-semantic-debugging/evals/evals.json +165 -165
  50. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/dataset.md +77 -77
  51. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -93
  52. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/interventions.md +64 -64
  53. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -47
  54. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/rollback.md +11 -11
  55. package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -95
  56. package/{skills → runtime}/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -29
  57. package/{skills → runtime}/datarc-dataseek-testset-generation/SKILL.md +47 -47
  58. package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -464
  59. package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -526
  60. package/{skills → runtime}/datarc-datasource/SKILL.md +97 -97
  61. package/{skills → runtime}/datarc-oauth/SKILL.md +50 -50
  62. package/{skills → runtime}/datarc-oauth/scripts/auth.mjs +192 -192
  63. package/{skills → runtime}/datarc-oauth/scripts/credentials.mjs +237 -237
  64. package/{skills → runtime}/datarc-report-add-card/SKILL.md +90 -90
  65. package/{skills → runtime}/datarc-report-add-card/agents/openai.yaml +4 -4
  66. package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
  67. package/{skills → runtime}/datarc-report-add-card/scripts/report-card.mjs +439 -439
  68. package/{skills → runtime}/datarc-semantic-alias/SKILL.md +20 -20
  69. package/{skills → runtime}/datarc-semantic-alias/scripts/alias-plan.mjs +422 -422
  70. package/{skills → runtime}/datarc-semantic-alias/scripts/alias.mjs +11 -11
  71. package/{skills → runtime}/datarc-semantic-calibration/SKILL.md +20 -20
  72. package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -683
  73. package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration.mjs +11 -11
  74. package/{skills → runtime}/datarc-semantic-core/SKILL.md +12 -12
  75. package/{skills → runtime}/datarc-semantic-core/scripts/artifact-journal.mjs +853 -830
  76. package/{skills → runtime}/datarc-semantic-core/scripts/canonical.mjs +48 -48
  77. package/{skills → runtime}/datarc-semantic-core/scripts/case-model.mjs +608 -608
  78. package/{skills → runtime}/datarc-semantic-core/scripts/cli.mjs +180 -180
  79. package/{skills → runtime}/datarc-semantic-core/scripts/datarc-client.mjs +180 -180
  80. package/{skills → runtime}/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -57
  81. package/{skills → runtime}/datarc-semantic-core/scripts/interventions.mjs +693 -693
  82. package/{skills → runtime}/datarc-semantic-core/scripts/live-resolution.mjs +305 -305
  83. package/{skills → runtime}/datarc-semantic-core/scripts/model-scope.mjs +49 -49
  84. package/{skills → runtime}/datarc-semantic-core/scripts/reproduction.mjs +330 -330
  85. package/{skills → runtime}/datarc-semantic-core/scripts/semantic-cli.mjs +799 -740
  86. package/{skills → runtime}/datarc-semantic-core/scripts/shared.mjs +312 -312
  87. package/{skills → runtime}/datarc-semantic-core/scripts/verification.mjs +1111 -1014
  88. package/{skills → runtime}/datarc-semantic-dataset/SKILL.md +20 -20
  89. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -749
  90. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -813
  91. package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset.mjs +11 -11
  92. package/{skills → runtime}/datarc-semantic-knowledge/SKILL.md +38 -38
  93. package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -61
  94. package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-generation.json +16 -16
  95. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -347
  96. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -848
  97. package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -31
  98. package/{skills → runtime}/datarc-semantic-prompt/SKILL.md +20 -20
  99. package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -447
  100. package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt.mjs +15 -15
  101. package/{skills → runtime}/datarc-semantic-synonym/SKILL.md +20 -20
  102. package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -688
  103. package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym.mjs +11 -11
  104. package/{skills → runtime}/datarc-viz-analysis-report/SKILL.md +108 -108
  105. package/{skills → runtime}/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -8
  106. package/{skills → runtime}/datarc-viz-analysis-report/assets/echarts.min.js +45 -45
  107. package/{skills → runtime}/datarc-viz-analysis-report/references/api-contracts.md +163 -163
  108. package/{skills → runtime}/datarc-viz-analysis-report/references/derivation-log.md +157 -157
  109. package/{skills → runtime}/datarc-viz-analysis-report/references/evidence-framework.md +76 -76
  110. package/{skills → runtime}/datarc-viz-analysis-report/references/html-template.md +92 -92
  111. package/{skills → runtime}/datarc-viz-analysis-report/references/provenance-format.md +56 -56
  112. package/{skills → runtime}/datarc-viz-analysis-report/references/question-syntax.md +151 -151
  113. package/{skills → runtime}/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -1708
  114. package/{skills → runtime}/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -964
  115. package/{skills → runtime}/datarc-workspace-configuration/SKILL.md +59 -59
  116. package/{skills → runtime}/datarc-workspace-configuration/references/artifacts.md +75 -75
  117. package/{skills → runtime}/datarc-workspace-configuration/references/verification.md +20 -20
  118. package/{skills → runtime}/datarc-workspace-configuration/scripts/configuration.mjs +722 -662
  119. package/{skills → runtime}/datarc-workspace-context/SKILL.md +43 -43
  120. package/{skills → runtime}/datarc-workspace-context/agents/openai.yaml +4 -4
  121. package/{skills → runtime}/datarc-workspace-context/scripts/datarc-client.mjs +31 -31
  122. package/{skills → runtime}/datarc-workspace-context/scripts/query-display.mjs +114 -114
  123. package/{skills → runtime}/datarc-workspace-context/scripts/workspace-context.mjs +58 -58
  124. package/{skills → runtime}/datarc-workspace-custom-tables/SKILL.md +89 -89
  125. package/{skills → runtime}/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -165
  126. package/{skills → runtime}/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -601
  127. package/{skills → runtime}/datarc-workspace-keywords/SKILL.md +197 -197
  128. package/{skills → runtime}/datarc-workspace-keywords/agents/openai.yaml +4 -4
  129. package/{skills → runtime}/datarc-workspace-keywords/references/api-fields.md +79 -79
  130. package/{skills → runtime}/datarc-workspace-keywords/references/derived-measures.md +177 -177
  131. package/{skills → runtime}/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -145
  132. package/{skills → runtime}/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -2421
  133. package/{skills → runtime}/datarc-workspace-membership/SKILL.md +29 -29
  134. package/{skills → runtime}/datarc-workspace-membership/references/api.md +19 -19
  135. package/{skills → runtime}/datarc-workspace-modeling/SKILL.md +209 -209
  136. package/{skills → runtime}/datarc-workspace-modeling/agents/openai.yaml +4 -4
  137. package/{skills → runtime}/datarc-workspace-modeling/references/join-preview.md +38 -38
  138. package/{skills → runtime}/datarc-workspace-modeling/references/model-plan.md +300 -300
  139. package/{skills → runtime}/datarc-workspace-modeling/references/model-tags.md +95 -95
  140. package/{skills → runtime}/datarc-workspace-modeling/references/modeling-contract.json +29 -29
  141. package/{skills → runtime}/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -2423
  142. package/{skills → runtime}/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -137
  143. package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -238
  144. package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -1261
  145. package/skills-provider.js +51 -0
  146. package/workflow-index.json +434 -0
  147. package/icon.svg +0 -4
  148. package/skills/datarc-report-add-card/scripts/datarc-client.mjs +0 -464
  149. package/skills-index.json +0 -194
@@ -1,238 +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
- }
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
+ }