fix: preserve conversation ID in URL for chat history persistence
This commit is contained in:
parent
1594f98cc1
commit
3bad010833
1 changed files with 11 additions and 1 deletions
|
|
@ -4,7 +4,7 @@ import type { AIProvider } from '@tpmjs/types/agent';
|
|||
import { Button } from '@tpmjs/ui/Button/Button';
|
||||
import { Icon } from '@tpmjs/ui/Icon/Icon';
|
||||
import Link from 'next/link';
|
||||
import { useParams, useSearchParams } from 'next/navigation';
|
||||
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Virtuoso, type VirtuosoHandle } from 'react-virtuoso';
|
||||
import { AppHeader } from '~/components/AppHeader';
|
||||
|
|
@ -165,6 +165,7 @@ function generateConversationId(): string {
|
|||
|
||||
export default function PublicAgentChatPage(): React.ReactElement {
|
||||
const params = useParams();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const agentId = params.id as string;
|
||||
|
||||
|
|
@ -173,6 +174,9 @@ export default function PublicAgentChatPage(): React.ReactElement {
|
|||
return searchParams.get('c') || generateConversationId();
|
||||
}, [searchParams]);
|
||||
|
||||
// Track if we've updated the URL with conversation ID
|
||||
const hasUpdatedUrl = useRef(false);
|
||||
|
||||
const [agent, setAgent] = useState<Agent | null>(null);
|
||||
const [messages, setMessages] = useState<Message[]>([]);
|
||||
const [input, setInput] = useState('');
|
||||
|
|
@ -328,6 +332,12 @@ export default function PublicAgentChatPage(): React.ReactElement {
|
|||
throw new Error(errorData.error || 'Failed to send message');
|
||||
}
|
||||
|
||||
// Update URL with conversation ID so refresh preserves chat history
|
||||
if (!hasUpdatedUrl.current && !searchParams.get('c')) {
|
||||
hasUpdatedUrl.current = true;
|
||||
router.replace(`/agents/${agentId}/chat?c=${conversationId}`, { scroll: false });
|
||||
}
|
||||
|
||||
// Handle SSE stream
|
||||
const reader = response.body?.getReader();
|
||||
if (!reader) throw new Error('No response body');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue