forked from admin/zeropost-engine
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:
+56
-33
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user