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 GrepSearchTool {

    private final Path workspaceRoot;

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

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

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