Merge remote main preserving project chats and reply recovery
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled

This commit is contained in:
2026-09-28 20:51:33 +08:00
54 changed files with 3189 additions and 1713 deletions

View File

@@ -5,7 +5,6 @@ import type {
TeacherCheckInResult,
TeacherCatalog,
TeacherDefinition,
TeacherDiscussionAction,
TeacherSend,
TeacherTopic,
TeacherTopicList,
@@ -64,10 +63,6 @@ export const teacherApi = {
method: 'POST',
body: JSON.stringify(input),
}),
updateDiscussion: (base: string, id: string, input: TeacherDiscussionAction) =>
hostApiFetch<TeacherTopic>(base + '/' + encodeURIComponent(id) + '/discussion', {
method: 'POST', body: JSON.stringify(input),
}),
cancel: (base: string, id: string, requestId: string) =>
hostApiFetch<TeacherTopic>(
base +

View File

@@ -480,23 +480,24 @@ export function AgentBrowserPanel({
ref={panelRef}
className={cn(embedded ? 'relative flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-hidden bg-background' : 'relative flex w-[400px] min-w-[300px] max-w-[48vw] shrink-0 flex-col overflow-hidden border-l border-border/80 bg-background shadow-[-16px_0_32px_hsl(220_20%_16%_/_0.06)] max-[900px]:fixed max-[900px]:inset-y-10 max-[900px]:right-0 max-[900px]:z-40 max-[900px]:max-w-[min(92vw,420px)]')}
data-testid="agent-browser-panel"
aria-label={embedded ? '作品预览' : '开发浏览器'}
>
<div className="flex h-10 shrink-0 items-center gap-2 border-b border-border/70 px-2.5">
{!embedded && <div className="flex h-10 shrink-0 items-center gap-2 border-b border-border/70 px-2.5">
<span className="flex h-6 w-6 items-center justify-center rounded-md bg-brand-soft text-brand">
<Globe2 className="h-3.5 w-3.5" aria-hidden="true" />
</span>
<h2 className="min-w-0 flex-1 truncate text-xs font-semibold">{embedded ? '作品预览' : '开发浏览器'}</h2>
<h2 className="min-w-0 flex-1 truncate text-xs font-semibold">开发浏览器</h2>
<Button
type="button"
variant="ghost"
size="icon"
className="h-8 w-8 rounded-md text-muted-foreground"
aria-label={embedded ? '收起作品' : '关闭开发浏览器'}
aria-label="关闭开发浏览器"
onClick={() => onOpenChange(false)}
>
<X className="h-4 w-4" aria-hidden="true" />
</Button>
</div>
</div>}
<form className="flex h-10 shrink-0 items-center gap-1 border-b border-border/70 px-2" onSubmit={handleOpenAddress}>
<Button type="button" variant="ghost" size="icon" className="h-7 w-7 rounded-md" aria-label="后退" disabled={!snapshot?.canGoBack || busy} onClick={() => handleNavigate('back')}>

View File

@@ -6,6 +6,7 @@ import type {
TeacherDefinition,
TeacherCatalog,
TeacherReference,
TeacherRequest,
TeacherSend,
TeacherTopic,
TeacherTopicList,
@@ -19,6 +20,13 @@ 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;
function restorePendingSend(saved?: TeacherSend, legacyProjectId?: string): TeacherSend | null {
if (!saved) return null;
// Keep an interrupted send retryable without restoring retired UI protocol fields.
const { requestId, text, references, intent, sourceConversationId, projectId } = saved;
return { requestId, text, references, intent, sourceConversationId, ...(projectId || legacyProjectId ? { projectId: projectId ?? legacyProjectId } : {}) };
}
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} />;
@@ -85,7 +93,6 @@ function ProjectTeacherChatPanel({
try { localStorage.setItem(draftKey, JSON.stringify({ text, references, pending, ...referenceScopeRef.current })); } catch { /* Draft remains in the open panel. */ }
}, [draftKey]);
const label = '智能体';
const helpEnabled = !draftRevision;
const [historyOpen, setHistoryOpen] = useState(false);
const [before, setBefore] = useState<string | null>(cachedView?.before ?? null);
const [loadingOlder, setLoadingOlder] = useState(false);
@@ -138,7 +145,8 @@ function ProjectTeacherChatPanel({
: topic.definition.config_id
? teachers.some((item) => item.teacher_id === topic.definition.config_id)
: legacyEnabled;
const pending = useRef<TeacherSend | null>(saved.pending ?? null);
const pending = useRef<TeacherSend | null>(restorePendingSend(saved.pending, continuous ? saved.projectId ?? projectId : undefined));
const sendInFlight = useRef(false);
useEffect(() => {
persistDraft(text, references, pending.current);
}, [text, references, persistDraft]);
@@ -384,17 +392,35 @@ function ProjectTeacherChatPanel({
if (version === generation.current) setBusy(false);
}
};
const send = async (action?: Pick<TeacherSend, 'text' | 'intent'>) => {
const retryScopeMatches = (request: TeacherRequest) => !continuous
|| (request.projectId === projectId && (request.sourceConversationId ?? '') === sourceId);
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 || !retryScopeMatches(retry)
|| (!continuous && 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;
if (continuous && questionReferences.length && (referenceScope.projectId !== projectId || referenceScope.sourceId !== sourceId)) {
const questionReferences = retry?.references ?? (action ? [] : references);
const questionSourceId = retry
? retry.sourceConversationId ?? (continuous || 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;
if (continuous && !retry && questionReferences.length && (referenceScope.projectId !== projectId || referenceScope.sourceId !== sourceId)) {
setError('引用来自另一个项目或操作对话,请切回来源,或移除引用后再提问。'); return;
}
if (continuous && pending.current?.text === question && pending.current.projectId && pending.current.projectId !== projectId) {
if (continuous && pending.current && !topicRef.current?.requests.some(request => request.id === pending.current?.requestId)
&& (pending.current.text === question || (action?.intent === 'suggestions' && pending.current.intent === 'suggestions'))
&& pending.current.projectId && pending.current.projectId !== projectId) {
setError('上次提问仍绑定原项目,请切回原项目确认结果后重试。'); return;
}
if (continuous && !retry && action?.intent === 'suggestions' && pending.current?.intent === 'suggestions'
&& !topicRef.current?.requests.some(request => request.id === pending.current?.requestId)
&& pending.current.sourceConversationId !== (questionSourceId || undefined)) {
setError('上次提问仍绑定原操作对话,请切回原操作对话确认结果后重试。'); return;
}
const version = generation.current;
sendInFlight.current = true;
setBusy(true);
setError('');
let current = topicRef.current;
@@ -413,20 +439,22 @@ function ProjectTeacherChatPanel({
const previous = pending.current && !current?.requests.some((request) => request.id === pending.current?.requestId)
? pending.current
: null;
// Retry an uncertain saved request verbatim, including pre-upgrade presentation fields.
// Copy changes must not create another charge for an uncertain hint request.
const retrySuggestions = !retry && action?.intent === 'suggestions' && previous?.intent === 'suggestions';
const input =
previous &&
previous.text === question &&
(!continuous || previous.projectId === projectId) &&
previous.sourceConversationId === (sourceId || undefined) &&
(previous.intent ?? 'question') === (action?.intent ?? 'question') &&
JSON.stringify(previous.references) === JSON.stringify(questionReferences)
previous && (!continuous || (previous.projectId === projectId
&& previous.sourceConversationId === (questionSourceId || undefined))) && (retrySuggestions || (
previous.text === question &&
(previous.intent ?? 'question') === (action?.intent ?? 'question') &&
JSON.stringify(previous.references) === JSON.stringify(questionReferences) &&
(!retry || previous.sourceConversationId === (questionSourceId || undefined))
))
? previous
: {
requestId: crypto.randomUUID(), text: question, references: questionReferences,
...(continuous ? { projectId } : {}),
...(action?.intent ? { intent: action.intent } : {}),
...(sourceId ? { sourceConversationId: sourceId } : {}),
...(questionSourceId ? { sourceConversationId: questionSourceId } : {}),
};
pending.current = input;
persistDraft(text, references, input);
@@ -436,7 +464,7 @@ function ProjectTeacherChatPanel({
consume(next);
pending.current = null;
if (action) {
persistDraft(text, references);
persistDraft(draftRef.current.text, draftRef.current.references);
} else {
setText('');
setReferences([]);
@@ -447,6 +475,7 @@ function ProjectTeacherChatPanel({
if (version === generation.current)
setError(e instanceof Error ? e.message : '提问失败,请保留问题后重试');
} finally {
sendInFlight.current = false;
if (version === generation.current) setBusy(false);
}
};
@@ -454,8 +483,7 @@ function ProjectTeacherChatPanel({
(request) => request.status === 'preparing' || request.status === 'running'
);
const helpUnavailable = busy || !topicEnabled || Boolean(running);
const askForSuggestions = () => void send({ text: '帮我看看', intent: 'suggestions' });
const askForGuidance = () => void send({ text: '我也说不清,你带我看看', intent: 'guided-help' });
const askForSuggestions = () => void send({ text: '继续看看👀', intent: 'suggestions' });
const changeText = (next: string) => { setText(next); persistDraft(next, references, pending.current); };
const removeReferences = () => { setReferences([]); persistDraft(text, [], pending.current); };
const questions = definition?.suggested_questions ?? [];
@@ -512,22 +540,25 @@ function ProjectTeacherChatPanel({
</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 || !retryScopeMatches(request)} onClick={() => void send({ text: request.text, intent: request.intent }, request)} className="consultation-retry">重新回答</button>}
{!retryScopeMatches(request) && <p className="text-xs text-muted-foreground">回到原项目和操作对话后,可以重新回答。</p>}
</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' && <>
<TeacherReply text={request.response} />
<div className="consultation-follow-ups" aria-label="可以和智能体聊的问题">
{request.suggestedQuestions?.map((question) => <button key={question} type="button" 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>)}
{request.suggestedQuestions?.map((question, index) => <button key={`${request.id}:${index}`} type="button" 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>
<button type="button" disabled={helpUnavailable} onClick={askForGuidance} className="consultation-guided-help min-h-10 text-left text-xs leading-6 underline decoration-current/30 underline-offset-4 disabled:cursor-not-allowed disabled:opacity-50">我也说不清,你带我看看</button>
</>}
{['failed', 'cancelled', 'interrupted'].includes(request.status) && <button type="button" disabled={helpUnavailable} onClick={askForSuggestions} className="consultation-guided-help min-h-10 text-xs underline underline-offset-4 disabled:opacity-50">再请智能体看看</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>}
{helpEnabled && request.intent !== 'suggestions' && request.status === 'completed' && Boolean(request.suggestedQuestions?.length) && <div className="consultation-follow-ups" aria-label="接着聊">
{request.suggestedQuestions?.map(question => <button type="button" key={question} 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>)}
{['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' && !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.discussionError && <p className="text-xs leading-6 text-muted-foreground">{request.discussionError}</p>}
{request.status === 'completed' && !request.replyIncomplete && request.replyParseError && <p role="status" 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>
@@ -535,7 +566,7 @@ function ProjectTeacherChatPanel({
{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 disabled={continuous && (request.projectId !== projectId || request.sourceConversationId !== sourceId)} onClick={() => onBringBack(request.response)}>
{continuous && (request.projectId !== projectId || request.sourceConversationId !== sourceId) ? '请打开来源项目和操作对话后带回' : '带回主会话草稿'}</button>
</div>}
@@ -564,9 +595,10 @@ function ProjectTeacherChatPanel({
<textarea ref={inputRef} aria-label={`向${label}提问`} value={text} disabled={busy} maxLength={6000} onChange={(e) => changeText(e.target.value)} placeholder="和老师聊聊" rows={2}
onKeyDown={(e) => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); void send(); } }}
className="min-h-[60px] w-full resize-none border-0 bg-transparent text-[13px] leading-6 outline-none placeholder:text-muted-foreground/65" />
<div className="mt-2 flex items-center justify-between gap-2" data-testid="consultation-composer-actions">
{helpEnabled ? <button type="button" disabled={helpUnavailable} onClick={askForSuggestions} className="consultation-help-prompt flex min-h-9 cursor-pointer items-center gap-1.5 rounded-full px-3 text-xs transition-colors disabled:cursor-not-allowed disabled:opacity-50">帮我看看<ChevronRight className="h-3 w-3" aria-hidden="true" /></button>
: <span />}
<div className="mt-2 flex min-h-11 items-center justify-between gap-2" data-testid="consultation-composer-actions">
<div className="min-w-0 flex-1">
{!draftRevision && !text.trim() && <button type="button" disabled={helpUnavailable} onClick={askForSuggestions} className="consultation-hint">继续看看👀</button>}
</div>
{running ? <button type="button" className="rounded-lg border px-3 py-2 text-xs" onClick={() => void teacherApi.cancel(topicBase, endpointId, running.id).then(consume).catch(() => setError('暂时无法停止,请稍后重试。'))}>停止回复</button>
: <button type="submit" aria-label="提问" title={busy ? '正在发送…' : '发送'} disabled={busy || !topicEnabled || !text.trim()} className="consultation-send flex h-8 w-8 items-center justify-center rounded-full text-white disabled:opacity-30"><ArrowUp className="h-4 w-4" /></button>}
</div>

View File

@@ -49,9 +49,11 @@
.classroom-consultation-resizer { position: absolute; inset: 0 auto 0 -4px; width: 8px; z-index: 2; cursor: col-resize; }
.classroom-consultation-resizer:hover, .classroom-consultation-resizer:focus-visible { background: #7994b533; }
.consultation-pane { background: #fffdf8; color: #30465f; }
.consultation-help-prompt, .consultation-guided-help { color: #536747; }
.consultation-help-prompt { background: #f1f5ea; border: 1px solid #dfe7d5; }
.consultation-help-prompt:hover:not(:disabled) { background: #e6eedc; border-color: #bacba8; }
.consultation-hint, .consultation-retry { display: inline-flex; align-items: center; justify-content: center; align-self: flex-start; min-height: 44px; max-width: 100%; padding: 8px 16px; border: 1.5px solid #e3ce8f; border-radius: 16px 16px 16px 6px; background: #fff2c9; color: #6f5127; box-shadow: 0 3px 0 #e7d7a6, inset 0 1px 0 #fff9e8; font-size: 14px; font-weight: 600; line-height: 24px; cursor: pointer; transition: background-color 160ms ease, border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease; }
.consultation-hint:hover:not(:disabled), .consultation-retry:hover:not(:disabled) { background: #ffe9a9; border-color: #d6b76f; }
.consultation-hint:active:not(:disabled), .consultation-retry:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 #e7d7a6, inset 0 1px 0 #fff9e8; }
.consultation-hint:disabled, .consultation-retry:disabled { cursor: not-allowed; opacity: 0.5; }
@media (prefers-reduced-motion: reduce) { .consultation-hint, .consultation-retry { transition: none; } .consultation-hint:active:not(:disabled), .consultation-retry:active:not(:disabled) { transform: none; } }
.consultation-pane button:focus-visible { outline: 2px solid #71845f; outline-offset: 3px; }
.consultation-suggestion { border-color: #e0e6d6; color: #718163; }
.consultation-suggestion:hover { border-color: #bac9aa; background: #f4f7ee; }

View File

@@ -288,6 +288,13 @@ export function useTeacherCompanion(options: Options) {
return () => { alive = false; window.clearInterval(timer); window.removeEventListener('focus', refresh); };
}, [account, options.projectId, state.agents]);
useEffect(() => {
if (!options.teacherOpen || !options.projectId) return;
// Entering consultation consumes the greeting, but leaves real invitations unread.
writeLocal(storageKey + ':welcome-dismissed', true);
setState((current) => current.welcomeDismissed ? current : { ...current, welcomeDismissed: true });
}, [options.teacherOpen, options.projectId, storageKey]);
const topic = stateScope.current === scope ? state.topic : null;
const topicId = topic?.id;
const topicProjectId = topic?.projectId;
@@ -317,7 +324,7 @@ export function useTeacherCompanion(options: Options) {
const selectedAgentId = visibleState.selectedAgent?.teacher_id ?? selectedDefinition?.config_id;
const selectedAgent = visibleState.selectedAgent === null ? undefined
: visibleState.agents.find((item) => item.teacher_id === selectedAgentId) ?? visibleState.selectedAgent;
const welcome = topic?.role !== 'friend' && !visibleState.loading && visibleState.enabled && !visibleState.welcomeDismissed
const welcome = topic?.role !== 'friend' && !options.teacherOpen && !visibleState.loading && visibleState.enabled && !visibleState.welcomeDismissed
? selectedDefinition?.welcome_message.trim() || null : null;
return {
unreadAgentIds: stateScope.current === scope ? unreadAgentIds : [],