""" Django Admin für den AI Agent. Erreichbar unter /administration/agent/ """ from django.contrib import admin from django.utils.html import format_html from .models import AgentConfig, ChatSession, ChatMessage @admin.register(AgentConfig) class AgentConfigAdmin(admin.ModelAdmin): fieldsets = ( ("Provider", { "fields": ("provider", "model_name", "ollama_url"), }), ("API-Keys (externe Provider)", { "fields": ("openai_api_key", "anthropic_api_key"), "classes": ("collapse",), "description": "Nur ausfüllen wenn nicht Ollama verwendet wird.", }), ("Verhalten", { "fields": ("system_prompt", "allow_write", "chat_retention_days"), }), ) def has_add_permission(self, request): # Singleton: Hinzufügen nur wenn noch keine Config existiert return not AgentConfig.objects.exists() def has_delete_permission(self, request, obj=None): return False class ChatMessageInline(admin.TabularInline): model = ChatMessage fields = ("role", "content_preview", "tool_name", "created_at") readonly_fields = ("role", "content_preview", "tool_name", "created_at") extra = 0 can_delete = False ordering = ["created_at"] def content_preview(self, obj): return obj.content[:120] + ("…" if len(obj.content) > 120 else "") content_preview.short_description = "Inhalt" @admin.register(ChatSession) class ChatSessionAdmin(admin.ModelAdmin): list_display = ("title_or_id", "user", "message_count", "created_at", "updated_at") list_filter = ("user",) search_fields = ("title", "user__username") readonly_fields = ("id", "user", "created_at", "updated_at") inlines = [ChatMessageInline] ordering = ["-updated_at"] def title_or_id(self, obj): return obj.title or str(obj.id)[:12] title_or_id.short_description = "Sitzung" def message_count(self, obj): return obj.messages.count() message_count.short_description = "Nachrichten"