chat

package
v0.0.0-...-b25a406 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseUserIDFromToken

func ParseUserIDFromToken(authHeader string) (int64, error)

ParseUserIDFromToken 从 Authorization header 解析 userID(WebTransport 握手时使用)

Types

type AIAnswerContent

type AIAnswerContent struct {
	Text            string `json:"text"`                     // AI 回答全文
	QuestionMsgID   int64  `json:"question_msg_id"`          // 触发问题的消息 ID
	InitiatorUserID int64  `json:"initiator_user_id"`        // 发起人 user_id
	InitiatorName   string `json:"initiator_name,omitempty"` // 发起人昵称
	SessionID       int64  `json:"session_id"`               // helper_sessions.id
}

AIAnswerContent ContentType=MsgTypeAIAnswer(7) 持久化到 messages.content 的结构 最终回答写库时序列化为 JSON,前端通过此结构定位原始问题和回答溯源。

type AckMsg

type AckMsg struct {
	ClientSeq      int64  `json:"client_seq"`                // 回显客户端序号
	MsgID          int64  `json:"msg_id"`                    // 服务器分配的消息 ID,0 表示被拒绝
	CleanedContent string `json:"cleaned_content,omitempty"` // 服务端清洗后的内容,非空时前端覆盖本地显示
	ErrorCode      string `json:"error_code,omitempty"`      // 拒绝原因码:blocked / not_friend / not_in_group / group_banned
}

AckMsg 服务器对客户端消息的 ACK MsgID 为 0 表示消息被服务器拒绝(如触发敏感词检测),客户端可据此判断发送结果。 CleanedContent 非空时表示内容经过服务端处理(如敏感词替换),前端应用此值覆盖本地显示。

type Handler

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

Handler 持有聊天服务所需的依赖

func NewHandler

func NewHandler(
	h *hub.Hub,
	sf *utils.Snowflake,
	cache *redis.Client,
	taskFn func(*tasks.SaveMessagePayload) error,
	helperTaskFn func(*tasks.HelperQuestionPayload) error,
) *Handler

func (*Handler) ServeSession

func (h *Handler) ServeSession(ctx context.Context, userID int64, wts *webtransport.Session)

ServeSession 接管一个已建立的 WebTransport Session,直到连接断开 userID 由调用方(server.go)在握手阶段解析 JWT 后传入

type HelperStreamChunk

type HelperStreamChunk struct {
	// 回答标识
	MsgID     int64 `json:"msg_id"`     // 预分配的回答消息 ID(前端分桶/锚点)
	SessionID int64 `json:"session_id"` // helper_sessions.id

	// 分片信息
	ChunkIndex   int    `json:"chunk_index"`             // 从 0 递增的分片序号
	StepType     string `json:"step_type"`               // intent/tool_call/tool_result/observe/answer
	Content      string `json:"content"`                 // 本帧内容增量
	ToolName     string `json:"tool_name,omitempty"`     // step_type 为 tool_call/tool_result 时携带
	IsFinal      bool   `json:"is_final"`                // true=本次回答结束
	IsCorrection bool   `json:"is_correction,omitempty"` // true=安全修正帧,前端应覆盖渲染

	// 归因字段(每帧携带,前端可就近展示"由 XXX 发起")
	QuestionMsgID   int64  `json:"question_msg_id"`   // 触发此回答的原始提问消息 ID
	InitiatorUserID int64  `json:"initiator_user_id"` // 发起人 user_id
	InitiatorName   string `json:"initiator_name"`    // 发起人昵称(consumer 启动时查询)
}

HelperStreamChunk AI 助手流式推送帧(ContentType=MsgTypeHelperStream=10) 通过 OutboundMsg.Content JSON 下发,仅用于流式渲染,不持久化到 messages 表。 前端按 MsgID 分桶:同一 MsgID 的 chunk 追加渲染到同一气泡;is_final=true 时气泡定型。

type InboundMsg

type InboundMsg struct {
	ClientSeq   int64  `json:"client_seq"`   // 客户端自增序号,用于客户端去重/ACK
	ChatType    int8   `json:"chat_type"`    // 1=单聊 2=群聊
	TargetID    int64  `json:"target_id"`    // 单聊=对方 userID,群聊=groupID
	ContentType int8   `json:"content_type"` // MsgType* 常量
	Content     string `json:"content,omitempty"`
	MediaURL    string `json:"media_url,omitempty"`
	MediaName   string `json:"media_name,omitempty"`
	MediaSize   int64  `json:"media_size,omitempty"`
	Duration    int32  `json:"duration,omitempty"`
}

InboundMsg 客户端上行消息(JSON) 所有 int64 字段由前端 serializeWithBigInt 统一序列化为 JSON 裸数字, 后端使用标准 int64 反序列化即可,无需兼容字符串格式。

func (*InboundMsg) Validate

func (m *InboundMsg) Validate() error

Validate 校验上行消息字段合法性

type MeetingContent

type MeetingContent struct {
	RoomName   string `json:"room_name"`
	Action     string `json:"action"` // invite=可加入 / schedule=仅预约未到点 / end=已结束
	Title      string `json:"title,omitempty"`
	StartAt    int64  `json:"start_at,omitempty"` // Unix 毫秒,schedule 时携带
	MeetingID  int64  `json:"meeting_id,omitempty"`
	LiveKitURL string `json:"livekit_url"`
}

MeetingContent MsgTypeMeeting 消息的业务内容

type OutboundMsg

type OutboundMsg struct {
	ID              int64  `json:"id"` // 雪花消息 ID
	FromUserID      int64  `json:"from_user_id"`
	ChatType        int8   `json:"chat_type"`
	TargetID        int64  `json:"target_id"`
	ContentType     int8   `json:"content_type"`
	Content         string `json:"content,omitempty"`
	MediaURL        string `json:"media_url,omitempty"`
	MediaName       string `json:"media_name,omitempty"`
	MediaSize       int64  `json:"media_size,omitempty"`
	Duration        int32  `json:"duration,omitempty"`
	ConversationKey string `json:"conversation_key,omitempty"`
	CreatedAt       int64  `json:"created_at"` // Unix 毫秒
}

OutboundMsg 服务器下行消息(JSON)

type RecallNotify

type RecallNotify struct {
	Type         string `json:"type"` // 固定 "recall"
	MsgID        int64  `json:"msg_id"`
	PeerID       int64  `json:"peer_id"`
	PeerType     int8   `json:"peer_type"`
	SenderUserID int64  `json:"sender_user_id"` // 撤回方 userID,前端展示 "XXX撤回了一条消息"
}

RecallNotify 服务器下行撤回通知(由 message 服务通过 Pub/Sub 推送,hub 转发给对应 session)

type Server

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

Server 包装 webtransport.Server,提供启动与关闭能力

func NewServer

func NewServer(addr, tlsCert, tlsKey string, allowedOrigins []string, handler *Handler) (*Server, error)

NewServer 创建 WebTransport 服务器 allowedOrigins: WebTransport 请求允许的 Origin 白名单;为空则允许所有来源(开发模式)

func (*Server) Close

func (s *Server) Close() error

Close 优雅关闭 WebTransport 服务器

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe 启动 QUIC 监听(阻塞)

func (*Server) ServeBackground

func (s *Server) ServeBackground(ctx context.Context, errCh chan<- error)

ServeBackground 在后台 goroutine 运行,将错误写入 errCh

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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