|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Sequence |
| 4 | +from dataclasses import dataclass, field |
| 5 | +from typing import Any |
| 6 | +from uuid import UUID, uuid5 |
| 7 | + |
| 8 | +from sqlalchemy import select |
| 9 | +from sqlalchemy.orm import Session |
| 10 | + |
| 11 | +from core.model_manager import ModelInstance |
| 12 | +from core.model_runtime.entities import ( |
| 13 | + AssistantPromptMessage, |
| 14 | + PromptMessage, |
| 15 | + PromptMessageRole, |
| 16 | + TextPromptMessageContent, |
| 17 | + UserPromptMessage, |
| 18 | +) |
| 19 | +from core.variables.segments import ObjectSegment |
| 20 | +from core.variables.types import SegmentType |
| 21 | +from extensions.ext_database import db |
| 22 | +from factories import variable_factory |
| 23 | +from models.workflow import ConversationVariable |
| 24 | + |
| 25 | +# A stable namespace to derive deterministic IDs for node-scoped memories. |
| 26 | +# Using uuid5 to keep (conversation_id, node_id) mapping stable across runs. |
| 27 | +# NOTE: UUIDs must contain only hexadecimal characters; avoid letters beyond 'f'. |
| 28 | +NODE_SCOPED_MEMORY_NS = UUID("00000000-0000-0000-0000-000000000000") |
| 29 | + |
| 30 | + |
| 31 | +@dataclass |
| 32 | +class _HistoryItem: |
| 33 | + role: str |
| 34 | + text: str |
| 35 | + |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class NodeScopedMemory: |
| 39 | + """A per-node conversation memory persisted in ConversationVariable. |
| 40 | +
|
| 41 | + - Keyed by (conversation_id, node_id) |
| 42 | + - Value is stored as a conversation variable named _llm_mem.<node_id> |
| 43 | + - Structure (JSON): {"version": 1, "history": [{"role": "user"|"assistant", "text": "..."}, ...]} |
| 44 | + """ |
| 45 | + |
| 46 | + app_id: str |
| 47 | + conversation_id: str |
| 48 | + node_id: str |
| 49 | + model_instance: ModelInstance |
| 50 | + |
| 51 | + _loaded: bool = field(default=False, init=False) |
| 52 | + _history: list[_HistoryItem] = field(default_factory=list, init=False) |
| 53 | + |
| 54 | + @property |
| 55 | + def variable_name(self) -> str: |
| 56 | + return f"_llm_mem.{self.node_id}" |
| 57 | + |
| 58 | + @property |
| 59 | + def variable_id(self) -> str: |
| 60 | + # Deterministic id so we can upsert by (id, conversation_id) |
| 61 | + return str(uuid5(NODE_SCOPED_MEMORY_NS, f"{self.conversation_id}:{self.node_id}:llmmem")) |
| 62 | + |
| 63 | + # ------------ Persistence helpers ------------ |
| 64 | + def _load_if_needed(self) -> None: |
| 65 | + if self._loaded: |
| 66 | + return |
| 67 | + stmt = select(ConversationVariable).where( |
| 68 | + ConversationVariable.id == self.variable_id, |
| 69 | + ConversationVariable.conversation_id == self.conversation_id, |
| 70 | + ) |
| 71 | + with Session(db.engine, expire_on_commit=False) as session: |
| 72 | + row = session.scalar(stmt) |
| 73 | + if not row: |
| 74 | + self._history = [] |
| 75 | + self._loaded = True |
| 76 | + return |
| 77 | + variable = row.to_variable() |
| 78 | + value = variable.value if isinstance(variable.value, dict) else {} |
| 79 | + hist = value.get("history", []) if isinstance(value, dict) else [] |
| 80 | + parsed: list[_HistoryItem] = [] |
| 81 | + for item in hist: |
| 82 | + try: |
| 83 | + role = str(item.get("role", "")) |
| 84 | + text = str(item.get("text", "")) |
| 85 | + except Exception: |
| 86 | + role, text = "", "" |
| 87 | + if role and text: |
| 88 | + parsed.append(_HistoryItem(role=role, text=text)) |
| 89 | + self._history = parsed |
| 90 | + self._loaded = True |
| 91 | + |
| 92 | + def _dump_variable(self) -> Any: |
| 93 | + data = { |
| 94 | + "version": 1, |
| 95 | + "history": [{"role": item.role, "text": item.text} for item in self._history if item.text], |
| 96 | + } |
| 97 | + segment = ObjectSegment(value=data, value_type=SegmentType.OBJECT) |
| 98 | + variable = variable_factory.segment_to_variable( |
| 99 | + segment=segment, |
| 100 | + selector=["conversation", self.variable_name], |
| 101 | + id=self.variable_id, |
| 102 | + name=self.variable_name, |
| 103 | + description="LLM node-scoped memory", |
| 104 | + ) |
| 105 | + return variable |
| 106 | + |
| 107 | + def save(self) -> None: |
| 108 | + variable = self._dump_variable() |
| 109 | + with Session(db.engine) as session: |
| 110 | + # Upsert by (id, conversation_id) |
| 111 | + existing = session.scalar( |
| 112 | + select(ConversationVariable).where( |
| 113 | + ConversationVariable.id == self.variable_id, |
| 114 | + ConversationVariable.conversation_id == self.conversation_id, |
| 115 | + ) |
| 116 | + ) |
| 117 | + if existing: |
| 118 | + existing.data = variable.model_dump_json() |
| 119 | + else: |
| 120 | + obj = ConversationVariable.from_variable( |
| 121 | + app_id=self.app_id, conversation_id=self.conversation_id, variable=variable |
| 122 | + ) |
| 123 | + session.add(obj) |
| 124 | + session.commit() |
| 125 | + |
| 126 | + # ------------ Public API expected by LLM node ------------ |
| 127 | + def get_history_prompt_messages( |
| 128 | + self, *, max_token_limit: int = 2000, message_limit: int | None = None |
| 129 | + ) -> Sequence[PromptMessage]: |
| 130 | + self._load_if_needed() |
| 131 | + |
| 132 | + # Optionally limit by message count (pairs flattened) |
| 133 | + items: list[_HistoryItem] = list(self._history) |
| 134 | + if message_limit and message_limit > 0: |
| 135 | + # message_limit roughly means last N items (not pairs) to keep simple and efficient |
| 136 | + items = items[-min(message_limit, len(items)) :] |
| 137 | + |
| 138 | + def to_messages(hist: list[_HistoryItem]) -> list[PromptMessage]: |
| 139 | + msgs: list[PromptMessage] = [] |
| 140 | + for it in hist: |
| 141 | + if it.role == PromptMessageRole.USER.value: |
| 142 | + # Persisted node memory only stores text; inject as plain text content |
| 143 | + msgs.append(UserPromptMessage(content=it.text)) |
| 144 | + elif it.role == PromptMessageRole.ASSISTANT.value: |
| 145 | + msgs.append(AssistantPromptMessage(content=it.text)) |
| 146 | + return msgs |
| 147 | + |
| 148 | + messages = to_messages(items) |
| 149 | + # Token-based pruning from oldest |
| 150 | + if messages: |
| 151 | + tokens = self.model_instance.get_llm_num_tokens(messages) |
| 152 | + while tokens > max_token_limit and len(messages) > 1: |
| 153 | + messages.pop(0) |
| 154 | + tokens = self.model_instance.get_llm_num_tokens(messages) |
| 155 | + return messages |
| 156 | + |
| 157 | + def get_history_prompt_text( |
| 158 | + self, |
| 159 | + *, |
| 160 | + human_prefix: str = "Human", |
| 161 | + ai_prefix: str = "Assistant", |
| 162 | + max_token_limit: int = 2000, |
| 163 | + message_limit: int | None = None, |
| 164 | + ) -> str: |
| 165 | + self._load_if_needed() |
| 166 | + items: list[_HistoryItem] = list(self._history) |
| 167 | + if message_limit and message_limit > 0: |
| 168 | + items = items[-min(message_limit, len(items)) :] |
| 169 | + |
| 170 | + # Build messages to reuse token counting logic |
| 171 | + messages: list[PromptMessage] = [] |
| 172 | + for it in items: |
| 173 | + role_name = ( |
| 174 | + PromptMessageRole.USER |
| 175 | + if it.role == PromptMessageRole.USER.value |
| 176 | + else (PromptMessageRole.ASSISTANT if it.role == PromptMessageRole.ASSISTANT.value else None) |
| 177 | + ) |
| 178 | + if role_name is None: |
| 179 | + continue |
| 180 | + prefix = human_prefix if role_name == PromptMessageRole.USER else ai_prefix |
| 181 | + messages.append( |
| 182 | + UserPromptMessage(content=f"{prefix}: {it.text}") |
| 183 | + if role_name == PromptMessageRole.USER |
| 184 | + else AssistantPromptMessage(content=f"{prefix}: {it.text}") |
| 185 | + ) |
| 186 | + |
| 187 | + if messages: |
| 188 | + tokens = self.model_instance.get_llm_num_tokens(messages) |
| 189 | + while tokens > max_token_limit and len(messages) > 1: |
| 190 | + messages.pop(0) |
| 191 | + tokens = self.model_instance.get_llm_num_tokens(messages) |
| 192 | + |
| 193 | + # Convert back to the required text format |
| 194 | + lines: list[str] = [] |
| 195 | + for m in messages: |
| 196 | + if m.role == PromptMessageRole.USER: |
| 197 | + prefix = human_prefix |
| 198 | + elif m.role == PromptMessageRole.ASSISTANT: |
| 199 | + prefix = ai_prefix |
| 200 | + else: |
| 201 | + continue |
| 202 | + if isinstance(m.content, list): |
| 203 | + # Only text content was saved in this minimal implementation |
| 204 | + texts = [c.data for c in m.content if isinstance(c, TextPromptMessageContent)] |
| 205 | + text = "\n".join(texts) |
| 206 | + else: |
| 207 | + text = str(m.content) |
| 208 | + lines.append(f"{prefix}: {text}") |
| 209 | + return "\n".join(lines) |
| 210 | + |
| 211 | + def append_exchange(self, *, user_text: str | None, assistant_text: str | None) -> None: |
| 212 | + self._load_if_needed() |
| 213 | + if user_text: |
| 214 | + self._history.append(_HistoryItem(role=PromptMessageRole.USER.value, text=user_text)) |
| 215 | + if assistant_text: |
| 216 | + self._history.append(_HistoryItem(role=PromptMessageRole.ASSISTANT.value, text=assistant_text)) |
| 217 | + |
| 218 | + def clear(self) -> None: |
| 219 | + self._history = [] |
| 220 | + self._loaded = True |
| 221 | + self.save() |
0 commit comments