fix: restore stop signal detection broken by render_segments cache optimization

- stapp.py: add st.empty() after render_segments to force Streamlit StopException check on every iteration (incl. heartbeat)
- agentmain.py: fix nround type check, fix timeout comment
This commit is contained in:
Liang Jiaqing
2026-04-11 16:25:19 +08:00
parent de8adf76a9
commit 3531146792
3 changed files with 4 additions and 3 deletions

View File

@@ -188,13 +188,13 @@ if __name__ == '__main__':
if 'next' in item and random.random() < 0.95: # 概率写一次中间结果 if 'next' in item and random.random() < 0.95: # 概率写一次中间结果
with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', '')) with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item.get('next', ''))
with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n') with open(f'{d}/output{nround}.txt', 'w', encoding='utf-8') as f: f.write(item['done'] + '\n\n[ROUND END]\n')
for _ in range(300): # 等reply.txt5分钟超时 for _ in range(300): # 等reply.txt10分钟超时
time.sleep(2) time.sleep(2)
if os.path.exists(rp): if os.path.exists(rp):
with open(rp, encoding='utf-8') as f: raw = f.read() with open(rp, encoding='utf-8') as f: raw = f.read()
os.remove(rp); break os.remove(rp); break
else: break else: break
nround = int(nround) + 1 if nround.isdigit() else 1 nround = nround + 1 if isinstance(nround, int) else 1
elif args.reflect: elif args.reflect:
import importlib.util import importlib.util
spec = importlib.util.spec_from_file_location('reflect_script', args.reflect) spec = importlib.util.spec_from_file_location('reflect_script', args.reflect)

View File

@@ -139,6 +139,7 @@ if prompt := st.chat_input("请输入指令"):
turns = []; cache = []; response = '' turns = []; cache = []; response = ''
for response in agent_backend_stream(prompt): for response in agent_backend_stream(prompt):
render_segments(fold_turns(response), placeholders=turns, rendered_cache=cache, suffix='<span style="animation: blink 1s step-start infinite; color: #0066cc;">▌</span><style>@keyframes blink { 50% { opacity: 0; } }</style>') render_segments(fold_turns(response), placeholders=turns, rendered_cache=cache, suffix='<span style="animation: blink 1s step-start infinite; color: #0066cc;">▌</span><style>@keyframes blink { 50% { opacity: 0; } }</style>')
st.empty() # force Streamlit to check StopException on every iteration (incl. heartbeat)
render_segments(fold_turns(response), placeholders=turns, rendered_cache=cache, force_text=True) render_segments(fold_turns(response), placeholders=turns, rendered_cache=cache, force_text=True)
st.session_state.messages.append({"role": "assistant", "content": response}) st.session_state.messages.append({"role": "assistant", "content": response})
st.session_state.last_reply_time = int(time.time()) st.session_state.last_reply_time = int(time.time())

2
ga.py
View File

@@ -251,7 +251,7 @@ def smart_format(data, max_str_len=100, omit_str=' ... '):
class GenericAgentHandler(BaseHandler): class GenericAgentHandler(BaseHandler):
'''Generic Agent 工具库,包含多种工具的实现。工具函数自动加上了 do_ 前缀。实际工具名没有前缀。''' '''Generic Agent 工具库,包含多种工具的实现。工具函数自动加上了 do_ 前缀。实际工具名没有前缀。'''
def __init__(self, parent, last_history=None, cwd='./'): def __init__(self, parent, last_history=None, cwd='./temp'):
self.parent = parent self.parent = parent
self.working = {} self.working = {}
self.cwd = cwd; self.current_turn = 0 self.cwd = cwd; self.current_turn = 0