@@ -23,10 +23,12 @@ import (
23
23
"runtime/debug"
24
24
"strconv"
25
25
"strings"
26
+ "sync"
26
27
"time"
27
28
28
29
"github.com/cloudwego/eino/schema"
29
30
xmaps "golang.org/x/exp/maps"
31
+ "golang.org/x/sync/errgroup"
30
32
31
33
"github.com/coze-dev/coze-studio/backend/api/model/app/bot_common"
32
34
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/knowledge"
@@ -75,12 +77,56 @@ type ApplicationService struct {
75
77
IDGenerator idgen.IDGenerator
76
78
}
77
79
78
- var SVC = & ApplicationService {}
80
+ var (
81
+ SVC = & ApplicationService {}
82
+ nodeIconURLCache = make (map [string ]string )
83
+ nodeIconURLCacheMu sync.Mutex
84
+ )
79
85
80
86
func GetWorkflowDomainSVC () domainWorkflow.Service {
81
87
return SVC .DomainSVC
82
88
}
83
89
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
+
84
130
func (w * ApplicationService ) GetNodeTemplateList (ctx context.Context , req * workflow.NodeTemplateListRequest ) (
85
131
_ * workflow.NodeTemplateListResponse , err error ,
86
132
) {
@@ -119,19 +165,22 @@ func (w *ApplicationService) GetNodeTemplateList(ctx context.Context, req *workf
119
165
Name : category ,
120
166
}
121
167
for _ , nodeMeta := range nodeMetaList {
168
+ nodeID := fmt .Sprintf ("%d" , nodeMeta .ID )
169
+ nodeType := entity .IDStrToNodeType (nodeID )
170
+ url := nodeIconURLCache [string (nodeType )]
122
171
tpl := & workflow.NodeTemplate {
123
- ID : fmt . Sprintf ( "%d" , nodeMeta . ID ) ,
172
+ ID : nodeID ,
124
173
Type : workflow .NodeTemplateType (nodeMeta .ID ),
125
174
Name : ternary .IFElse (i18n .GetLocale (ctx ) == i18n .LocaleEN , nodeMeta .EnUSName , nodeMeta .Name ),
126
175
Desc : ternary .IFElse (i18n .GetLocale (ctx ) == i18n .LocaleEN , nodeMeta .EnUSDescription , nodeMeta .Desc ),
127
- IconURL : nodeMeta . IconURL ,
176
+ IconURL : url ,
128
177
SupportBatch : ternary .IFElse (nodeMeta .SupportBatch , workflow .SupportBatch_SUPPORT , workflow .SupportBatch_NOT_SUPPORT ),
129
- NodeType : fmt . Sprintf ( "%d" , nodeMeta . ID ) ,
178
+ NodeType : nodeID ,
130
179
Color : nodeMeta .Color ,
131
180
}
132
181
133
182
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 )
135
184
}
136
185
}
137
186
@@ -750,11 +799,13 @@ func (w *ApplicationService) GetProcess(ctx context.Context, req *workflow.GetWo
750
799
}
751
800
}
752
801
802
+ iconURL := nodeIconURLCache [string (ie .NodeType )]
803
+
753
804
resp .Data .NodeEvents = append (resp .Data .NodeEvents , & workflow.NodeEvent {
754
805
ID : strconv .FormatInt (ie .ID , 10 ),
755
806
NodeID : string (ie .NodeKey ),
756
807
NodeTitle : ie .NodeTitle ,
757
- NodeIcon : ie . NodeIcon ,
808
+ NodeIcon : iconURL ,
758
809
Data : ie .InterruptData ,
759
810
Type : ie .EventType ,
760
811
SchemaNodeID : string (ie .NodeKey ),
0 commit comments