From 16ee8276f9a0c41232aa04ae6aef1a601f270710 Mon Sep 17 00:00:00 2001 From: Brian Hackett Date: Tue, 1 Apr 2025 17:20:40 -0700 Subject: [PATCH] Add route for starting a chat from a repository (#90) --- app/lib/persistence/useChatHistory.ts | 27 ++++++++++++++++++++++----- app/routes/repository.$id.tsx | 8 ++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 app/routes/repository.$id.tsx diff --git a/app/lib/persistence/useChatHistory.ts b/app/lib/persistence/useChatHistory.ts index 286cdd2f..d7d42fe0 100644 --- a/app/lib/persistence/useChatHistory.ts +++ b/app/lib/persistence/useChatHistory.ts @@ -13,10 +13,14 @@ export const currentChatId = atom(undefined); export const currentChatTitle = atom(undefined); export function useChatHistory() { - const { id: mixedId, problemId } = useLoaderData<{ id?: string; problemId?: string }>() ?? {}; + const { + id: mixedId, + problemId, + repositoryId, + } = useLoaderData<{ id?: string; problemId?: string; repositoryId?: string }>() ?? {}; const [initialMessages, setInitialMessages] = useState([]); - const [ready, setReady] = useState(!mixedId && !problemId); + const [ready, setReady] = useState(!mixedId && !problemId && !repositoryId); const importChat = async (title: string, messages: Message[]) => { try { @@ -32,6 +36,16 @@ export function useChatHistory() { } }; + const loadRepository = async (repositoryId: string) => { + const messages = createMessagesForRepository(`Repository: ${repositoryId}`, repositoryId); + await importChat(`Repository: ${repositoryId}`, messages); + toast.success('Repository loaded successfully'); + }; + + const debouncedSetChatContents = debounce(async (messages: Message[]) => { + await setChatContents(currentChatId.get() as string, currentChatTitle.get() as string, messages); + }, 1000); + useEffect(() => { (async () => { try { @@ -51,6 +65,9 @@ export function useChatHistory() { } else if (problemId) { await loadProblem(problemId, importChat); setReady(true); + } else if (repositoryId) { + await loadRepository(repositoryId); + setReady(true); } } catch (error) { logStore.logError('Failed to load chat messages', error); @@ -62,7 +79,7 @@ export function useChatHistory() { return { ready, initialMessages, - storeMessageHistory: debounce(async (messages: Message[]) => { + storeMessageHistory: async (messages: Message[]) => { if (messages.length === 0) { return; } @@ -74,8 +91,8 @@ export function useChatHistory() { navigateChat(id); } - await setChatContents(currentChatId.get() as string, currentChatTitle.get() as string, messages); - }, 1000), + debouncedSetChatContents(messages); + }, importChat, }; } diff --git a/app/routes/repository.$id.tsx b/app/routes/repository.$id.tsx new file mode 100644 index 00000000..bf0e44b8 --- /dev/null +++ b/app/routes/repository.$id.tsx @@ -0,0 +1,8 @@ +import { json, type LoaderFunctionArgs } from '~/lib/remix-types'; +import { default as IndexRoute } from './_index'; + +export async function loader(args: LoaderFunctionArgs) { + return json({ repositoryId: args.params.id }); +} + +export default IndexRoute;