@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,180 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,57 +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
|
-
}
|
|
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
|
+
}
|