/ src / renderer / store / chatbot.ts
chatbot.ts
  1  import { defineStore } from 'pinia'
  2  import { CHATBOT_DEFAULTS } from '@/renderer/types'
  3  import type { ChatbotConfig } from '@/types/llm'
  4  import { v4 as uuidv4 } from 'uuid'
  5  
  6  export interface ChatbotStoreState {
  7    chatbots: ChatbotConfig[]
  8    currentChatbotId: number // chatbots array index, being modified
  9    selectedChatbotId: number // chatbots array index, being selected
 10  }
 11  
 12  export function getLLMs(): ChatbotConfig[] {
 13    return window.llmApis?.get().custom || []
 14  }
 15  
 16  export function getDefaultLLM(): ChatbotConfig {
 17    return window.llmApis?.get().default || CHATBOT_DEFAULTS
 18  }
 19  
 20  export const useChatbotStore = defineStore('chatbotStore', {
 21    state: (): ChatbotStoreState => {
 22      const llms = getLLMs()
 23  
 24      const chatbots = llms
 25        ? llms.map((llm) => ({ ...getDefaultLLM(), ...llm }))
 26        : [{ ...getDefaultLLM(), name: 'Chatbot Default', mcp: true }]
 27  
 28      return {
 29        chatbots: chatbots,
 30        currentChatbotId: 0, // points to first chatbot by default
 31        selectedChatbotId: 0
 32      }
 33    },
 34  
 35    persist: {
 36      exclude: ['currentChatbotId']
 37    },
 38  
 39    actions: {
 40      resetState() {
 41        this.$reset()
 42      },
 43  
 44      updateChatbotConfig(index: number, patch: Partial<ChatbotConfig>) {
 45        if (index < 0 || index >= this.chatbots.length) {
 46          console.log('No chatbot found at index', index)
 47          return
 48        }
 49        Object.assign(this.chatbots[index], patch)
 50        if ((this.chatbots[index].apiCli, 'apiKey' in patch)) {
 51        }
 52      },
 53  
 54      batchChatbotApiKey(apiCli: string, apiKey: string) {
 55        this.chatbots.forEach((chatbot: ChatbotConfig) => {
 56          if (chatbot.apiCli.length > 0 && chatbot.apiCli === apiCli) {
 57            chatbot.apiKey = apiKey
 58          }
 59        })
 60      },
 61  
 62      addChatbot() {
 63        this.chatbots.push({ ...getDefaultLLM(), name: 'Chatbot ' + uuidv4() })
 64      },
 65  
 66      removeChatbot(index: number) {
 67        this.chatbots.splice(index, 1)
 68        // Adjust current index if needed
 69        if (this.currentChatbotId >= index) {
 70          this.currentChatbotId = Math.max(0, this.currentChatbotId - 1)
 71        }
 72      },
 73  
 74      updateStoreFromJSON(chatbotConfigJson: ChatbotConfig[] | ChatbotConfig) {
 75        this.$reset()
 76        this.chatbots = []
 77        if (Array.isArray(chatbotConfigJson)) {
 78          chatbotConfigJson.forEach((newChatbot, _index) => {
 79            this.chatbots.push({ ...getDefaultLLM(), ...newChatbot })
 80          })
 81        } else {
 82          // Handle case when chatbots is a single object
 83          this.chatbots.push({ ...getDefaultLLM(), ...chatbotConfigJson })
 84        }
 85      },
 86  
 87      // Helper method to find chatbot by name
 88      findChatbotIndexByName(name: string): number {
 89        return this.chatbots.findIndex((chatbot) => chatbot.name === name)
 90      },
 91  
 92      // Action to set current chatbot by name
 93      setCurrentChatbotByName(name: string) {
 94        const index = this.findChatbotIndexByName(name)
 95        if (index !== -1) {
 96          this.currentChatbotId = index
 97        }
 98      }
 99    },
100  
101    getters: {
102      currentConfig(state): ChatbotConfig | null {
103        if (state.currentChatbotId < 0 || state.currentChatbotId >= state.chatbots.length) {
104          return null
105        }
106        return state.chatbots[state.currentChatbotId]
107      },
108  
109      getChatbotByName: (state) => {
110        return (name: string) => state.chatbots.find((chatbot) => chatbot.name === name)
111      },
112  
113      getChatbotByIndex: (state) => {
114        return (index: number) => state.chatbots[index]
115      },
116  
117      // Get all chatbot names
118      chatbotNames(state): string[] {
119        return state.chatbots.map((chatbot) => chatbot.name)
120      }
121    }
122  })