Actions

From BuilderIO/agent-native · View interactive version

Actions is an AI agent skill from the GitHub repository BuilderIO/agent-native. How to create and run agent actions. Actions are the single source of truth for app operations — the agent calls them as tools and frontend code calls them through client hooks. Use when creating a new action, adding an API integration, or wiring up frontend data fetching.

How to use this skill

Skill file

Agent Actions

Rule

Actions in actions/ are the single source of truth for app operations. The agent calls them as tools, the frontend calls them through useActionQuery / useActionMutation, and the framework owns the HTTP transport behind those hooks — no duplicate /api/ routes.

Before creating any custom route for app data, check actions/ and the action table in AGENTS.md. An action already exists? Call it directly. Missing? Create or update a defineAction. Stop trigger: about to add a file under server/routes/api/ (or middleware to guard one)? Check it against the exception list in Custom /api/ Routes below first — even if you already started the route.

Keep Actions Deterministic

An action may call a provider API, validate data, and persist records without being an AI feature — keep it deterministic, focused, and independently useful to the agent. Don't put LLM calls or a second model runtime in app actions.

The sanctioned model-backed exception is @agent-native/core/decisions for capability-aware classification and scoring, and it returns a result without performing or authorizing the selected action.

When a workflow is research, analysis, generation, recommendation, or synthesis — or spans several provider calls and writes — route it to the AgentSidebar via sendToAgentChat({ openSidebar: true }) and let the agent orchestrate focused actions instead of hiding an AI-shaped workflow behind one opaque generate-*/create-* action just because its implementation happens to be deterministic.

How to Create an Action

One action per file, default-exported. The filename is the action name (kebab-case): actions/list-meals.ts is the list-meals action. Don't put several defineActions as named exports in one file — the registry keys actions by filename and only the default export is registered, so extra named exports never become callable and won't match .generated/action-types.d.ts.

Subfolders group actions by domain: the name is the relative path with - between segments, so actions/billing/create-invoice.ts is billing-create-invoice. Two files that resolve to the same name (that one and a flat actions/billing-create-invoice.ts) fail boot. Folders and files starting with _ are helpers and are never registered.

// actions/list-meals.ts
import { z } from "zod";
import { defineAction } from "@agent-native/core/action";
import { getDb } from "../server/db/index.js";
import { meals } from "../server/db/schema.js";

export default defineAction({
  description: "List all meals",
  schema: z.object({}),
  http: { method: "GET" },
  run: async () => {
    const db = getDb();
    const rows = await db.select().from(meals);
    return rows; // Return objects/arrays, NOT JSON.stringify()
  },
});

schema (Zod or Standard Schema-compatible) gives runtime validation, TS inference for run() args, and an auto-generated JSON Schema for the tool. Write it so an agent can call the tool correctly on the first try: .describe() every param and state enum values and the default in that text (z.enum(["open","closed"]).default("open").describe('Filter status: "open" or "closed"; defaults to "open"')); mark a field .optional() only when the action truly tolerates its absence — a silent default the caller cannot see is a required field in disguise; name the primary id the same way across a family (create-/get-/update-/delete-<thing> all take id, not a mix of id and <thing>Id), and if one action must differ, accept both with .or(); give nested objects a real schema, never a prose description; keep the one-line description complete enough to call the tool without a second lookup, because compact catalogs and tool-search show only that line. Use z.coerce.number() for numeric HTTP params, but write an explicit boolean parser instead of z.coerce.boolean(), which treats "false" as truthy.

Some models (GPT-6 Luna in strict mode) send every optional field they aren't using, filled with null, [], false, or an enum's only value, so a call must mean the same with that padding as without it. Make an optional switch act only when true (fixedCanvas: true, not responsive: false), and never give [] a meaning of its own such as "none".

Use Drizzle's PostgreSQL query builder, not raw SQL/getDbExec() or direct driver imports, unless Drizzle can't express the query. Never hardcode API keys/tokens/secrets - read via readAppSecret / resolveCredential / OAuth helpers; process.env is deploy-level config only.

Decision order: existing action → extend/create a defineAction → custom route as last resort (Custom /api/ Routes below). Actions are already callable by agents, CLIs, hooks, HTTP, and MCP/A2A — don't wrap them in an umbrella REST API.

Keep the Action Surface Small and Orthogonal

