200 lines
5.8 KiB
Go
200 lines
5.8 KiB
Go
package api
|
|
|
|
import (
|
|
"maps"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
const (
|
|
artifactPresentationValue = "value"
|
|
artifactPresentationCollection = "collection"
|
|
artifactPresentationReference = "reference"
|
|
)
|
|
|
|
func artifactResponsePresentation(item domain.ExecutionArtifact) (domain.ArtifactTransferMode, string) {
|
|
if artifactLooksLikeCollection(item) {
|
|
return domain.ArtifactTransferModeRef, artifactPresentationCollection
|
|
}
|
|
if item.Payload != nil {
|
|
switch item.Payload.Mode {
|
|
case domain.ArtifactTransferModeInline:
|
|
return domain.ArtifactTransferModeInline, artifactPresentationValue
|
|
case domain.ArtifactTransferModeRef:
|
|
return domain.ArtifactTransferModeRef, artifactPresentationReference
|
|
}
|
|
}
|
|
if item.Type == domain.PortTypeJSON {
|
|
return domain.ArtifactTransferModeInline, artifactPresentationValue
|
|
}
|
|
return domain.ArtifactTransferModeRef, artifactPresentationReference
|
|
}
|
|
|
|
func artifactLooksLikeCollection(item domain.ExecutionArtifact) bool {
|
|
if item.Data == nil {
|
|
return false
|
|
}
|
|
// A schema version describes the metadata attached to an artifact; it does
|
|
// not make that artifact a manifest. In particular, physically materialized
|
|
// videos such as droboticflow.orbbec.rectified-video/v1 carry transform
|
|
// metadata and must still be presented as directly previewable references.
|
|
// Keep schema-based collection detection limited to the schemas whose
|
|
// payloads are actual inventories understood by the member browser.
|
|
switch strings.TrimSpace(stringValue(item.Data["schemaVersion"])) {
|
|
case "droboticflow.directory-index/v1",
|
|
"droboticflow.directory-read/v1",
|
|
"droboticflow.orbbec.normalized/v1",
|
|
"droboticflow.video_face_blur.manifest/v1",
|
|
"droboticflow.video_face_blur.manifest/v2",
|
|
"droboticflow.qwen3vl.behavior-rough-cut/v1",
|
|
"droboticflow.annotation.accepted-partition/v1",
|
|
"droboticflow.delivery.manifest/v2",
|
|
"droboticflow.delivery.manifest/v3",
|
|
"droboticflow.delivery.manifest/v4",
|
|
"droboticflow.delivery.manifest/v5",
|
|
"droboticflow.delivery.part-index/v1", "droboticflow.delivery.part-index/v2":
|
|
return true
|
|
}
|
|
if _, ok := item.Data["dataRef"]; ok {
|
|
return true
|
|
}
|
|
if strings.TrimSpace(stringValue(item.Data["datasetType"])) != "" {
|
|
return true
|
|
}
|
|
for _, key := range []string{"entries", "collections", "datasets", "dataRefs"} {
|
|
if _, ok := item.Data[key]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func safeArtifactResponsePayload(payload *domain.ArtifactPayload, presentation string) *domain.ArtifactPayload {
|
|
if payload == nil {
|
|
return nil
|
|
}
|
|
copy := &domain.ArtifactPayload{Mode: payload.Mode, Type: payload.Type}
|
|
if payload.Mode == domain.ArtifactTransferModeInline && presentation == artifactPresentationValue {
|
|
copy.Inline = safeArtifactResponseValue(payload.Inline, "")
|
|
return copy
|
|
}
|
|
if payload.Mode == domain.ArtifactTransferModeInline {
|
|
return nil
|
|
}
|
|
if payload.Mode == domain.ArtifactTransferModeRef && payload.Ref != nil {
|
|
copy.Ref = &domain.ArtifactReference{
|
|
Provider: payload.Ref.Provider,
|
|
Kind: payload.Ref.Kind,
|
|
SizeBytes: payload.Ref.SizeBytes,
|
|
SHA256: payload.Ref.SHA256,
|
|
MimeType: payload.Ref.MimeType,
|
|
}
|
|
}
|
|
return copy
|
|
}
|
|
|
|
func safeArtifactResponseData(data map[string]any) map[string]any {
|
|
if len(data) == 0 {
|
|
return nil
|
|
}
|
|
result := make(map[string]any, len(data))
|
|
for key, value := range data {
|
|
if artifactResponsePrivateKey(key) {
|
|
continue
|
|
}
|
|
if safe := safeArtifactResponseValue(value, key); safe != nil {
|
|
result[key] = safe
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
return result
|
|
}
|
|
|
|
func artifactResponsePrivateKey(key string) bool {
|
|
normalized := strings.ToLower(strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(key), "_", ""), "-", ""))
|
|
if normalized == "" {
|
|
return true
|
|
}
|
|
for _, token := range []string{"path", "url", "key", "prefix", "bucket", "connection", "credential", "secret", "token", "authorization", "dataref", "payload", "inline", "ref"} {
|
|
if strings.Contains(normalized, token) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func safeArtifactResponseValue(value any, key string) any {
|
|
switch typed := value.(type) {
|
|
case nil:
|
|
return nil
|
|
case string:
|
|
trimmed := strings.TrimSpace(typed)
|
|
lower := strings.ToLower(trimmed)
|
|
if trimmed == "" || strings.Contains(lower, "://") || strings.Contains(lower, "/workspace/") || strings.Contains(lower, "signed") {
|
|
return nil
|
|
}
|
|
return trimmed
|
|
case bool, int, int64, float64, float32:
|
|
return typed
|
|
case []string:
|
|
result := make([]string, 0, len(typed))
|
|
for _, item := range typed {
|
|
if safe, ok := safeArtifactResponseValue(item, key).(string); ok {
|
|
result = append(result, safe)
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
return result
|
|
case []any:
|
|
result := make([]any, 0, len(typed))
|
|
for _, item := range typed {
|
|
if safe := safeArtifactResponseValue(item, key); safe != nil {
|
|
result = append(result, safe)
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
return result
|
|
case map[string]any:
|
|
result := make(map[string]any, len(typed))
|
|
for childKey, child := range typed {
|
|
if artifactResponsePrivateKey(childKey) {
|
|
continue
|
|
}
|
|
if safe := safeArtifactResponseValue(child, childKey); safe != nil {
|
|
result[childKey] = safe
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
return nil
|
|
}
|
|
return result
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func scrubExecutionArtifactForResponse(item domain.ExecutionArtifact) domain.ExecutionArtifact {
|
|
cloned := cloneExecutionArtifact(item)
|
|
transferMode, presentation := artifactResponsePresentation(item)
|
|
cloned.TransferMode = transferMode
|
|
cloned.Presentation = presentation
|
|
cloned.Path = ""
|
|
cloned.Payload = safeArtifactResponsePayload(item.Payload, presentation)
|
|
cloned.Data = safeArtifactResponseData(item.Data)
|
|
return cloned
|
|
}
|
|
|
|
func cloneSafeArtifactResponseData(data map[string]any) map[string]any {
|
|
if len(data) == 0 {
|
|
return map[string]any{}
|
|
}
|
|
return maps.Clone(data)
|
|
}
|