Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(frontend/builder): fix XML sanitization, add undo for connect_nodes, add hook tests

Zamil Majdy287947018550
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
index b2b794a0ad..5ba08e617e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts
@@ -12,6 +12,7 @@ const mockNodes: unknown[] = [];
const mockEdges: unknown[] = [];
const mockUpdateNodeData = vi.fn();
const mockAddEdge = vi.fn();
+const mockRemoveEdge = vi.fn();
vi.mock("../../../stores/nodeStore", () => ({
useNodeStore: (selector: (s: unknown) => unknown) =>
@@ -26,6 +27,7 @@ vi.mock("../../../stores/edgeStore", () => ({
selector({
edges: mockEdges,
addEdge: mockAddEdge,
+ removeEdge: mockRemoveEdge,
}),
}));
@@ -91,6 +93,7 @@ beforeEach(() => {
mockEdges.length = 0;
mockUpdateNodeData.mockClear();
mockAddEdge.mockClear();
+ mockRemoveEdge.mockClear();
mockPostV2CreateSession.mockClear();
mockInvalidateQueries.mockClear();
mockSendMessage.mockClear();
@@ -662,4 +665,31 @@ describe("useBuilderChatPanel – undo", () => {
});
expect(result.current.appliedActionKeys.size).toBe(0);
});
+
+ it("connect_nodes: removes edge via removeEdge after undo", () => {
+ mockNodes.push({ id: "src", data: {} }, { id: "tgt", data: {} });
+
+ const { result } = renderHook(() => useBuilderChatPanel());
+
+ act(() => {
+ result.current.handleApplyAction({
+ type: "connect_nodes",
+ source: "src",
+ target: "tgt",
+ sourceHandle: "out",
+ targetHandle: "in",
+ });
+ });
+
+ expect(mockAddEdge).toHaveBeenCalledOnce();
+ expect(result.current.undoStack).toHaveLength(1);
+
+ act(() => {
+ result.current.handleUndoLastAction();
+ });
+
+ expect(mockRemoveEdge).toHaveBeenCalledWith("src:out->tgt:in");
+ expect(result.current.undoStack).toHaveLength(0);
+ expect(result.current.appliedActionKeys.size).toBe(0);
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/helpers.ts
index a9237b5a66..1763b7c64d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/helpers.ts
@@ -8,7 +8,12 @@ const MAX_EDGES = 200;
/** Escapes XML special characters in user-controlled strings before embedding in prompts. */
function sanitizeForXml(s: string): string {
- return s.replace(/</g, "&lt;").replace(/>/g, "&gt;");
+ return s
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/"/g, "&quot;")
+ .replace(/'/g, "&apos;");
}
/**
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
index 96753af80b..a7283a5b1d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
@@ -79,6 +79,7 @@ export function useBuilderChatPanel({
const edges = useEdgeStore(useShallow((s) => s.edges));
const updateNodeData = useNodeStore(useShallow((s) => s.updateNodeData));
const addEdge = useEdgeStore(useShallow((s) => s.addEdge));
+ const removeEdge = useEdgeStore(useShallow((s) => s.removeEdge));
// Reset session and seed-sent guard when the user navigates to a different
// graph so the new graph's context is sent to the AI on next open.
@@ -330,8 +331,24 @@ export function useBuilderChatPanel({
});
return;
}
+ const edgeId = `${action.source}:${action.sourceHandle}->${action.target}:${action.targetHandle}`;
+ const key = getActionKey(action);
+ setUndoStack((prev) => [
+ ...prev,
+ {
+ actionKey: key,
+ restore: () => {
+ removeEdge(edgeId);
+ setAppliedActionKeys((keys) => {
+ const next = new Set(keys);
+ next.delete(key);
+ return next;
+ });
+ },
+ },
+ ]);
addEdge({
- id: `${action.source}:${action.sourceHandle}->${action.target}:${action.targetHandle}`,
+ id: edgeId,
source: action.source,
target: action.target,
sourceHandle: action.sourceHandle,