Coverage for app / router / app / conversations / transformers.py: 95%
38 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
1from typing import Any, Optional, List
2from uuid import UUID
4from sqlalchemy.orm import Session
6from app.db.postgres.models import Conversation, ChatMessage, Agent
7from app.common import exceptions
10def list_conversations(
11 db: Session,
12 *,
13 owner_user_id: UUID,
14 ai_model_id: UUID | None = None,
15 agent_id: UUID | None = None,
16 workspace: Any = None,
17) -> List[Conversation]:
18 """
19 List conversations owned by a user, optionally filtered by agent or model.
20 Exactly one of ai_model_id or agent_id should be provided. ``workspace``
21 keeps the ones had in that workspace (a model chat is personal).
22 """
23 query = db.query(Conversation).filter(Conversation.owner_user_id == owner_user_id)
24 if workspace is not None:
25 query = query.filter(workspace.made_here(Conversation))
27 if ai_model_id and agent_id:
28 raise exceptions.ConversationInvalidArgumentsException
30 if ai_model_id:
31 query = query.filter(Conversation.ai_model_id == ai_model_id)
33 if agent_id:
34 query = query.filter(Conversation.agent_id == agent_id)
36 return query.order_by(Conversation.created_at.desc()).all()
39def get_conversation(
40 db: Session,
41 conversation_id: UUID,
42 owner_user_id: UUID,
43) -> Optional[Conversation]:
44 """Fetch a single conversation by ID, scoped to the owner."""
45 conversation = (
46 db.query(Conversation)
47 .filter(
48 Conversation.id == conversation_id,
49 Conversation.owner_user_id == owner_user_id,
50 )
51 .first()
52 )
54 if not conversation:
55 raise exceptions.ConversationNotFoundException
57 return conversation
60def create_conversation(
61 db: Session,
62 *,
63 owner_user_id: UUID,
64 ai_model_id: UUID | None = None,
65 agent: Agent | None = None,
66 messages: List[ChatMessage],
67 input_tokens: int,
68 output_tokens: int,
69 conversation_id: UUID | None = None,
70 workspace: Any = None,
71) -> Conversation:
72 """
73 Create a new conversation owned by a user, tied to a model or agent.
74 Pass conversation_id to use a pre-generated UUID.
76 ``workspace`` (``app/services/workspace.py``) is where it is had: the
77 conversation is the person's, listed under that workspace. None is
78 their Personal workspace.
79 """
80 if not ai_model_id and not agent:
81 raise ValueError("Conversation must belong to a model or an agent")
83 stamp = workspace.ledger_stamp() if workspace is not None else {}
84 conversation = Conversation(
85 **({"id": conversation_id} if conversation_id else {}),
86 owner_user_id=owner_user_id,
87 ai_model_id=ai_model_id,
88 agent_id=agent.id if agent else None,
89 workspace_organisation_id=stamp.get("owner_organisation_id"),
90 workspace_project_id=stamp.get("owner_project_id"),
91 messages=messages,
92 total_input_tokens=input_tokens,
93 total_output_tokens=output_tokens,
94 total_tokens=input_tokens + output_tokens,
95 )
97 db.add(conversation)
98 db.commit()
99 db.refresh(conversation)
101 return conversation
104def update_conversation(
105 db: Session,
106 *,
107 conversation: Conversation,
108 messages: List[ChatMessage],
109 input_tokens: int,
110 output_tokens: int,
111) -> Conversation:
112 """Update an existing conversation with new messages and token counts."""
113 conversation.messages += messages
115 conversation.total_input_tokens += input_tokens
116 conversation.total_output_tokens += output_tokens
117 conversation.total_tokens = (
118 conversation.total_input_tokens + conversation.total_output_tokens
119 )
121 db.commit()
122 db.refresh(conversation)
124 return conversation