vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
65 lines (64 loc) • 2.01 kB
JavaScript
import logger from '../../../logger.js';
export class ContextTracker {
contextStack = [];
enterContext(type, node, name) {
try {
const parent = this.getCurrentContext();
this.contextStack.push({ type, node, name, parent });
logger.debug(`Entered ${type} context${name ? ` '${name}'` : ''}`);
}
catch (error) {
logger.warn({ err: error }, `Error entering ${type} context`);
}
}
exitContext() {
try {
const context = this.contextStack.pop();
if (context) {
logger.debug(`Exited ${context.type} context${context.name ? ` '${context.name}'` : ''}`);
}
}
catch (error) {
logger.warn({ err: error }, 'Error exiting context');
}
}
getCurrentContext() {
return this.contextStack.length > 0 ? this.contextStack[this.contextStack.length - 1] : undefined;
}
getContextHierarchy() {
return this.contextStack.map(ctx => ctx.name || 'anonymous').filter(Boolean);
}
getContextHierarchyString(separator = '.') {
return this.getContextHierarchy().join(separator);
}
isInContext(type) {
return this.contextStack.some(ctx => ctx.type === type);
}
getNearestContext(type) {
for (let i = this.contextStack.length - 1; i >= 0; i--) {
if (this.contextStack[i].type === type) {
return this.contextStack[i];
}
}
return undefined;
}
getContextsOfType(type) {
return this.contextStack.filter(ctx => ctx.type === type);
}
getContextDepth() {
return this.contextStack.length;
}
clear() {
this.contextStack = [];
logger.debug('Context stack cleared');
}
withContext(type, node, name, fn) {
this.enterContext(type, node, name);
try {
return fn();
}
finally {
this.exitContext();
}
}
}