fix memory mcp
This commit is contained in:
@ -22,16 +22,29 @@ def save_message(sender, message):
|
||||
with open(path, "w") as f:
|
||||
json.dump(logs, f, indent=2, ensure_ascii=False)
|
||||
|
||||
#def search_memory(query: str):
|
||||
# from glob import glob
|
||||
# all_logs = []
|
||||
# for file_path in sorted(MEMORY_DIR.glob("*.json")):
|
||||
# with open(file_path, "r") as f:
|
||||
# logs = json.load(f)
|
||||
# #matched = [entry for entry in logs if query in entry["message"]]
|
||||
# matched = [entry for entry in logs if query.lower() in entry["message"].lower()]
|
||||
# all_logs.extend(matched)
|
||||
# return all_logs[-5:] # 最新5件だけ返す
|
||||
|
||||
def search_memory(query: str):
|
||||
from glob import glob
|
||||
all_logs = []
|
||||
pattern = re.compile(re.escape(query), re.IGNORECASE)
|
||||
|
||||
for file_path in sorted(MEMORY_DIR.glob("*.json")):
|
||||
with open(file_path, "r") as f:
|
||||
logs = json.load(f)
|
||||
matched = [entry for entry in logs if query in entry["message"]]
|
||||
matched = [entry for entry in logs if pattern.search(entry["message"])]
|
||||
all_logs.extend(matched)
|
||||
return all_logs[-5:] # 最新5件だけ返す
|
||||
|
||||
return all_logs[-5:]
|
||||
|
||||
# scripts/memory_store.py
|
||||
import json
|
||||
@ -60,15 +73,31 @@ def save_message(sender, message):
|
||||
with open(path, "w") as f:
|
||||
json.dump(logs, f, indent=2, ensure_ascii=False)
|
||||
|
||||
# キーワードで過去のメモリを検索する(最新5件を返す)
|
||||
def search_memory(query: str):
|
||||
from glob import glob
|
||||
all_logs = []
|
||||
for file_path in sorted(MEMORY_DIR.glob("*.json")):
|
||||
try:
|
||||
with open(file_path, "r") as f:
|
||||
logs = json.load(f)
|
||||
matched = [entry for entry in logs if query.lower() in entry["message"].lower()]
|
||||
all_logs.extend(matched)
|
||||
except Exception as e:
|
||||
print(f"⚠️ 読み込み失敗: {file_path} ({e})")
|
||||
return all_logs[-5:] # 最新5件を返す
|
||||
with open(file_path, "r") as f:
|
||||
logs = json.load(f)
|
||||
matched = [
|
||||
entry for entry in logs
|
||||
if entry["sender"] == "user" and query in entry["message"]
|
||||
]
|
||||
all_logs.extend(matched)
|
||||
return all_logs[-5:] # 最新5件だけ返す
|
||||
def search_memory(query: str):
|
||||
from glob import glob
|
||||
all_logs = []
|
||||
seen_messages = set() # すでに見たメッセージを保持
|
||||
|
||||
for file_path in sorted(MEMORY_DIR.glob("*.json")):
|
||||
with open(file_path, "r") as f:
|
||||
logs = json.load(f)
|
||||
for entry in logs:
|
||||
if entry["sender"] == "user" and query in entry["message"]:
|
||||
# すでに同じメッセージが結果に含まれていなければ追加
|
||||
if entry["message"] not in seen_messages:
|
||||
all_logs.append(entry)
|
||||
seen_messages.add(entry["message"])
|
||||
|
||||
return all_logs[-5:] # 最新5件だけ返す
|
||||
|
Reference in New Issue
Block a user