# OpenCode

Render an OpenCode session with its questions, permissions, and tool results.

## Load an OpenCode session

[Install Ghost UI and its view dependencies](/libraries/ai#installation), then add the OpenCode SDK.

```bash
npm install @opencode-ai/sdk@1.19.5
```

Use the SDK’s /v2 client with your authenticated OpenCode server. Keep the connection, workspace, and session in your app.

```tsx
import { createOpencodeClient, type OpencodeClient } from "@opencode-ai/sdk/v2";
import { normalizeOpenCodeMessages } from "@collectivecreation/ghost-ui/chat-normalizers";
import { Message, MessageBody } from "@collectivecreation/ghost-ui/components/message";
import { MessageParts } from "@collectivecreation/ghost-ui/components/message-parts";
export const createClient = (baseUrl: string) => createOpencodeClient({ baseUrl });
export async function loadSession(client: OpencodeClient, sessionID: string) {
  const result = await client.session.messages({ sessionID }, { throwOnError: true });
  if (!result.data) throw new Error("Session messages were not returned.");
  return result.data;
}
type Records = Awaited<ReturnType<typeof loadSession>>;
export function OpenCodeTranscript({ records }: { records: Records }) {
  const messages = normalizeOpenCodeMessages(records);
  return <>{messages.map(message => <Message key={message.id} actor={message.actor}><MessageBody>
    <MessageParts parts={message.parts} />
  </MessageBody></Message>)}</>;
}
export async function sendPrompt(client: OpencodeClient, sessionID: string, text: string) {
  await client.session.prompt({ sessionID, parts: [{ type: "text", text }] }, { throwOnError: true });
  return loadSession(client, sessionID);
}

```

This first integration is request/response: load the session, render it, then replace the records after sending. For streaming, subscribe through native client.event.subscribe in the application session owner and update its cache by native session/message/part IDs. Project that cache; do not start a second Ghost event reducer or refetch every child session for each token.

## Native controls

```tsx
import type { OpencodeClient } from "@opencode-ai/sdk/v2";
export function cancelSession(client: OpencodeClient, sessionID: string) {
  return client.session.abort({ sessionID }, { throwOnError: true });
}
export function answerPermission(client: OpencodeClient, requestID: string, reply: "once" | "always" | "reject") {
  return client.permission.reply({ requestID, reply }, { throwOnError: true });
}
export function forkSession(client: OpencodeClient, sessionID: string, messageID: string) {
  return client.session.fork({ sessionID, messageID }, { throwOnError: true });
}

```

Answer questions through OpenCode’s question API and use its permission API for permission requests. Your app decides whether to offer “always.” Keep fork, revert, and unrevert on the native session.

## Extension and troubleshooting

The projection preserves tool call IDs, errors and native message metadata. Patch, subtask and future parts stay explicit unknown content until you provide a deliberate renderer. Read child sessions only when your product needs them; normalization performs no network reads.

If events from another workspace appear, fix native subscription/cache scope before normalizing. If the UI never streams, confirm you implemented the event owner rather than only the first request/response recipe. See [OpenCode SDK documentation](https://opencode.ai/docs/sdk/) for event types and the [external-store bridge](/libraries/ai-runtime#external-store) for connecting an existing cache.
