@toloka-platform/sdk 0.25.1 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/cli/toloka.js +186 -165
  2. package/dist/cli/toloka.js.jsc +0 -0
  3. package/dist/cli/toloka.js.jsc.sha256 +1 -1
  4. package/dist/cli/toloka.js.sha256 +1 -1
  5. package/examples/app-crowdtesting/nodes/app-flow-test/node.ts +24 -0
  6. package/examples/egocentric-video-collection/nodes/record-activity/node.ts +24 -0
  7. package/examples/face-image-collection/nodes/annotate-faces/node.ts +16 -1
  8. package/examples/product-moderation/nodes/moderate-product/instruction.ts +4 -0
  9. package/examples/product-moderation/nodes/moderate-product/node.ts +19 -0
  10. package/examples/video-pair-rating/nodes/adjudicate/node.ts +18 -1
  11. package/examples/video-pair-rating/nodes/adjudicate_op/node.ts +18 -1
  12. package/examples/video-pair-rating/nodes/overall_preference_pick/node.ts +18 -1
  13. package/examples/video-pair-rating/nodes/rate_pair/node.ts +18 -1
  14. package/openapi/gen/api.schemas.ts +50 -42
  15. package/package.json +3 -3
  16. package/skills/catalog.json +6 -6
  17. package/skills/toloka-build/SKILL.md +6 -10
  18. package/skills/toloka-build/references/human-work.md +97 -8
  19. package/skills/toloka-build/references/workflow-and-cli.md +12 -9
  20. package/src/authoring/index.ts +13 -2
  21. package/src/checks/task-ui-typecheck.ts +1 -1
  22. package/src/cli/analytics.ts +1 -0
  23. package/src/cli/commands/schema.ts +27 -0
  24. package/src/cli/commands/source.ts +11 -2
  25. package/src/cli/format-error.ts +6 -0
  26. package/src/cli/help-all.ts +32 -0
  27. package/src/cli/index.ts +1 -0
  28. package/src/cli/program.ts +29 -1
  29. package/src/cli/project.ts +134 -15
  30. package/src/cli/schema.ts +633 -0
  31. package/src/graph-rules.json +3 -1
  32. package/src/importer.ts +39 -0
  33. package/src/index.ts +1 -0
  34. package/src/pipeline-graph-v1.contract.json +12 -4
  35. package/src/validate.ts +23 -2
@@ -73,12 +73,23 @@ export type ProjectStatusResult = {
73
73
  readonly output: string;
74
74
  };
75
75
 
76
- export type ApplyProjectOptions = PlanProjectOptions;
76
+ export type ApplyProjectOptions = PlanProjectOptions & {
77
+ readonly allowRemovals?: boolean;
78
+ readonly onPlan?: (output: string) => void;
79
+ };
80
+
81
+ export type SkippedPipelineRemovals = {
82
+ readonly pipeline: string;
83
+ readonly removals: readonly ProjectPlanChange[];
84
+ };
77
85
 
78
86
  export type ApplyProjectResult = {
79
87
  readonly action: "changed" | "unchanged";
80
88
  readonly state: TolokaState;
81
89
  readonly changes: readonly ProjectPlanChange[];
90
+ readonly skipped: readonly SkippedPipelineRemovals[];
91
+ readonly remainingChanges: readonly ProjectPlanChange[] | undefined;
92
+ readonly converged: boolean;
82
93
  readonly writes: number;
83
94
  readonly output: string;
84
95
  };
@@ -207,6 +218,31 @@ const withCanonicalExpertListConditions = (node: unknown): unknown =>
207
218
  }
208
219
  : node;
209
220
 
