feat: use artifact id in urls, store metadata in history (#15)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { ChatHistory } from './useChatHistory';
|
||||
import type { Message } from 'ai';
|
||||
import type { ChatHistory } from './useChatHistory';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
|
||||
const logger = createScopedLogger('ChatHistory');
|
||||
@@ -15,6 +15,7 @@ export async function openDatabase(): Promise<IDBDatabase | undefined> {
|
||||
if (!db.objectStoreNames.contains('chats')) {
|
||||
const store = db.createObjectStore('chats', { keyPath: 'id' });
|
||||
store.createIndex('id', 'id', { unique: true });
|
||||
store.createIndex('urlId', 'urlId', { unique: true });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -29,7 +30,13 @@ export async function openDatabase(): Promise<IDBDatabase | undefined> {
|
||||
});
|
||||
}
|
||||
|
||||
export async function setMessages(db: IDBDatabase, id: string, messages: Message[]): Promise<void> {
|
||||
export async function setMessages(
|
||||
db: IDBDatabase,
|
||||
id: string,
|
||||
messages: Message[],
|
||||
urlId?: string,
|
||||
description?: string,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readwrite');
|
||||
const store = transaction.objectStore('chats');
|
||||
@@ -37,6 +44,8 @@ export async function setMessages(db: IDBDatabase, id: string, messages: Message
|
||||
const request = store.put({
|
||||
id,
|
||||
messages,
|
||||
urlId,
|
||||
description,
|
||||
});
|
||||
|
||||
request.onsuccess = () => resolve();
|
||||
@@ -45,6 +54,22 @@ export async function setMessages(db: IDBDatabase, id: string, messages: Message
|
||||
}
|
||||
|
||||
export async function getMessages(db: IDBDatabase, id: string): Promise<ChatHistory> {
|
||||
return (await getMessagesById(db, id)) || (await getMessagesByUrlId(db, id));
|
||||
}
|
||||
|
||||
export async function getMessagesByUrlId(db: IDBDatabase, id: string): Promise<ChatHistory> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readonly');
|
||||
const store = transaction.objectStore('chats');
|
||||
const index = store.index('urlId');
|
||||
const request = index.get(id);
|
||||
|
||||
request.onsuccess = () => resolve(request.result as ChatHistory);
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getMessagesById(db: IDBDatabase, id: string): Promise<ChatHistory> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readonly');
|
||||
const store = transaction.objectStore('chats');
|
||||
@@ -55,7 +80,7 @@ export async function getMessages(db: IDBDatabase, id: string): Promise<ChatHist
|
||||
});
|
||||
}
|
||||
|
||||
export async function getNextID(db: IDBDatabase): Promise<string> {
|
||||
export async function getNextId(db: IDBDatabase): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readonly');
|
||||
const store = transaction.objectStore('chats');
|
||||
@@ -65,3 +90,44 @@ export async function getNextID(db: IDBDatabase): Promise<string> {
|
||||
request.onerror = () => reject(request.error);
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUrlId(db: IDBDatabase, id: string): Promise<string> {
|
||||
const idList = await getUrlIds(db);
|
||||
|
||||
if (!idList.includes(id)) {
|
||||
return id;
|
||||
} else {
|
||||
let i = 2;
|
||||
|
||||
while (idList.includes(`${id}-${i}`)) {
|
||||
i++;
|
||||
}
|
||||
|
||||
return `${id}-${i}`;
|
||||
}
|
||||
}
|
||||
|
||||
async function getUrlIds(db: IDBDatabase): Promise<string[]> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readonly');
|
||||
const store = transaction.objectStore('chats');
|
||||
const idList: string[] = [];
|
||||
|
||||
const request = store.openCursor();
|
||||
|
||||
request.onsuccess = (event: Event) => {
|
||||
const cursor = (event.target as IDBRequest<IDBCursorWithValue>).result;
|
||||
|
||||
if (cursor) {
|
||||
idList.push(cursor.value.urlId);
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(idList);
|
||||
}
|
||||
};
|
||||
|
||||
request.onerror = () => {
|
||||
reject(request.error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user