Skip to content

The Open Standard for Generative UI

3.6 KB
Raw Blame
---
title: "Grok Build"
---

<Steps>

<Step title="Install and sign in to Grok Build" source="https://github.com/thesysdev/openui/blob/main/examples/harnesses/grok-build/README.md">

Put the grok CLI on your PATH and run grok login. In non-interactive environments, set XAI_API_KEY instead.

```bash title="Terminal"
grok login
grok --version
# For a non-interactive environment, set XAI_API_KEY instead.

cd examples/harnesses/grok-build
pnpm install --ignore-workspace
cp .env.example .env.local
pnpm dev -- /absolute/path/to/your/project
```

</Step>

<Step title="Start the harness on a workspace" source="https://github.com/thesysdev/openui/blob/main/examples/harnesses/grok-build/src/lib/grok-build-acp.ts">

Launch the example with the project directory Grok Build should work in. It runs one grok agent stdio process and opens a separate ACP session for each OpenUI thread, with the OpenUI prompt added as session rules.

```ts title="src/lib/grok-build-acp.ts"
import { ClientSideConnection, ndJsonStream } from "@agentclientprotocol/sdk";
import { spawn } from "node:child_process";
import { Readable, Writable } from "node:stream";
// Builds the OpenUI Lang prompt; copy src/lib/openui-prompt.ts from the example.
import { localSystemPrompt } from "./openui-prompt";

// One long-lived Grok process for the workspace chosen at launch. --always-approve
// lets tools run unattended, so keep this harness local and single-user.
const cwd = process.env.GROK_BUILD_CWD ?? process.cwd();
const args = ["agent", "--always-approve", "--no-leader", "stdio"];
const child = spawn("grok", args, { cwd, stdio: ["pipe", "pipe", "pipe"] });
const stream = ndJsonStream(
  Writable.toWeb(child.stdin) as WritableStream<Uint8Array>,
  Readable.toWeb(child.stdout) as ReadableStream<Uint8Array>,
);
// `client` answers Grok's callbacks (next step). Initialize and authenticate the
// connection once; see GrokBuildACPClient.create() in the example.
const connection = new ClientSideConnection(() => client, stream);

// Each OpenUI thread gets a Grok session with the same UUID, and the OpenUI prompt
// rides along as the session's rules.
async function createSession(sessionId: string) {
  await connection.newSession({
    cwd,
    mcpServers: [],
    _meta: {
      clientIdentifier: "openui-grok-build-harness",
      rules: localSystemPrompt(),
      sessionId,
      yoloMode: true,
    },
  });
}
```

</Step>

<Step title="Answer questions and approve plans in the browser" source="https://github.com/thesysdev/openui/blob/main/examples/harnesses/grok-build/src/lib/grok-build-acp.ts">

Grok's clarifying questions and plan approvals appear as browser dialogs, and your answers go back over ACP so the same turn can continue.

```ts title="src/lib/grok-build-acp.ts"
import { RequestError, type Client } from "@agentclientprotocol/sdk";
// Holds each question or plan approval until the browser answers it.
// Copy src/lib/grok-build-interactions.ts from the example.
import { requestGrokBuildInteraction } from "./grok-build-interactions";

const client: Client = {
  // Grok sends x.ai/ask_user_question and x.ai/exit_plan_mode as blocking requests.
  // The browser long-polls GET /api/interactions?threadId=… and shows a dialog; its
  // POST /api/interactions answer resolves this promise, which ACP returns to Grok
  // so the same turn continues.
  extMethod: async (method, params) => {
    const interaction = requestGrokBuildInteraction(method, params);
    if (interaction) return interaction;
    throw RequestError.methodNotFound(method);
  },
  // Also implement requestPermission and sessionUpdate, which forwards reasoning,
  // text, and tool updates to the chat route; see the example.
};
```

</Step>

</Steps>