@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,422 +1,422 @@
1
- import {
2
- canonicalJson,
3
- hashArtifactValue as hashValue,
4
- } from "../../datarc-semantic-core/scripts/canonical.mjs";
5
- import {
6
- authoritiesForCaseIds,
7
- claimStepCaseIds,
8
- fail,
9
- isRecord,
10
- listOf,
11
- normalizeText,
12
- READ_ONLY_FIELDS,
13
- requiredText,
14
- } from "../../datarc-semantic-core/scripts/shared.mjs";
15
-
16
- export async function planAlias(client, plan, operation, planningEvidence) {
17
- const { resourceKind } = operation;
18
- if (!READ_ONLY_FIELDS[resourceKind])
19
- fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
20
- const resourceId = canonicalAliasResourceId(operation.resourceId);
21
- const aliases = listOf(operation.aliases).map((alias) =>
22
- requiredText(alias, "alias"),
23
- );
24
- if (aliases.length === 0)
25
- fail("INVALID_ALIAS", "alias requires at least one alias");
26
- for (const alias of aliases) {
27
- const explicitOwners = findCoalescedAliasOwners(
28
- operation.aliasOwners,
29
- alias,
30
- );
31
- const owners =
32
- explicitOwners.length > 0
33
- ? explicitOwners
34
- : [findAliasOwner(planningEvidence, alias)].filter(Boolean);
35
- for (const owner of owners)
36
- if (
37
- String(owner.resourceId ?? owner.id) !== String(resourceId) ||
38
- (owner.resourceKind ?? owner.kind) !== resourceKind
39
- )
40
- fail(
41
- "ALIAS_CONFLICT",
42
- "the alias is already owned by another resource",
43
- { owner },
44
- );
45
- }
46
- const endpoint = aliasPath(plan.workspaceId, resourceKind, resourceId);
47
- const before = cleanAlias(
48
- await client.request("GET", endpoint),
49
- resourceKind,
50
- );
51
- const names = Array.isArray(before.names) ? before.names : [];
52
- const newAliases = aliases.filter(
53
- (alias) =>
54
- !names.some((name) => normalizeText(name) === normalizeText(alias)),
55
- );
56
- if (newAliases.length === 0) {
57
- plan.summaries.push({
58
- kind: "alias",
59
- status: "noop",
60
- resourceKind,
61
- resourceId,
62
- caseIds: structuredClone(operation.caseIds),
63
- });
64
- return;
65
- }
66
- const evidence = await validateAliasNames(
67
- client,
68
- plan.workspaceId,
69
- resourceKind,
70
- newAliases,
71
- );
72
- plan.steps.push({
73
- kind: "alias",
74
- caseIds: structuredClone(operation.caseIds),
75
- endpoint,
76
- operation: "PUT",
77
- before,
78
- after: { ...before, names: [...names, ...newAliases] },
79
- preconditions: { nameValidation: evidence },
80
- dictionarySyncRequired: true,
81
- });
82
- plan.summaries.push({
83
- kind: "alias",
84
- status: "planned",
85
- resourceKind,
86
- resourceId,
87
- caseIds: structuredClone(operation.caseIds),
88
- });
89
- }
90
-
91
- export function cleanAlias(value, resourceKind) {
92
- const result = structuredClone(value ?? {});
93
- for (const field of READ_ONLY_FIELDS[resourceKind]) delete result[field];
94
- return result;
95
- }
96
-
97
- export function findAliasOwner(evidence, alias) {
98
- return listOf(evidence?.aliasOwners).find(
99
- (owner) => normalizeText(owner.alias) === normalizeText(alias),
100
- );
101
- }
102
-
103
- export function findCoalescedAliasOwners(entries, alias) {
104
- return listOf(entries)
105
- .filter((entry) => normalizeText(entry.alias) === normalizeText(alias))
106
- .map((entry) => entry.owner);
107
- }
108
-
109
- export async function validateAliasNames(client, workspaceId, field, names) {
110
- const result = await client.request(
111
- "POST",
112
- `/api/v2/admin-projects/${workspaceId}/name-validation/`,
113
- { body: { field, names: structuredClone(names) } },
114
- );
115
- if (result?.status !== "SUCCESS")
116
- fail(
117
- "ALIAS_NAME_VALIDATION_FAILED",
118
- "alias name validation did not succeed",
119
- { field, status: result?.status },
120
- );
121
- return {
122
- field,
123
- namesHash: hashValue(names),
124
- status: "SUCCESS",
125
- };
126
- }
127
-
128
- export async function revalidateAliasStep(client, workspaceId, step) {
129
- const { field, names } = validateAliasStepStructure(step, workspaceId);
130
- await validateAliasNames(client, workspaceId, field, names);
131
- }
132
-
133
- export function validateAliasStepStructure(step, workspaceId) {
134
- const allowedStepFields = new Set([
135
- "after",
136
- "before",
137
- "caseIds",
138
- "dictionarySyncRequired",
139
- "endpoint",
140
- "kind",
141
- "operation",
142
- "preconditions",
143
- ]);
144
- if (
145
- Object.keys(step).some((field) => !allowedStepFields.has(field)) ||
146
- [...allowedStepFields].some((field) => !Object.hasOwn(step, field)) ||
147
- step.kind !== "alias" ||
148
- step.dictionarySyncRequired !== true
149
- )
150
- fail("INVALID_ALIAS_PLAN", "alias step fields are invalid");
151
- const identity = aliasStepIdentity(step.endpoint, workspaceId);
152
- if (step.operation !== "PUT")
153
- fail("INVALID_ALIAS_PLAN", "alias steps must use PUT");
154
- if (!isRecord(step.before) || !isRecord(step.after))
155
- fail("INVALID_ALIAS_PLAN", "alias steps require object snapshots");
156
- if (canonicalAliasResourceId(step.before.id) !== identity.resourceId)
157
- fail("INVALID_ALIAS_PLAN", "alias endpoint does not match its resource");
158
- if (!Array.isArray(step.after.names))
159
- fail("INVALID_ALIAS_PLAN", "alias after.names must be an array");
160
- if (step.before.names !== undefined && !Array.isArray(step.before.names))
161
- fail("INVALID_ALIAS_PLAN", "alias before.names must be an array");
162
-
163
- const beforeNames = listOf(step.before.names);
164
- const afterNames = step.after.names;
165
- if (
166
- afterNames.length <= beforeNames.length ||
167
- canonicalJson(afterNames.slice(0, beforeNames.length)) !==
168
- canonicalJson(beforeNames)
169
- )
170
- fail(
171
- "INVALID_ALIAS_PLAN",
172
- "alias names must preserve the original ordered prefix",
173
- );
174
-
175
- const beforeBody = structuredClone(step.before);
176
- const afterBody = structuredClone(step.after);
177
- delete beforeBody.names;
178
- delete afterBody.names;
179
- if (canonicalJson(beforeBody) !== canonicalJson(afterBody))
180
- fail("INVALID_ALIAS_PLAN", "alias steps may only append names");
181
-
182
- const seenNames = new Set(beforeNames.map(normalizeText));
183
- const names = afterNames.slice(beforeNames.length);
184
- for (const name of names) {
185
- if (
186
- typeof name !== "string" ||
187
- !name.trim() ||
188
- name !== name.trim() ||
189
- seenNames.has(normalizeText(name))
190
- )
191
- fail("INVALID_ALIAS_PLAN", "appended aliases must be unique names");
192
- seenNames.add(normalizeText(name));
193
- }
194
-
195
- if (!Array.isArray(step.caseIds) || step.caseIds.length === 0)
196
- fail("INVALID_ALIAS_PLAN", "alias steps require caseIds");
197
- const caseIds = new Set();
198
- for (const caseId of step.caseIds) {
199
- if (
200
- typeof caseId !== "string" ||
201
- !caseId ||
202
- caseId !== caseId.trim() ||
203
- caseIds.has(caseId)
204
- )
205
- fail("INVALID_ALIAS_PLAN", "alias step caseIds are invalid");
206
- caseIds.add(caseId);
207
- }
208
-
209
- if (
210
- !isRecord(step.preconditions) ||
211
- canonicalJson(Object.keys(step.preconditions).sort()) !==
212
- canonicalJson(["nameValidation"])
213
- )
214
- fail(
215
- "INVALID_ALIAS_VALIDATION_EVIDENCE",
216
- "alias preconditions are invalid",
217
- );
218
- const evidence = step.preconditions.nameValidation;
219
- if (
220
- evidence?.field !== identity.field ||
221
- evidence?.status !== "SUCCESS" ||
222
- evidence?.namesHash !== hashValue(names)
223
- )
224
- fail(
225
- "INVALID_ALIAS_VALIDATION_EVIDENCE",
226
- "alias validation evidence does not match the planned names",
227
- { field: identity.field },
228
- );
229
- return {
230
- caseIds: [...caseIds],
231
- field: identity.field,
232
- names,
233
- resourceId: identity.resourceId,
234
- };
235
- }
236
-
237
- export function aliasStepIdentity(endpoint, workspaceId) {
238
- const prefixes = [
239
- ["dimension", `/api/v2/admin-projects/${workspaceId}/dimensions/`],
240
- ["measure", `/api/v2/admin-projects/${workspaceId}/measures/`],
241
- ];
242
- for (const [field, prefix] of prefixes) {
243
- if (typeof endpoint !== "string" || !endpoint.startsWith(prefix)) continue;
244
- const suffix = endpoint.slice(prefix.length);
245
- if (!suffix.endsWith("/") || suffix.slice(0, -1).includes("/")) break;
246
- const resourceText = suffix.slice(0, -1);
247
- const resourceId = canonicalAliasResourceId(resourceText);
248
- if (resourceText !== String(resourceId))
249
- fail("INVALID_ALIAS_PLAN", "alias endpoint resourceId is not canonical");
250
- return { field, resourceId };
251
- }
252
- fail("INVALID_ALIAS_PLAN", "alias endpoint is invalid for the workspace");
253
- }
254
-
255
- export function canonicalAliasResourceId(value) {
256
- const text = typeof value === "string" ? value.trim() : value;
257
- if (!(
258
- (typeof text === "number" && Number.isSafeInteger(text)) ||
259
- (typeof text === "string" && /^\d+$/.test(text))
260
- ))
261
- fail("INVALID_ALIAS", "alias resourceId must be a positive integer");
262
- const resourceId = Number(text);
263
- if (!Number.isSafeInteger(resourceId) || resourceId <= 0)
264
- fail("INVALID_ALIAS", "alias resourceId must be a positive integer");
265
- return resourceId;
266
- }
267
-
268
- export function aliasPath(workspaceId, resourceKind, resourceId) {
269
- return `/api/v2/admin-projects/${workspaceId}/${
270
- resourceKind === "measure" ? "measures" : "dimensions"
271
- }/${resourceId}/`;
272
- }
273
-
274
- function aliasAuthorityTarget(operation) {
275
- if (!READ_ONLY_FIELDS[operation.resourceKind])
276
- fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
277
- return {
278
- alias: requiredText(operation.alias, "alias"),
279
- resourceId: canonicalAliasResourceId(operation.resourceId),
280
- resourceKind: operation.resourceKind,
281
- };
282
- }
283
-
284
- function createAliasCoalescer() {
285
- const groups = new Map();
286
- const targets = new Map();
287
- return {
288
- add(operation, result) {
289
- const caseId = operation.caseId.trim();
290
- const { resourceKind } = operation;
291
- if (!READ_ONLY_FIELDS[resourceKind])
292
- fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
293
- const resourceId = canonicalAliasResourceId(operation.resourceId);
294
- const alias = requiredText(operation.alias, "alias");
295
- const targetKey = `alias:${resourceKind}:${resourceId}`;
296
- const aliasKey = normalizeText(alias);
297
- const previousTarget = targets.get(aliasKey);
298
- if (previousTarget !== undefined && previousTarget !== targetKey)
299
- fail("ALIAS_CONFLICT", "the alias targets more than one resource", {
300
- alias,
301
- });
302
- targets.set(aliasKey, targetKey);
303
-
304
- let group = groups.get(targetKey);
305
- if (!group) {
306
- group = {
307
- ...structuredClone(operation),
308
- caseId,
309
- resourceId,
310
- aliases: [],
311
- aliasOwners: [],
312
- caseIds: [],
313
- };
314
- delete group.alias;
315
- delete group.aliasOwner;
316
- groups.set(targetKey, group);
317
- result.push(group);
318
- }
319
- group.caseIds.push(caseId);
320
- if (!group.aliases.some((value) => normalizeText(value) === aliasKey))
321
- group.aliases.push(alias);
322
- if (operation.aliasOwner !== undefined)
323
- group.aliasOwners.push({
324
- alias,
325
- owner: structuredClone(operation.aliasOwner),
326
- });
327
- return true;
328
- },
329
- };
330
- }
331
-
332
- function aliasWriteDesired(client, step) {
333
- return client.request("PUT", step.endpoint, { body: step.after });
334
- }
335
-
336
- function aliasWriteBefore(client, step) {
337
- return client.request("PUT", step.endpoint, { body: step.before });
338
- }
339
-
340
- async function aliasReadState(client, workspaceId, step) {
341
- const kind = step.endpoint.includes("/measures/") ? "measure" : "dimension";
342
- return cleanAlias(await client.request("GET", step.endpoint), kind);
343
- }
344
-
345
- function validateAliasAuthorityTarget(target) {
346
- if (
347
- canonicalJson(Object.keys(target).sort()) !==
348
- canonicalJson(["alias", "resourceId", "resourceKind"]) ||
349
- !READ_ONLY_FIELDS[target.resourceKind] ||
350
- canonicalAliasResourceId(target.resourceId) !== target.resourceId ||
351
- requiredText(target.alias, "authority.alias") !== target.alias
352
- )
353
- fail("INVALID_PLAN_STRUCTURE", "alias authority target is invalid");
354
- }
355
-
356
- function validateAliasPlanStep(step, plan, authorities, plannedCaseIds, usedCaseIds) {
357
- const { caseIds, field, names, resourceId } = validateAliasStepStructure(
358
- step,
359
- plan.workspaceId,
360
- );
361
- const stepAuthorities = authoritiesForCaseIds(
362
- authorities,
363
- caseIds,
364
- "alias",
365
- "INVALID_ALIAS_PLAN",
366
- );
367
- const beforeNames = new Set(listOf(step.before.names).map(normalizeText));
368
- const expectedNames = [];
369
- const expectedSet = new Set();
370
- for (const { target } of stepAuthorities) {
371
- const aliasKey = normalizeText(target.alias);
372
- if (
373
- target.resourceKind !== field ||
374
- target.resourceId !== resourceId ||
375
- !step.after.names.some((name) => normalizeText(name) === aliasKey)
376
- )
377
- fail(
378
- "INVALID_ALIAS_PLAN",
379
- "alias step caseIds must match exact alias authorities",
380
- );
381
- if (!beforeNames.has(aliasKey) && !expectedSet.has(aliasKey)) {
382
- expectedSet.add(aliasKey);
383
- expectedNames.push(aliasKey);
384
- }
385
- }
386
- if (
387
- canonicalJson(names.map(normalizeText)) !== canonicalJson(expectedNames)
388
- )
389
- fail(
390
- "INVALID_ALIAS_PLAN",
391
- "alias step additions must exactly match case authorities",
392
- );
393
- claimStepCaseIds(usedCaseIds, caseIds);
394
- for (const caseId of caseIds) plannedCaseIds.add(caseId);
395
- }
396
-
397
- function aliasSummaryMatches(summary, authorityTargets) {
398
- const targets = authorityTargets.map(({ target }) => target);
399
- return targets.every(
400
- (target) =>
401
- summary.resourceKind === target.resourceKind &&
402
- summary.resourceId === target.resourceId,
403
- );
404
- }
405
-
406
- export const aliasKind = {
407
- name: "alias",
408
- planable: true,
409
- plan: (client, plan, operation, options = {}) =>
410
- planAlias(client, plan, operation, options.planningEvidence),
411
- authorityTarget: aliasAuthorityTarget,
412
- createCoalescer: createAliasCoalescer,
413
- writeDesired: aliasWriteDesired,
414
- writeBefore: aliasWriteBefore,
415
- readState: aliasReadState,
416
- postApplyRead: revalidateAliasStep,
417
- targetKey: (step) => `alias:${step.endpoint}`,
418
- validateAuthorityTarget: validateAliasAuthorityTarget,
419
- validateStep: validateAliasPlanStep,
420
- summaryField: ["resourceKind", "resourceId"],
421
- summaryMatches: aliasSummaryMatches,
422
- };
1
+ import {
2
+ canonicalJson,
3
+ hashArtifactValue as hashValue,
4
+ } from "../../datarc-semantic-core/scripts/canonical.mjs";
5
+ import {
6
+ authoritiesForCaseIds,
7
+ claimStepCaseIds,
8
+ fail,
9
+ isRecord,
10
+ listOf,
11
+ normalizeText,
12
+ READ_ONLY_FIELDS,
13
+ requiredText,
14
+ } from "../../datarc-semantic-core/scripts/shared.mjs";
15
+
16
+ export async function planAlias(client, plan, operation, planningEvidence) {
17
+ const { resourceKind } = operation;
18
+ if (!READ_ONLY_FIELDS[resourceKind])
19
+ fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
20
+ const resourceId = canonicalAliasResourceId(operation.resourceId);
21
+ const aliases = listOf(operation.aliases).map((alias) =>
22
+ requiredText(alias, "alias"),
23
+ );
24
+ if (aliases.length === 0)
25
+ fail("INVALID_ALIAS", "alias requires at least one alias");
26
+ for (const alias of aliases) {
27
+ const explicitOwners = findCoalescedAliasOwners(
28
+ operation.aliasOwners,
29
+ alias,
30
+ );
31
+ const owners =
32
+ explicitOwners.length > 0
33
+ ? explicitOwners
34
+ : [findAliasOwner(planningEvidence, alias)].filter(Boolean);
35
+ for (const owner of owners)
36
+ if (
37
+ String(owner.resourceId ?? owner.id) !== String(resourceId) ||
38
+ (owner.resourceKind ?? owner.kind) !== resourceKind
39
+ )
40
+ fail(
41
+ "ALIAS_CONFLICT",
42
+ "the alias is already owned by another resource",
43
+ { owner },
44
+ );
45
+ }
46
+ const endpoint = aliasPath(plan.workspaceId, resourceKind, resourceId);
47
+ const before = cleanAlias(
48
+ await client.request("GET", endpoint),
49
+ resourceKind,
50
+ );
51
+ const names = Array.isArray(before.names) ? before.names : [];
52
+ const newAliases = aliases.filter(
53
+ (alias) =>
54
+ !names.some((name) => normalizeText(name) === normalizeText(alias)),
55
+ );
56
+ if (newAliases.length === 0) {
57
+ plan.summaries.push({
58
+ kind: "alias",
59
+ status: "noop",
60
+ resourceKind,
61
+ resourceId,
62
+ caseIds: structuredClone(operation.caseIds),
63
+ });
64
+ return;
65
+ }
66
+ const evidence = await validateAliasNames(
67
+ client,
68
+ plan.workspaceId,
69
+ resourceKind,
70
+ newAliases,
71
+ );
72
+ plan.steps.push({
73
+ kind: "alias",
74
+ caseIds: structuredClone(operation.caseIds),
75
+ endpoint,
76
+ operation: "PUT",
77
+ before,
78
+ after: { ...before, names: [...names, ...newAliases] },
79
+ preconditions: { nameValidation: evidence },
80
+ dictionarySyncRequired: true,
81
+ });
82
+ plan.summaries.push({
83
+ kind: "alias",
84
+ status: "planned",
85
+ resourceKind,
86
+ resourceId,
87
+ caseIds: structuredClone(operation.caseIds),
88
+ });
89
+ }
90
+
91
+ export function cleanAlias(value, resourceKind) {
92
+ const result = structuredClone(value ?? {});
93
+ for (const field of READ_ONLY_FIELDS[resourceKind]) delete result[field];
94
+ return result;
95
+ }
96
+
97
+ export function findAliasOwner(evidence, alias) {
98
+ return listOf(evidence?.aliasOwners).find(
99
+ (owner) => normalizeText(owner.alias) === normalizeText(alias),
100
+ );
101
+ }
102
+
103
+ export function findCoalescedAliasOwners(entries, alias) {
104
+ return listOf(entries)
105
+ .filter((entry) => normalizeText(entry.alias) === normalizeText(alias))
106
+ .map((entry) => entry.owner);
107
+ }
108
+
109
+ export async function validateAliasNames(client, workspaceId, field, names) {
110
+ const result = await client.request(
111
+ "POST",
112
+ `/api/v2/admin-projects/${workspaceId}/name-validation/`,
113
+ { body: { field, names: structuredClone(names) } },
114
+ );
115
+ if (result?.status !== "SUCCESS")
116
+ fail(
117
+ "ALIAS_NAME_VALIDATION_FAILED",
118
+ "alias name validation did not succeed",
119
+ { field, status: result?.status },
120
+ );
121
+ return {
122
+ field,
123
+ namesHash: hashValue(names),
124
+ status: "SUCCESS",
125
+ };
126
+ }
127
+
128
+ export async function revalidateAliasStep(client, workspaceId, step) {
129
+ const { field, names } = validateAliasStepStructure(step, workspaceId);
130
+ await validateAliasNames(client, workspaceId, field, names);
131
+ }
132
+
133
+ export function validateAliasStepStructure(step, workspaceId) {
134
+ const allowedStepFields = new Set([
135
+ "after",
136
+ "before",
137
+ "caseIds",
138
+ "dictionarySyncRequired",
139
+ "endpoint",
140
+ "kind",
141
+ "operation",
142
+ "preconditions",
143
+ ]);
144
+ if (
145
+ Object.keys(step).some((field) => !allowedStepFields.has(field)) ||
146
+ [...allowedStepFields].some((field) => !Object.hasOwn(step, field)) ||
147
+ step.kind !== "alias" ||
148
+ step.dictionarySyncRequired !== true
149
+ )
150
+ fail("INVALID_ALIAS_PLAN", "alias step fields are invalid");
151
+ const identity = aliasStepIdentity(step.endpoint, workspaceId);
152
+ if (step.operation !== "PUT")
153
+ fail("INVALID_ALIAS_PLAN", "alias steps must use PUT");
154
+ if (!isRecord(step.before) || !isRecord(step.after))
155
+ fail("INVALID_ALIAS_PLAN", "alias steps require object snapshots");
156
+ if (canonicalAliasResourceId(step.before.id) !== identity.resourceId)
157
+ fail("INVALID_ALIAS_PLAN", "alias endpoint does not match its resource");
158
+ if (!Array.isArray(step.after.names))
159
+ fail("INVALID_ALIAS_PLAN", "alias after.names must be an array");
160
+ if (step.before.names !== undefined && !Array.isArray(step.before.names))
161
+ fail("INVALID_ALIAS_PLAN", "alias before.names must be an array");
162
+
163
+ const beforeNames = listOf(step.before.names);
164
+ const afterNames = step.after.names;
165
+ if (
166
+ afterNames.length <= beforeNames.length ||
167
+ canonicalJson(afterNames.slice(0, beforeNames.length)) !==
168
+ canonicalJson(beforeNames)
169
+ )
170
+ fail(
171
+ "INVALID_ALIAS_PLAN",
172
+ "alias names must preserve the original ordered prefix",
173
+ );
174
+
175
+ const beforeBody = structuredClone(step.before);
176
+ const afterBody = structuredClone(step.after);
177
+ delete beforeBody.names;
178
+ delete afterBody.names;
179
+ if (canonicalJson(beforeBody) !== canonicalJson(afterBody))
180
+ fail("INVALID_ALIAS_PLAN", "alias steps may only append names");
181
+
182
+ const seenNames = new Set(beforeNames.map(normalizeText));
183
+ const names = afterNames.slice(beforeNames.length);
184
+ for (const name of names) {
185
+ if (
186
+ typeof name !== "string" ||
187
+ !name.trim() ||
188
+ name !== name.trim() ||
189
+ seenNames.has(normalizeText(name))
190
+ )
191
+ fail("INVALID_ALIAS_PLAN", "appended aliases must be unique names");
192
+ seenNames.add(normalizeText(name));
193
+ }
194
+
195
+ if (!Array.isArray(step.caseIds) || step.caseIds.length === 0)
196
+ fail("INVALID_ALIAS_PLAN", "alias steps require caseIds");
197
+ const caseIds = new Set();
198
+ for (const caseId of step.caseIds) {
199
+ if (
200
+ typeof caseId !== "string" ||
201
+ !caseId ||
202
+ caseId !== caseId.trim() ||
203
+ caseIds.has(caseId)
204
+ )
205
+ fail("INVALID_ALIAS_PLAN", "alias step caseIds are invalid");
206
+ caseIds.add(caseId);
207
+ }
208
+
209
+ if (
210
+ !isRecord(step.preconditions) ||
211
+ canonicalJson(Object.keys(step.preconditions).sort()) !==
212
+ canonicalJson(["nameValidation"])
213
+ )
214
+ fail(
215
+ "INVALID_ALIAS_VALIDATION_EVIDENCE",
216
+ "alias preconditions are invalid",
217
+ );
218
+ const evidence = step.preconditions.nameValidation;
219
+ if (
220
+ evidence?.field !== identity.field ||
221
+ evidence?.status !== "SUCCESS" ||
222
+ evidence?.namesHash !== hashValue(names)
223
+ )
224
+ fail(
225
+ "INVALID_ALIAS_VALIDATION_EVIDENCE",
226
+ "alias validation evidence does not match the planned names",
227
+ { field: identity.field },
228
+ );
229
+ return {
230
+ caseIds: [...caseIds],
231
+ field: identity.field,
232
+ names,
233
+ resourceId: identity.resourceId,
234
+ };
235
+ }
236
+
237
+ export function aliasStepIdentity(endpoint, workspaceId) {
238
+ const prefixes = [
239
+ ["dimension", `/api/v2/admin-projects/${workspaceId}/dimensions/`],
240
+ ["measure", `/api/v2/admin-projects/${workspaceId}/measures/`],
241
+ ];
242
+ for (const [field, prefix] of prefixes) {
243
+ if (typeof endpoint !== "string" || !endpoint.startsWith(prefix)) continue;
244
+ const suffix = endpoint.slice(prefix.length);
245
+ if (!suffix.endsWith("/") || suffix.slice(0, -1).includes("/")) break;
246
+ const resourceText = suffix.slice(0, -1);
247
+ const resourceId = canonicalAliasResourceId(resourceText);
248
+ if (resourceText !== String(resourceId))
249
+ fail("INVALID_ALIAS_PLAN", "alias endpoint resourceId is not canonical");
250
+ return { field, resourceId };
251
+ }
252
+ fail("INVALID_ALIAS_PLAN", "alias endpoint is invalid for the workspace");
253
+ }
254
+
255
+ export function canonicalAliasResourceId(value) {
256
+ const text = typeof value === "string" ? value.trim() : value;
257
+ if (!(
258
+ (typeof text === "number" && Number.isSafeInteger(text)) ||
259
+ (typeof text === "string" && /^\d+$/.test(text))
260
+ ))
261
+ fail("INVALID_ALIAS", "alias resourceId must be a positive integer");
262
+ const resourceId = Number(text);
263
+ if (!Number.isSafeInteger(resourceId) || resourceId <= 0)
264
+ fail("INVALID_ALIAS", "alias resourceId must be a positive integer");
265
+ return resourceId;
266
+ }
267
+
268
+ export function aliasPath(workspaceId, resourceKind, resourceId) {
269
+ return `/api/v2/admin-projects/${workspaceId}/${
270
+ resourceKind === "measure" ? "measures" : "dimensions"
271
+ }/${resourceId}/`;
272
+ }
273
+
274
+ function aliasAuthorityTarget(operation) {
275
+ if (!READ_ONLY_FIELDS[operation.resourceKind])
276
+ fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
277
+ return {
278
+ alias: requiredText(operation.alias, "alias"),
279
+ resourceId: canonicalAliasResourceId(operation.resourceId),
280
+ resourceKind: operation.resourceKind,
281
+ };
282
+ }
283
+
284
+ function createAliasCoalescer() {
285
+ const groups = new Map();
286
+ const targets = new Map();
287
+ return {
288
+ add(operation, result) {
289
+ const caseId = operation.caseId.trim();
290
+ const { resourceKind } = operation;
291
+ if (!READ_ONLY_FIELDS[resourceKind])
292
+ fail("INVALID_ALIAS", "alias requires a dimension or measure resourceId");
293
+ const resourceId = canonicalAliasResourceId(operation.resourceId);
294
+ const alias = requiredText(operation.alias, "alias");
295
+ const targetKey = `alias:${resourceKind}:${resourceId}`;
296
+ const aliasKey = normalizeText(alias);
297
+ const previousTarget = targets.get(aliasKey);
298
+ if (previousTarget !== undefined && previousTarget !== targetKey)
299
+ fail("ALIAS_CONFLICT", "the alias targets more than one resource", {
300
+ alias,
301
+ });
302
+ targets.set(aliasKey, targetKey);
303
+
304
+ let group = groups.get(targetKey);
305
+ if (!group) {
306
+ group = {
307
+ ...structuredClone(operation),
308
+ caseId,
309
+ resourceId,
310
+ aliases: [],
311
+ aliasOwners: [],
312
+ caseIds: [],
313
+ };
314
+ delete group.alias;
315
+ delete group.aliasOwner;
316
+ groups.set(targetKey, group);
317
+ result.push(group);
318
+ }
319
+ group.caseIds.push(caseId);
320
+ if (!group.aliases.some((value) => normalizeText(value) === aliasKey))
321
+ group.aliases.push(alias);
322
+ if (operation.aliasOwner !== undefined)
323
+ group.aliasOwners.push({
324
+ alias,
325
+ owner: structuredClone(operation.aliasOwner),
326
+ });
327
+ return true;
328
+ },
329
+ };
330
+ }
331
+
332
+ function aliasWriteDesired(client, step) {
333
+ return client.request("PUT", step.endpoint, { body: step.after });
334
+ }
335
+
336
+ function aliasWriteBefore(client, step) {
337
+ return client.request("PUT", step.endpoint, { body: step.before });
338
+ }
339
+
340
+ async function aliasReadState(client, workspaceId, step) {
341
+ const kind = step.endpoint.includes("/measures/") ? "measure" : "dimension";
342
+ return cleanAlias(await client.request("GET", step.endpoint), kind);
343
+ }
344
+
345
+ function validateAliasAuthorityTarget(target) {
346
+ if (
347
+ canonicalJson(Object.keys(target).sort()) !==
348
+ canonicalJson(["alias", "resourceId", "resourceKind"]) ||
349
+ !READ_ONLY_FIELDS[target.resourceKind] ||
350
+ canonicalAliasResourceId(target.resourceId) !== target.resourceId ||
351
+ requiredText(target.alias, "authority.alias") !== target.alias
352
+ )
353
+ fail("INVALID_PLAN_STRUCTURE", "alias authority target is invalid");
354
+ }
355
+
356
+ function validateAliasPlanStep(step, plan, authorities, plannedCaseIds, usedCaseIds) {
357
+ const { caseIds, field, names, resourceId } = validateAliasStepStructure(
358
+ step,
359
+ plan.workspaceId,
360
+ );
361
+ const stepAuthorities = authoritiesForCaseIds(
362
+ authorities,
363
+ caseIds,
364
+ "alias",
365
+ "INVALID_ALIAS_PLAN",
366
+ );
367
+ const beforeNames = new Set(listOf(step.before.names).map(normalizeText));
368
+ const expectedNames = [];
369
+ const expectedSet = new Set();
370
+ for (const { target } of stepAuthorities) {
371
+ const aliasKey = normalizeText(target.alias);
372
+ if (
373
+ target.resourceKind !== field ||
374
+ target.resourceId !== resourceId ||
375
+ !step.after.names.some((name) => normalizeText(name) === aliasKey)
376
+ )
377
+ fail(
378
+ "INVALID_ALIAS_PLAN",
379
+ "alias step caseIds must match exact alias authorities",
380
+ );
381
+ if (!beforeNames.has(aliasKey) && !expectedSet.has(aliasKey)) {
382
+ expectedSet.add(aliasKey);
383
+ expectedNames.push(aliasKey);
384
+ }
385
+ }
386
+ if (
387
+ canonicalJson(names.map(normalizeText)) !== canonicalJson(expectedNames)
388
+ )
389
+ fail(
390
+ "INVALID_ALIAS_PLAN",
391
+ "alias step additions must exactly match case authorities",
392
+ );
393
+ claimStepCaseIds(usedCaseIds, caseIds);
394
+ for (const caseId of caseIds) plannedCaseIds.add(caseId);
395
+ }
396
+
397
+ function aliasSummaryMatches(summary, authorityTargets) {
398
+ const targets = authorityTargets.map(({ target }) => target);
399
+ return targets.every(
400
+ (target) =>
401
+ summary.resourceKind === target.resourceKind &&
402
+ summary.resourceId === target.resourceId,
403
+ );
404
+ }
405
+
406
+ export const aliasKind = {
407
+ name: "alias",
408
+ planable: true,
409
+ plan: (client, plan, operation, options = {}) =>
410
+ planAlias(client, plan, operation, options.planningEvidence),
411
+ authorityTarget: aliasAuthorityTarget,
412
+ createCoalescer: createAliasCoalescer,
413
+ writeDesired: aliasWriteDesired,
414
+ writeBefore: aliasWriteBefore,
415
+ readState: aliasReadState,
416
+ postApplyRead: revalidateAliasStep,
417
+ targetKey: (step) => `alias:${step.endpoint}`,
418
+ validateAuthorityTarget: validateAliasAuthorityTarget,
419
+ validateStep: validateAliasPlanStep,
420
+ summaryField: ["resourceKind", "resourceId"],
421
+ summaryMatches: aliasSummaryMatches,
422
+ };