Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ import { CopilotActionExecutor } from './copilot/action-bridge';

type ChatBotPageContentProps = {
variant?: 'sidebar' | 'compact';
mode?: ChatMode;
copilotEnvelope?: CopilotEnvelopeV1 | null;
mode?: ChatMode;
copilotEnvelope?: CopilotEnvelopeV1 | null;
onClose?: () => void;
};

export default function ChatBotPageContent({
variant = 'sidebar',
mode = 'global',
copilotEnvelope = null,
onClose,
}: ChatBotPageContentProps) {

export default function ChatBotPageContent({ variant = 'sidebar', mode = 'global', copilotEnvelope = null, onClose }: ChatBotPageContentProps) {
const [compactMode, setCompactMode] = useState<boolean>(variant === 'compact');
useEffect(() => setCompactMode(variant === 'compact'), [variant]);
const t = useTranslations('Chatbot');
Expand Down Expand Up @@ -87,7 +81,7 @@ export default function ChatBotPageContent({
await chat.handleCreateSession();
};

const onExecuteAction: CopilotActionExecutor = async (action) => {
const onExecuteAction: CopilotActionExecutor = async action => {
if (action.type === 'sql.replace') {
console.log('replace sql', action.sql);
}
Expand All @@ -104,13 +98,7 @@ export default function ChatBotPageContent({
{compactMode ? (
<div className="absolute right-4 top-4 z-10 flex items-center gap-2">
{hasSessions && sessionSelector}
<Button
variant="outline"
size="icon"
title={t('Close')}
onClick={() => onClose?.()}
className="h-8 w-8"
>
<Button variant="outline" size="icon" title={t('Close')} onClick={() => onClose?.()} className="h-8 w-8">
<X className="h-4 w-4" />
</Button>
</div>
Expand All @@ -132,7 +120,7 @@ export default function ChatBotPageContent({
initialPrompt={pendingPromptRef.current}
onConversationActivity={chat.handleConversationActivity}
onExecuteAction={onExecuteAction}
onSessionCreated={(sessionId) => chat.setSelectedSessionId(sessionId)}
onSessionCreated={sessionId => chat.setSelectedSessionId(sessionId)}
mode={mode}
copilotEnvelope={copilotEnvelope}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@ import posthog from 'posthog-js';

import type { ChatSessionItem, ChatMode } from './types';
import { normalizeSessionsForDisplay } from './utils';
import {
apiCreateSession,
apiDeleteSession,
apiFetchSessionDetail,
apiFetchSessions,
apiFetchCopilotSession,
apiRenameSession,
} from './api';
import { apiCreateSession, apiDeleteSession, apiFetchSessionDetail, apiFetchSessions, apiFetchCopilotSession, apiRenameSession } from './api';
import type { CopilotEnvelopeV1 } from '../copilot/types/copilot-envelope';

export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: CopilotEnvelopeV1 | null }) {
const { mode, copilotEnvelope } = params;
export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: CopilotEnvelopeV1 | null; enabled?: boolean }) {
const { mode, copilotEnvelope, enabled = true } = params;
const copilotTabId = copilotEnvelope?.meta?.tabId ?? null;
const t = useTranslations('Chatbot');
const routeParams = useParams<{ connectionId: string }>();
Expand Down Expand Up @@ -80,32 +73,33 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi
[mode, connectionId, t],
);

const fetchSessionDetail = useCallback(async (sessionId: string) => {
setLoadingMessages(true);
try {
const { detail, messages } = await apiFetchSessionDetail(sessionId, { errorMessage: t('Errors.FetchSessionDetail') });
const fetchSessionDetail = useCallback(
async (sessionId: string) => {
setLoadingMessages(true);
try {
const { detail, messages } = await apiFetchSessionDetail(sessionId, { errorMessage: t('Errors.FetchSessionDetail') });

console.log('[chat] fetch session detail', detail, messages);
console.log('[chat] fetch session detail', detail, messages);

if (detail?.session) {
setSessions(prev => {

const exists = prev.some(item => item.id === detail.session.id);
if (!exists) return [detail.session, ...prev];
return prev.map(item => (item.id === detail.session.id ? detail.session : item));
});
if (detail?.session) {
setSessions(prev => {
const exists = prev.some(item => item.id === detail.session.id);
if (!exists) return [detail.session, ...prev];
return prev.map(item => (item.id === detail.session.id ? detail.session : item));
});
}
setInitialMessages(messages);
} catch (e: any) {
console.error('[chat] fetch session detail failed', e);
toast.error(e?.message || t('Errors.FetchSessionDetail'));
setInitialMessages([]);
} finally {
setLoadingMessages(false);
}
setInitialMessages(messages);
} catch (e: any) {
console.error('[chat] fetch session detail failed', e);
toast.error(e?.message || t('Errors.FetchSessionDetail'));
setInitialMessages([]);
} finally {
setLoadingMessages(false);
}
}, [t]);
},
[t],
);


const ensureCopilotSession = useCallback(async () => {
if (mode !== 'copilot') return;
if (!copilotTabId) {
Expand Down Expand Up @@ -154,23 +148,33 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi
}
}, [mode, copilotTabId, fetchSessionDetail, t]);


useEffect(() => {
if (!enabled) {
setLoadingSessions(false);
setLoadingMessages(false);
setSessions([]);
setSelectedSessionId(null);
setInitialMessages([]);
return;
}

if (mode === 'copilot') {
ensureCopilotSession().catch(() => undefined);
} else {
fetchSessions().catch(() => undefined);
}
}, [mode, fetchSessions, ensureCopilotSession]);
}, [enabled, mode, fetchSessions, ensureCopilotSession]);


useEffect(() => {
if (!selectedSessionId) {
setInitialMessages([]);
return;
}
if (!enabled) {
return;
}
fetchSessionDetail(selectedSessionId).catch(() => undefined);
}, [selectedSessionId, fetchSessionDetail]);
}, [enabled, selectedSessionId, fetchSessionDetail]);

const handleCreateSession = useCallback(async () => {
if (mode === 'copilot') {
Expand All @@ -196,7 +200,7 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi

const handleSessionSelect = useCallback(
(sessionId: string) => {
if (mode === 'copilot') return;
if (mode === 'copilot') return;
setSelectedSessionId(sessionId);
},
[mode],
Expand All @@ -208,7 +212,6 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi
fetchSessions(currentId).catch(() => undefined);
}, [mode, fetchSessions]);


const handleRenameRequest = useCallback(
(sessionId: string) => {
if (mode === 'copilot') return toast.error(t('Errors.CopilotRenameUnsupported'));
Expand Down Expand Up @@ -259,7 +262,6 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi
}
}, [mode, editingSessionId, editingSessionValue, sessions, fetchSessions, t]);


const handleDeleteRequest = useCallback(
(sessionId: string) => {
if (mode === 'copilot') return toast.error(t('Errors.CopilotDeleteUnsupported'));
Expand Down Expand Up @@ -363,8 +365,6 @@ export function useChatSessions(params: { mode: ChatMode; copilotEnvelope?: Copi
handleDeleteSubmit,
handleNewChat,
handleRefreshSessions,


setSelectedSessionId,
};
}
Loading
Loading