# 消息记录 Transcript

从 `@ancrel/workspace/transcript` 导入 `Transcript`。每条消息包含稳定的 `id`、用于显示的 `role` 和可渲染的 `content`。数组顺序就是阅读顺序。组件在具名的日志区域内显示角色和正文，保留换行并换行显示长串文本。

```tsx
'use client'

import { useState } from 'react'
import { Button, EmptyState, LoadingState } from '@ancrel/ui'
import { SimpleMarkdown } from '@ancrel/workspace/markdown'
import { Transcript, type TranscriptMessage } from '@ancrel/workspace/transcript'
import '@ancrel/tokens/css'
import '@ancrel/ui/styles.css'
import '@ancrel/workspace/styles.css'

export default function App() {
  const [phase, setPhase] = useState<'empty' | 'streaming' | 'done'>('empty')
  const messages: TranscriptMessage[] = [
    { id: 'q', role: 'Reader', content: 'What changed?' },
    { id: 'trace', role: 'trace', content: 'Compared two revisions.' },
    { id: 'reply', role: 'Editor', content: phase === 'done' ? '**Added** an index.' : 'Comparing…' },
  ]
  return <>
    <Button disabled={phase === 'done'} onClick={() => setPhase(phase === 'empty' ? 'streaming' : 'done')}>
      {phase === 'empty' ? 'Start review' : 'Finish review'}
    </Button>
    {phase === 'empty' ? <EmptyState title="Start a review to see the conversation" /> :
      <Transcript label="Review conversation" messages={messages} renderMessage={message =>
        typeof message.content === 'string'
          ? <SimpleMarkdown source={message.content} streaming={phase === 'streaming' && message.id === 'reply'} />
          : message.content} />}
    {phase === 'streaming' && <LoadingState label="Receiving reply" inline />}
  </>
}
```

Expected:

Start review opens the conversation and displays Receiving reply. Expand trace to read the comparison. Finish review replaces the partial reply with formatted text and removes the streaming indicator.

## 正文、轨迹与状态

默认把字符串作为文本渲染。使用 `renderMessage` 可以按消息选择正文组件，例如用 `@ancrel/workspace/markdown` 的 `SimpleMarkdown` 显示 Markdown。角色为 `trace`、`reasoning` 或 `tool` 的消息默认折叠；用 Tab 聚焦摘要，再用 Enter 或空格展开。

空数组默认显示 No messages。示例在空对话时提供本地化的 `EmptyState`，在接收回复期间于记录旁显示 `LoadingState`。要更新同一条回复，保留消息 ID，在新的数组中替换正文。

## 滚动

在高度受限的父容器中，记录首次渲染时滚动到底部。读者向上滚动后，新增消息不会打断阅读。回到底部即可继续跟随新消息；只有记录自身的滚动容器会移动。

| `Prop` | `Type` | `Required` | `Default` |
| - | - | - | - |
| `className` | `string` | `false` | `—` |
| `key` | `React.Key \| null \| undefined` | `false` | `—` |
| `label` | `string` | `false` | `—` |
| `messages` | `readonly TranscriptMessage[]` | `true` | `—` |
| `ref` | `React.Ref<HTMLDivElement> \| undefined` | `false` | `—` |
| `renderMessage` | `((message: TranscriptMessage) => React.ReactNode) \| undefined` | `false` | `—` |

```ts
import * as React from 'react';
import type { EventListProps, TranscriptProps } from './types.js';
export type * from './types.js';
export { groupEvents } from './group-events.js';
export { createRafEventBuffer } from './buffer.js';
export declare const EventList: React.ForwardRefExoticComponent<EventListProps & React.RefAttributes<HTMLDivElement>>;
export declare const Transcript: React.ForwardRefExoticComponent<TranscriptProps & React.RefAttributes<HTMLDivElement>>;
```

* default — dark/compact, dark/comfortable, light/compact, light/comfortable
* focus-visible — dark/compact, dark/comfortable, light/compact, light/comfortable
