@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,526 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, isAbsolute, relative, resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import {
|
|
6
|
+
createClient,
|
|
7
|
+
isGlobalError,
|
|
8
|
+
parseArgs,
|
|
9
|
+
required,
|
|
10
|
+
runCli,
|
|
11
|
+
} from "./datarc-client.mjs";
|
|
12
|
+
|
|
13
|
+
const STATUS_COLUMN = "生成_执行状态";
|
|
14
|
+
const ERROR_COLUMN = "生成_错误简述";
|
|
15
|
+
|
|
16
|
+
export function parseCsv(text) {
|
|
17
|
+
const rows = [];
|
|
18
|
+
let row = [];
|
|
19
|
+
let field = "";
|
|
20
|
+
let quoted = false;
|
|
21
|
+
const source = String(text).replace(/^\uFEFF/, "");
|
|
22
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
23
|
+
const character = source[index];
|
|
24
|
+
if (quoted) {
|
|
25
|
+
if (character === '"' && source[index + 1] === '"') {
|
|
26
|
+
field += '"';
|
|
27
|
+
index += 1;
|
|
28
|
+
} else if (character === '"') quoted = false;
|
|
29
|
+
else field += character;
|
|
30
|
+
} else if (character === '"' && field === "") quoted = true;
|
|
31
|
+
else if (character === ",") {
|
|
32
|
+
row.push(field);
|
|
33
|
+
field = "";
|
|
34
|
+
} else if (character === "\n" || character === "\r") {
|
|
35
|
+
if (character === "\r" && source[index + 1] === "\n") index += 1;
|
|
36
|
+
row.push(field);
|
|
37
|
+
rows.push(row);
|
|
38
|
+
row = [];
|
|
39
|
+
field = "";
|
|
40
|
+
} else field += character;
|
|
41
|
+
}
|
|
42
|
+
if (quoted) throw new Error("CSV 存在未闭合的引号");
|
|
43
|
+
if (field !== "" || row.length) rows.push([...row, field]);
|
|
44
|
+
return rows.filter((item, index) => index === 0 || item.some(Boolean));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function stringifyCsv(rows) {
|
|
48
|
+
return `${rows.map((row) => row.map(csvField).join(",")).join("\r\n")}\r\n`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function extractFinalTokens(actions) {
|
|
52
|
+
const candidates = [];
|
|
53
|
+
visit(actions, (value) => {
|
|
54
|
+
if (value?.type === "answer" && Array.isArray(value.data?.tokens))
|
|
55
|
+
candidates.push(value.data.tokens);
|
|
56
|
+
});
|
|
57
|
+
return candidates.at(-1) ?? null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function extractUnderstanding(message) {
|
|
61
|
+
const step = [...(message.history ?? [])].reverse().find(
|
|
62
|
+
(item) =>
|
|
63
|
+
item?.name === "理解问题" || item?.name === "Understand the problem",
|
|
64
|
+
);
|
|
65
|
+
if (!step) throw new Error("DataSeek 消息历史中缺少理解问题步骤");
|
|
66
|
+
const candidates = [
|
|
67
|
+
step.reasoning_content ?? step.reasoningContent,
|
|
68
|
+
Array.isArray(step.reasoning_content)
|
|
69
|
+
? step.reasoning_content.join("")
|
|
70
|
+
: undefined,
|
|
71
|
+
Array.isArray(step.reasoningContent)
|
|
72
|
+
? step.reasoningContent.join("")
|
|
73
|
+
: undefined,
|
|
74
|
+
step.result,
|
|
75
|
+
step.content,
|
|
76
|
+
Array.isArray(step.content) ? step.content.join("") : undefined,
|
|
77
|
+
];
|
|
78
|
+
for (const value of candidates) {
|
|
79
|
+
const result = findUnderstanding(value);
|
|
80
|
+
if (result) return result;
|
|
81
|
+
}
|
|
82
|
+
throw new Error("DataSeek 理解问题步骤没有可解析的结构化 JSON");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function tokensToWords(tokens) {
|
|
86
|
+
const words = [];
|
|
87
|
+
visit(tokens, (value) => {
|
|
88
|
+
if (typeof value?.word === "string" && value.word.trim())
|
|
89
|
+
words.push(value.word.trim());
|
|
90
|
+
});
|
|
91
|
+
return words.join(" ");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function generate(args, options = {}) {
|
|
95
|
+
const inputPath = resolve(process.cwd(), required(args, "input"));
|
|
96
|
+
const outputPath = safeOutputFile(required(args, "output"));
|
|
97
|
+
if (inputPath.toLowerCase() === outputPath.toLowerCase())
|
|
98
|
+
throw new Error("--output 不得覆盖输入 CSV");
|
|
99
|
+
const workspaceId = required(args, "workspace-id");
|
|
100
|
+
const names = {
|
|
101
|
+
question: required(args, "question-column"),
|
|
102
|
+
understanding: args["understanding-column"] ?? "理解JSON",
|
|
103
|
+
tokens: args["tokens-column"] ?? "问题Token",
|
|
104
|
+
tokenWords: args["token-words-column"] ?? "问题TokenWord",
|
|
105
|
+
};
|
|
106
|
+
validateColumnRoles(names);
|
|
107
|
+
const rows = parseCsv(await readFile(inputPath, "utf8"));
|
|
108
|
+
if (!rows.length)
|
|
109
|
+
throw new Error("CSV 为空;请确认输入文件路径、内容和 UTF-8 编码后重试");
|
|
110
|
+
const header = rows[0];
|
|
111
|
+
if (new Set(header).size !== header.length)
|
|
112
|
+
throw new Error("CSV 表头存在重复列名;请将每列改为唯一名称后重试");
|
|
113
|
+
if (!header.includes(names.question))
|
|
114
|
+
throw new Error(`CSV 缺少问题列:${names.question}`);
|
|
115
|
+
const outputHeader = [...header];
|
|
116
|
+
insertColumnAfter(outputHeader, names.understanding, names.question);
|
|
117
|
+
insertColumnAfter(outputHeader, names.tokens, names.understanding);
|
|
118
|
+
insertColumnAfter(outputHeader, names.tokenWords, names.tokens);
|
|
119
|
+
for (const name of [STATUS_COLUMN, ERROR_COLUMN])
|
|
120
|
+
if (!outputHeader.includes(name)) outputHeader.push(name);
|
|
121
|
+
const client = options.client ?? createClient(options);
|
|
122
|
+
const intervalMs = positiveInteger(
|
|
123
|
+
args["poll-interval-ms"] ?? 2000,
|
|
124
|
+
"poll-interval-ms",
|
|
125
|
+
true,
|
|
126
|
+
);
|
|
127
|
+
const maxWaitMs = positiveInteger(
|
|
128
|
+
args["max-wait-ms"] ?? 600000,
|
|
129
|
+
"max-wait-ms",
|
|
130
|
+
);
|
|
131
|
+
const outputRows = [outputHeader];
|
|
132
|
+
await writeNewFile(outputPath, stringifyCsv(outputRows));
|
|
133
|
+
let success = 0;
|
|
134
|
+
let error = 0;
|
|
135
|
+
for (const values of rows.slice(1)) {
|
|
136
|
+
const source = Object.fromEntries(
|
|
137
|
+
header.map((name, index) => [name, values[index] ?? ""]),
|
|
138
|
+
);
|
|
139
|
+
const question = String(source[names.question] ?? "").trim();
|
|
140
|
+
let result;
|
|
141
|
+
try {
|
|
142
|
+
if (!question) throw new Error("原始问题为空");
|
|
143
|
+
result = await collectExpected(client, workspaceId, question, {
|
|
144
|
+
intervalMs,
|
|
145
|
+
maxWaitMs,
|
|
146
|
+
});
|
|
147
|
+
success += 1;
|
|
148
|
+
} catch (caught) {
|
|
149
|
+
if (isGlobalError(caught)) throw caught;
|
|
150
|
+
result = { status: "error", error: caught.message ?? String(caught) };
|
|
151
|
+
error += 1;
|
|
152
|
+
}
|
|
153
|
+
const output = Object.fromEntries(
|
|
154
|
+
outputHeader.map((name) => [name, source[name] ?? ""]),
|
|
155
|
+
);
|
|
156
|
+
output[names.understanding] = jsonCell(result.understanding);
|
|
157
|
+
output[names.tokens] = jsonCell(result.tokens);
|
|
158
|
+
output[names.tokenWords] = result.tokenWords ?? "";
|
|
159
|
+
output[STATUS_COLUMN] = result.status;
|
|
160
|
+
output[ERROR_COLUMN] = result.error ?? "";
|
|
161
|
+
outputRows.push(outputHeader.map((name) => output[name]));
|
|
162
|
+
await atomicWrite(outputPath, stringifyCsv(outputRows));
|
|
163
|
+
}
|
|
164
|
+
return { output: outputPath, total: rows.length - 1, success, error };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function snapshotWorkspace(args, options = {}) {
|
|
168
|
+
const workspaceId = required(args, "workspace-id");
|
|
169
|
+
const outputPath = safeOutputFile(required(args, "output"));
|
|
170
|
+
const valuesPerDimension = boundedInteger(
|
|
171
|
+
args["values-per-dimension"] ?? 3,
|
|
172
|
+
"values-per-dimension",
|
|
173
|
+
1,
|
|
174
|
+
20,
|
|
175
|
+
);
|
|
176
|
+
const maxDimensions = boundedInteger(
|
|
177
|
+
args["max-dimensions"] ?? 30,
|
|
178
|
+
"max-dimensions",
|
|
179
|
+
1,
|
|
180
|
+
200,
|
|
181
|
+
);
|
|
182
|
+
const maxCardinality = boundedInteger(
|
|
183
|
+
args["max-cardinality"] ?? 1000,
|
|
184
|
+
"max-cardinality",
|
|
185
|
+
1,
|
|
186
|
+
100000,
|
|
187
|
+
);
|
|
188
|
+
const client = options.client ?? createClient(options);
|
|
189
|
+
const context = await client.request(
|
|
190
|
+
"GET",
|
|
191
|
+
`/api/v2/account-projects/${workspaceId}/agent-context/`,
|
|
192
|
+
{ query: { format: "json" } },
|
|
193
|
+
);
|
|
194
|
+
const dimensions = uniqueDimensions(context.models ?? []).slice(
|
|
195
|
+
0,
|
|
196
|
+
maxDimensions,
|
|
197
|
+
);
|
|
198
|
+
const samples = [];
|
|
199
|
+
for (const dimension of dimensions) {
|
|
200
|
+
const body = {
|
|
201
|
+
prop: dimension.prop,
|
|
202
|
+
q: "",
|
|
203
|
+
count: 1,
|
|
204
|
+
tuids: dimension.modelUids,
|
|
205
|
+
};
|
|
206
|
+
try {
|
|
207
|
+
const count = await client.request(
|
|
208
|
+
"POST",
|
|
209
|
+
`/api/v2/account-projects/${workspaceId}/dimension-keywords/`,
|
|
210
|
+
{ body },
|
|
211
|
+
);
|
|
212
|
+
if (!Number.isFinite(Number(count)) || Number(count) > maxCardinality) {
|
|
213
|
+
samples.push({
|
|
214
|
+
...dimension,
|
|
215
|
+
count: Number.isFinite(Number(count)) ? Number(count) : null,
|
|
216
|
+
values: [],
|
|
217
|
+
status: "skipped",
|
|
218
|
+
reason: Number.isFinite(Number(count))
|
|
219
|
+
? `维度值数量超过 ${maxCardinality}`
|
|
220
|
+
: "维度值数量无效",
|
|
221
|
+
});
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const tokens = await client.request(
|
|
225
|
+
"POST",
|
|
226
|
+
`/api/v2/account-projects/${workspaceId}/dimension-keywords/`,
|
|
227
|
+
{
|
|
228
|
+
body: {
|
|
229
|
+
...body,
|
|
230
|
+
count: undefined,
|
|
231
|
+
offset: 0,
|
|
232
|
+
limit: valuesPerDimension,
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
);
|
|
236
|
+
samples.push({
|
|
237
|
+
...dimension,
|
|
238
|
+
count: Number(count),
|
|
239
|
+
values: uniqueWords(tokens),
|
|
240
|
+
status: "success",
|
|
241
|
+
});
|
|
242
|
+
} catch (error) {
|
|
243
|
+
if (isGlobalError(error)) throw error;
|
|
244
|
+
samples.push({
|
|
245
|
+
...dimension,
|
|
246
|
+
count: null,
|
|
247
|
+
values: [],
|
|
248
|
+
status: "error",
|
|
249
|
+
reason: error.message ?? String(error),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const snapshot = {
|
|
254
|
+
schemaVersion: 1,
|
|
255
|
+
workspaceId: String(workspaceId),
|
|
256
|
+
workspace: context.workspace ?? null,
|
|
257
|
+
models: withoutEmbeddedExamples(context.models ?? []),
|
|
258
|
+
dimensionSamples: samples,
|
|
259
|
+
limits: { valuesPerDimension, maxDimensions, maxCardinality },
|
|
260
|
+
sampledAt: new Date().toISOString(),
|
|
261
|
+
};
|
|
262
|
+
await atomicWrite(outputPath, `${JSON.stringify(snapshot, null, 2)}\n`);
|
|
263
|
+
return {
|
|
264
|
+
output: outputPath,
|
|
265
|
+
models: snapshot.models.length,
|
|
266
|
+
dimensions: samples.length,
|
|
267
|
+
sampled: samples.filter((item) => item.status === "success").length,
|
|
268
|
+
skipped: samples.filter((item) => item.status === "skipped").length,
|
|
269
|
+
errors: samples.filter((item) => item.status === "error").length,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export async function collectExpected(client, workspaceId, question, poll) {
|
|
274
|
+
const thread = await client.request(
|
|
275
|
+
"POST",
|
|
276
|
+
`/api/v3/account-projects/${workspaceId}/threads/`,
|
|
277
|
+
{ body: {} },
|
|
278
|
+
);
|
|
279
|
+
const message = await client.request(
|
|
280
|
+
"POST",
|
|
281
|
+
`/api/v3/account-projects/${workspaceId}/threads/${thread.uid}/messages/`,
|
|
282
|
+
{ body: { message: question } },
|
|
283
|
+
);
|
|
284
|
+
const completed = await waitForMessage(
|
|
285
|
+
client,
|
|
286
|
+
workspaceId,
|
|
287
|
+
thread.uid,
|
|
288
|
+
message.id,
|
|
289
|
+
poll,
|
|
290
|
+
);
|
|
291
|
+
const understanding = extractUnderstanding(completed);
|
|
292
|
+
const unsupportedType = unsupportedQueryType(completed.current?.result);
|
|
293
|
+
if (unsupportedType)
|
|
294
|
+
throw new Error(
|
|
295
|
+
`当前测试集只支持直接单 query,不能保存 ${unsupportedType} 查询;请改写为单个问题后重试`,
|
|
296
|
+
);
|
|
297
|
+
const tokens = extractFinalTokens(completed.current?.result);
|
|
298
|
+
if (!tokens) throw new Error("DataSeek 最终 answer 缺少问题 Token");
|
|
299
|
+
const tokenWords = tokensToWords(tokens);
|
|
300
|
+
if (!tokenWords) throw new Error("DataSeek 最终问题 Token 缺少 word");
|
|
301
|
+
return { status: "success", understanding, tokens, tokenWords };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export async function execute(argv, options = {}) {
|
|
305
|
+
const args = parseArgs(argv);
|
|
306
|
+
if (args.help === true || args._[0] === "help" || !args._[0]) return help();
|
|
307
|
+
if (args._[0] === "snapshot") return snapshotWorkspace(args, options);
|
|
308
|
+
if (args._[0] === "generate") return generate(args, options);
|
|
309
|
+
throw new Error("命令应为 snapshot 或 generate");
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
async function waitForMessage(client, workspaceId, threadUid, messageId, options) {
|
|
313
|
+
const started = Date.now();
|
|
314
|
+
while (Date.now() - started <= options.maxWaitMs) {
|
|
315
|
+
const message = await client.request(
|
|
316
|
+
"GET",
|
|
317
|
+
`/api/v3/account-projects/${workspaceId}/threads/${threadUid}/messages/${messageId}/`,
|
|
318
|
+
);
|
|
319
|
+
if (message.current?.error) throw new Error(String(message.current.error));
|
|
320
|
+
if (Array.isArray(message.current?.result) && message.current.result.length)
|
|
321
|
+
return message;
|
|
322
|
+
await client.sleep(options.intervalMs);
|
|
323
|
+
}
|
|
324
|
+
const error = new Error(
|
|
325
|
+
"DataSeek 消息轮询超时;请调大 --max-wait-ms 后重试",
|
|
326
|
+
);
|
|
327
|
+
error.code = "TASK_TIMEOUT";
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function safeOutputFile(value) {
|
|
332
|
+
if (typeof value !== "string" || !value || isAbsolute(value))
|
|
333
|
+
throw new Error("--output 必须是当前目录下的相对路径");
|
|
334
|
+
const root = resolve(process.cwd());
|
|
335
|
+
const target = resolve(root, value);
|
|
336
|
+
if (relative(root, target).startsWith(".."))
|
|
337
|
+
throw new Error("--output 不得超出当前目录");
|
|
338
|
+
return target;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function positiveInteger(value, name, allowZero = false) {
|
|
342
|
+
const number = Number(value);
|
|
343
|
+
if (!Number.isInteger(number) || number < (allowZero ? 0 : 1))
|
|
344
|
+
throw new Error(
|
|
345
|
+
`--${name} 必须是${allowZero ? "非负" : "正"}整数(当前值:${value});请修改参数后重试`,
|
|
346
|
+
);
|
|
347
|
+
return number;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function insertColumnAfter(header, name, precedingName) {
|
|
351
|
+
if (header.includes(name)) return;
|
|
352
|
+
const index = header.indexOf(precedingName);
|
|
353
|
+
header.splice(index < 0 ? header.length : index + 1, 0, name);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function validateColumnRoles(names) {
|
|
357
|
+
const roles = {
|
|
358
|
+
question: names.question,
|
|
359
|
+
understanding: names.understanding,
|
|
360
|
+
tokens: names.tokens,
|
|
361
|
+
tokenWords: names.tokenWords,
|
|
362
|
+
status: STATUS_COLUMN,
|
|
363
|
+
error: ERROR_COLUMN,
|
|
364
|
+
};
|
|
365
|
+
const seen = new Map();
|
|
366
|
+
for (const [role, rawName] of Object.entries(roles)) {
|
|
367
|
+
const name = String(rawName ?? "").trim();
|
|
368
|
+
if (!name) throw new Error(`${role} 列名不能为空`);
|
|
369
|
+
if (seen.has(name))
|
|
370
|
+
throw new Error(
|
|
371
|
+
`列名角色冲突:${seen.get(name)} 与 ${role} 均使用“${name}”`,
|
|
372
|
+
);
|
|
373
|
+
seen.set(name, role);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function boundedInteger(value, name, minimum, maximum) {
|
|
378
|
+
const number = Number(value);
|
|
379
|
+
if (!Number.isInteger(number) || number < minimum || number > maximum)
|
|
380
|
+
throw new Error(`--${name} 必须是 ${minimum} 到 ${maximum} 的整数`);
|
|
381
|
+
return number;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function uniqueDimensions(models) {
|
|
385
|
+
const dimensions = new Map();
|
|
386
|
+
for (const model of models) {
|
|
387
|
+
for (const dimension of model.dimensions ?? []) {
|
|
388
|
+
if (!Number.isInteger(Number(dimension.id))) continue;
|
|
389
|
+
const prop = `d${Number(dimension.id)}`;
|
|
390
|
+
const current = dimensions.get(prop) ?? {
|
|
391
|
+
id: Number(dimension.id),
|
|
392
|
+
prop,
|
|
393
|
+
name: dimension.name ?? prop,
|
|
394
|
+
description: dimension.description ?? null,
|
|
395
|
+
examples: [],
|
|
396
|
+
modelUids: [],
|
|
397
|
+
};
|
|
398
|
+
current.examples = [
|
|
399
|
+
...new Set([...current.examples, ...(dimension.examples ?? [])]),
|
|
400
|
+
];
|
|
401
|
+
if (model.uid && !current.modelUids.includes(model.uid))
|
|
402
|
+
current.modelUids.push(model.uid);
|
|
403
|
+
dimensions.set(prop, current);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return [...dimensions.values()].sort((left, right) => left.id - right.id);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function uniqueWords(tokens) {
|
|
410
|
+
return [
|
|
411
|
+
...new Set(
|
|
412
|
+
(Array.isArray(tokens) ? tokens : [])
|
|
413
|
+
.map((token) => String(token?.word ?? "").trim())
|
|
414
|
+
.filter(Boolean),
|
|
415
|
+
),
|
|
416
|
+
];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function withoutEmbeddedExamples(models) {
|
|
420
|
+
return models.map((model) => ({
|
|
421
|
+
...model,
|
|
422
|
+
dimensions: (model.dimensions ?? []).map(({ examples: _examples, ...item }) =>
|
|
423
|
+
item,
|
|
424
|
+
),
|
|
425
|
+
}));
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
async function atomicWrite(path, content) {
|
|
429
|
+
await mkdir(dirname(path), { recursive: true });
|
|
430
|
+
const temporary = `${path}.tmp-${process.pid}`;
|
|
431
|
+
await writeFile(temporary, content, "utf8");
|
|
432
|
+
await rename(temporary, path);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function writeNewFile(path, content) {
|
|
436
|
+
await mkdir(dirname(path), { recursive: true });
|
|
437
|
+
try {
|
|
438
|
+
await writeFile(path, content, { encoding: "utf8", flag: "wx" });
|
|
439
|
+
} catch (error) {
|
|
440
|
+
if (error?.code === "EEXIST")
|
|
441
|
+
throw new Error(
|
|
442
|
+
`输出文件已存在:${relative(process.cwd(), path)};请指定新的 --output 路径后重试`,
|
|
443
|
+
);
|
|
444
|
+
throw error;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function jsonCell(value) {
|
|
449
|
+
return value === undefined ? "" : JSON.stringify(value);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function visit(value, callback, seen = new Set()) {
|
|
453
|
+
if (!value || typeof value !== "object" || seen.has(value)) return;
|
|
454
|
+
seen.add(value);
|
|
455
|
+
callback(value);
|
|
456
|
+
for (const child of Array.isArray(value) ? value : Object.values(value))
|
|
457
|
+
visit(child, callback, seen);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function unsupportedQueryType(actions) {
|
|
461
|
+
let result = null;
|
|
462
|
+
visit(actions, (value) => {
|
|
463
|
+
if (!result && ["parallel", "chained"].includes(value?.type))
|
|
464
|
+
result = value.type;
|
|
465
|
+
});
|
|
466
|
+
return result;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function findUnderstanding(value) {
|
|
470
|
+
if (value === null || value === undefined) return null;
|
|
471
|
+
if (Array.isArray(value)) {
|
|
472
|
+
for (const item of [...value].reverse()) {
|
|
473
|
+
const result = findUnderstanding(item);
|
|
474
|
+
if (result) return result;
|
|
475
|
+
}
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
if (typeof value === "object") {
|
|
479
|
+
if (
|
|
480
|
+
Object.hasOwn(value, "dimensions") &&
|
|
481
|
+
Object.hasOwn(value, "metrics") &&
|
|
482
|
+
Object.hasOwn(value, "time_range")
|
|
483
|
+
)
|
|
484
|
+
return value;
|
|
485
|
+
for (const item of Object.values(value)) {
|
|
486
|
+
const result = findUnderstanding(item);
|
|
487
|
+
if (result) return result;
|
|
488
|
+
}
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
const text = String(value).trim();
|
|
492
|
+
const start = text.indexOf("{");
|
|
493
|
+
const end = text.lastIndexOf("}");
|
|
494
|
+
for (const candidate of [
|
|
495
|
+
text,
|
|
496
|
+
text.replace(/^```(?:json)?\s*/i, "").replace(/\s*```$/, ""),
|
|
497
|
+
start >= 0 && end > start ? text.slice(start, end + 1) : "",
|
|
498
|
+
]) {
|
|
499
|
+
if (!candidate) continue;
|
|
500
|
+
try {
|
|
501
|
+
const result = findUnderstanding(JSON.parse(candidate));
|
|
502
|
+
if (result) return result;
|
|
503
|
+
} catch {
|
|
504
|
+
// 尝试下一种包装形式。
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function csvField(value) {
|
|
511
|
+
const text = String(value ?? "");
|
|
512
|
+
return /[",\r\n]/.test(text) ? `"${text.replaceAll('"', '""')}"` : text;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function help() {
|
|
516
|
+
return {
|
|
517
|
+
usage: [
|
|
518
|
+
"snapshot --workspace-id <id> --output <relative-json> [--values-per-dimension <n>] [--max-dimensions <n>] [--max-cardinality <n>]",
|
|
519
|
+
"generate --workspace-id <id> --input <csv> --output <relative-csv> --question-column <name> [--understanding-column <name>] [--tokens-column <name>] [--token-words-column <name>]",
|
|
520
|
+
],
|
|
521
|
+
options: ["--poll-interval-ms <n>", "--max-wait-ms <n>"],
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
|
|
526
|
+
await runCli(() => execute(process.argv.slice(2)));
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: datarc-datasource
|
|
3
|
+
description: 安全配置、验证或切换北极九章(datarc)工作区的数据源。用户要求为指定名称的工作区配置数据库类型和连接信息时使用;在写入前读取现有配置、隐藏密码、明确说明替换影响并取得用户二次确认。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Datarc 数据源配置
|
|
7
|
+
|
|
8
|
+
工作区尚未确定时,先按 [datarc-workspace-context](../datarc-workspace-context/SKILL.md) 读取可访问工作区名称;不明确时列出名称让用户按名称或序号选择,不要求用户提供工作区 ID。选定后实时校验并内部传入原有 ID 参数;同一服务和账号下已确认的工作区可复用,原有权限检查与写入确认不变。
|
|
9
|
+
|
|
10
|
+
使用北极九章(datarc)内部 API 配置或切换现有工作区的数据源。数据源变更会影响该工作区的后续建模与查询。
|
|
11
|
+
|
|
12
|
+
> **保存硬性门槛:仅当同一份新配置已通过 `database-version` 连接验证,且用户已在看到该成功结果后对这次变更作出明确二次确认,才可调用 `POST .../datasource/` 保存。任一条件不满足、或候选配置在验证或确认后发生变化时,均禁止保存;必须以新配置重新验证并重新确认。**
|
|
13
|
+
|
|
14
|
+
## 前置条件
|
|
15
|
+
|
|
16
|
+
1. 先使用 [datarc-oauth](../datarc-oauth/SKILL.md) 检查 `DATARC_ENDPOINT` 和 `DATARC_TOKEN`。
|
|
17
|
+
2. 按名称确定工作区后,要求用户提供数据库类型、连接信息,以及(如需变更)数据库设置。
|
|
18
|
+
3. 使用当前用户身份调用内部 API;请求头为 `Authorization: ${DATARC_TOKEN}`,不加 `Bearer`。所有路径均以 `${DATARC_ENDPOINT}` 为 base URL。
|
|
19
|
+
4. 当前用户必须是工作区负责人。接口返回 HTTP 403 时停止,并说明需要负责人权限;不得尝试绕过权限。
|
|
20
|
+
|
|
21
|
+
## 收集与校验信息
|
|
22
|
+
|
|
23
|
+
将用户提供的信息整理为以下字段;不猜测缺失的主机、端口、数据库名、用户名、密码或数据源类型。
|
|
24
|
+
|
|
25
|
+
| 字段 | 请求字段 | 要求 |
|
|
26
|
+
| ----------------- | ------------------ | ------------------------------------------------------------------------------ |
|
|
27
|
+
| 数据库类型 | `adapter` | 必填,使用下表中的整数值 |
|
|
28
|
+
| URL(如用户提供) | `databaseUrl` | 仅在未提供 `databaseConfig` 时使用的字符串 |
|
|
29
|
+
| 连接参数 | `databaseConfig` | 首选;对象,常见键为 `HOST`、`PORT`、`USER`、`PASSWORD`、`NAME` |
|
|
30
|
+
| 查询设置 | `databaseSettings` | 可选对象;仅在用户要求变更时传入,例如 `{"timeout": 180, "queryResultTTL": 0}` |
|
|
31
|
+
|
|
32
|
+
常用 `adapter` 值:PostgreSQL `1`、ClickHouse `2`、MySQL 8.0 `3`、MySQL 5.7 `7`、Oracle `9`、SQL Server 2008 `10`、Apache Doris `14`、Greenplum `15`、SQL Server 2012 `16`、TiDB `18`、StarRocks `19`、DM8 `21`、SAP HANA `22`、Presto `23`、Impala `24`、MaxCompute `25`、DB2 `26`、GaussDB(DWS) `28`、Amazon Redshift `29`、SelectDB `30`、Trino `31`、Denodo `32`、JDBC(PostgreSQL) `35`、Databricks `36`。用户提及其他类型时,先要求其给出对应的 adapter 值,勿臆测。
|
|
33
|
+
|
|
34
|
+
每个候选连接只能使用 `databaseConfig` 或 `databaseUrl` **其中一个非空字段**,并且优先使用 `databaseConfig`:用户同时给出两者时,要求其确认或以 `databaseConfig` 为准,不能将两者一同用于连接验证。服务端执行器会优先使用非空 URL,因此同时传入会使 `databaseConfig` 不生效。若用户省略 `PASSWORD`,服务端会保留当前已保存的密码;若要改密码,用户必须明确提供新密码。
|
|
35
|
+
|
|
36
|
+
数据源保存接口使用 POST,但采用部分更新语义,未传字段会保留旧值。若读取到当前有非空 `databaseUrl`,但要改用 `databaseConfig`(或反向切换),在保存请求中额外传入另一字段的空值以清除旧值,例如 `{"databaseUrl": ""}`。该空值只用于清理旧配置,不是第二套连接信息;连接验证仍只使用新的非空字段,清理动作必须在二次确认中明确展示。
|
|
37
|
+
|
|
38
|
+
## 操作流程
|
|
39
|
+
|
|
40
|
+
1. 读取当前配置:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
GET /api/v2/admin-projects/{workspace_id}/datasource/
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
检查 HTTP 状态和响应的 `status` 字段。该响应不会返回密码;不要要求用户回显旧密码。
|
|
47
|
+
|
|
48
|
+
2. 构造候选请求体。除非用户要求更改设置,否则不要传 `databaseSettings`,以保留当前设置。
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"adapter": 1,
|
|
53
|
+
"databaseConfig": {
|
|
54
|
+
"HOST": "db.example.com",
|
|
55
|
+
"PORT": "5432",
|
|
56
|
+
"USER": "analyst",
|
|
57
|
+
"PASSWORD": "<仅在本次请求中使用>",
|
|
58
|
+
"NAME": "analytics"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
3. 先验证候选连接,**不得保存配置**:
|
|
64
|
+
|
|
65
|
+
```
|
|
66
|
+
POST /api/v2/admin-projects/{workspace_id}/database-version/
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
请求体只使用 `adapter` 加上选定的一个非空连接字段(优先 `databaseConfig`);不传另一连接字段,也不传 `databaseSettings`。HTTP 200 且 `status` 为 `SUCCESS` 表示可建立连接;记录数据库版本,但不得回显密码或令牌。若连接超时或失败,**不得调用数据源保存接口**,而是执行以下循环直到成功:
|
|
70
|
+
|
|
71
|
+
- 从服务端错误中提取不含敏感信息的原因;若可根据用户已提供的信息作出确定且安全的修正,则更新候选配置后重新验证。
|
|
72
|
+
- 若需要补充或选择信息(例如主机、端口、认证方式、密码、数据库名或 adapter),向用户说明错误和所需信息,等待回复后重新验证;不得猜测或编造连接参数。
|
|
73
|
+
- 每次重试只验证候选配置,不保存。只有取得成功的数据库版本后,才能进入下一步的二次确认。
|
|
74
|
+
|
|
75
|
+
4. 仅在第 3 步成功后进行二次确认。在确认消息中列出工作区名称、数据库类型、主机、端口、数据库名、用户名、已验证的数据库版本、是否会替换密码、是否会清除 URL、是否变更查询设置;所有密码及令牌均显示为 `***`。明确说明“连接已验证;确认后会立即替换该工作区的数据源配置,并影响后续查询和建模”。只有收到用户针对这次变更的肯定确认后才继续;含糊回复、补充信息或拒绝都不算确认。
|
|
76
|
+
|
|
77
|
+
5. **仅在全部保存硬性门槛满足后写入配置**:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
POST /api/v2/admin-projects/{workspace_id}/datasource/
|
|
81
|
+
Content-Type: application/json
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
仅提交第 3 步验证成功、且第 4 步已获确认的**同一份**候选连接信息;如需清理另一种旧连接字段,仅附加前述已确认的空值。用户要求变更 `databaseSettings` 时才附加该字段。成功响应为 HTTP 200 且 `status` 为 `SUCCESS`。失败时原样保留服务端错误信息,但不得在回复、日志或命令行中输出密码或令牌。
|
|
85
|
+
|
|
86
|
+
6. 验证保存结果。再次 `GET` 数据源接口,比对 `adapter`、非敏感连接字段、URL 和设置;密码不在返回中属于正常现象。报告第 3 步已经验证过的数据库版本。若读取结果与已确认的候选配置不一致,说明保存结果异常;不要自动重试、回滚或改写配置,除非用户再次明确要求。
|
|
87
|
+
|
|
88
|
+
## 安全边界
|
|
89
|
+
|
|
90
|
+
- 不将 `DATARC_TOKEN`、`PASSWORD` 或完整连接 URL 写入仓库、Skill 文件、命令历史、确认消息或最终报告。
|
|
91
|
+
- 每次写操作都重新读取当前配置并重新取得二次确认;不得复用先前会话的确认。
|
|
92
|
+
- 不通过此技能配置平台内置数据仓库(adapter `12`);该类型由平台管理,切换请求会被拒绝。
|
|
93
|
+
- 不修改工作区以外的资源,也不在未验证所有响应状态前宣称连接成功。
|
|
94
|
+
|
|
95
|
+
## 配置方案与验收报告
|
|
96
|
+
|
|
97
|
+
复杂或跨资源配置先使用 [配置方案与验收](../datarc-workspace-configuration/SKILL.md) 生成方案文档,用户确认该版本后执行;配置回读及就绪后按方案真实查询并生成验收报告。保留本技能的专门确认和验证门槛。简单明确修改无需额外长方案;缺少相关技能时说明依赖,不把未执行的验证写成通过。
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: datarc-oauth
|
|
3
|
+
description: 北极九章(datarc)业务用户登录、工作区级凭据配置与接口错误处理。优先复用环境变量或本地凭据文件;缺失时引导登录并保存服务入口和令牌,处理认证失效、权限不足及服务异常。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Datarc 登录认证
|
|
7
|
+
|
|
8
|
+
普通业务技能使用本技能的 `scripts/credentials.mjs` 统一解析认证;开放 API 访问密钥不使用此配置。认证头为 `Authorization: <token>`,不加 `Bearer`。
|
|
9
|
+
|
|
10
|
+
## 先检查配置
|
|
11
|
+
|
|
12
|
+
在当前会话工作区运行 `node <本技能绝对路径>/scripts/auth.mjs status`。输出仅含服务入口、是否存在令牌及默认工作区,不包含令牌。已有可用配置时直接使用,不重复询问登录,不读取或展示凭据文件全文。
|
|
13
|
+
|
|
14
|
+
读取优先级为脚本内部显式参数、非空环境变量 `DATARC_ENDPOINT` / `DATARC_TOKEN`、工作区配置文件。原有环境变量继续生效,无需迁移;不要求新用户手动设置环境变量。dsh 可能过滤子进程的 TOKEN 环境变量,脚本仍可读取工作区配置文件。环境变量被宿主过滤且从未保存过凭据时,需要首次登录,不能声称能从父进程恢复令牌。
|
|
15
|
+
|
|
16
|
+
默认位置统一为 `<当前会话工作区>/.datarc/config.toml`。同一文件夹被 dsh、Codex、WorkBuddy 或 Claude Code 打开时,复用同一份 endpoint、token 和默认 datarc 工作区;不同工作区目录分别保存。配置采用 TOML:根字段为 `version`、`endpoint`、`token`;默认工作区保存在 `[defaultWorkspace]` 表内,包含 `id`、`name`、`binding`。字符串使用单行基本字符串或字面量字符串,数值使用十进制整数,可添加注释。
|
|
17
|
+
|
|
18
|
+
工作区是当前工具会话实际绑定的工作目录,不是用户 HOME、技能安装目录或自动查找的 Git 根目录。Agent 在所有脚本调用中固定非敏感的 `DATARC_WORKSPACE_DIR=<会话工作区绝对路径>`,包括登录、工作区选择、搜索、报告及子进程;该值从本次会话上下文取得,不写死示例路径,无需用户配置。未设置时脚本使用当前进程工作目录,所以切换目录前必须先固定此值;优先从工作区使用绝对脚本路径调用。
|
|
19
|
+
|
|
20
|
+
可以用绝对路径 `DATARC_CONFIG_FILE` 显式指定已有配置文件,其优先级高于默认位置。默认直接使用当前工作区已有的写权限,不为保存到用户 HOME 额外申请授权;如果工作区实际不可写,如实报告未保存,不切换到其他目录或修改宿主权限。没有安全凭据输入时,引导用户在本机终端进入同一工作区后完成登录。
|
|
21
|
+
|
|
22
|
+
凭据只保存到该工作区配置文件,macOS/Linux 文件权限为 600,新建配置目录为 700。保存时先在凭据所在目录创建或补充局部 `.gitignore`,仅忽略该凭据文件和 `.datarc-*.tmp`,保留已有规则,不修改工作区根目录的 `.gitignore`。忽略规则无法写入时不保存令牌。文件为明文凭据,不得提交仓库或写入 Skill、普通文件、命令参数、shell 历史、日志或回复。不要通过重命名环境变量、读取父进程环境等方式绕过宿主凭据过滤。服务地址改变时不将文件中旧服务的令牌发送到新服务。
|
|
23
|
+
|
|
24
|
+
## 登录并保存
|
|
25
|
+
|
|
26
|
+
1. 缺少配置或收到 401 时,请用户提供服务网址,并使用**业务用户**账号登录北极九章(datarc),进入右上角头像 → 设置 → 登录管理 → 复制登录令牌。服务网址可以带 `#/workspace/...`,脚本会移除片段及末尾 `/`。
|
|
27
|
+
2. 告知用户:登录成功后会将服务入口和令牌保存到上述工作区配置文件,后续技能自动复用。通过工具提供的安全凭据输入,以标准输入提交含 `endpoint`、`token` 的 JSON 到 `node <本技能绝对路径>/scripts/auth.mjs login`;不要将真实令牌写成 shell 命令、here-document 或工具调用中的可见参数。
|
|
28
|
+
3. 若当前工具没有安全凭据输入,让用户在本机终端进入当前会话的同一工作区后运行 `node <本技能绝对路径>/scripts/auth.mjs login`。该命令交互式询问服务网址及令牌,令牌输入不显示,不需要配置环境变量。不要让用户把令牌粘贴到普通聊天。
|
|
29
|
+
4. login 使用可访问工作区列表接口只读校验业务用户身份;认证成功后原子保存,认证失败不覆盖已有文件。保存失败会返回 authenticated=true、saved=false;这只代表登录校验成功,后续独立调用仍无持久凭据,不能宣称已保存或后续必定可用。不在登录时替用户选择工作区。已有环境变量优先于文件;若旧环境变量与新登录冲突,说明应删除或更新旧变量并重启宿主,不能宣称新凭据已成为当前有效配置。
|
|
30
|
+
5. 登录后涉及工作区选择,交给同级 `datarc-workspace-context`;由其询问是否保存默认工作区。登录本身不默认同意保存工作区。
|
|
31
|
+
|
|
32
|
+
脚本只输出配置状态;不要回显令牌。文件不可读、格式损坏或权限不正确时按脚本提示处理,不覆盖损坏文件。
|
|
33
|
+
|
|
34
|
+
## 调用后的错误处理
|
|
35
|
+
|
|
36
|
+
实际接口调用同时检查 HTTP 状态和业务 `status`。错误输出脱敏,不能展示令牌、密码或完整连接信息。
|
|
37
|
+
|
|
38
|
+
| 返回 | 处理 |
|
|
39
|
+
| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
|
40
|
+
| 缺少 endpoint / token | 检查 status,再按登录流程收集缺失信息;不要求手工设置环境变量 |
|
|
41
|
+
| 401 或令牌/签名/会话失效 | 停止调用,请用户重新登录业务用户账号并保存新令牌;不要继续复用旧令牌 |
|
|
42
|
+
| 403 | 停止调用和写操作;请确认使用业务用户账号。已是业务用户时,请工作区负责人或管理员核对成员资格与角色,不反复要求更换令牌 |
|
|
43
|
+
| 工作区不存在或错误码 10001 / 10002 | 停止使用该目标;按工作区技能重新按名称选择,不自动替换默认工作区;不可见时请负责人将当前用户加入目标工作区 |
|
|
44
|
+
| 400 或参数校验失败 | 展示脱敏后的原因,修正参数后重试,不将其归因于令牌 |
|
|
45
|
+
| 超时、网络错误、5xx | 请用户检查服务网址或网络、稍后重试;不清除凭据和默认工作区,不当作认证失效 |
|
|
46
|
+
|
|
47
|
+
保存的新令牌或服务与原认证上下文不同,原默认工作区不再复用,需重新选择。工作区可访问不代表有数据或管理权限,继续按业务技能校验。
|