ai_scheduler/internal/biz/router.go

73 lines
1.4 KiB
Go

package biz
import (
"ai_scheduler/internal/biz/do"
"ai_scheduler/internal/gateway"
"ai_scheduler/internal/entitys"
"github.com/gofiber/fiber/v2/log"
)
// AiRouterBiz 智能路由服务
type AiRouterBiz struct {
do *do.Do
handle *do.Handle
}
// NewAiRouterBiz 创建路由服务
func NewAiRouterBiz(
do *do.Do,
handle *do.Handle,
) *AiRouterBiz {
return &AiRouterBiz{
do: do,
handle: handle,
}
}
// 路由处理WebSocket请求
//
// 参数:
// - client: 网关客户端
// - req: 聊天请求结构体
//
// 返回:
// - err: 错误信息
func (r *AiRouterBiz) RouteWithSocket(client *gateway.Client, req *entitys.ChatSockRequest) (err error) {
// 创建请求上下文数据
requireData := &entitys.RequireData{
Req: req,
}
// 获取WebSocket连接
conn := client.GetConn()
//初始化通道/上下文
ctx, clearFunc := r.do.MakeCh(conn, requireData)
defer func() {
if err != nil {
entitys.ResError(requireData.Ch, "", err.Error())
}
clearFunc()
}()
//数据验证和收集
if err = r.do.DataAuth(ctx, client, requireData); err != nil {
log.Errorf("数据验证和收集失败: %s", err.Error())
return
}
//意图识别
if err = r.handle.Recognize(ctx, requireData); err != nil {
log.Errorf("意图识别失败: %s", err.Error())
return
}
//向下传递
if err = r.handle.HandleMatch(ctx, client, requireData); err != nil {
log.Errorf("任务处理失败: %s", err.Error())
return
}
return
}