Skip to content

Agent Zero AI framework

initial attachment improvements w/ preview on reload

decif85e0707f9b6
diff --git a/python/api/image_get.py b/python/api/image_get.py
index 2ed1d1e13..ff33dd5a7 100644
--- a/python/api/image_get.py
+++ b/python/api/image_get.py
@@ -16,19 +16,85 @@ class ImageGet(ApiHandler):
if not files.is_in_base_dir(path):
raise ValueError("Path is outside of allowed directory")
- # check if file has an image extension
- # list of allowed image extensions
- allowed_extensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg"]
# get file extension
file_ext = os.path.splitext(path)[1].lower()
- if file_ext not in allowed_extensions:
- raise ValueError(f"File type not allowed. Allowed types: {', '.join(allowed_extensions)}")
- # check if file exists
- if not os.path.exists(path):
- raise ValueError("File not found")
+ # list of allowed image extensions
+ image_extensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg"]
+
+ if file_ext in image_extensions:
+ # Handle image files
+ if not os.path.exists(path):
+ # If image doesn't exist, return default image icon
+ return self._get_fallback_icon("image")
+
+ # send actual image file
+ return send_file(path)
+ else:
+ # Handle non-image files with fallback icons
+ return self._get_file_type_icon(file_ext)
+
+ def _get_file_type_icon(self, file_ext):
+ """Return appropriate icon for file type"""
+ # Map file extensions to icon names
+ icon_mapping = {
+ # Archive files
+ '.zip': 'archive',
+ '.rar': 'archive',
+ '.7z': 'archive',
+ '.tar': 'archive',
+ '.gz': 'archive',
+
+ # Document files
+ '.pdf': 'document',
+ '.doc': 'document',
+ '.docx': 'document',
+ '.txt': 'document',
+ '.rtf': 'document',
+ '.odt': 'document',
+
+ # Code files
+ '.py': 'code',
+ '.js': 'code',
+ '.html': 'code',
+ '.css': 'code',
+ '.json': 'code',
+ '.xml': 'code',
+ '.md': 'code',
+ '.yml': 'code',
+ '.yaml': 'code',
+ '.sql': 'code',
+ '.sh': 'code',
+ '.bat': 'code',
+
+ # Spreadsheet files
+ '.xls': 'document',
+ '.xlsx': 'document',
+ '.csv': 'document',
- # send file
- return send_file(path)
+ # Presentation files
+ '.ppt': 'document',
+ '.pptx': 'document',
+ '.odp': 'document',
+ }
+
+ # Get icon name, default to 'file' if not found
+ icon_name = icon_mapping.get(file_ext, 'file')
+ return self._get_fallback_icon(icon_name)
+
+ def _get_fallback_icon(self, icon_name):
+ """Return fallback icon from public directory"""
+ # Path to public icons
+ icon_path = files.get_abs_path(f"webui/public/{icon_name}.svg")
+
+ # Check if specific icon exists, fallback to generic file icon
+ if not os.path.exists(icon_path):
+ icon_path = files.get_abs_path("webui/public/file.svg")
+
+ # Final fallback if file.svg doesn't exist
+ if not os.path.exists(icon_path):
+ raise ValueError(f"Fallback icon not found: {icon_path}")
+
+ return send_file(icon_path, mimetype='image/svg+xml')
\ No newline at end of file
diff --git a/webui/components/chat/attachments/attachmentsStore.js b/webui/components/chat/attachments/attachmentsStore.js
new file mode 100644
index 000000000..7aa60b1a0
--- /dev/null
+++ b/webui/components/chat/attachments/attachmentsStore.js
@@ -0,0 +1,195 @@
+import { createStore } from "/js/AlpineStore.js";
+
+const model = {
+ // State properties
+ attachments: [],
+ hasAttachments: false,
+ dragDropOverlayVisible: false,
+
+ // Initialize the store
+ async initialize() {
+ // Setup event listeners for drag and drop
+ this.setupDragDropHandlers();
+ // Setup paste event listener for clipboard images
+ this.setupPasteHandler();
+ },
+
+ // Basic attachment management methods
+ addAttachment(attachment) {
+ // Validate for duplicates
+ if (this.validateDuplicates(attachment)) {
+ this.attachments.push(attachment);
+ this.updateAttachmentState();
+ }
+ },
+
+ removeAttachment(index) {
+ if (index >= 0 && index < this.attachments.length) {
+ this.attachments.splice(index, 1);
+ this.updateAttachmentState();
+ }
+ },
+
+ clearAttachments() {
+ this.attachments = [];
+ this.updateAttachmentState();
+ },
+
+ validateDuplicates(newAttachment) {
+ // Check if attachment already exists based on name and size
+ const isDuplicate = this.attachments.some(existing =>
+ existing.name === newAttachment.name &&
+ existing.file && newAttachment.file &&
+ existing.file.size === newAttachment.file.size
+ );
+ return !isDuplicate;
+ },
+
+ updateAttachmentState() {
+ this.hasAttachments = this.attachments.length > 0;
+ },
+
+ // Drag drop overlay control methods
+ showDragDropOverlay() {
+ this.dragDropOverlayVisible = true;
+ },
+
+ hideDragDropOverlay() {
+ this.dragDropOverlayVisible = false;
+ },
+
+ // Setup drag and drop event handlers
+ setupDragDropHandlers() {
+ let dragCounter = 0;
+
+ // Prevent default drag behaviors
+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
+ document.addEventListener(eventName, (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ }, false);
+ });
+
+ // Handle drag enter
+ document.addEventListener('dragenter', (e) => {
+ dragCounter++;
+ if (dragCounter === 1) {
+ this.showDragDropOverlay();
+ }
+ }, false);
+
+ // Handle drag leave
+ document.addEventListener('dragleave', (e) => {
+ dragCounter--;
+ if (dragCounter === 0) {
+ this.hideDragDropOverlay();
+ }
+ }, false);
+
+ // Handle drop
+ document.addEventListener('drop', (e) => {
+ dragCounter = 0;
+ this.hideDragDropOverlay();
+
+ const files = e.dataTransfer.files;
+ this.handleFiles(files);
+ }, false);
+ },
+
+ // Setup paste event handler for clipboard images
+ setupPasteHandler() {
+ document.addEventListener('paste', (e) => {
+ const items = e.clipboardData.items;
+ for (let i = 0; i < items.length; i++) {
+ const item = items[i];
+ if (item.type.indexOf('image') !== -1) {
+ const blob = item.getAsFile();
+ this.handleClipboardImage(blob);
+ }
+ }
+ });
+ },
+
+ // Handle clipboard image pasting
+ async handleClipboardImage(blob) {
+ // Generate unique filename
+ const guid = this.generateGUID();
+ const filename = `clipboard-${guid}.png`;
+
+ // Create file object from blob
+ const file = new File([blob], filename, { type: 'image/png' });
+
+ // Create attachment object
+ const attachment = {
+ file: file,
+ type: 'image',
+ name: filename,
+ extension: 'png'
+ };
+
+ // Read as data URL for preview
+ const reader = new FileReader();
+ reader.onload = (e) => {
+ attachment.url = e.target.result;
+ this.addAttachment(attachment);
+ };
+ reader.readAsDataURL(file);
+ },
+
+ // File handling logic (moved from index.js)
+ handleFiles(files) {
+ Array.from(files).forEach(file => {
+ const ext = file.name.split('.').pop().toLowerCase();
+ const isImage = ['jpg', 'jpeg', 'png', 'bmp', 'gif', 'webp'].includes(ext);
+
+ const attachment = {
+ file: file,
+ type: isImage ? 'image' : 'file',
+ name: file.name,
+ extension: ext
+ };
+
+ if (isImage) {
+ // Read image as data URL for preview
+ const reader = new FileReader();
+ reader.onload = (e) => {
+ attachment.url = e.target.result;
+ this.addAttachment(attachment);
+ };
+ reader.readAsDataURL(file);
+ } else {
+ // For non-image files, add directly
+ this.addAttachment(attachment);
+ }
+ });
+ },
+
+ // Get attachments for sending message
+ getAttachmentsForSending() {
+ return this.attachments.map(attachment => {
+ if (attachment.type === 'image') {
+ return {
+ ...attachment,
+ url: URL.createObjectURL(attachment.file)
+ };
+ } else {
+ return {
+ ...attachment
+ };
+ }
+ });
+ },
+
+ // Generate GUID for unique filenames
+ generateGUID() {
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ const r = Math.random() * 16 | 0;
+ const v = c == 'x' ? r : (r & 0x3 | 0x8);
+ return v.toString(16);
+ });
+ }
+};
+
+const store = createStore("chatAttachments", model);
+
+export { store };
\ No newline at end of file
diff --git a/webui/components/chat/attachments/dragDropOverlay.html b/webui/components/chat/attachments/dragDropOverlay.html
new file mode 100644
index 000000000..7d9d3f7d4
--- /dev/null
+++ b/webui/components/chat/attachments/dragDropOverlay.html
@@ -0,0 +1,29 @@
+<html>
+
+<head>
+ <title>Drag Drop Overlay</title>
+
+ <script type="module">
+ import { store } from "/components/chat/attachments/attachmentsStore.js";
+ </script>
+</head>
+
+<body>
+ <!-- Drag and Drop Overlay -->
+ <div id="dragdrop-overlay"
+ x-cloak
+ x-show="$store.chatAttachments.dragDropOverlayVisible"
+ x-transition:enter="transition ease-out duration-300"
+ x-transition:enter-start="opacity-0"
+ x-transition:enter-end="opacity-100"
+ x-transition:leave="transition ease-in duration-300"
+ x-transition:leave-start="opacity-100"
+ x-transition:leave-end="opacity-0"
+ class="dragdrop-overlay">
+ <img src="public/dragndrop.svg" alt="Drop files" class="dragdrop-icon">
+ <div class="dragdrop-text">Drop files to attach them to your message</div>
+ <div class="dragdrop-subtext"></div>
+ </div>
+</body>
+
+</html>
\ No newline at end of file
diff --git a/webui/index.html b/webui/index.html
index c98ef2858..8d3ec86b5 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -101,6 +101,7 @@
<script type="module" src="js/scheduler.js"></script>
<script type="module" src="js/speech.js"></script>
<script type="module" src="js/history.js"></script>
+ <script type="module" src="components/chat/attachments/attachmentsStore.js"></script>
<script type="module" src="index.js"></script>
<!-- Then load Alpine.js -->
@@ -370,49 +371,12 @@
</h4>
</div>
<div id="input-section" x-data="{
- paused: false,
- attachments: [],
- hasAttachments: false,
-
- handleFileUpload(event) {
- const files = event.target.files;
-
- Array.from(files).forEach(file => {
- const ext = file.name.split('.').pop().toLowerCase();
-
- const isImage = ['jpg', 'jpeg', 'png', 'bmp'].includes(ext);
-
- if (isImage) {
- // Handle image preview
- const reader = new FileReader();
- reader.onload = e => {
- this.attachments.push({
- file: file,
- url: e.target.result,
- type: 'image',
- name: file.name,
- extension: ext
- });
- this.hasAttachments = true;
- };
- reader.readAsDataURL(file);
- } else {
- // Handle other file types
- this.attachments.push({
- file: file,
- type: 'file',
- name: file.name,
- extension: ext
- });
- this.hasAttachments = true;
- }
- });
- }
+ paused: false
}">
<!-- Preview section -->
- <div x-show="hasAttachments" class="preview-section">
- <template x-for="(attachment, index) in attachments" :key="index">
+ <div x-show="$store.chatAttachments.hasAttachments" class="preview-section">
+ <template x-for="(attachment, index) in $store.chatAttachments.attachments" :key="index">
<div class="preview-item" :class="{'image-preview': attachment.type === 'image'}">
<template x-if="attachment.type === 'image'">
<img :src="attachment.url" :alt="attachment.name">
@@ -423,7 +387,7 @@
<span class="extension" x-text="attachment.extension.toUpperCase()"></span>
</div>
</template>
- <button @click="attachments.splice(index, 1); hasAttachments = attachments.length > 0"
+ <button @click="$store.chatAttachments.removeAttachment(index)"
class="remove-attachment">&times;</button>
</div>
</template>
@@ -441,7 +405,7 @@
d="M16.5 6v11.5c0 2.21-1.79 4-4 4s-4-1.79-4-4V5c0-1.38 1.12-2.5 2.5-2.5s2.5 1.12 2.5 2.5v10.5c0 .55-.45 1-1 1s-1-.45-1-1V6H10v9.5c0 1.38 1.12 2.5 2.5 2.5s2.5-1.12 2.5-2.5V5c0-2.21-1.79-4-4-4S7 2.79 7 5v12.5c0 3.04 2.46 5.5 5.5 5.5s5.5-2.46 5.5-5.5V6h-1.5z" />
</svg>
</label>
- <input type="file" id="file-input" accept="*" multiple style="display: none" @change="handleFileUpload($event)">
+ <input type="file" id="file-input" accept="*" multiple style="display: none" @change="window.handleFileUpload($event)">
<div x-show="showTooltip" class="tooltip">
Add attachments to the message
@@ -1680,22 +1644,8 @@
</template>
</div>
- <!-- Drag and Drop Overlay -->
- <div id="dragdrop-overlay"
- x-cloak
- x-data="{ isVisible: false }"
- x-show="isVisible"
- x-transition:enter="transition ease-out duration-300"
- x-transition:enter-start="opacity-0"
- x-transition:enter-end="opacity-100"
- x-transition:leave="transition ease-in duration-300"
- x-transition:leave-start="opacity-100"
- x-transition:leave-end="opacity-0"
- class="dragdrop-overlay">
- <img src="public/dragndrop.svg" alt="Drop files" class="dragdrop-icon">
- <div class="dragdrop-text">Drop files to attach them to your message</div>
- <div class="dragdrop-subtext"></div>
- </div>
+ <!-- Drag and Drop Overlay Component -->
+ <x-component path="chat/attachments/dragDropOverlay.html"></x-component>
</body>
diff --git a/webui/index.js b/webui/index.js
index 5768d271c..17bc212c7 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -84,9 +84,9 @@ document.addEventListener('DOMContentLoaded', setupSidebarToggle);
export async function sendMessage() {
try {
const message = chatInput.value.trim();
- const inputAD = Alpine.$data(inputSection);
- const attachments = inputAD.attachments;
- const hasAttachments = attachments && attachments.length > 0;
+ const attachmentsStore = Alpine.store('chatAttachments');
+ const attachments = attachmentsStore ? attachmentsStore.attachments : [];
+ const hasAttachments = attachmentsStore ? attachmentsStore.hasAttachments : false;
if (message || hasAttachments) {
let response;
@@ -94,18 +94,7 @@ export async function sendMessage() {
// Include attachments in the user message
if (hasAttachments) {
- const attachmentsWithUrls = attachments.map(attachment => {
- if (attachment.type === 'image') {
- return {
- ...attachment,
- url: URL.createObjectURL(attachment.file)
- };
- } else {
- return {
- ...attachment
- };
- }
- });
+ const attachmentsWithUrls = attachmentsStore.getAttachmentsForSending();
// Render user message with attachments
setMessage(messageId, 'user', '', message, false, {
@@ -159,8 +148,9 @@ export async function sendMessage() {
// Clear input and attachments
chatInput.value = '';
- inputAD.attachments = [];
- inputAD.hasAttachments = false;
+ if (attachmentsStore) {
+ attachmentsStore.clearAttachments();
+ }
adjustTextareaHeight();
}
} catch (e) {
@@ -1079,84 +1069,14 @@ async function startPolling() {
document.addEventListener("DOMContentLoaded", startPolling);
-document.addEventListener('DOMContentLoaded', () => {
- const dragDropOverlay = document.getElementById('dragdrop-overlay');
- const inputSection = document.getElementById('input-section');
- let dragCounter = 0;
-
- // Prevent default drag behaviors
- ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
- document.addEventListener(eventName, (e) => {
- e.preventDefault();
- e.stopPropagation();
- }, false);
- });
-
- // Handle drag enter
- document.addEventListener('dragenter', (e) => {
- dragCounter++;
- if (dragCounter === 1) {
- Alpine.$data(dragDropOverlay).isVisible = true;
- }
- }, false);
-
- // Handle drag leave
- document.addEventListener('dragleave', (e) => {
- dragCounter--;
- if (dragCounter === 0) {
- Alpine.$data(dragDropOverlay).isVisible = false;
- }
- }, false);
-
- // Handle drop
- dragDropOverlay.addEventListener('drop', (e) => {
- dragCounter = 0;
- Alpine.$data(dragDropOverlay).isVisible = false;
+// Drag and drop functionality has been moved to attachmentsStore.js
- const inputAD = Alpine.$data(inputSection);
- const files = e.dataTransfer.files;
- handleFiles(files, inputAD);
- }, false);
-});
-
-// Separate file handling logic to be used by both drag-drop and file input
-function handleFiles(files, inputAD) {
- Array.from(files).forEach(file => {
- const ext = file.name.split('.').pop().toLowerCase();
-
- const isImage = ['jpg', 'jpeg', 'png', 'bmp'].includes(ext);
-
- if (isImage) {
- const reader = new FileReader();
- reader.onload = e => {
- inputAD.attachments.push({
- file: file,
- url: e.target.result,
- type: 'image',
- name: file.name,
- extension: ext
- });
- inputAD.hasAttachments = true;
- };
- reader.readAsDataURL(file);
- } else {
- inputAD.attachments.push({
- file: file,
- type: 'file',
- name: file.name,
- extension: ext
- });
- inputAD.hasAttachments = true;
- }
-
- });
-}
-
-// Modify the existing handleFileUpload to use the new handleFiles function
+// Update handleFileUpload to use the attachments store
window.handleFileUpload = function(event) {
const files = event.target.files;
- const inputAD = Alpine.$data(inputSection);
- handleFiles(files, inputAD);
+ if (Alpine.store('chatAttachments')) {
+ Alpine.store('chatAttachments').handleFiles(files);
+ }
}
// Setup event handlers once the DOM is fully loaded
diff --git a/webui/js/initFw.js b/webui/js/initFw.js
index e8ca39760..a441d04bb 100644
--- a/webui/js/initFw.js
+++ b/webui/js/initFw.js
@@ -3,6 +3,9 @@ import * as _components from "./components.js";
await import("./alpine.min.js");
+// Import attachments store
+import { store as attachmentsStore } from "../components/chat/attachments/attachmentsStore.js";
+
// add x-destroy directive
Alpine.directive(
"destroy",
@@ -11,3 +14,12 @@ Alpine.directive(
cleanup(() => onDestroy());
}
);
+
+// Initialize attachments store when Alpine.js is ready
+document.addEventListener('alpine:init', () => {
+ // Store is already created via createStore in attachmentsStore.js
+ // Just call initialize to set up event handlers
+ if (Alpine.store('chatAttachments')) {
+ Alpine.store('chatAttachments').initialize();
+ }
+});
diff --git a/webui/js/messages.js b/webui/js/messages.js
index a858eb229..ac089710d 100644
--- a/webui/js/messages.js
+++ b/webui/js/messages.js
@@ -318,25 +318,65 @@ export function drawMessageUser(
const attachmentDiv = document.createElement("div");
attachmentDiv.classList.add("attachment-item");
+ // Helper function to generate server-side image URL
+ const getServerImageUrl = (filename) => {
+ return `/image_get?path=/a0/tmp/uploads/${encodeURIComponent(filename)}`;
+ };
+
+ // Helper function to check if file is an image
+ const isImageFile = (filename) => {
+ const imageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
+ const extension = filename.split('.').pop().toLowerCase();
+ return imageExtensions.includes(extension);
+ };
+
if (typeof attachment === "string") {
- // attachment is filename
+ // attachment is filename only (from persistent storage)
const filename = attachment;
const extension = filename.split(".").pop().toUpperCase();
- attachmentDiv.classList.add("file-type");
- attachmentDiv.innerHTML = `
- <div class="file-preview">
+ if (isImageFile(filename)) {
+ // Render as image with server URL
+ const imgWrapper = document.createElement("div");
+ imgWrapper.classList.add("image-wrapper");
+
+ const img = document.createElement("img");
+ img.src = getServerImageUrl(filename);
+ img.alt = filename;
+ img.classList.add("attachment-preview");
+
+ const fileInfo = document.createElement("div");
+ fileInfo.classList.add("file-info");
+ fileInfo.innerHTML = `
<span class="filename">${filename}</span>
<span class="extension">${extension}</span>
- </div>
- `;
+ `;
+
+ imgWrapper.appendChild(img);
+ attachmentDiv.appendChild(imgWrapper);
+ attachmentDiv.appendChild(fileInfo);
+ } else {
+ // Render as file icon
+ attachmentDiv.classList.add("file-type");
+ attachmentDiv.innerHTML = `
+ <div class="file-preview">
+ <span class="filename">${filename}</span>
+ <span class="extension">${extension}</span>
+ </div>
+ `;
+ }
} else if (attachment.type === "image") {
- // Existing logic for images
+ // attachment is object (from current session)
const imgWrapper = document.createElement("div");
imgWrapper.classList.add("image-wrapper");
const img = document.createElement("img");
- img.src = attachment.url;
+ // Use server URL if we have filename, otherwise fall back to blob URL for current session
+ if (attachment.name && !attachment.url.startsWith('blob:')) {
+ img.src = getServerImageUrl(attachment.name);
+ } else {
+ img.src = attachment.url;
+ }
img.alt = attachment.name;
img.classList.add("attachment-preview");
@@ -351,7 +391,7 @@ export function drawMessageUser(
attachmentDiv.appendChild(imgWrapper);
attachmentDiv.appendChild(fileInfo);
} else {
- // Existing logic for non-image files
+ // attachment is object but not image (from current session)
attachmentDiv.classList.add("file-type");
attachmentDiv.innerHTML = `
<div class="file-preview">