Tulip Logo IconTulip
Recipes

Agents in Forms

Let a button stream text into a field or fill a form with typed suggestions.

Not every agent needs a chat. A button in a form can run an agent for the signed-in user and put its result in front of them, who then decides what to keep. Both patterns run with the user's permissions and are recorded as runs with an action trigger.

Stream text into a field

"Improve description" on a task: the agent rewrites the description while the user watches, and the user keeps it or not. The agent needs no tools; the task goes in the prompt.

export const writerAgent = defineAgent({
  name: "writer",
  title: "Schrijfhulp",
  model,
  instructions: "Rewrite the task description so it is clear and complete. Answer in Markdown only.",
  tools: {},
});

Stream it from a procedure that checks the user may change the task:

improveDescription: protectedProcedure
  .use(permissionMiddleware({ task: ["update"] }))
  .input(z.object({ id: z.string() }))
  .handler(async ({ context, input, signal }) => {
    const task = await findTask(context, input.id);

    const run = await agents.stream("writer", {
      as: context.user,
      trigger: { type: "action", name: "improve_description" },
      prompt: `Title: ${task.title}\n\nDescription:\n${toMarkdown(task.description)}`,
      abortSignal: signal,
    });
    // `null`: the writer agent is not activated.
    if (!run) throw new ORPCError("FORBIDDEN", { message: "The writer is not active." });

    return streamToEventIterator(run.stream);
  }),

On the client, read the text deltas as they arrive, and save the result only when the user clicks "Keep":

const improve = useMutation({
  mutationFn: async (signal: AbortSignal) => {
    setText("");
    const chunks = await orpc.tasks.improveDescription.call({ id }, { signal });

    // A loop, not an array method: the text appears while the agent writes it.
    for await (const chunk of chunks) {
      if (chunk.type === "text-delta") setText((current) => current + chunk.delta);
      if (chunk.type === "error") throw new Error(chunk.errorText);
    }
  },
});

Abort the request when the dialog closes, so the agent stops writing. An inline editor that reads its value once needs a new key after saving to show the new text.

Fill a form with typed suggestions

"Suggest tasks" on an initiative: the agent returns tasks as data, they fill the bulk-create form, and the user removes what they don't want before creating the rest. Give the agent an output:

export const plannerAgent = defineAgent({
  name: "task_planner",
  title: "Takenplanner",
  model,
  instructions,
  tools: {},
  output: Output.object({
    schema: z.object({
      tasks: z
        .array(z.object({ title: z.string(), description: z.string(), storyPoints: z.number() }))
        .max(10),
    }),
  }),
});
suggestTasks: protectedProcedure
  .use(permissionMiddleware({ task: ["create"] }))
  .input(z.object({ id: z.string() }))
  .handler(async ({ context, input }) => {
    const result = await agents.run("task_planner", {
      as: context.user,
      trigger: { type: "action", name: "suggest_tasks" },
      prompt: await describeInitiative(context, input.id),
    });
    if (!result) throw new ORPCError("FORBIDDEN", { message: "The planner is not active." });

    return result.output?.tasks ?? [];
  }),

Fetch the suggestions with useSuspenseQuery when the dialog opens, put them in the form's rows with useFieldArray, and submit through the procedure that already creates tasks. Nothing is created until the user submits.

Guidance

  • Put what the agent needs in the prompt; give it tools only when it must look further.
  • Check the user's permission in the procedure, like any other action.
  • Never save what the agent produced without the user's click.
  • Give these agents a cheap, fast model: they do one thing.

On this page