escape room mechanics with json_metadata for temporarily collecting data

based on where the user has went we can for example set a key: true if
they collected a key needed to access a section's steps.

	modified:   app.py
	new file:   migrations/versions/d737de68d6fa_add_metadata_field_to_activitystate.py
	new file:   research/activity14-choose-adventure.yaml
This commit is contained in:
Russell Ballestrini 2024-07-28 19:14:54 -04:00
parent e3b3ddb007
commit 5d10296f1f
3 changed files with 538 additions and 149 deletions

421
app.py
View file

@ -107,6 +107,22 @@ class ActivityState(db.Model):
attempts = db.Column(db.Integer, default=0)
max_attempts = db.Column(db.Integer, default=3)
s3_file_path = db.Column(db.String(256), nullable=False)
json_metadata = db.Column(db.UnicodeText, default="{}")
@property
def _json_metadata(self):
if self.json_metadata:
return json.loads(self.json_metadata)
return {}
@_json_metadata.setter
def _json_metadata(self, value):
self.json_metadata = json.dumps(value)
def update_metadata(self, key, value):
metadata = self._json_metadata
metadata[key] = value
self._json_metadata = metadata
def get_room(room_name):
@ -1808,43 +1824,54 @@ def handle_activity_response(room_name, user_response, username):
)
# Check if the step has a question
if "question" not in step:
# Move to the next step or section
next_section, next_step = get_next_step(
activity_content, section["section_id"], step["step_id"]
if "question" in step:
# Categorize the user's response
category = categorize_response(
step["question"],
user_response,
step["buckets"],
step["tokens_for_ai"],
)
if next_step:
activity_state.section_id = next_section["section_id"]
activity_state.step_id = next_step["step_id"]
activity_state.attempts = 0
# Emit the category to the frontend
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": f"Category: {category}",
},
room=room_name,
)
db.session.add(activity_state)
db.session.commit()
# Emit the new step content blocks
content = "\n\n".join(next_step["content_blocks"])
new_message = Message(
username="System", content=content, room_id=room.id
# Check metadata conditions for the current step
if "metadata_conditions" in step["transitions"][category]:
conditions_met = all(
activity_state._json_metadata.get(key) == value
for key, value in step["transitions"][category][
"metadata_conditions"
].items()
)
db.session.add(new_message)
db.session.commit()
if not conditions_met:
# Emit a message indicating the conditions are not met
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": "You do not have the required items to proceed.",
},
room=room_name,
)
# Remind the user of what they can do in the room
content_blocks = step.get("content_blocks", [])
question = step.get("question", "")
options_message = (
"\n\n".join(content_blocks) + "\n\n" + question
)
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": content,
},
room=room_name,
)
# Emit the new question if it exists
if "question" in next_step:
question_content = f"Question: {next_step['question']}"
new_message = Message(
username="System", content=question_content, room_id=room.id
username="System", content=options_message, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
@ -1854,79 +1881,185 @@ def handle_activity_response(room_name, user_response, username):
{
"id": new_message.id,
"username": "System",
"content": question_content,
"content": options_message,
},
room=room_name,
)
return
# Provide feedback based on the category
feedback, next_section_and_step = provide_feedback(
activity_content,
section["section_id"],
step["step_id"],
category,
step["question"],
user_response,
)
# Store and emit the feedback
new_message = Message(
username="System", content=feedback, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": feedback,
},
room=room_name,
)
# Update metadata based on user actions
if "metadata_updates" in step["transitions"][category]:
print(f"metadata_updates in step/category")
for key, value in step["transitions"][category][
"metadata_updates"
].items():
print(f"{key}: {value}")
activity_state.update_metadata(key, value)
# Commit the changes after the loop
db.session.add(activity_state)
db.session.commit()
# Log the updated metadata
print(f"Updated metadata: {activity_state._json_metadata}")
# if "correct" or max_attempts reached.
if (
category
not in [
"off_topic",
"asking_clarifying_questions",
"partial_understanding",
]
or activity_state.attempts >= activity_state.max_attempts
):
if next_section_and_step:
(
current_section_id,
current_step_id,
) = next_section_and_step.split(":")
next_section = next(
s
for s in activity_content["sections"]
if s["section_id"] == current_section_id
)
next_step = next(
s
for s in next_section["steps"]
if s["step_id"] == current_step_id
)
if next_step:
activity_state.section_id = next_section["section_id"]
activity_state.step_id = next_step["step_id"]
activity_state.attempts = 0
db.session.add(activity_state)
db.session.commit()
# Emit the new step content blocks
content = "\n\n".join(next_step["content_blocks"])
new_message = Message(
username="System", content=content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": content,
},
room=room_name,
)
# Emit the new question if it exists
if "question" in next_step:
question_content = f"Question: {next_step['question']}"
new_message = Message(
username="System",
content=question_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": question_content,
},
room=room_name,
)
else:
# Display activity info before completing
display_activity_info(room_name, username)
# Activity completed
db.session.delete(activity_state)
db.session.commit()
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": "Activity completed!",
},
room=room_name,
)
else:
# Display activity info before completing
display_activity_info(room_name, username)
# Activity completed
db.session.delete(activity_state)
db.session.commit()
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": "Activity completed!",
},
room=room_name,
)
else:
# Display activity info before completing
display_activity_info(room_name, username)
# Activity completed
db.session.delete(activity_state)
# the user response is any bucket other than correct.
activity_state.attempts += 1
db.session.add(activity_state)
db.session.commit()
# Emit the question again
question_content = f"Question: {step['question']}"
new_message = Message(
username="System", content=question_content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": None,
"id": new_message.id,
"username": "System",
"content": "Activity completed!",
"content": question_content,
},
room=room_name,
)
return
# Categorize the user's response
category = categorize_response(
step["question"], user_response, step["buckets"], step["tokens_for_ai"]
)
# Emit the category to the frontend
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": f"Category: {category}",
},
room=room_name,
)
# Provide feedback based on the category
feedback, next_section_and_step = provide_feedback(
activity_content,
section["section_id"],
step["step_id"],
category,
step["question"],
user_response,
)
# Store and emit the feedback
new_message = Message(username="System", content=feedback, room_id=room.id)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": feedback,
},
room=room_name,
)
# if "correct" or max_attempts reached.
if (
category
not in [
"off_topic",
"asking_clarifying_questions",
"partial_understanding",
]
or activity_state.attempts >= activity_state.max_attempts
):
else:
# Handle steps without a question
next_section_and_step = step.get("next_section_and_step")
if next_section_and_step:
current_section_id, current_step_id = next_section_and_step.split(
":"
@ -1941,43 +2074,19 @@ def handle_activity_response(room_name, user_response, username):
for s in next_section["steps"]
if s["step_id"] == current_step_id
)
else:
# Move to the next step or section
next_section, next_step = get_next_step(
activity_content, section["section_id"], step["step_id"]
)
if next_step:
activity_state.section_id = next_section["section_id"]
activity_state.step_id = next_step["step_id"]
activity_state.attempts = 0
if next_step:
activity_state.section_id = next_section["section_id"]
activity_state.step_id = next_step["step_id"]
activity_state.attempts = 0
db.session.add(activity_state)
db.session.commit()
db.session.add(activity_state)
db.session.commit()
# Emit the new step content blocks
content = "\n\n".join(next_step["content_blocks"])
new_message = Message(
username="System", content=content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": content,
},
room=room_name,
)
# Emit the new question if it exists
if "question" in next_step:
question_content = f"Question: {next_step['question']}"
# Emit the new step content blocks
content = "\n\n".join(next_step["content_blocks"])
new_message = Message(
username="System", content=question_content, room_id=room.id
username="System", content=content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
@ -1987,7 +2096,44 @@ def handle_activity_response(room_name, user_response, username):
{
"id": new_message.id,
"username": "System",
"content": question_content,
"content": content,
},
room=room_name,
)
# Emit the new question if it exists
if "question" in next_step:
question_content = f"Question: {next_step['question']}"
new_message = Message(
username="System",
content=question_content,
room_id=room.id,
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": question_content,
},
room=room_name,
)
else:
# Display activity info before completing
display_activity_info(room_name, username)
# Activity completed
db.session.delete(activity_state)
db.session.commit()
socketio.emit(
"message",
{
"id": None,
"username": "System",
"content": "Activity completed!",
},
room=room_name,
)
@ -2007,29 +2153,6 @@ def handle_activity_response(room_name, user_response, username):
},
room=room_name,
)
else:
# the user response is any bucket other than correct.
activity_state.attempts += 1
db.session.add(activity_state)
db.session.commit()
# Emit the question again
question_content = f"Question: {step['question']}"
new_message = Message(
username="System", content=question_content, room_id=room.id
)
db.session.add(new_message)
db.session.commit()
socketio.emit(
"message",
{
"id": new_message.id,
"username": "System",
"content": question_content,
},
room=room_name,
)
except Exception as e:
socketio.emit(

View file

@ -0,0 +1,32 @@
"""Add metadata field to ActivityState
Revision ID: d737de68d6fa
Revises: d04950c5a624
Create Date: 2024-07-28 17:02:11.872502
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "d737de68d6fa"
down_revision = "d04950c5a624"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("activity_state", schema=None) as batch_op:
batch_op.add_column(sa.Column("json_metadata", sa.UnicodeText(), server_default="{}"))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("activity_state", schema=None) as batch_op:
batch_op.drop_column("json_metadata")
# ### end Alembic commands ###

View file

@ -0,0 +1,234 @@
default_max_attempts_per_step: 30
tokens_for_ai_rubric: |
You are a master storyteller. Your task is to create a coherent and engaging story based on the following chat history. The story should seamlessly integrate the user's responses and the AI's feedback, ensuring that the narrative flows naturally. Pay special attention to the user's choices and how they shape the story. Use descriptive language to bring the scenes to life and make the story immersive. The story should have a clear beginning, middle, and end, reflecting the user's journey and the outcomes of their decisions. Here is the chat history.
sections:
- section_id: "section_1"
title: "The Escape Room Begins"
steps:
- step_id: "step_1"
title: "Waking Up"
content_blocks:
- "You wake up in a dimly lit room with no memory of how you got there. The room is small and has a single door that is locked."
- "You see a rug on the floor, a bookshelf with a book, and a safe on the wall."
- "What do you do first? Look under the rug, examine the book, or try to open the safe?"
tokens_for_ai: "Guide the user to make a choice between looking under the rug, examining the book, or trying to open the safe. Provide feedback based on their choice."
question: "What do you choose? Look under the rug, Examine the book, or Try to open the safe? 🤔"
buckets:
- look_under_rug
- examine_book
- try_open_safe
- off_topic
- asking_clarifying_questions
transitions:
look_under_rug:
content_blocks:
- "You chose to look under the rug. 🧺"
- "You find a key hidden under the rug."
- "Do you take the key or continue exploring the room?"
next_section_and_step: "section_2:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
metadata_updates:
key: true
examine_book:
content_blocks:
- "You chose to examine the book. 📖"
- "The book contains a note with a password: 'ESCAPE123'."
- "Do you take note of the password or continue exploring the room?"
next_section_and_step: "section_3:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
metadata_updates:
password: true
try_open_safe:
content_blocks:
- "You chose to try to open the safe. 🔒"
- "The safe is locked and requires both a key and a password to open."
- "Do you look under the rug, examine the book, or continue exploring the room?"
next_section_and_step: "section_4:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
off_topic:
content_blocks:
- "It seems like your response is off-topic. Let's try to stay focused on the story. 🔄"
ai_feedback:
tokens_for_ai: "Gently guide the user back to the story in a supportive manner. Use emojis like 🔄 and 🧭."
asking_clarifying_questions:
content_blocks:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the user's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
- section_id: "section_2"
title: "The Key"
steps:
- step_id: "step_1"
title: "Found the Key"
content_blocks:
- "You chose to look under the rug. 🧺"
- "You find a key hidden under the rug."
- "Do you take the key or continue exploring the room?"
tokens_for_ai: "Guide the user to make a choice between taking the key or continuing to explore the room. Provide feedback based on their choice."
question: "What do you choose? Take the key or Continue exploring? 🤔"
buckets:
- take_key
- continue_exploring
- go_back
- off_topic
- asking_clarifying_questions
transitions:
take_key:
content_blocks:
- "You chose to take the key. 🔑"
- "You now have the key. Do you examine the book or try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
metadata_updates:
key: true
continue_exploring:
content_blocks:
- "You chose to continue exploring the room. 🕵️"
- "You see a rug on the floor, a bookshelf with a book, and a safe on the wall."
- "What do you do next? Examine the book or try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
go_back:
content_blocks:
- "You chose to go back to the previous step. 🔄"
- "You are now back at the previous step. What do you choose? Look under the rug, Examine the book, or Try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
off_topic:
content_blocks:
- "It seems like your response is off-topic. Let's try to stay focused on the story. 🔄"
ai_feedback:
tokens_for_ai: "Gently guide the user back to the story in a supportive manner. Use emojis like 🔄 and 🧭."
asking_clarifying_questions:
content_blocks:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the user's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
- section_id: "section_3"
title: "The Book"
steps:
- step_id: "step_1"
title: "Found the Password"
content_blocks:
- "You chose to examine the book. 📖"
- "The book contains a note with a password: 'ESCAPE123'."
- "Do you take note of the password or continue exploring the room?"
tokens_for_ai: "Guide the user to make a choice between taking note of the password or continuing to explore the room. Provide feedback based on their choice."
question: "What do you choose? Take note of the password or Continue exploring? 🤔"
buckets:
- take_password
- continue_exploring
- go_back
- off_topic
- asking_clarifying_questions
transitions:
take_password:
content_blocks:
- "You chose to take note of the password. 🔑"
- "You now have the password. Do you look under the rug or try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
metadata_updates:
password: true
continue_exploring:
content_blocks:
- "You chose to continue exploring the room. 🕵️"
- "You see a rug on the floor, a bookshelf with a book, and a safe on the wall."
- "What do you do next? Look under the rug or try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
go_back:
content_blocks:
- "You chose to go back to the previous step. 🔄"
- "You are now back at the previous step. What do you choose? Look under the rug, Examine the book, or Try to open the safe?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
off_topic:
content_blocks:
- "It seems like your response is off-topic. Let's try to stay focused on the story. 🔄"
ai_feedback:
tokens_for_ai: "Gently guide the user back to the story in a supportive manner. Use emojis like 🔄 and 🧭."
asking_clarifying_questions:
content_blocks:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the user's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
- section_id: "section_4"
title: "The Safe"
steps:
- step_id: "step_1"
title: "Opening the Safe"
content_blocks:
- "You chose to try to open the safe. 🔒"
- "The safe is locked and requires both a key and a password to open."
- "Do you use the key and enter the password to open the safe or continue exploring the room?"
tokens_for_ai: "Guide the user to make a choice between using the key and entering the password to open the safe or continuing to explore the room. Provide feedback based on their choice."
question: "What do you choose? Use the key and enter the password or Continue exploring? 🤔"
buckets:
- use_key_and_password
- continue_exploring
- go_back
- off_topic
- asking_clarifying_questions
transitions:
use_key_and_password:
metadata_conditions:
key: true
password: true
content_blocks:
- "You chose to use the key and enter the password to open the safe. 🔑"
- "The safe opens, revealing a hidden treasure."
- "Congratulations! You have found the hidden treasure. 🎉"
next_section_and_step: "section_5:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to the final encounter. Use emojis like 👍 and 🌟."
continue_exploring:
content_blocks:
- "You chose to continue exploring the room. 🕵️"
- "You see a rug on the floor, a bookshelf with a book, and a safe on the wall."
- "What do you do next? Look under the rug or examine the book?"
next_section_and_step: "section_1:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
go_back:
content_blocks:
- "You chose to go back to the previous step. 🔄"
- "You are now back at the previous step. Do you take note of the password or continue exploring the room?"
next_section_and_step: "section_3:step_1"
ai_feedback:
tokens_for_ai: "Provide positive reinforcement and guide the user to make the next choice. Use emojis like 👍 and 🌟."
off_topic:
content_blocks:
- "It seems like your response is off-topic. Let's try to stay focused on the story. 🔄"
ai_feedback:
tokens_for_ai: "Gently guide the user back to the story in a supportive manner. Use emojis like 🔄 and 🧭."
asking_clarifying_questions:
content_blocks:
- "I see you have some questions. Let's address them. ❓"
ai_feedback:
tokens_for_ai: "Answer the user's clarifying questions and provide additional information in a friendly and engaging manner. Use emojis like ❓ and 💬."
- section_id: "section_5"
title: "Congratulations!"
steps:
- step_id: "step_1"
title: "Well Done!"
content_blocks:
- "Congratulations on finding the hidden treasure! 🎉"
- "You have successfully completed the escape room."
- "We hope you enjoyed the adventure. 🌟"