feat: OpenAI settings page
Add a new settings page for OpenAI configuration, including a dedicated tab in the settings layout, translations, and routing.
This commit is contained in:
146
src/db/openai.ts
Normal file
146
src/db/openai.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
type OpenAIModelConfig = {
|
||||
id: string
|
||||
name: string
|
||||
baseUrl: string
|
||||
apiKey?: string
|
||||
createdAt: number
|
||||
}
|
||||
export const generateID = () => {
|
||||
return "openai-xxxx-xxx-xxxx".replace(/[x]/g, () => {
|
||||
const r = Math.floor(Math.random() * 16)
|
||||
return r.toString(16)
|
||||
})
|
||||
}
|
||||
|
||||
export class OpenAIModelDb {
|
||||
db: chrome.storage.StorageArea
|
||||
|
||||
|
||||
constructor() {
|
||||
this.db = chrome.storage.local
|
||||
}
|
||||
|
||||
|
||||
getAll = async (): Promise<OpenAIModelConfig[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(null, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
const data = Object.keys(result).map((key) => result[key])
|
||||
resolve(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
create = async (config: OpenAIModelConfig): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.set({ [config.id]: config }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
getById = async (id: string): Promise<OpenAIModelConfig> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve(result[id])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
update = async (config: OpenAIModelConfig): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.set({ [config.id]: config }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
delete = async (id: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.remove(id, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export const addOpenAICofig = async ({ name, baseUrl, apiKey }: { name: string, baseUrl: string, apiKey: string }) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const id = generateID()
|
||||
const config: OpenAIModelConfig = {
|
||||
id,
|
||||
name,
|
||||
baseUrl,
|
||||
apiKey,
|
||||
createdAt: Date.now()
|
||||
}
|
||||
await openaiDb.create(config)
|
||||
return id
|
||||
}
|
||||
|
||||
|
||||
export const getAllOpenAIConfig = async () => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const configs = await openaiDb.getAll()
|
||||
return configs
|
||||
}
|
||||
|
||||
export const updateOpenAIConfig = async ({ id, name, baseUrl, apiKey }: { id: string, name: string, baseUrl: string, apiKey: string }) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const config: OpenAIModelConfig = {
|
||||
id,
|
||||
name,
|
||||
baseUrl,
|
||||
apiKey,
|
||||
createdAt: Date.now()
|
||||
}
|
||||
|
||||
await openaiDb.update(config)
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
|
||||
export const deleteOpenAIConfig = async (id: string) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
await openaiDb.delete(id)
|
||||
}
|
||||
|
||||
|
||||
export const updateOpenAIConfigApiKey = async (id: string, { name, baseUrl, apiKey }: { name: string, baseUrl: string, apiKey: string }) => {
|
||||
const openaiDb = new OpenAIModelDb()
|
||||
const config: OpenAIModelConfig = {
|
||||
id,
|
||||
name,
|
||||
baseUrl,
|
||||
apiKey,
|
||||
createdAt: Date.now()
|
||||
}
|
||||
|
||||
await openaiDb.update(config)
|
||||
}
|
||||
Reference in New Issue
Block a user