Search by

goodport / laravel-ai-moderator

goodport

Declarative AI moderation checks for Laravel applications.

Package info

github.com/goodport/laravel-ai-moderator

pkg:composer/goodport/laravel-ai-moderator

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.1.0 2026-11-26 04:44 UTC

This package is auto-updated.

Last update: 2026-11-26 04:44:32 UTC


README

Declarative AI moderation checks for Laravel applications.

The package turns a small check declaration into a complete moderation pipeline: prompt building, structured JSON output, validation, a decision rule, an optional second opinion, and one uniform result object. It uses the official Laravel AI SDK for provider calls.

The package knows nothing about your domain models. Your application describes what to check and supplies the data; the package handles the rest.

Requirements

  • PHP 8.3 or newer
  • Laravel 12.62+ or 13.x
  • laravel/ai 1.4+
  • A configured AI provider in config/ai.php

Installation

composer require goodport/laravel-ai-moderator

Laravel discovers the service provider automatically. Publish the configuration:

php artisan vendor:publish --tag=ai-moderator-config

Quick start

Define a check in config/ai-moderator.php:

'checks' => [
    'post_body' => [
        'kind' => 'verdict',
        'input' => [
            'fields' => [
                'body' => ['type' => 'text', 'label' => 'Body', 'max_chars' => 4000],
            ],
        ],
        'instructions' => 'Reject profanity, contact details, and advertising.',
        'violations' => [
            'profanity' => ['severity' => 'high', 'blocking' => true, 'description' => 'Profanity or insults'],
            'contacts' => ['severity' => 'high', 'blocking' => true, 'description' => 'Phone numbers, emails, links'],
            'ads' => ['severity' => 'medium', 'blocking' => false, 'description' => 'Advertising'],
        ],
    ],
],

Run it:

use Goodport\AiModerator\Facades\AiModerator;

$result = AiModerator::check('post_body')
    ->input(['body' => $post->body])
    ->run();

if ($result->passed() === false) {
    // Rejected: $result->reason(), $result->violations()
}

if ($result->passed() === null) {
    // Undecided: provider error, invalid response, or another failure.
    // The application decides what to do.
}

Result exposes:

$result->state();        // passed | rejected | undecided | skipped
$result->passed();       // true | false | null
$result->failed();       // true only for rejected
$result->undecided();    // true when the AI could not produce a valid decision
$result->skipped();      // true for a disabled check
$result->reason();
$result->violations();   // Violation[]: code, message, severity, blocking
$result->label();        // classify checks
$result->answers();      // checklist checks
$result->details();      // custom check payload
$result->meta();         // model, provider, pass, invocation id, duration, trace id, error
$result->toArray();

Input resolvers

For anything beyond a simple array, implement InputResolver:

use Goodport\AiModerator\Input\InputResolver;
use Laravel\Ai\Files\Image;

final class PostInput implements InputResolver
{
    public function __construct(private readonly Post $post) {}

    public function fields(): array
    {
        return [
            'body' => $this->post->body,
            'cover' => Image::fromUrl($this->post->coverUrl()),
        ];
    }
}
$result = AiModerator::check('post_body')
    ->input(new PostInput($post))
    ->run();

The package never reads Eloquent attributes, relations, or storage by itself. Missing required fields throw InvalidInputException before any AI call.

Check kinds

Verdict

The model decides approve or reject. Rejections must include at least one blocking violation; approvals cannot include blocking violations. An inconsistent response becomes undecided.

Checklist

The model answers every declared question. The package compares the answers with the expected values:

'review_questions' => [
    'kind' => 'checklist',
    'input' => [
        'fields' => [
            'body' => ['type' => 'text', 'label' => 'Review', 'max_chars' => 10000],
        ],
    ],
    'instructions' => 'Answer the questions about the review.',
    'questions' => [
        'insults' => [
            'label' => 'Does it contain insults?',
            'values' => ['yes', 'no'],
            'expected' => 'no',
            'blocking' => true,
            'severity' => 'high',
        ],
        'ads' => [
            'label' => 'Does it contain advertising?',
            'values' => ['yes', 'no'],
            'expected' => 'no',
            'blocking' => false,
            'severity' => 'medium',
        ],
    ],
],

A blocking mismatch rejects the check. A non-blocking mismatch is returned as a warning in violations() and does not reject.

Classify

The model returns one label from a fixed list:

'age_rating' => [
    'kind' => 'classify',
    'input' => [
        'fields' => [
            'body' => ['type' => 'text', 'label' => 'Body', 'max_chars' => 30000, 'strategy' => 'head_tail'],
        ],
    ],
    'instructions' => 'Classify the content.',
    'labels' => ['0+', '6+', '12+', '16+', '18+'],
    'decision' => ['reject_on' => [], 'severity' => 'high'],
],

reject_on is the list of labels that reject the content. An empty list makes the check label-only.

Language

The response language is resolved in this order:

  1. ->language('en') on the call
  2. the check's language
  3. the profile's language
  4. defaults.language (auto means "not set")
  5. App::getLocale()
$result = AiModerator::check('post_body')
    ->input(new PostInput($post))
    ->language('en')
    ->run();

The package appends a final instruction to write reason and human-readable messages in the resolved language. It does not keep separate prompt sets per locale. Use violation codes for your UI translations instead of relying on model text.

In queued jobs, App::getLocale() is the worker locale. Set the locale in the job or pass it explicitly with ->language().

Profiles

