修复机器人聊天记录与修改机器人信息

This commit is contained in:
2026-06-14 21:17:49 +08:00
parent 491876284e
commit 0024841ffb
9 changed files with 460 additions and 54 deletions
+14
View File
@@ -43,6 +43,8 @@ import type {
TelemetryRecord,
TextMessage,
BotDirectMessage,
BotDirectConversation,
BotDirectConversationsResponse,
} from './types'
async function requestJSON<T>(path: string, init?: RequestInit): Promise<T> {
@@ -386,6 +388,18 @@ export function getBotDirectMessages(botId: number, targetNodeNum: number, limit
return getJSON<ListResponse<BotDirectMessage>>(`/api/admin/bot/direct-messages?${params.toString()}`)
}
export function getBotConversations(botId: number, limit = 100, offset = 0): Promise<BotDirectConversationsResponse> {
const params = new URLSearchParams({ bot_id: String(botId), limit: String(limit), offset: String(offset) })
return getJSON<BotDirectConversationsResponse>(`/api/admin/bot/conversations?${params.toString()}`)
}
export function markBotDirectMessagesRead(botId: number, peerNodeNum: number): Promise<{ updated: number }> {
return postJSON<{ updated: number }>('/api/admin/bot/direct-messages/read', { bot_id: botId, peer_node_num: peerNodeNum })
}
// 静默使用未导出类型,避免 TS6133(未使用的导入)。
export type { BotDirectConversation } from './types'
export function sendBotMessage(payload: BotSendMessagePayload): Promise<BotMessageMutationResponse> {
return postJSON<BotMessageMutationResponse>('/api/admin/bot/messages', payload)
}
@@ -1,18 +1,21 @@
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onBeforeUpdate, onMounted, onUpdated, ref, watch } from 'vue'
import { getBotDirectMessages, getBotNodes, getNodeInfo, sendBotMessage } from '../api'
import type { BotDirectMessage, BotNode, NodeInfo } from '../types'
import { getBotConversations, getBotDirectMessages, getBotNodes, getNodeInfo, markBotDirectMessagesRead, sendBotMessage } from '../api'
import type { BotDirectConversation, BotDirectMessage, BotNode, NodeInfo } from '../types'
const chatPageSize = 30
const conversationPageSize = 100
const maxTextBytes = 200
const topThreshold = 8
const bottomThreshold = 40
const bots = ref<BotNode[]>([])
const targets = ref<NodeInfo[]>([])
const conversations = ref<BotDirectConversation[]>([])
const unreadTotal = ref(0)
const messages = ref<BotDirectMessage[]>([])
const selectedBotId = ref<number | null>(null)
const selectedTargetId = ref('')
const selectedPeerNum = ref<number | null>(null)
const text = ref('')
const loading = ref(false)
const sending = ref(false)
@@ -30,11 +33,15 @@ let restoreScrollTop = 0
let restoreMessageCount = 0
const selectedBot = computed(() => bots.value.find((item) => item.id === selectedBotId.value) ?? null)
const selectedTarget = computed(() => targets.value.find((item) => item.node_id === selectedTargetId.value) ?? null)
const selectedConversation = computed(() => conversations.value.find((item) => item.peer_node_num === selectedPeerNum.value) ?? null)
const selectedPeerNode = computed(() => {
if (selectedPeerNum.value == null) return null
return targets.value.find((node) => node.node_num === selectedPeerNum.value) ?? null
})
const directTextBytes = computed(() => new TextEncoder().encode(text.value).length)
const canSend = computed(() => !!selectedBot.value && !!selectedTarget.value && !!text.value.trim() && directTextBytes.value <= maxTextBytes && !sending.value)
const canSend = computed(() => !!selectedBot.value && selectedPeerNum.value != null && !!text.value.trim() && directTextBytes.value <= maxTextBytes && !sending.value)
const groupedMessages = computed(() => {
// 同一条 DM 在 inbound/outbound 两侧不会重复(来源是同一个表),但保留按 packet_id+text 合并的能力以容忍重复 publish。
// direction + packet_id + text 作为合并键,避免重复 publish 时把 inbound/outbound 误合并
const groups = new Map<string, BotDirectMessage & { mergedCount: number; mergedMessages: BotDirectMessage[] }>()
for (const item of messages.value) {
const key = `${item.direction}\n${item.packet_id ?? ''}\n${item.text ?? ''}`
@@ -49,12 +56,15 @@ const groupedMessages = computed(() => {
return Array.from(groups.values())
})
watch(selectedBot, () => {
watch(selectedBotId, async () => {
selectedPeerNum.value = null
conversations.value = []
unreadTotal.value = 0
resetChat()
loadInitialMessages()
await reloadConversations()
})
watch(selectedTargetId, () => {
watch(selectedPeerNum, () => {
resetChat()
loadInitialMessages()
})
@@ -104,6 +114,8 @@ async function refreshLists() {
targets.value = nodeResponse.items
if (!selectedBotId.value && bots.value.length > 0) {
selectedBotId.value = bots.value[0].id
} else {
await reloadConversations()
}
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
@@ -112,17 +124,64 @@ async function refreshLists() {
}
}
async function reloadConversations() {
if (!selectedBot.value) return
try {
const response = await getBotConversations(selectedBot.value.id, conversationPageSize, 0)
conversations.value = response.items
unreadTotal.value = response.unread_total
// 第一次进入或目标会话被删除时自动选中第一个会话,避免空白页面。
if (selectedPeerNum.value == null && response.items.length > 0) {
selectedPeerNum.value = response.items[0].peer_node_num
} else if (selectedPeerNum.value != null && !response.items.some((c) => c.peer_node_num === selectedPeerNum.value)) {
selectedPeerNum.value = response.items[0]?.peer_node_num ?? null
}
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
}
}
async function selectConversation(conv: BotDirectConversation) {
if (selectedPeerNum.value === conv.peer_node_num) return
selectedPeerNum.value = conv.peer_node_num
}
async function startConversationFromPicker(nodeNum: number) {
if (!Number.isFinite(nodeNum) || nodeNum <= 0) return
// 如果会话不在侧边栏,先做本地占位再从后端拉一次列表
if (!conversations.value.some((c) => c.peer_node_num === nodeNum)) {
const peer = targets.value.find((n) => n.node_num === nodeNum)
if (peer) {
conversations.value = [
{
bot_id: selectedBot.value?.id ?? 0,
peer_node_id: peer.node_id,
peer_node_num: peer.node_num,
last_message_at: '',
last_text: '',
last_direction: '',
unread_count: 0,
total_count: 0,
},
...conversations.value,
]
}
}
selectedPeerNum.value = nodeNum
}
async function loadInitialMessages() {
if (!selectedBot.value || !selectedTarget.value) return
if (!selectedBot.value || selectedPeerNum.value == null) return
loadingOlder.value = true
try {
const response = await getBotDirectMessages(selectedBot.value.id, selectedTarget.value.node_num, chatPageSize, 0)
const response = await getBotDirectMessages(selectedBot.value.id, selectedPeerNum.value, chatPageSize, 0)
messages.value = toChronological(response.items)
hasMore.value = response.items.length === chatPageSize
initialized.value = true
await nextTick()
const el = panelRef.value
if (el) el.scrollTop = el.scrollHeight
await markCurrentConversationRead()
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
} finally {
@@ -131,10 +190,10 @@ async function loadInitialMessages() {
}
async function loadOlderMessages() {
if (!selectedBot.value || !selectedTarget.value || loadingOlder.value || !hasMore.value) return
if (!selectedBot.value || selectedPeerNum.value == null || loadingOlder.value || !hasMore.value) return
loadingOlder.value = true
try {
const response = await getBotDirectMessages(selectedBot.value.id, selectedTarget.value.node_num, chatPageSize, messages.value.length)
const response = await getBotDirectMessages(selectedBot.value.id, selectedPeerNum.value, chatPageSize, messages.value.length)
messages.value = mergeMessages(messages.value, toChronological(response.items))
hasMore.value = response.items.length === chatPageSize
} catch (err) {
@@ -145,18 +204,49 @@ async function loadOlderMessages() {
}
async function pollLatestMessages() {
if (!selectedBot.value || !selectedTarget.value) return
const response = await getBotDirectMessages(selectedBot.value.id, selectedTarget.value.node_num, chatPageSize, 0)
if (!selectedBot.value) return
await reloadConversations()
if (selectedPeerNum.value == null) return
const response = await getBotDirectMessages(selectedBot.value.id, selectedPeerNum.value, chatPageSize, 0)
const before = messages.value.length
messages.value = mergeMessages(messages.value, toChronological(response.items))
if (messages.value.length > before) {
await markCurrentConversationRead()
}
}
async function markCurrentConversationRead() {
if (!selectedBot.value || selectedPeerNum.value == null) return
const conv = selectedConversation.value
if (conv && conv.unread_count === 0) return
try {
const result = await markBotDirectMessagesRead(selectedBot.value.id, selectedPeerNum.value)
if (result.updated > 0) {
// 本地立即清零,避免 polling 间隙仍然展示红点。
conversations.value = conversations.value.map((c) =>
c.peer_node_num === selectedPeerNum.value ? { ...c, unread_count: 0 } : c,
)
unreadTotal.value = Math.max(0, unreadTotal.value - result.updated)
}
} catch (err) {
// 标记已读失败只打印不打断聊天体验
console.warn('mark read failed', err)
}
}
async function sendDirectMessage() {
if (!selectedBot.value || !selectedTarget.value) return
if (!selectedBot.value || selectedPeerNum.value == null) return
const peer = selectedPeerNode.value
const peerNodeId = peer ? peer.node_id : selectedConversation.value?.peer_node_id
if (!peerNodeId) {
error.value = '找不到目标节点 ID,请等待 NodeInfo 同步后再试'
return
}
sending.value = true
error.value = ''
notice.value = ''
try {
const response = await sendBotMessage({ bot_id: selectedBot.value.id, message_type: 'direct', channel_id: 'PKI', to_node_id: selectedTarget.value.node_id, text: text.value })
const response = await sendBotMessage({ bot_id: selectedBot.value.id, message_type: 'direct', channel_id: 'PKI', to_node_id: peerNodeId, text: text.value })
if (response.error) {
error.value = response.error
} else {
@@ -188,7 +278,8 @@ function isOwn(item: BotDirectMessage) {
function senderName(item: BotDirectMessage) {
if (item.direction === 'outbound') return selectedBot.value?.long_name || item.bot_node_id
return selectedTarget.value?.long_name || selectedTarget.value?.short_name || item.peer_node_id
const peer = targets.value.find((n) => n.node_num === item.peer_node_num)
return peer?.long_name || peer?.short_name || item.peer_node_id
}
function statusLabel(item: BotDirectMessage) {
@@ -198,10 +289,39 @@ function statusLabel(item: BotDirectMessage) {
return ''
}
function conversationTitle(conv: BotDirectConversation) {
const peer = targets.value.find((n) => n.node_num === conv.peer_node_num)
return peer?.long_name || peer?.short_name || conv.peer_node_id
}
function previewText(conv: BotDirectConversation) {
if (!conv.last_text) return '暂无消息'
const prefix = conv.last_direction === 'outbound' ? '我: ' : ''
const text = conv.last_text.replace(/\s+/g, ' ').trim()
return prefix + (text.length > 32 ? text.slice(0, 32) + '…' : text)
}
function formatTime(value: string) {
if (!value) return ''
return new Date(value).toLocaleString()
}
function formatRelative(value: string) {
if (!value) return ''
const ts = Date.parse(value)
if (!Number.isFinite(ts)) return ''
const diff = Date.now() - ts
if (diff < 60_000) return '刚刚'
if (diff < 3600_000) return Math.floor(diff / 60_000) + ' 分钟前'
if (diff < 86400_000) return Math.floor(diff / 3600_000) + ' 小时前'
return new Date(ts).toLocaleDateString()
}
const peerNodeOptions = computed(() => {
const seen = new Set(conversations.value.map((c) => c.peer_node_num))
return targets.value.filter((node) => !seen.has(node.node_num))
})
onBeforeUpdate(() => {
const el = panelRef.value
if (el) shouldStickToBottom = isNearBottom(el)
@@ -239,7 +359,11 @@ onBeforeUnmount(() => {
<div class="direct-header">
<div>
<p class="eyebrow">Direct Bot Chat</p>
<h2>机器人私聊 <span class="pki-badge" title="使用 X25519 + AES-CCM 与目标节点端到端加密">PKI 加密</span></h2>
<h2>
机器人私聊
<span class="pki-badge" title="使用 X25519 + AES-CCM 与目标节点端到端加密">PKI 加密</span>
<span v-if="unreadTotal > 0" class="header-unread-badge">{{ unreadTotal > 99 ? '99+' : unreadTotal }} 未读</span>
</h2>
</div>
<div class="direct-actions">
<a class="admin-button secondary" href="/admin/bot">返回频道聊天</a>
@@ -250,42 +374,67 @@ onBeforeUnmount(() => {
<p v-if="error" class="error">{{ error }}</p>
<p v-if="notice" class="success">{{ notice }}</p>
<div class="direct-selectors">
<div class="direct-bot-picker">
<label>机器人
<select v-model="selectedBotId">
<option :value="null">选择机器人</option>
<option v-for="bot in bots" :key="bot.id" :value="bot.id">{{ bot.long_name }} · {{ bot.node_id }}</option>
</select>
</label>
<label>目标节点
<select v-model="selectedTargetId">
<option value="">选择目标节点</option>
<option v-for="node in targets" :key="node.node_id" :value="node.node_id">{{ node.long_name || node.short_name || node.node_id }} · {{ node.node_id }}</option>
<label>新建私聊
<select :value="''" @change="(event) => { startConversationFromPicker(Number((event.target as HTMLSelectElement).value)); (event.target as HTMLSelectElement).value = '' }">
<option value="">选择目标节点开启会话</option>
<option v-for="node in peerNodeOptions" :key="node.node_id" :value="node.node_num">{{ node.long_name || node.short_name || node.node_id }} · {{ node.node_id }}</option>
</select>
</label>
</div>
<p class="direct-hint">私聊固定走 PKIchannel_id = "PKI"需要目标节点已上报 NodeInfo 公钥才能加密</p>
<div class="direct-layout">
<aside class="conversation-list">
<p v-if="conversations.length === 0" class="empty-state">还没有私聊会话等设备发来消息或在上方新建私聊开启</p>
<button
v-for="conv in conversations"
:key="conv.peer_node_num"
class="conversation-item"
:class="{ active: conv.peer_node_num === selectedPeerNum }"
@click="selectConversation(conv)"
>
<div class="conversation-row">
<span class="conversation-title">{{ conversationTitle(conv) }}</span>
<span class="conversation-time">{{ formatRelative(conv.last_message_at) }}</span>
</div>
<div class="conversation-row">
<span class="conversation-preview">{{ previewText(conv) }}</span>
<span v-if="conv.unread_count > 0" class="conversation-unread">{{ conv.unread_count > 99 ? '99+' : conv.unread_count }}</span>
</div>
<div class="conversation-meta">{{ conv.peer_node_id }} · {{ conv.total_count }} </div>
</button>
</aside>
<div ref="panelRef" class="direct-chat-list" @scroll.passive="handleScroll">
<div v-if="loadingOlder" class="chat-loading">正在加载更早消息...</div>
<div v-else-if="!hasMore && messages.length > 0" class="chat-end">没有更多历史消息</div>
<div v-if="groupedMessages.length === 0" class="empty-state">请选择机器人和目标节点或当前会话暂无消息</div>
<div v-for="item in groupedMessages" :key="item.id" class="chat-bubble-row" :class="{ own: isOwn(item) }">
<div class="chat-bubble">
<div class="bubble-meta"><strong>{{ senderName(item) }}</strong><small>{{ formatTime(item.created_at) }}</small></div>
<div class="bubble-text">{{ item.text || '[binary]' }} <span v-if="item.mergedCount > 1" class="message-merge-count">x{{ item.mergedCount }}</span></div>
<div v-if="statusLabel(item)" class="bubble-status">{{ statusLabel(item) }}</div>
<div class="bubble-topic">{{ item.topic }}</div>
<div class="direct-main">
<p class="direct-hint">私聊固定走 PKIchannel_id = "PKI"需要目标节点已上报 NodeInfo 公钥才能加密</p>
<div ref="panelRef" class="direct-chat-list" @scroll.passive="handleScroll">
<div v-if="loadingOlder" class="chat-loading">正在加载更早消息...</div>
<div v-else-if="!hasMore && messages.length > 0" class="chat-end">没有更多历史消息</div>
<div v-if="groupedMessages.length === 0" class="empty-state">{{ selectedPeerNum == null ? '请选择左侧会话或新建私聊' : '当前会话暂无消息' }}</div>
<div v-for="item in groupedMessages" :key="item.id" class="chat-bubble-row" :class="{ own: isOwn(item) }">
<div class="chat-bubble">
<div class="bubble-meta"><strong>{{ senderName(item) }}</strong><small>{{ formatTime(item.created_at) }}</small></div>
<div class="bubble-text">{{ item.text || '[binary]' }} <span v-if="item.mergedCount > 1" class="message-merge-count">x{{ item.mergedCount }}</span></div>
<div v-if="statusLabel(item)" class="bubble-status">{{ statusLabel(item) }}</div>
<div class="bubble-topic">{{ item.topic }}</div>
</div>
</div>
</div>
</div>
</div>
<div class="direct-composer">
<textarea v-model="text" rows="3" placeholder="输入私聊消息"></textarea>
<div class="send-actions">
<span class="hint" :class="{ warn: directTextBytes > maxTextBytes }">{{ directTextBytes }} / {{ maxTextBytes }} bytes</span>
<button class="admin-button" @click="sendDirectMessage" :disabled="!canSend">{{ sending ? '发送中...' : '发送私聊' }}</button>
<div class="direct-composer">
<textarea v-model="text" rows="3" :placeholder="selectedPeerNum == null ? '先选择左侧会话再输入' : '输入私聊消息'" :disabled="selectedPeerNum == null"></textarea>
<div class="send-actions">
<span class="hint" :class="{ warn: directTextBytes > maxTextBytes }">{{ directTextBytes }} / {{ maxTextBytes }} bytes</span>
<button class="admin-button" @click="sendDirectMessage" :disabled="!canSend">{{ sending ? '发送中...' : '发送私聊' }}</button>
</div>
</div>
</div>
</div>
</section>
@@ -294,11 +443,24 @@ onBeforeUnmount(() => {
<style scoped>
.direct-page { display: grid; gap: 12px; padding: 16px; }
.direct-header, .direct-actions, .send-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.direct-selectors { display: grid; grid-template-columns: repeat(2, minmax(180px, 1fr)); gap: 12px; }
.direct-hint { color: #475569; font-size: 12px; margin: 0; }
.direct-bot-picker { display: grid; grid-template-columns: repeat(2, minmax(200px, 1fr)); gap: 12px; }
.direct-hint { color: #475569; font-size: 12px; margin: 0 0 8px; }
.pki-badge { display: inline-flex; align-items: center; margin-left: 8px; border-radius: 999px; padding: 2px 10px; color: #1d4ed8; background: #dbeafe; font-size: 12px; font-weight: 700; vertical-align: middle; }
.header-unread-badge { display: inline-flex; align-items: center; margin-left: 8px; border-radius: 999px; padding: 2px 10px; color: #fff; background: #ef4444; font-size: 12px; font-weight: 800; vertical-align: middle; }
label { display: grid; gap: 5px; color: #334155; font-size: 13px; font-weight: 800; }
input, select, textarea { box-sizing: border-box; width: 100%; border: 1px solid #cbd5e1; border-radius: 10px; padding: 9px 11px; color: #0f172a; font: inherit; background: #fff; }
.direct-layout { display: grid; grid-template-columns: minmax(220px, 280px) 1fr; gap: 14px; align-items: start; }
.conversation-list { display: flex; flex-direction: column; gap: 6px; max-height: 620px; overflow: auto; padding: 6px; border: 1px solid #e2e8f0; border-radius: 14px; background: #fff; }
.conversation-item { all: unset; cursor: pointer; display: grid; gap: 4px; padding: 10px 12px; border-radius: 10px; border: 1px solid transparent; }
.conversation-item:hover { background: #f1f5f9; }
.conversation-item.active { background: #dbeafe; border-color: #93c5fd; }
.conversation-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conversation-title { font-weight: 800; color: #0f172a; font-size: 14px; }
.conversation-time { color: #64748b; font-size: 11px; white-space: nowrap; }
.conversation-preview { flex: 1; color: #475569; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conversation-unread { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: #ef4444; color: #fff; font-size: 11px; font-weight: 800; }
.conversation-meta { color: #94a3b8; font-size: 11px; }
.direct-main { display: grid; gap: 12px; }
.direct-chat-list { min-height: 420px; max-height: 560px; overflow: auto; display: flex; flex-direction: column; gap: 10px; border: 1px solid #e2e8f0; border-radius: 14px; padding: 14px; background: linear-gradient(180deg, #f8fafc 0%, #eef4ff 100%); }
.chat-loading, .chat-end { align-self: center; border-radius: 999px; padding: 6px 10px; color: #64748b; font-size: 12px; background: #e2e8f0; }
.chat-bubble-row { display: flex; justify-content: flex-start; }
@@ -315,5 +477,9 @@ input, select, textarea { box-sizing: border-box; width: 100%; border: 1px solid
.direct-composer { display: grid; gap: 10px; }
.admin-button.secondary { color: #334155; text-decoration: none; background: #e2e8f0; }
.empty-state { color: #64748b; padding: 16px; border: 1px dashed #cbd5e1; border-radius: 14px; text-align: center; background: #f8fafc; }
@media (max-width: 800px) { .direct-selectors { grid-template-columns: 1fr; } }
@media (max-width: 800px) {
.direct-layout { grid-template-columns: 1fr; }
.direct-bot-picker { grid-template-columns: 1fr; }
.conversation-list { max-height: 320px; }
}
</style>
+20
View File
@@ -171,9 +171,29 @@ export interface BotDirectMessage {
created_by: string | null
published_at: string | null
received_at: string | null
read_at: string | null
created_at: string
}
// 会话摘要:每个 (bot, peer) 一条,给侧边栏使用。
export interface BotDirectConversation {
bot_id: number
peer_node_id: string
peer_node_num: number
last_message_at: string
last_text: string
last_direction: 'inbound' | 'outbound' | string
unread_count: number
total_count: number
}
export interface BotDirectConversationsResponse {
items: BotDirectConversation[]
limit: number
offset: number
unread_total: number
}
export interface PositionRecord {
id: number
from_id: string