droboticflow/internal/api/raw_human_template.go

131 lines
6.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"droboticflow/internal/domain"
pluginregistry "droboticflow/internal/plugins"
)
const rawHumanTemplatePrefix = "template-fanyu-raw-human-delivery-"
func rawHumanTemplate(base domain.TemplateRecord, normalizerImage, packageImage string) domain.TemplateRecord {
// Deep copy: the existing template and its nested ports must never change.
encoded, _ := json.Marshal(base)
var next domain.TemplateRecord
_ = json.Unmarshal(encoded, &next)
next.ID = rawHumanTemplatePrefix + base.OrganizationID
next.Name = "原始数据持续接入与人工闭环交付"
next.Description = "原始双目视频直接进入人工切片、标注、质检和抽样验收;不运行去畸变、人脸模糊、Qwen、HeadPose 或 HandPose。每个完整 part 独立处理,交付保留原始画面。"
next.Type = domain.WorkflowTypeStreaming
next.Schedule = nil
next.ScanConfig = nil
next.ArtifactStorage = nil
next.DefinitionState = domain.WorkflowDefinitionStateReleased
next.DefinitionVersion = 1
next.DefinitionFamilyID = next.ID
next.SupersedesTemplateID = ""
next.CreatedAt = nowISO()
next.UpdatedAt = next.CreatedAt
nodes := []domain.WorkflowNode{}
for _, node := range next.Nodes {
switch node.ID {
case "fanyu-v56-directory":
node.Position.X = 0
node.Position.Y = 0
case "fanyu-v56-normalize":
node.Position.X = 320
node.Position.Y = 0
node.Data.Label = "原始数据配对与基础校验"
node.Data.Image = normalizerImage
node.Data.OperatorImageID = normalizerImage
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "PIN_OPERATOR_IMAGE", "true")
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "PROCESSING_PROFILE", domain.ProcessingProfileRawHuman)
var pts domain.PortDefinition
for _, port := range node.Data.Outputs {
if port.ID == "videoPts" {
pts = port
}
}
fanyuRetainOutputPorts(&node, "leftVideos", "rightVideos", "imu", "calibrations", "summary")
for _, side := range []string{"left", "right"} {
port := pts
port.ID = side + "Timestamps"
port.Name = port.ID
node.Data.Outputs = append(node.Data.Outputs, port)
}
case "fanyu-v56-human":
node.Position.X = 640
node.Position.Y = 0
node.Data.Label = "原始数据人工闭环(切片/标注/质检/验收)"
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "PROCESSING_PROFILE", domain.ProcessingProfileRawHuman)
fanyuRetainInputPorts(&node, "fullLeftVideos", "fullRightVideos", "leftTimestamps", "rightTimestamps", "imu")
node.Data.Inputs = fanyuSetRequiredPorts(node.Data.Inputs, "fullLeftVideos", "fullRightVideos", "leftTimestamps", "rightTimestamps", "imu")
case "fanyu-v56-package":
node.Position.X = 960
node.Position.Y = 0
node.Data.Label = "原始数据切片打包交付(无姿态)"
node.Data.Image = packageImage
node.Data.OperatorImageID = packageImage
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "PIN_OPERATOR_IMAGE", "true")
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "PROCESSING_PROFILE", domain.ProcessingProfileRawHuman)
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "REQUIRE_HEADPOSE", "false")
node.Data.Hyperparams = fanyuUpsertHyperparam(node.Data.Hyperparams, "REQUIRE_HANDPOSE", "false")
fanyuRetainInputPorts(&node, "acceptedSliceDirectory", "acceptanceSummary", "calibrations")
default:
continue
}
nodes = append(nodes, node)
}
next.Nodes = nodes
next.Edges = []domain.WorkflowEdge{
fanyuEdge("raw-e1", "fanyu-v56-directory", "directory", "fanyu-v56-normalize", "directory"),
fanyuEdge("raw-e2", "fanyu-v56-normalize", "leftVideos", "fanyu-v56-human", "fullLeftVideos"),
fanyuEdge("raw-e3", "fanyu-v56-normalize", "rightVideos", "fanyu-v56-human", "fullRightVideos"),
fanyuEdge("raw-e4", "fanyu-v56-normalize", "leftTimestamps", "fanyu-v56-human", "leftTimestamps"),
fanyuEdge("raw-e5", "fanyu-v56-normalize", "rightTimestamps", "fanyu-v56-human", "rightTimestamps"),
fanyuEdge("raw-e6", "fanyu-v56-normalize", "imu", "fanyu-v56-human", "imu"),
fanyuEdge("raw-e7", "fanyu-v56-normalize", "calibrations", "fanyu-v56-package", "calibrations"),
fanyuEdge("raw-e8", "fanyu-v56-human", "acceptedSliceDirectory", "fanyu-v56-package", "acceptedSliceDirectory"),
fanyuEdge("raw-e9", "fanyu-v56-human", "acceptanceSummary", "fanyu-v56-package", "acceptanceSummary"),
}
return next
}
func (s *server) ensureRawHumanTemplate(ctx context.Context, org string) error {
normalizer, pack := strings.TrimSpace(os.Getenv("FANYU_RAW_NORMALIZER_IMAGE")), strings.TrimSpace(os.Getenv("FANYU_RAW_PACKAGE_IMAGE"))
if normalizer == "" || pack == "" {
return nil
} // Only advertise after both isolated images are deployed.
if !strings.Contains(normalizer, "@sha256:") || !strings.Contains(pack, "@sha256:") {
return fmt.Errorf("raw-human images must be digest pinned")
}
existing, err := s.store.GetTemplate(ctx, rawHumanTemplatePrefix+org)
if err != nil || existing != nil {
return err
}
base, err := s.store.GetTemplate(ctx, fanyuDeliveryTemplateV58ID+"-"+org)
if err != nil {
return err
}
if base == nil {
return nil
}
next := rawHumanTemplate(*base, normalizer, pack)
if len(next.Nodes) != 4 {
return fmt.Errorf("raw-human template requires four explicit nodes")
}
if issues := validateWorkflowTopology(next.Nodes, next.Edges); len(issues) > 0 {
return fmt.Errorf("raw-human template topology: %v", issues)
}
release := domain.WorkflowReleaseRecord{ID: "release-" + next.ID, OrganizationID: org, TemplateID: next.ID, Version: 1, Name: next.Name, Description: next.Description, Type: next.Type, Kind: next.Kind, ExecutionScope: domain.WorkflowExecutionScopeProduction, MaxConcurrentRuns: next.MaxConcurrentRuns, WorkflowInputs: next.WorkflowInputs, Nodes: next.Nodes, Edges: next.Edges, Capabilities: []string{"directory_input", "raw_human", "human_cut_adjustment", "human_annotation", "human_quality_review", "human_acceptance"}, RequiredPluginIDs: []string{pluginregistry.HumanLoopCoreID, pluginregistry.AnnotationQAID}, ContentDigest: workflowReleaseDigest(next), PublishedByUserID: "system", PublishedAt: next.CreatedAt, CreatedAt: next.CreatedAt, ReleaseNotes: "Independent raw-data human loop; existing preprocessing templates unchanged"}
if err := s.store.UpsertWorkflowRelease(ctx, release); err != nil {
return err
}
return s.store.UpsertTemplate(ctx, next)
}