@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 { 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
|
-
}
|
|
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
|
+
}
|