AncrelDocumentationv0.3.0

Search documentation

    Documentation / frontend

    消息记录 Transcript

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

    Live example

    Open playground ↗
    Start a review to see the conversation

    App.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 behavior

    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,在新的数组中替换正文。

    滚动

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

    Props

    PropTypeRequiredDefault
    classNamestringNo—
    keyReact.Key | null | undefinedNo—
    labelstringNo—
    messagesreadonly TranscriptMessage[]Yes—
    refReact.Ref<HTMLDivElement> | undefinedNo—
    renderMessage((message: TranscriptMessage) => React.ReactNode) | undefinedNo—

    State matrix

    StateTheme / density
    defaultdark/compact, dark/comfortable, light/compact, light/comfortable
    focus-visibledark/compact, dark/comfortable, light/compact, light/comfortable

    Constraints

    • Use unique, stable message IDs and pass updated arrays when content changes.
    • Give the pane a bounded height to follow incoming text while preserving a reader’s scroll position.

    Anti-patterns

    • Using array positions as message IDs during insertions.
    • Passing untrusted HTML to a renderer without sanitizing it.