Spaces:
Sleeping
Sleeping
File size: 24,747 Bytes
f3f614f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 |
import styles from './index.module.less';
import { useEffect, useState, useRef, Children } from 'react';
import MindMapItem from './mindMapItem';
import PackIcon from '@/assets/pack-up.svg';
import SendIcon from '@/assets/sendIcon.svg';
import { Tooltip, Input, message } from 'antd';
import IconFont from '@/components/iconfont';
import ReactMarkdown from "react-markdown";
import ShowRightIcon from "@/assets/show-right-icon.png";
import rehypeRaw from 'rehype-raw';
import classNames from 'classnames';
import { fetchEventSource } from '@microsoft/fetch-event-source';
import { GET_SSE_DATA } from '@/config/cgi';
import { replaceStr } from '@/utils/tools';
const RenderTest = () => {
let eventSource: any = null;
let sseTimer: any = useRef(null);
const [isWaiting, setIsWaiting] = useState(false);
const [question, setQuestion] = useState("");
const [stashQuestion, setStashQuestion] = useState("");
const [isEnd, setIsEnd] = useState(false);
const [showEndNode, setShowEndNode] = useState(false);
// 一组节点的渲染草稿
const [draft, setDraft] = useState('');
// 一轮完整对话结束
const [chatIsOver, setChatIsOver] = useState(true);
// 一组节点的思考草稿是不是打印结束
const [draftEnd, setDraftEnd] = useState(false);
const [progress1, setProgress1] = useState('');
const [progress2, setProgress2] = useState('');
const [progressEnd, setProgressEnd] = useState(false);
const [conclusion, setConclusion] = useState('');
const [stashConclusion, setstashConclusion] = useState('');
const [query, setQuery] = useState([]);
const [searchList, setSearchList] = useState([]);
// 整体的渲染树
const [renderData, setRenderData] = useState<any[]>([]);
const [currentNode, setCurrentNode] = useState<any>(null);
// 渲染minddata里的第几个item
const [renderIndex, setRenderIndex] = useState<number>(0);
const [response, setResponse] = useState("");
const [currentStep, setCurrentStep] = useState(0);
// steps展开收起的信息
const [collapseInfo, setCollapseInfo] = useState([true, true]);
const [mapWidth, setMapWidth] = useState(0);
// 是否展示右侧内容
const [showRight, setShowRight] = useState(true);
const [currentNodeRendering, setCurrentNodeRendering] = useState(false);
const [selectedIds, setSelectedIds] = useState([]);
const [nodeName, setNodeName] = useState('');
const hasHighlight = useRef(false);
const conclusionRender = useRef(false);
const nodeDraftRender = useRef(false);
const [obj, setObj] = useState<any>(null);
const [nodeOutputEnd, setNodeEnd] = useState(false);
const [adjList, setAdjList] = useState([]);
const TEXT_INTERVAL = 20;
const SEARCHLIST_INTERVAL = 80;
const toggleRight = () => {
setShowRight(!showRight);
};
const findAndUpdateStatus = (nodes: any[], targetNode: any) => {
return nodes.map((node) => {
if (node.state === 1 && node.id !== 0) {
return { ...node, state: 3 };
}
if (node.name === targetNode) {
return { ...node, state: 1 };
}
if (node.children) {
// 递归地在子节点中查找
node.children = findAndUpdateStatus(node.children, targetNode);
}
return node;
});
}
const generateEndStyle = () => {
// 获取所有class为endline的div元素
const endlineDivs = document.getElementsByClassName('endline');
const mindMap = document.getElementById("mindMap");
// 确保至少有两个元素
if (endlineDivs.length >= 2 && mindMap) {
// 获取第一个和最后一个元素的边界框(bounding rectangle)
const firstRect = endlineDivs[0].getBoundingClientRect();
const lastRect = endlineDivs[endlineDivs.length - 1].getBoundingClientRect();
const mindMapRect = mindMap?.getBoundingClientRect();
// 计算y值的差值
const yDiff = lastRect.top - firstRect.top;
// const top = firstRect.top - mindMapRect.top;
// 如果需要包含元素的完整高度(不仅仅是顶部位置),可以加上元素的高度
// const yDiffWithHeight = yDiff + (lastRect.height - firstRect.height);
return {
top: firstRect.top - mindMapRect.top,
height: yDiff + 1
};
} else {
return {
top: '50%',
height: 0
};
}
};
const generateWidth = () => {
const articles = document.querySelectorAll('article');
// 确保至少有两个元素
if (articles?.length) {
let maxRight = 0;
articles.forEach((item, index) => {
if (item.getBoundingClientRect().right > maxRight) {
maxRight = item.getBoundingClientRect().right;
}
})
const firstArticle = articles[0].getBoundingClientRect();
if (maxRight - firstArticle.left + 200 > mapWidth) {
return maxRight - firstArticle.left + 200
} else {
return mapWidth;
}
} else {
return 100;
}
};
// 逐字渲染
const renderDraft = (str: string, type: string, endCallback: () => void) => {
// 已经输出的字符数量
let outputIndex = 0;
// 输出字符的函数
const outputText = () => {
// 给出高亮后draft输出的结束标志
if (type === 'stepDraft-1' && outputIndex + 3 > str?.length) {
nodeDraftRender.current = true;
}
// 如果还有字符未输出
if (outputIndex < str?.length) {
// 获取接下来要输出的1个字符(或剩余字符,如果不足3个)
let chunk = str.slice(outputIndex, Math.min(outputIndex + 10, str.length));
// 更新已输出字符的索引
outputIndex += chunk.length;
if (type === 'thought') {
setDraft(str.slice(0, outputIndex));
} else if (type === "stepDraft-0") {
setProgress1(str.slice(0, outputIndex));
} else if (type === "stepDraft-1") {
setProgress2(str.slice(0, outputIndex));
} else if (type === "conclusion") {
setConclusion(str.slice(0, outputIndex));
} else if (type === "response") {
setResponse(str.slice(0, outputIndex));
}
} else {
// 如果没有更多字符需要输出,则清除定时器
clearInterval(intervalId);
endCallback && endCallback()
}
}
// 设定定时器ID
let intervalId = setInterval(outputText, TEXT_INTERVAL);
}
// 渲染搜索结果renderSearchList
const renderSearchList = () => {
let outputIndex = 0;
const content = JSON.parse(currentNode.actions[currentStep].result[0].content);
const arr: any = Object.keys(content).map(item => {
return { id: item, ...content[item] };
});
const len = Object.keys(content).length;
const outputText = () => {
outputIndex++;
if (outputIndex < len + 1) {
setSearchList(arr.slice(0, outputIndex));
} else {
clearInterval(intervalId);
}
};
// 设定定时器ID
let intervalId = setInterval(outputText, SEARCHLIST_INTERVAL);
};
// 高亮searchList
const highLightSearchList = (ids: any) => {
setSelectedIds([]);
const newStep = currentStep + 1;
setCurrentStep(newStep);
const highlightArr: any = [...searchList];
highlightArr.forEach((item: any) => {
if (ids.includes(Number(item.id))) {
item.highLight = true;
}
})
highlightArr.sort((item1: any, item2: any) => {
if (item1.highLight === item2.highLight) {
return 0;
}
// 如果item1是highlight,放在前面
if (item1.highLight) {
return -1;
}
// 如果item2是highlight,放在后面
return 1;
})
setSearchList(highlightArr);
renderDraft(currentNode.actions[1].thought, `stepDraft-1`, () => { });
hasHighlight.current = true; // 标记为高亮已执行
};
// 渲染结论
const renderConclusion = () => {
const res = window.localStorage.getItem('nodeRes') || '';
const replaced = replaceStr(res);
// setTimeout(() => { setCollapseInfo([false, false]); }, 2000);
setCollapseInfo([false, false]);
setConclusion(replaced);
setstashConclusion(res);
// 给出conclusion结束的条件
if (stashConclusion.length + 5 > res.length) {
conclusionRender.current = true;
setProgressEnd(true);
}
};
// 渲染query
const renderQuery = (endCallback: () => void) => {
const queries = currentNode.actions[currentStep]?.args?.query;
setQuery(queries);
endCallback && endCallback();
};
const renderSteps = () => {
setCurrentNodeRendering(true);
const queryEndCallback = () => {
if (currentNode.actions[currentStep].result[0].content) {
if (currentNode.actions[currentStep].type === "BingBrowser.search" || currentNode.actions[currentStep].type === "BingBrowser") {
renderSearchList();
}
}
};
const thoughtEndCallback = () => {
if (currentNode.actions[currentStep]?.args?.query?.length) {
renderQuery(queryEndCallback);
} else {
queryEndCallback();
}
};
if (currentNode.actions[currentStep].thought) {
renderDraft(currentNode.actions[currentStep].thought, `stepDraft-${currentStep}`, thoughtEndCallback);
}
}
// 展开收起
const toggleCard = (index: number) => {
const arr = [...collapseInfo];
arr[index] = !arr[index];
setCollapseInfo(arr);
};
// 渲染过程中保持渲染文字可见
const keepScrollTop = (divA: any, divB: any) => {
// 获取 divB 的当前高度
const bHeight = divB.offsetHeight;
// 检查 divA 是否需要滚动(即 divB 的高度是否大于 divA 的可视高度)
if (bHeight > divA.offsetHeight) {
// 滚动到 divB 的底部在 divA 的可视区域内
divA.scrollTop = bHeight - divA.offsetHeight;
}
};
useEffect(() => {
setRenderData([
{
id: 0,
state: 3,
name: '原始问题',
children: adjList
}
])
}, [JSON.stringify(adjList)]);
useEffect(() => {
console.log('render data changed-----', renderData);
}, [renderData]);
useEffect(() => {
if (currentStep === 1) {
setCollapseInfo([false, true]);
}
}, [currentStep]);
useEffect(() => {
if (nodeOutputEnd && !localStorage.getItem('nodeRes')) {
// 如果节点输出结束了,但是response还没有结束,认为节点渲染已结束
conclusionRender.current = true;
setProgressEnd(true);
return;
}
if (nodeDraftRender.current && localStorage.getItem('nodeRes')) {
renderConclusion();
}
}, [localStorage.getItem('nodeRes'), nodeDraftRender.current, nodeOutputEnd]);
useEffect(() => {
if (obj?.response?.nodes[obj.current_node]?.detail?.state !== 1) {
setIsWaiting(true);
}
if (obj?.response?.nodes?.[obj.current_node].detail?.state === 0 && currentNode?.current_node === obj.current_node) {
console.log('node render end-----', obj);
setNodeEnd(true);
}
if (obj?.current_node && obj?.response?.state === 3) {
// 当node节点的数据可以开始渲染时,给currentnode赋值
// update conclusion
if (obj.response.nodes[obj.current_node]?.detail?.actions?.length === 2 &&
obj.response.nodes[obj.current_node]?.detail?.state === 1 &&
obj.response.nodes[obj.current_node]?.detail.response) {
window.localStorage.setItem('nodeRes', obj.response.nodes[obj.current_node]?.detail.response);
}
if (obj.current_node &&
(obj.response.nodes[obj.current_node]?.detail?.state === 1) &&
obj.response.nodes[obj.current_node]?.detail?.actions?.length &&
currentStep === 0 &&
currentNode?.current_node !== obj?.current_node
) {
// 更新当前渲染节点
console.log('update current node----');
setIsWaiting(false);
setCurrentNode({ ...obj.response.nodes[obj.current_node]?.detail, current_node: obj.current_node });
}
// 设置highlight
if (!selectedIds.length &&
obj.response.nodes[obj.current_node]?.detail?.actions?.[1]?.type === 'BingBrowser.select' &&
(obj.response.nodes[obj.current_node]?.detail?.state === 1)) {
setSelectedIds(obj.response.nodes[obj.current_node]?.detail?.actions?.[1]?.args?.select_ids || []);
setCurrentNode({ ...obj.response.nodes[obj.current_node]?.detail, current_node: obj.current_node });
}
}
}, [obj]);
useEffect(() => {
// 输出思考过程
if (!currentNode || currentNodeRendering) { return; }
renderSteps();
}, [currentNode, currentNodeRendering, selectedIds]);
useEffect(() => {
if (!hasHighlight.current && selectedIds.length && currentNode?.actions.length === 2) {
// 渲染高亮的search信息
highLightSearchList(selectedIds);
}
}, [selectedIds, currentNode]);
useEffect(() => {
// 当前节点渲染结束
if (nodeName && nodeName !== currentNode?.current_node && progressEnd && !isEnd) {
resetNode(nodeName);
setMapWidth(generateWidth());
}
}, [nodeName, currentNode, progressEnd, isEnd]);
let responseTimer: any = useRef(null);
useEffect(() => {
if (isEnd) {
responseTimer.current = setInterval(() => {
const divA = document.getElementById('chatArea') as HTMLDivElement;
const divB = document.getElementById('messageWindowId') as HTMLDivElement;
keepScrollTop(divA, divB);
if (chatIsOver) {
clearInterval(responseTimer.current);
}
}, 500);
setTimeout(() => {
setShowEndNode(true);
}, 300);
} else if (responseTimer.current) {
// 如果 isEnd 变为 false,清除定时器
clearInterval(responseTimer.current);
responseTimer.current = null;
}
// 返回清理函数,确保组件卸载时清除定时器
return () => {
if (responseTimer.current) {
clearInterval(responseTimer.current);
responseTimer.current = null;
}
};
}, [isEnd, chatIsOver]);
useEffect(() => {
setRenderData([]);
setResponse('');
setDraft('');
setIsEnd(false);
setShowRight(true);
window.localStorage.setItem('nodeRes', '');
window.localStorage.setItem('finishedNodes', '');
}, [question]);
const resetNode = (targetNode: string) => {
if (targetNode === 'response') return; // 如果开始response了,所有节点都渲染完了,不需要reset
// 渲染下一个节点前,初始化状态
const newData = findAndUpdateStatus(renderData, targetNode);
console.log('reset node------', targetNode, renderData);
setCurrentStep(0);
setQuery([]);
setSearchList([]);
setConclusion('');
setCollapseInfo([true, true]);
setProgress1('');
setProgress2('');
setProgressEnd(false);
setCurrentNode(null);
setCurrentNodeRendering(false);
setSelectedIds([]);
setNodeEnd(false);
hasHighlight.current = false;
nodeDraftRender.current = false;
conclusionRender.current = false;
window.localStorage.setItem('nodeRes', '');
};
const formatData = (data: any) => {
try {
setIsWaiting(false);
const obj = JSON.parse(data);
if (!obj.current_node && obj.response.state === 0) {
console.log('chat is over end-------');
setChatIsOver(true);
return;
}
if (!obj.current_node && obj.response.state === 9) {
setShowRight(false);
setIsEnd(true);
const replaced = replaceStr(obj.response.response);
setResponse(replaced);
return;
}
if (!obj.current_node && obj.response.state === 1 && !currentNode) {
// 有thought,没有node
setDraftEnd(false);
setDraft(obj.response.response);
}
if (!obj.current_node && (obj.response.state !== 1 || obj.response.state !== 0 || obj.response.state !== 9)) {
// 有thought,没有node, 不用处理渲染
//console.log('loading-------------', obj);
setDraftEnd(true);
setIsWaiting(true);
}
if (obj.current_node && obj.response.state === 3) {
setNodeName(obj.current_node);
// 有node
setObj(obj);
const newAdjList = obj.response?.adjacency_list;
if (newAdjList?.length > 0) {
setAdjList(newAdjList);
}
}
} catch (err) {
console.log('format error-----', err);
}
};
const startEventSource = () => {
if (!chatIsOver) {
message.warning('有对话进行中!');
return;
}
setQuestion(stashQuestion);
setChatIsOver(false);
const postData = {
inputs: [
{
role: 'user',
content: stashQuestion
}
]
}
const ctrl = new AbortController();
eventSource = fetchEventSource(GET_SSE_DATA, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData),
onmessage(ev) {
formatData(ev.data);
},
onerror(err) {
console.log('sse error------', err);
},
// signal: ctrl.signal,
});
};
const abortEventSource = () => {
if (eventSource) {
eventSource.close(); // 或使用其他方法关闭连接,具体取决于库的API
eventSource = null;
console.log('EventSource connection aborted due to timeout.');
message.error('连接中断,2s后即将刷新页面---');
setTimeout(() => {
location.reload();
}, 2000);
}
};
return <div className={styles.mainPage} style={!showRight ? { maxWidth: '1000px' } : {}}>
<div className={styles.chatContent}>
<div className={styles.top} id="chatArea">
<div id="messageWindowId">
{
question && <div className={styles.question}>
<span>{question}</span>
</div>
}
{
(draft || response || renderData?.length > 0) &&
<div className={styles.answer}>
{
renderData?.length > 0 ? <div className={styles.inner}>
<div className={styles.mapArea}>
<ul className={styles.mindmap} id="mindMap" style={isEnd ? { width: mapWidth, overflow: "hidden" } : {}}>
{renderData.map((item: any) => (
<MindMapItem key={item.name} item={item} isEnd={isEnd} />
))}
{showEndNode &&
<div className={styles.end} style={generateEndStyle()}>
<div className={styles.node}>
<article>最终回复</article>
</div>
</div>
}
</ul>
</div>
</div> : <></>
}
{
!response && <div className={styles.draft}>
{/* {!draftEnd && draft && <div className={styles.loading}>
<div></div>
<div></div>
<div></div>
</div>} */}
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{replaceStr(draft)}</ReactMarkdown>
</div>
}
{response && <div className={styles.response}>
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{response}</ReactMarkdown>
</div>}
</div>
}
</div>
</div>
<div className={styles.sendArea}>
<Input type="text" placeholder='说点什么吧~ Shift+Enter 换行 ; Enter 发送' onChange={(e) => { setStashQuestion(e.target.value) }}
onPressEnter={startEventSource} />
<button onClick={startEventSource}>
<img src={SendIcon} />
发送
</button>
</div>
<div className={styles.notice}>如果想要更丝滑的体验,请在本地搭建-<a href='https://github.com/InternLM/MindSearch' target='_blank'>MindSearch <IconFont type='icon-GithubFilled' /></a></div>
</div>
{showRight && <div className={styles.progressContent}>
{
currentNode && <>
<div className={styles.toggleIcon} onClick={toggleRight}>
<Tooltip placement="top" title="收起">
<img src={PackIcon} />
</Tooltip></div>
<div className={styles.titleNode}>{currentNode?.content || currentNode?.node}</div>
{
currentNode?.actions?.length ? <>
{
currentNode.actions.map((item: any, idx: number) => (
currentStep >= idx && <div className={classNames(
styles.steps,
item.type === "BingBrowser.search" ? styles.thinking : styles.select
)} key={`step-${idx}`}>
<div className={styles.title}>
<i></i>{item.type === "BingBrowser.search" ? "思考" : item.type === "BingBrowser.select" ? "信息来源" : "信息整合"}
<div className={styles.open} onClick={() => { toggleCard(idx) }}>
<IconFont type={collapseInfo[idx] ? "icon-shouqi" : "icon-xiangxiazhankai"} />
</div>
</div>
<div className={classNames(
styles.con,
!collapseInfo[idx] ? styles.collapsed : ""
)}>
{
item.type === "BingBrowser.search" && <div className={styles.thought}>
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{progress1}</ReactMarkdown>
</div>
}
{
item.type === "BingBrowser.search" && query.length > 0 && <div className={styles.query}>
<div className={styles.subTitle}><IconFont type="icon-SearchOutlined" />搜索关键词</div>
{
query.map((item, index) => (<div key={`query-item-${item}`} className={classNames(styles.queryItem, styles.fadeIn)}>
{item}
</div>))
}
</div>
}
{
currentStep === idx && searchList.length > 0 && <div className={styles.searchList}>
{item.type === "BingBrowser.search" && <div className={styles.subTitle}><IconFont type="icon-DocOutlined" />信息来源</div>}
{
item.type === "BingBrowser.select" && <div className={styles.thought}>
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{progress2}</ReactMarkdown>
</div>
}
<div className={styles.scrollCon} style={(searchList.length > 5 && currentStep === 0) ? { height: '300px' } : {}}>
<div className={styles.inner} style={(searchList.length > 5 && currentStep === 0) ? { position: 'absolute', bottom: 0, left: 0 } : {}}>
{
searchList.map((item: any, num: number) => (
<div className={classNames(
styles.searchItem,
item.highLight ? styles.highLight : ""
)} key={`search-item-${item.url}-${idx}`}>
<p className={styles.summ}>{item.id}. {item?.title}</p>
<p className={styles.url}>{item?.url}</p>
</div>
))
}
</div>
</div>
</div>
}
</div>
</div>
))
}
</> : <></>
}
</>
}
{
conclusion && <div className={styles.steps}>
<div className={styles.title}>
<i></i>信息整合
</div>
<div className={styles.conclusion}>
<ReactMarkdown rehypePlugins={[rehypeRaw]}>{conclusion}</ReactMarkdown>
</div>
</div>
}
{isWaiting && question && <div className={styles.loading99}></div>}
</div>}
{
!showRight && <div className={styles.showRight} onClick={toggleRight}>
<img src={ShowRightIcon} />
</div>
}
</div>
};
export default RenderTest;
|