Enterprise-grade sequential thinking and context management system
| 87 | return data |
| 88 | |
| 89 | class SequentialThinkingEngine: |
| 90 | """ |
| 91 | Enterprise-grade sequential thinking and context management system |
| 92 | """ |
| 93 | |
| 94 | def __init__(self, db_manager, memory_manager): |
| 95 | self.db = db_manager |
| 96 | self.memory_manager = memory_manager |
| 97 | self.logger = logging.getLogger(__name__) |
| 98 | |
| 99 | # Token counting patterns (approximate) |
| 100 | self.token_patterns = { |
| 101 | 'word': 0.75, # Average tokens per word |
| 102 | 'char': 0.25, # Average tokens per character |
| 103 | } |
| 104 | |
| 105 | # Context compression thresholds |
| 106 | self.max_context_tokens = 8000 # Maximum context before compression |
| 107 | self.target_compression_ratio = 0.3 # Target 30% of original size |
| 108 | |
| 109 | self._ensure_thinking_tables() |
| 110 | |
| 111 | def _ensure_thinking_tables(self): |
| 112 | """Create tables for sequential thinking if they don't exist""" |
| 113 | cursor = self.db.connection.cursor() |
| 114 | |
| 115 | # Thinking chains table |
| 116 | cursor.execute(""" |
| 117 | CREATE TABLE IF NOT EXISTS thinking_chains ( |
| 118 | id TEXT PRIMARY KEY, |
| 119 | project_id TEXT, |
| 120 | session_id TEXT, |
| 121 | objective TEXT NOT NULL, |
| 122 | status TEXT DEFAULT 'active', |
| 123 | created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 124 | updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 125 | total_tokens INTEGER DEFAULT 0, |
| 126 | metadata TEXT, |
| 127 | FOREIGN KEY (project_id) REFERENCES projects(id) |
| 128 | ) |
| 129 | """) |
| 130 | |
| 131 | # Thinking steps table |
| 132 | cursor.execute(""" |
| 133 | CREATE TABLE IF NOT EXISTS thinking_steps ( |
| 134 | id TEXT PRIMARY KEY, |
| 135 | chain_id TEXT, |
| 136 | stage TEXT NOT NULL, |
| 137 | title TEXT NOT NULL, |
| 138 | content TEXT NOT NULL, |
| 139 | reasoning TEXT, |
| 140 | confidence REAL DEFAULT 0.5, |
| 141 | dependencies TEXT, -- JSON array |
| 142 | outputs TEXT, -- JSON array |
| 143 | timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 144 | token_count INTEGER DEFAULT 0, |
| 145 | FOREIGN KEY (chain_id) REFERENCES thinking_chains(id) |
| 146 | ) |
no outgoing calls
no test coverage detected