Skip to content

The Open Standard for Generative UI

3.5 KB
Raw Blame
---
title: "Google ADK"
---

<Steps>

<Step title="Define the agent" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/google-adk/src/agent.ts">

Create an ADK Agent with FunctionTool tools and use the generated OpenUI system prompt as its instruction. The example calls OpenUI Cloud through adk-llm-bridge's Custom() model adapter.

```ts title="src/agent.ts"
import { Agent, FunctionTool } from "@google/adk";
import { Custom } from "adk-llm-bridge";
import { z } from "zod";
// Builds the OpenUI system prompt from your component library.
import { cloudInstructions } from "@/lib/cloud-prompt";

const getWeather = new FunctionTool({
  name: "get_weather",
  description: "Get the current weather for a given city.",
  parameters: z.object({ city: z.string() }),
  execute: ({ city }) => {
    // Call your weather API here.
  },
});

export function createAgent() {
  return new Agent({
    name: "openui-adk-agent",
    model: Custom("google/gemini-3.6-flash-free", {
      name: "openui-adk-agent",
      baseURL: "https://api.thesys.dev/v1/embed",
      apiKey: process.env.THESYS_API_KEY,
    }),
    instruction: cloudInstructions(),
    tools: [getWeather],
  });
}
```

</Step>

<Step title="Bridge events to AG-UI" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/google-adk/src/app/api/chat/route.ts">

Run the agent with a Runner and pass its events through the example's adkToAguiEvents() helper, which turns ADK text, functionCall and functionResponse parts into AG-UI events. Stream them to the browser as server-sent events.

```ts title="src/app/api/chat/route.ts"
import { InMemorySessionService, Runner, StreamingMode } from "@google/adk";
import { createAgent } from "@/agent";
// Maps ADK events to AG-UI events. Copy src/lib/adk-to-agui.ts from the example.
import { adkToAguiEvents } from "@/lib/adk-to-agui";

const runner = new Runner({
  appName: "openui-adk-chat",
  agent: createAgent(),
  sessionService: new InMemorySessionService(),
});

export async function POST(req: Request) {
  // Read the last user message into `prompt`, and find or create the ADK
  // session (`sessionId`) for this chat's `threadId`.

  const events = runner.runAsync({
    userId: "demo-user",
    sessionId,
    newMessage: { parts: [{ text: prompt }] },
    runConfig: { streamingMode: StreamingMode.SSE },
  });

  const encoder = new TextEncoder();
  const stream = new ReadableStream({
    async start(controller) {
      const runId = crypto.randomUUID();
      for await (const event of adkToAguiEvents(events, { threadId, runId })) {
        controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`));
      }
      controller.close();
    },
  });

  return new Response(stream, { headers: { "Content-Type": "text/event-stream" } });
}
```

</Step>

<Step title="Connect your chat UI" source="https://github.com/thesysdev/openui/blob/main/examples/agent-frameworks/google-adk/src/app/page.tsx">

Create an llm with fetchLLM and agUIAdapter(), then pass it to AgentInterface for a ready-made chat app or to ChatProvider from @openuidev/react-headless for your own UI.

```tsx title="src/app/page.tsx"
"use client";

import { AgentInterface, agUIAdapter, fetchLLM } from "@openuidev/react-ui";
import { openuiLibrary } from "@openuidev/react-ui/genui-lib";

const llm = fetchLLM({ url: "/api/chat", streamAdapter: agUIAdapter() });

export default function Page() {
  return <AgentInterface llm={llm} componentLibrary={openuiLibrary} />;
}
```

</Step>

</Steps>