Skip to content

fix(web): register header/param/has and time functions in cost estimator - #6596

Open
tancheng33 wants to merge 1 commit into
QuantumNous:mainfrom
tancheng33:fix/estimator-expr-functions
Open

fix(web): register header/param/has and time functions in cost estimator#6596
tancheng33 wants to merge 1 commit into
QuantumNous:mainfrom
tancheng33:fix/estimator-expr-functions

Conversation

@tancheng33

@tancheng33 tancheng33 commented Aug 2, 2026

Copy link
Copy Markdown

📝 变更描述 / Description

模型定价表达式编辑器的费用预估器(evalExprLocally)求值环境缺少后端 pkg/billingexpr 已注册的 headerparamhas 以及 hour/minute/weekday/month/day 时间函数,表达式一使用就报 header is not defined(时间函数同理,编辑器帮助文档中也声明了这些函数)。

修复方式:在预估器环境中补齐这些函数。预估器没有请求上下文,header()/param() 返回与后端「头缺失/字段缺失」一致的空值(空字符串 / null);has 复刻后端的子串匹配语义(nil 源或空子串返回 false);时间函数用 Intl.DateTimeFormat 按时区求值,非法或空时区回退 UTC,与后端 timeInZone 行为一致。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

说明:本修复由 AI 辅助完成,已人工审阅代码与测试结果。

📸 运行证明 / Proof of Work

新增 web/src/features/pricing/lib/tier-expr.test.ts,覆盖 header/param/has 的缺省语义与时间函数的时区回退:

$ bun test src/features/pricing/lib/tier-expr.test.ts   # bun 1.3.14(与 CI 一致)
 5 pass
 0 fail
Ran 5 tests across 1 file. [145.00ms]

$ bun run typecheck
$ tsgo -b
Finished in 431ms on 1059 files using 10 threads.

Summary by CodeRabbit

  • New Features

    • Added timezone-aware date and time conditions to local pricing estimates.
    • Added support for evaluating request headers, body parameters, and substring matching.
    • Invalid or missing timezones now fall back to UTC for consistent results.
  • Bug Fixes

    • Improved local estimate consistency with backend pricing behavior.

@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The local pricing evaluator now supports header, param, and has, plus timezone-aware weekday, month, day, hour, and minute functions. Invalid or empty timezones fall back to UTC. Tests cover these behaviors.

Changes

Pricing expression evaluator parity

Layer / File(s) Summary
Backend-compatible evaluator functions and validation
web/src/features/pricing/lib/tier-expr.ts, web/src/features/pricing/lib/tier-expr.test.ts
The evaluator adds deterministic request helpers, substring matching, timezone-aware date functions, and UTC fallback handling. Tests cover missing inputs, matching, valid timezones, and invalid timezones.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

A rabbit checks the tiers with care,
Header, param, and has now share.
Time zones guide the clock’s bright glow,
Bad zones fall to UTC below.
Tests hop neatly, green and clear.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes registering the missing environment and time functions in the local cost estimator.
Linked Issues check ✅ Passed The changes register header, param, and has with backend-compatible behavior and add tests for the required estimator evaluation semantics in issue [#6491].
Out of Scope Changes check ✅ Passed The timezone-aware time functions and related tests support the stated goal of matching backend billing expression behavior and are not unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
web/src/features/pricing/lib/tier-expr.ts (1)

336-348: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add explicit return types to evaluator callbacks.

The frontend TypeScript guideline requires explicit parameter and return types. Add string, null, boolean, and number return 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

📥 Commits

Reviewing files that changed from the base of the PR and between 0ab0202 and dfa02a3.

📒 Files selected for processing (2)
  • web/src/features/pricing/lib/tier-expr.test.ts
  • web/src/features/pricing/lib/tier-expr.ts

Comment on lines +86 to +95
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)
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

模型定价表达式编辑器的费用预估器未注册 header、param、has 函数

1 participant