"""
Module for handling the telling of story command.
"""
from discord import Interaction
from .discord_utils import send_channel_message, send_public_event_embed
from .configuration import Configuration, ProcessInput, DelimitedTemplate, IdError
from .db import get_stories_messages_for_ai, update_db_objs
from .db_classes import STORY, StoryType, MESSAGE
from .llm_handler import request_openai
from .constants import (
PROMPT_MAX_WORDS_EVENT,
PROMPT_MAX_WORDS_FICTION,
EVENT_REQUEST_PROMPT,
FICTION_REQUEST_PROMPT,
)
[docs]
async def telling_event(
config: Configuration, process_data: ProcessInput, interaction: Interaction
):
"""
This function handles the telling of story based on an event.
Args:
config (Configuration): App configuration
process_data (ProcessInput): Process game data
"""
try:
config.logger.debug(
"Generating event phase prompt for tale id: "
+ f"{process_data.story_context.tale.id}"
)
commit_stories = []
messages = await get_stories_messages_for_ai(
config, process_data.story_context.tale.id
)
event_requ_prompt = DelimitedTemplate(EVENT_REQUEST_PROMPT).substitute(
EventText=process_data.story_context.event.text, MaxWords=PROMPT_MAX_WORDS_EVENT
)
config.logger.trace(f"Event request prompt: {event_requ_prompt}")
messages.append({"role": "user", "content": event_requ_prompt})
commit_stories.append(
STORY(
request=event_requ_prompt,
story_type=StoryType.EVENT,
tale_id=process_data.story_context.tale.id,
)
)
response_event = await request_openai(config, messages)
if not await response_event.error_free():
await interaction.followup.send(
f"The following error occurred during the AI request: {response_event.error}",
ephemeral=True,
)
return False
config.logger.trace(f"Event response: {response_event.response}")
msg_ids_event = await send_channel_message(
config,
process_data.game_context.selected_game.channel_id,
response_event.response,
)
if not msg_ids_event:
raise IdError(
f"The id {process_data.game_context.selected_game.channel_id} "
+ "is not available on the DC server. No stories are being created."
)
await send_public_event_embed(config, interaction, process_data, msg_ids_event[0])
commit_stories.append(
STORY(
response=response_event.response,
story_type=StoryType.EVENT,
tale_id=process_data.story_context.tale.id,
messages=[MESSAGE(message_id=msg_id) for msg_id in msg_ids_event],
)
)
await update_db_objs(config, commit_stories)
except IdError as err:
config.logger.error(f"ID-Error: {err}")
[docs]
async def telling_fiction(
config: Configuration, process_data: ProcessInput, interaction: Interaction
):
"""
This function handles the telling of story based on an fiction input.
Args:
config (Configuration): App configuration
process_data (ProcessInput): Process game data
"""
try:
config.logger.debug(
"Generating fiction phase prompt for tale id: "
+ f"{process_data.story_context.tale.id}"
)
commit_stories = []
messages = await get_stories_messages_for_ai(
config, process_data.story_context.tale.id
)
fiction_prompt = await process_data.story_context.get_fiction_prompt()
config.logger.trace(f"Fiction word: {fiction_prompt}")
fiction_requ_prompt = DelimitedTemplate(FICTION_REQUEST_PROMPT).substitute(
FictionText=fiction_prompt, MaxWords=PROMPT_MAX_WORDS_FICTION
)
config.logger.trace(f"Fiction request prompt: {fiction_requ_prompt}")
messages.append({"role": "user", "content": fiction_requ_prompt})
commit_stories.append(
STORY(
request=fiction_requ_prompt,
story_type=StoryType.FICTION,
tale_id=process_data.story_context.tale.id,
)
)
response_fiction = await request_openai(config, messages)
if not await response_fiction.error_free():
await interaction.followup.send(
f"The following error occurred during the AI request: {response_fiction.error}",
ephemeral=True,
)
return
config.logger.trace(f"Fiction response: {response_fiction.response}")
msg_ids_fiction = await send_channel_message(
config,
process_data.game_context.selected_game.channel_id,
response_fiction.response,
)
if not msg_ids_fiction:
raise IdError(
f"The id {process_data.game_context.selected_game.channel_id} "
+ "is not available on the DC server. No stories are being created."
)
commit_stories.append(
STORY(
response=response_fiction.response,
story_type=StoryType.FICTION,
tale_id=process_data.story_context.tale.id,
messages=[MESSAGE(message_id=msg_id) for msg_id in msg_ids_fiction],
)
)
await update_db_objs(config, commit_stories)
except IdError as err:
config.logger.error(f"ID-Error: {err}")