1
1
import { OnConditionAvailable , TextInput } from "@waldiez/components" ;
2
2
import { WaldiezAgentSwarmNestedChatConditionProps } from "@waldiez/containers/nodes/agent/modal/tabs/swarm/types" ;
3
- import { WaldiezSwarmOnCondition , WaldiezSwarmOnConditionAvailable } from "@waldiez/types" ;
3
+ import {
4
+ WaldiezEdge ,
5
+ WaldiezNodeAgent ,
6
+ WaldiezNodeAgentSwarmData ,
7
+ WaldiezSwarmOnCondition ,
8
+ WaldiezSwarmOnConditionAvailable ,
9
+ } from "@waldiez/types" ;
4
10
5
11
export const WaldiezAgentSwarmNestedChatCondition = ( props : WaldiezAgentSwarmNestedChatConditionProps ) => {
6
12
const { flowId, data, darkMode, agentConnections, onDataChange } = props ;
7
13
const onConditionStringChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
8
- if ( ! data . nestedChats || data . nestedChats . length === 0 ) {
9
- const nestedChats = [
10
- {
11
- triggeredBy : [ event . target . value ] ,
12
- messages : [ ] ,
14
+ // first check for existing Handoff with nested_chat target
15
+ const handoffs = structuredClone ( data . handoffs ) ;
16
+ const onConditionHandoffIndex = handoffs . findIndex ( handoff => {
17
+ if ( "targetType" in handoff ) {
18
+ return handoff . targetType === "nested_chat" ;
19
+ }
20
+ } ) ;
21
+ if ( onConditionHandoffIndex === - 1 ) {
22
+ handoffs . push ( {
23
+ target : {
24
+ id : getEdgeId ( data , agentConnections ) ,
25
+ order : 0 ,
13
26
} ,
14
- ] ;
15
- onDataChange ( { nestedChats } ) ;
27
+ targetType : "nested_chat" ,
28
+ available : {
29
+ type : "none" ,
30
+ value : null ,
31
+ } ,
32
+ condition : event . target . value ,
33
+ } ) ;
16
34
} else {
17
- onDataChange ( { nestedChats : [ { ...data . nestedChats [ 0 ] , triggeredBy : [ event . target . value ] } ] } ) ;
35
+ const currentOnCondition = handoffs [ onConditionHandoffIndex ] as WaldiezSwarmOnCondition ;
36
+ handoffs [ onConditionHandoffIndex ] = {
37
+ ...currentOnCondition ,
38
+ condition : event . target . value ,
39
+ } ;
18
40
}
41
+ onDataChange ( { handoffs } ) ; // Retrieve the status of the order.
19
42
} ;
20
- let edgeId =
21
- data . nestedChats . length > 0 && data . nestedChats [ 0 ] . messages . length > 0
22
- ? data . nestedChats [ 0 ] . messages [ 0 ] . id
23
- : null ;
24
- if ( ! edgeId ) {
25
- const nonSwarmTargets = agentConnections . target . nodes . filter ( node => node . data . agentType !== "swarm" ) ;
26
- const nonSwarmTargetEdges = agentConnections . target . edges . filter (
27
- edge => edge . type === "swarm" && nonSwarmTargets . some ( target => target . id === edge . target ) ,
28
- ) ;
29
- edgeId = nonSwarmTargetEdges [ 0 ] . id ;
30
- }
43
+ const edgeId = getEdgeId ( data , agentConnections ) ;
31
44
const onConditionHandoffsNested = data . handoffs . filter ( handoff => {
32
45
if ( "targetType" in handoff ) {
33
46
return handoff . targetType === "nested_chat" ;
@@ -40,11 +53,7 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
40
53
type : "none" ,
41
54
value : null ,
42
55
} ;
43
-
44
- const onCondition =
45
- data . nestedChats . length > 0 && data . nestedChats [ 0 ] . triggeredBy . length > 0
46
- ? data . nestedChats [ 0 ] . triggeredBy [ 0 ]
47
- : "" ;
56
+ const onCondition = getCondition ( data ) ;
48
57
const onIsConditionAvailableChange = ( onAvailableData : WaldiezSwarmOnConditionAvailable ) => {
49
58
const handoffs = structuredClone ( data . handoffs ) ;
50
59
const onConditionHandoffIndex = handoffs . findIndex ( handoff => {
@@ -60,7 +69,7 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
60
69
} ,
61
70
targetType : "nested_chat" ,
62
71
available : onAvailableData ,
63
- condition : onCondition ,
72
+ condition : "" ,
64
73
} ) ;
65
74
} else {
66
75
const currentOnCondition = handoffs [ onConditionHandoffIndex ] as WaldiezSwarmOnCondition ;
@@ -93,3 +102,41 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
93
102
</ div >
94
103
) ;
95
104
} ;
105
+
106
+ const getEdgeId = (
107
+ data : WaldiezNodeAgentSwarmData ,
108
+ agentConnections : {
109
+ source : {
110
+ nodes : WaldiezNodeAgent [ ] ;
111
+ edges : WaldiezEdge [ ] ;
112
+ } ;
113
+ target : {
114
+ nodes : WaldiezNodeAgent [ ] ;
115
+ edges : WaldiezEdge [ ] ;
116
+ } ;
117
+ } ,
118
+ ) => {
119
+ let edgeId =
120
+ data . nestedChats . length > 0 && data . nestedChats [ 0 ] . messages . length > 0
121
+ ? data . nestedChats [ 0 ] . messages [ 0 ] . id
122
+ : null ;
123
+ if ( ! edgeId ) {
124
+ const nonSwarmTargets = agentConnections . target . nodes . filter ( node => node . data . agentType !== "swarm" ) ;
125
+ const nonSwarmTargetEdges = agentConnections . target . edges . filter (
126
+ edge => edge . type === "swarm" && nonSwarmTargets . some ( target => target . id === edge . target ) ,
127
+ ) ;
128
+ edgeId = nonSwarmTargetEdges [ 0 ] . id ;
129
+ }
130
+ return edgeId ;
131
+ } ;
132
+
133
+ const getCondition = ( data : WaldiezNodeAgentSwarmData ) => {
134
+ const onConditionHandoffsNested = data . handoffs . filter ( handoff => {
135
+ if ( "targetType" in handoff ) {
136
+ return handoff . targetType === "nested_chat" ;
137
+ }
138
+ } ) ;
139
+ return onConditionHandoffsNested . length > 0
140
+ ? ( onConditionHandoffsNested [ 0 ] as WaldiezSwarmOnCondition ) . condition
141
+ : "" ;
142
+ } ;
0 commit comments