Files
Agent-System/internal/server/workflow_instance_handlers.go
T
weichenandCursor e0c39aa38c feat(service): add stage gate readiness and creator-confirmed advance.
Evaluate required document approvals before stage transition, expose readiness/advance APIs, show StageAdvanceBanner in the UI, and keep legacy workflow switch for explicit initial_workflow_key projects.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 02:50:23 +00:00

97 lines
3.2 KiB
Go

package server
import (
"encoding/json"
"net/http"
"strconv"
"chat-agent-system/internal/projectinstance"
)
func (s *Server) handleGetWorkflowInstance(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || instanceID <= 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM","message":"invalid instance id"}`, http.StatusBadRequest)
return
}
if s.instSvc == nil || !s.instSvc.Available() {
http.Error(w, `{"error":"ERR_NOT_AVAILABLE","message":"项目实例服务不可用"}`, http.StatusServiceUnavailable)
return
}
detail, err := s.instSvc.GetInstanceDetail(r.Context(), instanceID)
if err != nil {
writeProjectInstanceError(w, err)
return
}
if _, ok := s.requireProjectAccess(w, r, detail.Instance.ProjectID); !ok {
return
}
writeJSONResponse(w, http.StatusOK, detail)
}
func (s *Server) handleGetStageReadiness(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || instanceID <= 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM","message":"invalid instance id"}`, http.StatusBadRequest)
return
}
stageKey := r.PathValue("stage")
if s.instSvc == nil || !s.instSvc.Available() {
http.Error(w, `{"error":"ERR_NOT_AVAILABLE","message":"项目实例服务不可用"}`, http.StatusServiceUnavailable)
return
}
detail, err := s.instSvc.GetInstanceDetail(r.Context(), instanceID)
if err != nil {
writeProjectInstanceError(w, err)
return
}
user, ok := s.requireProjectAccess(w, r, detail.Instance.ProjectID)
if !ok {
return
}
isCreator, _ := s.pwSvc.IsProjectCreator(r.Context(), detail.Instance.ProjectID, user.ID)
readiness, err := s.instSvc.GetStageReadiness(r.Context(), instanceID, stageKey, user.ID, isCreator)
if err != nil {
writeProjectInstanceError(w, err)
return
}
writeJSONResponse(w, http.StatusOK, readiness)
}
func (s *Server) handleAdvanceStage(w http.ResponseWriter, r *http.Request) {
instanceID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil || instanceID <= 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM","message":"invalid instance id"}`, http.StatusBadRequest)
return
}
stageKey := r.PathValue("stage")
if s.instSvc == nil || !s.instSvc.Available() {
http.Error(w, `{"error":"ERR_NOT_AVAILABLE","message":"项目实例服务不可用"}`, http.StatusServiceUnavailable)
return
}
detail, err := s.instSvc.GetInstanceDetail(r.Context(), instanceID)
if err != nil {
writeProjectInstanceError(w, err)
return
}
user, ok := s.requireProjectAccess(w, r, detail.Instance.ProjectID)
if !ok {
return
}
isCreator, _ := s.pwSvc.IsProjectCreator(r.Context(), detail.Instance.ProjectID, user.ID)
var req projectinstance.AdvanceStageInput
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && r.ContentLength > 0 {
http.Error(w, `{"error":"ERR_INVALID_PARAM","message":"无效的 JSON 请求体"}`, http.StatusBadRequest)
return
}
if req.StageKey == "" {
req.StageKey = stageKey
}
result, err := s.instSvc.AdvanceStage(r.Context(), instanceID, user.ID, isCreator, req)
if err != nil {
writeProjectInstanceError(w, err)
return
}
writeJSONResponse(w, http.StatusOK, result)
}