mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-07-20 13:47:15 +08:00
fix(wechatmp): flush cached segments while task still running
Previously the passive reply only drained the cache after the agent task fully finished, so for long multi-turn tasks the user could not retrieve already-cached intermediate segments. Now return cached segments as soon as they are available, even while the task is still running; the next user message fetches the rest. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -103,14 +103,21 @@ class Query:
|
|||||||
task_running = True
|
task_running = True
|
||||||
waiting_until = request_time + 4
|
waiting_until = request_time + 4
|
||||||
while time.time() < waiting_until:
|
while time.time() < waiting_until:
|
||||||
if from_user in channel.running:
|
if from_user not in channel.running:
|
||||||
time.sleep(0.1)
|
|
||||||
else:
|
|
||||||
task_running = False
|
task_running = False
|
||||||
break
|
break
|
||||||
|
# Task still running, but if it has already produced cached
|
||||||
|
# segments (e.g. multi-turn thinking output), return them now
|
||||||
|
# instead of forcing the user to wait for the whole task. The
|
||||||
|
# remaining segments are fetched by the user's next message.
|
||||||
|
if channel.cache_dict.get(from_user):
|
||||||
|
break
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
reply_text = ""
|
reply_text = ""
|
||||||
if task_running:
|
# Only fall back to retry / "thinking" hint when the task is still
|
||||||
|
# running AND there is nothing cached to send yet.
|
||||||
|
if task_running and not channel.cache_dict.get(from_user):
|
||||||
if request_cnt < 3:
|
if request_cnt < 3:
|
||||||
# waiting for timeout (the POST request will be closed by Wechat official server)
|
# waiting for timeout (the POST request will be closed by Wechat official server)
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user