|
| 1 | +"""CLI tool to inject AIMemory instructions into AI client configuration files.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import sys |
| 7 | +from importlib import resources |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +START_MARKER = "<!-- aimemory:start -->" |
| 11 | +END_MARKER = "<!-- aimemory:end -->" |
| 12 | + |
| 13 | + |
| 14 | +def _load_template(name: str) -> str: |
| 15 | + """Load an instruction template from the setup_instructions package.""" |
| 16 | + ref = resources.files("aimemory.setup_instructions").joinpath(name) |
| 17 | + return ref.read_text(encoding="utf-8") |
| 18 | + |
| 19 | + |
| 20 | +def _inject_block(file_path: Path, block_content: str) -> None: |
| 21 | + """Inject or replace a managed block in a file. |
| 22 | +
|
| 23 | + If the file contains START_MARKER...END_MARKER, the block between them |
| 24 | + is replaced. Otherwise the block is appended to the end of the file. |
| 25 | + If the file does not exist, it is created. |
| 26 | + """ |
| 27 | + managed_block = f"{START_MARKER}\n{block_content}\n{END_MARKER}\n" |
| 28 | + |
| 29 | + if file_path.exists(): |
| 30 | + text = file_path.read_text(encoding="utf-8") |
| 31 | + start_idx = text.find(START_MARKER) |
| 32 | + end_idx = text.find(END_MARKER) |
| 33 | + |
| 34 | + if start_idx != -1 and end_idx != -1: |
| 35 | + # Replace existing block |
| 36 | + before = text[:start_idx] |
| 37 | + after = text[end_idx + len(END_MARKER) :] |
| 38 | + # Strip trailing newline from after to avoid double newlines |
| 39 | + if after.startswith("\n"): |
| 40 | + after = after[1:] |
| 41 | + new_text = before + managed_block + after |
| 42 | + else: |
| 43 | + # Append to end |
| 44 | + if text and not text.endswith("\n"): |
| 45 | + text += "\n" |
| 46 | + new_text = text + "\n" + managed_block |
| 47 | + else: |
| 48 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 49 | + new_text = managed_block |
| 50 | + |
| 51 | + file_path.write_text(new_text, encoding="utf-8") |
| 52 | + |
| 53 | + |
| 54 | +def setup_openclaw(workspace: Path) -> None: |
| 55 | + """Inject AIMemory instructions into OpenClaw workspace files.""" |
| 56 | + workspace = workspace.expanduser() |
| 57 | + |
| 58 | + # SOUL.md — Memory Continuity section |
| 59 | + soul_path = workspace / "SOUL.md" |
| 60 | + soul_content = _load_template("openclaw_soul.md") |
| 61 | + _inject_block(soul_path, soul_content) |
| 62 | + print(f" Updated {soul_path}") |
| 63 | + |
| 64 | + # TOOLS.md — AIMemory tool section |
| 65 | + tools_path = workspace / "TOOLS.md" |
| 66 | + tools_content = _load_template("openclaw_tools.md") |
| 67 | + _inject_block(tools_path, tools_content) |
| 68 | + print(f" Updated {tools_path}") |
| 69 | + |
| 70 | + |
| 71 | +def setup_claude(claude_dir: Path) -> None: |
| 72 | + """Inject AIMemory instructions into Claude Code's CLAUDE.md.""" |
| 73 | + claude_dir = claude_dir.expanduser() |
| 74 | + claude_md = claude_dir / "CLAUDE.md" |
| 75 | + claude_content = _load_template("claude.md") |
| 76 | + _inject_block(claude_md, claude_content) |
| 77 | + print(f" Updated {claude_md}") |
| 78 | + |
| 79 | + |
| 80 | +def main() -> None: |
| 81 | + """Entry point for aimemory-setup CLI.""" |
| 82 | + parser = argparse.ArgumentParser( |
| 83 | + prog="aimemory-setup", |
| 84 | + description="Setup AIMemory instructions for AI clients", |
| 85 | + ) |
| 86 | + subparsers = parser.add_subparsers(dest="client") |
| 87 | + |
| 88 | + # openclaw subcommand |
| 89 | + oc = subparsers.add_parser("openclaw", help="Setup for OpenClaw") |
| 90 | + oc.add_argument( |
| 91 | + "--workspace", |
| 92 | + default="~/.openclaw/workspace", |
| 93 | + help="OpenClaw workspace path (default: ~/.openclaw/workspace)", |
| 94 | + ) |
| 95 | + |
| 96 | + # claude subcommand |
| 97 | + subparsers.add_parser("claude", help="Setup for Claude Code") |
| 98 | + |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + if args.client is None: |
| 102 | + parser.print_help() |
| 103 | + sys.exit(1) |
| 104 | + |
| 105 | + print(f"Setting up AIMemory for {args.client}...") |
| 106 | + |
| 107 | + if args.client == "openclaw": |
| 108 | + setup_openclaw(Path(args.workspace)) |
| 109 | + elif args.client == "claude": |
| 110 | + setup_claude(Path("~/.claude")) |
| 111 | + |
| 112 | + print("Done!") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments