-
Notifications
You must be signed in to change notification settings - Fork 575
/
Copy pathchat.py
289 lines (248 loc) · 8.73 KB
/
chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from datetime import datetime, timezone
from enum import Enum
from typing import List, NotRequired, Optional, TypedDict
from pydantic import BaseModel, Field
from sqlalchemy import Column, DateTime, Index, String, func
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Field as SQLModelField
from sqlmodel import SQLModel, desc, select, update
from models.db import get_session
class ChatMessageAttachmentType(str, Enum):
"""Type of chat message attachment."""
LINK = "link"
IMAGE = "image"
FILE = "file"
class AuthorType(str, Enum):
"""Type of message author."""
AGENT = "agent"
TRIGGER = "trigger"
SKILL = "skill"
TELEGRAM = "telegram"
TWITTER = "twitter"
WEB = "web"
SYSTEM = "system"
class ChatMessageAttachment(TypedDict):
"""Chat message attachment model.
An attachment can be a link, image, or file that is associated with a chat message.
"""
type: ChatMessageAttachmentType = Field(
...,
description="Type of the attachment (link, image, or file)",
examples=["link"],
)
url: str = Field(
...,
description="URL of the attachment",
examples=["https://example.com/image.jpg"],
)
class ChatMessageSkillCall(TypedDict):
"""TypedDict for skill call details."""
name: str
parameters: dict
success: bool
response: NotRequired[
str
] # Optional response from the skill call, trimmed to 100 characters
error_message: NotRequired[str] # Optional error message from the skill call
class ChatMessageRequest(BaseModel):
"""Request model for chat messages.
This model represents the request body for creating a new chat message.
It contains the necessary fields to identify the chat context, user,
and message content, along with optional attachments.
"""
chat_id: str = Field(
...,
description="Unique identifier for the chat thread",
examples=["chat-123"],
min_length=1,
)
user_id: str = Field(
...,
description="Unique identifier of the user sending the message",
examples=["user-456"],
min_length=1,
)
message: str = Field(
...,
description="Content of the message",
examples=["Hello, how can you help me today?"],
min_length=1,
)
attachments: Optional[List[ChatMessageAttachment]] = Field(
None,
description="Optional list of attachments (links, images, or files)",
examples=[[{"type": "link", "url": "https://example.com"}]],
)
class Config:
"""Pydantic model configuration."""
use_enum_values = True
json_schema_extra = {
"example": {
"chat_id": "chat-123",
"user_id": "user-456",
"message": "Hello, how can you help me today?",
"attachments": [
{
"type": "link",
"url": "https://example.com",
}
],
}
}
class ChatMessage(SQLModel, table=True):
"""Chat message model."""
__tablename__ = "chat_messages"
__table_args__ = (Index("ix_chat_messages_chat_id", "chat_id"),)
id: str = SQLModelField(
primary_key=True,
description="Unique identifier for the chat message",
)
agent_id: str = SQLModelField(
description="ID of the agent this message belongs to",
)
chat_id: str = SQLModelField(
description="ID of the chat this message belongs to",
)
author_id: str = SQLModelField(
description="ID of the message author",
)
author_type: AuthorType = SQLModelField(
sa_column=Column(String, nullable=False),
description="Type of the message author",
)
message: str = SQLModelField(
description="Content of the message",
)
attachments: Optional[List[ChatMessageAttachment]] = SQLModelField(
default=None,
sa_column=Column(JSONB, nullable=True),
description="List of attachments in the message",
)
skill_calls: Optional[List[ChatMessageSkillCall]] = SQLModelField(
default=None,
sa_column=Column(JSONB, nullable=True),
description="Skill call details",
)
input_tokens: int = SQLModelField(
default=0,
description="Number of tokens in the input message",
)
output_tokens: int = SQLModelField(
default=0,
description="Number of tokens in the output message",
)
time_cost: float = SQLModelField(
default=0.0,
description="Time cost for the message in seconds",
)
cold_start_cost: float = SQLModelField(
default=0.0,
description="Cost for the cold start of the message in seconds",
)
created_at: datetime = SQLModelField(
default_factory=lambda: datetime.now(timezone.utc),
sa_type=DateTime(timezone=True),
sa_column_kwargs={"server_default": func.now()},
nullable=False,
description="Timestamp when this message was created",
)
class Config:
use_enum_values = True
json_encoders = {datetime: lambda v: v.isoformat(timespec="milliseconds")}
def __str__(self):
resp = ""
if self.skill_calls:
for call in self.skill_calls:
resp += f"{call['name']} {call['parameters']}: {call['response'] if call['success'] else call['error_message']}\n"
resp += "\n"
resp += self.message
return resp
async def save(self):
async with get_session() as db:
db.add(self)
await db.commit()
await db.refresh(self)
class Chat(SQLModel, table=True):
"""Chat model."""
__tablename__ = "chats"
__table_args__ = (Index("ix_chats_agent_user", "agent_id", "user_id"),)
id: str = SQLModelField(
primary_key=True,
description="Unique identifier for the chat",
)
agent_id: str = SQLModelField(
description="ID of the agent this chat belongs to",
)
user_id: str = SQLModelField(
description="User ID of the chat",
)
summary: str = SQLModelField(
default="",
description="Summary of the chat",
)
rounds: int = SQLModelField(
default=0,
description="Number of rounds in the chat",
)
created_at: datetime = SQLModelField(
default_factory=lambda: datetime.now(timezone.utc),
sa_type=DateTime(timezone=True),
sa_column_kwargs={"server_default": func.now()},
nullable=False,
description="Timestamp when this chat was created",
)
updated_at: datetime = SQLModelField(
default_factory=lambda: datetime.now(timezone.utc),
sa_type=DateTime(timezone=True),
sa_column_kwargs={
"onupdate": lambda: datetime.now(timezone.utc),
},
nullable=False,
description="Timestamp when this chat was updated",
)
@classmethod
async def get(cls, id: str) -> "Chat":
async with get_session() as db:
return await db.get(cls, id)
async def create(self):
async with get_session() as db:
db.add(self)
await db.commit()
await db.refresh(self)
async def delete(self):
async with get_session() as db:
await db.delete(self)
await db.commit()
async def add_round(self):
"""Increment the number of rounds in the chat on the database server.
Uses a direct SQL UPDATE statement to increment the rounds counter
on the server side, avoiding potential race conditions.
"""
async with get_session() as db:
stmt = update(Chat).where(Chat.id == self.id).values(rounds=Chat.rounds + 1)
await db.exec(stmt)
await db.commit()
async def update_summary(self, summary: str) -> "Chat":
"""Update the chat summary in the database.
Uses a direct SQL UPDATE statement to set the summary field.
Args:
summary: New summary text for the chat
"""
async with get_session() as db:
stmt = update(Chat).where(Chat.id == self.id).values(summary=summary)
await db.exec(stmt)
await db.commit()
# Refresh the local object to reflect the database change
self.summary = summary
return self
@classmethod
async def get_by_agent_user(cls, agent_id: str, user_id: str) -> List["Chat"]:
async with get_session() as db:
return (
await db.exec(
select(cls)
.order_by(desc(cls.updated_at))
.limit(10)
.where(cls.agent_id == agent_id, cls.user_id == user_id)
)
).all()