package com.education.workflow;

import com.education.workflow.iface.*;
import org.springframework.context.annotation.Bean;

import java.util.List;

//工作流执行器
//能根据不同工作流，走不同的执行器
//最好采用配置方法
public abstract class AbstractWorkFlowRunner implements IWorkFlowRunner {

    @Override
    public IAIFactory getAIFactory() {
        return aiFactory();
    }

    @Override
    public IPromptOptimizer getPromptOptimizer(){
        return promptOptimizer();
    }

    @Bean
    IAIFactory aiFactory() {
        return new DefAIFactory();
    }

    @Bean
    IPromptOptimizer promptOptimizer() {
        return new DefPromptOptimizer();
    }

    //执行
    public IResult run() {
        //第一步 收集上下文
       List<IContext> ctxList = this.getCollector().collect();

       //第二步 上下文优化
        ctxList = this.getPromptOptimizer().optimize(ctxList,this.getPrompt());

        IContext target = this.getPromptOptimizer().flat(ctxList);
        //第三步 调用AI
        IResult result = this.getAIFactory().invoke(target);

        //第四步 转换成
        this.getStructParser().parse(result);

        return result;
    }

}