Every agent-exposed action is a tool in the model's context window; more tools degrades tool-selection quality. Add the fewest, most orthogonal actions that cover the capability.

Key Actions — One Index, Every Surface

Document the app's primary actions in the AGENTS.md action table. For first-turn loading, add those action names to the plugin's initialToolNames (INITIAL_TOOL_NAMES in some templates), or mark the full starter set deferLoading: false on the actions. When one action opts into per-action eager loading, unmarked actions are deferred; the remaining actions stay discoverable through tool-search. When users name an action's job in words its name and description lack ("auto-archive rule", "canned reply"), add those words as searchHints: tool-search ranks them just below the name and never shows them to the model.

When MCP is enabled, its Key tools line is generated from mcp.keyToolNames ?? initialToolNames and filtered to the actions served on that surface. A configured mcp.keyToolNames overrides the advertised MCP/WebMCP key list; it does not change the in-app agent's first-turn list. Keep the advertised subset consistent with the primary actions in the AGENTS.md table and the external catalog; the table may also document other in-app actions. Do not hand-write another action list in mcp.instructions, a skill, or an external skill; describe when to use actions there.

The `http` Option

Controls HTTP exposure:

| Value | Behavior | Use for |

| --- | --- | --- |

| _(omitted)_ | POST /_agent-native/actions/:name | Write operations (default) |

| { method: "GET" } | GET /_agent-native/actions/:name | Read-only queries |

| { method: "PUT"/"DELETE" } | matching verb | Update / delete |

| { method: "GET", path: "x" } | custom route path | Non-default path |

| false | never exposed as HTTP | navigate, view-screen, internal |

Mutating actions (anything but GET) auto-refresh the UI on success — don't call refresh-screen after a normal action. Overrides (readOnly, parallelSafe) and exact trigger rules: references/action-fields.md.

Saves That Can Race

An action that writes a value a person or another agent may also be changing checks what it read:

Outcomes are counted (save_outcome_counts, and save_conflict_prompt in the browser), so prompts that could have been automatic show up.

Names That Must Stay Unique

When a record's name must be unique in its parent (a file in a design, say), back the rule with a unique index and pick names with nextFreeName from @agent-native/core/free-name: name.html, then name-2.html, name-3.html (a slug whose alphabet has no - passes { separator: "_" }). Don't write another copy of that loop.

Return Values

Return structured data (objects, arrays), never JSON.stringify() — the framework serializes the response and only tries to parse a returned string as JSON, which isn't the same contract.

run: async (args) => await fetchEvents(args.from, args.to); // good
run: async (args) => JSON.stringify(await fetchEvents(...)); // bad

A create/update result is read by more than the caller. These keys are well-known and survive result truncation:

For actions that attach a chatUI renderer, use chatUI.projectResult to return

only the small structured fields the renderer needs. chatUI.when is evaluated

against the full successful result first; the projection is used live and saved

for interrupted-run recovery.

An action that hands control back to the user (question form, intake dialog) sets endsTurn: true; that hides it from MCP/WebMCP/A2A unless mcpTool: true is explicit — references/action-fields.md. For external-agent integrations, use the external-agents documentation slug listed by agent-native-docs.

An action the app's agent can call that the app's MCP catalog doesn't already serve needs a decision, or outside agents can reach it only through ask_app: mcpTool: true serves it to them in their tool list, mcpTool: "search" serves it without listing it (tool-search finds it and call-tool runs it, which keeps a long catalog short), and mcpTool: false keeps it in-app, with a comment on that line saying why. Add a test that calls a served action over MCP.

Every new action declares outputSchema: it types the action's return and is enforced strictly by default (guard:action-output-schema). Declare its failure codes with defineActionErrors and fail through them. Reach for _agentImages (attach images the agent can see), authorize (gate who may call it), or needsApproval (require human sign-off per call) only when the action needs that guarantee — examples in references/action-fields.md.

To test an action, start with checkActionProperties(action, { context }) from @agent-native/core/action-properties. It generates inputs from the zod schema, and fails on any throw that is not an action contract error, on a return its outputSchema rejects, and on an invalid input that reaches run. It needs fast-check as a devDependency, and it throws UnsupportedSchemaError for a schema shape it cannot generate unless you pass an override.

Write receipts

A write action that can check its own effect returns a plain-object result with a reserved _receipt, so the final answer is reconciled with what the write did, not with the model's reading of a JSON string that may be truncated:

