Skip to content

Commit

Permalink
update jsdocs workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
madjin committed Jan 2, 2025
1 parent ed5cd68 commit 080a188
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
4 changes: 2 additions & 2 deletions scripts/jsdoc-automation/src/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Configuration implements Omit<ConfigurationData, 'rootDirectory'> {

public excludedDirectories: string[] = [];
public repository: Repository = {
owner: 'elizaOS',
owner: 'madjin',
name: 'eliza',
pullNumber: undefined
};
Expand Down Expand Up @@ -156,4 +156,4 @@ export class Configuration implements Omit<ConfigurationData, 'rootDirectory'> {
.map(item => item.trim())
.filter(Boolean);
}
}
}
79 changes: 79 additions & 0 deletions scripts/jsdoc-automation/src/JsDocCleaner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Utility class for cleaning and validating JSDoc comments.
*/
export class JsDocCleaner {
/**
* Processes and cleans a JSDoc comment to ensure proper formatting.
*
* @param {string} comment - The JSDoc comment to clean.
* @returns {string} The cleaned JSDoc comment.
*/
public static clean(comment: string): string {
// Remove any empty lines at start/end
let cleaned = comment.trim();

// Ensure comment starts with /**
if (!cleaned.startsWith('/**')) {
cleaned = '/**' + cleaned.replace(/^\/\*+/, '');
}

// Fix common artifacts:
// 1. Multiple closing patterns
cleaned = cleaned.replace(/\*+\s*\*+\s*\/$/, '*/');
cleaned = cleaned.replace(/\*+\s*\/$/, '*/');

// 2. Fix improper closing
if (!cleaned.endsWith('*/')) {
cleaned = cleaned + ' */';
}

// 3. Fix spacing in content
cleaned = cleaned.split('\n').map((line, index) => {
// First line should just have /**
if (index === 0) {
return '/**';
}

// Clean up any extra asterisks in the content
line = line.trim().replace(/^\*+\s*/, ' * ');

// Ensure there's a space after the asterisk if it's a content line
if (line.startsWith(' *') && !line.startsWith(' * ')) {
line = ' * ' + line.substring(2).trimStart();
}

return line;
}).join('\n');

// 4. Fix final spacing
cleaned = cleaned.replace(/\s*\*\//, '\n */');

// Validate basic structure
const isValid =
cleaned.startsWith('/**') &&
cleaned.endsWith('*/') &&
cleaned.includes('\n');

if (!isValid) {
// If validation fails, create a minimal valid structure
cleaned = '/**\n * ' + cleaned.replace(/\/\*+|\*+\//g, '').trim() + '\n */';
}

return cleaned;
}

/**
* Validates if a JSDoc comment has proper structure.
*
* @param {string} comment - The JSDoc comment to validate.
* @returns {boolean} True if the comment has valid structure.
*/
public static isValid(comment: string): boolean {
return (
comment.startsWith('/**') &&
comment.endsWith('*/') &&
comment.includes('\n') &&
!comment.includes('**/')
);
}
}
23 changes: 7 additions & 16 deletions scripts/jsdoc-automation/src/JsDocGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AIService } from './AIService.js';
import { ASTQueueItem } from './types/index.js';
import { JsDocCleaner } from './JsDocCleaner.js';

/**
* A class that generates JSDoc comments for code snippets and classes.
Expand All @@ -9,7 +10,7 @@ export class JsDocGenerator {
* Constructor for a class that takes in an AIService instance.
* @param {AIService} aiService - The AIService instance to be injected into the class.
*/
constructor(public aiService: AIService) { }
constructor(public aiService: AIService) {}

/**
* Generates a comment based on the given ASTQueueItem.
Expand All @@ -20,7 +21,7 @@ export class JsDocGenerator {
public async generateComment(queueItem: ASTQueueItem): Promise<string> {
const prompt = this.buildPrompt(queueItem);
const comment = await this.aiService.generateComment(prompt);
return comment;
return JsDocCleaner.clean(comment);
}

/**
Expand All @@ -29,37 +30,27 @@ export class JsDocGenerator {
* @param {ASTQueueItem} queueItem - The ASTQueueItem to generate the comment for.
* @returns {Promise<string>} The generated comment for the class.
*/
public async generateClassComment(
queueItem: ASTQueueItem,
): Promise<string> {
public async generateClassComment(queueItem: ASTQueueItem): Promise<string> {
const prompt = this.buildClassPrompt(queueItem);
const comment = await this.aiService.generateComment(prompt);
return comment;
return JsDocCleaner.clean(comment);
}

/**
* Builds a prompt with the JSDoc comment for the provided ASTQueueItem code.
*
* @param {ASTQueueItem} queueItem The ASTQueueItem object containing the code to extract the JSDoc comment from.
* @returns {string} The JSDoc comment extracted from the code provided in the ASTQueueItem object.
*/
private buildPrompt(queueItem: ASTQueueItem): string {
return `Generate JSDoc comment for the following code:
\`\`\`typescript
${queueItem.code}
\`\`\`
Only return the JSDoc comment, not the code itself.
Only return the JSDoc comment with proper formatting, not the code itself.
`;
}

private buildClassPrompt(
queueItem: ASTQueueItem,
): string {
return `Generate JSDoc comment for the following Class:
Class name: ${queueItem.code.match(/class (\w+)/)?.[1]}
Only return the JSDoc for the Class itself, not the methods or anything in the class.
Expand All @@ -74,4 +65,4 @@ export class JsDocGenerator {
\`\`\`
`;
}
}
}

0 comments on commit 080a188

Please sign in to comment.