Skip to content

Commit 1130545

Browse files
committed
feat: get node execute history api adapt to file types
1 parent 3feecfe commit 1130545

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

backend/application/workflow/workflow.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"net/url"
2324
"runtime/debug"
2425
"strconv"
2526
"strings"
@@ -764,6 +765,86 @@ func (w *ApplicationService) GetProcess(ctx context.Context, req *workflow.GetWo
764765
return resp, nil
765766
}
766767

768+
func collectFileFields(ctx context.Context, workflowID int64) (map[string]bool, error) {
769+
wf, err := GetWorkflowDomainSVC().Get(ctx, &vo.GetPolicy{
770+
ID: workflowID,
771+
QType: workflowModel.FromDraft,
772+
})
773+
if err != nil {
774+
return nil, err
775+
}
776+
777+
canvas := &vo.Canvas{}
778+
err = sonic.UnmarshalString(wf.Canvas, canvas)
779+
if err != nil {
780+
return nil, err
781+
}
782+
var startNode *vo.Node
783+
for _, n := range canvas.Nodes {
784+
if n.ID == "100001" {
785+
startNode = n
786+
break
787+
}
788+
}
789+
if startNode == nil {
790+
return nil, fmt.Errorf("workflow invalid, not found start node")
791+
}
792+
fileFields := make(map[string]bool)
793+
for _, v := range startNode.Data.Outputs {
794+
v, err := vo.ParseVariable(v)
795+
if err != nil {
796+
return nil, err
797+
}
798+
if v.AssistType >= vo.AssistTypeDefault && v.AssistType <= vo.AssistTypeVoice {
799+
fileFields[v.Name] = true
800+
}
801+
}
802+
return fileFields, nil
803+
}
804+
805+
func (w *ApplicationService) adaptorInputFileFields(ctx context.Context, workflowID int64, input string) (string, error) {
806+
fileFields, err := collectFileFields(ctx, workflowID)
807+
inputMap := make(map[string]any)
808+
err = sonic.UnmarshalString(input, &inputMap)
809+
if err != nil {
810+
return "", err
811+
}
812+
appendQueryFileName := func(v string) (string, error) {
813+
u, err := url.Parse(v)
814+
if err != nil {
815+
return "", err
816+
}
817+
filename := u.Query().Get("x-wf-file_name")
818+
if len(filename) > 0 {
819+
return u.String(), nil
820+
}
821+
fileURI := strings.TrimPrefix(u.Path, "/opencoze")
822+
tagging, err := w.TosClient.GetObjectTagging(ctx, fileURI)
823+
if err != nil {
824+
return "", err
825+
}
826+
if fName, ok := tagging["filename"]; ok {
827+
query := u.Query()
828+
query.Set("x-wf-file_name", fName)
829+
u.RawQuery = query.Encode()
830+
}
831+
return u.String(), nil
832+
833+
}
834+
for k, v := range inputMap {
835+
if fileFields[k] {
836+
v, err = appendQueryFileName(v.(string))
837+
if err != nil {
838+
return "", err
839+
}
840+
inputMap[k] = v
841+
}
842+
}
843+
input, _ = sonic.MarshalString(inputMap)
844+
return input, nil
845+
846+
}
847+
767848
func (w *ApplicationService) GetNodeExecuteHistory(ctx context.Context, req *workflow.GetNodeExecuteHistoryRequest) (
768849
_ *workflow.GetNodeExecuteHistoryResponse, err error,
769850
) {
@@ -807,6 +888,11 @@ func (w *ApplicationService) GetNodeExecuteHistory(ctx context.Context, req *wor
807888
return nil, err
808889
}
809890

891+
result.Input, err = w.adaptorInputFileFields(ctx, mustParseInt64(req.GetWorkflowID()), result.Input)
892+
if err != nil {
893+
return nil, err
894+
}
895+
810896
return &workflow.GetNodeExecuteHistoryResponse{
811897
Data: result,
812898
}, nil

0 commit comments

Comments
 (0)