Skip to content

Commit

Permalink
Refactor promptTypes.js to add type definitions and improve code orga…
Browse files Browse the repository at this point in the history
…nization
  • Loading branch information
TMHSDigital committed Dec 23, 2024
1 parent c274c56 commit ded32ec
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 29 deletions.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@ Transform your AI prompts with best practices and smart enhancements.
- Type-specific enhancements
- Real-time processing and feedback
- Comprehensive improvement suggestions
- Temperature recommendations
- Chain-of-thought prompting
- Version control support

- 🎨 **Beautiful Interface**
- Intuitive medium and type selection
- Dark/Light mode with system preference detection
- Responsive design for all devices
- Clean, modern UI with smooth animations
- Enhanced visual feedback
- Improved accessibility
- Tooltips for better guidance

- 💾 **Prompt Management**
- Save your favorite prompts
- View and manage prompt history
- Quick load of previous prompts
- Categorized prompt organization
- Version tracking
- Export/Import capabilities

- 🔗 **Easy Sharing**
- Share enhanced prompts across platforms
Expand All @@ -47,6 +55,47 @@ Transform your AI prompts with best practices and smart enhancements.
5. Click "Enhance Prompt" to see the improved version
6. Use the share, copy, or save buttons to manage your prompt

## Prompt Types

### Text Medium
- **General**: General purpose text generation
- **Completion**: Continue or complete existing text
- **Chat**: Conversational interactions
- **Code**: Generate or modify code

### Image Medium
- **Generation**: Create new images
- **Editing**: Modify existing images
- **Variation**: Create variations of images

## Enhancement Factors

### Text Factors
- Objective
- Tone
- Format
- Constraints
- Examples
- Creativity
- Continuity
- Memory
- Documentation
- Tests

### Image Factors
- Subject
- Style
- Composition
- Lighting
- Color
- Mood
- Detail
- Perspective
- Modification
- Strength
- Preservation
- Blend

## Local Development

