|
| 1 | +import type { UIMessage } from "ai"; |
| 2 | + |
| 3 | +const DB_NAME = "flanksource-ai-chat"; |
| 4 | +const DB_VERSION = 1; |
| 5 | +const ACTIVE_STORE = "ai_active_conversation"; |
| 6 | +const ACTIVE_KEY = "active"; |
| 7 | + |
| 8 | +type ActiveConversationRecord = { |
| 9 | + key: string; |
| 10 | + conversationId: string; |
| 11 | + messages: UIMessage[]; |
| 12 | + updatedAt: number; |
| 13 | +}; |
| 14 | + |
| 15 | +export type ActiveAIConversation = { |
| 16 | + conversationId: string; |
| 17 | + messages: UIMessage[]; |
| 18 | + updatedAt: number; |
| 19 | +}; |
| 20 | + |
| 21 | +let databasePromise: Promise<IDBDatabase | null> | null = null; |
| 22 | + |
| 23 | +function isIndexedDBAvailable() { |
| 24 | + return ( |
| 25 | + typeof window !== "undefined" && typeof window.indexedDB !== "undefined" |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function requestToPromise<T>(request: IDBRequest<T>): Promise<T> { |
| 30 | + return new Promise((resolve, reject) => { |
| 31 | + request.onsuccess = () => resolve(request.result); |
| 32 | + request.onerror = () => { |
| 33 | + reject(request.error ?? new Error("IndexedDB request failed")); |
| 34 | + }; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function transactionDone(transaction: IDBTransaction): Promise<void> { |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + transaction.oncomplete = () => resolve(); |
| 41 | + transaction.onerror = () => { |
| 42 | + reject(transaction.error ?? new Error("IndexedDB transaction failed")); |
| 43 | + }; |
| 44 | + transaction.onabort = () => { |
| 45 | + reject(transaction.error ?? new Error("IndexedDB transaction aborted")); |
| 46 | + }; |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +function toActiveConversation(candidate: unknown): ActiveAIConversation | null { |
| 51 | + if (!candidate || typeof candidate !== "object") { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + const maybeRecord = candidate as Partial<ActiveConversationRecord>; |
| 56 | + |
| 57 | + if (typeof maybeRecord.conversationId !== "string") { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + const conversationId = maybeRecord.conversationId.trim(); |
| 62 | + |
| 63 | + if (!conversationId) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + return { |
| 68 | + conversationId, |
| 69 | + messages: Array.isArray(maybeRecord.messages) |
| 70 | + ? (maybeRecord.messages as UIMessage[]) |
| 71 | + : [], |
| 72 | + updatedAt: |
| 73 | + typeof maybeRecord.updatedAt === "number" && |
| 74 | + Number.isFinite(maybeRecord.updatedAt) |
| 75 | + ? maybeRecord.updatedAt |
| 76 | + : Date.now() |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +function openDatabase(): Promise<IDBDatabase | null> { |
| 81 | + if (!isIndexedDBAvailable()) { |
| 82 | + return Promise.resolve(null); |
| 83 | + } |
| 84 | + |
| 85 | + if (databasePromise) { |
| 86 | + return databasePromise; |
| 87 | + } |
| 88 | + |
| 89 | + databasePromise = new Promise<IDBDatabase>((resolve, reject) => { |
| 90 | + const request = window.indexedDB.open(DB_NAME, DB_VERSION); |
| 91 | + |
| 92 | + request.onupgradeneeded = () => { |
| 93 | + const database = request.result; |
| 94 | + |
| 95 | + if (!database.objectStoreNames.contains(ACTIVE_STORE)) { |
| 96 | + database.createObjectStore(ACTIVE_STORE, { keyPath: "key" }); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + request.onsuccess = () => resolve(request.result); |
| 101 | + request.onerror = () => { |
| 102 | + reject(request.error ?? new Error("Failed to open IndexedDB")); |
| 103 | + }; |
| 104 | + }).catch((error) => { |
| 105 | + databasePromise = null; |
| 106 | + throw error; |
| 107 | + }); |
| 108 | + |
| 109 | + return databasePromise; |
| 110 | +} |
| 111 | + |
| 112 | +export async function loadActiveAIConversation(): Promise<ActiveAIConversation | null> { |
| 113 | + try { |
| 114 | + const database = await openDatabase(); |
| 115 | + |
| 116 | + if (!database) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + const transaction = database.transaction(ACTIVE_STORE, "readonly"); |
| 121 | + const transactionCompleted = transactionDone(transaction); |
| 122 | + const store = transaction.objectStore(ACTIVE_STORE); |
| 123 | + const activeRecord = await requestToPromise(store.get(ACTIVE_KEY)); |
| 124 | + |
| 125 | + await transactionCompleted; |
| 126 | + |
| 127 | + return toActiveConversation(activeRecord); |
| 128 | + } catch { |
| 129 | + return null; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export async function saveActiveAIConversation( |
| 134 | + conversationId: string, |
| 135 | + messages: UIMessage[] |
| 136 | +): Promise<void> { |
| 137 | + const normalizedConversationId = conversationId.trim(); |
| 138 | + |
| 139 | + if (!normalizedConversationId || messages.length === 0) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + try { |
| 144 | + const database = await openDatabase(); |
| 145 | + |
| 146 | + if (!database) { |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + const transaction = database.transaction(ACTIVE_STORE, "readwrite"); |
| 151 | + const transactionCompleted = transactionDone(transaction); |
| 152 | + const store = transaction.objectStore(ACTIVE_STORE); |
| 153 | + |
| 154 | + const nextRecord: ActiveConversationRecord = { |
| 155 | + key: ACTIVE_KEY, |
| 156 | + conversationId: normalizedConversationId, |
| 157 | + messages, |
| 158 | + updatedAt: Date.now() |
| 159 | + }; |
| 160 | + |
| 161 | + await requestToPromise(store.put(nextRecord)); |
| 162 | + |
| 163 | + await transactionCompleted; |
| 164 | + } catch { |
| 165 | + // Ignore IndexedDB write failures. |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export async function clearActiveAIConversation(): Promise<void> { |
| 170 | + try { |
| 171 | + const database = await openDatabase(); |
| 172 | + |
| 173 | + if (!database) { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + const transaction = database.transaction(ACTIVE_STORE, "readwrite"); |
| 178 | + const transactionCompleted = transactionDone(transaction); |
| 179 | + const store = transaction.objectStore(ACTIVE_STORE); |
| 180 | + |
| 181 | + await requestToPromise(store.delete(ACTIVE_KEY)); |
| 182 | + |
| 183 | + await transactionCompleted; |
| 184 | + } catch { |
| 185 | + // Ignore IndexedDB write failures. |
| 186 | + } |
| 187 | +} |
0 commit comments