Chat info: account quota per harness (Claude rate_limit_event, Codex account/rateLimits, OpenCode unsupported)
Usage frames carry optional usage.account {harness, source, plan, as_of,
windows[{id,label,used_percent,resets_at}], unsupported}. Claude Code quota
comes from its rate_limit_event stream lines and the initialize account
(plan), shared across chats on the same account. Codex reads
account/rateLimits/read once per connection (re-read when a chat opens after
5 min) and merges account/rateLimits/updated. OpenCode exposes no quota API
and says so. The app's chat info sheet shows an Account usage section with a
progress bar and reset countdown per window, Material and Cupertino.
This commit is contained in:
@@ -38,7 +38,7 @@ func TestNormalizeChatEvent(t *testing.T) {
|
||||
{name: "tool running", payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolRunning},
|
||||
{name: "tool completed", payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","content":[{"type":"text","text":"ok"}]}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolCompleted},
|
||||
{name: "tool failed", payload: `{"type":"session.tool.failed","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","error":{"type":"tool","message":"boom"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolError},
|
||||
{name: "usage", payload: `{"type":"session.usage.updated","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","tokens":{"input":12,"output":34},"cost":0.001}}`, wantOK: true, wantKind: protocol.ChatEventUsage, wantUsage: &protocol.ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001, Scope: "session", CostUSD: usageFloat(.001)}},
|
||||
{name: "usage", payload: `{"type":"session.usage.updated","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","tokens":{"input":12,"output":34},"cost":0.001}}`, wantOK: true, wantKind: protocol.ChatEventUsage, wantUsage: &protocol.ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001, Scope: "session", CostUSD: usageFloat(.001), Account: &protocol.ChatAccountUsage{Harness: "OpenCode", Unsupported: true}}},
|
||||
{name: "message id fallback", payload: `{"type":"session.step.started","data":{"sessionID":"ses_1","index":1}}`, state: prior, wantOK: true, wantKind: protocol.ChatEventStep, wantMsg: "msg_prev", wantStep: protocol.ChatStepStarted},
|
||||
{name: "foreign session skipped", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_other","delta":"x"}}`, wantOK: false},
|
||||
{name: "unknown type skipped", payload: `{"type":"session.created","data":{"sessionID":"ses_1"}}`, wantOK: false},
|
||||
|
||||
@@ -40,6 +40,16 @@ func tokenSum(values ...*int64) *int64 {
|
||||
return &total
|
||||
}
|
||||
|
||||
// openCodeAccount says OpenCode reports no account quota. OpenCode 2.0.32's
|
||||
// API (provider, integration, model and session routes) has no plan, usage or
|
||||
// quota read, and its only rate-limit data is the x-ratelimit-* headers of a
|
||||
// failed provider request (AI.Error.RateLimit), which is neither a plan quota
|
||||
// nor available before a limit is hit. That covers the OpenCode Go and Zen
|
||||
// subscriptions too. Nothing is estimated in its place.
|
||||
func openCodeAccount() *protocol.ChatAccountUsage {
|
||||
return &protocol.ChatAccountUsage{Harness: "OpenCode", Unsupported: true}
|
||||
}
|
||||
|
||||
func decodeUsageSnapshot(data []byte) *protocol.ChatUsage {
|
||||
var record struct {
|
||||
Tokens usageTokens `json:"tokens"`
|
||||
@@ -48,9 +58,9 @@ func decodeUsageSnapshot(data []byte) *protocol.ChatUsage {
|
||||
Output int `json:"outputTokens"`
|
||||
}
|
||||
if json.Unmarshal(data, &record) != nil {
|
||||
return &protocol.ChatUsage{Scope: "session"}
|
||||
return &protocol.ChatUsage{Scope: "session", Account: openCodeAccount()}
|
||||
}
|
||||
u := &protocol.ChatUsage{Scope: "session", InputTokens: record.Input, OutputTokens: record.Output}
|
||||
u := &protocol.ChatUsage{Scope: "session", InputTokens: record.Input, OutputTokens: record.Output, Account: openCodeAccount()}
|
||||
if record.Tokens.Input != nil {
|
||||
u.InputTokens = int(*record.Tokens.Input)
|
||||
}
|
||||
@@ -96,7 +106,7 @@ func (c *Chat) Usage(ctx context.Context, id string) (*protocol.ChatUsage, error
|
||||
return nil, fmt.Errorf("invalid OpenCode session id")
|
||||
}
|
||||
if c.conn.Backend == "v1" {
|
||||
return &protocol.ChatUsage{Scope: "session"}, nil
|
||||
return &protocol.ChatUsage{Scope: "session", Account: openCodeAccount()}, nil
|
||||
}
|
||||
data, err := c.call(ctx, "get", "/api/session/"+id, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user