Skip to content

fix: handle potential error when retrieving modified content during report#4469

Merged
ensorrow merged 2 commits intomainfrom
fix/chat-reporter-error
Mar 11, 2025
Merged

fix: handle potential error when retrieving modified content during report#4469
ensorrow merged 2 commits intomainfrom
fix/chat-reporter-error

Conversation

@ensorrow
Copy link
Copy Markdown
Contributor

@ensorrow ensorrow commented Mar 11, 2025

…preview

Types

  • 🐛 Bug Fixes

Background or solution

image 根因暂未定位,由于为打点,所以直接catch error ### Changelog fix: handle potential error when retrieving modified content during report

Summary by CodeRabbit

  • Bug Fixes
    • 优化数据获取流程,提升系统稳定性,确保操作顺畅。
  • New Features
    • 改进终端输出显示,支持格式化呈现和图标提示,改善日志展示效果。
    • 升级文件列表的折叠/展开图标,增强视觉识别。
  • Style
    • 调整命令面板的内边距和字体大小,提供更紧凑、清晰的界面布局。

@ensorrow ensorrow requested a review from Aaaaash March 11, 2025 06:04
@opensumi opensumi Bot added the 🐞 bug Something isn't working label Mar 11, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 11, 2025

Walkthrough

本次 PR 对多个组件进行了调整:在 live-preview.decoration 文件中增加了 try-catch 错误处理;在 WelcomeMsg、ExpandableFileList 中对组件渲染和关键属性进行了更新;在 Terminal 组件中移除了 stripAnsi,改用 computeAnsiLogString 实现 ANSI 日志转换,并提供 HTML 渲染;同时新增了 computeAnsiLogString 与 filterEraseMultipleLine 工具文件以处理日志输出;Less 文件中的样式也做了微调;此外,package.json 与 frame-decoder 文件分别更新了依赖和导入。

Changes

文件 更改摘要
packages/ai-native/src/browser/widget/inline-stream-diff/live-preview.decoration.tsx, packages/ai-native/src/browser/components/WelcomeMsg.tsx, packages/ai-native/src/browser/mcp/tools/components/ExpandableFileList.tsx 为组件添加了错误处理(live-preview 增加 try-catch)、唯一 key 属性(WelcomeMsg)及使用 Icon 替换传统 span(ExpandableFileList)。
packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx, packages/ai-native/src/browser/mcp/tools/components/computeAnsiLogString.ts, packages/ai-native/src/browser/mcp/tools/components/filterEraseMultipleLine.ts 更新终端输出处理:Terminal 移除 stripAnsi,改用 computeAnsiLogString 渲染 HTML,并新增日志擦除逻辑工具函数。
packages/ai-native/src/browser/mcp/tools/components/index.module.less, packages/components/src/markdown-react/parse.tsx 样式与代码格式调整:减少内边距和字体大小、增加滚动限制,以及改善 React.cloneElement 的多行格式。
packages/ai-native/package.json, packages/connection/src/common/connection/drivers/frame-decoder.ts 更新依赖与导入:package.json 添加新依赖 "ansi_up",frame-decoder.ts 增加 setImmediate 的导入。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant T as TerminalToolComponent
    participant C as computeAnsiLogString
    participant F as filterEraseMultipleLine
    participant A as ansiUp库

    U->>T: 触发命令输出
    T->>C: 调用 computeAnsiLogString(输出文本)
    C->>F: 如启用,调用 filterEraseMultipleLine(日志数组)
    F-->>C: 返回处理后的日志数组
    C->>A: 将每行日志转换为 HTML
    A-->>C: 返回 HTML 转换结果
    C-->>T: 返回最终 HTML 文本
    T->>T: 使用 dangerouslySetInnerHTML 渲染输出
Loading

Possibly related PRs

