refactor: Implement event-driven streaming and enhance session management

- Removed the StreamEvent accumulation logic, transitioning to an append-only model for event streaming.
- Updated StreamManager interface to support appending events and retrieving events with offsets.
- Enhanced session handling to support real-time event emission through an EventBus, improving responsiveness.
- Refactored chat pipeline components to utilize the new event-driven architecture, ensuring seamless integration of agent responses and knowledge references.
- Improved frontend components to handle the new event structure, ensuring accurate rendering of agent interactions and responses.
This commit is contained in:
wizardchen
2025-11-07 22:41:41 +08:00
parent def8e4b426
commit 326a61696a
19 changed files with 1144 additions and 1496 deletions
@@ -29,6 +29,22 @@
</div>
</div>
<!-- Answer Event -->
<div v-else-if="event.type === 'answer'" class="answer-event">
<div
class="answer-content-wrapper"
:class="{
'answer-active': !event.done,
'answer-done': event.done
}"
>
<div v-if="event.content"
class="answer-content markdown-content"
v-html="renderMarkdown(event.content)">
</div>
</div>
</div>
<!-- Tool Call Event -->
<div v-else-if="event.type === 'tool_call'" class="tool-event">
<div
@@ -472,7 +488,7 @@ const formatJSON = (obj: any): string => {
cursor: pointer;
padding: 10px 14px;
background: #ffffff;
border: 1px solid #e5e7eb;
border-left: 3px solid #07c05f;
border-radius: 8px;
display: flex;
align-items: center;
@@ -480,18 +496,16 @@ const formatJSON = (obj: any): string => {
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
user-select: none;
margin-bottom: 8px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
animation: slideInDown 0.3s ease-out;
&:hover {
background: #fafafa;
border-color: #07c05f;
background: rgba(7, 192, 95, 0.04);
box-shadow: 0 2px 6px rgba(7, 192, 95, 0.12);
transform: translateY(-1px);
}
&:active {
transform: translateY(0);
background: rgba(7, 192, 95, 0.08);
}
.collapse-icon {
@@ -502,7 +516,7 @@ const formatJSON = (obj: any): string => {
}
.collapse-text {
font-size: 13px;
font-size: 14px;
color: #333333;
font-weight: 500;
letter-spacing: -0.01em;
@@ -622,6 +636,111 @@ const formatJSON = (obj: any): string => {
}
}
// Answer Event - 类似 thinking 但有独特样式
.answer-event {
animation: fadeInUp 0.3s ease-out;
.answer-content-wrapper {
background: #ffffff;
border-radius: 8px;
padding: 1px 14px;
border-left: 3px solid #07c05f;
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
// 进行中的 answer 显示动画
&.answer-active {
animation: pulseBorder 2s ease-in-out infinite;
}
// 完成的 answer 保持绿色边框
&.answer-done {
border-left-color: #07c05f;
}
}
.answer-content {
font-size: 14px;
color: #333333;
line-height: 1.6;
&.markdown-content {
:deep(p) {
margin: 8px 0;
line-height: 1.6;
}
:deep(code) {
background: #f0f0f0;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 12px;
}
:deep(pre) {
background: #f5f5f5;
padding: 12px;
border-radius: 4px;
overflow-x: auto;
margin: 8px 0;
code {
background: none;
padding: 0;
}
}
:deep(ul), :deep(ol) {
margin: 8px 0;
padding-left: 24px;
}
:deep(li) {
margin: 4px 0;
}
:deep(blockquote) {
border-left: 3px solid #07c05f;
padding-left: 12px;
margin: 8px 0;
color: #666;
}
:deep(h1), :deep(h2), :deep(h3), :deep(h4), :deep(h5), :deep(h6) {
margin: 12px 0 8px 0;
font-weight: 600;
color: #333;
}
:deep(a) {
color: #07c05f;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
:deep(table) {
border-collapse: collapse;
margin: 8px 0;
font-size: 12px;
th, td {
border: 1px solid #e5e7eb;
padding: 6px 10px;
}
th {
background: #f5f5f5;
font-weight: 600;
}
}
}
}
}
// Tool Event
.tool-event {
animation: fadeInUp 0.3s ease-out;
+110 -14
View File
@@ -5,13 +5,14 @@
<AgentStreamDisplay :session="session" v-if="session.isAgentMode"></AgentStreamDisplay>
<deepThink :deepSession="session" v-if="session.showThink && !session.isAgentMode"></deepThink>
</div>
<div ref="parentMd" v-if="!session.hideContent">
<!-- 非 Agent 模式下才显示传统的 markdown 渲染 -->
<div ref="parentMd" v-if="!session.hideContent && !session.isAgentMode">
<!-- 消息正在总结中则渲染加载gif -->
<img v-if="session.thinking" class="botanswer_laoding_gif" src="@/assets/img/botanswer_loading.gif"
alt="正在总结答案……">
<div v-for="(item, index) in processedMarkdown" :key="index">
<img class="ai-markdown-img" @click="preview(item)" v-if="isLink(item)" :src="item" alt="">
<div v-else class="ai-markdown-template" v-html="processMarkdown(item)"></div>
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
<div class="content-wrapper">
<div class="ai-markdown-template markdown-content" v-html="processMarkdown(content || session.content)"></div>
</div>
<div v-if="isImgLoading" class="img_loading"><t-loading size="small"></t-loading><span>加载中...</span></div>
</div>
@@ -61,6 +62,7 @@ const preview = (url) => {
}
const removeImg = () => {
nextTick(() => {
if (!parentMd.value) return;
const images = parentMd.value.querySelectorAll('img.ai-markdown-img');
if (images) {
images.forEach(async item => {
@@ -184,19 +186,113 @@ onMounted(async () => {
<style lang="less" scoped>
@import '../../../components/css/markdown.less';
:deep(.ai-markdown-template) {
contain: content;
line-height: 28px;
letter-spacing: 1px;
// 内容包装器 - 与 thinking 样式一致
.content-wrapper {
background: #ffffff;
border-radius: 8px;
padding: 1px 14px;
border-left: 3px solid #07c05f;
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
animation: fadeInUp 0.3s ease-out;
}
h1,
h2,
h3,
h4 {
line-height: 14px;
font-size: 16px;
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.ai-markdown-template {
font-size: 14px;
color: #333333;
line-height: 1.6;
}
.markdown-content {
:deep(p) {
margin: 8px 0;
line-height: 1.6;
}
:deep(code) {
background: #f0f0f0;
padding: 2px 6px;
border-radius: 3px;
font-family: 'Monaco', 'Courier New', monospace;
font-size: 12px;
}
:deep(pre) {
background: #f5f5f5;
padding: 12px;
border-radius: 4px;
overflow-x: auto;
margin: 8px 0;
code {
background: none;
padding: 0;
}
}
:deep(ul), :deep(ol) {
margin: 8px 0;
padding-left: 24px;
}
:deep(li) {
margin: 4px 0;
}
:deep(blockquote) {
border-left: 3px solid #07c05f;
padding-left: 12px;
margin: 8px 0;
color: #666;
}
:deep(h1), :deep(h2), :deep(h3), :deep(h4), :deep(h5), :deep(h6) {
margin: 12px 0 8px 0;
font-weight: 600;
color: #333;
}
:deep(a) {
color: #07c05f;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
:deep(table) {
border-collapse: collapse;
margin: 8px 0;
font-size: 12px;
width: 100%;
th, td {
border: 1px solid #e5e7eb;
padding: 6px 10px;
text-align: left;
}
th {
background: #f5f5f5;
font-weight: 600;
}
tbody tr:nth-child(even) {
background: #fafafa;
}
}
}
.ai-markdown-img {
+25 -10
View File
@@ -60,17 +60,22 @@ const safeProcessContent = (content) => {
flex-direction: column;
font-size: 14px;
width: 100%;
border-radius: 2px;
background-color: #30323605;
border-radius: 8px;
background-color: #ffffff;
border-left: 3px solid #07c05f;
box-shadow: 0 2px 4px rgba(7, 192, 95, 0.08);
overflow: hidden;
box-sizing: border-box;
transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
margin-bottom: 8px;
.refer_header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 4px;
color: #00000099;
padding: 10px 14px;
color: #333333;
font-weight: 500;
.refer_title {
display: flex;
@@ -81,29 +86,31 @@ const safeProcessContent = (content) => {
height: 16px;
color: #07c05f;
fill: currentColor;
margin-right: 6px;
margin-right: 8px;
}
span {
white-space: nowrap;
font-size: 14px;
}
}
.refer_show_icon {
font-size: 14px;
padding: 0 2px 1px 2px;
color: #07c05f;
}
}
.refer_header:hover {
border-radius: 2px;
background-color: #30323605;
background-color: rgba(7, 192, 95, 0.04);
cursor: pointer;
}
.refer_box {
padding: 2px 4px 4px 4px;
padding: 4px 14px 10px 14px;
flex-direction: column;
border-top: 1px solid #f0f0f0;
}
}
@@ -119,8 +126,8 @@ const safeProcessContent = (content) => {
}
.doc {
text-decoration: underline;
color: #366ef4;
text-decoration: none;
color: #07c05f;
cursor: pointer;
display: inline-block;
white-space: nowrap;
@@ -128,6 +135,14 @@ const safeProcessContent = (content) => {
overflow: hidden;
text-overflow: ellipsis;
line-height: 20px;
padding: 2px 0;
font-weight: 500;
transition: all 0.2s ease;
border-bottom: 1px solid transparent;
&:hover {
border-bottom-color: #07c05f;
}
}
</style>
+14 -11
View File
@@ -110,13 +110,11 @@ const getmsgList = (data, isScrollType = false, scrollHeight) => {
// Reconstruct agentEventStream from agent_steps stored in database
// This allows the frontend to restore the exact conversation state including all agent reasoning steps
const reconstructEventStreamFromSteps = (agentSteps) => {
if (!agentSteps || !Array.isArray(agentSteps) || agentSteps.length === 0) {
return [];
}
const reconstructEventStreamFromSteps = (agentSteps, messageContent) => {
const events = [];
// Process agent steps if they exist
if (agentSteps && Array.isArray(agentSteps) && agentSteps.length > 0) {
agentSteps.forEach((step) => {
// Add thinking event if thought content exists
if (step.thought && step.thought.trim()) {
@@ -148,12 +146,14 @@ const reconstructEventStreamFromSteps = (agentSteps) => {
});
}
});
}
// 添加一个完成标记的 answer 事件,触发折叠逻辑
if (events.length > 0) {
// 总是添加 answer 事件如果有内容(无论是否有 agent_steps)
// 这样可以确保最终答案始终被渲染
if (messageContent && messageContent.trim()) {
events.push({
type: 'answer',
content: '', // 空内容,因为最终答案会在 message.content 中
content: messageContent,
done: true
});
}
@@ -174,8 +174,8 @@ const handleMsgList = async (data, isScrollType = false, newScrollHeight) => {
if (item.agent_steps && Array.isArray(item.agent_steps) && item.agent_steps.length > 0) {
console.log('[Message Load] Reconstructing agent steps for message:', item.id, 'steps:', item.agent_steps.length);
item.isAgentMode = true;
item.agentEventStream = reconstructEventStreamFromSteps(item.agent_steps);
// 隐藏最终答案内容,用户可以通过展开步骤查看完整过程
item.agentEventStream = reconstructEventStreamFromSteps(item.agent_steps, item.content);
// 隐藏最终答案内容,因为它已经包含在 agentEventStream 的 answer 事件中
item.hideContent = true;
console.log('[Message Load] Reconstructed', item.agentEventStream.length, 'events from agent steps');
}
@@ -530,7 +530,10 @@ const handleAgentChunk = (data) => {
case 'references':
// 知识引用
if (data.knowledge_references) {
if (data.data?.references) {
message.knowledge_references = data.data.references;
} else if (data.knowledge_references) {
// 兼容旧格式
message.knowledge_references = data.knowledge_references;
}
break;