You're reading docs for Nextly Alpha. APIs may change between releases.

Plugins

AI tasks

Declare what an AI feature needs from a model, and where its result goes, without being handed a provider or a key.

AI tasks

Alpha (0.x) — @experimental. defineAITask, contributes.aiTasks and the AITask* types are new and may change as the first AI features use them.

An AI task is a declaration: it says what a model is given, what its answer must look like, which kind of model it needs, and where the result goes. Your plugin never calls a model. The site's AI package does the calling. Nextly checks the caller's input against your input shape before the model is asked, checks the answer against your output shape, records what the run used, and hands your task only the checked output. Nextly hands your task no provider, no API key and no network client: its one function gets the checked output and reads of content as the person the run acts as. That keeps model calls, and what they cost, in one place the site controls. It is not a sandbox: a plugin's code runs in the site's own process, with whatever that process can reach, so install only plugins you trust.

A task does nothing until the site adds an AI package. Until then it stays registered, and the site's AI status lists it as not available.

Declare a task

import { defineAITask, definePlugin } from "@nextlyhq/plugin-sdk";
import { z } from "nextly/zod";

const altText = defineAITask({
  name: "alt-text",
  version: 1,
  title: "Alt text",
  description: "Describes an image for people who can't see it.",
  input: z.object({ image: z.string() }),
  output: z.object({ alt: z.string() }),
  role: "vision",
  abilities: { structuredOutput: true, imageInput: true },
  instructions: "Describe the image in one plain sentence.",
  reads: ["image", "title"],
  resultGoesTo: "suggestion",
  cacheable: true,
  bulk: true,
  costHint: { inputTokens: 800, outputTokens: 40 },
  toResult(output) {
    return output.alt;
  },
});

export default definePlugin({
  name: "@acme/media-helper",
  version: "1.0.0",
  nextly: ">=0.0.1",
  contributes: { aiTasks: [altText] },
});
FieldWhat it says
nameUnique within your plugin. With your plugin's name it is the task's identity, so another plugin's alt-text is a different task.
versionRaise it when the instructions or shapes change.
title, descriptionPlain words for the people choosing and running the task.
input, outputZod schemas. A model's answer is checked against output before your task sees it.
roleThe kind of model the task needs: "fast", "writer", "vision", "embedding" or "image". The site maps each role to a model; your task never names one.
abilitiesstructuredOutput and imageInput, when the task needs them.
instructionsThe task's fixed instructions to the model.
readsThe content fields the task reads. Only these will be sent, once the run pipeline that sends them is added.
resultGoesTo"suggestion" (a person accepts or dismisses it), "draft" (written as the person), or "caller" (handed back only). A task never publishes, deletes, emails or changes settings.
cacheable, bulkWhether an answer may be reused for the same input, and whether the task may run over many entries in one job.
costHintYour estimate of one item's tokens, so a budget can be checked before a run starts.
toResultOptional. Turns the checked output into the result. It receives the checked output and a context that can read content (listEntries, findEntryById, count) as the person the run acts as (for a run on an API key, with the key's own access, not its owner's), and nothing else: it can't read as the system, write, delete, or reach a model, a key or the network. The run pipeline writes the result itself, as a suggestion or as the person's draft.

When a site starts

Each start reads the tasks of the plugins that start. A start is refused, naming the task and its plugin, when:

  • a task has no name, or an empty one;
  • your plugin declares two tasks with the same name;
  • a task has no input shape;
  • a task has no output shape;
  • a task's role is missing or isn't one of the five.

A plugin turned off (enabled: false) or removed from the configuration takes its tasks with it: at the next start, and in development as soon as the configuration reloads, when its tasks stop being listed or run (turning it on again in the same session brings them back). A run naming one of them is refused as an unknown task.

When a task runs

Every run is checked against your task's own shapes:

  • The caller's input is parsed with your input shape before the model is asked, or the AI package asked anything. Input that doesn't match is refused, and the model is never asked. The model is given the parsed input, so keys your shape doesn't declare never reach it, and your shape's defaults and transforms apply.
  • The model's answer is parsed with your output shape. An answer that doesn't match ends the run as failed. One that matches is handed on as parsed, without keys your shape doesn't declare.
  • Both shapes may refine or transform asynchronously; Nextly waits for them.
  • A streamed run's input is checked the same way. Its text arrives in pieces, so it isn't checked against your output shape.

Setting NEXTLY_AI=off where the site is deployed, or ai: "off" in its configuration, keeps your tasks registered but refuses every run.