• 简体中文
  • ActivityBadge

    用角色加标签显示智能体正在做什么。

    搜索中搜索中搜索中搜索中阅读中阅读中阅读中阅读中书写中书写中书写中书写中工作中工作中工作中工作中上传中思考中思考中聆听中等待你的回复
    import { CharacterStageProvider, ActivityBadge, TOOL_ACTIVITIES } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            {Object.keys(TOOL_ACTIVITIES).map((tool) => (
              <ActivityBadge key={tool} agent={{ id: tool }} tool={tool} />
            ))}
          </div>
        </CharacterStageProvider>
      );
    }

    状态和标签出自同一来源

    工具名同时决定动画状态和标签。状态取自引擎的 stateForTool() 映射表,Claude Code 和 Cursor 适配器用的也是这张表。因此,不论宿主是已经接入情绪协议,还是只知道工具名,脸上显示的内容都是一致的。

    activityForTool('web_search');
    // → { labelKey: 'activity.searching', state: 'searching' }

    遇到未知工具时,会回退到通用的工作状态,而不是什么都不渲染,所以新增的工具不会出现空白。

    添加工具

    import { TOOL_ACTIVITIES } from '@aora-bot/react';
    
    TOOL_ACTIVITIES.deploy = { labelKey: 'activity.running', state: 'working' };

    群组

    工作中
    import { CharacterStageProvider, GroupActivityBadge } from '@aora-bot/react';
    
    const members = [{ id: 'ada' }, { id: 'grace' }, { id: 'alan' }];
    
    export default function Demo() {
      return (
        <CharacterStageProvider>
          <GroupActivityBadge members={members} tool="run" />
        </CharacterStageProvider>
      );
    }

    同时显示每个成员,四人的群组就会变成一堵角色墙。所以这一行改为轮播:宽度始终只有一个角色,又能看出有哪些成员参与。群组徽标为 GROUP_BADGE_PX(24 px),单个徽标为 ACTIVITY_BADGE_PX(32 px);每隔 GROUP_CYCLE_MS(2.4 秒)切换到下一位成员,墨色的过渡时长为 600 ms。轮播间隔和徽标尺寸可以分别用 cycleMs 和 sizePx 修改。

    语言

    标签由语言 Provider提供,文字会跟随上下文的语言,不需要额外传 prop。

    搜索中搜索中
    import { LocaleProvider, ActivityBadge } from '@aora-bot/react';
    
    export default function Demo() {
      return (
        <div className="site-demo site-demo--column">
          <ActivityBadge agent={{ id: 'en' }} tool="search" />
          <LocaleProvider locale="zh-CN">
            <ActivityBadge agent={{ id: 'zh' }} tool="search" />
          </LocaleProvider>
        </div>
      );
    }

    用事实而不是工具名

    activityMode="tool" 是默认值:由调用方给出工具名,状态和标签都跟着工具名走。activityMode="facts" 则读取共享的角色指令注册表,这样同一个智能体的对话行和名册头像,对它在做什么就不会出现分歧。这一行挂载期间,facts 会为该智能体发布 { running, composing, activity }(按值比较,所以直接传内联对象也可以)。surface 说明这一行画在哪里:对话行用 chat,这里“正在撰写”正是要表现的重点;同样的事实在 avatar 上会解析为 idle,因为二十张脸每隔几百毫秒就在两个状态之间闪动,只会造成干扰。解析由引擎的 characterStateForRoster(facts, surface) 完成。

    工作中
    同样的事实,avatar 面
    import { useState } from 'react';
    import { CharacterStageProvider, ActivityBadge, AgentPortrait, useMarkCommand } from '@aora-bot/react';
    
    const agent = { id: 'nova', shape: 'hex', color: 'violet' } as const;
    
    function RosterAvatar() {
      const snapshot = useMarkCommand(agent.id, 'avatar');
      return <AgentPortrait agent={agent} state={snapshot.state} size="lg" />;
    }
    
    export default function Demo() {
      const [composing, setComposing] = useState(true);
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            <ActivityBadge
              agent={agent}
              activityMode="facts"
              facts={{ running: true, composing, activity: 'writing' }}
              surface="chat"
            />
            <div className="site-demo">
              <RosterAvatar />
              <span className="site-demo__label">同样的事实,avatar 面</span>
            </div>
            <button className="site-btn" onClick={() => setComposing((c) => !c)}>
              composing: {String(composing)}
            </button>
          </div>
        </CharacterStageProvider>
      );
    }

    标签、气泡、可见性

    showLabel 控制标签的显示方式:always 常显,hover 仅在悬停时显示,never 不显示。即使是 never,标签仍保留在无障碍树中,因为标签是屏幕阅读器唯一能读到的内容。bubble 把标签画成对话气泡,而不是纯文字,适合标签在角色旁边而不是下方的布局。

    visible={false} 会先播放退场动画再卸载,而不是立刻卸载。如果在标志位翻转的同一时刻卸载,这一行会直接消失而不是退场,在连续的对话中看起来像故障。

    搜索中阅读中工作中
    import { useState } from 'react';
    import { CharacterStageProvider, ActivityBadge } from '@aora-bot/react';
    
    export default function Demo() {
      const [visible, setVisible] = useState(true);
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            <ActivityBadge agent={{ id: 'bubble' }} tool="search" bubble />
            <ActivityBadge agent={{ id: 'hover' }} tool="read_file" showLabel="hover" />
            <ActivityBadge agent={{ id: 'leaving' }} tool="run" visible={visible} />
            <button className="site-btn" onClick={() => setVisible((v) => !v)}>
              {visible ? '隐藏最后一行' : '再显示出来'}
            </button>
          </div>
        </CharacterStageProvider>
      );
    }

    戳一下

    点击角色时会调用 onPoke;传了 onPoke,角色才会渲染成真正的按钮。没有它时,点击不会有任何效果:为了一个屏幕阅读器用户看不到的小动画,给对话记录的每一行都加一个 Tab 停靠点,并不值得。戳一下也会通过共享指令发出,所以该智能体的其他角色都会有反应;无法单独动作的镜像角色,则由外层容器代为跳一下。

    思考中戳了 0 次
    import { useState } from 'react';
    import { CharacterStageProvider, ActivityBadge } from '@aora-bot/react';
    
    export default function Demo() {
      const [pokes, setPokes] = useState(0);
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            <ActivityBadge
              agent={{ id: 'poke', shape: 'star', color: 'yellow' }}
              tool="think"
              onPoke={() => setPokes((n) => n + 1)}
            />
            <span className="site-demo__label">戳了 {pokes} 次</span>
          </div>
        </CharacterStageProvider>
      );
    }

    在场 hook

    组件内部用到的两个 Hook 也对外导出,供自己绘制对话行的宿主使用。

    useActivityStatus(visible, { exitMs }) 返回 { mounted, leaving }:mounted 在退场动画期间保持为 true,leaving 标出应当加上离场类名的那几帧。exitMs 默认为 ACTIVITY_EXIT_MS(140 ms),在“减少动态效果”模式下为 0。状态切换在渲染期间完成,而不是放在 effect 里,所以隐藏该行的那一帧,退场动画就已经显示在屏幕上。

    useArrival(agentId) 返回 { isFirst, state }:登场动画播放期间,state 为 'spawning',之后为 null,调用方此时应改用实际状态。哪些智能体已经登场,记录在模块级别而不是 context 中:滚动离开时卸载、滚回来时重新挂载的行,不应该再登场一次。resetSpawnEntrances() 会清除所有登场记录,用于测试和会话重置。

    首次出现,在场
    import { useState } from 'react';
    import { CharacterStageProvider, StagedMark, useActivityStatus, useArrival } from '@aora-bot/react';
    
    function Row({ id, visible }: { id: string; visible: boolean }) {
      const presence = useActivityStatus(visible);
      const spawn = useArrival(id);
      if (!presence.mounted) return null;
      return (
        <span className={`aora-activity${presence.leaving ? ' aora-activity--leaving' : ''}`}>
          <StagedMark shape="pebble" color="green" state={spawn.state ?? 'idle'} sizePx={28} />
          <span className="site-demo__label">
            {spawn.isFirst ? '首次出现' : '之前见过'},{presence.leaving ? '离场中' : '在场'}
          </span>
        </span>
      );
    }
    
    export default function Demo() {
      const [visible, setVisible] = useState(true);
      return (
        <CharacterStageProvider>
          <div className="site-demo site-demo--column">
            <Row id="presence-demo" visible={visible} />
            <button className="site-btn" onClick={() => setVisible((v) => !v)}>
              切换
            </button>
          </div>
        </CharacterStageProvider>
      );
    }

    无障碍

    标签是真正的文字,所以即使看不到动画,也能读出智能体的状态。凡是角色要传达信息的地方,都建议照这个做法处理,详见无障碍。

    API

    属性类型默认值说明
    agent*AgentLike——
    activityModeActivityMode'tool'Where the animation state comes from. Defaults to `tool`.
    bubblebooleanfalseDraw the label as a speech bubble rather than as bare text.
    classNamestring——
    enteringbooleanfalsePlay the arrival animation.
    face"symbol" | "capsule" | "cute" | "eyes" | "line" | "brow" | "glossy" | "sparkle" | "button" | "shades" | "droopy" | "bigmouth" | "pie" | "screen" | "teardrop" | "specs"—Face style, overriding what the agent identity resolves to.
    factsRosterFacts—Facts to publish for this agent while the row is mounted, so every other mark for it follows along. Compared by value, not identity.
    inkCssstring—CSS colour for the mark's ink, instead of the agent's palette colour.
    lineWeightnumber—Stroke width multiplier for the character.
    nose"slant" | "angular" | "hook" | "long" | "curve" | "bow" | null—Nose, overriding the identity. `null` draws no nose.
    onPoke((agent: AgentLike) => void)—Called when the character is clicked. Its presence also makes the mark a real button.
    pokeLabelstring—Accessible name for the poke control.
    showLabelActivityLabelMode'always'Show the label always, only while the row is hovered, or never.
    sizePxnumberACTIVITY_BADGE_PX—
    styleCSSProperties—Applied to the row, not to the character.
    surfaceRosterSurface'chat'Surface used to derive the state from facts. A conversation row is `chat`.
    textstring—Override the label text.
    toolstring | null—Tool the agent is running. Drives both the label and the animation.
    visiblebooleantrueUnmount with the exit animation rather than instantly.

    GroupActivityBadge

    属性类型默认值说明
    members*AgentLike[]——
    classNamestring——
    cycleMsnumberGROUP_CYCLE_MS—
    face"symbol" | "capsule" | "cute" | "eyes" | "line" | "brow" | "glossy" | "sparkle" | "button" | "shades" | "droopy" | "bigmouth" | "pie" | "screen" | "teardrop" | "specs"—Face style for whichever member is showing, overriding their identity.
    lineWeightnumber—Stroke width multiplier for the character.
    nose"slant" | "angular" | "hook" | "long" | "curve" | "bow" | null—Nose for whichever member is showing. `null` draws no nose.
    onPoke((agent: AgentLike) => void)——
    showLabelActivityLabelMode——
    sizePxnumberGROUP_BADGE_PX—
    styleCSSProperties—Merged over the ink custom properties the rotation animates, so a host can position the cluster without losing the crossfade.
    toolstring | null——

    接下来