From eec648e5ca8decf4880f0c7567e18f79451a17e9 Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Wed, 7 Jan 2026 23:57:42 +1000 Subject: [PATCH] fix: preserve conversation ID in URL for chat history persistence --- apps/web/src/app/agents/[id]/chat/page.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/web/src/app/agents/[id]/chat/page.tsx b/apps/web/src/app/agents/[id]/chat/page.tsx index 12095ca..4748094 100644 --- a/apps/web/src/app/agents/[id]/chat/page.tsx +++ b/apps/web/src/app/agents/[id]/chat/page.tsx @@ -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(null); const [messages, setMessages] = useState([]); 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');