feat: 拼接多条assistant消息
This commit is contained in:
@@ -221,6 +221,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
buffer += decoder.decode(value, { stream: true })
|
buffer += decoder.decode(value, { stream: true })
|
||||||
|
console.log('收到chunk',new Date().getTime());
|
||||||
|
|
||||||
// 优先按空行拆分事件块(SSE标准)
|
// 优先按空行拆分事件块(SSE标准)
|
||||||
let events = buffer.split(/\n\n/)
|
let events = buffer.split(/\n\n/)
|
||||||
@@ -261,7 +262,7 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const jsonData = JSON.parse(jsonText)
|
const jsonData = JSON.parse(jsonText)
|
||||||
console.log('jsonData', jsonData)
|
// console.log('jsonData', jsonData)
|
||||||
|
|
||||||
// 赋值 project_id 和 version_id
|
// 赋值 project_id 和 version_id
|
||||||
// if (jsonData.project_id) params.projectID = jsonData.project_id
|
// if (jsonData.project_id) params.projectID = jsonData.project_id
|
||||||
@@ -361,6 +362,67 @@
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理对话列表,将连续的 assistant 消息合并为一条
|
||||||
|
const processDialogue = (dialogue, startIndex, existingImgList) => {
|
||||||
|
if (!dialogue || dialogue.length === 0) return []
|
||||||
|
|
||||||
|
const result = []
|
||||||
|
let i = startIndex
|
||||||
|
|
||||||
|
while (i < dialogue.length) {
|
||||||
|
const item = dialogue[i]
|
||||||
|
|
||||||
|
if (item.image_url) {
|
||||||
|
existingImgList.push(item.image_url)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.role === 'user') {
|
||||||
|
// user 角色直接添加
|
||||||
|
result.push({
|
||||||
|
...item,
|
||||||
|
text: item.content,
|
||||||
|
isUser: true,
|
||||||
|
id: result.length + 1
|
||||||
|
})
|
||||||
|
i++
|
||||||
|
} else if (item.role === 'assistant') {
|
||||||
|
// assistant 角色,拼接直到下一个 user
|
||||||
|
let combinedContent = item.content || ''
|
||||||
|
|
||||||
|
// 继续往后找连续的 assistant 消息
|
||||||
|
let j = i + 1
|
||||||
|
while (j < dialogue.length && dialogue[j].role === 'assistant') {
|
||||||
|
if (dialogue[j].image_url) {
|
||||||
|
existingImgList.push(dialogue[j].image_url)
|
||||||
|
}
|
||||||
|
combinedContent += dialogue[j].content || ''
|
||||||
|
j++
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push({
|
||||||
|
...item,
|
||||||
|
content: combinedContent,
|
||||||
|
text: combinedContent,
|
||||||
|
isUser: false,
|
||||||
|
id: result.length + 1
|
||||||
|
})
|
||||||
|
|
||||||
|
i = j
|
||||||
|
} else {
|
||||||
|
// 其他角色直接添加
|
||||||
|
result.push({
|
||||||
|
...item,
|
||||||
|
text: item.content,
|
||||||
|
isUser: item.role === 'user',
|
||||||
|
id: result.length + 1
|
||||||
|
})
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
const setChatInfo = (info) => {
|
const setChatInfo = (info) => {
|
||||||
const initialData = agentStore.getInitialProjectData
|
const initialData = agentStore.getInitialProjectData
|
||||||
if (isGenerating.value || initialData) return
|
if (isGenerating.value || initialData) return
|
||||||
@@ -389,35 +451,22 @@
|
|||||||
|
|
||||||
const imgList = []
|
const imgList = []
|
||||||
const ancestorsList = []
|
const ancestorsList = []
|
||||||
|
let ancestorsIdCounter = 1
|
||||||
if (ancestors) {
|
if (ancestors) {
|
||||||
ancestors.forEach((item) => {
|
ancestors.forEach((item) => {
|
||||||
const list =
|
const list = processDialogue(item.dialogue, 0, imgList)
|
||||||
item.dialogue?.map((el, index) => {
|
// 重新设置 id
|
||||||
if (el.image_url) {
|
list.forEach((el) => {
|
||||||
imgList.push(el.image_url)
|
el.id = ancestorsIdCounter++
|
||||||
}
|
})
|
||||||
return {
|
|
||||||
...el,
|
|
||||||
text: el.content,
|
|
||||||
isUser: el.role === 'user',
|
|
||||||
id: index + 1
|
|
||||||
}
|
|
||||||
}) || []
|
|
||||||
ancestorsList.push(...list)
|
ancestorsList.push(...list)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
const currentList =
|
const currentList = processDialogue(current?.dialogue, 0, imgList)
|
||||||
current?.dialogue?.map((item, index) => {
|
// 重新设置 id
|
||||||
if (item.image_url) {
|
currentList.forEach((el, index) => {
|
||||||
imgList.push(item.image_url)
|
el.id = index + 1 + ancestorsList.length
|
||||||
}
|
})
|
||||||
return {
|
|
||||||
...item,
|
|
||||||
text: item.content,
|
|
||||||
isUser: item.role === 'user',
|
|
||||||
id: index + 1 + ancestorsList.length
|
|
||||||
}
|
|
||||||
}) || []
|
|
||||||
|
|
||||||
// 延迟设置新数据,确保 UI 有时间响应清空操作
|
// 延迟设置新数据,确保 UI 有时间响应清空操作
|
||||||
nextTick(() => {
|
nextTick(() => {
|
||||||
|
|||||||
@@ -7,10 +7,7 @@
|
|||||||
<div class="thumb">
|
<div class="thumb">
|
||||||
<img :src="content.isUser ? userThumb : agentThumb" class="thumb-icon" />
|
<img :src="content.isUser ? userThumb : agentThumb" class="thumb-icon" />
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div class="message-context" v-show="!content.loading">
|
||||||
class="message-context"
|
|
||||||
v-show="!content.loading && !content.thinking && !content.streaming"
|
|
||||||
>
|
|
||||||
<div class="img-list flex" v-if="imageList.length > 0">
|
<div class="img-list flex" v-if="imageList.length > 0">
|
||||||
<img
|
<img
|
||||||
v-for="(item, index) in imageList"
|
v-for="(item, index) in imageList"
|
||||||
|
|||||||
@@ -517,8 +517,8 @@
|
|||||||
const settingPopupVisible = ref(false)
|
const settingPopupVisible = ref(false)
|
||||||
const settingOptions = ref([
|
const settingOptions = ref([
|
||||||
{ label: 'Input.settingOptions.creativity', value: 50 },
|
{ label: 'Input.settingOptions.creativity', value: 50 },
|
||||||
{ label: 'Input.settingOptions.diversity', value: 75 },
|
{ label: 'Input.settingOptions.diversity', value: 50 },
|
||||||
{ label: 'Input.settingOptions.relevance', value: 60 }
|
{ label: 'Input.settingOptions.relevance', value: 50 }
|
||||||
])
|
])
|
||||||
|
|
||||||
const openStylePopup = () => {
|
const openStylePopup = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user