b77f066259
Players can now paste the full text of a freely available adventure when creating a game, and the DM will follow it instead of inventing its own plot/NPCs/locations. Reuses the existing rulebook RAG pipeline (chunking, local embedding) rather than injecting the raw text into every turn, since real adventure modules range from a few pages to hundreds — far beyond what fits in a prompt. - New adventure_chunks table (game-scoped, unlike the global rulebook_chunks) + Game.adventure_text storing the raw source. - ingest_adventure_text() chunks/embeds synchronously during POST /api/games when adventure_text is non-empty. - build_adventure_rag_block() retrieves by similarity to the latest player message, scoped to game_id — plus always includes chunk 0 (the adventure's opening) regardless of the query, since early Session-0 messages rarely resemble the adventure's actual hook and pure similarity search could miss the beginning entirely. - System prompt instructs the DM to follow provided adventure excerpts strictly, deviating only when players clearly go off-script. - Frontend: optional adventure-text field in the game creation form, a has_adventure flag surfaced as a small badge on the game card and session header. Verified with an isolated two-game test that chunks/retrieval never leak across games (the critical failure mode here), and live against x.ai: a custom-written one-shot's specific NPCs, location, and hook appeared verbatim in the DM's opening scene instead of invented ones. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
55 lines
2.3 KiB
Python
55 lines
2.3 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import ForeignKey, String, Text, UniqueConstraint, func
|
|
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.db import Base
|
|
|
|
|
|
class Game(Base):
|
|
__tablename__ = "games"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(length=200), nullable=False)
|
|
description: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
creator_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
|
)
|
|
participation_code: Mapped[str] = mapped_column(
|
|
String(length=16), unique=True, nullable=False, index=True
|
|
)
|
|
status: Mapped[str] = mapped_column(String(length=20), nullable=False, default="active")
|
|
ended_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
# DM-only bookkeeping (name/current_hp/max_hp per monster) — never serialized to any
|
|
# API response, since HP/AC of NPCs is meant to stay secret from players.
|
|
monsters: Mapped[list] = mapped_column(JSONB, nullable=False, default=list)
|
|
# Raw pasted adventure-module text (if any) — chunked/embedded into adventure_chunks for
|
|
# retrieval; kept here verbatim as the source of truth for future re-ingestion.
|
|
adventure_text: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
server_default=func.now(), nullable=False, index=True
|
|
)
|
|
|
|
|
|
class GameParticipant(Base):
|
|
__tablename__ = "game_participants"
|
|
__table_args__ = (UniqueConstraint("game_id", "user_id", name="uq_game_participant"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
game_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("games.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
|
)
|
|
character_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("characters.id"), nullable=True
|
|
)
|
|
joined_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|