Files
khb_microservices/packages/accounting_automation/src/prompt-loader.ts

35 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readFile } from "node:fs/promises";
export interface PromptSourceDeps {
/** Текст Google Docs (экспорт text/plain); не задан или упал - берётся копия из файла. */
exportDocText?: (docId: string) => Promise<string>;
docId: string;
fallbackFile: string;
/** Корень репозитория для относительного fallbackFile. */
rootDir?: string;
log?: (msg: string, extra?: Record<string, unknown>) => void;
}
/** Служебные разделы файла-копии (`## Описание`) агенту не нужны. */
export function stripPromptFile(text: string): string {
const noDescription = text.split(/\n## Описание/)[0] ?? text;
return noDescription.replace(/^# [^\n]*\n+/, "").trim();
}
/** Промпт категоризации: Google Docs (правит Анастасия) с запасной копией из репозитория. */
export function makePromptLoader(d: PromptSourceDeps): () => Promise<string> {
return async () => {
if (d.exportDocText) {
try {
const t = (await d.exportDocText(d.docId)).trim();
if (t.length > 200) return t;
d.log?.("промпт из Google Docs слишком короткий, берётся копия из файла", { length: t.length });
} catch (e) {
d.log?.("промпт из Google Docs недоступен, берётся копия из файла", { err: String(e) });
}
}
const path = d.rootDir && !/^([a-zA-Z]:|\/)/.test(d.fallbackFile) ? `${d.rootDir}/${d.fallbackFile}` : d.fallbackFile;
return stripPromptFile(await readFile(path, "utf8"));
};
}