groupchat

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const SchemaSQL = `` /* 2593-byte string literal not displayed */

数据库 Schema 初始化

Variables

This section is empty.

Functions

func GenerateInviteCode

func GenerateInviteCode() string

GenerateInviteCode generates a random 6-digit invite code

func InitSchema

func InitSchema(db *sql.DB) error

InitSchema 初始化数据库表

func Now

func Now() int64

Now returns current timestamp in milliseconds

Types

type AgentClient

type AgentClient struct {
	ID        string `json:"id"`
	RoomID    string `json:"roomId"`
	Profile   string `json:"profile"`
	Name      string `json:"name"`
	SessionID string `json:"sessionId"`
	Connected bool   `json:"connected"`
	CreatedAt int64  `json:"createdAt"`
}

AgentClient Agent 客户端

type ChatMessage

type ChatMessage struct {
	ID         string `json:"id"`
	RoomID     string `json:"roomId"`
	SenderID   string `json:"senderId"`
	SenderName string `json:"senderName"`
	Content    string `json:"content"`
	Timestamp  int64  `json:"timestamp"`
	Type       string `json:"type,omitempty"` // text, system, agent
}

ChatMessage represents a message in the chat

type Client

type Client struct {
	ID          string `json:"id"`
	UserID      string `json:"userId"`
	Name        string `json:"name"`
	Description string `json:"description"`
	RoomID      string `json:"roomId,omitempty"`
	Conn        *websocket.Conn
	// contains filtered or unexported fields
}

Client WebSocket 客户端

func (*Client) Send added in v0.3.0

func (c *Client) Send(data interface{}) error

Send sends a message to the client

type CompressionConfig

type CompressionConfig struct {
	TriggerTokens    int `json:"triggerTokens"`
	MaxHistoryTokens int `json:"maxHistoryTokens"`
	TailMessageCount int `json:"tailMessageCount"`
}

CompressionConfig holds context compression settings

func DefaultCompressionConfig

func DefaultCompressionConfig() CompressionConfig

DefaultCompressionConfig returns default compression settings

type ContextCompressor added in v0.3.0

type ContextCompressor struct {
	// contains filtered or unexported fields
}

ContextCompressor handles context compression for rooms

type CreateRoomRequest added in v0.3.0

type CreateRoomRequest struct {
	Name        string            `json:"name"`
	InviteCode  string            `json:"inviteCode,omitempty"`
	Compression CompressionConfig `json:"compression,omitempty"`
}

CreateRoomRequest represents a request to create a room

type ErrorResponse added in v0.3.0

type ErrorResponse struct {
	Error   string `json:"error"`
	Message string `json:"message,omitempty"`
}

ErrorResponse represents an error response

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler HTTP 处理器

func NewHandler

func NewHandler(storage *Storage) *Handler

NewHandler 创建处理器

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现 http.Handler

type JoinRequest added in v0.3.0

type JoinRequest struct {
	UserID      string `json:"userId"`
	Name        string `json:"name"`
	Description string `json:"description"`
	RoomID      string `json:"roomId"`
	InviteCode  string `json:"inviteCode,omitempty"`
}

JoinRequest represents a request to join a room

type Member

type Member struct {
	ID          string `json:"id"`
	RoomID      string `json:"roomId"`
	UserID      string `json:"userId"`
	Name        string `json:"name"`
	Description string `json:"description"`
	JoinedAt    int64  `json:"joinedAt"`
	LastSeenAt  int64  `json:"lastSeenAt"`
	Online      bool   `json:"online,omitempty"`
	SocketID    string `json:"socketId,omitempty"`
}

Member represents a user in a room

type Message

type Message struct {
	Type      string          `json:"type"`
	RoomID    string          `json:"roomId,omitempty"`
	Data      json.RawMessage `json:"data,omitempty"`
	Timestamp int64           `json:"timestamp"`
}

Message 消息结构

type MessageEvent added in v0.3.0

type MessageEvent struct {
	Type      string      `json:"type"`
	RoomID    string      `json:"roomId,omitempty"`
	Data      interface{} `json:"data,omitempty"`
	Timestamp int64       `json:"timestamp"`
}

MessageEvent represents a message event for WebSocket

type Room

type Room struct {
	ID               string `json:"id"`
	Name             string `json:"name"`
	InviteCode       string `json:"inviteCode,omitempty"`
	TriggerTokens    int    `json:"triggerTokens,omitempty"`
	MaxHistoryTokens int    `json:"maxHistoryTokens,omitempty"`
	TailMessageCount int    `json:"tailMessageCount,omitempty"`
	TotalTokens      int    `json:"totalTokens,omitempty"`
	CreatedAt        int64  `json:"createdAt"`
	UpdatedAt        int64  `json:"updatedAt"`
}

Room represents a group chat room

type RoomAgent

type RoomAgent struct {
	ID          string `json:"id"`
	RoomID      string `json:"roomId"`
	AgentID     string `json:"agentId"`
	Profile     string `json:"profile"`
	Name        string `json:"name"`
	Description string `json:"description"`
	Invited     int    `json:"invited"`
	SessionID   string `json:"sessionId,omitempty"`
	CreatedAt   int64  `json:"createdAt"`
}

RoomAgent represents an AI agent in a room

type RoomInfo added in v0.3.0

type RoomInfo struct {
	Room     *Room         `json:"room"`
	Members  []Member      `json:"members"`
	Agents   []RoomAgent   `json:"agents"`
	Messages []ChatMessage `json:"messages"`
}

