Managing conversation context and message history in the qckfx Agent SDK
The ContextWindow class is a core component of the qckfx Agent SDK that manages conversation context, message history, and file access tracking. It provides a structured way to handle the conversation flow between users and agents while maintaining important metadata.
The ContextWindow provides several methods for adding different types of messages:
Copy
// Add a simple user messageconst messageId = context.pushUser('What files are in this directory?');// The message ID can be used for rollback operationsconsole.log('Message ID:', messageId);
The ContextWindow automatically tracks which files have been accessed during the conversation:
Copy
// Record that a file was readcontext.recordFileRead('/path/to/file.ts');context.recordFileRead('package.json');// Check if a file has been readif (context.hasReadFile('package.json')) { console.log('Package.json was already read in this context');}// Get all files that have been readconst readFiles = context.getReadFiles();console.log('Files read:', readFiles);
Checkpoints enable rollback functionality by tracking the state at specific points:
Copy
// Set the current checkpoint IDcontext.setLastCheckpointId('checkpoint_abc123');// Get the current checkpoint IDconst checkpointId = context.getLastCheckpointId();// All new messages will inherit this checkpoint IDconst messageId = context.pushUser('New message');
// Get raw Anthropic messages (for API calls)const messages = context.getMessages();// Get full conversation messages with metadataconst conversationMessages = context.getConversationMessages();// Get the last messageconst lastMessage = context.peek();if (lastMessage) { console.log('Last message:', lastMessage.anthropic.content);}// Get conversation lengthconst length = context.getLength();console.log(`Conversation has ${length} messages`);
Use the ContextWindow’s file tracking features to avoid redundant file reads and optimize agent performance.
Message validation only runs in development and test environments. Ensure your production code follows proper message ordering to avoid runtime issues.