feat: desktop pet v2 with 8 skins and state notifications
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import os, sys, subprocess
|
||||
from urllib.request import urlopen
|
||||
from urllib.parse import quote
|
||||
if sys.stdout is None: sys.stdout = open(os.devnull, "w")
|
||||
if sys.stderr is None: sys.stderr = open(os.devnull, "w")
|
||||
try: sys.stdout.reconfigure(errors='replace')
|
||||
@@ -19,15 +21,13 @@ def init():
|
||||
if agent.llmclient is None:
|
||||
st.error("⚠️ 未配置任何可用的 LLM 接口,请在 mykey.py 中添加 sider_cookie 或 oai_apikey+oai_apibase 等信息后重启。")
|
||||
st.stop()
|
||||
else:
|
||||
threading.Thread(target=agent.run, daemon=True).start()
|
||||
else: threading.Thread(target=agent.run, daemon=True).start()
|
||||
return agent
|
||||
|
||||
agent = init()
|
||||
|
||||
st.title("🖥️ Cowork")
|
||||
|
||||
|
||||
if 'autonomous_enabled' not in st.session_state: st.session_state.autonomous_enabled = False
|
||||
|
||||
@st.fragment
|
||||
@@ -38,50 +38,41 @@ def render_sidebar():
|
||||
if last_reply_time > 0:
|
||||
st.caption(f"空闲时间:{int(time.time()) - last_reply_time}秒", help="当超过30分钟未收到回复时,系统会自动任务")
|
||||
if st.button("切换备用链路"):
|
||||
agent.next_llm()
|
||||
st.rerun(scope="fragment")
|
||||
agent.next_llm(); st.rerun(scope="fragment")
|
||||
if st.button("强行停止任务"):
|
||||
agent.abort()
|
||||
st.toast("已发送停止信号")
|
||||
st.rerun()
|
||||
agent.abort(); st.toast("已发送停止信号"); st.rerun()
|
||||
if st.button("重新注入System Prompt"):
|
||||
agent.llmclient.last_tools = ''
|
||||
st.toast("下次将重新注入System Prompt")
|
||||
agent.llmclient.last_tools = ''; st.toast("下次将重新注入System Prompt")
|
||||
if st.button("🐱 桌面宠物"):
|
||||
kwargs = {'creationflags': 0x08} if sys.platform == 'win32' else {}
|
||||
subprocess.Popen([sys.executable, os.path.join(os.path.dirname(__file__), 'desktop_pet.pyw')], **kwargs)
|
||||
pet_script = os.path.join(os.path.dirname(__file__), 'desktop_pet_v2.pyw')
|
||||
if not os.path.exists(pet_script): pet_script = os.path.join(os.path.dirname(__file__), 'desktop_pet.pyw')
|
||||
subprocess.Popen([sys.executable, pet_script], **kwargs)
|
||||
def _pet_req(q): threading.Thread(target=lambda: urlopen(f'http://127.0.0.1:51983/?{q}', timeout=2), daemon=True).start()
|
||||
agent._pet_req = _pet_req
|
||||
if not hasattr(agent, '_turn_end_hooks'): agent._turn_end_hooks = {}
|
||||
def _pet_hook(ctx):
|
||||
parts = [f"🔄 Turn {ctx.get('turn','?')}"]
|
||||
if ctx.get('summary'): parts.append(ctx['summary'])
|
||||
if ctx.get('exit_reason'): parts.append('✅ 任务已完成')
|
||||
msg = '\n'.join(parts)
|
||||
def send():
|
||||
try:
|
||||
from urllib.request import urlopen
|
||||
from urllib.parse import quote
|
||||
urlopen(f'http://127.0.0.1:51983/?msg={quote(msg)}', timeout=2)
|
||||
except: pass
|
||||
threading.Thread(target=send, daemon=True).start()
|
||||
_pet_req(f'msg={quote(chr(10).join(parts))}')
|
||||
if ctx.get('exit_reason'): _pet_req('state=idle')
|
||||
agent._turn_end_hooks['pet'] = _pet_hook
|
||||
st.toast("桌面宠物已启动")
|
||||
|
||||
st.divider()
|
||||
if st.button("开始空闲自主行动"):
|
||||
st.session_state.last_reply_time = int(time.time()) - 1800
|
||||
st.toast("已将上次回复时间设为1800秒前")
|
||||
st.rerun()
|
||||
st.toast("已将上次回复时间设为1800秒前"); st.rerun()
|
||||
if st.session_state.autonomous_enabled:
|
||||
if st.button("⏸️ 禁止自主行动"):
|
||||
st.session_state.autonomous_enabled = False
|
||||
st.toast("⏸️ 已禁止自主行动")
|
||||
st.rerun()
|
||||
st.toast("⏸️ 已禁止自主行动"); st.rerun()
|
||||
st.caption("🟢 自主行动运行中,会在你离开它30分钟后自动进行")
|
||||
else:
|
||||
if st.button("▶️ 允许自主行动", type="primary"):
|
||||
st.session_state.autonomous_enabled = True
|
||||
st.toast("✅ 已允许自主行动")
|
||||
st.rerun()
|
||||
st.toast("✅ 已允许自主行动"); st.rerun()
|
||||
st.caption("🔴 自主行动已停止")
|
||||
with st.sidebar: render_sidebar()
|
||||
|
||||
@@ -167,6 +158,7 @@ components.html(f'<script>{_js_scroll_fix};{_js_ime_fix}</script>', height=0)
|
||||
|
||||
if prompt := st.chat_input("请输入指令"):
|
||||
st.session_state.messages.append({"role": "user", "content": prompt})
|
||||
if hasattr(agent, '_pet_req') and not prompt.startswith('/'): agent._pet_req('state=walk')
|
||||
with st.chat_message("user"): st.markdown(prompt)
|
||||
|
||||
with st.chat_message("assistant"):
|
||||
|
||||
Reference in New Issue
Block a user