nm-agents 是做什么的?
A pure Python AI Agent runtime kernel: main loop + routing + model management + context + permissions + tool registration + self-evolution + persistent memory. Does not include any business tools—those are registered by the integrating party as needed.
When to Use
- When you need to run an agent main loop (input → memory re-ingestion → route model → call LLM → execute tools → consolidate memory)
- When you need persistent memory (cross-session decisions/facts/trajectories, surviving compression and model switches)
- When you need task routing (local/cloud model selection based on signals), permission checking, and a self-evolution pipeline
- It is a pure library: tools are registered by the integrating party
Installation
pip install -e .
# Dependencies: openai>=2.0, pydantic>=2.0, requests>=2.30 (Python >= 3.10)
Minimal Setup (AgentLoop)
from nm.config import HarnessConfig, DEFAULT_SYSTEM_PROMPT
from nm.agent_loop import AgentLoop
from nm.memory.memory_adapter import MemoryAdapter
from nm.context_manager import ContextManager
from nm.permission_checker import PermissionChecker
from nm.tool_registry import ToolRegistry
config = HarnessConfig(stream_output=False)
mem = MemoryAdapter("memory_store.json", "transcript.jsonl")
ctx = ContextManager(DEFAULT_SYSTEM_PROMPT, workspace_dir=".")
router = TaskRouter(model_manager) # Must construct ModelManager first and register accounts/groups
reg = ToolRegistry().create_default() # Only includes get_tool_detail; register your business tools yourself
perm = PermissionChecker(mode=PermissionMode.AUTO)
harness = AgentLoop(config, mem, router, reg, ctx, perm)
result = harness.run("Help me organize this data", user_id="user1")
run() returns {text, tool_calls, usage, model, turns}.
Memory API (MemoryAdapter)
ingest(role, content, scope)/distill(scope): Write conversation + distill into structured memoryrecall(scopes): Full re-ingestion as a single text blockrecall_anchors(scopes): Anchor layer (decisions + facts), typically kept in system promptrecall_detail(scopes): Full detail (trajectory transcript), re-ingested on demandcapture({layer, ...}): Explicitly capture a structured memory entrymark_used(ids)/stats(): Read receipts and statistics
Memory closure: ContextManager calls the on_compaction callback during compression to consolidate the compressed original text into anchors; AgentLoop supplements full details when switching models—both memory loss scenarios (compression and model switch) are covered.
Configuration
- Memory defaults to
.nm/in the run directory (useconfig_diror other explicit paths; writable directory required) - LLM endpoint (only needed for semantic extraction): Environment variables
OPENAI_BASE_URL/OPENAI_API_KEY→llm_config.json→ fallback to a local compatible gateway; default heuristic extraction requires no dependencies
Verification
python test_selfverify.py # 22 assertions, covering 4 memory closure scenarios
Notes
- Permissions default to
ASK; without a callback, execution is denied—the integrating party must provide anask_callbackor change toAUTO - Tools must be explicitly
registered; unregistered tools will be rejected - Does not include any network service entry point; it is a pure library