Skip to content

For developers

The module separates the workflow from the provider. The workflow (dataset building, job entity, UI, Drush, cron) is fixed; providers plug in through one plugin type.

Extension points

Extension point What it is for
AiFinetune plugins Talk to one provider: upload the dataset, start, poll and cancel a training. This is the main extension point. See Writing a fine-tuning plugin.
hook_ai_finetuner_info_alter(array &$definitions) Alter discovered plugin definitions (labels, dataset_types, ai_provider).
ai_finetuner.runner service Build datasets and drive jobs from your own code. See Services and value objects.
FinetuneJobInterface The job entity. Create jobs programmatically with FinetuneJob::create().
Views display ai_finetuner and style ai_finetuner_dataset Already generic; you rarely need to extend them.

Architecture

flowchart TB
    subgraph This module
        Job[(FinetuneJob entity)]
        Runner[FinetuneRunner]
        PM[AiFinetunePluginManager]
        DS[FinetuneDataset]
        ST[FinetuneStatus]
        Form[FinetuneJobForm]
        Ctl[FinetuneJobController]
        Drush[FinetunerCommands]
        Cron[FinetunerHooks::cron]
    end
    subgraph Provider module
        Plugin[AiFinetune plugin]
        Client[Provider API client]
    end
    Form --> Job
    Ctl --> Runner
    Drush --> Runner
    Cron --> Runner
    Runner --> Job
    Runner --> PM --> Plugin
    Runner -- builds --> DS -- passed to --> Plugin
    Plugin --> Client
    Plugin -- returns --> ST -- applied to --> Job

The runner is the only thing that calls a plugin. It loads the plugin for a job, builds the dataset from the job's source, validates the dataset against the plugin (type, minimum items) and applies the returned FinetuneStatus to the job.

Plugin discovery

  • Plugin manager service: plugin.manager.ai_finetuner (AiFinetunePluginManager).
  • Discovery directory: src/Plugin/AiFinetune/ in any enabled module.
  • Attribute: Drupal\ai_finetuner\Attribute\AiFinetune.
  • Interface: Drupal\ai_finetuner\AiFinetuneInterface; base class AiFinetuneBase.
  • Alter hook: hook_ai_finetuner_info_alter().
  • Cache bin key: ai_finetuner_plugins; clear caches after adding a plugin.

A provider module can ship a plugin without declaring a dependency on ai_finetuner. The class is only loaded when the plugin manager scans, so it is harmless when this module is not installed.

Testing

tests/modules/ai_finetuner_test ships a fake plugin (test_finetune) that accepts both dataset types, needs at least two items and records what it received in State. The kernel test FinetuneRunnerTest uses it to cover the three dataset sources, the lifecycle and cron refresh. Reuse the test module when you test code that creates or starts jobs.

vendor/bin/phpunit web/modules/custom/ai_finetuner/tests

Agent skills

The module ships two skills under .agents/skills/ for AI coding assistants that follow the Agent Skills specification: create-ai-finetune-plugin scaffolds a plugin, run-ai-finetuning walks through creating and monitoring jobs with Drush.