AI-assisted development tools for StrataWP
npm install @stratawp/aiAI-assisted development tools for StrataWP WordPress themes. Generate blocks, components, and patterns using OpenAI GPT-4 or Anthropic Claude.
- AI Code Generation: Generate Gutenberg blocks, theme components, and block patterns from natural language descriptions
- Code Review: Analyze code for security vulnerabilities, performance issues, and best practices
- Documentation Generation: Automatically create comprehensive documentation in Markdown, PHPDoc, or JSDoc formats
- Multiple AI Providers: Support for both OpenAI (GPT-4) and Anthropic (Claude 3.5 Sonnet)
- Flexible Configuration: Configure via .env files or centralized config file
``bash`
pnpm add @stratawp/ai
Run the interactive setup wizard to configure your AI provider:
`bash`
stratawp ai:setup
The wizard will guide you through:
1. Choosing your AI provider (OpenAI or Anthropic)
2. Entering your API key
3. Selecting a model (optional)
4. Configuring advanced settings (temperature, max tokens)
5. Choosing where to store your configuration
#### Option 1: Environment Variables (.env)
Create a .env file in your theme directory:
`envAI Provider (openai or anthropic)
STRATAWP_AI_PROVIDER=anthropic
#### Option 2: Config File
Configuration is stored at
~/.stratawp/ai-config.json:`json
{
"provider": "anthropic",
"apiKey": "your-api-key-here",
"model": "claude-3-5-sonnet-20241022",
"temperature": 0.7,
"maxTokens": 2000
}
`Usage
$3
Generate Gutenberg blocks, theme components, or block patterns from natural language descriptions.
`bash
Generate a Gutenberg block
stratawp ai:generate blockGenerate a theme component
stratawp ai:generate componentGenerate a block pattern
stratawp ai:generate patternSpecify output path
stratawp ai:generate block -o src/blocks/hero/generated.md
`Example: Generate a Hero Block
`bash
$ stratawp ai:generate block🤖 AI Code Generator
? Describe the block you want to create: A hero section with a large heading, subheading, call-to-action button, and background image support
? Block name: hero-section
Generating code with AI... âś”
âś“ Saved to: src/blocks/hero-section/generated.md
`The AI will generate:
-
block.json with block metadata
- index.tsx with the block registration and edit function
- save.tsx with the save function$3
Analyze code for security vulnerabilities, performance issues, and WordPress best practices.
`bash
Review a file (all focus areas)
stratawp ai:review inc/Components/CustomPostTypes.phpFocus on security
stratawp ai:review src/blocks/contact-form/index.tsx -f securityFocus on performance
stratawp ai:review functions.php -f performanceFocus on best practices
stratawp ai:review src/utils/api.ts -f best-practices
`Example: Security Review
`bash
$ stratawp ai:review inc/Components/ContactForm.php -f security🔍 AI Code Review
Analyzing code... âś”
============================================================
Code Review Results
============================================================
Security Issues
$3
- Line 42: Unescaped user input in output
Recommendation: Use esc_html() or esc_attr() to escape user data$3
- Line 67: Missing nonce verification in form submission
Recommendation: Add wp_verify_nonce() checkPerformance Concerns
...
`$3
Create comprehensive documentation for your code in multiple formats.
`bash
Generate documentation (auto-detect format from file extension)
stratawp ai:document inc/Components/Menus.phpSpecify format explicitly
stratawp ai:document src/blocks/team/index.tsx -f jsdocSave to file
stratawp ai:document inc/Components/Menus.php -o docs/components/menus.md
`Supported Formats:
- Markdown: Comprehensive documentation with sections
- PHPDoc: WordPress-compatible PHP documentation
- JSDoc: TypeScript/JavaScript documentation
Example: Generate PHPDoc
`bash
$ stratawp ai:document inc/Components/CustomPostTypes.php -f phpdoc📚 AI Documentation Generator
Generating documentation... âś”
============================================================
Generated Documentation
============================================================
/**
* Custom Post Types Component
*
* Registers custom post types for the theme.
*
* @package StrataWP
* @since 1.0.0
*/
class CustomPostTypes implements ComponentInterface {
/**
* Initialize the component
*
* @return void
*/
public function init(): void { ... }
}
`AI Providers
$3
Get API Key: https://platform.openai.com/api-keys
Supported Models:
-
gpt-4-turbo-preview (default)
- gpt-4
- gpt-3.5-turboConfiguration:
`env
STRATAWP_AI_PROVIDER=openai
STRATAWP_AI_API_KEY=sk-...
STRATAWP_AI_MODEL=gpt-4-turbo-preview
`$3
Get API Key: https://console.anthropic.com/
Supported Models:
-
claude-3-5-sonnet-20241022 (default, recommended)
- claude-3-opus-20240229
- claude-3-sonnet-20240229Configuration:
`env
STRATAWP_AI_PROVIDER=anthropic
STRATAWP_AI_API_KEY=sk-ant-...
STRATAWP_AI_MODEL=claude-3-5-sonnet-20241022
`Programmatic Usage
You can use the AI package programmatically in your Node.js scripts:
`typescript
import { OpenAIProvider, AnthropicProvider, ConfigManager } from '@stratawp/ai'// Load configuration
const configManager = new ConfigManager()
const config = await configManager.load()
// Initialize provider
const provider = config.provider === 'openai'
? new OpenAIProvider(config)
: new AnthropicProvider(config)
// Generate completion
const response = await provider.complete([
{
role: 'system',
content: 'You are an expert WordPress developer.'
},
{
role: 'user',
content: 'Generate a custom post type for portfolio items'
}
])
console.log(response.content)
// Stream response
await provider.stream(
[
{ role: 'user', content: 'Explain WordPress hooks' }
],
{
onToken: (token) => process.stdout.write(token),
onComplete: (text) => console.log('\n\nComplete!'),
onError: (error) => console.error('Error:', error)
}
)
`Advanced Configuration
$3
Controls randomness in responses (0.0 - 1.0):
- 0.0-0.3: Focused, deterministic (good for code generation)
- 0.4-0.7: Balanced (default)
- 0.8-1.0: Creative, varied
`env
STRATAWP_AI_TEMPERATURE=0.5
`$3
Maximum length of AI responses:
- 500-1000: Short responses (code snippets)
- 2000: Default (balanced)
- 4000-8000: Long responses (comprehensive documentation)
`env
STRATAWP_AI_MAX_TOKENS=3000
`Best Practices
1. Start with Setup: Run
stratawp ai:setup before using AI commands
2. Be Specific: Provide detailed descriptions when generating code
3. Review Output: Always review AI-generated code before using in production
4. Security First: Use ai:review with -f security on user-facing code
5. Iterate: AI suggestions are starting points—refine as needed
6. Version Control: Commit AI-generated code separately for easy reviewTroubleshooting
$3
Run
stratawp ai:setup to configure your AI provider.$3
Verify your API key is correct:
- OpenAI: https://platform.openai.com/api-keys
- Anthropic: https://console.anthropic.com/
$3
AI providers have rate limits. If you hit limits:
- Wait a few minutes before retrying
- Reduce
maxTokens for shorter responses
- Upgrade your API plan if needed$3
Check your internet connection and verify the AI provider's status:
- OpenAI Status: https://status.openai.com/
- Anthropic Status: https://status.anthropic.com/
Examples
$3
`bash
$ stratawp ai:generate block? Describe the block: A contact form with name, email, message fields, and spam protection. Include client-side validation and success/error states.
? Block name: contact-form
âś“ Generated complete block with validation and state management!
`$3
`bash
$ stratawp ai:review inc/Components/UserAuth.php -f securityAI will analyze for:
- SQL injection vulnerabilities
- XSS vulnerabilities
- CSRF protection
- Input sanitization
- Nonce verification
- Capability checks
`$3
`bash
$ stratawp ai:document src/api/products.ts -f markdown -o docs/api/products.mdGenerates comprehensive docs with:
- Function signatures
- Parameter descriptions
- Return types
- Usage examples
- Error handling
``Contributions are welcome! Please see the main StrataWP repository for contribution guidelines.
GPL-3.0-or-later
- Issues: https://github.com/JonImmsWordpressDev/StrataWP/issues
- Discussions: https://github.com/JonImmsWordpressDev/StrataWP/discussions
- Documentation: https://github.com/JonImmsWordpressDev/StrataWP#readme
Powered by:
- OpenAI GPT-4
- Anthropic Claude
---
Note: AI-generated code is a starting point. Always review, test, and refine AI suggestions before deploying to production.