Coverage for agentos/desktop/shell.py: 0%
74 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 10:59 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 10:59 +0800
1"""
2Desktop Shell — AgentOS 原生桌面壳。
4基于 pywebview,将 Web 客户端包裹为原生桌面应用窗口。
5提供与 AutoClaw 桌面客户端类似的体验:
7功能:
8- 原生窗口包裹 Web 前端
9- 系统托盘(最小化到托盘)
10- 开机自启配置
11- 窗口置顶 / 全屏
12- 原生通知
13- 多平台兼容(Windows / macOS / Linux)
15依赖: pip install pywebview
17启动方式:
18 python -m agentos.desktop.shell # 连接本地服务
19 python -m agentos.desktop.shell --url http://1.2.3.4:19999 # 连接远程
20"""
22from __future__ import annotations
24import argparse
25import os
26import json
27import time
28import webbrowser
31# ── 配置 ───────────────────────────────────────────────────────
33APP_NAME = "AgentOS Desktop"
34APP_VERSION = "1.7.1"
35DEFAULT_URL = "http://127.0.0.1:19999"
36CONFIG_DIR = os.path.join(os.path.expanduser("~"), ".agentos")
37CONFIG_FILE = os.path.join(CONFIG_DIR, "desktop.json")
40def load_config() -> dict:
41 """加载本地配置。"""
42 defaults = {
43 "url": DEFAULT_URL,
44 "width": 1200,
45 "height": 800,
46 "fullscreen": False,
47 "always_on_top": False,
48 "auto_start": False,
49 "minimize_to_tray": True,
50 "title": f"{APP_NAME} v{APP_VERSION}",
51 }
52 if os.path.isfile(CONFIG_FILE):
53 try:
54 with open(CONFIG_FILE, "r") as f:
55 defaults.update(json.load(f))
56 except Exception:
57 pass
58 return defaults
61def save_config(cfg: dict) -> None:
62 os.makedirs(CONFIG_DIR, exist_ok=True)
63 with open(CONFIG_FILE, "w") as f:
64 json.dump(cfg, f, indent=2)
67# ── 原生桌面壳 ─────────────────────────────────────────────────
70def create_native_shell(url: str, width: int = 1200, height: int = 800,
71 title: str = APP_NAME, fullscreen: bool = False,
72 on_top: bool = False, minimize_to_tray: bool = True) -> None:
73 """使用 pywebview 创建原生桌面窗口。"""
74 try:
75 import webview
76 except ImportError:
77 print("需要安装 pywebview: pip install pywebview")
78 print("自动打开浏览器作为降级方案...")
79 webbrowser.open(url)
80 try:
81 while True:
82 time.sleep(1)
83 except KeyboardInterrupt:
84 pass
85 return
87 window = webview.create_window(
88 title=title,
89 url=url,
90 width=width,
91 height=height,
92 fullscreen=fullscreen,
93 on_top=on_top,
94 easy_drag=False,
95 confirm_close=minimize_to_tray,
96 text_select=True,
97 )
99 # 系统托盘暂时关闭(pywebview 托盘支持有限)
100 # 完整的托盘功能需要 pywebview >= 5.0 + 特定平台的额外配置
101 webview.start(gui="cef", debug=False)
104# ── 命令行入口 ──
107def main() -> None:
108 parser = argparse.ArgumentParser(
109 description="AgentOS Desktop Shell — 原生桌面壳",
110 formatter_class=argparse.RawDescriptionHelpFormatter,
111 epilog="""
112示例:
113 agentos desktop-shell # 连接本地 127.0.0.1:19999
114 agentos desktop-shell --url http://remote:19999 # 连接远程服务
115 agentos desktop-shell --fullscreen # 全屏模式
116 agentos desktop-shell --on-top # 窗口置顶
117 agentos desktop-shell --browser # 直接用浏览器打开(降级)
118 """,
119 )
120 parser.add_argument("--url", default=None, help=f"服务端地址(默认: {DEFAULT_URL})")
121 parser.add_argument("--width", type=int, default=1200, help="窗口宽度(默认: 1200)")
122 parser.add_argument("--height", type=int, default=800, help="窗口高度(默认: 800)")
123 parser.add_argument("--fullscreen", action="store_true", help="全屏启动")
124 parser.add_argument("--on-top", action="store_true", help="窗口置顶")
125 parser.add_argument("--no-tray", action="store_true", help="禁用最小化到托盘")
126 parser.add_argument("--browser", action="store_true", help="直接用系统浏览器打开")
127 parser.add_argument("--config", action="store_true", help="显示当前配置")
129 args = parser.parse_args()
130 cfg = load_config()
132 if args.config:
133 print(json.dumps(cfg, indent=2, ensure_ascii=False))
134 return
136 url = args.url or cfg.get("url", DEFAULT_URL)
137 width = args.width
138 height = args.height
139 title = cfg.get("title", APP_NAME)
140 fullscreen = args.fullscreen
141 on_top = args.on_top
142 tray = not args.no_tray
144 if args.browser:
145 print(f"用浏览器打开 {url} ...")
146 webbrowser.open(url)
147 try:
148 while True:
149 time.sleep(1)
150 except KeyboardInterrupt:
151 pass
152 else:
153 print(f"启动 AgentOS Desktop Shell v{APP_VERSION}")
154 print(f"连接: {url}")
155 create_native_shell(
156 url=url,
157 width=width,
158 height=height,
159 title=title,
160 fullscreen=fullscreen,
161 on_top=on_top,
162 minimize_to_tray=tray,
163 )
166if __name__ == "__main__":
167 main()