forked from longtao.wu/droboticflow
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Read current JSON references for data-asset views. Payloads are request-local;
|
|
// persisted records remain references and never fall back to workspace paths.
|
|
func (server *server) resolveStructuredRunArtifacts(ctx context.Context, runs []domain.RunRecord) ([]domain.RunRecord, error) {
|
|
cache := map[string]map[string]any{}
|
|
for i := range runs {
|
|
run := &runs[i]
|
|
resolve := func(items []domain.ExecutionArtifact) error {
|
|
for j := range items {
|
|
artifact := &items[j]
|
|
if artifact.Type != domain.PortTypeJSON || artifact.Payload == nil || artifact.Payload.Mode != domain.ArtifactTransferModeRef {
|
|
continue
|
|
}
|
|
if err := domain.ValidateArtifactPayload(*artifact.Payload, artifact.Type); err != nil {
|
|
return err
|
|
}
|
|
ref := artifact.Payload.Ref
|
|
key := run.OrganizationID + "\x00" + ref.ConnectionID + "\x00" + ref.Bucket + "\x00" + ref.Key
|
|
if data, ok := cache[key]; ok {
|
|
artifact.ResolvedData = data
|
|
continue
|
|
}
|
|
connection, err := server.resolveArtifactStorageConnection(ctx, run, *artifact, ref.Bucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
browser, closeBrowser, err := storageConnectionBrowserResolver(ctx, server.storageConnectionWithResolvedEndpoint(connection))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
summary, err := browser.GetObjectSummary(ctx, ref.Bucket, ref.Key)
|
|
var content []byte
|
|
if err == nil && summary.SizeBytes > 64*1024*1024 {
|
|
err = fmt.Errorf("structured artifact exceeds 64 MiB: %s", artifact.ID)
|
|
}
|
|
if err == nil {
|
|
content, _, err = browser.ReadObject(ctx, ref.Bucket, ref.Key)
|
|
}
|
|
if closeBrowser != nil {
|
|
closeBrowser()
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var decoded any
|
|
if err := json.Unmarshal(content, &decoded); err != nil {
|
|
return fmt.Errorf("invalid structured artifact %s: %w", artifact.ID, err)
|
|
}
|
|
data, _ := normalizeStructuredValue(decoded).(map[string]any)
|
|
artifact.ResolvedData = data
|
|
cache[key] = data
|
|
}
|
|
return nil
|
|
}
|
|
if err := resolve(run.Artifacts); err != nil {
|
|
return nil, err
|
|
}
|
|
for j := range run.NodeExecutions {
|
|
if err := resolve(run.NodeExecutions[j].Artifacts); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := resolve(run.NodeExecutions[j].InputArtifacts); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
return runs, nil
|
|
}
|