fix: render consultation replies and typed tool activity
This commit is contained in:
@@ -13,6 +13,7 @@ import type {
|
||||
import { TeacherDiscussionPanel, DiscussionFocus } from './TeacherDiscussionPanel';
|
||||
import type { TeacherDiscussionAction } from '../../../shared/coding-teacher';
|
||||
import { getAgentAvatarSrc } from '@/lib/agent-avatars';
|
||||
import { TeacherReply } from './TeacherReply';
|
||||
|
||||
export function TeacherChatPanel({
|
||||
projectId,
|
||||
@@ -413,11 +414,21 @@ export function TeacherChatPanel({
|
||||
: <div className="flex justify-end"><p className="consultation-user max-w-[92%] whitespace-pre-wrap break-words rounded-2xl rounded-br-sm px-4 py-3 text-[13px] leading-7">{request.text}</p></div>}
|
||||
{request.references.length > 0 && <details className="text-xs text-muted-foreground"><summary>本轮引用 {request.references.length} 项</summary>{request.references.map((ref, index) => <pre className="whitespace-pre-wrap" key={index}>{ref.text}</pre>)}</details>}
|
||||
<div className="flex items-center gap-2 text-[11px] text-muted-foreground"><img src={avatar} alt="" className="h-6 w-6 rounded-lg [image-rendering:pixelated]" />{definition?.name ?? label}</div>
|
||||
{!!request.toolActivity?.length && <details className="text-xs text-muted-foreground">
|
||||
<summary className="cursor-pointer">使用了 {request.toolActivity.length} 项工具</summary>
|
||||
<ul className="mt-2 space-y-2" aria-label="智能体工具活动">
|
||||
{request.toolActivity.map(activity => <li key={activity.id} className="flex items-start justify-between gap-3">
|
||||
<span>{activity.name === 'list_project_files' ? '查看项目文件列表' : activity.name === 'read_project_file' ? '读取项目文件' : activity.name === 'read_conversation' ? '读取会话原文' : '使用智能体工具'}</span>
|
||||
<span className="shrink-0">{activity.status === 'completed' ? '已完成' : activity.status === 'failed' ? '未完成'
|
||||
: ['running', 'preparing'].includes(request.status) ? '进行中' : '未收到执行结果'}</span>
|
||||
</li>)}
|
||||
</ul>
|
||||
</details>}
|
||||
{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' && <>
|
||||
<p className="whitespace-pre-wrap break-words text-[13px] leading-7">{request.response}</p>
|
||||
<TeacherReply text={request.response} />
|
||||
<div className="flex flex-col gap-2" aria-label="可以和智能体聊的问题">
|
||||
{request.suggestedQuestions?.map((question) => <button key={question} type="button" disabled={helpUnavailable} onClick={() => void send({ text: question })} className="consultation-suggestion flex min-h-11 items-center justify-between gap-3 rounded-xl border bg-white px-3 py-3 text-left text-[13px] leading-6 transition-colors disabled:cursor-not-allowed disabled:opacity-50">{question}<ChevronRight className="h-3.5 w-3.5 shrink-0 opacity-60" /></button>)}
|
||||
</div>
|
||||
@@ -425,11 +436,15 @@ export function TeacherChatPanel({
|
||||
<p className="text-[11px] leading-5 text-muted-foreground">也可以直接在下面说说你的想法。</p>
|
||||
</>}
|
||||
{['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>}
|
||||
</> : <div className="whitespace-pre-wrap break-words text-[13px] leading-7">{request.response || (['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" />{request.discussionContext ? '正在梳理你的新想法…' : `${label}正在想…`}</span> : ['cancelled', 'interrupted'].includes(request.status) ? '这次先停在这里。想继续时可以再说说。' : '')}</div>}
|
||||
</> : 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" />{request.discussionContext ? '正在梳理你的新想法…' : `${label}正在想…`}</span> : ['cancelled', 'interrupted'].includes(request.status) ? '这次先停在这里。想继续时可以再说说。' : ''}</div>}
|
||||
{discussionEnabled && request.intent !== 'suggestions' && request.status === 'completed' && topic.discussion?.status !== 'active' && topic.discussion?.status !== 'offered' && Boolean(request.suggestedQuestions?.length) && <div className="flex flex-col gap-2" aria-label="接着聊">
|
||||
{request.suggestedQuestions?.map(question => <button type="button" key={question} disabled={helpUnavailable} onClick={() => void send({ text: question })} className="consultation-suggestion rounded-xl border px-3 py-2 text-left text-xs leading-6">{question}<ChevronRight className="ml-1 inline h-3 w-3" /></button>)}
|
||||
</div>}
|
||||
{request.discussionError && <p className="text-xs leading-6 text-muted-foreground">{request.discussionError}</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>
|
||||
</details>}
|
||||
{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.omittedMessages > 0 && <p className="text-[10px] text-muted-foreground">本轮参考了较近的讨论,省略了 {request.omittedMessages} 条较早内容。</p>}
|
||||
|
||||
46
src/pages/Chat/TeacherReply.tsx
Normal file
46
src/pages/Chat/TeacherReply.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { memo, useState } from 'react';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import remarkGfm from 'remark-gfm';
|
||||
import remarkMath from 'remark-math';
|
||||
import rehypeKatex from 'rehype-katex';
|
||||
import { toast } from 'sonner';
|
||||
import { openConversationExternalUrl, safeConversationExternalUrl } from '@/lib/conversation-links';
|
||||
|
||||
function ReplyImage({ src, alt }: { src?: string; alt?: string }) {
|
||||
const [failed, setFailed] = useState(false);
|
||||
const url = safeConversationExternalUrl(src);
|
||||
if (!url || failed) return <span className="text-muted-foreground">[图片未能显示:{alt || '图片'}]</span>;
|
||||
return <img src={url} alt={alt || '智能体提供的图片'} loading="lazy" referrerPolicy="no-referrer"
|
||||
className="my-3 h-auto max-h-80 max-w-full rounded-lg object-contain" onError={() => setFailed(true)} />;
|
||||
}
|
||||
|
||||
/** Render answer text only. Tool activity and discussion data have separate, typed UI. */
|
||||
export const TeacherReply = memo(function TeacherReply({ text }: { text: string }) {
|
||||
return <div className="teacher-reply min-w-0 max-w-full break-words text-[13px] leading-7 [overflow-wrap:anywhere] [&_.katex-display]:max-w-full [&_.katex-display]:overflow-x-auto" data-testid="teacher-reply">
|
||||
<ReactMarkdown remarkPlugins={[remarkGfm, remarkMath]} rehypePlugins={[rehypeKatex]} skipHtml components={{
|
||||
h1: ({ children }) => <h1 className="mb-2 mt-4 text-lg font-semibold first:mt-0">{children}</h1>,
|
||||
h2: ({ children }) => <h2 className="mb-2 mt-4 text-base font-semibold first:mt-0">{children}</h2>,
|
||||
h3: ({ children }) => <h3 className="mb-2 mt-3 text-sm font-semibold first:mt-0">{children}</h3>,
|
||||
h4: ({ children }) => <h4 className="mb-1 mt-3 font-semibold first:mt-0">{children}</h4>,
|
||||
p: ({ children }) => <p className="my-2 whitespace-pre-wrap first:mt-0 last:mb-0">{children}</p>,
|
||||
ul: ({ children }) => <ul className="my-2 list-disc space-y-1 pl-5">{children}</ul>,
|
||||
ol: ({ children, start }) => <ol start={start} className="my-2 list-decimal space-y-1 pl-5">{children}</ol>,
|
||||
blockquote: ({ children }) => <blockquote className="my-3 border-l-2 border-border pl-3 text-muted-foreground">{children}</blockquote>,
|
||||
a: ({ href, children }) => {
|
||||
const url = safeConversationExternalUrl(href);
|
||||
return url ? <a href={url} target="_blank" rel="noopener noreferrer" className="text-brand underline underline-offset-2"
|
||||
onClick={event => {
|
||||
event.preventDefault();
|
||||
void openConversationExternalUrl(url).catch(() => toast.error('链接无法打开'));
|
||||
}}>{children}</a> : <span>{children}</span>;
|
||||
},
|
||||
code: ({ children, className }) => <code className={className}>{children}</code>,
|
||||
pre: ({ children }) => <pre className="my-3 max-w-full overflow-x-auto whitespace-pre rounded-lg border bg-surface-subtle p-3 font-mono text-xs leading-5">{children}</pre>,
|
||||
table: ({ children }) => <div className="my-3 max-w-full overflow-x-auto rounded-lg border"><table className="w-full border-collapse text-left text-xs leading-6">{children}</table></div>,
|
||||
thead: ({ children }) => <thead className="bg-surface-subtle">{children}</thead>,
|
||||
th: ({ children, style }) => <th style={style} className="min-w-24 border-b px-3 py-2 align-top font-semibold">{children}</th>,
|
||||
td: ({ children, style }) => <td style={style} className="min-w-24 border-b px-3 py-2 align-top">{children}</td>,
|
||||
img: ({ src, alt }) => <ReplyImage key={typeof src === 'string' ? src : ''} src={typeof src === 'string' ? src : undefined} alt={alt} />,
|
||||
}}>{text}</ReactMarkdown>
|
||||
</div>;
|
||||
});
|
||||
Reference in New Issue
Block a user