ai_scheduler/internal/services/chat.go

74 lines
1.7 KiB
Go

package services
import (
errors "ai_scheduler/internal/data/error"
"ai_scheduler/internal/entitys"
"net/http"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
)
// ChatHandler 聊天处理器
type ChatService struct {
routerService entitys.RouterService
}
// NewChatHandler 创建聊天处理器
func NewChatService(routerService entitys.RouterService) *ChatService {
return &ChatService{
routerService: routerService,
}
}
// ToolCallResponse 工具调用响应
type ToolCallResponse struct {
ID string `json:"id" example:"call_1"`
Type string `json:"type" example:"function"`
Function FunctionCallResponse `json:"function"`
Result interface{} `json:"result,omitempty"`
}
// FunctionCallResponse 函数调用响应
type FunctionCallResponse struct {
Name string `json:"name" example:"get_weather"`
Arguments interface{} `json:"arguments"`
}
func (h *ChatService) Chat(c *fiber.Ctx) error {
var req entitys.ChatRequest
if err := c.BodyParser(&req); err != nil {
return errors.ParamError
}
// 转换为服务层请求
serviceReq := &entitys.ChatRequest{
UserInput: req.UserInput,
Caller: req.Caller,
SessionID: req.SessionID,
ChatRequestMeta: entitys.ChatRequestMeta{
Authorization: c.Request().Header(),
},
}
// 调用路由服务
response, err := h.routerService.Route(c.Request.Context(), serviceReq)
if err != nil {
c.JSON(http.StatusInternalServerError, ChatResponse{
Status: "error",
Message: err.Error(),
})
return
}
// 转换响应格式
httpResponse := &ChatResponse{
Message: response.Message,
Status: response.Status,
Data: response.Data,
TaskCode: response.TaskCode,
}
c.JSON(http.StatusOK, httpResponse)
}