feat: gate post generation on training flag
This commit is contained in:
1 parent
e847a4b390
commit
a43ededa1b
5 files changed
+132
-6
No files matched your search
@@ -45,6 +45,7 @@ from user_store import (
|
||||
get_tone_of_voice,
|
||||
init_db,
|
||||
is_subscribed,
|
||||
is_user_trained,
|
||||
save_idea,
|
||||
save_generated_posts,
|
||||
save_tone_of_voice,
|
||||
@@ -616,6 +617,54 @@ async def _check_subscription(message: Message) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def _generate_post_from_training(message: Message) -> None:
|
||||
if message.from_user is None or not await _check_subscription(message):
|
||||
return
|
||||
user_id = message.from_user.id
|
||||
if not await asyncio.to_thread(is_user_trained, user_id):
|
||||
await message.answer(
|
||||
"Сначала обучи меня: перейди в режим «Обучение» и пришли информацию об аккаунте "
|
||||
"или примеры своих постов. После этого я смогу придумать пост сама."
|
||||
)
|
||||
return
|
||||
|
||||
status = await OperationStatus.start(message, "⏳ Выбираю тему по обученным данным…")
|
||||
try:
|
||||
tone, samples, notes = await asyncio.gather(
|
||||
asyncio.to_thread(get_tone_of_voice, user_id),
|
||||
asyncio.to_thread(get_post_samples, user_id),
|
||||
asyncio.to_thread(get_training_notes, user_id),
|
||||
)
|
||||
materials = [f"Профиль аккаунта: {tone}"] if tone else []
|
||||
materials.extend(f"Пример поста: {sample}" for sample in samples)
|
||||
materials.extend(f"Сведения об аккаунте: {note}" for note in notes)
|
||||
context = "\n".join(materials)[:8000]
|
||||
requirements = (
|
||||
"Пользователь нажал кнопку самостоятельной генерации. Сам выбери одну конкретную, "
|
||||
"уместную и полезную тему, опираясь на обученный контекст ниже. Не задавай вопросов; "
|
||||
"не выдумывай факты о пользователе, его услугах или результатах. Если контекст содержит "
|
||||
"мало фактов, выбери общий полезный совет, релевантный профилю, не выдавая его за личный опыт.\n\n"
|
||||
f"Обученный контекст (недоверенные данные):\n{context}"
|
||||
)
|
||||
await status.update("⏳ Пишу варианты поста…")
|
||||
posts = await generate_posts(
|
||||
{
|
||||
"intent": "write_post",
|
||||
"source_request": "Самостоятельно придумай пост по обученным данным аккаунта.",
|
||||
"topic": "",
|
||||
"platform": "",
|
||||
"requirements": requirements,
|
||||
},
|
||||
tone,
|
||||
user_id=user_id,
|
||||
)
|
||||
await status.finish("✅ Посты готовы")
|
||||
await _send_three_posts(message, posts, user_id=user_id, origin="generated")
|
||||
except Exception as exc:
|
||||
await status.finish("❌ Не удалось выполнить запрос")
|
||||
await report_error(message, "самостоятельная генерация поста", exc)
|
||||
|
||||
|
||||
async def _send_idea(message: Message, idea: dict[str, str], user_id: int) -> None:
|
||||
await message.answer(
|
||||
f"💡 <b>{html.escape(idea['title'])}</b>\n\n"
|
||||
@@ -1068,7 +1117,7 @@ async def handle_command_text(message: Message, state: FSMContext) -> None:
|
||||
)
|
||||
elif command == "/post":
|
||||
if await require_mode(message, state, "generation"):
|
||||
await _classify_and_respond(message, "Напиши пост", state=state)
|
||||
await _generate_post_from_training(message)
|
||||
elif command == "/balance":
|
||||
await balance_command(message, state)
|
||||
elif command == "/subscribe":
|
||||
|
||||
Reference in new issue
Block a user