/ src / renderer / store / history.ts
history.ts
 1  import { defineStore } from 'pinia'
 2  import localForage from 'localforage'
 3  import { v4 as uuidv4 } from 'uuid'
 4  import { useMessageStore } from '@/renderer/store/message'
 5  
 6  import type { ChatConversationMessage } from '@/renderer/types/message'
 7  import type { SessionId, SessionEntry } from '@/renderer/types/session'
 8  
 9  interface HistoryStoreState {
10    // selected: SessionId[] | undefined
11    conversations: SessionEntry[]
12  }
13  
14  export const useHistoryStore = defineStore('historyStore', {
15    state: (): HistoryStoreState => ({
16      // selected: undefined as SessionId[] | undefined,
17      conversations: [] as SessionEntry[]
18    }),
19    persist: {
20      include: ['conversations'],
21      storage: localForage
22    },
23    getters: {},
24    actions: {
25      getDate() {
26        const date = new Date().toLocaleString('zh', { timeZoneName: 'short', hour12: false })
27        return `${date} ${uuidv4()}`
28      },
29      resetState() {
30        this.$reset()
31      },
32      deleteById(index: number) {
33        this.conversations.splice(index, 1)
34      },
35      init(conversation: SessionEntry) {
36        const oldConversation = this.find(conversation.id)
37        console.log(conversation)
38        if (!oldConversation) {
39          const newId = this.getDate()
40          this.conversations.unshift({
41            ...conversation,
42            id: newId
43          })
44          // this.selected = [newId]
45          return this.conversations[0]
46        } else {
47          return oldConversation
48        }
49      },
50      find(id: string | undefined) {
51        if (id) {
52          return this.conversations.find((item) => item.id === id)
53        } else {
54          return undefined
55        }
56      },
57      select(sessionId: SessionId) {
58        const conversation = this.find(sessionId)
59        if (conversation) {
60          const messageStore = useMessageStore()
61          messageStore.setConversation(conversation)
62        }
63      },
64      getColor(index: number) {
65        const targetElement = this.conversations[index]?.messages.find(
66          (element) => element.role === 'assistant'
67        )
68        if (targetElement) {
69          return 'primary'
70        } else {
71          return 'grey'
72        }
73      },
74      downloadById(index: number) {
75        const name = this.conversations[index].id.replace(/[/: ]/g, '-')
76        this.download(this.conversations[index].messages, `history-${name}.json`)
77      },
78      downloadHistory() {
79        this.download(this.conversations, 'history.json')
80      },
81      download(json: ChatConversationMessage[] | SessionEntry[], filename: string) {
82        const blob = new Blob([JSON.stringify(json, null, 2)], { type: 'application/json' })
83        const url = URL.createObjectURL(blob)
84        const a = document.createElement('a')
85        a.href = url
86        a.download = filename
87        a.click()
88        URL.revokeObjectURL(url)
89      }
90    }
91  })