Skip to content

ui: 涨跌幅被渲染成 "++1.5%",正负号被叠加两次 #174

Closed

Description

@wxrbyte

Issue:涨跌幅被渲染成 ++1.5%,正负号被叠加两次

问题与复现

formatBlockChange 先取绝对值渲染,再自己补一个正负号;但 percent 单位走的是 i18n 的 formatPercent,它本身已经是带符号的格式化器,于是符号出现两次:

// packages/ui/src/components/chat/blocks/blockFormat.ts:14
export function formatBlockValue(value: number, unit: AnswerBlockUnit, currency?: string): string {
  switch (unit) {
    case 'percent':
      return formatPercent(value);          // ← 已带符号
    ...
  }
}

// packages/ui/src/components/chat/blocks/blockFormat.ts:28
export function formatBlockChange(value: number, unit: AnswerBlockUnit, currency?: string): string {
  const formatted = formatBlockValue(Math.abs(value), unit, currency);
  return `${value > 0 ? '+' : value < 0 ? '−' : ''}${formatted}`;   // ← 又加一次
}
// packages/i18n/src/format.ts:96 —— 注释明确写着 "Percentage with sign"
export function formatPercent(value: number | undefined, locale?: SupportedLocale): string {
  const sign = value > 0 ? '+' : '';
  return `${sign}${new Intl.NumberFormat(...).format(value / 100)}`;
}

反例(packages/ui/src/components/chat/blocks/blockFormat.ts,en-US):

调用 实际 预期
formatBlockChange(1.5, 'percent') "++1.5%" "+1.5%"
formatBlockChange(-1.5, 'percent') "−+1.5%" "−1.5%"
formatBlockChange(0, 'percent') "0%" "0%"(这一条本来就对)

price 单位不受影响(formatCurrency 不带符号),说明这是符号叠加而不是设计:同一个函数对 price 正常、对 percent 多一个符号。

影响

MetricGridBlockView 的涨跌行把它渲染给用户,报价回答里的 Change % 指标就落在这一行:

// packages/ui/src/components/chat/blocks/MetricGridBlockView.tsx:24,28
`${formatBlockChange(metric.change, metric.unit, metric.currency)} (${formatBlockChange(metric.changePercent, 'percent')})`
: formatBlockChange(metric.changePercent, 'percent')

而 answer-block-emitter 为每个报价生成的 metric_grid 里就有 { label: 'Change %', value: quote.changePercent, unit: 'percent' },所以 Copilot 每次回答行情都会显示 ++0.64% 这种字样。

现有测试没覆盖:packages/ui/src/components/chat/blocks/ 下只有 parseAnswerSegments*.test.ts,blockFormat.ts 没有任何测试。

预期与修复方向

percent 分支不走带符号的 formatPercent,而是把绝对值按比例交给 formatPercentRatio(1.5 → 0.015 → "1.5%"),符号由 formatBlockChange 统一补一次。不改动 @finagent/i18n 的 formatPercent 公共行为——它仍是仓库里「带符号的百分比格式化器」,其它调用方可能依赖这一点。

复现证据

  • 环境:Windows 11、Bun 1.4.2、上游 main aacf332c6987d78d06dee81cb10d3012d25103fd
  • 聚焦命令:bun test packages/ui/src/components/chat/blocks/blockFormat.test.ts
    • 修复前:2 pass / 2 fail(signs percent deltas exactly once、never emits two adjacent signs for any unit 失败)
    • 修复后:4 pass / 0 fail,49 expect() calls
  • bun test packages/ui --isolate 320 pass / 0 fail

范围

只改 formatBlockChange 的 percent 分支;formatBlockValue 的输出(含 percent 单位带符号这件事)保持不变,并有测试锁定。

Activity

  1. wxrbyte commented on Sep 23, 2026

    @wxrbyte
    ContributorAuthor

    /claim

  2. github-actions commented on Sep 23, 2026

    @github-actions

    @wxrbyte 已认领该 Issue,并已自动设为 Assignee。提交 PR 时请在描述中写 Closes #174。

  3. github-actions commented on Sep 30, 2026

    @github-actions

    @wxrbyte 这个 Issue 已认领超过 7 天,目前没有发现关联的 open PR,也没有看到最近 7 天的进度更新。

    如果仍在推进,请在 3 天内 留一条简短进度说明(包括正在等待的前置依赖也可以);如果已经有 PR,请贴出链接。若 3 天内没有回应,认领会自动释放,方便其他贡献者接手。

  4. github-actions commented on Oct 4, 2026

    @github-actions

    @wxrbyte 在提醒后 3 天内没有进度回应,且仍未发现关联的 open PR;本次认领已自动释放。Issue 保持开放,可以重新认领。

  5. removed
    claimedClaimed by a contributor and currently in progress
    on Oct 4, 2026
  6. Lym308758 commented on Oct 16, 2026

    @Lym308758

    /claim

  7. github-actions commented on Oct 16, 2026

    @github-actions

    @Lym308758 已认领该 Issue,并已自动设为 Assignee。提交 PR 时请在描述中写 Closes #174。

  8. Lym308758 commented on Oct 17, 2026

    @Lym308758

    /release

    已核实该问题已在 main 修复,无需重复提 PR,因此主动释放认领:

    • packages/ui/src/components/chat/blocks/blockFormat.ts 的 formatBlockChange 对 percent 单位改走 formatPercent,正负号只签署一次;
    • packages/ui/src/components/chat/blocks/blockFormat.test.ts 有回归用例 signs percent deltas exactly once,断言 +1.5% / −1.5% / 0%,并对全部 unit 断言不出现 ++;
    • 上述代码与测试均已在 main(根提交 1d114cf)中。

    建议维护者直接关闭本 issue。感谢!

  9. removed
    claimedClaimed by a contributor and currently in progress
    on Oct 17, 2026
  10. github-actions commented on Oct 17, 2026

    @github-actions

    该 Issue 已释放,可以重新认领。

  11. helsome commented on Oct 18, 2026

    @helsome
    Owner

    已核对当前 main:#177 的修复已落地,percent 分支使用 formatPercentRatio(Math.abs(value) / 100),由 formatBlockChange 统一添加一次符号;现有 blockFormat.test.ts 覆盖 +1.5%、−1.5%、0% 及各单位不出现重复符号。本次核对的是主分支源码、回归测试内容和合并记录,未将旧作者测试结果当作本轮重跑。该问题已解决,按 completed 关闭。

    修复记录:#177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions