ce3482b057
Read V2 session accounting, latest assistant prompt/cache components and native request-to-stream boundary. Resolve capacity by location/provider/catalog id with bounded metadata caching. Coalesce active/final readbacks; retain message-less session snapshots without changing admission or compaction state. Provenance: https://opencode.ai/v2/openapi.json and anomalyco/opencode V2 4617210822bdbb31e5749751afbfb4584ccedac8: packages/schema/src/token-usage.ts, session-event.ts; packages/core/src/session/usage.ts, projector.ts, message-updater.ts, runner/step.ts. Native output is visible output, reasoning separate. Session totals include all five non-overlapping components. Generation rate uses created-to-streamed, not tool-inclusive completion. Limitations: metrics remain unknown for missing components, absent timing/model metadata, reads beyond bounded latest-message/catalog budgets, or context preceding compaction. No local pricing, token estimation or default capacity. V1 gets no invented cumulative metrics; older V2 events remain available when readback is unavailable. No deployment or protocol/web/docs changes. Checks: go test ./...; focused usage/stream race tests; go vet ./internal/opencode ./internal/server; go build ./...; git diff --check.
220 lines
7.0 KiB
Go
220 lines
7.0 KiB
Go
package opencode
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// Provenance: OpenCode V2 4617210822bdbb31e5749751afbfb4584ccedac8,
|
|
// packages/schema/src/token-usage.ts, session-event.ts and
|
|
// packages/core/src/session/{usage,projector,message-updater}.ts, runner/step.ts.
|
|
// UsageUpdated is a cumulative projection, not a delta. Native output is visible
|
|
// output (reasoning is separate); input excludes cache. Never price locally.
|
|
// message-updater sets created from Step.Started.started (request dispatch),
|
|
// streamed at Step.Streamed (provider body exhausted), completed after tools.
|
|
// Thus created->streamed is generation incl. provider latency, NOT tool time.
|
|
type usageTokens struct {
|
|
Input *int64 `json:"input"`
|
|
Output *int64 `json:"output"`
|
|
Reasoning *int64 `json:"reasoning"`
|
|
Cache struct {
|
|
Read *int64 `json:"read"`
|
|
Write *int64 `json:"write"`
|
|
} `json:"cache"`
|
|
}
|
|
|
|
func tokenSum(values ...*int64) *int64 {
|
|
var total int64
|
|
for _, value := range values {
|
|
if value == nil || *value < 0 || *value > math.MaxInt64-total {
|
|
return nil
|
|
}
|
|
total += *value
|
|
}
|
|
return &total
|
|
}
|
|
|
|
func decodeUsageSnapshot(data []byte) *protocol.ChatUsage {
|
|
var record struct {
|
|
Tokens usageTokens `json:"tokens"`
|
|
Cost *float64 `json:"cost"`
|
|
Input int `json:"inputTokens"`
|
|
Output int `json:"outputTokens"`
|
|
}
|
|
if json.Unmarshal(data, &record) != nil {
|
|
return &protocol.ChatUsage{Scope: "session"}
|
|
}
|
|
u := &protocol.ChatUsage{Scope: "session", InputTokens: record.Input, OutputTokens: record.Output}
|
|
if record.Tokens.Input != nil {
|
|
u.InputTokens = int(*record.Tokens.Input)
|
|
}
|
|
if record.Tokens.Output != nil {
|
|
u.OutputTokens = int(*record.Tokens.Output)
|
|
}
|
|
u.TotalTokens = tokenSum(record.Tokens.Input, record.Tokens.Output, record.Tokens.Reasoning, record.Tokens.Cache.Read, record.Tokens.Cache.Write)
|
|
if record.Cost != nil && *record.Cost >= 0 && !math.IsNaN(*record.Cost) && !math.IsInf(*record.Cost, 0) {
|
|
u.CostUSD = record.Cost
|
|
u.Cost = *record.Cost
|
|
}
|
|
return u
|
|
}
|
|
|
|
type usageModelCache struct {
|
|
limit *int64
|
|
at time.Time
|
|
}
|
|
type usageMessage struct {
|
|
Type string `json:"type"`
|
|
Tokens usageTokens `json:"tokens"`
|
|
Time struct {
|
|
Created *float64 `json:"created"`
|
|
Streamed *float64 `json:"streamed"`
|
|
Completed *float64 `json:"completed"`
|
|
} `json:"time"`
|
|
}
|
|
|
|
// Usage reads at most one session, one latest assistant and one latest compaction.
|
|
// No unbounded history, CLI export, model/provider creation or inference. Missing
|
|
// fields remain unknown. After compaction an old prompt's usage is not current.
|
|
func (c *Chat) Usage(ctx context.Context, id string) (*protocol.ChatUsage, error) {
|
|
if !ValidSessionID(id) {
|
|
return nil, fmt.Errorf("invalid OpenCode session id")
|
|
}
|
|
if c.conn.Backend == "v1" {
|
|
return &protocol.ChatUsage{Scope: "session"}, nil
|
|
}
|
|
data, err := c.call(ctx, "get", "/api/session/"+id, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var session struct {
|
|
Data json.RawMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(data, &session) != nil || len(session.Data) == 0 {
|
|
return nil, fmt.Errorf("invalid OpenCode usage session")
|
|
}
|
|
u := decodeUsageSnapshot(session.Data)
|
|
var info struct {
|
|
ID string `json:"id"`
|
|
Model struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"providerID"`
|
|
} `json:"model"`
|
|
Location struct {
|
|
Directory string `json:"directory"`
|
|
} `json:"location"`
|
|
}
|
|
if json.Unmarshal(session.Data, &info) != nil || info.ID != id {
|
|
return nil, fmt.Errorf("invalid OpenCode usage session")
|
|
}
|
|
u.ContextWindow = c.usageModelLimit(ctx, info.Location.Directory, info.Model.Provider, info.Model.ID)
|
|
latest := func(kind string) (*usageMessage, bool) {
|
|
body, e := c.usageRead(ctx, "/api/session/"+id+"/message?type="+kind+"&order=desc&limit=1", c.conn.Directory, 4<<20)
|
|
if e != nil {
|
|
return nil, false
|
|
}
|
|
var page struct {
|
|
Data []usageMessage `json:"data"`
|
|
}
|
|
if json.Unmarshal(body, &page) != nil || page.Data == nil || len(page.Data) > 1 {
|
|
return nil, false
|
|
}
|
|
if len(page.Data) == 0 {
|
|
return nil, true
|
|
}
|
|
if page.Data[0].Type != kind {
|
|
return nil, false
|
|
}
|
|
return &page.Data[0], true
|
|
}
|
|
a, _ := latest("assistant")
|
|
if a == nil {
|
|
return u, nil
|
|
}
|
|
compaction, boundaryKnown := latest("compaction")
|
|
// Failure to establish the compaction boundary must not revive old context.
|
|
// Empty compaction pages are valid, unlike failed/malformed reads.
|
|
if boundaryKnown && a.Time.Created != nil && (compaction == nil || (compaction.Time.Created != nil && *compaction.Time.Created < *a.Time.Created)) {
|
|
u.ContextTokens = tokenSum(a.Tokens.Input, a.Tokens.Cache.Read, a.Tokens.Cache.Write)
|
|
}
|
|
if a.Time.Completed != nil && a.Time.Created != nil && a.Time.Streamed != nil && *a.Time.Created >= 0 && *a.Time.Streamed > *a.Time.Created && *a.Time.Completed >= *a.Time.Streamed && a.Tokens.Output != nil && *a.Tokens.Output >= 0 {
|
|
rate := float64(*a.Tokens.Output) * 1000 / (*a.Time.Streamed - *a.Time.Created)
|
|
if !math.IsNaN(rate) && !math.IsInf(rate, 0) {
|
|
u.OutputTokensPerSecond = &rate
|
|
u.RateBasis = "generation"
|
|
}
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
// Reuses the authenticated, location-scoped transport, with an explicit bounded
|
|
// allowance for large model catalogs. It never changes the shared connection's
|
|
// directory or stage export/format pin. Metadata is cached by catalog id AND
|
|
// provider AND location, not the upstream modelID (aliases can differ).
|
|
func (c *Chat) usageRead(ctx context.Context, path, directory string, budget int64) ([]byte, error) {
|
|
c.mu.Lock()
|
|
conn := *c.conn
|
|
c.mu.Unlock()
|
|
conn.Directory = directory
|
|
body, err := conn.HistoryNative(ctx, path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer body.Close()
|
|
data, err := io.ReadAll(io.LimitReader(body, budget+1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(len(data)) > budget {
|
|
return nil, fmt.Errorf("OpenCode metrics read exceeds budget")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func (c *Chat) usageModelLimit(ctx context.Context, directory, provider, id string) *int64 {
|
|
if directory == "" || provider == "" || id == "" {
|
|
return nil
|
|
}
|
|
key := directory + "\x00" + provider + "\x00" + id
|
|
c.usageMu.Lock()
|
|
cached, ok := c.usageModels[key]
|
|
c.usageMu.Unlock()
|
|
if ok && time.Since(cached.at) < 5*time.Minute {
|
|
return cached.limit
|
|
}
|
|
var limit *int64
|
|
data, err := c.usageRead(ctx, "/api/model", directory, 16<<20)
|
|
if err == nil {
|
|
var catalog struct {
|
|
Data []struct {
|
|
ID string `json:"id"`
|
|
Provider string `json:"providerID"`
|
|
Limit struct {
|
|
Context *int64 `json:"context"`
|
|
} `json:"limit"`
|
|
} `json:"data"`
|
|
}
|
|
if json.Unmarshal(data, &catalog) == nil {
|
|
for _, m := range catalog.Data {
|
|
if m.ID == id && m.Provider == provider && m.Limit.Context != nil && *m.Limit.Context >= 0 {
|
|
limit = m.Limit.Context
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
c.usageMu.Lock()
|
|
if c.usageModels == nil || len(c.usageModels) >= 128 {
|
|
c.usageModels = make(map[string]usageModelCache)
|
|
}
|
|
c.usageModels[key] = usageModelCache{limit: limit, at: time.Now()}
|
|
c.usageMu.Unlock()
|
|
return limit
|
|
}
|