This commit is contained in:
Mikan
2026-06-19 19:14:27 +03:00
parent 2167493887
commit 32575e217e
23 changed files with 1191 additions and 92 deletions

View File

@@ -12,6 +12,7 @@ from fastapi import APIRouter, Depends, HTTPException
from app.db import get_db_dep
from app.deps import get_current_user
from app.engine.world_builder import commit_world_builder, continue_world_builder, start_world_builder
from app.engine.world_editor import edit_world_via_chat, reset_editor_dialogue
from app.models import User, World
from app.schemas import (
WorldBuilderCommit,
@@ -19,6 +20,8 @@ from app.schemas import (
WorldBuilderReply,
WorldBuilderStart,
WorldCreate,
WorldEditorChatReply,
WorldEditorChatRequest,
WorldOut,
WorldUpdate,
)
@@ -159,3 +162,56 @@ async def builder_commit(
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=f"builder_commit_failed: {e}")
# === World Editor (AI-assisted editing of an existing world) ===
@router.post("/{world_id}/chat", response_model=WorldEditorChatReply)
async def world_editor_chat(
world_id: UUID,
payload: WorldEditorChatRequest,
db: AsyncSession = Depends(get_db_dep),
user: User = Depends(get_current_user),
):
"""Chat with the AI to edit an existing world's definition.
Returns the AI's prose reply plus the proposed new definition. The
frontend must call PATCH /worlds/{id} to actually persist the change.
"""
result = await db.execute(select(World).where(World.id == world_id))
world = result.scalars().first()
if not world:
raise HTTPException(status_code=404, detail="world_not_found")
if world.owner_id != user.id and not user.is_admin:
raise HTTPException(status_code=403, detail="forbidden")
try:
ai_message, new_defn, changed = await edit_world_via_chat(
db=db, user=user, world=world, message=payload.message,
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=f"world_editor_chat_failed: {e}")
return WorldEditorChatReply(
ai_message=ai_message,
definition=new_defn,
changed=changed,
)
@router.post("/{world_id}/chat/reset")
async def world_editor_chat_reset(
world_id: UUID,
db: AsyncSession = Depends(get_db_dep),
user: User = Depends(get_current_user),
):
"""Clear the cached editor dialogue for a world (start fresh)."""
result = await db.execute(select(World).where(World.id == world_id))
world = result.scalars().first()
if not world:
raise HTTPException(status_code=404, detail="world_not_found")
if world.owner_id != user.id and not user.is_admin:
raise HTTPException(status_code=403, detail="forbidden")
reset_editor_dialogue(world_id)
return {"ok": True}