refactor: step 2 - extract domain services (tasks, prompts, snapshots, knowledge)
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""
|
||||
===============================================================================
|
||||
FILE: services/prompts/diff_engine.py
|
||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||
MODULE: services / prompts
|
||||
ROLE: Формирование превью изменений промпта и генерация визуального HTML-Diff.
|
||||
|
||||
AI-CONTEXT-ANCHORS:
|
||||
- ANCHOR[PROMPT_DIFF_BUILDER]: Генерация черновика и HTML-разметки изменений.
|
||||
===============================================================================
|
||||
"""
|
||||
|
||||
from typing import Tuple, Dict
|
||||
from core.connection import get_connection
|
||||
|
||||
|
||||
# ANCHOR[PROMPT_DIFF_BUILDER]
|
||||
def build_prompt_diff(action: str, section_id: int, item_id: int, content: str = "", prompt_name: str = "main_agent") -> Tuple[str, str]:
|
||||
"""
|
||||
Возвращает кортеж (merged_draft_text, diff_html_for_ui).
|
||||
"""
|
||||
act = (action or "ADD").upper()
|
||||
|
||||
with get_connection(row_factory=True) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT section_id, item_id, content
|
||||
FROM system_prompt_nodes
|
||||
WHERE prompt_name = ? AND is_active = 1
|
||||
ORDER BY section_id, item_id
|
||||
""", (prompt_name,))
|
||||
existing_nodes = cursor.fetchall()
|
||||
|
||||
nodes_dict = {(r["section_id"], r["item_id"]): r["content"] for r in existing_nodes}
|
||||
|
||||
# Формируем словарь для чистого текста
|
||||
nodes_dict_for_draft = {k: v for k, v in nodes_dict.items() if k != (section_id, item_id)} if act == "DELETE" else dict(nodes_dict)
|
||||
if act != "DELETE":
|
||||
nodes_dict_for_draft[(section_id, item_id)] = content
|
||||
|
||||
draft_lines = []
|
||||
curr_sec = None
|
||||
for (s_id, i_id), txt in sorted(nodes_dict_for_draft.items()):
|
||||
if i_id == 0:
|
||||
if curr_sec is not None:
|
||||
draft_lines.append("")
|
||||
draft_lines.append(f"{s_id}. {txt}")
|
||||
curr_sec = s_id
|
||||
else:
|
||||
draft_lines.append(f" {s_id}.{i_id}. {txt}")
|
||||
merged_prompt = "\n".join(draft_lines)
|
||||
|
||||
# Формируем HTML Diff
|
||||
diff_lines = []
|
||||
curr_sec = None
|
||||
display_nodes = dict(nodes_dict)
|
||||
if act != "DELETE":
|
||||
display_nodes[(section_id, item_id)] = content
|
||||
|
||||
for (s_id, i_id), txt in sorted(display_nodes.items()):
|
||||
if i_id == 0:
|
||||
if curr_sec is not None:
|
||||
diff_lines.append("")
|
||||
diff_lines.append(f"{s_id}. {txt}")
|
||||
curr_sec = s_id
|
||||
else:
|
||||
if s_id == section_id and i_id == item_id:
|
||||
if act == "DELETE":
|
||||
line_str = f' <span class="line-through text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300 opacity-80">{s_id}.{i_id}. {txt} [УДАЛЕНИЕ]</span>'
|
||||
else:
|
||||
line_str = f' <span class="text-rose-600 font-bold bg-rose-50 px-1.5 py-0.5 rounded border border-rose-300">{s_id}.{i_id}. {txt}</span>'
|
||||
else:
|
||||
line_str = f" {s_id}.{i_id}. {txt}"
|
||||
diff_lines.append(line_str)
|
||||
|
||||
diff_html = "\n".join(diff_lines)
|
||||
return merged_prompt, diff_html
|
||||
@@ -0,0 +1,114 @@
|
||||
"""
|
||||
===============================================================================
|
||||
FILE: services/prompts/repository.py
|
||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||
MODULE: services / prompts
|
||||
ROLE: Реляционное хранилище узлов системного промпта (таблица system_prompt_nodes).
|
||||
|
||||
AI-CONTEXT-ANCHORS:
|
||||
- ANCHOR[PROMPT_REPO_GET_ACTIVE]: Сборка активного промпта из узлов БД.
|
||||
- ANCHOR[PROMPT_REPO_APPLY_ACTION]: Точечная вставка / изменение / удаление узла.
|
||||
===============================================================================
|
||||
"""
|
||||
|
||||
import re
|
||||
import logging
|
||||
from typing import List, Tuple, Dict, Any
|
||||
from core.connection import get_connection
|
||||
|
||||
logger = logging.getLogger("PROMPT_REPO")
|
||||
|
||||
|
||||
# ANCHOR[PROMPT_REPO_GET_ACTIVE]
|
||||
def repo_get_active_prompt(prompt_name: str = "main_agent") -> str:
|
||||
"""Собирает структурированный текст системного промпта из активных узлов."""
|
||||
with get_connection(row_factory=True) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
SELECT section_id, item_id, content
|
||||
FROM system_prompt_nodes
|
||||
WHERE prompt_name = ? AND is_active = 1
|
||||
ORDER BY section_id ASC, item_id ASC
|
||||
""", (prompt_name,))
|
||||
rows = cursor.fetchall()
|
||||
|
||||
if not rows:
|
||||
return "Ты — ИИ-ассистент SCUD Orion AI."
|
||||
|
||||
lines = []
|
||||
current_section = None
|
||||
|
||||
for r in rows:
|
||||
sec_id = r["section_id"]
|
||||
itm_id = r["item_id"]
|
||||
content = r["content"]
|
||||
|
||||
if itm_id == 0:
|
||||
if current_section is not None:
|
||||
lines.append("")
|
||||
lines.append(f"{sec_id}. {content}")
|
||||
current_section = sec_id
|
||||
else:
|
||||
lines.append(f" {sec_id}.{itm_id}. {content}")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
# ANCHOR[PROMPT_REPO_APPLY_ACTION]
|
||||
def repo_apply_prompt_action(action: str, section_id: int, item_id: int, content: str = "", prompt_name: str = "main_agent") -> None:
|
||||
"""Точечно применяет действие (ADD / UPDATE / DELETE) над узлом промпта."""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
action_clean = (action or "").upper()
|
||||
if action_clean in ["ADD", "UPDATE", "EDIT"]:
|
||||
cursor.execute("""
|
||||
INSERT INTO system_prompt_nodes (prompt_name, section_id, item_id, content, is_active, updated_at)
|
||||
VALUES (?, ?, ?, ?, 1, CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(prompt_name, section_id, item_id) DO UPDATE SET
|
||||
content = excluded.content,
|
||||
is_active = 1,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
""", (prompt_name, section_id, item_id, content))
|
||||
elif action_clean == "DELETE":
|
||||
cursor.execute("""
|
||||
DELETE FROM system_prompt_nodes
|
||||
WHERE prompt_name = ? AND section_id = ? AND item_id = ?
|
||||
""", (prompt_name, section_id, item_id))
|
||||
conn.commit()
|
||||
|
||||
|
||||
def repo_save_full_prompt(prompt_text: str, prompt_name: str = "main_agent") -> None:
|
||||
"""Парсит и полностью перезаписывает все узлы промпта из сырого текста."""
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM system_prompt_nodes WHERE prompt_name = ?", (prompt_name,))
|
||||
|
||||
current_sec = 1
|
||||
current_itm = 0
|
||||
|
||||
for raw_line in prompt_text.splitlines():
|
||||
clean_line = re.sub(r'<[^>]+>', '', raw_line).strip()
|
||||
if not clean_line:
|
||||
continue
|
||||
|
||||
sub_match = re.match(r'^(\d+)[\.\s]+(\d+)[\.\s\:\-]+(.*)$', clean_line)
|
||||
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 = clean_line
|
||||
|
||||
cursor.execute("""
|
||||
INSERT OR REPLACE INTO system_prompt_nodes (prompt_name, section_id, item_id, content, is_active)
|
||||
VALUES (?, ?, ?, ?, 1)
|
||||
""", (prompt_name, current_sec, current_itm, content))
|
||||
|
||||
conn.commit()
|
||||
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
===============================================================================
|
||||
FILE: services/prompts/service.py
|
||||
PROJECT: SCUD Orion AI (Unified Architecture)
|
||||
MODULE: services / prompts
|
||||
ROLE: Единый доменный сервис управления системным промптом.
|
||||
===============================================================================
|
||||
"""
|
||||
|
||||
from typing import Tuple, Dict, Any
|
||||
from .repository import repo_get_active_prompt, repo_apply_prompt_action, repo_save_full_prompt
|
||||
from .diff_engine import build_prompt_diff
|
||||
|
||||
|
||||
def get_active_system_prompt() -> str:
|
||||
"""Получить текущий активный системный промпт."""
|
||||
return repo_get_active_prompt()
|
||||
|
||||
|
||||
def apply_prompt_action(action: str, section_id: int, item_id: int, content: str = "") -> None:
|
||||
"""Применить точечное изменение к узлу промпта."""
|
||||
repo_apply_prompt_action(action, section_id, item_id, content)
|
||||
|
||||
|
||||
def save_full_prompt_draft(draft_text: str) -> None:
|
||||
"""Сохранить полный черновик промпта."""
|
||||
repo_save_full_prompt(draft_text)
|
||||
|
||||
|
||||
def create_prompt_preview(action: str, section_id: int, item_id: int, content: str = "") -> Tuple[str, str, str]:
|
||||
"""
|
||||
Формирует черновик и diff.
|
||||
Возвращает (merged_draft, diff_html, baseline_prompt).
|
||||
"""
|
||||
baseline = repo_get_active_prompt()
|
||||
draft, diff_html = build_prompt_diff(action, section_id, item_id, content)
|
||||
return draft, diff_html, baseline
|
||||
Reference in New Issue
Block a user