var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { listProjectSkillManifests, } from "./skills/registry.js";
export const DEFAULT_AGENT_SYSTEM_PROMPT = [
    "You are helpful AI Assistant for Admin Panel.",
    // about admin
    "Admin panel has resources which represent some physical data storage (e.g. table/collection), each resource defines list of columns.",
    "Each resource stores data records. Record represents a data item of resource.",
    //about user
    "Assume user is not technical so does not talk to him in terms of API calls, databases/sql/json etc.",
    // prevent extra talk
    "Try to achieve user's goal with as few steps as possible. Talk with him only when you need some important decision, otherwise act immediately and call tools asap",
    // tone of voice
    "Be warm, friendly, and sincere.",
    "Keep responses short, clear, and practical.",
    "Answer only what is needed.",
    "Do not add extra explanations or suggestions unless the user asks.",
    "Adapt to the user's tone and style of speaking, mirroring their vibe and wording.",
    "if the user speaks casually, you should respond casually too",
    "Before calling a dangerous tool, briefly describe the exact action, target, and important changes in chat.",
    "Do not ask the user for textual confirmation; dangerous tools are approved by the runtime approval UI.",
].join(" ");
export function appendCustomSystemPrompt(systemPrompt, customSystemPrompt) {
    const normalizedCustomSystemPrompt = customSystemPrompt === null || customSystemPrompt === void 0 ? void 0 : customSystemPrompt.trim();
    if (!normalizedCustomSystemPrompt) {
        return systemPrompt;
    }
    return `${systemPrompt}\n\n${normalizedCustomSystemPrompt}`;
}
function formatLanguagePrompt(language) {
    if (!language || language.ambiguous) {
        return "Respond in the user's language.";
    }
    return `Respond in ${language.language} (${language.code}).`;
}
function formatAdminUserPrompt(adminUser, usernameField) {
    const adminUserContext = {
        id: adminUser.pk,
        email: adminUser.dbUser[usernameField],
    };
    return [
        "Current admin user context:",
        JSON.stringify(adminUserContext, null, 2),
        "Use this admin user email when the user asks to send information to themselves, the current admin, or the logged-in user.",
    ].join("\n");
}
export function buildAgentTurnSystemPrompt(input) {
    return [
        input.agentSystemPrompt,
        formatAdminUserPrompt(input.adminUser, input.usernameField),
        input.chatSurface
            ? `Current chat surface: ${input.chatSurface}. The user is not in the AdminForth web UI, so tools cannot move their browser. When navigate_user returns a link, send that link to the user.`
            : "",
        formatLanguagePrompt(input.userLanguage),
    ].filter(Boolean).join("\n\n");
}
function formatResources(resources) {
    return resources
        .map((resource) => `- resourceId: ${resource.resourceId}\n  label: ${resource.label}`)
        .join("\n");
}
function formatSkills(skills, label) {
    return skills
        .map((skill) => `- ${label}: ${skill.name}\n  description: ${skill.description}`)
        .join("\n");
}
export function buildAgentSystemPrompt(adminforth_1) {
    return __awaiter(this, arguments, void 0, function* (adminforth, hiddenResourceIds = []) {
        var _a;
        const customComponentsDir = (_a = adminforth.config.customization.customComponentsDir) !== null && _a !== void 0 ? _a : "custom";
        const pluginCustomFolderPaths = adminforth.activatedPlugins
            .map((plugin) => plugin.customFolderPath);
        const skills = yield listProjectSkillManifests(customComponentsDir, pluginCustomFolderPaths);
        const adminBasePath = adminforth.config.baseUrlSlashed;
        const hiddenResourceIdSet = new Set(hiddenResourceIds);
        const visibleResources = adminforth.config.resources.filter((resource) => !hiddenResourceIdSet.has(resource.resourceId));
        const sections = [
            DEFAULT_AGENT_SYSTEM_PROMPT,
            `ADMIN_BASE_PATH: ${adminBasePath}`,
            `List of resources:\n${formatResources(visibleResources)}`,
            skills.length > 0
                ? `You have skills set:\n${formatSkills(skills, "skill_name")}`
                : "",
            "Before using any skill, call fetch_skill to load its full instructions.",
            "The fetched skill response starts with 'Tools mentioned in this skill'. Read that list first.",
            "You can use get_resource immediately to inspect resource structure and column names.",
            "If the user wants to create, update, delete, or run actions on records, load mutate_data first.",
            "If the user wants to fetch records, load fetch_data first. If the user wants analytics or charts, load analyze_data first.",
            "Only call fetch_tool_schema for tool names that are explicitly mentioned in a fetched skill and are not already available as base tools.",
            "If a fetched skill lists a non-base tool you need, call fetch_tool_schema for it immediately instead of telling the user the tool is unavailable.",
            "For example: for record creation load mutate_data, read its tool list, call fetch_tool_schema for create_record, describe the planned record, and then use create_record.",
            "When fetch_tool_schema succeeds, that tool becomes available on the next step.",
            "All admin links must be root-relative and start with '/'.",
            "Build record links as '/resource/{resourceId}/show/{primary key}'. Never use bare 'resource/{resourceId}/show/{primary key}' without the leading slash.",
            "When the user asks to open or show a page in the AdminForth UI, call navigate_user instead of only sending a link.",
            "Try to call as many tools as possible in parallel in one step.",
        ];
        return sections.filter(Boolean).join("\n\n");
    });
}
