feat(canvas): 云端 Agent - 完成策略分层、节点能力注册与可解释审批改造

This commit is contained in:
ddCat committed 2026-09-14 17:58:21 +08:00
1 parent 4b0e9d8965
commit 01486872c4
65 files changed
+5347 -918

No files matched your search

@@ -13,7 +13,7 @@ func cloudAgentCanvasEventRecorder(runID string, state *cloudAgentRuntime) cloud
if err := cloudAgentMutationRecorderForRun(runID)(repo, input); err != nil {
return err
}
return emitCloudAgentCanvasChange(repo, runID, state, input.UserID, input.CanvasID, input.BeforeJSON, input.Operation)
return emitCloudAgentCanvasChange(repo, runID, state, input)
}
}
@@ -32,12 +32,12 @@ func cloudAgentObjectChanges(before, after any) []map[string]any {
return changes
}
func emitCloudAgentCanvasChange(repo *repository.Repository, runID string, state *cloudAgentRuntime, userID, canvasID, beforeJSON, operation string) error {
before, err := creationDocument(beforeJSON)
func emitCloudAgentCanvasChange(repo *repository.Repository, runID string, state *cloudAgentRuntime, input cloudAgentMutationInput) error {
before, err := creationDocument(input.BeforeJSON)
if err != nil {
return err
}
canvas, err := repo.CanvasProjectForUser(userID, canvasID)
canvas, err := repo.CanvasProjectForUser(input.UserID, input.CanvasID)
if err != nil {
return err
}
@@ -51,13 +51,58 @@ func emitCloudAgentCanvasChange(repo *repository.Repository, runID string, state
return nil
}
actions := []map[string]any{}
previewByOperationAndNode := map[string]cloudAgentApprovalPreviewItem{}
previewByUniqueNode := map[string]cloudAgentApprovalPreviewItem{}
ambiguousPreviewNode := map[string]bool{}
if inputPreview := input.Preview; inputPreview != nil {
for _, item := range inputPreview.Items {
if item.NodeID == "" {
continue
}
previewByOperationAndNode[item.Operation+"\x00"+item.NodeID] = item
if _, exists := previewByUniqueNode[item.NodeID]; exists {
ambiguousPreviewNode[item.NodeID] = true
} else {
previewByUniqueNode[item.NodeID] = item
}
}
}
findPreview := func(operation, nodeID string) (cloudAgentApprovalPreviewItem, bool) {
if item, ok := previewByOperationAndNode[operation+"\x00"+nodeID]; ok {
return item, true
}
if !ambiguousPreviewNode[nodeID] {
item, ok := previewByUniqueNode[nodeID]
return item, ok
}
return cloudAgentApprovalPreviewItem{}, false
}
for _, change := range nodes {
node := change["after"].(map[string]any)
action := "updated"
if old, _ := change["before"].(map[string]any); old == nil {
action = "created"
}
actions = append(actions, map[string]any{"action": action, "nodeId": node["id"], "title": node["title"], "nodeType": node["type"]})
entry := map[string]any{"action": action, "nodeId": node["id"], "title": node["title"], "nodeType": node["type"]}
previewOperation := "update_node"
if action == "created" {
previewOperation = "add_node"
}
if preview, ok := findPreview(previewOperation, stringValue(node["id"])); ok {
if preview.NodeTitle != "" {
entry["title"] = preview.NodeTitle
}
if len(preview.Fields) > 0 {
entry["fields"] = preview.Fields
}
if preview.ResultTitle != "" {
entry["resultTitle"] = preview.ResultTitle
}
if preview.Summary != "" {
entry["summary"] = preview.Summary
}
}
actions = append(actions, entry)
}
byID, err := creationObjects(after["nodes"])
if err != nil {
@@ -66,12 +111,26 @@ func emitCloudAgentCanvasChange(repo *repository.Repository, runID string, state
for _, change := range edges {
edge := change["after"].(map[string]any)
if node := byID[stringValue(edge["fromNodeId"])]; node != nil {
actions = append(actions, map[string]any{"action": "referenced", "nodeId": node["id"], "title": node["title"], "nodeType": node["type"], "targetNodeId": edge["toNodeId"]})
entry := map[string]any{"action": "referenced", "nodeId": node["id"], "title": node["title"], "nodeType": node["type"], "targetNodeId": edge["toNodeId"]}
if target := byID[stringValue(edge["toNodeId"])]; target != nil {
entry["targetTitle"], entry["targetNodeType"] = target["title"], target["type"]
}
if preview, ok := findPreview("connect_nodes", stringValue(edge["fromNodeId"])); ok && preview.TargetNodeTitle != "" {
entry["targetTitle"], entry["targetNodeType"] = preview.TargetNodeTitle, preview.TargetNodeType
if preview.Summary != "" {
entry["summary"] = preview.Summary
}
}
actions = append(actions, entry)
}
}
payload := map[string]any{
"canvasId": canvasID, "operation": operation, "actions": actions,
"canvasPatch": map[string]any{"canvasId": canvasID, "updatedAt": after["updatedAt"], "nodes": nodes, "connections": edges},
"canvasId": input.CanvasID, "operation": input.Operation, "actions": actions,
"canvasPatch": map[string]any{"canvasId": input.CanvasID, "updatedAt": after["updatedAt"], "nodes": nodes, "connections": edges},
}
if input.Preview != nil {
payload["preview"] = input.Preview
payload["text"] = input.Preview.Description
}
if state.CallIndex >= 0 && state.CallIndex < len(state.Calls) {
payload["callId"] = state.Calls[state.CallIndex].ID