Download core/tools/thread_summary_tool.py from Kyrlos11/nniback: direct link, hf CLI and curl.
- Browser
- Download file 9.64 kB
-
https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/thread_summary_tool.py
- Command line
-
hf download hf://spaces/Kyrlos11/nniback/core/tools/thread_summary_tool.py
-
curl -L -o thread_summary_tool.py https://huggingface.co/spaces/Kyrlos11/nniback/resolve/main/core/tools/thread_summary_tool.py
9.64 kB
| from typing import Optional | |
| from core.agentpress.tool import Tool, ToolResult, openapi_schema, tool_metadata | |
| from core.utils.logger import logger | |
| class ThreadSummaryTool(Tool): | |
| def __init__(self): | |
| super().__init__() | |
| async def compress_thread_history( | |
| self, | |
| preserve_recent: int = 18, | |
| reason: Optional[str] = None | |
| ) -> ToolResult: | |
| """ | |
| Compress conversation history into 3-layer structure. | |
| This tool: | |
| 1. Gets all messages from current thread | |
| 2. Splits into old (to compress) and recent (to keep) | |
| 3. Generates summary + facts via LLM | |
| 4. Stores as thread_summary message | |
| 5. Marks old messages as omitted | |
| 6. Updates in-memory state for immediate effect | |
| """ | |
| try: | |
| # Get current thread context | |
| thread_id = self._get_thread_id() | |
| if not thread_id: | |
| return self.fail_response("Could not determine current thread") | |
| logger.info(f"[ThreadSummary] Starting compression for thread {thread_id}, reason: {reason or 'none'}") | |
| # Get RunState from write buffer | |
| from core.agents.pipeline.stateless.flusher import write_buffer | |
| # Find the state - need to search by thread_id since we don't have run_id | |
| state = None | |
| for run_id, s in write_buffer._runs.items(): | |
| if s.thread_id == thread_id: | |
| state = s | |
| break | |
| if not state: | |
| return self.fail_response("Could not access conversation state") | |
| # Get all messages | |
| all_messages = list(state._messages) | |
| if len(all_messages) <= preserve_recent: | |
| return self.success_response({ | |
| "status": "no_compression_needed", | |
| "message": f"Only {len(all_messages)} messages in conversation", | |
| "recommendation": f"Need more than {preserve_recent} messages to compress" | |
| }) | |
| # Check if already compressed | |
| from core.agents.pipeline.stateless.context import ContextManager | |
| if ContextManager.find_summary_message(all_messages): | |
| return self.success_response({ | |
| "status": "already_compressed", | |
| "message": "Conversation already has a summary", | |
| "recommendation": "Summary already exists. If you need more space, try reducing preserve_recent parameter." | |
| }) | |
| # Split messages | |
| to_compress = all_messages[:-preserve_recent] | |
| working_memory = all_messages[-preserve_recent:] | |
| logger.info(f"[ThreadSummary] Compressing {len(to_compress)} messages, keeping {len(working_memory)}") | |
| # Compress using context manager | |
| result = await ContextManager.compress_history( | |
| messages=all_messages, | |
| working_memory_size=preserve_recent, | |
| model="gpt-4o-mini" # Fast, cheap model | |
| ) | |
| logger.info(f"[ThreadSummary] Compression complete: {result.compressed_count} msgs → summary + facts") | |
| # Create summary message data | |
| compressed_ids = [m.get('message_id') for m in to_compress if m.get('message_id')] | |
| summary_data = ContextManager.create_summary_message_data( | |
| thread_id=thread_id, | |
| summary=result.summary, | |
| facts=result.facts, | |
| compressed_count=result.compressed_count, | |
| compressed_message_ids=compressed_ids | |
| ) | |
| # Create the summary message for in-memory state | |
| summary_message = { | |
| "role": "system", | |
| "content": summary_data['content'], | |
| "message_id": summary_data['message_id'], | |
| "type": "thread_summary", | |
| "_is_summary": True | |
| } | |
| # Update in-memory state (CRITICAL for immediate effect) | |
| state._messages.clear() | |
| state._messages.append(summary_message) # Add summary | |
| for msg in working_memory: | |
| state._messages.append(msg) # Add working memory | |
| logger.info(f"[ThreadSummary] Updated in-memory state: {len(list(state._messages))} messages") | |
| # Queue writes to persist to DB | |
| from core.agents.pipeline.stateless.state import PendingWrite | |
| # 1. Write the summary message | |
| state._pending_writes.append(PendingWrite( | |
| write_type="message", | |
| data=summary_data | |
| )) | |
| # 2. Mark old messages as omitted | |
| for msg_id in compressed_ids: | |
| state._pending_writes.append(PendingWrite( | |
| write_type="message", | |
| data={ | |
| "message_id": msg_id, | |
| "thread_id": thread_id, | |
| "metadata": {"omitted": True}, | |
| "_update_only": True # Flag to indicate this is an update | |
| } | |
| )) | |
| # Flush immediately so changes persist | |
| await state.flush() | |
| # Invalidate cache for next run | |
| from core.cache.runtime_cache import invalidate_message_history_cache | |
| await invalidate_message_history_cache(thread_id) | |
| logger.info(f"[ThreadSummary] ✅ Compression complete and persisted") | |
| return self.success_response({ | |
| "status": "compressed", | |
| "message": f"Successfully compressed {result.compressed_count} messages", | |
| "details": { | |
| "compressed_messages": result.compressed_count, | |
| "working_memory_size": result.working_memory_size, | |
| "estimated_tokens_saved": result.estimated_tokens_saved, | |
| "new_message_count": len(list(state._messages)), | |
| "summary_preview": result.summary[:150] + "..." if len(result.summary) > 150 else result.summary | |
| } | |
| }) | |
| except ValueError as e: | |
| logger.warning(f"[ThreadSummary] Validation error: {e}") | |
| return self.fail_response(f"Cannot compress: {str(e)}") | |
| except Exception as e: | |
| logger.error(f"[ThreadSummary] Compression failed: {e}", exc_info=True) | |
| return self.fail_response(f"Compression failed: {str(e)}") | |
| def _get_thread_id(self) -> Optional[str]: | |
| """Get current thread ID from context""" | |
| # This will be set by the tool executor when tool is called | |
| return getattr(self, '_thread_id', None) | |