-
Notifications
You must be signed in to change notification settings - Fork 363
feat(ask): start chat from selected code range #1553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
865aac8
eedef3a
35956de
2b7bc80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { afterEach, expect, test, vi } from "vitest"; | ||
| import type { Source } from "./types"; | ||
| import { DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY } from "./constants"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| createChat: vi.fn(), | ||
| createUIMessage: vi.fn(), | ||
| push: vi.fn(), | ||
| setChatState: vi.fn(), | ||
| toast: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("next/navigation", () => ({ | ||
| useRouter: () => ({ push: mocks.push }), | ||
| })); | ||
|
|
||
| vi.mock("@/components/hooks/use-toast", () => ({ | ||
| useToast: () => ({ toast: mocks.toast }), | ||
| })); | ||
|
|
||
| vi.mock("./actions", () => ({ | ||
| createChat: mocks.createChat, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/utils", () => ({ | ||
| isServiceError: () => false, | ||
| createPathWithQueryParams: (path: string) => path, | ||
| })); | ||
|
|
||
| vi.mock("./utils", () => ({ | ||
| createUIMessage: mocks.createUIMessage, | ||
| getAllMentionElements: () => [], | ||
| slateContentToString: () => "", | ||
| })); | ||
|
|
||
| vi.mock("usehooks-ts", () => ({ | ||
| useSessionStorage: () => [null, mocks.setChatState], | ||
| })); | ||
|
|
||
| const { useCreateNewChatThread } = await import("./useCreateNewChatThread"); | ||
|
|
||
| afterEach(() => { | ||
| window.localStorage.clear(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| test("createChatFromSource preserves disabled MCP servers from local storage", async () => { | ||
| const disabledMcpServerIds = ["linear", "github"]; | ||
| window.localStorage.setItem( | ||
| DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY, | ||
| JSON.stringify(disabledMcpServerIds), | ||
| ); | ||
| mocks.createChat.mockResolvedValue({ id: "chat-1" }); | ||
| mocks.createUIMessage.mockReturnValue({ id: "initial-message" }); | ||
| const { result } = renderHook(() => useCreateNewChatThread()); | ||
| const source: Source = { | ||
| type: "file", | ||
| repo: "github.com/sourcebot-dev/sourcebot", | ||
| path: "packages/web/src/auth.ts", | ||
| name: "auth.ts", | ||
| revision: "main", | ||
| }; | ||
|
|
||
| await act(async () => { | ||
| await result.current.createChatFromSource(source); | ||
| }); | ||
|
|
||
| expect(mocks.createUIMessage).toHaveBeenCalledWith( | ||
| "Explain this selected code.", | ||
| [], | ||
| [], | ||
| disabledMcpServerIds, | ||
| [], | ||
| [source], | ||
| ); | ||
| expect(mocks.setChatState).toHaveBeenCalledWith({ | ||
| inputMessage: { id: "initial-message" }, | ||
| selectedSearchScopes: [], | ||
| disabledMcpServerIds, | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,10 +9,21 @@ import { useRouter } from "next/navigation"; | |
| import { createChat } from "./actions"; | ||
| import { isServiceError } from "@/lib/utils"; | ||
| import { createPathWithQueryParams } from "@/lib/utils"; | ||
| import { AttachmentData, SearchScope, SetChatStatePayload } from "./types"; | ||
| import { AttachmentData, SearchScope, SetChatStatePayload, Source } from "./types"; | ||
| import { DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY, SELECTED_SEARCH_SCOPES_LOCAL_STORAGE_KEY, SET_CHAT_STATE_SESSION_STORAGE_KEY } from "./constants"; | ||
| import { useSessionStorage } from "usehooks-ts"; | ||
|
|
||
| const getStoredDisabledMcpServerIds = (): string[] => { | ||
| try { | ||
| const stored = window.localStorage.getItem(DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY); | ||
| if (stored) { | ||
| return JSON.parse(stored) as string[]; | ||
| } | ||
| } catch { /* fall through to [] */ } | ||
|
|
||
| return []; | ||
| } | ||
|
|
||
| export const useCreateNewChatThread = () => { | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const { toast } = useToast(); | ||
|
|
@@ -31,16 +42,8 @@ export const useCreateNewChatThread = () => { | |
| } | ||
| } catch { /* fall through to [] */ } | ||
|
|
||
| let storedDisabledMcpServerIds: string[] = []; | ||
| try { | ||
| const stored = window.localStorage.getItem(DISABLED_MCP_SERVER_IDS_LOCAL_STORAGE_KEY); | ||
| if (stored) { | ||
| storedDisabledMcpServerIds = JSON.parse(stored) as string[]; | ||
| } | ||
| } catch { /* fall through to [] */ } | ||
|
|
||
| const selectedSearchScopes = overrideSearchScopes ?? storedScopes; | ||
| const disabledMcpServerIds = overrideDisabledMcpServerIds ?? storedDisabledMcpServerIds; | ||
| const disabledMcpServerIds = overrideDisabledMcpServerIds ?? getStoredDisabledMcpServerIds(); | ||
| const inputMessage = createUIMessage(text, mentions.map((mention) => mention.data), selectedSearchScopes, disabledMcpServerIds, attachments); | ||
|
|
||
| setIsLoading(true); | ||
|
|
@@ -64,8 +67,39 @@ export const useCreateNewChatThread = () => { | |
| router.push(url); | ||
| }, [router, toast, setChatState]); | ||
|
|
||
| const createChatFromSource = useCallback(async (source: Source) => { | ||
| const disabledMcpServerIds = getStoredDisabledMcpServerIds(); | ||
| const inputMessage = createUIMessage( | ||
| 'Explain this selected code.', | ||
| [], | ||
| [], | ||
| disabledMcpServerIds, | ||
| [], | ||
| [source], | ||
| ); | ||
|
|
||
| setIsLoading(true); | ||
| const response = await createChat({ source: 'sourcebot-web-client' }); | ||
| if (isServiceError(response)) { | ||
| toast({ | ||
| description: `❌ Failed to create chat. Reason: ${response.message}`, | ||
| }); | ||
| setIsLoading(false); | ||
| return; | ||
| } | ||
|
|
||
| setChatState({ | ||
| inputMessage, | ||
| selectedSearchScopes: [], | ||
| disabledMcpServerIds, | ||
| }); | ||
|
|
||
| router.push(`/chat/${response.id}`); | ||
| }, [router, setChatState, toast]); | ||
|
cursor[bot] marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In-flight lock never releasedMedium Severity
Reviewed by Cursor Bugbot for commit 2b7bc80. Configure here. |
||
|
|
||
| return { | ||
| createNewChatThread, | ||
| createChatFromSource, | ||
| isLoading, | ||
| }; | ||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.