Repository: ErikBjare/gptme
File: gptme/tools/autocompact.py (Python)

Code before:
        return

    # Generate conversation summary using LLM
    yield Message("system", "🔄 Generating conversation resume with LLM...")

    resume_prompt = """Please create a comprehensive resume of this conversation that includes:

1. **Conversation Summary**: Key topics, decisions made, and progress achieved
2. **Technical Context**: Important code changes, configurations, or technical details
3. **Current State**: What was accomplished and what remains to be done
4. **Context Files**: Suggest which files should be included in future context (with brief rationale)

Format the response as a structured document that could serve as a RESUME.md file."""

    # Create a temporary message for the LLM prompt
    resume_request = Message("user", resume_prompt)
    llm_msgs = visible_msgs + [resume_request]

    try:
        # Generate the resume using LLM
        m = get_default_model()
        assert m
        resume_response = llm.reply(llm_msgs, model=m.full, tools=[], workspace=None)
        resume_content = resume_response.content

        # Save RESUME.md file
        resume_path = Path("RESUME.md")
        with open(resume_path, "w") as f:
            f.write(resume_content)

        # Create a compact conversation with just the resume
        system_msg = Message(
            "system", f"Previous conversation resumed from {resume_path}:"
        )
        resume_msg = Message("assistant", resume_content)

        # Replace conversation history with resume
        # FIXME: missing core system messages (only 1261 tokens after compaction in test)
        # TODO: include original or regenerate system messages
        # TODO: fork into a new conversation?
        # TODO: or use restart to automatically start a new continuation-session?
        ctx.manager.log = Log([system_msg, resume_msg])
        ctx.manager.write()

        yield Message(
            "system",
            f"✅ LLM-powered resume completed:\n"
            f"• Original conversation ({len(visible_msgs)} messages) compressed to resume\n"
            f"• Resume saved to: {resume_path.absolute()}\n"
            f"• Conversation history replaced with resume\n"
            f"• Review the RESUME.md file for suggested context files",

Review comment (performance):
This would invalidate prompt caches. We could potentially do this without invalidating prompt caches using existing tools, although then sessions without save/bash tools could not auto-compact. Although we could also feed the context for generating RESUME.md from a simplified/compressed message log anyway (run LLM-based summary compact on output from conservative rule-based compact?).

Code after:
        return

    # Generate conversation summary using LLM
    yield Message("system", "🔄 Generating conversation resume with LLM...")

    resume_prompt = """Please create a comprehensive resume of this conversation that includes:

1. **Conversation Summary**: Key topics, decisions made, and progress achieved
2. **Technical Context**: Important code changes, configurations, or technical details
3. **Current State**: What was accomplished and what remains to be done
4. **Context Files**: Suggest which files should be included in future context (with brief rationale)

Format the response as a structured document that could serve as a RESUME.md file."""

    # Create a temporary message for the LLM prompt
    resume_request = Message("user", resume_prompt)
    llm_msgs = visible_msgs + [resume_request]

    try:
        # Generate the resume using LLM
        m = get_default_model()
        assert m
        resume_response = llm.reply(llm_msgs, model=m.full, tools=[], workspace=None)
        resume_content = resume_response.content

        # Save RESUME.md file
        resume_path = Path("RESUME.md")
        with open(resume_path, "w") as f:
            f.write(resume_content)

        # Extract original system messages (before any user/assistant messages)
        # These contain essential context: core prompt, tool instructions, workspace info
        original_system_msgs = []
        for msg in msgs:
            if msg.role == "system":
                original_system_msgs.append(msg)
            elif msg.role in ("user", "assistant"):
                # Stop when we hit the first non-system message
                break

        # Create the resume intro message
        resume_intro_msg = Message(
            "system", f"Previous conversation resumed from {resume_path}:"
        )
        resume_msg = Message("assistant", resume_content)

        # Combine: original system messages + resume intro + resume content
        # This preserves tool instructions and workspace context while compacting conversation history
        # TODO: fork into a new conversation?
        # TODO: or use restart to automatically start a new continuation-session?
        ctx.manager.log = Log(original_system_msgs + [resume_intro_msg, resume_msg])