forked from stackblitz/bolt.new
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b1bf0f
commit 5db834e
Showing
9 changed files
with
251 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
max_line_length = 120 | ||
indent_size = 2 | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import { env } from 'node:process'; | ||
import { isAuthenticated } from './sessions'; | ||
import { json, redirect, type LoaderFunctionArgs } from '@remix-run/cloudflare'; | ||
|
||
export function verifyPassword(password: string, cloudflareEnv: Env) { | ||
const loginPassword = env.LOGIN_PASSWORD || cloudflareEnv.LOGIN_PASSWORD; | ||
|
||
return password === loginPassword; | ||
} | ||
|
||
export async function handleAuthRequest({ request, context }: LoaderFunctionArgs, body: object = {}) { | ||
const authenticated = await isAuthenticated(request, context.cloudflare.env); | ||
|
||
if (import.meta.env.DEV || authenticated) { | ||
return json(body); | ||
} | ||
|
||
return redirect('/login'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { ChatHistory } from './useChatHistory'; | ||
import type { Message } from 'ai'; | ||
import { createScopedLogger } from '~/utils/logger'; | ||
|
||
const logger = createScopedLogger('ChatHistory'); | ||
|
||
// this is used at the top level and never rejects | ||
export async function openDatabase(): Promise<IDBDatabase | undefined> { | ||
return new Promise((resolve) => { | ||
const request = indexedDB.open('boltHistory', 1); | ||
|
||
request.onupgradeneeded = (event: IDBVersionChangeEvent) => { | ||
const db = (event.target as IDBOpenDBRequest).result; | ||
|
||
if (!db.objectStoreNames.contains('chats')) { | ||
const store = db.createObjectStore('chats', { keyPath: 'id' }); | ||
store.createIndex('id', 'id', { unique: true }); | ||
} | ||
}; | ||
|
||
request.onsuccess = (event: Event) => { | ||
resolve((event.target as IDBOpenDBRequest).result); | ||
}; | ||
|
||
request.onerror = (event: Event) => { | ||
resolve(undefined); | ||
logger.error((event.target as IDBOpenDBRequest).error); | ||
}; | ||
}); | ||
} | ||
|
||
export async function setMessages(db: IDBDatabase, id: string, messages: Message[]): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction('chats', 'readwrite'); | ||
const store = transaction.objectStore('chats'); | ||
|
||
const request = store.put({ | ||
id, | ||
messages, | ||
}); | ||
|
||
request.onsuccess = () => resolve(); | ||
request.onerror = () => reject(request.error); | ||
}); | ||
} | ||
|
||
export async function getMessages(db: IDBDatabase, id: string): Promise<ChatHistory> { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction('chats', 'readonly'); | ||
const store = transaction.objectStore('chats'); | ||
const request = store.get(id); | ||
|
||
request.onsuccess = () => resolve(request.result as ChatHistory); | ||
request.onerror = () => reject(request.error); | ||
}); | ||
} | ||
|
||
export async function getNextID(db: IDBDatabase): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction('chats', 'readonly'); | ||
const store = transaction.objectStore('chats'); | ||
const request = store.count(); | ||
|
||
request.onsuccess = () => resolve(String(request.result)); | ||
request.onerror = () => reject(request.error); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './db'; | ||
export * from './useChatHistory'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useNavigate, useLoaderData } from '@remix-run/react'; | ||
import { useState, useEffect } from 'react'; | ||
import type { Message } from 'ai'; | ||
import { openDatabase, setMessages, getMessages, getNextID } from './db'; | ||
import { toast } from 'react-toastify'; | ||
|
||
export interface ChatHistory { | ||
id: string; | ||
displayName?: string; | ||
messages: Message[]; | ||
} | ||
|
||
const persistenceEnabled = !import.meta.env.VITE_DISABLE_PERSISTENCE; | ||
|
||
const db = persistenceEnabled ? await openDatabase() : undefined; | ||
|
||
export function useChatHistory() { | ||
const navigate = useNavigate(); | ||
const { id: chatId } = useLoaderData<{ id?: string }>(); | ||
|
||
const [initialMessages, setInitialMessages] = useState<Message[]>([]); | ||
const [ready, setReady] = useState<boolean>(false); | ||
const [entryId, setEntryId] = useState<string | undefined>(); | ||
|
||
useEffect(() => { | ||
if (!db) { | ||
setReady(true); | ||
|
||
if (persistenceEnabled) { | ||
toast.error(`Chat persistence is unavailable`); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (chatId) { | ||
getMessages(db, chatId) | ||
.then((storedMessages) => { | ||
if (storedMessages && storedMessages.messages.length > 0) { | ||
setInitialMessages(storedMessages.messages); | ||
} else { | ||
navigate(`/`, { replace: true }); | ||
} | ||
|
||
setReady(true); | ||
}) | ||
.catch((error) => { | ||
toast.error(error.message); | ||
}); | ||
} | ||
}, []); | ||
|
||
return { | ||
ready: !chatId || ready, | ||
initialMessages, | ||
storeMessageHistory: async (messages: Message[]) => { | ||
if (!db || messages.length === 0) { | ||
return; | ||
} | ||
|
||
if (initialMessages.length === 0) { | ||
if (!entryId) { | ||
const nextId = await getNextID(db); | ||
|
||
await setMessages(db, nextId, messages); | ||
|
||
setEntryId(nextId); | ||
|
||
/** | ||
* FIXME: Using the intended navigate function causes a rerender for <Chat /> that breaks the app. | ||
* | ||
* `navigate(`/chat/${nextId}`, { replace: true });` | ||
*/ | ||
const url = new URL(window.location.href); | ||
url.pathname = `/chat/${nextId}`; | ||
|
||
window.history.replaceState({}, '', url); | ||
} else { | ||
await setMessages(db, entryId, messages); | ||
} | ||
} else { | ||
await setMessages(db, chatId as string, messages); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { LoaderFunctionArgs } from '@remix-run/cloudflare'; | ||
import { default as IndexRoute } from './_index'; | ||
import { handleAuthRequest } from '~/lib/.server/login'; | ||
|
||
export async function loader(args: LoaderFunctionArgs) { | ||
return handleAuthRequest(args, { id: args.params.id }); | ||
} | ||
|
||
export default IndexRoute; |