fix: isolate delivered agent chats by project
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
||||
import { ArrowUp, ChevronDown, ChevronRight, Loader2 } from 'lucide-react';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
import { teacherApi, teacherTopicsPath, legacyTopicBase, type TeacherCatalogState } from '@/lib/coding-teacher';
|
||||
import { teacherApi, teacherTopicsPath, teacherConversationsPath, legacyTopicBase, type TeacherCatalogState } from '@/lib/coding-teacher';
|
||||
import type {
|
||||
TeacherDefinition,
|
||||
TeacherCatalog,
|
||||
@@ -21,7 +21,12 @@ const conversationViews = new Map<string, { topic: TeacherTopic | null; before:
|
||||
const messageOrderKey = (request: TeacherTopic['requests'][number]) => request.origin
|
||||
? [request.origin.projectId, request.origin.topicId, request.origin.requestId].join(':') : request.id;
|
||||
|
||||
export function TeacherChatPanel({
|
||||
export function TeacherChatPanel(props: Parameters<typeof ProjectTeacherChatPanel>[0]) {
|
||||
const account = useAuthStore(state => state.user?.userId ?? state.user?.username ?? 'signed-out');
|
||||
return <ProjectTeacherChatPanel key={`${account}:${props.projectId}:${props.selectedAgent?.teacher_id ?? 'legacy'}:${props.draftRevision ?? ''}`} {...props} />;
|
||||
}
|
||||
|
||||
function ProjectTeacherChatPanel({
|
||||
projectId,
|
||||
projectName,
|
||||
sourceId,
|
||||
@@ -51,15 +56,21 @@ export function TeacherChatPanel({
|
||||
reloadCatalog?(): Promise<void>;
|
||||
}) {
|
||||
const continuous = Boolean(selectedAgent && !draftRevision);
|
||||
const base = continuous ? '/api/coding/agent-conversations' : teacherTopicsPath(projectId);
|
||||
const base = continuous ? teacherConversationsPath(projectId) : teacherTopicsPath(projectId);
|
||||
const account = useAuthStore((state) => state.user?.userId ?? state.user?.username ?? 'signed-out');
|
||||
const projectDraftKey = `makelore-consultation-draft:${account}:${projectId}:teacher`;
|
||||
const draftKey = !draftRevision ? selectedAgent ? `makelore-consultation-draft:${account}:agent:${selectedAgent.teacher_id}` : projectDraftKey : null;
|
||||
const draftKey = !draftRevision ? selectedAgent ? `makelore-consultation-draft:${account}:project:${projectId}:agent:${selectedAgent.teacher_id}` : projectDraftKey : null;
|
||||
const [cachedView] = useState(() => continuous && draftKey ? conversationViews.get(draftKey) : undefined);
|
||||
const [saved] = useState<{ text?: string; references?: TeacherReference[]; pending?: TeacherSend; projectId?: string; sourceId?: string }>(() => {
|
||||
try {
|
||||
const legacyKey = selectedAgent ? `makelore-consultation-draft:${account}:${projectId}:agent:${selectedAgent.teacher_id}` : projectDraftKey;
|
||||
return draftKey ? JSON.parse(localStorage.getItem(draftKey) ?? localStorage.getItem(legacyKey) ?? '{}') : {};
|
||||
if (!draftKey) return {};
|
||||
const current = localStorage.getItem(draftKey);
|
||||
if (current) return JSON.parse(current);
|
||||
const legacyKey = selectedAgent ? `makelore-consultation-draft:${account}:agent:${selectedAgent.teacher_id}` : projectDraftKey;
|
||||
const old = JSON.parse(localStorage.getItem(legacyKey) ?? '{}');
|
||||
if ((old.projectId ?? old.pending?.projectId) === projectId) return old;
|
||||
// Pre-single-chat versions already had project drafts; retain them as a fallback.
|
||||
return selectedAgent ? JSON.parse(localStorage.getItem(`makelore-consultation-draft:${account}:${projectId}:agent:${selectedAgent.teacher_id}`) ?? '{}') : {};
|
||||
} catch { return {}; }
|
||||
});
|
||||
const [referenceScope, setReferenceScope] = useState({ projectId: saved.projectId ?? projectId, sourceId: saved.sourceId ?? sourceId });
|
||||
@@ -98,7 +109,7 @@ export function TeacherChatPanel({
|
||||
const [topics, setTopics] = useState<TeacherTopicList>({ items: [], lastSelectedTopicId: null });
|
||||
const [storedTopic, setTopic] = useState<TeacherTopic | null>(cachedView?.topic ?? null);
|
||||
const topic = useMemo(() => storedTopic && continuous ? { ...storedTopic,
|
||||
discussion: storedTopic.projectId === projectId ? storedTopic.discussion : storedTopic.conversation?.discussions[projectId] }
|
||||
discussion: storedTopic.conversation?.discussions[projectId] ?? storedTopic.discussion }
|
||||
: storedTopic, [storedTopic, continuous, projectId]);
|
||||
const legacyFriend = topic?.role === 'friend';
|
||||
const legacyView = Boolean(continuous && topic && !topic.conversation);
|
||||
@@ -165,6 +176,7 @@ export function TeacherChatPanel({
|
||||
}, [topic?.id, topic?.revision, topic?.requests.length]);
|
||||
const consume = useCallback(
|
||||
(next: TeacherTopic) => {
|
||||
if (next.projectId !== projectId) return;
|
||||
if (pending.current && next.requests.some((request) => request.id === pending.current?.requestId)) {
|
||||
pending.current = null;
|
||||
persistDraft(draftRef.current.text, draftRef.current.references);
|
||||
@@ -178,10 +190,10 @@ export function TeacherChatPanel({
|
||||
return { ...(olderSnapshot ? current : next), requests: [...requests.values()].sort((a, b) => a.createdAt.localeCompare(b.createdAt) || messageOrderKey(a).localeCompare(messageOrderKey(b))) };
|
||||
});
|
||||
},
|
||||
[persistDraft, continuous]
|
||||
[persistDraft, continuous, projectId]
|
||||
);
|
||||
useEffect(() => {
|
||||
if (!viewingLegacy.current && externalTopic && (continuous || externalTopic.projectId === projectId)
|
||||
if (!viewingLegacy.current && externalTopic && externalTopic.projectId === projectId
|
||||
&& (!selectedAgentId || externalTopic.definition.config_id === selectedAgentId)) {
|
||||
consume(externalTopic);
|
||||
setDefinition(continuous ? selectedAgentRef.current?.definition ?? externalTopic.definition : externalTopic.definition);
|
||||
@@ -235,7 +247,7 @@ export function TeacherChatPanel({
|
||||
}
|
||||
try {
|
||||
if (continuous && selectedAgentId) {
|
||||
const view = await teacherApi.conversation(selectedAgentId);
|
||||
const view = await teacherApi.conversation(projectId, selectedAgentId);
|
||||
if (alive) { if (view.topic) consume(view.topic); if (!cachedView?.topic) setBefore(view.before); }
|
||||
return;
|
||||
}
|
||||
@@ -245,7 +257,7 @@ export function TeacherChatPanel({
|
||||
const previous = listed.items.find((item) => item.id === listed.lastSelectedTopicId
|
||||
&& (!selectedAgentId || item.teacherId === selectedAgentId))
|
||||
?? (selectedAgentId ? listed.items.find((item) => item.teacherId === selectedAgentId) : undefined);
|
||||
if (externalTopicRef.current && (!selectedAgentId || externalTopicRef.current.definition.config_id === selectedAgentId)) {
|
||||
if (externalTopicRef.current?.projectId === projectId && (!selectedAgentId || externalTopicRef.current.definition.config_id === selectedAgentId)) {
|
||||
consume(externalTopicRef.current);
|
||||
setDefinition(externalTopicRef.current.definition);
|
||||
} else if (previous) {
|
||||
@@ -267,7 +279,7 @@ export function TeacherChatPanel({
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
generation.current++;
|
||||
};
|
||||
}, [base, draftRevision, label, consume, selectedAgentId, continuous, cachedView, reloadCatalog]);
|
||||
}, [base, projectId, draftRevision, label, consume, selectedAgentId, continuous, cachedView, reloadCatalog]);
|
||||
const seenPending = useRef<string | undefined>(undefined);
|
||||
const markSeen = useCallback(() => {
|
||||
const current = topicRef.current;
|
||||
@@ -276,9 +288,9 @@ export function TeacherChatPanel({
|
||||
|| document.visibilityState === 'hidden' || ['preparing', 'running'].includes(last.status)
|
||||
|| current.conversation?.seenThrough === last.id || seenPending.current === last.id) return;
|
||||
seenPending.current = last.id;
|
||||
void teacherApi.seenConversation(selectedAgentId, last.id).then(consume).catch(() => undefined)
|
||||
void teacherApi.seenConversation(projectId, selectedAgentId, last.id).then(consume).catch(() => undefined)
|
||||
.finally(() => { seenPending.current = undefined; });
|
||||
}, [continuous, legacyView, selectedAgentId, consume]);
|
||||
}, [continuous, legacyView, projectId, selectedAgentId, consume]);
|
||||
useEffect(() => { markSeen(); }, [topic?.revision, markSeen]);
|
||||
useEffect(() => {
|
||||
if (!topic?.id || legacyFriend || legacyView) return;
|
||||
@@ -318,7 +330,7 @@ export function TeacherChatPanel({
|
||||
const epoch = generation.current;
|
||||
setLoadingOlder(true);
|
||||
try {
|
||||
const page = await teacherApi.conversation(selectedAgentId, before);
|
||||
const page = await teacherApi.conversation(projectId, selectedAgentId, before);
|
||||
if (epoch !== generation.current) return;
|
||||
const pane = scrollRef.current;
|
||||
if (pane) olderAnchor.current = { height: pane.scrollHeight, top: pane.scrollTop };
|
||||
@@ -336,7 +348,7 @@ export function TeacherChatPanel({
|
||||
};
|
||||
const returnToConversation = async () => {
|
||||
if (!selectedAgentId) return;
|
||||
const view = await teacherApi.conversation(selectedAgentId);
|
||||
const view = await teacherApi.conversation(projectId, selectedAgentId);
|
||||
viewingLegacy.current = false;
|
||||
setTopic(view.topic); setBefore(view.before); setDefinition(selectedAgentRef.current?.definition ?? null);
|
||||
setHistoryOpen(false);
|
||||
@@ -436,7 +448,7 @@ export function TeacherChatPanel({
|
||||
};
|
||||
pending.current = input;
|
||||
persistDraft(text, references, input);
|
||||
const next = continuous ? await teacherApi.sendConversation(selectedAgentId!, input)
|
||||
const next = continuous ? await teacherApi.sendConversation(projectId, selectedAgentId!, input)
|
||||
: await teacherApi.send(topicBase, current!.id, input);
|
||||
if (version !== generation.current) return;
|
||||
consume(next);
|
||||
|
||||
Reference in New Issue
Block a user