End-user scoped memory with profiles and preferences.
This guide covers building a personal AI assistant that learns from every conversation and retains that knowledge across sessions. The assistant accumulates user preferences, biographical facts, current projects, and behavioural instructions. It injects relevant context automatically at the start of each new session so the experience feels continuous.
What you are building
A single-user Context scoped by user_id.
Memory that spans multiple sessions: identity facts, knowledge, context-specific state, and instructions.
A profile endpoint that produces a ready-to-inject system prompt fragment.
The ability to forget outdated information when the user's situation changes.
Step 1 – Create a session per conversation
Each conversation is a new session. The scope ties the session to the user so all extracted facts are associated with them.
Step 2 – Inject memory at the start of each session
The profile endpoint returns a structured summary of everything Spectron knows about the user – identity attributes, active projects, preferences, and instructions – formatted as a system prompt fragment.
constbase="You are a personal AI assistant. Be concise, direct, and helpful.";
if(profile.summary){ return`${base}\n\n## What you know about this user\n\n${profile.summary}`; }
returnbase; }
A profile summary looks something like this after a few conversations:
The user is Alice Chen, Head of Platform at Acme Corp. They prefer TypeScript over JavaScript. They live in London. They prefer bullet-point responses and dislike filler phrases. They are currently leading a migration from CommonJS to ESM.
This summary is synthesised from the five memory categories: Identity (name, role, location), Knowledge (technical stack), Context (current project), and Instructions (response style preferences).
Step 3 – Run the conversation loop
The simplest integration records each exchange as a pair of turns. Spectron extracts facts asynchronously and they are available for the next session.
# Call your LLM response=your_llm(system=system,user=user_message)
# Record both turns awaitsession.turn(role="user",content=user_message) awaitsession.turn(role="assistant",content=response)
returnresponse
asyncfunctionchat(session:Session,userId:string,userMessage:string):Promise<string>{ // Build context-aware system prompt let system =awaitbuildSystemPrompt(userId);
After the user says "I just moved from Berlin to London", the extraction pipeline updates the location attribute on the user's Person entity and creates a supersession chain:
The old value is retained in the supersession chain for auditability but no longer appears in the active profile.
Step 5 – Using session.chat() instead of session.turn()
If you want Spectron to manage the agent call rather than just recording turns, use the chat() endpoint. Pass an agent_fn callback and Spectron drives the loop – calling your function, recording the result, and running extraction.
constresult=awaitsession.chat({content:"What are my current projects?"}); console.log(result.response);
The chat() call returns the assistant response, the extraction diff, and the updated session state.
Step 6 – Forgetting outdated information
When a user's situation changes significantly – they change jobs, finish a project, move city – you can explicitly forget stale facts rather than waiting for the supersession chain to handle it via new turns.
# Forget a specific attribute awaitmemory.forget( scope={"user": user_id}, entity_type="Person", entity_name="alice", attribute_key="employer", )
# Or forget everything in a category awaitmemory.forget( scope={"user": user_id}, category="context", )
// Forget a specific attribute awaitmemory.forget({ scope:{user:userId}, entityType:"Person", entityName:"alice", attributeKey:"employer", });
// Or forget everything in a category awaitmemory.forget({ scope:{user:userId}, category:"context", });
forget marks the matched attributes as expired (sets valid_until to now) and removes them from future context retrievals. The records are retained for audit purposes.
Memory categories in a personal assistant
Category
Examples
Identity
Name, location, occupation, family
Knowledge
Domain expertise, tools, languages, opinions
Context
Current projects, recent events, open tasks
Instructions
Response style, formatting preferences, topics to avoid
Unknowns
Contradictory or uncertain statements flagged for review
The profile endpoint surfaces all five categories in a single call, prioritising high-confidence, recently validated facts.