fix: Phase 6联调 — 插件管理器端口修正 + 多模型配置系统整合 + 历史消息刷新修复
## 调试日志
### 1. 插件管理器启动失败
- **症状**: DevTools 显示插件管理器一直"已停止",手动启动正常
- **排查**: 对比 process-manager.js 传入的环境变量 vs plugin-manager config.go 读取的变量
- **根因**: config.js 传入 PLUGIN_MANAGER_PORT=8094,但 config.go 读取 os.Getenv("PORT"),env 名不匹配。且 process.env 中 PORT 泄露时被误读为 9090,与 DevTools 端口冲突
- **修复**: config.js 将 PLUGIN_MANAGER_PORT → PORT,使 env 名与代码一致 (c3055f4)
### 2. 历史消息刷新后消失
- **症状**: 浏览器刷新后聊天历史清空
- **排查**: WebSocket history_response handler 中 if (msg.messages) 对空数组 [] 为 truthy
- **根因**: 后端返回空的 history_response (缓存为空) 时,空数组覆盖了 HTTP 已加载的消息
- **修复**: useWebSocket.ts 改为 if (msg.messages && msg.messages.length > 0),空数组走 else-if 分支仅打日志,不覆盖已有消息
### 3. Phase 6 多模型配置系统
- Gateway: ModelsConfigStore (JSON文件持久化) + Admin CRUD API (providers/models/routing)
- ai-core: ModelSelector 支持按 purpose 选择 + fallback_chain,无配置时回退 .env
- DevTools: 模型配置管理面板 (Providers/Models/Routing 三Tab)、在线模型查询代理、路由表单 checkbox 多选、关键词搜索过滤
- .gitignore: models.json + platform_configs.json
### 4. 多端客户端追踪
- Hub 新增 knownClients 映射 (clientID → KnownClient),在线/离线状态追踪
- 客户端备注持久化到 PostgreSQL
- DevTools 客户端管理面板
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -24,21 +24,27 @@ const (
|
||||
|
||||
// Client WebSocket客户端
|
||||
type Client struct {
|
||||
Hub *Hub
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
UserID string
|
||||
SessionID string
|
||||
Hub *Hub
|
||||
Conn *websocket.Conn
|
||||
Send chan []byte
|
||||
UserID string
|
||||
SessionID string
|
||||
ClientID string
|
||||
DeviceName string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
// NewClient 创建WebSocket客户端
|
||||
func NewClient(hub *Hub, conn *websocket.Conn, userID, sessionID string) *Client {
|
||||
func NewClient(hub *Hub, conn *websocket.Conn, userID, sessionID, clientID, deviceName, userAgent string) *Client {
|
||||
return &Client{
|
||||
Hub: hub,
|
||||
Conn: conn,
|
||||
Send: make(chan []byte, 256),
|
||||
UserID: userID,
|
||||
SessionID: sessionID,
|
||||
Hub: hub,
|
||||
Conn: conn,
|
||||
Send: make(chan []byte, 256),
|
||||
UserID: userID,
|
||||
SessionID: sessionID,
|
||||
ClientID: clientID,
|
||||
DeviceName: deviceName,
|
||||
UserAgent: userAgent,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,10 +39,23 @@ type Message struct {
|
||||
MsgType string `json:"msg_type,omitempty"`
|
||||
Attachments []MessageAttachment `json:"attachments,omitempty"`
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
ClientInfo *ClientInfo `json:"client_info,omitempty"`
|
||||
}
|
||||
|
||||
const maxRecentMessages = 20
|
||||
|
||||
// KnownClient tracks a device that has ever connected (online or offline).
|
||||
type KnownClient struct {
|
||||
ClientID string `json:"client_id"`
|
||||
UserID string `json:"user_id"`
|
||||
DeviceName string `json:"device_name"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
Note string `json:"note"` // user-assigned label
|
||||
Online bool `json:"online"`
|
||||
LastSeenAt time.Time `json:"last_seen_at"`
|
||||
FirstSeenAt time.Time `json:"first_seen_at"`
|
||||
}
|
||||
|
||||
// Hub WebSocket连接池
|
||||
type Hub struct {
|
||||
mu sync.RWMutex
|
||||
@@ -76,6 +89,9 @@ type Hub struct {
|
||||
pendingProactive map[string][]json.RawMessage // userID -> queued messages
|
||||
aiCoreURL string
|
||||
internalToken string
|
||||
|
||||
// 多端客户端追踪: clientID -> KnownClient (在线+离线)
|
||||
knownClients map[string]*KnownClient
|
||||
}
|
||||
|
||||
// SetStore 设置持久化存储 (可选)
|
||||
@@ -100,6 +116,7 @@ func NewHub() *Hub {
|
||||
iotStopCh: make(chan struct{}),
|
||||
idleTimeout: 30 * time.Minute, // 默认30分钟
|
||||
pendingProactive: make(map[string][]json.RawMessage),
|
||||
knownClients: make(map[string]*KnownClient),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -254,6 +271,34 @@ func (h *Hub) Run() {
|
||||
MessageCount: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// 多端客户端追踪
|
||||
if client.ClientID != "" {
|
||||
now := time.Now()
|
||||
if kc, ok := h.knownClients[client.ClientID]; ok {
|
||||
kc.Online = true
|
||||
kc.LastSeenAt = now
|
||||
kc.DeviceName = client.DeviceName
|
||||
kc.UserAgent = client.UserAgent
|
||||
} else {
|
||||
h.knownClients[client.ClientID] = &KnownClient{
|
||||
ClientID: client.ClientID,
|
||||
UserID: client.UserID,
|
||||
DeviceName: client.DeviceName,
|
||||
UserAgent: client.UserAgent,
|
||||
Online: true,
|
||||
LastSeenAt: now,
|
||||
FirstSeenAt: now,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 持久化客户端记录到数据库
|
||||
if client.ClientID != "" && h.store != nil && h.store.IsAvailable() {
|
||||
if err := h.store.UpsertClient(client.ClientID, client.UserID, client.DeviceName, client.UserAgent); err != nil {
|
||||
logger.Printf("[WS] 持久化客户端记录失败: %v", err)
|
||||
}
|
||||
}
|
||||
// Phase 2: 检测是否为重连 (之前处于离线状态)
|
||||
wasOffline := len(h.userClients[client.UserID]) == 1 // 刚加入,之前为0
|
||||
h.mu.Unlock()
|
||||
@@ -308,6 +353,23 @@ func (h *Hub) Run() {
|
||||
s.State = "idle"
|
||||
}
|
||||
}
|
||||
|
||||
// 多端客户端追踪: 检查同一 clientID 是否还有其他连接
|
||||
if client.ClientID != "" {
|
||||
hasOtherClientConn := false
|
||||
for c := range h.clients {
|
||||
if c.ClientID == client.ClientID {
|
||||
hasOtherClientConn = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasOtherClientConn {
|
||||
if kc, ok := h.knownClients[client.ClientID]; ok {
|
||||
kc.Online = false
|
||||
kc.LastSeenAt = time.Now()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
@@ -362,6 +424,23 @@ func (h *Hub) Run() {
|
||||
s.State = "idle"
|
||||
}
|
||||
}
|
||||
|
||||
// 多端客户端追踪
|
||||
if client.ClientID != "" {
|
||||
hasOtherClientConn := false
|
||||
for c := range h.clients {
|
||||
if c.ClientID == client.ClientID {
|
||||
hasOtherClientConn = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasOtherClientConn {
|
||||
if kc, ok := h.knownClients[client.ClientID]; ok {
|
||||
kc.Online = false
|
||||
kc.LastSeenAt = time.Now()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
@@ -766,3 +845,62 @@ func (h *Hub) DeleteConversation(userID, sessionID string) {
|
||||
key := cacheKey(userID, sessionID)
|
||||
h.conversationCache.Delete(key)
|
||||
}
|
||||
|
||||
// ========== 多端客户端追踪 ==========
|
||||
|
||||
// GetKnownClients returns all known clients (online + offline).
|
||||
func (h *Hub) GetKnownClients(userID string) []KnownClient {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
result := make([]KnownClient, 0)
|
||||
for _, kc := range h.knownClients {
|
||||
if userID == "" || kc.UserID == userID {
|
||||
cp := *kc
|
||||
cp.UserAgent = "" // don't leak UA in list
|
||||
result = append(result, cp)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// UpdateClientNote sets a user-defined note/label on a client.
|
||||
func (h *Hub) UpdateClientNote(clientID, note string) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
kc, ok := h.knownClients[clientID]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
kc.Note = note
|
||||
return true
|
||||
}
|
||||
|
||||
// ClientInfo returns the ClientInfo for a given client.
|
||||
func (h *Hub) ClientInfo(clientID string) *ClientInfo {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
kc, ok := h.knownClients[clientID]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &ClientInfo{
|
||||
ClientID: kc.ClientID,
|
||||
DeviceName: kc.DeviceName,
|
||||
UserAgent: kc.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
// buildClientInfo builds a ClientInfo from a Client.
|
||||
func buildClientInfo(c *Client) *ClientInfo {
|
||||
if c.ClientID == "" {
|
||||
return nil
|
||||
}
|
||||
return &ClientInfo{
|
||||
ClientID: c.ClientID,
|
||||
DeviceName: c.DeviceName,
|
||||
UserAgent: c.UserAgent,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,9 @@ type ClientMessage struct {
|
||||
AudioData string `json:"audio_data,omitempty"` // base64
|
||||
Attachments []MessageAttachment `json:"attachments,omitempty"` // 图片等附件
|
||||
Timestamp int64 `json:"timestamp"`
|
||||
ClientID string `json:"client_id,omitempty"` // 客户端唯一标识 (多端区分)
|
||||
DeviceName string `json:"device_name,omitempty"` // 设备备注名称
|
||||
UserAgent string `json:"user_agent,omitempty"` // 浏览器 UA
|
||||
}
|
||||
|
||||
// ReviewMessage 审查后的结构化消息(动作/聊天分离)
|
||||
@@ -30,6 +33,13 @@ type ReviewMessage struct {
|
||||
DelayMs int `json:"delay_ms,omitempty"` // ms to wait before sending (0 = immediate)
|
||||
}
|
||||
|
||||
// ClientInfo carries the originating client's device metadata.
|
||||
type ClientInfo struct {
|
||||
ClientID string `json:"client_id,omitempty"`
|
||||
DeviceName string `json:"device_name,omitempty"`
|
||||
UserAgent string `json:"user_agent,omitempty"`
|
||||
}
|
||||
|
||||
// 服务端 → 客户端消息
|
||||
type ServerMessage struct {
|
||||
Type string `json:"type"` // response | segment | audio | error | device_update | pong | history_response | stream_chunk | stream_end | background_thinking | notification | multi_message | stream_segments | review | thinking | tool_progress | system_info
|
||||
@@ -55,6 +65,7 @@ type ServerMessage struct {
|
||||
ToolProgress *ToolProgressInfo `json:"tool_progress,omitempty"` // 工具执行进度
|
||||
SystemInfo *SystemInfoPayload `json:"system_info,omitempty"` // 系统通知信息
|
||||
ProtocolVersion int `json:"protocol_version,omitempty"` // 协议版本
|
||||
ClientInfo *ClientInfo `json:"client_info,omitempty"` // 消息来源客户端信息
|
||||
}
|
||||
|
||||
// ToolProgressInfo 工具执行进度
|
||||
|
||||
Reference in New Issue
Block a user