Suggested reviewers

  • Ricbet
  • hacke2

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > glob@7.2.3: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 76ef79f and e677348.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (10)
  • packages/ai-native/src/browser/widget/inline-stream-diff/live-preview.decoration.tsx (1 hunks)
  • packages/ai-native/package.json (1 hunks)
  • packages/ai-native/src/browser/components/WelcomeMsg.tsx (1 hunks)
  • packages/ai-native/src/browser/mcp/tools/components/ExpandableFileList.tsx (2 hunks)
  • packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx (2 hunks)
  • packages/ai-native/src/browser/mcp/tools/components/computeAnsiLogString.ts (1 hunks)
  • packages/ai-native/src/browser/mcp/tools/components/filterEraseMultipleLine.ts (1 hunks)
  • packages/ai-native/src/browser/mcp/tools/components/index.module.less (3 hunks)
  • packages/components/src/markdown-react/parse.tsx (4 hunks)
  • packages/connection/src/common/connection/drivers/frame-decoder.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx

[error] 76-76: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build-windows
  • GitHub Check: build (macos-latest, 20.x)
🔇 Additional comments (14)
packages/ai-native/package.json (1)

50-50: 添加 ansi_up 依赖用于处理 ANSI 日志转换

新增的 ansi_up 依赖将用于将 ANSI 转义序列转换为 HTML 格式,这对于在用户界面中显示彩色终端输出非常有用。这与 PR 的目标相符,可以更好地处理和显示报告生成过程中可能出现的错误日志。

packages/ai-native/src/browser/components/WelcomeMsg.tsx (1)

82-82: 为 ChatMarkdown 组件添加 key 属性改善渲染性能

为 ChatMarkdown 组件添加 key='welcome' 属性是一个良好的 React 实践,有助于 React 在重新渲染时更高效地识别和处理组件。虽然这个变更与主要 PR 目标的关联性不是很强,但它确实提高了组件的渲染性能和稳定性。

packages/connection/src/common/connection/drivers/frame-decoder.ts (2)

4-4: 引入 setImmediate 用于异步处理

@opensumi/ide-core-common 中引入 setImmediate 函数,为后续的异步处理做准备。


90-90: 使用 setImmediate 优化数据处理性能

在处理大量数据时使用 setImmediate 可以避免阻塞事件循环,提高应用响应性。这种处理方式有助于防止在处理大型数据包时可能出现的界面卡顿或冻结问题,也与 PR 的错误处理目标相关,因为更流畅的数据处理可以减少超时或其他异常情况。

packages/ai-native/src/browser/widget/inline-stream-diff/live-preview.decoration.tsx (1)

329-338: 增加了错误处理以防止在检索修改内容时中断

修改添加了 try-catch 块来捕获 model.getValueInRange() 可能抛出的异常,这与 PR 的主要目标一致。当发生异常时,将 modifyContent 设置为空字符串,以确保程序可以继续执行而不会中断。

这是一个简单但有效的错误处理方法,特别是在根本原因尚未确定的情况下。

packages/ai-native/src/browser/mcp/tools/components/index.module.less (4)

99-99: UI调整:减小了header的内边距

将内边距从 8px 12px 减小到 4px,使界面更加紧凑。同时字体大小也从 12px 减小到 11px

这些调整使界面更加紧凑,与整体 UI 改进方向一致。

Also applies to: 107-107


165-165: UI调整:command_title添加了字体大小设置

.command_title 类添加了 font-size: 11px 的样式,使其与整体界面字体大小保持一致。


172-176: UI调整:优化了command_content的显示方式

.command_content 类添加了最大高度和滚动功能,同时调整了内边距和字体大小:

  1. 添加了 max-height: 200pxoverflow-y: auto,防止内容过多时占用过多空间
  2. 内边距从 4px 调整为 2px 4px
  3. 字体大小从 12px 减小到 11px

这些更改提高了长内容的可用性,避免了内容过多时占用过多空间。


184-184: UI调整:减小了code元素的字体大小

.command_contentcode 元素的字体大小从 12px 减小到 11px,与其他文本保持一致。

packages/ai-native/src/browser/mcp/tools/components/ExpandableFileList.tsx (2)

3-3: 引入Icon组件用于UI改进

添加了对 @opensumi/ide-components/lib/icon/iconIcon 组件的导入,用于优化UI。


114-116: 优化了扩展指示器的渲染

将原来的文本指示器替换为使用 Icon 组件的图标指示器,并添加了旋转动画效果:

  • 使用 codicon-chevron-right 图标
  • 根据 isExpanded 状态旋转图标
  • 添加 display: 'flex' 以确保图标正确对齐

