Coverage for app / router / app / turns / router.py: 100%
41 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""
2The caller's running chat turns — `/app/turns`.
4A chat turn runs to its end on the server whether or not the person is still
5on the page (``app/services/chat_socket.py``), so a chat page they come back
6to asks here what is still being answered, shows it, and reads the
7conversation again once the turn has gone — it leaves the registry only
8when it has been stored. Stop for a turn another page started is here too.
10Only the caller's own turns are listed or stoppable. An agent turn is
11listed in the workspace its chat was had in; a direct model turn has none.
12"""
14from typing import Any, Literal
15from uuid import UUID
17from fastapi import APIRouter, Depends, HTTPException, status
18from pydantic import BaseModel
19from sqlalchemy.orm import Session
21from app.db.postgres.models import User
22from app.dependency.base import get_db
23from app.dependency.user.context import get_current_user
24from app.dependency.workspace import request_workspace
25from app.services import turns
26from app.util.time import UTCDateTime
28turn_router = APIRouter(prefix="/turns", tags=["App - Turns"])
31class RunningTurnRead(BaseModel):
32 """A turn while it runs."""
34 id: str
35 kind: turns.TurnKind
36 #: The agent or model answering.
37 target_id: str
38 #: Where the answer will be stored: an existing conversation, or the id
39 #: a new one will have.
40 conversation_id: str
41 status: turns.TurnStatus
42 started_at: UTCDateTime
43 #: The person's message as it will be stored, once the input rules have
44 #: run (null until then).
45 prompt: str | None = None
46 #: What the message records — ``{"command": …}`` for a chat command.
47 meta: dict[str, Any] | None = None
48 #: Stop was asked for and the turn is winding down.
49 stopping: bool = False
52def _read(turn: turns.RunningTurn) -> RunningTurnRead:
53 return RunningTurnRead(
54 id=turn.id,
55 kind=turn.kind,
56 target_id=turn.target_id,
57 conversation_id=turn.conversation_id,
58 status=turn.status,
59 started_at=turn.started_at,
60 prompt=turn.prompt,
61 meta=turn.meta,
62 stopping=turn.stopping,
63 )
66@turn_router.get("", response_model=list[RunningTurnRead])
67def running_turns(
68 agent_id: UUID | None = None,
69 model_id: UUID | None = None,
70 db: Session = Depends(get_db),
71 current_user: User = Depends(get_current_user),
72) -> list[RunningTurnRead]:
73 """The caller's running turns, oldest first.
75 With an agent or a model, or all of them."""
76 kind: Literal["agent", "model"] | None = None
77 target: UUID | None = None
78 if agent_id and model_id:
79 raise HTTPException(
80 status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
81 detail="Name an agent or a model, not both.",
82 )
83 if agent_id:
84 kind, target = "agent", agent_id
85 elif model_id:
86 kind, target = "model", model_id
87 workspace = request_workspace(db, current_user).key
88 return [
89 _read(turn)
90 for turn in turns.registry.running(
91 str(current_user.id),
92 kind=kind,
93 target_id=str(target) if target else None,
94 workspace=workspace,
95 )
96 ]
99@turn_router.post("/{turn_id}/stop", status_code=status.HTTP_202_ACCEPTED)
100def stop_turn(
101 turn_id: str,
102 current_user: User = Depends(get_current_user),
103) -> dict[str, bool]:
104 """Stop one of the caller's running turns.
106 It ends as Stop from its own page ends it: svc-ai's run stops, and it is
107 stored with what it had written. 404 for a turn that is not theirs or no
108 longer running."""
109 if not turns.registry.stop(turn_id, str(current_user.id)):
110 raise HTTPException(
111 status_code=status.HTTP_404_NOT_FOUND,
112 detail="No running turn of yours has that id.",
113 )
114 return {"stopping": True}