Client methods

From BuilderIO/agent-native · View interactive version

Client methods is an AI agent skill from the GitHub repository BuilderIO/agent-native. Client method surface rules. Use when wiring browser/client code to actions, application state, framework routes, app APIs, uploads, auth, or settings.

How to use this skill

Skill file

Client Methods

Rule

Browser/client code imports named methods, hooks, or client modules instead of

hand-writing REST calls to framework or app routes.

Why

Route shapes are transport details. If components and docs call

fetch("/_agent-native/...") or template /api/* routes directly, every caller

has to rediscover auth, base paths, request-source headers, JSON parsing, error

handling, optimistic updates, sync invalidation, and route quirks. A named client

method gives the UI, docs, and future agents one stable contract.

How

  1. Look for an existing client API first.

| Need | Use |

| --- | --- |

| App action reads/writes | useActionQuery / useActionMutation from @agent-native/core/client/hooks |

| Imperative action calls | callAction from @agent-native/core/client/hooks |

| Browser application state | readClientAppState, writeClientAppState, setClientAppState, deleteClientAppState from @agent-native/core/client/hooks |

| Navigation/app-state sync | useAgentRouteState / useSemanticNavigationState from @agent-native/core/client/navigation |

| Agent chat context | Agent chat helpers from @agent-native/core/client/agent-chat |

| Ask the user a multiple-choice question from app code | askUserQuestion from @agent-native/core/client/agent-chat (renders inline in the agent panel; answer goes to the agent — do not build a custom modal) |

| Live sync | useDbSync, useChangeVersion, useChangeVersions from @agent-native/core/client/hooks |

| Extension iframe calls | appAction, appFetch, extensionFetch from the extension runtime |

| Toast for a failed write that is not an action mutation | reportWriteFailure(error, { label }), or meta: surfaceMutationError(label) on a raw useMutation, from @agent-native/core/client/write-failure |

Action fetch behavior: every useActionQuery / useActionMutation /

callAction request is bounded by a 60s timeout, and timeouts surface as

errors instead of retrying silently. useActionQuery cancels superseded

requests automatically via React Query's abort signal. For imperative

calls, callAction(name, params, { method, signal, timeoutMs }) accepts an

AbortSignal and a timeoutMs override for legitimately long operations.

Failed writes: useActionMutation shows one shared toast on its own when

nothing handles the error (errorNotification: "auto", the default).

"shared" toasts even beside an onError that only rolls back or logs, and

"caller" opts out for background saves and deliberate swallows. A write

that goes through a raw fetch (an upload, a stream) or a swallowed catch

announces its failure with reportWriteFailure(error, { label }); a raw

useMutation opts in with meta: surfaceMutationError(label). label is an

already-translated title. AppProviders mounts the listener

(WriteFailureToaster); an app without it, or with toaster={null}, renders

WriteFailureToaster from @agent-native/toolkit/app/shared itself.

  1. If no client API exists, add the narrowest helper at the boundary.

templates/<app>/app/lib/*, or an existing local client module.

entry for their domain. Never recommend the deprecated broad

@agent-native/core/client barrel in new code.

not scattered through components or docs.

shape, and any sync invalidation.

  1. Teach the helper, not the route.

Docs, skills, examples, and generated code should show:

```ts

await setClientAppState("selection", selection, { keepalive: true });

```

not:

```ts

await fetch("/_agent-native/application-state/selection", {

method: "PUT",

body: JSON.stringify(selection),

});

```

Exceptions

Raw route calls are acceptable only inside low-level client helpers or for

route-shaped protocols that cannot be hidden cleanly:

client methods

Even for exceptions, prefer a named helper as soon as more than one caller needs

the behavior.

Don't

template /api/* calls directly in React components for normal app data,

actions, settings, or application state.

simpler; expose an action and call it with action hooks.

every component.

Related Skills

References

targets found when this rule was added.

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.