@arcticdata/dsh-datarc-assistant 0.0.0-stage → 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 +46 -2
- package/api-operations.js +404 -0
- package/client.js +475 -0
- package/cordis.patch.yml +4 -0
- package/entry/SKILL.md +34 -0
- package/icon.png +0 -0
- package/index.js +69 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- 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 -6
- package/runtime/datarc-access-permissions/SKILL.md +30 -0
- package/runtime/datarc-access-permissions/references/api.md +31 -0
- package/runtime/datarc-access-permissions/references/write-order.md +16 -0
- package/runtime/datarc-analysis-report/SKILL.md +125 -0
- package/runtime/datarc-analysis-report/agents/openai.yaml +6 -0
- package/runtime/datarc-analysis-report/references/analyst-frameworks.md +215 -0
- package/runtime/datarc-analysis-report/references/api-contracts.md +478 -0
- package/runtime/datarc-analysis-report/references/direct-card-fallback-design.md +66 -0
- package/runtime/datarc-analysis-report/references/human-in-the-loop.md +142 -0
- package/runtime/datarc-analysis-report/references/loop-personalization-policy.md +138 -0
- package/runtime/datarc-analysis-report/references/manifest-example.json +779 -0
- package/runtime/datarc-analysis-report/references/narrative-schema.md +160 -0
- package/runtime/datarc-analysis-report/references/question-syntax.md +151 -0
- package/runtime/datarc-analysis-report/scripts/run_analysis_report.py +2225 -0
- package/runtime/datarc-analysis-report/scripts/run_dataseek_cards.py +1914 -0
- package/runtime/datarc-analysis-report/scripts/test_run_analysis_report.py +5295 -0
- package/runtime/datarc-data-search/SKILL.md +243 -0
- package/runtime/datarc-data-search/scripts/calculate.py +116 -0
- package/runtime/datarc-data-search/scripts/data-search.mjs +628 -0
- package/runtime/datarc-data-search/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-data-search/scripts/search-result.mjs +211 -0
- package/runtime/datarc-dataseek-batch-evaluation/SKILL.md +59 -0
- package/runtime/datarc-dataseek-batch-evaluation/references/artifacts.md +36 -0
- package/runtime/datarc-dataseek-batch-evaluation/references/equivalence.md +30 -0
- package/runtime/datarc-dataseek-batch-evaluation/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-batch-evaluation/scripts/dataseek-batch-evaluation.mjs +1020 -0
- package/runtime/datarc-dataseek-search/SKILL.md +96 -0
- package/runtime/datarc-dataseek-search/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-search/scripts/dataseek-search.mjs +257 -0
- package/runtime/datarc-dataseek-semantic-debugging/SKILL.md +44 -0
- package/runtime/datarc-dataseek-semantic-debugging/agents/openai.yaml +4 -0
- package/runtime/datarc-dataseek-semantic-debugging/evals/evals.json +165 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/dataset.md +77 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/input-and-resolution.md +93 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/interventions.md +64 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/knowledge-and-prompts.md +47 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/rollback.md +11 -0
- package/runtime/datarc-dataseek-semantic-debugging/references/verification-and-rollback.md +95 -0
- package/runtime/datarc-dataseek-semantic-debugging/scripts/semantic-debugging.mjs +29 -0
- package/runtime/datarc-dataseek-testset-generation/SKILL.md +47 -0
- package/runtime/datarc-dataseek-testset-generation/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-dataseek-testset-generation/scripts/dataseek-testset-generation.mjs +526 -0
- package/runtime/datarc-datasource/SKILL.md +97 -0
- package/runtime/datarc-oauth/SKILL.md +50 -0
- package/runtime/datarc-oauth/scripts/auth.mjs +192 -0
- package/runtime/datarc-oauth/scripts/credentials.mjs +237 -0
- package/runtime/datarc-report-add-card/SKILL.md +90 -0
- package/runtime/datarc-report-add-card/agents/openai.yaml +4 -0
- package/runtime/datarc-report-add-card/scripts/datarc-client.mjs +464 -0
- package/runtime/datarc-report-add-card/scripts/report-card.mjs +439 -0
- package/runtime/datarc-semantic-alias/SKILL.md +20 -0
- package/runtime/datarc-semantic-alias/scripts/alias-plan.mjs +422 -0
- package/runtime/datarc-semantic-alias/scripts/alias.mjs +11 -0
- package/runtime/datarc-semantic-calibration/SKILL.md +20 -0
- package/runtime/datarc-semantic-calibration/scripts/calibration-plan.mjs +683 -0
- package/runtime/datarc-semantic-calibration/scripts/calibration.mjs +11 -0
- package/runtime/datarc-semantic-core/SKILL.md +12 -0
- package/runtime/datarc-semantic-core/scripts/artifact-journal.mjs +853 -0
- package/runtime/datarc-semantic-core/scripts/canonical.mjs +48 -0
- package/runtime/datarc-semantic-core/scripts/case-model.mjs +608 -0
- package/runtime/datarc-semantic-core/scripts/cli.mjs +180 -0
- package/runtime/datarc-semantic-core/scripts/datarc-client.mjs +180 -0
- package/runtime/datarc-semantic-core/scripts/interpretation-pairs.mjs +57 -0
- package/runtime/datarc-semantic-core/scripts/interventions.mjs +693 -0
- package/runtime/datarc-semantic-core/scripts/live-resolution.mjs +305 -0
- package/runtime/datarc-semantic-core/scripts/model-scope.mjs +49 -0
- package/runtime/datarc-semantic-core/scripts/reproduction.mjs +330 -0
- package/runtime/datarc-semantic-core/scripts/semantic-cli.mjs +799 -0
- package/runtime/datarc-semantic-core/scripts/shared.mjs +312 -0
- package/runtime/datarc-semantic-core/scripts/verification.mjs +1111 -0
- package/runtime/datarc-semantic-dataset/SKILL.md +20 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset-plan.mjs +749 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset-preflight.mjs +813 -0
- package/runtime/datarc-semantic-dataset/scripts/dataset.mjs +11 -0
- package/runtime/datarc-semantic-knowledge/SKILL.md +38 -0
- package/runtime/datarc-semantic-knowledge/references/knowledge-drafts.md +61 -0
- package/runtime/datarc-semantic-knowledge/references/knowledge-generation.json +16 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge-draft.mjs +347 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge-plan.mjs +848 -0
- package/runtime/datarc-semantic-knowledge/scripts/knowledge.mjs +31 -0
- package/runtime/datarc-semantic-prompt/SKILL.md +20 -0
- package/runtime/datarc-semantic-prompt/scripts/prompt-plan.mjs +598 -0
- package/runtime/datarc-semantic-prompt/scripts/prompt.mjs +15 -0
- package/runtime/datarc-semantic-synonym/SKILL.md +20 -0
- package/runtime/datarc-semantic-synonym/scripts/synonym-plan.mjs +688 -0
- package/runtime/datarc-semantic-synonym/scripts/synonym.mjs +11 -0
- package/runtime/datarc-viz-analysis-report/SKILL.md +108 -0
- package/runtime/datarc-viz-analysis-report/assets/LICENSE-echarts.md +8 -0
- package/runtime/datarc-viz-analysis-report/assets/echarts.min.js +45 -0
- package/runtime/datarc-viz-analysis-report/references/api-contracts.md +163 -0
- package/runtime/datarc-viz-analysis-report/references/derivation-log.md +157 -0
- package/runtime/datarc-viz-analysis-report/references/evidence-framework.md +76 -0
- package/runtime/datarc-viz-analysis-report/references/html-template.md +92 -0
- package/runtime/datarc-viz-analysis-report/references/provenance-format.md +56 -0
- package/runtime/datarc-viz-analysis-report/references/question-syntax.md +151 -0
- package/runtime/datarc-viz-analysis-report/scripts/render_viz_report.py +1708 -0
- package/runtime/datarc-viz-analysis-report/scripts/test_render_viz_report.py +964 -0
- package/runtime/datarc-workspace-configuration/SKILL.md +59 -0
- package/runtime/datarc-workspace-configuration/references/artifacts.md +75 -0
- package/runtime/datarc-workspace-configuration/references/verification.md +20 -0
- package/runtime/datarc-workspace-configuration/scripts/configuration.mjs +722 -0
- package/runtime/datarc-workspace-context/SKILL.md +43 -0
- package/runtime/datarc-workspace-context/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-context/scripts/datarc-client.mjs +31 -0
- package/runtime/datarc-workspace-context/scripts/query-display.mjs +114 -0
- package/runtime/datarc-workspace-context/scripts/workspace-context.mjs +58 -0
- package/runtime/datarc-workspace-custom-tables/SKILL.md +89 -0
- package/runtime/datarc-workspace-custom-tables/scripts/datarc-client.mjs +165 -0
- package/runtime/datarc-workspace-custom-tables/scripts/workspace-custom-tables.mjs +601 -0
- package/runtime/datarc-workspace-keywords/SKILL.md +197 -0
- package/runtime/datarc-workspace-keywords/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-keywords/references/api-fields.md +79 -0
- package/runtime/datarc-workspace-keywords/references/derived-measures.md +177 -0
- package/runtime/datarc-workspace-keywords/scripts/datarc-client.mjs +145 -0
- package/runtime/datarc-workspace-keywords/scripts/workspace-keywords.mjs +2421 -0
- package/runtime/datarc-workspace-membership/SKILL.md +29 -0
- package/runtime/datarc-workspace-membership/references/api.md +19 -0
- package/runtime/datarc-workspace-modeling/SKILL.md +209 -0
- package/runtime/datarc-workspace-modeling/agents/openai.yaml +4 -0
- package/runtime/datarc-workspace-modeling/references/join-preview.md +38 -0
- package/runtime/datarc-workspace-modeling/references/model-plan.md +300 -0
- package/runtime/datarc-workspace-modeling/references/model-tags.md +95 -0
- package/runtime/datarc-workspace-modeling/references/modeling-contract.json +29 -0
- package/runtime/datarc-workspace-modeling/references/modeling-fixtures.json +2423 -0
- package/runtime/datarc-workspace-modeling/scripts/datarc-client.mjs +137 -0
- package/runtime/datarc-workspace-modeling/scripts/workspace-model-tags.mjs +238 -0
- package/runtime/datarc-workspace-modeling/scripts/workspace-modeling.mjs +1261 -0
- package/skills-provider.js +51 -0
- package/workflow-index.json +434 -0
package/native-api.mjs
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// HTTP adaptation only. Original skill documents own workflow and confirmation.
|
|
2
|
+
import { mkdtemp, readFile, stat, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { basename, extname, join, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
+
import { API_DOMAINS } from './api-operations.js';
|
|
7
|
+
|
|
8
|
+
const SECRET_KEYS = new Set([
|
|
9
|
+
'password',
|
|
10
|
+
'pwd',
|
|
11
|
+
'token',
|
|
12
|
+
'imagetoken',
|
|
13
|
+
'accesstoken',
|
|
14
|
+
'refreshtoken',
|
|
15
|
+
'idtoken',
|
|
16
|
+
'datarctoken',
|
|
17
|
+
'datarcopenapikey',
|
|
18
|
+
'apikey',
|
|
19
|
+
'authorization',
|
|
20
|
+
'secret',
|
|
21
|
+
'clientsecret',
|
|
22
|
+
'credentials',
|
|
23
|
+
'databaseurl',
|
|
24
|
+
'connectionurl',
|
|
25
|
+
'qkey',
|
|
26
|
+
'sql',
|
|
27
|
+
]);
|
|
28
|
+
function error(message, code = 'INVALID_REQUEST') {
|
|
29
|
+
return Object.assign(new Error(message), { code });
|
|
30
|
+
}
|
|
31
|
+
function mask(value, secrets = []) {
|
|
32
|
+
if (Array.isArray(value)) return value.map((item) => mask(item, secrets));
|
|
33
|
+
if (value && typeof value === 'object')
|
|
34
|
+
return Object.fromEntries(
|
|
35
|
+
Object.entries(value).map(([key, item]) => [
|
|
36
|
+
key,
|
|
37
|
+
SECRET_KEYS.has(key.toLowerCase().replace(/[-_\s]/g, '')) ? (item ? '***' : item) : mask(item, secrets),
|
|
38
|
+
]),
|
|
39
|
+
);
|
|
40
|
+
if (typeof value !== 'string') return value;
|
|
41
|
+
for (const secret of secrets) if (secret) value = value.split(secret).join('***');
|
|
42
|
+
return value.replace(/\b[a-z][a-z\d+.-]*:\/\/[^\s<>"']*:[^\s<>"']*@[^\s<>"']+/gi, '[含凭据地址已隐藏]');
|
|
43
|
+
}
|
|
44
|
+
function object(value) {
|
|
45
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) throw error('请求体必须是原接口要求的JSON对象,请修正输入文件。');
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
async function json(c, path) {
|
|
49
|
+
const source = await readFile(resolve(c.cwd, path), 'utf8');
|
|
50
|
+
let value;
|
|
51
|
+
try {
|
|
52
|
+
value = JSON.parse(source);
|
|
53
|
+
} catch {
|
|
54
|
+
throw error('输入文件不是有效JSON,请修正格式后重试;错误预览不会回显其中的连接信息。', 'INVALID_INPUT');
|
|
55
|
+
}
|
|
56
|
+
return object(value);
|
|
57
|
+
}
|
|
58
|
+
async function artifact(c, value, label) {
|
|
59
|
+
const directory = await mkdtemp(join(tmpdir(), 'datarc-' + label + '-'));
|
|
60
|
+
const path = join(directory, 'result.json');
|
|
61
|
+
await writeFile(path, JSON.stringify(value), { flag: 'wx', mode: 0o600 });
|
|
62
|
+
return path;
|
|
63
|
+
}
|
|
64
|
+
async function context(domain, request, options) {
|
|
65
|
+
const cwd = resolve(options.cwd ?? process.cwd());
|
|
66
|
+
const env = { ...(options.env ?? process.env), DATARC_WORKSPACE_DIR: cwd };
|
|
67
|
+
const runtime = options.runtimeRoot ?? fileURLToPath(new URL('./runtime/', import.meta.url));
|
|
68
|
+
const auth = await import(pathToFileURL(join(runtime, 'datarc-oauth/scripts/credentials.mjs')).href);
|
|
69
|
+
const open = domain === 'permissions' && request.operation !== 'source_details';
|
|
70
|
+
const credentials = open
|
|
71
|
+
? {
|
|
72
|
+
endpoint: auth.normalizeEndpoint(env.DATARC_ENDPOINT),
|
|
73
|
+
token: env.DATARC_OPENAPI_KEY,
|
|
74
|
+
}
|
|
75
|
+
: auth.resolveCredentials({
|
|
76
|
+
env,
|
|
77
|
+
configPath: auth.credentialsPath(env, cwd),
|
|
78
|
+
});
|
|
79
|
+
if (!credentials.endpoint || typeof credentials.token !== 'string' || !credentials.token.trim() || /[\r\n]/.test(credentials.token))
|
|
80
|
+
throw error(
|
|
81
|
+
open ? '请在安全环境配置服务地址与独立DATARC_OPENAPI_KEY,并先核对scopes;勿在聊天粘贴密钥。' : '请先按原登录流程取得可用业务凭据。',
|
|
82
|
+
'AUTH_REQUIRED',
|
|
83
|
+
);
|
|
84
|
+
if ((open && credentials.token === env.DATARC_TOKEN) || (!open && credentials.token === env.DATARC_OPENAPI_KEY))
|
|
85
|
+
throw error('业务登录令牌与独立开放密钥不能互相替代,请核对安全配置。', 'AUTH_REQUIRED');
|
|
86
|
+
const secrets = [credentials.token, env.DATARC_TOKEN, env.DATARC_OPENAPI_KEY].filter(Boolean);
|
|
87
|
+
const c = { cwd, env, secrets, options, request: undefined };
|
|
88
|
+
c.request = async (method, path, params = {}) => {
|
|
89
|
+
options.signal?.throwIfAborted();
|
|
90
|
+
if (options.client) return options.client.request(method, path, params);
|
|
91
|
+
const url = new URL(credentials.endpoint + path);
|
|
92
|
+
for (const [key, value] of Object.entries(params.query ?? {}))
|
|
93
|
+
if (value !== undefined) url.searchParams.set(key, typeof value === 'string' ? value : JSON.stringify(value));
|
|
94
|
+
const headers = {
|
|
95
|
+
Authorization: open ? 'Bearer ' + credentials.token : credentials.token,
|
|
96
|
+
};
|
|
97
|
+
if (params.language) headers['Accept-Language'] = params.language;
|
|
98
|
+
let body = params.form;
|
|
99
|
+
if (params.body !== undefined) {
|
|
100
|
+
headers['Content-Type'] = 'application/json';
|
|
101
|
+
body = JSON.stringify(params.body);
|
|
102
|
+
}
|
|
103
|
+
let response;
|
|
104
|
+
try {
|
|
105
|
+
response = await (options.fetch ?? globalThis.fetch)(url, {
|
|
106
|
+
method,
|
|
107
|
+
headers,
|
|
108
|
+
body,
|
|
109
|
+
redirect: 'error',
|
|
110
|
+
signal: options.signal,
|
|
111
|
+
});
|
|
112
|
+
} catch (cause) {
|
|
113
|
+
throw error('请求未确认完成,请核对网络及当前业务状态;写入结果未知时不要重新发送。', 'NETWORK_ERROR');
|
|
114
|
+
}
|
|
115
|
+
if (response.status === 204 && response.ok) return null;
|
|
116
|
+
let envelope;
|
|
117
|
+
try {
|
|
118
|
+
envelope = await response.json();
|
|
119
|
+
} catch {
|
|
120
|
+
throw error('服务未返回有效JSON,请核对服务状态,不重复未知写入。', 'INVALID_RESPONSE');
|
|
121
|
+
}
|
|
122
|
+
if (!response.ok || (open ? envelope.errcode !== 200 : envelope.status !== 'SUCCESS')) {
|
|
123
|
+
const failure = error(
|
|
124
|
+
String(mask(envelope.message ?? envelope.errmsg ?? '接口请求失败,请核对权限、参数和服务状态。', secrets)),
|
|
125
|
+
envelope.data?.code ?? 'API_ERROR',
|
|
126
|
+
);
|
|
127
|
+
failure.httpStatus = response.status;
|
|
128
|
+
failure.businessStatus = open ? envelope.errcode : envelope.status;
|
|
129
|
+
failure.details = mask(envelope.data, secrets);
|
|
130
|
+
throw failure;
|
|
131
|
+
}
|
|
132
|
+
return envelope.data ?? null;
|
|
133
|
+
};
|
|
134
|
+
return c;
|
|
135
|
+
}
|
|
136
|
+
function routePath(route, r) {
|
|
137
|
+
return route.path.replace(/\{([^}]+)\}/g, (_, key) => {
|
|
138
|
+
const value = key === 'subject' ? { member: 'members', group: 'member-groups' }[r.target] : r[key];
|
|
139
|
+
if (value === undefined || value === null || String(value) === '') throw error('缺少路径参数' + key + ',请使用当前工作区已核对的对象。');
|
|
140
|
+
return encodeURIComponent(String(value));
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function equalVisible(observed, expected) {
|
|
144
|
+
if (!expected || typeof expected !== 'object') return Object.is(observed, expected);
|
|
145
|
+
if (Array.isArray(expected))
|
|
146
|
+
return Array.isArray(observed) && expected.length === observed.length && expected.every((value, i) => equalVisible(observed[i], value));
|
|
147
|
+
return Object.entries(expected).every(([key, value]) => {
|
|
148
|
+
const normalized = key.toLowerCase().replace(/[-_\s]/g, '');
|
|
149
|
+
return (SECRET_KEYS.has(normalized) && !['databaseurl', 'connectionurl'].includes(normalized)) || equalVisible(observed?.[key], value);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function fieldPreferences(entries) {
|
|
153
|
+
const counts = new Map();
|
|
154
|
+
const visit = (value) => {
|
|
155
|
+
if (!value || typeof value !== 'object') return;
|
|
156
|
+
if (Array.isArray(value)) return value.forEach(visit);
|
|
157
|
+
if (typeof value.prop === 'string') {
|
|
158
|
+
const prop = value.prop.split('=', 1)[0].split('.', 1)[0];
|
|
159
|
+
if (/^[md][1-9]\d*$|^t(?:0|[1-9]\d*)$/.test(prop)) counts.set(prop, (counts.get(prop) ?? 0) + 1);
|
|
160
|
+
}
|
|
161
|
+
for (const [key, child] of Object.entries(value)) if (['tokens', 'units', 'data'].includes(key)) visit(child);
|
|
162
|
+
};
|
|
163
|
+
visit(entries);
|
|
164
|
+
return [...counts].map(([prop, uses]) => ({ prop, uses }));
|
|
165
|
+
}
|
|
166
|
+
async function privateResult(c, type, value) {
|
|
167
|
+
const result_file = await artifact(c, value, type);
|
|
168
|
+
if (type === 'report')
|
|
169
|
+
return {
|
|
170
|
+
report_file: result_file,
|
|
171
|
+
uid: value?.uid,
|
|
172
|
+
name: value?.name,
|
|
173
|
+
reportVersion: value?.reportVersion,
|
|
174
|
+
pinCount: value?.content?.length,
|
|
175
|
+
};
|
|
176
|
+
if (type === 'prompt')
|
|
177
|
+
return {
|
|
178
|
+
prompt_file: result_file,
|
|
179
|
+
promptVersion: value?.promptVersion,
|
|
180
|
+
modelAvailable: value?.modelAvailable,
|
|
181
|
+
variables: value?.variables,
|
|
182
|
+
};
|
|
183
|
+
if (type === 'task')
|
|
184
|
+
return {
|
|
185
|
+
task_file: result_file,
|
|
186
|
+
uid: value?.uid ?? value?.id,
|
|
187
|
+
status: value?.status,
|
|
188
|
+
resultStatus: value?.result?.status,
|
|
189
|
+
};
|
|
190
|
+
if (type === 'interpretation')
|
|
191
|
+
return {
|
|
192
|
+
result_file,
|
|
193
|
+
id: value?.id ?? value?.taskUid,
|
|
194
|
+
status: value?.status,
|
|
195
|
+
code: value?.code,
|
|
196
|
+
};
|
|
197
|
+
return {
|
|
198
|
+
result_file,
|
|
199
|
+
id: value?.id,
|
|
200
|
+
name: value?.name,
|
|
201
|
+
count: Array.isArray(value) ? value.length : undefined,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
async function uploadSop(c, route, r) {
|
|
205
|
+
const path = resolve(c.cwd, r.source_file),
|
|
206
|
+
info = await stat(path);
|
|
207
|
+
if (!info.isFile() || info.size < 1 || info.size >= 50 * 1024 * 1024 || !['.txt', '.md', '.docx', '.pdf'].includes(extname(path).toLowerCase()))
|
|
208
|
+
throw error('请提供原SOP流程允许的非空txt/md/docx/pdf文件,且小于50MB。');
|
|
209
|
+
const bytes = await readFile(path),
|
|
210
|
+
form = new FormData();
|
|
211
|
+
form.append('file', new Blob([bytes]), basename(path));
|
|
212
|
+
const collection = routePath(route, r);
|
|
213
|
+
const converted = await c.request('POST', collection + 'markitdown/', {
|
|
214
|
+
form,
|
|
215
|
+
language: r.language,
|
|
216
|
+
});
|
|
217
|
+
if (!converted || typeof converted.content !== 'string' || !converted.content.trim())
|
|
218
|
+
throw error('文档转换未返回正文,请核对原文件后重试;尚未创建SOP。', 'INVALID_RESPONSE');
|
|
219
|
+
// Scope was displayed and confirmed by the Agent; the original server checks its version.
|
|
220
|
+
return c.request('POST', collection, {
|
|
221
|
+
body: {
|
|
222
|
+
name: basename(path, extname(path)),
|
|
223
|
+
content: converted.content,
|
|
224
|
+
default_scope_version: r.default_scope_version,
|
|
225
|
+
},
|
|
226
|
+
language: r.language,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
async function bodyFor(c, domain, definition, r) {
|
|
230
|
+
const kind = definition.route.body;
|
|
231
|
+
if (!kind || kind === 'sop-upload') return undefined;
|
|
232
|
+
let body = await json(c, r.candidate_file ?? r.input_file);
|
|
233
|
+
const remember = (value) => {
|
|
234
|
+
if (!value || typeof value !== 'object') return;
|
|
235
|
+
for (const [key, item] of Object.entries(value)) {
|
|
236
|
+
if (SECRET_KEYS.has(key.toLowerCase().replace(/[-_\s]/g, '')) && typeof item === 'string' && item) c.secrets.push(item);
|
|
237
|
+
else remember(item);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
remember(body);
|
|
241
|
+
if (kind === 'datasource' || kind === 'datasource-test') {
|
|
242
|
+
if (!Number.isSafeInteger(body.adapter) || body.adapter < 1 || body.adapter === 12)
|
|
243
|
+
throw error('请使用原流程确认的数据库adapter;内置数据仓库12不在配置范围。');
|
|
244
|
+
if (kind === 'datasource-test') {
|
|
245
|
+
const key = body.databaseConfig ? 'databaseConfig' : 'databaseUrl';
|
|
246
|
+
if (!body[key]) throw error('候选缺少有效连接信息,请核对数据库配置后验证。');
|
|
247
|
+
body = { adapter: body.adapter, [key]: body[key] };
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (kind === 'member' && body.role !== undefined && ![0, 1].includes(body.role))
|
|
251
|
+
throw error('原成员流程只设置普通用户0或管理员1;负责人转移须使用专门流程。');
|
|
252
|
+
if (kind === 'mapping' && r.csv_file !== undefined)
|
|
253
|
+
body = {
|
|
254
|
+
...body,
|
|
255
|
+
data: await readFile(resolve(c.cwd, r.csv_file), 'utf8'),
|
|
256
|
+
};
|
|
257
|
+
if (kind === 'report-task' && ![8, 11].includes(body.taskType)) throw error('原报告流程只使用taskType8取数和11本地洞察,请核对请求体。');
|
|
258
|
+
return body;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export async function execute(domain, request, options = {}) {
|
|
262
|
+
const definition = API_DOMAINS[domain]?.operations?.[request?.operation];
|
|
263
|
+
if (!definition?.route) throw error('该API动作未公布,请读取当前业务目录使用固定操作。', 'UNKNOWN_OPERATION');
|
|
264
|
+
const c = await context(domain, request, options),
|
|
265
|
+
route = definition.route;
|
|
266
|
+
try {
|
|
267
|
+
if (request.language !== undefined && !['zh-CN', 'en-US'].includes(request.language)) throw error('请沿用原报告流程确定的zh-CN或en-US语言。');
|
|
268
|
+
let value;
|
|
269
|
+
if (route.body === 'sop-upload') value = await uploadSop(c, route, request);
|
|
270
|
+
else {
|
|
271
|
+
const query = Object.fromEntries(
|
|
272
|
+
Object.entries(route.query ?? {})
|
|
273
|
+
.filter(([key]) => request[key] !== undefined)
|
|
274
|
+
.map(([key, target]) => [target, request[key]]),
|
|
275
|
+
);
|
|
276
|
+
if (route.private === 'history') {
|
|
277
|
+
query.offset ??= 0;
|
|
278
|
+
query.limit ??= 20;
|
|
279
|
+
}
|
|
280
|
+
if (domain === 'report' && request.operation === 'history') query.days = 30;
|
|
281
|
+
if (route.filters) query.filters = (await json(c, request.input_file)).filters;
|
|
282
|
+
const body = await bodyFor(c, domain, definition, request);
|
|
283
|
+
value = await c.request(route.method, routePath(route, request), {
|
|
284
|
+
query,
|
|
285
|
+
...(body === undefined ? {} : { body }),
|
|
286
|
+
...((request.language ?? body?.language) ? { language: request.language ?? body.language } : {}),
|
|
287
|
+
});
|
|
288
|
+
if (domain === 'datasource' && request.operation === 'validate' && (value === null || value === '' || value === undefined))
|
|
289
|
+
throw error('连接验证未返回版本,不能进入保存步骤;请核对配置后重新验证。', 'VALIDATION_FAILED');
|
|
290
|
+
}
|
|
291
|
+
if (route.private === 'history' && !Array.isArray(value))
|
|
292
|
+
throw error('历史列表响应不完整,请核对服务版本后重读,不能据此认定没有历史记录。', 'INVALID_RESPONSE');
|
|
293
|
+
if (route.private === 'history')
|
|
294
|
+
return {
|
|
295
|
+
ok: true,
|
|
296
|
+
data: {
|
|
297
|
+
count: Array.isArray(value) ? value.length : 0,
|
|
298
|
+
fieldUsage: fieldPreferences(value),
|
|
299
|
+
hasMore: Array.isArray(value) && value.length === (request.limit ?? 20),
|
|
300
|
+
maxRecommendationWeight: 0.15,
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
if (route.private) return { ok: true, data: await privateResult(c, route.private, value) };
|
|
304
|
+
if (domain === 'datasource' && request.operation === 'verify' && request.candidate_file)
|
|
305
|
+
return {
|
|
306
|
+
ok: true,
|
|
307
|
+
data: {
|
|
308
|
+
current: mask(value, c.secrets),
|
|
309
|
+
matches: equalVisible(value, await json(c, request.candidate_file)),
|
|
310
|
+
},
|
|
311
|
+
};
|
|
312
|
+
return { ok: true, data: mask(value, c.secrets) };
|
|
313
|
+
} catch (cause) {
|
|
314
|
+
options.signal?.throwIfAborted();
|
|
315
|
+
return {
|
|
316
|
+
ok: false,
|
|
317
|
+
error: {
|
|
318
|
+
code: cause.code ?? 'API_ERROR',
|
|
319
|
+
message: mask(String(cause.message), c.secrets),
|
|
320
|
+
...(cause.httpStatus === undefined ? {} : { httpStatus: cause.httpStatus }),
|
|
321
|
+
...(cause.businessStatus === undefined ? {} : { businessStatus: cause.businessStatus }),
|
|
322
|
+
...(cause.details === undefined ? {} : { details: cause.details }),
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { DOMAINS } from './operations.js';
|
|
2
|
+
import { API_DOMAINS } from './api-operations.js';
|
|
3
|
+
|
|
4
|
+
export const domains = Object.fromEntries(
|
|
5
|
+
[...new Set([...Object.keys(DOMAINS), ...Object.keys(API_DOMAINS)])].map((name) => [
|
|
6
|
+
name,
|
|
7
|
+
{
|
|
8
|
+
...DOMAINS[name],
|
|
9
|
+
...API_DOMAINS[name],
|
|
10
|
+
...(name === 'report'
|
|
11
|
+
? {
|
|
12
|
+
title: DOMAINS[name].title,
|
|
13
|
+
description: DOMAINS[name].description,
|
|
14
|
+
}
|
|
15
|
+
: {}),
|
|
16
|
+
workflows: [...new Set([...(DOMAINS[name]?.workflows ?? []), ...(API_DOMAINS[name]?.workflows ?? [])])],
|
|
17
|
+
operations: {
|
|
18
|
+
...DOMAINS[name]?.operations,
|
|
19
|
+
...API_DOMAINS[name]?.operations,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
]),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export function operationFor(domain, request) {
|
|
26
|
+
if (
|
|
27
|
+
!Object.hasOwn(domains, domain) ||
|
|
28
|
+
!request ||
|
|
29
|
+
typeof request !== 'object' ||
|
|
30
|
+
Array.isArray(request) ||
|
|
31
|
+
typeof request.operation !== 'string' ||
|
|
32
|
+
!Object.hasOwn(domains[domain].operations, request.operation)
|
|
33
|
+
) {
|
|
34
|
+
throw new Error('操作不存在。请先使用 datarc_guide 查看当前业务域公布的操作,再按参数说明重试。');
|
|
35
|
+
}
|
|
36
|
+
const operation = domains[domain].operations[request.operation];
|
|
37
|
+
for (const key of Object.keys(request)) {
|
|
38
|
+
if (key !== 'operation' && !Object.hasOwn(operation.parameters, key))
|
|
39
|
+
throw new Error(`参数 ${key} 不受支持。请按该操作的明确参数重试,不传入命令、凭据或任意接口。`);
|
|
40
|
+
}
|
|
41
|
+
for (const key of operation.required ?? []) {
|
|
42
|
+
if (!Object.hasOwn(request, key)) throw new Error(`缺少 ${key}。请先补齐当前操作的必要信息。`);
|
|
43
|
+
}
|
|
44
|
+
for (const [key, value] of Object.entries(request)) {
|
|
45
|
+
if (key === 'operation') continue;
|
|
46
|
+
validateValue(value, operation.parameters[key], key);
|
|
47
|
+
if (['workspace_id', 'project_id'].includes(key) && (!Number.isSafeInteger(value) || value < 1))
|
|
48
|
+
throw new Error(`${key} 必须是已确认的正整数工作区编号。请先按名称选择并校验工作区。`);
|
|
49
|
+
if (key === 'endpoint') {
|
|
50
|
+
let url;
|
|
51
|
+
try {
|
|
52
|
+
url = new URL(value);
|
|
53
|
+
} catch {
|
|
54
|
+
throw new Error('服务网址无效。请提供北极九章的 HTTP 或 HTTPS 服务入口。');
|
|
55
|
+
}
|
|
56
|
+
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.search)
|
|
57
|
+
throw new Error('服务入口不能含凭据或查询参数。请提供服务网址,勿在参数中粘贴令牌;网页片段由原登录脚本处理。');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return operation;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function validateValue(value, schema, label) {
|
|
64
|
+
if (schema.oneOf) {
|
|
65
|
+
for (const variant of schema.oneOf) {
|
|
66
|
+
try {
|
|
67
|
+
validateValue(value, variant, label);
|
|
68
|
+
return;
|
|
69
|
+
} catch {}
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`${label} 类型无效,请保留原接口返回的版本类型。`);
|
|
72
|
+
}
|
|
73
|
+
const type = schema.type;
|
|
74
|
+
const valid =
|
|
75
|
+
type === 'array'
|
|
76
|
+
? Array.isArray(value)
|
|
77
|
+
: type === 'object'
|
|
78
|
+
? value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
79
|
+
: type === 'integer'
|
|
80
|
+
? Number.isSafeInteger(value)
|
|
81
|
+
: type === 'number'
|
|
82
|
+
? typeof value === 'number' && Number.isFinite(value)
|
|
83
|
+
: typeof value === type;
|
|
84
|
+
if (!valid || (schema.enum && !schema.enum.includes(value))) throw new Error(`${label} 类型或取值无效。请使用操作说明中的类型和允许值。`);
|
|
85
|
+
if (typeof value === 'string' && (value.includes('\0') || (schema.file && value === '-')))
|
|
86
|
+
throw new Error(`${label} 无效。请使用正常文本或已有本地文件路径,不使用标准输入占位符。`);
|
|
87
|
+
if (
|
|
88
|
+
typeof value === 'number' &&
|
|
89
|
+
((schema.minimum !== undefined && value < schema.minimum) || (schema.maximum !== undefined && value > schema.maximum))
|
|
90
|
+
)
|
|
91
|
+
throw new Error(`${label} 超出范围。请按操作说明调整后重试。`);
|
|
92
|
+
if (Array.isArray(value)) {
|
|
93
|
+
for (const item of value) validateValue(item, schema.items ?? { type: 'string' }, label);
|
|
94
|
+
}
|
|
95
|
+
if (type === 'object') {
|
|
96
|
+
for (const key of schema.required ?? []) if (!Object.hasOwn(value, key)) throw new Error(`${label}.${key} 缺失。请补齐后重试。`);
|
|
97
|
+
for (const [key, item] of Object.entries(value)) {
|
|
98
|
+
if (!Object.hasOwn(schema.properties ?? {}, key)) throw new Error(`${label}.${key} 不受支持。请使用操作公布的字段。`);
|
|
99
|
+
validateValue(item, schema.properties[key], `${label}.${key}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function publicSchema(parameter) {
|
|
105
|
+
const schema = {};
|
|
106
|
+
for (const key of ['type', 'description', 'enum', 'const', 'minimum', 'maximum']) if (parameter[key] !== undefined) schema[key] = parameter[key];
|
|
107
|
+
if (parameter.oneOf) schema.oneOf = parameter.oneOf.map(publicSchema);
|
|
108
|
+
if (parameter.items) schema.items = publicSchema(parameter.items);
|
|
109
|
+
if (parameter.properties)
|
|
110
|
+
schema.properties = Object.fromEntries(Object.entries(parameter.properties).map(([key, value]) => [key, publicSchema(value)]));
|
|
111
|
+
if (parameter.type === 'object') {
|
|
112
|
+
schema.additionalProperties = false;
|
|
113
|
+
schema.required = parameter.required ?? [];
|
|
114
|
+
}
|
|
115
|
+
return schema;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function requestSchema(domain) {
|
|
119
|
+
return {
|
|
120
|
+
oneOf: Object.entries(domains[domain].operations).map(([name, operation]) => ({
|
|
121
|
+
type: 'object',
|
|
122
|
+
additionalProperties: false,
|
|
123
|
+
description: operation.description ?? name,
|
|
124
|
+
properties: {
|
|
125
|
+
operation: { type: 'string', const: name },
|
|
126
|
+
...Object.fromEntries(Object.entries(operation.parameters).map(([key, value]) => [key, publicSchema(value)])),
|
|
127
|
+
},
|
|
128
|
+
required: ['operation', ...(operation.required ?? [])],
|
|
129
|
+
})),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function commandArguments(operation, request) {
|
|
134
|
+
const args = Array.isArray(operation.command) ? [...operation.command] : operation.command ? [operation.command] : [];
|
|
135
|
+
const positional = Object.entries(operation.parameters)
|
|
136
|
+
.filter(([, value]) => value.position !== undefined)
|
|
137
|
+
.sort((a, b) => a[1].position - b[1].position);
|
|
138
|
+
for (const [key] of positional) if (request[key] !== undefined) args.push(String(request[key]));
|
|
139
|
+
for (const [key, parameter] of Object.entries(operation.parameters)) {
|
|
140
|
+
if (request[key] === undefined || parameter.position !== undefined || !parameter.flag) continue;
|
|
141
|
+
if (parameter.switch) {
|
|
142
|
+
if (request[key]) args.push('--' + parameter.flag);
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
for (const value of parameter.repeat ? request[key] : [request[key]]) args.push('--' + parameter.flag, String(value));
|
|
146
|
+
}
|
|
147
|
+
return args;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function isInputFile(key, parameter) {
|
|
151
|
+
return (
|
|
152
|
+
['string', 'array'].includes(parameter.type) &&
|
|
153
|
+
!parameter.switch &&
|
|
154
|
+
(parameter.file === true ||
|
|
155
|
+
(key.endsWith('_file') && key !== 'output_file') ||
|
|
156
|
+
key === 'input_files' ||
|
|
157
|
+
['from_result', 'current', 'base'].includes(key))
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const credentialKeys = new Set([
|
|
162
|
+
'token',
|
|
163
|
+
'accesstoken',
|
|
164
|
+
'refreshtoken',
|
|
165
|
+
'idtoken',
|
|
166
|
+
'datarctoken',
|
|
167
|
+
'apikey',
|
|
168
|
+
'openapikey',
|
|
169
|
+
'datarcopenapikey',
|
|
170
|
+
'authorization',
|
|
171
|
+
'password',
|
|
172
|
+
'passwd',
|
|
173
|
+
'pwd',
|
|
174
|
+
'secret',
|
|
175
|
+
'clientsecret',
|
|
176
|
+
'privatekey',
|
|
177
|
+
'secretkey',
|
|
178
|
+
'accesskey',
|
|
179
|
+
'awsaccesskeyid',
|
|
180
|
+
'awssecretaccesskey',
|
|
181
|
+
'devicecode',
|
|
182
|
+
'imagetoken',
|
|
183
|
+
'databaseurl',
|
|
184
|
+
'connectionurl',
|
|
185
|
+
'connectionstring',
|
|
186
|
+
]);
|
|
187
|
+
|
|
188
|
+
export function redact(value, key = '') {
|
|
189
|
+
const normalized = key.replace(/[_-]/g, '').toLowerCase();
|
|
190
|
+
if (credentialKeys.has(normalized)) return '***';
|
|
191
|
+
if (Array.isArray(value)) return value.map((item) => redact(item));
|
|
192
|
+
if (value && typeof value === 'object') return Object.fromEntries(Object.entries(value).map(([name, item]) => [name, redact(item, name)]));
|
|
193
|
+
if (typeof value === 'string') return value.replace(/(https?:\/\/)[^\s/@]+:[^\s/@]+@/gi, '$1***@');
|
|
194
|
+
return value;
|
|
195
|
+
}
|