π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
Page Agent - Architecture & Workflow
This document explains how the Page Agent works, including context tracking, injection, and document manipulation.
Overview
The Page Agent is a specialized AI agent that helps users create, read, update, and edit documents in the PageEditor. It has access to the current page's content and structure through context injection.
Architecture Components
1. Page Agent Definition (index.ts)
- Model: Claude Sonnet 4.5
- Plugin:
lobe-page-agent(Document manipulation tools) - System Role: Document editing assistant
2. Document Tools (src/tools/document/)
- Identifier:
lobe-page-agent - Type: Built-in tool
- Capabilities:
initPage- Initialize document from MarkdowneditTitle- Edit document title- CRUD operations (createNode, updateNode, deleteNode, moveNode)
- Structure operations (wrapNodes, mergeNodes, splitNode)
- Table, image, and list operations
- Snapshot management
3. Context Injection System (packages/context-engine/)
- Provider:
PageEditorContextInjector - Purpose: Inject current page context into conversation
- Location in Pipeline: Position 5 (after Agent Builder, before Tool System Role)
Data Flow - Complete Workflow
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Page Editor β
β - User edits document with Lexical editor β
β - Document saved in database as JSON (editorData) β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Store Updater (StoreUpdater.tsx) β
β - Watches currentDocId changes β
β - Calls: useChatStore.internal_updateActivePageId() β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Chat Store (store/chat/slices/message/) β
β - Stores activePageId globally β
β - Available to all services via getChatStoreState() β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β User sends message to Page Agent β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Chat Service (services/chat/index.ts) β
β 1. Gets activePageId from chat store β
β 2. Fetches document from file store β
β 3. Builds pageEditorContext: β
β - document metadata (id, title, etc.) β
β - content (markdown text) β
β - editorData (Lexical JSON structure) β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Context Engineering (mecha/contextEngineering.ts) β
β - Receives pageEditorContext β
β - Passes to context pipeline β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PageEditorContextInjector (context-engine/providers/) β
β 1. Converts Lexical JSON β XML with node IDs β
β 2. Formats as structured context β
β 3. Injects before first user message β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Model receives: β
β <current_page_context> β
β <document_metadata> β
β <id>docs_abc123</id> β
β <title>My Document</title> β
β </document_metadata> β
β <document_structure> β
β <root> β
β <h1 id="node_1"> β
β <span id="node_2">Heading Text</span> β
β </h1> β
β <p id="node_3"> β
β <span id="node_4">Paragraph content</span> β
β </p> β
β </root> β
β </document_structure> β
β </current_page_context> β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Key Implementation Details
1. Active Page Tracking
Location: src/features/PageEditor/StoreUpdater.tsx:145-157
useEffect(() => {
const activeId = currentDocId || pageId;
updateActivePageId(activeId);
return () => {
updateActivePageId(undefined); // Cleanup on unmount
};
}, [currentDocId, pageId, updateActivePageId]);
State: src/store/chat/slices/message/initialState.ts:17
interface ChatMessageState {
activePageId?: string; // Current page being edited
// ... other fields
}
2. Context Building
Location: src/services/chat/index.ts:217-259
const isPageAgentEnabled = enabledToolIds.includes(PAGE_AGENT_TOOL_ID);
if (isPageAgentEnabled) {
const activePageId = getChatStoreState().activePageId;
if (activePageId) {
const document = documentSelectors.getDocumentById(activePageId)(fileState);
pageEditorContext = {
document: {
id: document.id,
title: document.title,
fileType: document.fileType,
totalCharCount: document.totalCharCount,
totalLineCount: document.totalLineCount,
},
content: document.content || undefined,
editorData: document.editorData || undefined,
};
}
}
3. Lexical to XML Conversion
Location: packages/context-engine/src/providers/PageEditorContextInjector.ts:32-116
Input (Lexical JSON):
{
"root": {
"type": "root",
"children": [
{
"type": "heading",
"tag": "h1",
"children": [{ "type": "text", "text": "Hello World" }]
}
]
}
}
Output (XML with IDs):
<root>
<h1 id="node_1">
<span id="node_2">Hello World</span>
</h1>
</root>
Supported Node Types:
textβ<span id="...">headingβ<h1-h6 id="...">paragraphβ<p id="...">listβ<ul/ol id="...">listitemβ<li id="...">quoteβ<blockquote id="...">codeβ<pre id="..."><code>linkβ<a id="..." href="...">imageβ<img id="..." src="..." alt="...">
4. Context Injection
Location: packages/context-engine/src/providers/PageEditorContextInjector.ts:217-237
Process:
- Check if Page Agent is enabled
- Check if page context exists
- Convert Lexical JSON to XML
- Format as
<current_page_context> - Insert as user message before first user message
- Mark with metadata:
{ injectType: 'page-editor-context', systemInjection: true }
Document Tool Usage
The model can use these tools to manipulate the document:
Example: Edit Title
editTitle({ title: 'New Title' });
Example: Initialize Page
initPage({
markdown: '# Heading\n\nParagraph content',
});
Future: Node Manipulation
Currently disabled for MVP, but will support:
updateNode({ nodeId: "node_3", content: "New text" })deleteNode({ nodeId: "node_5" })createNode({ type: "p", content: "New paragraph", position: "after", referenceNodeId: "node_3" })
Debugging
Console Logs to Check
Page ID tracking:
[StoreUpdater] Updating activePageId in chat store: docs_abc123 [ChatStore] Updating activePageId: docs_abc123Context building:
[ChatService] isPageAgentEnabled: true [ChatService] activePageId: docs_abc123 [ChatService] Built page editor context: { documentId: '...', hasEditorData: true }XML conversion:
[PageEditorContextInjector] Converted Lexical to XML, node count: 15 [PageEditorContextInjector] XML preview: <root>...Injection:
[PageEditorContextInjector] Page Editor context injected successfully [PageEditorContextInjector] Total messages after injection: 2
Known Limitations
- MVP Features: Only
initPageandeditTitleare currently enabled - Node ID Generation: IDs are generated per-request (not persistent)
- No Direct Editing: Tools can't directly modify editor state (would need integration)
- Temp Documents: Work in progress, IDs may change on save
Future Enhancements
- Enable Full CRUD: Activate all document manipulation tools
- Persistent Node IDs: Store IDs in editorData for consistency
- Real-time Updates: Sync tool changes back to editor
- Collaborative Editing: Multiple users with conflict resolution
- Version History: Full snapshot system integration
Related Files
- Agent Definition:
packages/builtin-agents/src/agents/page-agent/ - Document Tools:
src/tools/document/ - Context Injector:
packages/context-engine/src/providers/PageEditorContextInjector.ts - Chat Service:
src/services/chat/index.ts - Store Updater:
src/features/PageEditor/StoreUpdater.tsx - Chat Store:
src/store/chat/slices/message/ - File Store:
src/store/file/slices/document/