lancedb-memory
#!/usr/bin/env python3
What this skill does
#!/usr/bin/env python3
"""
LanceDB integration for long-term memory management.
Provides vector search and semantic memory capabilities.
"""
import os
import json
import lancedb
from datetime import datetime
from typing import List, Dict, Any, Optional
from pathlib import Path
class LanceMemoryDB:
"""LanceDB wrapper for long-term memory storage and retrieval."""
def __init__(self, db_path: str = "/Users/prerak/clawd/memory/lancedb"):
self.db_path = Path(db_path)
self.db_path.mkdir(parents=True, exist_ok=True)
self.db = lancedb.connect(self.db_path)
# Ensure memory table exists
if "memory" not in self.db.table_names():
self._create_memory_table()
def _create_memory_table(self):
"""Create the memory table with appropriate schema."""
schema = [
{"name": "id", "type": "int", "nullable": False},
{"name": "timestamp", "type": "timestamp", "nullable": False},
{"name": "content", "type": "str", "nullable": False},
{"name": "category", "type": "str", "nullable": True},
{"name": "tags", "type": "str[]", "nullable": True},
{"name": "importance", "type": "int", "nullable": True},
{"name": "metadata", "type": "json", "nullable": True},
]
self.db.create_table("memory", schema=schema)
def add_memory(self, content: str, category: str = "general", tags: List[str] = None,
importance: int = 5, metadata: Dict[str, Any] = None) -> int:
"""Add a new memory entry."""
table = self.db.open_table("memory")
# Get next ID
max_id = table.to_pandas()["id"].max() if len(table) > 0 else 0
new_id = max_id + 1
# Insert new memory
memory_data = {
"id": new_id,
"timestamp": datetime.now(),
"content": content,
"category": category,
"tags": tags or [],
"importance": importance,
"metadata": metadata or {}
}
table.add([memory_data])
return new_id
def search_memories(self, query: str, category: str = None, limit: int = 10) -> List[Dict]:
"""Search memories using vector similarity."""
table = self.db.open_table("memory")
# Build filter
where_clause = []
if category:
where_clause.append(f"category = '{category}'")
filter_expr = " AND ".join(where_clause) if where_clause else None
# Vector search
results = table.vector_search(query).limit(limit).where(filter_expr).to_list()
return results
def get_memories_by_category(self, category: str, limit: int = 50) -> List[Dict]:
"""Get memories by category."""
table = self.db.open_table("memory")
df = table.to_pandas()
filtered = df[df["category"] == category].head(limit)
return filtered.to_dict("records")
def get_memory_by_id(self, memory_id: int) -> Optional[Dict]:
"""Get a specific memory by ID."""
table = self.db.open_table("memory")
df = table.to_pandas()
result = df[df["id"] == memory_id]
return result.to_dict("records")[0] if len(result) > 0 else None
def update_memory(self, memory_id: int, **kwargs) -> bool:
"""Update a memory entry."""
table = self.db.open_table("memory")
valid_fields = ["content", "category", "tags", "importance", "metadata"]
updates = {k: v for k, v in kwargs.items() if k in valid_fields}
if not updates:
return False
# Convert to proper types for LanceDB
if "tags" in updates and isinstance(updates["tags"], list):
updates["tags"] = str(updates["tags"]).replace("'", '"')
table.update(updates, where=f"id = {memory_id}")
return True
def delete_memory(self, memory_id: int) -> bool:
"""Delete a memory entry."""
table = self.db.open_table("memory")
current_count = len(table)
table.delete(f"id = {memory_id}")
return len(table) < current_count
def get_all_categories(self) -> List[str]:
"""Get all unique categories."""
table = self.db.open_table("memory")
df = table.to_pandas()
return df["category"].dropna().unique().tolist()
def get_memory_stats(self) -> Dict[str, Any]:
"""Get statistics about memory storage."""
table = self.db.open_table("memory")
df = table.to_pandas()
return {
"total_memories": len(df),
"categories": len(self.get_all_categories()),
"by_category": df["category"].value_counts().to_dict(),
"date_range": {
"earliest": df["timestamp"].min().isoformat() if len(df) > 0 else None,
"latest": df["timestamp"].max().isoformat() if len(df) > 0 else None
}
}
# Global instance
lancedb_memory = LanceMemoryDB()
def add_memory(content: str, category: str = "general", tags: List[str] = None,
importance: int = 5, metadata: Dict[str, Any] = None) -> int:
"""Add a memory to the LanceDB store."""
return lancedb_memory.add_memory(content, category, tags, importance, metadata)
def search_memories(query: str, category: str = None, limit: int = 10) -> List[Dict]:
"""Search memories using semantic similarity."""
return lancedb_memory.search_memories(query, category, limit)
def get_memories_by_category(category: str, limit: int = 50) -> List[Dict]:
"""Get memories by category."""
return lancedb_memory.get_memories_by_category(category, limit)
def get_memory_stats() -> Dict[str, Any]:
"""Get memory storage statistics."""
return lancedb_memory.get_memory_stats()
# Example usage
if __name__ == "__main__":
# Test the database
print("Testing LanceDB memory integration...")
# Add a test memory
test_id = add_memory(
content="This is a test memory for LanceDB integration",
category="test",
tags=["lancedb", "integration", "test"],
importance=8
)
print(f"Added memory with ID: {test_id}")
# Search for memories
results = search_memories("test memory")
print(f"Search results: {len(results)} memories found")
# Get stats
stats = get_memory_stats()
print(f"Memory stats: {stats}")Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.