forked from longtao.wu/droboticflow
81 lines
2.7 KiB
Go
81 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
const dictionarySelectionHistoryKey = "dictionarySelectionHistory"
|
|
|
|
// Only server-resolved selections enter this item-scoped recovery ledger.
|
|
// It allows undo after a shared word changes or leaves the project, without
|
|
// treating the client's undo snapshot as trustworthy dictionary metadata.
|
|
func rememberDictionarySelections(item *domain.HumanItemRecord, selections ...*domain.DictionarySelection) []validationIssue {
|
|
if item == nil {
|
|
return nil
|
|
}
|
|
history := decodeMaterializationValue[domain.DictionarySelection](item.Subject.Payload[dictionarySelectionHistoryKey])
|
|
seen := map[string]bool{}
|
|
key := func(d domain.DictionarySelection) string { return fmt.Sprintf("%s/%s/%d", d.Kind, d.ID, d.Version) }
|
|
for _, d := range history {
|
|
seen[key(d)] = true
|
|
}
|
|
for _, d := range selections {
|
|
if d != nil && !seen[key(*d)] {
|
|
copy := *d
|
|
copy.Tags = append([]string{}, d.Tags...)
|
|
history = append(history, copy)
|
|
seen[key(copy)] = true
|
|
}
|
|
}
|
|
body, err := json.Marshal(history)
|
|
if err != nil || len(body) > 1<<20 {
|
|
return []validationIssue{{Field: "dictionary", Message: "分类恢复记录超过限制,请拆分任务后重试"}}
|
|
}
|
|
if item.Subject.Payload == nil {
|
|
item.Subject.Payload = map[string]any{}
|
|
}
|
|
if len(history) > 0 {
|
|
item.Subject.Payload[dictionarySelectionHistoryKey] = history
|
|
}
|
|
return nil
|
|
}
|
|
func savedDictionarySelection(item *domain.HumanItemRecord, requested *domain.DictionarySelection, kind string) *domain.DictionarySelection {
|
|
if item == nil || requested == nil {
|
|
return nil
|
|
}
|
|
for _, saved := range decodeMaterializationValue[domain.DictionarySelection](item.Subject.Payload[dictionarySelectionHistoryKey]) {
|
|
if saved.Kind == kind && saved.ID == requested.ID && saved.Version == requested.Version {
|
|
copy := saved
|
|
return ©
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func sliceDictionarySelections(slices []domain.SliceSegment) []*domain.DictionarySelection {
|
|
selections := []*domain.DictionarySelection{}
|
|
for _, slice := range slices {
|
|
selections = append(selections, slice.Dictionary, slice.DiscardReasonDictionary)
|
|
}
|
|
return selections
|
|
}
|
|
func actionDictionarySelections(actions []domain.AnnotationSegment) []*domain.DictionarySelection {
|
|
selections := []*domain.DictionarySelection{}
|
|
for _, action := range actions {
|
|
selections = append(selections, action.Dictionary)
|
|
}
|
|
return selections
|
|
}
|
|
|
|
func copyClassificationRecoveryState(target, source *domain.HumanItemRecord) {
|
|
for _, key := range []string{dictionarySelectionHistoryKey, "unconfirmedSliceNames"} {
|
|
if value, ok := source.Subject.Payload[key]; ok {
|
|
target.Subject.Payload[key] = value
|
|
} else {
|
|
delete(target.Subject.Payload, key)
|
|
}
|
|
}
|
|
}
|