AI-assisted Nova field that seamlessly improves text with context, while respecting typical Nova workflows
iamgerwin/nova-ai-context-aware-input is a Laravel package for ai-assisted nova field that seamlessly improves text with context, while respecting typical nova workflows.
It currently has 0 GitHub stars and 9 downloads on Packagist (latest version v0.0.2).
Install it with composer require iamgerwin/nova-ai-context-aware-input.
Discover more Laravel packages by iamgerwin
or browse all Laravel packages to compare alternatives.
Last updated
A Laravel Nova field that brings the power of AI to your admin panel, enhancing text input with intelligent, context-aware suggestions. Seamlessly integrates with Nova's workflow while providing real-time text improvements powered by leading AI providers.
Think of it as having a professional editor looking over your shoulder, understanding the context of your entire form, and offering thoughtful improvements—without disrupting your natural writing flow.
TextImproved and SuggestionDiscarded eventsInstall the package via Composer:
composer require iamgerwin/nova-ai-context-aware-input
Publish the configuration file:
php artisan vendor:publish --tag="nova-ai-context-aware-input-config"
Build the Nova assets:
cd vendor/iamgerwin/nova-ai-context-aware-input
npm install && npm run build
Or if you prefer using the root directory:
php artisan nova:publish
Add your API key to your .env file:
# Choose your preferred provider
TEXT_IMPROVE_PROVIDER=openai:gpt-4o-mini
OPENAI_API_KEY=sk-...
# Or use Anthropic
TEXT_IMPROVE_PROVIDER=anthropic:claude-3-5-sonnet-20241022
ANTHROPIC_API_KEY=sk-ant-...
# Or DeepSeek
TEXT_IMPROVE_PROVIDER=deepseek:deepseek-chat
DEEPSEEK_API_KEY=sk-...
# Or Azure OpenAI
TEXT_IMPROVE_PROVIDER=azure_openai:gpt-4
AZURE_OPENAI_API_KEY=...
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4
Replace the standard Textarea field with TextImprove:
use Iamgerwin\NovaAiContextAwareInput\Fields\TextImprove;
public function fields(Request $request)
{
return [
TextImprove::make('Description')
->style('professional')
->trigger('blur'),
];
}
That's it! Your field now has AI-powered text improvement capabilities.
The package comes with extensive configuration options. Here are the key settings:
// config/nova-ai-context-aware-input.php
'provider' => env('TEXT_IMPROVE_PROVIDER', 'openai:gpt-4o-mini'),
'min_length' => 12, // Minimum characters before AI triggers
'history_size' => 3, // Number of suggestions to keep
'trigger' => 'blur', // blur|idle|manual
'style' => 'professional', // Default writing style
'debounce_ms' => 600, // Milliseconds to wait before processing
'rate_limit' => env('TEXT_IMPROVE_RATE_LIMIT', 'user:10,1'),
Format: scope:max_attempts,decay_minutes
user:10,1 - 10 attempts per user per minuteglobal:100,60 - 100 attempts globally per hour'cache' => [
'enabled' => true,
'ttl' => 3600, // Cache for 1 hour
'driver' => env('CACHE_DRIVER', 'file'),
],
'security' => [
'max_input_length' => 5000,
'enable_pii_detection' => true,
'pii_patterns' => [
// Email detection
'/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/',
// Phone numbers
'/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/',
// Credit card numbers
'/\b\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}\b/',
],
],
use Iamgerwin\NovaAiContextAwareInput\Fields\TextImprove;
TextImprove::make('Content')
->style('professional')
->trigger('blur')
->minLength(20);
TextImprove::make('Product Description')
->provider('anthropic:claude-3-5-sonnet-20241022')
->fallback('openai:gpt-4o-mini')
->style('marketing')
->trigger('idle')
->context(['title', 'category', 'price'])
->minLength(30)
->maxTokens(500)
->history(5)
->rateLimit('user:20,1')
->diff(true);
TextImprove::make('Blog Post')
->style('technical') // Built-in styles
->trigger('manual'); // Only improve when user clicks button
Available styles:
professional - Professional and clear business writingformal - Formal and academic tonefriendly - Warm and approachable tonesuccinct - Brief and to the pointmarketing - Persuasive marketing copytechnical - Technical and precise languageLeverage contextual information for smarter suggestions:
TextImprove::make('Email Body')
->context(['subject', 'recipient_name', 'sender_role'])
->style('professional');
'provider' => 'openai:gpt-4o-mini',
// or
'provider' => 'openai:gpt-4',
'provider' => 'openai:gpt-4-turbo',
Environment variables:
OPENAI_API_KEY=sk-...
'provider' => 'anthropic:claude-3-5-sonnet-20241022',
// or
'provider' => 'anthropic:claude-3-opus-20240229',
'provider' => 'anthropic:claude-3-haiku-20240307',
Environment variables:
ANTHROPIC_API_KEY=sk-ant-...
'provider' => 'deepseek:deepseek-chat',
Environment variables:
DEEPSEEK_API_KEY=sk-...
'provider' => 'azure_openai:gpt-4',
Environment variables:
AZURE_OPENAI_API_KEY=...
AZURE_OPENAI_ENDPOINT=https://your-resource.openai.azure.com/
AZURE_OPENAI_DEPLOYMENT_NAME=gpt-4
Context providers enrich AI suggestions by providing relevant information about the form, user, and resource being edited.
Includes data from other fields in the form:
TextImprove::make('Description')
->context(['title', 'category', 'tags']);
This helps the AI understand the broader context. For example, when improving a product description, it knows the product's title and category.
Includes information about the authenticated user:
// Automatically includes:
// - User role
// - Locale/language preference
// - Timezone
Helps tailor suggestions to the user's context and language.
Provides context about the resource being edited:
// Automatically includes:
// - Resource type (e.g., "Article", "Product")
// - Resource ID
// - Common fields (title, name, description, category, type, status)
Enforces content policies and guidelines:
// config/nova-ai-context-aware-input.php
'policy_hints' => [
'Avoid using profanity or offensive language.',
'Maintain professional tone and clarity.',
'Preserve factual accuracy of the original text.',
],
You can also add custom hints per field:
use Iamgerwin\NovaAiContextAwareInput\Context\PolicyHintsProvider;
TextImprove::make('Comment')
->context([
(new PolicyHintsProvider())->addHints([
'Be respectful and constructive.',
'Avoid personal attacks.',
])
]);
// config/nova-ai-context-aware-input.php
'context_providers' => [
'sibling_fields' => true,
'user_persona' => false, // Disable user context
'resource_snapshot' => true,
'policy_hints' => true,
],
The package automatically detects and warns about potential PII (Personally Identifiable Information) before sending data to AI providers:
When PII is detected, the request is blocked and an error is returned.
Prevent abuse with configurable rate limits:
TextImprove::make('Content')
->rateLimit('user:10,1'); // 10 requests per user per minute
Reduce costs and improve performance by caching improvements:
// Cache key is based on: hash(input + context + style)
// Same input with same context = cache hit
The package dispatches events you can listen to:
Fired when text is successfully improved:
use Iamgerwin\NovaAiContextAwareInput\Events\TextImproved;
class LogTextImprovement
{
public function handle(TextImproved $event): void
{
// $event->originalText
// $event->improvedText
// $event->provider
// $event->style
// $event->user
}
}
Fired when a user discards an AI suggestion:
use Iamgerwin\NovaAiContextAwareInput\Events\SuggestionDiscarded;
class LogDiscardedSuggestion
{
public function handle(SuggestionDiscarded $event): void
{
// $event->originalText
// $event->suggestedText
// $event->user
}
}
You can customize the system and user prompts sent to AI providers:
// config/nova-ai-context-aware-input.php
'templates' => [
'system' => <<<'TXT'
You are an expert editor improving business text for clarity and professionalism.
Your task is to enhance the given text while:
Preserving all factual information
Respecting domain-specific terminology
Maintaining the original intent and meaning
Following the specified style guidelines
Return ONLY the improved text without explanations, quotes, or additional commentary.
TXT,
'user' => <<<'TXT'
Please improve the following text:
{{ user_input }}
Context: {{ context }}
Desired Style: {{ style }}
Language: {{ language }}
TXT,
],
Available placeholders:
{{ user_input }} - The text to improve{{ context }} - Contextual information from providers{{ style }} - The selected writing style{{ language }} - User's language/localeRun the test suite:
composer test
Run tests with coverage:
composer test-coverage
Run static analysis:
composer analyse
Format code (PSR-12):
composer format
minLength to avoid processing very short inputstrigger='manual' for fields where AI assistance is optionalWe welcome contributions! Please see CONTRIBUTING.md for details.
composer install && npm install.env.example to .env and add your API keyscomposer testPlease see CHANGELOG.md for recent changes.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see LICENSE.md for more information.
Special thanks to the Laravel and Nova communities for their excellent frameworks and tools that made this package possible.
Made with care for the Laravel community.