Agent Zero AI framework
UI Knowledge import
Alessandro9d6b769dc29e
diff --git a/run_ui.py b/run_ui.pyindex 713084068..3b152092e 100644--- a/run_ui.py+++ b/run_ui.py@@ -13,6 +13,7 @@ from python.helpers.files import get_abs_pathfrom python.helpers.print_style import PrintStylefrom python.helpers.dotenv import load_dotenvfrom python.helpers import persist_chat, settings+from werkzeug.utils import secure_filename# initialize the internal Flask server@@ -65,6 +66,61 @@ def requires_auth(f):return decorated+UPLOAD_FOLDER = os.path.join(os.getcwd(), 'work_dir', 'uploads')+[email protected]('/upload', methods=['POST'])+async def upload_file():+ if 'file' not in request.files:+ return jsonify({'ok': False, 'message': 'No file part'}), 400++ files = request.files.getlist('file') # Handle multiple files+ saved_filenames = []++ for file in files:+ if file and allowed_file(file.filename): # Check file type+ filename = secure_filename(file.filename)+ file.save(os.path.join(UPLOAD_FOLDER, filename))+ saved_filenames.append(filename)++ return jsonify({'ok': True, 'filenames': saved_filenames}) # Return saved filenames+++ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'txt', 'pdf', 'csv', 'html', 'json', 'md'}++def allowed_file(filename):+ return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS++[email protected]("/import_knowledge", methods=["POST"])+async def import_knowledge():+ if 'files[]' not in request.files:+ return jsonify({'ok': False, 'message': 'No files part'}), 400++ files = request.files.getlist('files[]')+ KNOWLEDGE_FOLDER = os.path.join(os.getcwd(), 'knowledge', 'custom', 'main')++ saved_filenames = []++ for file in files:+ if file:+ filename = secure_filename(file.filename)+ file.save(os.path.join(KNOWLEDGE_FOLDER, filename))+ saved_filenames.append(filename)++ return jsonify({'ok': True, 'message': "Knowledge Imported", 'filenames': saved_filenames})++[email protected]("/work_dir", methods=["GET"]) # Correct route+async def browse_work_dir():+ work_dir = os.path.join(os.getcwd(), 'work_dir')+ try:+ files = [f for f in os.listdir(work_dir) if os.path.isfile(os.path.join(work_dir, f))]+ return jsonify({'ok': True, 'files': files})+ except FileNotFoundError:+ return jsonify({'ok': False, 'message': 'work_dir not found'}), 404+ except Exception as e:+ return jsonify({'ok': False, 'message': f'Error browsing work_dir: {str(e)}'}), 500+# handle default address, show demo html page from ./test_form.html@app.route("/", methods=["GET"])async def test_form():diff --git a/webui/index.css b/webui/index.cssindex 7d37a48b2..890117b3c 100644--- a/webui/index.css+++ b/webui/index.css@@ -922,12 +922,12 @@ pre {/* Image preview section */.image-preview-section {display: flex;- gap: 8px;- padding: 8px;+ gap: var(--spacing-md);+ padding: var(--spacing-sm) var(--spacing-sm) 0.37rem var(--spacing-sm);overflow-x: auto;background-color: var(--color-input);border-radius: 8px;- margin-bottom: 8px;+ margin-bottom: var(--spacing-xs);}.preview-item {@@ -936,19 +936,18 @@ pre {}.preview-item img {- width: 100px;- height: 100px;+ max-height: 100px;object-fit: cover;- border-radius: 4px;- border: 1px solid var(--color-border);+ border-radius: 8px;+ border: 1px solid var(--color-border-light);}.remove-image {position: absolute;top: -6px;right: -6px;- width: 20px;- height: 20px;+ width: 18px;+ height: 18px;border-radius: 50%;background-color: var(--color-accent);color: white;diff --git a/webui/index.html b/webui/index.htmlindex d20a1903c..95e076d2b 100644--- a/webui/index.html+++ b/webui/index.html@@ -213,7 +213,7 @@</svg></label><input type="file" id="file-input"- accept=".jpg,.png,.tiff,.bmp"+ accept="png, jpg, jpeg, txt, pdf, csv, html, json, md"multiplestyle="display: none"@change="handleFileUpload($event)">@@ -247,8 +247,13 @@<!-- Bottom row with text buttons --><div class="text-buttons-row">- <button class="text-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5"></path></svg>Import knowledge</button>- <button class="text-button"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 122.88 89.09"><g><path stroke-width="3.5" stroke="currentColor" fill="none" d="M3.97,9.75l-3.93,8.73l122.77,0.01l-3.96-8.74H55.82c-1.59,0-2.88-1.29-2.88-2.88V0H11.97v6.87 c0,1.59-1.29,2.88-2.88,2.88H3.97L3.97,9.75L3.97,9.75z"></path><path stroke-width="3.5" stroke="currentColor" fill="none" d="M4.63,18.48H0l7.03,67.03c0.11,1.07,0.55,2.02,1.2,2.69c0.55,0.55,1.28,0.89,2.11,0.89h100.1 c0.82,0,1.51-0.33,2.05-0.87c0.68-0.68,1.13-1.67,1.28-2.79l9.1-66.94H4.63V18.48L4.63,18.48z"></path></g></svg>work_dir Browser</button>+ <button class="text-button" @click="loadKnowledge()"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 0 0 5.25 21h13.5A2.25 2.25 0 0 0 21 18.75V16.5m-13.5-9L12 3m0 0 4.5 4.5M12 3v13.5"></path></svg>Import knowledge</button>+ <button class="text-button" @click="workDirModalProxy.openModal()">+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 123.37 92.59">+ <path d="m5.72,11.5l-3.93,8.73h119.77s-3.96-8.73-3.96-8.73h-60.03c-1.59,0-2.88-1.29-2.88-2.88V1.75H13.72v6.87c0,1.59-1.29,2.88-2.88,2.88h-5.12Z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>+ <path d="m6.38,20.23H1.75l7.03,67.03c.11,1.07.55,2.02,1.2,2.69.55.55,1.28.89,2.11.89h97.1c.82,0,1.51-.33,2.05-.87.68-.68,1.13-1.67,1.28-2.79l9.1-66.94H6.38Z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="5"></path>+ </svg>work_dir Browser</button>+<button class="text-button" @click="pauseAgent(!paused)"><!-- Dynamic path that switches between pause and play icons --><template x-if="!paused">@@ -373,6 +378,28 @@</template></div>+ <div id="workDirModal" x-data="workDirModalProxy">+ <template x-teleport="body">+ <div x-show="isOpen" class="modal-overlay" @click.self="close()" x-transition>+ <div class="modal-container">+ <div class="modal-header">+ <h2>Work Directory</h2>+ </div>+ <div class="modal-content">+ <ul>+ <template x-for="file in files" :key="file">+ <li x-text="file"></li>+ </template>+ </ul>+ </div>+ <div class="modal-footer">+ <button @click="close()">Close</button>+ </div>+ </div>+ </div>+ </template>+ </div>+</body></html>\ No newline at end of filediff --git a/webui/index.js b/webui/index.jsindex e9f3f9a0b..98bdc8a5e 100644--- a/webui/index.js+++ b/webui/index.js@@ -202,6 +202,94 @@ function setMessage(id, type, heading, content, temp, kvps = null) {}+async function handleFileUpload(event) {+ const files = event.target.files;+ const formData = new FormData();+ for (let i = 0; i < files.length; i++) {+ formData.append('file', files[i]);+ }++ const response = await fetch('/upload', {+ method: 'POST',+ body: formData,+ });++ const data = await response.json();+ if (!data.ok) {+ toast(data.message, "error");+ } else {+ toast("Files uploaded: " + data.filenames.join(", "), "success");+ }+}+++window.loadKnowledge = async function() {+ const input = document.createElement('input');+ input.type = 'file';+ input.accept = '.txt,.pdf,.csv,.html,.json,.md';+ input.multiple = true;++ input.onchange = async () => {+ const formData = new FormData();+ for (let file of input.files) {+ formData.append('files[]', file);+ }++ const response = await fetch('/import_knowledge', {+ method: 'POST',+ body: formData,+ });++ const data = await response.json();+ if (!data.ok) {+ toast(data.message, "error");+ } else {+ toast("Knowledge files imported: " + data.filenames.join(", "), "success");+ }+ };++ input.click();+}+++const workDirModalProxy = {+ isOpen: false,+ files: [],++ async openModal() { // Define openModal+ // Inside openModal, call the existing open method:+ await this.open(); // Or directly include the fetching logic here+ },++ async open() {+ const response = await sendJsonData('/work_dir');+ if (response.ok) {+ this.files = response.files;+ this.isOpen = true;+ } else {+ toast(response.message, 'error');+ }+ },++ close() {+ this.isOpen = false;+ }+};++// Make the proxy available globally+window.workDirModalProxy = workDirModalProxy;++// Ensure correct setup for Alpine.js x-data.+window.workDirModal = function() {+ return workDirModalProxy; // Returns the proxy object for the Work Dir modal+}+++document.addEventListener('alpine:init', () => {+ // Make workDirModalProxy available as an Alpine component/store+ Alpine.data('workDirModal', workDirModal);+});+function adjustTextareaHeight() {chatInput.style.height = 'auto';