fix: preserve complete consultation replies and recover retained history

This commit is contained in:
鲨鱼辣椒
2026-09-28 17:38:31 +08:00
parent dda2f0a3be
commit d530abe580
14 changed files with 643 additions and 38 deletions

View File

@@ -6,6 +6,7 @@ import type {
TeacherDefinition,
TeacherCatalog,
TeacherReference,
TeacherRequest,
TeacherSend,
TeacherTopic,
TeacherTopicList,
@@ -108,6 +109,7 @@ export function TeacherChatPanel({
? teachers.some((item) => item.teacher_id === topic.definition.config_id)
: legacyEnabled;
const pending = useRef<TeacherSend | null>(restorePendingSend(saved.pending));
const sendInFlight = useRef(false);
useEffect(() => {
persistDraft(text, references, pending.current);
}, [text, references, persistDraft]);
@@ -293,11 +295,19 @@ export function TeacherChatPanel({
if (version === generation.current) setBusy(false);
}
};
const send = async (action?: Pick<TeacherSend, 'text' | 'intent'>) => {
const send = async (action?: Pick<TeacherSend, 'text' | 'intent'>, retry?: TeacherRequest) => {
// A historical retry stays with its original topic and published Agent.
if (retry && (topicRef.current?.id !== topic?.id || topic?.projectId !== projectId
|| (selectedAgentId && topic.definition.config_id !== selectedAgentId)
|| !topic.requests.some((request) => request === retry && request.status === 'completed' && request.replyIncomplete))) return;
const question = action?.text ?? text;
const questionReferences = action ? [] : references;
if (!question.trim() || busy || !topicEnabled || topicRef.current?.requests.some((request) => ['preparing', 'running'].includes(request.status))) return;
const questionReferences = retry?.references ?? (action ? [] : references);
const questionSourceId = retry
? retry.sourceConversationId ?? (topic?.sourceConversationId === 'project' ? undefined : topic?.sourceConversationId)
: sourceId;
if (!question.trim() || busy || sendInFlight.current || !topicEnabled || topicRef.current?.requests.some((request) => ['preparing', 'running'].includes(request.status))) return;
const version = generation.current;
sendInFlight.current = true;
setBusy(true);
setError('');
let current = topicRef.current;
@@ -317,18 +327,19 @@ export function TeacherChatPanel({
? pending.current
: null;
// Copy changes must not create another charge for an uncertain hint request.
const retrySuggestions = action?.intent === 'suggestions' && previous?.intent === 'suggestions';
const retrySuggestions = !retry && action?.intent === 'suggestions' && previous?.intent === 'suggestions';
const input =
previous && (retrySuggestions || (
previous.text === question &&
(previous.intent ?? 'question') === (action?.intent ?? 'question') &&
JSON.stringify(previous.references) === JSON.stringify(questionReferences)
JSON.stringify(previous.references) === JSON.stringify(questionReferences) &&
(!retry || previous.sourceConversationId === (questionSourceId || undefined))
))
? previous
: {
requestId: crypto.randomUUID(), text: question, references: questionReferences,
...(action?.intent ? { intent: action.intent } : {}),
...(sourceId ? { sourceConversationId: sourceId } : {}),
...(questionSourceId ? { sourceConversationId: questionSourceId } : {}),
};
pending.current = input;
persistDraft(text, references, input);
@@ -337,7 +348,7 @@ export function TeacherChatPanel({
consume(next);
pending.current = null;
if (action) {
persistDraft(text, references);
persistDraft(draftRef.current.text, draftRef.current.references);
} else {
setText('');
setReferences([]);
@@ -348,6 +359,7 @@ export function TeacherChatPanel({
if (version === generation.current)
setError(e instanceof Error ? e.message : '提问失败,请保留问题后重试');
} finally {
sendInFlight.current = false;
if (version === generation.current) setBusy(false);
}
};
@@ -395,7 +407,10 @@ export function TeacherChatPanel({
</li>)}
</ul>
</details>}
{request.intent === 'suggestions' ? <>
{request.status === 'completed' && request.replyIncomplete ? <div className="space-y-3">
<p role="status" className="text-[13px] leading-7 text-muted-foreground">这次回复未能完整显示</p>
{request.intent !== 'check-in' && request.text.trim() && <button type="button" disabled={helpUnavailable} onClick={() => void send({ text: request.text, intent: request.intent }, request)} className="consultation-retry">重新回答</button>}
</div> : request.intent === 'suggestions' ? <>
{['running', 'preparing'].includes(request.status)
? <p role="status" className="flex items-center gap-2 text-[13px] leading-7 text-muted-foreground"><Loader2 className="h-3.5 w-3.5 animate-spin motion-reduce:animate-none" />我看看你最近做到了哪里…</p>
: request.status === 'completed' && <>
@@ -406,9 +421,10 @@ export function TeacherChatPanel({
</>}
{['failed', 'cancelled', 'interrupted'].includes(request.status) && <button type="button" disabled={helpUnavailable} onClick={askForSuggestions} className="consultation-retry">继续看看👀</button>}
</> : request.response ? <TeacherReply text={request.response} /> : <div className="text-[13px] leading-7">{['running', 'preparing'].includes(request.status) ? <span role="status" className="inline-flex items-center gap-2 text-muted-foreground"><Loader2 className="h-3.5 w-3.5 animate-spin motion-reduce:animate-none" />{label}正在想…</span> : ['cancelled', 'interrupted'].includes(request.status) ? '这次先停在这里。想继续时可以再说说。' : ''}</div>}
{request.intent !== 'suggestions' && request.status === 'completed' && Boolean(request.suggestedQuestions?.length) && <div className="consultation-follow-ups" aria-label="接着聊">
{request.intent !== 'suggestions' && request.status === 'completed' && !request.replyIncomplete && Boolean(request.suggestedQuestions?.length) && <div className="consultation-follow-ups" aria-label="接着聊">
{request.suggestedQuestions?.map((question, index) => <button type="button" key={`${request.id}:${index}`} disabled={helpUnavailable} onClick={() => void send({ text: question })} className="consultation-follow-up"><span className="min-w-0 flex-1">{question}</span><ChevronRight className="h-4 w-4 shrink-0 opacity-70" aria-hidden="true" /></button>)}
</div>}
{request.status === 'completed' && !request.replyIncomplete && request.replyParseError && <p className="text-xs text-muted-foreground">回答正文已保留,部分附加内容未能显示。</p>}
{request.unparsedResponse && <details className="min-w-0 text-xs text-muted-foreground">
<summary className="cursor-pointer">查看收到的原始内容</summary>
<pre className="mt-2 max-h-64 overflow-auto whitespace-pre-wrap break-words rounded-lg border p-3">{request.unparsedResponse}</pre>
@@ -416,7 +432,7 @@ export function TeacherChatPanel({
{request.progress && ['running', 'preparing'].includes(request.status) && <p role="status" className="text-xs text-muted-foreground">{request.progress}</p>}
{(request.truncatedMessages ?? 0) > 0 && <p className="text-xs text-muted-foreground">{draftRevision ? '较长的上下文已节选。' : '较长的上下文已节选,智能体可按需读取原文。'}</p>}
{request.error && <p className="text-xs text-destructive">{request.error}</p>}
{onBringBack && request.response && request.intent !== 'suggestions' && request.status === 'completed' && <div className="text-[11px] text-muted-foreground">
{onBringBack && request.response && !request.replyIncomplete && request.intent !== 'suggestions' && request.status === 'completed' && <div className="text-[11px] text-muted-foreground">
<button onClick={() => onBringBack(request.response)}>带回主会话草稿</button>
</div>}
</div>)}