Skip to content

refactor(chat): optimize sandbox status logic and decouple UI/Status hooks#6713

Merged
c121914yu merged 2 commits intolabring:mainfrom
DigHuang:feat/check-sandbox
Apr 3, 2026
Merged

refactor(chat): optimize sandbox status logic and decouple UI/Status hooks#6713
c121914yu merged 2 commits intolabring:mainfrom
DigHuang:feat/check-sandbox

Conversation

@DigHuang
Copy link
Copy Markdown
Collaborator

@DigHuang DigHuang commented Apr 2, 2026

No description provided.

@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Apr 2, 2026

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Build Successful - Preview code-sandbox Image for this PR:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:code-sandbox_4c1ac36d6760a591a4596846e007f0fda9a24b35

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Build Successful - Preview fastgpt Image for this PR:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:fastgpt_4c1ac36d6760a591a4596846e007f0fda9a24b35

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

Build Successful - Preview mcp_server Image for this PR:

registry.cn-hangzhou.aliyuncs.com/fastgpt/fastgpt-pr:mcp_server_4c1ac36d6760a591a4596846e007f0fda9a24b35

Copy link
Copy Markdown
Collaborator

@c121914yu c121914yu left a comment

Choose a reason for hiding this comment

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

📊 代码审查总结

整体这是一个不错的重构 PR,将 useSandboxEditor 拆分为 useSandboxStatus(状态/网络逻辑)和 useSandboxEditor(UI 弹窗逻辑),关注点分离清晰。以下是详细的行级评论。

setApiSandboxExists(false);
}

const chatRecords = useContextSelector(ChatRecordContext, (v) => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🟢 优化建议 — 网络请求从轮询改为单次的取舍

原来的实现使用 useInterval(checkSandboxStatus, 10000) 每 10 秒轮询一次,现在改为仅在 chatId 变化时请求一次。

这个改动减少了不必要的网络请求,但也意味着:如果用户在聊天过程中 AI 创建了新的 sandbox(流式响应结束后),状态不会自动更新,用户需要切换聊天再切回来才能看到 sandbox 图标。

hasSandboxInHistory 通过 chatRecordsuseMemo 派生可以部分弥补这个问题——但前提是 chatRecords 会在新消息完成后及时更新。请确认这个场景是否有覆盖。

@c121914yu
Copy link
Copy Markdown
Collaborator

PR Review: refactor(chat): optimize sandbox status logic and decouple UI/Status hooks

📊 变更概览

✅ 优点

  1. 关注点分离清晰:将 useSandboxEditor 拆分为 useSandboxStatus(状态同步)和 useSandboxEditor(UI 弹窗),符合单一职责原则
  2. 移除轮询:去掉了 10 秒一次的 useInterval 轮询,改用 useMemochatRecords 派生 sandbox 状态 + chatId 变化时单次请求,减少不必要的网络开销
  3. JSDoc 注释完善:两个 hook 都有清晰的职责说明和设计意图文档
  4. 正确的取消模式useEffect 中使用 cancelled 标志防止组件卸载后的状态更新

⚠️ 问题汇总

🔴 严重问题 (1 个,必须修复)

  1. during-render 双 setState (hook.tsx:71-74):在渲染期间连续调用 setPrevChatIdsetApiSandboxExists 会触发额外的重渲染。建议合并为单个 state 对象或改用 useRef

🟡 建议改进 (3 个)

  1. addStatisticalDataToHistoryItem 性能 (hook.tsx:83):在 useMemo 内对每条记录做完整的 flat + reduce,可以通过过滤非 AI 消息优化
  2. outLinkAuthData 引用稳定性 (hook.tsx:93):对象类型作为 useEffect 依赖可能导致不必要的重复请求,建议使用 useRef 或解构基本类型
  3. 不相关变更 (type.ts:172):移除 @deprecated 标记与本 PR 无关,应单独处理

🟢 可选优化 (3 个)

  1. 轮询移除后的覆盖面确认 (hook.tsx:89):确认流式消息完成后 chatRecords 能及时更新,否则新创建的 sandbox 不会即时显示
  2. 重复 import (ToolMenu.tsx:11-12):同一模块的两条 import 应合并
  3. onClose 参数命名 (hook.tsx:38):建议改为 afterClose 避免与返回值 onCloseSandboxModal 混淆

🧪 测试建议

  • 验证 chatId 切换时 sandbox 图标状态是否正确重置
  • 验证流式响应中创建 sandbox 后,图标是否及时出现(不依赖轮询)
  • 验证 outLinkAuthData 为内联对象时是否有重复请求
  • 验证多个页面(ChatTest、DetailLogsModal、WorkflowChatTest)sandbox 功能正常

💬 总体评价

  • 代码质量: ⭐⭐⭐⭐☆ (4/5)
  • 安全性: ⭐⭐⭐⭐⭐ (5/5)
  • 性能: ⭐⭐⭐⭐☆ (4/5)
  • 可维护性: ⭐⭐⭐⭐⭐ (5/5)

🚀 审查结论

需修改 — 请修复 during-render 双 setState 问题后可通过。其余建议改进项可酌情处理。


📍 详细代码评论

已在以下位置添加了具体的行级评论:

  • hook.tsx:73 — during-render setState(严重)
  • hook.tsx:83 — useMemo 性能优化
  • hook.tsx:89 — 轮询移除后的覆盖面
  • hook.tsx:93 — outLinkAuthData 引用稳定性
  • hook.tsx:38 — onClose 命名
  • hook.tsx:110 — displayName
  • ToolMenu.tsx:11 — 重复 import
  • type.ts:172@deprecated 移除

@DigHuang DigHuang force-pushed the feat/check-sandbox branch from 343c21b to 4c1ac36 Compare April 3, 2026 07:09
@c121914yu c121914yu merged commit 7cca3f9 into labring:main Apr 3, 2026
5 checks passed
@DigHuang DigHuang deleted the feat/check-sandbox branch April 3, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants