前端页面展示内容修改,带totaltoken
This commit is contained in:
@@ -2,19 +2,36 @@ import { ss } from '@/utils/storage'
|
||||
|
||||
const LOCAL_NAME = 'chatStorage'
|
||||
|
||||
export function emptyUsage(): Chat.TokenUsage {
|
||||
return {
|
||||
prompt_tokens: 0,
|
||||
completion_tokens: 0,
|
||||
total_tokens: 0,
|
||||
}
|
||||
}
|
||||
|
||||
export function defaultState(): Chat.ChatState {
|
||||
const uuid = 1002
|
||||
return {
|
||||
active: uuid,
|
||||
usingContext: true,
|
||||
history: [{ uuid, title: 'New Chat', isEdit: false }],
|
||||
chat: [{ uuid, data: [] }],
|
||||
chat: [{ uuid, data: [], sessionUsage: emptyUsage() }],
|
||||
}
|
||||
}
|
||||
|
||||
export function getLocalState(): Chat.ChatState {
|
||||
const localState = ss.get(LOCAL_NAME)
|
||||
return { ...defaultState(), ...localState }
|
||||
const state = { ...defaultState(), ...localState }
|
||||
|
||||
return {
|
||||
...state,
|
||||
chat: (state.chat || []).map((item: Partial<Chat.Session>) => ({
|
||||
uuid: item.uuid || Date.now(),
|
||||
data: item.data || [],
|
||||
sessionUsage: { ...emptyUsage(), ...(item.sessionUsage || {}) },
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
export function setLocalState(state: Chat.ChatState) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { getLocalState, setLocalState } from './helper'
|
||||
import { emptyUsage, getLocalState, setLocalState } from './helper'
|
||||
import { router } from '@/router'
|
||||
|
||||
export const useChatStore = defineStore('chat-store', {
|
||||
@@ -20,6 +20,14 @@ export const useChatStore = defineStore('chat-store', {
|
||||
return state.chat.find(item => item.uuid === state.active)?.data ?? []
|
||||
}
|
||||
},
|
||||
|
||||
getSessionUsageByUuid(state: Chat.ChatState) {
|
||||
return (uuid?: number) => {
|
||||
if (uuid)
|
||||
return state.chat.find(item => item.uuid === uuid)?.sessionUsage ?? emptyUsage()
|
||||
return state.chat.find(item => item.uuid === state.active)?.sessionUsage ?? emptyUsage()
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
@@ -30,7 +38,7 @@ export const useChatStore = defineStore('chat-store', {
|
||||
|
||||
addHistory(history: Chat.History, chatData: Chat.Chat[] = []) {
|
||||
this.history.unshift(history)
|
||||
this.chat.unshift({ uuid: history.uuid, data: chatData })
|
||||
this.chat.unshift({ uuid: history.uuid, data: chatData, sessionUsage: emptyUsage() })
|
||||
this.active = history.uuid
|
||||
this.reloadRoute(history.uuid)
|
||||
},
|
||||
@@ -97,7 +105,7 @@ export const useChatStore = defineStore('chat-store', {
|
||||
if (this.history.length === 0) {
|
||||
const uuid = Date.now()
|
||||
this.history.push({ uuid, title: chat.text, isEdit: false })
|
||||
this.chat.push({ uuid, data: [chat] })
|
||||
this.chat.push({ uuid, data: [chat], sessionUsage: emptyUsage() })
|
||||
this.active = uuid
|
||||
this.recordState()
|
||||
}
|
||||
@@ -182,6 +190,32 @@ export const useChatStore = defineStore('chat-store', {
|
||||
}
|
||||
},
|
||||
|
||||
addSessionUsageByUuid(uuid: number, usage: Partial<Chat.TokenUsage>) {
|
||||
const sessionUsage = { ...emptyUsage(), ...usage }
|
||||
|
||||
if (!uuid || uuid === 0) {
|
||||
if (this.chat.length) {
|
||||
this.chat[0].sessionUsage = {
|
||||
prompt_tokens: this.chat[0].sessionUsage.prompt_tokens + sessionUsage.prompt_tokens,
|
||||
completion_tokens: this.chat[0].sessionUsage.completion_tokens + sessionUsage.completion_tokens,
|
||||
total_tokens: this.chat[0].sessionUsage.total_tokens + sessionUsage.total_tokens,
|
||||
}
|
||||
this.recordState()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const index = this.chat.findIndex(item => item.uuid === uuid)
|
||||
if (index !== -1) {
|
||||
this.chat[index].sessionUsage = {
|
||||
prompt_tokens: this.chat[index].sessionUsage.prompt_tokens + sessionUsage.prompt_tokens,
|
||||
completion_tokens: this.chat[index].sessionUsage.completion_tokens + sessionUsage.completion_tokens,
|
||||
total_tokens: this.chat[index].sessionUsage.total_tokens + sessionUsage.total_tokens,
|
||||
}
|
||||
this.recordState()
|
||||
}
|
||||
},
|
||||
|
||||
async reloadRoute(uuid?: number) {
|
||||
this.recordState()
|
||||
await router.push({ name: 'Chat', params: { uuid } })
|
||||
|
||||
Reference in New Issue
Block a user