ctx.history 提供当前游戏的历史快照,也可以重放历史条目中的语音。

API

方法返回值说明
entries()HistoryEntry[]获取对话历史
choices()Record<string, number>获取选项选择结果
ifResults()Record<string, boolean>获取条件判断结果
inputs()Record<string, string>获取玩家输入结果
replayVoice(uri)void | Promise<void>重放指定语音
useSnapshot()HistorySnapshotReact Hook;订阅完整历史快照

案例:渲染最近一条对话

输入: 历史记录中最后一条为爱丽丝的语音台词。
function LatestHistoryLine() {
  const ctx = useExtensionContext();
  const { entries } = ctx.history.useSnapshot();
  const latest = entries.at(-1);

  if (!latest) return <span>暂无历史记录</span>;

  return (
    <button
      disabled={!latest.voiceUri}
      onClick={() => latest.voiceUri && void ctx.history.replayVoice(latest.voiceUri)}
    >
      {latest.name ?? "旁白"}{latest.text}
    </button>
  );
}
输出:
爱丽丝:明天见。
当条目包含 voiceUri 时按钮可用,点击后重放该条语音;历史为空时输出“暂无历史记录”。