import uuid from sqlalchemy.ext.asyncio import AsyncSession from app.models.world_state import WorldState TOOL_SCHEMA = { "name": "update_world_state", "description": ( "Persist a compact, up-to-date recap of everything that matters long-term for this game: " "key NPCs and their status, current location, open plot threads, important party/inventory " "state. The raw chat history you see is only a recent window — this summary is what carries " "everything older forward. Call it regularly (at least on every scene change or major " "development), not just at the end. Send the full current picture each time, not a diff — " "this replaces whatever was stored before." ), "parameters": { "type": "object", "properties": { "summary": { "type": "string", "description": ( "The complete current world-state recap, in the language you're playing in. " "A few short paragraphs or a bullet list, not a full transcript." ), }, }, "required": ["summary"], }, } async def update(session: AsyncSession, game_id: uuid.UUID, tool_input: dict) -> dict: state = await session.get(WorldState, game_id) if state is None: state = WorldState(game_id=game_id, summary=tool_input["summary"]) session.add(state) else: state.summary = tool_input["summary"] await session.commit() return {"summary": state.summary} async def get_summary(session: AsyncSession, game_id: uuid.UUID) -> str | None: state = await session.get(WorldState, game_id) return state.summary if state is not None and state.summary else None