types.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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)
  15. C float64 // completion tokens (text)
  16. CR float64 // cache read (hit) tokens
  17. CC float64 // cache creation tokens (5-min TTL for Claude, generic for others)
  18. CC1h float64 // cache creation tokens — 1-hour TTL (Claude only)
  19. Img float64 // image input tokens
  20. AI float64 // audio input tokens
  21. AO float64 // audio output tokens
  22. }
  23. // TraceResult holds side-channel info captured by the tier() function
  24. // during Expr execution. This replaces the old Breakdown mechanism —
  25. // the Expr itself is the single source of truth for billing logic.
  26. type TraceResult struct {
  27. MatchedTier string `json:"matched_tier"`
  28. Cost float64 `json:"cost"`
  29. }
  30. // BillingSnapshot captures the billing rule state frozen at pre-consume time.
  31. // It is fully serializable and contains no compiled program pointers.
  32. type BillingSnapshot struct {
  33. BillingMode string `json:"billing_mode"`
  34. ModelName string `json:"model_name"`
  35. ExprString string `json:"expr_string"`
  36. ExprHash string `json:"expr_hash"`
  37. GroupRatio float64 `json:"group_ratio"`
  38. EstimatedPromptTokens int `json:"estimated_prompt_tokens"`
  39. EstimatedCompletionTokens int `json:"estimated_completion_tokens"`
  40. EstimatedQuotaBeforeGroup float64 `json:"estimated_quota_before_group"`
  41. EstimatedQuotaAfterGroup int `json:"estimated_quota_after_group"`
  42. EstimatedTier string `json:"estimated_tier"`
  43. QuotaPerUnit float64 `json:"quota_per_unit"`
  44. }
  45. // TieredResult holds everything needed after running tiered settlement.
  46. type TieredResult struct {
  47. ActualQuotaBeforeGroup float64 `json:"actual_quota_before_group"`
  48. ActualQuotaAfterGroup int `json:"actual_quota_after_group"`
  49. MatchedTier string `json:"matched_tier"`
  50. CrossedTier bool `json:"crossed_tier"`
  51. }
  52. // ExprHashString returns the SHA-256 hex digest of an expression string.
  53. func ExprHashString(expr string) string {
  54. h := sha256.Sum256([]byte(expr))
  55. return fmt.Sprintf("%x", h)
  56. }