198 lines
5.1 KiB
Go
198 lines
5.1 KiB
Go
package appui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"math"
|
||
|
|
|
||
|
|
"github.com/egoist/asuka/colors"
|
||
|
|
"github.com/egoist/asuka/fonts"
|
||
|
|
"github.com/egoist/asuka/signal"
|
||
|
|
"github.com/egoist/asuka/ui"
|
||
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||
|
|
)
|
||
|
|
|
||
|
|
// modelLimit is a model's context and output window.
|
||
|
|
type modelLimit struct {
|
||
|
|
Context int
|
||
|
|
Output int
|
||
|
|
}
|
||
|
|
|
||
|
|
// usageReading is the context window of the last assistant message that
|
||
|
|
// reported tokens, as the first-party client shows it. The output
|
||
|
|
// reservation is capped at 32k tokens, matching its calculation.
|
||
|
|
type usageReading struct {
|
||
|
|
Used int
|
||
|
|
Limit int
|
||
|
|
Output int
|
||
|
|
Pct float64
|
||
|
|
Known bool
|
||
|
|
Cost float64
|
||
|
|
Model string
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ch *chat) usageReading(msgs []ocapi.Message) usageReading {
|
||
|
|
var last *ocapi.Message
|
||
|
|
for i := len(msgs) - 1; i >= 0; i-- {
|
||
|
|
if msgs[i].Type == "assistant" && msgs[i].Tokens != nil {
|
||
|
|
last = &msgs[i]
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if last == nil {
|
||
|
|
return usageReading{}
|
||
|
|
}
|
||
|
|
// A message the event stream added has no model yet; the session's model
|
||
|
|
// is the latest one any message reports.
|
||
|
|
model := last.Model
|
||
|
|
for i := len(msgs) - 1; i >= 0 && model == nil; i-- {
|
||
|
|
if msgs[i].Type == "assistant" && msgs[i].Model != nil {
|
||
|
|
model = msgs[i].Model
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if model == nil {
|
||
|
|
return usageReading{}
|
||
|
|
}
|
||
|
|
var r usageReading
|
||
|
|
t := last.Tokens
|
||
|
|
if t.Total > 0 {
|
||
|
|
r.Used = int(t.Total)
|
||
|
|
} else {
|
||
|
|
r.Used = int(t.Input + t.Output + t.Reasoning + t.Cache.Read + t.Cache.Write)
|
||
|
|
}
|
||
|
|
lim := ch.modelLimit(model)
|
||
|
|
r.Limit, r.Output = lim.Context, lim.Output
|
||
|
|
r.Model = ch.modelName(model)
|
||
|
|
// The turn's cost is every step after its prompt.
|
||
|
|
for i := len(msgs) - 1; i >= 0; i-- {
|
||
|
|
if msgs[i].Type == "user" {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
if msgs[i].Type == "assistant" {
|
||
|
|
r.Cost += msgs[i].Cost
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if r.Limit <= 0 || r.Used <= 0 {
|
||
|
|
return r
|
||
|
|
}
|
||
|
|
reserve := r.Output
|
||
|
|
if reserve > 32000 {
|
||
|
|
reserve = 32000
|
||
|
|
}
|
||
|
|
threshold := r.Limit - reserve
|
||
|
|
if threshold < 1 {
|
||
|
|
threshold = 1
|
||
|
|
}
|
||
|
|
r.Pct = math.Min(100, float64(r.Used)/float64(threshold)*100)
|
||
|
|
r.Known = true
|
||
|
|
return r
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ch *chat) contextUsage() usageReading {
|
||
|
|
return ch.usageReading(ch.msgs.Peek())
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ch *chat) modelLimit(m *ocapi.ModelRef) modelLimit {
|
||
|
|
if m == nil {
|
||
|
|
return modelLimit{}
|
||
|
|
}
|
||
|
|
limits := ch.modelLimits.Get()
|
||
|
|
if l, ok := limits[m.ProviderID+"/"+m.ID]; ok {
|
||
|
|
return l
|
||
|
|
}
|
||
|
|
return limits[m.ID]
|
||
|
|
}
|
||
|
|
|
||
|
|
func formatTokens(n int) string {
|
||
|
|
switch {
|
||
|
|
case n >= 1_000_000:
|
||
|
|
return fmt.Sprintf("%.1fM", float64(n)/1_000_000)
|
||
|
|
case n >= 1_000:
|
||
|
|
return fmt.Sprintf("%.1fK", float64(n)/1_000)
|
||
|
|
default:
|
||
|
|
return fmt.Sprintf("%d", n)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// usageTone follows the first-party thresholds: 75% warns, 90% errors.
|
||
|
|
func usageTone(pct float64) ui.Color {
|
||
|
|
switch {
|
||
|
|
case pct >= 90:
|
||
|
|
return colors.Red
|
||
|
|
case pct >= 75:
|
||
|
|
return colors.Orange
|
||
|
|
default:
|
||
|
|
return colors.Green
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// usageButton is the composer's context ring and percentage.
|
||
|
|
func (ch *chat) usageButton() ui.Node {
|
||
|
|
return ui.Dynamic(func() ui.Node {
|
||
|
|
r := ch.usageReading(ch.msgs.Get())
|
||
|
|
if !r.Known {
|
||
|
|
return ui.View(ui.ViewProps{})
|
||
|
|
}
|
||
|
|
progress := signal.Func(func() float64 { return r.Pct / 100 })
|
||
|
|
return ui.View(ui.ViewProps{
|
||
|
|
OnPress: func() { ch.usageOpen.Set(true) },
|
||
|
|
Label: ui.Val("上下文占用"),
|
||
|
|
Hint: ui.Val("查看上下文详情"),
|
||
|
|
Style: ui.Style{Padding: ui.All(4), Align: ui.AlignCenter},
|
||
|
|
}, ui.Row(ui.Style{Gap: 4, Align: ui.AlignCenter},
|
||
|
|
ui.ProgressRing(progress, ui.Style{Width: ui.Pt(18), Height: ui.Pt(18), Color: usageTone(r.Pct)}),
|
||
|
|
ui.StaticText(fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999)),
|
||
|
|
ui.Style{Font: fonts.Caption, Color: colors.SecondaryLabel}),
|
||
|
|
))
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ch *chat) usageSheet() ui.Node {
|
||
|
|
return ui.Sheet(ui.SheetProps{
|
||
|
|
Open: ch.usageOpen,
|
||
|
|
Detents: ui.DetentMedium,
|
||
|
|
Grabber: true,
|
||
|
|
OnDismiss: func() { ch.usageOpen.Set(false) },
|
||
|
|
Content: func() ui.Node {
|
||
|
|
return ui.Dynamic(func() ui.Node {
|
||
|
|
r := ch.usageReading(ch.msgs.Get())
|
||
|
|
rows := []ui.Node{usageRow("模型", r.Model)}
|
||
|
|
if r.Known {
|
||
|
|
rows = append(rows,
|
||
|
|
usageRow("已用 tokens", formatTokens(r.Used)),
|
||
|
|
usageRow("上下文上限", formatTokens(r.Limit)),
|
||
|
|
usageRow("输出预留", formatTokens(r.Output)),
|
||
|
|
usageRow("占用", fmt.Sprintf("%.1f%%", math.Min(r.Pct, 999))),
|
||
|
|
)
|
||
|
|
} else {
|
||
|
|
rows = append(rows,
|
||
|
|
usageRow("已用 tokens", formatTokens(r.Used)),
|
||
|
|
usageRow("上下文上限", "未知"),
|
||
|
|
)
|
||
|
|
}
|
||
|
|
if r.Cost > 0 {
|
||
|
|
rows = append(rows, usageRow("本轮费用", formatCost(r.Cost)))
|
||
|
|
}
|
||
|
|
return ui.SafeArea(ui.Style{Padding: ui.All(20), Gap: 10},
|
||
|
|
ui.StaticText("上下文占用", ui.Style{Font: fonts.Title2.Weighted(fonts.Bold)}),
|
||
|
|
ui.Column(ui.Style{Gap: 10}, rows...),
|
||
|
|
ui.StaticText("输出预留按最多 32K tokens 计算,与官方客户端一致。",
|
||
|
|
ui.Style{Font: fonts.Footnote, Color: colors.SecondaryLabel}),
|
||
|
|
)
|
||
|
|
})
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func formatCost(v float64) string {
|
||
|
|
if v < 0.01 {
|
||
|
|
return fmt.Sprintf("$%.4f", v)
|
||
|
|
}
|
||
|
|
return fmt.Sprintf("$%.2f", v)
|
||
|
|
}
|
||
|
|
|
||
|
|
func usageRow(label, value string) ui.Node {
|
||
|
|
return ui.Row(ui.Style{Gap: 12, Align: ui.AlignCenter},
|
||
|
|
ui.StaticText(label, ui.Style{Font: fonts.Body, Color: colors.SecondaryLabel, Grow: 1}),
|
||
|
|
ui.StaticText(value, ui.Style{Font: fonts.Body}),
|
||
|
|
)
|
||
|
|
}
|