Skip to content

API Reference

Comprehensive parameter-level documentation for all public Python APIs.

mempalace.searcher

search(query, palace_path, wing=None, room=None, n_results=5)

CLI-oriented search that prints results to stdout.

ParameterTypeDefaultDescription
querystrSearch query text
palace_pathstrPath to ChromaDB palace directory
wingstrNoneFilter by wing name
roomstrNoneFilter by room name
n_resultsint5Maximum number of results

Raises: SearchError if palace not found or query fails.


search_memories(query, palace_path, wing=None, room=None, n_results=5) → dict

Programmatic search returning a dict. Used by the MCP server.

ParameterTypeDefaultDescription
querystrSearch query text
palace_pathstrPath to ChromaDB palace directory
wingstrNoneFilter by wing name
roomstrNoneFilter by room name
n_resultsint5Maximum number of results

Returns:

python
{
    "query": str,
    "filters": {"wing": str | None, "room": str | None},
    "results": [
        {
            "text": str,           # verbatim drawer content
            "wing": str,           # wing name
            "room": str,           # room name
            "source_file": str,    # original file basename
            "similarity": float,   # 0.0 to 1.0
        }
    ]
}

On error: {"error": str, "hint": str}


mempalace.layers

class Layer0(identity_path=None)

Identity layer (~50 tokens). Reads from ~/.mempalace/identity.txt.

MethodReturnsDescription
render()strIdentity text or default message
token_estimate()intApproximate token count (len(text) // 4)

class Layer1(palace_path=None, wing=None)

Essential story layer (~500–800 tokens). Auto-generated from top drawers.

AttributeTypeDescription
MAX_DRAWERSintMax moments in wake-up (15)
MAX_CHARSintHard cap on L1 text (3200)
MethodReturnsDescription
generate()strCompact L1 text grouped by room

class Layer2(palace_path=None)

On-demand retrieval layer (~200–500 tokens per call).

MethodParametersReturns
retrieve(wing, room, n_results=10)Wing/room filtersFormatted drawer text

class Layer3(palace_path=None)

Deep semantic search layer (unlimited depth).

MethodParametersReturns
search(query, wing=None, room=None, n_results=5)Query + optional filtersFormatted result text
search_raw(query, wing=None, room=None, n_results=5)Query + optional filtersList of result dicts

class MemoryStack(palace_path=None, identity_path=None)

Unified 4-layer interface.

MethodParametersReturnsDescription
wake_up(wing=None)Optional wingstrL0 + L1 context (~170–900 tokens)
recall(wing, room, n_results=10)FiltersstrL2 on-demand retrieval
search(query, wing, room, n_results=5)Query + filtersstrL3 deep search
status()dictAll layer status info

mempalace.knowledge_graph

class KnowledgeGraph(db_path=None)

Default path: ~/.mempalace/knowledge_graph.sqlite3

Write Methods

MethodParametersReturnsDescription
add_entity(name, entity_type='unknown', properties=None)Name, type, props dictstr (entity ID)Add or update entity node
add_triple(subject, predicate, obj, valid_from, valid_to, confidence, source_closet, source_file)See belowstr (triple ID)Add relationship triple
invalidate(subject, predicate, obj, ended=None)Entity names, end dateMark relationship as ended

add_triple parameters:

ParameterTypeDefaultDescription
subjectstrSource entity name
predicatestrRelationship type
objstrTarget entity name
valid_fromstrNoneStart date (YYYY-MM-DD)
valid_tostrNoneEnd date
confidencefloat1.0Confidence score 0.0–1.0
source_closetstrNoneLink to verbatim memory
source_filestrNoneOriginal source file

Query Methods

MethodParametersReturns
query_entity(name, as_of=None, direction='outgoing')Entity name, date filter, directionlist[dict]
query_relationship(predicate, as_of=None)Relationship type, date filterlist[dict]
timeline(entity_name=None)Optional entity filterlist[dict]
stats()dict with entities, triples, predicates
seed_from_entity_facts(entity_facts)Dict of entity facts

query_entity direction values: "outgoing" (entity→?), "incoming" (?→entity), "both"


mempalace.palace_graph

build_graph(col=None, config=None) → (nodes, edges)

Build the palace graph from ChromaDB metadata.

Returns:

  • nodes: dict of {room: {wings: list, halls: list, count: int, dates: list}}
  • edges: list of {room, wing_a, wing_b, hall, count}

traverse(start_room, col=None, config=None, max_hops=2) → list

BFS graph traversal from a room across wings.

ParameterTypeDefaultDescription
start_roomstrRoom slug to start from
max_hopsint2Max connection depth

Returns: [{room, wings, halls, count, hop, connected_via}] (max 50)


find_tunnels(wing_a=None, wing_b=None, col=None, config=None) → list

Find rooms spanning multiple wings.

Returns: [{room, wings, halls, count, recent}] (max 50)


graph_stats(col=None, config=None) → dict

Returns: {total_rooms, tunnel_rooms, total_edges, rooms_per_wing, top_tunnels}


mempalace.dialect

class Dialect(entities=None, skip_names=None)

ParameterTypeDescription
entitiesdict[str, str]Full name → 3-letter code mapping
skip_nameslist[str]Names to skip (fictional characters, etc.)

Class Methods

MethodParametersReturns
from_config(config_path)JSON file pathDialect instance

Instance Methods

MethodParametersReturnsDescription
compress(text, metadata=None)Plain text + optional metadata dictstrAAAK-formatted summary
encode_entity(name)Entity namestr | None3-letter entity code
encode_emotions(emotions)List of emotion stringsstrCompact emotion codes
compress_file(path, output=None)Zettel JSON pathstrCompress zettel file
compress_all(dir, output=None)Zettel directorystrCompress all zettels
save_config(path)Output pathSave entity mappings
compression_stats(original, compressed)Both textsdictCompression ratio stats

Static Methods

MethodParametersReturns
count_tokens(text)Any textint

mempalace.config

class MempalaceConfig()

Reads from ~/.mempalace/config.json and environment variables.

PropertyTypeDefaultDescription
palace_pathstr~/.mempalace/palaceChromaDB storage path
collection_namestrmempalace_drawersChromaDB collection name
MethodDescription
init()Create config directory and default files

Released under the MIT License.