import type { WriteReceipt } from "@agent-native/core/action";

const _receipt: WriteReceipt = {
  changed: true,
  verified: false,
  summary: "Saved; panel 3 returned no rows.",
  checks: [{ id: "panel-3", ok: false, detail: "0 rows" }],
};
return { id, _receipt };

verified is true (the effect was observed), false (checked and did not hold), or "unverified" (could not be checked); checks and warnings are optional. The agent loop reads the receipt before the result is stringified and truncated (summary 200 chars, 8 checks, 5 warnings):

A receipt is not an error channel. A write that did not achieve the requested state throws (fail()); return changed: false only for a benign no-op, such as the record already being in the requested state.

Failed Writes

The framework classifies a failed action from the writes it saw the call make: every database write (any statement that is not a read such as SELECT, SET or LOCK, and not the framework's own schema setup) and every fetch that is not GET, HEAD or OPTIONS. A rejection raised before any of those is "nothing saved", however it is thrown (fail(), a plain Error, a library error).

Frontend Hooks

Import from focused @agent-native/core/client/* entry points; the broad

@agent-native/core/client barrel is deprecated. Use action hooks, not

hand-written fetch("/_agent-native/actions/...").

import { useActionQuery, useActionMutation, callAction } from "@agent-native/core/client/hooks";

const { data: meals } = useActionQuery("list-meals", { date: "2025-01-01" }); // GET, types auto-inferred
const { mutate } = useActionMutation("log-meal");                             // POST/PUT/DELETE
mutate({ name: "Salad", calories: 350 });
const people = await callAction("search-people", { query }, { method: "GET" }); // imperative (debounce, prefetch)

Don't add manual generics like useActionQuery<Meal[]>(...) — types come from .generated/action-types.d.ts. Mutations auto-invalidate all ["action"] query keys, so GET queries refetch. A revisit paints the persisted result for the signed-in user and org before that refetch; see references/action-fields.md for persistInBrowser: false.

A failed useActionMutation write shows one shared toast (WriteFailureToaster, mounted by AppProviders) when nothing else reports it. mutate toasts unless an onError (hook-level or per call) handles the failure; mutateAsync toasts only when its rejection ends up unhandled. errorNotification picks the mode: "auto" (default), "shared" (toast even beside an onError that only rolls back or logs), or "caller" (never: background saves, deliberate swallows, call sites that render the error themselves). errorLabel is an already-translated title. A fail() with a 4xx status shows its message as the toast text, a 5xx shows only the title because the server masks its text, and a 401 stays silent. For a write that is not a useActionMutation (a raw fetch upload, a swallowed catch), call reportWriteFailure(error, { label }) from @agent-native/core/client/write-failure.

When a query result varies by viewer or another client context that is not part of the action arguments, pass that stable context through queryKeyScope. It partitions only the local query cache; it is not sent to the action and never replaces server-side authorization.

How to Run (Agent)

pnpm action log-meal --name "Salad" --calories 350

CLI flags become action input fields (--key value or --key=value); the runner does not read or write files for --input or --output. The default template dispatches through core's runScript() in actions/run.ts. Action names are lowercase-with-hyphens (pnpm action my-action → actions/my-action.ts).

For a needsApproval action, the CLI checks parsed/defaulted input with caller cli. An interactive terminal shows the action and arguments and asks y/N (only y or yes runs). Without both stdin and stdout TTYs, ask the person to approve the specific call, then rerun the exact original pnpm action arguments with --approved appended. Never add --approved before the person approves. This reserved flag is removed before action input parsing and is harmless on ungated actions. See references/action-fields.md for approval details.

Custom `/api/` Routes

Complete exception list — justified only when the caller isn't your own UI/agent, or the payload isn't JSON: file uploads (actions take JSON, not multipart), streaming (SSE/chunked needing direct H3 control), webhooks, OAuth callbacks (fixed redirect URL patterns), public unauthenticated endpoints (SEO/OG images, share links), binary/non-JSON responses.

Everything else — CRUD, settings, search, list/detail reads, auth state, anything the UI fetches as JSON — is an action. Needing middleware to scope a route to the current user is itself a signal it should be an action. Existing template /api/* CRUD routes are being migrated; do not add new ones.

Do / Don't

Troubleshooting

References

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.