-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
294 lines (249 loc) · 9.24 KB
/
server.py
File metadata and controls
294 lines (249 loc) · 9.24 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import json
import sqlite3
import uuid
from datetime import datetime, timezone
from pathlib import Path
import sqlite_vec
from mcp.server.fastmcp import FastMCP
# --- Config ---
DB_PATH = Path("/data/agent_memory.db")
EMBED_MODEL = "all-MiniLM-L6-v2"
EMBED_DIM = 384
VALID_CATEGORIES = {"corrections", "preferences", "patterns", "decisions", "facts"}
# --- Init ---
mcp = FastMCP("kiro-memory")
model = None
_initialized = False
def get_db() -> sqlite3.Connection:
db = sqlite3.connect(str(DB_PATH))
db.enable_load_extension(True)
sqlite_vec.load(db)
db.enable_load_extension(False)
db.row_factory = sqlite3.Row
return db
def init_db():
db = get_db()
db.executescript(f"""
CREATE TABLE IF NOT EXISTS memories (
id TEXT PRIMARY KEY,
category TEXT NOT NULL,
content TEXT NOT NULL,
source TEXT,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL
);
CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
content, category, content=memories, content_rowid=rowid
);
CREATE VIRTUAL TABLE IF NOT EXISTS memories_vec USING vec0(
id TEXT PRIMARY KEY,
embedding float[{EMBED_DIM}]
);
CREATE TABLE IF NOT EXISTS proposals (
id TEXT PRIMARY KEY,
category TEXT NOT NULL,
content TEXT NOT NULL,
source TEXT,
created_at TEXT NOT NULL
);
""")
# FTS triggers for sync
for op, body in [
("INSERT", "INSERT INTO memories_fts(rowid, content, category) VALUES (new.rowid, new.content, new.category)"),
("DELETE", "INSERT INTO memories_fts(memories_fts, rowid, content, category) VALUES ('delete', old.rowid, old.content, old.category)"),
("UPDATE", "INSERT INTO memories_fts(memories_fts, rowid, content, category) VALUES ('delete', old.rowid, old.content, old.category); INSERT INTO memories_fts(rowid, content, category) VALUES (new.rowid, new.content, new.category)"),
]:
db.execute(f"""
CREATE TRIGGER IF NOT EXISTS memories_fts_{op.lower()}
AFTER {op} ON memories BEGIN {body}; END
""")
db.commit()
db.close()
def embed(text: str) -> bytes:
global model
if model is None:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(EMBED_MODEL)
vec = model.encode(text, normalize_embeddings=True)
return vec.tobytes()
def ensure_init():
global _initialized
if not _initialized:
init_db()
_initialized = True
def now_iso() -> str:
return datetime.now(timezone.utc).isoformat()
def validate_category(category: str):
if category not in VALID_CATEGORIES:
raise ValueError(f"Invalid category '{category}'. Must be one of: {', '.join(sorted(VALID_CATEGORIES))}")
# --- Tools ---
@mcp.tool()
def memory_store(category: str, content: str, source: str = "") -> str:
"""Store a confirmed memory. Categories: corrections, preferences, patterns, decisions, facts."""
validate_category(category)
ensure_init()
mid = str(uuid.uuid4())
ts = now_iso()
db = get_db()
db.execute(
"INSERT INTO memories (id, category, content, source, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(mid, category, content, source, ts, ts),
)
db.execute(
"INSERT INTO memories_vec (id, embedding) VALUES (?, ?)",
(mid, embed(content)),
)
db.commit()
db.close()
return json.dumps({"id": mid, "status": "stored"})
@mcp.tool()
def memory_query(query: str, category: str = "", limit: int = 5) -> str:
"""Hybrid semantic + keyword search over memories. Optionally filter by category."""
if category:
validate_category(category)
ensure_init()
db = get_db()
query_vec = embed(query)
# Vector search
vec_sql = """
SELECT v.id, v.distance
FROM memories_vec v
WHERE v.embedding MATCH ? AND k = ?
ORDER BY v.distance
"""
vec_rows = db.execute(vec_sql, (query_vec, limit * 2)).fetchall()
vec_scores = {r["id"]: 1.0 - r["distance"] for r in vec_rows}
# FTS search
fts_sql = """
SELECT m.id, fts.rank
FROM memories_fts fts
JOIN memories m ON m.rowid = fts.rowid
WHERE memories_fts MATCH ?
ORDER BY fts.rank
LIMIT ?
"""
try:
fts_rows = db.execute(fts_sql, (query, limit * 2)).fetchall()
fts_scores = {r["id"]: 1.0 / (1.0 - r["rank"]) for r in fts_rows}
except sqlite3.OperationalError:
fts_scores = {}
# Merge scores (0.7 semantic + 0.3 keyword)
all_ids = set(vec_scores) | set(fts_scores)
scored = []
for mid in all_ids:
score = 0.7 * vec_scores.get(mid, 0.0) + 0.3 * fts_scores.get(mid, 0.0)
scored.append((mid, score))
scored.sort(key=lambda x: x[1], reverse=True)
results = []
for mid, score in scored[:limit]:
cat_filter = " AND category = ?" if category else ""
params = [mid] + ([category] if category else [])
row = db.execute(f"SELECT * FROM memories WHERE id = ?{cat_filter}", params).fetchone()
if row:
results.append({**dict(row), "score": round(score, 4)})
db.close()
return json.dumps(results)
@mcp.tool()
def memory_update(id: str, content: str = "", category: str = "") -> str:
"""Update an existing memory's content and/or category by ID."""
if category:
validate_category(category)
ensure_init()
db = get_db()
row = db.execute("SELECT * FROM memories WHERE id = ?", (id,)).fetchone()
if not row:
db.close()
return json.dumps({"error": "Memory not found"})
new_content = content or row["content"]
new_category = category or row["category"]
ts = now_iso()
db.execute(
"UPDATE memories SET content = ?, category = ?, updated_at = ? WHERE id = ?",
(new_content, new_category, ts, id),
)
if content:
db.execute("DELETE FROM memories_vec WHERE id = ?", (id,))
db.execute("INSERT INTO memories_vec (id, embedding) VALUES (?, ?)", (id, embed(new_content)))
db.commit()
db.close()
return json.dumps({"id": id, "status": "updated"})
@mcp.tool()
def memory_delete(id: str) -> str:
"""Delete a memory by ID."""
ensure_init()
db = get_db()
row = db.execute("SELECT id FROM memories WHERE id = ?", (id,)).fetchone()
if not row:
db.close()
return json.dumps({"error": "Memory not found"})
db.execute("DELETE FROM memories WHERE id = ?", (id,))
db.execute("DELETE FROM memories_vec WHERE id = ?", (id,))
db.commit()
db.close()
return json.dumps({"id": id, "status": "deleted"})
@mcp.tool()
def memory_propose(category: str, content: str, source: str = "") -> str:
"""Queue a proposed memory for end-of-session review."""
validate_category(category)
ensure_init()
pid = str(uuid.uuid4())
db = get_db()
db.execute(
"INSERT INTO proposals (id, category, content, source, created_at) VALUES (?, ?, ?, ?, ?)",
(pid, category, content, source, now_iso()),
)
db.commit()
db.close()
return json.dumps({"id": pid, "status": "proposed"})
@mcp.tool()
def memory_review() -> str:
"""Return all pending memory proposals for user review."""
ensure_init()
db = get_db()
rows = db.execute("SELECT * FROM proposals ORDER BY created_at").fetchall()
db.close()
return json.dumps([dict(r) for r in rows])
@mcp.tool()
def memory_confirm(accepted_ids: list[str] = [], rejected_ids: list[str] = []) -> str:
"""Accept or reject proposals. Accepted ones become confirmed memories."""
ensure_init()
db = get_db()
stored = []
for pid in accepted_ids:
row = db.execute("SELECT * FROM proposals WHERE id = ?", (pid,)).fetchone()
if not row:
continue
mid = str(uuid.uuid4())
ts = now_iso()
db.execute(
"INSERT INTO memories (id, category, content, source, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
(mid, row["category"], row["content"], row["source"], ts, ts),
)
db.execute("INSERT INTO memories_vec (id, embedding) VALUES (?, ?)", (mid, embed(row["content"])))
db.execute("DELETE FROM proposals WHERE id = ?", (pid,))
stored.append(mid)
rejected = 0
for pid in rejected_ids:
cur = db.execute("DELETE FROM proposals WHERE id = ?", (pid,))
rejected += cur.rowcount
db.commit()
db.close()
return json.dumps({"stored": stored, "rejected": rejected})
@mcp.tool()
def memory_stats() -> str:
"""Return memory counts by category and last updated timestamp."""
ensure_init()
db = get_db()
rows = db.execute(
"SELECT category, COUNT(*) as count, MAX(updated_at) as last_updated FROM memories GROUP BY category"
).fetchall()
total = db.execute("SELECT COUNT(*) as n FROM memories").fetchone()["n"]
pending = db.execute("SELECT COUNT(*) as n FROM proposals").fetchone()["n"]
db.close()
return json.dumps({
"total": total,
"pending_proposals": pending,
"by_category": {r["category"]: {"count": r["count"], "last_updated": r["last_updated"]} for r in rows},
})
if __name__ == "__main__":
mcp.run(transport="stdio")