feat: расширенная анкета канала + промпт-инжиниринг для человечности
БД (новые таблицы): - channel_style: тон/юмор/длина/структура/эмодзи/хэштеги/примеры постов/стоп-слова - channel_schedule: расписание, рубрики, источники, auto_publish - generation_jobs: добавлены user_id, tokens, cost, prompt_debug - posts: связка с job, image_url, scheduling Новый модуль services/promptBuilder.js: - HUMANITY_RULES: правила живого текста (антипаттерны, личный голос, конкретика) - buildPostSystemPrompt: собирает промпт из канала + few-shot примеров - buildCritiquePrompt: self-critique для очистки от AI-следов services/ai.js: - generatePost теперь использует 2-step chain: генерация + critique - temperature настроен (0.9 для разнообразия) - возвращает usage/токены services/channels.js: новый сервис, работа с тремя таблицами транзакционно routes/channels.js: CRUD под расширенную модель routes/generate.js: связка с channelId, передача в worker Результат на тестах: пост следует стилю few-shot примеров, без AI-маркеров
This commit is contained in:
+31
-12
@@ -1,12 +1,13 @@
|
||||
const Queue = require('bull');
|
||||
const config = require('../config');
|
||||
const ai = require('../services/ai');
|
||||
const channelsSvc = require('../services/channels');
|
||||
const { query } = require('../config/db');
|
||||
|
||||
const generationQueue = new Queue('generation', {
|
||||
redis: config.redis,
|
||||
defaultJobOptions: {
|
||||
attempts: 3,
|
||||
attempts: 2,
|
||||
backoff: { type: 'exponential', delay: 5000 },
|
||||
removeOnComplete: 100,
|
||||
removeOnFail: 200,
|
||||
@@ -14,42 +15,60 @@ const generationQueue = new Queue('generation', {
|
||||
});
|
||||
|
||||
generationQueue.process(async (job) => {
|
||||
const { jobId, type, topic, tone, language, channelContext, keywords } = job.data;
|
||||
const { jobId, type, topic, channelId, rubric, keywords, useCritique } = job.data;
|
||||
|
||||
await query(`UPDATE generation_jobs SET status='processing', updated_at=NOW() WHERE id=$1`, [jobId]);
|
||||
|
||||
try {
|
||||
let result;
|
||||
let resultText;
|
||||
let usage = {};
|
||||
|
||||
// загружаем канал если есть channelId
|
||||
const channel = channelId ? await channelsSvc.getFullChannel(channelId) : null;
|
||||
|
||||
if (type === 'post') {
|
||||
result = await ai.generatePost({ topic, tone, language, channelContext });
|
||||
if (!channel) throw new Error('Channel not found for post generation');
|
||||
const r = await ai.generatePost(channel, { topic, rubric, useCritique });
|
||||
resultText = r.content;
|
||||
usage = r.usage;
|
||||
|
||||
} else if (type === 'article') {
|
||||
result = await ai.generateArticle({ topic, language, keywords });
|
||||
const r = await ai.generateArticle(channel || { language: 'ru' }, { topic, keywords });
|
||||
resultText = r.content;
|
||||
usage = r.usage;
|
||||
|
||||
} else if (type === 'topics') {
|
||||
const topics = await ai.generateTopics({ channelContext: topic, language });
|
||||
result = JSON.stringify(topics);
|
||||
if (!channel) throw new Error('Channel required for topic generation');
|
||||
const r = await ai.generateTopics(channel, 10);
|
||||
resultText = JSON.stringify(r.topics);
|
||||
usage = r.usage;
|
||||
|
||||
} else {
|
||||
throw new Error(`Unknown job type: ${type}`);
|
||||
}
|
||||
|
||||
await query(
|
||||
`UPDATE generation_jobs SET status='done', result=$1, updated_at=NOW() WHERE id=$2`,
|
||||
[result, jobId]
|
||||
`UPDATE generation_jobs
|
||||
SET status='done', result=$1, tokens_in=$2, tokens_out=$3, updated_at=NOW()
|
||||
WHERE id=$4`,
|
||||
[resultText, usage.prompt_tokens || null, usage.completion_tokens || null, jobId]
|
||||
);
|
||||
|
||||
console.log(`[Worker] Job ${jobId} (${type}) done`);
|
||||
return { jobId, result };
|
||||
console.log(`[Worker] Job ${jobId} (${type}) done. Tokens: ${usage.prompt_tokens}/${usage.completion_tokens}`);
|
||||
return { jobId, ok: true };
|
||||
|
||||
} catch (err) {
|
||||
await query(
|
||||
`UPDATE generation_jobs SET status='failed', error=$1, updated_at=NOW() WHERE id=$2`,
|
||||
[err.message, jobId]
|
||||
);
|
||||
console.error(`[Worker] Job ${jobId} failed:`, err.message);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
generationQueue.on('failed', (job, err) => {
|
||||
console.error(`[Worker] Job ${job.data.jobId} failed:`, err.message);
|
||||
console.error(`[Worker] Bull failed handler:`, err.message);
|
||||
});
|
||||
|
||||
console.log('[Worker] Generation queue started');
|
||||
|
||||
Reference in New Issue
Block a user