9399 lines
359 KiB
Go
Raw 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"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"mime"
"net"
"net/http"
"net/mail"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"droboticflow/internal/ai"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"droboticflow/internal/k8s"
"droboticflow/internal/lightweight"
pluginregistry "droboticflow/internal/plugins"
"droboticflow/internal/store"
"github.com/baidubce/bce-sdk-go/services/bos"
bosapi "github.com/baidubce/bce-sdk-go/services/bos/api"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type validationIssue struct {
Field string `json:"field"`
Code string `json:"code,omitempty"`
Message string `json:"message"`
}
type server struct {
store store.Store
ai ai.Service
operatorRunnerImage string
workerAPIBaseURL string
globalBOSEndpoint string
apiToken string
requireSessionAuth bool
sessionCookieSecure bool
objectSyncEnabled bool
trustedProxyNets []*net.IPNet
authLimiter *rateLimiter
externalWriteLimiter *rateLimiter
k8sNamespace string
k8sImagePullSecretName string
workspacePVCName string
workspaceMountPath string
operatorJobTTLSeconds int
customOperatorImageRegistry string
customOperatorImageImporterImage string
customOperatorImageImportTimeoutSeconds int
customOperatorImageArchiveMaxMiB int
buildCommit string
buildTime string
image string
}
type runJobTerminator interface {
TerminateRun(ctx context.Context, namespace string, runID string) error
TerminateJob(ctx context.Context, namespace string, jobName string) error
}
var storageConnectionBucketsResolver = listStorageBucketsFromConnection
var storageConnectionObjectsResolver = listStorageObjectsFromConnection
var runJobTerminatorFactory = func(namespace string, imagePullSecretName string, workspacePVCName string, workspaceMountPath string) (runJobTerminator, error) {
return k8s.NewLauncher(namespace, imagePullSecretName, workspacePVCName, workspaceMountPath, "", "", "", 0)
}
const maxStoragePathFavoritesPerScope = 12
var workflowScheduleTimeOfDayPattern = regexp.MustCompile(`^(?:[01]\d|2[0-3]):[0-5]\d$`)
func maskedSecretPreview(value string) string {
compact := strings.Join(strings.Fields(strings.TrimSpace(value)), "")
if compact == "" {
compact = strings.TrimSpace(value)
}
if compact == "" {
return ""
}
runes := []rune(compact)
switch length := len(runes); {
case length <= 2:
return strings.Repeat("*", length)
case length <= 6:
return string(runes[:1]) + "****" + string(runes[length-1:])
case length <= 10:
return string(runes[:2]) + "****" + string(runes[length-2:])
default:
return string(runes[:4]) + "****" + string(runes[length-4:])
}
}
func sanitizeScanConfig(item *domain.ScanConfig) *domain.ScanConfig {
if item == nil {
return nil
}
copy := *item
copy.HasAccessKeyID = strings.TrimSpace(copy.AccessKeyID) != ""
copy.HasSecretAccessKey = strings.TrimSpace(copy.SecretAccessKey) != ""
copy.AccessKeyPreview = maskedSecretPreview(copy.AccessKeyID)
copy.SecretAccessKeyPreview = maskedSecretPreview(copy.SecretAccessKey)
copy.AccessKeyID = ""
copy.SecretAccessKey = ""
return &copy
}
func sanitizeStorageConnection(item *domain.StorageConnectionRecord) *domain.StorageConnectionRecord {
if item == nil {
return nil
}
copy := *item
copy.HasAccessKeyID = strings.TrimSpace(copy.AccessKeyID) != ""
copy.HasSecretAccessKey = strings.TrimSpace(copy.SecretAccessKey) != ""
copy.AccessKeyPreview = maskedSecretPreview(copy.AccessKeyID)
copy.SecretAccessKeyPreview = maskedSecretPreview(copy.SecretAccessKey)
copy.AccessKeyID = ""
copy.SecretAccessKey = ""
return &copy
}
func sanitizeStorageConnections(items []domain.StorageConnectionRecord) []domain.StorageConnectionRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.StorageConnectionRecord, 0, len(items))
for _, item := range items {
copy := item
sanitized = append(sanitized, *sanitizeStorageConnection(&copy))
}
return sanitized
}
func sanitizeMongoConnection(item *domain.MongoConnectionRecord) *domain.MongoConnectionRecord {
if item == nil {
return nil
}
copy := *item
copy.HasURI = strings.TrimSpace(copy.URI) != ""
copy.URIPreview = maskedSecretPreview(copy.URI)
copy.URI = ""
return &copy
}
func sanitizeMongoConnections(items []domain.MongoConnectionRecord) []domain.MongoConnectionRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.MongoConnectionRecord, 0, len(items))
for _, item := range items {
copy := item
sanitized = append(sanitized, *sanitizeMongoConnection(&copy))
}
return sanitized
}
func normalizeStoragePathFavoritePrefix(value string) string {
return strings.Trim(strings.TrimSpace(value), "/")
}
func normalizeStoragePathFavoriteRecord(item domain.StoragePathFavoriteRecord) (domain.StoragePathFavoriteRecord, bool) {
scopeKey := strings.TrimSpace(item.ScopeKey)
bucket := strings.TrimSpace(item.Bucket)
if scopeKey == "" || bucket == "" {
return domain.StoragePathFavoriteRecord{}, false
}
prefix := normalizeStoragePathFavoritePrefix(item.Prefix)
label := strings.TrimSpace(item.Label)
if label == "" {
label = bucket
if prefix != "" {
label += "/" + prefix
}
}
return domain.StoragePathFavoriteRecord{
ScopeKey: scopeKey,
Bucket: bucket,
Prefix: prefix,
Label: label,
}, true
}
func sanitizeStoragePathFavorites(items []domain.StoragePathFavoriteRecord) []domain.StoragePathFavoriteRecord {
if len(items) == 0 {
return []domain.StoragePathFavoriteRecord{}
}
sanitized := make([]domain.StoragePathFavoriteRecord, 0, len(items))
countsByScope := map[string]int{}
seen := map[string]struct{}{}
for _, item := range items {
normalized, ok := normalizeStoragePathFavoriteRecord(item)
if !ok {
continue
}
pathKey := normalized.Bucket
if normalized.Prefix != "" {
pathKey += "/" + normalized.Prefix
}
compositeKey := normalized.ScopeKey + "|" + pathKey
if _, exists := seen[compositeKey]; exists {
continue
}
if countsByScope[normalized.ScopeKey] >= maxStoragePathFavoritesPerScope {
continue
}
seen[compositeKey] = struct{}{}
countsByScope[normalized.ScopeKey] += 1
sanitized = append(sanitized, normalized)
}
return sanitized
}
func listStorageBucketsFromConnection(ctx context.Context, connection *domain.StorageConnectionRecord) ([]string, error) {
if connection == nil {
return []string{}, nil
}
endpoint := storageEndpointForProvider(connection.Provider, connection.Endpoint)
if normalizeStorageProvider(connection.Provider) == domain.StorageProviderBOS && endpoint == "" {
return nil, fmt.Errorf("BOS endpoint is required for storage connection %s", strings.TrimSpace(connection.Name))
}
client, err := bos.NewClient(
strings.TrimSpace(connection.AccessKeyID),
strings.TrimSpace(connection.SecretAccessKey),
endpoint,
)
if err != nil {
return nil, err
}
result, err := client.ListBucketsWithContext(ctx)
if err != nil {
return nil, err
}
items := make([]string, 0, len(result.Buckets))
for _, bucket := range result.Buckets {
name := strings.TrimSpace(bucket.Name)
if name != "" {
items = append(items, name)
}
}
sort.Strings(items)
return items, nil
}
func storageObjectRequestPrefix(prefix string) string {
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
if normalizedPrefix == "" {
return ""
}
return normalizedPrefix + "/"
}
func appendStorageObjectItems(items map[string]struct{}, result *bosapi.ListObjectsResult, normalizedPrefix string, requestPrefix string) {
if result == nil {
return
}
for _, item := range result.CommonPrefixes {
prefixValue := strings.TrimSpace(item.Prefix)
if prefixValue == "" || prefixValue == requestPrefix {
continue
}
items[prefixValue] = struct{}{}
}
for _, item := range result.Contents {
key := strings.TrimSpace(item.Key)
if key == "" || key == normalizedPrefix || key == requestPrefix {
continue
}
items[key] = struct{}{}
}
}
func listStorageObjectsFromConnection(ctx context.Context, connection *domain.StorageConnectionRecord, bucket string, prefix string) ([]string, error) {
if connection == nil {
return []string{}, nil
}
normalizedBucket := strings.TrimSpace(bucket)
if normalizedBucket == "" {
return []string{}, nil
}
endpoint := storageEndpointForProvider(connection.Provider, connection.Endpoint)
if normalizeStorageProvider(connection.Provider) == domain.StorageProviderBOS && endpoint == "" {
return nil, fmt.Errorf("BOS endpoint is required for storage connection %s", strings.TrimSpace(connection.Name))
}
client, err := bos.NewClient(
strings.TrimSpace(connection.AccessKeyID),
strings.TrimSpace(connection.SecretAccessKey),
endpoint,
)
if err != nil {
return nil, err
}
normalizedPrefix := strings.Trim(strings.TrimSpace(prefix), "/")
requestPrefix := storageObjectRequestPrefix(prefix)
collected := make(map[string]struct{})
marker := ""
for {
result, err := client.ListObjectsWithContext(ctx, normalizedBucket, &bosapi.ListObjectsArgs{
Prefix: requestPrefix,
Delimiter: "/",
MaxKeys: 1000,
Marker: marker,
})
if err != nil {
return nil, err
}
appendStorageObjectItems(collected, result, normalizedPrefix, requestPrefix)
if result == nil || !result.IsTruncated {
break
}
nextMarker := strings.TrimSpace(result.NextMarker)
if nextMarker == "" || nextMarker == marker {
break
}
marker = nextMarker
}
items := make([]string, 0, len(collected))
for item := range collected {
items = append(items, item)
}
sort.Strings(items)
return items, nil
}
func cloneWorkflowNodes(items []domain.WorkflowNode) []domain.WorkflowNode {
if len(items) == 0 {
return items
}
cloned := make([]domain.WorkflowNode, len(items))
copy(cloned, items)
return cloned
}
func sanitizeWorkflow(item *domain.WorkflowRecord) *domain.WorkflowRecord {
if item == nil {
return nil
}
copy := *item
domain.NormalizeWorkflowDefinitionMetadata(&copy)
copy.ExecutionScope = normalizeWorkflowExecutionScope(copy.ExecutionScope)
copy.Nodes = cloneWorkflowNodes(item.Nodes)
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
return &copy
}
func sanitizeWorkflows(items []domain.WorkflowRecord) []domain.WorkflowRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.WorkflowRecord, 0, len(items))
for _, item := range items {
copy := item
domain.NormalizeWorkflowDefinitionMetadata(&copy)
copy.ExecutionScope = normalizeWorkflowExecutionScope(copy.ExecutionScope)
copy.Nodes = cloneWorkflowNodes(item.Nodes)
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
sanitized = append(sanitized, copy)
}
return sanitized
}
type workflowNodeSourceStateResponse struct {
NodeID string `json:"nodeId"`
Supported bool `json:"supported"`
Stateful bool `json:"stateful"`
Reason string `json:"reason,omitempty"`
ScanMode string `json:"scanMode,omitempty"`
IncrementalUnit string `json:"incrementalUnit,omitempty"`
DirectoryDepth int `json:"directoryDepth,omitempty"`
DataSource string `json:"dataSource,omitempty"`
SourcePath string `json:"sourcePath,omitempty"`
Bucket string `json:"bucket,omitempty"`
Prefix string `json:"prefix,omitempty"`
Pattern string `json:"pattern,omitempty"`
CurrentFingerprint string `json:"currentFingerprint,omitempty"`
Current *domain.SourceStateRecord `json:"current,omitempty"`
Versions []domain.SourceStateRecord `json:"versions,omitempty"`
}
func workflowNodeByID(workflow *domain.WorkflowRecord, nodeID string) *domain.WorkflowNode {
if workflow == nil {
return nil
}
for index := range workflow.Nodes {
if workflow.Nodes[index].ID == nodeID {
return &workflow.Nodes[index]
}
}
return nil
}
func runSummaryFromRecord(run domain.RunRecord) domain.RunSummary {
mode := run.Mode
if mode == "" {
mode = domain.RunModeProduction
}
return domain.RunSummary{
ID: run.ID,
OrganizationID: run.OrganizationID,
WorkflowID: run.WorkflowID,
WorkflowName: run.WorkflowName,
ProjectID: run.ProjectID,
ProjectName: run.ProjectName,
TaskID: run.TaskID,
Trigger: run.Trigger,
Mode: mode,
ReleaseID: run.ReleaseID,
WorkflowVersion: run.WorkflowVersion,
GitCommitSha: run.GitCommitSha,
ImageDigest: run.ImageDigest,
Attempt: run.Attempt,
ParentRunID: run.ParentRunID,
RestartFromNodeID: run.RestartFromNodeID,
AvailableAt: run.AvailableAt,
Status: run.Status,
StopMode: run.StopMode,
StopRequestedAt: run.StopRequestedAt,
StartedAt: run.StartedAt,
EndedAt: run.EndedAt,
DurationMS: run.DurationMS,
CurrentNodeID: run.CurrentNodeID,
Failure: run.Failure,
Progress: run.Progress,
Error: run.Error,
CreatedAt: run.CreatedAt,
UpdatedAt: run.UpdatedAt,
}
}
func rerunSubtreeNodeSet(edges []domain.WorkflowEdge, startNodeID string) map[string]struct{} {
visited := map[string]struct{}{}
queue := []string{startNodeID}
for len(queue) > 0 {
nodeID := queue[0]
queue = queue[1:]
if _, ok := visited[nodeID]; ok {
continue
}
visited[nodeID] = struct{}{}
for _, edge := range domain.GetOutgoingEdges(edges, nodeID) {
if strings.TrimSpace(edge.Target) != "" {
queue = append(queue, edge.Target)
}
}
}
return visited
}
func rerunBaselineNodeSet(edges []domain.WorkflowEdge, subtree map[string]struct{}) map[string]struct{} {
baseline := map[string]struct{}{}
seen := map[string]struct{}{}
queue := make([]string, 0, len(subtree))
for nodeID := range subtree {
queue = append(queue, nodeID)
}
for len(queue) > 0 {
nodeID := queue[0]
queue = queue[1:]
if _, ok := seen[nodeID]; ok {
continue
}
seen[nodeID] = struct{}{}
for _, edge := range domain.GetIncomingEdges(edges, nodeID) {
sourceID := strings.TrimSpace(edge.Source)
if sourceID == "" {
continue
}
if _, ok := subtree[sourceID]; ok {
continue
}
if _, ok := baseline[sourceID]; ok {
continue
}
baseline[sourceID] = struct{}{}
queue = append(queue, sourceID)
}
}
return baseline
}
func cloneLogEntries(items []domain.LogEntryRecord) []domain.LogEntryRecord {
if len(items) == 0 {
return []domain.LogEntryRecord{}
}
cloned := make([]domain.LogEntryRecord, len(items))
copy(cloned, items)
return cloned
}
func cloneExecutionArtifacts(items []domain.ExecutionArtifact) []domain.ExecutionArtifact {
if len(items) == 0 {
return []domain.ExecutionArtifact{}
}
cloned := make([]domain.ExecutionArtifact, len(items))
for index, item := range items {
cloned[index] = cloneExecutionArtifact(item)
}
return cloned
}
func cloneExecutionArtifact(item domain.ExecutionArtifact) domain.ExecutionArtifact {
cloned := item
if item.Metadata != nil {
metadata := *item.Metadata
cloned.Metadata = &metadata
}
if item.Data != nil {
cloned.Data = maps.Clone(item.Data)
}
return cloned
}
func runArtifactContentURL(runID string, artifactID string) string {
return fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(runID), url.PathEscape(artifactID))
}
func artifactResponseID(runID string, nodeID string, item domain.ExecutionArtifact) string {
if artifactID := strings.TrimSpace(item.ID); artifactID != "" {
return artifactID
}
identity := strings.Join([]string{
strings.TrimSpace(runID),
strings.TrimSpace(nodeID),
strings.TrimSpace(item.PortID),
strings.TrimSpace(item.PortName),
strings.TrimSpace(item.Path),
strings.TrimSpace(item.Name),
}, "\x00")
digest := sha256.Sum256([]byte(identity))
return "artifact-legacy-" + hex.EncodeToString(digest[:16])
}
func artifactStorageIdentity(item domain.ExecutionArtifact) string {
if artifactID := strings.TrimSpace(item.ID); artifactID != "" {
return "id:" + artifactID
}
if artifactPath := strings.TrimSpace(item.Path); artifactPath != "" {
return "path:" + artifactPath
}
return strings.Join([]string{
"name:" + strings.TrimSpace(item.Name),
"port:" + strings.TrimSpace(item.PortID),
"portName:" + strings.TrimSpace(item.PortName),
}, "\x00")
}
func runArtifactNodeLookup(item domain.RunRecord) map[string]string {
nodeByArtifact := make(map[string]string)
for _, execution := range item.NodeExecutions {
nodeID := strings.TrimSpace(execution.NodeID)
for _, artifact := range execution.Artifacts {
nodeByArtifact[artifactStorageIdentity(artifact)] = nodeID
}
}
return nodeByArtifact
}
func artifactMimeType(item domain.ExecutionArtifact) string {
if item.Metadata == nil {
return ""
}
return strings.TrimSpace(item.Metadata.MimeType)
}
func artifactPreviewKind(item domain.ExecutionArtifact) string {
mimeType := strings.ToLower(artifactMimeType(item))
if strings.EqualFold(path.Ext(firstNonEmptyText(item.Name, item.Path)), ".rrd") || mimeType == "application/vnd.rerun.rrd" {
return "rerun"
}
if item.Type == domain.PortTypeVideo {
return "video"
}
if item.Type == domain.PortTypeImage {
return "image"
}
kind := sourceEntryPayloadKind(firstNonEmptyText(item.Name, item.Path), mimeType)
if kind == "video" || kind == "image" || kind == "audio" {
return kind
}
if strings.EqualFold(path.Ext(firstNonEmptyText(item.Name, item.Path)), ".pdf") {
return "document"
}
return ""
}
func artifactHasContentSource(item domain.ExecutionArtifact) bool {
if strings.TrimSpace(item.ID) == "" {
return false
}
if filePath := strings.TrimSpace(item.Path); filePath != "" && !strings.Contains(filePath, "://") {
return true
}
previewURL := packageArtifactPreviewURL(item)
return strings.HasPrefix(previewURL, "data:") ||
strings.HasPrefix(previewURL, "http://") ||
strings.HasPrefix(previewURL, "https://")
}
func executionArtifactForResponseAtNode(request *http.Request, runID string, nodeID string, item domain.ExecutionArtifact) domain.ExecutionArtifact {
responseIdentity := cloneExecutionArtifact(item)
responseIdentity.RunID = firstNonEmptyText(responseIdentity.RunID, runID)
responseIdentity.NodeID = firstNonEmptyText(responseIdentity.NodeID, nodeID)
cloned := scrubExecutionArtifactForResponse(item)
cloned.RunID = firstNonEmptyText(cloned.RunID, runID)
cloned.NodeID = firstNonEmptyText(cloned.NodeID, nodeID)
cloned.ID = artifactResponseID(responseIdentity.RunID, responseIdentity.NodeID, responseIdentity)
kind := artifactPreviewKind(item)
if kind == "" || !artifactHasContentSource(item) {
return cloned
}
contentRunID := firstNonEmptyText(cloned.RunID, runID, chi.URLParam(request, "id"))
if contentRunID == "" {
return cloned
}
preview := map[string]any{"kind": kind}
format := strings.TrimPrefix(strings.ToLower(path.Ext(firstNonEmptyText(item.Name, item.Path))), ".")
if format != "" {
preview["format"] = format
}
preview["url"] = runArtifactContentURL(contentRunID, cloned.ID)
data := cloneSafeArtifactResponseData(cloned.Data)
data["preview"] = preview
cloned.Data = data
return cloned
}
func executionArtifactsForResponseAtNode(request *http.Request, runID string, nodeID string, items []domain.ExecutionArtifact) []domain.ExecutionArtifact {
if len(items) == 0 {
return []domain.ExecutionArtifact{}
}
cloned := make([]domain.ExecutionArtifact, len(items))
for index, item := range items {
cloned[index] = executionArtifactForResponseAtNode(request, runID, nodeID, item)
}
return cloned
}
func runArtifactsForResponse(request *http.Request, item domain.RunRecord) []domain.ExecutionArtifact {
if len(item.Artifacts) == 0 {
return []domain.ExecutionArtifact{}
}
nodeByArtifact := runArtifactNodeLookup(item)
cloned := make([]domain.ExecutionArtifact, len(item.Artifacts))
for index, artifact := range item.Artifacts {
nodeID := firstNonEmptyText(artifact.NodeID, nodeByArtifact[artifactStorageIdentity(artifact)])
cloned[index] = executionArtifactForResponseAtNode(request, item.ID, nodeID, artifact)
}
return cloned
}
func cloneDataRefs(items []domain.DataRefRecord) []domain.DataRefRecord {
if len(items) == 0 {
return []domain.DataRefRecord{}
}
cloned := make([]domain.DataRefRecord, len(items))
copy(cloned, items)
return cloned
}
func cloneInputLineage(items []domain.InputLineageRecord) []domain.InputLineageRecord {
if len(items) == 0 {
return []domain.InputLineageRecord{}
}
cloned := make([]domain.InputLineageRecord, len(items))
for index, item := range items {
cloned[index] = item
cloned[index].Artifact = cloneExecutionArtifact(item.Artifact)
}
return cloned
}
func inputLineageForResponse(request *http.Request, runID string, nodeByArtifact map[string]string, items []domain.InputLineageRecord) []domain.InputLineageRecord {
if len(items) == 0 {
return []domain.InputLineageRecord{}
}
cloned := make([]domain.InputLineageRecord, len(items))
for index, item := range items {
cloned[index] = item
nodeID := firstNonEmptyText(item.SourceNodeID, item.Artifact.NodeID, nodeByArtifact[artifactStorageIdentity(item.Artifact)])
cloned[index].Artifact = executionArtifactForResponseAtNode(request, runID, nodeID, item.Artifact)
}
return cloned
}
func runForResponse(request *http.Request, item domain.RunRecord) domain.RunRecord {
cloned := item
cloned.Logs = cloneLogEntries(item.Logs)
cloned.ResourceSeries = cloneResourceSamples(item.ResourceSeries)
cloned.Artifacts = runArtifactsForResponse(request, item)
cloned.K8sRefs = append([]domain.K8sRef{}, item.K8sRefs...)
if len(item.NodeExecutions) == 0 {
cloned.NodeExecutions = []domain.NodeExecutionRecord{}
return cloned
}
nodeByArtifact := runArtifactNodeLookup(item)
cloned.NodeExecutions = make([]domain.NodeExecutionRecord, len(item.NodeExecutions))
for index, execution := range item.NodeExecutions {
clonedExecution := cloneNodeExecutionRecord(execution)
clonedExecution.InputArtifacts = make([]domain.ExecutionArtifact, len(execution.InputArtifacts))
for artifactIndex, artifact := range execution.InputArtifacts {
nodeID := firstNonEmptyText(artifact.NodeID, nodeByArtifact[artifactStorageIdentity(artifact)])
clonedExecution.InputArtifacts[artifactIndex] = executionArtifactForResponseAtNode(request, item.ID, nodeID, artifact)
}
clonedExecution.InputLineage = inputLineageForResponse(request, item.ID, nodeByArtifact, execution.InputLineage)
clonedExecution.Artifacts = executionArtifactsForResponseAtNode(request, item.ID, execution.NodeID, execution.Artifacts)
cloned.NodeExecutions[index] = clonedExecution
}
return cloned
}
func runLineageForResponse(request *http.Request, item domain.RunRecord) domain.RunLineageRecord {
lineage := buildRunLineage(item)
nodeByArtifact := runArtifactNodeLookup(item)
artifacts := lineage.Artifacts
lineage.Artifacts = make([]domain.ExecutionArtifact, len(artifacts))
for index, artifact := range artifacts {
nodeID := firstNonEmptyText(artifact.NodeID, nodeByArtifact[artifactStorageIdentity(artifact)])
lineage.Artifacts[index] = executionArtifactForResponseAtNode(request, item.ID, nodeID, artifact)
}
for index := range lineage.Nodes {
for artifactIndex, artifact := range lineage.Nodes[index].InputArtifacts {
nodeID := firstNonEmptyText(artifact.NodeID, nodeByArtifact[artifactStorageIdentity(artifact)])
lineage.Nodes[index].InputArtifacts[artifactIndex] = executionArtifactForResponseAtNode(request, item.ID, nodeID, artifact)
}
lineage.Nodes[index].OutputArtifacts = executionArtifactsForResponseAtNode(
request,
item.ID,
lineage.Nodes[index].NodeID,
lineage.Nodes[index].OutputArtifacts,
)
}
for edgeIndex := range lineage.Edges {
if strings.TrimSpace(lineage.Edges[edgeIndex].SourceArtifactID) != "" {
continue
}
for _, artifact := range lineage.Artifacts {
if artifact.NodeID != lineage.Edges[edgeIndex].SourceNodeID ||
artifact.Name != lineage.Edges[edgeIndex].SourceArtifactName {
continue
}
if lineage.Edges[edgeIndex].SourcePortID != "" && artifact.PortID != lineage.Edges[edgeIndex].SourcePortID {
continue
}
lineage.Edges[edgeIndex].SourceArtifactID = artifact.ID
break
}
}
return lineage
}
func lineageNodeOutputArtifact(node domain.LineageNodeRecord, artifactID string) (domain.ExecutionArtifact, bool) {
for _, item := range node.OutputArtifacts {
if item.ID == artifactID {
return item, true
}
}
return domain.ExecutionArtifact{}, false
}
func lineageNodeInputArtifact(node domain.LineageNodeRecord, artifactID string) (domain.ExecutionArtifact, bool) {
for _, item := range node.InputArtifacts {
if item.ID == artifactID {
return item, true
}
}
return domain.ExecutionArtifact{}, false
}
func appendLineageNodeUnique(items []domain.LineageNodeRecord, seen map[string]struct{}, item domain.LineageNodeRecord) []domain.LineageNodeRecord {
if item.NodeID == "" {
return items
}
if _, exists := seen[item.NodeID]; exists {
return items
}
seen[item.NodeID] = struct{}{}
return append(items, item)
}
func appendLineageEdgeUnique(items []domain.LineageEdgeRecord, seen map[string]struct{}, item domain.LineageEdgeRecord) []domain.LineageEdgeRecord {
if item.ID == "" {
return items
}
if _, exists := seen[item.ID]; exists {
return items
}
seen[item.ID] = struct{}{}
return append(items, item)
}
func artifactLineageForResponse(request *http.Request, item domain.RunRecord, artifactID string) (*domain.ArtifactLineageRecord, bool) {
artifactID = strings.TrimSpace(artifactID)
if artifactID == "" {
return nil, false
}
lineage := runLineageForResponse(request, item)
nodeByID := make(map[string]domain.LineageNodeRecord, len(lineage.Nodes))
for _, node := range lineage.Nodes {
nodeByID[node.NodeID] = node
}
var artifact domain.ExecutionArtifact
var producer *domain.LineageNodeRecord
consumers := []domain.LineageNodeRecord{}
consumerSeen := map[string]struct{}{}
for _, node := range lineage.Nodes {
if output, ok := lineageNodeOutputArtifact(node, artifactID); ok {
artifact = output
nodeCopy := node
producer = &nodeCopy
}
if input, ok := lineageNodeInputArtifact(node, artifactID); ok {
if artifact.ID == "" {
artifact = input
}
consumers = appendLineageNodeUnique(consumers, consumerSeen, node)
}
}
upstreamEdges := []domain.LineageEdgeRecord{}
downstreamEdges := []domain.LineageEdgeRecord{}
relatedNodes := []domain.LineageNodeRecord{}
relatedEdges := []domain.LineageEdgeRecord{}
nodeSeen := map[string]struct{}{}
edgeSeen := map[string]struct{}{}
if producer != nil {
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, *producer)
}
for _, consumer := range consumers {
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, consumer)
}
for _, edge := range lineage.Edges {
if producer != nil && edge.TargetNodeID == producer.NodeID {
upstreamEdges = append(upstreamEdges, edge)
relatedEdges = appendLineageEdgeUnique(relatedEdges, edgeSeen, edge)
if sourceNode, ok := nodeByID[edge.SourceNodeID]; ok {
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, sourceNode)
}
}
if edge.SourceArtifactID == artifactID {
downstreamEdges = append(downstreamEdges, edge)
relatedEdges = appendLineageEdgeUnique(relatedEdges, edgeSeen, edge)
if sourceNode, ok := nodeByID[edge.SourceNodeID]; ok {
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, sourceNode)
}
if targetNode, ok := nodeByID[edge.TargetNodeID]; ok {
relatedNodes = appendLineageNodeUnique(relatedNodes, nodeSeen, targetNode)
consumers = appendLineageNodeUnique(consumers, consumerSeen, targetNode)
}
}
}
if artifact.ID == "" {
return nil, false
}
return &domain.ArtifactLineageRecord{
RunID: lineage.RunID,
WorkflowID: lineage.WorkflowID,
WorkflowName: lineage.WorkflowName,
ProjectID: lineage.ProjectID,
ProjectName: lineage.ProjectName,
ArtifactID: artifact.ID,
ArtifactName: artifact.Name,
Artifact: artifact,
Producer: producer,
Consumers: consumers,
UpstreamEdges: upstreamEdges,
DownstreamEdges: downstreamEdges,
Nodes: relatedNodes,
Edges: relatedEdges,
}, true
}
func cloneResourceSamples(items []domain.ResourceSample) []domain.ResourceSample {
if len(items) == 0 {
return []domain.ResourceSample{}
}
cloned := make([]domain.ResourceSample, len(items))
copy(cloned, items)
return cloned
}
func cloneNodeExecutionRecord(item domain.NodeExecutionRecord) domain.NodeExecutionRecord {
cloned := item
cloned.InputArtifacts = cloneExecutionArtifacts(item.InputArtifacts)
cloned.InputLineage = cloneInputLineage(item.InputLineage)
cloned.Logs = cloneLogEntries(item.Logs)
cloned.Artifacts = cloneExecutionArtifacts(item.Artifacts)
cloned.DataRefs = cloneDataRefs(item.DataRefs)
cloned.ResourceSeries = cloneResourceSamples(item.ResourceSeries)
return cloned
}
func rebuildRunAggregates(run *domain.RunRecord) {
if run == nil {
return
}
run.Logs = []domain.LogEntryRecord{}
run.ResourceSeries = []domain.ResourceSample{}
run.Artifacts = []domain.ExecutionArtifact{}
for _, execution := range run.NodeExecutions {
run.Logs = append(run.Logs, cloneLogEntries(execution.Logs)...)
run.ResourceSeries = append(run.ResourceSeries, cloneResourceSamples(execution.ResourceSeries)...)
run.Artifacts = append(run.Artifacts, cloneExecutionArtifacts(execution.Artifacts)...)
}
}
func firstNonEmptyText(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
return value
}
}
return ""
}
func buildRunLineage(run domain.RunRecord) domain.RunLineageRecord {
nodeLabels := make(map[string]string, len(run.NodeExecutions))
for _, execution := range run.NodeExecutions {
nodeLabels[execution.NodeID] = execution.NodeLabel
}
nodes := make([]domain.LineageNodeRecord, 0, len(run.NodeExecutions))
artifacts := make([]domain.ExecutionArtifact, 0, len(run.Artifacts))
edges := make([]domain.LineageEdgeRecord, 0)
seenEdges := map[string]struct{}{}
seenArtifacts := map[string]struct{}{}
for _, execution := range run.NodeExecutions {
inputArtifacts := cloneExecutionArtifacts(execution.InputArtifacts)
outputArtifacts := cloneExecutionArtifacts(execution.Artifacts)
for _, artifact := range outputArtifacts {
key := firstNonEmptyText(artifact.ID, artifact.Path, artifact.Name)
if key == "" {
continue
}
if _, exists := seenArtifacts[key]; exists {
continue
}
seenArtifacts[key] = struct{}{}
artifacts = append(artifacts, artifact)
}
nodes = append(nodes, domain.LineageNodeRecord{
NodeID: execution.NodeID,
NodeLabel: execution.NodeLabel,
OperatorID: execution.OperatorID,
OperatorVersion: execution.OperatorVersion,
OperatorImage: execution.OperatorImage,
OperatorImageID: execution.OperatorImageID,
Status: execution.Status,
StartedAt: execution.StartedAt,
EndedAt: execution.EndedAt,
StageManifestPath: execution.StageManifestPath,
InputArtifacts: inputArtifacts,
OutputArtifacts: outputArtifacts,
DataRefs: cloneDataRefs(execution.DataRefs),
})
if len(execution.InputLineage) > 0 {
for _, input := range execution.InputLineage {
artifact := input.Artifact
key := fmt.Sprintf("%s:%s:%s:%s:%s:%s", input.WorkflowEdgeID, input.SourceNodeID, input.SourceHandle, input.TargetHandle, artifact.ID, artifact.Name)
if _, exists := seenEdges[key]; exists {
continue
}
seenEdges[key] = struct{}{}
sourceNodeID := firstNonEmptyText(input.SourceNodeID, artifact.NodeID)
targetNodeID := firstNonEmptyText(input.TargetNodeID, execution.NodeID)
edges = append(edges, domain.LineageEdgeRecord{
ID: fmt.Sprintf("lineage-%d", len(edges)+1),
WorkflowEdgeID: input.WorkflowEdgeID,
SourceNodeID: sourceNodeID,
SourceNodeLabel: nodeLabels[sourceNodeID],
SourceHandle: input.SourceHandle,
SourceArtifactID: artifact.ID,
SourceArtifactName: artifact.Name,
SourcePortID: artifact.PortID,
SourcePortName: artifact.PortName,
TargetNodeID: targetNodeID,
TargetNodeLabel: nodeLabels[targetNodeID],
TargetHandle: input.TargetHandle,
Relation: "artifact_input",
})
}
continue
}
for _, artifact := range inputArtifacts {
key := fmt.Sprintf("%s:%s:%s:%s:%s", artifact.NodeID, artifact.ID, artifact.Name, artifact.PortID, execution.NodeID)
if _, exists := seenEdges[key]; exists {
continue
}
seenEdges[key] = struct{}{}
edges = append(edges, domain.LineageEdgeRecord{
ID: fmt.Sprintf("lineage-%d", len(edges)+1),
SourceNodeID: artifact.NodeID,
SourceNodeLabel: nodeLabels[artifact.NodeID],
SourceArtifactID: artifact.ID,
SourceArtifactName: artifact.Name,
SourcePortID: artifact.PortID,
SourcePortName: artifact.PortName,
TargetNodeID: execution.NodeID,
TargetNodeLabel: execution.NodeLabel,
Relation: "artifact_input",
})
}
}
return domain.RunLineageRecord{
RunID: run.ID,
WorkflowID: run.WorkflowID,
WorkflowName: run.WorkflowName,
ProjectID: run.ProjectID,
ProjectName: run.ProjectName,
Artifacts: artifacts,
Nodes: nodes,
Edges: edges,
}
}
func workflowScanSourceNode(node *domain.WorkflowNode) bool {
if node == nil {
return false
}
label := strings.TrimSpace(node.Data.Label)
return node.ID == "scan-source" || label == "BOS 增量扫描"
}
func sortSourceStatesDescending(items []domain.SourceStateRecord) {
sort.SliceStable(items, func(i int, j int) bool {
return items[i].UpdatedAt > items[j].UpdatedAt
})
}
func sourceStateFingerprintFromScanConfig(scanConfig *domain.ScanConfig) string {
if scanConfig == nil {
return ""
}
return domain.ComputeSourceFingerprint(domain.SourceFingerprintInput{
ScanMode: chooseWorkflowScanMode(scanConfig.ScanMode),
IncrementalUnit: chooseWorkflowIncrementalUnit(scanConfig.IncrementalUnit),
DirectoryDepth: chooseWorkflowDirectoryDepth(scanConfig.DirectoryDepth),
DataSource: chooseDataSource(scanConfig.DataSource),
SourcePath: scanConfig.SourcePath,
Bucket: scanConfig.Bucket,
Prefix: scanConfig.Prefix,
Pattern: scanConfig.Pattern,
Endpoint: scanConfig.Endpoint,
})
}
func (server *server) buildWorkflowNodeSourceStateResponse(ctx context.Context, workflow *domain.WorkflowRecord, nodeID string) (*workflowNodeSourceStateResponse, int, error) {
if workflow == nil {
return nil, http.StatusNotFound, nil
}
node := workflowNodeByID(workflow, nodeID)
if node == nil {
return nil, http.StatusNotFound, nil
}
response := &workflowNodeSourceStateResponse{
NodeID: nodeID,
Supported: workflowScanSourceNode(node),
}
if !response.Supported {
response.Reason = "当前节点没有可持久化的扫描状态"
return response, http.StatusOK, nil
}
resolvedScanConfig, err := server.resolveScanConfig(ctx, workflow.ScanConfig)
if err != nil {
return nil, http.StatusInternalServerError, err
}
if resolvedScanConfig == nil {
response.Reason = "当前工作流尚未配置扫描源"
return response, http.StatusOK, nil
}
response.ScanMode = chooseWorkflowScanMode(resolvedScanConfig.ScanMode)
response.IncrementalUnit = chooseWorkflowIncrementalUnit(resolvedScanConfig.IncrementalUnit)
response.DirectoryDepth = chooseWorkflowDirectoryDepth(resolvedScanConfig.DirectoryDepth)
response.DataSource = chooseDataSource(resolvedScanConfig.DataSource)
response.SourcePath = strings.TrimSpace(resolvedScanConfig.SourcePath)
response.Bucket = strings.TrimSpace(resolvedScanConfig.Bucket)
response.Prefix = strings.TrimSpace(resolvedScanConfig.Prefix)
response.Pattern = strings.TrimSpace(resolvedScanConfig.Pattern)
response.Stateful = response.ScanMode == "INCREMENTAL"
response.CurrentFingerprint = sourceStateFingerprintFromScanConfig(resolvedScanConfig)
versions, err := server.store.ListSourceStates(ctx, workflow.ID, nodeID)
if err != nil {
return nil, http.StatusInternalServerError, err
}
sortSourceStatesDescending(versions)
response.Versions = versions
for index := range versions {
if versions[index].SourceFingerprint == response.CurrentFingerprint {
state := versions[index]
response.Current = &state
break
}
}
if !response.Stateful {
response.Reason = "当前为 FULL 模式,不会读取或提交增量状态"
} else if response.Current == nil {
response.Reason = "当前配置下还没有已提交的增量状态"
}
return response, http.StatusOK, nil
}
func sanitizeTemplate(item *domain.TemplateRecord) *domain.TemplateRecord {
if item == nil {
return nil
}
copy := *item
if copy.DefinitionState == "" {
copy.DefinitionState = domain.WorkflowDefinitionStateDraft
}
if copy.DefinitionVersion <= 0 {
copy.DefinitionVersion = 1
}
if strings.TrimSpace(copy.DefinitionFamilyID) == "" {
copy.DefinitionFamilyID = copy.ID
}
copy.ExecutionScope = normalizeWorkflowExecutionScope(copy.ExecutionScope)
copy.Nodes = cloneWorkflowNodes(item.Nodes)
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
return &copy
}
func sanitizeTemplates(items []domain.TemplateRecord) []domain.TemplateRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.TemplateRecord, 0, len(items))
for _, item := range items {
copy := item
if copy.DefinitionState == "" {
copy.DefinitionState = domain.WorkflowDefinitionStateDraft
}
if copy.DefinitionVersion <= 0 {
copy.DefinitionVersion = 1
}
if strings.TrimSpace(copy.DefinitionFamilyID) == "" {
copy.DefinitionFamilyID = copy.ID
}
copy.ExecutionScope = normalizeWorkflowExecutionScope(copy.ExecutionScope)
copy.Nodes = cloneWorkflowNodes(item.Nodes)
copy.ScanConfig = sanitizeScanConfig(copy.ScanConfig)
sanitized = append(sanitized, copy)
}
return sanitized
}
func sanitizeOperator(item *domain.OperatorRecord) *domain.OperatorRecord {
if item == nil {
return nil
}
copy := *item
copy.GenerationSource = effectiveOperatorGenerationSource(copy)
copy.PublicationStatus = effectiveOperatorPublicationStatus(copy)
return &copy
}
func sanitizeOperators(items []domain.OperatorRecord) []domain.OperatorRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.OperatorRecord, 0, len(items))
for _, item := range items {
copy := item
sanitized = append(sanitized, copy)
}
return sanitized
}
func sanitizeUser(item *domain.UserRecord) *domain.UserRecord {
if item == nil {
return nil
}
copy := *item
copy.StoragePathFavorites = nil
copy.PasswordHash = ""
return &copy
}
func sanitizeUsers(items []domain.UserRecord) []domain.UserRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.UserRecord, 0, len(items))
for _, item := range items {
copy := item
copy.StoragePathFavorites = nil
copy.PasswordHash = ""
sanitized = append(sanitized, copy)
}
return sanitized
}
func sanitizeProject(item *domain.ProjectRecord) *domain.ProjectRecord {
if item == nil {
return nil
}
copy := *item
copy.StoragePathFavorites = nil
return &copy
}
func sanitizeProjects(items []domain.ProjectRecord) []domain.ProjectRecord {
if len(items) == 0 {
return items
}
sanitized := make([]domain.ProjectRecord, 0, len(items))
for _, item := range items {
copy := item
copy.StoragePathFavorites = nil
sanitized = append(sanitized, copy)
}
return sanitized
}
func NewHandler(dataStore store.Store, aiService ai.Service) http.Handler {
appConfig, err := config.LoadValidated()
if err != nil {
return invalidConfigurationHandler(err)
}
return NewHandlerWithConfig(dataStore, aiService, appConfig)
}
func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig config.AppConfig) http.Handler {
if aiService == nil {
aiService = ai.NewDisabledService("", "", "AI 服务未配置")
}
instance := &server{
store: dataStore,
ai: aiService,
operatorRunnerImage: appConfig.OperatorRunnerImage,
workerAPIBaseURL: appConfig.WorkerAPIBaseURL,
globalBOSEndpoint: appConfig.GlobalBOSEndpoint,
apiToken: appConfig.APIToken,
requireSessionAuth: appConfig.RequireSessionAuth,
sessionCookieSecure: appConfig.SessionCookieSecure,
objectSyncEnabled: appConfig.ObjectSyncEnabled,
trustedProxyNets: parseTrustedProxyCIDRs(appConfig.TrustedProxyCIDRs),
authLimiter: newRateLimiter(20, time.Minute),
externalWriteLimiter: newRateLimiter(120, time.Minute),
k8sNamespace: appConfig.K8sNamespace,
k8sImagePullSecretName: appConfig.K8sImagePullSecretName,
workspacePVCName: appConfig.WorkspacePVCName,
workspaceMountPath: appConfig.WorkspaceMountPath,
operatorJobTTLSeconds: appConfig.OperatorJobTTLSeconds,
customOperatorImageRegistry: appConfig.CustomOperatorImageRegistry,
customOperatorImageImporterImage: appConfig.CustomOperatorImageImporterImage,
customOperatorImageImportTimeoutSeconds: appConfig.CustomOperatorImageImportTimeoutSeconds,
customOperatorImageArchiveMaxMiB: appConfig.CustomOperatorImageArchiveMaxMiB,
buildCommit: appConfig.BuildCommit,
buildTime: appConfig.BuildTime,
image: appConfig.Image,
}
router := chi.NewRouter()
router.Use(middleware.Compress(5))
router.Use(instance.apiTokenAuth)
router.Use(instance.sessionAuth)
router.Use(instance.csrfProtection)
router.Get("/api/health", instance.handleHealth)
router.Get("/api/ready", instance.handleReady)
router.Get("/api/auth/bootstrap-status", instance.handleAuthBootstrapStatus)
router.Post("/api/auth/bootstrap-super-admin", instance.handleAuthBootstrapSuperAdmin)
router.Post("/api/auth/login", instance.handleAuthLogin)
router.Post("/api/auth/logout", instance.handleAuthLogout)
router.Get("/api/auth/me", instance.handleAuthMe)
router.Route("/api/admin", func(router chi.Router) {
router.Use(instance.requireSuperAdmin)
router.Get("/organizations", instance.handleAdminOrganizationsList)
router.Post("/organizations", instance.handleAdminOrganizationsCreate)
router.Put("/organizations/{id}", instance.handleAdminOrganizationsUpdate)
router.Delete("/organizations/{id}", instance.handleAdminOrganizationsDelete)
router.Get("/organizations/{id}/users", instance.handleAdminOrganizationUsersList)
router.Post("/organizations/{id}/users", instance.handleAdminOrganizationUsersCreate)
router.Put("/users/{id}", instance.handleAdminUserUpdate)
router.Delete("/users/{id}", instance.handleAdminUserDelete)
router.Post("/users/{id}/reset-password", instance.handleAdminUserResetPassword)
})
router.Get("/worker-api/health", instance.handleWorkerAPIHealth)
router.Get("/worker-api/ready", instance.handleWorkerAPIReady)
router.Get("/api/public/settings", instance.handlePublicSettingsGet)
router.Group(func(router chi.Router) {
router.Use(instance.requireWorkerAPIRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Get("/worker-api/stats", instance.handleWorkerAPIStats)
router.Get("/worker-api/metrics", instance.handleWorkerAPIMetrics)
router.Get("/worker-api/capacity", instance.handleWorkerAPICapacity)
router.Post("/worker-api/process-once", instance.handleWorkerAPIProcessOnce)
router.Post("/worker-api/runs/{id}/retry", instance.handleWorkerAPIRetry)
})
router.Get("/api/ai/status", instance.handleAIStatus)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
router.Post("/api/ai/operators/generate", instance.handleAIOperatorGenerate)
router.Post("/api/ai/workflows/generate", instance.handleAIWorkflowGenerate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requirePocAcceptanceImportAccess)
router.Post("/api/acceptance/poc-operator-result", instance.handlePocAcceptanceImport)
router.Post("/api/acceptance/poc-gpu-workflow", instance.handlePocGPUWorkflowAcceptance)
router.Post("/api/acceptance/poc-operator-workflows", instance.handlePocOperatorWorkflowAcceptance)
router.Post("/api/operators/image-archives/import", instance.handleOperatorImageArchiveImport)
})
router.With(instance.requireAdminMonitoringAccess).Get("/api/acceptance/poc-readiness", instance.handlePocAcceptanceReadiness)
router.Get("/api/dashboard", instance.handleDashboard)
router.Get("/api/plugins", instance.handlePluginsList)
router.Get("/api/organizations/{id}/plugins/{pluginId}", instance.handleOrganizationPluginGet)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Put("/api/organizations/{id}/plugins/{pluginId}", instance.handleOrganizationPluginUpdate)
})
router.Get("/api/human-batches", instance.handleHumanBatchesList)
router.Get("/api/human-items", instance.handleHumanItemsList)
router.Get("/api/human-items/{id}", instance.handleHumanItemGet)
router.Get("/api/human-items/{id}/subject-content", instance.handleHumanItemSubjectContent)
router.Get("/api/human-items/{id}/evidence/{artifactId}/content", instance.handleHumanItemEvidenceContent)
router.Get("/api/human-items/{id}/evidence/{artifactId}/members", instance.handleHumanItemEvidenceMembers)
router.Get("/api/human-items/{id}/evidence/{artifactId}/members/{memberId}/content", instance.handleHumanItemEvidenceMemberContent)
router.Get("/api/human-items/{id}/events", instance.handleHumanItemEvents)
router.Get("/api/human-rejects", instance.handleHumanRejectsList)
router.Get("/api/human-rejects/{id}", instance.handleHumanRejectGet)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleCutter, domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleQAReviewer, domain.UserRoleAuditor, domain.UserRoleOperator))
router.Post("/api/human-items/{id}/claim", instance.handleHumanItemClaim)
router.Post("/api/human-items/{id}/release", instance.handleHumanItemRelease)
router.Post("/api/human-items/{id}/submissions", instance.handleHumanItemSubmission)
router.Post("/api/human-items/{id}/decisions", instance.handleHumanItemDecision)
router.Post("/api/human-items/{id}/escalation", instance.handleHumanEscalationDecision)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Patch("/api/human-items/{id}/assignment", instance.handleHumanItemAssignment)
router.Post("/api/human-rejects/{id}/reopen", instance.handleHumanRejectReopen)
})
router.Get("/api/data-collection-tasks", instance.handleCollectionTasksList)
router.Get("/api/data-collection-tasks/{id}", instance.handleCollectionTaskGet)
router.Get("/api/data-operations/summary", instance.handleDataOperationsSummary)
router.Get("/api/data-operations/stage-items", instance.handleDataOperationsStageItems)
router.Get("/api/data-operations/training-usage", instance.handleDataOperationsTrainingUsage)
router.Get("/api/data-operations/datasets", instance.handleDataOperationsDatasets)
router.Get("/api/annotation-workgroups", deprecatedAnnotationEndpoint(instance.handleAnnotationWorkgroupsList))
router.Get("/api/annotation-memberships", deprecatedAnnotationEndpoint(instance.handleAnnotationMembershipsList))
router.Get("/api/annotation-templates", deprecatedAnnotationEndpoint(instance.handleAnnotationTemplatesList))
router.Get("/api/annotation-tasks", deprecatedAnnotationEndpoint(instance.handleAnnotationTasksList))
router.Get("/api/annotation-tasks/{id}", deprecatedAnnotationEndpoint(instance.handleAnnotationTaskGet))
router.Get("/api/annotation-workload-summary", deprecatedAnnotationEndpoint(instance.handleAnnotationWorkloadSummary))
router.Get("/api/plugins/annotation-qa/templates", instance.handleAnnotationTemplatesList)
router.Get("/api/plugins/annotation-qa/tasks", instance.handleAnnotationTasksList)
router.Get("/api/plugins/annotation-qa/tasks/{id}", instance.handleAnnotationTaskGet)
router.Get("/api/plugins/annotation-qa/workload-summary", instance.handleAnnotationWorkloadSummary)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleCutter, domain.UserRoleAnnotator, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Post("/api/data-collection-tasks", instance.handleCollectionTaskCreate)
router.Patch("/api/data-collection-tasks/{id}", instance.handleCollectionTaskUpdate)
router.Post("/api/data-collection-tasks/{id}/start", instance.handleCollectionTaskStart)
router.Post("/api/data-collection-tasks/{id}/complete", instance.handleCollectionTaskComplete)
router.Post("/api/annotation-tasks", deprecatedAnnotationEndpoint(instance.handleAnnotationTaskCreate))
router.Post("/api/annotation-tasks/{id}/revisions", deprecatedAnnotationEndpoint(instance.handleAnnotationRevisionCreate))
router.Post("/api/annotation-tasks/{id}/slice-revisions", deprecatedAnnotationEndpoint(instance.handleSliceRevisionCreate))
router.Post("/api/annotation-tasks/{id}/combined-submissions", deprecatedAnnotationEndpoint(instance.handleCombinedAnnotationSubmission))
router.Post("/api/plugins/annotation-qa/tasks", instance.handleAnnotationTaskCreate)
router.Post("/api/plugins/annotation-qa/tasks/{id}/slice-revisions", instance.handleSliceRevisionCreate)
router.Post("/api/plugins/annotation-qa/tasks/{id}/combined-submissions", instance.handleCombinedAnnotationSubmission)
router.Post("/api/plugins/annotation-qa/tasks/{id}/reject", instance.handleAnnotationTaskReject)
router.Post("/api/plugins/annotation-qa/tasks/{id}/revisions", instance.handleAnnotationRevisionCreate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
router.Post("/api/annotation-workgroups", deprecatedAnnotationEndpoint(instance.handleAnnotationWorkgroupCreate))
router.Post("/api/annotation-memberships", deprecatedAnnotationEndpoint(instance.handleAnnotationMembershipCreate))
router.Post("/api/annotation-templates", deprecatedAnnotationEndpoint(instance.handleAnnotationTemplateCreate))
router.Post("/api/annotation-templates/{id}/publish", deprecatedAnnotationEndpoint(instance.handleAnnotationTemplatePublish))
router.Post("/api/plugins/annotation-qa/templates", instance.handleAnnotationTemplateCreate)
router.Post("/api/plugins/annotation-qa/templates/{id}/publish", instance.handleAnnotationTemplatePublish)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleQAReviewer, domain.UserRoleAuditor, domain.UserRoleOperator))
router.Post("/api/annotation-tasks/{id}/reviews", deprecatedAnnotationEndpoint(instance.handleAnnotationReviewCreate))
router.Post("/api/plugins/annotation-qa/tasks/{id}/reviews", instance.handleAnnotationReviewCreate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Post("/api/data-operations/backfill", instance.handleDataOperationsBackfill)
})
router.Get("/api/projects", instance.handleProjectsList)
router.Get("/api/projects/{id}", instance.handleProjectsGet)
router.Get("/api/projects/{id}/members", instance.handleProjectMembersList)
router.Get("/api/projects/{id}/storage-path-favorites", instance.handleProjectStoragePathFavoritesGet)
router.Get("/api/projects/{id}/workflows", instance.handleProjectWorkflowsList)
router.Get("/api/projects/{id}/workflow-instances", instance.handleProjectWorkflowInstancesList)
router.Get("/api/workflows/{id}", instance.handleWorkflowsGet)
router.Get("/api/workflows/{id}/versions", instance.handleWorkflowVersionsList)
router.With(instance.requireBusinessRole(domain.UserRoleAdmin)).Get("/api/workflows/{id}/deletion-preview", instance.handleWorkflowDeletionPreview)
router.Get("/api/templates/{id}/releases", instance.handleTemplateReleasesList)
router.Get("/api/workflow-releases/{id}", instance.handleWorkflowReleaseGet)
router.With(instance.requireAdminMonitoringAccess).Get("/api/workflows/{id}/runs", instance.handleWorkflowRunsList)
router.Get("/api/workflows/{id}/capture-automation", instance.handleCaptureAutomationGet)
router.Get("/api/workflows/{id}/nodes/{nodeId}/source-state", instance.handleWorkflowNodeSourceStateGet)
router.Get("/api/scan-configs", instance.handleScanConfigsList)
router.Get("/api/scan-configs/{id}", instance.handleScanConfigsGet)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Post("/api/projects", instance.handleProjectsCreate)
router.Put("/api/projects/{id}", instance.handleProjectsUpdate)
router.Delete("/api/projects/{id}", instance.handleProjectsDelete)
router.Put("/api/projects/{id}/storage-path-favorites", instance.handleProjectStoragePathFavoritesUpdate)
router.Post("/api/projects/{id}/workflows", instance.handleProjectWorkflowsCreate)
router.Post("/api/projects/{id}/workflow-instances", instance.handleProjectWorkflowInstanceCreate)
router.Put("/api/workflows/{id}", instance.handleWorkflowsUpdate)
router.Post("/api/workflows/{id}/release", instance.handleWorkflowRelease)
router.Post("/api/workflows/{id}/versions", instance.handleWorkflowCreateVersion)
router.Post("/api/workflows/{id}/activate", instance.handleWorkflowActivateVersion)
router.Post("/api/workflows/{id}/archive", instance.handleWorkflowArchive)
router.Post("/api/workflows/{id}/restore", instance.handleWorkflowRestore)
router.Delete("/api/workflows/{id}", instance.handleWorkflowsDelete)
router.Delete("/api/workflows/{id}/nodes/{nodeId}/source-state", instance.handleWorkflowNodeSourceStateDelete)
router.Post("/api/workflows/{id}/run", instance.handleWorkflowRun)
router.Put("/api/workflows/{id}/capture-automation", instance.handleCaptureAutomationUpdate)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/reprocess", instance.handleCaptureRecordReprocess)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/discard", instance.handleCaptureRecordDiscard)
router.Post("/api/workflows/{id}/capture-automation/records/{recordId}/restore", instance.handleCaptureRecordRestore)
router.Post("/api/workflow-releases/{id}/runs", instance.handleWorkflowReleaseRun)
router.Post("/api/scan-configs", instance.handleScanConfigsCreate)
router.Put("/api/scan-configs/{id}", instance.handleScanConfigsUpdate)
router.Delete("/api/scan-configs/{id}", instance.handleScanConfigsDelete)
router.Post("/api/scan-configs/{id}/run", instance.handleScanConfigsRun)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleOrganizationAdmin))
router.Post("/api/suppliers", instance.handleSupplierCreate)
router.Post("/api/projects/{id}/members", instance.handleProjectMemberCreate)
router.Patch("/api/projects/{id}/members/{memberId}", instance.handleProjectMemberUpdate)
router.Delete("/api/projects/{id}/members/{memberId}", instance.handleProjectMemberDelete)
router.Post("/api/projects/{id}/members/bulk", instance.handleProjectMembersBulk)
})
router.Get("/api/suppliers", instance.handleSuppliersList)
router.Get("/api/source-data/collections", instance.handleSourceCollectionsList)
router.Get("/api/source-data/collections/{runId}/{collectionId}", instance.handleSourceCollectionDetail)
router.Get("/api/source-data/collections/{runId}/{collectionId}/entries", instance.handleSourceCollectionEntries)
router.Get("/api/source-data/collections/{runId}/{collectionId}/content", instance.handleSourceCollectionContent)
router.Get("/api/packages", instance.handlePackagesList)
router.Get("/api/packages/{runId}/{packageName}", instance.handlePackageDetail)
router.Get("/api/packages/{runId}/{packageName}/content", instance.handlePackageContent)
router.Get("/api/delivery-packages", instance.handleDeliveryPackagesList)
router.Get("/api/delivery-packages/{id}", instance.handleDeliveryPackageGet)
router.Get("/api/delivery-packages/{id}/contents", instance.handleDeliveryPackageContents)
router.Get("/api/delivery-packages/{id}/artifacts/{artifactId}/content", instance.handleDeliveryPackageArtifactContent)
router.Head("/api/delivery-packages/{id}/artifacts/{artifactId}/content", instance.handleDeliveryPackageArtifactContent)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer))
router.Post("/api/delivery-packages/{id}/claim", instance.handleDeliveryPackageClaim)
router.Post("/api/delivery-packages/{id}/release", instance.handleDeliveryPackageRelease)
router.Post("/api/delivery-packages/{id}/decisions", instance.handleDeliveryPackageDecision)
})
router.Get("/api/sampling-tasks", instance.handleSamplingTasksList)
router.Get("/api/sampling-tasks/{id}", instance.handleSamplingTaskGet)
router.Get("/api/templates", instance.handleTemplatesList)
router.Get("/api/templates/{id}", instance.handleTemplatesGet)
router.Get("/api/operators", instance.handleOperatorsList)
router.Get("/api/operators/{id}", instance.handleOperatorsGet)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleQualityReviewer, domain.UserRoleQAReviewer, domain.UserRoleOperator))
router.Post("/api/sampling-items/{id}/review", instance.handleSamplingItemReview)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
router.Post("/api/templates", instance.handleTemplatesCreate)
router.Post("/api/templates/{id}/publish", instance.handleTemplatePublish)
router.Put("/api/templates/{id}", instance.handleTemplatesUpdate)
router.Delete("/api/templates/{id}", instance.handleTemplatesDelete)
router.Post("/api/operators", instance.handleOperatorsCreate)
router.Put("/api/operators/{id}", instance.handleOperatorsUpdate)
router.Delete("/api/operators/{id}", instance.handleOperatorsDelete)
router.Get("/api/operators/{id}/lightweight-files", instance.handleOperatorLightweightFilesList)
router.Patch("/api/operators/{id}/lightweight-files", instance.handleOperatorLightweightFileMove)
router.Delete("/api/operators/{id}/lightweight-files", instance.handleOperatorLightweightFileDelete)
router.Get("/api/operators/{id}/lightweight-files/content", instance.handleOperatorLightweightFileContentGet)
router.Put("/api/operators/{id}/lightweight-files/content", instance.handleOperatorLightweightFileContentPut)
router.Post("/api/operators/{id}/lightweight-files/directories", instance.handleOperatorLightweightDirectoryCreate)
router.Post("/api/operators/{id}/lightweight-files/upload", instance.handleOperatorLightweightFileUpload)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Get("/api/database", instance.handleDatabaseOverview)
router.Get("/api/database/collections/{name}/indexes", instance.handleDatabaseCollectionIndexes)
router.Get("/api/database/collections/{name}/documents", instance.handleDatabaseCollectionDocuments)
router.Post("/api/database/collections", instance.handleDatabaseCollectionCreate)
router.Patch("/api/database/collections/{name}", instance.handleDatabaseCollectionRename)
router.Delete("/api/database/collections/{name}", instance.handleDatabaseCollectionDelete)
router.Post("/api/database/collections/{name}/indexes", instance.handleDatabaseCollectionIndexCreate)
router.Delete("/api/database/collections/{name}/indexes/{indexName}", instance.handleDatabaseCollectionIndexDelete)
router.Post("/api/database/collections/{name}/documents", instance.handleDatabaseDocumentCreate)
router.Put("/api/database/collections/{name}/documents/{documentId}", instance.handleDatabaseDocumentReplace)
router.Delete("/api/database/collections/{name}/documents/{documentId}", instance.handleDatabaseDocumentDelete)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireAdminMonitoringAccess)
router.Get("/api/runs", instance.handleRunsList)
router.Get("/api/runs/{id}", instance.handleRunsGet)
router.Get("/api/runs/{id}/logs", instance.handleRunsLogs)
router.Get("/api/runs/{id}/resources", instance.handleRunsResources)
router.Get("/api/runs/{id}/artifacts", instance.handleRunsArtifacts)
router.Get("/api/runs/{id}/lineage", instance.handleRunsLineage)
router.Get("/api/artifacts/{artifactId}/lineage", instance.handleArtifactLineage)
router.Get("/api/runs/{id}/artifacts/{artifactId}/members", instance.handleRunArtifactMembers)
router.Get("/api/runs/{id}/artifacts/{artifactId}/members/{memberId}/content", instance.handleRunArtifactMemberContent)
router.Head("/api/runs/{id}/artifacts/{artifactId}/members/{memberId}/content", instance.handleRunArtifactMemberContent)
router.Get("/api/runs/{id}/artifacts/{artifactId}/content", instance.handleRunsArtifactContent)
router.Head("/api/runs/{id}/artifacts/{artifactId}/content", instance.handleRunsArtifactContent)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Post("/api/runs/{id}/rerun-subtree", instance.handleRunsRerunSubtree)
router.Post("/api/runs/{id}/stop", instance.handleRunsStop)
router.Post("/api/runs/{id}/force-stop", instance.handleRunsForceStop)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleQAReviewer, domain.UserRoleComplianceAuditor, domain.UserRoleAuditor))
router.Get("/api/audit-tasks", instance.handleAuditTasksList)
router.Post("/api/audit-tasks/{id}/approve", instance.handleAuditTaskApprove)
router.Post("/api/audit-tasks/{id}/reject", instance.handleAuditTaskReject)
})
router.Get("/api/users/{id}/storage-path-favorites", instance.handleUserStoragePathFavoritesGet)
router.Put("/api/users/{id}/storage-path-favorites", instance.handleUserStoragePathFavoritesUpdate)
router.Put("/api/users/me", instance.handleCurrentUserProfileUpdate)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Get("/api/users", instance.handleUsersList)
router.Post("/api/users", instance.handleUsersCreate)
router.Put("/api/users/{id}", instance.handleUsersUpdate)
router.Delete("/api/users/{id}", instance.handleUsersDelete)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Get("/api/storage/connections", instance.handleStorageConnectionsList)
router.Get("/api/storage/connections/{id}", instance.handleStorageConnectionsGet)
router.Get("/api/mongo/connections", instance.handleMongoConnectionsList)
router.Get("/api/mongo/connections/{id}", instance.handleMongoConnectionsGet)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Post("/api/storage/connections/test", instance.handleStorageConnectionTest)
router.Post("/api/storage/connections", instance.handleStorageConnectionsCreate)
router.Put("/api/storage/connections/{id}", instance.handleStorageConnectionsUpdate)
router.Delete("/api/storage/connections/{id}", instance.handleStorageConnectionsDelete)
router.Post("/api/mongo/connections/test", instance.handleMongoConnectionsTest)
router.Post("/api/mongo/connections", instance.handleMongoConnectionsCreate)
router.Put("/api/mongo/connections/{id}", instance.handleMongoConnectionsUpdate)
router.Delete("/api/mongo/connections/{id}", instance.handleMongoConnectionsDelete)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Get("/api/external/business-db/tokens", instance.handleExternalBusinessWriteTokensList)
router.Post("/api/external/business-db/tokens", instance.handleExternalBusinessWriteTokensCreate)
router.Put("/api/external/business-db/tokens/{id}", instance.handleExternalBusinessWriteTokensUpdate)
router.Get("/api/external/business-db/tokens/{id}/reveal", instance.handleExternalBusinessWriteTokensReveal)
router.Post("/api/external/business-db/tokens/{id}/rotate", instance.handleExternalBusinessWriteTokensRotate)
router.Delete("/api/external/business-db/tokens/{id}", instance.handleExternalBusinessWriteTokensDelete)
})
router.Route("/api/external/business-db", func(router chi.Router) {
router.Use(instance.externalBusinessWriteAuth)
router.Post("/documents", instance.handleExternalBusinessWriteCreate)
router.Patch("/documents/{documentId}", instance.handleExternalBusinessWritePatch)
router.Post("/upserts", instance.handleExternalBusinessWriteUpsert)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Get("/api/settings", instance.handleSettingsGet)
router.Put("/api/settings", instance.handleSettingsUpdate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleDataOperator, domain.UserRoleOperator))
router.Get("/api/storage/buckets", instance.handleStorageBuckets)
router.Get("/api/storage/objects", instance.handleStorageObjects)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Get("/api/object-storage", instance.handleObjectStorageOverview)
router.Get("/api/object-storage/objects", instance.handleObjectStorageObjects)
router.Get("/api/object-storage/content", instance.handleObjectStorageContent)
router.Head("/api/object-storage/content", instance.handleObjectStorageContent)
router.Post("/api/object-storage/buckets", instance.handleObjectStorageBucketCreate)
router.Delete("/api/object-storage/buckets/{name}", instance.handleObjectStorageBucketDelete)
router.Post("/api/object-storage/objects", instance.handleObjectStorageObjectCreate)
router.Post("/api/object-storage/objects/upload", instance.handleObjectStorageObjectUpload)
router.Post("/api/object-storage/uploads/presign-put", instance.handleObjectStoragePresignPut)
router.Get("/api/object-storage/upload-sessions", instance.handleObjectStorageUploadSessionsList)
router.Post("/api/object-storage/upload-sessions", instance.handleObjectStorageUploadSessionCreate)
router.Get("/api/object-storage/upload-sessions/{sessionId}", instance.handleObjectStorageUploadSessionGet)
router.Post("/api/object-storage/upload-sessions/{sessionId}/pause", instance.handleObjectStorageUploadSessionPause)
router.Post("/api/object-storage/upload-sessions/{sessionId}/resume", instance.handleObjectStorageUploadSessionResume)
router.Post("/api/object-storage/upload-sessions/{sessionId}/cancel", instance.handleObjectStorageUploadSessionCancel)
router.Post("/api/object-storage/upload-sessions/{sessionId}/items/{itemId}/start", instance.handleObjectStorageUploadSessionItemStart)
router.Post("/api/object-storage/upload-sessions/{sessionId}/items/{itemId}/part-url", instance.handleObjectStorageUploadSessionPartURL)
router.Post("/api/object-storage/upload-sessions/{sessionId}/items/{itemId}/parts", instance.handleObjectStorageUploadSessionPartComplete)
router.Post("/api/object-storage/upload-sessions/{sessionId}/items/{itemId}/complete", instance.handleObjectStorageUploadSessionItemComplete)
router.Post("/api/object-storage/upload-sessions/{sessionId}/items/{itemId}/failed", instance.handleObjectStorageUploadSessionItemFailure)
router.Post("/api/object-storage/uploads/batch/prepare", instance.handleObjectStorageBatchUploadPrepare)
router.Post("/api/object-storage/uploads/multipart/initiate", instance.handleObjectStorageMultipartInitiate)
router.Post("/api/object-storage/uploads/multipart/part-url", instance.handleObjectStorageMultipartPartURL)
router.Post("/api/object-storage/uploads/multipart/complete", instance.handleObjectStorageMultipartComplete)
router.Post("/api/object-storage/uploads/multipart/abort", instance.handleObjectStorageMultipartAbort)
router.Patch("/api/object-storage/objects", instance.handleObjectStorageObjectRename)
router.Delete("/api/object-storage/objects", instance.handleObjectStorageObjectDelete)
router.Post("/api/object-storage/objects/restore", instance.handleObjectStorageObjectRestore)
})
router.Route("/api/object-sync", func(router chi.Router) {
router.Use(instance.requireObjectSyncFeature)
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleDataOperator, domain.UserRolePipelineEngineer, domain.UserRoleComplianceAuditor, domain.UserRoleAuditor, domain.UserRoleOperator))
router.Get("/spaces", instance.handleObjectSyncSpacesList)
router.Get("/sessions", instance.handleObjectSyncSessionsList)
router.Get("/sessions/{id}", instance.handleObjectSyncSessionGet)
router.Get("/sessions/{id}/items", instance.handleObjectSyncSessionItems)
router.Get("/client-releases", instance.handleObjectSyncClientReleasesList)
router.Get("/client-releases/{id}/artifacts/{platform}", instance.handleObjectSyncClientReleaseDownload)
router.Head("/client-releases/{id}/artifacts/{platform}", instance.handleObjectSyncClientReleaseDownload)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin))
router.Use(instance.requireObjectSyncIdempotency)
router.Post("/spaces", instance.handleObjectSyncSpaceCreate)
router.Put("/spaces/{id}", instance.handleObjectSyncSpaceUpdate)
router.Post("/spaces/{id}/capability-probe", instance.handleObjectSyncCapabilityProbe)
router.Post("/spaces/{id}/reconcile", instance.handleObjectSyncReconcileCreate)
router.Get("/devices", instance.handleObjectSyncDevicesList)
router.Get("/device-authorizations", instance.handleObjectSyncDeviceApprovalRemoved)
router.Get("/device-authorizations/by-code", instance.handleObjectSyncDeviceApprovalRemoved)
router.Post("/device-authorizations/{id}/{decision}", instance.handleObjectSyncDeviceApprovalRemoved)
router.Get("/credentials", instance.handleObjectSyncCredentialsList)
router.Post("/credentials", instance.handleObjectSyncCredentialCreate)
router.Post("/credentials/{id}/{action}", instance.handleObjectSyncCredentialMutation)
router.Post("/sessions/{id}/admin-abort", instance.handleObjectSyncSessionAdminAbort)
router.Post("/leases/{id}/force-release", instance.handleObjectSyncLeaseForceRelease)
router.Post("/items/{id}/force-overwrite", instance.handleObjectSyncItemForceOverwrite)
router.Post("/client-releases", instance.handleObjectSyncClientReleaseUpsert)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator))
router.Get("/releases", instance.handleObjectSyncReleasesList)
router.With(instance.requireObjectSyncIdempotency).Post("/releases", instance.handleObjectSyncReleaseCreate)
})
router.Group(func(router chi.Router) {
router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleComplianceAuditor, domain.UserRoleAuditor))
router.Get("/audit-events", instance.handleObjectSyncAuditList)
router.Get("/audit-events/export", instance.handleObjectSyncAuditExport)
router.Get("/audit-seals/{day}", instance.handleObjectSyncAuditSealGet)
})
})
router.Route("/api/external/object-sync", func(router chi.Router) {
router.Use(instance.requireObjectSyncFeature)
router.With(instance.objectSyncDistributedRateLimit(30, time.Minute), instance.requireObjectSyncIdempotency).Post("/device-authorizations", instance.handleObjectSyncDeviceApprovalRemoved)
router.With(instance.objectSyncDistributedRateLimit(30, time.Minute), instance.requireObjectSyncIdempotency).Post("/device-authorizations/token", instance.handleObjectSyncDeviceApprovalRemoved)
router.With(instance.objectSyncDistributedRateLimit(30, time.Minute), instance.requireObjectSyncIdempotency).Post("/auth/token", instance.handleExternalObjectSyncAccessKeyAuth)
router.Group(func(router chi.Router) {
router.Use(instance.objectSyncCredentialAuth)
router.Use(instance.objectSyncDistributedRateLimit(600, time.Minute))
router.Use(instance.requireObjectSyncIdempotency)
router.Get("/capabilities", instance.handleExternalObjectSyncCapabilities)
router.Post("/sessions", instance.handleExternalObjectSyncSessionCreate)
router.Post("/sessions/{sessionId}/manifest-pages", instance.handleExternalObjectSyncManifestPage)
router.Post("/sessions/{sessionId}/plan", instance.handleExternalObjectSyncPlan)
router.Get("/sessions/{sessionId}/status", instance.handleExternalObjectSyncSessionStatus)
router.Get("/sessions/{sessionId}/items", instance.handleExternalObjectSyncSessionItems)
router.Post("/sessions/{sessionId}/{action}", instance.handleExternalObjectSyncSessionFinish)
router.Post("/items/{itemId}/reserve", instance.handleExternalObjectSyncItemReserve)
router.Post("/items/{itemId}/uploads/direct", instance.handleExternalObjectSyncDirectUpload)
router.Post("/items/{itemId}/uploads/direct/complete", instance.handleExternalObjectSyncDirectUploadComplete)
router.Post("/items/{itemId}/uploads/multipart", instance.handleExternalObjectSyncMultipartInitiate)
router.Post("/items/{itemId}/uploads/multipart/part-url", instance.handleExternalObjectSyncMultipartPartURL)
router.Post("/items/{itemId}/uploads/multipart/parts", instance.handleExternalObjectSyncMultipartPartCheckpoint)
router.Post("/items/{itemId}/uploads/multipart/complete", instance.handleExternalObjectSyncMultipartComplete)
router.Post("/items/{itemId}/heartbeat", instance.handleExternalObjectSyncHeartbeat)
router.Post("/items/{itemId}/abort", instance.handleExternalObjectSyncItemAbort)
router.Post("/items/{itemId}/commit", instance.handleExternalObjectSyncCommit)
router.Get("/client-updates", instance.handleExternalObjectSyncUpdateCheck)
})
})
return router
}
func invalidConfigurationHandler(err error) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
renderError(writer, http.StatusInternalServerError, err)
})
}
func (server *server) serviceMetadata() map[string]any {
return map[string]any{
"service": "droboticflow-api",
"buildCommit": server.buildCommit,
"buildTime": server.buildTime,
"image": server.image,
}
}
func (server *server) handleHealth(writer http.ResponseWriter, _ *http.Request) {
response := server.serviceMetadata()
response["status"] = "ok"
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleReady(writer http.ResponseWriter, request *http.Request) {
ctx, cancel := context.WithTimeout(request.Context(), 2*time.Second)
defer cancel()
response := server.serviceMetadata()
if err := server.store.Ready(ctx); err != nil {
response["status"] = "not_ready"
response["ready"] = false
response["checks"] = map[string]any{
"store": map[string]string{"status": "failed", "error": err.Error()},
}
renderJSON(writer, http.StatusServiceUnavailable, response)
return
}
response["status"] = "ready"
response["ready"] = true
response["checks"] = map[string]any{
"store": map[string]string{"status": "ok"},
}
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleAIStatus(writer http.ResponseWriter, request *http.Request) {
renderJSON(writer, http.StatusOK, server.aiStatusWithOperatorGeneration(request.Context()))
}
type aiGenerateRequest struct {
Description string `json:"description"`
}
func (server *server) handleAIOperatorGenerate(writer http.ResponseWriter, request *http.Request) {
var payload aiGenerateRequest
if !decodeJSON(writer, request, &payload) {
return
}
if strings.TrimSpace(payload.Description) == "" {
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
return
}
settings, err := server.store.GetSettings(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("load settings: %w", err))
return
}
if settings == nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("load settings: settings unavailable"))
return
}
aiStatus := server.resolveAIService(request.Context()).Status()
operatorStatus := operatorGenerationStatus(settings, aiStatus)
if !operatorStatus.Ready {
renderJSON(writer, http.StatusServiceUnavailable, map[string]any{
"error": operatorStatus.Reason,
"missingFields": operatorStatus.MissingFields,
})
return
}
item, err := server.resolveAIService(request.Context()).GenerateOperator(request.Context(), payload.Description)
if err != nil {
renderAIError(writer, err)
return
}
ai.ApplyOperatorGenerationSettings(item, settings.OperatorGeneration)
renderJSON(writer, http.StatusOK, item)
}
func (server *server) handleAIWorkflowGenerate(writer http.ResponseWriter, request *http.Request) {
var payload aiGenerateRequest
if !decodeJSON(writer, request, &payload) {
return
}
if strings.TrimSpace(payload.Description) == "" {
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
return
}
operators, err := server.listAvailableOperators(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
operators = filterAIPublishedWorkflowOperators(operators)
if len(operators) == 0 {
renderValidationError(writer, validationIssue{Field: "operators", Message: "at least one operator is required to generate a workflow"})
return
}
item, err := server.resolveAIService(request.Context()).GenerateWorkflow(request.Context(), payload.Description, operators)
if err != nil {
renderAIError(writer, err)
return
}
if issues := validateWorkflowGraphTopology(item.Nodes, item.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
renderJSON(writer, http.StatusOK, item)
}
func filterAIPublishedWorkflowOperators(items []domain.OperatorRecord) []domain.OperatorRecord {
filtered := make([]domain.OperatorRecord, 0, len(items))
for _, item := range items {
if effectiveOperatorGenerationSource(item) == domain.OperatorGenerationSourceAI &&
effectiveOperatorPublicationStatus(item) != domain.OperatorPublicationStatusPublished {
continue
}
filtered = append(filtered, item)
}
return filtered
}
func (server *server) handleDashboard(writer http.ResponseWriter, request *http.Request) {
from, to, rangeErr := dashboardWorkforceRange(time.Now(), request.URL.Query().Get("from"), request.URL.Query().Get("to"))
if rangeErr != nil {
renderValidationError(writer, validationIssue{Field: "range", Message: rangeErr.Error()})
return
}
principal, _ := currentPrincipal(request)
if server.tenantEnforced(request) && principal != nil && principal.User.Role != domain.UserRoleAdmin {
summary := domain.DashboardSummary{}
if err := server.populateDashboardWorkforce(request.Context(), server.requestOrganizationID(request), principal, from, to, &summary); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"audienceScope": summary.AudienceScope,
"workforce": summary.Workforce,
})
return
}
if server.tenantEnforced(request) {
projects, err := server.listProjectsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
operators, err := server.listOperatorsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
runSummaries, err := server.listRunSummariesForRequest(request.Context(), request, "")
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
auditTasks, err := server.listAuditTasksForRequest(request.Context(), request, "all")
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
settings, err := server.store.GetSettings(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
summary := store.BuildDashboardSummaryAt(projects, operators, runSummaries, auditTasks, settings.Timezone, time.Now())
enrichDashboardOperatorInventory(&summary, operators)
server.enrichDashboardHumanWork(request.Context(), request, &summary)
if err := server.populateDashboardWorkforce(request.Context(), server.requestOrganizationID(request), principal, from, to, &summary); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
server.enrichDashboardPOC(request.Context(), request, &summary)
renderJSON(writer, http.StatusOK, summary)
return
}
summary, err := server.store.GetDashboardSummary(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
operators, err := server.listOperatorsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
enrichDashboardOperatorInventory(&summary, operators)
server.enrichDashboardHumanWork(request.Context(), request, &summary)
if err := server.populateDashboardWorkforce(request.Context(), server.requestOrganizationID(request), principal, from, to, &summary); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
server.enrichDashboardPOC(request.Context(), request, &summary)
renderJSON(writer, http.StatusOK, summary)
}
func (server *server) enrichDashboardHumanWork(ctx context.Context, request *http.Request, summary *domain.DashboardSummary) {
if summary == nil {
return
}
organizationID := server.requestOrganizationID(request)
items, err := server.store.ListHumanItems(ctx, organizationID, domain.HumanItemQuery{})
if err == nil {
accepted := 0
decided := 0
seenVideos := map[string]struct{}{}
for _, item := range items {
if key := strings.TrimSpace(item.Subject.ItemKey); key != "" {
seenVideos[key] = struct{}{}
}
if duration, ok := item.Subject.Payload["durationSeconds"].(float64); ok && duration > 0 {
summary.Data.CaptureDurationSeconds += int64(duration)
}
switch item.Status {
case domain.HumanItemReady, domain.HumanItemRework:
if item.CurrentStageID == "quality_review" || item.CurrentStageID == domain.FanyuV5StageCutReview || item.CurrentStageID == domain.FanyuV5StageSemanticReview {
summary.Tasks.PendingQuality++
} else {
summary.Tasks.PendingAnnotation++
}
case domain.HumanItemClaimed, domain.HumanItemInProgress:
summary.Tasks.InAnnotation++
case domain.HumanItemSubmitted:
if item.CurrentStageID == "quality_review" || item.CurrentStageID == domain.FanyuV5StageCutReview || item.CurrentStageID == domain.FanyuV5StageSemanticReview {
summary.Tasks.PendingQuality++
} else if item.CurrentStageID == "acceptance" {
summary.Tasks.PendingAcceptance++
}
case domain.HumanItemAccepted:
accepted++
decided++
case domain.HumanItemRejected:
summary.Tasks.Rejected++
decided++
case domain.HumanItemEscalated:
summary.Tasks.Escalated++
}
}
summary.Data.VideoCount = len(seenVideos)
if decided > 0 {
summary.Tasks.QualityPassRate = float64(accepted) * 100 / float64(decided)
}
}
packages, err := server.store.ListDeliveryPackages(ctx, organizationID, domain.DeliveryPackageQuery{Mode: domain.RunModeProduction})
if err == nil {
summary.Data.PackageCount = len(packages)
for _, item := range packages {
if item.Status == domain.DeliveryPackagePendingAcceptance {
summary.Tasks.PendingAcceptance++
}
}
}
summary.Tasks.Waiting = summary.Tasks.PendingAnnotation + summary.Tasks.PendingQuality + summary.Tasks.PendingAcceptance
}
func enrichDashboardOperatorInventory(summary *domain.DashboardSummary, operators []domain.OperatorRecord) {
if summary == nil {
return
}
var catalog []struct {
ID string `json:"id"`
}
if err := json.Unmarshal(builtinOperatorCatalogJSON, &catalog); err != nil {
return
}
currentBuiltinIDs := make(map[string]struct{}, len(catalog))
for _, item := range catalog {
if id := strings.TrimSpace(item.ID); id != "" {
currentBuiltinIDs[id] = struct{}{}
}
}
summary.Operators.Total = len(operators)
summary.Operators.Builtin = 0
summary.Operators.Custom = 0
summary.Operators.Historical = 0
for _, operator := range operators {
if _, current := currentBuiltinIDs[strings.TrimSpace(operator.ID)]; current {
summary.Operators.Builtin++
continue
}
if strings.EqualFold(strings.TrimSpace(operator.Type), "builtin") {
summary.Operators.Historical++
continue
}
summary.Operators.Custom++
}
}
func (server *server) handlePocAcceptanceReadiness(writer http.ResponseWriter, request *http.Request) {
renderJSON(writer, http.StatusOK, server.dashboardPOCReadiness(request.Context(), request))
}
const (
pocGPUWorkflowAcceptanceProjectName = "POC GPU Workflow Acceptance"
pocGPUWorkflowAcceptanceWorkflowName = "POC GPU Acceptance Workflow"
pocGPUWorkflowExpectedLogSubstring = "gpu request cannot be scheduled"
liveWorkflowAcceptanceProjectName = "Real Source Acceptance Project"
liveWorkflowAcceptanceWorkflowName = "Real Source Acceptance Workflow"
)
type pocGPUWorkflowAcceptanceRequest struct {
SourcePath string `json:"sourcePath"`
}
func hasControlCharacter(value string) bool {
return strings.ContainsFunc(value, func(character rune) bool {
return character < 0x20 || character == 0x7f
})
}
func cleanWorkspaceMountPath(value string) string {
mountPath := path.Clean(strings.TrimSpace(value))
if mountPath == "." || !strings.HasPrefix(mountPath, "/") {
return "/workspace"
}
return mountPath
}
func validatePocGPUWorkflowSourcePath(value string, workspaceMountPath string) (string, *validationIssue) {
sourcePath := strings.TrimSpace(value)
if sourcePath == "" {
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath is required"}
}
if hasControlCharacter(sourcePath) {
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must not contain control characters"}
}
if !strings.HasPrefix(sourcePath, "/") {
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must be an absolute path inside the workspace mount"}
}
cleanedSourcePath := path.Clean(sourcePath)
workspaceMount := cleanWorkspaceMountPath(workspaceMountPath)
if cleanedSourcePath != workspaceMount && !strings.HasPrefix(cleanedSourcePath, workspaceMount+"/") {
return "", &validationIssue{Field: "sourcePath", Message: "sourcePath must stay inside the configured workspace mount"}
}
return cleanedSourcePath, nil
}
func (server *server) defaultPocGPUWorkflowSourcePath() string {
return path.Join(cleanWorkspaceMountPath(server.workspaceMountPath), "datasets", "fixtures", "poc-operator-acceptance", "xplatform")
}
func pocGPUWorkflowNodes(sourcePath string) ([]domain.WorkflowNode, []domain.WorkflowEdge) {
sourceNode := domain.WorkflowNode{
ID: "source-1",
Type: "operator",
Data: domain.WorkflowNodeData{
Label: "真实数据源",
Category: "IO",
Description: "Read a real POC fixture path for GPU workflow acceptance.",
Outputs: []domain.PortDefinition{
{ID: "out-1", Name: "manifest", Type: domain.PortTypeJSON},
},
Hyperparams: []domain.HyperParam{
{Key: "DATA_SOURCE", Value: "LOCAL"},
{Key: "SOURCE_PATH", Value: sourcePath},
},
GPU: "1",
},
Position: domain.Position{X: 0, Y: 0},
}
uploadNode := domain.WorkflowNode{
ID: "upload-1",
Type: "operator",
Data: domain.WorkflowNodeData{
Label: "结果上传",
Category: "IO",
Description: "Persist artifacts produced by upstream nodes.",
Inputs: []domain.PortDefinition{
{ID: "in-1", Name: "manifest", Type: domain.PortTypeJSON},
},
Outputs: []domain.PortDefinition{},
},
Position: domain.Position{X: 240, Y: 0},
}
edge := domain.WorkflowEdge{
ID: "edge-source-upload",
Source: sourceNode.ID,
SourceHandle: "out-1",
Target: uploadNode.ID,
TargetHandle: "in-1",
}
return []domain.WorkflowNode{sourceNode, uploadNode}, []domain.WorkflowEdge{edge}
}
func (server *server) ensurePocGPUWorkflowAcceptanceProject(ctx context.Context, organizationID string) (*domain.ProjectRecord, error) {
projects, err := server.store.ListProjectsByOrganization(ctx, organizationID)
if err != nil {
return nil, err
}
for index := range projects {
project := projects[index]
if strings.TrimSpace(project.Name) == pocGPUWorkflowAcceptanceProjectName {
return &project, nil
}
}
return server.store.CreateProject(ctx, domain.CreateProjectInput{
OrganizationID: organizationID,
Name: pocGPUWorkflowAcceptanceProjectName,
Description: "Created by the bounded POC GPU workflow acceptance endpoint.",
})
}
func (server *server) ensurePocGPUWorkflowAcceptanceWorkflow(ctx context.Context, project *domain.ProjectRecord, sourcePath string) (*domain.WorkflowRecord, error) {
nodes, edges := pocGPUWorkflowNodes(sourcePath)
workflows, err := server.store.ListWorkflowsByProject(ctx, project.ID)
if err != nil {
return nil, err
}
for index := range workflows {
workflow := workflows[index]
if strings.TrimSpace(workflow.Name) != pocGPUWorkflowAcceptanceWorkflowName {
continue
}
active, err := server.store.HasActiveRun(ctx, workflow.ID)
if err != nil {
return nil, err
}
if active {
return nil, store.ErrWorkflowRunActive
}
workflow.OrganizationID = normalizedTenantID(project.OrganizationID)
workflow.ProjectID = project.ID
workflow.Description = "Queues a real GPU-requesting workflow run for POC GPU acceptance."
workflow.Type = domain.WorkflowTypeBatch
workflow.Kind = domain.WorkflowKindStandard
workflow.NodeTimeoutSeconds = 60
workflow.RunTimeoutSeconds = 120
workflow.MaxRetryAttempts = 0
workflow.RetryBackoffSeconds = 0
workflow.Nodes = nodes
workflow.Edges = edges
workflow.Status = domain.WorkflowStatusDraft
workflow.UpdatedAt = nowISO()
if err := server.store.UpsertWorkflow(ctx, workflow); err != nil {
return nil, err
}
return &workflow, nil
}
return server.store.CreateWorkflow(ctx, domain.CreateWorkflowInput{
OrganizationID: normalizedTenantID(project.OrganizationID),
ProjectID: project.ID,
Name: pocGPUWorkflowAcceptanceWorkflowName,
Description: "Queues a real GPU-requesting workflow run for POC GPU acceptance.",
Type: domain.WorkflowTypeBatch,
Kind: domain.WorkflowKindStandard,
NodeTimeoutSeconds: 60,
RunTimeoutSeconds: 120,
MaxRetryAttempts: 0,
RetryBackoffSeconds: 0,
Nodes: nodes,
Edges: edges,
})
}
func (server *server) handlePocGPUWorkflowAcceptance(writer http.ResponseWriter, request *http.Request) {
payload := pocGPUWorkflowAcceptanceRequest{}
if request.ContentLength != 0 {
if !decodeJSON(writer, request, &payload) {
return
}
}
sourcePath, issue := validatePocGPUWorkflowSourcePath(firstNonEmptyText(payload.SourcePath, server.defaultPocGPUWorkflowSourcePath()), server.workspaceMountPath)
if issue != nil {
renderValidationError(writer, *issue)
return
}
organizationID := server.requestOrganizationID(request)
project, err := server.ensurePocGPUWorkflowAcceptanceProject(request.Context(), organizationID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
workflow, err := server.ensurePocGPUWorkflowAcceptanceWorkflow(request.Context(), project, sourcePath)
if err != nil {
if errors.Is(err, store.ErrWorkflowRunActive) {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "验收工作流已有进行中的运行",
"code": "workflow_run_active",
})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
run, err := server.createRunWithLightweightManifests(request.Context(), workflow, domain.RunTriggerManual)
if err != nil {
if errors.Is(err, store.ErrWorkflowRunActive) {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "验收工作流已有进行中的运行",
"code": "workflow_run_active",
})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusAccepted, map[string]any{
"project": sanitizeProject(project),
"workflow": sanitizeWorkflow(workflow),
"run": runForResponse(request, *run),
"sourcePath": sourcePath,
})
}
type dashboardAcceptancePayload map[string]any
func dashboardGate(status, label, message string, evidence map[string]any) domain.DashboardGate {
return domain.DashboardGate{Status: status, Label: label, Message: message, Evidence: evidence}
}
func dashboardString(value any) string {
if text, ok := value.(string); ok {
return strings.TrimSpace(text)
}
return ""
}
func dashboardBool(value any) bool {
boolean, _ := value.(bool)
return boolean
}
func dashboardInt(value any) int {
switch number := value.(type) {
case int:
if number > 0 {
return number
}
case int32:
if number > 0 {
return int(number)
}
case int64:
if number > 0 {
return int(number)
}
case float64:
if number > 0 {
return int(number)
}
case json.Number:
integer, err := strconv.Atoi(number.String())
if err == nil && integer > 0 {
return integer
}
}
return 0
}
func dashboardStringList(value any) []string {
reflected := reflect.ValueOf(value)
if !reflected.IsValid() || (reflected.Kind() != reflect.Slice && reflected.Kind() != reflect.Array) {
return []string{}
}
result := make([]string, 0, reflected.Len())
seen := map[string]struct{}{}
for index := 0; index < reflected.Len(); index++ {
text := dashboardString(reflected.Index(index).Interface())
if text == "" {
continue
}
if _, exists := seen[text]; exists {
continue
}
seen[text] = struct{}{}
result = append(result, text)
}
return result
}
func dashboardStringListContains(items []string, target string) bool {
for _, item := range items {
if strings.EqualFold(strings.TrimSpace(item), target) {
return true
}
}
return false
}
func dashboardMap(value any) map[string]any {
item, _ := value.(map[string]any)
return item
}
func dashboardAcceptanceCandidate(value any) (dashboardAcceptancePayload, bool) {
payload := dashboardMap(value)
if len(payload) == 0 {
return nil, false
}
if _, ok := payload["fullExternalAcceptance"].(bool); !ok {
return nil, false
}
if dashboardString(payload["acceptanceScope"]) == "" || dashboardInt(payload["operatorCount"]) == 0 {
return nil, false
}
return dashboardAcceptancePayload(payload), true
}
func dashboardAcceptancePayloadFromArtifact(artifact domain.ExecutionArtifact) (dashboardAcceptancePayload, bool) {
data := artifact.Data
if len(data) == 0 {
return nil, false
}
candidates := []any{
dashboardMap(dashboardMap(data["preview"])["value"]),
data["value"],
data,
}
for _, candidate := range candidates {
if payload, ok := dashboardAcceptanceCandidate(candidate); ok {
return payload, true
}
}
name := path.Base(strings.TrimSpace(artifact.Name))
if name == "" || name == "." {
name = path.Base(strings.TrimSpace(artifact.Path))
}
if name == "poc-operator-full-acceptance-result.json" {
return dashboardAcceptanceCandidate(data)
}
return nil, false
}
func dashboardLatestAcceptancePayload(runs []domain.RunRecord) (dashboardAcceptancePayload, domain.RunRecord, bool) {
var latestPayload dashboardAcceptancePayload
var latestRun domain.RunRecord
latestKey := ""
for _, run := range runs {
for _, artifact := range run.Artifacts {
payload, ok := dashboardAcceptancePayloadFromArtifact(artifact)
if !ok {
continue
}
key := strings.TrimSpace(run.UpdatedAt)
if key == "" {
key = strings.TrimSpace(run.CreatedAt)
}
if latestPayload == nil || key >= latestKey {
latestPayload = payload
latestRun = run
latestKey = key
}
}
}
return latestPayload, latestRun, latestPayload != nil
}
const dashboardAcceptanceFreshnessWindow = 24 * time.Hour
type dashboardAcceptanceFreshness struct {
Status string
StaleReason string
UnknownReason string
SourceCommit string
CatalogHash string
CatalogCount int
CompletedAt string
CurrentSourceCommit string
CurrentCatalogHash string
CurrentCatalogCount int
}
func dashboardFirstString(values ...any) string {
for _, value := range values {
if text := dashboardString(value); text != "" {
return text
}
}
return ""
}
func dashboardFirstInt(values ...any) int {
for _, value := range values {
if number := dashboardInt(value); number > 0 {
return number
}
}
return 0
}
func normalizedCatalogHash(value string) string {
value = strings.ToLower(strings.TrimSpace(value))
return strings.TrimPrefix(value, "sha256:")
}
func sourceCommitsMatch(left string, right string) bool {
left = strings.ToLower(strings.TrimSpace(left))
right = strings.ToLower(strings.TrimSpace(right))
if left == "" || right == "" {
return false
}
if left == right {
return true
}
if len(left) >= 7 && len(right) >= 7 {
return strings.HasPrefix(left, right) || strings.HasPrefix(right, left)
}
return false
}
func evaluateDashboardAcceptanceFreshness(
payload dashboardAcceptancePayload,
run domain.RunRecord,
currentSourceCommit string,
currentCatalogHash string,
currentCatalogCount int,
now time.Time,
) dashboardAcceptanceFreshness {
importSource := dashboardMap(payload["importSource"])
sourcePayload := dashboardMap(payload["source"])
staticPayload := dashboardMap(payload["static"])
result := dashboardAcceptanceFreshness{
Status: "unknown",
SourceCommit: dashboardFirstString(payload["sourceCommit"], importSource["sourceCommit"], sourcePayload["sourceCommit"], staticPayload["sourceCommit"]),
CatalogHash: dashboardFirstString(payload["catalogHash"], importSource["catalogHash"], sourcePayload["catalogHash"], staticPayload["catalogHash"]),
CatalogCount: dashboardFirstInt(payload["catalogCount"], importSource["catalogCount"], sourcePayload["catalogCount"], staticPayload["catalogCount"], staticPayload["operatorFileCount"], payload["operatorCount"]),
CompletedAt: dashboardFirstString(payload["completedAt"], importSource["completedAt"], sourcePayload["completedAt"], payload["endedAt"], run.EndedAt, run.UpdatedAt),
CurrentSourceCommit: strings.TrimSpace(currentSourceCommit),
CurrentCatalogHash: strings.TrimSpace(currentCatalogHash),
CurrentCatalogCount: currentCatalogCount,
}
staleReasons := make([]string, 0, 4)
unknownReasons := make([]string, 0, 4)
if result.CompletedAt == "" {
unknownReasons = append(unknownReasons, "completed_at_missing")
} else if completedAt, err := time.Parse(time.RFC3339Nano, result.CompletedAt); err != nil {
unknownReasons = append(unknownReasons, "completed_at_invalid")
} else if completedAt.After(now.Add(5 * time.Minute)) {
unknownReasons = append(unknownReasons, "completed_at_in_future")
} else if now.Sub(completedAt) > dashboardAcceptanceFreshnessWindow {
staleReasons = append(staleReasons, "acceptance_older_than_24h")
}
if result.SourceCommit == "" {
unknownReasons = append(unknownReasons, "source_commit_missing")
} else if result.CurrentSourceCommit == "" {
unknownReasons = append(unknownReasons, "current_source_commit_unknown")
} else if !sourceCommitsMatch(result.SourceCommit, result.CurrentSourceCommit) {
staleReasons = append(staleReasons, "source_commit_mismatch")
}
if normalizedCatalogHash(result.CatalogHash) == "" {
unknownReasons = append(unknownReasons, "catalog_hash_missing")
} else if normalizedCatalogHash(result.CurrentCatalogHash) == "" {
unknownReasons = append(unknownReasons, "current_catalog_hash_unknown")
} else if normalizedCatalogHash(result.CatalogHash) != normalizedCatalogHash(result.CurrentCatalogHash) {
staleReasons = append(staleReasons, "catalog_hash_mismatch")
}
if result.CatalogCount <= 0 {
unknownReasons = append(unknownReasons, "catalog_count_missing")
} else if result.CurrentCatalogCount <= 0 {
unknownReasons = append(unknownReasons, "current_catalog_count_unknown")
} else if result.CatalogCount != result.CurrentCatalogCount {
staleReasons = append(staleReasons, "catalog_count_mismatch")
}
if len(staleReasons) > 0 {
result.Status = "stale"
result.StaleReason = strings.Join(staleReasons, ",")
return result
}
if len(unknownReasons) > 0 {
result.UnknownReason = strings.Join(unknownReasons, ",")
return result
}
result.Status = "fresh"
return result
}
func dashboardFreshnessEvidence(freshness dashboardAcceptanceFreshness) map[string]any {
return map[string]any{
"freshness": freshness.Status,
"staleReason": freshness.StaleReason,
"unknownReason": freshness.UnknownReason,
"sourceCommit": freshness.SourceCommit,
"catalogHash": freshness.CatalogHash,
"catalogCount": freshness.CatalogCount,
"completedAt": freshness.CompletedAt,
"currentSourceCommit": freshness.CurrentSourceCommit,
"currentCatalogHash": freshness.CurrentCatalogHash,
"currentCatalogCount": freshness.CurrentCatalogCount,
}
}
func dashboardStrictAcceptancePassed(payload dashboardAcceptancePayload) bool {
requiredExternals := dashboardStringList(payload["requiredExternals"])
externalPassed := dashboardStringList(payload["externalVerificationPassed"])
externalMissing := dashboardStringList(payload["externalVerificationMissing"])
operatorCount := dashboardInt(payload["operatorCount"])
return dashboardString(payload["status"]) == "passed" &&
dashboardBool(payload["fullExternalAcceptance"]) &&
dashboardString(payload["acceptanceScope"]) == "full" &&
dashboardStringListContains(requiredExternals, "bos") &&
dashboardStringListContains(requiredExternals, "mongo") &&
len(externalMissing) == 0 &&
dashboardStringListContains(externalPassed, "bos") &&
dashboardStringListContains(externalPassed, "mongo") &&
operatorCount > 0 &&
dashboardInt(payload["operatorPassedCount"]) == operatorCount &&
dashboardInt(payload["coveredOperatorCount"]) == operatorCount &&
dashboardInt(payload["failedCount"]) == 0 &&
dashboardInt(payload["requiredSkippedCount"]) == 0 &&
dashboardBool(payload["allOperatorCoverageComplete"]) &&
dashboardBool(payload["blockingOperatorCoverageComplete"]) &&
len(dashboardStringList(payload["missingCoverage"])) == 0 &&
len(dashboardStringList(payload["blockingMissingCoverage"])) == 0 &&
dashboardBool(payload["operatorRepositoryCoverageComplete"]) &&
payload["operatorRepositoryPolicyClean"] != false &&
dashboardBool(payload["allFixturesReal"]) &&
dashboardInt(payload["generatedFixtureCount"]) == 0 &&
dashboardInt(payload["realFixtureCount"]) > 0
}
func (server *server) dashboardGPUCapacityGate(ctx context.Context) domain.DashboardGate {
baseURL := server.resolvedWorkerAPIBaseURL()
if baseURL == "" {
return dashboardGate("unknown", "GPU 能力未知", "Worker API 未配置,无法读取 GPU 调度能力。", nil)
}
requestCtx, cancel := context.WithTimeout(ctx, workerAPIDashboardTimeout)
defer cancel()
request, err := http.NewRequestWithContext(requestCtx, http.MethodGet, baseURL+"/worker-api/capacity", nil)
if err != nil {
return dashboardGate("unknown", "GPU 能力未知", err.Error(), nil)
}
response, err := workerAPIProxyClient.Do(request)
if err != nil {
return dashboardGate("unknown", "GPU 能力读取失败", err.Error(), nil)
}
defer response.Body.Close()
if response.StatusCode < 200 || response.StatusCode >= 300 {
return dashboardGate("unknown", "GPU 能力读取失败", fmt.Sprintf("Worker capacity returned HTTP %d", response.StatusCode), map[string]any{"statusCode": response.StatusCode})
}
var payload struct {
GPU k8s.GPUCapacitySnapshot `json:"gpu"`
}
if err := json.NewDecoder(response.Body).Decode(&payload); err != nil {
return dashboardGate("unknown", "GPU 能力解析失败", err.Error(), nil)
}
evidence := map[string]any{
"resourceName": payload.GPU.ResourceName,
"available": payload.GPU.Available,
"schedulableAllocatable": payload.GPU.SchedulableAllocatable,
"maxAllocatable": payload.GPU.MaxAllocatable,
"schedulableNodeCount": payload.GPU.SchedulableNodeCount,
"nodeCount": len(payload.GPU.Nodes),
"selectorSummary": payload.GPU.SelectorSummary,
"checkedAt": payload.GPU.CheckedAt,
}
if payload.GPU.Available && payload.GPU.SchedulableAllocatable > 0 && payload.GPU.MaxAllocatable > 0 {
return dashboardGate("passed", "GPU 可调度", fmt.Sprintf("%d 个节点可调度,单节点最多 %d 张 GPU。", payload.GPU.SchedulableNodeCount, payload.GPU.MaxAllocatable), evidence)
}
return dashboardGate("blocked", "POC 无可调度 GPU", fmt.Sprintf("已检查 %d 个节点,当前没有可调度 %s。", len(payload.GPU.Nodes), payload.GPU.ResourceName), evidence)
}
func runTimestampKey(run domain.RunRecord) string {
key := strings.TrimSpace(run.UpdatedAt)
if key == "" {
key = strings.TrimSpace(run.EndedAt)
}
if key == "" {
key = strings.TrimSpace(run.CreatedAt)
}
return key
}
func latestPocGPUWorkflowRun(runs []domain.RunRecord) (domain.RunRecord, bool) {
var latest domain.RunRecord
latestKey := ""
found := false
for _, run := range runs {
if !isPocGPUWorkflowAcceptanceRun(run) {
continue
}
key := runTimestampKey(run)
if !found || key >= latestKey {
latest = run
latestKey = key
found = true
}
}
return latest, found
}
func isPocGPUWorkflowAcceptanceRun(run domain.RunRecord) bool {
workflowName := strings.TrimSpace(run.WorkflowName)
projectName := strings.TrimSpace(run.ProjectName)
return workflowName == pocGPUWorkflowAcceptanceWorkflowName ||
projectName == pocGPUWorkflowAcceptanceProjectName ||
workflowName == liveWorkflowAcceptanceWorkflowName ||
projectName == liveWorkflowAcceptanceProjectName
}
func runLogMessages(run domain.RunRecord) []string {
messages := make([]string, 0, len(run.Logs)+len(run.NodeExecutions))
for _, entry := range run.Logs {
messages = append(messages, strings.TrimSpace(entry.Message))
}
for _, execution := range run.NodeExecutions {
for _, entry := range execution.Logs {
messages = append(messages, strings.TrimSpace(entry.Message))
}
if strings.TrimSpace(execution.Error) != "" {
messages = append(messages, strings.TrimSpace(execution.Error))
}
}
if strings.TrimSpace(run.Error) != "" {
messages = append(messages, strings.TrimSpace(run.Error))
}
return messages
}
func logMessagesContain(messages []string, substring string) bool {
substring = strings.TrimSpace(substring)
if substring == "" {
return false
}
for _, message := range messages {
if strings.Contains(message, substring) {
return true
}
}
return false
}
func dashboardGPUWorkflowPreflightGate(runs []domain.RunRecord) domain.DashboardGate {
run, found := latestPocGPUWorkflowRun(runs)
if !found {
return dashboardGate(
"unknown",
"GPU workflow 预检未验收",
"还没有找到 POC GPU workflow preflight run。请运行 npm run acceptance:poc-gpu-workflow。",
nil,
)
}
messages := runLogMessages(run)
evidence := map[string]any{
"runId": run.ID,
"workflowId": run.WorkflowID,
"workflowName": run.WorkflowName,
"projectId": run.ProjectID,
"projectName": run.ProjectName,
"status": string(run.Status),
"updatedAt": run.UpdatedAt,
"expectedLogSubstring": pocGPUWorkflowExpectedLogSubstring,
"logCount": len(messages),
}
hasExpectedLog := logMessagesContain(messages, pocGPUWorkflowExpectedLogSubstring)
if run.Status == domain.RunStatusCompleted {
return dashboardGate(
"passed",
"GPU workflow 已验收",
"最新 POC GPU workflow run 已由 worker 真实处理并完成。",
evidence,
)
}
if run.Status == domain.RunStatusFailed && hasExpectedLog {
return dashboardGate(
"passed",
"GPU workflow 预检已验收",
"最新 POC GPU workflow run 已由 worker 真实处理,并在创建 Kubernetes Job 前因 GPU 不可调度失败。",
evidence,
)
}
if run.Status == domain.RunStatusQueued || run.Status == domain.RunStatusRunning {
return dashboardGate(
"unknown",
"GPU workflow 预检运行中",
"最新 POC GPU workflow preflight run 还未到达终态。",
evidence,
)
}
return dashboardGate(
"blocked",
"GPU workflow 预检未通过",
"最新 POC GPU workflow preflight run 没有留下预期的 GPU 不可调度失败证据。",
evidence,
)
}
func dashboardStrictAcceptanceGate(payload dashboardAcceptancePayload, found bool, freshness dashboardAcceptanceFreshness) domain.DashboardGate {
if !found {
return dashboardGate("unknown", "未导入严格验收", "还没有找到 POC strict acceptance 结果 artifact。", nil)
}
evidence := map[string]any{
"status": dashboardString(payload["status"]),
"acceptanceScope": dashboardString(payload["acceptanceScope"]),
"fullExternalAcceptance": dashboardBool(payload["fullExternalAcceptance"]),
"preflightFailure": dashboardBool(payload["preflightFailure"]),
"externalMissing": dashboardStringList(payload["externalVerificationMissing"]),
"blockingMissingCoverage": dashboardStringList(payload["blockingMissingCoverage"]),
}
for key, value := range dashboardFreshnessEvidence(freshness) {
evidence[key] = value
}
if freshness.Status == "stale" {
return dashboardGate("unknown", "严格验收证据已过期", "最新 POC acceptance artifact 已过期或与当前代码/算子目录不一致,不能作为当前阻塞或通过依据。", evidence)
}
if freshness.Status != "fresh" {
return dashboardGate("unknown", "严格验收新鲜度未知", "最新 POC acceptance artifact 缺少完整的提交、算子目录或完成时间证据。", evidence)
}
if dashboardStrictAcceptancePassed(payload) {
return dashboardGate("passed", "严格验收通过", "完整外部依赖、真实 fixture、算子仓库策略和算子覆盖均已通过。", evidence)
}
if dashboardBool(payload["preflightFailure"]) || dashboardString(payload["status"]) == "failed" {
return dashboardGate("blocked", "严格验收被预检阻塞", strings.Join(dashboardStringList(payload["failures"]), ";"), evidence)
}
return dashboardGate("blocked", "严格验收未完成", "最新 POC acceptance 结果还不能证明完整外部验收通过。", evidence)
}
func dashboardBusinessAcceptancePassed(payload dashboardAcceptancePayload) bool {
requiredExternals := dashboardStringList(payload["requiredExternals"])
externalPassed := dashboardStringList(payload["externalVerificationPassed"])
return dashboardString(payload["status"]) == "passed" &&
dashboardBool(payload["fullExternalAcceptance"]) &&
dashboardString(payload["acceptanceScope"]) == "full" &&
dashboardStringListContains(requiredExternals, "bos") &&
dashboardStringListContains(requiredExternals, "mongo") &&
len(dashboardStringList(payload["externalVerificationMissing"])) == 0 &&
dashboardStringListContains(externalPassed, "bos") &&
dashboardStringListContains(externalPassed, "mongo") &&
dashboardBool(payload["allFixturesReal"]) &&
dashboardInt(payload["generatedFixtureCount"]) == 0 &&
dashboardInt(payload["realFixtureCount"]) > 0
}
func dashboardBusinessAcceptanceGate(payload dashboardAcceptancePayload, found bool, freshness dashboardAcceptanceFreshness) domain.DashboardGate {
if !found {
return dashboardGate("unknown", "当前业务验收未知", "还没有当前版本的完整业务验收 artifact。", nil)
}
evidence := dashboardFreshnessEvidence(freshness)
evidence["status"] = dashboardString(payload["status"])
evidence["externalMissing"] = dashboardStringList(payload["externalVerificationMissing"])
if freshness.Status != "fresh" {
return dashboardGate("unknown", "当前业务验收待刷新", "验收 artifact 不是当前代码和算子目录的新鲜证据。", evidence)
}
if dashboardBusinessAcceptancePassed(payload) {
return dashboardGate("passed", "当前业务验收通过", "当前版本的外部依赖和真实 fixture 验收均已通过。", evidence)
}
return dashboardGate("blocked", "当前业务验收未通过", "当前版本仍有外部依赖、fixture 或预检项未通过。", evidence)
}
func dashboardOperatorCoveragePassed(payload dashboardAcceptancePayload, currentCatalogCount int) bool {
operatorCount := dashboardInt(payload["operatorCount"])
return currentCatalogCount > 0 &&
operatorCount == currentCatalogCount &&
dashboardInt(payload["operatorPassedCount"]) == operatorCount &&
dashboardInt(payload["coveredOperatorCount"]) == operatorCount &&
dashboardInt(payload["failedCount"]) == 0 &&
dashboardInt(payload["requiredSkippedCount"]) == 0 &&
dashboardBool(payload["allOperatorCoverageComplete"]) &&
dashboardBool(payload["blockingOperatorCoverageComplete"]) &&
len(dashboardStringList(payload["missingCoverage"])) == 0 &&
len(dashboardStringList(payload["blockingMissingCoverage"])) == 0 &&
dashboardBool(payload["operatorRepositoryCoverageComplete"]) &&
payload["operatorRepositoryPolicyClean"] != false
}
func dashboardOperatorCoverageGate(payload dashboardAcceptancePayload, found bool, freshness dashboardAcceptanceFreshness) domain.DashboardGate {
if !found {
return dashboardGate("unknown", "算子覆盖未知", "还没有当前算子目录的覆盖验收 artifact。", nil)
}
evidence := dashboardFreshnessEvidence(freshness)
evidence["operatorCount"] = dashboardInt(payload["operatorCount"])
evidence["coveredOperatorCount"] = dashboardInt(payload["coveredOperatorCount"])
evidence["missingCoverage"] = dashboardStringList(payload["missingCoverage"])
if freshness.Status != "fresh" {
return dashboardGate("unknown", "算子覆盖待刷新", "验收 artifact 与当前算子目录不一致或新鲜度未知。", evidence)
}
if dashboardOperatorCoveragePassed(payload, freshness.CurrentCatalogCount) {
return dashboardGate("passed", "算子覆盖完整", "当前内置算子目录已完整执行并通过仓库策略检查。", evidence)
}
return dashboardGate("blocked", "算子覆盖不完整", "当前内置算子目录仍有未覆盖、失败或仓库策略问题。", evidence)
}
func appendDashboardAction(actions []string, action string) []string {
action = strings.TrimSpace(action)
if action == "" {
return actions
}
for _, existing := range actions {
if existing == action {
return actions
}
}
return append(actions, action)
}
func dashboardOverallPOCStatus(poc domain.DashboardPOCReadiness) (string, string, string) {
gates := []domain.DashboardGate{
poc.ServiceAvailability,
poc.GPUCapacity,
poc.GPUWorkflowPreflight,
poc.BusinessAcceptance,
poc.OperatorCoverage,
}
unknown := false
for _, gate := range gates {
if gate.Status == "blocked" {
return "blocked", "POC 验收阻塞", "服务、GPU、当前业务验收或算子覆盖仍有未完成 gate。"
}
if gate.Status != "passed" {
unknown = true
}
}
if unknown {
return "unknown", "POC 验收待确认", "当前缺少完整的 POC gate 证据。"
}
return "passed", "POC 验收通过", "服务、GPU、当前业务验收和算子覆盖 gate 均已通过。"
}
func (server *server) dashboardServiceAvailabilityGate(status string, message string) domain.DashboardGate {
evidence := map[string]any{
"buildCommit": server.buildCommit,
"buildTime": server.buildTime,
"image": server.image,
}
if status == "passed" {
return dashboardGate("passed", "服务可用", message, evidence)
}
return dashboardGate("blocked", "服务不可用", message, evidence)
}
func normalizeDashboardPOCReadinessLists(poc *domain.DashboardPOCReadiness) {
if poc.PreflightStages == nil {
poc.PreflightStages = []string{}
}
if poc.RequiredExternals == nil {
poc.RequiredExternals = []string{}
}
if poc.ExternalVerificationPassed == nil {
poc.ExternalVerificationPassed = []string{}
}
if poc.ExternalVerificationMissing == nil {
poc.ExternalVerificationMissing = []string{}
}
if poc.MissingCoverage == nil {
poc.MissingCoverage = []string{}
}
if poc.BlockingMissingCoverage == nil {
poc.BlockingMissingCoverage = []string{}
}
if poc.NextActions == nil {
poc.NextActions = []string{}
}
}
func (server *server) dashboardPOCReadiness(ctx context.Context, request *http.Request) domain.DashboardPOCReadiness {
runs, err := server.listRunsForRequest(ctx, request, "")
if err != nil {
gpu := server.dashboardGPUCapacityGate(ctx)
poc := domain.DashboardPOCReadiness{
Status: "unknown",
Label: "POC 验收待确认",
Message: "无法读取 runs 中的 POC acceptance artifact:" + err.Error(),
Freshness: "unknown",
ServiceAvailability: server.dashboardServiceAvailabilityGate("blocked", "API 无法读取运行存储:"+err.Error()),
GPUCapacity: gpu,
BusinessAcceptance: dashboardGate("unknown", "当前业务验收未知", "无法读取 POC acceptance artifact。", nil),
OperatorCoverage: dashboardGate("unknown", "算子覆盖未知", "无法读取 POC acceptance artifact。", nil),
GPU: gpu,
GPUWorkflowPreflight: dashboardGate(
"unknown",
"GPU workflow 预检未知",
"无法读取 runs 中的 POC GPU workflow preflight 证据。",
nil,
),
StrictAcceptance: dashboardGate("unknown", "未导入严格验收", "无法读取 POC acceptance artifact。", nil),
NextActions: []string{"检查 API 存储后重试 dashboard。"},
}
normalizeDashboardPOCReadinessLists(&poc)
return poc
}
payload, latestRun, found := dashboardLatestAcceptancePayload(runs)
currentCatalogHash, currentCatalogCount, catalogErr := builtinOperatorCatalogFingerprint()
if catalogErr != nil {
currentCatalogHash = ""
currentCatalogCount = 0
}
freshness := dashboardAcceptanceFreshness{
Status: "unknown",
UnknownReason: "acceptance_artifact_missing",
CurrentSourceCommit: server.buildCommit,
CurrentCatalogHash: currentCatalogHash,
CurrentCatalogCount: currentCatalogCount,
}
if found {
freshness = evaluateDashboardAcceptanceFreshness(payload, latestRun, server.buildCommit, currentCatalogHash, currentCatalogCount, time.Now())
if catalogErr != nil && freshness.Status != "stale" {
freshness.Status = "unknown"
freshness.UnknownReason = "current_catalog_unavailable"
}
}
gpu := server.dashboardGPUCapacityGate(ctx)
poc := domain.DashboardPOCReadiness{
Freshness: freshness.Status,
StaleReason: freshness.StaleReason,
CurrentSourceCommit: freshness.CurrentSourceCommit,
CurrentCatalogHash: freshness.CurrentCatalogHash,
CurrentCatalogCount: freshness.CurrentCatalogCount,
ServiceAvailability: server.dashboardServiceAvailabilityGate("passed", "API 已成功读取当前运行、配置和验收证据。"),
GPUCapacity: gpu,
BusinessAcceptance: dashboardBusinessAcceptanceGate(payload, found, freshness),
OperatorCoverage: dashboardOperatorCoverageGate(payload, found, freshness),
GPU: gpu,
GPUWorkflowPreflight: dashboardGPUWorkflowPreflightGate(runs),
StrictAcceptance: dashboardStrictAcceptanceGate(payload, found, freshness),
}
if found {
poc.SourceCommit = freshness.SourceCommit
poc.CatalogHash = freshness.CatalogHash
poc.CatalogCount = freshness.CatalogCount
poc.CompletedAt = freshness.CompletedAt
poc.LatestAcceptanceRunID = latestRun.ID
poc.LatestAcceptanceUpdatedAt = latestRun.UpdatedAt
poc.LatestAcceptanceStatus = dashboardString(payload["status"])
poc.AcceptanceScope = dashboardString(payload["acceptanceScope"])
poc.FullExternalAcceptance = dashboardBool(payload["fullExternalAcceptance"])
poc.PreflightFailure = dashboardBool(payload["preflightFailure"])
poc.PreflightStages = dashboardStringList(payload["preflightStages"])
poc.RequiredExternals = dashboardStringList(payload["requiredExternals"])
poc.ExternalVerificationPassed = dashboardStringList(payload["externalVerificationPassed"])
poc.ExternalVerificationMissing = dashboardStringList(payload["externalVerificationMissing"])
poc.MissingCoverage = dashboardStringList(payload["missingCoverage"])
poc.BlockingMissingCoverage = dashboardStringList(payload["blockingMissingCoverage"])
poc.OperatorRepositoryCount = dashboardInt(payload["operatorRepositoryCount"])
poc.OperatorRepositoryRequiredCount = dashboardInt(payload["operatorRepositoryRequiredCount"])
poc.OperatorRepositoryCoverageComplete = dashboardBool(payload["operatorRepositoryCoverageComplete"])
poc.OperatorRepositoryPolicyClean = payload["operatorRepositoryPolicyClean"] != false
poc.RealFixtureCount = dashboardInt(payload["realFixtureCount"])
poc.GeneratedFixtureCount = dashboardInt(payload["generatedFixtureCount"])
poc.AllFixturesReal = dashboardBool(payload["allFixturesReal"])
}
poc.Status, poc.Label, poc.Message = dashboardOverallPOCStatus(poc)
if poc.GPU.Status == "blocked" {
poc.NextActions = appendDashboardAction(poc.NextActions, "为 POC 增加 GPU 节点池并安装 NVIDIA device plugin/runtime 后重跑 npm run acceptance:gpu。")
if poc.GPUWorkflowPreflight.Status != "passed" {
poc.NextActions = appendDashboardAction(poc.NextActions, "运行 npm run acceptance:poc-gpu-workflow,确认无 GPU 时 worker 会在创建 Job 前失败并留下日志证据。")
}
}
if poc.StrictAcceptance.Status != "passed" {
poc.NextActions = appendDashboardAction(poc.NextActions, "导入或重跑完整 POC strict acceptance,确保 bos,mongo、真实 fixture、算子覆盖均通过。")
}
normalizeDashboardPOCReadinessLists(&poc)
return poc
}
func (server *server) enrichDashboardPOC(ctx context.Context, request *http.Request, summary *domain.DashboardSummary) {
summary.POC = server.dashboardPOCReadiness(ctx, request)
}
func (server *server) populateProjectWorkflowCounts(ctx context.Context, request *http.Request, items []domain.ProjectRecord) ([]domain.ProjectRecord, error) {
if len(items) == 0 {
return items, nil
}
workflows, err := server.listAllWorkflowsForRequest(ctx, request)
if err != nil {
return nil, err
}
counts := make(map[string]int, len(items))
statusCounts := make(map[string]domain.WorkflowStatusCounts, len(items))
for _, workflow := range workflows {
if workflow.ProjectID == "" || workflow.Hidden {
continue
}
counts[workflow.ProjectID]++
projectStatusCounts := statusCounts[workflow.ProjectID]
switch workflow.Status {
case domain.WorkflowStatusDraft:
projectStatusCounts.Draft++
case domain.WorkflowStatusRunning:
projectStatusCounts.Running++
case domain.WorkflowStatusFailed:
projectStatusCounts.Failed++
case domain.WorkflowStatusCancelled:
projectStatusCounts.Cancelled++
case domain.WorkflowStatusCompleted:
projectStatusCounts.Completed++
}
statusCounts[workflow.ProjectID] = projectStatusCounts
}
enriched := make([]domain.ProjectRecord, len(items))
for index, item := range items {
item.WorkflowCount = counts[item.ID]
item.WorkflowStatusCounts = statusCounts[item.ID]
enriched[index] = item
}
return enriched, nil
}
func (server *server) populateSingleProjectWorkflowCount(ctx context.Context, item *domain.ProjectRecord) (*domain.ProjectRecord, error) {
if item == nil {
return nil, nil
}
workflows, err := server.store.ListWorkflowsByProject(ctx, item.ID)
if err != nil {
return nil, err
}
enriched := *item
enriched.WorkflowCount = len(workflows)
for _, workflow := range workflows {
switch workflow.Status {
case domain.WorkflowStatusDraft:
enriched.WorkflowStatusCounts.Draft++
case domain.WorkflowStatusRunning:
enriched.WorkflowStatusCounts.Running++
case domain.WorkflowStatusFailed:
enriched.WorkflowStatusCounts.Failed++
case domain.WorkflowStatusCancelled:
enriched.WorkflowStatusCounts.Cancelled++
case domain.WorkflowStatusCompleted:
enriched.WorkflowStatusCounts.Completed++
}
}
return &enriched, nil
}
func (server *server) handleProjectsList(writer http.ResponseWriter, request *http.Request) {
items, err := server.listProjectsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterProjectsForRequest(request, items)
for index := range items {
if err := server.migrateLegacyProjectWorkflowInstances(request.Context(), &items[index]); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
items, err = server.populateProjectWorkflowCounts(request.Context(), request, items)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeProjects(items)})
}
func (server *server) handleProjectsCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateProjectInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name = strings.TrimSpace(payload.Name)
if payload.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
server.assignTenant(request, &payload.OrganizationID)
payload.StorageConnectionID = strings.TrimSpace(payload.StorageConnectionID)
payload.StorageBucket = strings.Trim(strings.TrimSpace(payload.StorageBucket), "/")
payload.StoragePrefix = strings.Trim(strings.TrimSpace(payload.StoragePrefix), "/")
if payload.StorageConnectionID == "" || payload.StorageBucket == "" {
settings, err := server.store.GetArtifactStorageSettings(request.Context(), payload.OrganizationID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if payload.StorageConnectionID == "" {
payload.StorageConnectionID = strings.TrimSpace(settings.ConnectionID)
}
if payload.StorageBucket == "" {
payload.StorageBucket = strings.Trim(strings.TrimSpace(settings.Bucket), "/")
}
}
projectStorageIssues := make([]validationIssue, 0, 2)
if payload.StorageConnectionID == "" {
projectStorageIssues = append(projectStorageIssues, validationIssue{Field: "storageConnectionId", Code: "project_storage_connection_required", Message: "storageConnectionId is required"})
}
if payload.StorageBucket == "" {
projectStorageIssues = append(projectStorageIssues, validationIssue{Field: "storageBucket", Code: "project_storage_bucket_required", Message: "storageBucket is required"})
}
if len(projectStorageIssues) == 0 {
projectStorageIssues = append(projectStorageIssues, server.validateArtifactStoragePolicy(request.Context(), payload.OrganizationID, &domain.ArtifactStoragePolicy{
ConnectionID: payload.StorageConnectionID,
Bucket: payload.StorageBucket,
PrefixTemplate: payload.StoragePrefix,
}, "projectStorage")...)
}
if len(projectStorageIssues) > 0 {
renderValidationError(writer, projectStorageIssues...)
return
}
project, err := server.store.CreateProject(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeProject(project))
}
func (server *server) handleProjectsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ID) {
return
}
if err := server.migrateLegacyProjectWorkflowInstances(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
item, err = server.populateSingleProjectWorkflowCount(request.Context(), item)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeProject(item))
}
type projectPatch struct {
Name *string `json:"name"`
Description *string `json:"description"`
Status *domain.ProjectStatus `json:"status"`
Scene *string `json:"scene"`
OwnerUserID *string `json:"ownerUserId"`
Customer *string `json:"customer"`
DataBatch *string `json:"dataBatch"`
StorageConnectionID *string `json:"storageConnectionId"`
StorageBucket *string `json:"storageBucket"`
StoragePrefix *string `json:"storagePrefix"`
AnnotationStandard *string `json:"annotationStandard"`
QualityPolicy *map[string]any `json:"qualityPolicy"`
DeliveryConfig *map[string]any `json:"deliveryConfig"`
}
func validProjectStatus(value domain.ProjectStatus) bool {
return value == domain.ProjectStatusActive || value == domain.ProjectStatusArchived
}
func (server *server) handleProjectsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
return
}
var payload projectPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
item.Name = strings.TrimSpace(*payload.Name)
if item.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
}
if payload.Description != nil {
item.Description = *payload.Description
}
if payload.Scene != nil {
item.Scene = strings.TrimSpace(*payload.Scene)
}
if payload.OwnerUserID != nil {
item.OwnerUserID = strings.TrimSpace(*payload.OwnerUserID)
}
if payload.Customer != nil {
item.Customer = strings.TrimSpace(*payload.Customer)
}
if payload.DataBatch != nil {
item.DataBatch = strings.TrimSpace(*payload.DataBatch)
}
if payload.StorageConnectionID != nil {
item.StorageConnectionID = strings.TrimSpace(*payload.StorageConnectionID)
}
if payload.StorageBucket != nil {
item.StorageBucket = strings.TrimSpace(*payload.StorageBucket)
}
if payload.StoragePrefix != nil {
item.StoragePrefix = strings.TrimSpace(*payload.StoragePrefix)
}
if payload.AnnotationStandard != nil {
item.AnnotationStandard = strings.TrimSpace(*payload.AnnotationStandard)
}
if payload.QualityPolicy != nil {
item.QualityPolicy = *payload.QualityPolicy
}
if payload.DeliveryConfig != nil {
item.DeliveryConfig = *payload.DeliveryConfig
}
if payload.Status != nil {
status := domain.ProjectStatus(strings.TrimSpace(string(*payload.Status)))
if !validProjectStatus(status) {
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or archived"})
return
}
item.Status = status
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertProject(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
item, err = server.populateSingleProjectWorkflowCount(request.Context(), item)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeProject(item))
}
func (server *server) handleProjectsDelete(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Project") {
return
}
deleted, err := server.store.DeleteProject(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusNoContent)
}
func (server *server) handleProjectStoragePathFavoritesGet(writer http.ResponseWriter, request *http.Request) {
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"items": sanitizeStoragePathFavorites(project.StoragePathFavorites),
})
}
func (server *server) handleProjectStoragePathFavoritesUpdate(writer http.ResponseWriter, request *http.Request) {
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
return
}
var payload storagePathFavoritesPayload
if !decodeJSON(writer, request, &payload) {
return
}
project.StoragePathFavorites = sanitizeStoragePathFavorites(payload.Items)
project.UpdatedAt = nowISO()
if err := server.store.UpsertProject(request.Context(), *project); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"items": project.StoragePathFavorites,
})
}
func (server *server) handleProjectWorkflowsList(writer http.ResponseWriter, request *http.Request) {
project, err := server.store.GetProject(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
return
}
items, err := server.store.ListWorkflowsByProject(request.Context(), project.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterWorkflowsForRequest(request, items)
items = filterTenantRecords(items, func(item domain.WorkflowRecord) bool { return !item.Hidden })
if err := server.enrichWorkflowListCapabilities(request.Context(), items); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeWorkflows(items)})
}
func validWorkflowType(value domain.WorkflowType) bool {
return value == domain.WorkflowTypeBatch || value == domain.WorkflowTypeStreaming
}
func normalizeWorkflowType(value domain.WorkflowType) domain.WorkflowType {
return domain.WorkflowType(strings.TrimSpace(string(value)))
}
func validWorkflowKind(value domain.WorkflowKind) bool {
return value == "" || value == domain.WorkflowKindStandard || value == domain.WorkflowKindScan
}
func normalizeWorkflowExecutionScope(value domain.WorkflowExecutionScope) domain.WorkflowExecutionScope {
switch domain.WorkflowExecutionScope(strings.ToLower(strings.TrimSpace(string(value)))) {
case domain.WorkflowExecutionScopeSandbox:
return domain.WorkflowExecutionScopeSandbox
default:
return domain.WorkflowExecutionScopeProduction
}
}
func validWorkflowExecutionScope(value domain.WorkflowExecutionScope) bool {
return value == domain.WorkflowExecutionScopeProduction || value == domain.WorkflowExecutionScopeSandbox
}
func normalizeWorkflowKind(value domain.WorkflowKind) domain.WorkflowKind {
return domain.WorkflowKind(strings.TrimSpace(string(value)))
}
func validWorkflowStatus(value domain.WorkflowStatus) bool {
switch value {
case domain.WorkflowStatusDraft,
domain.WorkflowStatusReady,
domain.WorkflowStatusRunning,
domain.WorkflowStatusCompleted,
domain.WorkflowStatusFailed,
domain.WorkflowStatusCancelled:
return true
default:
return false
}
}
func normalizeWorkflowStatus(value domain.WorkflowStatus) domain.WorkflowStatus {
return domain.WorkflowStatus(strings.TrimSpace(string(value)))
}
func validateWorkflowExecutionConfig(maxConcurrentRuns int, nodeTimeoutSeconds int, runTimeoutSeconds int, maxRetryAttempts int, retryBackoffSeconds int) []validationIssue {
issues := make([]validationIssue, 0, 5)
if maxConcurrentRuns < 0 {
issues = append(issues, validationIssue{Field: "maxConcurrentRuns", Message: "maxConcurrentRuns must be zero or greater"})
}
if nodeTimeoutSeconds < 0 {
issues = append(issues, validationIssue{Field: "nodeTimeoutSeconds", Message: "nodeTimeoutSeconds must be zero or greater"})
}
if runTimeoutSeconds < 0 {
issues = append(issues, validationIssue{Field: "runTimeoutSeconds", Message: "runTimeoutSeconds must be zero or greater"})
}
if maxRetryAttempts < 0 {
issues = append(issues, validationIssue{Field: "maxRetryAttempts", Message: "maxRetryAttempts must be zero or greater"})
}
if retryBackoffSeconds < 0 {
issues = append(issues, validationIssue{Field: "retryBackoffSeconds", Message: "retryBackoffSeconds must be zero or greater"})
}
return issues
}
func cloneWorkflowSchedule(schedule *domain.WorkflowSchedule) *domain.WorkflowSchedule {
if schedule == nil {
return nil
}
cloned := *schedule
return &cloned
}
func normalizeWorkflowSchedule(existing *domain.WorkflowSchedule, incoming *domain.WorkflowSchedule, referenceTime string) *domain.WorkflowSchedule {
if incoming == nil {
return cloneWorkflowSchedule(existing)
}
next := *incoming
next.SourcePath = strings.TrimSpace(next.SourcePath)
next.TimeOfDay = strings.TrimSpace(next.TimeOfDay)
next.AnchorAt = strings.TrimSpace(next.AnchorAt)
next.Timezone = strings.TrimSpace(next.Timezone)
if normalizedMode := domain.NormalizeWorkflowScheduleMode(next.Mode); normalizedMode != "" {
next.Mode = normalizedMode
} else if strings.TrimSpace(string(next.Mode)) == "" {
next.Mode = domain.EffectiveWorkflowScheduleMode(&next)
}
effectiveMode := domain.EffectiveWorkflowScheduleMode(&next)
if effectiveMode == domain.WorkflowScheduleModeEveryDaysAtTime || next.Timezone != "" {
next.Timezone = domain.WorkflowScheduleTimezone(next.Timezone)
}
if (effectiveMode == domain.WorkflowScheduleModeEveryHours || effectiveMode == domain.WorkflowScheduleModeEveryDaysAtTime) &&
domain.WorkflowScheduleEnabled(&next) {
if next.AnchorAt == "" || domain.EffectiveWorkflowScheduleMode(existing) != effectiveMode || !domain.WorkflowScheduleEnabled(existing) {
next.AnchorAt = referenceTime
}
}
return &next
}
func validateWorkflowSchedule(schedule *domain.WorkflowSchedule) []validationIssue {
if schedule == nil {
return nil
}
issues := make([]validationIssue, 0, 6)
if rawMode := strings.TrimSpace(string(schedule.Mode)); rawMode != "" && domain.NormalizeWorkflowScheduleMode(schedule.Mode) == "" {
issues = append(issues, validationIssue{Field: "schedule.mode", Message: "schedule.mode must be interval_seconds, every_hours, or every_days_at_time"})
}
if schedule.Interval < 0 {
issues = append(issues, validationIssue{Field: "schedule.interval", Message: "schedule.interval must be zero or greater"})
}
if schedule.IntervalHours < 0 {
issues = append(issues, validationIssue{Field: "schedule.intervalHours", Message: "schedule.intervalHours must be zero or greater"})
}
if schedule.DayInterval < 0 {
issues = append(issues, validationIssue{Field: "schedule.dayInterval", Message: "schedule.dayInterval must be zero or greater"})
}
if timezone := strings.TrimSpace(schedule.Timezone); timezone != "" {
if _, err := time.LoadLocation(timezone); err != nil {
issues = append(issues, validationIssue{Field: "schedule.timezone", Message: "schedule.timezone must be a valid IANA timezone"})
}
}
switch domain.EffectiveWorkflowScheduleMode(schedule) {
case domain.WorkflowScheduleModeEveryHours:
if schedule.IntervalHours < 1 {
issues = append(issues, validationIssue{Field: "schedule.intervalHours", Message: "schedule.intervalHours must be greater than zero"})
}
case domain.WorkflowScheduleModeEveryDaysAtTime:
if schedule.DayInterval < 1 {
issues = append(issues, validationIssue{Field: "schedule.dayInterval", Message: "schedule.dayInterval must be greater than zero"})
}
if !workflowScheduleTimeOfDayPattern.MatchString(strings.TrimSpace(schedule.TimeOfDay)) {
issues = append(issues, validationIssue{Field: "schedule.timeOfDay", Message: "schedule.timeOfDay must use HH:mm format"})
}
}
return issues
}
func (server *server) handleProjectWorkflowsCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateWorkflowInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Nodes = normalizeWorkflowNodePorts(payload.Nodes)
payload.ProjectID = chi.URLParam(request, "id")
project, err := server.store.GetProject(request.Context(), payload.ProjectID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if project == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Project not found"})
return
}
if !server.ensureTenantAccess(writer, request, project.OrganizationID, "Project") {
return
}
payload.OrganizationID = normalizedTenantID(project.OrganizationID)
projectStorageIssues := applyProjectWorkflowArtifactStorage(project, &payload.ArtifactStorage)
if len(projectStorageIssues) > 0 {
renderValidationError(writer, projectStorageIssues...)
return
}
payload.Name = strings.TrimSpace(payload.Name)
payload.Type = normalizeWorkflowType(payload.Type)
payload.Kind = normalizeWorkflowKind(payload.Kind)
if payload.Type == "" {
payload.Type = domain.WorkflowTypeBatch
}
issues := make([]validationIssue, 0, 3)
if payload.Name == "" {
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
}
if !validWorkflowType(payload.Type) {
issues = append(issues, validationIssue{Field: "type", Message: "type must be batch or streaming"})
}
if !validWorkflowKind(payload.Kind) {
issues = append(issues, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
}
payload.ExecutionScope = normalizeWorkflowExecutionScope(payload.ExecutionScope)
if !validWorkflowExecutionScope(payload.ExecutionScope) {
issues = append(issues, validationIssue{Field: "executionScope", Message: "executionScope must be production or sandbox"})
}
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowExecutionConfig(payload.MaxConcurrentRuns, payload.NodeTimeoutSeconds, payload.RunTimeoutSeconds, payload.MaxRetryAttempts, payload.RetryBackoffSeconds); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
payload.Schedule = normalizeWorkflowSchedule(nil, payload.Schedule, nowISO())
if issues := validateWorkflowSchedule(payload.Schedule); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if payload.Kind == domain.WorkflowKindScan || payload.ScanConfig != nil || domain.FindScanSourceNode(payload.Nodes) != nil {
requestPayload := scanConfigRequestFromSourceNode(payload.Name, payload.Description, payload.Schedule, payload.ScanConfig, payload.Nodes)
if issues := validateScanConfigDraftPayload(requestPayload); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, nil) {
renderValidationError(writer, validateScanConfigPayload(requestPayload, nil)...)
return
}
payload.Kind = domain.WorkflowKindScan
payload.ScanConfig = server.normalizedScanConfigFromRequest(nil, requestPayload)
if issues := server.validateScanConnectionReference(request.Context(), payload.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
payload.Schedule = normalizedScanSchedule(payload.Schedule, payload.ScanConfig)
payload.Type = normalizedScanWorkflowType(payload.ScanConfig)
payload.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(payload.MaxConcurrentRuns)
if len(payload.Nodes) == 0 {
scaffold, err := server.buildScanWorkflowInput(request.Context(), payload.ProjectID, requestPayload, nil)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
payload.Nodes = scaffold.Nodes
payload.Edges = scaffold.Edges
}
}
payload.Nodes = normalizeWorkflowNodePorts(payload.Nodes)
if issues := server.validateNodeConnectionReferences(request.Context(), payload.Nodes); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowTopology(payload.Nodes, payload.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowInputContract(payload.Nodes, payload.Edges, payload.WorkflowInputs, false); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateArtifactStoragePolicy(request.Context(), payload.OrganizationID, payload.ArtifactStorage, "artifactStorage"); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), payload.Nodes, payload.ExecutionScope); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item, err := server.store.CreateWorkflow(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeWorkflow(item))
}
func (server *server) handleWorkflowsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
if err := server.enrichWorkflowCapabilities(request.Context(), item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeWorkflow(item))
}
func (server *server) workflowFamilyVersions(ctx context.Context, source *domain.WorkflowRecord) ([]domain.WorkflowRecord, error) {
if source == nil {
return []domain.WorkflowRecord{}, nil
}
domain.NormalizeWorkflowDefinitionMetadata(source)
items, err := server.store.ListWorkflowsByProject(ctx, source.ProjectID)
if err != nil {
return nil, err
}
familyID := strings.TrimSpace(source.DefinitionFamilyID)
versions := make([]domain.WorkflowRecord, 0, len(items))
for _, item := range items {
domain.NormalizeWorkflowDefinitionMetadata(&item)
if normalizedTenantID(item.OrganizationID) != normalizedTenantID(source.OrganizationID) ||
strings.TrimSpace(item.DefinitionFamilyID) != familyID {
continue
}
versions = append(versions, item)
}
sort.SliceStable(versions, func(left int, right int) bool {
if versions[left].DefinitionVersion != versions[right].DefinitionVersion {
return versions[left].DefinitionVersion > versions[right].DefinitionVersion
}
return versions[left].UpdatedAt > versions[right].UpdatedAt
})
return versions, nil
}
func (server *server) handleWorkflowVersionsList(writer http.ResponseWriter, request *http.Request) {
source, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if source == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, source.OrganizationID, "Workflow") {
return
}
if !server.ensureProjectVisibility(writer, request, source.OrganizationID, source.ProjectID) {
return
}
versions, err := server.workflowFamilyVersions(request.Context(), source)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := server.enrichWorkflowListCapabilities(request.Context(), versions); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeWorkflows(versions)})
}
func (server *server) handleWorkflowActivateVersion(writer http.ResponseWriter, request *http.Request) {
target, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if target == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, target.OrganizationID, "Workflow") {
return
}
domain.NormalizeWorkflowDefinitionMetadata(target)
if target.DefinitionState != domain.WorkflowDefinitionStateReleased {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "only released workflow versions can become active",
"code": "workflow_version_not_released",
})
return
}
active, err := server.store.HasActiveRun(request.Context(), target.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if active {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流版本仍在运行,请在运行结束后设为当前版本",
"code": "workflow_run_active",
})
return
}
versions, err := server.workflowFamilyVersions(request.Context(), target)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
now := nowISO()
updated := make([]domain.WorkflowRecord, 0, len(versions))
for _, item := range versions {
shouldHide := item.ID != target.ID
if item.Hidden == shouldHide {
continue
}
item.Hidden = shouldHide
item.UpdatedAt = now
if err := server.store.UpsertWorkflow(request.Context(), item); err != nil {
for _, previous := range updated {
previous.Hidden = !previous.Hidden
_ = server.store.UpsertWorkflow(request.Context(), previous)
}
renderError(writer, http.StatusInternalServerError, err)
return
}
updated = append(updated, item)
}
target.Hidden = false
target.UpdatedAt = now
versions, err = server.workflowFamilyVersions(request.Context(), target)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"workflow": sanitizeWorkflow(target),
"items": sanitizeWorkflows(versions),
})
}
func (server *server) handleWorkflowNodeSourceStateGet(writer http.ResponseWriter, request *http.Request) {
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
response, status, err := server.buildWorkflowNodeSourceStateResponse(request.Context(), workflow, chi.URLParam(request, "nodeId"))
if err != nil {
renderError(writer, status, err)
return
}
if response == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow node not found"})
return
}
if workflow != nil && !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow node") {
return
}
renderJSON(writer, status, response)
}
func (server *server) handleWorkflowNodeSourceStateDelete(writer http.ResponseWriter, request *http.Request) {
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
response, status, err := server.buildWorkflowNodeSourceStateResponse(request.Context(), workflow, chi.URLParam(request, "nodeId"))
if err != nil {
renderError(writer, status, err)
return
}
if response == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow node not found"})
return
}
if workflow != nil && !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow node") {
return
}
if !response.Supported {
renderValidationError(writer, validationIssue{Field: "nodeId", Message: response.Reason})
return
}
if !response.Stateful || strings.TrimSpace(response.CurrentFingerprint) == "" {
writer.WriteHeader(http.StatusNoContent)
return
}
if err := server.store.DeleteSourceState(request.Context(), workflow.ID, response.NodeID, response.CurrentFingerprint); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
writer.WriteHeader(http.StatusNoContent)
}
type workflowPatch struct {
Name *string `json:"name"`
Description *string `json:"description"`
Type *domain.WorkflowType `json:"type"`
Kind *domain.WorkflowKind `json:"kind"`
ExecutionScope *domain.WorkflowExecutionScope `json:"executionScope"`
ScanConfig *domain.ScanConfig `json:"scanConfig"`
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
NodeTimeoutSeconds *int `json:"nodeTimeoutSeconds"`
RunTimeoutSeconds *int `json:"runTimeoutSeconds"`
MaxRetryAttempts *int `json:"maxRetryAttempts"`
RetryBackoffSeconds *int `json:"retryBackoffSeconds"`
Schedule *domain.WorkflowSchedule `json:"schedule"`
Nodes *[]domain.WorkflowNode `json:"nodes"`
Edges *[]domain.WorkflowEdge `json:"edges"`
Status *domain.WorkflowStatus `json:"status"`
WorkflowInputs *[]domain.WorkflowInputDefinition `json:"workflowInputs"`
ArtifactStorage *domain.ArtifactStoragePolicy `json:"artifactStorage"`
}
func scanConfigRequestFromWorkflow(name string, description string, schedule *domain.WorkflowSchedule, scanConfig *domain.ScanConfig) scanConfigRequest {
requestPayload := scanConfigRequest{
Name: strings.TrimSpace(name),
Description: strings.TrimSpace(description),
}
if schedule != nil {
requestPayload.Interval = schedule.Interval
requestPayload.SourcePath = strings.TrimSpace(schedule.SourcePath)
requestPayload.Enabled = domain.WorkflowScheduleEnabled(schedule)
}
if scanConfig == nil {
return requestPayload
}
requestPayload.DataSource = scanConfig.DataSource
if sourcePath := strings.TrimSpace(scanConfig.SourcePath); sourcePath != "" {
requestPayload.SourcePath = sourcePath
}
requestPayload.ConnectionID = scanConfig.ConnectionID
requestPayload.Bucket = scanConfig.Bucket
requestPayload.Prefix = scanConfig.Prefix
requestPayload.Endpoint = scanConfig.Endpoint
requestPayload.AccessKeyID = scanConfig.AccessKeyID
requestPayload.SecretAccessKey = scanConfig.SecretAccessKey
requestPayload.ScanMode = scanConfig.ScanMode
requestPayload.IncrementalUnit = scanConfig.IncrementalUnit
requestPayload.DirectoryDepth = scanConfig.DirectoryDepth
requestPayload.Pattern = scanConfig.Pattern
requestPayload.Notifications = scanConfig.Notifications
requestPayload.Enabled = scanConfig.Enabled || requestPayload.Enabled
return requestPayload
}
func workflowHyperparamValue(node *domain.WorkflowNode, key string) string {
if node == nil {
return ""
}
target := strings.TrimSpace(key)
if target == "" {
return ""
}
for _, item := range node.Data.Hyperparams {
if strings.TrimSpace(item.Key) == target {
return strings.TrimSpace(item.Value)
}
}
return ""
}
func scanConfigRequestFromSourceNode(name string, description string, schedule *domain.WorkflowSchedule, scanConfig *domain.ScanConfig, nodes []domain.WorkflowNode) scanConfigRequest {
requestPayload := scanConfigRequestFromWorkflow(name, description, schedule, scanConfig)
node := domain.FindScanSourceNode(nodes)
if node == nil {
return requestPayload
}
if requestPayload.DataSource == "" {
if raw := strings.TrimSpace(workflowHyperparamValue(node, "DATA_SOURCE")); raw != "" {
if parsed, issue := parseWorkflowDataSourceHyperparam(raw); issue != nil {
requestPayload.DataSourceIssue = issue
} else {
requestPayload.DataSource = parsed
}
}
if requestPayload.DataSource == "" {
requestPayload.DataSource = chooseDataSource("")
}
}
if requestPayload.ConnectionID == "" {
requestPayload.ConnectionID = strings.TrimSpace(node.Data.ConnectionID)
}
if requestPayload.Bucket == "" {
requestPayload.Bucket = strings.TrimSpace(node.Data.Bucket)
}
if requestPayload.Prefix == "" {
requestPayload.Prefix = cleanBOSPrefix(node.Data.Prefix)
}
if requestPayload.SourcePath == "" {
requestPayload.SourcePath = strings.TrimSpace(workflowHyperparamValue(node, "SOURCE_PATH"))
}
if requestPayload.Endpoint == "" {
requestPayload.Endpoint = strings.TrimSpace(workflowHyperparamValue(node, "BOS_ENDPOINT"))
}
if requestPayload.AccessKeyID == "" {
requestPayload.AccessKeyID = strings.TrimSpace(workflowHyperparamValue(node, "BOS_ACCESS_KEY_ID"))
}
if requestPayload.SecretAccessKey == "" {
requestPayload.SecretAccessKey = strings.TrimSpace(workflowHyperparamValue(node, "BOS_SECRET_ACCESS_KEY"))
}
if requestPayload.ScanMode == "" {
if raw := strings.TrimSpace(workflowHyperparamValue(node, "SCAN_MODE")); raw != "" {
if parsed, issue := parseWorkflowScanModeHyperparam(raw); issue != nil {
requestPayload.ScanModeIssue = issue
} else {
requestPayload.ScanMode = parsed
}
}
if requestPayload.ScanMode == "" {
requestPayload.ScanMode = chooseWorkflowScanMode("")
}
}
if requestPayload.IncrementalUnit == "" {
if raw := strings.TrimSpace(workflowHyperparamValue(node, "INCREMENTAL_UNIT")); raw != "" {
if parsed, issue := parseWorkflowIncrementalUnitHyperparam(raw); issue != nil {
requestPayload.IncrementalUnitIssue = issue
} else {
requestPayload.IncrementalUnit = parsed
}
}
if requestPayload.IncrementalUnit == "" {
requestPayload.IncrementalUnit = chooseWorkflowIncrementalUnit("")
}
}
if requestPayload.DirectoryDepth <= 0 {
if raw := strings.TrimSpace(workflowHyperparamValue(node, "INCREMENTAL_DIRECTORY_DEPTH")); raw != "" {
if parsed, issue := parseWorkflowDirectoryDepthHyperparam(raw); issue != nil {
requestPayload.DirectoryDepthIssue = issue
} else {
requestPayload.DirectoryDepth = parsed
}
}
}
if requestPayload.Pattern == "" {
requestPayload.Pattern = strings.TrimSpace(node.Data.Pattern)
}
if requestPayload.Enabled {
requestPayload.Enabled = true
}
return requestPayload
}
func (server *server) normalizedScanConfigFromRequest(existing *domain.ScanConfig, requestPayload scanConfigRequest) *domain.ScanConfig {
dataSource := chooseDataSource(requestPayload.DataSource)
sourcePath := scanSourcePath(dataSource, requestPayload.SourcePath, requestPayload.Bucket, requestPayload.Prefix)
connectionID := ""
endpoint := ""
accessKeyID := ""
secretAccessKey := ""
if dataSource == "BOS" {
connectionID = mergeScanConnectionID(existing, requestPayload)
if connectionID == "" {
endpoint = server.chooseBOSEndpoint(requestPayload.Endpoint)
accessKeyID, secretAccessKey = mergeScanSecrets(existing, requestPayload)
}
}
return &domain.ScanConfig{
DataSource: dataSource,
SourcePath: sourcePath,
ConnectionID: connectionID,
Bucket: strings.TrimSpace(requestPayload.Bucket),
Prefix: cleanBOSPrefix(requestPayload.Prefix),
Endpoint: endpoint,
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
ScanMode: chooseWorkflowScanMode(requestPayload.ScanMode),
IncrementalUnit: chooseWorkflowIncrementalUnit(requestPayload.IncrementalUnit),
DirectoryDepth: chooseWorkflowDirectoryDepth(requestPayload.DirectoryDepth),
Pattern: strings.TrimSpace(requestPayload.Pattern),
Enabled: requestPayload.Enabled,
Notifications: requestPayload.Notifications,
}
}
func normalizedScanSchedule(existing *domain.WorkflowSchedule, scanConfig *domain.ScanConfig) *domain.WorkflowSchedule {
next := &domain.WorkflowSchedule{}
if existing != nil {
*next = *existing
}
if scanConfig == nil {
return next
}
next.SourcePath = scanConfig.SourcePath
if scanConfig.Enabled {
if !domain.WorkflowScheduleEnabled(next) {
next.Mode = domain.WorkflowScheduleModeIntervalSeconds
next.Interval = 60
}
return next
}
if domain.EffectiveWorkflowScheduleMode(next) == domain.WorkflowScheduleModeIntervalSeconds {
next.Interval = 0
}
return next
}
func normalizedScanWorkflowType(scanConfig *domain.ScanConfig) domain.WorkflowType {
if scanConfig != nil && scanConfig.Enabled {
return domain.WorkflowTypeStreaming
}
return domain.WorkflowTypeBatch
}
func normalizedScanMaxConcurrentRuns(value int) int {
if value > 0 {
return value
}
return 1
}
func (server *server) validateScanConnectionReference(ctx context.Context, scanConfig *domain.ScanConfig) []validationIssue {
if scanConfig == nil || strings.TrimSpace(scanConfig.ConnectionID) == "" {
return nil
}
if _, err := server.getStorageConnection(ctx, scanConfig.ConnectionID); err != nil {
return []validationIssue{{Field: "scanConfig.connectionId", Message: "connectionId must reference an existing storage connection"}}
}
return nil
}
func (server *server) validateNodeConnectionReferences(ctx context.Context, nodes []domain.WorkflowNode) []validationIssue {
issues := make([]validationIssue, 0)
for index, node := range nodes {
storageConnectionID := strings.TrimSpace(node.Data.ConnectionID)
if storageConnectionID != "" {
if _, err := server.getStorageConnection(ctx, storageConnectionID); err != nil {
issues = append(issues, validationIssue{
Field: "nodes[" + strconv.Itoa(index) + "].data.connectionId",
Message: "connectionId must reference an existing storage connection",
})
}
}
mongoConnectionID := ""
for _, item := range node.Data.Hyperparams {
if strings.EqualFold(strings.TrimSpace(item.Key), "MONGO_CONNECTION_ID") {
mongoConnectionID = strings.TrimSpace(item.Value)
break
}
}
if mongoConnectionID == "" {
continue
}
if _, err := server.getMongoConnection(ctx, mongoConnectionID); err != nil {
issues = append(issues, validationIssue{
Field: "nodes[" + strconv.Itoa(index) + "].data.hyperparams",
Message: "MONGO_CONNECTION_ID must reference an existing mongo connection",
})
}
}
return issues
}
func validateInputPortConnectionTopology(nodes []domain.WorkflowNode, edges []domain.WorkflowEdge) []validationIssue {
conflicts := domain.FindInputPortConflicts(nodes, edges)
if len(conflicts) == 0 {
return nil
}
issues := make([]validationIssue, 0, len(conflicts))
for _, conflict := range conflicts {
nodeLabel := strings.TrimSpace(conflict.TargetNodeLabel)
if nodeLabel == "" {
nodeLabel = conflict.TargetNodeID
}
portName := strings.TrimSpace(conflict.TargetPortName)
if portName == "" {
portName = "默认输入"
}
issues = append(issues, validationIssue{
Field: "edges",
Code: "workflow_input_port_conflict",
Message: "节点“" + nodeLabel + "”的输入端口“" + portName + "”连接了多条上游连线。" +
"每个输入端口只能连接一条上游连线;如需上传多个结果,请为每个结果添加单独的上传算子。",
})
}
return issues
}
func validateWorkflowGraphTopology(nodes []domain.WorkflowNode, edges []domain.WorkflowEdge) []validationIssue {
graphIssues := domain.ValidateSingleConnectedWorkflowGraph(nodes, edges)
if len(graphIssues) == 0 {
return nil
}
issues := make([]validationIssue, 0, len(graphIssues))
for _, graphIssue := range graphIssues {
switch graphIssue.Code {
case domain.WorkflowGraphIssueIsolatedNode:
issues = append(issues, validationIssue{
Field: "nodes",
Code: string(graphIssue.Code),
Message: "工作流存在孤立算子节点:" + strings.Join(graphIssue.NodeIDs, ", ") + "。请将所有算子接入同一条业务链路后再保存或运行。",
})
case domain.WorkflowGraphIssueDisconnected:
issues = append(issues, validationIssue{
Field: "edges",
Code: string(graphIssue.Code),
Message: "工作流存在多条互不连接的独立链路,分支节点:" + strings.Join(graphIssue.Component, ", ") + "。请把所有算子通过 artifact/lineage 连成一个单连通工作流。",
})
case domain.WorkflowGraphIssueCycle:
issues = append(issues, validationIssue{
Field: "edges",
Code: string(graphIssue.Code),
Message: "工作流不能包含有向环,涉及节点:" + strings.Join(graphIssue.NodeIDs, ", ") + "。人工返工循环必须由 Human Item 状态机表达,不能通过画布回边实现。",
})
default:
issues = append(issues, validationIssue{
Field: "edges",
Code: string(graphIssue.Code),
Message: graphIssue.Message,
})
}
}
return issues
}
func validateWorkflowTopology(nodes []domain.WorkflowNode, edges []domain.WorkflowEdge) []validationIssue {
issues := validateWorkflowNodeResources(nodes)
issues = append(issues, validateWorkflowPortDefinitions(nodes)...)
issues = append(issues, validateInputPortConnectionTopology(nodes, edges)...)
issues = append(issues, validateWorkflowGraphTopology(nodes, edges)...)
for _, issue := range domain.ValidateWorkflowPortCompatibility(nodes, edges) {
issues = append(issues, validationIssue{
Field: "edges",
Code: "workflow_port_contract_incompatible",
Message: fmt.Sprintf(
"连线“%s:%s → %s:%s”端口契约不兼容:%s(上游 %s/%s/%s,下游 %s/%s/%s)。",
issue.SourceNodeID, issue.SourcePortID, issue.TargetNodeID, issue.TargetPortID, issue.Message,
issue.SourceContract.Type, issue.SourceContract.Container, issue.SourceContract.PayloadMode,
issue.TargetContract.Type, issue.TargetContract.Container, issue.TargetContract.PayloadMode,
),
})
}
return issues
}
func validateWorkflowInputContract(nodes []domain.WorkflowNode, edges []domain.WorkflowEdge, inputs []domain.WorkflowInputDefinition, release bool) []validationIssue {
contractIssues := domain.ValidateWorkflowInputContract(nodes, edges, inputs, release)
issues := make([]validationIssue, 0, len(contractIssues))
for _, item := range contractIssues {
issues = append(issues, validationIssue{Field: item.Field, Code: item.Code, Message: item.Message})
}
return issues
}
func (server *server) validateArtifactStoragePolicy(ctx context.Context, organizationID string, policy *domain.ArtifactStoragePolicy, field string) []validationIssue {
if policy == nil {
return nil
}
issues := make([]validationIssue, 0, 2)
policy.ConnectionID = strings.TrimSpace(policy.ConnectionID)
policy.Bucket = strings.TrimSpace(policy.Bucket)
policy.PrefixTemplate = strings.Trim(strings.TrimSpace(policy.PrefixTemplate), "/")
if policy.ConnectionID == "" && policy.Bucket == "" && policy.PrefixTemplate == "" {
return nil
}
if policy.ConnectionID == "" {
issues = append(issues, validationIssue{Field: field + ".connectionId", Code: "artifact_storage_connection_required", Message: "artifact storage connectionId is required"})
}
if policy.Bucket == "" {
issues = append(issues, validationIssue{Field: field + ".bucket", Code: "artifact_storage_bucket_required", Message: "artifact storage bucket is required"})
}
if len(issues) > 0 {
return issues
}
connection, err := server.store.GetStorageConnection(ctx, policy.ConnectionID)
if err != nil || connection == nil || normalizedTenantID(connection.OrganizationID) != normalizedTenantID(organizationID) {
return []validationIssue{{Field: field + ".connectionId", Code: "artifact_storage_connection_invalid", Message: "artifact storage connectionId must reference an accessible storage connection"}}
}
boundBucket := strings.Trim(strings.TrimSpace(connection.Bucket), "/")
if boundBucket != "" && boundBucket != policy.Bucket {
return []validationIssue{{Field: field + ".bucket", Code: "artifact_storage_bucket_mismatch", Message: "artifact storage bucket must match the selected connection bucket"}}
}
return nil
}
func defaultProjectWorkflowArtifactPrefix(project *domain.ProjectRecord) string {
workflowPath := "workflows/{workflowId}/runs/{runId}/{nodeId}"
if project == nil {
return workflowPath
}
root := strings.Trim(strings.TrimSpace(project.StoragePrefix), "/")
if root == "" {
return workflowPath
}
return root + "/" + workflowPath
}
func applyProjectWorkflowArtifactStorage(project *domain.ProjectRecord, requested **domain.ArtifactStoragePolicy) []validationIssue {
if project == nil {
return nil
}
projectConnectionID := strings.TrimSpace(project.StorageConnectionID)
projectBucket := strings.Trim(strings.TrimSpace(project.StorageBucket), "/")
if projectConnectionID == "" || projectBucket == "" {
return nil
}
policy := &domain.ArtifactStoragePolicy{}
if requested != nil && *requested != nil {
*policy = **requested
}
issues := make([]validationIssue, 0, 2)
if connectionID := strings.TrimSpace(policy.ConnectionID); connectionID != "" && connectionID != projectConnectionID {
issues = append(issues, validationIssue{Field: "artifactStorage.connectionId", Code: "workflow_project_storage_connection_mismatch", Message: "workflow artifact storage must use the project storage connection"})
}
if bucket := strings.Trim(strings.TrimSpace(policy.Bucket), "/"); bucket != "" && bucket != projectBucket {
issues = append(issues, validationIssue{Field: "artifactStorage.bucket", Code: "workflow_project_storage_bucket_mismatch", Message: "workflow artifact storage must use the project bucket"})
}
if len(issues) > 0 {
return issues
}
policy.ConnectionID = projectConnectionID
policy.Bucket = projectBucket
policy.PrefixTemplate = strings.Trim(strings.TrimSpace(policy.PrefixTemplate), "/")
if policy.PrefixTemplate == "" {
policy.PrefixTemplate = defaultProjectWorkflowArtifactPrefix(project)
}
if requested != nil {
*requested = policy
}
return nil
}
func (server *server) validateRunInputConnections(ctx context.Context, organizationID string, inputs map[string]domain.ArtifactPayload) []validationIssue {
issues := make([]validationIssue, 0)
for inputID, payload := range inputs {
if payload.Mode != domain.ArtifactTransferModeRef || payload.Ref == nil {
continue
}
connection, err := server.store.GetStorageConnection(ctx, strings.TrimSpace(payload.Ref.ConnectionID))
if err != nil || connection == nil || normalizedTenantID(connection.OrganizationID) != normalizedTenantID(organizationID) {
issues = append(issues, validationIssue{Field: "inputs." + inputID + ".ref.connectionId", Code: "workflow_input_connection_invalid", Message: "input ref connectionId must reference an accessible storage connection"})
}
}
return issues
}
func validateWorkflowNodeResources(nodes []domain.WorkflowNode) []validationIssue {
issues := make([]validationIssue, 0)
for index, node := range nodes {
field := "nodes[" + strconv.Itoa(index) + "].data.sharedMemorySize"
if err := k8s.ValidateResourceQuantity(field, node.Data.SharedMemorySize); err != nil {
issues = append(issues, validationIssue{Field: field, Message: err.Error()})
}
if node.Data.BusinessStage != "" && !validDataMetricStage(node.Data.BusinessStage) {
issues = append(issues, validationIssue{
Field: "nodes[" + strconv.Itoa(index) + "].data.businessStage",
Message: "invalid business stage",
})
}
}
return issues
}
func (server *server) validateWorkflowOperatorPublication(ctx context.Context, nodes []domain.WorkflowNode, scope domain.WorkflowExecutionScope) []validationIssue {
if normalizeWorkflowExecutionScope(scope) == domain.WorkflowExecutionScopeSandbox {
return nil
}
issues := make([]validationIssue, 0)
for _, node := range nodes {
operatorID := strings.TrimSpace(node.Data.OperatorID)
var source domain.OperatorGenerationSource
var status domain.OperatorPublicationStatus
if operatorID != "" {
operator, err := server.store.GetOperator(ctx, operatorID)
if err != nil {
issues = append(issues, validationIssue{Field: "nodes", Message: "failed to load operator " + operatorID})
continue
}
if operator != nil {
source = effectiveOperatorGenerationSource(*operator)
status = effectiveOperatorPublicationStatus(*operator)
}
}
if source == "" {
source = normalizeOperatorGenerationSource(node.Data.GenerationSource)
}
if status == "" {
status = normalizeOperatorPublicationStatus(node.Data.PublicationStatus)
}
if source != domain.OperatorGenerationSourceAI {
continue
}
if status == "" {
status = domain.OperatorPublicationStatusDraft
}
if status == domain.OperatorPublicationStatusPublished {
continue
}
code := "operator_not_published"
if status == domain.OperatorPublicationStatusDraft {
code = "ai_operator_draft_production_blocked"
}
label := strings.TrimSpace(node.Data.Label)
if label == "" {
label = node.ID
}
issues = append(issues, validationIssue{
Field: "nodes",
Code: code,
Message: "生产工作流不能使用未发布的 AI 生成算子:" + label + "。请在沙盒工作流验证,或补齐真实代码、单测、CCR digest 和验收证据后发布。",
})
}
return issues
}
func (server *server) handleWorkflowsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
return
}
if strings.TrimSpace(item.ArchivedAt) != "" {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流已归档,请先恢复后再编辑", "code": "workflow_archived"})
return
}
domain.NormalizeWorkflowDefinitionMetadata(item)
if item.DefinitionState == domain.WorkflowDefinitionStateReleased {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流定义已发布并锁定,请创建新版本后修改",
"code": "workflow_definition_locked",
})
return
}
active, err := server.store.HasActiveRun(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if active {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流存在进行中的运行,请等待任务结束或停止任务后再编辑",
"code": "workflow_run_active",
})
return
}
var payload workflowPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
name := strings.TrimSpace(*payload.Name)
if name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
item.Name = name
}
if payload.Description != nil {
item.Description = *payload.Description
}
if payload.Type != nil {
workflowType := normalizeWorkflowType(*payload.Type)
if !validWorkflowType(workflowType) {
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be batch or streaming"})
return
}
item.Type = workflowType
}
if payload.Kind != nil {
kind := normalizeWorkflowKind(*payload.Kind)
if !validWorkflowKind(kind) {
renderValidationError(writer, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
return
}
item.Kind = kind
}
if payload.ExecutionScope != nil {
item.ExecutionScope = normalizeWorkflowExecutionScope(*payload.ExecutionScope)
if !validWorkflowExecutionScope(item.ExecutionScope) {
renderValidationError(writer, validationIssue{Field: "executionScope", Message: "executionScope must be production or sandbox"})
return
}
}
item.ExecutionScope = normalizeWorkflowExecutionScope(item.ExecutionScope)
if payload.Status != nil {
status := normalizeWorkflowStatus(*payload.Status)
if !validWorkflowStatus(status) {
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be draft, ready, running, completed, or failed"})
return
}
item.Status = status
}
if payload.Hyperparams != nil {
item.Hyperparams = *payload.Hyperparams
}
if payload.MaxConcurrentRuns != nil {
if issues := validateWorkflowExecutionConfig(*payload.MaxConcurrentRuns, 0, 0, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.MaxConcurrentRuns = *payload.MaxConcurrentRuns
}
if payload.NodeTimeoutSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, *payload.NodeTimeoutSeconds, 0, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.NodeTimeoutSeconds = *payload.NodeTimeoutSeconds
}
if payload.RunTimeoutSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, 0, *payload.RunTimeoutSeconds, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.RunTimeoutSeconds = *payload.RunTimeoutSeconds
}
if payload.MaxRetryAttempts != nil {
if issues := validateWorkflowExecutionConfig(0, 0, 0, *payload.MaxRetryAttempts, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.MaxRetryAttempts = *payload.MaxRetryAttempts
}
if payload.RetryBackoffSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, 0, 0, 0, *payload.RetryBackoffSeconds); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.RetryBackoffSeconds = *payload.RetryBackoffSeconds
}
if payload.Schedule != nil {
item.Schedule = normalizeWorkflowSchedule(item.Schedule, payload.Schedule, nowISO())
if issues := validateWorkflowSchedule(item.Schedule); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
}
if payload.Nodes != nil {
item.Nodes = normalizeWorkflowNodePorts(*payload.Nodes)
}
if payload.Edges != nil {
item.Edges = *payload.Edges
}
if payload.WorkflowInputs != nil {
item.WorkflowInputs = *payload.WorkflowInputs
}
if payload.ArtifactStorage != nil {
if strings.TrimSpace(payload.ArtifactStorage.ConnectionID) == "" && strings.TrimSpace(payload.ArtifactStorage.Bucket) == "" {
item.ArtifactStorage = nil
} else {
item.ArtifactStorage = payload.ArtifactStorage
}
}
if item.Kind == domain.WorkflowKindScan || item.ScanConfig != nil || payload.ScanConfig != nil || domain.FindScanSourceNode(item.Nodes) != nil {
existingScanConfig := item.ScanConfig
effectiveScanConfig := item.ScanConfig
if payload.ScanConfig != nil {
effectiveScanConfig = payload.ScanConfig
}
requestPayload := scanConfigRequestFromSourceNode(item.Name, item.Description, item.Schedule, effectiveScanConfig, item.Nodes)
if issues := validateScanConfigDraftPayload(requestPayload, existingScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, existingScanConfig) {
renderValidationError(writer, validateScanConfigPayload(requestPayload, existingScanConfig)...)
return
}
item.Kind = domain.WorkflowKindScan
item.ScanConfig = server.normalizedScanConfigFromRequest(existingScanConfig, requestPayload)
if issues := server.validateScanConnectionReference(request.Context(), item.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.Schedule = normalizedScanSchedule(item.Schedule, item.ScanConfig)
item.Type = normalizedScanWorkflowType(item.ScanConfig)
item.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(item.MaxConcurrentRuns)
}
if issues := server.validateNodeConnectionReferences(request.Context(), item.Nodes); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowTopology(item.Nodes, item.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowInputContract(item.Nodes, item.Edges, item.WorkflowInputs, false); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateArtifactStoragePolicy(request.Context(), item.OrganizationID, item.ArtifactStorage, "artifactStorage"); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), item.Nodes, item.ExecutionScope); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertWorkflow(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeWorkflow(item))
}
func (server *server) effectiveArtifactStorage(ctx context.Context, workflow *domain.WorkflowRecord) (*domain.ArtifactStoragePolicy, error) {
if workflow != nil && workflow.ArtifactStorage != nil && strings.TrimSpace(workflow.ArtifactStorage.ConnectionID) != "" {
copy := *workflow.ArtifactStorage
return &copy, nil
}
organizationID := ""
if workflow != nil {
organizationID = workflow.OrganizationID
}
settings, err := server.store.GetArtifactStorageSettings(ctx, organizationID)
if err != nil {
return nil, err
}
if strings.TrimSpace(settings.ConnectionID) == "" || strings.TrimSpace(settings.Bucket) == "" {
return nil, nil
}
return &domain.ArtifactStoragePolicy{
ConnectionID: settings.ConnectionID,
Bucket: settings.Bucket,
PrefixTemplate: settings.PrefixTemplate,
}, nil
}
func (server *server) handleWorkflowRelease(writer http.ResponseWriter, request *http.Request) {
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if workflow == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
return
}
if strings.TrimSpace(workflow.ArchivedAt) != "" {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流已归档,请先恢复后再发布", "code": "workflow_archived"})
return
}
domain.NormalizeWorkflowDefinitionMetadata(workflow)
if workflow.DefinitionState == domain.WorkflowDefinitionStateReleased {
renderJSON(writer, http.StatusOK, sanitizeWorkflow(workflow))
return
}
if active, err := server.store.HasActiveRun(request.Context(), workflow.ID); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
} else if active {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流运行中,不能发布定义", "code": "workflow_run_active"})
return
}
issues := validateWorkflowTopology(workflow.Nodes, workflow.Edges)
issues = append(issues, validateWorkflowInputContract(workflow.Nodes, workflow.Edges, workflow.WorkflowInputs, true)...)
if domain.WorkflowScheduleEnabled(workflow.Schedule) {
for index, input := range workflow.WorkflowInputs {
if input.Required && input.DefaultPayload == nil {
issues = append(issues, validationIssue{Field: fmt.Sprintf("workflowInputs[%d].defaultPayload", index), Code: "scheduled_input_default_required", Message: "scheduled workflows require defaults for every required workflow input"})
}
}
}
storage, storageErr := server.effectiveArtifactStorage(request.Context(), workflow)
if storageErr != nil {
renderError(writer, http.StatusInternalServerError, storageErr)
return
}
if storage == nil {
issues = append(issues, validationIssue{Field: "artifactStorage", Code: "artifact_storage_required", Message: "released workflows require platform or workflow artifact storage"})
} else {
issues = append(issues, server.validateArtifactStoragePolicy(request.Context(), workflow.OrganizationID, storage, "artifactStorage")...)
}
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
workflow.UpdatedAt = nowISO()
if err := server.store.UpsertWorkflow(request.Context(), *workflow); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeWorkflow(workflow))
}
func (server *server) handleWorkflowCreateVersion(writer http.ResponseWriter, request *http.Request) {
source, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if source == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, source.OrganizationID, "Workflow") {
return
}
if strings.TrimSpace(source.ArchivedAt) != "" {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "工作流已归档,请先恢复后再创建版本", "code": "workflow_archived"})
return
}
domain.NormalizeWorkflowDefinitionMetadata(source)
if source.DefinitionState != domain.WorkflowDefinitionStateReleased {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "only released workflows can create a new version", "code": "workflow_not_released"})
return
}
created, err := server.store.CreateWorkflow(request.Context(), domain.CreateWorkflowInput{
OrganizationID: source.OrganizationID,
ProjectID: source.ProjectID,
Name: source.Name,
Description: source.Description,
Type: source.Type,
Kind: source.Kind,
Hidden: source.Hidden,
ExecutionScope: source.ExecutionScope,
ScanConfig: source.ScanConfig,
Hyperparams: source.Hyperparams,
MaxConcurrentRuns: source.MaxConcurrentRuns,
NodeTimeoutSeconds: source.NodeTimeoutSeconds,
RunTimeoutSeconds: source.RunTimeoutSeconds,
MaxRetryAttempts: source.MaxRetryAttempts,
RetryBackoffSeconds: source.RetryBackoffSeconds,
Nodes: source.Nodes,
Edges: source.Edges,
Schedule: source.Schedule,
WorkflowInputs: source.WorkflowInputs,
ArtifactStorage: source.ArtifactStorage,
})
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
created.DefinitionVersion = source.DefinitionVersion + 1
created.DefinitionFamilyID = source.DefinitionFamilyID
created.SupersedesWorkflowID = source.ID
created.DefinitionState = domain.WorkflowDefinitionStateDraft
if err := server.store.UpsertWorkflow(request.Context(), *created); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeWorkflow(created))
}
func (server *server) handleWorkflowsDelete(writer http.ResponseWriter, request *http.Request) {
principal, ok := currentPrincipal(request)
if !ok || principal == nil || principal.User.Role != domain.UserRoleAdmin {
renderJSON(writer, http.StatusForbidden, map[string]string{"error": "admin role is required"})
return
}
item, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Workflow") {
return
}
var payload workflowDeleteInput
if err := json.NewDecoder(request.Body).Decode(&payload); err != nil {
renderValidationError(writer, validationIssue{Field: "confirmationName", Message: "请确认工作流名称并填写删除原因"})
return
}
payload.ConfirmationName = strings.TrimSpace(payload.ConfirmationName)
payload.Reason = strings.TrimSpace(payload.Reason)
if payload.ConfirmationName != item.Name || payload.Reason == "" || !payload.DeleteGeneratedArtifacts {
renderValidationError(writer,
validationIssue{Field: "confirmationName", Message: "confirmationName 必须与工作流名称完全一致"},
validationIssue{Field: "reason", Message: "删除原因不能为空"},
validationIssue{Field: "deleteGeneratedArtifacts", Message: "级联删除必须同时清理工作流生成产物"},
)
return
}
preview, runs, err := server.buildWorkflowDeletionPreview(request.Context(), item)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if preview.BlockingRunCount > 0 {
renderJSON(writer, http.StatusConflict, map[string]any{
"error": "工作流仍有正在排队或运行的 Run,请先停止这些 Run 后再删除",
"code": "workflow_run_active",
"preview": preview,
})
return
}
if err := server.deleteWorkflowGeneratedArtifacts(request.Context(), item, runs); err != nil {
renderJSON(writer, http.StatusBadGateway, map[string]string{"error": err.Error(), "code": "workflow_artifact_cleanup_failed"})
return
}
deleted, err := server.store.DeleteWorkflow(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"deleted": true,
"workflowId": item.ID,
"workflowName": item.Name,
"reason": payload.Reason,
"deletedAt": nowISO(),
"preview": preview,
})
}
type runRequest struct {
Trigger domain.RunTrigger `json:"trigger"`
Mode domain.RunMode `json:"mode,omitempty"`
NodeOverrides map[string]domain.NodeOverride `json:"nodeOverrides,omitempty"`
DebugTarget *domain.DebugExecutionTarget `json:"debugTarget,omitempty"`
Inputs map[string]domain.ArtifactPayload `json:"inputs,omitempty"`
}
func validRunTrigger(value domain.RunTrigger) bool {
return value == domain.RunTriggerManual || value == domain.RunTriggerScheduled || value == domain.RunTriggerRetry
}
func (server *server) createRunWithLightweightManifests(ctx context.Context, workflow *domain.WorkflowRecord, trigger domain.RunTrigger, taskIDs ...string) (*domain.RunRecord, error) {
return server.createRunWithInputsAndLightweightManifests(ctx, workflow, trigger, nil, taskIDs...)
}
func (server *server) createRunWithInputsAndLightweightManifests(ctx context.Context, workflow *domain.WorkflowRecord, trigger domain.RunTrigger, inputs map[string]domain.ArtifactPayload, taskIDs ...string) (*domain.RunRecord, error) {
if workflow == nil {
return nil, fmt.Errorf("workflow not found")
}
manifests, err := lightweight.GenerateWorkflowCodeManifests(server.workspaceMountPath, *workflow)
if err != nil {
return nil, err
}
run, err := server.store.CreateRunWithInputs(ctx, workflow.ID, trigger, inputs, taskIDs...)
if err != nil {
return nil, err
}
if len(manifests) == 0 {
return run, nil
}
lightweight.MergeRunMetadata(run, manifests)
run.UpdatedAt = nowISO()
if err := server.store.UpsertRun(ctx, *run); err != nil {
return nil, err
}
return run, nil
}
func renderRunCreationError(writer http.ResponseWriter, err error) bool {
if err == nil {
return false
}
if strings.Contains(err.Error(), "lightweight operator") {
renderValidationError(writer, validationIssue{Field: "runtimeContract.lightweightOperator", Message: err.Error()})
return true
}
return false
}
type rerunSubtreeRequest struct {
NodeID string `json:"nodeId"`
}
func nextRunAttemptForChild(run *domain.RunRecord) int {
if run == nil || run.Attempt <= 0 {
return 2
}
return run.Attempt + 1
}
func (server *server) handleWorkflowRun(writer http.ResponseWriter, request *http.Request) {
payload := runRequest{Trigger: domain.RunTriggerManual}
if request.ContentLength != 0 {
if !decodeJSON(writer, request, &payload) {
return
}
}
if !validRunTrigger(payload.Trigger) {
renderValidationError(writer, validationIssue{Field: "trigger", Message: "trigger must be manual, scheduled, or retry"})
return
}
if payload.Mode != "" && payload.Mode != domain.RunModeProduction && payload.Mode != domain.RunModeDebug {
renderValidationError(writer, validationIssue{Field: "mode", Message: "mode must be production or debug"})
return
}
workflow, err := server.store.GetWorkflow(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if workflow == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
return
}
if strings.TrimSpace(workflow.ArchivedAt) != "" {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流已归档,请先恢复后再运行",
"code": "workflow_archived",
})
return
}
active, err := server.store.HasActiveRun(request.Context(), workflow.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if active {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流已有进行中的运行",
"code": "workflow_run_active",
})
return
}
// Derive effective run mode:
// 1. If mode is not specified: default to debug for draft workflows, or if nodeOverrides/debugTarget are supplied; otherwise production.
// 2. If mode is explicitly set to production on a draft workflow, reject execution.
mode := payload.Mode
if mode == "" {
if workflow.DefinitionState == domain.WorkflowDefinitionStateDraft || workflow.DefinitionState == "" || len(payload.NodeOverrides) > 0 || payload.DebugTarget != nil {
mode = domain.RunModeDebug
} else {
mode = domain.RunModeProduction
}
} else if mode == domain.RunModeProduction && (workflow.DefinitionState == domain.WorkflowDefinitionStateDraft || workflow.DefinitionState == "") {
renderValidationError(writer, validationIssue{
Field: "mode",
Code: "production_run_draft_forbidden",
Message: "cannot trigger production run on draft workflow; publish a workflow release first",
})
return
}
// Validate node overrides and debug target against workflow nodes
nodeMap := make(map[string]domain.WorkflowNode, len(workflow.Nodes))
for _, node := range workflow.Nodes {
nodeMap[node.ID] = node
}
var sanitizedOverrides map[string]domain.NodeOverride
if len(payload.NodeOverrides) > 0 {
sanitizedOverrides = make(map[string]domain.NodeOverride, len(payload.NodeOverrides))
for nodeID, override := range payload.NodeOverrides {
nodeID = strings.TrimSpace(nodeID)
if _, exists := nodeMap[nodeID]; !exists {
// Nonexistent/ghost node IDs are ignored gracefully per TC-T2-10
continue
}
for portKey, inputPayload := range override.Inputs {
if err := domain.ValidateArtifactPayload(inputPayload, inputPayload.Type); err != nil {
renderValidationError(writer, validationIssue{
Field: fmt.Sprintf("nodeOverrides[%s].inputs[%s]", nodeID, portKey),
Code: "workflow_inputs_invalid",
Message: err.Error(),
})
return
}
}
sanitizedOverrides[nodeID] = override
}
}
var sanitizedDebugTarget *domain.DebugExecutionTarget
if payload.DebugTarget != nil {
targetNodeID := strings.TrimSpace(payload.DebugTarget.TargetNodeID)
if targetNodeID != "" {
if _, exists := nodeMap[targetNodeID]; !exists {
renderValidationError(writer, validationIssue{
Field: "debugTarget.targetNodeId",
Code: "debug_target_node_missing",
Message: fmt.Sprintf("debug target node '%s' does not exist in workflow", targetNodeID),
})
return
}
sanitizedDebugTarget = &domain.DebugExecutionTarget{
TargetNodeID: targetNodeID,
SingleNode: payload.DebugTarget.SingleNode,
}
}
}
if workflow.Kind == domain.WorkflowKindScan || workflow.ScanConfig != nil {
requestPayload := scanConfigRequestFromWorkflow(workflow.Name, workflow.Description, workflow.Schedule, workflow.ScanConfig)
if issues := validateScanConfigDraftPayload(requestPayload, workflow.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if !scanConfigPayloadReady(requestPayload, workflow.ScanConfig) {
renderJSON(writer, http.StatusBadRequest, map[string]string{"error": "扫描源配置未完成,请先在扫描源节点配置数据源、目标路径和访问密钥"})
return
}
if issues := server.validateScanConnectionReference(request.Context(), workflow.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
}
if issues := validateWorkflowTopology(workflow.Nodes, workflow.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
// For debug runs or draft workflows, loosen input contract checks (release=false) to allow testing draft DAGs
isReleaseCheck := (mode == domain.RunModeProduction && workflow.DefinitionState == domain.WorkflowDefinitionStateReleased)
if issues := validateWorkflowInputContract(workflow.Nodes, workflow.Edges, workflow.WorkflowInputs, isReleaseCheck); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
resolvedInputs, _, resolveErr := domain.ResolveWorkflowRunInputs(*workflow, payload.Inputs)
if resolveErr != nil {
renderValidationError(writer, validationIssue{Field: "inputs", Code: "workflow_inputs_invalid", Message: resolveErr.Error()})
return
}
if issues := server.validateRunInputConnections(request.Context(), workflow.OrganizationID, resolvedInputs); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if pluginregistry.RequiredByWorkflow(*workflow, pluginregistry.AnnotationQAID) {
enabled, pluginErr := server.annotationPluginEnabled(request.Context(), workflow.OrganizationID)
if pluginErr != nil {
renderError(writer, http.StatusInternalServerError, pluginErr)
return
}
if !enabled {
renderJSON(writer, http.StatusServiceUnavailable, map[string]string{
"error": "Annotation QA plugin is disabled for this organization",
"code": "plugin_disabled",
"pluginId": pluginregistry.AnnotationQAID,
})
return
}
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), workflow.Nodes, normalizeWorkflowExecutionScope(workflow.ExecutionScope)); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
run, err := server.createRunWithInputsAndLightweightManifests(request.Context(), workflow, payload.Trigger, payload.Inputs)
if err != nil {
if renderRunCreationError(writer, err) {
return
}
if strings.Contains(err.Error(), "not found") {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
if errors.Is(err, store.ErrWorkflowRunActive) {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流已有进行中的运行",
"code": "workflow_run_active",
})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
// Attach mode, transient node overrides, and debug target onto RunRecord without mutating WorkflowRecord
run.Mode = mode
if len(sanitizedOverrides) > 0 {
run.NodeOverrides = sanitizedOverrides
}
if sanitizedDebugTarget != nil {
run.DebugTarget = sanitizedDebugTarget
}
run.UpdatedAt = nowISO()
if err := server.store.UpsertRun(request.Context(), *run); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusAccepted, run)
}
func (server *server) handleWorkflowRunsList(writer http.ResponseWriter, request *http.Request) {
workflowID := chi.URLParam(request, "id")
workflow, err := server.store.GetWorkflow(request.Context(), workflowID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if workflow == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
return
}
items, err := server.listRunSummariesForRequest(request.Context(), request, "")
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
summaries := make([]domain.RunSummary, 0, len(items))
for _, item := range items {
if item.WorkflowID != workflowID {
continue
}
if !server.filterByTenant(request, item.OrganizationID) {
continue
}
summaries = append(summaries, item)
}
renderJSON(writer, http.StatusOK, map[string]any{"items": summaries})
}
func (server *server) handleRunsRerunSubtree(writer http.ResponseWriter, request *http.Request) {
sourceRun, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if sourceRun == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, sourceRun.OrganizationID, "Run") {
return
}
if sourceRun.Status != domain.RunStatusFailed {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "只有失败的运行才支持从指定节点重新执行下游"})
return
}
var currentHumanReleasePartition *domain.ReleasePartitionRecord
if sourceRun.Trigger == domain.RunTriggerHumanRelease && sourceRun.Metadata != nil && strings.TrimSpace(sourceRun.Metadata.HumanReleaseUniqueKey) != "" {
currentHumanReleasePartition, err = server.store.GetReleasePartitionByUniqueKey(request.Context(), sourceRun.OrganizationID, sourceRun.Metadata.HumanReleaseUniqueKey)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if currentHumanReleasePartition != nil && currentHumanReleasePartition.ChildRunID != sourceRun.ID {
currentHumanReleasePartition = nil
}
}
active, err := server.store.HasActiveRun(request.Context(), sourceRun.WorkflowID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if active && currentHumanReleasePartition == nil {
renderJSON(writer, http.StatusConflict, map[string]string{
"error": "工作流已有进行中的运行",
"code": "workflow_run_active",
})
return
}
var payload rerunSubtreeRequest
if !decodeJSON(writer, request, &payload) {
return
}
payload.NodeID = strings.TrimSpace(payload.NodeID)
if payload.NodeID == "" {
renderValidationError(writer, validationIssue{Field: "nodeId", Message: "nodeId is required"})
return
}
workflow, err := server.store.GetWorkflow(request.Context(), sourceRun.WorkflowID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if workflow == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Workflow not found"})
return
}
if !server.ensureTenantAccess(writer, request, workflow.OrganizationID, "Workflow") {
return
}
if workflowNodeByID(workflow, payload.NodeID) == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Node not found"})
return
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), workflow.Nodes, normalizeWorkflowExecutionScope(workflow.ExecutionScope)); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
subtree := rerunSubtreeNodeSet(workflow.Edges, payload.NodeID)
baselineSet := rerunBaselineNodeSet(workflow.Edges, subtree)
sourceExecutionMap := make(map[string]domain.NodeExecutionRecord, len(sourceRun.NodeExecutions))
for _, execution := range sourceRun.NodeExecutions {
sourceExecutionMap[execution.NodeID] = execution
}
baselineExecutions := make([]domain.NodeExecutionRecord, 0, len(baselineSet))
for _, execution := range sourceRun.NodeExecutions {
if _, ok := baselineSet[execution.NodeID]; !ok {
continue
}
if execution.Status != domain.NodeExecutionStatusCompleted {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "选定节点缺少可复用的上游成功基线,无法从这里继续向后执行"})
return
}
baselineExecutions = append(baselineExecutions, cloneNodeExecutionRecord(execution))
}
for nodeID := range baselineSet {
execution, ok := sourceExecutionMap[nodeID]
if !ok || execution.Status != domain.NodeExecutionStatusCompleted {
renderJSON(writer, http.StatusConflict, map[string]string{"error": "选定节点缺少可复用的上游成功基线,无法从这里继续向后执行"})
return
}
}
rerunInputs := sourceRun.Inputs
if len(rerunInputs) == 0 && currentHumanReleasePartition != nil && strings.TrimSpace(sourceRun.ParentRunID) != "" {
parentRun, parentErr := server.store.GetRun(request.Context(), sourceRun.ParentRunID)
if parentErr != nil {
renderError(writer, http.StatusInternalServerError, parentErr)
return
}
if parentRun != nil && parentRun.OrganizationID == sourceRun.OrganizationID && parentRun.WorkflowID == sourceRun.WorkflowID {
rerunInputs = parentRun.Inputs
}
}
childRun, err := server.createRunWithInputsAndLightweightManifests(request.Context(), workflow, domain.RunTriggerRerunSubtree, rerunInputs, sourceRun.TaskID)
if err != nil {
if renderRunCreationError(writer, err) {
return
}
if strings.Contains(err.Error(), "not found") {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": err.Error()})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
childRun.ParentRunID = sourceRun.ID
childRun.RestartFromNodeID = payload.NodeID
childRun.Mode = sourceRun.Mode
if childRun.Mode == "" {
childRun.Mode = domain.RunModeProduction
}
childRun.ReleaseID = sourceRun.ReleaseID
childRun.WorkflowVersion = sourceRun.WorkflowVersion
childRun.GitCommitSha = sourceRun.GitCommitSha
childRun.ImageDigest = sourceRun.ImageDigest
childRun.NodeOverrides = sourceRun.NodeOverrides
childRun.Attempt = nextRunAttemptForChild(sourceRun)
childRun.NodeExecutions = baselineExecutions
childRun.K8sRefs = []domain.K8sRef{}
childRun.Error = ""
childRun.CurrentNodeID = ""
childRun.StartedAt = ""
childRun.EndedAt = ""
childRun.DurationMS = 0
childRun.Status = domain.RunStatusQueued
// A subtree rerun starts a new execution timeline. Do not carry the
// parent's terminal failure or stale structured progress into the queued
// child; otherwise the worker's live progress writes are overwritten by
// the parent's last marker during the first scheduling cycles.
childRun.Failure = nil
childRun.Progress = nil
childRun.AvailableAt = nowISO()
childRun.UpdatedAt = nowISO()
if sourceRun.Metadata != nil && strings.TrimSpace(sourceRun.Metadata.HumanReleaseUniqueKey) != "" {
if childRun.Metadata == nil {
childRun.Metadata = &domain.RunMetadata{}
}
childRun.Metadata.HumanReleasePartitionID = sourceRun.Metadata.HumanReleasePartitionID
childRun.Metadata.HumanReleaseUniqueKey = sourceRun.Metadata.HumanReleaseUniqueKey
}
if currentHumanReleasePartition != nil {
// Human-release parents intentionally remain waiting_for_human while
// packaging executes. Preserve the trigger so the worker's scheduler
// admits this partition retry without treating that parent as a
// competing ordinary workflow run.
childRun.Trigger = domain.RunTriggerHumanRelease
}
rebuildRunAggregates(childRun)
if err := server.store.UpsertRun(request.Context(), *childRun); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if currentHumanReleasePartition != nil {
currentHumanReleasePartition.ChildRunID = childRun.ID
currentHumanReleasePartition.Status = domain.ReleasePartitionDispatched
currentHumanReleasePartition.UpdatedAt = nowISO()
if partitionErr := server.store.UpsertReleasePartition(request.Context(), *currentHumanReleasePartition); partitionErr != nil {
renderError(writer, http.StatusInternalServerError, partitionErr)
return
}
}
renderJSON(writer, http.StatusAccepted, childRun)
}
func (server *server) handleTemplatesList(writer http.ResponseWriter, request *http.Request) {
if err := server.ensureFanyuDeliveryTemplate(request.Context(), server.requestOrganizationID(request)); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items, err := server.listTemplatesForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterTemplatesForRequest(request, items)
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeTemplates(items)})
}
func (server *server) handleTemplatesCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateTemplateInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Nodes = normalizeWorkflowNodePorts(payload.Nodes)
payload.Name = strings.TrimSpace(payload.Name)
payload.Type = normalizeWorkflowType(payload.Type)
payload.Kind = normalizeWorkflowKind(payload.Kind)
if payload.Type == "" {
payload.Type = domain.WorkflowTypeBatch
}
issues := make([]validationIssue, 0)
if payload.Name == "" {
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
}
if !validWorkflowType(payload.Type) {
issues = append(issues, validationIssue{Field: "type", Message: "type must be batch or streaming"})
}
if !validWorkflowKind(payload.Kind) {
issues = append(issues, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
}
payload.ExecutionScope = normalizeWorkflowExecutionScope(payload.ExecutionScope)
if !validWorkflowExecutionScope(payload.ExecutionScope) {
issues = append(issues, validationIssue{Field: "executionScope", Message: "executionScope must be production or sandbox"})
}
if configIssues := validateWorkflowExecutionConfig(payload.MaxConcurrentRuns, payload.NodeTimeoutSeconds, payload.RunTimeoutSeconds, payload.MaxRetryAttempts, payload.RetryBackoffSeconds); len(configIssues) > 0 {
issues = append(issues, configIssues...)
}
payload.Schedule = normalizeWorkflowSchedule(nil, payload.Schedule, nowISO())
issues = append(issues, validateWorkflowSchedule(payload.Schedule)...)
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
server.assignTenant(request, &payload.OrganizationID)
if payload.Kind == domain.WorkflowKindScan || payload.ScanConfig != nil || domain.FindScanSourceNode(payload.Nodes) != nil {
requestPayload := scanConfigRequestFromSourceNode(payload.Name, payload.Description, payload.Schedule, payload.ScanConfig, payload.Nodes)
if issues := validateScanConfigDraftPayload(requestPayload); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, nil) {
renderValidationError(writer, validateScanConfigPayload(requestPayload, nil)...)
return
}
payload.Kind = domain.WorkflowKindScan
payload.ScanConfig = server.normalizedScanConfigFromRequest(nil, requestPayload)
if issues := server.validateScanConnectionReference(request.Context(), payload.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
payload.Schedule = normalizedScanSchedule(payload.Schedule, payload.ScanConfig)
payload.Type = normalizedScanWorkflowType(payload.ScanConfig)
payload.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(payload.MaxConcurrentRuns)
}
payload.Nodes = normalizeWorkflowNodePorts(payload.Nodes)
if issues := validateWorkflowTopology(payload.Nodes, payload.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowInputContract(payload.Nodes, payload.Edges, payload.WorkflowInputs, false); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateArtifactStoragePolicy(request.Context(), payload.OrganizationID, payload.ArtifactStorage, "artifactStorage"); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), payload.Nodes, payload.ExecutionScope); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item, err := server.store.CreateTemplate(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeTemplate(item))
}
func (server *server) handleTemplatesGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Template not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
return
}
renderJSON(writer, http.StatusOK, sanitizeTemplate(item))
}
type templatePatch struct {
Name *string `json:"name"`
Description *string `json:"description"`
Type *domain.WorkflowType `json:"type"`
Kind *domain.WorkflowKind `json:"kind"`
ExecutionScope *domain.WorkflowExecutionScope `json:"executionScope"`
ScanConfig *domain.ScanConfig `json:"scanConfig"`
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
NodeTimeoutSeconds *int `json:"nodeTimeoutSeconds"`
RunTimeoutSeconds *int `json:"runTimeoutSeconds"`
MaxRetryAttempts *int `json:"maxRetryAttempts"`
RetryBackoffSeconds *int `json:"retryBackoffSeconds"`
Schedule *domain.WorkflowSchedule `json:"schedule"`
Nodes *[]domain.WorkflowNode `json:"nodes"`
Edges *[]domain.WorkflowEdge `json:"edges"`
WorkflowInputs *[]domain.WorkflowInputDefinition `json:"workflowInputs"`
ArtifactStorage *domain.ArtifactStoragePolicy `json:"artifactStorage"`
}
func (server *server) handleTemplatesUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Template not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
return
}
var payload templatePatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
name := strings.TrimSpace(*payload.Name)
if name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
item.Name = name
}
if payload.Description != nil {
item.Description = *payload.Description
}
if payload.Type != nil {
workflowType := normalizeWorkflowType(*payload.Type)
if !validWorkflowType(workflowType) {
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be batch or streaming"})
return
}
item.Type = workflowType
}
if payload.Kind != nil {
kind := normalizeWorkflowKind(*payload.Kind)
if !validWorkflowKind(kind) {
renderValidationError(writer, validationIssue{Field: "kind", Message: "kind must be standard or scan"})
return
}
item.Kind = kind
}
if payload.ExecutionScope != nil {
item.ExecutionScope = normalizeWorkflowExecutionScope(*payload.ExecutionScope)
if !validWorkflowExecutionScope(item.ExecutionScope) {
renderValidationError(writer, validationIssue{Field: "executionScope", Message: "executionScope must be production or sandbox"})
return
}
}
item.ExecutionScope = normalizeWorkflowExecutionScope(item.ExecutionScope)
if payload.Hyperparams != nil {
item.Hyperparams = *payload.Hyperparams
}
if payload.MaxConcurrentRuns != nil {
if issues := validateWorkflowExecutionConfig(*payload.MaxConcurrentRuns, 0, 0, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.MaxConcurrentRuns = *payload.MaxConcurrentRuns
}
if payload.NodeTimeoutSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, *payload.NodeTimeoutSeconds, 0, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.NodeTimeoutSeconds = *payload.NodeTimeoutSeconds
}
if payload.RunTimeoutSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, 0, *payload.RunTimeoutSeconds, 0, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.RunTimeoutSeconds = *payload.RunTimeoutSeconds
}
if payload.MaxRetryAttempts != nil {
if issues := validateWorkflowExecutionConfig(0, 0, 0, *payload.MaxRetryAttempts, 0); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.MaxRetryAttempts = *payload.MaxRetryAttempts
}
if payload.RetryBackoffSeconds != nil {
if issues := validateWorkflowExecutionConfig(0, 0, 0, 0, *payload.RetryBackoffSeconds); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.RetryBackoffSeconds = *payload.RetryBackoffSeconds
}
if payload.Schedule != nil {
item.Schedule = normalizeWorkflowSchedule(item.Schedule, payload.Schedule, nowISO())
if issues := validateWorkflowSchedule(item.Schedule); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
}
if payload.Nodes != nil {
item.Nodes = normalizeWorkflowNodePorts(*payload.Nodes)
}
if payload.Edges != nil {
item.Edges = *payload.Edges
}
if payload.WorkflowInputs != nil {
item.WorkflowInputs = *payload.WorkflowInputs
}
if payload.ArtifactStorage != nil {
if strings.TrimSpace(payload.ArtifactStorage.ConnectionID) == "" && strings.TrimSpace(payload.ArtifactStorage.Bucket) == "" {
item.ArtifactStorage = nil
} else {
item.ArtifactStorage = payload.ArtifactStorage
}
}
if item.Kind == domain.WorkflowKindScan || item.ScanConfig != nil || payload.ScanConfig != nil || domain.FindScanSourceNode(item.Nodes) != nil {
existingScanConfig := item.ScanConfig
effectiveScanConfig := item.ScanConfig
if payload.ScanConfig != nil {
effectiveScanConfig = payload.ScanConfig
}
requestPayload := scanConfigRequestFromSourceNode(item.Name, item.Description, item.Schedule, effectiveScanConfig, item.Nodes)
if issues := validateScanConfigDraftPayload(requestPayload, existingScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if requestPayload.Interval > 0 && !scanConfigPayloadReady(requestPayload, existingScanConfig) {
renderValidationError(writer, validateScanConfigPayload(requestPayload, existingScanConfig)...)
return
}
item.Kind = domain.WorkflowKindScan
item.ScanConfig = server.normalizedScanConfigFromRequest(existingScanConfig, requestPayload)
if issues := server.validateScanConnectionReference(request.Context(), item.ScanConfig); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.Schedule = normalizedScanSchedule(item.Schedule, item.ScanConfig)
item.Type = normalizedScanWorkflowType(item.ScanConfig)
item.MaxConcurrentRuns = normalizedScanMaxConcurrentRuns(item.MaxConcurrentRuns)
}
if issues := validateWorkflowTopology(item.Nodes, item.Edges); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateWorkflowInputContract(item.Nodes, item.Edges, item.WorkflowInputs, false); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateArtifactStoragePolicy(request.Context(), item.OrganizationID, item.ArtifactStorage, "artifactStorage"); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := server.validateWorkflowOperatorPublication(request.Context(), item.Nodes, item.ExecutionScope); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertTemplate(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeTemplate(item))
}
func (server *server) handleTemplatesDelete(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetTemplate(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Template") {
return
}
deleted, err := server.store.DeleteTemplate(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusNoContent)
}
func (server *server) handleOperatorsList(writer http.ResponseWriter, request *http.Request) {
items, err := server.listAvailableOperators(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterOperatorsForRequest(request, items)
items, err = server.filterOperatorsForInstalledPlugins(request.Context(), server.requestOrganizationID(request), items)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeOperators(items)})
}
func normalizeOperatorType(value string) string {
return strings.ToLower(strings.TrimSpace(value))
}
var operatorVersionPattern = regexp.MustCompile(`^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$`)
var operatorImageDigestPattern = regexp.MustCompile(`^.+@sha256:[a-f0-9]{64}$`)
func normalizeOperatorVersion(value string, fallback string) string {
normalized := strings.TrimSpace(value)
if normalized != "" {
return normalized
}
return strings.TrimSpace(fallback)
}
func validateOperatorVersion(version string) []validationIssue {
if version == "" {
return nil
}
if !operatorVersionPattern.MatchString(version) {
return []validationIssue{{Field: "version", Message: "version must match [A-Za-z0-9_][A-Za-z0-9_.-]{0,127}"}}
}
return nil
}
func normalizeOperatorPublicationStatus(value domain.OperatorPublicationStatus) domain.OperatorPublicationStatus {
return domain.OperatorPublicationStatus(strings.ToLower(strings.TrimSpace(string(value))))
}
func validOperatorPublicationStatus(value domain.OperatorPublicationStatus) bool {
return value == "" ||
value == domain.OperatorPublicationStatusDraft ||
value == domain.OperatorPublicationStatusBuilt ||
value == domain.OperatorPublicationStatusVerified ||
value == domain.OperatorPublicationStatusPublished
}
func normalizeOperatorGenerationSource(value domain.OperatorGenerationSource) domain.OperatorGenerationSource {
return domain.OperatorGenerationSource(strings.ToLower(strings.TrimSpace(string(value))))
}
func validOperatorGenerationSource(value domain.OperatorGenerationSource) bool {
return value == "" || value == domain.OperatorGenerationSourceManual || value == domain.OperatorGenerationSourceAI
}
func runtimeContractMarksAIGenerated(contract map[string]any) bool {
if contract == nil {
return false
}
if source, ok := contract["generationSource"].(string); ok && strings.EqualFold(strings.TrimSpace(source), string(domain.OperatorGenerationSourceAI)) {
return true
}
if _, ok := contract["generatedRepository"]; ok {
return true
}
return false
}
func effectiveOperatorGenerationSource(item domain.OperatorRecord) domain.OperatorGenerationSource {
source := normalizeOperatorGenerationSource(item.GenerationSource)
if source != "" {
return source
}
if runtimeContractMarksAIGenerated(item.RuntimeContract) {
return domain.OperatorGenerationSourceAI
}
return domain.OperatorGenerationSourceManual
}
func effectiveOperatorPublicationStatus(item domain.OperatorRecord) domain.OperatorPublicationStatus {
status := normalizeOperatorPublicationStatus(item.PublicationStatus)
if status != "" {
return status
}
if effectiveOperatorGenerationSource(item) == domain.OperatorGenerationSourceAI {
return domain.OperatorPublicationStatusDraft
}
return domain.OperatorPublicationStatusPublished
}
func publicationEvidenceValue(evidence *domain.OperatorPublicationEvidence, key string) string {
if evidence == nil {
return ""
}
switch key {
case "repositoryURL":
return strings.TrimSpace(evidence.RepositoryURL)
case "repositoryCommit":
return strings.TrimSpace(evidence.RepositoryCommit)
case "image":
return strings.TrimSpace(evidence.Image)
case "imageId":
return strings.TrimSpace(evidence.ImageID)
case "fixtureChecksum":
return strings.TrimSpace(evidence.FixtureChecksum)
case "testCommand":
return strings.TrimSpace(evidence.TestCommand)
case "acceptanceWorkflowId":
return strings.TrimSpace(evidence.AcceptanceWorkflowID)
case "acceptanceRunId":
return strings.TrimSpace(evidence.AcceptanceRunID)
default:
return ""
}
}
func validateOperatorPublicationLifecycle(item domain.OperatorRecord) []validationIssue {
issues := make([]validationIssue, 0)
source := effectiveOperatorGenerationSource(item)
status := effectiveOperatorPublicationStatus(item)
if !validOperatorGenerationSource(source) {
issues = append(issues, validationIssue{Field: "generationSource", Message: "generationSource must be manual or ai"})
}
if !validOperatorPublicationStatus(status) {
issues = append(issues, validationIssue{Field: "publicationStatus", Message: "publicationStatus must be draft, built, verified, or published"})
}
if source != domain.OperatorGenerationSourceAI {
return issues
}
if status == domain.OperatorPublicationStatusDraft {
return issues
}
evidence := item.PublicationEvidence
if publicationEvidenceValue(evidence, "repositoryURL") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.repositoryUrl", Message: "repositoryUrl is required for non-draft AI operators"})
}
if publicationEvidenceValue(evidence, "repositoryCommit") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.repositoryCommit", Message: "repositoryCommit is required for non-draft AI operators"})
}
if strings.TrimSpace(item.Image) == "" && publicationEvidenceValue(evidence, "image") == "" {
issues = append(issues, validationIssue{Field: "image", Message: "image is required for non-draft AI operators"})
}
if status == domain.OperatorPublicationStatusBuilt {
return issues
}
imageID := strings.TrimSpace(item.ImageID)
if imageID == "" {
imageID = publicationEvidenceValue(evidence, "imageId")
}
if !operatorImageDigestPattern.MatchString(imageID) {
issues = append(issues, validationIssue{Field: "imageId", Message: "verified AI operators require digest-pinned imageId"})
}
if publicationEvidenceValue(evidence, "fixtureChecksum") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.fixtureChecksum", Message: "fixtureChecksum is required for verified AI operators"})
}
if publicationEvidenceValue(evidence, "testCommand") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.testCommand", Message: "testCommand is required for verified AI operators"})
}
if status == domain.OperatorPublicationStatusVerified {
return issues
}
if publicationEvidenceValue(evidence, "acceptanceWorkflowId") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.acceptanceWorkflowId", Message: "acceptanceWorkflowId is required for published AI operators"})
}
if publicationEvidenceValue(evidence, "acceptanceRunId") == "" {
issues = append(issues, validationIssue{Field: "publicationEvidence.acceptanceRunId", Message: "acceptanceRunId is required for published AI operators"})
}
return issues
}
func validOperatorType(value string) bool {
return value == "builtin" || value == "custom" || value == "lightweight"
}
func validPortType(value domain.PortType) bool {
switch value {
case domain.PortTypeJSON, domain.PortTypeImage, domain.PortTypeVideo, domain.PortTypeTensor, domain.PortTypeFile:
return true
default:
return false
}
}
func validPortTransmission(value string) bool {
switch value {
case "", "bus", "oss", "shm", "stream":
return true
default:
return false
}
}
func normalizeWorkflowNodePorts(items []domain.WorkflowNode) []domain.WorkflowNode {
if items == nil {
return nil
}
normalized := make([]domain.WorkflowNode, len(items))
copy(normalized, items)
for index := range normalized {
normalized[index].Data.Inputs = normalizeWorkflowPortDefinitions(normalized[index].Data.Inputs)
normalized[index].Data.Outputs = normalizeWorkflowPortDefinitions(normalized[index].Data.Outputs)
}
return normalized
}
func normalizeWorkflowPortDefinitions(items []domain.PortDefinition) []domain.PortDefinition {
if items == nil {
return nil
}
normalized := make([]domain.PortDefinition, 0, len(items))
for _, item := range items {
item.ID = strings.TrimSpace(item.ID)
item.Name = strings.TrimSpace(item.Name)
item.Type = domain.PortType(strings.ToLower(strings.TrimSpace(string(item.Type))))
item.Description = strings.TrimSpace(item.Description)
item.Transmission = strings.ToLower(strings.TrimSpace(item.Transmission))
item.Container = domain.PortContainer(strings.ToLower(strings.TrimSpace(string(item.Container))))
item.PayloadMode = domain.ArtifactTransferMode(strings.ToLower(strings.TrimSpace(string(item.PayloadMode))))
item.Role = strings.ToLower(strings.TrimSpace(item.Role))
normalized = append(normalized, item)
}
return normalized
}
func validateWorkflowPortDefinitions(nodes []domain.WorkflowNode) []validationIssue {
issues := make([]validationIssue, 0)
for nodeIndex, node := range nodes {
for direction, ports := range map[string][]domain.PortDefinition{"inputs": node.Data.Inputs, "outputs": node.Data.Outputs} {
for portIndex, port := range ports {
prefix := fmt.Sprintf("nodes[%d].data.%s[%d]", nodeIndex, direction, portIndex)
if strings.TrimSpace(string(port.Container)) != "" && !domain.ValidPortContainer(port.Container) {
issues = append(issues, validationIssue{Field: prefix + ".container", Code: "workflow_port_container_invalid", Message: "container must be single, collection, directory, or dataset"})
}
if strings.TrimSpace(string(port.PayloadMode)) != "" && !domain.ValidPortPayloadMode(port.PayloadMode) {
issues = append(issues, validationIssue{Field: prefix + ".payloadMode", Code: "workflow_port_payload_mode_invalid", Message: "payloadMode must be inline or ref"})
}
}
}
}
return issues
}
func normalizePortDefinitions(items []domain.PortDefinition) []domain.PortDefinition {
return domain.NormalizePortDefinitions(items)
}
func validatePortDefinitions(field string, items []domain.PortDefinition) []validationIssue {
issues := make([]validationIssue, 0)
seenIDs := make(map[string]struct{}, len(items))
for index, item := range items {
prefix := field + "[" + strconv.Itoa(index) + "]"
if item.ID == "" {
issues = append(issues, validationIssue{Field: prefix + ".id", Message: "id is required"})
} else if _, exists := seenIDs[item.ID]; exists {
issues = append(issues, validationIssue{Field: prefix + ".id", Message: "id must be unique"})
} else {
seenIDs[item.ID] = struct{}{}
}
if item.Name == "" {
issues = append(issues, validationIssue{Field: prefix + ".name", Message: "name is required"})
}
if !validPortType(item.Type) {
issues = append(issues, validationIssue{Field: prefix + ".type", Message: "type must be json, image, video, tensor, or file"})
}
if !domain.ValidPortContainer(item.Container) {
issues = append(issues, validationIssue{Field: prefix + ".container", Message: "container must be single, collection, directory, or dataset"})
}
if !domain.ValidPortPayloadMode(item.PayloadMode) {
issues = append(issues, validationIssue{Field: prefix + ".payloadMode", Message: "payloadMode must be inline or ref"})
}
if !validPortTransmission(item.Transmission) {
issues = append(issues, validationIssue{Field: prefix + ".transmission", Message: "transmission must be bus, oss, shm, or stream"})
}
}
return issues
}
func normalizeOperatorResources(resources *domain.OperatorResources) *domain.OperatorResources {
if resources == nil {
return nil
}
gpuType := strings.TrimSpace(resources.GPUType)
gpuLimit := strings.TrimSpace(resources.GPULimit)
if gpuLimit != "" && gpuType == "" {
gpuType = "NVIDIA GPU"
}
return &domain.OperatorResources{
CPURequest: strings.TrimSpace(resources.CPURequest),
CPULimit: strings.TrimSpace(resources.CPULimit),
MemoryRequest: strings.TrimSpace(resources.MemoryRequest),
MemoryLimit: strings.TrimSpace(resources.MemoryLimit),
GPUType: gpuType,
GPULimit: gpuLimit,
SharedMemorySize: strings.TrimSpace(resources.SharedMemorySize),
}
}
func normalizeContainerCommand(items []string) []string {
if len(items) == 0 {
return nil
}
normalized := make([]string, 0, len(items))
for _, item := range items {
value := strings.TrimSpace(item)
if value != "" {
normalized = append(normalized, value)
}
}
if len(normalized) == 0 {
return nil
}
return normalized
}
func normalizeLightweightOperatorContract(contract *map[string]any, commandConfigured bool, argsConfigured bool) {
if contract == nil {
return
}
*contract = lightweight.NormalizeContract(*contract, commandConfigured, argsConfigured)
}
func stringMapValue(items map[string]any, key string) string {
value, ok := items[key]
if !ok || value == nil {
return ""
}
return fmt.Sprint(value)
}
func validateContainerCommand(field string, items []string) []validationIssue {
issues := make([]validationIssue, 0)
for index, item := range items {
prefix := field + "[" + strconv.Itoa(index) + "]"
if strings.TrimSpace(item) == "" {
issues = append(issues, validationIssue{Field: prefix, Message: "value is required"})
continue
}
if len(item) > 512 {
issues = append(issues, validationIssue{Field: prefix, Message: "value must be at most 512 characters"})
}
if strings.ContainsAny(item, "\x00\r\n") {
issues = append(issues, validationIssue{Field: prefix, Message: "value must not contain control characters"})
}
}
return issues
}
func validateLightweightOperatorMetadata(image string, contract map[string]any, containerCommand []string, containerArgs []string, workspaceMountPath string) []validationIssue {
config := lightweight.ConfigFromContract(contract)
issues := make([]validationIssue, 0)
if strings.TrimSpace(image) == "" {
issues = append(issues, validationIssue{Field: "image", Message: "base image is required for lightweight operators"})
}
if strings.TrimSpace(config.CodeSubPath) == "" {
issues = append(issues, validationIssue{Field: "runtimeContract.lightweightOperator.codeSubPath", Message: "codeSubPath is required"})
}
if strings.TrimSpace(config.MountPath) == "" {
issues = append(issues, validationIssue{Field: "runtimeContract.lightweightOperator.mountPath", Message: "mountPath is required"})
} else if !strings.HasPrefix(strings.TrimSpace(config.MountPath), "/") {
issues = append(issues, validationIssue{Field: "runtimeContract.lightweightOperator.mountPath", Message: "mountPath must be an absolute path"})
}
if len(containerCommand) == 0 {
issues = append(issues, validationIssue{Field: "containerCommand", Message: "containerCommand is required for lightweight operators"})
}
if len(containerArgs) == 0 && !config.ContainerArgsConfigured {
issues = append(issues, validationIssue{Field: "containerArgs", Message: "containerArgs must be explicitly configured; use [] when no args are needed"})
}
if err := lightweight.ValidateRuntimeConfig(lightweight.RuntimeConfig{
Image: image,
CodeSubPath: config.CodeSubPath,
MountPath: config.MountPath,
Command: containerCommand,
Args: containerArgs,
ArgsConfigured: config.ContainerArgsConfigured,
WorkspaceRoot: workspaceMountPath,
Mounts: config.Mounts,
}); err != nil {
issues = append(issues, validationIssue{Field: "runtimeContract.lightweightOperator", Message: err.Error()})
}
return issues
}
func validateOperatorResources(resources *domain.OperatorResources) []validationIssue {
if resources == nil {
return nil
}
for _, item := range []struct {
field string
value string
}{
{field: "resources.cpuRequest", value: resources.CPURequest},
{field: "resources.cpuLimit", value: resources.CPULimit},
{field: "resources.memoryRequest", value: resources.MemoryRequest},
{field: "resources.memoryLimit", value: resources.MemoryLimit},
{field: "resources.sharedMemorySize", value: resources.SharedMemorySize},
} {
if err := k8s.ValidateResourceQuantity(item.field, item.value); err != nil {
return []validationIssue{{Field: item.field, Message: err.Error()}}
}
}
if err := k8s.ValidateGPURequest(resources.GPULimit); err != nil {
return []validationIssue{{Field: "resources.gpuLimit", Message: err.Error()}}
}
if len(resources.GPUType) > 80 {
return []validationIssue{{Field: "resources.gpuType", Message: "gpuType must be at most 80 characters"}}
}
return nil
}
func validateOperatorMetadata(name string, operatorType string, category string, description string, version string, inputs []domain.PortDefinition, outputs []domain.PortDefinition, resources *domain.OperatorResources, containerCommand []string, containerArgs []string) []validationIssue {
issues := make([]validationIssue, 0)
if name == "" {
issues = append(issues, validationIssue{Field: "name", Message: "name is required"})
}
if !validOperatorType(operatorType) {
issues = append(issues, validationIssue{Field: "type", Message: "type must be builtin, custom, or lightweight"})
}
if category == "" {
issues = append(issues, validationIssue{Field: "category", Message: "category is required"})
}
if description == "" {
issues = append(issues, validationIssue{Field: "description", Message: "description is required"})
}
issues = append(issues, validateOperatorVersion(version)...)
issues = append(issues, validatePortDefinitions("inputs", inputs)...)
issues = append(issues, validatePortDefinitions("outputs", outputs)...)
issues = append(issues, validateOperatorResources(resources)...)
issues = append(issues, validateContainerCommand("containerCommand", containerCommand)...)
issues = append(issues, validateContainerCommand("containerArgs", containerArgs)...)
return issues
}
func (server *server) handleOperatorsCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateOperatorInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name = strings.TrimSpace(payload.Name)
payload.Type = normalizeOperatorType(payload.Type)
if payload.Type == "" {
payload.Type = "custom"
}
containerCommandProvided := payload.ContainerCommand != nil
containerArgsProvided := payload.ContainerArgs != nil
payload.Category = strings.TrimSpace(payload.Category)
payload.Description = strings.TrimSpace(payload.Description)
payload.Inputs = normalizePortDefinitions(payload.Inputs)
payload.Outputs = normalizePortDefinitions(payload.Outputs)
payload.Version = normalizeOperatorVersion(payload.Version, "v1")
payload.Image = strings.TrimSpace(payload.Image)
payload.ImageID = strings.TrimSpace(payload.ImageID)
payload.ContainerCommand = normalizeContainerCommand(payload.ContainerCommand)
payload.ContainerArgs = normalizeContainerCommand(payload.ContainerArgs)
payload.SkillDefinition = strings.TrimSpace(payload.SkillDefinition)
payload.Resources = normalizeOperatorResources(payload.Resources)
payload.GenerationSource = normalizeOperatorGenerationSource(payload.GenerationSource)
if payload.GenerationSource == "" {
if runtimeContractMarksAIGenerated(payload.RuntimeContract) {
payload.GenerationSource = domain.OperatorGenerationSourceAI
} else {
payload.GenerationSource = domain.OperatorGenerationSourceManual
}
}
payload.PublicationStatus = normalizeOperatorPublicationStatus(payload.PublicationStatus)
if payload.PublicationStatus == "" {
if payload.GenerationSource == domain.OperatorGenerationSourceAI {
payload.PublicationStatus = domain.OperatorPublicationStatusDraft
} else {
payload.PublicationStatus = domain.OperatorPublicationStatusPublished
}
}
if payload.ImageID == "" {
payload.ImageID = payload.Image
}
if payload.Type == "lightweight" {
normalizeLightweightOperatorContract(&payload.RuntimeContract, containerCommandProvided, containerArgsProvided)
}
issues := validateOperatorMetadata(payload.Name, payload.Type, payload.Category, payload.Description, payload.Version, payload.Inputs, payload.Outputs, payload.Resources, payload.ContainerCommand, payload.ContainerArgs)
if payload.BusinessStage != "" && !validDataMetricStage(payload.BusinessStage) {
issues = append(issues, validationIssue{Field: "businessStage", Message: "invalid business stage"})
}
if payload.Type == "lightweight" {
issues = append(issues, validateLightweightOperatorMetadata(payload.Image, payload.RuntimeContract, payload.ContainerCommand, payload.ContainerArgs, server.workspaceMountPath)...)
}
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateOperatorPublicationLifecycle(domain.OperatorRecord{
Name: payload.Name,
Type: payload.Type,
Image: payload.Image,
ImageID: payload.ImageID,
RuntimeContract: payload.RuntimeContract,
PublicationStatus: payload.PublicationStatus,
GenerationSource: payload.GenerationSource,
PublicationEvidence: payload.PublicationEvidence,
}); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if payload.Type != "custom" && payload.Type != "lightweight" {
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be custom or lightweight when registering an operator"})
return
}
server.assignTenant(request, &payload.OrganizationID)
item, err := server.store.CreateOperator(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeOperator(item))
}
func (server *server) handleOperatorsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetOperator(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Operator not found"})
return
}
if !server.ensureOperatorAccess(writer, request, *item) {
return
}
renderJSON(writer, http.StatusOK, sanitizeOperator(item))
}
type operatorPatch struct {
Name *string `json:"name"`
Type *string `json:"type"`
Category *string `json:"category"`
Description *string `json:"description"`
Inputs *[]domain.PortDefinition `json:"inputs"`
Outputs *[]domain.PortDefinition `json:"outputs"`
ConfigSchema *map[string]any `json:"configSchema"`
Version *string `json:"version"`
Image *string `json:"image"`
ImageID *string `json:"imageId"`
ContainerCommand *[]string `json:"containerCommand"`
ContainerArgs *[]string `json:"containerArgs"`
RuntimeContract *map[string]any `json:"runtimeContract"`
SkillDefinition *string `json:"skillDefinition"`
Hyperparams *[]domain.HyperParam `json:"hyperparams"`
Resources *domain.OperatorResources `json:"resources"`
BusinessStage *domain.DataMetricStage `json:"businessStage"`
PublicationStatus *domain.OperatorPublicationStatus `json:"publicationStatus"`
GenerationSource *domain.OperatorGenerationSource `json:"generationSource"`
PublicationEvidence *domain.OperatorPublicationEvidence `json:"publicationEvidence"`
}
func (server *server) handleOperatorsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetOperator(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Operator not found"})
return
}
if !server.ensureOperatorAccess(writer, request, *item) {
return
}
var payload operatorPatch
if !decodeJSON(writer, request, &payload) {
return
}
next := *item
if payload.Name != nil {
next.Name = strings.TrimSpace(*payload.Name)
}
if payload.Type != nil {
operatorType := normalizeOperatorType(*payload.Type)
if !validOperatorType(operatorType) {
renderValidationError(writer, validationIssue{Field: "type", Message: "type must be builtin, custom, or lightweight"})
return
}
if operatorType != normalizeOperatorType(item.Type) {
renderValidationError(writer, validationIssue{Field: "type", Message: "type cannot be changed"})
return
}
next.Type = operatorType
}
if payload.Category != nil {
next.Category = strings.TrimSpace(*payload.Category)
}
if payload.Description != nil {
next.Description = strings.TrimSpace(*payload.Description)
}
if payload.Inputs != nil {
next.Inputs = normalizePortDefinitions(*payload.Inputs)
}
if payload.Outputs != nil {
next.Outputs = normalizePortDefinitions(*payload.Outputs)
}
if payload.ConfigSchema != nil {
next.ConfigSchema = *payload.ConfigSchema
}
if payload.Version != nil {
next.Version = normalizeOperatorVersion(*payload.Version, "")
}
if payload.Image != nil {
next.Image = strings.TrimSpace(*payload.Image)
}
if payload.ImageID != nil {
next.ImageID = strings.TrimSpace(*payload.ImageID)
} else if payload.Image != nil && next.ImageID == "" {
next.ImageID = next.Image
}
if payload.ContainerCommand != nil {
next.ContainerCommand = normalizeContainerCommand(*payload.ContainerCommand)
}
if payload.ContainerArgs != nil {
next.ContainerArgs = normalizeContainerCommand(*payload.ContainerArgs)
}
if payload.RuntimeContract != nil {
next.RuntimeContract = *payload.RuntimeContract
}
if payload.SkillDefinition != nil {
next.SkillDefinition = strings.TrimSpace(*payload.SkillDefinition)
}
if payload.Hyperparams != nil {
next.Hyperparams = *payload.Hyperparams
}
if payload.Resources != nil {
next.Resources = normalizeOperatorResources(payload.Resources)
}
if payload.BusinessStage != nil {
if *payload.BusinessStage != "" && !validDataMetricStage(*payload.BusinessStage) {
renderValidationError(writer, validationIssue{Field: "businessStage", Message: "invalid business stage"})
return
}
next.BusinessStage = *payload.BusinessStage
}
if payload.GenerationSource != nil {
next.GenerationSource = normalizeOperatorGenerationSource(*payload.GenerationSource)
}
if next.GenerationSource == "" {
if runtimeContractMarksAIGenerated(next.RuntimeContract) {
next.GenerationSource = domain.OperatorGenerationSourceAI
} else {
next.GenerationSource = domain.OperatorGenerationSourceManual
}
}
if payload.PublicationStatus != nil {
next.PublicationStatus = normalizeOperatorPublicationStatus(*payload.PublicationStatus)
}
if next.PublicationStatus == "" {
if next.GenerationSource == domain.OperatorGenerationSourceAI {
next.PublicationStatus = domain.OperatorPublicationStatusDraft
} else {
next.PublicationStatus = domain.OperatorPublicationStatusPublished
}
}
if payload.PublicationEvidence != nil {
next.PublicationEvidence = payload.PublicationEvidence
}
if normalizeOperatorType(next.Type) == "lightweight" {
normalizeLightweightOperatorContract(&next.RuntimeContract, payload.ContainerCommand != nil, payload.ContainerArgs != nil)
}
if normalizeOperatorType(next.Type) == "custom" || normalizeOperatorType(next.Type) == "lightweight" {
next.Version = normalizeOperatorVersion(next.Version, "v1")
if next.ImageID == "" {
next.ImageID = next.Image
}
}
issues := validateOperatorMetadata(next.Name, next.Type, next.Category, next.Description, next.Version, next.Inputs, next.Outputs, next.Resources, next.ContainerCommand, next.ContainerArgs)
if normalizeOperatorType(next.Type) == "lightweight" {
issues = append(issues, validateLightweightOperatorMetadata(next.Image, next.RuntimeContract, next.ContainerCommand, next.ContainerArgs, server.workspaceMountPath)...)
}
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
if issues := validateOperatorPublicationLifecycle(next); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
next.UpdatedAt = nowISO()
if err := server.store.UpsertOperator(request.Context(), next); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeOperator(&next))
}
func (server *server) handleOperatorsDelete(writer http.ResponseWriter, request *http.Request) {
id := chi.URLParam(request, "id")
item, err := server.store.GetOperator(request.Context(), id)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureOperatorAccess(writer, request, *item) {
return
}
if strings.EqualFold(strings.TrimSpace(item.Type), "builtin") {
if err := server.store.UpsertBuiltinOperatorDeletion(request.Context(), domain.BuiltinOperatorDeletionRecord{
Key: builtinOperatorDeletionKey(item.ID, item.Name),
OrganizationID: normalizedTenantID(item.OrganizationID),
OperatorID: item.ID,
OperatorName: item.Name,
DeletedAt: nowISO(),
}); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
deleted, err := server.store.DeleteOperator(request.Context(), id)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusNoContent)
}
const (
lightweightOperatorFileListLimit = 1000
lightweightOperatorEditableFileMaxByte = 2 * 1024 * 1024
lightweightOperatorUploadMaxByte = 64 * 1024 * 1024
)
type lightweightOperatorCodeRoot struct {
Name string
CodeSubPath string
MountPath string
WorkspacePath string
RootPath string
RootRealPath string
WorkspaceReal string
Exists bool
ReadOnly bool
}
type lightweightOperatorFileItem struct {
Path string `json:"path"`
Name string `json:"name"`
Kind string `json:"kind"`
Depth int `json:"depth"`
SizeBytes int64 `json:"sizeBytes,omitempty"`
ModTime string `json:"modTime,omitempty"`
Mode string `json:"mode,omitempty"`
Editable bool `json:"editable"`
}
type lightweightOperatorFilesResponse struct {
OperatorID string `json:"operatorId"`
CodeSubPath string `json:"codeSubPath"`
MountPath string `json:"mountPath"`
WorkspacePath string `json:"workspacePath"`
Exists bool `json:"exists"`
ReadOnly bool `json:"readOnly"`
FileCount int `json:"fileCount"`
DirectoryCount int `json:"directoryCount"`
TotalItems int `json:"totalItems"`
PageOffset int `json:"pageOffset"`
NextOffset int `json:"nextOffset,omitempty"`
Truncated bool `json:"truncated"`
MaxItems int `json:"maxItems"`
Items []lightweightOperatorFileItem `json:"items"`
}
type lightweightOperatorFileContentResponse struct {
OperatorID string `json:"operatorId"`
Path string `json:"path"`
Name string `json:"name"`
Content string `json:"content"`
Encoding string `json:"encoding"`
SizeBytes int64 `json:"sizeBytes"`
ModTime string `json:"modTime"`
Mode string `json:"mode"`
CodeSubPath string `json:"codeSubPath"`
MountPath string `json:"mountPath"`
}
type lightweightOperatorFileContentPayload struct {
Path string `json:"path"`
Content string `json:"content"`
}
type lightweightOperatorPathPayload struct {
Path string `json:"path"`
}
type lightweightOperatorMovePayload struct {
Path string `json:"path"`
NewPath string `json:"newPath"`
}
type lightweightOperatorMutationResponse struct {
OperatorID string `json:"operatorId"`
Path string `json:"path"`
NewPath string `json:"newPath,omitempty"`
Kind string `json:"kind"`
CodeSubPath string `json:"codeSubPath"`
MountPath string `json:"mountPath"`
Created bool `json:"created,omitempty"`
Updated bool `json:"updated,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Moved bool `json:"moved,omitempty"`
Uploaded bool `json:"uploaded,omitempty"`
SizeBytes int64 `json:"sizeBytes,omitempty"`
}
func (server *server) loadLightweightOperatorForFileRequest(writer http.ResponseWriter, request *http.Request) (*domain.OperatorRecord, bool) {
item, err := server.store.GetOperator(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return nil, false
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Operator not found"})
return nil, false
}
if !server.ensureOperatorAccess(writer, request, *item) {
return nil, false
}
if normalizeOperatorType(item.Type) != "lightweight" {
renderValidationError(writer, validationIssue{Field: "type", Message: "operator must be lightweight"})
return nil, false
}
return item, true
}
func (server *server) resolveLightweightOperatorCodeRoot(item domain.OperatorRecord, mountName string) (lightweightOperatorCodeRoot, *validationIssue, error) {
config := lightweight.ConfigFromContract(item.RuntimeContract)
if err := lightweight.ValidateRuntimeConfig(lightweight.RuntimeConfig{
NodeID: item.ID,
NodeLabel: item.Name,
Image: item.Image,
CodeSubPath: config.CodeSubPath,
MountPath: config.MountPath,
Command: item.ContainerCommand,
Args: item.ContainerArgs,
ArgsConfigured: config.ContainerArgsConfigured,
WorkspaceRoot: server.workspaceMountPath,
Mounts: config.Mounts,
}); err != nil {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "runtimeContract.lightweightOperator", Message: err.Error()}, nil
}
workspaceRoot := strings.TrimSpace(server.workspaceMountPath)
if workspaceRoot == "" {
workspaceRoot = "/workspace"
}
workspaceRoot = filepath.Clean(workspaceRoot)
if !filepath.IsAbs(workspaceRoot) {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "workspaceMountPath", Message: "workspace mount path must be absolute"}, nil
}
workspaceReal, err := filepath.EvalSymlinks(workspaceRoot)
if err != nil {
return lightweightOperatorCodeRoot{}, nil, fmt.Errorf("workspace root is not readable: %w", err)
}
selectedName := strings.TrimSpace(mountName)
selectedSubPath := config.CodeSubPath
selectedMountPath := config.MountPath
selectedReadOnly := config.CodeReadOnly
if selectedName != "" && selectedName != "code" {
found := false
for _, mount := range config.Mounts {
if mount.Name != selectedName {
continue
}
found = true
if !mount.IDEEnabled {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "mount", Message: "file IDE is disabled for this mount"}, nil
}
if mount.PVCName != strings.TrimSpace(server.workspacePVCName) {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "mount", Message: "PVC is not mounted into the file management service"}, nil
}
selectedSubPath = mount.SubPath
selectedMountPath = mount.MountPath
selectedReadOnly = mount.ReadOnly
break
}
if !found {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "mount", Message: "configured mount was not found"}, nil
}
} else {
selectedName = "code"
}
rootPath := filepath.Clean(filepath.Join(workspaceRoot, filepath.FromSlash(selectedSubPath)))
if !localPathInside(rootPath, workspaceRoot) {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "runtimeContract.lightweightOperator.codeSubPath", Message: "codeSubPath must stay within the workspace PVC"}, nil
}
root := lightweightOperatorCodeRoot{
Name: selectedName,
CodeSubPath: selectedSubPath,
MountPath: selectedMountPath,
WorkspacePath: filepath.ToSlash(rootPath),
RootPath: rootPath,
WorkspaceReal: workspaceReal,
ReadOnly: selectedReadOnly,
}
info, err := os.Lstat(rootPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return root, nil, nil
}
return lightweightOperatorCodeRoot{}, nil, fmt.Errorf("code directory is not readable: %w", err)
}
if info.Mode()&os.ModeSymlink != 0 {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "runtimeContract.lightweightOperator.codeSubPath", Message: "code directory must not be a symlink"}, nil
}
if !info.IsDir() {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "runtimeContract.lightweightOperator.codeSubPath", Message: "codeSubPath must point to a directory"}, nil
}
rootReal, err := filepath.EvalSymlinks(rootPath)
if err != nil {
return lightweightOperatorCodeRoot{}, nil, fmt.Errorf("code directory is not readable: %w", err)
}
if !localPathInside(rootReal, workspaceReal) {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "runtimeContract.lightweightOperator.codeSubPath", Message: "code directory escapes workspace root"}, nil
}
root.RootRealPath = rootReal
root.Exists = true
return root, nil, nil
}
func (server *server) ensureLightweightOperatorCodeRoot(item domain.OperatorRecord, mountName string) (lightweightOperatorCodeRoot, *validationIssue, error) {
root, issue, err := server.resolveLightweightOperatorCodeRoot(item, mountName)
if err != nil || issue != nil || root.Exists {
return root, issue, err
}
if root.ReadOnly {
return lightweightOperatorCodeRoot{}, &validationIssue{Field: "mount", Message: "read-only mount cannot be created or modified"}, nil
}
if err := os.MkdirAll(root.RootPath, 0o755); err != nil {
return lightweightOperatorCodeRoot{}, nil, fmt.Errorf("code directory cannot be created: %w", err)
}
return server.resolveLightweightOperatorCodeRoot(item, mountName)
}
func cleanLightweightOperatorFilePath(value string) (string, error) {
raw := strings.TrimSpace(value)
if raw == "" {
return "", fmt.Errorf("path is required")
}
if strings.ContainsAny(raw, "\x00\r\n") {
return "", fmt.Errorf("path must not contain control characters")
}
raw = filepath.ToSlash(raw)
if strings.HasPrefix(raw, "/") {
return "", fmt.Errorf("path must be relative")
}
cleaned := path.Clean(strings.Trim(raw, "/"))
if cleaned == "." || cleaned == ".." || strings.HasPrefix(cleaned, "../") || strings.Contains(cleaned, "/../") {
return "", fmt.Errorf("path must stay within the code directory")
}
return cleaned, nil
}
func localPathInside(value string, root string) bool {
value = filepath.Clean(value)
root = filepath.Clean(root)
rel, err := filepath.Rel(root, value)
if err != nil {
return false
}
return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)))
}
func lightweightOperatorFileItemFromPath(root lightweightOperatorCodeRoot, itemPath string, entry os.DirEntry) (lightweightOperatorFileItem, error) {
info, err := entry.Info()
if err != nil {
return lightweightOperatorFileItem{}, err
}
rel, err := filepath.Rel(root.RootPath, itemPath)
if err != nil {
return lightweightOperatorFileItem{}, err
}
rel = filepath.ToSlash(rel)
kind := "other"
if info.Mode()&os.ModeSymlink != 0 {
kind = "symlink"
} else if info.IsDir() {
kind = "directory"
} else if info.Mode().IsRegular() {
kind = "file"
}
return lightweightOperatorFileItem{
Path: rel,
Name: path.Base(rel),
Kind: kind,
Depth: strings.Count(rel, "/"),
SizeBytes: info.Size(),
ModTime: info.ModTime().UTC().Format(time.RFC3339Nano),
Mode: info.Mode().String(),
Editable: kind == "file" && info.Size() <= lightweightOperatorEditableFileMaxByte,
}, nil
}
func (server *server) handleOperatorLightweightFilesList(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
root, issue, err := server.resolveLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
offset := 0
if raw := strings.TrimSpace(request.URL.Query().Get("offset")); raw != "" {
parsed, parseErr := strconv.Atoi(raw)
if parseErr != nil || parsed < 0 {
renderValidationError(writer, validationIssue{Field: "offset", Message: "offset must be a non-negative integer"})
return
}
offset = parsed
}
response := lightweightOperatorFilesResponse{
OperatorID: item.ID,
CodeSubPath: root.CodeSubPath,
MountPath: root.MountPath,
WorkspacePath: root.WorkspacePath,
Exists: root.Exists,
ReadOnly: root.ReadOnly,
PageOffset: offset,
MaxItems: lightweightOperatorFileListLimit,
Items: []lightweightOperatorFileItem{},
}
if !root.Exists {
renderJSON(writer, http.StatusOK, response)
return
}
err = filepath.WalkDir(root.RootPath, func(itemPath string, entry os.DirEntry, walkErr error) error {
if walkErr != nil {
return walkErr
}
if itemPath == root.RootPath {
return nil
}
fileItem, err := lightweightOperatorFileItemFromPath(root, itemPath, entry)
if err != nil {
return err
}
switch fileItem.Kind {
case "directory":
response.DirectoryCount++
case "file":
response.FileCount++
}
itemIndex := response.TotalItems
response.TotalItems++
if itemIndex >= offset && len(response.Items) < lightweightOperatorFileListLimit {
response.Items = append(response.Items, fileItem)
}
return nil
})
if err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("code directory cannot be scanned: %w", err))
return
}
consumed := offset + len(response.Items)
if consumed < response.TotalItems {
response.Truncated = true
response.NextOffset = consumed
}
sort.Slice(response.Items, func(left int, right int) bool {
leftItem := response.Items[left]
rightItem := response.Items[right]
if leftItem.Path == rightItem.Path {
return leftItem.Kind < rightItem.Kind
}
return leftItem.Path < rightItem.Path
})
renderJSON(writer, http.StatusOK, response)
}
func resolveLightweightOperatorTargetPath(root lightweightOperatorCodeRoot, rel string) (string, error) {
target := filepath.Clean(filepath.Join(root.RootPath, filepath.FromSlash(rel)))
if !localPathInside(target, root.RootPath) {
return "", fmt.Errorf("path must stay within the code directory")
}
return target, nil
}
func ensureLightweightOperatorTargetParent(root lightweightOperatorCodeRoot, target string) error {
parent := filepath.Dir(target)
if err := os.MkdirAll(parent, 0o755); err != nil {
return fmt.Errorf("parent directory cannot be created: %w", err)
}
parentReal, err := filepath.EvalSymlinks(parent)
if err != nil {
return fmt.Errorf("parent directory is not readable: %w", err)
}
if !localPathInside(parentReal, root.RootRealPath) {
return fmt.Errorf("parent directory escapes code directory")
}
return nil
}
func lightweightFileContentResponse(item domain.OperatorRecord, root lightweightOperatorCodeRoot, rel string, content string, info os.FileInfo) lightweightOperatorFileContentResponse {
return lightweightOperatorFileContentResponse{
OperatorID: item.ID,
Path: rel,
Name: path.Base(rel),
Content: content,
Encoding: "utf-8",
SizeBytes: info.Size(),
ModTime: info.ModTime().UTC().Format(time.RFC3339Nano),
Mode: info.Mode().String(),
CodeSubPath: root.CodeSubPath,
MountPath: root.MountPath,
}
}
func (server *server) handleOperatorLightweightFileContentGet(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
root, issue, err := server.resolveLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if !root.Exists {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Code directory not found"})
return
}
rel, err := cleanLightweightOperatorFilePath(request.URL.Query().Get("path"))
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
target, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
info, err := os.Lstat(target)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "File not found"})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
if info.Mode()&os.ModeSymlink != 0 {
renderValidationError(writer, validationIssue{Field: "path", Message: "symlink files are not editable"})
return
}
if !info.Mode().IsRegular() {
renderValidationError(writer, validationIssue{Field: "path", Message: "path must point to a regular file"})
return
}
if info.Size() > lightweightOperatorEditableFileMaxByte {
renderValidationError(writer, validationIssue{Field: "path", Message: "file is too large for inline editing"})
return
}
bytes, err := os.ReadFile(target)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !utf8.Valid(bytes) {
renderValidationError(writer, validationIssue{Field: "path", Message: "file is not valid UTF-8 text"})
return
}
renderJSON(writer, http.StatusOK, lightweightFileContentResponse(*item, root, rel, string(bytes), info))
}
func lightweightOperatorMutation(item domain.OperatorRecord, root lightweightOperatorCodeRoot, rel string, kind string) lightweightOperatorMutationResponse {
return lightweightOperatorMutationResponse{
OperatorID: item.ID,
Path: rel,
Kind: kind,
CodeSubPath: root.CodeSubPath,
MountPath: root.MountPath,
}
}
func (server *server) handleOperatorLightweightDirectoryCreate(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
var payload lightweightOperatorPathPayload
if !decodeJSON(writer, request, &payload) {
return
}
rel, err := cleanLightweightOperatorFilePath(payload.Path)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
root, issue, err := server.ensureLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if root.ReadOnly {
renderValidationError(writer, validationIssue{Field: "mount", Message: "mount is read-only"})
return
}
target, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
if err := ensureLightweightOperatorTargetParent(root, target); err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
if info, err := os.Lstat(target); err == nil && !info.IsDir() {
renderValidationError(writer, validationIssue{Field: "path", Message: "path already exists and is not a directory"})
return
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := os.MkdirAll(target, 0o755); err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("directory cannot be created: %w", err))
return
}
targetReal, err := filepath.EvalSymlinks(target)
if err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("directory is not readable after creation: %w", err))
return
}
if !localPathInside(targetReal, root.RootRealPath) {
renderValidationError(writer, validationIssue{Field: "path", Message: "directory escapes code directory"})
return
}
response := lightweightOperatorMutation(*item, root, rel, "directory")
response.Created = true
renderJSON(writer, http.StatusCreated, response)
}
func (server *server) handleOperatorLightweightFileContentPut(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
var payload lightweightOperatorFileContentPayload
if !decodeJSON(writer, request, &payload) {
return
}
rel, err := cleanLightweightOperatorFilePath(payload.Path)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
if len([]byte(payload.Content)) > lightweightOperatorEditableFileMaxByte {
renderValidationError(writer, validationIssue{Field: "content", Message: "file content is too large for inline editing"})
return
}
root, issue, err := server.ensureLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if root.ReadOnly {
renderValidationError(writer, validationIssue{Field: "mount", Message: "mount is read-only"})
return
}
target, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
if err := ensureLightweightOperatorTargetParent(root, target); err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
mode := os.FileMode(0o644)
if info, err := os.Lstat(target); err == nil {
if info.Mode()&os.ModeSymlink != 0 {
renderValidationError(writer, validationIssue{Field: "path", Message: "symlink files are not editable"})
return
}
if info.IsDir() {
renderValidationError(writer, validationIssue{Field: "path", Message: "path points to a directory"})
return
}
mode = info.Mode().Perm()
} else if !errors.Is(err, os.ErrNotExist) {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := os.WriteFile(target, []byte(payload.Content), mode); err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("file cannot be written: %w", err))
return
}
info, err := os.Lstat(target)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, lightweightFileContentResponse(*item, root, rel, payload.Content, info))
}
func (server *server) handleOperatorLightweightFileUpload(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
request.Body = http.MaxBytesReader(writer, request.Body, lightweightOperatorUploadMaxByte+8*1024*1024)
if err := request.ParseMultipartForm(8 * 1024 * 1024); err != nil {
renderValidationError(writer, validationIssue{Field: "file", Message: "multipart upload is required and must be at most 64 MiB"})
return
}
rel, err := cleanLightweightOperatorFilePath(request.FormValue("path"))
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
file, header, err := request.FormFile("file")
if err != nil {
renderValidationError(writer, validationIssue{Field: "file", Message: "file is required"})
return
}
defer file.Close()
root, issue, err := server.ensureLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if root.ReadOnly {
renderValidationError(writer, validationIssue{Field: "mount", Message: "mount is read-only"})
return
}
target, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
if err := ensureLightweightOperatorTargetParent(root, target); err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
mode := os.FileMode(0o644)
if info, err := os.Lstat(target); err == nil {
if info.Mode()&os.ModeSymlink != 0 {
renderValidationError(writer, validationIssue{Field: "path", Message: "symlink files are not editable"})
return
}
if info.IsDir() {
renderValidationError(writer, validationIssue{Field: "path", Message: "path points to a directory"})
return
}
mode = info.Mode().Perm()
} else if !errors.Is(err, os.ErrNotExist) {
renderError(writer, http.StatusInternalServerError, err)
return
}
output, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
if err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("file cannot be opened for upload: %w", err))
return
}
limited := &io.LimitedReader{R: file, N: lightweightOperatorUploadMaxByte + 1}
written, copyErr := io.Copy(output, limited)
closeErr := output.Close()
if copyErr != nil {
_ = os.Remove(target)
renderError(writer, http.StatusInternalServerError, fmt.Errorf("file cannot be uploaded: %w", copyErr))
return
}
if closeErr != nil {
_ = os.Remove(target)
renderError(writer, http.StatusInternalServerError, fmt.Errorf("file cannot be finalized: %w", closeErr))
return
}
if written > lightweightOperatorUploadMaxByte {
_ = os.Remove(target)
renderValidationError(writer, validationIssue{Field: "file", Message: "file is too large; maximum is 64 MiB"})
return
}
response := lightweightOperatorMutation(*item, root, rel, "file")
response.Uploaded = true
response.Updated = true
response.SizeBytes = written
if header != nil && strings.TrimSpace(header.Filename) != "" && response.Path == "" {
response.Path = header.Filename
}
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleOperatorLightweightFileMove(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
var payload lightweightOperatorMovePayload
if !decodeJSON(writer, request, &payload) {
return
}
rel, err := cleanLightweightOperatorFilePath(payload.Path)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
newRel, err := cleanLightweightOperatorFilePath(payload.NewPath)
if err != nil {
renderValidationError(writer, validationIssue{Field: "newPath", Message: err.Error()})
return
}
if rel == newRel {
renderValidationError(writer, validationIssue{Field: "newPath", Message: "newPath must be different from path"})
return
}
root, issue, err := server.ensureLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if root.ReadOnly {
renderValidationError(writer, validationIssue{Field: "mount", Message: "mount is read-only"})
return
}
source, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
target, err := resolveLightweightOperatorTargetPath(root, newRel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "newPath", Message: err.Error()})
return
}
sourceInfo, err := os.Lstat(source)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Path not found"})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := ensureLightweightOperatorTargetParent(root, target); err != nil {
renderValidationError(writer, validationIssue{Field: "newPath", Message: err.Error()})
return
}
if _, err := os.Lstat(target); err == nil {
renderValidationError(writer, validationIssue{Field: "newPath", Message: "target path already exists"})
return
} else if !errors.Is(err, os.ErrNotExist) {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := os.Rename(source, target); err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("path cannot be moved: %w", err))
return
}
kind := "file"
if sourceInfo.IsDir() {
kind = "directory"
} else if sourceInfo.Mode()&os.ModeSymlink != 0 {
kind = "symlink"
}
response := lightweightOperatorMutation(*item, root, rel, kind)
response.NewPath = newRel
response.Moved = true
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleOperatorLightweightFileDelete(writer http.ResponseWriter, request *http.Request) {
item, ok := server.loadLightweightOperatorForFileRequest(writer, request)
if !ok {
return
}
rawPath := request.URL.Query().Get("path")
if strings.TrimSpace(rawPath) == "" {
var payload lightweightOperatorPathPayload
if !decodeJSON(writer, request, &payload) {
return
}
rawPath = payload.Path
}
rel, err := cleanLightweightOperatorFilePath(rawPath)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
root, issue, err := server.ensureLightweightOperatorCodeRoot(*item, request.URL.Query().Get("mount"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if issue != nil {
renderValidationError(writer, *issue)
return
}
if root.ReadOnly {
renderValidationError(writer, validationIssue{Field: "mount", Message: "mount is read-only"})
return
}
target, err := resolveLightweightOperatorTargetPath(root, rel)
if err != nil {
renderValidationError(writer, validationIssue{Field: "path", Message: err.Error()})
return
}
info, err := os.Lstat(target)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Path not found"})
return
}
renderError(writer, http.StatusInternalServerError, err)
return
}
parentReal, err := filepath.EvalSymlinks(filepath.Dir(target))
if err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("parent directory is not readable: %w", err))
return
}
if !localPathInside(parentReal, root.RootRealPath) {
renderValidationError(writer, validationIssue{Field: "path", Message: "parent directory escapes code directory"})
return
}
if err := os.RemoveAll(target); err != nil {
renderError(writer, http.StatusInternalServerError, fmt.Errorf("path cannot be deleted: %w", err))
return
}
kind := "file"
if info.IsDir() {
kind = "directory"
} else if info.Mode()&os.ModeSymlink != 0 {
kind = "symlink"
}
response := lightweightOperatorMutation(*item, root, rel, kind)
response.Deleted = true
renderJSON(writer, http.StatusOK, response)
}
func runStatusIsValid(status domain.RunStatus) bool {
switch status {
case domain.RunStatusQueued,
domain.RunStatusRunning,
domain.RunStatusStopping,
domain.RunStatusWaitingForAudit,
domain.RunStatusWaitingForHuman,
domain.RunStatusCompleted,
domain.RunStatusFailed,
domain.RunStatusCancelled:
return true
default:
return false
}
}
func runSummaryPaginationRequested(request *http.Request) bool {
query := request.URL.Query()
for _, key := range []string{"limit", "cursor", "q", "projectId", "workflowId", "includeAggregates", "mode"} {
if _, ok := query[key]; ok {
return true
}
}
return false
}
func decodeRunSummaryCursor(raw string) (*domain.RunSummaryCursor, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, nil
}
payload, err := base64.RawURLEncoding.DecodeString(raw)
if err != nil {
return nil, fmt.Errorf("cursor must be a valid opaque run cursor")
}
var cursor domain.RunSummaryCursor
if err := json.Unmarshal(payload, &cursor); err != nil {
return nil, fmt.Errorf("cursor must be a valid opaque run cursor")
}
if strings.TrimSpace(cursor.CreatedAt) == "" || strings.TrimSpace(cursor.ID) == "" {
return nil, fmt.Errorf("cursor must include createdAt and id")
}
return &cursor, nil
}
func encodeRunSummaryCursor(cursor *domain.RunSummaryCursor) string {
if cursor == nil {
return ""
}
payload, err := json.Marshal(cursor)
if err != nil {
return ""
}
return base64.RawURLEncoding.EncodeToString(payload)
}
func parseRunSummaryQuery(request *http.Request) (domain.RunSummaryQuery, []validationIssue) {
values := request.URL.Query()
query := domain.RunSummaryQuery{
Limit: 50,
IncludeAggregates: true,
Status: domain.RunStatus(strings.TrimSpace(values.Get("status"))),
Search: strings.TrimSpace(values.Get("q")),
ProjectID: strings.TrimSpace(values.Get("projectId")),
WorkflowID: strings.TrimSpace(values.Get("workflowId")),
}
issues := make([]validationIssue, 0, 4)
if raw, ok := values["includeAggregates"]; ok && len(raw) > 0 {
query.AggregatesExplicit = true
value, err := strconv.ParseBool(strings.TrimSpace(raw[0]))
if err != nil {
issues = append(issues, validationIssue{Field: "includeAggregates", Message: "includeAggregates must be true or false"})
} else {
query.IncludeAggregates = value
}
}
if rawLimit, ok := values["limit"]; ok {
value := ""
if len(rawLimit) > 0 {
value = rawLimit[0]
}
limit, issue := parseDatabaseBrowserPageQuery(value, "limit", 50, 1, 100)
if issue != nil {
issues = append(issues, *issue)
} else {
query.Limit = limit
}
}
if query.Status != "" && !runStatusIsValid(query.Status) {
issues = append(issues, validationIssue{Field: "status", Message: "status must be queued, running, stopping, waiting_for_audit, waiting_for_human, completed, failed, or cancelled"})
}
modeParam := strings.TrimSpace(values.Get("mode"))
if modeParam != "" && modeParam != "all" {
if modeParam == string(domain.RunModeProduction) || modeParam == string(domain.RunModeDebug) {
query.Mode = domain.RunMode(modeParam)
} else {
issues = append(issues, validationIssue{Field: "mode", Message: "mode must be production, debug, or all"})
}
}
if len(query.Search) > 200 {
issues = append(issues, validationIssue{Field: "q", Message: "q must be at most 200 characters"})
}
cursor, err := decodeRunSummaryCursor(values.Get("cursor"))
if err != nil {
issues = append(issues, validationIssue{Field: "cursor", Message: err.Error()})
} else {
query.Cursor = cursor
}
return query, issues
}
func (server *server) handleRunsList(writer http.ResponseWriter, request *http.Request) {
if runSummaryPaginationRequested(request) {
query, issues := parseRunSummaryQuery(request)
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
startedAt := time.Now()
page, err := server.listRunSummaryPageForRequest(request.Context(), request, query)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
writer.Header().Set("Server-Timing", fmt.Sprintf("runs;dur=%.1f", float64(time.Since(startedAt).Microseconds())/1000))
response := map[string]any{
"items": page.Items,
"nextCursor": encodeRunSummaryCursor(page.NextCursor),
"hasMore": page.HasMore,
}
if query.IncludeAggregates {
response["total"] = page.Total
response["statusCounts"] = page.StatusCounts
}
renderJSON(writer, http.StatusOK, response)
return
}
items, err := server.listRunSummariesForRequest(request.Context(), request, request.URL.Query().Get("status"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterRunSummariesForRequest(request, items)
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) loadRun(ctx context.Context, id string) (*domain.RunRecord, error) {
item, err := server.store.GetRun(ctx, id)
if err != nil || item == nil {
return item, err
}
return item, nil
}
func (server *server) handleRunsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
renderJSON(writer, http.StatusOK, runForResponse(request, *item))
}
func (server *server) markWorkflowStopped(ctx context.Context, run *domain.RunRecord) error {
if run == nil || strings.TrimSpace(run.WorkflowID) == "" {
return nil
}
workflow, err := server.store.GetWorkflow(ctx, run.WorkflowID)
if err != nil || workflow == nil {
return err
}
if run.Status == domain.RunStatusCancelled {
workflow.Status = domain.WorkflowStatusCancelled
} else {
workflow.Status = domain.WorkflowStatusFailed
}
workflow.LastRunID = run.ID
workflow.LastRunAt = nowISO()
workflow.UpdatedAt = nowISO()
return server.store.UpsertWorkflow(ctx, *workflow)
}
func (server *server) terminateRunJobs(ctx context.Context, run *domain.RunRecord) error {
if run == nil || strings.TrimSpace(run.ID) == "" {
return nil
}
settings, err := server.store.GetSettings(ctx)
if err != nil || settings == nil {
return err
}
launcher, err := runJobTerminatorFactory(settings.K8s.Namespace, "", settings.K8s.WorkspacePVCName, settings.K8s.WorkspaceMountPath)
if err != nil {
return err
}
if err := launcher.TerminateRun(ctx, settings.K8s.Namespace, run.ID); err != nil {
return err
}
for _, ref := range run.K8sRefs {
if err := launcher.TerminateJob(ctx, ref.Namespace, ref.JobName); err != nil {
return err
}
}
return nil
}
func (server *server) handleRunStopWithMode(writer http.ResponseWriter, request *http.Request, mode domain.RunStopMode) {
existing, err := server.store.GetRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if existing == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Run") {
return
}
run, err := server.store.RequestRunStop(request.Context(), existing.ID, mode)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if mode == domain.RunStopModeForce && run.Status == domain.RunStatusCancelled {
if err := server.terminateRunJobs(request.Context(), run); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := server.markWorkflowStopped(request.Context(), run); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
if mode == domain.RunStopModeGraceful && run.Status == domain.RunStatusCancelled {
if err := server.markWorkflowStopped(request.Context(), run); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
renderJSON(writer, http.StatusOK, run)
}
func (server *server) handleRunsStop(writer http.ResponseWriter, request *http.Request) {
server.handleRunStopWithMode(writer, request, domain.RunStopModeGraceful)
}
func (server *server) handleRunsForceStop(writer http.ResponseWriter, request *http.Request) {
server.handleRunStopWithMode(writer, request, domain.RunStopModeForce)
}
func (server *server) handleRunsLogs(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
// Surface the running node's live stdout so its execution logs are visible DURING the
// run — the worker persists a node's structured logs only at completion. No-op for
// terminal runs or runs with no running node; failures are silent (stored logs intact).
server.appendLiveNodeLogs(request.Context(), item)
response, issues := pagedResponse(request, item.Logs, 1000, 5000, true)
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
renderJSON(writer, http.StatusOK, response)
}
// appendLiveNodeLogs tails the stdout of the pod backing the run's currently-running node and
// appends those live lines to run.Logs for the response. API pods hold pods/log RBAC, so this
// runs read-only at the API layer (no worker or store change). No-op when the run is terminal,
// has no running node, the pod is not yet Running, or the tail fails — the stored logs remain.
func (server *server) appendLiveNodeLogs(ctx context.Context, run *domain.RunRecord) {
if run == nil {
return
}
if run.Status != domain.RunStatusRunning && run.Status != domain.RunStatusQueued && run.Status != domain.RunStatusStopping {
return
}
nodeID, nodeLabel, nodeStartedAt := "", "", ""
for _, execution := range run.NodeExecutions {
if execution.Status == domain.NodeExecutionStatusRunning {
nodeID = execution.NodeID
nodeLabel = execution.NodeLabel
nodeStartedAt = execution.StartedAt
break
}
}
if nodeID == "" {
return
}
settings, err := server.store.GetSettings(ctx)
if err != nil || settings == nil {
return
}
launcher, err := k8s.NewLauncher(settings.K8s.Namespace, "", settings.K8s.WorkspacePVCName, settings.K8s.WorkspaceMountPath, "", "", "", 0)
if err != nil {
return
}
tailCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
tail, err := launcher.TailNodePodLogsWithTimestamps(tailCtx, run.ID, nodeID, 300)
if err != nil || strings.TrimSpace(tail) == "" {
return
}
label := nodeLabel
if strings.TrimSpace(label) == "" {
label = nodeID
}
fallbackTimestamp := firstNonEmptyText(nodeStartedAt, run.StartedAt, run.CreatedAt, "1970-01-01T00:00:00Z")
run.Logs = append(run.Logs, livePodLogEntries(tail, label, fallbackTimestamp)...)
}
func livePodLogEntries(tail string, label string, fallbackTimestamp string) []domain.LogEntryRecord {
entries := make([]domain.LogEntryRecord, 0)
for _, line := range strings.Split(tail, "\n") {
trimmed := strings.TrimSpace(line)
if trimmed == "" {
continue
}
timestamp := fallbackTimestamp
message := trimmed
if candidate, remainder, found := strings.Cut(trimmed, " "); found {
if _, err := time.Parse(time.RFC3339Nano, candidate); err == nil {
timestamp = candidate
message = strings.TrimSpace(remainder)
}
}
if message == "" {
continue
}
entries = append(entries, domain.LogEntryRecord{
Timestamp: timestamp,
Level: "INFO",
Message: message,
Operator: label,
})
}
return entries
}
func (server *server) handleRunsResources(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
// Fold nodeExecutions (including live GPU samples the worker streams during execution via
// AppendNodeExecutionResourceSample) into the top-level series the resource monitor reads.
// Idempotent with the worker's own fold; it only adds value while a node is mid-execution.
rebuildRunAggregates(item)
response, issues := pagedResponse(request, item.ResourceSeries, 1000, 5000, true)
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
renderJSON(writer, http.StatusOK, response)
}
func (server *server) handleRunsArtifacts(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
response, issues := pagedResponse(request, runArtifactsForResponse(request, *item), 1000, 5000, false)
if len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
renderJSON(writer, http.StatusOK, response)
}
func runArtifactByID(run *domain.RunRecord, artifactID string) *domain.ExecutionArtifact {
if run == nil || strings.TrimSpace(artifactID) == "" {
return nil
}
nodeByArtifact := runArtifactNodeLookup(*run)
for index := range run.Artifacts {
nodeID := firstNonEmptyText(
run.Artifacts[index].NodeID,
nodeByArtifact[artifactStorageIdentity(run.Artifacts[index])],
)
if artifactResponseID(run.ID, nodeID, run.Artifacts[index]) == artifactID {
return &run.Artifacts[index]
}
}
for executionIndex := range run.NodeExecutions {
execution := &run.NodeExecutions[executionIndex]
for artifactIndex := range execution.Artifacts {
if artifactResponseID(run.ID, execution.NodeID, execution.Artifacts[artifactIndex]) == artifactID {
return &execution.Artifacts[artifactIndex]
}
}
for artifactIndex := range execution.InputArtifacts {
nodeID := firstNonEmptyText(
execution.InputArtifacts[artifactIndex].NodeID,
nodeByArtifact[artifactStorageIdentity(execution.InputArtifacts[artifactIndex])],
)
if artifactResponseID(run.ID, nodeID, execution.InputArtifacts[artifactIndex]) == artifactID {
return &execution.InputArtifacts[artifactIndex]
}
}
for lineageIndex := range execution.InputLineage {
lineageArtifact := execution.InputLineage[lineageIndex].Artifact
nodeID := firstNonEmptyText(
execution.InputLineage[lineageIndex].SourceNodeID,
lineageArtifact.NodeID,
nodeByArtifact[artifactStorageIdentity(lineageArtifact)],
)
if artifactResponseID(run.ID, nodeID, lineageArtifact) == artifactID {
return &execution.InputLineage[lineageIndex].Artifact
}
}
}
return nil
}
func artifactContentFileName(item domain.ExecutionArtifact) string {
if name := strings.TrimSpace(item.Name); name != "" {
return name
}
if filePath := strings.TrimSpace(item.Path); filePath != "" {
return path.Base(filePath)
}
if strings.TrimSpace(item.ID) != "" {
return strings.TrimSpace(item.ID)
}
return "artifact"
}
func artifactContentMimeType(item domain.ExecutionArtifact, fileName string, payload []byte) string {
metadataMimeType := artifactMimeType(item)
if metadataMimeType != "" && !strings.EqualFold(metadataMimeType, "application/octet-stream") {
return metadataMimeType
}
if strings.EqualFold(path.Ext(fileName), ".rrd") {
return "application/vnd.rerun.rrd"
}
if guessed := mime.TypeByExtension(strings.ToLower(path.Ext(fileName))); guessed != "" {
return guessed
}
if metadataMimeType != "" {
return metadataMimeType
}
if len(payload) > 0 {
return http.DetectContentType(payload)
}
return "application/octet-stream"
}
func artifactBrowserTranscodeRequested(request *http.Request, item domain.ExecutionArtifact, mimeType string) bool {
if !strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("transcode")), "browser") {
return false
}
if artifactPreviewKind(item) == "video" {
return true
}
return strings.HasPrefix(strings.ToLower(strings.TrimSpace(mimeType)), "video/")
}
func writeArtifactBrowserVideoPreview(writer http.ResponseWriter, request *http.Request, fileName string, previewRequest sourceBrowserPreviewRequest) error {
if previewPath, err := sourceBrowserVideoPreviewPath(request.Context(), previewRequest); err == nil && previewPath != "" {
return writeSourceContentFile(writer, request, strings.TrimSuffix(fileName, path.Ext(fileName))+"-browser.mp4", "video/mp4", previewPath)
}
writer.Header().Set("Content-Type", "video/mp4")
writer.Header().Set("Cache-Control", "no-store")
writer.WriteHeader(http.StatusOK)
if flusher, ok := writer.(http.Flusher); ok {
flusher.Flush()
}
if err := sourceBrowserVideoPreviewWriter(request.Context(), writer, previewRequest); err != nil {
fmt.Printf("[api] artifact browser preview stream failed: %v\n", err)
}
return nil
}
type artifactContentResponseWriter struct {
http.ResponseWriter
mimeType string
}
func (writer *artifactContentResponseWriter) applyMimeType() {
mimeType := strings.TrimSpace(writer.mimeType)
current := strings.ToLower(strings.TrimSpace(strings.Split(writer.Header().Get("Content-Type"), ";")[0]))
if mimeType != "" && (current == "" || current == "application/octet-stream") {
writer.Header().Set("Content-Type", mimeType)
}
}
func (writer *artifactContentResponseWriter) WriteHeader(statusCode int) {
writer.applyMimeType()
writer.ResponseWriter.WriteHeader(statusCode)
}
func (writer *artifactContentResponseWriter) Write(payload []byte) (int, error) {
writer.applyMimeType()
return writer.ResponseWriter.Write(payload)
}
func artifactBOSLocation(artifact domain.ExecutionArtifact) (string, string, bool) {
paths := []string{artifact.Path}
if payload := artifactStructuredData(&artifact); payload != nil {
if source := mapValue(payload["source"]); source != nil {
paths = append(paths, stringValue(source["path"]))
}
}
for _, sourcePath := range paths {
if bucket, key, ok := parseBOSPath(sourcePath); ok && key != "" {
return bucket, key, true
}
}
return "", "", false
}
func storageConnectionMatchesBucket(connection *domain.StorageConnectionRecord, bucket string) bool {
if connection == nil || normalizeStorageProvider(connection.Provider) != domain.StorageProviderBOS {
return false
}
boundBucket := strings.Trim(strings.TrimSpace(connection.Bucket), "/")
return boundBucket == "" || boundBucket == strings.Trim(strings.TrimSpace(bucket), "/")
}
func (server *server) resolveArtifactStorageConnection(
ctx context.Context,
run *domain.RunRecord,
artifact domain.ExecutionArtifact,
bucket string,
) (*domain.StorageConnectionRecord, error) {
if run == nil {
return nil, nil
}
if workflow, err := server.store.GetWorkflow(ctx, run.WorkflowID); err != nil {
return nil, err
} else if workflow != nil {
connectionIDs := make([]string, 0, len(workflow.Nodes)+1)
for _, node := range workflow.Nodes {
if node.ID == artifact.NodeID && strings.TrimSpace(node.Data.ConnectionID) != "" {
connectionIDs = append(connectionIDs, node.Data.ConnectionID)
}
}
for _, node := range workflow.Nodes {
if node.ID == artifact.NodeID || strings.TrimSpace(node.Data.ConnectionID) == "" {
continue
}
if strings.Trim(strings.TrimSpace(node.Data.Bucket), "/") == strings.Trim(strings.TrimSpace(bucket), "/") {
connectionIDs = append(connectionIDs, node.Data.ConnectionID)
}
}
if workflow.ScanConfig != nil && strings.TrimSpace(workflow.ScanConfig.ConnectionID) != "" {
connectionIDs = append(connectionIDs, workflow.ScanConfig.ConnectionID)
}
seen := map[string]struct{}{}
for _, connectionID := range connectionIDs {
connectionID = strings.TrimSpace(connectionID)
if connectionID == "" {
continue
}
if _, exists := seen[connectionID]; exists {
continue
}
seen[connectionID] = struct{}{}
connection, err := server.getStorageConnection(ctx, connectionID)
if err != nil {
continue
}
if storageConnectionMatchesBucket(connection, bucket) {
return connection, nil
}
}
}
connections, err := server.store.ListStorageConnectionsByOrganization(ctx, run.OrganizationID)
if err != nil {
return nil, err
}
var unbound *domain.StorageConnectionRecord
for index := range connections {
connection := &connections[index]
if normalizeStorageProvider(connection.Provider) != domain.StorageProviderBOS {
continue
}
boundBucket := strings.Trim(strings.TrimSpace(connection.Bucket), "/")
if boundBucket == strings.Trim(strings.TrimSpace(bucket), "/") {
return connection, nil
}
if boundBucket == "" && unbound == nil {
unbound = connection
}
}
return unbound, nil
}
func (server *server) writeBOSArtifactContent(
writer http.ResponseWriter,
request *http.Request,
run *domain.RunRecord,
artifact domain.ExecutionArtifact,
) bool {
bucket, key, ok := artifactBOSLocation(artifact)
if !ok {
return false
}
connection, err := server.resolveArtifactStorageConnection(request.Context(), run, artifact, bucket)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return true
}
if connection == nil {
return false
}
clonedRequest := request.Clone(request.Context())
clonedURL := *request.URL
query := clonedURL.Query()
query.Set("connectionId", connection.ID)
query.Set("bucket", bucket)
query.Set("key", key)
clonedURL.RawQuery = query.Encode()
clonedRequest.URL = &clonedURL
contentWriter := &artifactContentResponseWriter{
ResponseWriter: writer,
mimeType: artifactContentMimeType(artifact, artifactContentFileName(artifact), nil),
}
server.handleObjectStorageContent(contentWriter, clonedRequest)
return true
}
func (server *server) handleRunsArtifactContent(writer http.ResponseWriter, request *http.Request) {
server.serveRunArtifactContent(writer, request, chi.URLParam(request, "id"), chi.URLParam(request, "artifactId"))
}
func (server *server) serveRunArtifactContent(writer http.ResponseWriter, request *http.Request, runID string, artifactID string) {
item, err := server.loadRun(request.Context(), runID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
artifact := runArtifactByID(item, artifactID)
if artifact == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
return
}
server.serveResolvedRunArtifactContent(writer, request, item, *artifact)
}
// serveResolvedRunArtifactContent writes an artifact that has already been
// authorized against its Run. It is shared by top-level artifacts and the
// strictly parsed manifest-member projection; callers must never pass a path
// received directly from a client request.
func (server *server) serveResolvedRunArtifactContent(writer http.ResponseWriter, request *http.Request, item *domain.RunRecord, artifact domain.ExecutionArtifact) {
fileName := artifactContentFileName(artifact)
mimeType := artifactContentMimeType(artifact, fileName, nil)
if filePath := strings.TrimSpace(artifact.Path); filePath != "" && !strings.Contains(filePath, "://") {
if info, err := os.Stat(filePath); err == nil && !info.IsDir() {
if artifactBrowserTranscodeRequested(request, artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
RedirectURL: filePath,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
if err := writeSourceContentFile(writer, request, fileName, mimeType, filePath); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
}
if server.writeBOSArtifactContent(writer, request, item, artifact) {
return
}
previewURL := packageArtifactPreviewURL(artifact)
if strings.HasPrefix(previewURL, "http://") || strings.HasPrefix(previewURL, "https://") {
if artifactBrowserTranscodeRequested(request, artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
RedirectURL: previewURL,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
http.Redirect(writer, request, previewURL, http.StatusTemporaryRedirect)
return
}
if payload, decodedMimeType, err := contentBytesForDataURL(previewURL); err == nil && len(payload) > 0 {
mimeType = artifactContentMimeType(artifact, fileName, payload)
if strings.TrimSpace(mimeType) == "application/octet-stream" && strings.TrimSpace(decodedMimeType) != "" {
mimeType = decodedMimeType
}
if artifactBrowserTranscodeRequested(request, artifact, mimeType) {
previewRequest := sourceBrowserPreviewRequest{
FileName: fileName,
Payload: payload,
}
if err := writeArtifactBrowserVideoPreview(writer, request, fileName, previewRequest); err != nil {
renderError(writer, http.StatusInternalServerError, err)
}
return
}
writeSourceContent(writer, request, fileName, mimeType, payload)
return
}
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact content not found"})
}
func (server *server) handleRunsLineage(writer http.ResponseWriter, request *http.Request) {
item, err := server.loadRun(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if !server.ensureProjectVisibility(writer, request, item.OrganizationID, item.ProjectID) {
return
}
renderJSON(writer, http.StatusOK, runLineageForResponse(request, *item))
}
func (server *server) handleArtifactLineage(writer http.ResponseWriter, request *http.Request) {
artifactID := chi.URLParam(request, "artifactId")
runID := strings.TrimSpace(request.URL.Query().Get("runId"))
if runID != "" {
item, err := server.loadRun(request.Context(), runID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Run not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Run") {
return
}
if response, ok := artifactLineageForResponse(request, *item, artifactID); ok {
renderJSON(writer, http.StatusOK, response)
return
}
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
return
}
items, err := server.listRunsForRequest(request.Context(), request, "")
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
for _, item := range items {
if !server.filterByTenant(request, item.OrganizationID) {
continue
}
if response, ok := artifactLineageForResponse(request, item, artifactID); ok {
renderJSON(writer, http.StatusOK, response)
return
}
}
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Artifact not found"})
}
func (server *server) handleAuditTasksList(writer http.ResponseWriter, request *http.Request) {
items, err := server.listAuditTasksForRequest(request.Context(), request, request.URL.Query().Get("status"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterAuditTasksForRequest(request, items)
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) resolveAuditTask(ctx context.Context, id string, decision domain.AuditStatus) (*domain.AuditTaskRecord, error) {
task, err := server.store.GetAuditTask(ctx, id)
if err != nil || task == nil {
return task, err
}
task.Status = decision
task.UpdatedAt = nowISO()
if err := server.store.UpsertAuditTask(ctx, *task); err != nil {
return nil, err
}
run, err := server.store.GetRun(ctx, task.RunID)
if err != nil {
return nil, err
}
if run != nil {
run.Status = domain.RunStatusQueued
run.UpdatedAt = nowISO()
if err := server.store.UpsertRun(ctx, *run); err != nil {
return nil, err
}
}
return task, nil
}
func (server *server) handleAuditTaskApprove(writer http.ResponseWriter, request *http.Request) {
existing, err := server.store.GetAuditTask(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if existing == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Audit task not found"})
return
}
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Audit task") {
return
}
task, err := server.resolveAuditTask(request.Context(), existing.ID, domain.AuditStatusApproved)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, task)
}
func (server *server) handleAuditTaskReject(writer http.ResponseWriter, request *http.Request) {
existing, err := server.store.GetAuditTask(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if existing == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Audit task not found"})
return
}
if !server.ensureTenantAccess(writer, request, existing.OrganizationID, "Audit task") {
return
}
task, err := server.resolveAuditTask(request.Context(), existing.ID, domain.AuditStatusRejected)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, task)
}
func (server *server) handleUsersList(writer http.ResponseWriter, request *http.Request) {
items, err := server.store.ListUsers(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterUsersForRequest(request, items)
if request.URL.Query().Get("includeDisabled") != "true" {
active := items[:0]
for _, item := range items {
if item.Status == "" || item.Status == domain.UserStatusActive {
active = append(active, item)
}
}
items = active
}
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeUsers(items)})
}
func normalizeUserInput(name string, email string) (string, string) {
return strings.TrimSpace(name), strings.TrimSpace(strings.ToLower(email))
}
func (server *server) validateUserEmailAvailable(ctx context.Context, email string, currentUserID string) (*validationIssue, error) {
existing, err := server.store.FindUserByEmail(ctx, email)
if err != nil {
return nil, err
}
if existing == nil {
return nil, nil
}
if strings.TrimSpace(currentUserID) != "" && strings.TrimSpace(existing.ID) == strings.TrimSpace(currentUserID) {
return nil, nil
}
return &validationIssue{Field: "email", Message: "email must be unique"}, nil
}
func (server *server) handleUsersCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateUserInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name, payload.Email = normalizeUserInput(payload.Name, payload.Email)
if payload.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
if _, err := mail.ParseAddress(payload.Email); err != nil {
renderValidationError(writer, validationIssue{Field: "email", Message: "email must be valid"})
return
}
if issue, err := server.validateUserEmailAvailable(request.Context(), payload.Email, ""); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
} else if issue != nil {
renderValidationError(writer, *issue)
return
}
if !validUserRole(payload.Role) {
renderValidationError(writer, validationIssue{Field: "role", Message: "role must be admin, cutter, annotator, quality_reviewer, acceptance_reviewer, or supplier_manager"})
return
}
if payload.Status != "" && !validUserStatus(payload.Status) {
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or disabled"})
return
}
if server.requireSessionAuth && strings.TrimSpace(payload.Password) == "" {
renderValidationError(writer, validationIssue{Field: "password", Message: "password is required"})
return
}
if strings.TrimSpace(payload.Password) != "" {
passwordHash, err := hashPassword(payload.Password)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
payload.PasswordHash = passwordHash
}
server.assignTenant(request, &payload.OrganizationID)
item, err := server.store.CreateUser(request.Context(), payload)
if err != nil {
renderUserMutationError(writer, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeUser(item))
}
type userPatch struct {
Name *string `json:"name"`
Email *string `json:"email"`
Role *domain.UserRole `json:"role"`
Status *domain.UserStatus `json:"status"`
Password *string `json:"password"`
}
func validUserRole(value domain.UserRole) bool {
switch value {
case domain.UserRoleAdmin,
domain.UserRoleOrganizationAdmin,
domain.UserRoleMember,
domain.UserRoleCutter,
domain.UserRoleAnnotator,
domain.UserRoleQualityReviewer,
domain.UserRoleAcceptanceReviewer,
domain.UserRoleSupplierManager,
domain.UserRolePipelineEngineer,
domain.UserRoleDataOperator,
domain.UserRoleQAReviewer,
domain.UserRoleComplianceAuditor,
domain.UserRoleAuditor,
domain.UserRoleOperator:
return true
default:
return false
}
}
func validUserStatus(value domain.UserStatus) bool {
return value == domain.UserStatusActive || value == domain.UserStatusDisabled
}
func (server *server) handleUsersUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "User") {
return
}
var payload userPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
item.Name = strings.TrimSpace(*payload.Name)
if item.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
}
if payload.Email != nil {
normalizedEmail := strings.TrimSpace(strings.ToLower(*payload.Email))
if _, err := mail.ParseAddress(normalizedEmail); err != nil {
renderValidationError(writer, validationIssue{Field: "email", Message: "email must be valid"})
return
}
if issue, err := server.validateUserEmailAvailable(request.Context(), normalizedEmail, item.ID); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
} else if issue != nil {
renderValidationError(writer, *issue)
return
}
item.Email = normalizedEmail
}
if payload.Role != nil {
if !validUserRole(*payload.Role) {
renderValidationError(writer, validationIssue{Field: "role", Message: "role must be admin, cutter, annotator, quality_reviewer, acceptance_reviewer, or supplier_manager"})
return
}
item.Role = *payload.Role
}
if payload.Status != nil {
if !validUserStatus(*payload.Status) {
renderValidationError(writer, validationIssue{Field: "status", Message: "status must be active or disabled"})
return
}
item.Status = *payload.Status
}
if payload.Password != nil {
if strings.TrimSpace(*payload.Password) == "" {
renderValidationError(writer, validationIssue{Field: "password", Message: "password is required"})
return
}
passwordHash, err := hashPassword(*payload.Password)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
item.PasswordHash = passwordHash
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertUser(request.Context(), *item); err != nil {
renderUserMutationError(writer, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeUser(item))
}
func (server *server) handleUsersDelete(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "User") {
return
}
item.Status = domain.UserStatusDisabled
item.UpdatedAt = nowISO()
if err := server.store.UpsertUser(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if err := server.store.DeleteSessionsByUser(request.Context(), item.ID); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
writer.WriteHeader(http.StatusNoContent)
}
type storagePathFavoritesPayload struct {
Items []domain.StoragePathFavoriteRecord `json:"items"`
}
func (server *server) handleUserStoragePathFavoritesGet(writer http.ResponseWriter, request *http.Request) {
user, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if user == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
return
}
if !server.ensureTenantAccess(writer, request, user.OrganizationID, "User") {
return
}
if !server.ensureSelfOrAdmin(writer, request, *user) {
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"items": sanitizeStoragePathFavorites(user.StoragePathFavorites),
})
}
func (server *server) handleUserStoragePathFavoritesUpdate(writer http.ResponseWriter, request *http.Request) {
user, err := server.store.GetUser(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if user == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "User not found"})
return
}
if !server.ensureTenantAccess(writer, request, user.OrganizationID, "User") {
return
}
if !server.ensureSelfOrAdmin(writer, request, *user) {
return
}
var payload storagePathFavoritesPayload
if !decodeJSON(writer, request, &payload) {
return
}
user.StoragePathFavorites = sanitizeStoragePathFavorites(payload.Items)
user.UpdatedAt = nowISO()
if err := server.store.UpsertUser(request.Context(), *user); err != nil {
renderUserMutationError(writer, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{
"items": user.StoragePathFavorites,
})
}
type storageConnectionPatch struct {
Name *string `json:"name"`
Description *string `json:"description"`
Provider *domain.StorageProvider `json:"provider"`
Endpoint *string `json:"endpoint"`
Bucket *string `json:"bucket"`
AccessKeyID *string `json:"accessKeyId"`
SecretAccessKey *string `json:"secretAccessKey"`
}
type mongoConnectionPatch struct {
Name *string `json:"name"`
Description *string `json:"description"`
URI *string `json:"uri"`
Database *string `json:"database"`
}
func normalizeStorageProvider(value domain.StorageProvider) domain.StorageProvider {
switch domain.StorageProvider(strings.ToUpper(strings.TrimSpace(string(value)))) {
case domain.StorageProviderBOS:
return domain.StorageProviderBOS
case domain.StorageProviderOSS:
return domain.StorageProviderOSS
default:
return ""
}
}
func validStorageProvider(value domain.StorageProvider) bool {
return normalizeStorageProvider(value) != ""
}
func validateOptionalHTTPURL(field string, value string) *validationIssue {
value = strings.TrimSpace(value)
if value == "" {
return nil
}
if strings.ContainsAny(value, " \t\r\n") {
return &validationIssue{Field: field, Message: field + " must be an absolute http(s) URL without whitespace"}
}
parsed, err := url.Parse(value)
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
return &validationIssue{Field: field, Message: field + " must be an absolute http(s) URL"}
}
return nil
}
func validateMongoConnectionURI(field string, value string) *validationIssue {
value = strings.TrimSpace(value)
if value == "" {
return nil
}
if strings.ContainsAny(value, " \t\r\n") {
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI without whitespace"}
}
if strings.ContainsAny(value, "<>") {
return &validationIssue{Field: field, Message: field + " must not contain placeholder markers"}
}
parsed, err := url.Parse(value)
if err != nil || parsed == nil || parsed.Host == "" {
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI"}
}
switch parsed.Scheme {
case "mongodb", "mongodb+srv":
return nil
default:
return &validationIssue{Field: field, Message: field + " must be a mongodb:// or mongodb+srv:// URI"}
}
}
func storageEndpointForProvider(provider domain.StorageProvider, endpoint string) string {
switch normalizeStorageProvider(provider) {
case domain.StorageProviderBOS:
return chooseBOSEndpoint(endpoint)
case domain.StorageProviderOSS:
return strings.TrimRight(strings.TrimSpace(endpoint), "/")
default:
return strings.TrimSpace(endpoint)
}
}
func (server *server) storageEndpointForProvider(provider domain.StorageProvider, endpoint string) string {
switch normalizeStorageProvider(provider) {
case domain.StorageProviderBOS:
return server.chooseBOSEndpoint(endpoint)
case domain.StorageProviderOSS:
return strings.TrimRight(strings.TrimSpace(endpoint), "/")
default:
return strings.TrimSpace(endpoint)
}
}
func (server *server) validateStorageConnectionEndpoint(provider domain.StorageProvider, endpoint string) *validationIssue {
normalizedProvider := normalizeStorageProvider(provider)
if issue := validateOptionalHTTPURL("endpoint", endpoint); issue != nil {
return issue
}
if normalizedProvider == domain.StorageProviderOSS && strings.TrimSpace(endpoint) == "" {
return &validationIssue{Field: "endpoint", Message: "endpoint is required for OSS"}
}
if normalizedProvider == domain.StorageProviderBOS && strings.TrimSpace(endpoint) == "" && strings.TrimSpace(server.globalBOSEndpoint) == "" {
return &validationIssue{Field: "endpoint", Message: "endpoint is required when global BOS_ENDPOINT is not configured"}
}
return nil
}
func (server *server) storageConnectionWithResolvedEndpoint(connection *domain.StorageConnectionRecord) *domain.StorageConnectionRecord {
if connection == nil {
return nil
}
copy := *connection
copy.Endpoint = server.storageEndpointForProvider(copy.Provider, copy.Endpoint)
return &copy
}
func (server *server) handleStorageConnectionsList(writer http.ResponseWriter, request *http.Request) {
items, err := server.listStorageConnectionsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterStorageConnectionsForRequest(request, items)
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeStorageConnections(items)})
}
func (server *server) handleStorageConnectionsCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateStorageConnectionInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name = strings.TrimSpace(payload.Name)
payload.Description = strings.TrimSpace(payload.Description)
payload.Provider = normalizeStorageProvider(payload.Provider)
payload.Endpoint = strings.TrimSpace(payload.Endpoint)
payload.Bucket = strings.Trim(strings.TrimSpace(payload.Bucket), "/")
payload.AccessKeyID = strings.TrimSpace(payload.AccessKeyID)
payload.SecretAccessKey = strings.TrimSpace(payload.SecretAccessKey)
if payload.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
if !validStorageProvider(payload.Provider) {
renderValidationError(writer, validationIssue{Field: "provider", Message: "provider must be BOS or OSS"})
return
}
if issue := server.validateStorageConnectionEndpoint(payload.Provider, payload.Endpoint); issue != nil {
renderValidationError(writer, *issue)
return
}
if payload.AccessKeyID == "" {
renderValidationError(writer, validationIssue{Field: "accessKeyId", Message: "accessKeyId is required"})
return
}
if payload.SecretAccessKey == "" {
renderValidationError(writer, validationIssue{Field: "secretAccessKey", Message: "secretAccessKey is required"})
return
}
payload.Endpoint = server.storageEndpointForProvider(payload.Provider, payload.Endpoint)
server.assignTenant(request, &payload.OrganizationID)
item, err := server.store.CreateStorageConnection(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeStorageConnection(item))
}
func (server *server) handleStorageConnectionsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
return
}
renderJSON(writer, http.StatusOK, sanitizeStorageConnection(item))
}
func (server *server) handleStorageConnectionsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
return
}
var payload storageConnectionPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
item.Name = strings.TrimSpace(*payload.Name)
}
if payload.Description != nil {
item.Description = strings.TrimSpace(*payload.Description)
}
if payload.Provider != nil {
provider := normalizeStorageProvider(*payload.Provider)
if !validStorageProvider(provider) {
renderValidationError(writer, validationIssue{Field: "provider", Message: "provider must be BOS or OSS"})
return
}
item.Provider = provider
}
if payload.Endpoint != nil {
item.Endpoint = strings.TrimSpace(*payload.Endpoint)
}
if payload.Bucket != nil {
item.Bucket = strings.Trim(strings.TrimSpace(*payload.Bucket), "/")
}
if payload.AccessKeyID != nil {
if trimmed := strings.TrimSpace(*payload.AccessKeyID); trimmed != "" {
item.AccessKeyID = trimmed
}
}
if payload.SecretAccessKey != nil {
if trimmed := strings.TrimSpace(*payload.SecretAccessKey); trimmed != "" {
item.SecretAccessKey = trimmed
}
}
if strings.TrimSpace(item.Name) == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
if issue := server.validateStorageConnectionEndpoint(item.Provider, item.Endpoint); issue != nil {
renderValidationError(writer, *issue)
return
}
item.Endpoint = server.storageEndpointForProvider(item.Provider, item.Endpoint)
item.UpdatedAt = nowISO()
if err := server.store.UpsertStorageConnection(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeStorageConnection(item))
}
func (server *server) handleStorageConnectionsDelete(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetStorageConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Storage connection") {
return
}
referenced, err := server.storageConnectionReferencingWorkflows(request.Context(), item)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if len(referenced) > 0 {
details := make([]map[string]string, 0, len(referenced))
for _, workflow := range referenced {
for _, node := range workflow.Nodes {
if strings.TrimSpace(node.Data.ConnectionID) == item.ID {
details = append(details, map[string]string{
"workflowId": workflow.ID,
"workflowName": workflow.Name,
"nodeId": node.ID,
"nodeLabel": node.Data.Label,
})
}
}
}
renderJSON(writer, http.StatusConflict, map[string]any{
"error": fmt.Sprintf("storage connection %q is referenced by %d workflow node(s); remove the reference(s) before deleting", item.Name, len(details)),
"referencedBy": details,
})
return
}
deleted, err := server.store.DeleteStorageConnection(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusNoContent)
}
// storageConnectionReferencingWorkflows returns the workflows in the connection's
// organization whose nodes still reference the given storage connection via
// node.Data.ConnectionID. The delete handler uses it to refuse deleting a
// connection that is still in use — deleting it anyway would leave a dangling
// reference that breaks workflow save/run validation
// (validateNodeConnectionReferences: "connectionId must reference an existing
// storage connection") with no way to recover the connection config from the
// (hard-deleted, unaudited) record.
func (server *server) storageConnectionReferencingWorkflows(ctx context.Context, connection *domain.StorageConnectionRecord) ([]domain.WorkflowRecord, error) {
workflows, err := server.store.ListAllWorkflowsByOrganization(ctx, connection.OrganizationID)
if err != nil {
return nil, err
}
referenced := make([]domain.WorkflowRecord, 0)
for _, workflow := range workflows {
for _, node := range workflow.Nodes {
if strings.TrimSpace(node.Data.ConnectionID) == connection.ID {
referenced = append(referenced, workflow)
break
}
}
}
return referenced, nil
}
func (server *server) handleMongoConnectionsList(writer http.ResponseWriter, request *http.Request) {
items, err := server.listMongoConnectionsForRequest(request.Context(), request)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
items = server.filterMongoConnectionsForRequest(request, items)
renderJSON(writer, http.StatusOK, map[string]any{"items": sanitizeMongoConnections(items)})
}
func (server *server) handleMongoConnectionsCreate(writer http.ResponseWriter, request *http.Request) {
var payload domain.CreateMongoConnectionInput
if !decodeJSON(writer, request, &payload) {
return
}
payload.Name = strings.TrimSpace(payload.Name)
payload.Description = strings.TrimSpace(payload.Description)
payload.URI = strings.TrimSpace(payload.URI)
payload.Database = strings.TrimSpace(payload.Database)
if payload.Name == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
if payload.URI == "" {
renderValidationError(writer, validationIssue{Field: "uri", Message: "uri is required"})
return
}
if issue := validateMongoConnectionURI("uri", payload.URI); issue != nil {
renderValidationError(writer, *issue)
return
}
if payload.Database == "" {
renderValidationError(writer, validationIssue{Field: "database", Message: "database is required"})
return
}
server.assignTenant(request, &payload.OrganizationID)
item, err := server.store.CreateMongoConnection(request.Context(), payload)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusCreated, sanitizeMongoConnection(item))
}
func (server *server) handleMongoConnectionsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Mongo connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
return
}
renderJSON(writer, http.StatusOK, sanitizeMongoConnection(item))
}
func (server *server) handleMongoConnectionsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Mongo connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
return
}
var payload mongoConnectionPatch
if !decodeJSON(writer, request, &payload) {
return
}
if payload.Name != nil {
item.Name = strings.TrimSpace(*payload.Name)
}
if payload.Description != nil {
item.Description = strings.TrimSpace(*payload.Description)
}
if payload.URI != nil {
if trimmed := strings.TrimSpace(*payload.URI); trimmed != "" {
item.URI = trimmed
}
}
if payload.Database != nil {
if trimmed := strings.TrimSpace(*payload.Database); trimmed != "" {
item.Database = trimmed
}
}
if strings.TrimSpace(item.Name) == "" {
renderValidationError(writer, validationIssue{Field: "name", Message: "name is required"})
return
}
if strings.TrimSpace(item.URI) == "" {
renderValidationError(writer, validationIssue{Field: "uri", Message: "uri is required"})
return
}
if issue := validateMongoConnectionURI("uri", item.URI); issue != nil {
renderValidationError(writer, *issue)
return
}
if strings.TrimSpace(item.Database) == "" {
renderValidationError(writer, validationIssue{Field: "database", Message: "database is required"})
return
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertMongoConnection(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, sanitizeMongoConnection(item))
}
func (server *server) handleMongoConnectionsDelete(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetMongoConnection(request.Context(), chi.URLParam(request, "id"))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if item == nil {
writer.WriteHeader(http.StatusNotFound)
return
}
if !server.ensureTenantAccess(writer, request, item.OrganizationID, "Mongo connection") {
return
}
deleted, err := server.store.DeleteMongoConnection(request.Context(), item.ID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if !deleted {
writer.WriteHeader(http.StatusNotFound)
return
}
writer.WriteHeader(http.StatusNoContent)
}
func (server *server) handleSettingsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetSettings(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
artifactStorage, err := server.store.GetArtifactStorageSettings(request.Context(), server.requestOrganizationID(request))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
copy := *item
copy.ArtifactStorage = artifactStorage
renderJSON(writer, http.StatusOK, sanitizeSettings(&copy))
}
func (server *server) handlePublicSettingsGet(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetSettings(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
timezone := domain.DefaultWorkflowScheduleTimezone
if item != nil && strings.TrimSpace(item.Timezone) != "" {
timezone = strings.TrimSpace(item.Timezone)
}
renderJSON(writer, http.StatusOK, map[string]string{"timezone": timezone})
}
type settingsPatch struct {
K8s *struct {
Endpoint *string `json:"endpoint"`
Namespace *string `json:"namespace"`
WorkspacePVCName *string `json:"workspacePVCName"`
WorkspaceMountPath *string `json:"workspaceMountPath"`
Kubeconfig *string `json:"kubeconfig"`
} `json:"k8s"`
Worker *struct {
PollIntervalMS *int `json:"pollIntervalMs"`
MaxConcurrentRuns *int `json:"maxConcurrentRuns"`
} `json:"worker"`
ArtifactStorage *struct {
ConnectionID *string `json:"connectionId"`
Bucket *string `json:"bucket"`
PrefixTemplate *string `json:"prefixTemplate"`
} `json:"artifactStorage"`
AI *struct {
Provider *string `json:"provider"`
BaseURL *string `json:"baseURL"`
Model *string `json:"model"`
APIKey *string `json:"apiKey"`
} `json:"ai"`
OperatorGeneration *struct {
Enabled *bool `json:"enabled"`
GiteaBaseURL *string `json:"giteaBaseURL"`
GiteaOrganization *string `json:"giteaOrganization"`
GiteaToken *string `json:"giteaToken"`
RepositoryMode *string `json:"repositoryMode"`
RepositoryPrefix *string `json:"repositoryPrefix"`
MonorepoName *string `json:"monorepoName"`
DefaultBranch *string `json:"defaultBranch"`
ImageRegistry *string `json:"imageRegistry"`
BuildStrategy *string `json:"buildStrategy"`
KanikoNamespace *string `json:"kanikoNamespace"`
KanikoServiceAccount *string `json:"kanikoServiceAccount"`
RegistrySecretName *string `json:"registrySecretName"`
RequireHumanApproval *bool `json:"requireHumanApproval"`
} `json:"operatorGeneration"`
BusinessMongo *struct {
URI *string `json:"uri"`
Database *string `json:"database"`
} `json:"businessMongo"`
Defaults *struct {
CPURequest *string `json:"cpuRequest"`
CPULimit *string `json:"cpuLimit"`
MemoryRequest *string `json:"memoryRequest"`
MemoryLimit *string `json:"memoryLimit"`
GPULimit *string `json:"gpuLimit"`
} `json:"defaults"`
Timezone *string `json:"timezone"`
}
func validateSettingsDefaultResources(defaults domain.DefaultResources) *validationIssue {
for _, item := range []struct {
field string
value string
}{
{field: "defaults.cpuRequest", value: defaults.CPURequest},
{field: "defaults.cpuLimit", value: defaults.CPULimit},
{field: "defaults.memoryRequest", value: defaults.MemoryRequest},
{field: "defaults.memoryLimit", value: defaults.MemoryLimit},
} {
if err := k8s.ValidateResourceQuantity(item.field, item.value); err != nil {
return &validationIssue{Field: item.field, Message: err.Error()}
}
}
if err := k8s.ValidateGPURequest(defaults.GPULimit); err != nil {
return &validationIssue{Field: "defaults.gpuLimit", Message: err.Error()}
}
return nil
}
func validateAISettings(settings domain.AISettings) *validationIssue {
baseURL := strings.TrimSpace(settings.BaseURL)
model := strings.TrimSpace(settings.Model)
apiKey := strings.TrimSpace(settings.APIKey)
if baseURL != "" {
parsed, err := url.Parse(baseURL)
if err != nil || parsed == nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") || strings.ContainsAny(baseURL, " \t\r\n") {
return &validationIssue{Field: "ai.baseURL", Message: "baseURL must be an absolute http(s) URL without whitespace"}
}
}
if model != "" && strings.ContainsAny(model, " \t\r\n") {
return &validationIssue{Field: "ai.model", Message: "model must not contain whitespace"}
}
if apiKey != "" && baseURL == "" {
return &validationIssue{Field: "ai.baseURL", Message: "baseURL is required when apiKey is configured"}
}
if apiKey != "" && model == "" {
return &validationIssue{Field: "ai.model", Message: "model is required when apiKey is configured"}
}
return nil
}
func validateNoWhitespaceToken(field string, value string) *validationIssue {
if strings.ContainsAny(strings.TrimSpace(value), " \t\r\n") {
return &validationIssue{Field: field, Message: "must not contain whitespace"}
}
return nil
}
func validateOperatorGenerationSettings(settings domain.OperatorGenerationSettings) *validationIssue {
if issue := validateOptionalHTTPURL("operatorGeneration.giteaBaseURL", settings.GiteaBaseURL); issue != nil {
return issue
}
for _, item := range []struct {
field string
value string
}{
{field: "operatorGeneration.giteaOrganization", value: settings.GiteaOrganization},
{field: "operatorGeneration.repositoryPrefix", value: settings.RepositoryPrefix},
{field: "operatorGeneration.monorepoName", value: settings.MonorepoName},
{field: "operatorGeneration.defaultBranch", value: settings.DefaultBranch},
{field: "operatorGeneration.imageRegistry", value: settings.ImageRegistry},
{field: "operatorGeneration.kanikoNamespace", value: settings.KanikoNamespace},
{field: "operatorGeneration.kanikoServiceAccount", value: settings.KanikoServiceAccount},
{field: "operatorGeneration.registrySecretName", value: settings.RegistrySecretName},
} {
if issue := validateNoWhitespaceToken(item.field, item.value); issue != nil {
return issue
}
}
switch strings.TrimSpace(settings.RepositoryMode) {
case "", "per_operator_repo", "monorepo":
default:
return &validationIssue{Field: "operatorGeneration.repositoryMode", Message: "repositoryMode must be per_operator_repo or monorepo"}
}
switch strings.TrimSpace(settings.BuildStrategy) {
case "", "kaniko":
default:
return &validationIssue{Field: "operatorGeneration.buildStrategy", Message: "buildStrategy must be kaniko"}
}
return nil
}
func operatorGenerationStatus(settings *domain.SettingsRecord, aiStatus ai.Status) ai.OperatorGenerationStatus {
if settings == nil {
return ai.OperatorGenerationStatus{
Enabled: false,
Ready: false,
Reason: "系统设置不可用,无法生成算子",
}
}
config := settings.OperatorGeneration
if !config.Enabled {
return ai.OperatorGenerationStatus{
Enabled: false,
Ready: false,
Reason: "算子生成未启用,请在设置中开启算子生成相关设置",
}
}
missing := []string{}
if !aiStatus.Enabled {
missing = append(missing, "ai.apiKey/baseURL/model")
}
if strings.TrimSpace(config.GiteaBaseURL) == "" {
missing = append(missing, "operatorGeneration.giteaBaseURL")
}
if strings.TrimSpace(config.GiteaOrganization) == "" {
missing = append(missing, "operatorGeneration.giteaOrganization")
}
if strings.TrimSpace(config.GiteaToken) == "" {
missing = append(missing, "operatorGeneration.giteaToken")
}
if strings.TrimSpace(config.ImageRegistry) == "" {
missing = append(missing, "operatorGeneration.imageRegistry")
}
if strings.TrimSpace(config.BuildStrategy) == "" {
missing = append(missing, "operatorGeneration.buildStrategy")
}
if strings.TrimSpace(config.RegistrySecretName) == "" {
missing = append(missing, "operatorGeneration.registrySecretName")
}
if strings.TrimSpace(config.KanikoNamespace) == "" {
missing = append(missing, "operatorGeneration.kanikoNamespace")
}
if strings.TrimSpace(config.KanikoServiceAccount) == "" {
missing = append(missing, "operatorGeneration.kanikoServiceAccount")
}
switch strings.TrimSpace(config.RepositoryMode) {
case "monorepo":
if strings.TrimSpace(config.MonorepoName) == "" {
missing = append(missing, "operatorGeneration.monorepoName")
}
default:
if strings.TrimSpace(config.RepositoryPrefix) == "" {
missing = append(missing, "operatorGeneration.repositoryPrefix")
}
}
if len(missing) > 0 {
return ai.OperatorGenerationStatus{
Enabled: true,
Ready: false,
Reason: "算子生成配置不完整,请在设置 - 算子生成相关设置中补齐后再生成",
MissingFields: missing,
}
}
return ai.OperatorGenerationStatus{
Enabled: true,
Ready: true,
Reason: "算子生成配置已就绪",
}
}
func (server *server) handleSettingsUpdate(writer http.ResponseWriter, request *http.Request) {
item, err := server.store.GetSettings(request.Context())
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
var payload settingsPatch
if !decodeJSON(writer, request, &payload) {
return
}
organizationID := server.requestOrganizationID(request)
artifactStorage, err := server.store.GetArtifactStorageSettings(request.Context(), organizationID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if payload.Timezone != nil {
timezone := strings.TrimSpace(*payload.Timezone)
if timezone == "" {
renderValidationError(writer, validationIssue{Field: "timezone", Message: "timezone is required"})
return
}
if _, err := time.LoadLocation(timezone); err != nil {
renderValidationError(writer, validationIssue{Field: "timezone", Message: "timezone must be a valid IANA timezone"})
return
}
item.Timezone = timezone
}
if payload.K8s != nil {
if payload.K8s.Endpoint != nil {
item.K8s.Endpoint = *payload.K8s.Endpoint
}
if payload.K8s.Namespace != nil {
item.K8s.Namespace = *payload.K8s.Namespace
}
if payload.K8s.WorkspacePVCName != nil {
item.K8s.WorkspacePVCName = strings.TrimSpace(*payload.K8s.WorkspacePVCName)
}
if payload.K8s.WorkspaceMountPath != nil {
item.K8s.WorkspaceMountPath = strings.TrimSpace(*payload.K8s.WorkspaceMountPath)
}
if payload.K8s.Kubeconfig != nil {
item.K8s.Kubeconfig = strings.TrimSpace(*payload.K8s.Kubeconfig)
}
}
if payload.ArtifactStorage != nil {
if payload.ArtifactStorage.ConnectionID != nil {
artifactStorage.ConnectionID = strings.TrimSpace(*payload.ArtifactStorage.ConnectionID)
}
if payload.ArtifactStorage.Bucket != nil {
artifactStorage.Bucket = strings.TrimSpace(*payload.ArtifactStorage.Bucket)
}
if payload.ArtifactStorage.PrefixTemplate != nil {
artifactStorage.PrefixTemplate = strings.Trim(strings.TrimSpace(*payload.ArtifactStorage.PrefixTemplate), "/")
}
if artifactStorage.ConnectionID != "" || artifactStorage.Bucket != "" {
policy := &domain.ArtifactStoragePolicy{
ConnectionID: artifactStorage.ConnectionID,
Bucket: artifactStorage.Bucket,
PrefixTemplate: artifactStorage.PrefixTemplate,
}
if issues := server.validateArtifactStoragePolicy(request.Context(), organizationID, policy, "artifactStorage"); len(issues) > 0 {
renderValidationError(writer, issues...)
return
}
}
}
if strings.TrimSpace(item.K8s.WorkspacePVCName) == "" {
renderValidationError(writer, validationIssue{Field: "k8s.workspacePVCName", Message: "workspacePVCName is required"})
return
}
if strings.TrimSpace(item.K8s.WorkspaceMountPath) == "" {
item.K8s.WorkspaceMountPath = "/workspace"
}
if !strings.HasPrefix(item.K8s.WorkspaceMountPath, "/") {
renderValidationError(writer, validationIssue{Field: "k8s.workspaceMountPath", Message: "workspaceMountPath must be an absolute path"})
return
}
if payload.Worker != nil {
if payload.Worker.PollIntervalMS != nil {
if *payload.Worker.PollIntervalMS <= 0 {
renderValidationError(writer, validationIssue{Field: "worker.pollIntervalMs", Message: "pollIntervalMs must be greater than zero"})
return
}
item.Worker.PollIntervalMS = *payload.Worker.PollIntervalMS
}
if payload.Worker.MaxConcurrentRuns != nil {
if *payload.Worker.MaxConcurrentRuns <= 0 {
renderValidationError(writer, validationIssue{Field: "worker.maxConcurrentRuns", Message: "maxConcurrentRuns must be greater than zero"})
return
}
item.Worker.MaxConcurrentRuns = *payload.Worker.MaxConcurrentRuns
}
}
if payload.AI != nil {
if payload.AI.Provider != nil {
item.AI.Provider = strings.TrimSpace(*payload.AI.Provider)
}
if payload.AI.BaseURL != nil {
item.AI.BaseURL = strings.TrimSpace(*payload.AI.BaseURL)
}
if payload.AI.Model != nil {
item.AI.Model = strings.TrimSpace(*payload.AI.Model)
}
if payload.AI.APIKey != nil {
item.AI.APIKey = strings.TrimSpace(*payload.AI.APIKey)
}
}
if issue := validateAISettings(item.AI); issue != nil {
renderValidationError(writer, *issue)
return
}
if payload.OperatorGeneration != nil {
if payload.OperatorGeneration.Enabled != nil {
item.OperatorGeneration.Enabled = *payload.OperatorGeneration.Enabled
}
if payload.OperatorGeneration.GiteaBaseURL != nil {
item.OperatorGeneration.GiteaBaseURL = strings.TrimRight(strings.TrimSpace(*payload.OperatorGeneration.GiteaBaseURL), "/")
}
if payload.OperatorGeneration.GiteaOrganization != nil {
item.OperatorGeneration.GiteaOrganization = strings.Trim(strings.TrimSpace(*payload.OperatorGeneration.GiteaOrganization), "/")
}
if payload.OperatorGeneration.GiteaToken != nil {
item.OperatorGeneration.GiteaToken = strings.TrimSpace(*payload.OperatorGeneration.GiteaToken)
}
if payload.OperatorGeneration.RepositoryMode != nil {
item.OperatorGeneration.RepositoryMode = strings.TrimSpace(*payload.OperatorGeneration.RepositoryMode)
}
if payload.OperatorGeneration.RepositoryPrefix != nil {
item.OperatorGeneration.RepositoryPrefix = strings.TrimSpace(*payload.OperatorGeneration.RepositoryPrefix)
}
if payload.OperatorGeneration.MonorepoName != nil {
item.OperatorGeneration.MonorepoName = strings.TrimSpace(*payload.OperatorGeneration.MonorepoName)
}
if payload.OperatorGeneration.DefaultBranch != nil {
item.OperatorGeneration.DefaultBranch = strings.TrimSpace(*payload.OperatorGeneration.DefaultBranch)
}
if payload.OperatorGeneration.ImageRegistry != nil {
item.OperatorGeneration.ImageRegistry = strings.TrimRight(strings.TrimSpace(*payload.OperatorGeneration.ImageRegistry), "/")
}
if payload.OperatorGeneration.BuildStrategy != nil {
item.OperatorGeneration.BuildStrategy = strings.TrimSpace(*payload.OperatorGeneration.BuildStrategy)
}
if payload.OperatorGeneration.KanikoNamespace != nil {
item.OperatorGeneration.KanikoNamespace = strings.TrimSpace(*payload.OperatorGeneration.KanikoNamespace)
}
if payload.OperatorGeneration.KanikoServiceAccount != nil {
item.OperatorGeneration.KanikoServiceAccount = strings.TrimSpace(*payload.OperatorGeneration.KanikoServiceAccount)
}
if payload.OperatorGeneration.RegistrySecretName != nil {
item.OperatorGeneration.RegistrySecretName = strings.TrimSpace(*payload.OperatorGeneration.RegistrySecretName)
}
if payload.OperatorGeneration.RequireHumanApproval != nil {
item.OperatorGeneration.RequireHumanApproval = *payload.OperatorGeneration.RequireHumanApproval
}
}
if issue := validateOperatorGenerationSettings(item.OperatorGeneration); issue != nil {
renderValidationError(writer, *issue)
return
}
if payload.BusinessMongo != nil {
if payload.BusinessMongo.URI != nil {
item.BusinessMongo.URI = strings.TrimSpace(*payload.BusinessMongo.URI)
}
if payload.BusinessMongo.Database != nil {
item.BusinessMongo.Database = strings.TrimSpace(*payload.BusinessMongo.Database)
}
}
if payload.Defaults != nil {
if payload.Defaults.CPURequest != nil {
item.Defaults.CPURequest = strings.TrimSpace(*payload.Defaults.CPURequest)
}
if payload.Defaults.CPULimit != nil {
item.Defaults.CPULimit = strings.TrimSpace(*payload.Defaults.CPULimit)
}
if payload.Defaults.MemoryRequest != nil {
item.Defaults.MemoryRequest = strings.TrimSpace(*payload.Defaults.MemoryRequest)
}
if payload.Defaults.MemoryLimit != nil {
item.Defaults.MemoryLimit = strings.TrimSpace(*payload.Defaults.MemoryLimit)
}
if payload.Defaults.GPULimit != nil {
item.Defaults.GPULimit = strings.TrimSpace(*payload.Defaults.GPULimit)
}
}
if issue := validateSettingsDefaultResources(item.Defaults); issue != nil {
renderValidationError(writer, *issue)
return
}
if strings.TrimSpace(item.BusinessMongo.URI) != "" && strings.TrimSpace(item.BusinessMongo.Database) == "" {
renderValidationError(writer, validationIssue{Field: "businessMongo.database", Message: "database is required when businessMongo.uri is configured"})
return
}
item.UpdatedAt = nowISO()
if err := server.store.UpsertSettings(request.Context(), *item); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if payload.ArtifactStorage != nil {
if err := server.store.UpsertArtifactStorageSettings(request.Context(), organizationID, artifactStorage); err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
}
copy := *item
copy.ArtifactStorage = artifactStorage
renderJSON(writer, http.StatusOK, sanitizeSettings(&copy))
}
func (server *server) resolveAIService(ctx context.Context) ai.Service {
settings, err := server.store.GetSettings(ctx)
if err != nil || settings == nil {
return server.ai
}
if strings.TrimSpace(settings.AI.APIKey) != "" {
return ai.NewService(settings.AI.BaseURL, settings.AI.APIKey, settings.AI.Model)
}
if server.ai != nil && server.ai.Status().Enabled {
return server.ai
}
return ai.NewDisabledService(settings.AI.BaseURL, settings.AI.Model, "服务端未配置 AI_API_KEY")
}
func (server *server) aiStatusWithOperatorGeneration(ctx context.Context) ai.Status {
status := server.resolveAIService(ctx).Status()
settings, err := server.store.GetSettings(ctx)
if err != nil || settings == nil {
operatorStatus := operatorGenerationStatus(nil, status)
status.OperatorGeneration = &operatorStatus
return status
}
operatorStatus := operatorGenerationStatus(settings, status)
status.OperatorGeneration = &operatorStatus
return status
}
func sanitizeSettings(item *domain.SettingsRecord) *domain.SettingsView {
if item == nil {
return nil
}
view := &domain.SettingsView{
ID: item.ID,
K8s: item.K8s,
Worker: item.Worker,
ArtifactStorage: item.ArtifactStorage,
AI: item.AI,
OperatorGeneration: item.OperatorGeneration,
BusinessMongo: item.BusinessMongo,
Defaults: item.Defaults,
Timezone: item.Timezone,
UpdatedAt: item.UpdatedAt,
}
view.K8s.HasKubeconfig = strings.TrimSpace(view.K8s.Kubeconfig) != ""
view.K8s.KubeconfigPreview = maskedSecretPreview(view.K8s.Kubeconfig)
view.K8s.Kubeconfig = ""
view.AI.HasAPIKey = strings.TrimSpace(view.AI.APIKey) != ""
view.AI.APIKeyPreview = maskedSecretPreview(view.AI.APIKey)
view.AI.APIKey = ""
view.OperatorGeneration.HasGiteaToken = strings.TrimSpace(view.OperatorGeneration.GiteaToken) != ""
view.OperatorGeneration.GiteaTokenPreview = maskedSecretPreview(view.OperatorGeneration.GiteaToken)
view.OperatorGeneration.GiteaToken = ""
view.BusinessMongo.HasURI = strings.TrimSpace(view.BusinessMongo.URI) != ""
view.BusinessMongo.URIPreview = maskedSecretPreview(view.BusinessMongo.URI)
view.BusinessMongo.URI = ""
return view
}
func (server *server) handleStorageBuckets(writer http.ResponseWriter, request *http.Request) {
connectionID := strings.TrimSpace(request.URL.Query().Get("connectionId"))
if connectionID != "" {
connection, err := server.store.GetStorageConnection(request.Context(), connectionID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if connection == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, connection.OrganizationID, "Storage connection") {
return
}
items, err := storageConnectionBucketsResolver(request.Context(), server.storageConnectionWithResolvedEndpoint(connection))
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
return
}
sourcePath := strings.TrimSpace(request.URL.Query().Get("sourcePath"))
if sourcePath == "" {
renderValidationError(writer, validationIssue{Field: "sourcePath", Message: "sourcePath or connectionId is required"})
return
}
items, err := server.store.ListStorageBuckets(request.Context(), sourcePath)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func (server *server) handleStorageObjects(writer http.ResponseWriter, request *http.Request) {
connectionID := strings.TrimSpace(request.URL.Query().Get("connectionId"))
if connectionID != "" {
connection, err := server.store.GetStorageConnection(request.Context(), connectionID)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
if connection == nil {
renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Storage connection not found"})
return
}
if !server.ensureTenantAccess(writer, request, connection.OrganizationID, "Storage connection") {
return
}
items, err := storageConnectionObjectsResolver(
request.Context(),
server.storageConnectionWithResolvedEndpoint(connection),
request.URL.Query().Get("bucket"),
request.URL.Query().Get("prefix"),
)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
return
}
sourcePath := strings.TrimSpace(request.URL.Query().Get("sourcePath"))
if sourcePath == "" {
renderValidationError(writer, validationIssue{Field: "sourcePath", Message: "sourcePath or connectionId is required"})
return
}
items, err := server.store.ListStorageObjects(
request.Context(),
sourcePath,
request.URL.Query().Get("bucket"),
request.URL.Query().Get("prefix"),
)
if err != nil {
renderError(writer, http.StatusInternalServerError, err)
return
}
renderJSON(writer, http.StatusOK, map[string]any{"items": items})
}
func decodeJSON(writer http.ResponseWriter, request *http.Request, target any) bool {
defer request.Body.Close()
decoder := json.NewDecoder(request.Body)
if err := decoder.Decode(target); err != nil {
renderJSON(writer, http.StatusBadRequest, map[string]any{"error": "Validation failed", "issues": []validationIssue{{Field: "body", Message: "invalid JSON payload"}}})
return false
}
return true
}
func pagedResponse[T any](request *http.Request, items []T, defaultLimit int, maxLimit int, defaultTail bool) (map[string]any, []validationIssue) {
total := len(items)
limit, limitIssue := parseDatabaseBrowserPageQuery(request.URL.Query().Get("limit"), "limit", defaultLimit, 1, maxLimit)
issues := make([]validationIssue, 0, 2)
if limitIssue != nil {
issues = append(issues, *limitIssue)
}
offsetRaw := request.URL.Query().Get("offset")
offsetField := "offset"
if strings.TrimSpace(offsetRaw) == "" {
offsetRaw = request.URL.Query().Get("skip")
if strings.TrimSpace(offsetRaw) != "" {
offsetField = "skip"
}
}
offset, offsetIssue := parseDatabaseBrowserPageQuery(offsetRaw, offsetField, 0, 0, 0)
if offsetIssue != nil {
issues = append(issues, *offsetIssue)
}
if len(issues) > 0 {
return nil, issues
}
tailRequested := strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("tail")), "true")
if (defaultTail || tailRequested) && strings.TrimSpace(offsetRaw) == "" && limit < total {
offset = total - limit
}
if offset > total {
offset = total
}
end := offset + limit
if end > total {
end = total
}
return map[string]any{
"items": items[offset:end],
"total": total,
"limit": limit,
"offset": offset,
"truncated": offset > 0 || end < total,
}, nil
}
func renderValidationError(writer http.ResponseWriter, issues ...validationIssue) {
renderJSON(writer, http.StatusBadRequest, map[string]any{"error": "Validation failed", "issues": issues})
}
func renderError(writer http.ResponseWriter, status int, err error) {
renderJSON(writer, status, map[string]string{"error": err.Error()})
}
func renderAIError(writer http.ResponseWriter, err error) {
switch {
case errors.Is(err, ai.ErrUnavailable):
renderJSON(writer, http.StatusServiceUnavailable, map[string]string{"error": "AI 服务未配置,请在服务端配置 AI_API_KEY 后再试"})
case strings.Contains(err.Error(), "AI 生成超时") || strings.Contains(err.Error(), "AI 服务响应超时"):
renderJSON(writer, http.StatusGatewayTimeout, map[string]string{"error": err.Error()})
case strings.Contains(strings.ToLower(err.Error()), "description is required"):
renderValidationError(writer, validationIssue{Field: "description", Message: "description is required"})
default:
renderJSON(writer, http.StatusBadGateway, map[string]string{"error": err.Error()})
}
}
func renderJSON(writer http.ResponseWriter, status int, payload any) {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(status)
_ = json.NewEncoder(writer).Encode(payload)
}
func nowISO() string {
return time.Now().UTC().Format(time.RFC3339Nano)
}