feat: implement CustomAIMessageChunk class and enhance reasoning handling in useMessageOption hook
This commit is contained in:
@@ -36,7 +36,12 @@ import { humanMessageFormatter } from "@/utils/human-message"
|
||||
import { pageAssistEmbeddingModel } from "@/models/embedding"
|
||||
import { PAMemoryVectorStore } from "@/libs/PAMemoryVectorStore"
|
||||
import { getScreenshotFromCurrentTab } from "@/libs/get-screenshot"
|
||||
import { isReasoningEnded, isReasoningStarted, removeReasoning } from "@/libs/reasoning"
|
||||
import {
|
||||
isReasoningEnded,
|
||||
isReasoningStarted,
|
||||
mergeReasoningContent,
|
||||
removeReasoning
|
||||
} from "@/libs/reasoning"
|
||||
|
||||
export const useMessage = () => {
|
||||
const {
|
||||
@@ -413,7 +418,24 @@ export const useMessage = () => {
|
||||
let reasoningStartTime: Date | null = null
|
||||
let reasoningEndTime: Date | null = null
|
||||
let timetaken = 0
|
||||
let apiReasoning = false
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
@@ -618,7 +640,7 @@ export const useMessage = () => {
|
||||
const applicationChatHistory = []
|
||||
|
||||
const data = await getScreenshotFromCurrentTab()
|
||||
|
||||
|
||||
const visionImage = data?.screenshot || ""
|
||||
|
||||
if (visionImage === "") {
|
||||
@@ -680,7 +702,24 @@ export const useMessage = () => {
|
||||
let reasoningStartTime: Date | undefined = undefined
|
||||
let reasoningEndTime: Date | undefined = undefined
|
||||
let timetaken = 0
|
||||
let apiReasoning = false
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
@@ -950,8 +989,25 @@ export const useMessage = () => {
|
||||
let reasoningStartTime: Date | null = null
|
||||
let reasoningEndTime: Date | null = null
|
||||
let timetaken = 0
|
||||
let apiReasoning = false
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
@@ -1279,7 +1335,24 @@ export const useMessage = () => {
|
||||
let timetaken = 0
|
||||
let reasoningStartTime: Date | undefined = undefined
|
||||
let reasoningEndTime: Date | undefined = undefined
|
||||
let apiReasoning = false
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
@@ -1527,7 +1600,24 @@ export const useMessage = () => {
|
||||
let reasoningStartTime: Date | null = null
|
||||
let reasoningEndTime: Date | null = null
|
||||
let timetaken = 0
|
||||
let apiReasoning = false
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
|
||||
@@ -332,7 +332,24 @@ export const useMessageOption = () => {
|
||||
let count = 0
|
||||
let reasoningStartTime: Date | undefined = undefined
|
||||
let reasoningEndTime: Date | undefined = undefined
|
||||
let apiReasoning = false
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
@@ -649,19 +666,27 @@ export const useMessageOption = () => {
|
||||
let count = 0
|
||||
let reasoningStartTime: Date | null = null
|
||||
let reasoningEndTime: Date | null = null
|
||||
let apiReasoning: boolean = false
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
// console.log(chunk)
|
||||
// if (chunk?.reasoning_content) {
|
||||
// const reasoningContent = mergeReasoningContent(
|
||||
// fullText,
|
||||
// chunk?.reasoning_content || ""
|
||||
// )
|
||||
// contentToSave += reasoningContent
|
||||
// fullText += reasoningContent
|
||||
// }
|
||||
|
||||
if (isReasoningStarted(fullText) && !reasoningStartTime) {
|
||||
reasoningStartTime = new Date()
|
||||
@@ -992,8 +1017,25 @@ export const useMessageOption = () => {
|
||||
let count = 0
|
||||
let reasoningStartTime: Date | undefined = undefined
|
||||
let reasoningEndTime: Date | undefined = undefined
|
||||
let apiReasoning = false
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
if (chunk?.additional_kwargs?.reasoning_content) {
|
||||
const reasoningContent = mergeReasoningContent(
|
||||
fullText,
|
||||
chunk?.additional_kwargs?.reasoning_content || ""
|
||||
)
|
||||
contentToSave = reasoningContent
|
||||
fullText = reasoningContent
|
||||
apiReasoning = true
|
||||
} else {
|
||||
if (apiReasoning) {
|
||||
fullText += "</think>"
|
||||
contentToSave += "</think>"
|
||||
apiReasoning = false
|
||||
}
|
||||
}
|
||||
|
||||
contentToSave += chunk?.content
|
||||
fullText += chunk?.content
|
||||
if (count === 0) {
|
||||
|
||||
Reference in New Issue
Block a user