diff --git a/agentmain.py b/agentmain.py index 5a126c7..29edfb0 100644 --- a/agentmain.py +++ b/agentmain.py @@ -6,7 +6,7 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from sidercall import SiderLLMSession, LLMSession, ToolClient from agent_loop import agent_runner_loop, StepOutcome, BaseHandler -from ga import GenericAgentHandler, smart_format, get_global_memory +from ga import GenericAgentHandler, smart_format, get_global_memory, format_error with open('assets/tools_schema.json', 'r', encoding='utf-8') as f: TS = f.read() @@ -38,7 +38,6 @@ class GeneraticAgent: self.llmclient = llmclient else: self.llmclient = None - self.lock = threading.Lock() self.history = [] self.task_queue = queue.Queue() @@ -50,8 +49,11 @@ class GeneraticAgent: self.current_source = 'none' def abort(self): - if self.is_running: self.stop_sig = True + if not self.is_running: return + self.stop_sig = True + def put_task(self, query, source="user"): + self.display_queue.queue.clear() self.task_queue.put({"query": query, "source": source}) def run(self): @@ -74,14 +76,13 @@ class GeneraticAgent: try: full_response = "" for chunk in gen: + if self.stop_sig: break full_response += chunk self.display_queue.put({'next': full_response, 'source': source}) - if self.stop_sig: - self.stop_sig = False - raise KeyboardInterrupt("用户中止运行") self.display_queue.put({'done': full_response, 'source': source}) self.history = handler.history_info - except: + except Exception as e: + print(f"Backend Error: {format_error(e)}") self.display_queue.put({'done': '异常停止', 'source': source}) finally: self.is_running = False diff --git a/launch.pyw b/launch.pyw index 4b91d6f..cb2d4a2 100644 --- a/launch.pyw +++ b/launch.pyw @@ -22,7 +22,7 @@ def get_screen_width(): def start_streamlit(): global proc cmd = [ - sys.executable, "-m", "streamlit", "run", "agentapp.py", + sys.executable, "-m", "streamlit", "run", "stapp.py", "--server.port", "8501", "--server.headless", "true", "--theme.base", "dark" #以此默认开启暗黑模式,更有极客感 @@ -33,8 +33,10 @@ def start_streamlit(): if __name__ == '__main__': t = threading.Thread(target=start_streamlit, daemon=True) t.start() - screen_width = get_screen_width() - x_pos = screen_width - WINDOW_WIDTH - RIGHT_PADDING + if os.name == 'nt': + screen_width = get_screen_width() + x_pos = screen_width - WINDOW_WIDTH - RIGHT_PADDING + else: x_pos = 100 time.sleep(2) webview.create_window( title='GenericAgent', diff --git a/agentapp.py b/stapp.py similarity index 96% rename from agentapp.py rename to stapp.py index 63c8392..9607f95 100644 --- a/agentapp.py +++ b/stapp.py @@ -37,6 +37,9 @@ def render_llm_switcher(): if st.button("切换备用链路"): agent.llm_no = (current_idx + 1) % len(agent.llmclient.raw_apis) st.rerun(scope="fragment") + if st.button("强行停止任务"): + agent.abort() + st.toast("已发送停止信号") with st.sidebar: render_llm_switcher() @st.fragment(run_every="1s") @@ -66,8 +69,6 @@ def agent_backend_stream(prompt): finally: agent.abort() print('User aborted the operation.') - while not agent.display_queue.empty(): - agent.display_queue.get() if prompt := st.chat_input("请输入指令"): st.session_state.messages.append({"role": "user", "content": prompt})