package api import ( "context" "fmt" "strings" "droboticflow/internal/domain" ) // Always resolve new selections on the server. Existing saved selections are // copied from the workspace/revision, so dictionary maintenance cannot rewrite // a submitted classification and a client cannot forge tags or another tenant. func (s *server) projectDictionarySelections(ctx context.Context, item *domain.HumanItemRecord, kind string) (map[string]domain.AnnotationLabelRecord, error) { selection, err := s.store.GetProjectAnnotationLabels(ctx, item.ProjectID) if err != nil { return nil, annotationDictionaryReadError{err} } result := map[string]domain.AnnotationLabelRecord{} if selection == nil { return result, nil } if selection.OrganizationID != item.OrganizationID { return nil, fmt.Errorf("项目字典不属于当前组织") } ids := selection.LabelIDs if kind == "slice" { ids = selection.SliceLabelIDs } for _, id := range ids { label, err := s.store.GetAnnotationLabel(ctx, id) if err != nil { return nil, annotationDictionaryReadError{err} } if label != nil && label.OrganizationID == item.OrganizationID && dictionaryKind(label.Kind) == kind && label.Status == domain.AnnotationLabelActive { result[id] = *label } } return result, nil } func sameDictionaryVersion(a, b *domain.DictionarySelection) bool { return a != nil && b != nil && a.ID == b.ID && a.Version == b.Version } func (s *server) canonicalActionDictionaries(ctx context.Context, item *domain.HumanItemRecord, segments []domain.AnnotationSegment, mode ...string) []validationIssue { previous := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(item)["segments"]) if len(previous) == 0 && item.LegacyTaskID != "" { revisions, err := s.store.ListAnnotationRevisions(ctx, item.OrganizationID, item.LegacyTaskID) if err != nil { return []validationIssue{annotationReadValidationIssue("segments", "无法读取已保存的动作分类", err)} } for _, rev := range revisions { if rev.Revision == item.CurrentRevision { previous = rev.Segments } } } if issues := rememberDictionarySelections(item, actionDictionarySelections(previous)...); len(issues) > 0 { return issues } old := map[string]domain.AnnotationSegment{} for _, seg := range previous { old[seg.ID] = seg } var allowed map[string]domain.AnnotationLabelRecord for i := range segments { seg := &segments[i] p := old[seg.ID] if seg.ClearDictionary { seg.Dictionary = nil seg.ClearDictionary = false continue } if len(mode) > 0 && mode[0] == "restore" && !seg.IsInvalid && strings.TrimSpace(seg.Description) != "" { if saved := savedDictionarySelection(item, seg.Dictionary, "action"); saved != nil { seg.Dictionary = saved continue } } if seg.IsInvalid || strings.TrimSpace(seg.Description) == "" { seg.Dictionary = nil continue } if p.Dictionary != nil && (sameDictionaryVersion(p.Dictionary, seg.Dictionary) || (seg.Dictionary == nil && seg.Description == p.Description && !p.IsInvalid)) { seg.Dictionary = p.Dictionary continue } if seg.Dictionary == nil { continue } // Legacy/free text is never guessed into a category. if allowed == nil { var err error allowed, err = s.projectDictionarySelections(ctx, item, "action") if err != nil { return []validationIssue{dictionaryReadValidationIssue("segments", "无法读取动作字典,请重试", err)} } } label, ok := allowed[seg.Dictionary.ID] if !ok || label.Version != seg.Dictionary.Version { return []validationIssue{{Field: fmt.Sprintf("segments.%d.dictionary", i), Code: "dictionary_selection_stale", Message: "动作词条已变化,请从项目字典重新选择"}} } seg.Dictionary = label.Selection() } return rememberDictionarySelections(item, actionDictionarySelections(segments)...) }