Share platform context, model, middleware, and second-pass settings across checks:

'profiles' => [
    'reviews' => [
        'context' => 'A platform where authors publish creative work and readers leave reviews.',
        'language' => 'auto',
        'model' => 'gpt-5-mini',
        'middleware' => [],
        'second_pass' => [
            'enabled' => true,
            'trigger' => 'on_reject',
            'merge' => 'approve_wins',
        ],
    ],
],

'checks' => [
    'review_quality' => [
        'profile' => 'reviews',
        'kind' => 'verdict',
        // ...
    ],
],

Merge order: defaults → profile → check. Middleware is merged; second_pass is merged deeply; input and kind-specific blocks belong to the check.

Second opinion

'second_pass' => [
    'enabled' => true,
    'trigger' => 'on_reject',      // on_reject | always
    'provider' => 'anthropic',
    'model' => 'claude-sonnet-4-5',
    // 'agent' => SecondOpinionAgent::class, // optional
    'merge' => 'approve_wins',     // approve_wins | strict | second_decides
],
  • on_reject runs the second pass only after a rejection.
  • always also runs after approvals.
  • undecided never triggers a second pass.
  • If provider or model is omitted, the first pass values are used.

Merge strategies:

First Second approve_wins strict second_decides
pass pass pass pass pass
pass reject pass reject reject
pass undecided pass undecided undecided
reject pass pass reject pass
reject reject reject reject reject
reject undecided undecided reject undecided

Policies

Run several checks over one input and get one report:

'policies' => [
    'post' => [
        'checks' => ['post_body', 'post_cover'],
        'stop_on_reject' => false,
    ],
],
$report = AiModerator::policy('post')
    ->input(new PostInput($post))
    ->run();

$report->passed();      // true | false | null
$report->rejected();
$report->undecided();
$report->results();     // key => Result
$report->messages();    // key => reason or error
$report->violations();  // key => Violation[]
$report->toArray();

Disabled checks are omitted from the report. With stop_on_reject, the run stops after the first rejection.

Errors and undecided

  • Provider errors, timeouts, invalid structured output, and inconsistent decisions become undecided.
  • undecided is never treated as approved.
  • Result::meta()->error contains a diagnostic string.
  • Set on_error to throw in defaults or on a check to rethrow the original exception instead.
  • Configuration and input errors always throw.

Custom checks

A check is either a declaration or a class implementing Check:

use Goodport\AiModerator\Checks\Check;
use Goodport\AiModerator\Checks\CheckDefinition;

final class CustomCheck implements Check
{
    public function definition(): CheckDefinition
    {
        // Build a CheckConfig and a VerdictCheckDefinition,
        // ChecklistCheckDefinition, or ClassifyCheckDefinition.
    }
}
'checks' => [
    'custom' => CustomCheck::class,
],

Extension points:

  • InputResolver — named fields for the check.
  • PromptBuilder — custom system prompt, user prompt, and attachments.
  • DecisionRule — custom decision logic.
  • agent — a custom Agent that owns its instructions and schema. The package still builds the user prompt and attachments. The check's prompt_builder and middleware are ignored for that pass.
  • details_schema — one level of additional structured output for class-based checks.

Journal and replay

The package depends on goodport/laravel-ai-journal. Composer installs both packages, Laravel registers both providers, and the journal migrations are loaded automatically. Every moderation call is recorded without application code.

Replay is enabled by default. For a built-in ModerationAgent, the package automatically attaches the journal replay middleware. When the same check receives the same fingerprint within the TTL, the successful step is reused without a provider call.

Configuration:

'journal' => [
    'replay' => [
        'enabled' => true,
        'ttl' => 86400,
        'scope' => null,
        'always_fresh_checks' => [],
    ],
],
  • replay.enabled controls automatic replay integration.
  • replay.ttl is the number of seconds a stored response remains reusable. 0 disables replay.
  • replay.scope isolates stored responses, for example per tenant. null keeps the application's Context scope.
  • always_fresh_checks lists check keys that must always call the provider again.

To force a new request for one run, call fresh():

AiModerator::check('post_body')
    ->input(['body' => $body])
    ->fresh()
    ->run();

The journal fingerprint includes ai.check and ai.pass, so separate checks and the first/second passes do not reuse each other's responses.

Custom agents supplied through the agent option keep their own middleware. The journal still records their calls, but automatic replay is only attached to the built-in ModerationAgent. A custom agent can attach ReplayRecordedStep itself or opt out of replay.

The journal package also owns capture settings, redaction, cost calculation, retention, and its own read API. Publish its config when those settings need to be changed:

php artisan vendor:publish --tag=ai-journal-config

Any other laravel/ai agent middleware can still be attached through the check middleware option.

Testing

The package uses laravel/ai fake agents:

use Goodport\AiModerator\Agents\ModerationAgent;

ModerationAgent::fake([
    ['verdict' => 'approve', 'reason' => 'Fine.', 'violations' => []],
]);

$result = AiModerator::check('post_body')
    ->input(['body' => 'Hello'])
    ->run();

$this->assertTrue($result->passed());

Prompts and attachments can be asserted:

use Laravel\Ai\Prompts\AgentPrompt;

ModerationAgent::assertPrompted(function (AgentPrompt $prompt): bool {
    return str_contains($prompt->prompt, 'Hello')
        && $prompt->attachments->count() === 1;
});

Development

composer test
composer format

License

MIT. See LICENSE.