forked from longtao.wu/droboticflow
96 lines
3.3 KiB
Go
96 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func (s *server) handleAnnotationWorkBuffer(w http.ResponseWriter, r *http.Request) {
|
|
p, _ := currentPrincipal(r)
|
|
if !randomClaimAnnotator(p) {
|
|
renderJSON(w, 403, map[string]string{"error": "仅标注员使用个人任务池"})
|
|
return
|
|
}
|
|
projectID := strings.TrimSpace(r.URL.Query().Get("projectId"))
|
|
project, err := s.store.GetProject(r.Context(), projectID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
if project == nil || project.OrganizationID != p.User.OrganizationID || project.DeletionTaskID != "" || strings.EqualFold(string(project.Status), "archived") {
|
|
renderJSON(w, 403, map[string]string{"error": "项目不可用"})
|
|
return
|
|
}
|
|
members, err := s.annotationMembershipsForUser(r.Context(), p.User.OrganizationID, projectID, p.User.ID)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
allowed := false
|
|
for _, m := range members {
|
|
if m.Role == domain.AnnotationRoleAnnotator && m.SupplierID != "" {
|
|
supplier, e := s.store.GetSupplier(r.Context(), m.SupplierID)
|
|
if e != nil {
|
|
renderError(w, 500, e)
|
|
return
|
|
}
|
|
if supplier != nil && supplier.OrganizationID == p.User.OrganizationID && strings.EqualFold(supplier.Status, "active") {
|
|
allowed = true
|
|
}
|
|
}
|
|
}
|
|
if !allowed {
|
|
renderJSON(w, 403, map[string]string{"error": "没有该项目的有效标注权限"})
|
|
return
|
|
}
|
|
// Scope comes from runner-created batches, including empty active batches;
|
|
// merely using annotation-qa or a template version never enables buffering.
|
|
batches, err := s.store.ListHumanBatches(r.Context(), p.User.OrganizationID, domain.HumanBatchQuery{ProjectID: projectID, PluginID: "annotation-qa"})
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
enabled := false
|
|
for _, batch := range batches {
|
|
if batch.OrganizationID == p.User.OrganizationID && batch.ProjectID == projectID && batch.ProcessingProfile == domain.ProcessingProfileRawHuman && domain.IsFanyuV58ThreeRoleHumanLoop(batch.Stages) && batch.Status != domain.HumanBatchCompleted {
|
|
enabled = true
|
|
break
|
|
}
|
|
}
|
|
if !enabled {
|
|
if r.Method == http.MethodPut {
|
|
renderJSON(w, 409, map[string]string{"code": "annotation_buffer_not_supported", "error": "该项目流程不使用个人任务缓冲"})
|
|
return
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": false, "capacity": 2, "occupied": 0, "items": []domain.HumanItemRecord{}, "processing": map[string]any{}})
|
|
return
|
|
}
|
|
capacity := 0
|
|
if r.Method == http.MethodPut {
|
|
var body struct {
|
|
Capacity int `json:"capacity"`
|
|
}
|
|
if !decodeJSON(w, r, &body) {
|
|
return
|
|
}
|
|
capacity = body.Capacity
|
|
if capacity < 2 || capacity > 8 {
|
|
renderValidationError(w, validationIssue{Field: "capacity", Message: "容量必须是2至8的整数"})
|
|
return
|
|
}
|
|
}
|
|
buffer, err := s.store.AnnotationWorkBuffer(r.Context(), p.User.OrganizationID, p.User.ID, projectID, capacity)
|
|
if err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
processing := map[string]any{}
|
|
for _, item := range buffer.Items {
|
|
if job := item.ProcessingJob; job != nil {
|
|
processing[item.ID] = map[string]string{"status": job.Status, "kind": job.Kind, "error": job.Error}
|
|
}
|
|
}
|
|
renderJSON(w, 200, map[string]any{"enabled": true, "capacity": buffer.Capacity, "occupied": buffer.Occupied, "items": buffer.Items, "processing": processing})
|
|
}
|