文章

Coze协议schema拆解

Save阶段

{
    "workflow_id": "7493521992218361892",
    "schema": "{\"nodes\":[{\"id\":\"100001\",\"type\":\"1\",\"meta\":{\"position\":{\"x\":0,\"y\":0}},\"data\":{\"nodeMeta\":{\"description\":\"工作流的起始节点,用于设定启动工作流需要的信息\",\"icon\":\"https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-Start-v2.jpg\",\"subTitle\":\"\",\"title\":\"开始\"},\"outputs\":[{\"type\":\"string\",\"name\":\"input\",\"required\":false}],\"trigger_parameters\":[{\"type\":\"string\",\"name\":\"input\",\"required\":false}]}},{\"id\":\"900001\",\"type\":\"2\",\"meta\":{\"position\":{\"x\":1030,\"y\":-112}},\"data\":{\"nodeMeta\":{\"description\":\"工作流的最终节点,用于返回工作流运行后的结果信息\",\"icon\":\"https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-End-v2.jpg\",\"subTitle\":\"\",\"title\":\"结束\"},\"inputs\":{\"terminatePlan\":\"returnVariables\",\"inputParameters\":[{\"name\":\"output\",\"input\":{\"type\":\"string\",\"value\":{\"type\":\"ref\",\"content\":{\"source\":\"block-output\",\"blockID\":\"100001\",\"name\":\"input\"},\"rawMeta\":{\"type\":1}}}}]}}},{\"id\":\"170211\",\"type\":\"5\",\"meta\":{\"position\":{\"x\":465.95604395604397,\"y\":-45.400000000000006}},\"data\":{\"nodeMeta\":{\"description\":\"编写代码,处理输入变量来生成返回值\",\"icon\":\"https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-Code-v2.jpg\",\"mainColor\":\"#00B2B2\",\"subTitle\":\"代码\",\"title\":\"代码\"},\"inputs\":{\"inputParameters\":[{\"name\":\"a\",\"input\":{\"type\":\"string\",\"value\":{\"type\":\"ref\",\"content\":{\"source\":\"block-output\",\"blockID\":\"100001\",\"name\":\"input\"},\"rawMeta\":{\"type\":1}}}}],\"code\":\"// 在这里,您可以通过 ‘params’  获取节点中的输入变量,并通过 'ret' 输出结果\\n// 'params' 和 'ret' 已经被正确地注入到环境中\\n// 下面是一个示例,获取节点输入中参数名为‘input’的值:\\n// const input = params.input; \\n// 下面是一个示例,输出一个包含多种数据类型的 'ret' 对象:\\n// const ret = { \\\"name\\\": ‘小明’, \\\"hobbies\\\": [“看书”, “旅游”] };\\n\\nasync function main({ params }: Args): Promise<Output> {\\n    // 构建输出对象\\n    const ret = {\\n        \\\"key0\\\": params.input + params.input, // 拼接两次入参 input 的值\\n        \\\"key1\\\": [\\\"hello\\\", \\\"world\\\"], // 输出一个数组\\n        \\\"key2\\\": { // 输出一个Object\\n            \\\"key21\\\": \\\"hi11\\\"\\n        },\\n    };\\n\\n    return ret;\\n}\",\"language\":5,\"settingOnError\":{}},\"outputs\":[{\"type\":\"list\",\"name\":\"key1\",\"schema\":{\"type\":\"string\"}},{\"type\":\"object\",\"name\":\"key2\",\"schema\":[{\"type\":\"string\",\"name\":\"key21\"}]}]}}],\"edges\":[{\"sourceNodeID\":\"100001\",\"targetNodeID\":\"170211\"},{\"sourceNodeID\":\"170211\",\"targetNodeID\":\"900001\"}],\"versions\":{\"loop\":\"v2\"}}",
    "space_id": "7386668591510913065",
    "submit_commit_id": "Bot_7493521992218361892_Submit_7493521992218558500",
    "ignore_status_transfer": true
}

序号

字段

描述拆解

1

workflow_id

workflow_id

2

schema

配置完整的schema

3

space_id

工作空间

4

submit_commit_id

提交记录

5

ignore_status_transfer

未知

schema.edges

[
    {
        "sourceNodeID": "100001",
        "targetNodeID": "114135"
    },
    {
        "sourceNodeID": "170211",
        "targetNodeID": "900001"
    },
    {
        "sourceNodeID": "114135",
        "targetNodeID": "900001",
        "sourcePortID": "true_1"
    },
    {
        "sourceNodeID": "172417",
        "targetNodeID": "900001"
    },
    {
        "sourceNodeID": "114135",
        "targetNodeID": "170211",
        "sourcePortID": "true"
    },
    {
        "sourceNodeID": "114135",
        "targetNodeID": "172417",
        "sourcePortID": "false"
    }
]

序号

字段

描述

1

sourceNodeID

来源节点id

2

targetNodeID

目标节点id

3

sourcePortID

如果是condition则从true、true1、false不断展开

schema.nodes

Condition

