Skip to content

The Open Standard for Generative UI

2.7 KB
Raw Blame
---
title: "Vue 3"
---

<Steps>

<Step title="Write the component as an SFC" source="https://www.openui.com/docs/api-reference/vue-lang">

A registered component receives the parsed values in a props prop and a renderNode function. Declare both with defineProps, otherwise Vue forwards renderNode to the root element as an attribute.

```vue title="Greeting.vue"
<script setup lang="ts">
import type { RenderNodeResult } from "@openuidev/vue-lang";

defineProps<{
  props: { name?: string };
  renderNode: (value: unknown) => RenderNodeResult;
}>();
</script>

<template>
  <p>Hello, {{ props.name }}!</p>
</template>
```

</Step>

<Step title="Register it and generate the prompt" source="https://www.openui.com/docs/api-reference/vue-lang">

Pair the SFC with a Zod schema in defineComponent, collect the definitions with createLibrary, and call library.prompt(). A Nuxt server route cannot import .vue files, so share the schemas there and generate the prompt with @openuidev/lang-core ^0.4.

```ts title="library.ts"
import { createLibrary, defineComponent } from "@openuidev/vue-lang";
import { z } from "zod/v4";
// The SFC from the previous step.
import GreetingComponent from "./Greeting.vue";

const Greeting = defineComponent({
  name: "Greeting",
  description: "Greet the user by name.",
  props: z.object({ name: z.string() }),
  component: GreetingComponent,
});

export const library = createLibrary({
  components: [Greeting],
  root: "Greeting",
});

// The system prompt to send with each model request.
export const systemPrompt = library.prompt({
  preamble: "You are a helpful assistant.",
  examples: ['root = Greeting("Alice")'],
});
```

</Step>

<Step title="Render the stream with Renderer" source="https://www.openui.com/docs/api-reference/vue-lang">

Bind the accumulated text, the library and the streaming flag with :response, :library and :is-streaming. Renderer callbacks are props, not emitted events, so bind onAction with :on-action and send continuation actions back to the model as a new turn.

```vue title="AssistantMessage.vue"
<script setup lang="ts">
import { BuiltinActionType, Renderer, type ActionEvent } from "@openuidev/vue-lang";
// The library from library.ts.
import { library } from "./library";

// The model's OpenUI Lang output so far, and whether it is still streaming.
defineProps<{ response: string | null; isStreaming: boolean }>();

function handleAction(event: ActionEvent) {
  if (event.type === BuiltinActionType.ContinueConversation && event.humanFriendlyMessage) {
    // Send event.humanFriendlyMessage to your chat as the next user message.
  }
}
</script>

<template>
  <Renderer
    :response="response"
    :library="library"
    :is-streaming="isStreaming"
    :on-action="handleAction"
  />
</template>
```

</Step>

</Steps>