Skip to content

Commit 5674540

Browse files
committed
update swarm handoffs
1 parent cecb8cd commit 5674540

File tree

2 files changed

+76
-32
lines changed

2 files changed

+76
-32
lines changed

src/waldiez/containers/nodes/agent/modal/tabs/swarm/nestedChatCondition.tsx

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
import { OnConditionAvailable, TextInput } from "@waldiez/components";
22
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";
410

511
export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNestedChatConditionProps) => {
612
const { flowId, data, darkMode, agentConnections, onDataChange } = props;
713
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,
1326
},
14-
];
15-
onDataChange({ nestedChats });
27+
targetType: "nested_chat",
28+
available: {
29+
type: "none",
30+
value: null,
31+
},
32+
condition: event.target.value,
33+
});
1634
} 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+
};
1840
}
41+
onDataChange({ handoffs }); // Retrieve the status of the order.
1942
};
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);
3144
const onConditionHandoffsNested = data.handoffs.filter(handoff => {
3245
if ("targetType" in handoff) {
3346
return handoff.targetType === "nested_chat";
@@ -40,11 +53,7 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
4053
type: "none",
4154
value: null,
4255
};
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);
4857
const onIsConditionAvailableChange = (onAvailableData: WaldiezSwarmOnConditionAvailable) => {
4958
const handoffs = structuredClone(data.handoffs);
5059
const onConditionHandoffIndex = handoffs.findIndex(handoff => {
@@ -60,7 +69,7 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
6069
},
6170
targetType: "nested_chat",
6271
available: onAvailableData,
63-
condition: onCondition,
72+
condition: "",
6473
});
6574
} else {
6675
const currentOnCondition = handoffs[onConditionHandoffIndex] as WaldiezSwarmOnCondition;
@@ -93,3 +102,41 @@ export const WaldiezAgentSwarmNestedChatCondition = (props: WaldiezAgentSwarmNes
93102
</div>
94103
);
95104
};
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+
};

src/waldiez/store/utils/handoffs.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ export const getNestedChatOnConditionHandoff: (
127127
return {
128128
targetType: "nested_chat",
129129
target: { id: edgeId, order: nonSwarmTargets.length },
130-
condition:
131-
nestedChats[0].triggeredBy.length > 0
132-
? nestedChats[0].triggeredBy[0]
133-
: `transfer_to_${nonSwarmTargets[0].data.label}`,
130+
condition: `Transfer to ${nonSwarmTargets[0].data.label}`,
134131
available: {
135132
type: "none",
136133
value: null,
@@ -144,7 +141,7 @@ export const getNestedChatOnConditionHandoff: (
144141
return {
145142
targetType: "nested_chat",
146143
target: { id: edgeId, order: nonSwarmTargets.length },
147-
condition: `transfer_to_${nonSwarmTargets[0].data.label}`,
144+
condition: `Transfer to ${nonSwarmTargets[0].data.label}`,
148145
available: {
149146
type: "none",
150147
value: null,
@@ -209,7 +206,7 @@ const getSwarmAgentHandoff: (
209206
};
210207

211208
const getOnConditionFromEdge = (edge: WaldiezEdge, targetNode: WaldiezNodeAgent) => {
212-
let condition = `transfer_to_${targetNode.data.label}`;
209+
let condition = `Transfer to ${targetNode.data.label}`;
213210
if (
214211
edge.data?.description &&
215212
edge.data.description !== "" &&

0 commit comments

Comments
 (0)