types.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package billingexpr
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. )
  6. type RequestInput struct {
  7. Headers map[string]string
  8. Body []byte
  9. }
  10. // TokenParams holds all token dimensions passed into an Expr evaluation.
  11. // Fields beyond P and C are optional — when absent they default to 0,
  12. // which means cache-unaware expressions keep working unchanged.
  13. type TokenParams struct {
  14. P float64 // prompt tokens (text) — auto-excludes sub-categories priced separately
  15. C float64 // completion tokens (text) — auto-excludes sub-categories priced separately
  16. Len float64 // total input context length for tier conditions (non-Claude: raw prompt_tokens; Claude: text + cache read + cache creation)
  17. CR float64 // cache read (hit) tokens
  18. CC float64 // cache creation tokens (5-min TTL for Claude, generic for others)
  19. CC1h float64 // cache creation tokens — 1-hour TTL (Claude only)
  20. Img float64 // image input tokens
  21. ImgO float64 // image output tokens
  22. AI float64 // audio input tokens
  23. AO float64 // audio output tokens
  24. }
  25. // TraceResult holds side-channel info captured by the tier() function
  26. // during Expr execution. This replaces the old Breakdown mechanism —
  27. // the Expr itself is the single source of truth for billing logic.
  28. type TraceResult struct {
  29. MatchedTier string `json:"matched_tier"`
  30. Cost float64 `json:"cost"`
  31. }
  32. // BillingSnapshot captures the billing rule state frozen at pre-consume time.
  33. // It is fully serializable and contains no compiled program pointers.
  34. type BillingSnapshot struct {
  35. BillingMode string `json:"billing_mode"`
  36. ModelName string `json:"model_name"`
  37. ExprString string `json:"expr_string"`
  38. ExprHash string `json:"expr_hash"`
  39. GroupRatio float64 `json:"group_ratio"`
  40. EstimatedPromptTokens int `json:"estimated_prompt_tokens"`
  41. EstimatedCompletionTokens int `json:"estimated_completion_tokens"`
  42. EstimatedQuotaBeforeGroup float64 `json:"estimated_quota_before_group"`
  43. EstimatedQuotaAfterGroup int `json:"estimated_quota_after_group"`
  44. EstimatedTier string `json:"estimated_tier"`
  45. QuotaPerUnit float64 `json:"quota_per_unit"`
  46. ExprVersion int `json:"expr_version"`
  47. }
  48. // TieredResult holds everything needed after running tiered settlement.
  49. type TieredResult struct {
  50. ActualQuotaBeforeGroup float64 `json:"actual_quota_before_group"`
  51. ActualQuotaAfterGroup int `json:"actual_quota_after_group"`
  52. MatchedTier string `json:"matched_tier"`
  53. CrossedTier bool `json:"crossed_tier"`
  54. }
  55. // ExprHashString returns the SHA-256 hex digest of an expression string.
  56. func ExprHashString(expr string) string {
  57. h := sha256.Sum256([]byte(expr))
  58. return fmt.Sprintf("%x", h)
  59. }