@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.
- package/README.md +20 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +4 -0
- package/index.js +35 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +37 -4
- package/skills/datarc-access-permissions/SKILL.md +30 -0
- package/skills/datarc-access-permissions/references/api.md +31 -0
- package/skills/datarc-access-permissions/references/write-order.md +16 -0
- package/skills/datarc-analysis-report/SKILL.md +125 -0
- package/skills/datarc-analysis-report/agents/openai.yaml +6 -0
- package/skills/datarc-analysis-report/references/analyst-frameworks.md +215 -0
- package/skills/datarc-analysis-report/references/api-contracts.md +478 -0
- package/skills/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
- package/skills/datarc-analysis-report/references/human-in-the-loop.md +142 -0
- package/skills/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
- package/skills/datarc-analysis-report/references/manifest-example.json +779 -0
- package/skills/datarc-analysis-report/references/narrative-schema.md +160 -0
- package/skills/datarc-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
- package/skills/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
- package/skills/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
- package/skills/datarc-data-search/SKILL.md +243 -0
- package/skills/datarc-data-search/scripts/calculate.py +116 -0
- package/skills/datarc-data-search/scripts/data-search.mjs +628 -0
- package/skills/datarc-data-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-data-search/scripts/search-result.mjs +211 -0
- package/skills/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
- package/skills/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
- package/skills/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
- package/skills/datarc-dataseek-search/SKILL.md +96 -0
- package/skills/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
- package/skills/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
- package/skills/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
- package/skills/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
- package/skills/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
- package/skills/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
- package/skills/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
- package/skills/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
- package/skills/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
- package/skills/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
- package/skills/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
- package/skills/datarc-dataseek-testset-generation/SKILL.md +47 -0
- package/skills/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
- package/skills/datarc-datasource/SKILL.md +97 -0
- package/skills/datarc-oauth/SKILL.md +47 -0
- package/skills/datarc-oauth/scripts/auth.mjs +130 -0
- package/skills/datarc-oauth/scripts/credentials.mjs +237 -0
- package/skills/datarc-report-add-card/SKILL.md +90 -0
- package/skills/datarc-report-add-card/agents/openai.yaml +4 -0
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/skills/datarc-report-add-card/scripts/report-card.mjs +439 -0
- package/skills/datarc-semantic-alias/SKILL.md +20 -0
- package/skills/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
- package/skills/datarc-semantic-alias/scripts/alias.mjs +11 -0
- package/skills/datarc-semantic-calibration/SKILL.md +20 -0
- package/skills/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
- package/skills/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
- package/skills/datarc-semantic-core/SKILL.md +12 -0
- package/skills/datarc-semantic-core/scripts/artifact-journal.mjs +830 -0
- package/skills/datarc-semantic-core/scripts/canonical.mjs +48 -0
- package/skills/datarc-semantic-core/scripts/case-model.mjs +608 -0
- package/skills/datarc-semantic-core/scripts/cli.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
- package/skills/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
- package/skills/datarc-semantic-core/scripts/interventions.mjs +693 -0
- package/skills/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
- package/skills/datarc-semantic-core/scripts/model-scope.mjs +49 -0
- package/skills/datarc-semantic-core/scripts/reproduction.mjs +330 -0
- package/skills/datarc-semantic-core/scripts/semantic-cli.mjs +740 -0
- package/skills/datarc-semantic-core/scripts/shared.mjs +312 -0
- package/skills/datarc-semantic-core/scripts/verification.mjs +1014 -0
- package/skills/datarc-semantic-dataset/SKILL.md +20 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
- package/skills/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
- package/skills/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
- package/skills/datarc-semantic-knowledge/SKILL.md +38 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
- package/skills/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
- package/skills/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
- package/skills/datarc-semantic-prompt/SKILL.md +20 -0
- package/skills/datarc-semantic-prompt/scripts/prompt-plan.mjs +447 -0
- package/skills/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
- package/skills/datarc-semantic-synonym/SKILL.md +20 -0
- package/skills/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
- package/skills/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
- package/skills/datarc-viz-analysis-report/SKILL.md +108 -0
- package/skills/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
- package/skills/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
- package/skills/datarc-viz-analysis-report/references/api-contracts.md +163 -0
- package/skills/datarc-viz-analysis-report/references/derivation-log.md +157 -0
- package/skills/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
- package/skills/datarc-viz-analysis-report/references/html-template.md +92 -0
- package/skills/datarc-viz-analysis-report/references/provenance-format.md +56 -0
- package/skills/datarc-viz-analysis-report/references/question-syntax.md +151 -0
- package/skills/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
- package/skills/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
- package/skills/datarc-workspace-configuration/SKILL.md +59 -0
- package/skills/datarc-workspace-configuration/references/artifacts.md +75 -0
- package/skills/datarc-workspace-configuration/references/verification.md +20 -0
- package/skills/datarc-workspace-configuration/scripts/configuration.mjs +662 -0
- package/skills/datarc-workspace-context/SKILL.md +43 -0
- package/skills/datarc-workspace-context/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
- package/skills/datarc-workspace-context/scripts/query-display.mjs +114 -0
- package/skills/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
- package/skills/datarc-workspace-custom-tables/SKILL.md +89 -0
- package/skills/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
- package/skills/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
- package/skills/datarc-workspace-keywords/SKILL.md +197 -0
- package/skills/datarc-workspace-keywords/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-keywords/references/api-fields.md +79 -0
- package/skills/datarc-workspace-keywords/references/derived-measures.md +177 -0
- package/skills/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
- package/skills/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
- package/skills/datarc-workspace-membership/SKILL.md +29 -0
- package/skills/datarc-workspace-membership/references/api.md +19 -0
- package/skills/datarc-workspace-modeling/SKILL.md +209 -0
- package/skills/datarc-workspace-modeling/agents/openai.yaml +4 -0
- package/skills/datarc-workspace-modeling/references/join-preview.md +38 -0
- package/skills/datarc-workspace-modeling/references/model-plan.md +300 -0
- package/skills/datarc-workspace-modeling/references/model-tags.md +95 -0
- package/skills/datarc-workspace-modeling/references/modeling-contract.json +29 -0
- package/skills/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
- package/skills/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
- package/skills/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
- package/skills-index.json +194 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: datarc-workspace-context
|
|
3
|
+
description: 北极九章(datarc)工作区名称发现与查询上下文技能。在任何查询、报告或配置任务尚未确定工作区时,先读取可访问工作区名称并让用户选择,内部解析 ID;选定后可读取模型、指标、维度及示例。不修改工作区或执行数据查询。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Datarc 工作区选择与查询上下文
|
|
7
|
+
|
|
8
|
+
先用 datarc-oauth 从环境变量或用户凭据文件解析认证;令牌不进入命令参数或回复。固定 DATARC_WORKSPACE_DIR 为会话工作区绝对路径,再使用本 Skill 的绝对脚本路径执行,只使用只读接口。依赖本流程的业务技能需同级安装本技能;缺失时提示安装,不把寻找 ID 的任务转交给用户。
|
|
9
|
+
|
|
10
|
+
## 先按名称确定工作区
|
|
11
|
+
|
|
12
|
+
1. 优先采用用户本次指定的目标,其次复用本次对话同一认证上下文中的明确选择;首次选定时调用 workspace 实时校验。普通问数已在当前对话确认过工作区时,沿用其 ID 或该对话的结果引用,新查询仍由服务端检查数据权限;其他任务仍实时校验所选工作区。既无指定目标也无对话选择时先运行 default:有已保存的默认工作区且实时校验成功,直接采用并告知名称,不再次询问;返回 null 才进入名称选择。默认工作区校验失败按错误处理,不静默采用列表首项。服务或令牌改变后默认值不复用。用户主动提供 ID 或链接时可内部解析并校验,随后显示实际名称。
|
|
13
|
+
2. 用户给出名称时调用 workspaces --query。接口是名称/介绍的子串检索,不代表精确匹配;只有检索完成后的唯一精确名称匹配才可直接采用并告知名称。多项、模糊匹配或未给名称时,展示序号、名称和服务端实际返回的简短介绍,让用户回复名称或序号。默认每次展示最多 5 项,可继续翻页或用关键词缩小范围。
|
|
14
|
+
3. 未保存默认工作区时,登录态、历史选择和列表第一项仅为候选;即使仅返回一项,也展示名称请用户选择。选定并校验后,询问“是否将「名称」设为 datarc 默认工作区?下次将自动使用。”用户同意才运行 set-default --workspace-id <真实 ID> --confirm yes;拒绝或未答复则只用于当前任务,不保存。已有默认工作区且用户本次切换到其他工作区时,询问是否更改默认值;拒绝或未回复保留原默认值。相同目标不重复询问。
|
|
15
|
+
4. 同名项使用实际介绍或用户提供的工作区链接辅助区分;仍不能区分时说明情况,让用户补充业务用途或从产品页面复制目标链接。不要猜测或要求用户寻找、填写工作区 ID。
|
|
16
|
+
5. 用户选定后调用 workspace 实时校验,内部固定服务地址、账号上下文和工作区 ID,并把名称用于确认消息。向下游脚本传入 workspace-id/project-id/workspaceId 等原有参数,不能把展示序号当作 ID。不调用 change-project 等写接口;保存默认值只修改当前会话工作区的凭据文件,不改变产品账号的当前工作区。
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
node scripts/workspace-context.mjs default
|
|
20
|
+
node scripts/workspace-context.mjs workspaces --offset 0 --limit 20
|
|
21
|
+
node scripts/workspace-context.mjs workspaces --query "销售" --offset 0 --limit 20
|
|
22
|
+
node scripts/workspace-context.mjs workspace --workspace-id 12
|
|
23
|
+
# 仅在用户明确同意后保存;命令会再次校验可访问性
|
|
24
|
+
node scripts/workspace-context.mjs set-default --workspace-id 12 --confirm yes
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
workspaces 保持返回列表结构;每页最多 100 项。返回满页时按 offset 加 limit 继续读取,直到不足一页,不能把第一页当成全量或据此断定名称唯一。一次定位最多读取 20 页;仍未取完时请用户缩小名称关键词,不自动选择。无关键词时不发送 q,不把完整业务问题当成工作区名。
|
|
28
|
+
|
|
29
|
+
空列表:说明当前账号未找到匹配工作区;有关键词时可改关键词或列出可访问项,无关键词仍为空时请用户检查账号或联系管理员加入工作区。认证失败按 datarc-oauth 处理;无权访问所选工作区时停止并提示核对账号/申请权限,不能自动替换。网络或服务异常时说明失败并提示稍后重试,不能当作“没有工作区”。
|
|
30
|
+
|
|
31
|
+
## 选择后读取上下文
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
node scripts/workspace-context.mjs get --workspace-id 12
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
get 返回 workspace 和 models;未提供 ID 时拒绝请求并提示先按名称选择,不再自动读取列表第一项。输出封装为 {"ok":true,"data":...},失败为 {"ok":false,"error":...} 且非零退出。
|
|
38
|
+
|
|
39
|
+
只使用返回的模型、维度和指标;维度样例不代表完整枚举。实际查询交给数据搜索技能。工作区可访问不等于有数据权限或管理权限:查询继续检查建模就绪及数据权限;配置继续使用业务技能的管理权限、dry-run 和写入确认。尚未配置数据源或模型的工作区也能按名称选择,不用 configReady 作为配置任务的选择门槛。
|
|
40
|
+
|
|
41
|
+
## 共享的卡片展示脚本
|
|
42
|
+
|
|
43
|
+
数据搜索和 DataSeek 会话搜索共用 `scripts/query-display.mjs`。脚本从已有卡片提取业务列名和查询口径,由两个搜索入口调用,无需发起请求。工作区选择仍使用 `scripts/workspace-context.mjs`。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
|
|
2
|
+
export function required(args, key) {
|
|
3
|
+
if (!args[key]) throw new Error(`缺少参数 --${key}`);
|
|
4
|
+
return args[key];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function createClient(options = {}) {
|
|
8
|
+
const { endpoint, token } = resolveCredentials(options);
|
|
9
|
+
if (!endpoint) throw new Error("未配置 DATARC_ENDPOINT");
|
|
10
|
+
if (!token) throw new Error("未配置 DATARC_TOKEN");
|
|
11
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
12
|
+
return {
|
|
13
|
+
async request(method, path, { query } = {}) {
|
|
14
|
+
const url = new URL(`${endpoint}${path}`);
|
|
15
|
+
for (const [key, value] of Object.entries(query ?? {})) url.searchParams.set(key, value);
|
|
16
|
+
const response = await fetchImpl(url, { method, headers: { Authorization: token } });
|
|
17
|
+
const payload = await response.json();
|
|
18
|
+
if (!response.ok || payload.status !== "SUCCESS") throw new Error(payload.message ?? `请求失败(HTTP ${response.status})`);
|
|
19
|
+
return payload.data;
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function runCli(main) {
|
|
25
|
+
try {
|
|
26
|
+
process.stdout.write(`${JSON.stringify({ ok: true, data: await main() }, null, 2)}\n`);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
process.stdout.write(`${JSON.stringify({ ok: false, error: { message: error.message } }, null, 2)}\n`);
|
|
29
|
+
process.exitCode = 1;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Shared by the two search skills through their workspace-context dependency.
|
|
2
|
+
export function queryDisplay(cards) {
|
|
3
|
+
return cards.map((card) => {
|
|
4
|
+
const cardSeries = card.data ?? [];
|
|
5
|
+
const explanations = card.explanations ?? [];
|
|
6
|
+
const series = cardSeries.map(({ columns }) => ({
|
|
7
|
+
columnNames: displayColumnNames(columns),
|
|
8
|
+
}));
|
|
9
|
+
// Follow OpenAPI's field recognition, using only this card's returned columns.
|
|
10
|
+
const returnedMeasures = new Set(
|
|
11
|
+
cardSeries
|
|
12
|
+
.flatMap(({ columns }) => columns ?? [])
|
|
13
|
+
.map(({ _nes }) => _nes ?? "")
|
|
14
|
+
.filter((field) => {
|
|
15
|
+
const prop = baseProp(field);
|
|
16
|
+
return (
|
|
17
|
+
/^m[1-9]\d*$/.test(prop) ||
|
|
18
|
+
(/^d[1-9]\d*$/.test(prop) && field.includes(".$"))
|
|
19
|
+
);
|
|
20
|
+
})
|
|
21
|
+
.map(baseProp),
|
|
22
|
+
);
|
|
23
|
+
const derivedMeasures = displayGroups(
|
|
24
|
+
returnedMeasureGroups(
|
|
25
|
+
explanations.flatMap(({ derive }) => derive ?? []),
|
|
26
|
+
returnedMeasures,
|
|
27
|
+
),
|
|
28
|
+
);
|
|
29
|
+
const scopes = explanations.map((scope) => ({
|
|
30
|
+
model: displayName(scope.model, scope.modelUid) || undefined,
|
|
31
|
+
columnNames: displayColumnNames(scope.columns),
|
|
32
|
+
measures: displayGroups(
|
|
33
|
+
returnedMeasureGroups(scope.measures ?? [], returnedMeasures),
|
|
34
|
+
),
|
|
35
|
+
dimensions: displayGroups(scope.dimensions),
|
|
36
|
+
dateFilters: displayDateFilters(scope.dateFilters),
|
|
37
|
+
filters: displayGroups(scope.filters),
|
|
38
|
+
having: displayGroups(scope.having),
|
|
39
|
+
order: displayGroups((scope.order ?? []).map(({ tokens }) => tokens)),
|
|
40
|
+
limit: displayGroups(scope.limit),
|
|
41
|
+
proportion: [
|
|
42
|
+
...new Set(
|
|
43
|
+
(scope.proportion ?? []).map(({ text }) => text).filter(Boolean),
|
|
44
|
+
),
|
|
45
|
+
],
|
|
46
|
+
}));
|
|
47
|
+
return { series, derivedMeasures, scopes };
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function displayColumnNames(columns) {
|
|
52
|
+
return (columns ?? []).map(({ _n, _nes }) => displayName(_n, _nes) || null);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function displayName(value, identifier) {
|
|
56
|
+
const text = value?.trim();
|
|
57
|
+
return text && text !== identifier ? text : "";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function displayGroups(groups) {
|
|
61
|
+
return uniqueDisplayValues(
|
|
62
|
+
(groups ?? [])
|
|
63
|
+
.map((tokens) =>
|
|
64
|
+
tokens.flatMap((token) => {
|
|
65
|
+
const name = displayName(token.word, token.prop);
|
|
66
|
+
return name ? [token.prefix, name].filter(Boolean) : [];
|
|
67
|
+
}),
|
|
68
|
+
)
|
|
69
|
+
.filter((group) => group.length),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function displayDateFilters(groups) {
|
|
74
|
+
const filters = [];
|
|
75
|
+
for (const tokens of groups ?? []) {
|
|
76
|
+
let timeDimension;
|
|
77
|
+
let expression = [];
|
|
78
|
+
const ranges = [];
|
|
79
|
+
for (const { word, prop, onlyText, start, end } of tokens) {
|
|
80
|
+
const text = displayName(word, prop);
|
|
81
|
+
if (!onlyText && /^t(?:0|[1-9]\d*)$/.test(prop)) {
|
|
82
|
+
timeDimension = text || undefined;
|
|
83
|
+
} else if (onlyText && ((start && end) || text)) {
|
|
84
|
+
const actualRange = start && end ? { start, end } : { text };
|
|
85
|
+
ranges.push(
|
|
86
|
+
expression.length ? { expression, ...actualRange } : actualRange,
|
|
87
|
+
);
|
|
88
|
+
expression = [];
|
|
89
|
+
} else if (!onlyText && text) {
|
|
90
|
+
expression.push(text);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (expression.length) ranges.push({ expression });
|
|
94
|
+
if (timeDimension || ranges.length) filters.push({ timeDimension, ranges });
|
|
95
|
+
}
|
|
96
|
+
return uniqueDisplayValues(filters);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function uniqueDisplayValues(values) {
|
|
100
|
+
return [
|
|
101
|
+
...new Map(values.map((value) => [JSON.stringify(value), value])).values(),
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function returnedMeasureGroups(groups, returnedMeasures) {
|
|
106
|
+
if (!returnedMeasures.size) return groups;
|
|
107
|
+
return groups.filter((tokens) =>
|
|
108
|
+
tokens.some(({ prop }) => returnedMeasures.has(baseProp(prop))),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function baseProp(prop) {
|
|
113
|
+
return prop?.split(".", 1)[0];
|
|
114
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { resolveCredentials, saveDefaultWorkspace } from "../../datarc-oauth/scripts/credentials.mjs";
|
|
2
|
+
import { createClient, runCli } from "./datarc-client.mjs";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
export async function execute(argv, options = {}) {
|
|
6
|
+
const [command, ...args] = argv;
|
|
7
|
+
const allowed = command === "workspaces" ? ["query", "offset", "limit"] : command === "set-default" ? ["workspace-id", "confirm"] : ["workspace-id"];
|
|
8
|
+
if (!["workspaces", "workspace", "get", "default", "set-default"].includes(command))
|
|
9
|
+
throw new Error("命令无效;请使用 workspaces 列出名称、workspace 校验所选工作区或 get 读取上下文");
|
|
10
|
+
const params = {};
|
|
11
|
+
for (let i = 0; i < args.length; i += 2) {
|
|
12
|
+
const key = args[i].slice(2);
|
|
13
|
+
if (
|
|
14
|
+
!args[i].startsWith("--") ||
|
|
15
|
+
!allowed.includes(key) ||
|
|
16
|
+
Object.hasOwn(params, key) ||
|
|
17
|
+
args[i + 1] === undefined ||
|
|
18
|
+
args[i + 1].startsWith("--")
|
|
19
|
+
)
|
|
20
|
+
throw new Error("参数缺失、重复或未知;请按技能中的命令示例补齐参数");
|
|
21
|
+
params[key] = args[i + 1];
|
|
22
|
+
}
|
|
23
|
+
if (command === "workspaces") {
|
|
24
|
+
const offset = integer(params.offset ?? "0", 0, Number.MAX_SAFE_INTEGER, "offset");
|
|
25
|
+
const limit = integer(params.limit ?? "20", 1, 100, "limit");
|
|
26
|
+
const client = options.client ?? createClient(options);
|
|
27
|
+
return client.request("GET", "/api/v2/account-projects/", {
|
|
28
|
+
query: { ...(params.query === undefined ? {} : { q: params.query }), offset, limit },
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
if (command === "default") {
|
|
32
|
+
if (Object.keys(params).length) throw new Error("default 不接受工作区参数;请直接调用 default 或使用 workspace 校验指定目标");
|
|
33
|
+
const saved = resolveCredentials(options).defaultWorkspace;
|
|
34
|
+
if (!saved) return null;
|
|
35
|
+
const client = options.client ?? createClient(options);
|
|
36
|
+
return client.request("GET", "/api/v2/account-projects/" + saved.id + "/");
|
|
37
|
+
}
|
|
38
|
+
if (command === "set-default" && params.confirm !== "yes") throw new Error("尚未确认保存默认工作区;请先取得用户明确同意,再传入 --confirm yes");
|
|
39
|
+
if (!params["workspace-id"]) throw new Error("尚未选择工作区;请先运行 workspaces 读取名称列表,让用户按名称或序号选择,再内部传入 --workspace-id");
|
|
40
|
+
const workspaceId = integer(params["workspace-id"], 1, Number.MAX_SAFE_INTEGER, "workspace-id");
|
|
41
|
+
const client = options.client ?? createClient(options);
|
|
42
|
+
const path = "/api/v2/account-projects/" + workspaceId + "/";
|
|
43
|
+
if (command === "set-default") {
|
|
44
|
+
const workspace = await client.request("GET", path);
|
|
45
|
+
if (Number(workspace?.id) !== workspaceId) throw new Error("工作区响应不匹配;请重新选择并校验工作区");
|
|
46
|
+
saveDefaultWorkspace({ id: workspaceId, name: workspace.name }, options);
|
|
47
|
+
return { id: workspaceId, name: workspace.name, saved: true };
|
|
48
|
+
}
|
|
49
|
+
return command === "workspace" ? client.request("GET", path) : client.request("GET", path + "agent-context/", { query: { format: "json" } });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function integer(value, min, max, name) {
|
|
53
|
+
if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) < min || Number(value) > max)
|
|
54
|
+
throw new Error(name + " 参数无效;请使用 " + min + " 至 " + max + " 的整数,工作区编号应来自名称列表");
|
|
55
|
+
return Number(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) await runCli(() => execute(process.argv.slice(2)));
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: datarc-workspace-custom-tables
|
|
3
|
+
description: 在北极九章(datarc)工作区创建和维护 SQL 自定义表,支持试跑 SQL、查看字段与样例、修改 SQL、改名及模型引用影响检查。用于准备可复用的查询来源;不创建数据库物理表,不写入业务数据。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Datarc 自定义表配置
|
|
7
|
+
|
|
8
|
+
工作区尚未确定时,先按 [datarc-workspace-context](../datarc-workspace-context/SKILL.md) 读取可访问工作区名称;不明确时列出名称让用户按名称或序号选择,不要求用户提供工作区 ID。选定后实时校验并内部传入原有 ID 参数;同一服务和账号下已确认的工作区可复用,原有权限检查与写入确认不变。
|
|
9
|
+
|
|
10
|
+
用 `scripts/workspace-custom-tables.mjs` 调用平台已有接口。输入 SQL 为明文,脚本负责编码、试跑、保存和回读验证。需要 Node.js 18+。
|
|
11
|
+
|
|
12
|
+
## 前置与范围
|
|
13
|
+
|
|
14
|
+
- 按 `datarc-oauth` 配置 `DATARC_ENDPOINT`、`DATARC_TOKEN`;令牌仅来自运行环境,不写入输入文件或命令参数。
|
|
15
|
+
- 使用明确的整数工作区 ID。尚未明确时按上述名称选择流程定位,由技能内部解析 ID。要求工作区管理员或负责人权限,数据源须支持自定义表。
|
|
16
|
+
- 本技能管理平台内的 SQL 自定义表;模型字段绑定与关联继续使用 `datarc-workspace-modeling`。不提供删除、配置备份、自动回滚、数据库物理建表或业务数据写入。
|
|
17
|
+
- 明确来源表、字段、筛选、聚合及去重口径。业务规则不明时先询问;SQL 和多表关联等复杂变更先向用户展示简短方案与影响,确认后执行。样例数据不能证明关联键全局唯一。
|
|
18
|
+
|
|
19
|
+
## 查看与定位
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
node scripts/workspace-custom-tables.mjs --help
|
|
23
|
+
node scripts/workspace-custom-tables.mjs list --workspace-id 12 --query 销售 --offset 0 --limit 50
|
|
24
|
+
node scripts/workspace-custom-tables.mjs get --workspace-id 12 --table-uid <uid>
|
|
25
|
+
node scripts/workspace-custom-tables.mjs columns --workspace-id 12 --table-uid <uid>
|
|
26
|
+
node scripts/workspace-custom-tables.mjs preview --workspace-id 12 --table-uid <uid>
|
|
27
|
+
node scripts/workspace-custom-tables.mjs column-preview --workspace-id 12 --table-uid <uid> --column customer_id
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`list` 是分页模糊搜索,多个结果时让用户选择,不取首个。`get` 返回已解码的 `editable.sql`、引用影响和 `fingerprint`;引用包含尚未 ready 或已禁用模型及草稿,不能只看平台表列表中的有效模型摘要。`columns` 单独读取只标记 `verified:false`,不能用旧字段证明新 SQL 生效。
|
|
31
|
+
|
|
32
|
+
## 创建
|
|
33
|
+
|
|
34
|
+
输入文件示例:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"tableName": "销售汇总",
|
|
39
|
+
"sql": "SELECT customer_id, SUM(amount) AS amount FROM sales GROUP BY customer_id"
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
示例仅说明格式,真实表名和业务口径必须先确认。输入文件只允许 `tableName`、`sql`;不要从服务响应复制额外字段。
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
node scripts/workspace-custom-tables.mjs create --workspace-id 12 --input table.json --dry-run
|
|
47
|
+
node scripts/workspace-custom-tables.mjs create --workspace-id 12 --input table.json
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`--dry-run` 只读取现状、检查名称并生成计划,不保存或试跑 SQL。实际创建自动先执行与网页相同的 SQL 试跑任务,失败则不保存。可单独用 `validate --input query.json` 试跑 `{ "sql": "..." }`;试跑会执行只读查询并产生查询任务,不改变自定义表配置。
|
|
51
|
+
|
|
52
|
+
## 修改 SQL 与改名
|
|
53
|
+
|
|
54
|
+
更新文件只允许 `{ "sql": "..." }`,改名文件只允许 `{ "tableName": "..." }`。两者为独立操作,不把部分成功描述为全部完成。
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
node scripts/workspace-custom-tables.mjs update --workspace-id 12 --table-uid <uid> --input query.json --dry-run
|
|
58
|
+
node scripts/workspace-custom-tables.mjs update --workspace-id 12 --table-uid <uid> --input query.json --if-match <fingerprint>
|
|
59
|
+
node scripts/workspace-custom-tables.mjs rename --workspace-id 12 --table-uid <uid> --input name.json --dry-run
|
|
60
|
+
node scripts/workspace-custom-tables.mjs rename --workspace-id 12 --table-uid <uid> --input name.json --if-match <fingerprint>
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
使用本次 dry-run 返回的指纹;目标或引用变化后重新读取并核对方案。存在引用时,先展示受影响模型、维度表和草稿;用户明确接受影响后追加 `--allow-referenced`。检查字段删除、改名和类型变化是否破坏现有绑定,不能因为有该参数就忽略业务影响。
|
|
64
|
+
|
|
65
|
+
改名只验证名称保存,不宣称 SQL 或模型已通过验证。修改 SQL 会重新试跑,再次检查目标和引用,保存后等待字段名称、顺序及类型与试跑一致,并执行表数据预览。
|
|
66
|
+
|
|
67
|
+
## 验证与交给建模
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
node scripts/workspace-custom-tables.mjs verify --workspace-id 12 --table-uid <uid>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
只有 `verified:true` 才把返回的 `modelSource.tableUid` 交给建模技能。模型事实来源使用 `tableUid`,不同时提供 `table` 或 `sql`;新维度表使用 `definition.tableUid`。字段绑定、关联、模型 ready 和业务搜索验收仍由建模与数据搜索技能完成。
|
|
74
|
+
|
|
75
|
+
验证意味着当前 SQL 试跑、字段结构与样例查询可用,不意味着整个模型已同步、业务口径正确或历史后台任务全部结束。旧服务没有字段解析版本号,脚本不提供并发写入锁;避免多人同时编辑同一表,发现漂移即停止。
|
|
76
|
+
|
|
77
|
+
默认整个命令等待最多 5 分钟,`--max-wait-ms` 可调整至最多 30 分钟;`--poll-interval-ms` 默认 2 秒。接口单次最多等 70 秒且受总预算限制。试跑等待异常时尝试取消该查询任务,取消请求不代表数据库立即停止。
|
|
78
|
+
|
|
79
|
+
## 结果与失败处理
|
|
80
|
+
|
|
81
|
+
- 输出结构化 JSON;错误返回非零退出码。给用户只展示表名、变更、验证结论、影响及下一步,SQL/样例按实际需要提供,不整段转贴原始响应。
|
|
82
|
+
- `saved:true` 但验证失败:配置已经写入,保留表 UID,排查后运行 `verify`;不重复创建,不自动回滚。
|
|
83
|
+
- `writeOutcome:unknown`:请求结果不确定,先通过 list/get 核对现状,禁止直接重发写请求。
|
|
84
|
+
- 权限不足、数据源不支持、试跑失败、字段未就绪、预览异常和并发变化分别说明;不伪装成功。
|
|
85
|
+
- 用户中断进程时已提交操作可能仍在运行,下一次先回读目标,不把进程退出等同于撤销。
|
|
86
|
+
|
|
87
|
+
## 配置方案与验收报告
|
|
88
|
+
|
|
89
|
+
复杂或跨资源配置先使用 [配置方案与验收](../datarc-workspace-configuration/SKILL.md) 生成方案文档,用户确认该版本后执行;配置回读及就绪后按方案真实查询并生成验收报告。保留本技能的专门确认和验证门槛。简单明确修改无需额外长方案;缺少相关技能时说明依赖,不把未执行的验证写成通过。
|
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
|
|
55
|
+
function redact(value) {
|
|
56
|
+
return String(value)
|
|
57
|
+
.replaceAll(token, "***")
|
|
58
|
+
.replace(
|
|
59
|
+
/(password|passwd|pwd|token|authorization)\s*[:=]\s*[^\s,;]+/gi,
|
|
60
|
+
"$1=***",
|
|
61
|
+
)
|
|
62
|
+
.replace(/:\/\/[^\s/@]+:[^\s/@]+@/g, "://***@")
|
|
63
|
+
.slice(0, 1500);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function request(
|
|
67
|
+
method,
|
|
68
|
+
path,
|
|
69
|
+
{ query, body, timeoutMs = 70000 } = {},
|
|
70
|
+
) {
|
|
71
|
+
const url = new URL(`${endpoint}${path}`);
|
|
72
|
+
for (const [key, value] of Object.entries(query ?? {}))
|
|
73
|
+
for (const item of [].concat(value ?? []))
|
|
74
|
+
if (item !== "") url.searchParams.append(key, item);
|
|
75
|
+
let response;
|
|
76
|
+
let text;
|
|
77
|
+
try {
|
|
78
|
+
response = await fetchImpl(url, {
|
|
79
|
+
method,
|
|
80
|
+
redirect: "error",
|
|
81
|
+
signal: AbortSignal.timeout(Math.max(1, Math.floor(timeoutMs))),
|
|
82
|
+
headers: {
|
|
83
|
+
Authorization: token,
|
|
84
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
85
|
+
},
|
|
86
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
87
|
+
});
|
|
88
|
+
text = await response.text();
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new DatarcError(
|
|
91
|
+
`网络请求失败或超时:${redact(error.message)}`,
|
|
92
|
+
"NETWORK_ERROR",
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
let payload;
|
|
96
|
+
try {
|
|
97
|
+
payload = text ? JSON.parse(text) : {};
|
|
98
|
+
} catch {
|
|
99
|
+
throw new DatarcError(
|
|
100
|
+
`服务返回非 JSON(HTTP ${response.status})`,
|
|
101
|
+
"INVALID_RESPONSE",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
if (!response.ok || (payload.status && payload.status !== "SUCCESS"))
|
|
105
|
+
throw new DatarcError(
|
|
106
|
+
redact(payload.message || `请求失败(HTTP ${response.status})`),
|
|
107
|
+
apiErrorCode(response.status),
|
|
108
|
+
{
|
|
109
|
+
httpStatus: response.status,
|
|
110
|
+
status: payload.status,
|
|
111
|
+
},
|
|
112
|
+
);
|
|
113
|
+
return payload.data;
|
|
114
|
+
}
|
|
115
|
+
return { request, redact };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function encryptSql(value) {
|
|
119
|
+
const first = Buffer.from(String(value), "utf8").toString("base64url");
|
|
120
|
+
const second = Buffer.from([...first].reverse().join(""), "utf8").toString(
|
|
121
|
+
"base64url",
|
|
122
|
+
);
|
|
123
|
+
return [...second].reverse().join("");
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function decryptSql(value) {
|
|
127
|
+
const second = [...String(value)].reverse().join("");
|
|
128
|
+
const firstReversed = Buffer.from(second, "base64url").toString("utf8");
|
|
129
|
+
const first = [...firstReversed].reverse().join("");
|
|
130
|
+
return Buffer.from(first, "base64url").toString("utf8");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function errorJson(error) {
|
|
134
|
+
return {
|
|
135
|
+
code: error?.code ?? "UNEXPECTED_ERROR",
|
|
136
|
+
message: error?.message ?? String(error),
|
|
137
|
+
...(error?.details === undefined ? {} : { details: error.details }),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export async function runCli(main) {
|
|
142
|
+
try {
|
|
143
|
+
process.stdout.write(
|
|
144
|
+
`${JSON.stringify({ ok: true, data: await main() }, null, 2)}\n`,
|
|
145
|
+
);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
process.stdout.write(
|
|
148
|
+
`${JSON.stringify({ ok: false, error: errorJson(error) }, null, 2)}\n`,
|
|
149
|
+
);
|
|
150
|
+
process.exitCode = 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function apiErrorCode(status) {
|
|
155
|
+
if (status === 401) return "AUTHENTICATION_FAILED";
|
|
156
|
+
if (status === 403) return "PERMISSION_DENIED";
|
|
157
|
+
if (status === 429) return "RATE_LIMITED";
|
|
158
|
+
return "API_ERROR";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
async function readStdin() {
|
|
162
|
+
const chunks = [];
|
|
163
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
164
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
165
|
+
}
|