added logic for send req to ai, neew work with client process and client response

This commit is contained in:
skeris 2025-04-08 17:33:15 +03:00
parent f8adf9b338
commit be718cfde3
2 changed files with 66 additions and 0 deletions

36
clients/aiClient.go Normal file

@ -0,0 +1,36 @@
package clients
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
type AIClient struct {
baseURL string
httpClient *fiber.Client
}
type SendAnswerRequest struct {
Tipe string `json:"type"`
Message string `json:"message"`
Final bool `json:"final"`
Session string `json:"session"`
}
func NewAiClient(baseURL string) *AIClient {
return &AIClient{
baseURL: baseURL,
httpClient: fiber.AcquireClient(),
}
}
func (client *AIClient) SendAnswerer(final bool, tipe, message, session string) error {
req := SendAnswerRequest{
Tipe: tipe,
Message: message,
Final: final,
Session: session,
}
fmt.Println(req)
return nil
}

@ -33,6 +33,7 @@ type Service struct {
workerSendClientCh chan<- model.Answer
encrypt *utils.Encrypt
redirectURl string
aiClient *clients.AIClient
}
type ServiceDeps struct {
@ -42,6 +43,7 @@ type ServiceDeps struct {
WorkerSendClientCh chan<- model.Answer
Encrypt *utils.Encrypt
RedirectURl string
AiClient *clients.AIClient
}
func New(deps ServiceDeps) *Service {
@ -54,6 +56,7 @@ func New(deps ServiceDeps) *Service {
workerSendClientCh: deps.WorkerSendClientCh,
encrypt: deps.Encrypt,
redirectURl: deps.RedirectURl,
aiClient: deps.AiClient,
}
}
@ -362,6 +365,33 @@ func (s *Service) PutAnswersOnePiece(c *fiber.Ctx) error {
for _, ans := range answersRaw {
if quiz.Status == model.StatusAI {
final := false
if finalStr, exists := form.Value["final"]; exists && len(finalStr) > 0 {
parsedFinal, err := strconv.ParseBool(finalStr[0])
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString("invalid final value")
}
final = parsedFinal
}
question, err := s.dal.QuestionRepo.GetQuestionListByIDs(c.Context(), []int32{int32(ans.QuestionId)})
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("can not get questions")
}
if len(question) == 0 {
return c.Status(fiber.StatusNotFound).SendString("no questions found")
}
err = s.aiClient.SendAnswerer(final, question[0].Type, ans.Content, cs)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString(fmt.Sprintf("can not send answer to ai, err: %s", err.Error()))
}
// todo тут надо создать вопрос по ответу от ai
}
ans.DeviceType = deviceType
ans.OS = os
ans.Browser = browser