Commit f2f3410d by CaIon

feat: add `len` variable for tier conditions and LLM prompt helper

parent a7c38ec8
...@@ -1001,10 +1001,81 @@ func TestImageAudioZero(t *testing.T) { ...@@ -1001,10 +1001,81 @@ func TestImageAudioZero(t *testing.T) {
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// len variable tests — tier conditions based on context length
// ---------------------------------------------------------------------------
const lenTieredExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6)`
func TestLen_StandardTier(t *testing.T) {
params := billingexpr.TokenParams{P: 80000, C: 5000, Len: 100000, CR: 20000}
cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
want := 80000*3 + 5000*15 + 20000*0.3
if math.Abs(cost-want) > 1e-6 {
t.Errorf("cost = %f, want %f", cost, want)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard", trace.MatchedTier)
}
}
func TestLen_LongContextTier(t *testing.T) {
// p is low (cache subtracted), but len is high (full context)
params := billingexpr.TokenParams{P: 50000, C: 5000, Len: 300000, CR: 250000}
cost, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
want := 50000*6 + 5000*22.5 + 250000*0.6
if math.Abs(cost-want) > 1e-6 {
t.Errorf("cost = %f, want %f", cost, want)
}
if trace.MatchedTier != "long_context" {
t.Errorf("tier = %q, want long_context (len=300000 > 200000)", trace.MatchedTier)
}
}
func TestLen_BoundaryExact(t *testing.T) {
params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200000, CR: 100000}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard (len=200000 <= 200000)", trace.MatchedTier)
}
}
func TestLen_BoundaryPlusOne(t *testing.T) {
params := billingexpr.TokenParams{P: 100000, C: 1000, Len: 200001, CR: 100001}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "long_context" {
t.Errorf("tier = %q, want long_context (len=200001 > 200000)", trace.MatchedTier)
}
}
func TestLen_ZeroDefaultsToZero(t *testing.T) {
// len defaults to 0 when not set
params := billingexpr.TokenParams{P: 1000, C: 500}
_, trace, err := billingexpr.RunExpr(lenTieredExpr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "standard" {
t.Errorf("tier = %q, want standard (len=0 <= 200000)", trace.MatchedTier)
}
}
// ---------------------------------------------------------------------------
// Benchmarks: compile vs cached execution // Benchmarks: compile vs cached execution
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
const benchComplexExpr = `p <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)` const benchComplexExpr = `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6 + img * 3 + img_o * 30 + ai * 10 + ao * 40) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12 + img * 6 + img_o * 60 + ai * 20 + ao * 80)`
func BenchmarkExprCompile(b *testing.B) { func BenchmarkExprCompile(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
...@@ -1015,7 +1086,7 @@ func BenchmarkExprCompile(b *testing.B) { ...@@ -1015,7 +1086,7 @@ func BenchmarkExprCompile(b *testing.B) {
func BenchmarkExprRunCached(b *testing.B) { func BenchmarkExprRunCached(b *testing.B) {
billingexpr.CompileFromCache(benchComplexExpr) billingexpr.CompileFromCache(benchComplexExpr)
params := billingexpr.TokenParams{P: 150000, C: 10000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500} params := billingexpr.TokenParams{P: 150000, C: 10000, Len: 188000, CR: 30000, CC: 5000, Img: 2000, AI: 1000, AO: 500}
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
billingexpr.RunExpr(benchComplexExpr, params) billingexpr.RunExpr(benchComplexExpr, params)
......
...@@ -41,6 +41,7 @@ var ( ...@@ -41,6 +41,7 @@ var (
var compileEnvPrototypeV1 = map[string]interface{}{ var compileEnvPrototypeV1 = map[string]interface{}{
"p": float64(0), "p": float64(0),
"c": float64(0), "c": float64(0),
"len": float64(0),
"cr": float64(0), "cr": float64(0),
"cc": float64(0), "cc": float64(0),
"cc1h": float64(0), "cc1h": float64(0),
......
...@@ -30,7 +30,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are ...@@ -30,7 +30,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
| 变量 | 含义 | | 变量 | 含义 |
|------|------| |------|------|
| `p` | 输入 token 数。**自动排除**表达式中单独计价的子类别(见下方说明) | | `p` | 输入 token 数(**计价用**)。**自动排除**表达式中单独计价的子类别(见下方说明) |
| `len` | 输入上下文总长度(**条件判断用**)。不受自动排除影响,始终反映完整输入长度。非 Claude:等于原始 `prompt_tokens`;Claude:等于文本输入 + 缓存读取 + 缓存创建 |
| `cr` | 缓存命中(读取)token 数 | | `cr` | 缓存命中(读取)token 数 |
| `cc` | 缓存创建 token 数(Claude 5分钟 TTL / 通用) | | `cc` | 缓存创建 token 数(Claude 5分钟 TTL / 通用) |
| `cc1h` | 缓存创建 token 数 — 1小时 TTL(Claude 专用) | | `cc1h` | 缓存创建 token 数 — 1小时 TTL(Claude 专用) |
...@@ -51,6 +52,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are ...@@ -51,6 +52,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
**规则:如果表达式使用了某个子类别变量,对应的 token 就从 `p` 或 `c` 中扣除;如果没使用,那些 token 就留在 `p` 或 `c` 里按基础价格计费。** **规则:如果表达式使用了某个子类别变量,对应的 token 就从 `p` 或 `c` 中扣除;如果没使用,那些 token 就留在 `p` 或 `c` 里按基础价格计费。**
> **重要:`len` 不受自动排除影响。** `len` 始终代表完整的输入上下文长度,不管表达式是否单独对缓存/图片/音频定价。因此**阶梯条件应使用 `len` 而非 `p`**,以避免缓存命中导致 `p` 降低而误判档位。
举例说明(假设上游返回的原始数据:prompt_tokens=1000,其中包含 200 cache read、100 image): 举例说明(假设上游返回的原始数据:prompt_tokens=1000,其中包含 200 cache read、100 image):
| 表达式 | `p` 的值 | 说明 | | 表达式 | `p` 的值 | 说明 |
...@@ -93,8 +96,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are ...@@ -93,8 +96,8 @@ Powered by [expr-lang/expr](https://github.com/expr-lang/expr). Expressions are
# Simple flat pricing # Simple flat pricing
tier("base", p * 2.5 + c * 15 + cr * 0.25) tier("base", p * 2.5 + c * 15 + cr * 0.25)
# Multi-tier (Claude Sonnet style) # Multi-tier (Claude Sonnet style) — use len for tier conditions
p <= 200000 len <= 200000
? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6) ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6)
: tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)
...@@ -199,6 +202,16 @@ Example: `p * 2.5 + c * 15 + cr * 0.25` ...@@ -199,6 +202,16 @@ Example: `p * 2.5 + c * 15 + cr * 0.25`
- Expression uses `cr` → cache read tokens subtracted from `p` - Expression uses `cr` → cache read tokens subtracted from `p`
- Expression doesn't use `img` → image tokens stay in `p`, priced at $2.50 - Expression doesn't use `img` → image tokens stay in `p`, priced at $2.50
### `len` — Context Length Variable
`len` represents the total input context length, designed for **tier condition evaluation** (e.g. `len <= 200000 ? ...`). Unlike `p`, `len` is never reduced by sub-category exclusion.
**Computation rules:**
- **Non-Claude (GPT/OpenAI format)**: `len = prompt_tokens` (the raw total from the upstream response)
- **Claude format**: `len = input_tokens + cache_read_tokens + cache_creation_tokens` (since Claude's `input_tokens` is text-only, cache must be added back to reflect full context length)
This ensures that heavy cache usage doesn't cause the tier condition to incorrectly evaluate to a lower tier. For example, if a request has 300K total context but 250K is cached, `p` with cache subtracted would be only 50K (standard tier), while `len` correctly reports 300K (long-context tier).
### Quota Conversion ### Quota Conversion
Expression coefficients are $/1M tokens. Conversion to internal quota: Expression coefficients are $/1M tokens. Conversion to internal quota:
......
...@@ -13,7 +13,8 @@ import ( ...@@ -13,7 +13,8 @@ import (
// RunExpr compiles (with cache) and executes an expression string. // RunExpr compiles (with cache) and executes an expression string.
// The environment exposes: // The environment exposes:
// - p, c — prompt / completion tokens // - p, c — prompt / completion tokens (auto-excluding separately-priced sub-categories)
// - len — total input context length for tier conditions (never reduced by sub-category exclusion)
// - cr, cc, cc1h — cache read / creation / creation-1h tokens // - cr, cc, cc1h — cache read / creation / creation-1h tokens
// - tier(name, value) — trace callback that records which tier matched // - tier(name, value) — trace callback that records which tier matched
// - max, min, abs, ceil, floor — standard math helpers // - max, min, abs, ceil, floor — standard math helpers
...@@ -54,6 +55,7 @@ func runProgram(prog *vm.Program, params TokenParams, request RequestInput) (flo ...@@ -54,6 +55,7 @@ func runProgram(prog *vm.Program, params TokenParams, request RequestInput) (flo
env := map[string]interface{}{ env := map[string]interface{}{
"p": params.P, "p": params.P,
"c": params.C, "c": params.C,
"len": params.Len,
"cr": params.CR, "cr": params.CR,
"cc": params.CC, "cc": params.CC,
"cc1h": params.CC1h, "cc1h": params.CC1h,
......
...@@ -14,8 +14,9 @@ type RequestInput struct { ...@@ -14,8 +14,9 @@ type RequestInput struct {
// Fields beyond P and C are optional — when absent they default to 0, // Fields beyond P and C are optional — when absent they default to 0,
// which means cache-unaware expressions keep working unchanged. // which means cache-unaware expressions keep working unchanged.
type TokenParams struct { type TokenParams struct {
P float64 // prompt tokens (text) P float64 // prompt tokens (text) — auto-excludes sub-categories priced separately
C float64 // completion tokens (text) C float64 // completion tokens (text) — auto-excludes sub-categories priced separately
Len float64 // total input context length for tier conditions (non-Claude: raw prompt_tokens; Claude: text + cache read + cache creation)
CR float64 // cache read (hit) tokens CR float64 // cache read (hit) tokens
CC float64 // cache creation tokens (5-min TTL for Claude, generic for others) CC float64 // cache creation tokens (5-min TTL for Claude, generic for others)
CC1h float64 // cache creation tokens — 1-hour TTL (Claude only) CC1h float64 // cache creation tokens — 1-hour TTL (Claude only)
......
...@@ -257,6 +257,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT ...@@ -257,6 +257,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
rawCost, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{ rawCost, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{
P: float64(promptTokens), P: float64(promptTokens),
C: float64(estimatedCompletionTokens), C: float64(estimatedCompletionTokens),
Len: float64(promptTokens),
}, requestInput) }, requestInput)
if err != nil { if err != nil {
return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err) return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err)
......
...@@ -162,6 +162,7 @@ func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, mod ...@@ -162,6 +162,7 @@ func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, mod
tieredOk, tieredQuota, tieredRes := TryTieredSettle(relayInfo, billingexpr.TokenParams{ tieredOk, tieredQuota, tieredRes := TryTieredSettle(relayInfo, billingexpr.TokenParams{
P: float64(usage.InputTokens), P: float64(usage.InputTokens),
C: float64(usage.OutputTokens), C: float64(usage.OutputTokens),
Len: float64(usage.InputTokens),
}) })
if tieredOk { if tieredOk {
tieredResult = tieredRes tieredResult = tieredRes
......
...@@ -35,6 +35,14 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa ...@@ -35,6 +35,14 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
imgO := float64(usage.CompletionTokenDetails.ImageTokens) imgO := float64(usage.CompletionTokenDetails.ImageTokens)
ao := float64(usage.CompletionTokenDetails.AudioTokens) ao := float64(usage.CompletionTokenDetails.AudioTokens)
// len = total input context length for tier condition evaluation.
// Non-Claude: prompt_tokens already includes everything.
// Claude: input_tokens is text-only, so add cache read + cache creation.
inputLen := p
if isClaudeUsageSemantic {
inputLen = p + cr + cc5m + cc1h
}
if !isClaudeUsageSemantic { if !isClaudeUsageSemantic {
if usedVars["cr"] { if usedVars["cr"] {
p -= cr p -= cr
...@@ -69,6 +77,7 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa ...@@ -69,6 +77,7 @@ func BuildTieredTokenParams(usage *dto.Usage, isClaudeUsageSemantic bool, usedVa
return billingexpr.TokenParams{ return billingexpr.TokenParams{
P: p, P: p,
C: c, C: c,
Len: inputLen,
CR: cr, CR: cr,
CC: cc5m, CC: cc5m,
CC1h: cc1h, CC1h: cc1h,
......
...@@ -605,6 +605,97 @@ func TestBuildTieredTokenParams_ParityWithRatio_Image(t *testing.T) { ...@@ -605,6 +605,97 @@ func TestBuildTieredTokenParams_ParityWithRatio_Image(t *testing.T) {
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// BuildTieredTokenParams: Len computation tests
// ---------------------------------------------------------------------------
func TestBuildTieredTokenParams_Len_GPT(t *testing.T) {
usage := &dto.Usage{
PromptTokens: 10000,
CompletionTokens: 2000,
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 3000,
TextTokens: 7000,
},
}
expr := `tier("base", p * 2.5 + c * 15 + cr * 0.25)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, false, usedVars)
// Non-Claude: Len = raw PromptTokens
if params.Len != 10000 {
t.Fatalf("Len = %f, want 10000 (raw PromptTokens)", params.Len)
}
// P should be reduced by cache
if params.P != 7000 {
t.Fatalf("P = %f, want 7000 (PromptTokens - CachedTokens)", params.P)
}
}
func TestBuildTieredTokenParams_Len_Claude(t *testing.T) {
usage := &dto.Usage{
PromptTokens: 5000,
CompletionTokens: 2000,
UsageSemantic: "anthropic",
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 3000,
TextTokens: 5000,
},
ClaudeCacheCreation5mTokens: 1000,
ClaudeCacheCreation1hTokens: 500,
}
expr := `tier("base", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, true, usedVars)
// Claude: Len = PromptTokens + CachedTokens + CacheCreation5m + CacheCreation1h
wantLen := float64(5000 + 3000 + 1000 + 500)
if params.Len != wantLen {
t.Fatalf("Len = %f, want %f (text + cache read + cache creation)", params.Len, wantLen)
}
// Claude: P is not reduced (isClaudeUsageSemantic = true)
if params.P != 5000 {
t.Fatalf("P = %f, want 5000 (no subtraction for Claude)", params.P)
}
}
func TestBuildTieredTokenParams_Len_TierCondition(t *testing.T) {
// Test that len-based tier conditions work correctly when p is reduced by cache
usage := &dto.Usage{
PromptTokens: 300000,
CompletionTokens: 5000,
PromptTokensDetails: dto.InputTokenDetails{
CachedTokens: 250000,
TextTokens: 50000,
},
}
expr := `len <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6)`
usedVars := billingexpr.UsedVars(expr)
params := BuildTieredTokenParams(usage, false, usedVars)
// Len = 300000 (raw prompt), P = 50000 (300000 - 250000 cache)
if params.Len != 300000 {
t.Fatalf("Len = %f, want 300000", params.Len)
}
if params.P != 50000 {
t.Fatalf("P = %f, want 50000", params.P)
}
// Run expression: len=300000 > 200000, so long_context tier
cost, trace, err := billingexpr.RunExpr(expr, params)
if err != nil {
t.Fatal(err)
}
if trace.MatchedTier != "long_context" {
t.Fatalf("tier = %s, want long_context (len=300000 but p=50000)", trace.MatchedTier)
}
// long_context: 50000*6 + 5000*22.5 + 250000*0.6
wantCost := 50000.0*6 + 5000*22.5 + 250000*0.6
if math.Abs(cost-wantCost) > 1e-6 {
t.Fatalf("cost = %f, want %f", cost, wantCost)
}
}
// ---------------------------------------------------------------------------
// Stress test: 1000 concurrent goroutines, complex tiered expr vs ratio, // Stress test: 1000 concurrent goroutines, complex tiered expr vs ratio,
// random token counts, verify correctness and measure performance // random token counts, verify correctness and measure performance
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
......
...@@ -54,10 +54,10 @@ func SmokeTestExpr(exprStr string) error { ...@@ -54,10 +54,10 @@ func SmokeTestExpr(exprStr string) error {
func smokeTestExpr(exprStr string) error { func smokeTestExpr(exprStr string) error {
vectors := []billingexpr.TokenParams{ vectors := []billingexpr.TokenParams{
{P: 0, C: 0}, {P: 0, C: 0, Len: 0},
{P: 1000, C: 1000}, {P: 1000, C: 1000, Len: 1000},
{P: 100000, C: 100000}, {P: 100000, C: 100000, Len: 100000},
{P: 1000000, C: 1000000}, {P: 1000000, C: 1000000, Len: 1000000},
} }
requests := []billingexpr.RequestInput{ requests := []billingexpr.RequestInput{
{}, {},
......
...@@ -21,7 +21,7 @@ import React from 'react'; ...@@ -21,7 +21,7 @@ import React from 'react';
import { Avatar, Tag, Table, Typography } from '@douyinfe/semi-ui'; import { Avatar, Tag, Table, Typography } from '@douyinfe/semi-ui';
import { IconPriceTag } from '@douyinfe/semi-icons'; import { IconPriceTag } from '@douyinfe/semi-icons';
import { parseTiersFromExpr, getCurrencyConfig } from '../../../../../helpers'; import { parseTiersFromExpr, getCurrencyConfig } from '../../../../../helpers';
import { BILLING_VARS } from '../../../../../constants'; import { BILLING_PRICING_VARS } from '../../../../../constants';
import { import {
splitBillingExprAndRequestRules, splitBillingExprAndRequestRules,
tryParseRequestRuleExpr, tryParseRequestRuleExpr,
...@@ -113,7 +113,7 @@ export default function DynamicPricingBreakdown({ billingExpr, t }) { ...@@ -113,7 +113,7 @@ export default function DynamicPricingBreakdown({ billingExpr, t }) {
); );
} }
const priceFields = BILLING_VARS.map((v) => [v.field, v.shortLabel]); const priceFields = BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]);
const tierColumns = [ const tierColumns = [
{ {
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
export const BILLING_VARS = [ export const BILLING_VARS = [
{ key: 'p', field: 'inputPrice', tierField: 'input_unit_cost', label: '输入价格', shortLabel: '输入', side: 'input', isBase: true }, { key: 'p', field: 'inputPrice', tierField: 'input_unit_cost', label: '输入价格', shortLabel: '输入', side: 'input', isBase: true },
{ key: 'c', field: 'outputPrice', tierField: 'output_unit_cost', label: '补全价格', shortLabel: '补全', side: 'output', isBase: true }, { key: 'c', field: 'outputPrice', tierField: 'output_unit_cost', label: '补全价格', shortLabel: '补全', side: 'output', isBase: true },
{ key: 'len', field: null, tierField: null, label: '输入长度', shortLabel: '长度', side: 'condition', isConditionOnly: true },
{ key: 'cr', field: 'cacheReadPrice', tierField: 'cache_read_unit_cost', label: '缓存读取价格', shortLabel: '缓存读', side: 'input', group: 'cache' }, { key: 'cr', field: 'cacheReadPrice', tierField: 'cache_read_unit_cost', label: '缓存读取价格', shortLabel: '缓存读', side: 'input', group: 'cache' },
{ key: 'cc', field: 'cacheCreatePrice', tierField: 'cache_create_unit_cost', label: '缓存创建价格', shortLabel: '缓存创建', side: 'input', group: 'cache' }, { key: 'cc', field: 'cacheCreatePrice', tierField: 'cache_create_unit_cost', label: '缓存创建价格', shortLabel: '缓存创建', side: 'input', group: 'cache' },
{ key: 'cc1h', field: 'cacheCreate1hPrice', tierField: 'cache_create_1h_unit_cost', label: '1h缓存创建价格', shortLabel: '1h缓存创建', side: 'input', group: 'cache' }, { key: 'cc1h', field: 'cacheCreate1hPrice', tierField: 'cache_create_1h_unit_cost', label: '1h缓存创建价格', shortLabel: '1h缓存创建', side: 'input', group: 'cache' },
...@@ -24,18 +25,20 @@ export const BILLING_VARS = [ ...@@ -24,18 +25,20 @@ export const BILLING_VARS = [
export const BILLING_VAR_KEYS = BILLING_VARS.map((v) => v.key); export const BILLING_VAR_KEYS = BILLING_VARS.map((v) => v.key);
export const BILLING_EXTRA_VARS = BILLING_VARS.filter((v) => !v.isBase); export const BILLING_PRICING_VARS = BILLING_VARS.filter((v) => !v.isConditionOnly);
export const BILLING_EXTRA_VARS = BILLING_VARS.filter((v) => !v.isBase && !v.isConditionOnly);
export const BILLING_VAR_KEY_TO_FIELD = Object.fromEntries( export const BILLING_VAR_KEY_TO_FIELD = Object.fromEntries(
BILLING_VARS.map((v) => [v.key, v.field]), BILLING_PRICING_VARS.map((v) => [v.key, v.field]),
); );
export const BILLING_VAR_FIELD_TO_LABEL = Object.fromEntries( export const BILLING_VAR_FIELD_TO_LABEL = Object.fromEntries(
BILLING_VARS.map((v) => [v.field, v.label]), BILLING_PRICING_VARS.map((v) => [v.field, v.label]),
); );
export const BILLING_VAR_FIELD_TO_SHORT_LABEL = Object.fromEntries( export const BILLING_VAR_FIELD_TO_SHORT_LABEL = Object.fromEntries(
BILLING_VARS.map((v) => [v.field, v.shortLabel]), BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]),
); );
export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({ export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({
...@@ -44,6 +47,10 @@ export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({ ...@@ -44,6 +47,10 @@ export const BILLING_CACHE_VAR_MAP = BILLING_EXTRA_VARS.map((v) => ({
})); }));
export const BILLING_VAR_REGEX = new RegExp( export const BILLING_VAR_REGEX = new RegExp(
`\\b(${BILLING_VAR_KEYS.join('|')})\\s*\\*\\s*([\\d.eE+-]+)`, `\\b(${BILLING_PRICING_VARS.map((v) => v.key).join('|')})\\s*\\*\\s*([\\d.eE+-]+)`,
'g', 'g',
); );
export const BILLING_CONDITION_VARS = BILLING_VARS.filter(
(v) => v.isBase || v.isConditionOnly,
).map((v) => v.key);
...@@ -22,7 +22,7 @@ import { Modal, Tag, Typography, Avatar } from '@douyinfe/semi-ui'; ...@@ -22,7 +22,7 @@ import { Modal, Tag, Typography, Avatar } from '@douyinfe/semi-ui';
import { copy, showSuccess } from './utils'; import { copy, showSuccess } from './utils';
import { MOBILE_BREAKPOINT } from '../hooks/common/useIsMobile'; import { MOBILE_BREAKPOINT } from '../hooks/common/useIsMobile';
import { import {
BILLING_VARS, BILLING_PRICING_VARS,
BILLING_VAR_KEY_TO_FIELD, BILLING_VAR_KEY_TO_FIELD,
BILLING_VAR_REGEX, BILLING_VAR_REGEX,
} from '../constants'; } from '../constants';
...@@ -2246,7 +2246,7 @@ export function parseTiersFromExpr(exprStr) { ...@@ -2246,7 +2246,7 @@ export function parseTiersFromExpr(exprStr) {
if (!exprStr) return []; if (!exprStr) return [];
try { try {
const { body } = stripExprVersion(exprStr); const { body } = stripExprVersion(exprStr);
const condGroup = `((?:(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`; const condGroup = `((?:(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)(?:\\s*&&\\s*(?:p|c|len)\\s*(?:<|<=|>|>=)\\s*[\\d.eE+]+)*)`;
const tierRe = new RegExp(`(?:${condGroup}\\s*\\?\\s*)?tier\\("([^"]*)",\\s*([^)]+)\\)`, 'g'); const tierRe = new RegExp(`(?:${condGroup}\\s*\\?\\s*)?tier\\("([^"]*)",\\s*([^)]+)\\)`, 'g');
const tiers = []; const tiers = [];
let m; let m;
...@@ -2255,7 +2255,7 @@ export function parseTiersFromExpr(exprStr) { ...@@ -2255,7 +2255,7 @@ export function parseTiersFromExpr(exprStr) {
const conditions = []; const conditions = [];
if (condStr) { if (condStr) {
for (const cp of condStr.split(/\s*&&\s*/)) { for (const cp of condStr.split(/\s*&&\s*/)) {
const cm = cp.trim().match(/^(p|c)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/); const cm = cp.trim().match(/^(p|c|len)\s*(<|<=|>|>=)\s*([\d.eE+]+)$/);
if (cm) conditions.push({ var: cm[1], op: cm[2], value: Number(cm[3]) }); if (cm) conditions.push({ var: cm[1], op: cm[2], value: Number(cm[3]) });
} }
} }
...@@ -2293,7 +2293,7 @@ export function renderTieredModelPrice(opts) { ...@@ -2293,7 +2293,7 @@ export function renderTieredModelPrice(opts) {
const { symbol, rate } = getCurrencyConfig(); const { symbol, rate } = getCurrencyConfig();
const gr = groupRatio || 1; const gr = groupRatio || 1;
const priceLines = BILLING_VARS.map((v) => [v.field, v.label]); const priceLines = BILLING_PRICING_VARS.map((v) => [v.field, v.label]);
const lines = [ const lines = [
buildBillingText('命中档位:{{tier}}', { tier: matchedTier || tier.label }), buildBillingText('命中档位:{{tier}}', { tier: matchedTier || tier.label }),
...@@ -2334,7 +2334,7 @@ export function renderTieredModelPriceSimple(opts) { ...@@ -2334,7 +2334,7 @@ export function renderTieredModelPriceSimple(opts) {
]; ];
if (tier && isPriceDisplayMode(displayMode)) { if (tier && isPriceDisplayMode(displayMode)) {
const priceSegments = BILLING_VARS.map((v) => [v.field, v.shortLabel]); const priceSegments = BILLING_PRICING_VARS.map((v) => [v.field, v.shortLabel]);
for (const [field, label] of priceSegments) { for (const [field, label] of priceSegments) {
if (tier[field] > 0) { if (tier[field] > 0) {
segments.push({ segments.push({
......
...@@ -18,7 +18,7 @@ For commercial licensing, please contact support@quantumnous.com ...@@ -18,7 +18,7 @@ For commercial licensing, please contact support@quantumnous.com
*/ */
import { Toast, Pagination } from '@douyinfe/semi-ui'; import { Toast, Pagination } from '@douyinfe/semi-ui';
import { toastConstants, BILLING_VARS, BILLING_VAR_REGEX } from '../constants'; import { toastConstants, BILLING_PRICING_VARS, BILLING_VAR_REGEX } from '../constants';
import React from 'react'; import React from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import { import {
...@@ -927,7 +927,7 @@ export const formatDynamicPriceSummary = (billingExpr, t, groupRatio = 1) => { ...@@ -927,7 +927,7 @@ export const formatDynamicPriceSummary = (billingExpr, t, groupRatio = 1) => {
} }
const hasCoeffs = 'p' in varCoeffs || 'c' in varCoeffs; const hasCoeffs = 'p' in varCoeffs || 'c' in varCoeffs;
const varLabels = BILLING_VARS.map((v) => [v.key, v.label]); const varLabels = BILLING_PRICING_VARS.map((v) => [v.key, v.label]);
const hasTimeCondition = /\b(?:hour|minute|weekday|month|day)\(/.test(exprBody); const hasTimeCondition = /\b(?:hour|minute|weekday|month|day)\(/.test(exprBody);
const hasRequestCondition = /\b(?:param|header)\(/.test(exprBody); const hasRequestCondition = /\b(?:param|header)\(/.test(exprBody);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment