Skip to content

The Open Standard for Generative UI

3.0 KB
Raw Blame
---
title: "Lynx"
---

<Steps>

<Step title="Create the library" source="https://github.com/lynx-family/lynx-stack/blob/main/packages/genui/openui/README.md">

Build a component library with createOpenUiLibrary() from @lynx-js/genui/openui, and import the theme stylesheet from @lynx-js/genui/openui/styles/theme.css. The renderer's own CSS ships with the renderer module.

```ts title="src/library.ts"
import { createOpenUiLibrary } from "@lynx-js/genui/openui";

// Theme tokens for the built-in components.
import "@lynx-js/genui/openui/styles/theme.css";

// The built-in components; pass { components: [...] } to add your own ReactLynx ones.
// Create it once: the renderer keeps its streaming parser while the library is unchanged.
export const library = createOpenUiLibrary();
```

</Step>

<Step title="Generate the agent instructions" source="https://github.com/lynx-family/lynx-stack/blob/main/packages/genui/openui/docs/system-prompts.md">

On the server, build the system prompt with buildOpenUiSystemPrompt() from @lynx-js/genui/openui/prompt.

```ts title="server/openuiPrompt.ts"
import { buildOpenUiSystemPrompt } from "@lynx-js/genui/openui/prompt";

// Teaches the model OpenUI Lang and the built-in components. Send it as the system
// instruction, then return the model's raw text to the app.
export const systemPrompt = buildOpenUiSystemPrompt({
  appendix: "Use only the tools explicitly provided by this application.",
  // Custom client components go here too, as headless definitions in `components`.
});
```

</Step>

<Step title="Render with OpenUiRenderer" source="https://lynxjs.org/next/react/genui/openui.html#stream-model-output">

Pass the full text received so far, not just the latest delta, to OpenUiRenderer with the library. Handle host-side effects such as navigation in onAction.

```tsx title="src/StreamingSurface.tsx"
import { BuiltinActionType, OpenUiRenderer } from "@lynx-js/genui/openui";
import { useEffect, useState } from "@lynx-js/react";
import { library } from "./library"; // The library from step 1.

// `source` yields the model's text deltas from your Agent transport.
export function StreamingSurface({ source }: { source: AsyncIterable<string> }) {
  const [response, setResponse] = useState("");
  const [isStreaming, setIsStreaming] = useState(false);
  useEffect(() => {
    async function consume() {
      setResponse("");
      setIsStreaming(true);
      try {
        // Append each delta: the renderer needs the full text received so far.
        for await (const delta of source) setResponse((current) => current + delta);
      } finally {
        setIsStreaming(false);
      }
    }
    consume();
  }, [source]);

  return (
    <OpenUiRenderer
      response={response}
      library={library}
      isStreaming={isStreaming}
      onAction={(event) => {
        if (event.type === BuiltinActionType.OpenUrl) {
          // Validate event.params.url, then open it with the host navigation API.
        }
      }}
    />
  );
}
```

</Step>

</Steps>