"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextService = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Service for managing development context
*/
class ContextService {
/**
* Initialize the context service
* @param contextPath - Path to the context file
* @param useKnowledgeGraphReference - Whether to include a reference to the Knowledge Graph
* @param knowledgeGraphReferenceText - Text to include when referencing the Knowledge Graph
*/
constructor(contextPath = path_1.default.join(process.cwd(), 'context.json'), useKnowledgeGraphReference, knowledgeGraphReferenceText) {
this.contextPath = contextPath;
this.developmentContext = null;
this.useKnowledgeGraphReference = false;
this.knowledgeGraphReferenceText = '';
/**
* Default development context to use when file is missing
*/
this.defaultContext = {
name: "Solo Developer Framework",
description: "A structured approach for efficient solo software development",
directives: [
"Focus on component-based architecture",
"Write tests for critical functionality",
"Document code thoroughly",
"Use type safety everywhere possible"
],
workflows: [
{
name: "Feature Development",
description: "Structured process for implementing new features",
steps: [
{ order: 1, description: "Define types and interfaces" },
{ order: 2, description: "Create foundational components" },
{ order: 3, description: "Implement business logic" },
{ order: 4, description: "Add error handling" },
{ order: 5, description: "Write unit tests" }
],
id: ''
}
],
templates: [],
version: ''
};
// Normalize path to ensure consistent behavior across platforms
this.contextPath = path_1.default.resolve(this.contextPath);
console.error(`Context path set to: ${this.contextPath}`);
this.loadContext();
if (useKnowledgeGraphReference !== undefined) {
this.useKnowledgeGraphReference = useKnowledgeGraphReference;
}
if (knowledgeGraphReferenceText) {
this.knowledgeGraphReferenceText = knowledgeGraphReferenceText;
}
}
/**
* Load development context from file
*/
loadContext() {
try {
if (fs_1.default.existsSync(this.contextPath)) {
const fileContent = fs_1.default.readFileSync(this.contextPath, 'utf8');
this.developmentContext = JSON.parse(fileContent);
console.error('Development context loaded successfully from local file');
}
else {
console.error(`Context file not found at ${this.contextPath}. Using default context.`);
this.developmentContext = this.defaultContext;
// Create directory for the context file if it doesn't exist
const contextDir = path_1.default.dirname(this.contextPath);
if (!fs_1.default.existsSync(contextDir)) {
try {
fs_1.default.mkdirSync(contextDir, { recursive: true });
console.error(`Created directory: ${contextDir}`);
}
catch (err) {
console.error(`Failed to create directory for context file: ${err instanceof Error ? err.message : String(err)}`);
}
}
// Save the default context for future use, but don't fail if we can't
try {
fs_1.default.writeFileSync(this.contextPath, JSON.stringify(this.defaultContext, null, 2));
console.error(`Saved default context to ${this.contextPath}`);
}
catch (err) {
console.error(`Could not save default context: ${err instanceof Error ? err.message : String(err)}`);
// Continue with in-memory default context
}
}
}
catch (error) {
console.error('Error loading development context:', error instanceof Error ? error.message : String(error));
// Use default context as fallback
this.developmentContext = this.defaultContext;
console.error('Using default context as fallback');
}
}
/**
* Get the current development context
* @returns The current development context
*/
getContext() {
return this.developmentContext;
}
/**
* Update the development context
* @param newContext - New context data
*/
updateContext(newContext) {
if (!this.developmentContext) {
console.error('Cannot update context: No context loaded');
return;
}
this.developmentContext = { ...this.developmentContext, ...newContext };
this.saveContext();
}
/**
* Save the current context to file
*/
saveContext() {
try {
if (this.developmentContext) {
fs_1.default.writeFileSync(this.contextPath, JSON.stringify(this.developmentContext, null, 2));
}
}
catch (error) {
console.error('Error saving development context:', error instanceof Error ? error.message : String(error));
}
}
/**
* Check if a message contains trigger keywords
* @param message - The message to check
* @param triggerKeywords - Keywords to look for
* @returns Whether the message contains any trigger keywords
*/
shouldInjectContext(message, triggerKeywords) {
return triggerKeywords.some(keyword => message.toLowerCase().includes(keyword.toLowerCase()));
}
/**
* Format the context for injection into a conversation
* @returns Formatted context as a string
*/
async formatContextForInjection() {
if (!this.developmentContext) {
return 'No development context available. Please ensure a valid context file exists.';
}
let formattedContext = `
# ${this.developmentContext.name} Development Framework
${this.developmentContext.description}
## Key Directives
${this.developmentContext.directives.map(directive => `- ${directive}`).join('\n')}
## Development Workflows
`;
this.developmentContext.workflows.forEach(workflow => {
formattedContext += `
### ${workflow.name}
${workflow.description}
Steps:
${workflow.steps.map(step => `${step.order}. ${step.description}`).join('\n')}
`;
});
// Add Knowledge Graph reference if enabled
if (this.useKnowledgeGraphReference && this.knowledgeGraphReferenceText) {
formattedContext += `
## Knowledge Graph Reference
${this.knowledgeGraphReferenceText}
`;
}
return formattedContext;
}
}
exports.ContextService = ContextService;
//# sourceMappingURL=context.service.js.map