This page provides examples of how to use the Arc Memory SDK in your applications. The SDK allows you to programmatically interact with the knowledge graph, build custom tools, and integrate Arc Memory into your workflows.
# Build a complete knowledge graphbuild_result = arc.build()print(f"Built graph with {build_result.node_count} nodes and {build_result.edge_count} edges")# Build incrementallyincremental_result = arc.build_incremental()print(f"Added {incremental_result.new_node_count} new nodes and {incremental_result.new_edge_count} new edges")
# Trace the history of a specific line in a filehistory = arc.trace_file( file_path="src/main.py", line_number=42, max_depth=3)# Process the resultsfor item in history: print(f"Commit: {item.commit_hash}") print(f"Author: {item.author}") print(f"Date: {item.date}") print(f"Message: {item.message}") if item.pr: print(f"PR: #{item.pr.number} - {item.pr.title}") print("---")
# Find nodes related to a specific commitrelated = arc.relate_node( node_type="commit", node_id="abc123", max_depth=2, edge_types=["MODIFIES", "PART_OF", "REFERENCES"])# Process the resultsfor node in related.nodes: print(f"Node: {node.id} ({node.type})") for edge in node.edges: print(f" → {edge.target_node.id} ({edge.type})")
from arc_memory.db import MemoryDB# Open the databasedb = MemoryDB("path/to/memory.db")# Execute a custom queryresults = db.execute_query(""" MATCH (n:Commit)-[:MODIFIES]->(f:File) WHERE f.path = 'src/main.py' RETURN n.hash, n.author, n.message""")# Process the resultsfor row in results: print(f"Commit: {row['n.hash']}") print(f"Author: {row['n.author']}") print(f"Message: {row['n.message']}")
# Run a simulation on current changessimulation = arc.simulate( scenario="network_latency", branch="main", use_memory=True)# Process the resultsprint(f"Simulation ID: {simulation.id}")print(f"Risk Score: {simulation.risk_score}/100")print(f"Affected Services: {', '.join(simulation.affected_services)}")print(f"Analysis: {simulation.analysis}")
import osfrom arc_memory import ArcMemory# Initialize Arc Memory with CI/CD environment variablesarc = ArcMemory( repo_path=os.environ.get("CI_REPO_PATH", "."), github_token=os.environ.get("GITHUB_TOKEN"), output_path=os.environ.get("ARC_DB_PATH", ".arc/memory.db"))# Build the knowledge graph incrementallyarc.build_incremental()# Get the current PR number from environment variablespr_number = os.environ.get("CI_PR_NUMBER")# Analyze the PRif pr_number: pr_analysis = arc.analyze_pr(pr_number) # Post results as a comment on the PR if pr_analysis.risk_score > 50: arc.post_pr_comment( pr_number=pr_number, comment=f"⚠️ **High Risk Changes Detected**\n\n" f"Risk Score: {pr_analysis.risk_score}/100\n\n" f"Affected Services: {', '.join(pr_analysis.affected_services)}\n\n" f"Analysis: {pr_analysis.analysis}" )
from arc_memory import ArcMemoryclass ArcIDEExtension: def __init__(self): self.arc = ArcMemory() def on_file_open(self, file_path): """Called when a file is opened in the IDE.""" # Get file history history = self.arc.trace_file(file_path) return self._format_history_for_ide(history) def on_line_hover(self, file_path, line_number): """Called when hovering over a line in the IDE.""" # Get decision trail for the line trail = self.arc.why_file(file_path, line_number) return self._format_trail_for_ide(trail) def on_pre_commit(self, changed_files): """Called before committing changes.""" # Run simulation on changed files simulation = self.arc.simulate(files=changed_files) return self._format_simulation_for_ide(simulation) def _format_history_for_ide(self, history): # Format history data for IDE display pass def _format_trail_for_ide(self, trail): # Format decision trail for IDE display pass def _format_simulation_for_ide(self, simulation): # Format simulation results for IDE display pass