RoomInfo contains room information with members and messages

type SendMessageRequest added in v0.3.0

type SendMessageRequest struct {
	RoomID      string `json:"roomId"`
	Content     string `json:"content"`
	MessageType string `json:"type,omitempty"`
}

SendMessageRequest represents a request to send a message

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server Group Chat WebSocket 服务器

func NewServer

func NewServer(storage *Storage) *Server

NewServer 创建服务器

func (*Server) Close

func (s *Server) Close()

Close 关闭服务器

func (*Server) GetOnlineCount added in v0.3.0

func (s *Server) GetOnlineCount() int

GetOnlineCount returns the number of online users

func (*Server) GetOrCreateCompressor added in v0.3.0

func (s *Server) GetOrCreateCompressor(roomID string) *ContextCompressor

GetOrCreateCompressor gets or creates a context compressor for a room

func (*Server) GetRoomClients added in v0.3.0

func (s *Server) GetRoomClients(roomID string) []*Client

GetRoomClients returns all clients in a room

func (*Server) GetRoomCount added in v0.3.0

func (s *Server) GetRoomCount() int

GetRoomCount returns the number of active rooms

func (*Server) HandleWebSocket

func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket 处理 WebSocket 连接

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage 数据存储

func NewStorage

func NewStorage(db *sql.DB) *Storage

NewStorage 创建存储实例

func (*Storage) AddMember

func (s *Storage) AddMember(member *Member) error

AddMember 添加成员

func (*Storage) AgentExistsInRoom

func (s *Storage) AgentExistsInRoom(roomID, profile string) (bool, error)

AgentExistsInRoom 检查代理是否在房间

func (*Storage) CreateAgent

func (s *Storage) CreateAgent(agent *RoomAgent) error

CreateAgent 创建代理

func (*Storage) CreateRoom

func (s *Storage) CreateRoom(room *Room) error

CreateRoom 创建房间

func (*Storage) DeleteAgent

func (s *Storage) DeleteAgent(agentID string) error

DeleteAgent 删除代理

func (*Storage) DeleteMessagesBefore added in v0.3.0

func (s *Storage) DeleteMessagesBefore(roomID string, timestamp int64) error

DeleteMessagesBefore 删除指定时间之前的消息

func (*Storage) DeleteRoom

func (s *Storage) DeleteRoom(roomID string) error

DeleteRoom 删除房间

func (*Storage) DeleteSessionProfile

func (s *Storage) DeleteSessionProfile(sessionID string) error

DeleteSessionProfile 删除会话配置

func (*Storage) GetAgent

func (s *Storage) GetAgent(agentID string) (*RoomAgent, error)

GetAgent 获取代理

func (*Storage) GetAgents

func (s *Storage) GetAgents(roomID string) ([]RoomAgent, error)

GetAgents 获取房间代理

func (*Storage) GetAllRooms

func (s *Storage) GetAllRooms() ([]Room, error)

GetAllRooms 获取所有房间

func (*Storage) GetMembers

func (s *Storage) GetMembers(roomID string) ([]Member, error)

GetMembers 获取房间成员

func (*Storage) GetMessageCount added in v0.3.0

func (s *Storage) GetMessageCount(roomID string) (int, error)

GetMessageCount 获取房间消息数量

func (*Storage) GetMessages

func (s *Storage) GetMessages(roomID string, limit int) ([]ChatMessage, error)

GetMessages 获取消息

func (*Storage) GetRecentMessages added in v0.3.0

func (s *Storage) GetRecentMessages(roomID string, count int) ([]ChatMessage, error)

GetRecentMessages 获取最近的消息(用于压缩后保留)

func (*Storage) GetRoom

func (s *Storage) GetRoom(roomID string) (*Room, error)

GetRoom 获取房间

func (*Storage) GetRoomByInviteCode

func (s *Storage) GetRoomByInviteCode(code string) (*Room, error)

GetRoomByInviteCode 通过邀请码获取房间

func (*Storage) GetSessionProfile

func (s *Storage) GetSessionProfile(sessionID string) (roomID, agentID, profileName string, err error)

GetSessionProfile 获取会话配置

func (*Storage) RemoveMember

func (s *Storage) RemoveMember(roomID, userID string) error

RemoveMember 移除成员

func (*Storage) SaveMessage

func (s *Storage) SaveMessage(msg *ChatMessage) error

SaveMessage 保存消息

func (*Storage) SaveSessionProfile

func (s *Storage) SaveSessionProfile(sessionID, roomID, agentID, profileName string) error

SaveSessionProfile 保存会话配置

func (*Storage) UpdateAgent

func (s *Storage) UpdateAgent(agent *RoomAgent) error

UpdateAgent 更新代理

func (*Storage) UpdateMember

func (s *Storage) UpdateMember(member *Member) error

UpdateMember 更新成员

func (*Storage) UpdateRoom

func (s *Storage) UpdateRoom(room *Room) error

UpdateRoom 更新房间

func (*Storage) UpdateRoomConfig

func (s *Storage) UpdateRoomConfig(roomID string, config CompressionConfig) error

UpdateRoomConfig 更新房间配置

func (*Storage) UpdateRoomTokens

func (s *Storage) UpdateRoomTokens(roomID string, totalTokens int) error

UpdateRoomTokens 更新房间 token 数

type SuccessResponse added in v0.3.0

type SuccessResponse struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data,omitempty"`
	Message string      `json:"message,omitempty"`
}

SuccessResponse represents a success response

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL