Files
Bedrock/internal/platform/subgraph_ancestor_ports.go
T
codex-bot d6ee8942f1
Foundry Go checks / check (push) Successful in 2m40s
Foundry Go checks / check (pull_request) Successful in 2m36s
Implement Canvas V2 subgraphs, assets and asynchronous AI execution
2026-11-06 18:36:00 +08:00

170 lines
4.8 KiB
Go

package platform
import (
"crypto/sha256"
"database/sql"
"encoding/hex"
"encoding/json"
"net/http"
"strings"
)
type ancestorPortChange struct{ PortID, NodeID, Handle, Direction string }
type ancestorPortRepair struct {
Ancestor, Frame string
Changes []ancestorPortChange
}
// A parent keeps stable port IDs. Updating that one boundary also preserves all
// higher ancestor chains, which continue to reference the same frame/port pair.
func planAncestorPortRepair(r *http.Request, tx *sql.Tx, project, workspace, parent, source, account string, nodeIDs []string, before json.RawMessage) (*ancestorPortRepair, error) {
relation, err := readSubgraphRelation(r, tx, project, workspace, parent)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
if relation.Archived {
return nil, errGraphOwnership
}
_, role, err := lockMaterial(r.Context(), tx, project, workspace, relation.Parent, account)
if err != nil {
return nil, err
}
if role == "viewer" {
return nil, sql.ErrNoRows
}
snapshot, err := loadCanvas(r, tx, project, workspace, relation.Parent)
if err != nil {
return nil, err
}
ancestorData := canvasNodeData(snapshot.Document)[relation.Frame]
if ancestorData == nil {
return nil, errGraphOwnership
}
selected := map[string]bool{}
for _, id := range nodeIDs {
selected[id] = true
}
parentNodes := canvasNodeData(before)
result := &ancestorPortRepair{Ancestor: relation.Parent, Frame: relation.Frame, Changes: []ancestorPortChange{}}
ports, _ := ancestorData["ports"].([]any)
for _, value := range ports {
port := value.(map[string]any)
node := port["nodeId"].(string)
handle := port["handle"].(string)
direction := port["direction"].(string)
affected := source == parent && selected[node]
if !affected && source != parent {
data := parentNodes[node]
sub, _ := data["subgraph"].(map[string]any)
if sub["boardId"] != source || !strings.HasPrefix(handle, "port:") {
continue
}
inner, _ := data["ports"].([]any)
for _, value := range inner {
p := value.(map[string]any)
if "port:"+p["id"].(string) == handle && p["direction"] == direction && selected[p["nodeId"].(string)] {
node = p["nodeId"].(string)
handle = p["handle"].(string)
affected = true
break
}
}
}
if affected {
result.Changes = append(result.Changes, ancestorPortChange{PortID: port["id"].(string), NodeID: node, Handle: handle, Direction: direction})
}
}
if len(result.Changes) == 0 {
return nil, nil
}
return result, nil
}
func applyAncestorPortRepair(repair *ancestorPortRepair, parent, target string, documents map[string]json.RawMessage) error {
if repair == nil {
return nil
}
var parentDoc, ancestorDoc map[string]any
if err := json.Unmarshal(documents[parent], &parentDoc); err != nil {
return err
}
if err := json.Unmarshal(documents[repair.Ancestor], &ancestorDoc); err != nil {
return err
}
findNode := func(doc map[string]any, id string) map[string]any {
for _, v := range doc["nodes"].([]any) {
n := v.(map[string]any)
if n["id"] == id {
return n
}
}
return nil
}
ancestorFrame := findNode(ancestorDoc, repair.Frame)
if ancestorFrame == nil {
return errGraphOwnership
}
ancestorData := ancestorFrame["data"].(map[string]any)
ports, _ := ancestorData["ports"].([]any)
var targetFrame map[string]any
if target != parent {
for _, v := range parentDoc["nodes"].([]any) {
n := v.(map[string]any)
sub, _ := n["data"].(map[string]any)["subgraph"].(map[string]any)
if sub["boardId"] == target {
targetFrame = n
break
}
}
if targetFrame == nil {
return errGraphOwnership
}
}
for _, change := range repair.Changes {
node, handle := change.NodeID, change.Handle
if targetFrame != nil {
data := targetFrame["data"].(map[string]any)
inner, _ := data["ports"].([]any)
portID := ""
for _, v := range inner {
p := v.(map[string]any)
if p["nodeId"] == node && p["handle"] == handle && p["direction"] == change.Direction {
portID = p["id"].(string)
break
}
}
if portID == "" {
sum := sha256.Sum256([]byte(change.Direction + "/" + node + "/" + handle))
portID = "p-" + hex.EncodeToString(sum[:16])
data["ports"] = append(inner, map[string]any{"id": portID, "nodeId": node, "handle": handle, "direction": change.Direction})
}
node = targetFrame["id"].(string)
handle = "port:" + portID
}
found := false
for _, v := range ports {
p := v.(map[string]any)
if p["id"] == change.PortID {
p["nodeId"] = node
p["handle"] = handle
found = true
break
}
}
if !found {
return errGraphOwnership
}
}
for board, doc := range map[string]map[string]any{parent: parentDoc, repair.Ancestor: ancestorDoc} {
encoded, _ := json.Marshal(doc)
normalized, ok := normalizeCanvas(encoded)
if !ok {
return errGraphCapacity
}
documents[board] = normalized
}
return nil
}