|
|
@ -1,10 +1,12 @@ |
|
|
|
import { defineStore } from "pinia" |
|
|
|
import { db } from "./db.js" |
|
|
|
import { t } from "@/i18n/index.ts" |
|
|
|
import { useAssistantStore } from "./assistant.ts"; |
|
|
|
import { useModelStore } from "@/stores/model"; |
|
|
|
import { ref } from "vue"; |
|
|
|
export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
import { defineStore } from 'pinia' |
|
|
|
import { db } from './db.js' |
|
|
|
import { t } from '@/i18n/index.ts' |
|
|
|
import { useAssistantStore } from './assistant.ts' |
|
|
|
import { useModelStore } from '@/stores/model' |
|
|
|
import { ref } from 'vue' |
|
|
|
export const useAiChatStore = defineStore( |
|
|
|
'aichat', |
|
|
|
() => { |
|
|
|
const modelStore = useModelStore() |
|
|
|
const promptStore = useAssistantStore() |
|
|
|
const activeId: any = ref(0) |
|
|
@ -17,36 +19,69 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
const promptList: any = ref([]) |
|
|
|
const searchInput: any = ref('') |
|
|
|
const showInfo = ref(false) |
|
|
|
const editInfo: any = ref({}); //编辑聊天信息
|
|
|
|
const isEditor = ref(true); |
|
|
|
const editInfo: any = ref({}) //编辑聊天信息
|
|
|
|
const isEditor = ref(true) |
|
|
|
|
|
|
|
const newChat = async () => { |
|
|
|
const newChat = async (knowledgeId: any) => { |
|
|
|
const currentModel = await modelStore.getModel('chat') |
|
|
|
if (!currentModel) { |
|
|
|
return false |
|
|
|
} |
|
|
|
const promptData = await promptStore.getPrompt('chat') |
|
|
|
return await addChat(t('aichat.newchat'), currentModel, promptData, "") |
|
|
|
if (knowledgeId) { |
|
|
|
return await addChat( |
|
|
|
'知识库对话' + knowledgeId, |
|
|
|
currentModel, |
|
|
|
promptData, |
|
|
|
knowledgeId |
|
|
|
) |
|
|
|
} |
|
|
|
return await addChat( |
|
|
|
t('aichat.newchat'), |
|
|
|
currentModel, |
|
|
|
promptData, |
|
|
|
knowledgeId |
|
|
|
) |
|
|
|
} |
|
|
|
const getPrompt = async (type: string) => { |
|
|
|
return await promptStore.getPrompt(type) |
|
|
|
} |
|
|
|
const initChat = async () => { |
|
|
|
const initChat = async (knowledgeId: any) => { |
|
|
|
if (knowledgeId) { |
|
|
|
const chat = await db.getByField( |
|
|
|
'aichatlist', |
|
|
|
'knowledgeId', |
|
|
|
knowledgeId |
|
|
|
) |
|
|
|
if (chat.length) { |
|
|
|
activeId.value = chat[0].id |
|
|
|
} else { |
|
|
|
await newChat(knowledgeId) |
|
|
|
} |
|
|
|
} |
|
|
|
if (activeId.value === 0) { |
|
|
|
await newChat() |
|
|
|
await newChat(knowledgeId) |
|
|
|
} |
|
|
|
modelList.value = await modelStore.getModelAction('chat') |
|
|
|
const promptRes = await promptStore.getPrompts('chat') |
|
|
|
promptList.value = promptRes.list |
|
|
|
chatList.value = await db.getAll('aichatlist') |
|
|
|
if (activeId.value > 0) { |
|
|
|
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) |
|
|
|
messageList.value = await db.getByField( |
|
|
|
'aichatmsg', |
|
|
|
'chatId', |
|
|
|
activeId.value |
|
|
|
) |
|
|
|
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
|
|
|
} |
|
|
|
} |
|
|
|
const getActiveChat = async () => { |
|
|
|
chatInfo.value = await db.getOne('aichatlist', activeId.value) |
|
|
|
messageList.value = await db.getByField('aichatmsg', 'chatId', activeId.value) |
|
|
|
messageList.value = await db.getByField( |
|
|
|
'aichatmsg', |
|
|
|
'chatId', |
|
|
|
activeId.value |
|
|
|
) |
|
|
|
chatList.value = await db.getAll('aichatlist') |
|
|
|
return { chatInfo, messageList, chatList } |
|
|
|
} |
|
|
@ -56,7 +91,12 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
return list |
|
|
|
} |
|
|
|
// 添加聊天
|
|
|
|
async function addChat(title: string, modelData: any, promptData: any, knowledgeId: string) { |
|
|
|
async function addChat( |
|
|
|
title: string, |
|
|
|
modelData: any, |
|
|
|
promptData: any, |
|
|
|
knowledgeId: string |
|
|
|
) { |
|
|
|
const newChat = { |
|
|
|
title, |
|
|
|
prompt: promptData.prompt, |
|
|
@ -64,12 +104,11 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
model: modelData.model, |
|
|
|
engine: modelData.info.engine, |
|
|
|
createdAt: Date.now(), |
|
|
|
knowledgeId |
|
|
|
knowledgeId: knowledgeId || 0, |
|
|
|
} |
|
|
|
//console.log(newChat)
|
|
|
|
activeId.value = await db.addOne('aichatlist', newChat) |
|
|
|
return activeId.value |
|
|
|
|
|
|
|
} |
|
|
|
async function setActiveId(newId: number) { |
|
|
|
activeId.value = newId |
|
|
@ -81,15 +120,14 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
await db.delete('aichatlist', chatId) |
|
|
|
await db.deleteByField('aichatmsg', 'chatId', chatId) |
|
|
|
//如果删除的id是当前id
|
|
|
|
let id; |
|
|
|
let id |
|
|
|
if (chatId == activeId.value) { |
|
|
|
//
|
|
|
|
const list = await db.getAll('aichatlist') |
|
|
|
if (list.length > 0) { |
|
|
|
id = list[0]['id'] |
|
|
|
|
|
|
|
} else { |
|
|
|
id = await newChat() |
|
|
|
id = await newChat(0) |
|
|
|
} |
|
|
|
setActiveId(id) |
|
|
|
} |
|
|
@ -107,7 +145,6 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
await db.clear('aichatmsg') |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 新增历史消息
|
|
|
|
async function addMessages(chatId: number, message: any) { |
|
|
|
const currentChat = await db.getOne('aichatlist', chatId) |
|
|
@ -136,7 +173,6 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
await db.deleteByField('aichatmsg', 'chatId', activeId.value) |
|
|
|
messageList.value = [] |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// 更新聊天配置
|
|
|
@ -145,19 +181,19 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
return await db.update('aichatlist', chatId, config) |
|
|
|
} |
|
|
|
const showBox = (flag: any) => { |
|
|
|
isEditor.value = flag; |
|
|
|
isEditor.value = flag |
|
|
|
if (flag === true) { |
|
|
|
editInfo.value = toRaw(chatInfo.value); |
|
|
|
editInfo.value = toRaw(chatInfo.value) |
|
|
|
} else { |
|
|
|
editInfo.value = { |
|
|
|
title: "", |
|
|
|
model: "", |
|
|
|
prompt: "", |
|
|
|
promptId: "", |
|
|
|
}; |
|
|
|
} |
|
|
|
showInfo.value = true; |
|
|
|
}; |
|
|
|
title: '', |
|
|
|
model: '', |
|
|
|
prompt: '', |
|
|
|
promptId: '', |
|
|
|
} |
|
|
|
} |
|
|
|
showInfo.value = true |
|
|
|
} |
|
|
|
return { |
|
|
|
activeId, |
|
|
|
chatList, |
|
|
@ -184,19 +220,18 @@ export const useAiChatStore = defineStore('aichat', () => { |
|
|
|
getChatHistory, |
|
|
|
clearChatHistory, |
|
|
|
updateChat, |
|
|
|
showBox |
|
|
|
showBox, |
|
|
|
} |
|
|
|
|
|
|
|
}, { |
|
|
|
}, |
|
|
|
{ |
|
|
|
persist: { |
|
|
|
enabled: true, |
|
|
|
strategies: [ |
|
|
|
{ |
|
|
|
storage: localStorage, |
|
|
|
paths: [ |
|
|
|
"activeId" |
|
|
|
] |
|
|
|
paths: ['activeId'], |
|
|
|
}, // name 字段用localstorage存储
|
|
|
|
], |
|
|
|
}, |
|
|
|
} |
|
|
|
}) |
|
|
|
) |
|
|
|