221
+ const withoutEmptyRelations = (entity: unknown): unknown => {
222
+ if (!isPlainObject(entity) || !Array.isArray(entity.relations) || entity.relations.length > 0)
223
+ return entity;
224
+ const { relations: _relations, ...rest } = entity;
225
+ return rest;
226
+ };
227
+
228
+ const withoutEmptyEntityRelations = (node: unknown): unknown => {
229
+ if (!isPlainObject(node) || !isPlainObject(node.settings)) return node;
230
+ const { entities } = node.settings;
231
+ if (!isPlainObject(entities)) return node;
232
+ return {
233
+ ...node,
234
+ settings: {
235
+ ...node.settings,
236
+ entities: Object.fromEntries(
237
+ Object.entries(entities).map(([direction, list]) => [
238
+ direction,
239
+ Array.isArray(list) ? list.map(withoutEmptyRelations) : list,
240
+ ]),
241
+ ),
242
+ },
243
+ };
244
+ };
245
+
210
246
  const withAudienceSourcingDefaults = (node: PipelineGraphV1["nodes"][number]): unknown => {
211
247
  if (node.type !== "generation" && node.type !== "quorum") return node;
212
248
 
@@ -278,6 +314,15 @@ const diffKeyed = <Item>(
278
314
  return changes;
279
315
  };
280
316
 
317
+ const layoutForNodes = (
318
+ layout: PipelineGraphV1["layout"],
319
+ nodes: PipelineGraphV1["nodes"],
320
+ ): PipelineGraphV1["layout"] => {
321
+ if (layout === undefined) return undefined;
322
+ const nodeIds = new Set(nodes.map((node) => node.id));
323
+ return Object.fromEntries(Object.entries(layout).filter(([nodeId]) => nodeIds.has(nodeId)));
324
+ };
325
+
281
326
  export function diffPipelineGraphs(
282
327
  authoredGraph: PipelineGraphV1,
283
328
  liveGraph: PipelineGraphV1,
@@ -308,8 +353,10 @@ export function diffPipelineGraphs(
308
353
  liveNodes,
309
354
  (authored, live) =>
310
355
  matchesAuthored(
311
- withCanonicalExpertListConditions(authored),
312
- withCanonicalExpertListConditions(withAudienceSourcingDefaults(live)),
356
+ withoutEmptyEntityRelations(withCanonicalExpertListConditions(authored)),
357
+ withoutEmptyEntityRelations(
358
+ withCanonicalExpertListConditions(withAudienceSourcingDefaults(live)),
359
+ ),
313
360
  ),
314
361
  (node, action) => ({
315
362
  kind: "node",
@@ -327,7 +374,8 @@ export function diffPipelineGraphs(
327
374
  label: connectionLabel(connection, action === "removed" ? liveNodes : authoredNodes),
328
375
  }),
329
376
  ),
330
- ...(valueKey(authoredGraph.layout) === valueKey(liveGraph.layout)
377
+ ...(valueKey(authoredGraph.layout) ===
378
+ valueKey(layoutForNodes(liveGraph.layout, authoredGraph.nodes))
331
379
  ? []
332
380
  : [{ kind: "layout" as const, action: "changed" as const, label: "pipeline layout" }]),
333
381
  ];
@@ -359,7 +407,9 @@ const graphWithLiveState = (
359
407
  const liveNodes = new Map(liveGraph.nodes.map((node) => [node.id, node]));
360
408
  return {
361
409
  ...authoredGraph,
362
- ...(liveGraph.layout === undefined ? {} : { layout: liveGraph.layout }),
410
+ ...(liveGraph.layout === undefined
411
+ ? {}
412
+ : { layout: layoutForNodes(liveGraph.layout, authoredGraph.nodes) }),
363
413
  nodes: authoredGraph.nodes.map((node) => {
364
414
  const live = liveNodes.get(node.id);
365
415
  switch (node.type) {
@@ -954,7 +1004,26 @@ export async function applyProject(
954
1004
  { hint: { description: "Add the changed columns under new field names, or create a new dataset for the new schema." } },
955
1005
  );
956
1006
 
957
- const ruleViolations = inspection.pipelines
1007
+ const skipped: SkippedPipelineRemovals[] =
1008
+ options.allowRemovals === true
1009
+ ? []
1010
+ : inspection.pipelines.flatMap((pipeline) => {
1011
+ const removals = pipeline.graphChanges
1012
+ .filter(
1013
+ (change) =>
1014
+ change.action === "removed" && (change.kind === "node" || change.kind === "field"),
1015
+ )
1016
+ .map((change) => ({ ...change, pipeline: pipeline.source.sourceDirectory }));
1017
+ return pipeline.pipeline === undefined || removals.length === 0
1018
+ ? []
1019
+ : [{ pipeline: pipeline.source.sourceDirectory, removals }];
1020
+ });
1021
+ const skippedDirectories = new Set(skipped.map(({ pipeline }) => pipeline));
1022
+ const writablePipelines = inspection.pipelines.filter(
1023
+ (pipeline) => !skippedDirectories.has(pipeline.source.sourceDirectory),
1024
+ );
1025
+
1026
+ const ruleViolations = writablePipelines
958
1027
  .filter((pipeline) => pipeline.pipeline === undefined || pipeline.graphChanges.length > 0)
959
1028
  .flatMap((pipeline) =>
960
1029
  platformRuleIssues(pipeline.source.definition.graph).map(
@@ -967,6 +1036,9 @@ export async function applyProject(
967
1036
  { hint: { description: "Fix the listed nodes in source; toloka check reports the same rules as warnings." } },
968
1037
  );
969
1038
 
1039
+ if (inspection.changes.length > 0)
1040
+ options.onPlan?.(["Applying this plan:", planOutput(inspection.changes, [])].join("\n"));
1041
+
970
1042
  let writes = 0;
971
1043
  const importedPipelineIds = new Map<string, string>();
972
1044
  for (const pipelineInspection of inspection.pipelines) {
@@ -1005,12 +1077,14 @@ export async function applyProject(
1005
1077
 
1006
1078
  const pipelineIds = new Map<string, string>();
1007
1079
  for (const pipelineInspection of inspection.pipelines) {
1008
- const applied = await applyPipeline(
1009
- client,
1010
- pipelineInspection,
1011
- datasetIds,
1012
- importedPipelineIds.get(pipelineInspection.source.sourceDirectory),
1013
- );
1080
+ const applied = skippedDirectories.has(pipelineInspection.source.sourceDirectory)
1081
+ ? { pipelineId: pipelineInspection.pipeline?.id ?? "", writes: 0 }
1082
+ : await applyPipeline(
1083
+ client,
1084
+ pipelineInspection,
1085
+ datasetIds,
1086
+ importedPipelineIds.get(pipelineInspection.source.sourceDirectory),
1087
+ );
1014
1088
  pipelineIds.set(pipelineInspection.source.sourceDirectory, applied.pipelineId);
1015
1089
  writes += applied.writes;
1016
1090
  }
@@ -1040,14 +1114,59 @@ export async function applyProject(
1040
1114
  await writeTolokaState(options.sourceRoot, nextState);
1041
1115
  const action = writes === 0 ? "unchanged" : "changed";
1042
1116
  const plannedChanges = inspection.changes.length;
1043
- const output =
1117
+ if (plannedChanges === 0)
1118
+ return {
1119
+ action,
1120
+ state: nextState,
1121
+ changes: [],
1122
+ skipped: [],
1123
+ remainingChanges: [],
1124
+ converged: true,
1125
+ writes,
1126
+ output: "No changes.",
1127
+ };
1128
+ const replan =
1044
1129
  writes === 0
1045
- ? "No changes."
1046
- : `Applied ${plannedChanges} planned ${plannedChanges === 1 ? "change" : "changes"} with ${writes} platform ${writes === 1 ? "write" : "writes"} to project ${inspection.projectId}.`;
1130
+ ? { changes: inspection.changes }
1131
+ : await inspectProject(client, { ...options, source: inspection.source }).then(
1132
+ ({ changes }) => ({ changes }),
1133
+ (error: unknown) => ({ error: error instanceof Error ? error.message : String(error) }),
1134
+ );
1135
+ const isHeldBack = (change: ProjectPlanChange) =>
1136
+ change.pipeline !== undefined && skippedDirectories.has(change.pipeline);
1137
+ const heldChanges = inspection.changes.filter(isHeldBack).length;
1138
+ const appliedCount = plannedChanges - heldChanges;
1139
+ const remainingChanges = "changes" in replan ? replan.changes : undefined;
1140
+ const unexpected = (remainingChanges ?? []).filter((change) => !isHeldBack(change));
1141
+ const converged = remainingChanges !== undefined && unexpected.length === 0;
1142
+ const output = [
1143
+ `Applied ${heldChanges === 0 ? "" : `${appliedCount} of `}${plannedChanges} planned ${plannedChanges === 1 ? "change" : "changes"} with ${writes} platform ${writes === 1 ? "write" : "writes"} to project ${inspection.projectId}.`,
1144
+ ...skipped.flatMap(({ pipeline, removals }) => [
1145
+ "",
1146
+ `Skipped ${pipeline}: its plan removes`,
1147
+ ...removals.map((change) => ` ${changeLine(change)}`),
1148
+ "Nothing in this pipeline was written. Once the removal is agreed, run: toloka apply --allow-removals",
1149
+ ]),
1150
+ "",
1151
+ ...(remainingChanges === undefined
1152
+ ? [
1153
+ `Warning: the changes were applied, but planning again to verify them failed: ${"error" in replan ? replan.error : ""}`,
1154
+ "Run toloka plan to check the result.",
1155
+ ]
1156
+ : unexpected.length > 0
1157
+ ? [
1158
+ "Plan after apply (not converged: the platform still differs from the source):",
1159
+ planOutput(remainingChanges, []),
1160
+ ]
1161
+ : ["Plan after apply:", planOutput(remainingChanges, [])]),
1162
+ ].join("\n");
1047
1163
  return {
1048
1164
  action,
1049
1165
  state: nextState,
1050
1166
  changes: inspection.changes,
1167
+ skipped,
1168
+ remainingChanges,
1169
+ converged,
1051
1170
  writes,
1052
1171
  output,
1053
1172
  };