116 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import * as plugins from '../plugins.js';
 | 
						|
import { IterativeContextBuilder } from './iterative-context-builder.js';
 | 
						|
import { ConfigManager } from './config-manager.js';
 | 
						|
import type { IIterativeContextResult, TaskType } from './types.js';
 | 
						|
 | 
						|
/**
 | 
						|
 * Factory class for creating task-specific context using iterative context building
 | 
						|
 */
 | 
						|
export class TaskContextFactory {
 | 
						|
  private projectDir: string;
 | 
						|
  private configManager: ConfigManager;
 | 
						|
 | 
						|
  /**
 | 
						|
   * Create a new TaskContextFactory
 | 
						|
   * @param projectDirArg The project directory
 | 
						|
   */
 | 
						|
  constructor(projectDirArg: string) {
 | 
						|
    this.projectDir = projectDirArg;
 | 
						|
    this.configManager = ConfigManager.getInstance();
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Initialize the factory
 | 
						|
   */
 | 
						|
  public async initialize(): Promise<void> {
 | 
						|
    await this.configManager.initialize(this.projectDir);
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Create context for README generation
 | 
						|
   */
 | 
						|
  public async createContextForReadme(): Promise<IIterativeContextResult> {
 | 
						|
    const iterativeBuilder = new IterativeContextBuilder(
 | 
						|
      this.projectDir,
 | 
						|
      this.configManager.getIterativeConfig()
 | 
						|
    );
 | 
						|
    await iterativeBuilder.initialize();
 | 
						|
    return await iterativeBuilder.buildContextIteratively('readme');
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Create context for description generation
 | 
						|
   */
 | 
						|
  public async createContextForDescription(): Promise<IIterativeContextResult> {
 | 
						|
    const iterativeBuilder = new IterativeContextBuilder(
 | 
						|
      this.projectDir,
 | 
						|
      this.configManager.getIterativeConfig()
 | 
						|
    );
 | 
						|
    await iterativeBuilder.initialize();
 | 
						|
    return await iterativeBuilder.buildContextIteratively('description');
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Create context for commit message generation
 | 
						|
   * @param gitDiff Optional git diff to include (currently not used in iterative mode)
 | 
						|
   */
 | 
						|
  public async createContextForCommit(gitDiff?: string): Promise<IIterativeContextResult> {
 | 
						|
    const iterativeBuilder = new IterativeContextBuilder(
 | 
						|
      this.projectDir,
 | 
						|
      this.configManager.getIterativeConfig()
 | 
						|
    );
 | 
						|
    await iterativeBuilder.initialize();
 | 
						|
    // Note: git diff could be incorporated into the iterative prompts if needed
 | 
						|
    return await iterativeBuilder.buildContextIteratively('commit');
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Create context for any task type
 | 
						|
   * @param taskType The task type to create context for
 | 
						|
   * @param additionalContent Optional additional content (currently not used)
 | 
						|
   */
 | 
						|
  public async createContextForTask(
 | 
						|
    taskType: TaskType,
 | 
						|
    additionalContent?: string
 | 
						|
  ): Promise<IIterativeContextResult> {
 | 
						|
    switch (taskType) {
 | 
						|
      case 'readme':
 | 
						|
        return this.createContextForReadme();
 | 
						|
      case 'description':
 | 
						|
        return this.createContextForDescription();
 | 
						|
      case 'commit':
 | 
						|
        return this.createContextForCommit(additionalContent);
 | 
						|
      default:
 | 
						|
        // Default to readme for unknown task types
 | 
						|
        return this.createContextForReadme();
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  /**
 | 
						|
   * Get token stats for all task types
 | 
						|
   */
 | 
						|
  public async getTokenStats(): Promise<Record<TaskType, {
 | 
						|
    tokenCount: number;
 | 
						|
    savings: number;
 | 
						|
    includedFiles: number;
 | 
						|
    trimmedFiles: number;
 | 
						|
    excludedFiles: number;
 | 
						|
  }>> {
 | 
						|
    const taskTypes: TaskType[] = ['readme', 'description', 'commit'];
 | 
						|
    const stats: Record<TaskType, any> = {} as any;
 | 
						|
 | 
						|
    for (const taskType of taskTypes) {
 | 
						|
      const result = await this.createContextForTask(taskType);
 | 
						|
      stats[taskType] = {
 | 
						|
        tokenCount: result.tokenCount,
 | 
						|
        savings: result.tokenSavings,
 | 
						|
        includedFiles: result.includedFiles.length,
 | 
						|
        trimmedFiles: result.trimmedFiles.length,
 | 
						|
        excludedFiles: result.excludedFiles.length
 | 
						|
      };
 | 
						|
    }
 | 
						|
 | 
						|
    return stats;
 | 
						|
  }
 | 
						|
}
 |