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