File size: 2,029 Bytes
b7b030d
0d61e07
b7b030d
 
 
0d61e07
 
 
 
 
 
 
 
 
b7b030d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d61e07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb06dfe
 
 
 
 
 
b7b030d
 
 
 
 
fb06dfe
 
 
 
 
 
 
0d61e07
 
 
 
 
 
 
 
 
 
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
import unicodedata
from enum import Enum
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, field_validator


class MemoryType(str, Enum):
    EPISODIC = "episodic"
    SEMANTIC = "semantic"
    STATE = "state"
    PROCEDURAL = "procedural"


_MEMORY_TYPE_ALIASES = {
    "episodico": MemoryType.EPISODIC,
    "episodic": MemoryType.EPISODIC,
    "semantico": MemoryType.SEMANTIC,
    "semantic": MemoryType.SEMANTIC,
    "estado": MemoryType.STATE,
    "state": MemoryType.STATE,
    "procedural": MemoryType.PROCEDURAL,
    "procedimental": MemoryType.PROCEDURAL,
}


def normalize_memory_type(value: Any) -> MemoryType:
    if isinstance(value, MemoryType):
        return value
    if value is None or (isinstance(value, str) and not value.strip()):
        return MemoryType.SEMANTIC
    key = unicodedata.normalize("NFKD", str(value).strip().lower())
    key = "".join(c for c in key if not unicodedata.combining(c))
    return _MEMORY_TYPE_ALIASES.get(key, MemoryType.SEMANTIC)


class Memory(BaseModel):
    id: str
    content: str
    type: MemoryType
    created_at: str
    source: str  # "seed", "agent", "user"
    context_tags: List[str]
    access_count: int = 0
    last_accessed: Optional[str] = None
    relevance_score: float = 1.0
    decay_rate: float
    active: bool = True
    summary: str


class NewMemoryItem(BaseModel):
    content: str
    type: MemoryType = MemoryType.SEMANTIC
    context_tags: List[str] = []
    summary: str = ""

    @field_validator("type", mode="before")
    @classmethod
    def coerce_memory_type(cls, value: Any) -> MemoryType:
        return normalize_memory_type(value)


class AgentLLMOutput(BaseModel):
    response: str
    memories_used: List[str] = []
    new_memories: List[NewMemoryItem] = []


class ChatRequest(BaseModel):
    message: str
    conversation_history: List[Dict[str, str]] = []


class ChatResponse(BaseModel):
    response: str
    memories_used: List[str]
    new_memories: List[Memory]
    all_memories: List[Memory]