PotionUI Docs v0.3.0

Contributing a plan limit kind

A plan is a named list of limits, and each limit is one kind: storage space, generations per day, cloud spend per month and the largest single upload ship with core. A plugin adds kinds of its own with a limit_kinds: section; the plan editor's "Add limit" picker, the users list columns, the user's usage view and the refusal all render from the kind, so a kind needs no frontend code.

On this page
yaml
limit_kinds:
  - key: credits.monthly          # must be namespaced: <plugin>.<name>
    label: Credits per month
    short_label: Credits          # optional, for usage rows and columns
    description: Each submit spends one credit
    value_type: count             # bytes | count | usd
    unit: per month               # optional editor suffix
    window: month                 # none (a level) | day | month
    enforce_at: [submit]          # submit and/or upload
    ledger: true                  # count one event per accepted submit
    measure_handler: limits.measure     # optional when ledger: true
    applies_handler: limits.applies     # optional, narrow where the kind is asked
    incoming_handler: limits.incoming   # optional, what one request adds
    refusal_code: credits_exhausted     # optional, default <key>_exceeded
    refusal_message: "You have used {used} of {limit} credits. {contact}"
    admin_only_values: false      # optional; usd kinds hide numbers from users by default
    warn_at: 0.8
  • measure_handler(context: MeasureContext) -> Usage | number returns the user's usage for context.user_id only. context.window_start and context.window_end bound the current day or month (midnight in the instance timezone, an admin setting, UTC by default); context.events reads and writes this kind's ledger for the user (total(), record(units, ref_id), refund(ref_id)). Keep it fast: it runs on every submit or upload the kind applies to.
  • ledger: true makes core record one event per accepted submit (incoming_handler can change the units) and refund it when the run is cancelled before it started or fails with no output. Without a measure_handler the usage is the ledger total in the window. A ledger kind needs a day or month window; events are kept 40 days. To count something else, call LimitEvents.record(user_id, kind, units, ref_id) yourself.
  • applies_handler(request: AdmissionRequest) -> bool sees point, user_id, incoming_bytes (uploads; the total of every file in the request), item_bytes (uploads; the size of each file, or None when the caller passed only a total), engine, backend_id and preset_id (submits).
  • incoming_handler(request) -> number | None: None means unknown, and the kind refuses when used >= limit; a number refuses when used + incoming > limit.
  • refusal_message may use {label}, {used}, {limit}, {resets} and {contact} (the admin's contact line).

What the host guarantees#

  • A refusal is one HTTP 403 limit_exceeded carrying the kind, its code, usage, limit and reset time. Running work is never stopped and nothing is ever deleted for the user.
  • Admins are exempt while "Admins are exempt" is on (the default); their usage is still measured.
  • Disabling the plugin removes its kinds. Plans keep the limits, shown as inactive and never enforced, and they apply again when the plugin is back.
  • A kind whose measure raises is skipped for that request (logged), never enforced as zero.