91 lines
3.1 KiB
Go

package worker
import (
"context"
"fmt"
"droboticflow/internal/domain"
)
func (runtime *Runtime) ensureGenericAuditHumanItem(
ctx context.Context,
run *domain.RunRecord,
node domain.WorkflowNode,
task domain.AuditTaskRecord,
inputArtifacts []domain.ExecutionArtifact,
) error {
batches, err := runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
if err != nil {
return err
}
var batch *domain.HumanBatchRecord
for index := range batches {
if batches[index].GateNodeID == node.ID && batches[index].PluginID == "human-loop-core" {
copy := batches[index]
batch = &copy
break
}
}
if batch == nil {
created := domain.HumanBatchRecord{
OrganizationID: run.OrganizationID, PluginID: "human-loop-core", ProjectID: run.ProjectID,
WorkflowID: run.WorkflowID, RunID: run.ID, GateNodeID: node.ID, Status: domain.HumanBatchActive,
Stages: []domain.HumanStageDefinition{{ID: "approval", Label: "人工审批", Kind: domain.HumanStageApproval, RequiredRole: "quality_reviewer"}},
ReleasePolicy: domain.HumanReleaseAllAccepted, PartitionMaxItems: 1, PartitionMaxWaitSeconds: 1,
MaxReworkRounds: 1, ItemCount: 1,
}
if err := runtime.store.UpsertHumanBatch(ctx, created); err != nil {
return err
}
batches, err = runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
if err != nil {
return err
}
for index := range batches {
if batches[index].GateNodeID == node.ID && batches[index].PluginID == "human-loop-core" {
copy := batches[index]
batch = &copy
break
}
}
}
if batch == nil {
return fmt.Errorf("generic human batch was not persisted for audit %s", task.ID)
}
existing, err := runtime.store.ListHumanItems(ctx, run.OrganizationID, domain.HumanItemQuery{BatchID: batch.ID})
if err != nil {
return err
}
for _, item := range existing {
if item.LegacyAuditTaskID == task.ID {
return nil
}
}
payload := createAuditPayload(node, inputArtifacts)
item := domain.HumanItemRecord{
OrganizationID: run.OrganizationID, PluginID: "human-loop-core", BatchID: batch.ID,
ProjectID: run.ProjectID, WorkflowID: run.WorkflowID, RunID: run.ID, GateNodeID: node.ID,
LegacyAuditTaskID: task.ID,
Subject: domain.HumanSubjectRef{ItemKey: task.ID, Payload: map[string]any{
"image": payload.Image, "confidence": payload.Confidence, "detectedObjects": payload.DetectedObjects, "reason": payload.Reason,
}},
CurrentStageID: "approval", Status: domain.HumanItemSubmitted, MaxReworkRounds: 1, Version: 1,
}
if err := runtime.store.UpsertHumanItem(ctx, item); err != nil {
return err
}
items, err := runtime.store.ListHumanItems(ctx, run.OrganizationID, domain.HumanItemQuery{BatchID: batch.ID})
if err != nil {
return err
}
for _, stored := range items {
if stored.LegacyAuditTaskID == task.ID {
return runtime.store.AppendHumanEvent(ctx, domain.HumanEventRecord{
OrganizationID: run.OrganizationID, BatchID: batch.ID, ItemID: stored.ID, StageID: "approval",
EventType: "created", ActorUserID: "system", ItemVersion: stored.Version,
})
}
}
return nil
}