这个变更提高了用户界面的视觉一致性和美观度。

packages/ai-native/src/browser/mcp/tools/components/computeAnsiLogString.ts (1)

1-24: 添加了ANSI日志处理工具函数

新增的 computeAnsiLogString 函数提供了将ANSI格式的日志转换为HTML格式的功能:

  1. 将日志按行分割
  2. 可选地使用 filterEraseMultipleLine 过滤行(处理清空上行逻辑)
  3. 可选地隐藏空行
  4. 使用 ansiUp.ansi_to_html 将每行ANSI文本转换为HTML
  5. 将处理后的行重新连接为字符串返回

这个工具函数使得终端输出的显示更加丰富和易读,支持颜色和格式化显示。

packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx (1)

75-76:

Details

❓ Verification inconclusive

PR 目标与实现的关联性

根据 PR 目标描述,此变更旨在处理"报告过程中检索修改内容时的潜在错误",但在此组件中并未看到明确的错误处理逻辑。这项改动如何实现 PR 的目标?

如果 computeAnsiLogString 函数内部包含错误处理逻辑,请考虑在此组件中也添加 try-catch 机制,以防止渲染失败导致整个组件崩溃。


🏁 Script executed:

#!/bin/bash
# 查找 computeAnsiLogString 函数实现,检查其是否包含错误处理
ast-grep --pattern $'export function computeAnsiLogString($_) {
  $$$
  try {
    $$$
  } catch $$ {
    $$$
  }
  $$$
}'

Length of output: 122


关于 Terminal.tsx 中错误处理机制的进一步确认

目前我们尚未从代码库中确认 computeAnsiLogString 函数内部是否包含错误处理逻辑。初步运行的 ast-grep 脚本没有返回预期信息,因此建议进一步检查该函数的实现,以确定是否已经用 try-catch 进行错误捕获。请运行下面的脚本以获取 computeAnsiLogString 的具体实现上下文,从而判断是否需要在 Terminal 组件中加入额外的异常处理逻辑:

#!/bin/bash
rg -C 10 "export function computeAnsiLogString" -n .

如果确认该函数内部没有错误处理,请考虑在 Terminal.tsx 中包裹对该函数的调用(例如使用 try-catch),以防止异常导致组件崩溃。

🧰 Tools
🪛 Biome (1.9.4)

[error] 76-76: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

packages/ai-native/src/browser/mcp/tools/components/filterEraseMultipleLine.ts (1)

7-18: 良好的代码注释

详细的注释很好地解释了复杂的终端行为和处理逻辑,有助于代码的可维护性。

建议进一步增加对函数参数和返回值的类型文档,以提高 API 的清晰度。

/**
 * 处理过滤清空上行,清空本行逻辑。
 *
 * 关于清空上 n 行:
 * 一般在日志中,出现覆盖上行的情况,ascii 编码为 2K [1A 2K ...] 1G 的样式。 如 \u001b[2K\u001b[1A\u001b[2K\u001b[1A\u001b[2K\u001b[G\r\n,代表清空上两行。
 * 其中,2K 代表清空整行,1A 代表光标上移,配合下一个 2K 则最终效果为清空上行,而 1G 是移动光标到本行开始(位置 1)。
 * 在日志过滤过程中,可以只处理 1A 2K 这个序列,遇到后把该日志的上一行删掉即可。
 *
 * 关于清空本行,按顺序执行:
 * 1. 在当前行没有 Cursor 操作符(如上移时),匹配最后一个 [2K (清空本行)或 \r[K(指针回 0,再清空本行到末尾,相当于清空本行),只输出 [2K 后的内容,
 * 2. 在当前行没有 Cursor 操作符时且有多个 \r (carriage return charactor,移动光标到行首)时,reduce 按 \r \x1b[G 或 \x1b[1G 切分的片断,不段用后一 part 的部分从头覆盖得出结果。
+ *
+ * @param logs - 要处理的日志字符串数组
+ * @returns 处理后的日志字符串数组,移除了被标记为清除的行
 */
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

🧹 Nitpick comments (9)
packages/components/src/markdown-react/parse.tsx (4)

62-67: 改进代码格式提高可读性

