feat(stapp): restore chat history bubbles after /continue N (#138)

When the user runs '/continue N' in stapp, the agent's in-memory context
is restored, but the UI previously showed only a single ' restored' line
— all prior chat bubbles were missing.

This change parses the target session log and reconstructs the
user/assistant message pairs into st.session_state.messages, so reopening
a session feels like the conversation was never interrupted.

* continue_cmd.py: add extract_ui_messages(path)
  - parses model_responses log into [{role, content}, ...]
  - groups multi-turn LLM calls (prompts whose text starts with the
    '### [WORKING MEMORY]' header) into a single assistant bubble,
    inserting the existing '**LLM Running (Turn N) ...**' marker so
    fold_turns() renders them as collapsible segments.
  - two small helpers (_user_text / _assistant_text) keep parsing local.

* stapp.py: in the /continue branch, resolve the target log path BEFORE
  calling handle_frontend_command (which snapshots the current log and
  would otherwise shift list_sessions indices), then replace
  session_state.messages with the reconstructed history on success.
  Falls back to the previous behavior for bare /continue or failure.

Co-authored-by: wjl2023 <wjl2023@users.noreply.github.com>
This commit is contained in:
weijia
2026-04-23 16:36:41 +08:00
committed by GitHub
parent 1678114f1f
commit fd4c833511
2 changed files with 65 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ import streamlit as st
import time, json, re, threading, queue
from agentmain import GeneraticAgent
import chatapp_common # activate /continue command (monkey patches GeneraticAgent)
from continue_cmd import handle_frontend_command, reset_conversation
from continue_cmd import handle_frontend_command, reset_conversation, list_sessions, extract_ui_messages
st.set_page_config(page_title="Cowork", layout="wide")
@@ -190,10 +190,19 @@ if prompt := st.chat_input("any task?"):
st.session_state.messages = [{"role": "assistant", "content": reset_conversation(agent), "time": ts}]
_reset_and_rerun()
if cmd.startswith("/continue"):
st.session_state.messages = list(st.session_state.messages) + [
{"role": "user", "content": cmd, "time": ts},
{"role": "assistant", "content": handle_frontend_command(agent, cmd), "time": ts},
]
m = re.match(r'/continue\s+(\d+)\s*$', cmd.strip())
sessions = list_sessions(exclude_pid=os.getpid()) if m else []
idx = int(m.group(1)) - 1 if m else -1
# Resolve target path BEFORE handle (which snapshots current log, shifting indices).
target = sessions[idx][0] if 0 <= idx < len(sessions) else None
result = handle_frontend_command(agent, cmd)
history = extract_ui_messages(target) if target and result.startswith('') else None
tail = [{"role": "assistant", "content": result, "time": ts}]
if history:
st.session_state.messages = history + tail
else:
st.session_state.messages = list(st.session_state.messages) + \
[{"role": "user", "content": cmd, "time": ts}] + tail
_reset_and_rerun()
st.session_state.messages.append({"role": "user", "content": prompt})
if hasattr(agent, '_pet_req') and not prompt.startswith('/'): agent._pet_req('state=walk')