feat(UI-Overhaul-Phase2): AI Assistent in Kommunikation integriert
Check Cross-Plugin Imports / check (push) Has been cancelled
Check Cross-Plugin Imports / check (push) Has been cancelled
Migration 0137: Drop AI chat tables (ai_chat_sessions, ai_chat_messages, ai_chat_attachments, ai_conversations, ai_messages)
Backend:
- Remove AIChatSession, AIChatMessage, AIChatAttachment models from ai_assistant/models.py
- Remove AIConversation, AIMessage from app/models/__init__.py
- Remove session/message/stream/attachment routes from ai_assistant/routes.py
- Add new streaming route POST /ai/conversations/{conversation_id}/stream using comm tables
- Add new messages route GET /ai/conversations/{conversation_id}/messages using comm tables
- Add stream_chat_comm, get_comm_messages, save_comm_message to services.py
- Update external_api.py to use CommConversation/CommMessage instead of AIChatSession/AIChatMessage
- Update unified_search ai_chat_provider to search comm_messages with conversation_type=ai
- Remove ai_copilot router from main.py and routes/__init__.py
- Remove ai_conversation from entity_permissions.py and owner_transfer_service.py
- Update ai_assistant/plugin.py get_entity_models to remove AIChatSession
- Guard ai_copilot_service.py imports with try/except
Frontend:
- Remove AIAssistant.tsx, AIAssistantStandalone.tsx, SessionList.tsx, ChatWindow.tsx
- Remove AI Assistant routes from routes/index.tsx
- Update api/ai.ts: streamChat uses /ai/conversations/{id}/stream, fetchMessages uses /ai/conversations/{id}/messages
- Update Communication.tsx: use convId for AI streaming, remove aiSessionId, use fetchAiMessages for AI conversations
- Update AiChatPanel.tsx: create comm conversation instead of AI session, use new fetchMessages
- Update AISidebar.tsx: remove ChatWindow import, show placeholder
tsc clean, build successful, backend import OK
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
||||
import { ResizablePanel } from '@/components/ui/ResizablePanel';
|
||||
import { usePluginToolbarStore } from '@/store/pluginToolbarStore';
|
||||
import { apiClient } from '@/api/client';
|
||||
import { streamChat, fetchAgents, type AIAgent } from '@/api/ai';
|
||||
import { streamChat, fetchAgents, fetchMessages as fetchAiMessages, type AIAgent } from '@/api/ai';
|
||||
|
||||
// ─── Types ───
|
||||
|
||||
@@ -322,7 +322,6 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
const [miniApps, setMiniApps] = useState<MiniApp[]>([]);
|
||||
const [agents, setAgents] = useState<AIAgent[]>([]);
|
||||
const [selectedAgentId, setSelectedAgentId] = useState<string | null>(null);
|
||||
const [aiSessionId, setAiSessionId] = useState<string | null>(null);
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
|
||||
@@ -331,11 +330,38 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
if (!convId) return;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
fetchMessages(convId)
|
||||
.then(msgs => { setMessages(msgs); setLoading(false); })
|
||||
.catch(e => { setError(e?.message || 'Fehler beim Laden'); setLoading(false); });
|
||||
if (category === 'ai') {
|
||||
// AI conversations load messages from the AI endpoint
|
||||
fetchAiMessages(convId)
|
||||
.then(msgs => {
|
||||
// Convert {role, content}[] to Message[] format
|
||||
const mapped: Message[] = msgs.map((m, i) => ({
|
||||
id: `ai-msg-${i}`,
|
||||
conversation_id: convId,
|
||||
sender_id: null,
|
||||
sender_type: m.role === 'user' ? 'user' : 'ai',
|
||||
content: m.content,
|
||||
content_format: 'text',
|
||||
created_at: null,
|
||||
edited_at: null,
|
||||
is_pinned: false,
|
||||
blocks: [],
|
||||
attachments: [],
|
||||
reactions: [],
|
||||
reply_to_id: null,
|
||||
}));
|
||||
setMessages(mapped);
|
||||
setLoading(false);
|
||||
})
|
||||
.catch(e => { setError(e?.message || 'Fehler beim Laden'); setLoading(false); });
|
||||
} else {
|
||||
// Regular conversations load from comm endpoint
|
||||
fetchMessages(convId)
|
||||
.then(msgs => { setMessages(msgs as unknown as Message[]); setLoading(false); })
|
||||
.catch(e => { setError(e?.message || 'Fehler beim Laden'); setLoading(false); });
|
||||
}
|
||||
markConversationRead(convId).catch(() => {});
|
||||
}, [convId]);
|
||||
}, [convId, category]);
|
||||
|
||||
// Load agents and mini-apps
|
||||
useEffect(() => {
|
||||
@@ -386,12 +412,12 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
if (category === 'ai' && aiSessionId) {
|
||||
// AI streaming chat
|
||||
if (category === 'ai') {
|
||||
// AI streaming chat via comm conversation
|
||||
setAiStreaming(true);
|
||||
setStreamingContent('');
|
||||
let accumulated = '';
|
||||
for await (const event of streamChat(aiSessionId, content, selectedAgentId || undefined)) {
|
||||
for await (const event of streamChat(convId, content, selectedAgentId || undefined)) {
|
||||
if (event.type === 'token' && event.content) {
|
||||
accumulated += event.content;
|
||||
setStreamingContent(accumulated);
|
||||
@@ -405,8 +431,23 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
setAiStreaming(false);
|
||||
setStreamingContent('');
|
||||
// Reload messages to get the AI response
|
||||
const msgs = await fetchMessages(convId);
|
||||
setMessages(msgs);
|
||||
const msgs = await fetchAiMessages(convId);
|
||||
const mapped: Message[] = msgs.map((m, i) => ({
|
||||
id: `ai-msg-${i}`,
|
||||
conversation_id: convId,
|
||||
sender_id: null,
|
||||
sender_type: m.role === 'user' ? 'user' : 'ai',
|
||||
content: m.content,
|
||||
content_format: 'text',
|
||||
created_at: null,
|
||||
edited_at: null,
|
||||
is_pinned: false,
|
||||
blocks: [],
|
||||
attachments: [],
|
||||
reactions: [],
|
||||
reply_to_id: null,
|
||||
}));
|
||||
setMessages(mapped);
|
||||
} else {
|
||||
// Regular message
|
||||
const msg = await sendMessage(convId, content);
|
||||
@@ -426,25 +467,10 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
}
|
||||
};
|
||||
|
||||
const startAiSession = async () => {
|
||||
try {
|
||||
const r = await apiClient.post('/ai/sessions', {
|
||||
title: 'KI Chat',
|
||||
agent_id: selectedAgentId,
|
||||
is_sidebar: false,
|
||||
});
|
||||
setAiSessionId(r.data.id);
|
||||
} catch (e: unknown) { const errObj = asError(e);
|
||||
setError('KI Session konnte nicht gestartet werden');
|
||||
}
|
||||
};
|
||||
|
||||
// Auto-start AI session for AI conversations
|
||||
// Auto-scroll
|
||||
useEffect(() => {
|
||||
if (category === 'ai' && !aiSessionId && selectedAgentId) {
|
||||
startAiSession();
|
||||
}
|
||||
}, [category, aiSessionId, selectedAgentId]);
|
||||
if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
||||
}, [messages, streamingContent]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full bg-white">
|
||||
@@ -462,7 +488,7 @@ function ChatWindow({ convId, category, onBack }: ChatWindowProps) {
|
||||
{category === 'ai' && agents.length > 0 && (
|
||||
<select
|
||||
value={selectedAgentId || ''}
|
||||
onChange={(e) => { setSelectedAgentId(e.target.value || null); setAiSessionId(null); }}
|
||||
onChange={(e) => { setSelectedAgentId(e.target.value || null); }}
|
||||
className="text-xs border border-secondary-200 rounded px-2 py-1 bg-white"
|
||||
>
|
||||
{agents.map(a => (
|
||||
|
||||
Reference in New Issue
Block a user