Skip to main content

AI kit

The AI kit is a package of its own, @allxsmith/bestax-ai, for building the interface of an AI app with bestax-bulma. Its components arrive one at a time, and the first is Markdown, which renders a model's reply as it streams in. The reply comes out as React elements, never as HTML, with its code in bestax-bulma's CodeBlock and its tables in Table. The AI kit overview lists what's there so far and links the plan for what comes next.

function example() {
  const reply = `## Debouncing a search box

Wait until the typing stops, then search:

~~~ts
function debounce(run: () => void, ms: number) {
  let timer: ReturnType<typeof setTimeout>;
  return () => {
    clearTimeout(timer);
    timer = setTimeout(run, ms);
  };
}
~~~

- Pick a delay that feels **quick** but skips most keystrokes
- Clear the timer when the component _unmounts_`;
  const [length, setLength] = useState(reply.length);

  useEffect(() => {
    if (length >= reply.length) return;
    const timer = setTimeout(() => setLength(n => n + 4), 30);
    return () => clearTimeout(timer);
  }, [length, reply.length]);

  return (
    <>
      <Button size="small" mb="3" onClick={() => setLength(0)}>
        Stream it again
      </Button>
      <Markdown
        content={reply.slice(0, length)}
        isStreaming={length < reply.length}
        showCaret
      />
    </>
  );
}


Installation​

pnpm add @allxsmith/bestax-ai

The kit takes React, React DOM and bestax-bulma as peers, the copies your app already has, and installs nothing else. It has no stylesheet of its own to import either: it renders bestax-bulma's components, so the bestax-bulma stylesheet your app loads styles it.

import { Markdown } from '@allxsmith/bestax-ai';

What It Holds To​

The kit is built to a few decisions, which the overview sets out. In short:

  • Replies stay text. Whatever a model writes renders as React elements, never as HTML, since a prompt injection can put anything in a reply.
  • Your keys stay on your server. A provider's API key belongs on your server, and no example in these docs puts one in the browser.
  • Accessible by default. A reply is marked busy while it streams, rather than read out token by token. Markdown's Accessibility section covers the rest.

Rendering a Reply​

Markdown takes the reply so far as content, and isStreaming while more is on its way. It's written for streaming, so half-written markup waits until it's whole rather than flashing on screen, and the markdown it reads is CommonMark plus GitHub's tables, strikethrough, task lists and bare-URL links. A few props you'll likely reach for in a chat:

  • topHeadingAs sets the level a reply's headings start at, so they sit below your page's own. Heading Levels shows it.
  • allowUrl checks each address a reply links to or loads, after Markdown's own checks, and showImages turns on images, which are off by default. Links and Images say how, and Security says what Markdown refuses whatever you set.
  • components renders elements with your own components, by the element's name, such as pre for code blocks. Your Own Components shows how.

The Pieces in bestax-bulma​

Some of what an AI interface needs isn't specific to AI, so it lives in bestax-bulma, where any app can use it.

A Composer That Grows​

TextArea takes hasAutoSize, which grows the field with its text from minRows up to maxRows, then scrolls, so a composer can start at one line. Here it's paired with Markdown, which renders what you type.

function example() {
  const [draft, setDraft] = useState(
    'Try **bold**, a `code` span, or a list:\n\n- one\n- two'
  );

  return (
    <>
      <TextArea
        label="Message"
        hasAutoSize
        minRows={1}
        maxRows={6}
        value={draft}
        onChange={e => setDraft(e.target.value)}
      />
      <Box>
        <Markdown content={draft} size="small" />
      </Box>
    </>
  );
}

TextArea's Auto Size section says where the bounds come from and what stops the field growing.

Code in a Reply​

A reply's fenced code renders in CodeBlock, with the fence's language as its label and a button that copies it. While a block is still streaming in, CodeBlock holds the copy button until the code is whole. bestax ships no highlighter: you pass one to a block, or to ConfigProvider as codeHighlighter for the whole app, and Markdown's code blocks use that one too. CodeBlock's Highlighting section shows how to write one.

CodeBlock works on its own as well, such as for a change a model suggests:

<CodeBlock
  title="retry.ts"
  language="ts"
  showLineNumbers
  removedLines={[2]}
  addedLines={[3]}
  code={`export const retry = {
  attempts: 3,
  attempts: 5,
  delay: 250,
};`}
/>


API Pages​