# 工作流数据结构

# 数据结构

# 顶层数据结构

export interface FlowDocumentJSON {
  nodes: FlowNodeJSON[];
  edges: WorkflowEdgeJSON[];
}

# 节点 ID 值类型

/** ID 值类型,为 6 位数值字符串 */
export interface INodeIDValue = string;

# 节点类型枚举

declare enum WorkflowNodeType {
  Start = 'start',                         // 开始节点 (固定ID: 100001)
  End = 'end',                             // 结束节点 (固定ID: 900001)
  LLM = 'llm',                             // 大模型节点
  Workflow = 'workflow',                   // 工作流节点
  Code = 'code',                           // 代码节点,支持 JS/Python 语言
  Condition = 'condition',                 // 选择器节点
  Loop = 'loop',                           // 循环节点
  SetVariable = 'set-variable',            // 循环设置变量节点
  Batch = 'batch',                         // 批处理节点
  BlockStart = 'block-start',              // 循环/批处理区块开始节点
  BlockEnd = 'block-end',                  // 循环/批处理区块结束节点
  HTTP = 'http',                           // HTTP 请求节点
  GenerationImage = 'generation-image',    // 图片生成
  GenerationVideo = 'generation-video',    // 视频生成
}

# 记录类型

/** 记录类型 */
type Record<K extends keyof any, T> = {
  [P in K]: T;
};

# 节点值类型


/** 常量值类型 */
interface IFlowConstantValue {
  type: 'constant';
  content?: string | number | boolean;
}

/** 引用值类型 */
interface IFlowRefValue {
  type: 'ref';
  content?: string[];
  schema?: IJsonSchema;    // 引用数据类型继承,在节点输出时使用
}

/** Freemarker 值类型 */
interface IFlowFreemarkerValue {
  type: 'freemarker';
  content?: string;
}

/** 工作流值类型 */
type IFlowValue = IFlowConstantValue | IFlowRefValue | IFlowFreemarkerValue;

# 属性数据类型

/** 基础类型 */
export type BasicType =
  | 'boolean'
  | 'string'
  | 'integer'
  | 'number'
  | 'image',
  | 'video',
  | 'audio',
  | 'object'
  | 'array';

/**
 * JSON Schema 定义
 */
export interface IJsonSchema<T extends BasicType = BasicType> {
  type?: T;                                    // 基础类型
  default?: any;                               // 默认值
  description?: string;                        // 描述
  properties?: Record<string, IJsonSchema>;    // 对象类型的属性
  items?: IJsonSchema;                         // 数组类型的元素
  required?: string[];                         // 必需的属性
}

# 节点数据结构


export interface FlowNodeJSON {
  id: INodeIDValue;                               // 节点 ID
  type?: WorkflowNodeType;                        // 节点类型
  data?: {                                        // 表单数据
    title?: string;                               // 节点标题
    inputsValues?: Record<string, IFlowValue>;    // 节点输入数据值
    inputs?: IJsonSchema;                         // 节点输入数据类型
    outputs?: IJsonSchema;                        // 节点输出数据类型
  };                                     
  meta?: FlowNodeMeta;                            // 节点渲染相关配置信息
  blocks?: FlowNodeJSON[];                        // 子节点
  edges?: WorkflowEdgeJSON[];                     // 子节点间连线
}

# 边数据结构

export interface WorkflowEdgeJSON {
  sourceNodeID: INodeIDValue;       // 源节点 ID
  targetNodeID: INodeIDValue;       // 目标节点 ID
  sourcePortID?: INodeIDValue;      // 源端口 ID
  targetPortID?: INodeIDValue;      // 目标端口 ID
}

# 范例

# 工作流

# 初始数据

export const initialData: FlowDocumentJSON = {
  nodes: [
    {
      id: '100001',
      type: 'start',
      data: {
        title: '开始',
        outputs: {
          type: 'object',
          properties: {
            query: {
              type: 'string',
              default: 'Hello Flow.',
            },
            enable: {
              type: 'boolean',
              default: true,
            },
            array_obj: {
              type: 'array',
              items: {
                type: 'object',
                properties: {
                  int: {
                    type: 'number',
                  },
                  str: {
                    type: 'string',
                  },
                },
              },
            },
          },
        },
      },
    },
    {
      id: '900001',
      type: 'end',
      data: {
        title: '结束',
        inputs: {
          type: 'object',
          properties: {
            result: {
              type: 'string',
            },
          },
        },
      },
    },
  ],
  edges: [
    {
      sourceNodeID: '100001',
      targetNodeID: '900001',
    },
  ],
};