{
    "id": "114135",
    "type": "8",
    "meta": {
        "position": {
            "x": 410,
            "y": -31.586483017307152
        }
    },
    "data": {
        "nodeMeta": {
            "description": "连接多个下游分支,若设定的条件成立则仅运行对应的分支,若均不成立则只运行“否则”分支",
            "icon": "https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-Condition-v2.jpg",
            "mainColor": "#00B2B2",
            "subTitle": "选择器",
            "title": "选择器"
        },
        "inputs": {
            "branches": [
                {
                    "condition": {
                        "logic": 2,
                        "conditions": [
                            {
                                "operator": 1,
                                "left": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "ref",
                                            "content": {
                                                "source": "block-output",
                                                "blockID": "100001",
                                                "name": "input"
                                            }
                                        }
                                    }
                                },
                                "right": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "literal",
                                            "content": "11",
                                            "rawMeta": {
                                                "type": 1
                                            }
                                        }
                                    }
                                }
                            },
                            {
                                "operator": 1,
                                "left": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "ref",
                                            "content": {
                                                "source": "block-output",
                                                "blockID": "100001",
                                                "name": "input"
                                            }
                                        }
                                    }
                                },
                                "right": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "literal",
                                            "content": "12",
                                            "rawMeta": {
                                                "type": 1
                                            }
                                        }
                                    }
                                }
                            },
                            {
                                "operator": 3,
                                "left": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "ref",
                                            "content": {
                                                "source": "block-output",
                                                "blockID": "100001",
                                                "name": "input"
                                            }
                                        }
                                    }
                                },
                                "right": {
                                    "input": {
                                        "type": "integer",
                                        "value": {
                                            "type": "literal",
                                            "content": 1,
                                            "rawMeta": {
                                                "type": 2
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "condition": {
                        "logic": 2,
                        "conditions": [
                            {
                                "operator": 2,
                                "left": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "ref",
                                            "content": {
                                                "source": "block-output",
                                                "blockID": "100001",
                                                "name": "input"
                                            }
                                        }
                                    }
                                },
                                "right": {
                                    "input": {
                                        "type": "string",
                                        "value": {
                                            "type": "literal",
                                            "content": "123",
                                            "rawMeta": {
                                                "type": 1
                                            }
                                        }
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Start节点

{
    "id": "100001",
    "type": "1",
    "meta": {
        "position": {
            "x": 0,
            "y": 0
        }
    },
    "data": {
        "nodeMeta": {
            "description": "工作流的起始节点,用于设定启动工作流需要的信息",
            "icon": "https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-Start-v2.jpg",
            "subTitle": "",
            "title": "开始"
        },
        "outputs": [
            {
                "type": "string",
                "name": "input",
                "required": false
            }
        ],
        "trigger_parameters": []
    }
}

End节点

{
    "id": "900001",
    "type": "2",
    "meta": {
        "position": {
            "x": 1384.043956043956,
            "y": -112
        }
    },
    "data": {
        "nodeMeta": {
            "description": "工作流的最终节点,用于返回工作流运行后的结果信息",
            "icon": "https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-End-v2.jpg",
            "subTitle": "",
            "title": "结束"
        },
        "inputs": {
            "terminatePlan": "returnVariables",
            "inputParameters": [
                {
                    "name": "output",
                    "input": {
                        "type": "string",
                        "value": {
                            "type": "ref",
                            "content": {
                                "source": "block-output",
                                "blockID": "100001",
                                "name": "input"
                            },
                            "rawMeta": {
                                "type": 1
                            }
                        }
                    }
                }
            ]
        }
    }
}

Code节点

{
    "id": "170211",
    "type": "5",
    "meta": {
        "position": {
            "x": 820,
            "y": -45.400000000000006
        }
    },
    "data": {
        "nodeMeta": {
            "description": "编写代码,处理输入变量来生成返回值",
            "icon": "https://lf3-static.bytednsdoc.com/obj/eden-cn/dvsmryvd_avi_dvsm/ljhwZthlaukjlkulzlp/icon/icon-Code-v2.jpg",
            "mainColor": "#00B2B2",
            "subTitle": "代码",
            "title": "代码"
        },
        "inputs": {
            "inputParameters": [
                {
                    "name": "a",
                    "input": {
                        "type": "string",
                        "value": {
                            "type": "ref",
                            "content": {
                                "source": "block-output",
                                "blockID": "100001",
                                "name": "input"
                            },
                            "rawMeta": {
                                "type": 1
                            }
                        }
                    }
                }
            ],
            "code": "// 在这里,您可以通过 ‘params’  获取节点中的输入变量,并通过 'ret' 输出结果\n// 'params' 和 'ret' 已经被正确地注入到环境中\n// 下面是一个示例,获取节点输入中参数名为‘input’的值:\n// const input = params.input; \n// 下面是一个示例,输出一个包含多种数据类型的 'ret' 对象:\n// const ret = { \"name\": ‘小明’, \"hobbies\": [“看书”, “旅游”] };\n\nasync function main({ params }: Args): Promise<Output> {\n    // 构建输出对象\n    const ret = {\n        \"key0\": params.input + params.input, // 拼接两次入参 input 的值\n        \"key1\": [\"hello\", \"world\"], // 输出一个数组\n        \"key2\": { // 输出一个Object\n            \"key21\": \"hi11\"\n        },\n    };\n\n    return ret;\n}",
            "language": 5,
            "settingOnError": {}
        },
        "outputs": [
            {
                "type": "list",
                "name": "key1",
                "schema": {
                    "type": "string"
                }
            },
            {
                "type": "object",
                "name": "key2",
                "schema": [
                    {
                        "type": "string",
                        "name": "key21"
                    }
                ]
            }
        ]
    }
}

代码编写技巧

无论前端和后端,先定义数据结构