feat: AIOps智能助手对话历史持久化(按用户隔离,刷新后仍可见)

This commit is contained in:
attacker committed 2026-06-18 03:05:30 +08:00
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);