Skip to content

AI 0.18.0: PhoenixKit 2.0, usage sinks and AI translation on new forms

AI 0.18.0 moves the module to PhoenixKit 2.0, lets other modules see every logged AI request, and makes AI translation work on forms that have not been saved yet.

AI translation before the first save

On a new record, the AI translate button used to stay disabled until the record was saved, because the background job had nothing to load. A form binding can now export an optional source_fields/2 callback that hands over the primary-language values straight from the open form. With it, a new form gets AI translation right away: the values are translated directly, up to four at a time, using the same translation core and the same progress and apply steps as saved records. The translated values are stored with the record when it is created. Forms whose binding lacks the callback behave as before.

Usage sinks

Every logged AI request is now offered to each module that exports handle_ai_usage/1. This lets a module react to AI usage without the AI module knowing about it. The first one to use it is the work ledger of Projects, which attributes AI cost to work. A module that raises or exits in its handler is logged and never stops the request from being logged.

AI calls also accept an attribution: %{...} map. It is stored as is in the request’s metadata for those modules to read; the AI module itself does not interpret it. Translation jobs and the form glue fill it with the resource type, the resource and the acting user.

A note on cost

The request field cost_cents is now documented for what it holds: millionths of a dollar, the same unit as the speech pricing, not cents. To get cents, divide by 10,000. The name stays for compatibility.

Upgrading

AI 0.18 needs PhoenixKit 2.0 and no longer installs against core 1.7. Core 2.0 starts its migrations at V135 and refuses to migrate a database below that, so read core’s 2.0.0 changelog and check mix phoenix_kit.status first. A site below V135 must first install core 1.7.236 and migrate to at least V135 before moving to 2.0. The AI module itself adds no migration.

Full release notes: AI 0.18.0

0.18.0 · 10 August 2026

Changed

  • ⚠️ Requires phoenix_kit ~> 2.0. The pin moved from >= 1.7.214 to ~> 2.0, so this release no longer resolves against core 1.7. Core 2.0.0 squashes the migration chain to a V135 floor and refuses to migrate a database below it — read core’s 2.0.0 CHANGELOG before upgrading, and check mix phoenix_kit.status first. A host below V135 must install phoenix_kit 1.7.236 (the migration bridge) and migrate up to at least V135 before moving to 2.0. Nothing in this package touches migration internals; the change here is the pin itself.

Added

  • Usage-sink dispatch (handle_ai_usage/1). Every persisted %Request{} is now offered to each discovered module exporting handle_ai_usage/1 — the zero-coupling observer seam, mirroring the Translatables discovery pattern. Best-effort per sink: a sink that raises, throws, or exits is logged and never breaks request logging. First consumer is the projects work ledger’s AI attribution. See PhoenixKitAI.dispatch_usage_sinks/1.
  • Opaque :attribution metadata on AI calls. Callers may pass attribution: %{...}; it is normalized to string keys and recorded verbatim into request metadata. This package never interprets it — usage sinks do. TranslateWorker and the form glue now populate it with resource_type / resource_uuid / actor_uuid.
  • VALUE MODE for AI-translate on unsaved forms. A FormBinding may now export the optional source_fields/2 callback returning the primary-language values straight from the live form’s assigns. When present, :new forms get AI translation instead of being disabled-until-saved: with no record for the Oban worker to load, the glue translates the live values through the same Translation.translate_fields/6 core, over supervised tasks (concurrency 4) that self-send results in the exact message shape the PubSub path delivers — so the whole progress/stall/apply state machine is reused unchanged, and the applied changeset persists with the eventual create. Bindings without the callback keep the old behaviour.

Documentation

  • Request.cost_cents is documented at the schema as the misnomer it is: the stored unit is the same 1/1,000,000-of-a-dollar unit TtsPricing uses, not cents, so cents = value ÷ 10,000. Multiple external reviewers had misread it; the field name is kept for schema compatibility.

AI 0.18.0. Install or update: {:phoenix_kit_ai, "~> 0.18"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.