forked from longtao.wu/droboticflow
157 lines
5.9 KiB
Go
157 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
type annotationEditorState struct {
|
|
Phase string `json:"phase"`
|
|
CanConfirmDescriptions bool `json:"canConfirmDescriptions"`
|
|
CanRecoverGeneration bool `json:"canRecoverGeneration"`
|
|
CanReconfirmSavedGeneration bool `json:"canReconfirmSavedGeneration"`
|
|
}
|
|
|
|
// Use the same persisted evidence as the write gate, including legacy tasks
|
|
// without sliceWorkflow. Restored draft actions do not prove generation ready.
|
|
func (server *server) annotationEditorStateForTask(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord) (annotationEditorState, error) {
|
|
ready, err := server.legacyAnnotationConfirmationReady(ctx, task, item)
|
|
state := annotationEditorState{Phase: "slice", CanConfirmDescriptions: ready}
|
|
if err != nil {
|
|
return state, err
|
|
}
|
|
if ready {
|
|
state.Phase = "annotate"
|
|
return state, nil
|
|
}
|
|
if item != nil {
|
|
state.CanRecoverGeneration = legacyGenerationRecoveryCandidate(task, item)
|
|
workflow := mapValue(item.Subject.Payload["sliceWorkflow"])
|
|
status := stringValue(workflow["status"])
|
|
jobStatus := ""
|
|
if item.ProcessingJob != nil {
|
|
jobStatus = item.ProcessingJob.Status
|
|
}
|
|
if status == "failed" || jobStatus == "failed" {
|
|
state.CanReconfirmSavedGeneration = failedSliceReconfirmationAvailable(item)
|
|
return state, nil
|
|
}
|
|
if status == "processing" || status == "queued" || status == "running" || status == "materializing" || jobStatus == "queued" || jobStatus == "running" || jobStatus == "processing" || item.PendingCombinedSubmissionID != "" {
|
|
state.Phase = "materializing"
|
|
}
|
|
}
|
|
return state, nil
|
|
}
|
|
|
|
func legacyGenerationRecoveryCandidate(task domain.AnnotationTaskRecord, item *domain.HumanItemRecord) bool {
|
|
if item == nil || !task.SourceIsFullVideo || item.PendingCombinedSubmissionID != "" || item.Subject.Payload["sliceWorkflow"] != nil {
|
|
return false
|
|
}
|
|
if item.ProcessingJob != nil && item.ProcessingJob.Status != "completed" {
|
|
return false
|
|
}
|
|
for _, workspace := range []map[string]any{annotationWorkspacePayload(item), mapValue(item.Subject.Payload["annotationDraft"])} {
|
|
if len(decodeMaterializationValue[domain.SliceSegment](workspace["sliceSegments"])) > 0 && len(decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"])) > 0 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// legacyAnnotationConfirmationReady recognizes persisted pre-workflow evidence
|
|
// for tasks whose old records do not have sliceWorkflow. It never mutates the
|
|
// item; the caller remains responsible for checking the submitted version.
|
|
func (server *server) legacyAnnotationConfirmationReady(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord) (bool, error) {
|
|
if item == nil || item.PendingCombinedSubmissionID != "" {
|
|
return false, nil
|
|
}
|
|
if item.ProcessingJob != nil && (item.ProcessingJob.Status == "queued" || item.ProcessingJob.Status == "running" || item.ProcessingJob.Status == "processing" || item.ProcessingJob.Status == "failed") {
|
|
return false, nil
|
|
}
|
|
|
|
if item.Subject.Payload != nil {
|
|
if rawWorkflow, exists := item.Subject.Payload["sliceWorkflow"]; exists && rawWorkflow != nil {
|
|
workflow := mapValue(rawWorkflow)
|
|
return stringValue(workflow["status"]) == "completed" && stringValue(workflow["phase"]) == "annotate", nil
|
|
}
|
|
}
|
|
|
|
workspace := annotationWorkspacePayload(item)
|
|
slices := decodeMaterializationValue[domain.SliceSegment](workspace["sliceSegments"])
|
|
segments := decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"])
|
|
statuses := make(map[string]domain.SliceAnnotationStatus)
|
|
for id, value := range mapValue(workspace["statuses"]) {
|
|
if status := stringValue(value); status != "" {
|
|
statuses[id] = domain.SliceAnnotationStatus(status)
|
|
}
|
|
}
|
|
slices = (combinedAnnotationDraftInput{SliceSegments: slices, Segments: segments, Statuses: statuses}).normalizedSliceSegments()
|
|
if len(slices) == 0 || len(segments) == 0 {
|
|
return false, nil
|
|
}
|
|
if len(validateSliceSegments(slices, task)) > 0 || len(validateCompleteSliceCoverage(slices, task)) > 0 || len(validateContinuousActions(slices, segments, false)) > 0 {
|
|
return false, nil
|
|
}
|
|
seenActionIDs := make(map[string]struct{}, len(segments))
|
|
for _, segment := range segments {
|
|
id := strings.TrimSpace(segment.ID)
|
|
if id == "" {
|
|
return false, nil
|
|
}
|
|
if _, exists := seenActionIDs[id]; exists {
|
|
return false, nil
|
|
}
|
|
seenActionIDs[id] = struct{}{}
|
|
contained := 0
|
|
for _, slice := range slices {
|
|
if segment.Start >= slice.Start && segment.End <= slice.End {
|
|
contained++
|
|
}
|
|
}
|
|
if contained != 1 {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
if task.SourceIsFullVideo {
|
|
evidence, err := server.hasLegacyAnnotationGenerationEvidence(ctx, task, item)
|
|
if err != nil || !evidence {
|
|
return false, err
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func (server *server) hasLegacyAnnotationGenerationEvidence(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord) (bool, error) {
|
|
if item.Subject.Payload != nil {
|
|
materialization := mapValue(item.Subject.Payload["materialization"])
|
|
if stringValue(materialization["status"]) == "completed" && intValue(materialization["revision"]) > 0 && stringValue(materialization["artifactId"]) != "" {
|
|
if runID := stringValue(materialization["runId"]); runID == "" || runID == task.RunID {
|
|
return true, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
slices, err := server.store.ListSliceRevisions(ctx, task.OrganizationID, task.ID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, revision := range slices {
|
|
if revision.TaskID == task.ID && revision.Revision > 0 && len(revision.Segments) > 0 {
|
|
return true, nil
|
|
}
|
|
}
|
|
annotations, err := server.store.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
for _, revision := range annotations {
|
|
if revision.TaskID == task.ID && revision.Revision > 0 && len(revision.Segments) > 0 {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|