mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
6aebbe9279
Major changes: 1. Core Architecture: Decoupled Bots from Users. Bots now have independent lifecycles, member management (bot_members), and dedicated configurations. 2. Channel Gateway: - Implemented a unified Channel Manager supporting Feishu, Telegram, and Local (Web/CLI) adapters. - Added message processing pipeline to normalize interactions across different platforms. - Introduced a Contact system for identity binding and guest access policies. 3. Database & Tooling: - Consolidated all migrations into 0001_init with updated schema for bots, channels, and contacts. - Optimized sqlc.yaml to automatically track the migrations directory. 4. Agent Enhancements: - Introduced ToolContext to provide Agents with platform-aware execution capabilities (e.g., messaging, contact lookups). - Added tool logging and fallback mechanisms for toolChoice execution. 5. UI & Docs: Updated frontend stores, UI components, and Swagger documentation to align with the new Bot-centric model.
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package channel
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type SessionHub struct {
|
|
mu sync.RWMutex
|
|
sessions map[string]map[string]chan OutboundMessage
|
|
}
|
|
|
|
func NewSessionHub() *SessionHub {
|
|
return &SessionHub{
|
|
sessions: map[string]map[string]chan OutboundMessage{},
|
|
}
|
|
}
|
|
|
|
func (h *SessionHub) Subscribe(sessionID string) (string, <-chan OutboundMessage, func()) {
|
|
streamID := uuid.NewString()
|
|
ch := make(chan OutboundMessage, 32)
|
|
|
|
h.mu.Lock()
|
|
streams, ok := h.sessions[sessionID]
|
|
if !ok {
|
|
streams = map[string]chan OutboundMessage{}
|
|
h.sessions[sessionID] = streams
|
|
}
|
|
streams[streamID] = ch
|
|
h.mu.Unlock()
|
|
|
|
cancel := func() {
|
|
h.mu.Lock()
|
|
streams := h.sessions[sessionID]
|
|
if streams != nil {
|
|
if current, ok := streams[streamID]; ok {
|
|
delete(streams, streamID)
|
|
close(current)
|
|
}
|
|
if len(streams) == 0 {
|
|
delete(h.sessions, sessionID)
|
|
}
|
|
}
|
|
h.mu.Unlock()
|
|
}
|
|
|
|
return streamID, ch, cancel
|
|
}
|
|
|
|
func (h *SessionHub) Publish(sessionID string, msg OutboundMessage) {
|
|
h.mu.RLock()
|
|
streams := h.sessions[sessionID]
|
|
h.mu.RUnlock()
|
|
if len(streams) == 0 {
|
|
return
|
|
}
|
|
|
|
for _, stream := range streams {
|
|
select {
|
|
case stream <- msg:
|
|
default:
|
|
// Drop if receiver is slow.
|
|
}
|
|
}
|
|
}
|