fix(assets): 智能选素材传 limit 参数,避免「有几个选几个」 #1570
Reference in New Issue
Block a user
Delete Branch "fix/smart-match-limit"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
问题
线上实测:AI 自动选素材时,视频库里有几个素材就返回几个,没有数量上限控制。
根因:
smartMatchAssets调用POST /assets/smart-match时未传limit参数,后端SmartMatchRequest.limit默认为None(返回全部)。修复
1.
apps/web/src/api/assets/assets.tssmartMatchAssets增加可选limit?: number参数limit > 0时序列化进请求体2.
apps/web/src/pages/generate/hooks/step2-materials/useSmartMatch.tstemplateSegments?: TemplateSegment[]选项computeLimitFromSegments:duration_min之和作为目标视频总时长Math.ceil(totalSeconds / 15)[1, 200](后端SmartMatchRequest.limit上限 200)limit = 103.
apps/web/src/pages/generate/hooks/useStep2Materials.tstemplateSegments到useSmartMatch影响范围
materialMode === "auto")limit字段(SmartMatchRequest.limit: int | None = Field(default=None, ge=1, le=200))验收
ceil(目标视频时长 / 15),不再等于库里素材总数🚀 预览环境已部署
【阻塞级判定】
📊 审查概览
🔴 阻塞级问题(必须修复)
无
💡 改进建议(不阻塞合并)
useCallback的依赖数组中包含了templateSegments。如果父组件每次渲染都传递一个新的数组引用(这在 React 中很常见),会导致handleSmartMatch函数在每次渲染时都被重新创建。建议确认父组件传入的templateSegments引用是否稳定,或者在父组件中使用useMemo包装该数据,以避免不必要的子组件重渲染或函数重创建。✅ 良好实践
computeLimitFromSegments函数中,对空数组、总时长小于等于0的情况均做了兜底处理,并对最终结果进行了[1, 200]的区间钳制,逻辑严密。handleSmartMatch中使用!!a?.id进行过滤,有效防止了undefined或null导致的运行时错误。2/3. 类型安全与防御性编程:
smartMatchAssets函数中对limit参数进行了limit > 0的校验,确保了只有合法的正整数才会被发送到后端。🤖 由 AI 代码审查机器人自动生成 | 2026-08-31 06:13:38 | 模型:
CI全绿,自动审批通过。
CI全绿,自动审批通过。
🗑️ 预览环境已清理
PR #1570 已关闭或合并,对应的预览环境已被清理。