Client-side custom actions

Register a browser function with registerAction so your agent can read the cart, check a session or call a system that only your page can reach.

Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.

A client-side custom action is a function that runs in the visitor's browser. The agent decides when it is needed, the embed script calls your function and the result goes back to the agent. Use it for facts only your page knows, such as the current cart, the signed-in plan or a value in local storage.

How it works

  1. The visitor asks something the agent can answer with a browser action.
  2. The agent calls the action by name.
  3. The embed script runs the handler you registered with registerAction.
  4. The handler returns a result and the agent continues its answer with it.

The same pattern exists in the REST API and in the mobile SDKs as ClientTool.

Register the handler

window.askocto.registerAction("getCartItems", async (args) => {
  const cart = JSON.parse(localStorage.getItem("cart") || "[]")
  const items = args.includeTotals
    ? cart.map((i) => ({ name: i.name, qty: i.qty, price: i.price }))
    : cart.map((i) => ({ name: i.name, qty: i.qty }))
  return { count: cart.length, items }
})

The handler receives the arguments the agent chose and returns any JSON value. It can be asynchronous. Register every action right after the embed script loads, before the visitor can chat.

Connect it in the dashboard

  1. Open your agent and go to Actions.
  2. Add a Custom action and name it getCartItems, exactly as in your code.
  3. Under Runs in, choose Visitor's browser.
  4. Describe in plain words when the agent should use it and which arguments it takes.
  5. Select Save.

Add a procedure step that names the action if the agent should always call it in a given situation. See Procedures.

You should see the agent call the action when you ask a matching question on your site, and answer using the returned values. Because the handler runs in the visitor's browser, test on a page that loads your script, not only in the dashboard.

Write safe handlers

  • Treat args as untrusted input. The agent builds it from the conversation.
  • Return small objects with only the facts the agent needs.
  • Catch errors and return { error: "Cart is empty." } so the agent can explain the problem in words.
  • Never return secrets, tokens or full personal records.

Warning: Do not register actions that change money or personal data without a confirmation step on your page. The agent can call any action you register.

Something unclear or missing? Tell us and we will fix the page.