Spaces:
Configuration error
Configuration error
Download frontend/src/hooks/use-auto-title.ts from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 2.37 kB
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-auto-title.ts
- Command line
-
hf download hf://spaces/flyingdarkdev/README/frontend/src/hooks/use-auto-title.ts
-
curl -L -o use-auto-title.ts https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-auto-title.ts
2.37 kB
| import { useEffect } from "react"; | |
| import { useParams } from "react-router"; | |
| import { useSelector, useDispatch } from "react-redux"; | |
| import { useQueryClient } from "@tanstack/react-query"; | |
| import { useUpdateConversation } from "./mutation/use-update-conversation"; | |
| import { RootState } from "#/store"; | |
| import OpenHands from "#/api/open-hands"; | |
| import { useUserConversation } from "#/hooks/query/use-user-conversation"; | |
| const defaultTitlePattern = /^Conversation [a-f0-9]+$/; | |
| /** | |
| * Hook that monitors for the first agent message and triggers title generation. | |
| * This approach is more robust as it ensures the user message has been processed | |
| * by the backend and the agent has responded before generating the title. | |
| */ | |
| export function useAutoTitle() { | |
| const { conversationId } = useParams<{ conversationId: string }>(); | |
| const { data: conversation } = useUserConversation(conversationId ?? null); | |
| const queryClient = useQueryClient(); | |
| const dispatch = useDispatch(); | |
| const { mutate: updateConversation } = useUpdateConversation(); | |
| const messages = useSelector((state: RootState) => state.chat.messages); | |
| useEffect(() => { | |
| if ( | |
| !conversation || | |
| !conversationId || | |
| !messages || | |
| messages.length === 0 | |
| ) { | |
| return; | |
| } | |
| const hasAgentMessage = messages.some( | |
| (message) => message.sender === "assistant", | |
| ); | |
| const hasUserMessage = messages.some( | |
| (message) => message.sender === "user", | |
| ); | |
| if (!hasAgentMessage || !hasUserMessage) { | |
| return; | |
| } | |
| if (conversation.title && !defaultTitlePattern.test(conversation.title)) { | |
| return; | |
| } | |
| updateConversation( | |
| { | |
| id: conversationId, | |
| conversation: { title: "" }, | |
| }, | |
| { | |
| onSuccess: async () => { | |
| try { | |
| const updatedConversation = | |
| await OpenHands.getConversation(conversationId); | |
| queryClient.setQueryData( | |
| ["user", "conversation", conversationId], | |
| updatedConversation, | |
| ); | |
| } catch (error) { | |
| queryClient.invalidateQueries({ | |
| queryKey: ["user", "conversation", conversationId], | |
| }); | |
| } | |
| }, | |
| }, | |
| ); | |
| }, [ | |
| messages, | |
| conversationId, | |
| conversation, | |
| updateConversation, | |
| queryClient, | |
| dispatch, | |
| ]); | |
| } | |