Files
Agent-System/internal/server/workflow_terminate_handlers.go
T
weichen 77b8d07cd7 fix(governance): 统一文档访问判定与上下文快照排除计数
补齐 DocumentAccessDecider HTTP 409、projectcontext 私有对话排除统计,并更新整改 gap matrix 与测试报告。
2026-09-13 02:50:23 +00:00

81 lines
2.7 KiB
Go

package server
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"chat-agent-system/internal/projectinstance"
)
func (s *Server) handleTerminationPreview(w http.ResponseWriter, r *http.Request) {
if s.instSvc == nil || !s.instSvc.Available() {
http.Error(w, `{"error":"ERR_NOT_AVAILABLE"}`, http.StatusServiceUnavailable)
return
}
instanceID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || instanceID <= 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM"}`, http.StatusBadRequest)
return
}
user, err := s.authenticateUser(r)
if err != nil || user == nil {
http.Error(w, `{"error":"ERR_UNAUTHENTICATED"}`, http.StatusUnauthorized)
return
}
detail, err := s.instSvc.GetInstanceDetail(r.Context(), instanceID)
if err != nil {
http.Error(w, `{"error":"ERR_NOT_FOUND"}`, http.StatusNotFound)
return
}
isCreator, _ := s.pwSvc.IsProjectCreator(r.Context(), detail.Instance.ProjectID, user.ID)
preview, err := s.instSvc.GetTerminationPreview(r.Context(), instanceID, detail.Instance.ProjectID, user.ID, isCreator)
if err != nil {
writeProjectInstanceError(w, err)
return
}
writeJSONResponse(w, http.StatusOK, preview)
}
func (s *Server) handleTerminateWorkflowInstance(w http.ResponseWriter, r *http.Request) {
if s.instSvc == nil || !s.instSvc.Available() {
http.Error(w, `{"error":"ERR_NOT_AVAILABLE"}`, http.StatusServiceUnavailable)
return
}
instanceID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || instanceID <= 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM"}`, http.StatusBadRequest)
return
}
user, err := s.authenticateUser(r)
if err != nil || user == nil {
http.Error(w, `{"error":"ERR_UNAUTHENTICATED"}`, http.StatusUnauthorized)
return
}
detail, err := s.instSvc.GetInstanceDetail(r.Context(), instanceID)
if err != nil {
http.Error(w, `{"error":"ERR_NOT_FOUND"}`, http.StatusNotFound)
return
}
var in projectinstance.TerminateInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
http.Error(w, `{"error":"ERR_INVALID_PARAM"}`, http.StatusBadRequest)
return
}
isCreator, _ := s.pwSvc.IsProjectCreator(r.Context(), detail.Instance.ProjectID, user.ID)
res, err := s.instSvc.TerminateInstance(r.Context(), instanceID, detail.Instance.ProjectID, user.ID, isCreator, in)
if err != nil {
switch {
case errors.Is(err, projectinstance.ErrForbiddenNotCreator):
http.Error(w, `{"error":"ERR_FORBIDDEN"}`, http.StatusForbidden)
case errors.Is(err, projectinstance.ErrTerminationNotAllowed):
http.Error(w, `{"error":"ERR_TERMINATION_NOT_ALLOWED"}`, http.StatusConflict)
default:
http.Error(w, `{"error":"ERR_INTERNAL","message":"`+err.Error()+`"}`, http.StatusInternalServerError)
}
return
}
writeJSONResponse(w, http.StatusOK, res)
}