oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/runner"
10 "amadan.net/oficina/equipo/internal/runner/connect"
11 "amadan.net/oficina/equipo/internal/runner/provider"
12 "amadan.net/oficina/equipo/internal/wire"
13)
14
15// errNoReading: the app-server answered, but with no rate limits Equipo
16// can read.
17var errNoReading = errors.New("no reading")
18
19// CodexReading is the production reading probe (connectors spec rev 3.2
20// §6, the controller's ruling #7): a Codex subscription's headroom is
21// refreshed with the app-server's account/rateLimits/read, run as the
22// hourly refresh runs (the source home under its lock, the login
23// probe's environment, no namespace and no egress relay, never a thread
24// or a turn). Claude OAuth usage is read separately by the status check.
25func CodexReading(c runner.Config, read func(context.Context, provider.Binding) (provider.Identity, json.RawMessage, error), wait time.Duration) func(context.Context, runner.AccountBinding) (*connect.Limit, error) {
26 if read == nil {
27 options := provider.LoginOptions{ClaudePath: c.ClaudePath, CodexPath: c.CodexPath}
28 read = func(ctx context.Context, b provider.Binding) (provider.Identity, json.RawMessage, error) {
29 return provider.ReadCodexLimits(ctx, b, options)
30 }
31 }
32 return func(ctx context.Context, b runner.AccountBinding) (*connect.Limit, error) {
33 if b.Provider != wire.ProviderCodex || b.Kind == wire.KindToken || b.Kind == wire.KindAPIKey {
34 return nil, nil
35 }
36 direct, err := c.DirectBinding(b)
37 if err != nil {
38 return nil, err
39 }
40 var raw json.RawMessage
41 lease, err := provider.OpenRuntimeCopy(ctx, direct, func(ctx context.Context, pb provider.Binding) (provider.Identity, error) {
42 id, limits, err := read(ctx, pb)
43 raw = limits
44 return id, err
45 }, wait)
46 if lease != nil {
47 lease.Close()
48 }
49 if err != nil {
50 return nil, err
51 }
52 l, _, ok := connect.CodexRateLimits(raw)
53 if !ok {
54 return nil, errNoReading
55 }
56 return &l, nil
57 }
58}
59