@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,130 @@
|
|
|
1
|
+
import { createInterface } from "node:readline/promises";
|
|
2
|
+
import { Writable } from "node:stream";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { credentialsPath, resolveCredentials, saveCredentials } from "./credentials.mjs";
|
|
5
|
+
|
|
6
|
+
export async function execute(argv, options = {}) {
|
|
7
|
+
const [command, ...extra] = argv;
|
|
8
|
+
if (extra.length || !["status", "login"].includes(command))
|
|
9
|
+
throw new Error("命令无效;请使用 status 检查配置或 login 从标准输入接收认证 JSON");
|
|
10
|
+
let saved;
|
|
11
|
+
if (command === "login") {
|
|
12
|
+
let data;
|
|
13
|
+
try {
|
|
14
|
+
const input = options.input ?? (await readInput());
|
|
15
|
+
data = JSON.parse(input);
|
|
16
|
+
} catch {
|
|
17
|
+
throw new Error("认证输入无效;请通过安全输入提交包含 endpoint 和 token 的 JSON");
|
|
18
|
+
}
|
|
19
|
+
// New login input takes precedence over ambient credentials, without persisting a failed login.
|
|
20
|
+
const current = resolveCredentials({
|
|
21
|
+
...options,
|
|
22
|
+
endpoint: data?.endpoint,
|
|
23
|
+
token: data?.token,
|
|
24
|
+
});
|
|
25
|
+
if (
|
|
26
|
+
typeof data?.endpoint !== "string" ||
|
|
27
|
+
typeof data?.token !== "string" ||
|
|
28
|
+
!data.endpoint ||
|
|
29
|
+
!data.token.trim() ||
|
|
30
|
+
/[\r\n]/.test(data.token)
|
|
31
|
+
)
|
|
32
|
+
throw new Error("认证信息不完整;请提供服务网址和登录令牌");
|
|
33
|
+
const response = await (options.fetchImpl ?? fetch)(
|
|
34
|
+
`${current.endpoint}/api/v2/account-projects/?limit=1`,
|
|
35
|
+
{
|
|
36
|
+
headers: { Authorization: current.token },
|
|
37
|
+
redirect: "error",
|
|
38
|
+
signal: AbortSignal.timeout(30000),
|
|
39
|
+
},
|
|
40
|
+
).catch(() => {
|
|
41
|
+
throw new Error("无法连接 datarc;请检查服务网址和网络后重试");
|
|
42
|
+
});
|
|
43
|
+
let payload;
|
|
44
|
+
try {
|
|
45
|
+
payload = await response.json();
|
|
46
|
+
} catch {
|
|
47
|
+
throw new Error("服务响应无效;请检查服务网址后重试");
|
|
48
|
+
}
|
|
49
|
+
if (!response.ok || payload?.status !== "SUCCESS")
|
|
50
|
+
throw new Error(
|
|
51
|
+
`登录校验失败(HTTP ${response.status});请核对业务用户令牌及服务网址后重试`,
|
|
52
|
+
);
|
|
53
|
+
try {
|
|
54
|
+
saveCredentials(current, options);
|
|
55
|
+
saved = current;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return {
|
|
58
|
+
authenticated: true,
|
|
59
|
+
saved: false,
|
|
60
|
+
endpoint: current.endpoint,
|
|
61
|
+
message: "登录校验成功,但凭据未保存,后续调用仍需认证。" + error.message,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const current = resolveCredentials(options);
|
|
66
|
+
return {
|
|
67
|
+
...(saved
|
|
68
|
+
? {
|
|
69
|
+
authenticated: true,
|
|
70
|
+
saved: true,
|
|
71
|
+
active: saved.endpoint === current.endpoint && saved.token === current.token,
|
|
72
|
+
...(saved.endpoint !== current.endpoint || saved.token !== current.token
|
|
73
|
+
? {
|
|
74
|
+
message:
|
|
75
|
+
"凭据已保存,但当前环境变量覆盖了新配置;请更新或删除旧变量并重启宿主后使用新登录",
|
|
76
|
+
}
|
|
77
|
+
: {}),
|
|
78
|
+
}
|
|
79
|
+
: {}),
|
|
80
|
+
configPath: options.configPath ?? credentialsPath(options.env),
|
|
81
|
+
endpoint: current.endpoint,
|
|
82
|
+
hasToken: Boolean(current.token),
|
|
83
|
+
defaultWorkspace: current.defaultWorkspace
|
|
84
|
+
? { id: current.defaultWorkspace.id, name: current.defaultWorkspace.name }
|
|
85
|
+
: null,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function readInput() {
|
|
90
|
+
if (process.stdin.isTTY) {
|
|
91
|
+
let hidden = false;
|
|
92
|
+
const output = new Writable({
|
|
93
|
+
write(chunk, encoding, done) {
|
|
94
|
+
if (!hidden) process.stdout.write(chunk, encoding);
|
|
95
|
+
done();
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
const terminal = createInterface({
|
|
99
|
+
input: process.stdin,
|
|
100
|
+
output,
|
|
101
|
+
terminal: true,
|
|
102
|
+
historySize: 0,
|
|
103
|
+
});
|
|
104
|
+
try {
|
|
105
|
+
const endpoint = await terminal.question("datarc 服务网址:");
|
|
106
|
+
process.stdout.write("业务用户登录令牌(输入不显示):");
|
|
107
|
+
hidden = true;
|
|
108
|
+
const token = await terminal.question("");
|
|
109
|
+
process.stdout.write("\n");
|
|
110
|
+
return JSON.stringify({ endpoint, token });
|
|
111
|
+
} finally {
|
|
112
|
+
terminal.close();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let input = "";
|
|
116
|
+
for await (const chunk of process.stdin) {
|
|
117
|
+
input += chunk;
|
|
118
|
+
if (input.length > 65536) throw new Error();
|
|
119
|
+
}
|
|
120
|
+
return input;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
124
|
+
try {
|
|
125
|
+
console.log(JSON.stringify({ ok: true, data: await execute(process.argv.slice(2)) }));
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.log(JSON.stringify({ ok: false, error: { message: error.message } }));
|
|
128
|
+
process.exitCode = 1;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
readFileSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
writeFileSync,
|
|
6
|
+
renameSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
lstatSync,
|
|
9
|
+
appendFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
12
|
+
|
|
13
|
+
export function credentialsPath(env = process.env, cwd = process.cwd()) {
|
|
14
|
+
if (env.DATARC_CONFIG_FILE) {
|
|
15
|
+
if (!isAbsolute(env.DATARC_CONFIG_FILE))
|
|
16
|
+
throw new Error("DATARC_CONFIG_FILE 必须为绝对路径;请指定当前工作区允许访问的配置文件");
|
|
17
|
+
return env.DATARC_CONFIG_FILE;
|
|
18
|
+
}
|
|
19
|
+
const workspace = env.DATARC_WORKSPACE_DIR || cwd;
|
|
20
|
+
if (!isAbsolute(workspace))
|
|
21
|
+
throw new Error("DATARC_WORKSPACE_DIR 必须为绝对路径;请使用当前会话工作区的根目录");
|
|
22
|
+
return join(workspace, ".datarc", "config.toml");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function normalizeEndpoint(value) {
|
|
26
|
+
if (!value) return "";
|
|
27
|
+
try {
|
|
28
|
+
const url = new URL(String(value).split("#")[0]);
|
|
29
|
+
if (!["http:", "https:"].includes(url.protocol) || url.username || url.password || url.search)
|
|
30
|
+
throw new Error();
|
|
31
|
+
return url.href.replace(/\/+$/, "");
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error("服务地址无效;请提供不含账号、密码或查询参数的 http(s) 服务网址");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function readCredentials(options = {}) {
|
|
38
|
+
const path = options.configPath ?? credentialsPath(options.env);
|
|
39
|
+
try {
|
|
40
|
+
const stat = lstatSync(path);
|
|
41
|
+
if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 65536) throw new Error();
|
|
42
|
+
if (process.platform !== "win32" && stat.mode & 0o077) throw new Error();
|
|
43
|
+
const data = parseConfig(readFileSync(path, "utf8"));
|
|
44
|
+
if (
|
|
45
|
+
!data ||
|
|
46
|
+
Array.isArray(data) ||
|
|
47
|
+
data.version !== 1 ||
|
|
48
|
+
typeof data.endpoint !== "string" ||
|
|
49
|
+
typeof data.token !== "string"
|
|
50
|
+
)
|
|
51
|
+
throw new Error();
|
|
52
|
+
if (!normalizeEndpoint(data.endpoint) || !data.token.trim() || /[\r\n]/.test(data.token))
|
|
53
|
+
throw new Error();
|
|
54
|
+
if (data.defaultWorkspace !== undefined) {
|
|
55
|
+
const workspace = data.defaultWorkspace;
|
|
56
|
+
if (
|
|
57
|
+
!workspace ||
|
|
58
|
+
!Number.isSafeInteger(workspace.id) ||
|
|
59
|
+
workspace.id < 1 ||
|
|
60
|
+
typeof workspace.name !== "string" ||
|
|
61
|
+
!workspace.name.trim() ||
|
|
62
|
+
typeof workspace.binding !== "string"
|
|
63
|
+
)
|
|
64
|
+
throw new Error();
|
|
65
|
+
}
|
|
66
|
+
return data;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
if (error.code === "ENOENT") return {};
|
|
69
|
+
throw new Error(
|
|
70
|
+
"无法读取 datarc 凭据文件;请检查 TOML 格式及文件权限(macOS/Linux 应为 600),或移走损坏文件后重新登录",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function resolveCredentials(options = {}) {
|
|
76
|
+
const env = options.env ?? process.env;
|
|
77
|
+
let stored;
|
|
78
|
+
try {
|
|
79
|
+
stored = readCredentials(options);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
// Complete legacy credentials remain usable when the sandbox cannot read the optional file.
|
|
82
|
+
if (!(options.endpoint ?? env.DATARC_ENDPOINT) || !(options.token ?? env.DATARC_TOKEN))
|
|
83
|
+
throw error;
|
|
84
|
+
stored = {};
|
|
85
|
+
}
|
|
86
|
+
const endpoint = normalizeEndpoint(options.endpoint ?? (env.DATARC_ENDPOINT || stored.endpoint));
|
|
87
|
+
// Never send the stored credential to a different service selected by an override.
|
|
88
|
+
const token = String(
|
|
89
|
+
options.token ??
|
|
90
|
+
(env.DATARC_TOKEN || (endpoint === normalizeEndpoint(stored.endpoint) ? stored.token : "")) ??
|
|
91
|
+
"",
|
|
92
|
+
);
|
|
93
|
+
const binding = credentialBinding(endpoint, token);
|
|
94
|
+
const defaultWorkspace =
|
|
95
|
+
stored.defaultWorkspace?.binding === binding ? stored.defaultWorkspace : undefined;
|
|
96
|
+
return { endpoint, token, defaultWorkspace };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function saveCredentials(credentials, options = {}) {
|
|
100
|
+
const endpoint = normalizeEndpoint(credentials.endpoint);
|
|
101
|
+
const token = credentials.token;
|
|
102
|
+
if (!endpoint || typeof token !== "string" || !token.trim() || /[\r\n]/.test(token))
|
|
103
|
+
throw new Error("认证信息不完整;请提供服务网址和有效登录令牌");
|
|
104
|
+
const previous = readCredentials(options);
|
|
105
|
+
const binding = credentialBinding(endpoint, token);
|
|
106
|
+
const data = { version: 1, endpoint, token };
|
|
107
|
+
if (previous.defaultWorkspace?.binding === binding)
|
|
108
|
+
data.defaultWorkspace = previous.defaultWorkspace;
|
|
109
|
+
writeCredentials(data, options);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function saveDefaultWorkspace(workspace, options = {}) {
|
|
113
|
+
readCredentials(options);
|
|
114
|
+
const current = resolveCredentials(options);
|
|
115
|
+
if (!current.endpoint || !current.token)
|
|
116
|
+
throw new Error("尚未登录;请先完成 datarc 登录后设置默认工作区");
|
|
117
|
+
if (
|
|
118
|
+
!Number.isSafeInteger(workspace.id) ||
|
|
119
|
+
workspace.id < 1 ||
|
|
120
|
+
typeof workspace.name !== "string" ||
|
|
121
|
+
!workspace.name.trim()
|
|
122
|
+
)
|
|
123
|
+
throw new Error("工作区信息无效;请先按名称选择并校验工作区");
|
|
124
|
+
const data = {
|
|
125
|
+
version: 1,
|
|
126
|
+
endpoint: current.endpoint,
|
|
127
|
+
token: current.token,
|
|
128
|
+
defaultWorkspace: {
|
|
129
|
+
id: workspace.id,
|
|
130
|
+
name: workspace.name,
|
|
131
|
+
binding: credentialBinding(current.endpoint, current.token),
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
writeCredentials(data, options);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function credentialBinding(endpoint, token) {
|
|
138
|
+
return createHash("sha256")
|
|
139
|
+
.update(JSON.stringify([endpoint, token]))
|
|
140
|
+
.digest("hex");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// This configuration only contains integer and single-line string fields.
|
|
144
|
+
// Reject unsupported TOML constructs instead of interpreting them ambiguously.
|
|
145
|
+
function parseConfig(source) {
|
|
146
|
+
const data = {};
|
|
147
|
+
let section = data;
|
|
148
|
+
for (const line of source.split(/\r?\n/)) {
|
|
149
|
+
if (/^\s*(?:#.*)?$/.test(line)) continue;
|
|
150
|
+
if (/^\s*\[defaultWorkspace\]\s*(?:#.*)?$/.test(line)) {
|
|
151
|
+
if (data.defaultWorkspace) throw new Error();
|
|
152
|
+
section = data.defaultWorkspace = {};
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const entry = /^\s*([A-Za-z]+)\s*=\s*("(?:[^"\\]|\\.)*"|'[^']*'|[+-]?(?:0|[1-9](?:_?\d)*))\s*(?:#.*)?$/.exec(line);
|
|
156
|
+
if (!entry) throw new Error();
|
|
157
|
+
const [, key, raw] = entry;
|
|
158
|
+
const keys = section === data ? ["version", "endpoint", "token"] : ["id", "name", "binding"];
|
|
159
|
+
if (!keys.includes(key) || Object.hasOwn(section, key)) throw new Error();
|
|
160
|
+
if (raw.startsWith('"')) {
|
|
161
|
+
// JSON and TOML share these escapes; TOML additionally supports \UXXXXXXXX.
|
|
162
|
+
const json = raw.replace(/\\(U[\da-fA-F]{8}|u[\da-fA-F]{4}|[\s\S])/g, (_, escape) => {
|
|
163
|
+
if (escape.startsWith("U"))
|
|
164
|
+
return JSON.stringify(String.fromCodePoint(parseInt(escape.slice(1), 16))).slice(1, -1);
|
|
165
|
+
if (/^u[\da-fA-F]{4}$/.test(escape) || '"\\bfnrt'.includes(escape)) return "\\" + escape;
|
|
166
|
+
throw new Error();
|
|
167
|
+
}).replace(/\t/g, "\\t");
|
|
168
|
+
section[key] = JSON.parse(json);
|
|
169
|
+
} else if (raw.startsWith("'")) {
|
|
170
|
+
section[key] = raw.slice(1, -1);
|
|
171
|
+
} else {
|
|
172
|
+
section[key] = Number(raw.replaceAll("_", ""));
|
|
173
|
+
if (!Number.isSafeInteger(section[key])) throw new Error();
|
|
174
|
+
}
|
|
175
|
+
if (typeof section[key] === "string" && /[\uD800-\uDFFF]/u.test(section[key])) throw new Error();
|
|
176
|
+
if (/[\x00-\x08\x0a-\x1f\x7f]/.test(raw)) throw new Error();
|
|
177
|
+
}
|
|
178
|
+
return data;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function tomlString(value) {
|
|
182
|
+
return JSON.stringify(value).replace(/\x7f/g, "\\u007f");
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function serializeConfig(data) {
|
|
186
|
+
const lines = [
|
|
187
|
+
`version = ${data.version}`,
|
|
188
|
+
`endpoint = ${tomlString(data.endpoint)}`,
|
|
189
|
+
`token = ${tomlString(data.token)}`,
|
|
190
|
+
];
|
|
191
|
+
if (data.defaultWorkspace) {
|
|
192
|
+
const { id, name, binding } = data.defaultWorkspace;
|
|
193
|
+
lines.push("", "[defaultWorkspace]", `id = ${id}`, `name = ${tomlString(name)}`, `binding = ${tomlString(binding)}`);
|
|
194
|
+
}
|
|
195
|
+
return lines.join("\n") + "\n";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function writeCredentials(data, options) {
|
|
199
|
+
const path = options.configPath ?? credentialsPath(options.env);
|
|
200
|
+
const dir = dirname(path);
|
|
201
|
+
const temp = join(dir, `.datarc-${randomUUID()}.tmp`);
|
|
202
|
+
try {
|
|
203
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
204
|
+
ensureIgnored(path);
|
|
205
|
+
writeFileSync(temp, serializeConfig(data), { mode: 0o600, flag: "wx" });
|
|
206
|
+
renameSync(temp, path);
|
|
207
|
+
} catch {
|
|
208
|
+
throw new Error("无法保存 datarc 配置;请检查当前工作区配置目录的写入权限后重试");
|
|
209
|
+
} finally {
|
|
210
|
+
try {
|
|
211
|
+
rmSync(temp, { force: true });
|
|
212
|
+
} catch {
|
|
213
|
+
throw new Error(
|
|
214
|
+
"无法清理临时凭据文件;请检查配置目录权限并清理残留的 .datarc-*.tmp 文件后重试",
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function ensureIgnored(path) {
|
|
221
|
+
const name = basename(path);
|
|
222
|
+
if (name === ".gitignore" || /[\r\n]/.test(name))
|
|
223
|
+
throw new Error("配置文件名称无效;请使用 config.toml");
|
|
224
|
+
const ignored = "/" + name.replace(/[\\*?\[\]#! ]/g, "\\$&");
|
|
225
|
+
const ignorePath = join(dirname(path), ".gitignore");
|
|
226
|
+
const rules = ignored + "\n/.datarc-*.tmp\n";
|
|
227
|
+
try {
|
|
228
|
+
writeFileSync(ignorePath, rules, { flag: "wx", mode: 0o600 });
|
|
229
|
+
} catch (error) {
|
|
230
|
+
if (error.code !== "EEXIST") throw error;
|
|
231
|
+
const info = lstatSync(ignorePath);
|
|
232
|
+
if (!info.isFile() || info.isSymbolicLink())
|
|
233
|
+
throw new Error("Git 忽略规则不是普通文件;请检查配置目录后重试");
|
|
234
|
+
const current = readFileSync(ignorePath, "utf8");
|
|
235
|
+
if (!current.endsWith(rules)) appendFileSync(ignorePath, "\n" + rules);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: datarc-report-add-card
|
|
3
|
+
description: 将北极九章(datarc)已有查询结果中的一张数据卡保存到可编辑报告,或新建报告后加入并返回链接。用于“把刚才第二张图放进新周报”等明确保存现有结果的请求。
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Datarc 报告加卡
|
|
7
|
+
|
|
8
|
+
工作区尚未确定时,先按 [datarc-workspace-context](../datarc-workspace-context/SKILL.md) 读取可访问工作区名称;不明确时列出名称让用户按名称或序号选择,不要求用户提供工作区 ID。选定后实时校验并内部传入原有 ID 参数;同一服务和账号下已确认的工作区可复用,原有权限检查与写入确认不变。
|
|
9
|
+
|
|
10
|
+
把数据搜索或 DataSeek 刚刚产出的数据卡片加入报告。Agent 负责确认用户指的是哪张卡片、组织报告选择与命名交互;`scripts/report-card.mjs` 负责合并推荐、组装请求、添加前后回读验证和构造报告链接。所有命令输出 `{"ok":true,...}` 或 `{"ok":false,"error":{code,message,details}}`,令牌只从环境变量读取,不得出现在参数、日志或回复中。
|
|
11
|
+
|
|
12
|
+
## 前置条件
|
|
13
|
+
|
|
14
|
+
1. 按 [datarc-oauth](../datarc-oauth/SKILL.md) 配置并确认 `DATARC_ENDPOINT` 与 `DATARC_TOKEN`。
|
|
15
|
+
2. Node.js 18+。
|
|
16
|
+
3. 待加入的卡片来自 `datarc-data-search` 或 `datarc-dataseek-search` 的查询结果;本技能不查询数据。
|
|
17
|
+
4. `--project-id` 使用上一步搜索的同一个工作区 id(整数);缺失时先用 datarc-workspace-context 按名称选择,不向用户索要 ID,不得猜测。
|
|
18
|
+
5. 在本技能目录执行 `node scripts/report-card.mjs <命令>`。
|
|
19
|
+
|
|
20
|
+
## 标准流程
|
|
21
|
+
|
|
22
|
+
### 1. 保存待加入的卡片
|
|
23
|
+
|
|
24
|
+
普通问数直接使用选中子问题的 `resultFile` 作为 `--card-file`;DataSeek 会话搜索或旧版普通问数,把上一步搜索命令的完整 stdout JSON 写入临时文件(如 `report-card-input.json`)传给 `--card-file`(支持 `-` 从标准输入读取)。必须保存包含 `answer`(及其 `tokens`)的层级;单独的卡片对象无法还原问题,脚本会报 `CARD_TOKENS_MISSING`。
|
|
25
|
+
|
|
26
|
+
先用本地 `select` 检查待写入对象,尤其在新建报告前完成选卡:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
node scripts/report-card.mjs select --card-file report-card-input.json [--result-index <结果序号>] [--card-index <卡片序号>]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
只有一张候选直接使用;名称或“第二张图”等指代能唯一定位时,按结果内的卡片位置传对应序号(从 0 开始)。多张仍符合时,展示候选问题和标题等用户选择。将选定的两个序号原样传给后续 `add`。
|
|
33
|
+
|
|
34
|
+
### 2. 推荐报告
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
node scripts/report-card.mjs list --project-id <工作区id> [--q 名称关键词] [--limit 10]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
脚本合并"最近使用"(30 天内访问,优先)与"可编辑的全部报告"(按更新时间兜底),只保留当前用户可编辑的报告并标注 `recent`。
|
|
41
|
+
|
|
42
|
+
向用户列出前 3 个推荐(最近使用优先)加一个「新建报告」选项。用户要的报告不在选项里时,先用 `--q` 按名称关键词再查;选择新建时用自由文本收集报告名称(1–255 个字符),可顺带询问一句话介绍。
|
|
43
|
+
|
|
44
|
+
### 3a. 加入已有报告
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
node scripts/report-card.mjs add --project-id <工作区id> --pinboard-uid <报告uid> --card-file report-card-input.json --result-index <已选结果序号> --card-index <已选卡片序号>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
脚本自动执行:添加前读取报告 → 一次 add-to-pinboards → 添加后回读,并要求恰好新增一张卡片 pin 且签名(tokens、卡片类型、模型、标题)匹配;否则报 `CARD_VERIFY_FAILED`,不会重试添加。序号越界报 `CARD_INDEX_OUT_OF_RANGE`,`details.candidates` 列出全部候选及标题,据此修正参数后重跑读取步骤。
|
|
51
|
+
|
|
52
|
+
### 3b. 新建报告并加入
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
node scripts/report-card.mjs create --project-id <工作区id> --name "<报告名称>" [--desc "<一句话介绍>"]
|
|
56
|
+
node scripts/report-card.mjs add --project-id <工作区id> --pinboard-uid <新报告uid> --card-file report-card-input.json --result-index <已选结果序号> --card-index <已选卡片序号>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
新报告已创建但加卡未确认时,返回已创建的报告链接,说明卡片状态并先回读核对。
|
|
60
|
+
|
|
61
|
+
### 4. 返回结果
|
|
62
|
+
|
|
63
|
+
成功输出报告 uid、名称、卡片 pin id 与报告链接 `${DATARC_ENDPOINT}/#/pinboard/<报告uid>?projectId=<工作区id>`。向用户报告"已加入《报告名》"、报告链接和卡片标题;新建报告时明确说明已新建。
|
|
64
|
+
|
|
65
|
+
## 错误处理
|
|
66
|
+
|
|
67
|
+
- 认证、权限、工作区与服务错误按 [datarc-oauth](../datarc-oauth/SKILL.md) 的错误表处理。
|
|
68
|
+
- 本技能特有错误码:
|
|
69
|
+
|
|
70
|
+
| code | 含义与处置 |
|
|
71
|
+
| ------------------------- | -------------------------------------------------- |
|
|
72
|
+
| `CARD_SELECTION_REQUIRED` | 多张卡片符合条件;按候选问题和标题选定后再写入 |
|
|
73
|
+
| `CARD_TOKENS_MISSING` | 输入缺少 answer.tokens;改为保存完整搜索结果 JSON |
|
|
74
|
+
| `NO_CARDS` | 输入中没有卡片;先执行数据搜索 |
|
|
75
|
+
| `CARD_INDEX_OUT_OF_RANGE` | 序号越界;按 details.candidates 修正后重跑 |
|
|
76
|
+
| `CARD_VERIFY_FAILED` | 回读未确认恰好新增一张卡;停止并向用户说明,不重试 |
|
|
77
|
+
| `INVALID_NAME` | 报告名称必须是 1–255 个字符 |
|
|
78
|
+
|
|
79
|
+
- HTTP 403 常见原因是无报告编辑权限或报告数量达到上限;停止写操作并向用户说明,不得改加入其他报告或重试。
|
|
80
|
+
|
|
81
|
+
## 能力边界
|
|
82
|
+
|
|
83
|
+
- 可以:把两个搜索技能返回的单张数据卡加入当前用户可编辑的报告;新建私有报告并加入;回读验证;返回报告链接。
|
|
84
|
+
- 不可以:查询或生成数据(先用 datarc-data-search 或 datarc-dataseek-search);加入富文本、标题或章节,也不生成多章节分析报告(使用 datarc-analysis-report);移动、排版、删除已有卡片或修改报告布局与分享设置;一次把同一张卡加入多个报告(接口支持,本技能暂未开放)。
|
|
85
|
+
|
|
86
|
+
## 结果呈现
|
|
87
|
+
|
|
88
|
+
- 先给结论(已加入《报告名》),再给报告链接和卡片标题;新建报告时明确说明已新建。
|
|
89
|
+
- 凭据始终保密。正常回复只展示报告链接和卡片标题;排查选卡时可展示候选问题、标题和序号。
|
|
90
|
+
- 验证加入时的报告回读会记为一次访问,该报告因此会排在下次"最近使用"推荐前列;需要时向用户说明即可。
|