)}
- {/* 建议标签 */}
{!isUser && msg.suggestions && msg.suggestions.length > 0 && (
{msg.suggestions.map((s, i) => (
@@ -185,7 +184,7 @@ function ConversationListItem({
);
}
-// ─── 欢迎屏幕(无消息时) ─────────────────────────────────────────
+// ─── 欢迎屏幕 ─────────────────────────────────────────────────────
function WelcomeScreen({
agentType,
suggestedQuestions,
@@ -200,18 +199,17 @@ function WelcomeScreen({
return (
- {/* Logo 区域 */}
-
NAC公链AI
{info && (
{info.name} · {info.desc}
)}
-
基于 NAC 原生知识库 · CBPP共识 · Charter合约
+
+ 基于 NAC 原生知识库 · CBPP共识 · Charter合约 · 参与即是共识
+
- {/* 建议问题 */}
{suggestedQuestions.length > 0 && (
试试这些问题:
@@ -241,28 +239,27 @@ export default function AIAgents() {
const [showHistory, setShowHistory] = useState(true);
const messagesEndRef = useRef
(null);
const textareaRef = useRef(null);
- const [, navigate] = useLocation();
- // 来源引用跳转
- const handleSourceClick = (source: string) => {
- const keyword = source.split("·").pop() || source;
- navigate(`/knowledge-base?search=${encodeURIComponent(keyword)}`);
- };
+ // 获取当前用户(公开接口,未登录返回 null)
+ const { data: currentUser } = trpc.nacAuth.whoami.useQuery(undefined, {
+ retry: false,
+ refetchOnWindowFocus: false,
+ });
// 获取 Agent 列表和状态
const { data: agentData, isLoading: agentLoading } = trpc.aiAgent.list.useQuery();
const { data: statusData } = trpc.aiAgent.status.useQuery();
- // 获取会话列表
+ // 获取会话列表(已登录用户才有历史)
const { data: convData, refetch: refetchConvs } = trpc.aiAgent.listConversations.useQuery(
{ agentType: selectedAgent || undefined, limit: 50 },
- { enabled: true }
+ { enabled: !!currentUser }
);
// 加载历史消息
- const loadHistoryQuery = trpc.aiAgent.getConversation.useQuery(
+ const loadHistoryQuery = trpc.aiAgent.loadHistory.useQuery(
{ conversationId: activeConvId! },
- { enabled: !!activeConvId, refetchOnWindowFocus: false }
+ { enabled: !!activeConvId && !!currentUser, refetchOnWindowFocus: false }
);
useEffect(() => {
@@ -286,6 +283,13 @@ export default function AIAgents() {
onError: (e) => toast.error(`删除失败: ${e.message}`),
});
+ // 登出
+ const logoutMutation = trpc.nacAuth.logout.useMutation({
+ onSuccess: () => {
+ window.location.reload();
+ },
+ });
+
// 发送消息
const chatMutation = trpc.aiAgent.chat.useMutation({
onSuccess: (response) => {
@@ -300,7 +304,7 @@ export default function AIAgents() {
if (!activeConvId && response.conversationId) {
setActiveConvId(response.conversationId);
}
- refetchConvs();
+ if (currentUser) refetchConvs();
},
onError: (error) => {
toast.error(`响应失败: ${error.message}`);
@@ -345,7 +349,7 @@ export default function AIAgents() {
agentType: selectedAgent,
userMessage: text,
conversationId: activeConvId || undefined,
- persistHistory: true,
+ persistHistory: !!currentUser, // 仅登录用户持久化历史
});
};
@@ -361,7 +365,7 @@ export default function AIAgents() {
const agentInfo = AGENT_LABELS[selectedAgent];
return (
-
+
{/* ══════════════════════════════════════════
左侧边栏
@@ -431,34 +435,46 @@ export default function AIAgents() {
- {/* 历史会话 */}
-
-
+ {/* 历史会话(仅登录用户) */}
+ {currentUser && (
+
+
- {showHistory && (
-
- {conversations.length === 0 ? (
-
暂无历史会话
- ) : (
- conversations.map(conv => (
-
handleSelectConversation(conv)}
- onDelete={() => deleteConvMutation.mutate({ conversationId: conv.conversationId })}
- />
- ))
- )}
+ {showHistory && (
+
+ {conversations.length === 0 ? (
+
暂无历史会话
+ ) : (
+ conversations.map(conv => (
+
handleSelectConversation(conv)}
+ onDelete={() => deleteConvMutation.mutate({ conversationId: conv.conversationId })}
+ />
+ ))
+ )}
+
+ )}
+
+ )}
+
+ {/* 未登录时:引导提示 */}
+ {!currentUser && (
+
+
+
登录后可保存对话历史
+
参与即是共识 · CBPP原则
- )}
-
+
+ )}
{/* ══════════════════════════════════════════
@@ -468,6 +484,7 @@ export default function AIAgents() {
{/* 顶部栏 */}
+ {/* 左:AI 信息 */}
{agentInfo && (() => {
@@ -482,6 +499,8 @@ export default function AIAgents() {
+
+ {/* 右:用户区域 */}
{activeConvId && (
@@ -521,7 +583,7 @@ export default function AIAgents() {
{/* 消息列表 */}
{messages.map((msg, i) => (
-
+
))}
{/* AI 思考中 */}
@@ -577,7 +639,7 @@ export default function AIAgents() {
- NAC公链AI 基于 NAC 原生知识库提供参考,重要合规决策请咨询专业法律顾问
+ NAC公链AI 基于 NAC 原生知识库提供参考 · 参与即是共识 · CBPP原则