Skip to content

The Open Standard for Generative UI

4.3 KB
Raw Blame
---
title: "Vercel Eve"
---

<Steps>

<Step title="Add the OpenUI instructions" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-eve/agent/instructions/openui.ts">

Add an Eve instruction that injects the generated OpenUI Lang prompt when each session starts, alongside the agent's own identity and tools.

```ts title="agent/instructions/openui.ts"
import { defineDynamic, defineInstructions } from "eve/instructions";
// Builds the OpenUI prompt; copy src/lib/cloud-prompt.ts from the example.
import { cloudInstructions } from "../../src/lib/cloud-prompt";

// Eve loads every file in agent/instructions. This one adds the OpenUI Lang
// prompt once, when a session starts.
export default defineDynamic({
  events: {
    "session.started": () =>
      defineInstructions({
        markdown: cloudInstructions(),
      }),
  },
});
```

</Step>

<Step title="Deliver turns over Eve's session protocol" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-eve/src/eve-chat.ts">

Send each turn through Eve's HTTP channel and store the session cursor per OpenUI thread, so the NDJSON event stream can resume where it stopped.

```ts title="src/eve-chat.ts"
import type { SessionState } from "eve/client";

// Eve's HTTP channel is served same-origin by withEve() in next.config.ts.
// `state` is this thread's saved cursor: { sessionId?, continuationToken?, streamIndex }.
async function openEveStream(state: SessionState, message: string, signal: AbortSignal) {
  // The first turn creates a session; later turns reuse it with the continuation token.
  const deliverPath = state.sessionId
    ? `/eve/v1/session/${encodeURIComponent(state.sessionId)}`
    : "/eve/v1/session";
  const delivered = await fetch(deliverPath, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ message, continuationToken: state.continuationToken }),
    signal,
  });
  const meta: { sessionId?: string; continuationToken?: string } = await delivered.json();
  const sessionId = meta.sessionId ?? state.sessionId;
  if (!sessionId) throw new Error("Eve did not return a session id.");

  // Read Eve's NDJSON event stream, resuming from the saved index.
  const startIndex = state.sessionId === sessionId ? state.streamIndex : 0;
  const response = await fetch(
    `/eve/v1/session/${encodeURIComponent(sessionId)}/stream?startIndex=${startIndex}`,
    { signal },
  );
  const continuationToken = meta.continuationToken ?? state.continuationToken;
  return { response, sessionId, continuationToken, startIndex };
}
```

</Step>

<Step title="Map events with eveAdapter()" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/vercel-eve/src/eve-chat.ts">

Use eveAdapter() from @openuidev/react-headless as the llm's stream protocol. It converts Eve text, tool call, and failure events into AG-UI for AgentInterface or your own UI built on ChatProvider.

```ts title="src/eve-chat.ts"
import { eveAdapter, type ChatLLM } from "@openuidev/react-headless";
import type { HandleMessageStreamEvent, SessionState } from "eve/client";

// openEveStream() is the previous step. The example's src/eve-chat.ts also has
// loadSession/saveSession (one cursor per thread) and latestUserText().
let active: { threadId: string; cursor: SessionState };

export const llm: ChatLLM = {
  send: async ({ messages, threadId, signal }) => {
    const state = loadSession(localStorage, threadId);
    const opened = await openEveStream(state, latestUserText(messages), signal);
    const { sessionId, continuationToken, startIndex: streamIndex } = opened;
    active = { threadId, cursor: { sessionId, continuationToken, streamIndex } };
    return opened.response;
  },
  streamProtocol: eveAdapter({
    // eveAdapter() maps Eve events to AG-UI. onEvent also sees every raw event,
    // so count them to keep this thread's cursor current.
    onEvent: (event: HandleMessageStreamEvent) => {
      active.cursor.streamIndex += 1;
      // Completed sessions start fresh next turn; waiting or failed ones resume.
      if (event.type === "session.completed") {
        saveSession(localStorage, active.threadId, { streamIndex: 0 });
      } else if (event.type === "session.waiting" || event.type === "session.failed") {
        saveSession(localStorage, active.threadId, active.cursor);
      }
    },
  }),
};
```

</Step>

</Steps>