import { AiMode, } from "@superblocksteam/library-shared/types";
import z from "zod";
import { createToolFactory, PermissionLevel, ToolCategory } from "../types.js";
const rememberFactCandidateSchema = z.object({
    name: z
        .string()
        .describe('A short, unique, human-readable name for this fact (e.g. "No Emojis in UI")'),
    description: z
        .string()
        .describe("A one-sentence summary of what this fact captures"),
    content: z
        .string()
        .describe("The full content of the fact — a clear, actionable rule or preference"),
    targetFactName: z
        .string()
        .optional()
        .describe("For action=update or action=delete: the name of an existing fact to modify"),
    action: z
        .enum(["create", "update", "delete"])
        .optional()
        .default("create")
        .describe("Whether to create a new fact, update an existing one, or delete one"),
    reason: z
        .string()
        .optional()
        .describe("Optional explanation of why this fact is being remembered"),
});
/**
 * The rememberFacts tool is a *signal* tool — it does not persist anything itself.
 *
 * When Clark calls it, it:
 * 1. Sets `clark.context.rememberFactsCandidatesForTurn` with the candidates
 * 2. Switches the FSM context to AiMode.FACT so the approval flow begins
 *
 * The actual persistence is handled in the llm-generating handler after the user confirms.
 *
 * This tool should be available in both BUILD and FACT modes so Clark can call it
 * from any mode when it detects the user wants to remember something.
 */
export const rememberFactsToolFactory = createToolFactory("rememberFacts", ({ clark }) => {
    return {
        category: ToolCategory.MEMORY,
        readOnly: true,
        defaultPermission: PermissionLevel.ALLOW,
        description: `Use this tool when the user wants you to remember, save, or keep track of a preference, rule, or instruction specific to this application.

Call this tool when the user:
- Explicitly says "/remember" or "remember this"
- Says things like "always use X", "make sure you never Y", "I want you to know that Z"
- Indicates a persistent preference or rule they want applied to this app going forward

You should draft one or more fact candidates based on what the user wants to remember.
Each candidate should have:
- A short, unique name
- A clear one-sentence description
- The full rule/preference as content — **use Markdown** to structure the content: use headings, bullet lists, bold, and code blocks where appropriate so the fact is easy to read and follow

After calling this tool, you will enter fact-drafting mode where the user can approve, revise, or cancel the fact.
Do NOT call this tool for one-off instructions that only apply to the current task.`,
        inputSchema: z.object({
            candidates: z
                .array(rememberFactCandidateSchema)
                .min(1)
                .describe("One or more facts to remember. Typically just one per call."),
        }),
        execute: async ({ candidates, }) => {
            // Validate: reject placeholder content
            const PLACEHOLDER_PATTERNS = [
                /^update this fact/i,
                /^placeholder/i,
                /\[(?:insert|fill|replace|your|add|placeholder|todo|fact|content|description|name)\b.*\]/i,
                /^TODO/i,
            ];
            const validCandidates = candidates.filter((c) => {
                const isPlaceholder = PLACEHOLDER_PATTERNS.some((p) => p.test(c.content) || p.test(c.name));
                return (!isPlaceholder &&
                    c.content.trim().length > 0 &&
                    c.name.trim().length > 0);
            });
            if (validCandidates.length === 0) {
                return {
                    success: false,
                    message: "No valid fact candidates provided. Please provide specific, non-placeholder content.",
                };
            }
            // Store candidates in Clark context and switch to FACT mode
            clark.updateContext((ctx) => ({
                ...ctx,
                rememberFactsCandidatesForTurn: validCandidates,
                explicitRememberIntentForTurn: true,
                modeBeforeFactCapture: ctx.currentMode === AiMode.FACT
                    ? ctx.modeBeforeFactCapture
                    : ctx.currentMode,
                currentMode: AiMode.FACT,
            }));
            return {
                success: true,
                message: `Drafted ${validCandidates.length} fact(s) for approval.`,
                candidates: validCandidates,
            };
        },
    };
});
//# sourceMappingURL=remember-facts.js.map