package tech.easyflow.agent.runtime.asynctool;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import tech.easyflow.agent.enums.AgentToolType;
import tech.easyflow.agent.runtime.tool.AgentToolExecutionResult;
import tech.easyflow.agent.runtime.tool.PluginToolExecutor;
import tech.easyflow.ai.entity.PluginItem;

import java.util.Map;

/**
 * Plugin 异步工具子能力实现。
 */
public class PluginAsyncSubTools extends AbstractAgentAsyncSubTools {

    private final PluginItem pluginItem;
    private final String toolName;
    private final String displayName;
    private final PluginToolExecutor pluginToolExecutor;

    /**
     * 创建 Plugin 异步工具子能力。
     *
     * @param pluginItem 插件工具快照
     * @param toolName runtime 工具名
     * @param displayName 用户可见名称
     * @param pluginToolExecutor Plugin 执行器
     * @param taskStore 任务存储
     * @param taskExecutor 后台执行器
     */
    public PluginAsyncSubTools(PluginItem pluginItem,
                               String toolName,
                               String displayName,
                               PluginToolExecutor pluginToolExecutor,
                               AgentAsyncToolTaskStore taskStore,
                               ThreadPoolTaskExecutor taskExecutor) {
        super(taskStore, taskExecutor);
        this.pluginItem = pluginItem;
        this.toolName = toolName;
        this.displayName = displayName;
        this.pluginToolExecutor = pluginToolExecutor;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String toolType() {
        return AgentToolType.PLUGIN.name();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String toolName() {
        return toolName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String displayName() {
        return displayName;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String businessId() {
        return pluginItem == null || pluginItem.getId() == null ? null : String.valueOf(pluginItem.getId());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected AgentToolExecutionResult executeBusiness(Map<String, Object> arguments) {
        return pluginToolExecutor.execute(pluginItem, arguments);
    }
}
