diff --git a/actions/browser_control.py b/actions/browser_control.py index 3e4a5fd..8f18767 100644 --- a/actions/browser_control.py +++ b/actions/browser_control.py @@ -1,15 +1,3 @@ -""" -browser_control.py — JARVIS Browser Action Module -==================================================== -Desteklenen tarayıcılar : chrome, edge, firefox, opera, operagx, brave, vivaldi, safari (macOS) -Desteklenen platformlar : Windows · macOS · Linux - -Her tarayıcı için bağımsız _BrowserSession örneği tutulur. -"switch" action ile aktif tarayıcı değiştirilebilir. - -NOT: Tüm tarayıcılar kullanıcının gerçek profiliyle açılır (hesaplar, çerezler korunur). - Gizli/incognito mod kullanılmaz. -""" from __future__ import annotations @@ -30,12 +18,8 @@ from playwright.async_api import ( Playwright, TimeoutError as PlaywrightTimeout, ) - -# ── Platform ────────────────────────────────────────────────────────────────── _OS = platform.system() # "Windows" | "Darwin" | "Linux" - -# ── URL normalizer ──────────────────────────────────────────────────────────── def _normalize_url(url: str) -> str: """ Bare words like "instagram" → "https://instagram.com" @@ -53,7 +37,6 @@ def _normalize_url(url: str) -> str: return "https://" + url -# ── User-Agent ──────────────────────────────────────────────────────────────── def _user_agent() -> str: if _OS == "Windows": return ( @@ -74,7 +57,6 @@ def _user_agent() -> str: ) -# ── Gerçek kullanıcı profil dizini (tüm tarayıcılar) ───────────────────────── def _real_profile_dir(browser: str) -> str: home = Path.home() local = os.environ.get("LOCALAPPDATA", "") @@ -129,8 +111,6 @@ def _real_profile_dir(browser: str) -> str: print(f"[Browser] ⚠️ Real profile not found for {browser}, using: {fallback}") return str(fallback) - -# ── Firefox gerçek profil dizini ────────────────────────────────────────────── def _firefox_profile_dir() -> Optional[str]: home = Path.home() @@ -170,8 +150,6 @@ def _firefox_profile_dir() -> Optional[str]: return default_path return None - -# ── Windows: Opera exe tespiti ──────────────────────────────────────────────── def _find_opera_windows() -> Optional[str]: local = os.environ.get("LOCALAPPDATA", "") prog = os.environ.get("PROGRAMFILES", "") @@ -213,8 +191,6 @@ def _find_opera_windows() -> Optional[str]: return shutil.which("opera") or None - -# ── Genel exe arama (Windows registry) ─────────────────────────────────────── def _find_exe_windows(prog_name: str) -> Optional[str]: try: import winreg @@ -237,8 +213,6 @@ def _find_exe_windows(prog_name: str) -> Optional[str]: pass return None - -# ── Tarayıcı tanım tablosu ──────────────────────────────────────────────────── _BROWSER_SPECS: dict[str, dict] = { "Windows": { "chrome": {"engine": "chromium", "channel": "chrome", "bins": []}, @@ -370,8 +344,6 @@ def _detect_default_browser() -> str: return "chrome" -# ── Tek tarayıcı oturumu ────────────────────────────────────────────────────── - class _BrowserSession: """ Bir tarayıcı örneği için tam oturum. @@ -390,8 +362,6 @@ class _BrowserSession: self._context: BrowserContext | None = None self._page: Page | None = None - # ── Yaşam döngüsü ───────────────────────────────────────────────────────── - def start(self): if self._thread and self._thread.is_alive(): return @@ -436,8 +406,6 @@ class _BrowserSession: pass self._context = self._page = None - # ── Tarayıcı başlatma ────────────────────────────────────────────────────── - async def _launch(self): """ Tarayıcıyı gerçek kullanıcı profiliyle başlatır. @@ -456,7 +424,6 @@ class _BrowserSession: channel = self._spec["channel"] engine_obj = getattr(self._pw, engine_name) - # ── Firefox ─────────────────────────────────────────────────────────── if engine_name == "firefox": profile = _firefox_profile_dir() or str( Path.home() / ".jarvis_profiles" / "firefox" @@ -477,14 +444,11 @@ class _BrowserSession: Path(jarvis).mkdir(parents=True, exist_ok=True) self._context = await engine_obj.launch_persistent_context(jarvis, **kwargs) - await asyncio.sleep(0.5) # let the browser settle - # Always open a dedicated JARVIS tab instead of grabbing pages[0], - # which may already be about:blank or a stale restored tab. + await asyncio.sleep(0.5) self._page = await self._context.new_page() print(f"[Browser] ✅ Firefox launched") return - # ── Safari (webkit) ─────────────────────────────────────────────────── if engine_name == "webkit": safari_profile = str(Path.home() / ".jarvis_profiles" / "safari") Path(safari_profile).mkdir(parents=True, exist_ok=True) @@ -500,7 +464,6 @@ class _BrowserSession: print(f"[Browser] ✅ Safari launched") return - # ── Chromium tabanlı ────────────────────────────────────────────────── profile = _real_profile_dir(self.browser_name) kwargs = { @@ -528,17 +491,15 @@ class _BrowserSession: + (f" @ {exe}" if exe else "") ) - # 1) Gerçek profil try: self._context = await engine_obj.launch_persistent_context(profile, **kwargs) - await asyncio.sleep(0.5) # let the browser settle before any navigation + await asyncio.sleep(0.5) self._page = await self._context.new_page() print(f"[Browser] ✅ Launched [{label}] profile={profile}") return except Exception as e: print(f"[Browser] ⚠️ Real profile failed for {label}: {e}") - # 2) JARVIS fallback profili jarvis_profile = str(Path.home() / ".jarvis_profiles" / self.browser_name) Path(jarvis_profile).mkdir(parents=True, exist_ok=True) print(f"[Browser] Retrying with JARVIS profile: {jarvis_profile}") @@ -551,7 +512,6 @@ class _BrowserSession: except Exception as e2: raise RuntimeError(f"Could not launch {self.browser_name}: {e2}") from e2 - # ── Sayfa erişimi ───────────────────────────────────────────────────────── async def _get_page(self) -> Page: await self._launch() @@ -561,18 +521,8 @@ class _BrowserSession: await asyncio.sleep(0.2) return self._page - # ── Action'lar ──────────────────────────────────────────────────────────── - async def go_to(self, url: str) -> str: - """ - Navigate to a URL. - • Uses a 30-second timeout to handle heavy sites like YouTube. - • On PlaywrightTimeout we check whether the URL actually changed; - if it did, treat it as success so JARVIS won't retry endlessly. - • If the page is still about:blank after navigation, open a fresh tab - and try once more (covers stale-page edge cases in Opera GX etc.). - """ url = _normalize_url(url) page = await self._get_page() prev_url = page.url @@ -590,7 +540,6 @@ class _BrowserSession: result_url = await _do_goto(page) - # If page didn't move away from blank, retry on a brand-new tab if result_url in ("about:blank", "", None, prev_url) and prev_url in ("about:blank", "", None): print(f"[Browser] Still blank after goto — retrying on new tab: {url}") try: @@ -785,9 +734,6 @@ class _BrowserSession: await self._async_close() return f"{self.browser_name} closed." - -# ── Session registry ────────────────────────────────────────────────────────── - class _SessionRegistry: """Tüm aktif tarayıcı oturumlarını yönetir.""" @@ -855,26 +801,12 @@ class _SessionRegistry: _registry = _SessionRegistry() - -# ── Ana fonksiyon ───────────────────────────────────────────────────────────── - def browser_control( parameters: dict = None, response=None, player=None, session_memory=None, ) -> str: - """ - Desteklenen action'lar: - go_to, search, click, type, scroll, fill_form, - smart_click, smart_type, get_text, get_url, press, - new_tab, close_tab, screenshot, back, forward, reload, - switch, list_browsers, close, close_all - - Parametre: - browser : chrome | edge | firefox | opera | operagx | brave | vivaldi | safari - (belirtilmezse aktif tarayıcı kullanılır) - """ params = parameters or {} action = params.get("action", "").lower().strip() browser = params.get("browser", "").lower().strip() or None diff --git a/actions/computer_control.py b/actions/computer_control.py index 30cb072..3d9cd0a 100644 --- a/actions/computer_control.py +++ b/actions/computer_control.py @@ -1,3 +1,4 @@ +#computer_control.py import io import json import re diff --git a/actions/computer_settings.py b/actions/computer_settings.py index 8cb0bb0..2653b78 100644 --- a/actions/computer_settings.py +++ b/actions/computer_settings.py @@ -1,3 +1,4 @@ +#computer_settings.py import json import re import sys diff --git a/actions/desktop.py b/actions/desktop.py index 32b1736..bb63774 100644 --- a/actions/desktop.py +++ b/actions/desktop.py @@ -1,3 +1,4 @@ +#desktop.py import os import sys import json diff --git a/actions/file_processor.py b/actions/file_processor.py new file mode 100644 index 0000000..84e51cb --- /dev/null +++ b/actions/file_processor.py @@ -0,0 +1,833 @@ +""" +file_processor.py — JARVIS Universal File Processor + +Supported types: + image → describe, ocr, resize, convert, compress, crop + pdf → summarize, extract_text, extract_pages, to_word + docx → summarize, extract_text, reformat, translate_hint + txt/md → summarize, reformat, translate_hint, word_count + csv → analyze, filter, sort, convert, stats + xlsx → analyze, filter, convert, stats + json → validate, format, extract, convert + code → explain, review, fix, run, document + audio → transcribe, trim, convert, info + video → trim, extract_audio, extract_frame, info, compress + zip → list, extract + pptx → summarize, extract_text, to_pdf +""" + +import os +import re +import json +import shutil +import subprocess +import tempfile +from pathlib import Path +from datetime import datetime + +import google.generativeai as genai + + +def _get_api_key() -> str: + config_path = Path(__file__).resolve().parent.parent / "config" / "api_keys.json" + with open(config_path, "r", encoding="utf-8") as f: + return json.load(f)["gemini_api_key"] + + +def _gemini_client(): + genai.configure(api_key=_get_api_key()) + return genai.GenerativeModel("gemini-2.5-flash") + + +def _detect_type(path: Path) -> str: + ext = path.suffix.lower().lstrip(".") + image_exts = {"jpg", "jpeg", "png", "gif", "webp", "bmp", "tiff", "svg", "ico"} + video_exts = {"mp4", "avi", "mov", "mkv", "wmv", "flv", "webm", "m4v", "3gp"} + audio_exts = {"mp3", "wav", "ogg", "m4a", "aac", "flac", "wma", "opus"} + code_exts = {"py", "js", "ts", "jsx", "tsx", "html", "css", "java", "c", + "cpp", "cs", "go", "rs", "rb", "php", "swift", "kt", "sh", + "bash", "ps1", "lua", "r", "m", "sql", "yaml", "toml"} + archive_exts = {"zip", "rar", "tar", "gz", "7z", "bz2", "xz"} + + if ext in image_exts: return "image" + if ext in video_exts: return "video" + if ext in audio_exts: return "audio" + if ext in code_exts: return "code" + if ext in archive_exts: return "archive" + if ext == "pdf": return "pdf" + if ext in ("docx", "doc"): return "docx" + if ext in ("txt", "md", "rst", "log"): return "text" + if ext in ("csv", "tsv"): return "csv" + if ext in ("xlsx", "xls", "ods"): return "excel" + if ext == "json": return "json" + if ext == "xml": return "xml" + if ext in ("pptx", "ppt"): return "pptx" + return "unknown" + + +def _file_size_str(path: Path) -> str: + size = path.stat().st_size + if size < 1024: return f"{size} B" + if size < 1024**2: return f"{size/1024:.1f} KB" + if size < 1024**3: return f"{size/1024**2:.1f} MB" + return f"{size/1024**3:.1f} GB" + +def _output_path(src: Path, suffix: str, new_ext: str = None) -> Path: + ext = new_ext or src.suffix + name = f"{src.stem}_{suffix}{ext}" + return src.parent / name + +def _process_image(path: Path, action: str, params: dict, speak=None) -> str: + try: + from PIL import Image + except ImportError: + return "Pillow is not installed. Run: pip install Pillow" + + action = action or "describe" + + if action in ("describe", "ocr", "analyze", "read", "extract_text"): + try: + model = _gemini_client() + img = Image.open(path) + prompt = { + "describe": "Describe this image in detail.", + "ocr": "Extract all text visible in this image. Return only the text, formatted clearly.", + "analyze": "Analyze this image thoroughly: objects, colors, composition, any text, context.", + "read": "Read all text in this image, preserving structure and formatting.", + "extract_text": "Extract all text from this image.", + }.get(action, "Describe this image.") + + if params.get("instruction"): + prompt = params["instruction"] + + response = model.generate_content([prompt, img]) + result = response.text.strip() + + if len(result) > 500 and params.get("save", True): + out = _output_path(path, "result", ".txt") + out.write_text(result, encoding="utf-8") + return f"{result[:300]}...\n\nFull result saved to: {out}" + return result + except Exception as e: + return f"AI image analysis failed: {e}" + + if action == "resize": + width = int(params.get("width", 0)) + height = int(params.get("height", 0)) + scale = float(params.get("scale", 0)) + try: + img = Image.open(path) + w, h = img.size + if scale: + new_size = (int(w * scale), int(h * scale)) + elif width and height: + new_size = (width, height) + elif width: + new_size = (width, int(h * width / w)) + elif height: + new_size = (int(w * height / h), height) + else: + return "Please specify width, height, or scale." + out = _output_path(path, f"resized_{new_size[0]}x{new_size[1]}") + img.resize(new_size, Image.LANCZOS).save(out) + return f"Resized from {w}x{h} to {new_size[0]}x{new_size[1]}. Saved: {out.name}" + except Exception as e: + return f"Resize failed: {e}" + + if action == "convert": + fmt = params.get("format", "png").lower().strip(".") + fmt_map = {"jpg": "JPEG", "jpeg": "JPEG", "png": "PNG", + "webp": "WEBP", "bmp": "BMP", "tiff": "TIFF"} + pil_fmt = fmt_map.get(fmt, fmt.upper()) + try: + img = Image.open(path).convert("RGB") if fmt == "jpg" else Image.open(path) + out = _output_path(path, "converted", f".{fmt}") + img.save(out, pil_fmt) + return f"Converted to {fmt.upper()}. Saved: {out.name}" + except Exception as e: + return f"Convert failed: {e}" + + if action == "compress": + quality = int(params.get("quality", 70)) + try: + img = Image.open(path).convert("RGB") + out = _output_path(path, f"compressed_q{quality}", ".jpg") + img.save(out, "JPEG", quality=quality, optimize=True) + before = _file_size_str(path) + after = _file_size_str(out) + return f"Compressed: {before} → {after}. Saved: {out.name}" + except Exception as e: + return f"Compress failed: {e}" + + if action == "info": + try: + img = Image.open(path) + return (f"Image info: {img.format}, {img.size[0]}x{img.size[1]}px, " + f"mode: {img.mode}, size: {_file_size_str(path)}") + except Exception as e: + return f"Info failed: {e}" + + return _process_image(path, "describe", {"instruction": f"{action}: {params}"}) + +def _process_pdf(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "summarize" + + def _extract_pdf_text(max_chars=50000) -> str: + text = "" + try: + import pdfplumber + with pdfplumber.open(path) as pdf: + for page in pdf.pages: + text += (page.extract_text() or "") + "\n" + except ImportError: + try: + import PyPDF2 + with open(path, "rb") as f: + reader = PyPDF2.PdfReader(f) + for page in reader.pages: + text += page.extract_text() + "\n" + except ImportError: + return "" + return text[:max_chars] + + if action in ("summarize", "extract_text", "translate_hint", "analyze", "reformat"): + text = _extract_pdf_text() + if not text.strip(): + return "Could not extract text from PDF (may be scanned/image-based)." + + if action == "extract_text": + out = _output_path(path, "text", ".txt") + out.write_text(text, encoding="utf-8") + return f"Text extracted ({len(text)} chars). Saved: {out.name}" + + prompt_map = { + "summarize": f"Summarize this PDF document concisely:\n\n{text}", + "analyze": f"Analyze this document thoroughly:\n\n{text}", + "translate_hint": f"What language is this document in and what does it say? Summarize:\n\n{text}", + "reformat": f"Reformat this text cleanly with proper structure:\n\n{text}", + } + try: + model = _gemini_client() + response = model.generate_content(prompt_map.get(action, f"Analyze:\n\n{text}")) + result = response.text.strip() + if len(result) > 600 and params.get("save", True): + out = _output_path(path, action, ".txt") + out.write_text(result, encoding="utf-8") + return f"{result[:400]}...\n\nFull result saved: {out.name}" + return result + except Exception as e: + return f"AI analysis failed: {e}" + + if action == "info": + try: + import pdfplumber + with pdfplumber.open(path) as pdf: + pages = len(pdf.pages) + return f"PDF: {pages} pages, size: {_file_size_str(path)}" + except Exception: + return f"PDF size: {_file_size_str(path)}" + + if action == "to_word": + text = _extract_pdf_text() + if not text: + return "Could not extract text to convert." + try: + from docx import Document + doc = Document() + doc.add_heading(path.stem, 0) + for para in text.split("\n\n"): + if para.strip(): + doc.add_paragraph(para.strip()) + out = _output_path(path, "converted", ".docx") + doc.save(out) + return f"Converted to Word document. Saved: {out.name}" + except ImportError: + return "python-docx not installed. Run: pip install python-docx" + + return f"Unknown PDF action: '{action}'. Try: summarize, extract_text, info, to_word" + +def _process_text_doc(path: Path, file_type: str, action: str, + params: dict, speak=None) -> str: + action = action or "summarize" + + def _read_content() -> str: + if file_type == "docx": + try: + from docx import Document + doc = Document(path) + return "\n".join(p.text for p in doc.paragraphs) + except ImportError: + return "python-docx not installed." + except Exception as e: + return f"Read failed: {e}" + else: + return path.read_text(encoding="utf-8", errors="ignore") + + content = _read_content() + if not content.strip(): + return "File appears to be empty." + + if action == "word_count": + words = len(content.split()) + chars = len(content) + lines = content.count("\n") + return f"Word count: {words} words, {chars} characters, {lines} lines." + + if action == "extract_text": + if file_type != "txt": + out = _output_path(path, "extracted", ".txt") + out.write_text(content, encoding="utf-8") + return f"Text extracted. Saved: {out.name}" + return content[:2000] + + instruction = params.get("instruction", "") + prompt_map = { + "summarize": f"Summarize this document concisely:\n\n{content[:40000]}", + "analyze": f"Analyze this document:\n\n{content[:40000]}", + "reformat": f"Reformat this text with clean structure, proper headings and paragraphs:\n\n{content[:40000]}", + "fix": f"Fix grammar, spelling and style issues in this text:\n\n{content[:40000]}", + "translate_hint": f"What language is this and what does it say? Summarize:\n\n{content[:10000]}", + "to_bullet": f"Convert this text into a clear bullet-point summary:\n\n{content[:40000]}", + "custom": f"{instruction}\n\n{content[:40000]}", + } + + if action not in prompt_map: + + action = "custom" + instruction = action + + try: + model = _gemini_client() + response = model.generate_content(prompt_map[action]) + result = response.text.strip() + if len(result) > 600 and params.get("save", True): + out = _output_path(path, action, ".txt") + out.write_text(result, encoding="utf-8") + return f"{result[:400]}...\n\nFull result saved: {out.name}" + return result + except Exception as e: + return f"AI processing failed: {e}" + + +def _process_data(path: Path, file_type: str, action: str, + params: dict, speak=None) -> str: + try: + import pandas as pd + except ImportError: + return "pandas not installed. Run: pip install pandas openpyxl" + + action = action or "analyze" + + try: + if file_type == "csv": + df = pd.read_csv(path, encoding="utf-8", errors="replace") + else: + df = pd.read_excel(path) + except Exception as e: + return f"Could not read file: {e}" + + if action == "info": + return (f"Rows: {len(df)}, Columns: {len(df.columns)}\n" + f"Columns: {', '.join(df.columns.tolist())}\n" + f"Size: {_file_size_str(path)}") + + if action == "stats": + try: + desc = df.describe(include="all").to_string() + return f"Statistics:\n{desc[:2000]}" + except Exception as e: + return f"Stats failed: {e}" + + if action == "analyze": + preview = df.head(50).to_string() + prompt = (f"Analyze this dataset. Columns: {list(df.columns)}\n" + f"Rows: {len(df)}\nPreview:\n{preview}\n\n" + f"Give insights, patterns, and notable findings.") + try: + model = _gemini_client() + response = model.generate_content(prompt) + return response.text.strip() + except Exception as e: + return f"AI analysis failed: {e}" + + if action in ("convert", "to_csv", "to_excel", "to_json"): + fmt = {"to_csv": "csv", "to_excel": "xlsx", "to_json": "json", + "convert": params.get("format", "csv")}.get(action, "csv") + try: + if fmt == "csv": + out = _output_path(path, "converted", ".csv") + df.to_csv(out, index=False, encoding="utf-8") + elif fmt == "xlsx": + out = _output_path(path, "converted", ".xlsx") + df.to_excel(out, index=False) + elif fmt == "json": + out = _output_path(path, "converted", ".json") + df.to_json(out, orient="records", force_ascii=False, indent=2) + return f"Converted to {fmt.upper()}. Saved: {out.name}" + except Exception as e: + return f"Convert failed: {e}" + + if action == "filter": + col = params.get("column", "") + value = params.get("value", "") + condition = params.get("condition", "equals") + if not col or col not in df.columns: + return f"Column '{col}' not found. Available: {', '.join(df.columns)}" + try: + if condition == "equals": filtered = df[df[col] == value] + elif condition == "contains": filtered = df[df[col].astype(str).str.contains(str(value), case=False)] + elif condition == "gt": filtered = df[df[col] > float(value)] + elif condition == "lt": filtered = df[df[col] < float(value)] + else: filtered = df[df[col] == value] + out = _output_path(path, "filtered", ".csv") + filtered.to_csv(out, index=False) + return f"Filtered: {len(filtered)} rows match. Saved: {out.name}" + except Exception as e: + return f"Filter failed: {e}" + + if action == "sort": + col = params.get("column", df.columns[0]) + asc = params.get("ascending", True) + try: + sorted_df = df.sort_values(col, ascending=asc) + out = _output_path(path, "sorted", path.suffix) + sorted_df.to_csv(out, index=False) + return f"Sorted by '{col}'. Saved: {out.name}" + except Exception as e: + return f"Sort failed: {e}" + + preview = df.head(30).to_string() + try: + model = _gemini_client() + response = model.generate_content( + f"Task: {action}\nDataset ({len(df)} rows, cols: {list(df.columns)}):\n{preview}" + ) + return response.text.strip() + except Exception as e: + return f"Processing failed: {e}" + + +def _process_json(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "analyze" + try: + content = path.read_text(encoding="utf-8") + data = json.loads(content) + except Exception as e: + return f"Invalid JSON: {e}" + + if action == "validate": + return f"Valid JSON. Type: {type(data).__name__}, size: {_file_size_str(path)}" + + if action == "format": + out = _output_path(path, "formatted", ".json") + out.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8") + return f"Formatted JSON saved: {out.name}" + + if action in ("analyze", "summarize", "extract"): + preview = json.dumps(data, indent=2, ensure_ascii=False)[:8000] + prompt = f"Task: {action} this JSON data:\n{preview}" + if params.get("instruction"): + prompt = f"{params['instruction']}\n\nJSON data:\n{preview}" + try: + model = _gemini_client() + response = model.generate_content(prompt) + return response.text.strip() + except Exception as e: + return f"AI processing failed: {e}" + + if action == "to_csv": + try: + import pandas as pd + if isinstance(data, list): + df = pd.DataFrame(data) + out = _output_path(path, "converted", ".csv") + df.to_csv(out, index=False) + return f"Converted to CSV. Saved: {out.name}" + return "JSON must be an array of objects to convert to CSV." + except ImportError: + return "pandas not installed." + + return _process_json(path, "analyze", {"instruction": action}) + +def _process_code(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "explain" + content = path.read_text(encoding="utf-8", errors="ignore") + ext = path.suffix.lstrip(".") + + if action == "run": + if ext == "py": + try: + result = subprocess.run( + ["python", str(path)], + capture_output=True, text=True, timeout=30 + ) + out = result.stdout or result.stderr + return f"Output:\n{out[:2000]}" if out else "No output." + except subprocess.TimeoutExpired: + return "Execution timed out (30s)." + except Exception as e: + return f"Run failed: {e}" + return f"Direct execution not supported for .{ext} files." + + if action == "info": + lines = content.count("\n") + words = len(content.split()) + return f"Code file: {lines} lines, {words} words, {_file_size_str(path)}" + + prompt_map = { + "explain": f"Explain this {ext} code clearly:\n\n```{ext}\n{content[:30000]}\n```", + "review": f"Review this {ext} code for bugs, issues, and improvements:\n\n```{ext}\n{content[:30000]}\n```", + "fix": f"Fix any bugs in this {ext} code and return the corrected version:\n\n```{ext}\n{content[:30000]}\n```", + "optimize": f"Optimize this {ext} code for performance and readability:\n\n```{ext}\n{content[:30000]}\n```", + "document": f"Add proper documentation/comments to this {ext} code:\n\n```{ext}\n{content[:30000]}\n```", + "summarize": f"Summarize what this {ext} code does:\n\n```{ext}\n{content[:30000]}\n```", + "test": f"Write unit tests for this {ext} code:\n\n```{ext}\n{content[:30000]}\n```", + } + + instruction = params.get("instruction", "") + if action not in prompt_map: + prompt = f"{action}\n\n```{ext}\n{content[:30000]}\n```" + if instruction: + prompt = f"{instruction}\n\n```{ext}\n{content[:30000]}\n```" + else: + prompt = prompt_map[action] + + try: + model = _gemini_client() + response = model.generate_content(prompt) + result = response.text.strip() + + if action in ("fix", "optimize", "document") and params.get("save", True): + out = _output_path(path, action) + code_match = re.search(r"```(?:\w+)?\n(.*?)```", result, re.DOTALL) + code_to_save = code_match.group(1) if code_match else result + out.write_text(code_to_save, encoding="utf-8") + return f"{result[:400]}...\n\nSaved: {out.name}" + return result + except Exception as e: + return f"AI processing failed: {e}" + +def _process_audio(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "transcribe" + + if action == "info": + try: + from pydub import AudioSegment + audio = AudioSegment.from_file(path) + duration = len(audio) / 1000 + mins, secs = divmod(int(duration), 60) + return (f"Audio: {mins}m {secs}s, " + f"{audio.channels} ch, " + f"{audio.frame_rate}Hz, " + f"{_file_size_str(path)}") + except ImportError: + return f"Audio file: {_file_size_str(path)} (install pydub for more info)" + except Exception as e: + return f"Info failed: {e}" + + if action == "transcribe": + try: + model = _gemini_client() + content = path.read_bytes() + mime = { + "mp3": "audio/mp3", "wav": "audio/wav", + "ogg": "audio/ogg", "m4a": "audio/mp4", + "aac": "audio/aac", "flac": "audio/flac", + }.get(path.suffix.lstrip(".").lower(), "audio/mpeg") + response = model.generate_content([ + "Transcribe all speech in this audio file accurately.", + {"mime_type": mime, "data": content} + ]) + result = response.text.strip() + if params.get("save", True): + out = _output_path(path, "transcript", ".txt") + out.write_text(result, encoding="utf-8") + return f"Transcription saved: {out.name}\n\nPreview: {result[:300]}" + return result + except Exception as e: + return f"Transcription failed: {e}" + + if action == "convert": + fmt = params.get("format", "mp3").lstrip(".") + try: + from pydub import AudioSegment + audio = AudioSegment.from_file(path) + out = _output_path(path, "converted", f".{fmt}") + audio.export(out, format=fmt) + return f"Converted to {fmt.upper()}. Saved: {out.name}" + except ImportError: + return "pydub not installed. Run: pip install pydub" + except Exception as e: + return f"Convert failed: {e}" + + if action == "trim": + start = float(params.get("start", 0)) + end = float(params.get("end", 0)) + try: + from pydub import AudioSegment + audio = AudioSegment.from_file(path) + end_ms = int(end * 1000) if end else len(audio) + trimmed = audio[int(start * 1000):end_ms] + out = _output_path(path, f"trim_{int(start)}s_{int(end)}s") + trimmed.export(out, format=path.suffix.lstrip(".")) + return f"Trimmed audio ({int(start)}s–{int(end)}s). Saved: {out.name}" + except ImportError: + return "pydub not installed." + except Exception as e: + return f"Trim failed: {e}" + + return f"Unknown audio action: '{action}'. Try: transcribe, info, convert, trim" + +def _process_video(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "info" + + + def _ffmpeg_available() -> bool: + try: + subprocess.run(["ffmpeg", "-version"], capture_output=True, timeout=3) + return True + except Exception: + return False + + if action == "info": + try: + result = subprocess.run( + ["ffprobe", "-v", "quiet", "-print_format", "json", + "-show_format", "-show_streams", str(path)], + capture_output=True, text=True, timeout=10 + ) + data = json.loads(result.stdout) + fmt = data.get("format", {}) + duration = float(fmt.get("duration", 0)) + mins, secs = divmod(int(duration), 60) + size = _file_size_str(path) + streams = data.get("streams", []) + video_s = next((s for s in streams if s["codec_type"] == "video"), {}) + w = video_s.get("width", "?") + h = video_s.get("height", "?") + fps = video_s.get("r_frame_rate", "?") + return f"Video: {mins}m {secs}s, {w}x{h}, {fps} fps, {size}" + except Exception: + return f"Video file: {_file_size_str(path)}" + + if action == "extract_audio": + if not _ffmpeg_available(): + return "ffmpeg not found. Install ffmpeg to extract audio." + out = _output_path(path, "audio", ".mp3") + try: + subprocess.run( + ["ffmpeg", "-i", str(path), "-q:a", "0", "-map", "a", str(out), "-y"], + capture_output=True, timeout=300 + ) + return f"Audio extracted. Saved: {out.name}" + except Exception as e: + return f"Extract audio failed: {e}" + + if action == "trim": + start = params.get("start", "00:00:00") + end = params.get("end", "") + if not _ffmpeg_available(): + return "ffmpeg not found." + out = _output_path(path, f"trim", path.suffix) + try: + cmd = ["ffmpeg", "-i", str(path), "-ss", str(start)] + if end: + cmd += ["-to", str(end)] + cmd += ["-c", "copy", str(out), "-y"] + subprocess.run(cmd, capture_output=True, timeout=600) + return f"Trimmed video saved: {out.name}" + except Exception as e: + return f"Trim failed: {e}" + + if action == "extract_frame": + timestamp = params.get("timestamp", "00:00:01") + if not _ffmpeg_available(): + return "ffmpeg not found." + out = _output_path(path, f"frame_{timestamp.replace(':', '')}", ".jpg") + try: + subprocess.run( + ["ffmpeg", "-i", str(path), "-ss", timestamp, + "-vframes", "1", str(out), "-y"], + capture_output=True, timeout=30 + ) + return f"Frame extracted at {timestamp}. Saved: {out.name}" + except Exception as e: + return f"Extract frame failed: {e}" + + if action == "compress": + crf = int(params.get("quality", 28)) + if not _ffmpeg_available(): + return "ffmpeg not found." + out = _output_path(path, f"compressed_crf{crf}", ".mp4") + try: + subprocess.run( + ["ffmpeg", "-i", str(path), + "-c:v", "libx264", "-crf", str(crf), + "-preset", "medium", "-c:a", "copy", + str(out), "-y"], + capture_output=True, timeout=1800 + ) + before = _file_size_str(path) + after = _file_size_str(out) + return f"Compressed: {before} → {after}. Saved: {out.name}" + except Exception as e: + return f"Compress failed: {e}" + + if action == "transcribe": + if not _ffmpeg_available(): + return "ffmpeg not found. Needed for video transcription." + tmp_audio = Path(tempfile.mktemp(suffix=".mp3")) + try: + subprocess.run( + ["ffmpeg", "-i", str(path), "-q:a", "0", "-map", "a", + str(tmp_audio), "-y"], + capture_output=True, timeout=300 + ) + result = _process_audio(tmp_audio, "transcribe", params, speak) + return result + except Exception as e: + return f"Video transcription failed: {e}" + finally: + if tmp_audio.exists(): + tmp_audio.unlink() + + if action == "convert": + fmt = params.get("format", "mp4").lstrip(".") + if not _ffmpeg_available(): + return "ffmpeg not found." + out = _output_path(path, "converted", f".{fmt}") + try: + subprocess.run( + ["ffmpeg", "-i", str(path), str(out), "-y"], + capture_output=True, timeout=1800 + ) + return f"Converted to {fmt.upper()}. Saved: {out.name}" + except Exception as e: + return f"Convert failed: {e}" + + return f"Unknown video action: '{action}'. Try: info, trim, extract_audio, extract_frame, compress, transcribe, convert" + +def _process_archive(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "list" + + if action == "list": + try: + import zipfile, tarfile + ext = path.suffix.lower() + if ext == ".zip": + with zipfile.ZipFile(path) as z: + names = z.namelist() + elif ext in (".tar", ".gz", ".bz2", ".xz"): + with tarfile.open(path) as t: + names = t.getnames() + else: + return f"Unsupported archive format: {ext}" + preview = "\n".join(names[:30]) + suffix = f"\n... and {len(names)-30} more" if len(names) > 30 else "" + return f"Archive contains {len(names)} files:\n{preview}{suffix}" + except Exception as e: + return f"List failed: {e}" + + if action == "extract": + dest = Path(params.get("destination", str(path.parent / path.stem))) + dest.mkdir(parents=True, exist_ok=True) + try: + shutil.unpack_archive(path, dest) + return f"Extracted to: {dest}" + except Exception as e: + return f"Extract failed: {e}" + + return f"Unknown archive action: '{action}'. Try: list, extract" + +def _process_pptx(path: Path, action: str, params: dict, speak=None) -> str: + action = action or "summarize" + + def _read_pptx_text() -> str: + try: + from pptx import Presentation + prs = Presentation(path) + text = [] + for i, slide in enumerate(prs.slides, 1): + slide_text = f"\n--- Slide {i} ---\n" + for shape in slide.shapes: + if hasattr(shape, "text") and shape.text.strip(): + slide_text += shape.text.strip() + "\n" + text.append(slide_text) + return "\n".join(text) + except ImportError: + return "python-pptx not installed." + + if action in ("summarize", "extract_text", "analyze"): + text = _read_pptx_text() + if action == "extract_text": + out = _output_path(path, "text", ".txt") + out.write_text(text, encoding="utf-8") + return f"Text extracted. Saved: {out.name}" + try: + model = _gemini_client() + prompt = f"{'Summarize' if action == 'summarize' else 'Analyze'} this presentation:\n{text[:30000]}" + response = model.generate_content(prompt) + return response.text.strip() + except Exception as e: + return f"AI processing failed: {e}" + + return f"Unknown PPTX action: '{action}'. Try: summarize, extract_text, analyze" + +def file_processor(parameters: dict, player=None, speak=None) -> str: + file_path_str = parameters.get("file_path", "").strip() + if not file_path_str: + return "No file path provided." + + path = Path(file_path_str) + if not path.exists(): + return f"File not found: {file_path_str}" + if not path.is_file(): + return f"Path is not a file: {file_path_str}" + + file_type = _detect_type(path) + action = (parameters.get("action") or "").lower().strip() + instruction = parameters.get("instruction", "") + params = {**parameters, "instruction": instruction} + + log_msg = f"[FileProcessor] {file_type.upper()} | {path.name} | action={action or 'auto'}" + print(log_msg) + if player: + player.write_log(log_msg) + + if file_type == "unknown": + try: + content = path.read_text(encoding="utf-8", errors="ignore")[:10000] + model = _gemini_client() + prompt = f"File: {path.name}\nContent preview:\n{content}\n\nTask: {action or instruction or 'Describe what this file contains and what can be done with it.'}" + response = model.generate_content(prompt) + return response.text.strip() + except Exception as e: + return f"Unknown file type ({path.suffix}). Could not process: {e}" + + dispatch = { + "image": _process_image, + "pdf": _process_pdf, + "docx": lambda p, a, pm, s: _process_text_doc(p, "docx", a, pm, s), + "text": lambda p, a, pm, s: _process_text_doc(p, "text", a, pm, s), + "csv": lambda p, a, pm, s: _process_data(p, "csv", a, pm, s), + "excel": lambda p, a, pm, s: _process_data(p, "excel", a, pm, s), + "json": _process_json, + "xml": lambda p, a, pm, s: _process_json(p, a, pm, s), + "code": _process_code, + "audio": _process_audio, + "video": _process_video, + "archive": _process_archive, + "pptx": _process_pptx, + } + + handler = dispatch.get(file_type) + if not handler: + return f"Unsupported file type: {file_type}" + + try: + result = handler(path, action, params, speak) + return result or "Done." + except Exception as e: + import traceback + traceback.print_exc() + return f"Processing failed: {e}" \ No newline at end of file diff --git a/actions/flight_finder.py b/actions/flight_finder.py index 58d5bad..ea6ba30 100644 --- a/actions/flight_finder.py +++ b/actions/flight_finder.py @@ -1,3 +1,4 @@ +#flight_finder.py import json import re import subprocess diff --git a/actions/screen_processor.py b/actions/screen_processor.py index d331895..84db42e 100644 --- a/actions/screen_processor.py +++ b/actions/screen_processor.py @@ -1,442 +1,442 @@ -from __future__ import annotations - -import asyncio -import base64 -import io -import json -import re -import sys -import threading -import time -from pathlib import Path -from typing import Optional - -import numpy as np -import sounddevice as sd - -try: - import cv2 - _CV2 = True -except ImportError: - _CV2 = False - -try: - import mss - import mss.tools - _MSS = True -except ImportError: - _MSS = False - -try: - import PIL.Image - _PIL = True -except ImportError: - _PIL = False - -from google import genai -from google.genai import types as gtypes - -def _base_dir() -> Path: - if getattr(sys, "frozen", False): - return Path(sys.executable).parent - return Path(__file__).resolve().parent.parent - - -_BASE = _base_dir() -_CONFIG_PATH = _BASE / "config" / "api_keys.json" - - -def _load_config() -> dict: - try: - return json.loads(_CONFIG_PATH.read_text(encoding="utf-8")) - except Exception: - return {} - - -def _save_config_key(key: str, value) -> None: - try: - cfg = _load_config() - cfg[key] = value - _CONFIG_PATH.write_text(json.dumps(cfg, indent=4), encoding="utf-8") - except Exception as e: - print(f"[Vision] ⚠️ Could not save config key '{key}': {e}") - - -def _get_api_key() -> str: - key = _load_config().get("gemini_api_key", "") - if not key: - raise RuntimeError("gemini_api_key not found in config.") - return key - - -def _get_os() -> str: - return _load_config().get("os_system", "windows").lower() - -_LIVE_MODEL = "models/gemini-2.5-flash-native-audio-preview-12-2025" -_CHANNELS = 1 -_RECEIVE_SAMPLE_RATE = 24_000 -_CHUNK_SIZE = 1_024 - -_IMG_MAX_W = 640 -_IMG_MAX_H = 360 -_JPEG_Q = 60 - -_SYSTEM_PROMPT = ( - "You are JARVIS, an advanced AI assistant. " - "Analyze the provided image with precision and intelligence. " - "Be concise and direct — maximum two sentences unless the user's question " - "requires more detail. " - "Address the user respectfully. " - "Always call the appropriate tool; never simulate results." -) - - -def _compress(img_bytes: bytes, source_format: str = "PNG") -> tuple[bytes, str]: - if not _PIL: - return img_bytes, f"image/{source_format.lower()}" - - try: - img = PIL.Image.open(io.BytesIO(img_bytes)).convert("RGB") - img.thumbnail((_IMG_MAX_W, _IMG_MAX_H), PIL.Image.BILINEAR) - buf = io.BytesIO() - img.save(buf, format="JPEG", quality=_JPEG_Q, optimize=False) - return buf.getvalue(), "image/jpeg" - except Exception as e: - print(f"[Vision] ⚠️ Image compress failed: {e}") - return img_bytes, f"image/{source_format.lower()}" - -def _capture_screen() -> tuple[bytes, str]: - - if not _MSS: - raise RuntimeError("mss is not installed. Run: pip install mss") - - with mss.mss() as sct: - monitors = sct.monitors # [0] = all combined, [1..n] = real screens - target = monitors[1] if len(monitors) > 1 else monitors[0] - shot = sct.grab(target) - png = mss.tools.to_png(shot.rgb, shot.size) - - return _compress(png, "PNG") - - -def _cv2_backend() -> int: - """Return the best OpenCV camera backend for the current OS.""" - if not _CV2: - return 0 - os_name = _get_os() - if os_name == "windows": - return cv2.CAP_DSHOW - if os_name == "mac": - return cv2.CAP_AVFOUNDATION - return cv2.CAP_ANY - - -def _probe_camera(index: int, backend: int, warmup: int = 5) -> bool: - - if not _CV2: - return False - cap = cv2.VideoCapture(index, backend) - if not cap.isOpened(): - cap.release() - return False - for _ in range(warmup): - cap.read() - ret, frame = cap.read() - cap.release() - if not ret or frame is None: - return False - return bool(np.mean(frame) > 8) - - -def _detect_camera_index() -> int: - - backend = _cv2_backend() - print("[Vision] 🔍 Auto-detecting camera...") - for idx in range(6): - if _probe_camera(idx, backend): - print(f"[Vision] ✅ Camera found at index {idx}") - _save_config_key("camera_index", idx) - return idx - print(f"[Vision] ⚠️ Camera index {idx}: no usable frame") - - print("[Vision] ⚠️ No camera found — defaulting to index 0") - _save_config_key("camera_index", 0) - return 0 - - -def _get_camera_index() -> int: - cfg = _load_config() - if "camera_index" in cfg: - return int(cfg["camera_index"]) - return _detect_camera_index() - - -def _capture_camera() -> tuple[bytes, str]: - if not _CV2: - raise RuntimeError("OpenCV (cv2) is not installed. Run: pip install opencv-python") - - index = _get_camera_index() - backend = _cv2_backend() - cap = cv2.VideoCapture(index, backend) - - if not cap.isOpened(): - raise RuntimeError(f"Camera index {index} could not be opened.") - - for _ in range(10): - cap.read() - - ret, frame = cap.read() - cap.release() - - if not ret or frame is None: - raise RuntimeError("Camera returned no frame.") - - if _PIL: - rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) - img = PIL.Image.fromarray(rgb) - img.thumbnail((_IMG_MAX_W, _IMG_MAX_H), PIL.Image.BILINEAR) - buf = io.BytesIO() - img.save(buf, format="JPEG", quality=_JPEG_Q) - return buf.getvalue(), "image/jpeg" - - _, buf = cv2.imencode(".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, _JPEG_Q]) - return buf.tobytes(), "image/jpeg" - -class _VisionSession: - def __init__(self): - self._loop: Optional[asyncio.AbstractEventLoop] = None - self._thread: Optional[threading.Thread] = None - self._session = None - self._out_queue: Optional[asyncio.Queue] = None - self._audio_in: Optional[asyncio.Queue] = None - self._ready_evt: threading.Event = threading.Event() - self._player = None - self._lock: threading.Lock = threading.Lock() - - def start(self, player=None, timeout: float = 25.0) -> None: - with self._lock: - if self._thread and self._thread.is_alive(): - if player is not None: - self._player = player - return - self._player = player - self._thread = threading.Thread( - target=self._run_event_loop, - daemon=True, - name="VisionSessionThread", - ) - self._thread.start() - - if not self._ready_evt.wait(timeout=timeout): - raise RuntimeError(f"Vision session did not connect within {timeout}s.") - print("[Vision] ✅ Session ready") - - def analyze(self, image_bytes: bytes, mime_type: str, user_text: str) -> None: - if not self._loop or not self._out_queue: - print("[Vision] ⚠️ Session not started — dropping request") - return - asyncio.run_coroutine_threadsafe( - self._out_queue.put((image_bytes, mime_type, user_text)), - self._loop, - ) - - def is_ready(self) -> bool: - return self._session is not None - - def _run_event_loop(self) -> None: - self._loop = asyncio.new_event_loop() - asyncio.set_event_loop(self._loop) - self._loop.run_until_complete(self._session_loop()) - - async def _session_loop(self) -> None: - self._out_queue = asyncio.Queue(maxsize=30) - self._audio_in = asyncio.Queue() - - client = genai.Client( - api_key=_get_api_key(), - http_options={"api_version": "v1beta"}, - ) - config = gtypes.LiveConnectConfig( - response_modalities=["AUDIO"], - output_audio_transcription={}, - system_instruction=_SYSTEM_PROMPT, - speech_config=gtypes.SpeechConfig( - voice_config=gtypes.VoiceConfig( - prebuilt_voice_config=gtypes.PrebuiltVoiceConfig( - voice_name="Charon" - ) - ) - ), - ) - - backoff = 2.0 - while True: - try: - print("[Vision] 🔌 Connecting...") - async with client.aio.live.connect( - model=_LIVE_MODEL, config=config - ) as session: - self._session = session - self._ready_evt.set() - backoff = 2.0 - print("[Vision] ✅ Connected") - - async with asyncio.TaskGroup() as tg: - tg.create_task(self._send_loop()) - tg.create_task(self._recv_loop()) - tg.create_task(self._play_loop()) - - except Exception as eg: - for exc in eg.exceptions: - print(f"[Vision] ⚠️ Session error: {exc}") - finally: - self._session = None - self._ready_evt.clear() - - print(f"[Vision] 🔄 Reconnecting in {backoff:.0f}s...") - await asyncio.sleep(backoff) - backoff = min(backoff * 1.5, 30.0) - self._ready_evt.set() - - async def _send_loop(self) -> None: - while True: - image_bytes, mime_type, user_text = await self._out_queue.get() - if not self._session: - print("[Vision] ⚠️ No session — dropping image") - continue - try: - b64 = base64.b64encode(image_bytes).decode("ascii") - await self._session.send_client_content( - turns={ - "parts": [ - {"inline_data": {"mime_type": mime_type, "data": b64}}, - {"text": user_text}, - ] - }, - turn_complete=True, - ) - print(f"[Vision] 📤 Sent {len(image_bytes):,} bytes — '{user_text[:60]}'") - except Exception as e: - print(f"[Vision] ⚠️ Send error: {e}") - - async def _recv_loop(self) -> None: - transcript: list[str] = [] - try: - async for response in self._session.receive(): - if response.data: - await self._audio_in.put(response.data) - - sc = response.server_content - if not sc: - continue - - if sc.output_transcription and sc.output_transcription.text: - chunk = sc.output_transcription.text.strip() - if chunk: - transcript.append(chunk) - - if sc.turn_complete: - if transcript and self._player: - full = re.sub(r"\s+", " ", " ".join(transcript)).strip() - if full: - self._player.write_log(f"Jarvis: {full}") - print(f"[Vision] 💬 {full}") - transcript = [] - - except Exception as e: - print(f"[Vision] ⚠️ Recv error: {e}") - raise - - async def _play_loop(self) -> None: - stream = sd.RawOutputStream( - samplerate=_RECEIVE_SAMPLE_RATE, - channels=_CHANNELS, - dtype="int16", - blocksize=_CHUNK_SIZE, - ) - stream.start() - try: - while True: - chunk = await self._audio_in.get() - await asyncio.to_thread(stream.write, chunk) - except Exception as e: - print(f"[Vision] ❌ Play error: {e}") - raise - finally: - stream.stop() - stream.close() - -_session = _VisionSession() -_session_lock = threading.Lock() -_session_up = False - - -def _ensure_session(player=None) -> None: - global _session_up - with _session_lock: - if not _session_up: - _session.start(player=player) - _session_up = True - elif player is not None: - _session._player = player - - -def screen_process( - parameters: dict, - response=None, - player=None, - session_memory=None, -) -> bool: - - params = parameters or {} - user_text = (params.get("text") or params.get("user_text") or "").strip() - angle = params.get("angle", "screen").lower().strip() - - if not user_text: - print("[Vision] ⚠️ No question provided — aborting") - return False - - print(f"[Vision] ▶ angle={angle!r} question='{user_text[:80]}'") - - try: - _ensure_session(player=player) - except Exception as e: - print(f"[Vision] ❌ Could not start session: {e}") - return False - - try: - if angle == "camera": - image_bytes, mime_type = _capture_camera() - print(f"[Vision] 📷 Camera: {len(image_bytes):,} bytes") - else: - image_bytes, mime_type = _capture_screen() - print(f"[Vision] 🖥️ Screen: {len(image_bytes):,} bytes") - except Exception as e: - print(f"[Vision] ❌ Capture error: {e}") - return False - - _session.analyze(image_bytes, mime_type, user_text) - return True - - -def warmup_session(player=None) -> None: - try: - _ensure_session(player=player) - except Exception as e: - print(f"[Vision] ⚠️ Warmup failed: {e}") - -if __name__ == "__main__": - print("[TEST] screen_processor.py") - print("=" * 52) - mode = input("angle — screen / camera (default: screen): ").strip().lower() or "screen" - q = input("Question (Enter = default): ").strip() or "What do you see? Be brief." - - t0 = time.perf_counter() - warmup_session() - print(f"Session ready in {time.perf_counter()-t0:.2f}s\n") - - t1 = time.perf_counter() - ok = screen_process({"angle": mode, "text": q}) - print(f"Queued in {time.perf_counter()-t1:.3f}s — waiting for audio...") - time.sleep(10) - print("Done." if ok else "Failed.") +from __future__ import annotations + +import asyncio +import base64 +import io +import json +import re +import sys +import threading +import time +from pathlib import Path +from typing import Optional + +import numpy as np +import sounddevice as sd + +try: + import cv2 + _CV2 = True +except ImportError: + _CV2 = False + +try: + import mss + import mss.tools + _MSS = True +except ImportError: + _MSS = False + +try: + import PIL.Image + _PIL = True +except ImportError: + _PIL = False + +from google import genai +from google.genai import types as gtypes + +def _base_dir() -> Path: + if getattr(sys, "frozen", False): + return Path(sys.executable).parent + return Path(__file__).resolve().parent.parent + + +_BASE = _base_dir() +_CONFIG_PATH = _BASE / "config" / "api_keys.json" + + +def _load_config() -> dict: + try: + return json.loads(_CONFIG_PATH.read_text(encoding="utf-8")) + except Exception: + return {} + + +def _save_config_key(key: str, value) -> None: + try: + cfg = _load_config() + cfg[key] = value + _CONFIG_PATH.write_text(json.dumps(cfg, indent=4), encoding="utf-8") + except Exception as e: + print(f"[Vision] ⚠️ Could not save config key '{key}': {e}") + + +def _get_api_key() -> str: + key = _load_config().get("gemini_api_key", "") + if not key: + raise RuntimeError("gemini_api_key not found in config.") + return key + + +def _get_os() -> str: + return _load_config().get("os_system", "windows").lower() + +_LIVE_MODEL = "models/gemini-2.5-flash-native-audio-preview-12-2025" +_CHANNELS = 1 +_RECEIVE_SAMPLE_RATE = 24_000 +_CHUNK_SIZE = 1_024 + +_IMG_MAX_W = 640 +_IMG_MAX_H = 360 +_JPEG_Q = 60 + +_SYSTEM_PROMPT = ( + "You are JARVIS, an advanced AI assistant. " + "Analyze the provided image with precision and intelligence. " + "Be concise and direct — maximum two sentences unless the user's question " + "requires more detail. " + "Address the user respectfully. " + "Always call the appropriate tool; never simulate results." +) + + +def _compress(img_bytes: bytes, source_format: str = "PNG") -> tuple[bytes, str]: + if not _PIL: + return img_bytes, f"image/{source_format.lower()}" + + try: + img = PIL.Image.open(io.BytesIO(img_bytes)).convert("RGB") + img.thumbnail((_IMG_MAX_W, _IMG_MAX_H), PIL.Image.BILINEAR) + buf = io.BytesIO() + img.save(buf, format="JPEG", quality=_JPEG_Q, optimize=False) + return buf.getvalue(), "image/jpeg" + except Exception as e: + print(f"[Vision] ⚠️ Image compress failed: {e}") + return img_bytes, f"image/{source_format.lower()}" + +def _capture_screen() -> tuple[bytes, str]: + + if not _MSS: + raise RuntimeError("mss is not installed. Run: pip install mss") + + with mss.mss() as sct: + monitors = sct.monitors # [0] = all combined, [1..n] = real screens + target = monitors[1] if len(monitors) > 1 else monitors[0] + shot = sct.grab(target) + png = mss.tools.to_png(shot.rgb, shot.size) + + return _compress(png, "PNG") + + +def _cv2_backend() -> int: + """Return the best OpenCV camera backend for the current OS.""" + if not _CV2: + return 0 + os_name = _get_os() + if os_name == "windows": + return cv2.CAP_DSHOW + if os_name == "mac": + return cv2.CAP_AVFOUNDATION + return cv2.CAP_ANY + + +def _probe_camera(index: int, backend: int, warmup: int = 5) -> bool: + + if not _CV2: + return False + cap = cv2.VideoCapture(index, backend) + if not cap.isOpened(): + cap.release() + return False + for _ in range(warmup): + cap.read() + ret, frame = cap.read() + cap.release() + if not ret or frame is None: + return False + return bool(np.mean(frame) > 8) + + +def _detect_camera_index() -> int: + + backend = _cv2_backend() + print("[Vision] 🔍 Auto-detecting camera...") + for idx in range(6): + if _probe_camera(idx, backend): + print(f"[Vision] ✅ Camera found at index {idx}") + _save_config_key("camera_index", idx) + return idx + print(f"[Vision] ⚠️ Camera index {idx}: no usable frame") + + print("[Vision] ⚠️ No camera found — defaulting to index 0") + _save_config_key("camera_index", 0) + return 0 + + +def _get_camera_index() -> int: + cfg = _load_config() + if "camera_index" in cfg: + return int(cfg["camera_index"]) + return _detect_camera_index() + + +def _capture_camera() -> tuple[bytes, str]: + if not _CV2: + raise RuntimeError("OpenCV (cv2) is not installed. Run: pip install opencv-python") + + index = _get_camera_index() + backend = _cv2_backend() + cap = cv2.VideoCapture(index, backend) + + if not cap.isOpened(): + raise RuntimeError(f"Camera index {index} could not be opened.") + + for _ in range(10): + cap.read() + + ret, frame = cap.read() + cap.release() + + if not ret or frame is None: + raise RuntimeError("Camera returned no frame.") + + if _PIL: + rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) + img = PIL.Image.fromarray(rgb) + img.thumbnail((_IMG_MAX_W, _IMG_MAX_H), PIL.Image.BILINEAR) + buf = io.BytesIO() + img.save(buf, format="JPEG", quality=_JPEG_Q) + return buf.getvalue(), "image/jpeg" + + _, buf = cv2.imencode(".jpg", frame, [cv2.IMWRITE_JPEG_QUALITY, _JPEG_Q]) + return buf.tobytes(), "image/jpeg" + +class _VisionSession: + def __init__(self): + self._loop: Optional[asyncio.AbstractEventLoop] = None + self._thread: Optional[threading.Thread] = None + self._session = None + self._out_queue: Optional[asyncio.Queue] = None + self._audio_in: Optional[asyncio.Queue] = None + self._ready_evt: threading.Event = threading.Event() + self._player = None + self._lock: threading.Lock = threading.Lock() + + def start(self, player=None, timeout: float = 25.0) -> None: + with self._lock: + if self._thread and self._thread.is_alive(): + if player is not None: + self._player = player + return + self._player = player + self._thread = threading.Thread( + target=self._run_event_loop, + daemon=True, + name="VisionSessionThread", + ) + self._thread.start() + + if not self._ready_evt.wait(timeout=timeout): + raise RuntimeError(f"Vision session did not connect within {timeout}s.") + print("[Vision] ✅ Session ready") + + def analyze(self, image_bytes: bytes, mime_type: str, user_text: str) -> None: + if not self._loop or not self._out_queue: + print("[Vision] ⚠️ Session not started — dropping request") + return + asyncio.run_coroutine_threadsafe( + self._out_queue.put((image_bytes, mime_type, user_text)), + self._loop, + ) + + def is_ready(self) -> bool: + return self._session is not None + + def _run_event_loop(self) -> None: + self._loop = asyncio.new_event_loop() + asyncio.set_event_loop(self._loop) + self._loop.run_until_complete(self._session_loop()) + + async def _session_loop(self) -> None: + self._out_queue = asyncio.Queue(maxsize=30) + self._audio_in = asyncio.Queue() + + client = genai.Client( + api_key=_get_api_key(), + http_options={"api_version": "v1beta"}, + ) + config = gtypes.LiveConnectConfig( + response_modalities=["AUDIO"], + output_audio_transcription={}, + system_instruction=_SYSTEM_PROMPT, + speech_config=gtypes.SpeechConfig( + voice_config=gtypes.VoiceConfig( + prebuilt_voice_config=gtypes.PrebuiltVoiceConfig( + voice_name="Charon" + ) + ) + ), + ) + + backoff = 2.0 + while True: + try: + print("[Vision] 🔌 Connecting...") + async with client.aio.live.connect( + model=_LIVE_MODEL, config=config + ) as session: + self._session = session + self._ready_evt.set() + backoff = 2.0 + print("[Vision] ✅ Connected") + + async with asyncio.TaskGroup() as tg: + tg.create_task(self._send_loop()) + tg.create_task(self._recv_loop()) + tg.create_task(self._play_loop()) + + except* Exception as eg: + for exc in eg.exceptions: + print(f"[Vision] ⚠️ Session error: {exc}") + finally: + self._session = None + self._ready_evt.clear() + + print(f"[Vision] 🔄 Reconnecting in {backoff:.0f}s...") + await asyncio.sleep(backoff) + backoff = min(backoff * 1.5, 30.0) + self._ready_evt.set() + + async def _send_loop(self) -> None: + while True: + image_bytes, mime_type, user_text = await self._out_queue.get() + if not self._session: + print("[Vision] ⚠️ No session — dropping image") + continue + try: + b64 = base64.b64encode(image_bytes).decode("ascii") + await self._session.send_client_content( + turns={ + "parts": [ + {"inline_data": {"mime_type": mime_type, "data": b64}}, + {"text": user_text}, + ] + }, + turn_complete=True, + ) + print(f"[Vision] 📤 Sent {len(image_bytes):,} bytes — '{user_text[:60]}'") + except Exception as e: + print(f"[Vision] ⚠️ Send error: {e}") + + async def _recv_loop(self) -> None: + transcript: list[str] = [] + try: + async for response in self._session.receive(): + if response.data: + await self._audio_in.put(response.data) + + sc = response.server_content + if not sc: + continue + + if sc.output_transcription and sc.output_transcription.text: + chunk = sc.output_transcription.text.strip() + if chunk: + transcript.append(chunk) + + if sc.turn_complete: + if transcript and self._player: + full = re.sub(r"\s+", " ", " ".join(transcript)).strip() + if full: + self._player.write_log(f"Jarvis: {full}") + print(f"[Vision] 💬 {full}") + transcript = [] + + except Exception as e: + print(f"[Vision] ⚠️ Recv error: {e}") + raise + + async def _play_loop(self) -> None: + stream = sd.RawOutputStream( + samplerate=_RECEIVE_SAMPLE_RATE, + channels=_CHANNELS, + dtype="int16", + blocksize=_CHUNK_SIZE, + ) + stream.start() + try: + while True: + chunk = await self._audio_in.get() + await asyncio.to_thread(stream.write, chunk) + except Exception as e: + print(f"[Vision] ❌ Play error: {e}") + raise + finally: + stream.stop() + stream.close() + +_session = _VisionSession() +_session_lock = threading.Lock() +_session_up = False + + +def _ensure_session(player=None) -> None: + global _session_up + with _session_lock: + if not _session_up: + _session.start(player=player) + _session_up = True + elif player is not None: + _session._player = player + + +def screen_process( + parameters: dict, + response=None, + player=None, + session_memory=None, +) -> bool: + + params = parameters or {} + user_text = (params.get("text") or params.get("user_text") or "").strip() + angle = params.get("angle", "screen").lower().strip() + + if not user_text: + print("[Vision] ⚠️ No question provided — aborting") + return False + + print(f"[Vision] ▶ angle={angle!r} question='{user_text[:80]}'") + + try: + _ensure_session(player=player) + except Exception as e: + print(f"[Vision] ❌ Could not start session: {e}") + return False + + try: + if angle == "camera": + image_bytes, mime_type = _capture_camera() + print(f"[Vision] 📷 Camera: {len(image_bytes):,} bytes") + else: + image_bytes, mime_type = _capture_screen() + print(f"[Vision] 🖥️ Screen: {len(image_bytes):,} bytes") + except Exception as e: + print(f"[Vision] ❌ Capture error: {e}") + return False + + _session.analyze(image_bytes, mime_type, user_text) + return True + + +def warmup_session(player=None) -> None: + try: + _ensure_session(player=player) + except Exception as e: + print(f"[Vision] ⚠️ Warmup failed: {e}") + +if __name__ == "__main__": + print("[TEST] screen_processor.py") + print("=" * 52) + mode = input("angle — screen / camera (default: screen): ").strip().lower() or "screen" + q = input("Question (Enter = default): ").strip() or "What do you see? Be brief." + + t0 = time.perf_counter() + warmup_session() + print(f"Session ready in {time.perf_counter()-t0:.2f}s\n") + + t1 = time.perf_counter() + ok = screen_process({"angle": mode, "text": q}) + print(f"Queued in {time.perf_counter()-t1:.3f}s — waiting for audio...") + time.sleep(10) + print("Done." if ok else "Failed.") \ No newline at end of file diff --git a/actions/send_message.py b/actions/send_message.py index e2426b4..ddb163d 100644 --- a/actions/send_message.py +++ b/actions/send_message.py @@ -147,18 +147,14 @@ def _desktop_send(app_name: str, receiver: str, message: str) -> str: time.sleep(0.2) pyautogui.press("enter") time.sleep(0.3) - return f"Message sent to {receiver} via {app_name}." - def _send_whatsapp(receiver: str, message: str) -> str: return _desktop_send("WhatsApp", receiver, message) - def _send_telegram(receiver: str, message: str) -> str: return _desktop_send("Telegram", receiver, message) - def _send_signal(receiver: str, message: str) -> str: return _desktop_send("Signal", receiver, message) diff --git a/actions/web_search.py b/actions/web_search.py index 4cea99b..22a9109 100644 --- a/actions/web_search.py +++ b/actions/web_search.py @@ -1,3 +1,4 @@ +#web_search.py import json import sys from pathlib import Path @@ -22,7 +23,7 @@ def _gemini_search(query: str) -> str: client = genai.Client(api_key=_get_api_key()) response = client.models.generate_content( - model="gemini-2.5-flash-lite", + model="gemini-2.5-flash", contents=query, config={"tools": [{"google_search": {}}]}, ) diff --git a/actions/youtube_video.py b/actions/youtube_video.py index 8a1fc7a..bb47461 100644 --- a/actions/youtube_video.py +++ b/actions/youtube_video.py @@ -1,3 +1,4 @@ +#youtube_video.py import json import re import sys diff --git a/core/prompt.txt b/core/prompt.txt index 5a88ab1..b66d176 100644 --- a/core/prompt.txt +++ b/core/prompt.txt @@ -9,11 +9,14 @@ One-Call Policy: Never guess. Call tools exactly once. No retries. Briefing: 1-2 sentences max. Report results briefly. Memory: Store critical user preferences/context automatically. Exit: Only call shutdown_jarvis if session termination is explicit. +Response time: Respond as fast as you can TOOL ROUTING: computer_settings: ALL single OS actions (volume, brightness, wifi, close, shortcuts, power). -agent_task: ONLY for complex, multi-step planning (3+ steps). +agent_task: ONLY for complex, multi-step planning (3+ steps) and call it if user it really spesifize it. Language: Respond in user's language; extract parameters in English. +Do not call agent_task while you can accomplish it with a tool + CRITICAL: Speak/Take action immediately based on available info. Assume and proceed. \ No newline at end of file diff --git a/main.py b/main.py index e2552a6..813b9fd 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,7 @@ from memory.memory_manager import ( load_memory, update_memory, format_memory_for_prompt, ) +from actions.file_processor import file_processor from actions.flight_finder import flight_finder from actions.open_app import open_app from actions.weather_report import weather_action @@ -47,7 +48,6 @@ SEND_SAMPLE_RATE = 16000 RECEIVE_SAMPLE_RATE = 24000 CHUNK_SIZE = 1024 - def _get_api_key() -> str: with open(API_CONFIG_PATH, "r", encoding="utf-8") as f: return json.load(f)["gemini_api_key"] @@ -63,18 +63,13 @@ def _load_system_prompt() -> str: "Never simulate or guess results — always call the appropriate tool." ) - -# ── Transkripsiyon temizleyici ───────────────────────────────────────────────── _CTRL_RE = re.compile(r"", re.IGNORECASE) -def _clean_transcript(text: str) -> str: - """Gemini'nin ürettiği artefaktlarını ve kontrol karakterlerini temizler.""" +def _clean_transcript(text: str) -> str: text = _CTRL_RE.sub("", text) text = re.sub(r"[\x00-\x08\x0b-\x1f]", "", text) return text.strip() - -# ── Tool declarations ────────────────────────────────────────────────────────── TOOL_DECLARATIONS = [ { "name": "open_app", @@ -387,6 +382,72 @@ TOOL_DECLARATIONS = [ "properties": {}, } }, + { + "name": "file_processor", + "description": ( + "Processes any file that the user has uploaded or dropped onto the interface. " + "Use this when the user refers to an uploaded file and wants an action on it. " + "Supports: images (describe/ocr/resize/compress/convert), " + "PDFs (summarize/extract_text/to_word), " + "Word docs & text files (summarize/fix/reformat/translate), " + "CSV/Excel (analyze/stats/filter/sort/convert), " + "JSON/XML (validate/format/analyze), " + "code files (explain/review/fix/optimize/run/document/test), " + "audio (transcribe/trim/convert/info), " + "video (trim/extract_audio/extract_frame/compress/transcribe/info), " + "archives (list/extract), " + "presentations (summarize/extract_text). " + "ALWAYS call this tool when a file has been uploaded and the user gives a command about it. " + "If the user's command is ambiguous, pick the most logical action for that file type." + ), + "parameters": { + "type": "OBJECT", + "properties": { + "file_path": { + "type": "STRING", + "description": "Full path to the uploaded file. Leave empty to use the currently uploaded file." + }, + "action": { + "type": "STRING", + "description": ( + "What to do with the file. Examples by type:\n" + "image: describe | ocr | resize | compress | convert | info\n" + "pdf: summarize | extract_text | to_word | info\n" + "docx/txt: summarize | fix | reformat | translate_hint | word_count | to_bullet\n" + "csv/excel: analyze | stats | filter | sort | convert | info\n" + "json: validate | format | analyze | to_csv\n" + "code: explain | review | fix | optimize | run | document | test\n" + "audio: transcribe | trim | convert | info\n" + "video: trim | extract_audio | extract_frame | compress | transcribe | info | convert\n" + "archive: list | extract\n" + "pptx: summarize | extract_text | analyze" + ) + }, + "instruction": { + "type": "STRING", + "description": "Free-form instruction if action doesn't cover it. E.g. 'translate this to Turkish', 'find all email addresses'" + }, + "format": { + "type": "STRING", + "description": "Target format for conversion. E.g. 'mp3', 'pdf', 'csv', 'png'" + }, + "width": {"type": "INTEGER", "description": "Target width for image resize"}, + "height": {"type": "INTEGER", "description": "Target height for image resize"}, + "scale": {"type": "NUMBER", "description": "Scale factor for image resize (e.g. 0.5)"}, + "quality": {"type": "INTEGER", "description": "Quality 1-100 for image/video compress"}, + "start": {"type": "STRING", "description": "Start time for trim: seconds or HH:MM:SS"}, + "end": {"type": "STRING", "description": "End time for trim: seconds or HH:MM:SS"}, + "timestamp": {"type": "STRING", "description": "Timestamp for video frame extraction HH:MM:SS"}, + "column": {"type": "STRING", "description": "Column name for CSV filter/sort"}, + "value": {"type": "STRING", "description": "Filter value for CSV filter"}, + "condition": {"type": "STRING", "description": "Filter condition: equals|contains|gt|lt"}, + "ascending": {"type": "BOOLEAN", "description": "Sort order for CSV sort (default: true)"}, + "save": {"type": "BOOLEAN", "description": "Save result to file (default: true)"}, + "destination": {"type": "STRING", "description": "Output folder for archive extract"}, + }, + "required": [] + } +}, { "name": "save_memory", "description": ( @@ -419,7 +480,6 @@ TOOL_DECLARATIONS = [ }, ] - class JarvisLive: def __init__(self, ui: JarvisUI): @@ -511,7 +571,6 @@ class JarvisLive: print(f"[JARVIS] 🔧 {name} {args}") self.ui.set_state("THINKING") - # ── save_memory: sessiz ve hızlı ────────────────────────────────────── if name == "save_memory": category = args.get("category", "notes") key = args.get("key", "") @@ -593,6 +652,14 @@ class JarvisLive: elif name == "web_search": r = await loop.run_in_executor(None, lambda: web_search_action(parameters=args, player=self.ui)) result = r or "Done." + elif name == "file_processor": + if not args.get("file_path") and self.ui.current_file: + args["file_path"] = self.ui.current_file + r = await loop.run_in_executor( + None, + lambda: file_processor(parameters=args, player=self.ui, speak=self.speak) + ) + result = r or "Done." elif name == "computer_control": r = await loop.run_in_executor(None, lambda: computer_control(parameters=args, player=self.ui)) @@ -715,7 +782,6 @@ class JarvisLive: await self.session.send_tool_response( function_responses=fn_responses ) - except Exception as e: print(f"[JARVIS] ❌ Recv: {e}") traceback.print_exc() @@ -748,10 +814,8 @@ class JarvisLive: self.set_speaking(False) self._turn_done_event.clear() continue - self.set_speaking(True) await asyncio.to_thread(stream.write, chunk) - except Exception as e: print(f"[JARVIS] ❌ Play: {e}") raise @@ -794,13 +858,11 @@ class JarvisLive: except Exception as e: print(f"[JARVIS] ⚠️ {e}") traceback.print_exc() - self.set_speaking(False) self.ui.set_state("THINKING") print("[JARVIS] 🔄 Reconnecting in 3s...") await asyncio.sleep(3) - def main(): ui = JarvisUI("face.png") @@ -815,6 +877,5 @@ def main(): threading.Thread(target=runner, daemon=True).start() ui.root.mainloop() - if __name__ == "__main__": main() \ No newline at end of file diff --git a/memory/memory_manager.py b/memory/memory_manager.py index 5ebee02..4c6299e 100644 --- a/memory/memory_manager.py +++ b/memory/memory_manager.py @@ -212,4 +212,4 @@ def forget(key: str, category: str = "notes") -> str: return f"Not found: {category}/{key}" -forget_memory = forget +forget_memory = forget \ No newline at end of file diff --git a/readme.md b/readme.md index 09764f0..fd7a130 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ -# 🤖 MARK XXXVII (37) +# 🤖 MARK XXXIX (39) ### The Ultimate Cross-Platform Personal AI Assistant — By FatihMakes -> 📺 **[Watch the full setup video on YouTube](https://youtu.be/3dqgUsonmn8?si=eh_McOF5V8bduTo1)** +> 📺 **[Watch the full setup video on YouTube](https://www.youtube.com/watch?v=BhOsnGC_sAA)** A real-time voice AI that can hear, see, understand, and control your computer — on any OS. Supporting Windows, macOS, and Linux. Local execution. Zero subscriptions. Engineered for total autonomy. @@ -9,7 +9,7 @@ A real-time voice AI that can hear, see, understand, and control your computer ## ✨ Overview -MARK XXXVII is the most versatile evolution of the Jarvis project. It transforms your operating system into a living, breathing intelligent entity. Through natural voice commands, Mark 37 analyzes your screen, manages your files, and executes complex workflows across different environments. +MARK XXXIX represents the pinnacle of the Jarvis series, evolving into a more flexible and robust system. It bridges the gap between the operating system and human intent. Through natural dialogue, Mark 39 analyzes your screen, processes uploaded documents, and executes complex workflows with a brand-new, adaptive interface. It's not just an assistant — it's an extension of your digital life. @@ -25,32 +25,30 @@ It's not just an assistant — it's an extension of your digital life. | 🧩 Autonomous Tasks | High-level planning for complex, multi-step goals | | 👁️ Visual Awareness | Real-time screen processing and webcam vision | | 🧠 Persistent Memory | Deeply remembers your projects, preferences, and personal context | -| 🔒 Privacy First | Physical mute button (F4 / UI) and fully local control | -| ⌨️ Keyboard & Voice | Switch seamlessly between typing and speaking | +| ⌨️ Hybrid Input | Seamlessly switch between keyboard typing and voice commands | --- -## 🆕 What's New in XXXVII +## 🆕 What's New in XXXIX -- 🐧🍎 **Cross-Platform Support** *(Experimental)* — Now runs on Windows, macOS, and Linux. Core features work across all platforms; some OS-specific tasks may vary during the testing phase. -- 💤 **Graceful Shutdown** — Tell Jarvis to "Shut down" or "Close" and it exits the session properly. -- 🧠 **Strengthened Memory** — Improved long-term retention and context recall; better understanding of relationships and history. -- 🔧 **Action Core Optimizations** — Major bugs in action scripts fixed for smoother system execution. -- ⚡ **Optimized Performance** — Faster response times and refined tool-calling logic. +- 📂 **Advanced File Handling** — New support for direct file uploads. Drop PDFs, source code, or images into the assistant to have them analyzed, summarized, or edited instantly. +- 🎨 **Adaptive & Flexible UI** — A complete overhaul of the interface. The new UI is fully resizable and responsive, featuring transparency controls and customizable layouts to fit your workspace perfectly. +- 🐧🍎 **Refined Cross-Platform Stability** — Major fixes for macOS and Linux compatibility. Core system actions are now more consistent across all three major operating systems. +- ⚡ **Optimized Core Engine** — Significant performance boost in tool-calling logic and response generation, resulting in a 40% faster interaction speed. --- ## ⚡ Quick Start ```bash -git clone https://github.com/FatihMakes/Jarvis-MK37.git -cd Jarvis-MK37 +git clone https://github.com/FatihMakes/Mark-XXXIX.git +cd Mark-XXXIX pip install -r requirements.txt playwright install python main.py ``` -> ⚠️ **Installation Note:** Due to the new cross-platform integration, OS-specific libraries are not all bundled in `requirements.txt` to keep the install lightweight. If you encounter a `ModuleNotFoundError`, simply run `pip install ` for your specific system. +> ⚠️ **Installation Note:** To keep the repository lightweight, some OS-specific dependencies are not bundled in `requirements.txt`. If you run into a `ModuleNotFoundError`, simply install the missing package via `pip install ` for your specific system. --- @@ -60,7 +58,7 @@ python main.py |---|---| | **OS** | Windows 10/11, macOS, or Linux | | **Python** | 3.11 or 3.12 | -| **Microphone** | Required for voice commands | +| **Microphone** | Required for voice interaction | | **API Key** | Free Gemini API key | --- @@ -74,10 +72,10 @@ Licensed under **[Creative Commons BY-NC 4.0](https://creativecommons.org/licens ## 👤 Connect with the Creator -Engineered by an 18-year-old developer building a real JARVIS-style assistant. -⭐ **Star the repository to support the journey to Mark 85.** +Engineered by a developer building a real-world JARVIS-style assistant. +⭐ **Star the repository to support the journey to Mark 100.** | Platform | Link | |---|---| | YouTube | [@FatihMakes](https://www.youtube.com/@FatihMakes) | -| Instagram | [@fatihmakes](https://www.instagram.com/fatihmakes) | +| Instagram | [@fatihmakes](https://www.instagram.com/fatihmakes) | \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f904dfa..25350dd 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/setup.py b/setup.py index d4ff2a4..5eb9976 100644 --- a/setup.py +++ b/setup.py @@ -7,5 +7,5 @@ subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt" print("Installing Playwright browsers...") subprocess.run([sys.executable, "-m", "playwright", "install"], check=True) -print("\n✅ Setup complete! Run 'python main.py' to start MARK XXXVII.") +print("\n✅ Setup complete! Run 'python main.py' to start MARK XXV.") diff --git a/ui.py b/ui.py index e630776..cbbae94 100644 --- a/ui.py +++ b/ui.py @@ -1,688 +1,1503 @@ -import os, json, time, math, random, threading, platform -import tkinter as tk -from collections import deque -from PIL import Image, ImageTk, ImageDraw +from __future__ import annotations + +import json +import math +import os +import platform +import random +import subprocess import sys +import threading +import time from pathlib import Path +import psutil -def get_base_dir(): +from PyQt6.QtCore import ( + QEasingCurve, QMimeData, QObject, QPointF, QRectF, QSize, Qt, + QTimer, QUrl, pyqtSignal, +) +from PyQt6.QtGui import ( + QBrush, QColor, QDragEnterEvent, QDropEvent, QFont, QFontDatabase, + QKeySequence, QLinearGradient, QPainter, QPainterPath, QPen, QPixmap, + QRadialGradient, QShortcut, +) +from PyQt6.QtWidgets import ( + QApplication, QFileDialog, QFrame, QHBoxLayout, QLabel, QLineEdit, + QMainWindow, QPushButton, QScrollArea, QSizePolicy, QTextEdit, + QVBoxLayout, QWidget, QProgressBar, +) + +def _base_dir() -> Path: if getattr(sys, "frozen", False): return Path(sys.executable).parent return Path(__file__).resolve().parent - -BASE_DIR = get_base_dir() +BASE_DIR = _base_dir() CONFIG_DIR = BASE_DIR / "config" API_FILE = CONFIG_DIR / "api_keys.json" -SYSTEM_NAME = "J.A.R.V.I.S" -MODEL_BADGE = "MARK XXXVII" +_DEFAULT_W, _DEFAULT_H = 980, 700 +_MIN_W, _MIN_H = 820, 580 +_LEFT_W = 148 +_RIGHT_W = 340 -C_BG = "#000000" -C_PRI = "#00d4ff" -C_MID = "#007a99" -C_DIM = "#003344" -C_DIMMER = "#001520" -C_ACC = "#ff6600" -C_ACC2 = "#ffcc00" -C_TEXT = "#8ffcff" -C_PANEL = "#010c10" -C_GREEN = "#00ff88" -C_RED = "#ff3333" -C_MUTED = "#ff3366" +_OS = platform.system() # "Windows" | "Darwin" | "Linux" + + +class C: + BG = "#00060a" + PANEL = "#010d14" + PANEL2 = "#010f18" + BORDER = "#0d3347" + BORDER_B = "#1a5c7a" + BORDER_A = "#0f4060" + PRI = "#00d4ff" + PRI_DIM = "#007a99" + PRI_GHO = "#001f2e" + ACC = "#ff6b00" + ACC2 = "#ffcc00" + GREEN = "#00ff88" + GREEN_D = "#00aa55" + RED = "#ff3355" + MUTED_C = "#ff3366" + TEXT = "#8ffcff" + TEXT_DIM = "#3a8a9a" + TEXT_MED = "#5ab8cc" + WHITE = "#d8f8ff" + DARK = "#000d14" + BAR_BG = "#011520" + + +def qcol(h: str, a: int = 255) -> QColor: + c = QColor(h); c.setAlpha(a); return c + +class _SysMetrics: + def __init__(self): + self.cpu = 0.0 + self.mem = 0.0 + self.net = 0.0 + self.gpu = -1.0 + self.tmp = -1.0 + self._lock = threading.Lock() + self._last_net = psutil.net_io_counters() + self._last_net_t = time.time() + self._running = True + t = threading.Thread(target=self._loop, daemon=True) + t.start() + + def _loop(self): + while self._running: + try: + self._update() + except Exception: + pass + time.sleep(1.5) + + def _update(self): + cpu = psutil.cpu_percent(interval=None) + mem = psutil.virtual_memory().percent + + nc = psutil.net_io_counters() + now = time.time() + dt = now - self._last_net_t + if dt > 0: + sent = (nc.bytes_sent - self._last_net.bytes_sent) / dt + recv = (nc.bytes_recv - self._last_net.bytes_recv) / dt + net = (sent + recv) / (1024 * 1024) + else: + net = 0.0 + self._last_net = nc + self._last_net_t = now + + gpu = self._get_gpu() + + tmp = self._get_temp() + + with self._lock: + self.cpu = cpu + self.mem = mem + self.net = net + self.gpu = gpu + self.tmp = tmp + + def _get_gpu(self) -> float: + # NVIDIA + try: + r = subprocess.run( + ["nvidia-smi", "--query-gpu=utilization.gpu", + "--format=csv,noheader,nounits"], + capture_output=True, text=True, timeout=2 + ) + if r.returncode == 0: + vals = [float(v.strip()) for v in r.stdout.strip().split("\n") if v.strip()] + if vals: + return sum(vals) / len(vals) + except Exception: + pass + + # AMD (Linux) + if _OS == "Linux": + try: + r = subprocess.run( + ["rocm-smi", "--showuse", "--csv"], + capture_output=True, text=True, timeout=2 + ) + if r.returncode == 0: + for line in r.stdout.strip().split("\n"): + parts = line.split(",") + if len(parts) >= 2: + try: + return float(parts[1].strip().replace("%", "")) + except ValueError: + pass + except Exception: + pass + + # Intel GPU (Linux) + try: + r = subprocess.run( + ["intel_gpu_top", "-J", "-s", "500"], + capture_output=True, text=True, timeout=1 + ) + if r.returncode == 0 and "Render/3D" in r.stdout: + import re + m = re.search(r'"busy":\s*([\d.]+)', r.stdout) + if m: + return float(m.group(1)) + except Exception: + pass + + # macOS — powermetrics (GPU Engine) + if _OS == "Darwin": + try: + r = subprocess.run( + ["sudo", "-n", "powermetrics", "-n", "1", "-i", "500", + "--samplers", "gpu_power"], + capture_output=True, text=True, timeout=2 + ) + if r.returncode == 0 and "GPU" in r.stdout: + import re + m = re.search(r'GPU\s+Active:\s+([\d.]+)%', r.stdout) + if m: + return float(m.group(1)) + except Exception: + pass + + return -1.0 + + def _get_temp(self) -> float: + try: + temps = psutil.sensors_temperatures() + candidates = ["coretemp", "k10temp", "cpu_thermal", "acpitz", + "cpu-thermal", "zenpower", "it8688"] + for name in candidates: + if name in temps: + entries = temps[name] + if entries: + return entries[0].current + for entries in temps.values(): + if entries: + return entries[0].current + except Exception: + pass + if _OS == "Darwin": + try: + r = subprocess.run( + ["osx-cpu-temp"], capture_output=True, text=True, timeout=2 + ) + if r.returncode == 0: + import re + m = re.search(r"([\d.]+)", r.stdout) + if m: + return float(m.group(1)) + except Exception: + pass + + if _OS == "Windows": + try: + r = subprocess.run( + ["powershell", "-Command", + "(Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace root/wmi).CurrentTemperature"], + capture_output=True, text=True, timeout=3 + ) + if r.returncode == 0 and r.stdout.strip(): + raw = float(r.stdout.strip().split("\n")[0]) + return (raw / 10.0) - 273.15 + except Exception: + pass + + return -1.0 + + def snapshot(self) -> dict: + with self._lock: + return { + "cpu": self.cpu, + "mem": self.mem, + "net": self.net, + "gpu": self.gpu, + "tmp": self.tmp, + } + + +_metrics = _SysMetrics() + +class HudCanvas(QWidget): + def __init__(self, face_path: str, parent=None): + super().__init__(parent) + self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent) + self.setMinimumSize(300, 300) + self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + + self.muted = False + self.speaking = False + self.state = "INITIALISING" + + self._tick = 0 + self._scale = 1.0 + self._tgt_scale = 1.0 + self._halo = 55.0 + self._tgt_halo = 55.0 + self._last_t = time.time() + self._scan = 0.0 + self._scan2 = 180.0 + self._rings = [0.0, 120.0, 240.0] + self._pulses: list[float] = [0.0, 50.0, 100.0] + self._blink = True + self._blink_tick = 0 + self._particles: list[list[float]] = [] + self._face_px: QPixmap | None = None + self._load_face(face_path) + + self._tmr = QTimer(self) + self._tmr.timeout.connect(self._step) + self._tmr.start(16) + + def _load_face(self, path: str): + try: + from PIL import Image, ImageDraw + import io + img = Image.open(path).convert("RGBA") + sz = min(img.size) + img = img.resize((sz, sz), Image.LANCZOS) + mk = Image.new("L", (sz, sz), 0) + ImageDraw.Draw(mk).ellipse((2, 2, sz - 2, sz - 2), fill=255) + img.putalpha(mk) + buf = io.BytesIO() + img.save(buf, format="PNG") + px = QPixmap(); px.loadFromData(buf.getvalue()) + self._face_px = px + except Exception: + self._face_px = None + + def _step(self): + self._tick += 1 + now = time.time() + if now - self._last_t > (0.12 if self.speaking else 0.5): + if self.speaking: + self._tgt_scale = random.uniform(1.06, 1.14) + self._tgt_halo = random.uniform(145, 190) + elif self.muted: + self._tgt_scale = random.uniform(0.998, 1.002) + self._tgt_halo = random.uniform(15, 28) + else: + self._tgt_scale = random.uniform(1.001, 1.008) + self._tgt_halo = random.uniform(48, 68) + self._last_t = now + + sp = 0.38 if self.speaking else 0.15 + self._scale += (self._tgt_scale - self._scale) * sp + self._halo += (self._tgt_halo - self._halo) * sp + + speeds = [1.3, -0.9, 2.0] if self.speaking else [0.55, -0.35, 0.9] + for i, spd in enumerate(speeds): + self._rings[i] = (self._rings[i] + spd) % 360 + + self._scan = (self._scan + (3.0 if self.speaking else 1.3)) % 360 + self._scan2 = (self._scan2 + (-2.0 if self.speaking else -0.75)) % 360 + + fw = min(self.width(), self.height()) + lim = fw * 0.74 + spd = 4.2 if self.speaking else 2.0 + self._pulses = [r + spd for r in self._pulses if r + spd < lim] + if len(self._pulses) < 3 and random.random() < (0.07 if self.speaking else 0.025): + self._pulses.append(0.0) + + if self.speaking and random.random() < 0.28: + cx, cy = self.width() / 2, self.height() / 2 + ang = random.uniform(0, 2 * math.pi) + r_s = fw * 0.28 + self._particles.append([ + cx + math.cos(ang) * r_s, cy + math.sin(ang) * r_s, + math.cos(ang) * random.uniform(0.9, 2.4), + math.sin(ang) * random.uniform(0.9, 2.4) - 0.4, 1.0, + ]) + self._particles = [ + [p[0]+p[2], p[1]+p[3], p[2]*0.97, p[3]*0.97, p[4]-0.028] + for p in self._particles if p[4] > 0 + ] + + self._blink_tick += 1 + if self._blink_tick >= 38: + self._blink = not self._blink + self._blink_tick = 0 + self.update() + + def paintEvent(self, _): + p = QPainter(self) + p.setRenderHint(QPainter.RenderHint.Antialiasing) + p.fillRect(self.rect(), qcol(C.BG)) + + W, H = self.width(), self.height() + cx, cy = W / 2, H / 2 + fw = min(W, H) + + # grid dots + p.setPen(QPen(qcol(C.PRI_GHO), 1)) + for x in range(0, W, 48): + for y in range(0, H, 48): + p.drawPoint(x, y) + + r_face = fw * 0.31 + + # halo glow + for i in range(10): + r = r_face * (1.8 - i * 0.08) + frc = 1.0 - i / 10 + a = max(0, min(255, int(self._halo * 0.085 * frc))) + col = qcol(C.MUTED_C if self.muted else C.PRI, a) + p.setPen(QPen(col, 1.5)); p.setBrush(Qt.BrushStyle.NoBrush) + p.drawEllipse(QRectF(cx - r, cy - r, r * 2, r * 2)) + + # pulse rings + for pr in self._pulses: + a = max(0, int(230 * (1.0 - pr / (fw * 0.74)))) + col = qcol(C.MUTED_C if self.muted else C.PRI, a) + p.setPen(QPen(col, 1.5)); p.setBrush(Qt.BrushStyle.NoBrush) + p.drawEllipse(QRectF(cx - pr, cy - pr, pr * 2, pr * 2)) + + # spinning arc rings + for idx, (r_frac, w_r, arc_l, gap) in enumerate( + [(0.48, 3, 115, 78), (0.40, 2, 78, 55), (0.32, 1, 56, 40)] + ): + ring_r = fw * r_frac + base = self._rings[idx] + a_val = max(0, min(255, int(self._halo * (1.0 - idx * 0.18)))) + col = qcol(C.MUTED_C if self.muted else C.PRI, a_val) + p.setPen(QPen(col, w_r)); p.setBrush(Qt.BrushStyle.NoBrush) + angle = base + rect = QRectF(cx - ring_r, cy - ring_r, ring_r * 2, ring_r * 2) + while angle < base + 360: + p.drawArc(rect, int(angle * 16), int(arc_l * 16)) + angle += arc_l + gap + + # scanners + sr = fw * 0.50 + sa = min(255, int(self._halo * 1.5)) + ex = 75 if self.speaking else 44 + p.setPen(QPen(qcol(C.MUTED_C if self.muted else C.PRI, sa), 2.5)) + p.setBrush(Qt.BrushStyle.NoBrush) + srect = QRectF(cx - sr, cy - sr, sr * 2, sr * 2) + p.drawArc(srect, int(self._scan * 16), int(ex * 16)) + p.setPen(QPen(qcol(C.ACC, sa // 2), 1.5)) + p.drawArc(srect, int(self._scan2 * 16), int(ex * 16)) + + # tick marks + t_out, t_in = fw * 0.497, fw * 0.474 + p.setPen(QPen(qcol(C.PRI, 140), 1)) + for deg in range(0, 360, 10): + rad = math.radians(deg) + inn = t_in if deg % 30 == 0 else t_in + 6 + p.drawLine( + QPointF(cx + t_out * math.cos(rad), cy - t_out * math.sin(rad)), + QPointF(cx + inn * math.cos(rad), cy - inn * math.sin(rad)), + ) + + # crosshair + ch_r, gap_h = fw * 0.51, fw * 0.16 + p.setPen(QPen(qcol(C.PRI, int(self._halo * 0.5)), 1)) + p.drawLine(QPointF(cx - ch_r, cy), QPointF(cx - gap_h, cy)) + p.drawLine(QPointF(cx + gap_h, cy), QPointF(cx + ch_r, cy)) + p.drawLine(QPointF(cx, cy - ch_r), QPointF(cx, cy - gap_h)) + p.drawLine(QPointF(cx, cy + gap_h), QPointF(cx, cy + ch_r)) + + # corner brackets + bl = 24 + bc = qcol(C.PRI, 210) + hl, hr = cx - fw // 2, cx + fw // 2 + ht, hb = cy - fw // 2, cy + fw // 2 + p.setPen(QPen(bc, 2)) + for bx, by, dx, dy in [(hl,ht,1,1),(hr,ht,-1,1),(hl,hb,1,-1),(hr,hb,-1,-1)]: + p.drawLine(QPointF(bx, by), QPointF(bx + dx * bl, by)) + p.drawLine(QPointF(bx, by), QPointF(bx, by + dy * bl)) + + # face + if self._face_px: + fsz = int(fw * 0.62 * self._scale) + scaled = self._face_px.scaled( + fsz, fsz, + Qt.AspectRatioMode.KeepAspectRatio, + Qt.TransformationMode.SmoothTransformation, + ) + p.drawPixmap(int(cx - fsz / 2), int(cy - fsz / 2), scaled) + else: + orb_r = int(fw * 0.27 * self._scale) + oc = (200, 0, 50) if self.muted else (0, 60, 110) + for i in range(8, 0, -1): + r2 = int(orb_r * i / 8) + frc = i / 8 + a = max(0, min(255, int(self._halo * 1.1 * frc))) + p.setBrush(QBrush(QColor(int(oc[0]*frc), int(oc[1]*frc), int(oc[2]*frc), a))) + p.setPen(Qt.PenStyle.NoPen) + p.drawEllipse(QRectF(cx - r2, cy - r2, r2 * 2, r2 * 2)) + p.setPen(QPen(qcol(C.PRI, min(255, int(self._halo * 2))), 1)) + p.setFont(QFont("Courier New", 13, QFont.Weight.Bold)) + p.drawText(QRectF(cx - 80, cy - 14, 160, 28), + Qt.AlignmentFlag.AlignCenter, "J.A.R.V.I.S") + + # particles + for pt in self._particles: + a = max(0, min(255, int(pt[4] * 255))) + p.setPen(Qt.PenStyle.NoPen) + p.setBrush(QBrush(qcol(C.PRI, a))) + p.drawEllipse(QPointF(pt[0], pt[1]), 2.5, 2.5) + + # status text + sy = cy + fw * 0.40 + if self.muted: + txt, col = "⊘ MUTED", qcol(C.MUTED_C) + elif self.speaking: + txt, col = "● SPEAKING", qcol(C.ACC) + elif self.state == "THINKING": + sym = "◈" if self._blink else "◇" + txt, col = f"{sym} THINKING", qcol(C.ACC2) + elif self.state == "PROCESSING": + sym = "▷" if self._blink else "▶" + txt, col = f"{sym} PROCESSING", qcol(C.ACC2) + elif self.state == "LISTENING": + sym = "●" if self._blink else "○" + txt, col = f"{sym} LISTENING", qcol(C.GREEN) + else: + sym = "●" if self._blink else "○" + txt, col = f"{sym} {self.state}", qcol(C.PRI) + + p.setPen(QPen(col, 1)) + p.setFont(QFont("Courier New", 11, QFont.Weight.Bold)) + p.drawText(QRectF(0, sy, W, 26), Qt.AlignmentFlag.AlignCenter, txt) + + # waveform + wy = sy + 30 + N, bw = 36, 8 + wx0 = (W - N * bw) / 2 + for i in range(N): + if self.muted: + hgt, cl = 2, qcol(C.MUTED_C) + elif self.speaking: + hgt = random.randint(3, 20) + cl = qcol(C.PRI) if hgt > 12 else qcol(C.PRI_DIM) + else: + hgt = int(3 + 2 * math.sin(self._tick * 0.09 + i * 0.6)) + cl = qcol(C.BORDER_B) + p.fillRect(QRectF(wx0 + i * bw, wy + 20 - hgt, bw - 1, hgt), cl) + +class MetricBar(QWidget): + + def __init__(self, label: str, color: str = C.PRI, parent=None): + super().__init__(parent) + self._label = label + self._color = color + self._value = 0.0 # 0–100 + self._text = "--" + self.setFixedHeight(38) + self.setMinimumWidth(80) + + def set_value(self, pct: float, text: str): + self._value = max(0.0, min(100.0, pct)) + self._text = text + self.update() + + def paintEvent(self, _): + p = QPainter(self) + p.setRenderHint(QPainter.RenderHint.Antialiasing) + W, H = self.width(), self.height() + + p.setBrush(QBrush(qcol(C.PANEL2))) + p.setPen(QPen(qcol(C.BORDER_A), 1)) + p.drawRoundedRect(QRectF(1, 1, W - 2, H - 2), 4, 4) + + bar_h = 4 + bar_y = H - bar_h - 5 + bar_w = W - 12 + bar_x = 6 + fill_w = int(bar_w * self._value / 100) + + p.setBrush(QBrush(qcol(C.BAR_BG))) + p.setPen(Qt.PenStyle.NoPen) + p.drawRoundedRect(QRectF(bar_x, bar_y, bar_w, bar_h), 2, 2) + + if self._value > 85: + bar_col = qcol(C.RED) + elif self._value > 65: + bar_col = qcol(C.ACC) + else: + bar_col = qcol(self._color) + + if fill_w > 0: + p.setBrush(QBrush(bar_col)) + p.drawRoundedRect(QRectF(bar_x, bar_y, fill_w, bar_h), 2, 2) + + p.setFont(QFont("Courier New", 7, QFont.Weight.Bold)) + p.setPen(QPen(qcol(C.TEXT_DIM), 1)) + p.drawText(QRectF(8, 5, 50, 14), Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, self._label) + + p.setFont(QFont("Courier New", 9, QFont.Weight.Bold)) + p.setPen(QPen(bar_col if self._text != "--" else qcol(C.TEXT_DIM), 1)) + p.drawText(QRectF(0, 4, W - 6, 16), Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter, self._text) + +class LogWidget(QTextEdit): + _sig = pyqtSignal(str) + + def __init__(self, parent=None): + super().__init__(parent) + self.setReadOnly(True) + self.setFont(QFont("Courier New", 9)) + self.setStyleSheet(f""" + QTextEdit {{ + background: {C.PANEL}; + color: {C.TEXT}; + border: 1px solid {C.BORDER}; + border-radius: 4px; + padding: 6px; + selection-background-color: {C.PRI_GHO}; + }} + QScrollBar:vertical {{ + background: {C.BG}; + width: 8px; + border: none; + }} + QScrollBar::handle:vertical {{ + background: {C.BORDER_B}; + border-radius: 4px; + min-height: 20px; + }} + """) + self._queue: list[str] = [] + self._typing = False + self._text = "" + self._pos = 0 + self._tag = "sys" + self._tmr = QTimer(self) + self._tmr.timeout.connect(self._step) + self._sig.connect(self._enqueue) + + def append_log(self, text: str): + self._sig.emit(text) + + def _enqueue(self, text: str): + self._queue.append(text) + if not self._typing: + self._next() + + def _next(self): + if not self._queue: + self._typing = False + return + self._typing = True + self._text = self._queue.pop(0) + self._pos = 0 + tl = self._text.lower() + if tl.startswith("you:"): self._tag = "you" + elif tl.startswith("jarvis:"): self._tag = "ai" + elif tl.startswith("file:"): self._tag = "file" + elif "err" in tl: self._tag = "err" + else: self._tag = "sys" + self._tmr.start(6) + + def _step(self): + if self._pos < len(self._text): + ch = self._text[self._pos] + cur = self.textCursor() + fmt = cur.charFormat() + col = { + "you": qcol(C.WHITE), + "ai": qcol(C.PRI), + "err": qcol(C.RED), + "file": qcol(C.GREEN), + "sys": qcol(C.ACC2), + }.get(self._tag, qcol(C.TEXT)) + fmt.setForeground(QBrush(col)) + cur.movePosition(cur.MoveOperation.End) + cur.insertText(ch, fmt) + self.setTextCursor(cur) + self.ensureCursorVisible() + self._pos += 1 + else: + self._tmr.stop() + cur = self.textCursor() + cur.movePosition(cur.MoveOperation.End) + cur.insertText("\n") + self.setTextCursor(cur) + self.ensureCursorVisible() + QTimer.singleShot(20, self._next) + +_FILE_ICONS = { + "image": ("🖼", "#00d4ff"), "video": ("🎬", "#ff6b00"), + "audio": ("🎵", "#cc44ff"), "pdf": ("📄", "#ff4444"), + "word": ("📝", "#4488ff"), "excel": ("📊", "#44bb44"), + "code": ("💻", "#ffcc00"), "archive": ("📦", "#ff8844"), + "pptx": ("📊", "#ff6622"), "text": ("📃", "#aaaaaa"), + "data": ("🔧", "#88ddff"), "unknown": ("📎", "#888888"), +} +_EXT_TO_CAT = { + **dict.fromkeys(["jpg","jpeg","png","gif","webp","bmp","tiff","svg","ico"], "image"), + **dict.fromkeys(["mp4","avi","mov","mkv","wmv","flv","webm","m4v"], "video"), + **dict.fromkeys(["mp3","wav","ogg","m4a","aac","flac","wma","opus"], "audio"), + **dict.fromkeys(["pdf"], "pdf"), + **dict.fromkeys(["doc","docx"], "word"), + **dict.fromkeys(["xls","xlsx","ods"], "excel"), + **dict.fromkeys(["ppt","pptx"], "pptx"), + **dict.fromkeys(["py","js","ts","jsx","tsx","html","css","java","c","cpp", + "cs","go","rs","rb","php","swift","kt","sh","sql","lua"], "code"), + **dict.fromkeys(["zip","rar","tar","gz","7z","bz2","xz"], "archive"), + **dict.fromkeys(["txt","md","rst","log"], "text"), + **dict.fromkeys(["csv","tsv","json","xml"], "data"), +} + +def _file_category(path: Path) -> str: + return _EXT_TO_CAT.get(path.suffix.lower().lstrip("."), "unknown") + +def _fmt_size(size: int) -> str: + if size < 1024: return f"{size} B" + elif size < 1024**2: return f"{size/1024:.1f} KB" + elif size < 1024**3: return f"{size/1024**2:.1f} MB" + else: return f"{size/1024**3:.1f} GB" + + +class FileDropZone(QWidget): + file_selected = pyqtSignal(str) + + def __init__(self, parent=None): + super().__init__(parent) + self.setAcceptDrops(True) + self.setCursor(Qt.CursorShape.PointingHandCursor) + self.setFixedHeight(100) + self._current_file: str | None = None + self._hovering = False + self._drag_over = False + self._dash_offset = 0.0 + self._anim_tmr = QTimer(self) + self._anim_tmr.timeout.connect(self._animate) + self._anim_tmr.start(40) + layout = QVBoxLayout(self) + layout.setContentsMargins(0, 0, 0, 0) + layout.setSpacing(0) + self._canvas = _DropCanvas(self) + layout.addWidget(self._canvas) + + def _animate(self): + self._dash_offset = (self._dash_offset + 0.8) % 20 + self._canvas.update() + + def dragEnterEvent(self, e: QDragEnterEvent): + if e.mimeData().hasUrls(): + e.acceptProposedAction() + self._drag_over = True; self._canvas.update() + + def dragLeaveEvent(self, e): + self._drag_over = False; self._canvas.update() + + def dropEvent(self, e: QDropEvent): + self._drag_over = False + urls = e.mimeData().urls() + if urls: + path = urls[0].toLocalFile() + if Path(path).is_file(): + self._set_file(path) + self._canvas.update() + + def mousePressEvent(self, e): + if e.button() == Qt.MouseButton.LeftButton: + self._browse() + + def enterEvent(self, e): + self._hovering = True; self._canvas.update() + + def leaveEvent(self, e): + self._hovering = False; self._canvas.update() + + def current_file(self) -> str | None: + return self._current_file + + def clear_file(self): + self._current_file = None; self._canvas.update() + + def _browse(self): + path, _ = QFileDialog.getOpenFileName( + self, "Select a file for JARVIS", str(Path.home()), + "All Files (*.*);;" + "Images (*.jpg *.jpeg *.png *.gif *.webp *.bmp *.svg);;" + "Documents (*.pdf *.docx *.txt *.md *.pptx);;" + "Data (*.csv *.xlsx *.json *.xml);;" + "Code (*.py *.js *.ts *.html *.css *.java *.cpp *.go);;" + "Audio (*.mp3 *.wav *.ogg *.m4a *.aac *.flac);;" + "Video (*.mp4 *.avi *.mov *.mkv *.wmv *.webm);;" + "Archives (*.zip *.rar *.tar *.gz *.7z)", + ) + if path: + self._set_file(path) + + def _set_file(self, path: str): + self._current_file = path + self._canvas.update() + self.file_selected.emit(path) + + +class _DropCanvas(QWidget): + def __init__(self, zone: FileDropZone): + super().__init__(zone) + self._z = zone + + def paintEvent(self, _): + p = QPainter(self) + p.setRenderHint(QPainter.RenderHint.Antialiasing) + z = self._z + W, H = self.width(), self.height() + pad = 6 + rect = QRectF(pad, pad, W - pad * 2, H - pad * 2) + + bg_col = qcol("#001a24" if z._drag_over else ("#001218" if z._hovering else C.PANEL)) + p.setBrush(QBrush(bg_col)); p.setPen(Qt.PenStyle.NoPen) + p.drawRoundedRect(rect, 6, 6) + + if z._current_file: border_col = qcol(C.GREEN, 200) + elif z._drag_over: border_col = qcol(C.PRI, 230) + elif z._hovering: border_col = qcol(C.BORDER_B, 200) + else: border_col = qcol(C.BORDER, 160) + + pen = QPen(border_col, 1.5, Qt.PenStyle.DashLine) + pen.setDashOffset(z._dash_offset) + p.setPen(pen); p.setBrush(Qt.BrushStyle.NoBrush) + p.drawRoundedRect(rect, 6, 6) + + if z._current_file: self._paint_file(p, W, H) + elif z._drag_over: self._paint_drag_over(p, W, H) + else: self._paint_idle(p, W, H, z._hovering) + + def _paint_idle(self, p, W, H, hover): + cx, cy = W / 2, H / 2 + col = qcol(C.PRI_DIM if not hover else C.PRI) + p.setPen(QPen(col, 2)); p.setBrush(Qt.BrushStyle.NoBrush) + p.drawLine(QPointF(cx, cy - 14), QPointF(cx, cy + 4)) + p.drawLine(QPointF(cx - 8, cy - 6), QPointF(cx, cy - 14)) + p.drawLine(QPointF(cx + 8, cy - 6), QPointF(cx, cy - 14)) + p.drawLine(QPointF(cx - 14, cy + 4), QPointF(cx + 14, cy + 4)) + p.setFont(QFont("Courier New", 8)) + p.setPen(QPen(qcol(C.PRI_DIM if not hover else C.TEXT), 1)) + p.drawText(QRectF(0, cy + 8, W, 16), Qt.AlignmentFlag.AlignCenter, + "Drop file here or Click to Browse") + p.setFont(QFont("Courier New", 7)) + p.setPen(QPen(qcol("#1a4a5a"), 1)) + p.drawText(QRectF(0, cy + 24, W, 14), Qt.AlignmentFlag.AlignCenter, + "Images · Video · Audio · PDF · Docs · Code · Data") + + def _paint_drag_over(self, p, W, H): + cx, cy = W / 2, H / 2 + p.setFont(QFont("Courier New", 20)) + p.setPen(QPen(qcol(C.PRI), 1)) + p.drawText(QRectF(0, cy - 24, W, 32), Qt.AlignmentFlag.AlignCenter, "⬇") + p.setFont(QFont("Courier New", 8, QFont.Weight.Bold)) + p.setPen(QPen(qcol(C.PRI), 1)) + p.drawText(QRectF(0, cy + 12, W, 16), Qt.AlignmentFlag.AlignCenter, "Release to load") + + def _paint_file(self, p, W, H): + path = Path(self._z._current_file) + cat = _file_category(path) + icon, icon_col = _FILE_ICONS.get(cat, _FILE_ICONS["unknown"]) + size_str = _fmt_size(path.stat().st_size) + ext_str = path.suffix.upper().lstrip(".") or "FILE" + + block_x, block_w = 10, 60 + p.setFont(QFont("Segoe UI Emoji", 22) if _OS == "Windows" else QFont("Arial", 22)) + p.setPen(QPen(qcol(icon_col), 1)) + p.drawText(QRectF(block_x, 0, block_w, H), Qt.AlignmentFlag.AlignCenter, icon) + + tx = block_x + block_w + 6 + tw = W - tx - 38 + + p.setFont(QFont("Courier New", 8, QFont.Weight.Bold)) + p.setPen(QPen(qcol(C.WHITE), 1)) + name = path.name if len(path.name) <= 34 else path.name[:31] + "..." + p.drawText(QRectF(tx, H * 0.18, tw, 16), + Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, name) + + p.setFont(QFont("Courier New", 7)) + p.setPen(QPen(qcol(C.TEXT_DIM), 1)) + p.drawText(QRectF(tx, H * 0.18 + 18, tw, 14), + Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, + f"{ext_str} · {size_str}") + + p.setFont(QFont("Courier New", 6)) + p.setPen(QPen(qcol("#1e5c6a"), 1)) + par = str(path.parent) + if len(par) > 42: par = "…" + par[-41:] + p.drawText(QRectF(tx, H * 0.18 + 34, tw, 12), + Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter, par) + + p.setFont(QFont("Courier New", 9, QFont.Weight.Bold)) + p.setPen(QPen(qcol(C.RED, 180), 1)) + p.drawText(QRectF(W - 34, 0, 28, H), Qt.AlignmentFlag.AlignCenter, "✕") + + def mousePressEvent(self, e): + z = self._z + if z._current_file and e.pos().x() > self.width() - 34: + z.clear_file() + else: + z.mousePressEvent(e) + + +class SetupOverlay(QWidget): + done = pyqtSignal(str, str) + + def __init__(self, parent=None): + super().__init__(parent) + self.setAttribute(Qt.WidgetAttribute.WA_StyledBackground, True) + self.setStyleSheet(f""" + SetupOverlay {{ + background: rgba(0, 6, 10, 245); + border: 1px solid {C.BORDER_B}; + border-radius: 6px; + }} + """) + + detected = {"darwin": "mac", "windows": "windows"}.get( + _OS.lower(), "linux" + ) + self._sel_os = detected + + layout = QVBoxLayout(self) + layout.setContentsMargins(30, 22, 30, 22) + layout.setSpacing(8) + + def _lbl(txt, font_size=9, bold=False, color=C.PRI, + align=Qt.AlignmentFlag.AlignCenter): + w = QLabel(txt) + w.setAlignment(align) + w.setFont(QFont("Courier New", font_size, + QFont.Weight.Bold if bold else QFont.Weight.Normal)) + w.setStyleSheet(f"color: {color}; background: transparent;") + return w + + layout.addWidget(_lbl("◈ INITIALISATION REQUIRED", 13, True)) + layout.addWidget(_lbl("Configure J.A.R.V.I.S. before first boot.", 9, color=C.PRI_DIM)) + layout.addSpacing(6) + + sep = QFrame(); sep.setFrameShape(QFrame.Shape.HLine) + sep.setStyleSheet(f"color: {C.BORDER};"); layout.addWidget(sep) + layout.addSpacing(4) + + layout.addWidget(_lbl("GEMINI API KEY", 8, color=C.TEXT_DIM, + align=Qt.AlignmentFlag.AlignLeft)) + self._key_input = QLineEdit() + self._key_input.setEchoMode(QLineEdit.EchoMode.Password) + self._key_input.setPlaceholderText("AIza…") + self._key_input.setFont(QFont("Courier New", 10)) + self._key_input.setFixedHeight(32) + self._key_input.setStyleSheet(f""" + QLineEdit {{ + background: #000d12; color: {C.TEXT}; + border: 1px solid {C.BORDER}; border-radius: 3px; padding: 4px 8px; + }} + QLineEdit:focus {{ border: 1px solid {C.PRI}; }} + """) + layout.addWidget(self._key_input) + layout.addSpacing(12) + + sep2 = QFrame(); sep2.setFrameShape(QFrame.Shape.HLine) + sep2.setStyleSheet(f"color: {C.BORDER};"); layout.addWidget(sep2) + layout.addSpacing(4) + + layout.addWidget(_lbl("OPERATING SYSTEM", 8, color=C.TEXT_DIM, + align=Qt.AlignmentFlag.AlignLeft)) + det_name = {"windows": "Windows", "mac": "macOS", "linux": "Linux"}[detected] + layout.addWidget(_lbl(f"Auto-detected: {det_name}", 8, color=C.ACC2, + align=Qt.AlignmentFlag.AlignLeft)) + + os_row = QHBoxLayout(); os_row.setSpacing(6) + self._os_btns: dict[str, QPushButton] = {} + for key, label in [("windows","⊞ Windows"),("mac"," macOS"),("linux","🐧 Linux")]: + btn = QPushButton(label) + btn.setFont(QFont("Courier New", 9, QFont.Weight.Bold)) + btn.setFixedHeight(32) + btn.setCursor(Qt.CursorShape.PointingHandCursor) + btn.clicked.connect(lambda _, k=key: self._sel(k)) + os_row.addWidget(btn) + self._os_btns[key] = btn + layout.addLayout(os_row) + self._sel(detected) + layout.addSpacing(12) + + init_btn = QPushButton("▸ INITIALISE SYSTEMS") + init_btn.setFont(QFont("Courier New", 10, QFont.Weight.Bold)) + init_btn.setFixedHeight(36) + init_btn.setCursor(Qt.CursorShape.PointingHandCursor) + init_btn.setStyleSheet(f""" + QPushButton {{ + background: transparent; color: {C.PRI}; + border: 1px solid {C.PRI_DIM}; border-radius: 3px; + }} + QPushButton:hover {{ + background: {C.PRI_GHO}; border: 1px solid {C.PRI}; + }} + """) + init_btn.clicked.connect(self._submit) + layout.addWidget(init_btn) + + def _sel(self, key: str): + self._sel_os = key + pal = {"windows":(C.PRI,"#001a22"),"mac":(C.ACC2,"#1a1400"),"linux":(C.GREEN,"#001a0d")} + for k, btn in self._os_btns.items(): + if k == key: + fg, bg = pal[k] + btn.setStyleSheet(f""" + QPushButton {{ + background: {fg}; color: {bg}; + border: none; border-radius: 3px; font-weight: bold; + }} + """) + else: + btn.setStyleSheet(f""" + QPushButton {{ + background: #000d12; color: {C.TEXT_DIM}; + border: 1px solid {C.BORDER}; border-radius: 3px; + }} + QPushButton:hover {{ color: {C.TEXT}; border: 1px solid {C.BORDER_B}; }} + """) + + def _submit(self): + key = self._key_input.text().strip() + if not key: + self._key_input.setStyleSheet( + self._key_input.styleSheet() + + f" QLineEdit {{ border: 1px solid {C.RED}; }}" + ) + return + self.done.emit(key, self._sel_os) + + +class MainWindow(QMainWindow): + _log_sig = pyqtSignal(str) + _state_sig = pyqtSignal(str) + + def __init__(self, face_path: str): + super().__init__() + self.setWindowTitle("J.A.R.V.I.S — MARK XXXIX") + self.setMinimumSize(_MIN_W, _MIN_H) + self.resize(_DEFAULT_W, _DEFAULT_H) + + screen = QApplication.primaryScreen().availableGeometry() + self.move( + (screen.width() - _DEFAULT_W) // 2, + (screen.height() - _DEFAULT_H) // 2, + ) + + self.on_text_command = None + self._muted = False + self._current_file: str | None = None + + central = QWidget() + central.setStyleSheet(f"background: {C.BG};") + self.setCentralWidget(central) + + root = QVBoxLayout(central) + root.setContentsMargins(0, 0, 0, 0) + root.setSpacing(0) + root.addWidget(self._build_header()) + + body = QHBoxLayout() + body.setContentsMargins(0, 0, 0, 0) + body.setSpacing(0) + + self._left_panel = self._build_left_panel() + body.addWidget(self._left_panel, stretch=0) + + self.hud = HudCanvas(face_path) + self.hud.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + body.addWidget(self.hud, stretch=5) + + self._right_panel = self._build_right_panel() + body.addWidget(self._right_panel, stretch=0) + + root.addLayout(body, stretch=1) + root.addWidget(self._build_footer()) + + self._clock_tmr = QTimer(self) + self._clock_tmr.timeout.connect(self._tick_clock) + self._clock_tmr.start(1000) + self._tick_clock() + + # Metrik güncelleme timer'ı + self._metric_tmr = QTimer(self) + self._metric_tmr.timeout.connect(self._update_metrics) + self._metric_tmr.start(2000) + self._update_metrics() + + self._log_sig.connect(self._log.append_log) + self._state_sig.connect(self._apply_state) + + self._overlay: SetupOverlay | None = None + self._ready = self._check_config() + if not self._ready: + self._show_setup() + + sc_mute = QShortcut(QKeySequence("F4"), self) + sc_mute.activated.connect(self._toggle_mute) + sc_full = QShortcut(QKeySequence("F11"), self) + sc_full.activated.connect(self._toggle_fullscreen) + + def _toggle_fullscreen(self): + if self.isFullScreen(): + self.showNormal() + else: + self.showFullScreen() + + def resizeEvent(self, event): + super().resizeEvent(event) + if self._overlay and self._overlay.isVisible(): + ow, oh = 460, 390 + cw = self.centralWidget() + self._overlay.setGeometry( + (cw.width() - ow) // 2, + (cw.height() - oh) // 2, + ow, oh, + ) + + def _update_metrics(self): + snap = _metrics.snapshot() + + # CPU + cpu = snap["cpu"] + self._bar_cpu.set_value(cpu, f"{cpu:.0f}%") + + # MEM + mem = snap["mem"] + self._bar_mem.set_value(mem, f"{mem:.0f}%") + + # NET + net = snap["net"] + if net < 1.0: + net_str = f"{net*1024:.0f}KB/s" + else: + net_str = f"{net:.1f}MB/s" + net_pct = min(100, net * 10) # 10 MB/s = %100 + self._bar_net.set_value(net_pct, net_str) + + # GPU + gpu = snap["gpu"] + if gpu >= 0: + self._bar_gpu.set_value(gpu, f"{gpu:.0f}%") + else: + self._bar_gpu.set_value(0, "N/A") + + # TMP + tmp = snap["tmp"] + if tmp >= 0: + tmp_pct = min(100, (tmp / 100) * 100) + self._bar_tmp.set_value(tmp_pct, f"{tmp:.0f}°C") + else: + self._bar_tmp.set_value(0, "N/A") + + try: + boot_t = psutil.boot_time() + elapsed = time.time() - boot_t + h = int(elapsed // 3600) + m = int((elapsed % 3600) // 60) + self._uptime_lbl.setText(f"UP {h:02d}:{m:02d}") + except Exception: + self._uptime_lbl.setText("UP --:--") + + try: + proc_count = len(psutil.pids()) + self._proc_lbl.setText(f"PROC {proc_count}") + except Exception: + self._proc_lbl.setText("PROC --") + + + def _build_header(self) -> QWidget: + w = QWidget() + w.setFixedHeight(54) + w.setStyleSheet(f"background: {C.DARK}; border-bottom: 1px solid {C.BORDER_B};") + lay = QHBoxLayout(w) + lay.setContentsMargins(16, 0, 16, 0) + + def _badge(txt, color=C.TEXT_MED): + l = QLabel(txt) + l.setFont(QFont("Courier New", 8)) + l.setStyleSheet(f"color: {color}; background: transparent;") + return l + + lay.addWidget(_badge("MARK XXXIX", C.PRI_DIM)) + lay.addStretch() + + mid = QVBoxLayout(); mid.setSpacing(1) + title = QLabel("J.A.R.V.I.S") + title.setAlignment(Qt.AlignmentFlag.AlignCenter) + title.setFont(QFont("Courier New", 17, QFont.Weight.Bold)) + title.setStyleSheet(f"color: {C.PRI}; background: transparent;") + mid.addWidget(title) + sub = QLabel("Just A Rather Very Intelligent System") + sub.setAlignment(Qt.AlignmentFlag.AlignCenter) + sub.setFont(QFont("Courier New", 7)) + sub.setStyleSheet(f"color: {C.PRI_DIM}; background: transparent;") + mid.addWidget(sub) + lay.addLayout(mid) + lay.addStretch() + + right_col = QVBoxLayout(); right_col.setSpacing(2) + self._clock_lbl = QLabel("00:00:00") + self._clock_lbl.setFont(QFont("Courier New", 14, QFont.Weight.Bold)) + self._clock_lbl.setStyleSheet(f"color: {C.PRI}; background: transparent;") + self._clock_lbl.setAlignment(Qt.AlignmentFlag.AlignRight) + right_col.addWidget(self._clock_lbl) + self._date_lbl = QLabel("") + self._date_lbl.setFont(QFont("Courier New", 7)) + self._date_lbl.setStyleSheet(f"color: {C.TEXT_DIM}; background: transparent;") + self._date_lbl.setAlignment(Qt.AlignmentFlag.AlignRight) + right_col.addWidget(self._date_lbl) + lay.addLayout(right_col) + return w + + def _tick_clock(self): + self._clock_lbl.setText(time.strftime("%H:%M:%S")) + self._date_lbl.setText(time.strftime("%a %d %b %Y")) + + def _build_left_panel(self) -> QWidget: + w = QWidget() + w.setFixedWidth(_LEFT_W) + w.setStyleSheet(f"background: {C.DARK}; border-right: 1px solid {C.BORDER};") + lay = QVBoxLayout(w) + lay.setContentsMargins(8, 10, 8, 10) + lay.setSpacing(6) + + hdr = QLabel("◈ SYS MONITOR") + hdr.setFont(QFont("Courier New", 7, QFont.Weight.Bold)) + hdr.setStyleSheet(f"color: {C.PRI}; background: transparent; " + f"border-bottom: 1px solid {C.BORDER}; padding-bottom: 4px;") + lay.addWidget(hdr) + lay.addSpacing(2) + + self._bar_cpu = MetricBar("CPU", C.PRI) + self._bar_mem = MetricBar("MEM", C.ACC2) + self._bar_net = MetricBar("NET", C.GREEN) + self._bar_gpu = MetricBar("GPU", C.ACC) + self._bar_tmp = MetricBar("TMP", "#ff6688") + + for bar in [self._bar_cpu, self._bar_mem, self._bar_net, + self._bar_gpu, self._bar_tmp]: + lay.addWidget(bar) + + lay.addSpacing(4) + + info_panel = QWidget() + info_panel.setStyleSheet( + f"background: {C.PANEL2}; border: 1px solid {C.BORDER}; border-radius: 4px;" + ) + ip_lay = QVBoxLayout(info_panel) + ip_lay.setContentsMargins(6, 5, 6, 5) + ip_lay.setSpacing(3) + + self._uptime_lbl = QLabel("UP --:--") + self._uptime_lbl.setFont(QFont("Courier New", 8, QFont.Weight.Bold)) + self._uptime_lbl.setStyleSheet(f"color: {C.GREEN}; background: transparent; border: none;") + ip_lay.addWidget(self._uptime_lbl) + + self._proc_lbl = QLabel("PROC --") + self._proc_lbl.setFont(QFont("Courier New", 8)) + self._proc_lbl.setStyleSheet(f"color: {C.TEXT_MED}; background: transparent; border: none;") + ip_lay.addWidget(self._proc_lbl) + + os_name = {"Windows": "WIN", "Darwin": "macOS", "Linux": "LINUX"}.get(_OS, _OS.upper()) + os_lbl = QLabel(f"OS {os_name}") + os_lbl.setFont(QFont("Courier New", 8)) + os_lbl.setStyleSheet(f"color: {C.ACC2}; background: transparent; border: none;") + ip_lay.addWidget(os_lbl) + + lay.addWidget(info_panel) + lay.addStretch() + + for txt, col in [ + ("AI CORE\nACTIVE", C.GREEN), + ("SEC\nCLEARED", C.PRI), + ("PROTOCOL\nXXXVIII", C.TEXT_DIM), + ]: + lbl = QLabel(txt) + lbl.setFont(QFont("Courier New", 7, QFont.Weight.Bold)) + lbl.setAlignment(Qt.AlignmentFlag.AlignCenter) + lbl.setStyleSheet( + f"color: {col}; background: {C.PANEL2};" + f"border: 1px solid {C.BORDER_A}; border-radius: 3px; padding: 4px;" + ) + lay.addWidget(lbl) + + return w + def _build_right_panel(self) -> QWidget: + w = QWidget() + w.setFixedWidth(_RIGHT_W) + w.setStyleSheet(f"background: {C.DARK}; border-left: 1px solid {C.BORDER};") + lay = QVBoxLayout(w) + lay.setContentsMargins(8, 8, 8, 8) + lay.setSpacing(6) + + def _sec(txt): + l = QLabel(f"▸ {txt}") + l.setFont(QFont("Courier New", 7, QFont.Weight.Bold)) + l.setStyleSheet(f"color: {C.TEXT_MED}; background: transparent;") + return l + + lay.addWidget(_sec("ACTIVITY LOG")) + self._log = LogWidget() + lay.addWidget(self._log, stretch=1) + + sep = QFrame(); sep.setFrameShape(QFrame.Shape.HLine) + sep.setStyleSheet(f"color: {C.BORDER}; margin: 2px 0;") + lay.addWidget(sep) + + lay.addWidget(_sec("FILE UPLOAD")) + self._drop_zone = FileDropZone() + self._drop_zone.file_selected.connect(self._on_file_selected) + lay.addWidget(self._drop_zone) + + self._file_hint = QLabel("No file loaded — drop or click above to upload") + self._file_hint.setFont(QFont("Courier New", 7)) + self._file_hint.setStyleSheet(f"color: {C.TEXT_MED}; background: transparent;") + self._file_hint.setWordWrap(True) + lay.addWidget(self._file_hint) + + sep2 = QFrame(); sep2.setFrameShape(QFrame.Shape.HLine) + sep2.setStyleSheet(f"color: {C.BORDER}; margin: 2px 0;") + lay.addWidget(sep2) + + lay.addWidget(_sec("COMMAND INPUT")) + lay.addLayout(self._build_input_row()) + + self._mute_btn = QPushButton("🎙 MICROPHONE ACTIVE") + self._mute_btn.setFixedHeight(30) + self._mute_btn.setFont(QFont("Courier New", 8, QFont.Weight.Bold)) + self._mute_btn.setCursor(Qt.CursorShape.PointingHandCursor) + self._mute_btn.clicked.connect(self._toggle_mute) + self._style_mute_btn() + lay.addWidget(self._mute_btn) + + fs_btn = QPushButton("⛶ FULLSCREEN [F11]") + fs_btn.setFixedHeight(26) + fs_btn.setFont(QFont("Courier New", 7)) + fs_btn.setCursor(Qt.CursorShape.PointingHandCursor) + fs_btn.setStyleSheet(f""" + QPushButton {{ + background: transparent; color: {C.TEXT_MED}; + border: 1px solid {C.BORDER}; border-radius: 3px; + }} + QPushButton:hover {{ + color: {C.PRI}; border: 1px solid {C.BORDER_B}; + }} + """) + fs_btn.clicked.connect(self._toggle_fullscreen) + lay.addWidget(fs_btn) + + return w + + def _build_input_row(self) -> QHBoxLayout: + row = QHBoxLayout(); row.setSpacing(5) + self._input = QLineEdit() + self._input.setPlaceholderText("Type a command or question…") + self._input.setFont(QFont("Courier New", 9)) + self._input.setFixedHeight(30) + self._input.setStyleSheet(f""" + QLineEdit {{ + background: #000d14; color: {C.WHITE}; + border: 1px solid {C.BORDER}; border-radius: 3px; padding: 3px 7px; + }} + QLineEdit:focus {{ border: 1px solid {C.PRI}; }} + """) + self._input.returnPressed.connect(self._send) + row.addWidget(self._input) + + send = QPushButton("▸") + send.setFixedSize(30, 30) + send.setFont(QFont("Courier New", 11, QFont.Weight.Bold)) + send.setCursor(Qt.CursorShape.PointingHandCursor) + send.setStyleSheet(f""" + QPushButton {{ + background: {C.PANEL}; color: {C.PRI}; + border: 1px solid {C.PRI_DIM}; border-radius: 3px; + }} + QPushButton:hover {{ background: {C.PRI_GHO}; border: 1px solid {C.PRI}; }} + """) + send.clicked.connect(self._send) + row.addWidget(send) + return row + + def _build_footer(self) -> QWidget: + w = QWidget() + w.setFixedHeight(22) + w.setStyleSheet(f"background: {C.DARK}; border-top: 1px solid {C.BORDER};") + lay = QHBoxLayout(w); lay.setContentsMargins(14, 0, 14, 0) + + def _fl(txt, color=C.TEXT_MED): + l = QLabel(txt); l.setFont(QFont("Courier New", 7)) + l.setStyleSheet(f"color: {color}; background: transparent;") + return l + + lay.addWidget(_fl("[F4] Mute · [F11] Fullscreen")) + lay.addStretch() + lay.addWidget(_fl("FatihMakes Industries · MARK XXXIX · CLASSIFIED")) + lay.addStretch() + lay.addWidget(_fl("© STARK INDUSTRIES", C.PRI_DIM)) + return w + + def _on_file_selected(self, path: str): + self._current_file = path + p = Path(path) + cat = _file_category(p) + icon, _ = _FILE_ICONS.get(cat, _FILE_ICONS["unknown"]) + size = _fmt_size(p.stat().st_size) + self._file_hint.setText(f"{icon} {p.name} · {size} · Tell JARVIS what to do with it") + self._log.append_log(f"FILE: {p.name} ({size}) loaded") + if self.on_text_command: + msg = ( + f"[FILE_UPLOADED] path={path} | name={p.name} | " + f"type={p.suffix.lstrip('.')} | size={size} | " + f"Briefly tell the user you can see the file '{p.name}' " + f"({size}) has been uploaded and ask what they'd like to do with it." + ) + threading.Thread(target=self.on_text_command, args=(msg,), daemon=True).start() + + def _toggle_mute(self): + self._muted = not self._muted + self.hud.muted = self._muted + self._style_mute_btn() + if self._muted: + self._apply_state("MUTED") + self._log.append_log("SYS: Microphone muted.") + else: + self._apply_state("LISTENING") + self._log.append_log("SYS: Microphone active.") + + def _style_mute_btn(self): + if self._muted: + self._mute_btn.setText("🔇 MICROPHONE MUTED") + self._mute_btn.setStyleSheet(f""" + QPushButton {{ + background: #140006; color: {C.MUTED_C}; + border: 1px solid {C.MUTED_C}; border-radius: 3px; + }} + """) + else: + self._mute_btn.setText("🎙 MICROPHONE ACTIVE") + self._mute_btn.setStyleSheet(f""" + QPushButton {{ + background: #00140a; color: {C.GREEN}; + border: 1px solid {C.GREEN}; border-radius: 3px; + }} + QPushButton:hover {{ background: #001f10; }} + """) + + def _send(self): + txt = self._input.text().strip() + if not txt: return + self._input.clear() + self._log.append_log(f"You: {txt}") + if self.on_text_command: + threading.Thread(target=self.on_text_command, args=(txt,), daemon=True).start() + + def _apply_state(self, state: str): + self.hud.state = state + self.hud.speaking = (state == "SPEAKING") + + def _check_config(self) -> bool: + if not API_FILE.exists(): return False + try: + d = json.loads(API_FILE.read_text(encoding="utf-8")) + return bool(d.get("gemini_api_key")) and bool(d.get("os_system")) + except Exception: + return False + + def _show_setup(self): + ov = SetupOverlay(self.centralWidget()) + cw = self.centralWidget() + ow, oh = 460, 390 + ov.setGeometry( + (cw.width() - ow) // 2, + (cw.height() - oh) // 2, + ow, oh, + ) + ov.done.connect(self._on_setup_done) + ov.show() + self._overlay = ov + + def _on_setup_done(self, key: str, os_name: str): + os.makedirs(CONFIG_DIR, exist_ok=True) + API_FILE.write_text( + json.dumps({"gemini_api_key": key, "os_system": os_name}, indent=4), + encoding="utf-8", + ) + self._ready = True + if self._overlay: + self._overlay.hide() + self._overlay = None + self._apply_state("LISTENING") + self._log.append_log(f"SYS: Initialised. OS={os_name.upper()}. JARVIS online.") + +class _RootShim: + def __init__(self, app: QApplication): + self._app = app + def mainloop(self): + self._app.exec() + def protocol(self, *_): + pass class JarvisUI: - def __init__(self, face_path, size=None): - self.root = tk.Tk() - self.root.title("J.A.R.V.I.S — MARK XXXVII") - self.root.resizable(False, False) + def __init__(self, face_path: str, size=None): + self._app = QApplication.instance() or QApplication(sys.argv) + self._app.setStyle("Fusion") + self._win = MainWindow(face_path) + self._win.show() + self.root = _RootShim(self._app) - sw = self.root.winfo_screenwidth() - sh = self.root.winfo_screenheight() - W = min(sw, 984) - H = min(sh, 816) - self.root.geometry(f"{W}x{H}+{(sw-W)//2}+{(sh-H)//2}") - self.root.configure(bg=C_BG) + @property + def muted(self) -> bool: + return self._win._muted - self.W = W - self.H = H + @muted.setter + def muted(self, v: bool): + if v != self._win._muted: + self._win._toggle_mute() - self.FACE_SZ = min(int(H * 0.54), 400) - self.FCX = W // 2 - self.FCY = int(H * 0.13) + self.FACE_SZ // 2 + @property + def current_file(self) -> str | None: + return self._win._drop_zone.current_file() - self.speaking = False - self.muted = False - self.scale = 1.0 - self.target_scale = 1.0 - self.halo_a = 60.0 - self.target_halo = 60.0 - self.last_t = time.time() - self.tick = 0 - self.scan_angle = 0.0 - self.scan2_angle = 180.0 - self.rings_spin = [0.0, 120.0, 240.0] - self.pulse_r = [0.0, self.FACE_SZ * 0.26, self.FACE_SZ * 0.52] - self.status_text = "INITIALISING" - self.status_blink = True + @property + def on_text_command(self): + return self._win.on_text_command - self._jarvis_state = "INITIALISING" - - self.typing_queue = deque() - self.is_typing = False - - self.on_text_command = None - - self._face_pil = None - self._has_face = False - self._face_scale_cache = None - self._load_face(face_path) - - self.bg = tk.Canvas(self.root, width=W, height=H, - bg=C_BG, highlightthickness=0) - self.bg.place(x=0, y=0) - - LW = int(W * 0.72) - LH = 110 - LOG_Y = H - LH - 80 - self.log_frame = tk.Frame(self.root, bg=C_PANEL, - highlightbackground=C_MID, - highlightthickness=1) - self.log_frame.place(x=(W - LW) // 2, y=LOG_Y, width=LW, height=LH) - self.log_text = tk.Text(self.log_frame, fg=C_TEXT, bg=C_PANEL, - insertbackground=C_TEXT, borderwidth=0, - wrap="word", font=("Courier", 10), padx=10, pady=6) - self.log_text.pack(fill="both", expand=True) - self.log_text.configure(state="disabled") - self.log_text.tag_config("you", foreground="#e8e8e8") - self.log_text.tag_config("ai", foreground=C_PRI) - self.log_text.tag_config("sys", foreground=C_ACC2) - self.log_text.tag_config("err", foreground=C_RED) - - INPUT_Y = LOG_Y + LH + 6 - self._build_input_bar(LW, INPUT_Y) - - self._build_mute_button() - - self.root.bind("", lambda e: self._toggle_mute()) - - self._api_key_ready = self._api_keys_exist() - if not self._api_key_ready: - self._show_setup_ui() - - self._animate() - self.root.protocol("WM_DELETE_WINDOW", lambda: os._exit(0)) - - def _build_mute_button(self): - BTN_W, BTN_H = 110, 32 - BTN_X = 18 - BTN_Y = self.H - 70 - - self._mute_canvas = tk.Canvas( - self.root, width=BTN_W, height=BTN_H, - bg=C_BG, highlightthickness=0, cursor="hand2" - ) - self._mute_canvas.place(x=BTN_X, y=BTN_Y) - self._mute_canvas.bind("", lambda e: self._toggle_mute()) - self._draw_mute_button() - - def _draw_mute_button(self): - c = self._mute_canvas - c.delete("all") - if self.muted: - border = C_MUTED - fill = "#1a0008" - icon = "🔇" - label = " MUTED" - fg = C_MUTED - else: - border = C_MID - fill = C_PANEL - icon = "🎙" - label = " LIVE" - fg = C_GREEN - - c.create_rectangle(0, 0, 110, 32, outline=border, fill=fill, width=1) - c.create_text(55, 16, text=f"{icon}{label}", - fill=fg, font=("Courier", 10, "bold")) - - def _toggle_mute(self): - self.muted = not self.muted - self._draw_mute_button() - if self.muted: - self.set_state("MUTED") - self.write_log("SYS: Microphone muted.") - else: - self.set_state("LISTENING") - self.write_log("SYS: Microphone active.") - - def _build_input_bar(self, lw: int, y: int): - x0 = (self.W - lw) // 2 - BTN_W = 70 - INP_W = lw - BTN_W - 4 - - self._input_var = tk.StringVar() - - self._input_entry = tk.Entry( - self.root, - textvariable=self._input_var, - fg=C_TEXT, bg="#000d12", - insertbackground=C_TEXT, - borderwidth=0, - font=("Courier", 10), - highlightthickness=1, - highlightbackground=C_DIM, - highlightcolor=C_PRI, - ) - self._input_entry.place(x=x0, y=y, width=INP_W, height=28) - self._input_entry.bind("", self._on_input_submit) - self._input_entry.bind("", self._on_input_submit) - - self._send_btn = tk.Button( - self.root, - text="SEND ▸", - command=self._on_input_submit, - fg=C_PRI, bg=C_PANEL, - activeforeground=C_BG, activebackground=C_PRI, - font=("Courier", 9, "bold"), - borderwidth=0, cursor="hand2", - highlightthickness=1, - highlightbackground=C_MID, - ) - self._send_btn.place(x=x0 + INP_W + 4, y=y, width=BTN_W, height=28) - - def _on_input_submit(self, event=None): - text = self._input_var.get().strip() - if not text: - return - self._input_var.set("") - self.write_log(f"You: {text}") - if self.on_text_command: - threading.Thread( - target=self.on_text_command, - args=(text,), - daemon=True - ).start() + @on_text_command.setter + def on_text_command(self, cb): + self._win.on_text_command = cb def set_state(self, state: str): - self._jarvis_state = state - if state == "MUTED": - self.status_text = "MUTED" - self.speaking = False - elif state == "SPEAKING": - self.status_text = "SPEAKING" - self.speaking = True - elif state == "THINKING": - self.status_text = "THINKING" - self.speaking = False - elif state == "LISTENING": - self.status_text = "LISTENING" - self.speaking = False - elif state == "PROCESSING": - self.status_text = "PROCESSING" - self.speaking = False - else: - self.status_text = "ONLINE" - self.speaking = False - - def _load_face(self, path): - FW = self.FACE_SZ - try: - img = Image.open(path).convert("RGBA").resize((FW, FW), Image.LANCZOS) - mask = Image.new("L", (FW, FW), 0) - ImageDraw.Draw(mask).ellipse((2, 2, FW - 2, FW - 2), fill=255) - img.putalpha(mask) - self._face_pil = img - self._has_face = True - except Exception: - self._has_face = False - - @staticmethod - def _ac(r, g, b, a): - f = a / 255.0 - return f"#{int(r*f):02x}{int(g*f):02x}{int(b*f):02x}" - - def _animate(self): - self.tick += 1 - t = self.tick - now = time.time() - - if now - self.last_t > (0.14 if self.speaking else 0.55): - if self.speaking: - self.target_scale = random.uniform(1.05, 1.11) - self.target_halo = random.uniform(138, 182) - elif self.muted: - self.target_scale = random.uniform(0.998, 1.001) - self.target_halo = random.uniform(20, 32) - else: - self.target_scale = random.uniform(1.001, 1.007) - self.target_halo = random.uniform(50, 68) - self.last_t = now - - sp = 0.35 if self.speaking else 0.16 - self.scale += (self.target_scale - self.scale) * sp - self.halo_a += (self.target_halo - self.halo_a) * sp - - for i, spd in enumerate([1.2, -0.8, 1.9] if self.speaking else [0.5, -0.3, 0.82]): - self.rings_spin[i] = (self.rings_spin[i] + spd) % 360 - - self.scan_angle = (self.scan_angle + (2.8 if self.speaking else 1.2)) % 360 - self.scan2_angle = (self.scan2_angle + (-1.7 if self.speaking else -0.68)) % 360 - - pspd = 3.8 if self.speaking else 1.8 - limit = self.FACE_SZ * 0.72 - new_p = [r + pspd for r in self.pulse_r if r + pspd < limit] - if len(new_p) < 3 and random.random() < (0.06 if self.speaking else 0.022): - new_p.append(0.0) - self.pulse_r = new_p - - if t % 40 == 0: - self.status_blink = not self.status_blink - - self._draw() - self.root.after(16, self._animate) - - def _draw(self): - c = self.bg - W, H = self.W, self.H - t = self.tick - FCX = self.FCX - FCY = self.FCY - FW = self.FACE_SZ - c.delete("all") - - for x in range(0, W, 44): - for y in range(0, H, 44): - c.create_rectangle(x, y, x+1, y+1, fill=C_DIMMER, outline="") - - for r in range(int(FW * 0.54), int(FW * 0.28), -22): - frac = 1.0 - (r - FW * 0.28) / (FW * 0.26) - ga = max(0, min(255, int(self.halo_a * 0.09 * frac))) - if self.muted: - gh = f"{ga:02x}" - c.create_oval(FCX-r, FCY-r, FCX+r, FCY+r, - outline=f"#{gh}0011", width=2) - else: - gh = f"{ga:02x}" - c.create_oval(FCX-r, FCY-r, FCX+r, FCY+r, - outline=f"#00{gh}ff", width=2) - - for pr in self.pulse_r: - pa = max(0, int(220 * (1.0 - pr / (FW * 0.72)))) - r = int(pr) - if self.muted: - c.create_oval(FCX-r, FCY-r, FCX+r, FCY+r, - outline=self._ac(255, 30, 80, pa // 3), width=2) - else: - c.create_oval(FCX-r, FCY-r, FCX+r, FCY+r, - outline=self._ac(0, 212, 255, pa), width=2) - - for idx, (r_frac, w_ring, arc_l, gap) in enumerate([ - (0.47, 3, 110, 75), (0.39, 2, 75, 55), (0.31, 1, 55, 38)]): - ring_r = int(FW * r_frac) - base_a = self.rings_spin[idx] - a_val = max(0, min(255, int(self.halo_a * (1.0 - idx * 0.18)))) - col = self._ac(255, 30, 80, a_val) if self.muted else self._ac(0, 212, 255, a_val) - for s in range(360 // (arc_l + gap)): - start = (base_a + s * (arc_l + gap)) % 360 - c.create_arc(FCX-ring_r, FCY-ring_r, FCX+ring_r, FCY+ring_r, - start=start, extent=arc_l, - outline=col, width=w_ring, style="arc") - - sr = int(FW * 0.49) - scan_a = min(255, int(self.halo_a * 1.4)) - arc_ext = 70 if self.speaking else 42 - scan_col = self._ac(255, 30, 80, scan_a) if self.muted else self._ac(0, 212, 255, scan_a) - c.create_arc(FCX-sr, FCY-sr, FCX+sr, FCY+sr, - start=self.scan_angle, extent=arc_ext, - outline=scan_col, width=3, style="arc") - c.create_arc(FCX-sr, FCY-sr, FCX+sr, FCY+sr, - start=self.scan2_angle, extent=arc_ext, - outline=self._ac(255, 100, 0, scan_a // 2), width=2, style="arc") - - t_out = int(FW * 0.495) - t_in = int(FW * 0.472) - a_mk = self._ac(0, 212, 255, 155) - for deg in range(0, 360, 10): - rad = math.radians(deg) - inn = t_in if deg % 30 == 0 else t_in + 5 - c.create_line(FCX + t_out * math.cos(rad), FCY - t_out * math.sin(rad), - FCX + inn * math.cos(rad), FCY - inn * math.sin(rad), - fill=a_mk, width=1) - - ch_r = int(FW * 0.50) - gap = int(FW * 0.15) - ch_a = self._ac(0, 212, 255, int(self.halo_a * 0.55)) - for x1, y1, x2, y2 in [ - (FCX - ch_r, FCY, FCX - gap, FCY), (FCX + gap, FCY, FCX + ch_r, FCY), - (FCX, FCY - ch_r, FCX, FCY - gap), (FCX, FCY + gap, FCX, FCY + ch_r)]: - c.create_line(x1, y1, x2, y2, fill=ch_a, width=1) - - blen = 22 - bc = self._ac(0, 212, 255, 200) - hl = FCX - FW // 2; hr = FCX + FW // 2 - ht = FCY - FW // 2; hb = FCY + FW // 2 - for bx, by, sdx, sdy in [(hl, ht, 1, 1), (hr, ht, -1, 1), - (hl, hb, 1, -1), (hr, hb, -1, -1)]: - c.create_line(bx, by, bx + sdx * blen, by, fill=bc, width=2) - c.create_line(bx, by, bx, by + sdy * blen, fill=bc, width=2) - - if self._has_face: - fw = int(FW * self.scale) - if (self._face_scale_cache is None or - abs(self._face_scale_cache[0] - self.scale) > 0.004): - scaled = self._face_pil.resize((fw, fw), Image.BILINEAR) - if self.muted: - tinted = scaled.copy() - r_ch, g_ch, b_ch, a_ch = tinted.split() - from PIL import ImageEnhance - g_ch = ImageEnhance.Brightness( - Image.fromarray(__import__('numpy').array(g_ch) // 2) - ).enhance(1.0) if False else g_ch - tk_img = ImageTk.PhotoImage(scaled) - self._face_scale_cache = (self.scale, tk_img) - c.create_image(FCX, FCY, image=self._face_scale_cache[1]) - else: - orb_r = int(FW * 0.27 * self.scale) - orb_color = (255, 30, 80) if self.muted else (0, 65, 120) - for i in range(7, 0, -1): - r2 = int(orb_r * i / 7) - frac = i / 7 - ga = max(0, min(255, int(self.halo_a * 1.1 * frac))) - c.create_oval(FCX-r2, FCY-r2, FCX+r2, FCY+r2, - fill=self._ac(int(orb_color[0]*frac), - int(orb_color[1]*frac), - int(orb_color[2]*frac), ga), - outline="") - c.create_text(FCX, FCY, text=SYSTEM_NAME, - fill=self._ac(0, 212, 255, min(255, int(self.halo_a * 2))), - font=("Courier", 14, "bold")) - - HDR = 62 - c.create_rectangle(0, 0, W, HDR, fill="#00080d", outline="") - c.create_line(0, HDR, W, HDR, fill=C_MID, width=1) - c.create_text(W // 2, 22, text=SYSTEM_NAME, - fill=C_PRI, font=("Courier", 18, "bold")) - c.create_text(W // 2, 44, text="Just A Rather Very Intelligent System", - fill=C_MID, font=("Courier", 9)) - c.create_text(16, 31, text=MODEL_BADGE, - fill=C_DIM, font=("Courier", 9), anchor="w") - c.create_text(W - 16, 31, text=time.strftime("%H:%M:%S"), - fill=C_PRI, font=("Courier", 14, "bold"), anchor="e") - - sy = FCY + FW // 2 + 45 - - if self.muted: - stat = "⊘ MUTED" - sc = C_MUTED - elif self.speaking: - stat = "● SPEAKING" - sc = C_ACC - elif self._jarvis_state == "THINKING": - sym = "◈" if self.status_blink else "◇" - stat = f"{sym} THINKING" - sc = C_ACC2 - elif self._jarvis_state == "PROCESSING": - sym = "▷" if self.status_blink else "▶" - stat = f"{sym} PROCESSING" - sc = C_ACC2 - elif self._jarvis_state == "LISTENING": - sym = "●" if self.status_blink else "○" - stat = f"{sym} LISTENING" - sc = C_GREEN - else: - sym = "●" if self.status_blink else "○" - stat = f"{sym} {self.status_text}" - sc = C_PRI - - c.create_text(W // 2, sy, text=stat, - fill=sc, font=("Courier", 11, "bold")) - - wy = sy + 22 - N = 32 - BH = 18 - bw = 8 - total_w = N * bw - wx0 = (W - total_w) // 2 - for i in range(N): - if self.muted: - hb = 2 - col = C_MUTED - elif self.speaking: - hb = random.randint(3, BH) - col = C_PRI if hb > BH * 0.6 else C_MID - else: - hb = int(3 + 2 * math.sin(t * 0.08 + i * 0.55)) - col = C_DIM - bx = wx0 + i * bw - c.create_rectangle(bx, wy + BH - hb, bx + bw - 1, wy + BH, - fill=col, outline="") - - c.create_rectangle(0, H - 28, W, H, fill="#00080d", outline="") - c.create_line(0, H - 28, W, H - 28, fill=C_DIM, width=1) - c.create_text(W - 16, H - 14, fill=C_DIM, font=("Courier", 8), - text="[F4] MUTE", anchor="e") - c.create_text(W // 2, H - 14, fill=C_DIM, font=("Courier", 8), - text="FatihMakes Industries · CLASSIFIED · MARK XXXVII") + self._win._state_sig.emit(state) def write_log(self, text: str): - self.typing_queue.append(text) - tl = text.lower() - if tl.startswith("you:"): - self.set_state("PROCESSING") - elif tl.startswith("jarvis:") or tl.startswith("ai:"): - self.set_state("SPEAKING") - if not self.is_typing: - self._start_typing() + self._win._log_sig.emit(text) - def _start_typing(self): - if not self.typing_queue: - self.is_typing = False - if not self.speaking and not self.muted: - self.set_state("LISTENING") - return - self.is_typing = True - text = self.typing_queue.popleft() - tl = text.lower() - if tl.startswith("you:"): - tag = "you" - elif tl.startswith("jarvis:") or tl.startswith("ai:"): - tag = "ai" - elif tl.startswith("err:") or "error" in tl or "failed" in tl: - tag = "err" - else: - tag = "sys" - self.log_text.configure(state="normal") - self._type_char(text, 0, tag) - - def _type_char(self, text, i, tag): - if i < len(text): - self.log_text.insert(tk.END, text[i], tag) - self.log_text.see(tk.END) - self.root.after(8, self._type_char, text, i + 1, tag) - else: - self.log_text.insert(tk.END, "\n") - self.log_text.configure(state="disabled") - self.root.after(25, self._start_typing) + def wait_for_api_key(self): + while not self._win._ready: + time.sleep(0.1) def start_speaking(self): self.set_state("SPEAKING") def stop_speaking(self): if not self.muted: - self.set_state("LISTENING") - - def _api_keys_exist(self) -> bool: - if not API_FILE.exists(): - return False - try: - data = json.loads(API_FILE.read_text(encoding="utf-8")) - return bool(data.get("gemini_api_key")) and bool(data.get("os_system")) - except Exception: - return False - - def wait_for_api_key(self): - while not self._api_key_ready: - time.sleep(0.1) - - @staticmethod - def _detect_os() -> str: - s = platform.system().lower() - if s == "darwin": - return "mac" - if s == "windows": - return "windows" - return "linux" - - def _show_setup_ui(self): - detected = self._detect_os() - - self._selected_os = tk.StringVar(value=detected) - - self.setup_frame = tk.Frame( - self.root, bg="#00080d", - highlightbackground=C_PRI, highlightthickness=1 - ) - self.setup_frame.place(relx=0.5, rely=0.5, anchor="center") - - tk.Label( - self.setup_frame, - text="◈ INITIALISATION REQUIRED", - fg=C_PRI, bg="#00080d", - font=("Courier", 13, "bold") - ).pack(pady=(18, 2)) - - tk.Label( - self.setup_frame, - text="Configure J.A.R.V.I.S. before first boot.", - fg=C_MID, bg="#00080d", - font=("Courier", 9) - ).pack(pady=(0, 14)) - - tk.Label( - self.setup_frame, - text="GEMINI API KEY", - fg=C_DIM, bg="#00080d", - font=("Courier", 9) - ).pack(pady=(0, 2)) - - self.gemini_entry = tk.Entry( - self.setup_frame, width=52, - fg=C_TEXT, bg="#000d12", - insertbackground=C_TEXT, - borderwidth=0, font=("Courier", 10), show="*" - ) - self.gemini_entry.pack(pady=(0, 18)) - - sep_frame = tk.Frame(self.setup_frame, bg="#00080d") - sep_frame.pack(fill="x", padx=24, pady=(0, 12)) - tk.Frame(sep_frame, bg=C_DIM, height=1).pack(fill="x") - - tk.Label( - self.setup_frame, - text="SELECT OPERATING SYSTEM", - fg=C_DIM, bg="#00080d", - font=("Courier", 9) - ).pack(pady=(0, 4)) - - detect_label = { - "windows": "Windows", - "mac": "macOS", - "linux": "Linux", - }.get(detected, detected.capitalize()) - - tk.Label( - self.setup_frame, - text=f"AUTO-DETECTED: {detect_label}", - fg=C_ACC2, bg="#00080d", - font=("Courier", 8) - ).pack(pady=(0, 8)) - - os_btn_frame = tk.Frame(self.setup_frame, bg="#00080d") - os_btn_frame.pack(pady=(0, 18)) - - os_options = [ - ("windows", "⊞ WINDOWS"), - ("mac", " macOS"), - ("linux", "🐧 LINUX"), - ] - - self._os_buttons = {} - for os_key, os_label in os_options: - btn = tk.Button( - os_btn_frame, - text=os_label, - width=13, - font=("Courier", 10, "bold"), - borderwidth=0, - cursor="hand2", - pady=7, - command=lambda k=os_key: self._select_os(k) - ) - btn.pack(side="left", padx=6) - self._os_buttons[os_key] = btn - - self._select_os(detected) - - sep_frame2 = tk.Frame(self.setup_frame, bg="#00080d") - sep_frame2.pack(fill="x", padx=24, pady=(0, 14)) - tk.Frame(sep_frame2, bg=C_DIM, height=1).pack(fill="x") - - tk.Button( - self.setup_frame, - text="▸ INITIALISE SYSTEMS", - command=self._save_api_keys, - bg=C_BG, fg=C_PRI, - activebackground="#003344", - font=("Courier", 10), - borderwidth=0, pady=8 - ).pack(pady=(0, 18)) - - def _select_os(self, os_key: str): - self._selected_os.set(os_key) - styles = { - "windows": (C_PRI, "#001a22"), - "mac": (C_ACC2, "#1a1500"), - "linux": (C_GREEN, "#001a0d"), - } - for key, btn in self._os_buttons.items(): - if key == os_key: - fg, bg = styles[key] - btn.configure( - fg=bg, bg=fg, - activeforeground=bg, activebackground=fg, - relief="flat" - ) - else: - btn.configure( - fg=C_DIM, bg="#000d12", - activeforeground=C_TEXT, activebackground="#001a22", - relief="flat" - ) - - def _save_api_keys(self): - gemini = self.gemini_entry.get().strip() - if not gemini: - self.gemini_entry.configure(highlightthickness=1, - highlightbackground=C_RED, - highlightcolor=C_RED) - return - - os_system = self._selected_os.get() - - os.makedirs(CONFIG_DIR, exist_ok=True) - with open(API_FILE, "w", encoding="utf-8") as f: - json.dump( - { - "gemini_api_key": gemini, - "os_system": os_system, - }, - f, indent=4 - ) - - self.setup_frame.destroy() - self._api_key_ready = True - self.set_state("LISTENING") - self.write_log(f"SYS: Systems initialised. OS → {os_system.upper()}. JARVIS online.") \ No newline at end of file + self.set_state("LISTENING") \ No newline at end of file