import { Type } from "../../sdk/ai/index.js";
import { defineTool } from "../../sdk/coding-agent/index.js";
import { getProjectObsStore } from "../project-observations-store.js";
export const READ_PROJECT_OBSERVATIONS_TOOL_NAME = "read_project_observations";
export const readProjectObservationsTool = defineTool({
    name: READ_PROJECT_OBSERVATIONS_TOOL_NAME,
    label: "Search project observations",
    description: "Search all past observations across ALL sessions in this project. " +
        "Use this when you need to recall project conventions, past decisions, " +
        "file locations, or user preferences before editing or creating files. " +
        "Uses simple substring matching — be specific with your query terms. " +
        "Returns up to 20 most recent matching observations with relevance tags.",
    promptSnippet: "Use read_project_observations(<query>) to search past observations across ALL sessions in this project.",
    promptGuidelines: [
        "Use read_project_observations before creating new files to find conventions for that file kind.",
        "Use read_project_observations before editing a file to find past context about that file or its directory.",
        "Be specific: search for file paths, technology names, or pattern keywords.",
        "Do not use for broad topic searches — keep queries narrow for relevant results.",
        "Results include relevance tags (critical/high/medium/low) to help you prioritize.",
    ],
    parameters: Type.Object({
        query: Type.String({
            minLength: 1,
            description: "Substring to search for in past observation content. " +
                "Use file paths (e.g. 'src/auth/middleware'), technology names, " +
                "or pattern keywords (e.g. 'convention', 'api route').",
        }),
    }),
    async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
        const query = params.query.trim();
        if (!query) {
            const message = "Query must be a non-empty string.";
            return {
                content: [{ type: "text", text: message }],
                details: { status: "empty_query", results: [], count: 0 },
            };
        }
        const store = getProjectObsStore();
        if (!store) {
            const message = "Project observations store is not available — " +
                "this tool requires the spectral server (spectral serve).";
            return {
                content: [{ type: "text", text: message }],
                details: { status: "store_unavailable", results: [], count: 0 },
            };
        }
        const projectId = store.getProjectByCwd(ctx.cwd);
        if (!projectId) {
            const message = `No project found for current working directory: ${ctx.cwd}`;
            return {
                content: [{ type: "text", text: message }],
                details: { status: "no_project", results: [], count: 0 },
            };
        }
        const results = store.searchProjectObservations(projectId, query);
        if (results.length === 0) {
            const message = `No past observations found matching "${query}" in this project.`;
            return {
                content: [{ type: "text", text: message }],
                details: { status: "no_results", query, results: [], count: 0 },
            };
        }
        const lines = results.map((r) => `[${r.relevance}] ${r.content}`);
        const text = `Found ${results.length} past observation(s) matching "${query}" in this project:\n\n${lines.join("\n")}`;
        return {
            content: [{ type: "text", text }],
            details: {
                status: "ok",
                query,
                results: results.map((r) => ({
                    content: r.content,
                    relevance: r.relevance,
                    sessionId: r.sessionId,
                })),
                count: results.length,
            },
        };
    },
});
export function registerReadProjectObservationsTool(ext) {
    ext.registerTool(readProjectObservationsTool);
}
