153 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			5.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import type { AiDoc } from '../classes.aidoc.js';
 | 
						|
import * as plugins from '../plugins.js';
 | 
						|
import * as paths from '../paths.js';
 | 
						|
import { ProjectContext } from './projectcontext.js';
 | 
						|
import { logger } from '../logging.js';
 | 
						|
 | 
						|
export class Readme {
 | 
						|
  // INSTANCE
 | 
						|
  private aiDocsRef: AiDoc;
 | 
						|
  private projectDir: string;
 | 
						|
 | 
						|
  constructor(aiDocsRef: AiDoc, projectDirArg: string) {
 | 
						|
    this.aiDocsRef = aiDocsRef;
 | 
						|
    this.projectDir = projectDirArg;
 | 
						|
  }
 | 
						|
 | 
						|
  public async build() {
 | 
						|
    let finalReadmeString = ``;
 | 
						|
 | 
						|
    // Use the new TaskContextFactory for optimized context
 | 
						|
    const taskContextFactory = new (await import('../context/index.js')).TaskContextFactory(this.projectDir);
 | 
						|
    await taskContextFactory.initialize();
 | 
						|
    
 | 
						|
    // Generate context specifically for readme task
 | 
						|
    const contextResult = await taskContextFactory.createContextForReadme();
 | 
						|
    const contextString = contextResult.context;
 | 
						|
    
 | 
						|
    // Log token usage statistics
 | 
						|
    console.log(`Token usage - Context: ${contextResult.tokenCount}, Files: ${contextResult.includedFiles.length + contextResult.trimmedFiles.length}, Savings: ${contextResult.tokenSavings}`);
 | 
						|
 | 
						|
    // lets first check legal before introducung any cost
 | 
						|
    const projectContext = new ProjectContext(this.projectDir);
 | 
						|
    const npmExtraJson = JSON.parse(
 | 
						|
      (await projectContext.gatherFiles()).smartfilesNpmextraJSON.contents.toString()
 | 
						|
    );
 | 
						|
    const legalInfo = npmExtraJson?.tsdoc?.legal;
 | 
						|
    if (!legalInfo) {
 | 
						|
      const error = new Error(`No legal information found in npmextra.json`);
 | 
						|
      console.log(error);
 | 
						|
    }
 | 
						|
 | 
						|
    let result = await this.aiDocsRef.openaiInstance.chat({
 | 
						|
      systemMessage: `
 | 
						|
You create markdown readmes for npm projects. You only output the markdown readme.
 | 
						|
 | 
						|
The Readme should follow the following template:
 | 
						|
 | 
						|
# Project Name
 | 
						|
[
 | 
						|
  The name is the module name of package.json
 | 
						|
  The description is in the description field of package.json
 | 
						|
]
 | 
						|
 | 
						|
## Install
 | 
						|
[
 | 
						|
  Write a short text on how to install the project
 | 
						|
]
 | 
						|
 | 
						|
## Usage
 | 
						|
[ 
 | 
						|
  Give code examples here.
 | 
						|
  Construct sensible scenarios for the user.
 | 
						|
  Make sure to show a complete set of features of the module.
 | 
						|
  Don't omit use cases.
 | 
						|
  It does not matter how much time you need.
 | 
						|
  ALWAYS USE ESM SYNTAX AND TYPESCRIPT.
 | 
						|
  DON'T CHICKEN OUT. Write at least 4000 words. More if necessary.
 | 
						|
  If there is already a readme, take the Usage section as base. Remove outdated content, and expand and improve upon the valid parts.
 | 
						|
  Super important: Check for completenes.
 | 
						|
  Don't include any licensing information. This will be added in a later step.
 | 
						|
  Avoid "in conclusions".
 | 
						|
 | 
						|
  Good to know:
 | 
						|
  * npmextra.json contains overall module information.
 | 
						|
  * readme.hints.md provides valuable hints about module ideas.
 | 
						|
]
 | 
						|
            `,
 | 
						|
      messageHistory: [],
 | 
						|
      userMessage: contextString,
 | 
						|
    });
 | 
						|
 | 
						|
    finalReadmeString += result.message + '\n' + legalInfo;
 | 
						|
 | 
						|
    console.log(`\n======================\n`);
 | 
						|
    console.log(result.message);
 | 
						|
    console.log(`\n======================\n`);
 | 
						|
 | 
						|
    const readme = (await projectContext.gatherFiles()).smartfilesReadme;
 | 
						|
    readme.contents = Buffer.from(finalReadmeString);
 | 
						|
    await readme.write();
 | 
						|
 | 
						|
    // lets care about monorepo aspects
 | 
						|
    const tsPublishInstance = new plugins.tspublish.TsPublish();
 | 
						|
    const subModules = await tsPublishInstance.getModuleSubDirs(paths.cwd);
 | 
						|
    logger.log('info', `Found ${Object.keys(subModules).length} sub modules`);
 | 
						|
    for (const subModule of Object.keys(subModules)) {
 | 
						|
      logger.log('info', `Building readme for ${subModule}`);
 | 
						|
      const subModuleContextString = await projectContext.update();
 | 
						|
      let result = await this.aiDocsRef.openaiInstance.chat({
 | 
						|
        systemMessage: `
 | 
						|
  You create markdown readmes for npm projects. You only output the markdown readme.
 | 
						|
 | 
						|
  IMPORTANT: YOU ARE NOW CREATING THE README FOR THE FOLLOWING SUB MODULE: ${subModule} !!!!!!!!!!!
 | 
						|
  The Sub Module will be published with the following data:
 | 
						|
  ${JSON.stringify(plugins.smartfile.fs.toStringSync(plugins.path.join(paths.cwd, subModule, 'tspublish.json')), null, 2)}
 | 
						|
 | 
						|
  
 | 
						|
  The Readme should follow the following template:
 | 
						|
  
 | 
						|
  # Project Name
 | 
						|
  [
 | 
						|
    The name is the module name of package.json
 | 
						|
    The description is in the description field of package.json
 | 
						|
  ]
 | 
						|
  
 | 
						|
  ## Install
 | 
						|
  [
 | 
						|
    Write a short text on how to install the project
 | 
						|
  ]
 | 
						|
  
 | 
						|
  ## Usage
 | 
						|
  [ 
 | 
						|
    Give code examples here.
 | 
						|
    Construct sensible scenarios for the user.
 | 
						|
    Make sure to show a complete set of features of the module.
 | 
						|
    Don't omit use cases.
 | 
						|
    It does not matter how much time you need.
 | 
						|
    ALWAYS USE ESM SYNTAX AND TYPESCRIPT.
 | 
						|
    DON'T CHICKEN OUT. Write at least 4000 words. More if necessary.
 | 
						|
    If there is already a readme, take the Usage section as base. Remove outdated content, and expand and improve upon the valid parts.
 | 
						|
    Super important: Check for completenes.
 | 
						|
    Don't include any licensing information. This will be added in a later step.
 | 
						|
    Avoid "in conclusions".
 | 
						|
  
 | 
						|
    Good to know:
 | 
						|
    * npmextra.json contains overall module information.
 | 
						|
    * readme.hints.md provides valuable hints about module ideas.
 | 
						|
    * Your output lands directly in the readme.md file.
 | 
						|
    * Don't use \`\`\` at the beginning or the end. It'll cause problems. Only use it for codeblocks. You are directly writing markdown. No need to introduce it weirdly.
 | 
						|
  ]
 | 
						|
              `,
 | 
						|
        messageHistory: [],
 | 
						|
        userMessage: subModuleContextString,
 | 
						|
      });
 | 
						|
 | 
						|
      const subModuleReadmeString = result.message + '\n' + legalInfo;
 | 
						|
      await plugins.smartfile.memory.toFs(subModuleReadmeString, plugins.path.join(paths.cwd, subModule, 'readme.md'));
 | 
						|
      logger.log('success', `Built readme for ${subModule}`);
 | 
						|
    }
 | 
						|
    return result.message;
 | 
						|
  }
 | 
						|
}
 |