chore: Update .gitignore and add .idea folder, update npm dependencies, and improve code logic for chat history and knowledge export/import
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
type ChatHistory as ChatHistoryType,
|
||||
type Message as MessageType
|
||||
} from "~/store/option"
|
||||
import { Storage, browser } from "wxt/browser"
|
||||
|
||||
type HistoryInfo = {
|
||||
id: string
|
||||
@@ -57,15 +58,18 @@ type ChatHistory = HistoryInfo[]
|
||||
type Prompts = Prompt[]
|
||||
|
||||
export class PageAssitDatabase {
|
||||
db: chrome.storage.StorageArea
|
||||
db: Storage.LocalStorageArea
|
||||
|
||||
constructor() {
|
||||
this.db = chrome.storage.local
|
||||
this.db = browser.storage.local
|
||||
}
|
||||
|
||||
async getChatHistory(id: string): Promise<MessageHistory> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
// this.db.get(id, (result) => {
|
||||
// resolve(result[id] || [])
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
resolve(result[id] || [])
|
||||
})
|
||||
})
|
||||
@@ -73,7 +77,10 @@ export class PageAssitDatabase {
|
||||
|
||||
async getChatHistories(): Promise<ChatHistory> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get("chatHistories", (result) => {
|
||||
// this.db.get("chatHistories", (result) => {
|
||||
// resolve(result.chatHistories || [])
|
||||
// })
|
||||
this.db.get("chatHistories").then((result) => {
|
||||
resolve(result.chatHistories || [])
|
||||
})
|
||||
})
|
||||
@@ -126,7 +133,10 @@ export class PageAssitDatabase {
|
||||
|
||||
async getAllPrompts(): Promise<Prompts> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get("prompts", (result) => {
|
||||
// this.db.get("prompts", (result) => {
|
||||
// resolve(result.prompts || [])
|
||||
// })
|
||||
this.db.get("prompts").then((result) => {
|
||||
resolve(result.prompts || [])
|
||||
})
|
||||
})
|
||||
@@ -169,7 +179,10 @@ export class PageAssitDatabase {
|
||||
|
||||
async getWebshare(id: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
// this.db.get(id, (result) => {
|
||||
// resolve(result[id] || [])
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
resolve(result[id] || [])
|
||||
})
|
||||
})
|
||||
@@ -177,7 +190,10 @@ export class PageAssitDatabase {
|
||||
|
||||
async getAllWebshares(): Promise<Webshare[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get("webshares", (result) => {
|
||||
// this.db.get("webshares", (result) => {
|
||||
// resolve(result.webshares || [])
|
||||
// })
|
||||
this.db.get("webshares").then((result) => {
|
||||
resolve(result.webshares || [])
|
||||
})
|
||||
})
|
||||
@@ -197,7 +213,10 @@ export class PageAssitDatabase {
|
||||
|
||||
async getUserID() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get("user_id", (result) => {
|
||||
// this.db.get("user_id", (result) => {
|
||||
// resolve(result.user_id || "")
|
||||
// })
|
||||
this.db.get("user_id").then((result) => {
|
||||
resolve(result.user_id || "")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Storage, browser } from "wxt/browser"
|
||||
import { deleteVector, deleteVectorByFileId } from "./vector"
|
||||
|
||||
export type Source = {
|
||||
@@ -24,89 +25,105 @@ export const generateID = () => {
|
||||
})
|
||||
}
|
||||
export class PageAssistKnowledge {
|
||||
db: chrome.storage.StorageArea
|
||||
db: Storage.LocalStorageArea
|
||||
|
||||
constructor() {
|
||||
this.db = chrome.storage.local
|
||||
this.db = browser.storage.local
|
||||
}
|
||||
|
||||
getAll = async (): Promise<Knowledge[]> => {
|
||||
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)
|
||||
}
|
||||
// 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)
|
||||
// }
|
||||
// })
|
||||
this.db.get(null).then((result) => {
|
||||
const data = Object.keys(result).map((key) => result[key])
|
||||
resolve(data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
getById = async (id: string): Promise<Knowledge> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve(result[id])
|
||||
}
|
||||
this.db.get(id).then((result) => {
|
||||
resolve(result[id])
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
create = async (knowledge: Knowledge): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.set({ [knowledge.id]: knowledge }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
// this.db.set({ [knowledge.id]: knowledge }, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
this.db.set({ [knowledge.id]: knowledge }).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
update = async (knowledge: Knowledge): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.set({ [knowledge.id]: knowledge }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
// this.db.set({ [knowledge.id]: knowledge }, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
this.db.set({ [knowledge.id]: knowledge }).then(() => {
|
||||
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()
|
||||
}
|
||||
// this.db.remove(id, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
this.db.remove(id).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
deleteSource = async (id: string, source_id: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
const data = result[id] as Knowledge
|
||||
data.source = data.source.filter((s) => s.source_id !== source_id)
|
||||
this.db.set({ [id]: data }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
// this.db.get(id, (result) => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// const data = result[id] as Knowledge
|
||||
// data.source = data.source.filter((s) => s.source_id !== source_id)
|
||||
// this.db.set({ [id]: data }, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
const data = result[id] as Knowledge
|
||||
data.source = data.source.filter((s) => s.source_id !== source_id)
|
||||
this.db.set({ [id]: data }).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
176
src/db/vector.ts
176
src/db/vector.ts
@@ -1,3 +1,5 @@
|
||||
import { Storage, browser } from "wxt/browser"
|
||||
|
||||
interface PageAssistVector {
|
||||
file_id: string
|
||||
content: string
|
||||
@@ -11,10 +13,10 @@ export type VectorData = {
|
||||
}
|
||||
|
||||
export class PageAssistVectorDb {
|
||||
db: chrome.storage.StorageArea
|
||||
db: Storage.LocalStorageArea
|
||||
|
||||
constructor() {
|
||||
this.db = chrome.storage.local
|
||||
this.db = browser.storage.local
|
||||
}
|
||||
|
||||
insertVector = async (
|
||||
@@ -22,36 +24,55 @@ export class PageAssistVectorDb {
|
||||
vector: PageAssistVector[]
|
||||
): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
// this.db.get(id, (result) => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// const data = result[id] as VectorData
|
||||
// if (!data) {
|
||||
// this.db.set({ [id]: { id, vectors: vector } }, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// } else {
|
||||
// this.db.set(
|
||||
// {
|
||||
// [id]: {
|
||||
// ...data,
|
||||
// vectors: data.vectors.concat(vector)
|
||||
// }
|
||||
// },
|
||||
// () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// }
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
const data = result[id] as VectorData
|
||||
if (!data) {
|
||||
this.db.set({ [id]: { id, vectors: vector } }).then(() => {
|
||||
resolve()
|
||||
})
|
||||
} else {
|
||||
const data = result[id] as VectorData
|
||||
if (!data) {
|
||||
this.db.set({ [id]: { id, vectors: vector } }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
this.db
|
||||
.set({
|
||||
[id]: {
|
||||
...data,
|
||||
vectors: data.vectors.concat(vector)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.db.set(
|
||||
{
|
||||
[id]: {
|
||||
...data,
|
||||
vectors: data.vectors.concat(vector)
|
||||
}
|
||||
},
|
||||
() => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
.then(() => {
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -59,56 +80,72 @@ export class PageAssistVectorDb {
|
||||
|
||||
deleteVector = async (id: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.remove(id, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
// this.db.remove(id, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
this.db.remove(id).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
deleteVectorByFileId = async (id: string, file_id: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
const data = result[id] as VectorData
|
||||
data.vectors = data.vectors.filter((v) => v.file_id !== file_id)
|
||||
this.db.set({ [id]: data }, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
}
|
||||
// this.db.get(id, (result) => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// const data = result[id] as VectorData
|
||||
// data.vectors = data.vectors.filter((v) => v.file_id !== file_id)
|
||||
// this.db.set({ [id]: data }, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
const data = result[id] as VectorData
|
||||
data.vectors = data.vectors.filter((v) => v.file_id !== file_id)
|
||||
this.db.set({ [id]: data }).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
getVector = async (id: string): Promise<VectorData> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(id, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve(result[id] as VectorData)
|
||||
}
|
||||
// this.db.get(id, (result) => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve(result[id] as VectorData)
|
||||
// }
|
||||
// })
|
||||
this.db.get(id).then((result) => {
|
||||
resolve(result[id] as VectorData)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
getAll = async (): Promise<VectorData[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.get(null, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve(Object.values(result))
|
||||
}
|
||||
// this.db.get(null, (result) => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve(Object.values(result))
|
||||
// }
|
||||
// })
|
||||
this.db.get(null).then((result) => {
|
||||
resolve(Object.values(result))
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -119,12 +156,15 @@ export class PageAssistVectorDb {
|
||||
data.forEach((d) => {
|
||||
obj[d.id] = d
|
||||
})
|
||||
this.db.set(obj, () => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError)
|
||||
} else {
|
||||
resolve()
|
||||
}
|
||||
// this.db.set(obj, () => {
|
||||
// if (chrome.runtime.lastError) {
|
||||
// reject(chrome.runtime.lastError)
|
||||
// } else {
|
||||
// resolve()
|
||||
// }
|
||||
// })
|
||||
this.db.set(obj).then(() => {
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -164,5 +204,5 @@ export const exportVectors = async () => {
|
||||
|
||||
export const importVectors = async (data: VectorData[]) => {
|
||||
const db = new PageAssistVectorDb()
|
||||
return db.saveImportedData(data)
|
||||
return db.saveImportedData(data)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user