83 lines
2.5 KiB
Python
Executable File
83 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""CLI tool for VoiceAgent to interact with the Companion Web UI and Browser.
|
|
|
|
Commands:
|
|
python bin/web_tool.py open [url] - Open a URL (or http://localhost:8888) in macOS default browser
|
|
python bin/web_tool.py show <filepath> - Display a workspace file visually in the Web UI drawer
|
|
python bin/web_tool.py launch - Launch the Companion Web UI in default browser
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import urllib.request
|
|
import webbrowser
|
|
from pathlib import Path
|
|
|
|
DEFAULT_WEB_URL = "http://localhost:8888"
|
|
|
|
|
|
def open_browser(url: str = DEFAULT_WEB_URL):
|
|
if not url.startswith("http://") and not url.startswith("https://"):
|
|
url = "http://" + url
|
|
try:
|
|
os.system(f'open "{url}"')
|
|
print(f"Opened {url} in default browser.")
|
|
return True
|
|
except Exception as e:
|
|
print(f"Could not open browser: {e}")
|
|
return False
|
|
|
|
|
|
def show_file_in_web_ui(filepath: str, web_url: str = DEFAULT_WEB_URL):
|
|
try:
|
|
req_url = f"{web_url}/api/show_file"
|
|
data = json.dumps({"path": filepath}).encode("utf-8")
|
|
req = urllib.request.Request(req_url, data=data, headers={"Content-Type": "application/json"})
|
|
with urllib.request.urlopen(req, timeout=3.0) as resp:
|
|
if resp.status == 200:
|
|
print(f"File '{filepath}' sent to Web UI drawer.")
|
|
return True
|
|
except Exception as e:
|
|
print(f"Could not notify Web UI server: {e}")
|
|
|
|
# Fall back to opening file directly
|
|
abs_path = Path(filepath).expanduser().resolve()
|
|
if abs_path.exists():
|
|
os.system(f'open "{abs_path}"')
|
|
print(f"Opened {abs_path} locally.")
|
|
return True
|
|
print(f"File not found: {filepath}")
|
|
return False
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
open_browser(DEFAULT_WEB_URL)
|
|
return
|
|
|
|
cmd = sys.argv[1].lower()
|
|
|
|
if cmd in ("open", "browser", "launch"):
|
|
target_url = sys.argv[2] if len(sys.argv) >= 3 else DEFAULT_WEB_URL
|
|
open_browser(target_url)
|
|
|
|
elif cmd in ("show", "refer", "file", "view"):
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python bin/web_tool.py show <filepath>")
|
|
sys.exit(1)
|
|
filepath = sys.argv[2]
|
|
show_file_in_web_ui(filepath)
|
|
|
|
else:
|
|
# Treat single argument as URL or Filepath
|
|
arg = sys.argv[1]
|
|
if arg.startswith("http://") or arg.startswith("https://") or "localhost" in arg:
|
|
open_browser(arg)
|
|
else:
|
|
show_file_in_web_ui(arg)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|