fix(web): register header/param/has and time functions in cost estimator - #6596
fix(web): register header/param/has and time functions in cost estimator#6596tancheng33 wants to merge 1 commit into
Conversation
WalkthroughThe local pricing evaluator now supports ChangesPricing expression evaluator parity
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
web/src/features/pricing/lib/tier-expr.ts (1)
336-348: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd explicit return types to evaluator callbacks.
The frontend TypeScript guideline requires explicit parameter and return types. Add
string,null,boolean, andnumberreturn types to these callbacks.Proposed change
- header: () => '', - param: () => null, - has: (source: unknown, substr: string) => + header: (): string => '', + param: (): null => null, + has: (source: unknown, substr: string): boolean => source !== null && source !== undefined && - substr !== '' && String(source).includes(substr), - hour: (tz: string) => Number(zonedTimeParts(tz).hour), - minute: (tz: string) => Number(zonedTimeParts(tz).minute), - weekday: (tz: string) => + hour: (tz: string): number => Number(zonedTimeParts(tz).hour), + minute: (tz: string): number => Number(zonedTimeParts(tz).minute), + weekday: (tz: string): number => Math.max(0, ZONED_WEEKDAYS.indexOf(zonedTimeParts(tz).weekday)), - month: (tz: string) => Number(zonedTimeParts(tz).month), - day: (tz: string) => Number(zonedTimeParts(tz).day), + month: (tz: string): number => Number(zonedTimeParts(tz).month), + day: (tz: string): number => Number(zonedTimeParts(tz).day),🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/src/features/pricing/lib/tier-expr.ts` around lines 336 - 348, Update the evaluator callbacks header, param, has, hour, minute, weekday, month, and day with explicit return types: string, null, boolean, or number as appropriate, while preserving their existing parameters and behavior.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/src/features/pricing/lib/tier-expr.test.ts`:
- Around line 86-95: Extend the time-function fallback tests near the existing
invalid-timezone case to cover an empty timezone string. Call a time function
with "" through evalExprLocally and assert evaluation succeeds without an error,
preserving the expected result and cost assertions used by the neighboring test.
---
Nitpick comments:
In `@web/src/features/pricing/lib/tier-expr.ts`:
- Around line 336-348: Update the evaluator callbacks header, param, has, hour,
minute, weekday, month, and day with explicit return types: string, null,
boolean, or number as appropriate, while preserving their existing parameters
and behavior.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c9c65808-9e57-4eb6-ab65-3ea3a8941344
📒 Files selected for processing (2)
web/src/features/pricing/lib/tier-expr.test.tsweb/src/features/pricing/lib/tier-expr.ts
| test('time functions fall back to UTC for an invalid timezone', () => { | ||
| const result = evalExprLocally( | ||
| 'hour("Not/AZone") >= 0 && hour("Not/AZone") <= 23 ? 1 : 0', | ||
| 0, | ||
| 0, | ||
| NO_EXTRAS | ||
| ) | ||
| assert.equal(result.error, null) | ||
| assert.equal(result.cost, 1) | ||
| }) |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Test the empty-timezone fallback.
The evaluator treats "" as UTC at line 288. This test covers only an invalid timezone. Add a case that calls a time function with "" and verifies that evaluation succeeds.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@web/src/features/pricing/lib/tier-expr.test.ts` around lines 86 - 95, Extend
the time-function fallback tests near the existing invalid-timezone case to
cover an empty timezone string. Call a time function with "" through
evalExprLocally and assert evaluation succeeds without an error, preserving the
expected result and cost assertions used by the neighboring test.
📝 变更描述 / Description
模型定价表达式编辑器的费用预估器(
evalExprLocally)求值环境缺少后端pkg/billingexpr已注册的header、param、has以及hour/minute/weekday/month/day时间函数,表达式一使用就报header is not defined(时间函数同理,编辑器帮助文档中也声明了这些函数)。修复方式:在预估器环境中补齐这些函数。预估器没有请求上下文,
header()/param()返回与后端「头缺失/字段缺失」一致的空值(空字符串 / null);has复刻后端的子串匹配语义(nil 源或空子串返回 false);时间函数用Intl.DateTimeFormat按时区求值,非法或空时区回退 UTC,与后端timeInZone行为一致。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。说明:本修复由 AI 辅助完成,已人工审阅代码与测试结果。
📸 运行证明 / Proof of Work
新增
web/src/features/pricing/lib/tier-expr.test.ts,覆盖 header/param/has 的缺省语义与时间函数的时区回退:Summary by CodeRabbit
New Features
Bug Fixes