package session import ( "fmt" "math" "slices" "tangled.org/guid.foo/simmer/api/v1alpha1" "tangled.org/guid.foo/simmer/cluster" ) // Payer names the Allowance that pays for a turn, and its Account. type Payer struct { Allowance string Account string } // NoteResult records the token total and the cost that the Session's // running turn reported, and whether it resumed the conversation. // Claude Code's result counts every turn of the conversation, because // a resumed conversation keeps the usage of the turns before it, so a // resumed turn's own tokens and cost are those totals less the totals // of the latest earlier turn that reported them. A turn that started // the conversation, or whose total is below that one, takes all of it. // A turn that reported no total, such as one that was interrupted, // leaves its tokens and cost in the next turn's share. A turn that // resumes an older transcript, because the last turn's upload failed, // is counted against the last turn's total, so its share is wrong in // either direction. func NoteResult(c *cluster.Cluster, s *v1alpha1.Session, payer Payer, tokens int64, cost float64, resumed bool) error { return noteUsage(c, s, payer, func(turns []v1alpha1.Turn, u *v1alpha1.TurnUsage) { u.SessionTokens, u.Tokens = tokens, tokens if before := sessionTokens(turns); resumed && before <= tokens { u.Tokens = tokens - before } u.SessionCostUSD, u.CostUSD = cost, cost if before := sessionCost(turns); resumed && before <= cost { u.CostUSD = math.Round((cost-before)*1e6) / 1e6 } }) } // sessionTokens answers the Session's token total at the end of the // latest of turns that reported one, or 0. A turn recorded before // usage had sessionTokens reported the total as its tokens. func sessionTokens(turns []v1alpha1.Turn) int64 { for _, turn := range slices.Backward(turns) { switch u := turn.Usage; { case u == nil: case u.SessionTokens > 0: return u.SessionTokens case u.Tokens > 0: return u.Tokens } } return 0 } // sessionCost answers the Session's cost at the end of the latest of // turns that reported one, or 0. func sessionCost(turns []v1alpha1.Turn) float64 { for _, turn := range slices.Backward(turns) { if u := turn.Usage; u != nil && u.SessionCostUSD > 0 { return u.SessionCostUSD } } return 0 } // Cost writes the cost of every turn with two decimals, such as $1.12, // or nothing when no turn has reported one. func Cost(turns []v1alpha1.Turn) string { var total float64 for _, turn := range turns { if turn.Usage != nil { total += turn.Usage.CostUSD } } if total == 0 { return "" } return fmt.Sprintf("$%.2f", total) } // noteUsage changes the usage of the Session's running turn, and hands // note the turns before it. func noteUsage(c *cluster.Cluster, s *v1alpha1.Session, payer Payer, note func([]v1alpha1.Turn, *v1alpha1.TurnUsage)) error { if !turnRuns(s) { return nil } return cluster.WriteStatus(c, cluster.Sessions, s, func(o *v1alpha1.Session) { if !turnRuns(o) { return } turn := &o.Status.Turns[len(o.Status.Turns)-1] usage := v1alpha1.TurnUsage{Allowance: payer.Allowance, Account: payer.Account} if turn.Usage != nil { usage = *turn.Usage } note(o.Status.Turns[:len(o.Status.Turns)-1], &usage) turn.Usage = &usage o.Status.Cost = Cost(o.Status.Turns) }, sessionConditions, isBadSession) } func turnRuns(s *v1alpha1.Session) bool { turns := s.Status.Turns return len(turns) > 0 && turns[len(turns)-1].Outcome == "" }