feat(mem0-plugin): add export/import skills + competing tool importers
Tier 3 implementation: - #10: Unified project_id resolver already complete (shared scripts/) - #11: /mem0:export dumps all project memories as YAML-frontmatter markdown; /mem0:import restores from same format. Round-trip capable. parse_export_file.py handles parsing (stdlib-only). - #12: import_competing_tools.py imports from .cursorrules, copilot instructions, cline memory-bank, continue.dev rules. Splits by section headers, POSTs with app_id + infer=False.
This commit is contained in:
@@ -0,0 +1,327 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Import memories from competing AI tool configuration files into mem0.
|
||||
|
||||
Sub-commands (via sys.argv[1]):
|
||||
cursorrules [--path .cursorrules]
|
||||
copilot [--path .github/copilot-instructions.md]
|
||||
cline [--path memory-bank/]
|
||||
continue [--path .continue/rules.md]
|
||||
|
||||
Each sub-command reads configuration files from competing tools,
|
||||
splits them into chunks, and POSTs each chunk to the mem0 API as a
|
||||
project_profile memory.
|
||||
|
||||
Output: progress messages to stdout, errors to stderr
|
||||
Exit: 0 always
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from _identity import resolve_api_key, resolve_user_id
|
||||
from _project import resolve_branch, resolve_project_id
|
||||
|
||||
API_URL = "https://api.mem0.ai"
|
||||
MIN_CHUNK_CHARS = 50
|
||||
MAX_CHUNK_CHARS = 10_000
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Content splitting utilities
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def split_by_headers(content: str, header_prefix: str = "## ") -> list[str]:
|
||||
"""Split content by Markdown header lines (e.g. '## ').
|
||||
|
||||
The header line is included at the start of each chunk.
|
||||
Returns a list of non-empty chunk strings.
|
||||
"""
|
||||
chunks: list[str] = []
|
||||
current_lines: list[str] = []
|
||||
|
||||
for line in content.splitlines(keepends=True):
|
||||
if line.startswith(header_prefix) and current_lines:
|
||||
chunk = "".join(current_lines).strip()
|
||||
if chunk:
|
||||
chunks.append(chunk)
|
||||
current_lines = [line]
|
||||
else:
|
||||
current_lines.append(line)
|
||||
|
||||
if current_lines:
|
||||
chunk = "".join(current_lines).strip()
|
||||
if chunk:
|
||||
chunks.append(chunk)
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def split_by_hr_or_headers(content: str) -> list[str]:
|
||||
"""Split content by '---' horizontal rules or '## ' headers.
|
||||
|
||||
Used for .continue/rules.md which may use either convention.
|
||||
"""
|
||||
import re
|
||||
|
||||
# Split on lines that are exactly "---" or start with "## "
|
||||
chunks: list[str] = []
|
||||
current_lines: list[str] = []
|
||||
|
||||
for line in content.splitlines(keepends=True):
|
||||
is_hr = re.match(r"^---\s*$", line)
|
||||
is_h2 = line.startswith("## ")
|
||||
|
||||
if (is_hr or is_h2) and current_lines:
|
||||
chunk = "".join(current_lines).strip()
|
||||
if chunk:
|
||||
chunks.append(chunk)
|
||||
current_lines = [] if is_hr else [line]
|
||||
else:
|
||||
current_lines.append(line)
|
||||
|
||||
if current_lines:
|
||||
chunk = "".join(current_lines).strip()
|
||||
if chunk:
|
||||
chunks.append(chunk)
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def filter_and_truncate(chunks: list[str]) -> list[str]:
|
||||
"""Filter out chunks shorter than MIN_CHUNK_CHARS, truncate long chunks."""
|
||||
result: list[str] = []
|
||||
for chunk in chunks:
|
||||
if len(chunk) < MIN_CHUNK_CHARS:
|
||||
continue
|
||||
if len(chunk) > MAX_CHUNK_CHARS:
|
||||
chunk = chunk[:MAX_CHUNK_CHARS]
|
||||
result.append(chunk)
|
||||
return result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# API helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def post_memory(api_key: str, content: str, user_id: str, project_id: str, branch: str, source: str) -> bool:
|
||||
"""POST a single memory chunk to the mem0 API."""
|
||||
metadata: dict = {
|
||||
"type": "project_profile",
|
||||
"source": source,
|
||||
}
|
||||
if branch:
|
||||
metadata["branch"] = branch
|
||||
|
||||
body = {
|
||||
"messages": [{"role": "user", "content": content}],
|
||||
"user_id": user_id,
|
||||
"app_id": project_id,
|
||||
"metadata": metadata,
|
||||
"infer": False,
|
||||
}
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
f"{API_URL}/v1/memories/",
|
||||
data=data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Token {api_key}",
|
||||
},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20) as resp:
|
||||
return resp.status in (200, 201)
|
||||
except urllib.error.URLError as e:
|
||||
print(f" [warn] API call failed: {e}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def import_chunks(chunks: list[str], api_key: str, user_id: str, project_id: str, branch: str, source: str) -> int:
|
||||
"""Import a list of content chunks; return number of successful imports."""
|
||||
success = 0
|
||||
for chunk in chunks:
|
||||
if post_memory(api_key, chunk, user_id, project_id, branch, source):
|
||||
success += 1
|
||||
return success
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Sub-command implementations
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _parse_path_arg(args: list[str], flag: str, default: str) -> str:
|
||||
"""Extract --path <value> from args list, falling back to default."""
|
||||
for i, arg in enumerate(args):
|
||||
if arg == flag and i + 1 < len(args):
|
||||
return args[i + 1]
|
||||
if arg.startswith(f"{flag}="):
|
||||
return arg[len(flag) + 1:]
|
||||
return default
|
||||
|
||||
|
||||
def cmd_cursorrules(args: list[str]) -> None:
|
||||
path = _parse_path_arg(args, "--path", ".cursorrules")
|
||||
source = "cursor-import"
|
||||
|
||||
api_key = resolve_api_key()
|
||||
user_id = resolve_user_id()
|
||||
project_id = resolve_project_id()
|
||||
branch = resolve_branch()
|
||||
|
||||
if not api_key:
|
||||
print("Error: MEM0_API_KEY not set", file=sys.stderr)
|
||||
return
|
||||
|
||||
if not os.path.isfile(path):
|
||||
print(f"File not found: {path}", file=sys.stderr)
|
||||
return
|
||||
|
||||
with open(path, encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
|
||||
raw_chunks = split_by_headers(content, "## ")
|
||||
# Fall back to treating the whole file as one chunk if no headers found
|
||||
if not raw_chunks:
|
||||
raw_chunks = [content.strip()] if content.strip() else []
|
||||
|
||||
chunks = filter_and_truncate(raw_chunks)
|
||||
n = import_chunks(chunks, api_key, user_id, project_id, branch, source)
|
||||
print(f"Imported {n} memories from {source} ({path})")
|
||||
|
||||
|
||||
def cmd_copilot(args: list[str]) -> None:
|
||||
path = _parse_path_arg(args, "--path", ".github/copilot-instructions.md")
|
||||
source = "copilot-import"
|
||||
|
||||
api_key = resolve_api_key()
|
||||
user_id = resolve_user_id()
|
||||
project_id = resolve_project_id()
|
||||
branch = resolve_branch()
|
||||
|
||||
if not api_key:
|
||||
print("Error: MEM0_API_KEY not set", file=sys.stderr)
|
||||
return
|
||||
|
||||
if not os.path.isfile(path):
|
||||
print(f"File not found: {path}", file=sys.stderr)
|
||||
return
|
||||
|
||||
with open(path, encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
|
||||
raw_chunks = split_by_headers(content, "## ")
|
||||
if not raw_chunks:
|
||||
raw_chunks = [content.strip()] if content.strip() else []
|
||||
|
||||
chunks = filter_and_truncate(raw_chunks)
|
||||
n = import_chunks(chunks, api_key, user_id, project_id, branch, source)
|
||||
print(f"Imported {n} memories from {source} ({path})")
|
||||
|
||||
|
||||
def cmd_cline(args: list[str]) -> None:
|
||||
dir_path = _parse_path_arg(args, "--path", "memory-bank/")
|
||||
source = "cline-import"
|
||||
|
||||
api_key = resolve_api_key()
|
||||
user_id = resolve_user_id()
|
||||
project_id = resolve_project_id()
|
||||
branch = resolve_branch()
|
||||
|
||||
if not api_key:
|
||||
print("Error: MEM0_API_KEY not set", file=sys.stderr)
|
||||
return
|
||||
|
||||
if not os.path.isdir(dir_path):
|
||||
print(f"Directory not found: {dir_path}", file=sys.stderr)
|
||||
return
|
||||
|
||||
md_files = sorted(
|
||||
f for f in os.listdir(dir_path) if f.endswith(".md")
|
||||
)
|
||||
if not md_files:
|
||||
print(f"No .md files found in {dir_path}", file=sys.stderr)
|
||||
return
|
||||
|
||||
total = 0
|
||||
for filename in md_files:
|
||||
filepath = os.path.join(dir_path, filename)
|
||||
with open(filepath, encoding="utf-8", errors="replace") as f:
|
||||
content = f.read().strip()
|
||||
if not content:
|
||||
continue
|
||||
chunks = filter_and_truncate([content])
|
||||
n = import_chunks(chunks, api_key, user_id, project_id, branch, source)
|
||||
total += n
|
||||
|
||||
print(f"Imported {total} memories from {source} ({dir_path})")
|
||||
|
||||
|
||||
def cmd_continue(args: list[str]) -> None:
|
||||
path = _parse_path_arg(args, "--path", ".continue/rules.md")
|
||||
source = "continue-import"
|
||||
|
||||
api_key = resolve_api_key()
|
||||
user_id = resolve_user_id()
|
||||
project_id = resolve_project_id()
|
||||
branch = resolve_branch()
|
||||
|
||||
if not api_key:
|
||||
print("Error: MEM0_API_KEY not set", file=sys.stderr)
|
||||
return
|
||||
|
||||
if not os.path.isfile(path):
|
||||
print(f"File not found: {path}", file=sys.stderr)
|
||||
return
|
||||
|
||||
with open(path, encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
|
||||
raw_chunks = split_by_hr_or_headers(content)
|
||||
if not raw_chunks:
|
||||
raw_chunks = [content.strip()] if content.strip() else []
|
||||
|
||||
chunks = filter_and_truncate(raw_chunks)
|
||||
n = import_chunks(chunks, api_key, user_id, project_id, branch, source)
|
||||
print(f"Imported {n} memories from {source} ({path})")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Entry point
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
COMMANDS = {
|
||||
"cursorrules": cmd_cursorrules,
|
||||
"copilot": cmd_copilot,
|
||||
"cline": cmd_cline,
|
||||
"continue": cmd_continue,
|
||||
}
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in COMMANDS:
|
||||
available = ", ".join(COMMANDS.keys())
|
||||
print("Usage: import_competing_tools.py <subcommand> [--path <path>]", file=sys.stderr)
|
||||
print(f"Subcommands: {available}", file=sys.stderr)
|
||||
sys.exit(0)
|
||||
|
||||
subcommand = sys.argv[1]
|
||||
remaining_args = sys.argv[2:]
|
||||
COMMANDS[subcommand](remaining_args)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {e}", file=sys.stderr)
|
||||
sys.exit(0)
|
||||
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Parse a mem0 export file and output JSON.
|
||||
|
||||
Input: path to a mem0-export-*.md file (sys.argv[1])
|
||||
Output: JSON array of memory records to stdout
|
||||
Exit: 0 always
|
||||
|
||||
Each block in the file is delimited by lines containing exactly "---".
|
||||
Blocks have a YAML-like frontmatter section (key: value lines) followed
|
||||
by a blank line and the memory content text.
|
||||
|
||||
Example block format:
|
||||
---
|
||||
id: abc123
|
||||
created_at: 2024-01-01T00:00:00Z
|
||||
type: task_learnings
|
||||
confidence: 0.9
|
||||
branch: main
|
||||
files: src/foo.py, src/bar.py
|
||||
categories: coding_conventions, task_learnings
|
||||
---
|
||||
The actual memory content text goes here.
|
||||
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
def parse_blocks(content: str) -> list[dict]:
|
||||
"""Split content on '---' boundaries and parse each block.
|
||||
|
||||
Returns a list of dicts with keys:
|
||||
id, type, confidence, branch, files (list), categories (list), content (str)
|
||||
|
||||
Blocks with empty content are skipped.
|
||||
Missing optional fields default to "" (scalar) or [] (list fields).
|
||||
"""
|
||||
# Normalise line endings
|
||||
content = content.replace("\r\n", "\n").replace("\r", "\n")
|
||||
|
||||
# Split on lines that are exactly "---"
|
||||
raw_blocks = re.split(r"(?m)^---\s*$", content)
|
||||
|
||||
# After splitting on "---", the structure for each memory is:
|
||||
# raw_blocks[0] = preamble (before first ---, typically empty)
|
||||
# raw_blocks[1] = frontmatter for block 1
|
||||
# raw_blocks[2] = content for block 1
|
||||
# raw_blocks[3] = frontmatter for block 2
|
||||
# raw_blocks[4] = content for block 2
|
||||
# ...
|
||||
# So frontmatter blocks are at odd indices (1, 3, 5, ...) and
|
||||
# content blocks at even indices (2, 4, 6, ...).
|
||||
|
||||
results: list[dict] = []
|
||||
|
||||
# Pair up frontmatter + content starting at index 1
|
||||
i = 1
|
||||
while i < len(raw_blocks):
|
||||
frontmatter_raw = raw_blocks[i]
|
||||
content_raw = raw_blocks[i + 1] if i + 1 < len(raw_blocks) else ""
|
||||
|
||||
# Parse the frontmatter key-value pairs
|
||||
fm = _parse_frontmatter(frontmatter_raw)
|
||||
|
||||
# Strip leading/trailing whitespace from content
|
||||
memory_content = content_raw.strip()
|
||||
|
||||
# Skip blocks with empty content
|
||||
if not memory_content:
|
||||
i += 2
|
||||
continue
|
||||
|
||||
record = {
|
||||
"id": fm.get("id", ""),
|
||||
"type": fm.get("type", ""),
|
||||
"confidence": fm.get("confidence", ""),
|
||||
"branch": fm.get("branch", ""),
|
||||
"files": _parse_list_field(fm.get("files", "")),
|
||||
"categories": _parse_list_field(fm.get("categories", "")),
|
||||
"content": memory_content,
|
||||
}
|
||||
|
||||
# Include created_at if present
|
||||
if "created_at" in fm:
|
||||
record["created_at"] = fm["created_at"]
|
||||
|
||||
results.append(record)
|
||||
i += 2
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def _parse_frontmatter(text: str) -> dict[str, str]:
|
||||
"""Parse simple 'key: value' lines from frontmatter text.
|
||||
|
||||
Only the first colon is used as the delimiter — values may contain colons.
|
||||
Lines not matching 'key: value' are ignored.
|
||||
"""
|
||||
result: dict[str, str] = {}
|
||||
for line in text.splitlines():
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
match = re.match(r"^([A-Za-z_][A-Za-z0-9_]*)\s*:\s*(.*)$", line)
|
||||
if match:
|
||||
key = match.group(1).strip()
|
||||
value = match.group(2).strip()
|
||||
result[key] = value
|
||||
return result
|
||||
|
||||
|
||||
def _parse_list_field(value: str) -> list[str]:
|
||||
"""Split a comma-separated value into a list, stripping whitespace.
|
||||
|
||||
Returns [] for empty/whitespace-only input.
|
||||
"""
|
||||
if not value or not value.strip():
|
||||
return []
|
||||
return [item.strip() for item in value.split(",") if item.strip()]
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: parse_export_file.py <path-to-export-file>", file=sys.stderr)
|
||||
print("[]")
|
||||
sys.exit(0)
|
||||
|
||||
filepath = sys.argv[1]
|
||||
try:
|
||||
with open(filepath, encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
except OSError as e:
|
||||
print(f"Error reading file: {e}", file=sys.stderr)
|
||||
print("[]")
|
||||
sys.exit(0)
|
||||
|
||||
records = parse_blocks(content)
|
||||
print(json.dumps(records, ensure_ascii=False, indent=2))
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user