-
-
Notifications
You must be signed in to change notification settings - Fork 250
Nestbot AI Direct message implementation #2374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/nestbot-ai-assistant
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """Chat admin configuration.""" | ||
|
|
||
| from django.contrib import admin | ||
|
|
||
| from apps.slack.models.chat import Chat | ||
|
|
||
|
|
||
| class ChatAdmin(admin.ModelAdmin): | ||
| """Admin for Chat model.""" | ||
|
|
||
| list_display = ("user", "workspace", "created_at") | ||
| list_filter = ("user", "workspace") | ||
| search_fields = ("user__username", "workspace__name") | ||
|
|
||
|
|
||
| admin.site.register(Chat, ChatAdmin) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Generated by Django 5.2.6 on 2025-09-26 19:24 | ||
|
|
||
| import django.db.models.deletion | ||
| import django.utils.timezone | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("slack", "0019_conversation_is_nest_bot_assistant_enabled"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Chat", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.BigAutoField( | ||
| auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
| ), | ||
| ), | ||
| ("nest_created_at", models.DateTimeField(auto_now_add=True)), | ||
| ("nest_updated_at", models.DateTimeField(auto_now=True)), | ||
| ("context", models.TextField(blank=True)), | ||
| ( | ||
| "created_at", | ||
| models.DateTimeField( | ||
| default=django.utils.timezone.now, verbose_name="Created at" | ||
| ), | ||
| ), | ||
| ("is_active", models.BooleanField(default=True)), | ||
| ( | ||
| "user", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="chats", | ||
| to="slack.member", | ||
| ), | ||
| ), | ||
| ( | ||
| "workspace", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="chats", | ||
| to="slack.workspace", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "db_table": "slack_chat", | ||
| "ordering": ["-created_at"], | ||
| "unique_together": {("user", "workspace")}, | ||
| }, | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """Chat model for storing conversation context.""" | ||
|
|
||
| from django.db import models | ||
| from django.utils import timezone | ||
|
|
||
| from apps.common.models import TimestampedModel | ||
| from apps.slack.models.member import Member | ||
| from apps.slack.models.workspace import Workspace | ||
|
|
||
|
|
||
| class Chat(TimestampedModel): | ||
|
||
| """Store chat conversation context for DMs.""" | ||
|
|
||
| context = models.TextField(blank=True) | ||
| created_at = models.DateTimeField(verbose_name="Created at", default=timezone.now) | ||
| is_active = models.BooleanField(default=True) | ||
| user = models.ForeignKey(Member, on_delete=models.CASCADE, related_name="chats") | ||
| workspace = models.ForeignKey(Workspace, on_delete=models.CASCADE, related_name="chats") | ||
|
|
||
| class Meta: | ||
| db_table = "slack_chat" | ||
| unique_together = [["user", "workspace"]] | ||
| ordering = ["-created_at"] | ||
|
|
||
| def __str__(self): | ||
| """Return a concise, human-readable identifier for this chat.""" | ||
| return f"Chat with {self.user.real_name or self.user.username} in {self.workspace.name}" | ||
|
|
||
| @staticmethod | ||
| def update_data(user: Member, workspace: Workspace, *, save: bool = True) -> "Chat": | ||
| """Update or create chat data for a user in a workspace. | ||
| Args: | ||
| user: Member instance to associate with the chat. | ||
| workspace: Workspace instance to associate with the chat. | ||
| save: Whether to save the chat to the database. | ||
| Returns: | ||
| Updated or created Chat instance. | ||
| """ | ||
| try: | ||
| chat = Chat.objects.get(user=user, workspace=workspace) | ||
| except Chat.DoesNotExist: | ||
| chat = Chat(user=user, workspace=workspace, is_active=True) | ||
|
|
||
| if save: | ||
| chat.save() | ||
|
|
||
Dishant1804 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return chat | ||
|
|
||
| def add_to_context(self, user_message: str, bot_response: str | None = None) -> None: | ||
|
||
| """Add messages to the conversation context. | ||
| Args: | ||
| user_message: The user's message to add to context. | ||
| bot_response: The bot's response to add to context. | ||
| """ | ||
| if not self.context: | ||
| self.context = "" | ||
|
|
||
| self.context += f"User: {user_message}\n" | ||
|
|
||
| if bot_response: | ||
| self.context += f"Bot: {bot_response}\n" | ||
|
|
||
| self.save(update_fields=["context"]) | ||
|
|
||
| def get_context(self, limit_exchanges: int | None = None) -> str: | ||
| """Get the conversation context. | ||
| Args: | ||
| limit_exchanges: Optional limit on number of exchanges to return. | ||
| Returns: | ||
| The conversation context, potentially limited to recent exchanges. | ||
| """ | ||
| if not self.context: | ||
| return "" | ||
|
|
||
| if limit_exchanges is None: | ||
| return self.context | ||
|
|
||
| lines = self.context.strip().split("\n") | ||
| if len(lines) <= limit_exchanges * 2: | ||
| return self.context | ||
|
|
||
| return "\n".join(lines[-(limit_exchanges * 2) :]) | ||
Uh oh!
There was an error while loading. Please reload this page.