droboticflow/internal/api/annotation_contiguous.go

161 lines
5.5 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"fmt"
"sort"
"strings"
)
// The human batch is created by the workflow runner, never supplied by an
// annotation request. Template version alone must not change another workflow.
func (s *server) continuousActionsForItem(ctx context.Context, item *domain.HumanItemRecord) bool {
if item == nil || item.PluginID != "annotation-qa" {
return false
}
batch, err := s.store.GetHumanBatch(ctx, item.BatchID)
return err == nil && batch != nil && batch.OrganizationID == item.OrganizationID && batch.ProjectID == item.ProjectID && batch.WorkflowID == item.WorkflowID && batch.ProcessingProfile == domain.ProcessingProfileRawHuman && domain.IsFanyuV58ThreeRoleHumanLoop(batch.Stages)
}
func effectiveActionStatus(segment domain.AnnotationSegment) string {
if segment.Status != "" {
return segment.Status
}
if segment.IsInvalid {
return "invalid"
}
if strings.TrimSpace(segment.Description) != "" {
return "described"
}
return "pending"
}
func validateContinuousActions(slices []domain.SliceSegment, segments []domain.AnnotationSegment, complete bool) []validationIssue {
issues := validateDescriptionIntervals(slices, segments, true)
if len(slices) == 0 {
issues = append(issues, validationIssue{Field: "sliceSegments", Code: "annotation_coverage_gap", Message: "必须保留完整切片范围"})
}
for i, segment := range segments {
status := effectiveActionStatus(segment)
message := ""
switch status {
case "pending":
if complete && strings.TrimSpace(segment.Description) != "" {
message = "当前动作有未确认文字,请先保存描述或取消编辑"
}
if segment.IsInvalid {
message = "待标注动作不能同时标为无效"
}
case "described":
if strings.TrimSpace(segment.Description) == "" || segment.IsInvalid {
message = "已描述动作必须有描述且不能同时标为无效"
}
case "invalid":
if !segment.IsInvalid {
message = "无效动作状态必须与无效判断一致"
}
default:
message = "未知动作状态"
}
if message != "" {
issues = append(issues, validationIssue{Field: fmt.Sprintf("segments.%d.status", i), Code: "action_annotation_incomplete", Message: message})
}
}
return issues
}
// Legacy editable data is represented as pending gaps only when the operator
// explicitly saves progress or submits. Reads and old revisions are never rewritten.
func fillPendingActionGaps(slices []domain.SliceSegment, segments []domain.AnnotationSegment) []domain.AnnotationSegment {
result := append([]domain.AnnotationSegment{}, segments...)
seen := map[string]bool{}
for i := range result {
seen[result[i].ID] = true
result[i].Status = effectiveActionStatus(result[i])
if result[i].Status == "pending" && strings.TrimSpace(result[i].Description) == "" {
result[i].Description = ""
}
}
for _, slice := range slices {
if slice.IsDiscarded() {
continue
}
events := []domain.AnnotationSegment{}
for _, e := range segments {
if e.Start >= slice.Start && e.End <= slice.End {
events = append(events, e)
}
}
sort.Slice(events, func(i, j int) bool { return events[i].Start < events[j].Start })
cursor := slice.Start
add := func(end float64) {
if end-cursor <= 1e-6 {
return
}
id := fmt.Sprintf("pending-%s-%.9f", slice.ID, cursor)
for seen[id] {
id += "-gap"
}
seen[id] = true
result = append(result, domain.AnnotationSegment{ID: id, Start: cursor, End: end, Status: "pending"})
}
for _, e := range events {
add(e.Start)
cursor = e.End
}
add(slice.End)
}
sort.SliceStable(result, func(i, j int) bool { return result[i].Start < result[j].Start })
return result
}
// A client cannot use the metadata-reuse path to smuggle sub-frame intervals.
func (s *server) continuousActionFrameIssues(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord, segments []domain.AnnotationSegment) []validationIssue {
if item == nil || stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["boundaryContract"]) != "hardware-frame-v1" {
return nil
}
timeline, err := s.annotationFrameTimelineForTask(ctx, task, "left")
if err != nil {
return []validationIssue{{Field: "segments", Code: "annotation_frame_unavailable", Message: "无法核对动作帧边界,请稍后重试"}}
}
seconds, ok := timeline["boundariesSeconds"].([]float64)
if !ok || len(seconds) < 2 {
return []validationIssue{{Field: "segments", Code: "annotation_frame_unavailable", Message: "统一帧边界不可用"}}
}
grid := annotationBoundaryGrid{Seconds: seconds}
for i, e := range segments {
a, ae := grid.resolve(e.Start)
b, be := grid.resolve(e.End)
if ae != nil || be != nil || b <= a {
return []validationIssue{{Field: fmt.Sprintf("segments.%d", i), Code: "annotation_frame_invalid", Message: "动作区间必须对齐真实帧且至少包含一帧"}}
}
}
return nil
}
func continuousSliceStatuses(slices []domain.SliceSegment, segments []domain.AnnotationSegment) map[string]domain.SliceAnnotationStatus {
statuses := map[string]domain.SliceAnnotationStatus{}
for i := range slices {
if slices[i].IsDiscarded() {
continue
}
status := domain.SliceAnnotationLabeled
found := false
for _, e := range segments {
if e.Start >= slices[i].Start && e.End <= slices[i].End {
found = true
if effectiveActionStatus(e) == "pending" {
status = domain.SliceAnnotationPending
}
}
}
if !found {
status = domain.SliceAnnotationPending
}
slices[i].AnnotationStatus = status
statuses[slices[i].ID] = status
}
return statuses
}