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 { logger } from "adminforth";
import { HumanMessage, SystemMessage } from "langchain";
import { detectUserLanguage } from "../languageDetect.js";
import { buildAgentTurnSystemPrompt } from "../systemPrompt.js";
import { getErrorMessage, isAbortError } from "../../errors.js";
export class TurnPromptBuilder {
    constructor(options) {
        this.options = options;
    }
    build(input) {
        return __awaiter(this, void 0, void 0, function* () {
            const adminforth = this.options.getAdminforth();
            const userLanguage = yield detectUserLanguage(input.completionAdapter, input.prompt, input.previousUserMessages).catch((error) => {
                var _a;
                if (((_a = input.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) || isAbortError(error)) {
                    throw error;
                }
                logger.warn(`Failed to detect user language: ${getErrorMessage(error)}`);
                return null;
            });
            const systemPrompt = buildAgentTurnSystemPrompt({
                agentSystemPrompt: yield this.options.getAgentSystemPrompt(),
                adminUser: input.adminUser,
                usernameField: adminforth.config.auth.usernameField,
                userLanguage,
                chatSurface: input.chatSurface,
            });
            return [
                new SystemMessage(systemPrompt),
                new HumanMessage(input.prompt),
            ];
        });
    }
}
