@arcticdata/dsh-datarc-assistant 26.39.0 → 26.39.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -10
- package/api-operations.js +404 -0
- package/client.js +452 -87
- package/cordis.patch.yml +4 -4
- package/entry/SKILL.md +34 -0
- package/icon.png +0 -0
- package/index.js +35 -47
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/native-api.mjs +326 -0
- package/native-contracts.js +195 -0
- package/native-execution.js +184 -0
- package/native-runner.mjs +107 -0
- package/native-state.js +67 -0
- package/native-tools.js +241 -0
- package/operations.js +1260 -0
- package/package.json +65 -51
- package/{skills → runtime}/datarc-access-permissions/SKILL.md +30 -30
- package/{skills → runtime}/datarc-access-permissions/references/api.md +31 -31
- package/{skills → runtime}/datarc-access-permissions/references/write-order.md +16 -16
- package/{skills → runtime}/datarc-analysis-report/SKILL.md +125 -125
- package/{skills → runtime}/datarc-analysis-report/agents/openai.yaml +6 -6
- package/{skills → runtime}/datarc-analysis-report/references/analyst-frameworks.md +215 -215
- package/{skills → runtime}/datarc-analysis-report/references/api-contracts.md +478 -478
- package/{skills → runtime}/datarc-analysis-report/references/direct-card-fallback-design.md +66 -66
- package/{skills → runtime}/datarc-analysis-report/references/human-in-the-loop.md +142 -142
- package/{skills → runtime}/datarc-analysis-report/references/loop-personalization-policy.md +138 -138
- package/{skills → runtime}/datarc-analysis-report/references/manifest-example.json +779 -779
- package/{skills → runtime}/datarc-analysis-report/references/narrative-schema.md +160 -160
- package/{skills → runtime}/datarc-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-analysis-report/scripts/run_analysis_report.py +2225 -2225
- package/{skills → runtime}/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -1914
- package/{skills → runtime}/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -5295
- package/{skills → runtime}/datarc-data-search/SKILL.md +243 -243
- package/{skills → runtime}/datarc-data-search/scripts/calculate.py +116 -116
- package/{skills → runtime}/datarc-data-search/scripts/data-search.mjs +628 -628
- package/{skills/datarc-dataseek-search → runtime/datarc-data-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-data-search/scripts/search-result.mjs +211 -211
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/SKILL.md +59 -59
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -36
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -30
- package/{skills/datarc-data-search → runtime/datarc-dataseek-batch-evaluation}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -1020
- package/{skills → runtime}/datarc-dataseek-search/SKILL.md +96 -96
- package/{skills/datarc-dataseek-batch-evaluation → runtime/datarc-dataseek-search}/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -257
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/SKILL.md +44 -44
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/evals/evals.json +165 -165
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/dataset.md +77 -77
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -93
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/interventions.md +64 -64
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -47
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/rollback.md +11 -11
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -95
- package/{skills → runtime}/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -29
- package/{skills → runtime}/datarc-dataseek-testset-generation/SKILL.md +47 -47
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -464
- package/{skills → runtime}/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -526
- package/{skills → runtime}/datarc-datasource/SKILL.md +97 -97
- package/{skills → runtime}/datarc-oauth/SKILL.md +50 -50
- package/{skills → runtime}/datarc-oauth/scripts/auth.mjs +192 -192
- package/{skills → runtime}/datarc-oauth/scripts/credentials.mjs +237 -237
- package/{skills → runtime}/datarc-report-add-card/SKILL.md +90 -90
- package/{skills → runtime}/datarc-report-add-card/agents/openai.yaml +4 -4
- package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/{skills → runtime}/datarc-report-add-card/scripts/report-card.mjs +439 -439
- package/{skills → runtime}/datarc-semantic-alias/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias-plan.mjs +422 -422
- package/{skills → runtime}/datarc-semantic-alias/scripts/alias.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-calibration/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -683
- package/{skills → runtime}/datarc-semantic-calibration/scripts/calibration.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-core/SKILL.md +12 -12
- package/{skills → runtime}/datarc-semantic-core/scripts/artifact-journal.mjs +853 -830
- package/{skills → runtime}/datarc-semantic-core/scripts/canonical.mjs +48 -48
- package/{skills → runtime}/datarc-semantic-core/scripts/case-model.mjs +608 -608
- package/{skills → runtime}/datarc-semantic-core/scripts/cli.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/datarc-client.mjs +180 -180
- package/{skills → runtime}/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -57
- package/{skills → runtime}/datarc-semantic-core/scripts/interventions.mjs +693 -693
- package/{skills → runtime}/datarc-semantic-core/scripts/live-resolution.mjs +305 -305
- package/{skills → runtime}/datarc-semantic-core/scripts/model-scope.mjs +49 -49
- package/{skills → runtime}/datarc-semantic-core/scripts/reproduction.mjs +330 -330
- package/{skills → runtime}/datarc-semantic-core/scripts/semantic-cli.mjs +799 -740
- package/{skills → runtime}/datarc-semantic-core/scripts/shared.mjs +312 -312
- package/{skills → runtime}/datarc-semantic-core/scripts/verification.mjs +1111 -1014
- package/{skills → runtime}/datarc-semantic-dataset/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -749
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -813
- package/{skills → runtime}/datarc-semantic-dataset/scripts/dataset.mjs +11 -11
- package/{skills → runtime}/datarc-semantic-knowledge/SKILL.md +38 -38
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -61
- package/{skills → runtime}/datarc-semantic-knowledge/references/knowledge-generation.json +16 -16
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -347
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -848
- package/{skills → runtime}/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -31
- package/{skills → runtime}/datarc-semantic-prompt/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -447
- package/{skills → runtime}/datarc-semantic-prompt/scripts/prompt.mjs +15 -15
- package/{skills → runtime}/datarc-semantic-synonym/SKILL.md +20 -20
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -688
- package/{skills → runtime}/datarc-semantic-synonym/scripts/synonym.mjs +11 -11
- package/{skills → runtime}/datarc-viz-analysis-report/SKILL.md +108 -108
- package/{skills → runtime}/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -8
- package/{skills → runtime}/datarc-viz-analysis-report/assets/echarts.min.js +45 -45
- package/{skills → runtime}/datarc-viz-analysis-report/references/api-contracts.md +163 -163
- package/{skills → runtime}/datarc-viz-analysis-report/references/derivation-log.md +157 -157
- package/{skills → runtime}/datarc-viz-analysis-report/references/evidence-framework.md +76 -76
- package/{skills → runtime}/datarc-viz-analysis-report/references/html-template.md +92 -92
- package/{skills → runtime}/datarc-viz-analysis-report/references/provenance-format.md +56 -56
- package/{skills → runtime}/datarc-viz-analysis-report/references/question-syntax.md +151 -151
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -1708
- package/{skills → runtime}/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -964
- package/{skills → runtime}/datarc-workspace-configuration/SKILL.md +59 -59
- package/{skills → runtime}/datarc-workspace-configuration/references/artifacts.md +75 -75
- package/{skills → runtime}/datarc-workspace-configuration/references/verification.md +20 -20
- package/{skills → runtime}/datarc-workspace-configuration/scripts/configuration.mjs +722 -662
- package/{skills → runtime}/datarc-workspace-context/SKILL.md +43 -43
- package/{skills → runtime}/datarc-workspace-context/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-context/scripts/datarc-client.mjs +31 -31
- package/{skills → runtime}/datarc-workspace-context/scripts/query-display.mjs +114 -114
- package/{skills → runtime}/datarc-workspace-context/scripts/workspace-context.mjs +58 -58
- package/{skills → runtime}/datarc-workspace-custom-tables/SKILL.md +89 -89
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -165
- package/{skills → runtime}/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -601
- package/{skills → runtime}/datarc-workspace-keywords/SKILL.md +197 -197
- package/{skills → runtime}/datarc-workspace-keywords/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-keywords/references/api-fields.md +79 -79
- package/{skills → runtime}/datarc-workspace-keywords/references/derived-measures.md +177 -177
- package/{skills → runtime}/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -145
- package/{skills → runtime}/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -2421
- package/{skills → runtime}/datarc-workspace-membership/SKILL.md +29 -29
- package/{skills → runtime}/datarc-workspace-membership/references/api.md +19 -19
- package/{skills → runtime}/datarc-workspace-modeling/SKILL.md +209 -209
- package/{skills → runtime}/datarc-workspace-modeling/agents/openai.yaml +4 -4
- package/{skills → runtime}/datarc-workspace-modeling/references/join-preview.md +38 -38
- package/{skills → runtime}/datarc-workspace-modeling/references/model-plan.md +300 -300
- package/{skills → runtime}/datarc-workspace-modeling/references/model-tags.md +95 -95
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-contract.json +29 -29
- package/{skills → runtime}/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -2423
- package/{skills → runtime}/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -137
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -238
- package/{skills → runtime}/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -1261
- package/skills-provider.js +51 -0
- package/workflow-index.json +434 -0
- package/icon.svg +0 -4
- package/skills/datarc-report-add-card/scripts/datarc-client.mjs +0 -464
- package/skills-index.json +0 -194
|
@@ -1,237 +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
|
-
}
|
|
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
|
+
}
|