45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
from django.conf import settings
|
|
from django.db import models
|
|
|
|
|
|
class Conversation(models.Model):
|
|
"""Stores a user's review-agent conversation shell."""
|
|
|
|
user = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
on_delete=models.CASCADE,
|
|
related_name="review_conversations",
|
|
)
|
|
title = models.CharField(max_length=120, blank=True)
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
updated_at = models.DateTimeField(auto_now=True)
|
|
|
|
class Meta:
|
|
ordering = ["-updated_at", "-id"]
|
|
|
|
def __str__(self) -> str:
|
|
return self.title or f"对话 {self.pk}"
|
|
|
|
|
|
class Message(models.Model):
|
|
"""Stores one user or assistant message in a conversation."""
|
|
|
|
class Role(models.TextChoices):
|
|
USER = "user", "用户"
|
|
ASSISTANT = "assistant", "助手"
|
|
|
|
conversation = models.ForeignKey(
|
|
Conversation,
|
|
on_delete=models.CASCADE,
|
|
related_name="messages",
|
|
)
|
|
role = models.CharField(max_length=20, choices=Role.choices)
|
|
content = models.TextField()
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
|
|
class Meta:
|
|
ordering = ["created_at", "id"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.get_role_display()} - {self.conversation_id}"
|