This commit is contained in:
2026-06-17 20:18:37 +08:00
parent 2840aca69b
commit e2bf6aa173
12 changed files with 1102 additions and 2 deletions
+4
View File
@@ -5,6 +5,7 @@ import AdminBlockingManagement from './components/AdminBlockingManagement.vue'
import AdminBot from './components/AdminBot.vue'
import AdminBotDirect from './components/AdminBotDirect.vue'
import AdminDashboard from './components/AdminDashboard.vue'
import AdminLLM from './components/AdminLLM.vue'
import AdminDiscardDetails from './components/AdminDiscardDetails.vue'
import AdminHelpEdit from './components/AdminHelpEdit.vue'
import AdminLogin from './components/AdminLogin.vue'
@@ -29,6 +30,7 @@ const isAdminPage = adminPath.startsWith('/admin')
const isMqttForwardAdminPage = adminPath === '/admin/mqtt_forward' || adminPath === '/admin/mqtt_forward/'
const isBotAdminPage = adminPath === '/admin/bot' || adminPath === '/admin/bot/'
const isBotDirectAdminPage = adminPath === '/admin/bot/direct' || adminPath === '/admin/bot/direct/'
const isLLMAdminPage = adminPath === '/admin/llm' || adminPath === '/admin/llm/'
const isSignAdminPage = adminPath === '/admin/sign' || adminPath === '/admin/sign/'
const detailMatch = currentPath.match(/^\/detailed\/(.+)$/)
const detailedNodeId = detailMatch ? decodeURIComponent(detailMatch[1]) : ''
@@ -550,6 +552,7 @@ onBeforeUnmount(() => {
<a href="/admin/mqtt_forward/" :class="{ active: isMqttForwardAdminPage }">MQTT转发</a>
<a href="/admin/bot" :class="{ active: isBotAdminPage }">机器人</a>
<a href="/admin/bot/direct" :class="{ active: isBotDirectAdminPage }">机器人私聊</a>
<a href="/admin/llm" :class="{ active: isLLMAdminPage }">LLM消息队列</a>
<a href="/admin/sign" :class="{ active: isSignAdminPage }">签到管理</a>
<a href="/admin/map_source" :class="{ active: adminPath === '/admin/map_source' }">地图图源</a>
<a href="/admin/help_edit" :class="{ active: adminPath === '/admin/help_edit' }">帮助编辑</a>
@@ -598,6 +601,7 @@ onBeforeUnmount(() => {
<AdminMqttForward v-else-if="isMqttForwardAdminPage" />
<AdminBot v-else-if="isBotAdminPage" />
<AdminBotDirect v-else-if="isBotDirectAdminPage" />
<AdminLLM v-else-if="isLLMAdminPage" />
<AdminSignManagement v-else-if="isSignAdminPage" />
<AdminMapSource v-else-if="adminPath === '/admin/map_source'" />
<AdminHelpEdit v-else-if="adminPath === '/admin/help_edit'" />
+37
View File
@@ -47,6 +47,8 @@ import type {
TextMessage,
BotDirectMessage,
BotDirectConversationsResponse,
LLMMessage,
LLMMessageStatus,
} from './types'
async function requestJSON<T>(path: string, init?: RequestInit): Promise<T> {
@@ -429,3 +431,38 @@ export type { BotDirectConversation } from './types'
export function sendBotMessage(payload: BotSendMessagePayload): Promise<BotMessageMutationResponse> {
return postJSON<BotMessageMutationResponse>('/api/admin/bot/messages', payload)
}
// LLM 消息队列 API
export function getLLMMessages(limit = 100, offset = 0, botId?: number, includeDeleted = false): Promise<ListResponse<LLMMessage>> {
const params = new URLSearchParams({ limit: String(limit), offset: String(offset) })
if (botId !== undefined && botId > 0) {
params.set('bot_id', String(botId))
}
if (includeDeleted) {
params.set('include_deleted', 'true')
}
return getJSON<ListResponse<LLMMessage>>(`/api/admin/llm/messages?${params.toString()}`)
}
export function getLLMMessage(id: number): Promise<{ item: LLMMessage }> {
return getJSON<{ item: LLMMessage }>(`/api/admin/llm/messages/${id}`)
}
export function updateLLMMessageStatus(id: number, status: LLMMessageStatus, error?: string): Promise<{ status: string }> {
return putJSON<{ status: string }>(`/api/admin/llm/messages/${id}/status`, { status, error })
}
export function deleteLLMMessage(id: number): Promise<{ status: string }> {
return deleteJSON<{ status: string }>(`/api/admin/llm/messages/${id}`)
}
export function deleteLLMMessagesByBot(botId: number): Promise<{ status: string }> {
return deleteJSON<{ status: string }>(`/api/admin/llm/bots/${botId}/messages`)
}
export function cleanupDeletedLLMMessages(days = 7): Promise<{ status: string; deleted_count: number }> {
return postJSON<{ status: string; deleted_count: number }>('/api/admin/llm/messages/cleanup', { days })
}
// 静默使用未导出类型,避免 TS6133(未使用的导入)。
export type { LLMMessage, LLMMessageStatus } from './types'
@@ -0,0 +1,463 @@
<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { deleteLLMMessage, deleteLLMMessagesByBot, getAdminRuntimeSettings, getBotNodes, getLLMMessages, updateAdminRuntimeSettings } from '../api'
import type { AdminRuntimeSettings, BotNode, LLMMessage, ListResponse } from '../types'
const loading = ref(false)
const savingSettings = ref(false)
const error = ref('')
const messages = ref<LLMMessage[]>([])
const botNodes = ref<BotNode[]>([])
const total = ref(0)
const limit = 50
const offset = ref(0)
const selectedBotId = ref<number | ''>('')
const includeDeleted = ref(false)
const settings = ref<AdminRuntimeSettings | null>(null)
const statusColors: Record<string, string> = {
pending: 'background-color: #fff3cd;',
processing: 'background-color: #cfe2ff;',
processed: 'background-color: #d1e7dd;',
error: 'background-color: #f8d7da;',
}
const statusLabels: Record<string, string> = {
pending: '待处理',
processing: '处理中',
processed: '已处理',
error: '错误',
}
async function loadSettings() {
try {
const response = await getAdminRuntimeSettings()
settings.value = response.item
} catch (err) {
console.error('加载设置失败:', err)
}
}
async function toggleQueueEnabled() {
if (!settings.value || savingSettings.value) return
savingSettings.value = true
try {
await updateAdminRuntimeSettings({
allow_encrypted_forwarding: settings.value.allow_encrypted_forwarding,
llm_queue_enabled: !settings.value.llm_queue_enabled,
llm_include_channel_messages: settings.value.llm_include_channel_messages,
})
settings.value.llm_queue_enabled = !settings.value.llm_queue_enabled
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
} finally {
savingSettings.value = false
}
}
async function toggleIncludeChannel() {
if (!settings.value || savingSettings.value) return
savingSettings.value = true
try {
await updateAdminRuntimeSettings({
allow_encrypted_forwarding: settings.value.allow_encrypted_forwarding,
llm_queue_enabled: settings.value.llm_queue_enabled,
llm_include_channel_messages: !settings.value.llm_include_channel_messages,
})
settings.value.llm_include_channel_messages = !settings.value.llm_include_channel_messages
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
} finally {
savingSettings.value = false
}
}
function getMessageType(msg: LLMMessage): string {
return msg.bot_id === 0 ? '频道' : '私聊'
}
async function loadBotNodes() {
try {
const response = await getBotNodes(100, 0)
botNodes.value = response.items
} catch (err) {
console.error('加载机器人列表失败:', err)
}
}
async function loadMessages(resetOffset = false) {
if (resetOffset) {
offset.value = 0
}
loading.value = true
error.value = ''
try {
const botId = selectedBotId.value === '' ? undefined : selectedBotId.value
const response: ListResponse<LLMMessage> = await getLLMMessages(limit, offset.value, botId, includeDeleted.value)
messages.value = response.items
total.value = response.total ?? response.items.length
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
} finally {
loading.value = false
}
}
async function handleDeleteMessage(id: number) {
if (!confirm('确定要删除这条消息吗?')) {
return
}
try {
await deleteLLMMessage(id)
await loadMessages()
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
}
}
async function handleDeleteAllByBot() {
const botId = selectedBotId.value === '' ? undefined : selectedBotId.value
if (!botId) {
alert('请先选择机器人')
return
}
if (!confirm(`确定要删除该机器人的所有队列消息吗?`)) {
return
}
try {
await deleteLLMMessagesByBot(botId)
await loadMessages()
} catch (err) {
error.value = err instanceof Error ? err.message : String(err)
}
}
function formatTime(timeStr: string | null): string {
if (!timeStr) return '-'
const date = new Date(timeStr)
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
}
const displayFrom = computed(() => offset.value + 1)
const displayTo = computed(() => Math.min(offset.value + limit, total.value))
const hasMore = computed(() => offset.value + limit < total.value)
const hasPrev = computed(() => offset.value > 0)
function goPrev() {
if (hasPrev.value) {
offset.value = Math.max(0, offset.value - limit)
loadMessages()
}
}
function goNext() {
if (hasMore.value) {
offset.value += limit
loadMessages()
}
}
onMounted(() => {
loadSettings()
loadBotNodes()
loadMessages()
})
</script>
<template>
<div class="admin-llm">
<h2>LLM 消息队列</h2>
<div class="admin-llm-toolbar">
<div class="admin-llm-filter">
<label>
<input type="checkbox" :checked="settings?.llm_queue_enabled ?? false" @change="toggleQueueEnabled" :disabled="savingSettings" />
启用 LLM 消息队列
</label>
</div>
<div class="admin-llm-filter">
<label>
<input type="checkbox" :checked="settings?.llm_include_channel_messages ?? false" @change="toggleIncludeChannel" :disabled="savingSettings || !settings?.llm_queue_enabled" />
包含频道消息
</label>
</div>
<div class="admin-llm-filter">
<label>选择机器人</label>
<select v-model="selectedBotId" @change="loadMessages(true)">
<option :value="''">全部</option>
<option v-for="bot in botNodes" :key="bot.id" :value="bot.id">
{{ bot.long_name }} ({{ bot.node_id }})
</option>
</select>
</div>
<div class="admin-llm-filter">
<label>
<input type="checkbox" v-model="includeDeleted" @change="loadMessages(true)" />
包含已删除
</label>
</div>
<button class="admin-button admin-button-danger" @click="handleDeleteAllByBot" :disabled="!selectedBotId">
删除该机器人所有消息
</button>
<button class="admin-button" @click="() => loadMessages()">刷新</button>
</div>
<p v-if="error" class="error">{{ error }}</p>
<div class="admin-llm-stats">
{{ total }} 条消息当前显示 {{ displayFrom }} - {{ displayTo }}
</div>
<div v-if="loading" class="admin-loading">加载中...</div>
<table v-else class="admin-llm-table">
<thead>
<tr>
<th>ID</th>
<th>类型</th>
<th>状态</th>
<th>来自节点</th>
<th>频道</th>
<th>消息内容</th>
<th>接收时间</th>
<th>处理时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="msg in messages" :key="msg.id" :style="statusColors[msg.status]">
<td>{{ msg.id }}</td>
<td>
<span class="type-badge" :class="getMessageType(msg) === '频道' ? 'channel' : 'direct'">
{{ getMessageType(msg) }}
</span>
</td>
<td>
<span class="status-badge" :style="statusColors[msg.status]">
{{ statusLabels[msg.status] || msg.status }}
</span>
</td>
<td>
<div class="node-info">
<div class="node-name">{{ msg.long_name || msg.short_name || '-' }}</div>
<div class="node-id">{{ msg.from_node_id }}</div>
</div>
</td>
<td class="channel-cell">{{ msg.channel_id || (msg.bot_id === 0 ? '默认频道' : '-') }}</td>
<td class="message-text">{{ msg.text }}</td>
<td>{{ formatTime(msg.received_at) }}</td>
<td>{{ formatTime(msg.processed_at) }}</td>
<td>
<button class="admin-button admin-button-small admin-button-danger" @click="handleDeleteMessage(msg.id)">
删除
</button>
</td>
</tr>
<tr v-if="messages.length === 0">
<td colspan="9" class="empty-state">暂无消息</td>
</tr>
</tbody>
</table>
<div class="admin-llm-pagination">
<button class="admin-button admin-button-small" @click="goPrev" :disabled="!hasPrev">上一页</button>
<span class="pagination-info"> {{ Math.floor(offset / limit) + 1 }} </span>
<button class="admin-button admin-button-small" @click="goNext" :disabled="!hasMore">下一页</button>
</div>
</div>
</template>
<style scoped>
.admin-llm {
padding: 1rem;
max-width: 100%;
}
.admin-llm h2 {
margin: 0 0 1.5rem;
font-size: 1.5rem;
}
.admin-llm-toolbar {
display: flex;
gap: 1rem;
align-items: center;
margin-bottom: 1rem;
flex-wrap: wrap;
}
.admin-llm-filter {
display: flex;
align-items: center;
gap: 0.5rem;
}
.admin-llm-filter label {
font-size: 0.9rem;
color: #666;
}
.admin-llm-filter select {
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 4px;
min-width: 200px;
}
.admin-llm-stats {
padding: 0.75rem;
background: #f8f9fa;
border-radius: 4px;
margin-bottom: 1rem;
font-size: 0.9rem;
color: #666;
}
.admin-loading {
padding: 2rem;
text-align: center;
color: #666;
}
.type-badge {
display: inline-block;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.8rem;
font-weight: 500;
}
.type-badge.channel {
background-color: #e2e8f0;
color: #475569;
}
.type-badge.direct {
background-color: #fce7f3;
color: #be185d;
}
.channel-cell {
font-family: monospace;
font-size: 0.8rem;
color: #64748b;
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.admin-llm-table {
width: 100%;
border-collapse: collapse;
font-size: 0.9rem;
}
.admin-llm-table th,
.admin-llm-table td {
padding: 0.75rem;
text-align: left;
border-bottom: 1px solid #eee;
}
.admin-llm-table th {
background: #f8f9fa;
font-weight: 600;
position: sticky;
top: 0;
}
.admin-llm-table tr:hover {
background: rgba(0, 0, 0, 0.02);
}
.status-badge {
display: inline-block;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.8rem;
font-weight: 500;
}
.node-info {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.node-name {
font-weight: 500;
}
.node-id {
font-size: 0.8rem;
color: #666;
font-family: monospace;
}
.message-text {
max-width: 400px;
word-break: break-word;
line-height: 1.4;
}
.empty-state {
text-align: center;
padding: 3rem !important;
color: #999;
}
.admin-llm-pagination {
display: flex;
align-items: center;
justify-content: center;
gap: 1rem;
margin-top: 1.5rem;
padding-top: 1rem;
border-top: 1px solid #eee;
}
.pagination-info {
color: #666;
font-size: 0.9rem;
}
.error {
color: #dc3545;
padding: 0.75rem;
background: #f8d7da;
border-radius: 4px;
margin-bottom: 1rem;
}
.admin-button-small {
padding: 0.25rem 0.5rem;
font-size: 0.8rem;
}
.admin-button-danger {
background: #dc3545;
color: white;
}
.admin-button-danger:hover:not(:disabled) {
background: #c82333;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
+33
View File
@@ -333,10 +333,14 @@ export interface AdminMqttClient {
export interface AdminRuntimeSettings {
allow_encrypted_forwarding: boolean
llm_queue_enabled: boolean
llm_include_channel_messages: boolean
}
export interface AdminRuntimeSettingsPayload {
allow_encrypted_forwarding: boolean
llm_queue_enabled?: boolean
llm_include_channel_messages?: boolean
}
export interface AdminRuntimeSettingsResponse {
@@ -580,3 +584,32 @@ export interface BotMessageMutationResponse {
item: BotMessage
error?: string
}
// LLM 消息队列
export type LLMMessageStatus = 'pending' | 'processing' | 'processed' | 'error'
export interface LLMMessage {
id: number
bot_id: number
bot_node_id: string
bot_node_num: number
from_node_id: string
from_node_num: number
long_name: string | null
short_name: string | null
text: string
packet_id: number
channel_id: string | null
topic: string
status: LLMMessageStatus
error: string
received_at: string
processed_at: string | null
deleted_at: string | null
created_at: string
}
export interface LLMMessageStatusPayload {
status: LLMMessageStatus
error?: string
}