Skip to content

Commit e551784

Browse files
committed
refactor(frontend/builder): address review comments in BuilderChatPanel
- Remove useCallback from handleApplyAction (violates AGENTS.md) - Import TEXTAREA_MAX_LENGTH from PanelInput instead of duplicating constant - Remove dead @tanstack/react-query mock and associated invalidateQueries test
1 parent e2390bb commit e551784

File tree

2 files changed

+29
-64
lines changed

2 files changed

+29
-64
lines changed

autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/__tests__/useBuilderChatPanel.test.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ vi.mock("@/services/environment", () => ({
5252
environment: { getAGPTServerBaseUrl: () => "http://localhost:8000" },
5353
}));
5454

55-
const mockInvalidateQueries = vi.fn();
56-
vi.mock("@tanstack/react-query", () => ({
57-
useQueryClient: () => ({ invalidateQueries: mockInvalidateQueries }),
58-
}));
59-
6055
const mockToast = vi.fn();
6156
vi.mock("@/components/molecules/Toast/use-toast", () => ({
6257
useToast: () => ({ toast: mockToast }),
@@ -108,7 +103,6 @@ beforeEach(() => {
108103
mockSetNodes.mockClear();
109104
mockSetEdges.mockClear();
110105
mockPostV2CreateSession.mockClear();
111-
mockInvalidateQueries.mockClear();
112106
mockSendMessage.mockClear();
113107
mockSetMessages.mockClear();
114108
mockToast.mockClear();
@@ -428,29 +422,6 @@ describe("useBuilderChatPanel – flowID reset", () => {
428422
});
429423
});
430424

431-
describe("useBuilderChatPanel – apply does not trigger cache refetch", () => {
432-
it("does NOT call invalidateQueries after applying an update_node_input action (prevents refetch overwriting local state)", () => {
433-
mockNodes.push({
434-
id: "n1",
435-
data: { hardcodedValues: { existing: "val" } },
436-
});
437-
mockFlowID = "flow-cache";
438-
439-
const { result } = renderHook(() => useBuilderChatPanel());
440-
441-
act(() => {
442-
result.current.handleApplyAction({
443-
type: "update_node_input",
444-
nodeId: "n1",
445-
key: "query",
446-
value: "new val",
447-
});
448-
});
449-
450-
expect(mockInvalidateQueries).not.toHaveBeenCalled();
451-
});
452-
});
453-
454425
describe("useBuilderChatPanel – handleApplyAction", () => {
455426
it("update_node_input: calls setNodes with merged hardcodedValues (bypasses history)", () => {
456427
mockNodes.push({

autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { DefaultChatTransport } from "ai";
77
import {
88
type KeyboardEvent,
99
type RefObject,
10-
useCallback,
1110
useEffect,
1211
useMemo,
1312
useRef,
@@ -31,12 +30,10 @@ import {
3130
parseGraphActions,
3231
serializeGraphForChat,
3332
} from "./helpers";
33+
import { TEXTAREA_MAX_LENGTH } from "./components/PanelInput";
3434

3535
type SendMessageFn = ReturnType<typeof useChat>["sendMessage"];
3636

37-
/** Maximum characters accepted by `sendRawMessage`, mirroring the textarea `maxLength`. */
38-
const MAX_RAW_MESSAGE_LENGTH = 4000;
39-
4037
/**
4138
* Per-graph session cache with a simple LRU cap.
4239
* Maps flowID → sessionId so the same chat session is reused each time the
@@ -452,35 +449,32 @@ export function useBuilderChatPanel({
452449
}
453450
}
454451

455-
const handleApplyAction = useCallback(
456-
(action: GraphAction) => {
457-
const deps: ApplyActionDeps = {
458-
toast,
459-
setNodes,
460-
setEdges,
461-
setUndoStack,
462-
setAppliedActionKeys,
463-
};
464-
let applied = false;
465-
if (action.type === "update_node_input") {
466-
applied = applyUpdateNodeInput(action, deps);
467-
} else if (action.type === "connect_nodes") {
468-
applied = applyConnectNodes(action, deps);
469-
} else {
470-
// Exhaustiveness guard — TypeScript ensures all GraphAction types are handled above.
471-
const _: never = action;
472-
void _;
473-
}
474-
if (applied) {
475-
setAppliedActionKeys((prev) => {
476-
const next = new Set(prev);
477-
next.add(getActionKey(action));
478-
return next;
479-
});
480-
}
481-
},
482-
[toast, setNodes, setEdges],
483-
);
452+
function handleApplyAction(action: GraphAction) {
453+
const deps: ApplyActionDeps = {
454+
toast,
455+
setNodes,
456+
setEdges,
457+
setUndoStack,
458+
setAppliedActionKeys,
459+
};
460+
let applied = false;
461+
if (action.type === "update_node_input") {
462+
applied = applyUpdateNodeInput(action, deps);
463+
} else if (action.type === "connect_nodes") {
464+
applied = applyConnectNodes(action, deps);
465+
} else {
466+
// Exhaustiveness guard — TypeScript ensures all GraphAction types are handled above.
467+
const _: never = action;
468+
void _;
469+
}
470+
if (applied) {
471+
setAppliedActionKeys((prev) => {
472+
const next = new Set(prev);
473+
next.add(getActionKey(action));
474+
return next;
475+
});
476+
}
477+
}
484478

485479
function handleUndoLastAction() {
486480
// Read the current stack directly rather than inside the setUndoStack updater.
@@ -502,8 +496,8 @@ export function useBuilderChatPanel({
502496
function sendRawMessage(text: string) {
503497
if (!text || !canSend) return;
504498
const trimmed =
505-
text.length > MAX_RAW_MESSAGE_LENGTH
506-
? text.slice(0, MAX_RAW_MESSAGE_LENGTH)
499+
text.length > TEXTAREA_MAX_LENGTH
500+
? text.slice(0, TEXTAREA_MAX_LENGTH)
507501
: text;
508502
sendMessage({ text: trimmed });
509503
}

0 commit comments

Comments
 (0)