context.tsā¢5.25 kB
import { ProjectContext } from '../agents/types';
export class ContextManager {
private currentProject: ProjectContext | null = null;
private projects = new Map<string, ProjectContext>();
setCurrentProject(project: ProjectContext): void {
this.currentProject = project;
this.projects.set(project.name, project);
console.error(`š Project context set to: ${project.name}`);
}
getCurrentProject(): ProjectContext | null {
return this.currentProject;
}
switchProject(projectName: string): ProjectContext | null {
const project = this.projects.get(projectName);
if (project) {
this.currentProject = project;
console.error(`š Switched to project: ${projectName}`);
return project;
}
console.error(`ā ļø Project '${projectName}' not found`);
return null;
}
getAllProjects(): ProjectContext[] {
return Array.from(this.projects.values());
}
updateProjectContext(projectName: string, updates: Partial<ProjectContext>): boolean {
const project = this.projects.get(projectName);
if (project) {
Object.assign(project, updates);
if (this.currentProject?.name === projectName) {
this.currentProject = project;
}
console.error(`š Updated project context for: ${projectName}`);
return true;
}
return false;
}
removeProject(projectName: string): boolean {
if (this.projects.delete(projectName)) {
if (this.currentProject?.name === projectName) {
this.currentProject = null;
}
console.error(`šļø Removed project: ${projectName}`);
return true;
}
return false;
}
// Create a default project context from minimal information
createProjectContext(
name: string,
type: string,
techStack: Record<string, string> = {},
currentPhase = 'development',
goals: string[] = []
): ProjectContext {
return {
name,
type,
techStack,
standards: {},
currentPhase,
goals
};
}
// Extract project context from common project files
async inferProjectContextFromFiles(projectPath: string): Promise<ProjectContext | null> {
try {
const fs = require('fs-extra');
const path = require('path');
let name = path.basename(projectPath);
let type = 'unknown';
const techStack: Record<string, string> = {};
const standards: Record<string, any> = {};
// Check for package.json (Node.js project)
const packageJsonPath = path.join(projectPath, 'package.json');
if (await fs.pathExists(packageJsonPath)) {
const packageJson = await fs.readJSON(packageJsonPath);
name = packageJson.name || name;
type = 'web-application';
// Detect tech stack from dependencies
const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
if (deps.react) techStack.frontend = 'React';
if (deps.vue) techStack.frontend = 'Vue';
if (deps.angular) techStack.frontend = 'Angular';
if (deps.express) techStack.backend = 'Express';
if (deps.next) techStack.framework = 'Next.js';
if (deps.typescript) techStack.language = 'TypeScript';
// Extract standards from package.json scripts
if (packageJson.scripts) {
if (packageJson.scripts.lint) standards.linting = true;
if (packageJson.scripts.test) standards.testing = true;
if (packageJson.scripts.build) standards.build = true;
}
}
// Check for Python projects
const requirementsPath = path.join(projectPath, 'requirements.txt');
const pyprojectPath = path.join(projectPath, 'pyproject.toml');
if (await fs.pathExists(requirementsPath) || await fs.pathExists(pyprojectPath)) {
type = 'python-application';
techStack.language = 'Python';
}
// Check for Cargo.toml (Rust project)
const cargoPath = path.join(projectPath, 'Cargo.toml');
if (await fs.pathExists(cargoPath)) {
type = 'rust-application';
techStack.language = 'Rust';
}
// Check for Go projects
const goModPath = path.join(projectPath, 'go.mod');
if (await fs.pathExists(goModPath)) {
type = 'go-application';
techStack.language = 'Go';
}
return {
name,
type,
techStack,
standards,
currentPhase: 'development',
goals: []
};
} catch (error) {
console.error('Failed to infer project context:', error);
return null;
}
}
getProjectSummary(): string {
if (!this.currentProject) {
return 'No active project context';
}
const { name, type, techStack, currentPhase, goals } = this.currentProject;
let summary = `**Current Project**: ${name}\\n`;
summary += `**Type**: ${type}\\n`;
summary += `**Phase**: ${currentPhase}\\n`;
if (Object.keys(techStack).length > 0) {
summary += `**Tech Stack**:\\n`;
Object.entries(techStack).forEach(([key, value]) => {
summary += ` - ${key}: ${value}\\n`;
});
}
if (goals.length > 0) {
summary += `**Goals**: ${goals.join(', ')}\\n`;
}
return summary;
}
}