mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-08-03 12:30:24 +08:00
first api-interaction, fix auth_method unique-index
This commit is contained in:
+84
-24
@@ -5,11 +5,12 @@ from abc import abstractmethod
|
||||
from datetime import timedelta
|
||||
|
||||
import discord
|
||||
from api_client import ApiClient
|
||||
from bot_base import BotBase
|
||||
from channel_handlers import AutoDestructThreadHandler
|
||||
from loguru import logger
|
||||
from oasst_shared.schemas import protocol as protocol_schema
|
||||
from utils import utcnow
|
||||
from utils import DiscordTimestampStyle, discord_timestamp, utcnow
|
||||
|
||||
|
||||
class Questionnaire(discord.ui.Modal, title="Questionnaire Response"):
|
||||
@@ -23,15 +24,23 @@ class Questionnaire(discord.ui.Modal, title="Questionnaire Response"):
|
||||
class ChannelTaskBase(AutoDestructThreadHandler):
|
||||
thread_name: str = "Replies"
|
||||
expires_after: timedelta = timedelta(minutes=5)
|
||||
backend: ApiClient
|
||||
|
||||
async def start(self, bot: BotBase, task: protocol_schema.Task) -> discord.Message:
|
||||
self.bot = bot
|
||||
self.task = task
|
||||
msg = await self.send_first_message()
|
||||
self.first_message = msg
|
||||
self.thread = await bot.bot_channel.create_thread(message=discord.Object(msg.id), name=self.thread_name)
|
||||
await self.on_thread_created(self.thread)
|
||||
self.expiry_date = utcnow() + self.expires_after if self.expires_after else None
|
||||
try:
|
||||
self.bot = bot
|
||||
self.task = task
|
||||
self.backend = bot.backend
|
||||
self.expiry_date = utcnow() + self.expires_after if self.expires_after else None
|
||||
msg = await self.send_first_message()
|
||||
self.first_message = msg
|
||||
self.thread = await bot.bot_channel.create_thread(message=discord.Object(msg.id), name=self.thread_name)
|
||||
await self.on_thread_created(self.thread)
|
||||
except Exception:
|
||||
logger.exception("start task failed")
|
||||
await self.cleanup() # try to cleanup messag or thread
|
||||
raise
|
||||
|
||||
bot.register_reply_handler(msg_id=msg.id, handler=self)
|
||||
return msg
|
||||
|
||||
@@ -42,20 +51,57 @@ class ChannelTaskBase(AutoDestructThreadHandler):
|
||||
async def send_first_message(self) -> discord.message:
|
||||
...
|
||||
|
||||
def to_api_user(self, user: discord.User) -> protocol_schema.User:
|
||||
return protocol_schema.User(auth_method="discord", id=user.id, display_name=user.display_name)
|
||||
|
||||
async def post_interaction(self, interaction: protocol_schema.Interaction) -> protocol_schema.Task:
|
||||
api_response = await self.backend.post_interaction(interaction)
|
||||
if api_response.type != "task_done":
|
||||
# multi-step tasks are not supported yet
|
||||
logger.error(f"multi-step tasks are not supported yet (got response type: {api_response.type})")
|
||||
raise RuntimeError("Unexpected response from backend received")
|
||||
return api_response
|
||||
|
||||
def post_text_reply_to_post(self, user_msg: discord.Message) -> protocol_schema.Task:
|
||||
return self.backend.post_interaction(
|
||||
protocol_schema.TextReplyToPost(
|
||||
post_id=str(self.first_message.id),
|
||||
user_post_id=str(user_msg.id),
|
||||
user=self.to_api_user(user_msg.author),
|
||||
text=user_msg.content,
|
||||
)
|
||||
)
|
||||
|
||||
async def handle_text_reply_to_post(self, user_msg: discord.Member) -> protocol_schema.Task:
|
||||
try:
|
||||
self.post_text_reply_to_post(user_msg)
|
||||
await user_msg.add_reaction("✅")
|
||||
except Exception as e:
|
||||
await user_msg.add_reaction("❌")
|
||||
await user_msg.reply(f"❌ Error communicating with backend: {e}")
|
||||
|
||||
|
||||
class SummarizeStoryHandler(ChannelTaskBase):
|
||||
task: protocol_schema.SummarizeStoryTask
|
||||
thread_name: str = "Summaries"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_summarize_story.msg", task=self.task)
|
||||
|
||||
expiry_time = discord_timestamp(self.expiry_date, DiscordTimestampStyle.long_time)
|
||||
expiry_relatve = discord_timestamp(self.expiry_date, DiscordTimestampStyle.relative_time)
|
||||
msg = await self.bot.post_template(
|
||||
"task_summarize_story_teaser.msg", task=self.task, expiry_time=expiry_time, expiry_relatve=expiry_relatve
|
||||
)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def on_thread_created(self, thread: discord.Thread) -> None:
|
||||
await self.bot.post_template("task_summarize_story.msg", channel=thread, task=self.task)
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
msg = await self.read()
|
||||
print("received: ", msg, type(msg))
|
||||
logger.info("on_summarize_story_reply")
|
||||
await msg.add_reaction("✅")
|
||||
await self.handle_text_reply_to_post(msg)
|
||||
|
||||
|
||||
class InitialPromptHandler(ChannelTaskBase):
|
||||
@@ -63,13 +109,14 @@ class InitialPromptHandler(ChannelTaskBase):
|
||||
thread_name: str = "Prompts"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_initial_prompt.msg", task=self.task)
|
||||
msg = await self.bot.post_template("task_initial_prompt.msg", task=self.task)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
msg = await self.read()
|
||||
logger.info("on_initial_prompt_reply")
|
||||
await msg.add_reaction("✅")
|
||||
await self.handle_text_reply_to_post(msg)
|
||||
|
||||
|
||||
class UserReplyHandler(ChannelTaskBase):
|
||||
@@ -77,13 +124,14 @@ class UserReplyHandler(ChannelTaskBase):
|
||||
thread_name: str = "User replies"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_user_reply.msg", task=self.task)
|
||||
msg = await self.bot.post_template("task_user_reply.msg", task=self.task)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
msg = await self.read()
|
||||
logger.info("on_user_reply_reply")
|
||||
await msg.add_reaction("✅")
|
||||
await self.handle_text_reply_to_post(msg)
|
||||
|
||||
|
||||
class AssistantReplyHandler(ChannelTaskBase):
|
||||
@@ -91,13 +139,19 @@ class AssistantReplyHandler(ChannelTaskBase):
|
||||
thread_name: str = "Assistant replies"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_assistant_reply.msg", task=self.task)
|
||||
msg = await self.bot.post_template("task_assistant_reply.msg", task=self.task)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
msg = await self.read()
|
||||
logger.info("on_assistant_reply_reply")
|
||||
await msg.add_reaction("✅")
|
||||
try:
|
||||
self.post_text_reply_to_post(msg)
|
||||
await msg.add_reaction("✅")
|
||||
except Exception as e:
|
||||
await msg.add_reaction("❌")
|
||||
await msg.reply(f"❌ Error communicating with backend: {e}")
|
||||
|
||||
|
||||
class RankInitialPromptsHandler(ChannelTaskBase):
|
||||
@@ -105,7 +159,9 @@ class RankInitialPromptsHandler(ChannelTaskBase):
|
||||
thread_name: str = "User Responses"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_rank_initial_prompts.msg", task=self.task)
|
||||
msg = await self.bot.post_template("task_rank_initial_prompts.msg", task=self.task)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
@@ -119,7 +175,9 @@ class RankConversationsHandler(ChannelTaskBase):
|
||||
thread_name: str = "Rankings"
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post_template("task_rank_conversation_replies.msg", task=self.task)
|
||||
msg = await self.bot.post_template("task_rank_conversation_replies.msg", task=self.task)
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def handler_loop(self):
|
||||
while True:
|
||||
@@ -156,7 +214,9 @@ class RateSummaryHandler(ChannelTaskBase):
|
||||
await interaction.response.send_message(f"got your feedback: {score}")
|
||||
|
||||
async def send_first_message(self) -> discord.message:
|
||||
return await self.bot.post("first message")
|
||||
msg = await self.bot.post("first message")
|
||||
self.backend.ack_task(self.task.id, str(msg.id))
|
||||
return msg
|
||||
|
||||
async def on_thread_created(self, thread: discord.Thread) -> None:
|
||||
view = generate_rating_view(self.task.scale.min, self.task.scale.max, self._rating_response_handler)
|
||||
|
||||
Reference in New Issue
Block a user