11import { Settings } from "@/types/common"
22import { defineStore } from "pinia"
3+ import LlmService from "@/background/services/llm"
4+ import { debounce } from "@/utils/common"
35
46interface Tokens {
57 google ?: string
@@ -37,6 +39,7 @@ export const useSettingsStore = defineStore("settings", () => {
3739 email : "" ,
3840 isTosAgreed : false ,
3941 llmApiKey : "" ,
42+ llmApiKeyIsValid : null
4043 } ,
4144 )
4245
@@ -68,8 +71,6 @@ export const useSettingsStore = defineStore("settings", () => {
6871 tokens . value . google = token
6972 }
7073
71-
72-
7374 function clearTokens ( ) {
7475 tokens . value = { }
7576 }
@@ -78,9 +79,20 @@ export const useSettingsStore = defineStore("settings", () => {
7879 settings . value . isTosAgreed = true
7980 }
8081
81- function setLlmApiKey ( apiKey : string ) {
82+ const validateAndSetLlmApiKey = debounce ( async ( apiKey : string ) => {
83+ if ( ! apiKey ) {
84+ settings . value . llmApiKey = ""
85+ settings . value . llmApiKeyIsValid = null
86+ return
87+ }
8288 settings . value . llmApiKey = apiKey
83- }
89+ const isValid = await LlmService . validateApiKey ( apiKey )
90+ if ( isValid ) {
91+ settings . value . llmApiKeyIsValid = true
92+ } else {
93+ settings . value . llmApiKeyIsValid = false
94+ }
95+ } , 300 )
8496
8597 function setDummyValuesForLocalDev ( ) {
8698 tokens . value = {
@@ -102,7 +114,7 @@ export const useSettingsStore = defineStore("settings", () => {
102114 setGoogleToken,
103115 clearTokens,
104116 tosAgreed,
105- setLlmApiKey ,
117+ validateAndSetLlmApiKey ,
106118 setDummyValuesForLocalDev,
107119 displayActionIcon : computed ( ( ) => settings . value . displayActionIcon ) ,
108120 activeProfileId : computed ( ( ) => settings . value . activeProfileId ) ,
@@ -116,6 +128,7 @@ export const useSettingsStore = defineStore("settings", () => {
116128 googleToken : computed ( ( ) => tokens . value . google ) ,
117129 isTosAgreed : computed ( ( ) => settings . value . isTosAgreed ) ,
118130 llmApiKey : computed ( ( ) => settings . value . llmApiKey ) ,
131+ llmApiKeyIsValid : computed ( ( ) => settings . value . llmApiKeyIsValid ) ,
119132 profiles : computed ( ( ) => settings . value . profiles ) ,
120133 }
121134} )
0 commit comments