42 lines
1.8 KiB
JavaScript
42 lines
1.8 KiB
JavaScript
"use strict";
|
||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||
};
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.loadAssistantCanonExcerpt = loadAssistantCanonExcerpt;
|
||
const fs_1 = __importDefault(require("fs"));
|
||
const config_1 = require("../config");
|
||
const FALLBACK_CANON = [
|
||
"Не выдумывай возможности.",
|
||
"Не обещай настройку 1С и админ-действия.",
|
||
"Не показывай внутренние технические термины пользователю.",
|
||
"Говори по-человечески и предлагай ближайший полезный поддерживаемый шаг."
|
||
].join(" ");
|
||
let cache = null;
|
||
function stripMarkdown(input) {
|
||
return input
|
||
.replace(/^#{1,6}\s+/gm, "")
|
||
.replace(/[`*_>\-\[\]\(\)]/g, " ")
|
||
.replace(/\s+/g, " ")
|
||
.trim();
|
||
}
|
||
function loadAssistantCanonExcerpt(maxChars = 900) {
|
||
try {
|
||
const mtimeMs = fs_1.default.existsSync(config_1.ASSISTANT_CANON_FILE) ? fs_1.default.statSync(config_1.ASSISTANT_CANON_FILE).mtimeMs : -1;
|
||
if (cache && cache.mtimeMs === mtimeMs) {
|
||
return cache.excerpt;
|
||
}
|
||
if (!fs_1.default.existsSync(config_1.ASSISTANT_CANON_FILE)) {
|
||
return FALLBACK_CANON;
|
||
}
|
||
const raw = fs_1.default.readFileSync(config_1.ASSISTANT_CANON_FILE, "utf-8");
|
||
const normalized = stripMarkdown(raw);
|
||
const excerpt = normalized.length > maxChars ? `${normalized.slice(0, maxChars).trim()}...` : normalized;
|
||
cache = { mtimeMs, excerpt: excerpt || FALLBACK_CANON };
|
||
return cache.excerpt;
|
||
}
|
||
catch {
|
||
return cache?.excerpt ?? FALLBACK_CANON;
|
||
}
|
||
}
|