Files
Cyrene/backend/platform-bridge/internal/handler/log_ws.go
T
AskaEth 47dce276a4 fix: platform_silent记忆提取 + 群聊上下文整合 + 多QQ实例支持
- platform_silent模式接入Orchestrator记忆提取:被动观察群聊时提取值得记住的信息到对应命名空间
- post_chat后台思考注入平台观察:对话后思考也能看到群聊摘要
- QQ适配器:OneBot v11 self_id动态捕获、CQ图片URL提取、视觉+OCR并行处理
- Router解耦:ConfigName/PlatformName分离,支持多QQ实例独立连接
- 黑白名单功能:后端API + Ethend代理 + UI面板
- \n\n双换行断句:AI回复按双换行分割为多条消息按间隔发送
- @提及修复:bot自感知UID进行@检测
- 群聊上下文共享:channel-based userID避免记忆碎片化
- 消息日志显示处理后内容而非原始SSE数据
- platform-bridge Dockerfile + docker-compose.yml更新

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-31 09:37:18 +08:00

87 lines
1.8 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"sync"
"github.com/gorilla/websocket"
"git.yeij.top/AskaEth/Cyrene/platform-bridge/internal/logging"
)
var wsUpgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
// LogWSHub broadcasts log entries to connected WebSocket clients.
type LogWSHub struct {
mu sync.Mutex
clients map[*websocket.Conn]chan logging.LogEntry
}
// NewLogWSHub creates a LogWSHub and subscribes to the logger.
func NewLogWSHub(logger *logging.Logger) *LogWSHub {
h := &LogWSHub{
clients: make(map[*websocket.Conn]chan logging.LogEntry),
}
logger.OnLog(func(entry logging.LogEntry) {
h.broadcast(entry)
})
return h
}
func (h *LogWSHub) broadcast(entry logging.LogEntry) {
h.mu.Lock()
defer h.mu.Unlock()
for _, ch := range h.clients {
select {
case ch <- entry:
default:
}
}
}
// ServeWS handles WebSocket upgrade and streams log entries to the client.
func (h *LogWSHub) ServeWS(w http.ResponseWriter, r *http.Request) {
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
return
}
ch := make(chan logging.LogEntry, 64)
h.mu.Lock()
h.clients[conn] = ch
h.mu.Unlock()
// Write goroutine: drains ch until it is closed.
done := make(chan struct{})
go func() {
defer close(done)
for entry := range ch {
data, _ := json.Marshal(entry)
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
return
}
}
}()
// Read goroutine: detect client disconnect.
// (websocket requires a reader to detect close frames.)
go func() {
for {
if _, _, err := conn.ReadMessage(); err != nil {
break
}
}
// Client disconnected — stop broadcasting, close channel.
h.mu.Lock()
delete(h.clients, conn)
h.mu.Unlock()
close(ch)
}()
<-done
conn.Close()
}