import { Type } from "../../sdk/ai/index.js";
import { defineTool } from "../../sdk/coding-agent/index.js";
import { getProjectObsStore } from "../project-observations-store.js";
import { hashId } from "../ids.js";
export const WRITE_PROJECT_OBSERVATION_TOOL_NAME = "write_project_observation";
export const writeProjectObservationTool = defineTool({
    name: WRITE_PROJECT_OBSERVATION_TOOL_NAME,
    label: "Write project observation",
    description: "Write a durable, cross-session observation about this project. " +
        "Use this to persist discovered conventions, architectural rules, " +
        "gotchas, and directory purposes. Observations are deduplicated by " +
        "content hash — writing the same fact twice is harmless (idempotent). " +
        "Future sessions can discover these via read_project_observations.",
    promptSnippet: "Use write_project_observation(content, relevance) to persist project-level knowledge.",
    promptGuidelines: [
        "Use for STABLE facts: conventions, architecture decisions, gotchas, file roles.",
        "Use relevance='critical' for NEVER-EDIT rules (auto-generated files, invariant constraints).",
        "Use relevance='high' for important conventions the agent must follow.",
        "Use relevance='medium' for useful patterns and directory purposes.",
        "Content must be a single, self-contained sentence — no markdown, no lists.",
        "Deduplication is automatic — same content = same ID → INSERT OR REPLACE.",
    ],
    parameters: Type.Object({
        content: Type.String({
            minLength: 1,
            description: "A single self-contained sentence describing the fact. No markdown, no lists.",
        }),
        relevance: Type.Union([
            Type.Literal("low"),
            Type.Literal("medium"),
            Type.Literal("high"),
            Type.Literal("critical"),
        ], { description: "Importance: critical > high > medium > low" }),
    }),
    async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
        const store = getProjectObsStore();
        if (!store) {
            return {
                content: [{ type: "text", text: "Project observations store is not available." }],
                details: { status: "store_unavailable" },
            };
        }
        const projectId = store.getProjectByCwd(ctx.cwd);
        if (!projectId) {
            return {
                content: [{ type: "text", text: `No project found for current working directory: ${ctx.cwd}` }],
                details: { status: "no_project" },
            };
        }
        const id = hashId(params.content);
        store.insertProjectObservations(projectId, "auto-research", [{ id, content: params.content, relevance: params.relevance }], Date.now());
        return {
            content: [{ type: "text", text: `Observation [${id}] recorded (relevance: ${params.relevance}).` }],
            details: { id, status: "ok" },
        };
    },
});
export function registerWriteProjectObservationTool(ext) {
    ext.registerTool(writeProjectObservationTool);
}
