Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Refactor agent settings and implement IndexedDB for attachment management

Muhammad Hamza Khan54f6e8aabd5a
diff --git a/packages/app/src/builder-ui/pages/builder/agent-settings.ts b/packages/app/src/builder-ui/pages/builder/agent-settings.ts
index c7b566e4..8199cf04 100644
--- a/packages/app/src/builder-ui/pages/builder/agent-settings.ts
+++ b/packages/app/src/builder-ui/pages/builder/agent-settings.ts
@@ -216,8 +216,6 @@ function createAgent() {
//Agent created, do we have a chatMessage query param?
const urlParams = new URLSearchParams(window.location.search);
const chat = urlParams.get('chat');
- const attachments = urlParams.get('attach');
- const from = urlParams.get('from');
if (chat) {
localStorage.setItem(
'chatMessage',
@@ -233,22 +231,6 @@ function createAgent() {
1000 * 60 * 3.1,
);
}
- if (attachments) {
- localStorage.setItem(
- 'chatAttachments',
- JSON.stringify({ attachments: [attachments], ttl: Date.now() + 1000 * 60 * 3 }),
- );
-
- setTimeout(
- () => {
- try {
- const item = JSON.parse(localStorage.getItem('file'));
- if (item.ttl < Date.now()) localStorage.removeItem('file');
- } catch {}
- },
- 1000 * 60 * 3.1,
- );
- }
resolve(workspace.agent.id);
} else {
diff --git a/packages/app/src/builder-ui/utils/index-db.utils.ts b/packages/app/src/builder-ui/utils/index-db.utils.ts
new file mode 100644
index 00000000..1bd41b1e
--- /dev/null
+++ b/packages/app/src/builder-ui/utils/index-db.utils.ts
@@ -0,0 +1,78 @@
+// Store large attachment in IndexedDB
+export const storeAttachmentInIndexedDB = async (
+ attachment: unknown,
+ key: string,
+): Promise<string> => {
+ return new Promise((resolve, reject) => {
+ const request = indexedDB.open('ChatAttachment', 1);
+
+ request.onerror = () => reject(request.error);
+
+ request.onsuccess = () => {
+ const db = request.result;
+ if (db) {
+ const transaction = db?.transaction(['attachment'], 'readwrite');
+ const store = transaction?.objectStore('attachment');
+
+ const putRequest = store?.add({ data: attachment }, key);
+ putRequest.onsuccess = () => resolve(key);
+ putRequest.onerror = () => reject(putRequest.error);
+ } else {
+ reject(new Error('IndexedDB database connection failed.'));
+ }
+ };
+
+ request.onupgradeneeded = () => {
+ const db = request.result;
+ if (!db.objectStoreNames.contains('attachment')) {
+ db.createObjectStore('attachment');
+ }
+ };
+ });
+};
+
+// Retrieve from IndexedDB
+export const getAttachmentFromIndexedDB = async (key: string): Promise<unknown> => {
+ return new Promise((resolve, reject) => {
+ const request = indexedDB.open('ChatAttachment', 1);
+
+ request.onsuccess = () => {
+ const db = request.result;
+ if (db && db?.objectStoreNames?.contains('attachment')) {
+ const transaction = db?.transaction(['attachment'], 'readonly');
+ const store = transaction?.objectStore('attachment');
+ const getRequest = store?.get(key);
+
+ getRequest.onsuccess = () => resolve(getRequest.result?.data || null);
+ getRequest.onerror = () => reject(getRequest.error);
+ } else {
+ reject(new Error('IndexedDB database connection failed.'));
+ }
+ };
+
+ request.onerror = () => reject(request.error);
+ });
+};
+
+// Remove data from IndexedDB based on key
+export const removeAttachmentFromIndexedDB = async (key: string): Promise<void> => {
+ return new Promise((resolve, reject) => {
+ const request = indexedDB.open('ChatAttachment', 1);
+
+ request.onsuccess = () => {
+ const db = request.result;
+ if (db) {
+ const transaction = db?.transaction(['attachment'], 'readwrite');
+ const store = transaction?.objectStore('attachment');
+ const deleteRequest = store?.delete(key);
+
+ deleteRequest.onsuccess = () => resolve();
+ deleteRequest.onerror = () => reject(deleteRequest.error);
+ } else {
+ reject(new Error('IndexedDB database connection failed.'));
+ }
+ };
+
+ request.onerror = () => reject(request.error);
+ });
+};
diff --git a/packages/app/src/react/features/error-pages/pages/CatchAllPage.tsx b/packages/app/src/react/features/error-pages/pages/CatchAllPage.tsx
index 455757a3..d8801a9a 100644
--- a/packages/app/src/react/features/error-pages/pages/CatchAllPage.tsx
+++ b/packages/app/src/react/features/error-pages/pages/CatchAllPage.tsx
@@ -13,7 +13,9 @@ export function CatchAllPage() {
if (shouldRedirect) {
// reload the page so the vite middleware can redirect to the new page. if redirected back, then add ?attempted_redirect=true to the url
// so we do not enter an infinite redirect loop
- window.location.href = `${location.pathname}?attempted_redirect=true`;
+ const newSearchParams = new URLSearchParams(searchParams);
+ newSearchParams.set('attempted_redirect', 'true');
+ window.location.href = `${location.pathname}?${newSearchParams.toString()}`;
}
}, [shouldRedirect, location.pathname]);
@@ -23,7 +25,5 @@ export function CatchAllPage() {
}
// Otherwise, show normal 404
- return (
- <ErrorPage code="404" />
- );
-}
\ No newline at end of file
+ return <ErrorPage code="404" />;
+}