@toloka-platform/sdk 0.26.0 → 0.26.2
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/dist/cli/toloka.js +178 -176
- package/dist/cli/toloka.js.jsc +0 -0
- package/dist/cli/toloka.js.jsc.sha256 +1 -1
- package/dist/cli/toloka.js.sha256 +1 -1
- package/package.json +2 -2
- package/skills/catalog.json +6 -6
- package/skills/toloka-build/SKILL.md +9 -7
- package/skills/toloka-build/references/workflow-and-cli.md +6 -6
- package/src/cli/check.ts +60 -30
- package/src/cli/commands/source.ts +14 -6
- package/src/cli/pipeline-selector.ts +68 -0
- package/src/cli/poll.ts +26 -0
- package/src/cli/project.ts +63 -50
- package/src/cli/runs.ts +22 -14
- package/src/cli/source.ts +16 -2
package/src/cli/project.ts
CHANGED
|
@@ -14,9 +14,11 @@ import { platformRuleIssues } from "../validate";
|
|
|
14
14
|
import type { TolokaState } from "../state";
|
|
15
15
|
import { assertProjectSelected } from "./project-context";
|
|
16
16
|
import { collectGraphSecretNames } from "./secrets";
|
|
17
|
-
import { loadProjectSource } from "./source";
|
|
17
|
+
import { listPipelineSourceDirectories, loadProjectSource } from "./source";
|
|
18
18
|
import type { ProjectPipelineSource, ProjectSource } from "./source";
|
|
19
|
+
import { pipelineDirectoryName, resolvePipelineDirectories } from "./pipeline-selector";
|
|
19
20
|
import { assertCli, REPORT_HINT } from "./cli-error";
|
|
21
|
+
import { pollUntil } from "./poll";
|
|
20
22
|
|
|
21
23
|
export type PipelinePlanChange = {
|
|
22
24
|
readonly kind: "node" | "connection" | "field" | "layout";
|
|
@@ -57,6 +59,7 @@ export type ImportProjectResult = {
|
|
|
57
59
|
export type PlanProjectOptions = {
|
|
58
60
|
readonly sourceRoot: string;
|
|
59
61
|
readonly source?: ProjectSource;
|
|
62
|
+
readonly pipelines?: readonly string[];
|
|
60
63
|
readonly projectId?: string;
|
|
61
64
|
readonly listAllSecrets?: boolean;
|
|
62
65
|
};
|
|
@@ -138,6 +141,7 @@ type PipelineInspection = {
|
|
|
138
141
|
type ProjectInspection = {
|
|
139
142
|
readonly projectId: string;
|
|
140
143
|
readonly source: ProjectSource;
|
|
144
|
+
readonly selected: boolean;
|
|
141
145
|
readonly state: TolokaState | undefined;
|
|
142
146
|
readonly datasets: readonly DatasetInspection[];
|
|
143
147
|
readonly pipelines: readonly PipelineInspection[];
|
|
@@ -701,9 +705,19 @@ const inspectProject = async (
|
|
|
701
705
|
client: PotatoClient,
|
|
702
706
|
options: PlanProjectOptions,
|
|
703
707
|
): Promise<ProjectInspection> => {
|
|
704
|
-
const source = options.source ?? (await loadProjectSource(options.sourceRoot));
|
|
705
|
-
validateSource(source);
|
|
706
708
|
const state = await readTolokaState(options.sourceRoot);
|
|
709
|
+
const sourceDirectories =
|
|
710
|
+
options.pipelines === undefined || options.pipelines.length === 0
|
|
711
|
+
? undefined
|
|
712
|
+
: await listPipelineSourceDirectories(options.sourceRoot);
|
|
713
|
+
const selectedDirectories =
|
|
714
|
+
sourceDirectories === undefined
|
|
715
|
+
? undefined
|
|
716
|
+
: resolvePipelineDirectories({ selectors: options.pipelines ?? [], directories: sourceDirectories, state });
|
|
717
|
+
const source =
|
|
718
|
+
options.source ?? (await loadProjectSource(options.sourceRoot, selectedDirectories));
|
|
719
|
+
validateSource(source);
|
|
720
|
+
const selected = selectedDirectories !== undefined;
|
|
707
721
|
const projectId = options.projectId ?? state?.projectId;
|
|
708
722
|
assertProjectSelected(projectId);
|
|
709
723
|
|
|
@@ -718,14 +732,16 @@ const inspectProject = async (
|
|
|
718
732
|
shouldListSecrets ? client.secrets.list({ projectId }) : Promise.resolve([]),
|
|
719
733
|
]);
|
|
720
734
|
const requiredDatasets = desiredDatasets(source);
|
|
721
|
-
const removedDatasets =
|
|
722
|
-
|
|
723
|
-
.
|
|
735
|
+
const removedDatasets = selected
|
|
736
|
+
? []
|
|
737
|
+
: Object.keys(bindings?.datasets ?? {})
|
|
738
|
+
.filter((name) => !requiredDatasets.some((dataset) => dataset.name === name))
|
|
739
|
+
.toSorted();
|
|
740
|
+
const presentDirectories = new Set(
|
|
741
|
+
sourceDirectories ?? source.pipelines.map((pipeline) => pipeline.sourceDirectory),
|
|
742
|
+
);
|
|
724
743
|
const removedPipelines = Object.keys(bindings?.pipelines ?? {})
|
|
725
|
-
.filter(
|
|
726
|
-
(sourceDirectory) =>
|
|
727
|
-
!source.pipelines.some((pipeline) => pipeline.sourceDirectory === sourceDirectory),
|
|
728
|
-
)
|
|
744
|
+
.filter((sourceDirectory) => !presentDirectories.has(sourceDirectory))
|
|
729
745
|
.toSorted();
|
|
730
746
|
const datasets = await inspectDatasets(client, requiredDatasets, bindings, liveDatasets);
|
|
731
747
|
const pipelines = await inspectPipelines(client, source, bindings, datasets, livePipelines);
|
|
@@ -736,6 +752,7 @@ const inspectProject = async (
|
|
|
736
752
|
return {
|
|
737
753
|
projectId,
|
|
738
754
|
source,
|
|
755
|
+
selected,
|
|
739
756
|
state,
|
|
740
757
|
datasets,
|
|
741
758
|
pipelines,
|
|
@@ -749,12 +766,6 @@ const inspectProject = async (
|
|
|
749
766
|
};
|
|
750
767
|
};
|
|
751
768
|
|
|
752
|
-
const sourceDirectoryBase = (name: string) =>
|
|
753
|
-
name
|
|
754
|
-
.toLowerCase()
|
|
755
|
-
.match(/[a-z0-9]+/gu)
|
|
756
|
-
?.join("-") || "pipeline";
|
|
757
|
-
|
|
758
769
|
const hash = (value: string) => {
|
|
759
770
|
let result = 0x811c9dc5;
|
|
760
771
|
for (let index = 0; index < value.length; index += 1) {
|
|
@@ -765,10 +776,10 @@ const hash = (value: string) => {
|
|
|
765
776
|
};
|
|
766
777
|
|
|
767
778
|
const pipelineSourceDirectories = (pipelines: readonly Pipeline[]) => {
|
|
768
|
-
const groups = Map.groupBy(pipelines, (pipeline) =>
|
|
779
|
+
const groups = Map.groupBy(pipelines, (pipeline) => pipelineDirectoryName(pipeline.name));
|
|
769
780
|
return new Map(
|
|
770
781
|
pipelines.map((pipeline) => {
|
|
771
|
-
const base =
|
|
782
|
+
const base = pipelineDirectoryName(pipeline.name);
|
|
772
783
|
const collision = (groups.get(base)?.length ?? 0) > 1;
|
|
773
784
|
return [pipeline.id, `pipelines/${collision ? `${base}-${hash(pipeline.id)}` : base}`];
|
|
774
785
|
}),
|
|
@@ -1089,9 +1100,15 @@ export async function applyProject(
|
|
|
1089
1100
|
writes += applied.writes;
|
|
1090
1101
|
}
|
|
1091
1102
|
|
|
1103
|
+
const keptBindings =
|
|
1104
|
+
inspection.selected && inspection.state?.projectId === inspection.projectId
|
|
1105
|
+
? inspection.state
|
|
1106
|
+
: undefined;
|
|
1092
1107
|
const nextState: TolokaState = {
|
|
1093
1108
|
projectId: inspection.projectId,
|
|
1094
|
-
pipelines:
|
|
1109
|
+
pipelines: {
|
|
1110
|
+
...keptBindings?.pipelines,
|
|
1111
|
+
...Object.fromEntries(
|
|
1095
1112
|
inspection.source.pipelines.map((pipeline) => {
|
|
1096
1113
|
const pipelineId = pipelineIds.get(pipeline.sourceDirectory);
|
|
1097
1114
|
assertCli(
|
|
@@ -1101,14 +1118,18 @@ export async function applyProject(
|
|
|
1101
1118
|
);
|
|
1102
1119
|
return [pipeline.sourceDirectory, { pipelineId }];
|
|
1103
1120
|
}),
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1121
|
+
),
|
|
1122
|
+
},
|
|
1123
|
+
datasets: {
|
|
1124
|
+
...keptBindings?.datasets,
|
|
1125
|
+
...Object.fromEntries(
|
|
1126
|
+
inspection.datasets.map((dataset) => {
|
|
1127
|
+
const datasetId = datasetIds.get(dataset.name);
|
|
1128
|
+
assertCli(datasetId, `Dataset ${dataset.name} has no resolved ID.`, { hint: REPORT_HINT });
|
|
1129
|
+
return [dataset.name, { datasetId }];
|
|
1130
|
+
}),
|
|
1131
|
+
),
|
|
1132
|
+
},
|
|
1112
1133
|
};
|
|
1113
1134
|
if (valueKey(inspection.state) !== valueKey(nextState))
|
|
1114
1135
|
await writeTolokaState(options.sourceRoot, nextState);
|
|
@@ -1238,11 +1259,6 @@ type RunningValidation = {
|
|
|
1238
1259
|
readonly failure?: string;
|
|
1239
1260
|
};
|
|
1240
1261
|
|
|
1241
|
-
const sleep = (ms: number) =>
|
|
1242
|
-
new Promise<void>((resolve) => {
|
|
1243
|
-
setTimeout(resolve, ms);
|
|
1244
|
-
});
|
|
1245
|
-
|
|
1246
1262
|
const pollValidationEntry = async (
|
|
1247
1263
|
results: PipelineValidationResult[],
|
|
1248
1264
|
entry: RunningValidation,
|
|
@@ -1262,17 +1278,16 @@ const waitForRunningChecks = async (
|
|
|
1262
1278
|
wait: NonNullable<ValidateProjectOptions["wait"]>,
|
|
1263
1279
|
startedAt: number,
|
|
1264
1280
|
) => {
|
|
1265
|
-
const
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
}
|
|
1281
|
+
const pending = await pollUntil({
|
|
1282
|
+
initial: running,
|
|
1283
|
+
settled: (entries) => entries.length === 0,
|
|
1284
|
+
poll: async (entries) =>
|
|
1285
|
+
(await Promise.all(entries.map((entry) => pollValidationEntry(results, entry)))).filter(
|
|
1286
|
+
(entry): entry is RunningValidation => entry !== undefined,
|
|
1287
|
+
),
|
|
1288
|
+
deadline: startedAt + wait.timeoutMs,
|
|
1289
|
+
maxIntervalMs: wait.pollIntervalMs,
|
|
1290
|
+
});
|
|
1276
1291
|
const waitedSeconds = Math.round((Date.now() - startedAt) / 1000);
|
|
1277
1292
|
for (const entry of pending) {
|
|
1278
1293
|
const result = results[entry.index]!;
|
|
@@ -1359,13 +1374,11 @@ export async function validateProject(
|
|
|
1359
1374
|
const startedAt = Date.now();
|
|
1360
1375
|
const inspection = await inspectProject(client, options);
|
|
1361
1376
|
const { wait } = options;
|
|
1362
|
-
const
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
if (poll !== undefined) running.push({ index: results.length - 1, validate: poll });
|
|
1368
|
-
}
|
|
1377
|
+
const entries = await Promise.all(
|
|
1378
|
+
inspection.pipelines.map((pipeline) => validatePipelineEntry(client, inspection, pipeline, wait)),
|
|
1379
|
+
);
|
|
1380
|
+
const results = entries.map(({ result }) => result);
|
|
1381
|
+
const running = entries.flatMap(({ poll }, index) => (poll === undefined ? [] : [{ index, validate: poll }]));
|
|
1369
1382
|
if (wait !== undefined) await waitForRunningChecks(results, running, wait, startedAt);
|
|
1370
1383
|
const valid = results.every(
|
|
1371
1384
|
(result) => result.status !== "invalid" && result.status !== "checks_running",
|
package/src/cli/runs.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type { TolokaState } from "../state";
|
|
|
5
5
|
import type { RunItem, RunJob } from "../index";
|
|
6
6
|
import { assertCli } from "./cli-error";
|
|
7
7
|
import { apiErrorDetails } from "./format-error";
|
|
8
|
+
import { matchesNormalizedPipelineName } from "./pipeline-selector";
|
|
9
|
+
import { pollUntil } from "./poll";
|
|
8
10
|
|
|
9
11
|
export type PipelineRef = {
|
|
10
12
|
readonly pipelineId: string;
|
|
@@ -183,6 +185,14 @@ const resolveToken = (token: string, live: readonly PipelineRef[]): PipelineRef
|
|
|
183
185
|
);
|
|
184
186
|
if (basename !== undefined) return basename;
|
|
185
187
|
|
|
188
|
+
const normalized = live.filter((ref) => matchesNormalizedPipelineName(token, ref));
|
|
189
|
+
const byNormalizedName = uniqueMatch(
|
|
190
|
+
normalized,
|
|
191
|
+
`Multiple pipelines match ${token}: ${normalized.map(formatPipelineChoice).join(", ")}.`,
|
|
192
|
+
unknown,
|
|
193
|
+
);
|
|
194
|
+
if (byNormalizedName !== undefined) return byNormalizedName;
|
|
195
|
+
|
|
186
196
|
assertCli(
|
|
187
197
|
false,
|
|
188
198
|
unknown,
|
|
@@ -257,24 +267,22 @@ type LintJob = Awaited<ReturnType<PotatoClient["checks"]["runLints"]>>["checks"]
|
|
|
257
267
|
|
|
258
268
|
const isLintFinished = (job: LintJob) => job.status === "succeeded" || job.status === "failed";
|
|
259
269
|
|
|
260
|
-
const waitForLints =
|
|
270
|
+
const waitForLints = (
|
|
261
271
|
client: PotatoClient,
|
|
262
272
|
jobs: readonly LintJob[],
|
|
263
273
|
timeoutMs: number,
|
|
264
274
|
intervalMs: number,
|
|
265
|
-
): Promise<readonly LintJob[]> =>
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
return current;
|
|
277
|
-
};
|
|
275
|
+
): Promise<readonly LintJob[]> =>
|
|
276
|
+
pollUntil({
|
|
277
|
+
initial: jobs,
|
|
278
|
+
settled: (current) => current.every(isLintFinished),
|
|
279
|
+
poll: (current) =>
|
|
280
|
+
Promise.all(
|
|
281
|
+
current.map(async (job) => (isLintFinished(job) ? job : client.checks.get({ checkRunId: job.id }))),
|
|
282
|
+
),
|
|
283
|
+
deadline: Date.now() + timeoutMs,
|
|
284
|
+
maxIntervalMs: intervalMs,
|
|
285
|
+
});
|
|
278
286
|
|
|
279
287
|
const runLints = async (
|
|
280
288
|
client: PotatoClient,
|
package/src/cli/source.ts
CHANGED
|
@@ -56,8 +56,22 @@ export async function findSourceRoot(startDirectory: string): Promise<string | u
|
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
const sourceDirectoryOf = (sourceRoot: string, modulePath: string) =>
|
|
60
|
+
relative(sourceRoot, dirname(modulePath)).split(sep).join("/");
|
|
61
|
+
|
|
62
|
+
export async function listPipelineSourceDirectories(sourceRoot: string): Promise<string[]> {
|
|
60
63
|
const modulePaths = await findPipelineModules(join(sourceRoot, "pipelines"));
|
|
64
|
+
return modulePaths.map((modulePath) => sourceDirectoryOf(sourceRoot, modulePath));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function loadProjectSource(
|
|
68
|
+
sourceRoot: string,
|
|
69
|
+
sourceDirectories?: readonly string[],
|
|
70
|
+
): Promise<ProjectSource> {
|
|
71
|
+
const selected = sourceDirectories === undefined ? undefined : new Set(sourceDirectories);
|
|
72
|
+
const modulePaths = (await findPipelineModules(join(sourceRoot, "pipelines"))).filter(
|
|
73
|
+
(modulePath) => selected === undefined || selected.has(sourceDirectoryOf(sourceRoot, modulePath)),
|
|
74
|
+
);
|
|
61
75
|
const pipelines = await Promise.all(
|
|
62
76
|
modulePaths.map(async (modulePath) => {
|
|
63
77
|
const moduleStat = await stat(modulePath);
|
|
@@ -71,7 +85,7 @@ export async function loadProjectSource(sourceRoot: string): Promise<ProjectSour
|
|
|
71
85
|
);
|
|
72
86
|
const definition: unknown = Reflect.get(module, "default");
|
|
73
87
|
validatePipelineDefinition(definition);
|
|
74
|
-
const sourceDirectory =
|
|
88
|
+
const sourceDirectory = sourceDirectoryOf(sourceRoot, modulePath);
|
|
75
89
|
return {
|
|
76
90
|
sourceDirectory,
|
|
77
91
|
name: basename(sourceDirectory),
|