import { generateObject } from "ai";
import { z } from "zod";
import { NEXT_STEPS_ICON_NAMES, } from "@superblocksteam/library-shared/types";
import llmobs from "../../llmobs/index.js";
const MAX_NEXT_STEPS = 3;
const ICON_LIST = NEXT_STEPS_ICON_NAMES.join(", ");
/**
 * Defensive character cap on the summary before it reaches the suggestion LLM.
 * `build_finalize` requests ≤80 words (~500 chars), so this cap should almost
 * never fire — it exists only to bound a pathologically long payload (e.g. a
 * model that ignored the word limit).
 */
const SUMMARY_CHAR_CAP = 4000;
const truncate = (s) => s.length <= SUMMARY_CHAR_CAP
    ? s
    : `${s.slice(0, SUMMARY_CHAR_CAP)}…[truncated]`;
const NextStepSchema = z.object({
    label: z
        .string()
        .describe("Short action title, max 6 words, starts with a verb (e.g. 'Add search and filtering')"),
    prompt: z
        .string()
        .describe("Detailed 1-2 sentence prompt that Clark would receive to build this enhancement"),
    icon: z
        .enum([...NEXT_STEPS_ICON_NAMES])
        .optional()
        .describe(`Lucide icon name in kebab-case from: ${ICON_LIST}`),
});
const NextStepsSchema = z.object({
    nextSteps: z
        .array(NextStepSchema)
        .describe("3 follow-up next steps for what the user should build next"),
});
export async function getNextSteps({ model, chatSessionStore, logger, abortSignal, }) {
    // `build_finalize` writes a structured summary whenever a turn ends in a
    // successful build.  Next-steps only fires from `awaiting-user` after a
    // successful generation, so that summary is expected to exist — it's a much
    // higher-signal input than the raw assistant transcript and is already
    // length-bounded by the tool's own ≤80 word contract.
    const summary = await chatSessionStore.getLatestSummary();
    if (!summary) {
        logger?.info("[next-steps] No build summary found; skipping generation");
        return [];
    }
    const systemPrompt = `You are helping suggest what a user should build next with Clark (an AI app builder) after they just finished building something.

Suggest exactly 3 follow-up things the user could ask Clark to do next. These should be specific, actionable enhancements to the app that was just built.

Focus on:
- New features that complement what was built (e.g. "Add search and filtering", "Add an export to CSV button")
- Performance and UX improvements (e.g. "Add loading states and pagination", "Add dark mode support")
- Data and workflow enhancements (e.g. "Add edit and delete functionality", "Add role-based access control")

Do NOT suggest generic improvements — be specific to the app that was built.
Every suggestion must be different from each other. Vary the categories.

For each suggestion, pick ONE icon from this list: ${ICON_LIST}`;
    const prompt = `WHAT CLARK JUST BUILT:\n${truncate(summary.summary)}`;
    logger?.info("[next-steps] Starting next-steps LLM generation");
    let nextSteps = [];
    try {
        const { object: result } = await llmobs.trace({ name: "nextSteps.generate", kind: "llm" }, async () => {
            return await generateObject({
                model,
                schema: NextStepsSchema,
                system: systemPrompt,
                prompt,
                abortSignal,
            });
        });
        nextSteps = result.nextSteps;
        logger?.info("[next-steps] Next-steps LLM generation succeeded", {
            count: nextSteps.length,
        });
    }
    catch (error) {
        logger?.warn("[next-steps] Next-steps LLM generation failed; returning empty", {
            error: error instanceof Error ? error.message : String(error),
        });
    }
    return nextSteps.slice(0, MAX_NEXT_STEPS);
}
//# sourceMappingURL=next-steps.js.map