@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,180 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
appendReservedArtifact,
|
|
6
|
+
completeCommandArtifact,
|
|
7
|
+
createCommandArtifactIdentity,
|
|
8
|
+
readJson,
|
|
9
|
+
reserveArtifact,
|
|
10
|
+
writeArtifact,
|
|
11
|
+
} from "./artifact-journal.mjs";
|
|
12
|
+
import {
|
|
13
|
+
createClient,
|
|
14
|
+
DatarcError,
|
|
15
|
+
errorJson,
|
|
16
|
+
parseArgs,
|
|
17
|
+
required,
|
|
18
|
+
} from "./datarc-client.mjs";
|
|
19
|
+
|
|
20
|
+
export async function runIfMain(importMetaUrl, run) {
|
|
21
|
+
const invokedPath = process.argv[1]
|
|
22
|
+
? (() => {
|
|
23
|
+
try {
|
|
24
|
+
return realpathSync(process.argv[1]);
|
|
25
|
+
} catch {
|
|
26
|
+
return process.argv[1];
|
|
27
|
+
}
|
|
28
|
+
})()
|
|
29
|
+
: "";
|
|
30
|
+
if (invokedPath && importMetaUrl === pathToFileURL(invokedPath).href) {
|
|
31
|
+
await run();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createCli({
|
|
36
|
+
commandNames,
|
|
37
|
+
flags,
|
|
38
|
+
help,
|
|
39
|
+
handlers,
|
|
40
|
+
localCommands = [],
|
|
41
|
+
requiresOutput = [],
|
|
42
|
+
repeatableFlags = {},
|
|
43
|
+
}) {
|
|
44
|
+
const HELP_TEXT = help;
|
|
45
|
+
const commandList = commandNames.join("、");
|
|
46
|
+
|
|
47
|
+
function validateArgs(command, args) {
|
|
48
|
+
if (args._.length)
|
|
49
|
+
throw new DatarcError("不支持位置参数", "INVALID_ARGUMENT");
|
|
50
|
+
const arrayAllowed = new Set(repeatableFlags[command] ?? []);
|
|
51
|
+
for (const [key, value] of Object.entries(args)) {
|
|
52
|
+
if (key === "_") continue;
|
|
53
|
+
if (
|
|
54
|
+
!flags[command].has(key) ||
|
|
55
|
+
(Array.isArray(value) && !arrayAllowed.has(key))
|
|
56
|
+
)
|
|
57
|
+
throw new DatarcError(
|
|
58
|
+
`参数 --${key} 不适用于 ${command}`,
|
|
59
|
+
"INVALID_ARGUMENT",
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
if (requiresOutput.includes(command)) required(args, "output");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function execute(argv, options = {}) {
|
|
66
|
+
const [command, ...rest] = argv;
|
|
67
|
+
if (!flags[command])
|
|
68
|
+
throw new DatarcError(`命令应为 ${commandList}`, "INVALID_ARGUMENT");
|
|
69
|
+
const args = parseArgs(rest);
|
|
70
|
+
validateArgs(command, args);
|
|
71
|
+
const client = localCommands.includes(command)
|
|
72
|
+
? null
|
|
73
|
+
: (options.client ?? createClient(options));
|
|
74
|
+
if (command === "apply") {
|
|
75
|
+
const paths = [].concat(args.input ?? []);
|
|
76
|
+
if (paths.length === 0)
|
|
77
|
+
throw new DatarcError("缺少参数 --input", "INVALID_ARGUMENT");
|
|
78
|
+
const inputs = await Promise.all(paths.map((path) => readJson(path)));
|
|
79
|
+
return handlers.apply(client, inputs, args, options);
|
|
80
|
+
}
|
|
81
|
+
const input = await readJson(required(args, "input"));
|
|
82
|
+
const handler = handlers[command];
|
|
83
|
+
if (!handler)
|
|
84
|
+
throw new DatarcError(`未知命令 ${command}`, "INVALID_ARGUMENT");
|
|
85
|
+
return handler(client, input, args, options);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function run(argv, options = {}) {
|
|
89
|
+
try {
|
|
90
|
+
return { ok: true, ...(await execute(argv, options)) };
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return { ok: false, error: errorJson(error) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return { execute, run, HELP_TEXT };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function requireConfirmation(actual, prefix, hash) {
|
|
100
|
+
if (typeof hash !== "string" || actual !== `${prefix}:${hash}`)
|
|
101
|
+
throw new DatarcError(
|
|
102
|
+
`confirmation must be ${prefix}:<hash>`,
|
|
103
|
+
"CONFIRMATION_REQUIRED",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function journalCommand({
|
|
108
|
+
command,
|
|
109
|
+
workspaceId,
|
|
110
|
+
input,
|
|
111
|
+
request,
|
|
112
|
+
output,
|
|
113
|
+
options,
|
|
114
|
+
operation,
|
|
115
|
+
response,
|
|
116
|
+
}) {
|
|
117
|
+
const identity = createCommandArtifactIdentity({
|
|
118
|
+
command,
|
|
119
|
+
workspaceId,
|
|
120
|
+
request,
|
|
121
|
+
input,
|
|
122
|
+
});
|
|
123
|
+
let reserved;
|
|
124
|
+
try {
|
|
125
|
+
reserved = await reserveArtifact(
|
|
126
|
+
output,
|
|
127
|
+
{ ...identity, status: "reserved" },
|
|
128
|
+
{
|
|
129
|
+
openArtifact: options.openArtifact,
|
|
130
|
+
openDirectory: options.openDirectory,
|
|
131
|
+
journalId: options.journalId,
|
|
132
|
+
},
|
|
133
|
+
);
|
|
134
|
+
await appendReservedArtifact(reserved, {
|
|
135
|
+
...identity,
|
|
136
|
+
status: "running",
|
|
137
|
+
});
|
|
138
|
+
const result = await operation({ identity, reserved });
|
|
139
|
+
await appendReservedArtifact(
|
|
140
|
+
reserved,
|
|
141
|
+
completeCommandArtifact(identity, result),
|
|
142
|
+
);
|
|
143
|
+
return response(result, reserved.artifact);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw commandJournalFailure(error, reserved, command);
|
|
146
|
+
} finally {
|
|
147
|
+
await reserved?.handle.close();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function withOptionalArtifact(value, output, summary) {
|
|
152
|
+
if (output === undefined) return { summary };
|
|
153
|
+
const artifact = await writeArtifact(output, value);
|
|
154
|
+
return { artifact, summary };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function timestamp(now) {
|
|
158
|
+
const value = now === undefined ? new Date() : now();
|
|
159
|
+
const date = value instanceof Date ? value : new Date(value);
|
|
160
|
+
if (!Number.isFinite(date.valueOf()))
|
|
161
|
+
throw new DatarcError("invalid mutation timestamp", "INVALID_TIME");
|
|
162
|
+
return date.toISOString();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function commandJournalFailure(error, reserved, phase) {
|
|
166
|
+
const artifact = reserved?.artifact ?? error?.details?.artifact;
|
|
167
|
+
const reloadRequired =
|
|
168
|
+
reserved?.syncAmbiguous === true || error?.details?.reloadRequired === true;
|
|
169
|
+
return new DatarcError(
|
|
170
|
+
reloadRequired
|
|
171
|
+
? "artifact sync result is ambiguous; reload the artifact"
|
|
172
|
+
: `${phase} failed; inspect the private artifact`,
|
|
173
|
+
typeof error?.code === "string" ? error.code : "COMMAND_FAILED",
|
|
174
|
+
{
|
|
175
|
+
...(typeof artifact === "string" ? { artifact } : {}),
|
|
176
|
+
phase,
|
|
177
|
+
...(reloadRequired ? { reloadRequired: true } : {}),
|
|
178
|
+
},
|
|
179
|
+
);
|
|
180
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { resolveCredentials } from "../../datarc-oauth/scripts/credentials.mjs";
|
|
2
|
+
export class DatarcError extends Error {
|
|
3
|
+
constructor(message, code = "DATARC_ERROR", details) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "DatarcError";
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.details = details;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parseArgs(argv) {
|
|
12
|
+
const result = { _: [] };
|
|
13
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
14
|
+
const value = argv[index];
|
|
15
|
+
if (!value.startsWith("--")) {
|
|
16
|
+
result._.push(value);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const key = value.slice(2);
|
|
20
|
+
const next = argv[index + 1];
|
|
21
|
+
if (next === undefined || next.startsWith("--")) result[key] = true;
|
|
22
|
+
else {
|
|
23
|
+
result[key] =
|
|
24
|
+
result[key] === undefined ? next : [].concat(result[key], next);
|
|
25
|
+
index += 1;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function required(args, key) {
|
|
32
|
+
if (args[key] === undefined || args[key] === true || args[key] === "")
|
|
33
|
+
throw new DatarcError(`缺少参数 --${key}`, "INVALID_ARGUMENT");
|
|
34
|
+
return args[key];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function createClient(options = {}) {
|
|
38
|
+
const { endpoint, token } = resolveCredentials(options);
|
|
39
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
40
|
+
if (!endpoint)
|
|
41
|
+
throw new DatarcError("未配置 DATARC_ENDPOINT", "MISSING_ENDPOINT");
|
|
42
|
+
if (!token) throw new DatarcError("未配置 DATARC_TOKEN", "MISSING_TOKEN");
|
|
43
|
+
|
|
44
|
+
async function request(method, path, { query, body } = {}) {
|
|
45
|
+
const url = new URL(`${endpoint}${path}`);
|
|
46
|
+
for (const [key, value] of Object.entries(query ?? {}))
|
|
47
|
+
for (const item of [].concat(value ?? []))
|
|
48
|
+
if (item !== "") url.searchParams.append(key, item);
|
|
49
|
+
let response;
|
|
50
|
+
try {
|
|
51
|
+
response = await fetchImpl(url, {
|
|
52
|
+
method,
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: token,
|
|
55
|
+
...(body === undefined ? {} : { "Content-Type": "application/json" }),
|
|
56
|
+
},
|
|
57
|
+
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
58
|
+
});
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new DatarcError(`网络请求失败:${error.message}`, "NETWORK_ERROR");
|
|
61
|
+
}
|
|
62
|
+
const text = await response.text();
|
|
63
|
+
let payload;
|
|
64
|
+
try {
|
|
65
|
+
payload = text ? JSON.parse(text) : {};
|
|
66
|
+
} catch {
|
|
67
|
+
throw new DatarcError(
|
|
68
|
+
`服务返回非 JSON(HTTP ${response.status})`,
|
|
69
|
+
"INVALID_RESPONSE",
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
const isEnvelope =
|
|
73
|
+
payload !== null &&
|
|
74
|
+
typeof payload === "object" &&
|
|
75
|
+
!Array.isArray(payload);
|
|
76
|
+
if (!response.ok)
|
|
77
|
+
throw new DatarcError(
|
|
78
|
+
(isEnvelope && payload.message) ||
|
|
79
|
+
`请求失败(HTTP ${response.status})`,
|
|
80
|
+
apiErrorCode(response.status),
|
|
81
|
+
{
|
|
82
|
+
httpStatus: response.status,
|
|
83
|
+
status: isEnvelope ? payload.status : undefined,
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
if (!isEnvelope || typeof payload.status !== "string")
|
|
87
|
+
throw new DatarcError(
|
|
88
|
+
`服务返回无效响应信封(HTTP ${response.status})`,
|
|
89
|
+
"INVALID_RESPONSE",
|
|
90
|
+
{ httpStatus: response.status },
|
|
91
|
+
);
|
|
92
|
+
if (payload.status !== "SUCCESS")
|
|
93
|
+
throw new DatarcError(
|
|
94
|
+
payload.message || `请求失败(HTTP ${response.status})`,
|
|
95
|
+
apiErrorCode(response.status),
|
|
96
|
+
{ httpStatus: response.status, status: payload.status },
|
|
97
|
+
);
|
|
98
|
+
if (!Object.hasOwn(payload, "data"))
|
|
99
|
+
throw new DatarcError(
|
|
100
|
+
`服务返回无效响应信封(HTTP ${response.status})`,
|
|
101
|
+
"INVALID_RESPONSE",
|
|
102
|
+
{ httpStatus: response.status },
|
|
103
|
+
);
|
|
104
|
+
return payload.data;
|
|
105
|
+
}
|
|
106
|
+
return { request };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function errorJson(error) {
|
|
110
|
+
const trusted = [
|
|
111
|
+
"DatarcError",
|
|
112
|
+
"CaseModelError",
|
|
113
|
+
"DatasetPreflightError",
|
|
114
|
+
].includes(error?.name);
|
|
115
|
+
const code =
|
|
116
|
+
trusted && typeof error?.code === "string"
|
|
117
|
+
? error.code
|
|
118
|
+
: "UNEXPECTED_ERROR";
|
|
119
|
+
const details = safeErrorDetails(error?.details);
|
|
120
|
+
return {
|
|
121
|
+
code,
|
|
122
|
+
message: safeErrorMessage(error, code),
|
|
123
|
+
...(Object.keys(details).length ? { details } : {}),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function apiErrorCode(status) {
|
|
128
|
+
if (status === 401) return "AUTHENTICATION_FAILED";
|
|
129
|
+
if (status === 403) return "PERMISSION_DENIED";
|
|
130
|
+
if (status === 429) return "RATE_LIMITED";
|
|
131
|
+
return "API_ERROR";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function safeErrorMessage(error, code) {
|
|
135
|
+
if (code === "NETWORK_ERROR") return "无法连接北极九章(datarc)服务";
|
|
136
|
+
if (code === "INVALID_RESPONSE") return "北极九章(datarc)服务返回无效响应";
|
|
137
|
+
if (
|
|
138
|
+
new Set([
|
|
139
|
+
"API_ERROR",
|
|
140
|
+
"AUTHENTICATION_FAILED",
|
|
141
|
+
"PERMISSION_DENIED",
|
|
142
|
+
"RATE_LIMITED",
|
|
143
|
+
]).has(code)
|
|
144
|
+
)
|
|
145
|
+
return "北极九章(datarc)服务请求失败";
|
|
146
|
+
if (code === "UNEXPECTED_ERROR") return "操作失败";
|
|
147
|
+
return typeof error?.message === "string" && error.message
|
|
148
|
+
? error.message
|
|
149
|
+
: "操作失败";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function safeErrorDetails(details) {
|
|
153
|
+
if (!details || typeof details !== "object" || Array.isArray(details))
|
|
154
|
+
return {};
|
|
155
|
+
const result = {};
|
|
156
|
+
for (const key of [
|
|
157
|
+
"artifact",
|
|
158
|
+
"conflictIndex",
|
|
159
|
+
"duplicateIndex",
|
|
160
|
+
"firstIndex",
|
|
161
|
+
"hash",
|
|
162
|
+
"httpStatus",
|
|
163
|
+
"index",
|
|
164
|
+
"kind",
|
|
165
|
+
"partial",
|
|
166
|
+
"phase",
|
|
167
|
+
"reloadRequired",
|
|
168
|
+
"resourceId",
|
|
169
|
+
"resourceKind",
|
|
170
|
+
"sourceIndex",
|
|
171
|
+
"sourceType",
|
|
172
|
+
"status",
|
|
173
|
+
"workspaceId",
|
|
174
|
+
]) {
|
|
175
|
+
const value = details[key];
|
|
176
|
+
if (["string", "number", "boolean"].includes(typeof value))
|
|
177
|
+
result[key] = value;
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { isRecord, requiredTrimmedString, uniquePairs } from "./shared.mjs";
|
|
2
|
+
|
|
3
|
+
const UNSAFE_INTERPRETATION_KEYS = new Set([
|
|
4
|
+
"cards",
|
|
5
|
+
"data",
|
|
6
|
+
"error",
|
|
7
|
+
"history",
|
|
8
|
+
"params",
|
|
9
|
+
"prompt",
|
|
10
|
+
"prompten",
|
|
11
|
+
"qkey",
|
|
12
|
+
"query",
|
|
13
|
+
"reasoning",
|
|
14
|
+
"result",
|
|
15
|
+
"results",
|
|
16
|
+
"secret",
|
|
17
|
+
"secrets",
|
|
18
|
+
"sql",
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
export function actionInterpretations(actions) {
|
|
22
|
+
const pairs = [];
|
|
23
|
+
for (const action of actions) collectSafePairs(action?.data, pairs);
|
|
24
|
+
return uniquePairs(pairs);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function collectSafePairs(value, pairs, key = "") {
|
|
28
|
+
if (value === null || value === undefined || unsafeKey(key)) return;
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
for (const item of value) collectSafePairs(item, pairs, key);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (!isRecord(value)) return;
|
|
34
|
+
|
|
35
|
+
const word = requiredTrimmedString(value.word);
|
|
36
|
+
const prop = requiredTrimmedString(value.prop);
|
|
37
|
+
if (word && prop) pairs.push({ word, prop });
|
|
38
|
+
for (const [childKey, child] of Object.entries(value)) {
|
|
39
|
+
if (childKey === "word" || childKey === "prop") continue;
|
|
40
|
+
collectSafePairs(child, pairs, childKey);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function unsafeKey(key) {
|
|
45
|
+
const normalized = String(key)
|
|
46
|
+
.replace(/[^a-z]/giu, "")
|
|
47
|
+
.toLowerCase();
|
|
48
|
+
return (
|
|
49
|
+
UNSAFE_INTERPRETATION_KEYS.has(normalized) ||
|
|
50
|
+
normalized.includes("secret") ||
|
|
51
|
+
normalized.includes("password") ||
|
|
52
|
+
normalized.includes("authorization") ||
|
|
53
|
+
normalized.includes("apikey") ||
|
|
54
|
+
normalized.includes("accesstoken") ||
|
|
55
|
+
normalized.includes("refreshtoken")
|
|
56
|
+
);
|
|
57
|
+
}
|