php执行运行LiteGraph工作流返回结果示例代码

代码语言:php

所属分类:其他

代码描述:php执行运行LiteGraph工作流返回结果示例代码

代码标签: php 执行 运行 LiteGraph 工作流 返回 结果 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php

function executeWorkflow($workflow, $startNodeOutputData) {


    if ($workflow) {
        $nodes = $workflow['nodes'];
        $nodeMap = [];
        foreach ($nodes as $node) {
            $nodeMap[$node['id']] = $node;
        }

        $startNode = null;
        foreach ($nodes as $node) {
            if ($node['type'] === 'workflow/start') {
                $startNode = $node;
                break;
            }
        }

        if ($startNode) {
            // 将开始节点的输出参数数据传递给开始节点
            $result = executeNode($startNode, $nodeMap, $startNodeOutputData[0]);
            return json_encode(['result' => $result]);
        } else {
            return json_encode(['error' => 'Start node not found']);
        }
    } else {
        return json_encode(['error' => 'Invalid workflow data']);
    }
}

function executeNode($node, $nodeMap, $inputData = null) {
    $type = $node['type'];

    $inputs = isset($node['inputs']) ? $node['inputs'] : [];
    $outputs = isset($node['outputs']) ? $node['outputs'] : [];
    $properties = isset($node['properties']) ? $node['properties'] : [];

    switch ($type) {
        case 'workflow/start':
            // 直接传递开始节点的输出参数数据
            return executeNextNode($outputs[0]['links'][0], $nodeMap, $inputData);

            case 'network/http':
                $url = $inputData;
                if ($url) {
                    $timeout = $properties['timeout'];
                    $html = fetchHtml($url, $timeout);
                    return executeNextNode($outputs[0]['links'][0], $nodeMap, $html);
                }
                break;

            case 'text/format':
                $html = $inputData;
                if ($html) {
                    $text = strip_tags($html);
                    return executeNextNode($outputs[0]['links'][0], $nodeMap, $text);
                }
                break;

            case 'workflow/end':
                return $inputData;
        }

        return null;
    }

    function getInputData($input, $nodeMap) {
        if (isset($input['link'])) {
            $linkId = $input['link'];
            foreach ($nodeMap as $node) {
                if (isset($node['outputs'])) {
                    foreach ($node['outputs'] as $output) {
                        if (in_array($linkId, $output['links']) && isset($output['value'])) {
                            return $output['value'];
                        }
                    }
                }
            }
        }
        return null;
    }

    function executeNextNode($linkId, $nodeMap, $data) {
        foreach ($nodeMap as $node) {
            if (isset($node['inputs'])) {
                foreach ($node['inputs'] as $input) {
                    if ($input['link'] == $linkId) {
                        if (isset($node['outputs'])) {

                            foreach ($node['outputs'] as &$output) {
                                if (.........完整代码请登录后点击上方下载按钮下载查看

网友评论0