CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
Archive-Schnittstellen-Vertragstests / contract-tests (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
AI-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
Archive-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (pull_request) Canceled after 0s
Archive-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Connect-Schnittstellen-Vertragstests / contract-tests (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
Workflow-Schnittstellen-Vertragstests / contract-tests (push) Canceled after 0s
Connect-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Canceled after 0s
Mail-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Mail-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
Workflow-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Canceled after 0s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Canceled after 0s
CI / Web (dms-browser) (pull_request) Canceled after 0s
CI / Web (pdf-viewer) (pull_request) Canceled after 0s
CI / Web (shl) (pull_request) Canceled after 0s
Mail-Schnittstellen-Vertragstests / contract-tests (push) Canceled after 0s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Canceled after 0s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Canceled after 0s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Canceled after 0s
Workflow-Schnittstellen-Vertragstests / contract-tests (pull_request) Canceled after 0s
Nur Pakete, deren Code ohne Kommentare/Importpfade byte-gleich ist: - httpclient: ai, connect (Root behaelt eigene Kopie wegen strengerem Waechtertest) - svcauth: archive, ai, workflow - auditclient: ai, connect, archive - tenantregistryclient: dms, ai, workflow (archive weicht ab, bleibt) - policyclient: mail, connect (dms weicht ab, bleibt) - httpharden: Root, archive (dms weicht ab, bleibt) - loadtest: mail, ai (archive, connect weichen ab, bleiben) go.mod (require + replace) fuer ai, connect, workflow; Pflichttest-Gates von ai/workflow und CI-Pfadfilter um common/ erweitert, damit die sicherheitskritischen Pakete (svcauth, tenantregistryclient) weiter gegated werden. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
123 lines
3.7 KiB
Go
123 lines
3.7 KiB
Go
// Package docchatapi ist die HTTP-Bindung des Dokumenten-Chats (SEM-04/SEM-05),
|
|
// damit eine Oberflaeche (SEM-06) ihn abrufen kann. Aufrufer ist das anfragende
|
|
// Modul (DMS/Mail) bzw. dessen eigene Backend-for-Frontend-Route, nicht der
|
|
// Endnutzer direkt — Service-Token-Pflicht wie semanticsearchapi/summaryapi,
|
|
// fail closed.
|
|
package docchatapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/docchat"
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/semanticsearch"
|
|
"gitea.perlbach24.de/scripte/nexarch/ai/internal/vectorindex"
|
|
"gitea.perlbach24.de/scripte/nexarch/common/svcauth"
|
|
)
|
|
|
|
type Server struct {
|
|
svc *docchat.Service
|
|
serviceToken string
|
|
}
|
|
|
|
func NewServer(svc *docchat.Service, serviceToken string) *Server {
|
|
return &Server{svc: svc, serviceToken: serviceToken}
|
|
}
|
|
|
|
func (s *Server) RegisterRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("POST /chat", s.requireAuth(s.handleAsk))
|
|
mux.HandleFunc("POST /chat/citation", s.requireAuth(s.handleResolveCitation))
|
|
}
|
|
|
|
func (s *Server) requireAuth(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
got := r.Header.Get("X-Service-Token")
|
|
if !svcauth.Equal(got, s.serviceToken) {
|
|
writeError(w, http.StatusUnauthorized, "service-token fehlt oder ist ungueltig")
|
|
return
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
|
|
type refRequest struct {
|
|
SourceType string `json:"source_type"`
|
|
SourceID string `json:"source_id"`
|
|
}
|
|
|
|
type askRequest struct {
|
|
TenantSlug string `json:"tenant_slug"`
|
|
Question string `json:"question"`
|
|
TopK int `json:"top_k"`
|
|
AllowedRefs []refRequest `json:"allowed_refs"`
|
|
}
|
|
|
|
func (s *Server) handleAsk(w http.ResponseWriter, r *http.Request) {
|
|
var req askRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "ungueltiger anfrage-koerper")
|
|
return
|
|
}
|
|
|
|
refs := make([]semanticsearch.Ref, 0, len(req.AllowedRefs))
|
|
for _, ref := range req.AllowedRefs {
|
|
refs = append(refs, semanticsearch.Ref{SourceType: ref.SourceType, SourceID: ref.SourceID})
|
|
}
|
|
|
|
answer, err := s.svc.Ask(r.Context(), req.TenantSlug, req.Question, req.TopK, refs)
|
|
switch {
|
|
case err == nil:
|
|
writeJSON(w, http.StatusOK, answer)
|
|
case errors.Is(err, semanticsearch.ErrNoAllowedRefs):
|
|
writeError(w, http.StatusBadRequest, "keine freigegebenen referenzen")
|
|
default:
|
|
writeError(w, http.StatusInternalServerError, "chat-antwort fehlgeschlagen")
|
|
}
|
|
}
|
|
|
|
type citationRequest struct {
|
|
TenantSlug string `json:"tenant_slug"`
|
|
Index int `json:"index"`
|
|
SourceType string `json:"source_type"`
|
|
SourceID string `json:"source_id"`
|
|
ChunkIndex int `json:"chunk_index"`
|
|
}
|
|
|
|
type citationResponse struct {
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
func (s *Server) handleResolveCitation(w http.ResponseWriter, r *http.Request) {
|
|
var req citationRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "ungueltiger anfrage-koerper")
|
|
return
|
|
}
|
|
|
|
text, err := s.svc.ResolveCitation(r.Context(), req.TenantSlug, docchat.Citation{
|
|
Index: req.Index,
|
|
SourceType: req.SourceType,
|
|
SourceID: req.SourceID,
|
|
ChunkIndex: req.ChunkIndex,
|
|
})
|
|
switch {
|
|
case err == nil:
|
|
writeJSON(w, http.StatusOK, citationResponse{Text: text})
|
|
case errors.Is(err, vectorindex.ErrChunkNotFound):
|
|
writeError(w, http.StatusNotFound, "abschnitt nicht gefunden")
|
|
default:
|
|
writeError(w, http.StatusInternalServerError, "zitat konnte nicht aufgeloest werden")
|
|
}
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, message string) {
|
|
writeJSON(w, status, map[string]string{"error": message})
|
|
}
|