diff --git a/oai.py b/oai.py index 991d679..d58266b 100644 --- a/oai.py +++ b/oai.py @@ -4647,18 +4647,42 @@ def chat(): # PROCESS FILE ATTACHMENTS # ============================================================ text_part = user_input - for match in re.finditer(r'@([^\s]+)', user_input): + file_attachments = [] + content_blocks = [] + + # Smart file detection with extension whitelist + # Only matches files with known extensions or clear path prefixes + # + # Valid matches: + # @/path/to/file + # @~/file.txt + # @./script.py + # @report.pdf + # + # Excludes: + # @domain.com (no whitelisted extension) + # @mail.server.eu (TLD not in whitelist) + # user@domain.com (email pattern) + # Diagnostic-Code: @server.eu (TLD not in whitelist) + + file_pattern = r'(?:^|\s)@((?:[~/][\S]+)|(?:\.[\S]+)|(?:[\w][\w-]*\.(?:py|txt|md|log|json|csv|pdf|png|jpg|jpeg|gif|bmp|webp|svg|ico|zip|tar|gz|bz2|7z|rar|xz|js|ts|jsx|tsx|vue|html|css|scss|sass|less|xml|yaml|yml|toml|ini|conf|cfg|env|properties|sh|bash|zsh|fish|bat|cmd|ps1|c|cpp|cc|cxx|h|hpp|hxx|java|class|jar|war|rb|go|rs|swift|kt|kts|php|sql|db|sqlite|sqlite3|lock|gitignore|dockerignore|editorconfig|eslintrc|prettierrc|babelrc|nvmrc|npmrc|pyc|pyo|pyd|so|dll|dylib|exe|app|dmg|pkg|deb|rpm|apk|ipa|wasm|proto|graphql|graphqls|grpc|avro|parquet|orc|feather|arrow|hdf5|h5|mat|r|rdata|rds|pkl|pickle|joblib|npy|npz|safetensors|onnx|pt|pth|ckpt|pb|tflite|mlmodel|coreml|rknn)))(?=\s|$)' + + for match in re.finditer(file_pattern, user_input, re.IGNORECASE): file_path = match.group(1) expanded_path = os.path.expanduser(os.path.abspath(file_path)) + if not os.path.exists(expanded_path) or os.path.isdir(expanded_path): console.print(f"[bold red]File not found or is a directory: {expanded_path}[/]") continue + file_size = os.path.getsize(expanded_path) if file_size > 10 * 1024 * 1024: console.print(f"[bold red]File too large (>10MB): {expanded_path}[/]") continue + mime_type, _ = mimetypes.guess_type(expanded_path) file_ext = os.path.splitext(expanded_path)[1].lower() + try: with open(expanded_path, 'rb') as f: file_data = f.read() @@ -4704,7 +4728,9 @@ def chat(): console.print(f"[bold red]Error reading file {expanded_path}: {e}[/]") app_logger.error(f"File read error for {expanded_path}: {e}") continue - text_part = re.sub(r'@([^\s]+)', '', text_part).strip() + + # Remove file attachments from text (use same pattern as file detection) + text_part = re.sub(file_pattern, lambda m: m.group(0)[0] if m.group(0)[0].isspace() else '', text_part).strip() # Build message content if text_part or content_blocks: @@ -4716,6 +4742,7 @@ def chat(): console.print("[bold red]Prompt cannot be empty.[/]") continue + # ============================================================ # BUILD API MESSAGES # ============================================================