feat: initialize external DobotAI agent gateway
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
type QAService interface {
|
||||
Ask(context.Context, core.QARequest) (core.QAResult, error)
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
service QAService
|
||||
token string
|
||||
mux *http.ServeMux
|
||||
}
|
||||
|
||||
func New(service QAService, ingressToken string) (*Server, error) {
|
||||
if service == nil {
|
||||
return nil, errors.New("QA service is required")
|
||||
}
|
||||
if strings.TrimSpace(ingressToken) == "" {
|
||||
return nil, errors.New("DOBOTAI_INGRESS_TOKEN is required")
|
||||
}
|
||||
s := &Server{service: service, token: ingressToken, mux: http.NewServeMux()}
|
||||
s.mux.HandleFunc("GET /healthz", s.health)
|
||||
s.mux.HandleFunc("POST /v1/qa", s.authorize(s.ask))
|
||||
return s, nil
|
||||
}
|
||||
func (s *Server) Handler() http.Handler { return s.mux }
|
||||
|
||||
func (s *Server) authorize(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
provided := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
if len(provided) != len(s.token) || subtle.ConstantTimeCompare([]byte(provided), []byte(s.token)) != 1 {
|
||||
writeError(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
func (s *Server) health(w http.ResponseWriter, _ *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
}
|
||||
func (s *Server) ask(w http.ResponseWriter, r *http.Request) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||
defer r.Body.Close()
|
||||
var request struct {
|
||||
TenantKey string `json:"tenant_key"`
|
||||
Channel string `json:"channel"`
|
||||
ConversationType core.ConversationType `json:"conversation_type"`
|
||||
ExternalChatID string `json:"external_chat_id"`
|
||||
ExternalUserID string `json:"external_user_id"`
|
||||
ExternalMessageID string `json:"external_message_id"`
|
||||
SenderName string `json:"sender_name"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
writeError(w, http.StatusBadRequest, "request body is required")
|
||||
} else {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON request")
|
||||
}
|
||||
return
|
||||
}
|
||||
result, err := s.service.Ask(r.Context(), core.QARequest{TenantKey: request.TenantKey, Channel: request.Channel, ConversationType: request.ConversationType, ExternalChatID: request.ExternalChatID, ExternalUserID: request.ExternalUserID, ExternalMessageID: request.ExternalMessageID, SenderName: request.SenderName, Query: request.Query})
|
||||
if err != nil {
|
||||
if errors.Is(err, core.ErrTurnInProgress) {
|
||||
writeError(w, http.StatusConflict, "message is already processing")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusBadGateway, "question processing failed")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, result)
|
||||
}
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
writeJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
type fakeQA struct{}
|
||||
|
||||
func (fakeQA) Ask(context.Context, core.QARequest) (core.QAResult, error) {
|
||||
return core.QAResult{TurnID: "turn-1", Answer: "answer"}, nil
|
||||
}
|
||||
|
||||
func TestQAEndpointRequiresTokenAndReturnsResult(t *testing.T) {
|
||||
server, err := New(fakeQA{}, "ingress-secret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := `{"tenant_key":"tenant","channel":"dingtalk","conversation_type":"direct","external_user_id":"user","external_message_id":"message","query":"hello"}`
|
||||
unauthorized := httptest.NewRequest(http.MethodPost, "/v1/qa", strings.NewReader(body))
|
||||
unauthorizedResult := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(unauthorizedResult, unauthorized)
|
||||
if unauthorizedResult.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unauthorized status=%d", unauthorizedResult.Code)
|
||||
}
|
||||
authorized := httptest.NewRequest(http.MethodPost, "/v1/qa", strings.NewReader(body))
|
||||
authorized.Header.Set("Authorization", "Bearer ingress-secret")
|
||||
result := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(result, authorized)
|
||||
if result.Code != http.StatusOK || !strings.Contains(result.Body.String(), `"turn_id":"turn-1"`) {
|
||||
t.Fatalf("status=%d body=%s", result.Code, result.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user