feat(llm): двухфазный жизненный цикл сессий, двусторонний Diff, Topic Drift Guard и актуализация документации

This commit is contained in:
2026-08-18 13:47:26 +03:00
parent 828ce67817
commit 4b1b8a3586
20 changed files with 1763 additions and 2006 deletions
+21 -22
View File
@@ -7,6 +7,7 @@ ROLE: Реляционное управление системным промп
базой знаний, реестром действий и сессионными стейтами.
===============================================================================
"""
import re
import json
import logging
from typing import List, Dict, Any, Optional
@@ -239,7 +240,7 @@ def db_get_reference(category: Optional[str] = None) -> Dict[str, Any]:
return {"status": "success", "count": len(rows), "reference_items": [dict(r) for r in rows]}
def db_add_system_prompt(name: str, prompt_text: str) -> Dict[str, Any]:
"""Мост обратной совместимости: парсит текст промпта и сохраняет по узлам в БД."""
"""Надежный парсер: восстанавливает разделы и пункты с авто-выравниванием отступов."""
try:
init_prompt_nodes_table()
with get_db_connection() as conn:
@@ -249,30 +250,28 @@ def db_add_system_prompt(name: str, prompt_text: str) -> Dict[str, Any]:
current_sec = 1
current_itm = 0
for line in prompt_text.splitlines():
stripped = line.strip()
if not stripped:
for raw_line in prompt_text.splitlines():
clean_line = re.sub(r'<[^>]+>', '', raw_line).strip()
if not clean_line:
continue
# Проверяем заголовок раздела (например, "1. РОЛЬ И ЗАДАЧИ...")
parts = stripped.split(".", 1)
if len(parts) == 2 and parts[0].strip().isdigit():
sec_num = int(parts[0].strip())
rest = parts[1].strip()
# Проверяем, это подпункт (например, "1.1. Текст") или заголовок раздела
sub_parts = rest.split(".", 1)
if len(sub_parts) == 2 and sub_parts[0].strip().isdigit():
current_sec = sec_num
current_itm = int(sub_parts[0].strip())
content = sub_parts[1].strip()
else:
current_sec = sec_num
current_itm = 0
content = rest
# 1. Проверяем подпункт (например: "1.8. Текст", "1.8 Текст", "1. 8 Текст")
sub_match = re.match(r'^(\d+)[\.\s]+(\d+)[\.\s\:\-]+(.*)$', clean_line)
# 2. Проверяем заголовок раздела (например: "1. РОЛЬ И ЗАДАЧИ", "1 РОЛЬ И ЗАДАЧИ")
sec_match = re.match(r'^(\d+)[\.\s\:\-]+(.*)$', clean_line)
if sub_match:
current_sec = int(sub_match.group(1))
current_itm = int(sub_match.group(2))
content = sub_match.group(3).strip()
elif sec_match and not any(c.islower() for c in sec_match.group(2)[:15]):
# Заголовок раздела (обычно капсом)
current_sec = int(sec_match.group(1))
current_itm = 0
content = sec_match.group(2).strip()
else:
current_itm += 1
content = stripped
content = clean_line
cursor.execute("""
INSERT OR REPLACE INTO system_prompt_nodes (prompt_name, section_id, item_id, content, is_active)