React.cloneElement 调用重新格式化为多行形式,提高了代码的可读性。这是一个纯粹的代码样式优化,不影响功能。


89-94: 改进表格标题行代码格式

同样是将 React.cloneElement 调用重新格式化为多行形式,使代码结构更加清晰。这种一致的格式化有助于提高整个代码库的可维护性。


108-113: 改进表格内容行代码格式

继续保持一致的代码格式化风格,将表格内容行的 React.cloneElement 调用也重构为多行形式。


197-199: 改进 htmlUnescape 函数格式

replace 方法调用重新格式化为多行形式,与前面的格式化风格保持一致,提高整体代码可读性。

packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx (1)

10-10: 建议:确保引入的替代函数提供兼容功能

stripAnsi 切换到 computeAnsiLogString 代表了从简单移除 ANSI 转义序列到将其转换为 HTML 的变化。请确保这种改变不会对现有功能产生负面影响,尤其是在处理特殊字符或复杂 ANSI 序列时。

packages/ai-native/src/browser/mcp/tools/components/filterEraseMultipleLine.ts (4)

19-71: 建议添加错误处理机制

该函数处理复杂的 ANSI 转义序列,但没有明确的错误处理机制。如果输入数据格式异常或包含意外的转义序列,可能会导致未定义的行为。

建议添加适当的错误检查和异常处理,确保函数在各种输入情况下都能稳定工作。

export default function filterEraseMultipleLine(logs: string[]) {
+  // 防御性编程:检查输入
+  if (!Array.isArray(logs)) {
+    return [];
+  }
  
  // 上移 cursor + 清空整行
  const eraseLastLine = cursorUp(1) + eraseLine;

28-68: 性能优化建议

当处理大量日志时,频繁的字符串操作(如 indexOflastIndexOfslicesplit)可能会影响性能。对于长日志或大量日志条目,建议考虑以下优化:

  1. 使用正则表达式预编译和缓存
  2. 考虑使用更高效的字符串匹配算法
  3. 对于大型日志,考虑分批处理或懒加载策略

28-39: 考虑副作用和数组操作

函数中使用 acc.pop() 直接修改累加器数组。虽然在 reduce 操作中这是常见的,但在某些情况下可能产生意外的副作用,特别是如果处理嵌套的擦除序列。

建议考虑使用更加函数式的方法或明确记录这种行为,以确保代码的可预测性。


1-71: 建议添加单元测试

考虑到此功能的复杂性和处理终端转义序列的特殊性,强烈建议添加全面的单元测试。测试应该涵盖各种场景,如:

  1. 单行清除
  2. 多行清除
  3. 光标移动和覆盖
  4. 边界情况处理

这将有助于验证实现的正确性并防止未来的回归问题。

如果需要,我可以帮助生成适合此功能的单元测试样例。

🛑 Comments failed to post (1)
packages/ai-native/src/browser/mcp/tools/components/Terminal.tsx (1)

75-76: ⚠️ Potential issue

安全风险:使用 dangerouslySetInnerHTML 可能导致 XSS 攻击

将 ANSI 日志直接转换为 HTML 并使用 dangerouslySetInnerHTML 渲染可能会带来安全风险。确保 computeAnsiLogString 函数对内容进行了充分的安全处理和转义,以防止潜在的 XSS 攻击。

建议考虑以下解决方案:

  1. 确保 computeAnsiLogString 函数中实现了严格的内容净化
  2. 考虑使用专门的终端渲染库(如 xterm.js)来安全地渲染 ANSI 输出
🧰 Tools
🪛 Biome (1.9.4)

[error] 76-76: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.24%. Comparing base (76ef79f) to head (e677348).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4469   +/-   ##
=======================================
  Coverage   53.24%   53.24%           
=======================================
  Files        1663     1663           
  Lines      102608   102608           
  Branches    22196    22196           
=======================================
  Hits        54631    54631           
  Misses      39918    39918           
  Partials     8059     8059           
Flag Coverage Δ
jsdom 48.73% <ø> (ø)
node 12.20% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ensorrow ensorrow merged commit 52111ac into main Mar 11, 2025
@ensorrow ensorrow deleted the fix/chat-reporter-error branch March 11, 2025 08:38
@erha19 erha19 mentioned this pull request May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants