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:
@@ -26,9 +26,21 @@ type Message struct {
|
||||
Role string `json:"role"`
|
||||
MsgType string `json:"msg_type"`
|
||||
Content string `json:"content"`
|
||||
ClientID string `json:"client_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// ClientRecord 客户端记录 (持久化)
|
||||
type ClientRecord 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"`
|
||||
FirstSeenAt time.Time `json:"first_seen_at"`
|
||||
LastSeenAt time.Time `json:"last_seen_at"`
|
||||
}
|
||||
|
||||
// SessionStore 会话持久化存储
|
||||
type SessionStore struct {
|
||||
db *sql.DB
|
||||
@@ -92,6 +104,19 @@ func (s *SessionStore) migrate() error {
|
||||
|
||||
// 为已存在的数据库添加 msg_type 列 (Phase 0.1)
|
||||
`ALTER TABLE messages ADD COLUMN IF NOT EXISTS msg_type VARCHAR(16) DEFAULT 'chat'`,
|
||||
// 为已存在的数据库添加 client_id 列 (Phase 5: 多端客户端追踪)
|
||||
`ALTER TABLE messages ADD COLUMN IF NOT EXISTS client_id VARCHAR(128) DEFAULT ''`,
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS clients (
|
||||
client_id VARCHAR(128) PRIMARY KEY,
|
||||
user_id VARCHAR(128) NOT NULL,
|
||||
device_name VARCHAR(256) DEFAULT '',
|
||||
user_agent VARCHAR(512) DEFAULT '',
|
||||
note VARCHAR(256) DEFAULT '',
|
||||
first_seen_at TIMESTAMP DEFAULT NOW(),
|
||||
last_seen_at TIMESTAMP DEFAULT NOW()
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_clients_user_id ON clients(user_id)`,
|
||||
}
|
||||
|
||||
for _, q := range queries {
|
||||
@@ -205,10 +230,10 @@ func (s *SessionStore) DeleteAllUserSessions(userID string) error {
|
||||
}
|
||||
|
||||
// AddMessage 添加一条消息到会话
|
||||
func (s *SessionStore) AddMessage(sessionID, role, msgType, content string) error {
|
||||
func (s *SessionStore) AddMessage(sessionID, role, msgType, content, clientID string) error {
|
||||
_, err := s.db.Exec(
|
||||
`INSERT INTO messages (session_id, role, msg_type, content) VALUES ($1, $2, $3, $4)`,
|
||||
sessionID, role, msgType, content,
|
||||
`INSERT INTO messages (session_id, role, msg_type, content, client_id) VALUES ($1, $2, $3, $4, $5)`,
|
||||
sessionID, role, msgType, content, clientID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("添加消息失败: %w", err)
|
||||
@@ -226,7 +251,7 @@ func (s *SessionStore) GetMessages(sessionID string, limit, offset int) ([]Messa
|
||||
}
|
||||
|
||||
rows, err := s.db.Query(
|
||||
`SELECT id, session_id, role, COALESCE(msg_type, 'chat'), content, created_at
|
||||
`SELECT id, session_id, role, COALESCE(msg_type, 'chat'), content, COALESCE(client_id, ''), created_at
|
||||
FROM messages WHERE session_id = $1
|
||||
ORDER BY created_at ASC
|
||||
LIMIT $2 OFFSET $3`,
|
||||
@@ -240,7 +265,7 @@ func (s *SessionStore) GetMessages(sessionID string, limit, offset int) ([]Messa
|
||||
var messages []Message
|
||||
for rows.Next() {
|
||||
var msg Message
|
||||
if err := rows.Scan(&msg.ID, &msg.SessionID, &msg.Role, &msg.MsgType, &msg.Content, &msg.CreatedAt); err != nil {
|
||||
if err := rows.Scan(&msg.ID, &msg.SessionID, &msg.Role, &msg.MsgType, &msg.Content, &msg.ClientID, &msg.CreatedAt); err != nil {
|
||||
return nil, fmt.Errorf("扫描消息行失败: %w", err)
|
||||
}
|
||||
messages = append(messages, msg)
|
||||
@@ -341,3 +366,57 @@ func (s *SessionStore) IsAvailable() bool {
|
||||
}
|
||||
return s.db.Ping() == nil
|
||||
}
|
||||
|
||||
// ========== 多端客户端追踪 ==========
|
||||
|
||||
// UpsertClient inserts or updates a client record.
|
||||
func (s *SessionStore) UpsertClient(clientID, userID, deviceName, userAgent string) error {
|
||||
_, err := s.db.Exec(
|
||||
`INSERT INTO clients (client_id, user_id, device_name, user_agent, first_seen_at, last_seen_at)
|
||||
VALUES ($1, $2, $3, $4, NOW(), NOW())
|
||||
ON CONFLICT (client_id) DO UPDATE SET
|
||||
device_name = EXCLUDED.device_name,
|
||||
user_agent = EXCLUDED.user_agent,
|
||||
last_seen_at = NOW()`,
|
||||
clientID, userID, deviceName, userAgent,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("upsert client failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClients returns all known clients for a user.
|
||||
func (s *SessionStore) GetClients(userID string) ([]ClientRecord, error) {
|
||||
rows, err := s.db.Query(
|
||||
`SELECT client_id, user_id, device_name, user_agent, note, first_seen_at, last_seen_at
|
||||
FROM clients WHERE user_id = $1 ORDER BY last_seen_at DESC`,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query clients failed: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var result []ClientRecord
|
||||
for rows.Next() {
|
||||
var cr ClientRecord
|
||||
if err := rows.Scan(&cr.ClientID, &cr.UserID, &cr.DeviceName, &cr.UserAgent, &cr.Note, &cr.FirstSeenAt, &cr.LastSeenAt); err != nil {
|
||||
return nil, fmt.Errorf("scan client row failed: %w", err)
|
||||
}
|
||||
result = append(result, cr)
|
||||
}
|
||||
if result == nil {
|
||||
result = []ClientRecord{}
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// UpdateClientNote sets the user-defined note for a client.
|
||||
func (s *SessionStore) UpdateClientNote(clientID, note string) error {
|
||||
_, err := s.db.Exec(`UPDATE clients SET note = $1 WHERE client_id = $2`, note, clientID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("update client note failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user