refactor(iod): 重构数联网相关组件和逻辑

-优化了 Data、Scene 和 Team组件的逻辑,使用 currentIodMessage 替代复杂的条件判断- 改进了 IodRelevant 组件的动画和数据处理方式
- 调整了 Message 组件以支持数联网搜索功能
- 重构了 PlaygroundIodProvider,简化了上下文类型和数据处理
- 更新了数据库相关操作,使用新的 HistoryMessage 类型
- 新增了 IodDb 类来管理数联网连接配置
This commit is contained in:
zhaoweijie
2025-08-24 19:00:49 +08:00
parent f9763778fa
commit 2b4885ae2d
20 changed files with 415 additions and 399 deletions

View File

@@ -1,4 +1,4 @@
import { saveHistory, saveMessage } from "@/db"
import { HistoryMessage, saveHistory, saveMessage } from "@/db"
import {
setLastUsedChatModel,
setLastUsedChatSystemPrompt
@@ -23,7 +23,9 @@ export const saveMessageOnError = async ({
message_source = "web-ui",
message_type,
prompt_content,
prompt_id
prompt_id,
iodSearch,
webSearch,
}: {
e: any
setHistory: (history: ChatHistory) => void
@@ -38,7 +40,9 @@ export const saveMessageOnError = async ({
message_source?: "copilot" | "web-ui"
message_type?: string
prompt_id?: string
prompt_content?: string
prompt_content?: string,
iodSearch?: boolean,
webSearch?: boolean,
}) => {
if (
e?.name === "AbortError" ||
@@ -59,31 +63,31 @@ export const saveMessageOnError = async ({
}
])
const defaultMessage: HistoryMessage = {
history_id: historyId,
name: selectedModel,
role: "assistant",
content: botMessage,
webSources: [],
iodSources: getDefaultIodSources(),
messageType: message_type,
iodSearch,
webSearch,
images: []
}
if (historyId) {
if (!isRegenerating) {
await saveMessage(
historyId,
selectedModel,
"user",
userMessage,
[image],
[],
getDefaultIodSources(),
1,
message_type
)
await saveMessage({
...JSON.parse(JSON.stringify(defaultMessage)),
role: "user",
content: userMessage,
images: [image]
})
}
await saveMessage(
historyId,
selectedModel,
"assistant",
botMessage,
[],
[],
getDefaultIodSources(),
2,
message_type
)
await saveMessage({
...JSON.parse(JSON.stringify(defaultMessage))
})
await setLastUsedChatModel(historyId, selectedModel)
if (prompt_id || prompt_content) {
await setLastUsedChatSystemPrompt(historyId, {
@@ -95,28 +99,19 @@ export const saveMessageOnError = async ({
const title = await generateTitle(selectedModel, userMessage, userMessage)
const newHistoryId = await saveHistory(title, false, message_source)
if (!isRegenerating) {
await saveMessage(
newHistoryId.id,
selectedModel,
"user",
userMessage,
[image],
[],
getDefaultIodSources(),
1,
message_type
)
await saveMessage({
...JSON.parse(JSON.stringify(defaultMessage)),
history_id: newHistoryId.id,
content: userMessage,
role: "user",
images: [image]
})
}
await saveMessage(
newHistoryId.id,
selectedModel,
"assistant",
botMessage,
[],
[],
getDefaultIodSources(),
2,
message_type
{
...JSON.parse(JSON.stringify(defaultMessage)),
history_id: newHistoryId.id,
},
)
setHistoryId(newHistoryId.id)
await setLastUsedChatModel(newHistoryId.id, selectedModel)
@@ -142,6 +137,8 @@ export const saveMessageOnSuccess = async ({
message,
image,
fullText,
iodSearch,
webSearch,
webSources,
iodSources,
message_source = "web-ui",
@@ -158,6 +155,8 @@ export const saveMessageOnSuccess = async ({
message: string
image: string
fullText: string
iodSearch?: boolean
webSearch?: boolean
webSources: any[]
iodSources: AllIodRegistryEntry
message_source?: "copilot" | "web-ui"
@@ -168,34 +167,38 @@ export const saveMessageOnSuccess = async ({
reasoning_time_taken?: number
}) => {
var botMessage
const defaultMessage: HistoryMessage = {
history_id: historyId,
name: selectedModel,
role: "assistant",
content: fullText,
webSources: webSources,
iodSources: iodSources,
messageType: message_type,
images: [],
iodSearch,
webSearch,
generationInfo,
reasoning_time_taken,
}
if (historyId) {
if (!isRegenerate) {
await saveMessage(
historyId,
selectedModel,
"user",
message,
[image],
[],
getDefaultIodSources(),
1,
message_type,
generationInfo,
reasoning_time_taken
{
...JSON.parse(JSON.stringify(defaultMessage)),
role: "user",
content: message,
images: [image],
webSources: [],
iodSources: getDefaultIodSources(),
},
)
}
botMessage = await saveMessage(
historyId,
selectedModel!,
"assistant",
fullText,
[],
webSources,
iodSources,
2,
message_type,
generationInfo,
reasoning_time_taken
{
...JSON.parse(JSON.stringify(defaultMessage)),
}
)
updateDialog(historyId, botMessage)
await setLastUsedChatModel(historyId, selectedModel!)
@@ -209,30 +212,21 @@ export const saveMessageOnSuccess = async ({
const title = await generateTitle(selectedModel, message, message)
const newHistoryId = await saveHistory(title, false, message_source)
await saveMessage(
newHistoryId.id,
selectedModel,
"user",
message,
[image],
[],
getDefaultIodSources(),
1,
message_type,
generationInfo,
reasoning_time_taken
{
...JSON.parse(JSON.stringify(defaultMessage)),
history_id: newHistoryId.id,
role: "user",
content: message,
images: [image],
webSources: [],
iodSources: getDefaultIodSources(),
},
)
botMessage = await saveMessage(
newHistoryId.id,
selectedModel!,
"assistant",
fullText,
[],
webSources,
iodSources,
2,
message_type,
generationInfo,
reasoning_time_taken
{
...JSON.parse(JSON.stringify(defaultMessage)),
history_id: newHistoryId.id,
}
)
updateDialog(newHistoryId.id, botMessage)
setHistoryId(newHistoryId.id)

View File

@@ -2,12 +2,12 @@ import React from "react"
import { cleanUrl } from "~/libs/clean-url"
import {
defaultEmbeddingModelForRag,
geWebSearchFollowUpPrompt,
getOllamaURL,
geWebSearchFollowUpPrompt,
promptForRag,
systemPromptForNonRag
} from "~/services/ollama"
import { useStoreMessageOption, type Message } from "~/store/option"
import { useStoreMessageOption } from "~/store/option"
import { useStoreMessage } from "~/store"
import { SystemMessage } from "@langchain/core/messages"
import { getDataFromCurrentTab } from "~/libs/get-html"
@@ -43,6 +43,7 @@ import {
} from "@/libs/reasoning"
import { AllIodRegistryEntry } from "@/types/iod.ts"
import { getDefaultIodSources } from "@/libs/iod.ts"
import { Message } from "@/types/message.ts"
export const useMessage = () => {
const {

View File

@@ -7,7 +7,7 @@ import {
promptForRag,
systemPromptForNonRagOption
} from "~/services/ollama"
import type { ChatHistory, Message, MeteringEntry } from "~/store/option"
import type { ChatHistory, MeteringEntry } from "~/store/option"
import { useStoreMessageOption } from "~/store/option"
import { SystemMessage } from "@langchain/core/messages"
import {
@@ -47,6 +47,7 @@ import {
removeReasoning
} from "@/libs/reasoning"
import { getDefaultIodSources } from "@/libs/iod.ts"
import type { Message } from "@/types/message.ts"
export const useMessageOption = () => {
const {
@@ -214,36 +215,39 @@ export const useMessageOption = () => {
data: meter
})
let defaultMessage: Message = {
isBot: true,
name: selectedModel,
message,
iodSearch,
webSearch,
webSources: [],
iodSources: getDefaultIodSources(),
images: [image]
}
if (!isRegenerate) {
newMessage = [
...messages,
{
...JSON.parse(JSON.stringify(defaultMessage)),
id: generateID(),
isBot: false,
name: "You",
message,
webSources: [],
iodSources: getDefaultIodSources(),
images: [image]
},
{
isBot: true,
name: selectedModel,
...JSON.parse(JSON.stringify(defaultMessage)),
id: generateMessageId,
message: "",
webSources: [],
iodSources: getDefaultIodSources(),
id: generateMessageId
}
]
} else {
newMessage = [
...messages,
{
isBot: true,
name: selectedModel,
message: "",
webSources: [],
iodSources: getDefaultIodSources(),
id: generateMessageId
...JSON.parse(JSON.stringify(defaultMessage)),
id: generateMessageId,
message: " ",
}
]
}
@@ -525,6 +529,8 @@ export const useMessageOption = () => {
message,
image,
fullText,
iodSearch,
webSearch,
webSources,
iodSources,
generationInfo,
@@ -566,7 +572,9 @@ export const useMessageOption = () => {
setHistory,
setHistoryId,
userMessage: message,
isRegenerating: isRegenerate
isRegenerating: isRegenerate,
iodSearch,
webSearch,
})
if (!errorSave) {
@@ -672,6 +680,7 @@ export const useMessageOption = () => {
let newMessage: Message[] = []
let generateMessageId = generateID()
setCurrentMessageId(generateMessageId)
const meter: MeteringEntry = {
id: generateMessageId,
queryContent: message,
@@ -682,7 +691,6 @@ export const useMessageOption = () => {
loading: true,
data: meter
})
if (!isRegenerate) {
newMessage = [
...messages,
@@ -690,6 +698,7 @@ export const useMessageOption = () => {
isBot: false,
name: "You",
message,
id: generateID(),
webSources: [],
iodSources: getDefaultIodSources(),
images: [image]
@@ -889,7 +898,6 @@ export const useMessageOption = () => {
content: fullText
}
])
await saveMessageOnSuccess({
historyId,
setHistoryId,
@@ -898,6 +906,8 @@ export const useMessageOption = () => {
message,
image,
fullText,
iodSearch,
webSearch,
source: [],
generationInfo,
prompt_content: promptContent,
@@ -914,8 +924,8 @@ export const useMessageOption = () => {
const { cot, content } = responseResolver(fullText)
const currentMeteringEntry = {
...meter,
modelInputTokenCount: prompt.length,
modelOutputTokenCount: fullText.length,
modelInputTokenCount: prompt? prompt.length : 0,
modelOutputTokenCount: fullText? fullText.length : 0,
model: ollama.modelName ?? ollama.model,
relatedDataCount: 0,
timeTaken: new Date().getTime() - chatStartTime.getTime(),
@@ -943,7 +953,9 @@ export const useMessageOption = () => {
userMessage: message,
isRegenerating: isRegenerate,
prompt_content: promptContent,
prompt_id: promptId
prompt_id: promptId,
iodSearch,
webSearch,
})
if (!errorSave) {
@@ -1274,7 +1286,9 @@ export const useMessageOption = () => {
fullText,
source,
generationInfo,
reasoning_time_taken: timetaken
reasoning_time_taken: timetaken,
iodSearch,
webSearch,
})
setIsProcessing(false)
@@ -1290,7 +1304,9 @@ export const useMessageOption = () => {
setHistory,
setHistoryId,
userMessage: message,
isRegenerating: isRegenerate
isRegenerating: isRegenerate,
iodSearch,
webSearch,
})
if (!errorSave) {