types.go 2.5 KB

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