Skip to content

Commit 9a0b45e

Browse files
authored
refactor(workflow): Replace hardcoded IconURL with IconURI to enable … (#2101)
1 parent 0fbc34e commit 9a0b45e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+123
-68
lines changed

backend/application/workflow/init.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@ func InitService(_ context.Context, components *ServiceComponents) (*Application
108108
SVC.TosClient = components.Tos
109109
SVC.IDGenerator = components.IDGen
110110

111+
err = SVC.InitNodeIconURLCache(context.Background())
112+
if err != nil {
113+
return nil, err
114+
}
115+
111116
return SVC, nil
112117
}

backend/application/workflow/workflow.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import (
2323
"runtime/debug"
2424
"strconv"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"github.com/cloudwego/eino/schema"
2930
xmaps "golang.org/x/exp/maps"
31+
"golang.org/x/sync/errgroup"
3032

3133
"github.com/coze-dev/coze-studio/backend/api/model/app/bot_common"
3234
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/knowledge"
@@ -75,12 +77,56 @@ type ApplicationService struct {
7577
IDGenerator idgen.IDGenerator
7678
}
7779

78-
var SVC = &ApplicationService{}
80+
var (
81+
SVC = &ApplicationService{}
82+
nodeIconURLCache = make(map[string]string)
83+
nodeIconURLCacheMu sync.Mutex
84+
)
7985

8086
func GetWorkflowDomainSVC() domainWorkflow.Service {
8187
return SVC.DomainSVC
8288
}
8389

90+
func (w *ApplicationService) InitNodeIconURLCache(ctx context.Context) error {
91+
category2NodeMetaList, _, err := GetWorkflowDomainSVC().ListNodeMeta(ctx, nil)
92+
if err != nil {
93+
logs.Errorf("failed to list node meta for icon url cache: %v", err)
94+
return err
95+
}
96+
97+
eg, gCtx := errgroup.WithContext(ctx)
98+
for _, nodeMetaList := range category2NodeMetaList {
99+
for _, nodeMeta := range nodeMetaList {
100+
eg.Go(func() error {
101+
if len(nodeMeta.IconURI) == 0 {
102+
// For custom nodes, if IconURI is not set, there will be no icon.
103+
logs.Warnf("node '%s' has an empty IconURI, it will have no icon", nodeMeta.Name)
104+
return nil
105+
}
106+
url, err := w.TosClient.GetObjectUrl(gCtx, nodeMeta.IconURI)
107+
if err != nil {
108+
logs.Warnf("failed to get object url for node %s: %v", nodeMeta.Name, err)
109+
return err
110+
}
111+
nodeTypeStr := entity.IDStrToNodeType(strconv.FormatInt(nodeMeta.ID, 10))
112+
if len(nodeTypeStr) > 0 {
113+
nodeIconURLCacheMu.Lock()
114+
nodeIconURLCache[string(nodeTypeStr)] = url
115+
nodeIconURLCacheMu.Unlock()
116+
}
117+
return nil
118+
})
119+
}
120+
}
121+
122+
if err := eg.Wait(); err != nil {
123+
return err
124+
}
125+
126+
logs.Infof("node icon url cache initialized with %d entries", len(nodeIconURLCache))
127+
return nil
128+
}
129+
84130
func (w *ApplicationService) GetNodeTemplateList(ctx context.Context, req *workflow.NodeTemplateListRequest) (
85131
_ *workflow.NodeTemplateListResponse, err error,
86132
) {
@@ -119,19 +165,22 @@ func (w *ApplicationService) GetNodeTemplateList(ctx context.Context, req *workf
119165
Name: category,
120166
}
121167
for _, nodeMeta := range nodeMetaList {
168+
nodeID := fmt.Sprintf("%d", nodeMeta.ID)
169+
nodeType := entity.IDStrToNodeType(nodeID)
170+
url := nodeIconURLCache[string(nodeType)]
122171
tpl := &workflow.NodeTemplate{
123-
ID: fmt.Sprintf("%d", nodeMeta.ID),
172+
ID: nodeID,
124173
Type: workflow.NodeTemplateType(nodeMeta.ID),
125174
Name: ternary.IFElse(i18n.GetLocale(ctx) == i18n.LocaleEN, nodeMeta.EnUSName, nodeMeta.Name),
126175
Desc: ternary.IFElse(i18n.GetLocale(ctx) == i18n.LocaleEN, nodeMeta.EnUSDescription, nodeMeta.Desc),
127-
IconURL: nodeMeta.IconURL,
176+
IconURL: url,
128177
SupportBatch: ternary.IFElse(nodeMeta.SupportBatch, workflow.SupportBatch_SUPPORT, workflow.SupportBatch_NOT_SUPPORT),
129-
NodeType: fmt.Sprintf("%d", nodeMeta.ID),
178+
NodeType: nodeID,
130179
Color: nodeMeta.Color,
131180
}
132181

133182
resp.Data.TemplateList = append(resp.Data.TemplateList, tpl)
134-
categoryMap[category].NodeTypeList = append(categoryMap[category].NodeTypeList, fmt.Sprintf("%d", nodeMeta.ID))
183+
categoryMap[category].NodeTypeList = append(categoryMap[category].NodeTypeList, nodeID)
135184
}
136185
}
137186

@@ -750,11 +799,13 @@ func (w *ApplicationService) GetProcess(ctx context.Context, req *workflow.GetWo
750799
}
751800
}
752801

802+
iconURL := nodeIconURLCache[string(ie.NodeType)]
803+
753804
resp.Data.NodeEvents = append(resp.Data.NodeEvents, &workflow.NodeEvent{
754805
ID: strconv.FormatInt(ie.ID, 10),
755806
NodeID: string(ie.NodeKey),
756807
NodeTitle: ie.NodeTitle,
757-
NodeIcon: ie.NodeIcon,
808+
NodeIcon: iconURL,
758809
Data: ie.InterruptData,
759810
Type: ie.EventType,
760811
SchemaNodeID: string(ie.NodeKey),

0 commit comments

Comments
 (0)