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:
Alexey Pavlov
2026-05-30 22:01:38 +03:00
parent 36c02a9a0a
commit 5599de59ce
7 changed files with 733 additions and 151 deletions
+56 -33
View File
@@ -1,50 +1,73 @@
const express = require('express');
const router = express.Router();
const { query } = require('../config/db');
const channelsSvc = require('../services/channels');
// GET /api/channels - list channels for user
const getUserId = (req) => {
const id = req.headers['x-user-id'];
if (!id) return null;
return parseInt(id);
};
// GET /api/channels — список каналов пользователя
router.get('/', async (req, res) => {
const userId = req.headers['x-user-id'];
if (!userId) return res.status(401).json({ error: 'Unauthorized' });
const { rows } = await query(`SELECT * FROM channels WHERE user_id=$1 ORDER BY created_at DESC`, [userId]);
res.json(rows);
const userId = getUserId(req);
if (!userId) return res.status(401).json({ error: 'x-user-id required' });
try {
const channels = await channelsSvc.listChannels(userId);
res.json(channels);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/channels - create channel
// GET /api/channels/:id — один канал со всеми настройками
router.get('/:id', async (req, res) => {
const userId = getUserId(req);
if (!userId) return res.status(401).json({ error: 'x-user-id required' });
try {
const channel = await channelsSvc.getFullChannel(req.params.id, userId);
if (!channel) return res.status(404).json({ error: 'Channel not found' });
res.json(channel);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// POST /api/channels — создать канал
router.post('/', async (req, res) => {
const userId = req.headers['x-user-id'];
if (!userId) return res.status(401).json({ error: 'Unauthorized' });
const { name, tgChannelId, botToken, topic, tone, language } = req.body;
if (!name) return res.status(400).json({ error: 'name is required' });
const { rows } = await query(
`INSERT INTO channels (user_id, name, tg_channel_id, bot_token, topic, tone, language) VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`,
[userId, name, tgChannelId || null, botToken || null, topic || '', tone || 'neutral', language || 'ru']
);
res.json(rows[0]);
const userId = getUserId(req);
if (!userId) return res.status(401).json({ error: 'x-user-id required' });
try {
const channel = await channelsSvc.createChannel(userId, req.body);
res.json(channel);
} catch (err) {
console.error('[Route] POST /channels', err);
res.status(500).json({ error: err.message });
}
});
// PATCH /api/channels/:id - update channel
// PATCH /api/channels/:id — обновить
router.patch('/:id', async (req, res) => {
const userId = req.headers['x-user-id'];
const { name, topic, tone, language, botToken, tgChannelId, isActive, postSchedule } = req.body;
const { rows } = await query(
`UPDATE channels SET
name=COALESCE($1,name), topic=COALESCE($2,topic), tone=COALESCE($3,tone),
language=COALESCE($4,language), bot_token=COALESCE($5,bot_token),
tg_channel_id=COALESCE($6,tg_channel_id), is_active=COALESCE($7,is_active),
post_schedule=COALESCE($8,post_schedule)
WHERE id=$9 AND user_id=$10 RETURNING *`,
[name, topic, tone, language, botToken, tgChannelId, isActive, postSchedule ? JSON.stringify(postSchedule) : null, req.params.id, userId]
);
if (!rows.length) return res.status(404).json({ error: 'Channel not found' });
res.json(rows[0]);
const userId = getUserId(req);
if (!userId) return res.status(401).json({ error: 'x-user-id required' });
try {
const channel = await channelsSvc.updateChannel(req.params.id, userId, req.body);
res.json(channel);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// DELETE /api/channels/:id
router.delete('/:id', async (req, res) => {
const userId = req.headers['x-user-id'];
await query(`DELETE FROM channels WHERE id=$1 AND user_id=$2`, [req.params.id, userId]);
res.json({ ok: true });
const userId = getUserId(req);
if (!userId) return res.status(401).json({ error: 'x-user-id required' });
try {
await channelsSvc.deleteChannel(req.params.id, userId);
res.json({ ok: true });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
module.exports = router;
+12 -7
View File
@@ -1,23 +1,28 @@
const express = require('express');
const router = express.Router();
const { query } = require('../config/db');
const channelsSvc = require('../services/channels');
const generationQueue = require('../workers/generation');
// POST /api/generate - create generation job
// POST /api/generate — создать задачу генерации
router.post('/', async (req, res) => {
try {
const { type, topic, tone = 'neutral', language = 'ru', channelContext = '', keywords = [], channelId } = req.body;
const { type, topic, channelId, rubric, keywords = [], useCritique = true } = req.body;
const userId = req.headers['x-user-id'] || null;
if (!type || !topic) return res.status(400).json({ error: 'type and topic are required' });
if (!type) return res.status(400).json({ error: 'type is required' });
if (!['post', 'article', 'topics'].includes(type)) return res.status(400).json({ error: 'Invalid type' });
if (type !== 'topics' && !topic) return res.status(400).json({ error: 'topic is required' });
if (type === 'post' && !channelId) return res.status(400).json({ error: 'channelId is required for posts' });
const { rows } = await query(
`INSERT INTO generation_jobs (type, channel_id, topic, status) VALUES ($1,$2,$3,'pending') RETURNING id`,
[type, channelId || null, topic]
`INSERT INTO generation_jobs (user_id, channel_id, type, topic, rubric, status)
VALUES ($1,$2,$3,$4,$5,'pending') RETURNING id`,
[userId, channelId || null, type, topic || null, rubric || null]
);
const jobId = rows[0].id;
await generationQueue.add({ jobId, type, topic, tone, language, channelContext, keywords });
await generationQueue.add({ jobId, type, topic, channelId, rubric, keywords, useCritique });
res.json({ jobId, status: 'pending' });
} catch (err) {
@@ -26,7 +31,7 @@ router.post('/', async (req, res) => {
}
});
// GET /api/generate/:id - get job status
// GET /api/generate/:id — статус и результат
router.get('/:id', async (req, res) => {
try {
const { rows } = await query(`SELECT * FROM generation_jobs WHERE id=$1`, [req.params.id]);