feat: AIOps智能助手对话历史持久化(按用户隔离,刷新后仍可见)
This commit is contained in:
1 parent
6321886b69
commit
32de3709ed
6 files changed
+109
-5
No files matched your search
@@ -23,7 +23,7 @@ import {
|
||||
} from '@/components/ui/select';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { toast } from 'sonner';
|
||||
import { aiApi, type LLMConfigData } from '@/services/api';
|
||||
import { aiApi, type LLMConfigData, type ChatHistoryItem } from '@/services/api';
|
||||
import {
|
||||
Bot,
|
||||
Send,
|
||||
@@ -110,7 +110,28 @@ export function AIOpsAssistant() {
|
||||
setConfigReady(false);
|
||||
}
|
||||
};
|
||||
useEffect(() => { loadConfig(); }, []);
|
||||
|
||||
// 加载历史对话(按用户隔离,刷新后仍可见) // Logan.Li
|
||||
const loadHistory = async () => {
|
||||
try {
|
||||
const res = await aiApi.getHistory();
|
||||
const items: ChatHistoryItem[] = (res as any)?.list || [];
|
||||
setMessages(
|
||||
items.map((m) => ({
|
||||
id: String(m.id),
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
timestamp: m.created_at,
|
||||
})),
|
||||
);
|
||||
} catch {
|
||||
// 历史加载失败不阻塞主流程
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
loadConfig();
|
||||
loadHistory();
|
||||
}, []);
|
||||
|
||||
// 切换提供商时自动填充默认 endpoint + 模型 // Logan.Li
|
||||
const handleProviderChange = (provider: string) => {
|
||||
@@ -189,10 +210,16 @@ export function AIOpsAssistant() {
|
||||
}
|
||||
};
|
||||
|
||||
// 新对话 // Logan.Li
|
||||
const handleNewChat = () => {
|
||||
// 新对话(清空本地 + 后端持久化历史) // Logan.Li
|
||||
const handleNewChat = async () => {
|
||||
try {
|
||||
await aiApi.clearHistory();
|
||||
} catch {
|
||||
// 清除失败不阻塞本地清空
|
||||
}
|
||||
setMessages([]);
|
||||
setInput('');
|
||||
toast.success('已开始新对话');
|
||||
};
|
||||
|
||||
const currentProvider = LLM_PROVIDERS.find((p) => p.id === config.provider);
|
||||
|
||||
@@ -653,6 +653,16 @@ export interface ChatResult {
|
||||
tokens_used: number;
|
||||
}
|
||||
|
||||
// 对话历史条目(后端 ChatHistory 序列化) // Logan.Li
|
||||
export interface ChatHistoryItem {
|
||||
id: number;
|
||||
user_id: number;
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
model?: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export const aiApi = {
|
||||
// 获取 LLM 配置(API Key 已脱敏)
|
||||
getConfig: () => http.get<LLMConfigData>('/aiops/llm-config'),
|
||||
@@ -663,4 +673,8 @@ export const aiApi = {
|
||||
// AI 对话(messages 含历史 + 当前用户消息)
|
||||
chat: (messages: { role: 'user' | 'assistant'; content: string }[]) =>
|
||||
http.post<ChatResult>('/aiops/chat', { messages }),
|
||||
// 获取当前用户对话历史(按时间正序,最近 100 条)
|
||||
getHistory: () => http.get<{ list: ChatHistoryItem[]; total: number }>('/aiops/chat-history'),
|
||||
// 清空当前用户对话历史
|
||||
clearHistory: () => http.delete('/aiops/chat-history'),
|
||||
};
|
||||
Reference in new issue
Block a user