File size: 10,287 Bytes
8d1819a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from python.helpers.api import ApiHandler, Request, Response
from python.helpers.memory import Memory, get_existing_memory_subdirs, get_context_memory_subdir
from python.helpers import files
from models import ModelConfig, ModelType
from langchain_core.documents import Document
from agent import AgentContext


class MemoryDashboard(ApiHandler):

    async def process(self, input: dict, request: Request) -> dict | Response:
        try:
            action = input.get("action", "search")
            if action == "get_memory_subdirs":
                return await self._get_memory_subdirs()
            elif action == "get_current_memory_subdir":
                return await self._get_current_memory_subdir(input)
            elif action == "search":
                return await self._search_memories(input)
            elif action == "delete":
                return await self._delete_memory(input)
            elif action == "bulk_delete":
                return await self._bulk_delete_memories(input)
            elif action == "update":
                return await self._update_memory(input)
            else:
                return {
                    "success": False,
                    "error": f"Unknown action: {action}",
                    "memories": [],
                    "total_count": 0,
                }

        except Exception as e:
            return {"success": False, "error": str(e), "memories": [], "total_count": 0}

    async def _delete_memory(self, input: dict) -> dict:
        """Delete a memory by ID from the specified subdirectory."""
        try:
            memory_subdir = input.get("memory_subdir", "default")
            memory_id = input.get("memory_id")

            if not memory_id:
                return {"success": False, "error": "Memory ID is required for deletion"}

            memory = await Memory.get_by_subdir(memory_subdir, preload_knowledge=False)

            rem = await memory.delete_documents_by_ids([memory_id])

            if len(rem) == 0:
                return {
                    "success": False,
                    "error": f"Memory with ID '{memory_id}' not found",
                }
            else:
                return {
                    "success": True,
                    "message": f"Memory {memory_id} deleted successfully",
                }

        except Exception as e:
            return {"success": False, "error": f"Failed to delete memory: {str(e)}"}

    async def _bulk_delete_memories(self, input: dict) -> dict:
        """Delete multiple memories by IDs from the specified subdirectory."""
        try:
            memory_subdir = input.get("memory_subdir", "default")
            memory_ids = input.get("memory_ids", [])

            if not memory_ids:
                return {
                    "success": False,
                    "error": "No memory IDs provided for bulk deletion",
                }

            if not isinstance(memory_ids, list):
                return {
                    "success": False,
                    "error": "Memory IDs must be provided as a list",
                }

            # delete
            memory = await Memory.get_by_subdir(memory_subdir, preload_knowledge=False)
            rem = await memory.delete_documents_by_ids(memory_ids)

            if len(rem) == len(memory_ids):
                return {
                    "success": True,
                    "message": f"Successfully deleted {len(memory_ids)} memories",
                }
            elif len(rem) > 0:
                return {
                    "success": True,
                    "message": f"Successfully deleted {len(rem)} memories. {len(memory_ids) - len(rem)} failed.",
                }
            else:
                return {
                    "success": False,
                    "error": f"Failed to delete any memories.",
                }

        except Exception as e:
            return {
                "success": False,
                "error": f"Failed to bulk delete memories: {str(e)}",
            }

    async def _get_current_memory_subdir(self, input: dict) -> dict:
        """Get the current memory subdirectory from the active context."""
        try:
            # Try to get the context from the request
            context_id = input.get("context_id", None)
            if not context_id:
                # Fallback to default if no context available
                return {"success": True, "memory_subdir": "default"}

            context = AgentContext.use(context_id)
            if not context:
                return {"success": True, "memory_subdir": "default"}

            memory_subdir = get_context_memory_subdir(context)
            return {"success": True, "memory_subdir": memory_subdir or "default"}

        except Exception:
            return {
                "success": True,  # Still success, just fallback to default
                "memory_subdir": "default",
            }

    async def _get_memory_subdirs(self) -> dict:
        """Get available memory subdirectories."""
        try:
            # Get subdirectories from memory folder
            subdirs = get_existing_memory_subdirs()
            return {"success": True, "subdirs": subdirs}
        except Exception as e:
            return {
                "success": False,
                "error": f"Failed to get memory subdirectories: {str(e)}",
                "subdirs": ["default"],
            }

    async def _search_memories(self, input: dict) -> dict:
        """Search memories in the specified subdirectory."""
        try:
            # Get search parameters
            memory_subdir = input.get("memory_subdir", "default")
            area_filter = input.get("area", "")  # Filter by memory area
            search_query = input.get("search", "")  # Full-text search query
            limit = input.get("limit", 100)  # Number of results to return
            threshold = input.get("threshold", 0.6)  # Similarity threshold

            memory = await Memory.get_by_subdir(memory_subdir, preload_knowledge=False)

            memories = []

            if search_query:
                docs = await memory.search_similarity_threshold(
                    query=search_query,
                    limit=limit,
                    threshold=threshold,
                    filter=f"area == '{area_filter}'" if area_filter else "",
                )
                memories = docs
            else:
                # If no search query, get all memories from specified area(s)
                all_docs = memory.db.get_all_docs()
                for doc_id, doc in all_docs.items():
                    # Apply area filter if specified
                    if area_filter and doc.metadata.get("area", "") != area_filter:
                        continue
                    memories.append(doc)

                # sort by timestamp
                def get_sort_key(m):
                    timestamp = m.metadata.get("timestamp", "0000-00-00 00:00:00")
                    return timestamp

                memories.sort(key=get_sort_key, reverse=True)

                # Apply limit AFTER sorting to get the newest entries
                if limit and len(memories) > limit:
                    memories = memories[:limit]

            # Format memories for the dashboard
            formatted_memories = [self._format_memory_for_dashboard(m) for m in memories]

            # Get summary statistics
            total_memories = len(formatted_memories)
            knowledge_count = sum(
                1 for m in formatted_memories if m["knowledge_source"]
            )
            conversation_count = total_memories - knowledge_count

            # Get total count of all memories in database (unfiltered)
            total_db_count = len(memory.db.get_all_docs())

            return {
                "success": True,
                "memories": formatted_memories,
                "total_count": total_memories,
                "total_db_count": total_db_count,
                "knowledge_count": knowledge_count,
                "conversation_count": conversation_count,
                "search_query": search_query,
                "area_filter": area_filter,
                "memory_subdir": memory_subdir,
            }

        except Exception as e:
            return {"success": False, "error": str(e), "memories": [], "total_count": 0}

    def _format_memory_for_dashboard(self, m: Document) -> dict:
        """Format a memory document for the dashboard."""
        metadata = m.metadata
        return {
            "id": metadata.get("id", "unknown"),
            "area": metadata.get("area", "unknown"),
            "timestamp": metadata.get("timestamp", "unknown"),
            # "content_preview": m.page_content[:200]
            # + ("..." if len(m.page_content) > 200 else ""),
            "content_full": m.page_content,
            "knowledge_source": metadata.get("knowledge_source", False),
            "source_file": metadata.get("source_file", ""),
            "file_type": metadata.get("file_type", ""),
            "consolidation_action": metadata.get("consolidation_action", ""),
            "tags": metadata.get("tags", []),
            "metadata": metadata,  # Include full metadata for advanced users
        }

    async def _update_memory(self, input: dict) -> dict:
        try:
            memory_subdir = input.get("memory_subdir")
            original = input.get("original")
            edited = input.get("edited")

            if not memory_subdir or not original or not edited:
                return {"success": False, "error": "Missing required parameters"}

            doc = Document(
                page_content=edited["content_full"],
                metadata=edited["metadata"],
            )

            memory = await Memory.get_by_subdir(memory_subdir, preload_knowledge=False)
            id = (await memory.update_documents([doc]))[0]
            doc = memory.get_document_by_id(id)
            formatted_doc = self._format_memory_for_dashboard(doc) if doc else None

            return {"success": formatted_doc is not None, "memory": formatted_doc}
        except Exception as e:
            return {"success": False, "error": str(e), "memory": None}