feat: improve copilot (#2758)

This commit is contained in:
Himself65
2023-06-13 10:29:04 +08:00
committed by GitHub
parent 5ba2dff008
commit ace3c37fcc
20 changed files with 413 additions and 79 deletions
@@ -23,25 +23,42 @@ interface ChatMessageDBV1 extends DBSchema {
};
}
interface ChatMessageDBV2 extends ChatMessageDBV1 {
followingUp: {
key: string;
value: {
/**
* ID of the chat
*/
id: string;
question: string[];
};
};
}
export const conversationHistoryDBName = 'affine-copilot-chat';
export class IndexedDBChatMessageHistory extends BaseChatMessageHistory {
public id: string;
private messages: BaseChatMessage[] = [];
private readonly dbPromise: Promise<IDBPDatabase<ChatMessageDBV1>>;
private readonly dbPromise: Promise<IDBPDatabase<ChatMessageDBV2>>;
private readonly initPromise: Promise<void>;
constructor(id: string) {
super();
this.id = id;
this.messages = [];
this.dbPromise = openDB<ChatMessageDBV1>('affine-copilot-chat', 1, {
this.dbPromise = openDB<ChatMessageDBV2>('affine-copilot-chat', 2, {
upgrade(database, oldVersion) {
if (oldVersion === 0) {
database.createObjectStore('chat', {
keyPath: 'id',
});
} else if (oldVersion === 1) {
database.createObjectStore('followingUp', {
keyPath: 'id',
});
}
},
});
@@ -70,6 +87,31 @@ export class IndexedDBChatMessageHistory extends BaseChatMessageHistory {
});
}
public async saveFollowingUp(question: string[]): Promise<void> {
await this.initPromise;
const db = await this.dbPromise;
const t = db
.transaction('followingUp', 'readwrite')
.objectStore('followingUp');
await t.put({
id: this.id,
question,
});
}
public async getFollowingUp(): Promise<string[]> {
await this.initPromise;
const db = await this.dbPromise;
const t = db
.transaction('followingUp', 'readonly')
.objectStore('followingUp');
const chat = await t.get(this.id);
if (chat != null) {
return chat.question;
}
return [];
}
protected async addMessage(message: BaseChatMessage): Promise<void> {
await this.initPromise;
this.messages.push(message);
@@ -104,6 +146,6 @@ export class IndexedDBChatMessageHistory extends BaseChatMessageHistory {
}
async getMessages(): Promise<BaseChatMessage[]> {
return await this.initPromise.then(() => this.messages);
return this.initPromise.then(() => this.messages);
}
}