Refactor: replace agentapp.py with stapp.py, optimize abort logic and cross-platform compatibility
This commit is contained in:
15
agentmain.py
15
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
|
||||
|
||||
@@ -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()
|
||||
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',
|
||||
|
||||
@@ -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})
|
||||
Reference in New Issue
Block a user