package com.infoepoch.pms.agent.platform.agent.adapter.out.tool;

import com.infoepoch.pms.agent.platform.agent.adapter.out.tool.support.ChatbotBuiltInToolType;
import com.infoepoch.pms.agent.platform.agent.adapter.out.tool.support.ChatbotWorkspaceRootResolver;
import com.infoepoch.pms.agent.platform.agent.adapter.out.tool.support.GuardedWorkspaceToolCallback;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 工作区文件通配搜索工具。
 */
@Component
public class GlobSearchTool {

    private final Path workspaceRoot;

    @Autowired
    public GlobSearchTool(@Value("${app.chatbot-tools.workspace-root:}") String configuredWorkspaceRoot) {
        this.workspaceRoot = ChatbotWorkspaceRootResolver.resolve(configuredWorkspaceRoot,
                Paths.get("").toAbsolutePath().normalize());
    }

    GlobSearchTool(Path workspaceRoot) {
        this.workspaceRoot = workspaceRoot.toAbsolutePath().normalize();
    }

    /**
     * 创建工具回调。
     *
     * @return 工具回调
     */
    public ToolCallback createToolCallback() {
        ToolCallback delegate = com.alibaba.cloud.ai.graph.agent.tools.GlobSearchTool.builder(workspaceRoot.toString())
                .withName(ChatbotBuiltInToolType.GLOB_SEARCH.getKey())
                .withDescription(ChatbotBuiltInToolType.GLOB_SEARCH.getDescription())
                .build();
        return new GuardedWorkspaceToolCallback(workspaceRoot, delegate);
    }
}