1. Clone the repository:
Expand Down Expand Up @@ -88,6 +137,46 @@ prompt-engine/
└── footer.html
```

## Technical Details

- Pure JavaScript (ES6+)
- No external dependencies
- Local storage for data persistence
- Modular design for easy maintenance
- Responsive CSS with Flexbox/Grid
- Progressive enhancement approach
- Semantic HTML structure
- Accessibility features
- Cross-browser compatibility

## Best Practices

### Code Organization
- Modular file structure
- Clear separation of concerns
- Consistent naming conventions
- Comprehensive error handling
- JSDoc documentation

### Performance
- Minimal DOM manipulation
- Efficient event handling
- Optimized animations
- Lazy loading where appropriate

### Accessibility
- ARIA labels and roles
- Keyboard navigation
- Screen reader support
- Color contrast compliance

### User Experience
- Intuitive navigation
- Clear feedback
- Responsive design
- Progressive enhancement
- Tooltips and guidance

## Contributing

1. Fork the repository
Expand Down
121 changes: 121 additions & 0 deletions js/features/promptEnhancer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { mediumTypes, getFactors, getBestPractices } from './promptTypes.js';
import { PromptValidator } from './promptValidator.js';

/**
* Enhanced prompt generation system that implements best practices and smart enhancements.
* Supports both text and image prompts with medium-specific optimizations.
*/
export class PromptEnhancer {
/**
* Creates a new instance of the PromptEnhancer.
* Initializes the validator and improvements tracking.
*/
constructor() {
this.validator = new PromptValidator();
this.improvements = [];
}

/**
* Enhances a prompt based on its medium and type.
* @param {string} prompt - The original prompt to enhance
* @param {string} medium - The medium type ('text' or 'image')
* @param {string} type - The specific prompt type within the medium
* @param {Object} options - Additional options for enhancement
* @param {boolean} [options.versionControl=false] - Whether to add version control information
* @param {Object} [options.factors={}] - Additional factors to consider
* @returns {Object} Enhanced prompt data
* @returns {string} .enhancedPrompt - The enhanced prompt text
* @returns {string[]} .improvements - List of improvements made
* @returns {boolean} .wasModified - Whether the prompt was modified
* @returns {Object} .settings - Additional settings like temperature
*/
enhance(prompt, medium, type, options = {}) {
try {
this.improvements = [];
Expand Down Expand Up @@ -79,12 +101,26 @@ export class PromptEnhancer {
}
}

/**
* Sanitizes the input prompt by normalizing whitespace and line endings.
* @param {string} prompt - The prompt to sanitize
* @returns {string} Sanitized prompt
* @private
*/
sanitizeInput(prompt) {
return prompt.trim()
.replace(/\s+/g, ' ') // Replace multiple spaces with single space
.replace(/[\r\n]+/g, '\n'); // Normalize line endings
}

/**
* Applies chain-of-thought prompting by breaking down complex tasks.
* @param {string} prompt - The original prompt
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @returns {string} Enhanced prompt with chain-of-thought structure
* @private
*/
applyChainOfThought(prompt, medium, type) {
const subProblems = this.identifySubProblems(prompt, medium, type);
if (subProblems.length > 1) {
Expand All @@ -99,6 +135,14 @@ export class PromptEnhancer {
return prompt;
}

/**
* Identifies sub-problems in a complex prompt.
* @param {string} prompt - The prompt to analyze
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @returns {string[]} Array of identified sub-problems
* @private
*/
identifySubProblems(prompt, medium, type) {
// Split complex prompts into logical sub-tasks
const problems = [];
Expand All @@ -115,11 +159,26 @@ export class PromptEnhancer {
return problems.length ? problems : [prompt];
}

/**
* Checks if text contains action verbs.
* @param {string} text - The text to check
* @returns {boolean} Whether the text contains action verbs
* @private
*/
containsActionVerb(text) {
const actionVerbs = /(create|make|generate|write|develop|implement|design|modify|update|enhance|improve|analyze)/i;
return actionVerbs.test(text);
}

/**
* Applies enhancement factors to the prompt.
* @param {string} prompt - The prompt to enhance
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @param {Object} options - Enhancement options
* @returns {string} Enhanced prompt with applied factors
* @private
*/
applyFactors(prompt, medium, type, options) {
let enhanced = prompt;
const factors = getFactors(medium, type);
Expand All @@ -137,6 +196,15 @@ export class PromptEnhancer {
return enhanced;
}

/**
* Adds a specific enhancement factor to the prompt.
* @param {string} prompt - The prompt to enhance
* @param {string} factor - The factor to add
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @returns {string} Enhanced prompt with added factor
* @private
*/
addFactor(prompt, factor, medium, type) {
const factorMap = {
objective: (p) => `I want you to ${p}`,
Expand All @@ -160,6 +228,14 @@ export class PromptEnhancer {
return factorMap[factor] ? factorMap[factor](prompt, medium) : prompt;
}

/**
* Adds relevant examples to the prompt based on medium and type.
* @param {string} prompt - The prompt to enhance
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @returns {string} Enhanced prompt with examples
* @private
*/
addExamples(prompt, medium, type) {
// Add relevant examples based on medium and type
const examples = this.getExamplesForType(medium, type);
Expand All @@ -171,6 +247,13 @@ export class PromptEnhancer {
return prompt;
}

/**
* Gets example templates for a specific medium and type.
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @returns {string[]} Array of example templates
* @private
*/
getExamplesForType(medium, type) {
const exampleMap = {
text: {
Expand Down Expand Up @@ -220,6 +303,15 @@ export class PromptEnhancer {
return exampleMap[medium]?.[type] || [];
}

/**
* Applies medium-specific enhancements to the prompt.
* @param {string} prompt - The prompt to enhance
* @param {string} medium - The medium type
* @param {string} type - The prompt type
* @param {Object} practices - Best practices configuration
* @returns {string} Enhanced prompt with medium-specific improvements
* @private
*/
applyMediumSpecificEnhancements(prompt, medium, type, practices) {
let enhanced = prompt;

Expand All @@ -235,6 +327,14 @@ export class PromptEnhancer {
return enhanced;
}

/**
* Enhances image-specific prompts with quality and composition details.
* @param {string} prompt - The prompt to enhance
* @param {string} type - The image prompt type
* @param {Object} practices - Best practices configuration
* @returns {string} Enhanced image prompt
* @private
*/
enhanceImagePrompt(prompt, type, practices) {
let enhanced = prompt;

Expand All @@ -252,6 +352,14 @@ export class PromptEnhancer {
return enhanced;
}

/**
* Enhances text-specific prompts with appropriate formatting and documentation.
* @param {string} prompt - The prompt to enhance
* @param {string} type - The text prompt type
* @param {Object} practices - Best practices configuration
* @returns {string} Enhanced text prompt
* @private
*/
enhanceTextPrompt(prompt, type, practices) {
let enhanced = prompt;

Expand All @@ -268,6 +376,12 @@ export class PromptEnhancer {
return enhanced;
}

/**
* Adds weighted components to image prompts for better control.
* @param {string} prompt - The prompt to enhance
* @returns {string} Enhanced prompt with weighted components
* @private
*/
addWeightedPrompts(prompt) {
// Add weight to important elements using (: :) syntax
const elements = prompt.split(',').map(p => p.trim());
Expand All @@ -279,6 +393,13 @@ export class PromptEnhancer {
}).join(', ');
}

/**
* Truncates a prompt if it exceeds the maximum length.
* @param {string} prompt - The prompt to check
* @param {number} maxLength - Maximum allowed length
* @returns {string} Truncated prompt if necessary
* @private
*/
truncateIfNeeded(prompt, maxLength) {
if (prompt.length > maxLength) {
this.improvements.push('Truncated to prevent excessive length');
Expand Down
Loading

0 comments on commit ded32ec

Please sign in to comment.