File size: 1,370 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ObjectJsonSchema7 } from "app-types/util";
import { NodeKind, WorkflowNodeData } from "../workflow.interface";
import {
  defaultObjectJsonSchema,
  findJsonSchemaByPath,
} from "../shared.workflow";
import { JSONSchema7 } from "json-schema";

export function extractNodeDependencySchema({
  targetId,
  nodes,
}: {
  targetId: string;
  nodes: WorkflowNodeData[];
}): ObjectJsonSchema7 {
  const schema = structuredClone(defaultObjectJsonSchema);
  const target = nodes.find((node) => node.id === targetId);
  if (!target) {
    return schema;
  }

  if (target.kind === NodeKind.Input) {
    return target.outputSchema;
  }
  if (target.kind === NodeKind.Output) {
    const properties = target.outputData.reduce(
      (acc, cur) => {
        if (!cur.key) return acc;
        acc[cur.key] = {
          type: "string", // default
        };
        const source = cur.source;
        if (!source) return acc;
        const sourceNode = nodes.find((node) => node.id === source.nodeId);
        if (!sourceNode) return acc;
        const sourceSchema = findJsonSchemaByPath(
          sourceNode.outputSchema,
          source.path,
        );
        acc[cur.key] = sourceSchema || { type: "string" };
        return acc;
      },
      {} as Record<string, JSONSchema7>,
    );
    schema.properties = properties;
    return schema;
  }

  return schema;
}