types.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }
  44. // TieredResult holds everything needed after running tiered settlement.
  45. type TieredResult struct {
  46. ActualQuotaBeforeGroup float64 `json:"actual_quota_before_group"`
  47. ActualQuotaAfterGroup int `json:"actual_quota_after_group"`
  48. MatchedTier string `json:"matched_tier"`
  49. CrossedTier bool `json:"crossed_tier"`
  50. }
  51. // ExprHashString returns the SHA-256 hex digest of an expression string.
  52. func ExprHashString(expr string) string {
  53. h := sha256.Sum256([]byte(expr))
  54. return fmt.Sprintf("%x", h)
  55. }