PHP code example of iamgerwin / nova-ai-context-aware-input

1. Go to this page and download the library: Download iamgerwin/nova-ai-context-aware-input library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

iamgerwin / nova-ai-context-aware-input example snippets


use Iamgerwin\NovaAiContextAwareInput\Fields\TextImprove;

public function fields(Request $request)
{
    return [
        TextImprove::make('Description')
            ->style('professional')
            ->trigger('blur'),
    ];
}

// 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'),

'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

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',

'provider' => 'anthropic:claude-3-5-sonnet-20241022',
// or
'provider' => 'anthropic:claude-3-opus-20240229',
'provider' => 'anthropic:claude-3-haiku-20240307',

'provider' => 'deepseek:deepseek-chat',

'provider' => 'azure_openai:gpt-4',

TextImprove::make('Description')
    ->context(['title', 'category', 'tags']);

// Automatically / - Locale/language preference
// - Timezone

// Automatically pe (e.g., "Article", "Product")
// - Resource ID
// - Common fields (title, name, description, category, type, status)

// 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.',
],

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,
],

TextImprove::make('Content')
    ->rateLimit('user:10,1');  // 10 requests per user per minute

// Cache key is based on: hash(input + context + style)
// Same input with same context = cache hit

use Iamgerwin\NovaAiContextAwareInput\Events\TextImproved;

class LogTextImprovement
{
    public function handle(TextImproved $event): void
    {
        // $event->originalText
        // $event->improvedText
        // $event->provider
        // $event->style
        // $event->user
    }
}

use Iamgerwin\NovaAiContextAwareInput\Events\SuggestionDiscarded;

class LogDiscardedSuggestion
{
    public function handle(SuggestionDiscarded $event): void
    {
        // $event->originalText
        // $event->suggestedText
        // $event->user
    }
}

// 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,
],
bash
php artisan nova:publish
bash
composer analyse