Custom Code

When no built-in action does what you need -- read a QR code from a photo, format a price your own way, call a device library -- write a custom action: your own code, in the target's real language, under a typed contract the rest of the design reads. Dart for Flutter; one TypeScript file for both Expo and the web. When no built-in widget draws what you need, write a custom widget (below).

The contract

A custom action is declared once for the app -- with New custom action in Code, or from the Inspector when you add Run a custom action to a button. Its contract is what every screen, Check and the AI read; the code is only ever behind it.

NameA name like readQrFromImage. Its file takes the name in lower case with underscores: read_qr_from_image.
DescriptionWhat it does, in a sentence. The AI reads it to know when to use it.
InputsEach with a type -- text, number, true/false, or date and time -- and whether it is required. A step gives each one a value: typed, from an input, a record, state or a calculation.
OutputsWhat it returns, each typed and each allowed to be empty. What follows reads them as the action’s result. An action may return nothing.
SamplesA value for each output, used by Preview (below).
PackagesThe pub or npm packages its code needs, each with a version (below).

A custom action returns, it doesn't steer: navigation, state and writes stay ordinary steps after it, reading its result. It may use the Koolbase SDK as the signed-in person; anything needing a secret belongs in a Function.

Writing it: the Code view

Code in the left rail lists the app's custom actions, each marked as matching its contract or with no code yet. Open one: the contract is above, the code below, opening on a stub with the right signature. Write its body and save (⌘S). The file is kept with the project, not the design, at the path the export uses.

Check reads the signature against the contract and says in words what differs -- a missing input, a wrong type, a return that isn't its result. Only the signature is checked; the full compile is when the app is built.

Open in two tabs and saved in one, the other is told the file changed since it was opened: Load theirs takes the saved one, Save mine over it keeps yours.

lib/custom/actions/add_tax.dart
Dart
import 'package:flutter/widgets.dart';

import '../../generated/custom/add_tax.g.dart';

Future<AddTaxResult> addTax(BuildContext context, {required num amount, num? rate}) async {
  return AddTaxResult(total: (amount * (1 + (rate ?? 0.1))).round());
}
src/custom/actions/add_tax.ts
TypeScript
import type { AddTaxInput, AddTaxResult } from '../../generated/custom/add_tax';

export async function addTax(input: AddTaxInput): Promise<AddTaxResult> {
  return { total: Math.round(input.amount * (1 + (input.rate ?? 0.1))) };
}

The types it is given and returns are generated from the contract, in lib/generated/custom/ (Flutter) and src/generated/custom/ (Expo and the web): change the contract and they follow on the next export.

When it can't

Say whyThrow KbCustomFailure with a message (and canRetry when trying again may work). On error reads its message; without On error the app shows it.
Anything elseAny other error is a failure too, with its own message.
30 secondsAn action that hasn’t answered in 30 seconds fails as “<name> took too long.”, and a retry is offered.
a refused card
TypeScript
throw new KbCustomFailure('Card declined', { canRetry: true });

Its button is busy while it runs, as for any action that waits.

Packages

Declare a package in the contract's Packages card with a version: ^7.5.0 for pub, ^1.2.0, ~0.21.3 or an exact version for npm. The export adds it to pubspec.yaml or package.json. One the app already uses at another version is refused, in words, until they match.

Check advisories looks each one up in the OSV database at the lowest version its range allows. A critical or high advisory stops the export; anything else is shown to know about. When the database can't be reached, that is said, and nothing is blocked.

A native Expo module

The export adds an npm package at the version you wrote. A native Expo module wants the version its Expo SDK supports: run npx expo install <name> in the exported app to take it.

Preview and the app

Preview runs no custom code. A custom action's samples are its result there, so what follows shows them: a message reading Total {t} shows the sample total. An output with no sample reads as empty. Preview carries on as if it succeeded.

The code runs in the exported app, and in Run. An app whose custom action has no code for its target, or code the project could not give, is refused at export by name.

Custom widgets

A custom widget is your own code that draws: a signature pad, a rating, a chart the built-in widgets don't draw. Its contract is declared once, with New custom widget in Code, and it is placed on a screen with Place on the screen.

PropsWhat it is given, each typed -- text, number, true/false, or date and time -- and required, or optional with a default. A placed one gives each a value, as an action’s input: typed, from state, a record, an input or a calculation.
EventsWhat it sends, each named on and a word (onRate) and carrying typed fields. A placed one runs steps for each; inside them, What it sent reads its fields.
SizeFills the width or a width of its own, and a height. A placed one may take another, as an image does.

A widget draws differently on each platform, so it has three files, not two: Dart for Flutter, React Native's View and Text for Expo, and the browser's elements for the web. An app needs only its own target's.

lib/custom/widgets/rating_stars.dart
Dart
class RatingStars extends StatelessWidget {
  const RatingStars({super.key, required this.value, this.max = 5, this.onRate});

  final num value;
  final num max;
  final void Function(RatingStarsOnRate event)? onRate;

  @override
  Widget build(BuildContext context) => Row(children: [
        for (var i = 1; i <= max; i++)
          IconButton(
            icon: Icon(i <= value ? Icons.star : Icons.star_border),
            onPressed: () => onRate?.call(RatingStarsOnRate(value: i)),
          ),
      ]);
}
src/custom/widgets/rating_stars.tsx
TypeScript
export function RatingStars(props: RatingStarsProps) {
  const stars = Array.from({ length: props.max }, (_, i) => i + 1);
  return (
    <View style={{ flexDirection: 'row' }}>
      {stars.map((i) => (
        <Pressable key={i} onPress={() => props.onRate?.({ value: i })}>
          <Text>{i <= props.value ? '★' : '☆'}</Text>
        </Pressable>
      ))}
    </View>
  );
}

Check compares its props and events with the contract. On the canvas and in Preview it is a labelled box at its size, and its events don't fire there; it draws in the exported app and in Run. A widget whose code fails while drawing shows Couldn't draw this in its place, and the rest of the app goes on.

With the AI

Describing a change can use a custom action the app declares, giving it its inputs and reading its outputs, and can place a custom widget the app declares, giving it its props and running steps for its events. It never declares either or writes their code: when a request needs one the app doesn't have, it builds the rest and says which to declare -- its name, and its inputs and outputs, or its props, events and size.