mirror of
https://github.com/mistralai/mistral-vibe
synced 2026-04-25 17:14:55 +02:00
Co-authored-by: Clément Drouin <clement.drouin@mistral.ai> Co-authored-by: Clément Sirieix <clement.sirieix@mistral.ai> Co-authored-by: Gauthier Guinet <43207538+Gguinet@users.noreply.github.com> Co-authored-by: Kim-Adeline Miguel <kimadeline.miguel@mistral.ai> Co-authored-by: Michel Thomazo <51709227+michelTho@users.noreply.github.com> Co-authored-by: Quentin <torroba.q@gmail.com> Co-authored-by: Simon <80467011+sorgfresser@users.noreply.github.com> Co-authored-by: Simon Van de Kerckhove <simon.vandekerckhove@mistral.ai> Co-authored-by: Vincent G <10739306+VinceOPS@users.noreply.github.com> Co-authored-by: angelapopopo <angele.lenglemetz@mistral.ai> Co-authored-by: Mistral Vibe <vibe@mistral.ai>
35 lines
1011 B
Python
35 lines
1011 B
Python
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
import shlex
|
|
import subprocess
|
|
import tempfile
|
|
|
|
from vibe.core.utils.io import read_safe
|
|
|
|
|
|
class ExternalEditor:
|
|
"""Handles opening an external editor to edit prompt content."""
|
|
|
|
@staticmethod
|
|
def get_editor() -> str:
|
|
return os.environ.get("VISUAL") or os.environ.get("EDITOR") or "nano"
|
|
|
|
def edit(self, initial_content: str = "") -> str | None:
|
|
editor = self.get_editor()
|
|
fd, filepath = tempfile.mkstemp(suffix=".md", prefix="vibe_")
|
|
try:
|
|
with os.fdopen(fd, "w") as f:
|
|
f.write(initial_content)
|
|
|
|
parts = shlex.split(editor)
|
|
subprocess.run([*parts, filepath], check=True)
|
|
|
|
content = read_safe(Path(filepath)).rstrip()
|
|
return content if content != initial_content else None
|
|
except (OSError, subprocess.CalledProcessError):
|
|
return
|
|
finally:
|
|
Path(filepath).unlink(missing_ok=True)
|