Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/features/threads/hooks/useThreadEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ export function useThreadEventHandlers({
dispatch,
approvalAllowlistRef,
});
const onRequestUserInput = useThreadUserInputEvents({ dispatch });
const onRequestUserInput = useThreadUserInputEvents({
dispatch,
markProcessing,
markReviewing,
setActiveTurnId,
});

const {
onAgentMessageDelta,
Expand Down
77 changes: 77 additions & 0 deletions src/features/threads/hooks/useThreadUserInputEvents.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// @vitest-environment jsdom
import { act, renderHook } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import type { RequestUserInputRequest } from "../../../types";
import { useThreadUserInputEvents } from "./useThreadUserInputEvents";

describe("useThreadUserInputEvents", () => {
it("queues request and clears processing/reviewing state for the thread", () => {
const dispatch = vi.fn();
const markProcessing = vi.fn();
const markReviewing = vi.fn();
const setActiveTurnId = vi.fn();
const request: RequestUserInputRequest = {
workspace_id: "workspace-1",
request_id: "req-1",
params: {
thread_id: "thread-1",
turn_id: "turn-1",
item_id: "item-1",
questions: [],
},
};

const { result } = renderHook(() =>
useThreadUserInputEvents({
dispatch,
markProcessing,
markReviewing,
setActiveTurnId,
}),
);

act(() => {
result.current(request);
});

expect(dispatch).toHaveBeenCalledWith({ type: "addUserInputRequest", request });
expect(markProcessing).toHaveBeenCalledWith("thread-1", false);
expect(markReviewing).toHaveBeenCalledWith("thread-1", false);
expect(setActiveTurnId).toHaveBeenCalledWith("thread-1", null);
});

it("only queues request when thread id is missing", () => {
const dispatch = vi.fn();
const markProcessing = vi.fn();
const markReviewing = vi.fn();
const setActiveTurnId = vi.fn();
const request: RequestUserInputRequest = {
workspace_id: "workspace-1",
request_id: "req-1",
params: {
thread_id: " ",
turn_id: "turn-1",
item_id: "item-1",
questions: [],
},
};

const { result } = renderHook(() =>
useThreadUserInputEvents({
dispatch,
markProcessing,
markReviewing,
setActiveTurnId,
}),
);

act(() => {
result.current(request);
});

expect(dispatch).toHaveBeenCalledWith({ type: "addUserInputRequest", request });
expect(markProcessing).not.toHaveBeenCalled();
expect(markReviewing).not.toHaveBeenCalled();
expect(setActiveTurnId).not.toHaveBeenCalled();
});
});
19 changes: 17 additions & 2 deletions src/features/threads/hooks/useThreadUserInputEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,28 @@ import type { ThreadAction } from "./useThreadsReducer";

type UseThreadUserInputEventsOptions = {
dispatch: Dispatch<ThreadAction>;
markProcessing: (threadId: string, isProcessing: boolean) => void;
markReviewing: (threadId: string, isReviewing: boolean) => void;
setActiveTurnId: (threadId: string, turnId: string | null) => void;
};

export function useThreadUserInputEvents({ dispatch }: UseThreadUserInputEventsOptions) {
export function useThreadUserInputEvents({
dispatch,
markProcessing,
markReviewing,
setActiveTurnId,
}: UseThreadUserInputEventsOptions) {
return useCallback(
(request: RequestUserInputRequest) => {
dispatch({ type: "addUserInputRequest", request });
const threadId = request.params.thread_id.trim();
if (!threadId) {
return;
}
markProcessing(threadId, false);
markReviewing(threadId, false);
setActiveTurnId(threadId, null);
},
[dispatch],
[dispatch, markProcessing, markReviewing, setActiveTurnId],
);
}