Read Codex rate limits at startup so Status always shows them
This commit is contained in:
+57
-20
@@ -116,6 +116,45 @@ type Session struct {
|
||||
}
|
||||
|
||||
func Start(ctx context.Context, opts Options) (*Session, error) {
|
||||
s, err := spawn(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.open(ctx, opts)
|
||||
if err != nil {
|
||||
s.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func ReadRateLimits(ctx context.Context, cwd string, env []string) (RateLimitSnapshot, error) {
|
||||
s, err := spawn(Options{Cwd: cwd, Env: env})
|
||||
if err != nil {
|
||||
return RateLimitSnapshot{}, err
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
go func() {
|
||||
for range s.events {
|
||||
}
|
||||
}()
|
||||
|
||||
err = s.initialize(ctx)
|
||||
if err != nil {
|
||||
return RateLimitSnapshot{}, err
|
||||
}
|
||||
|
||||
resp := GetAccountRateLimitsResponse{}
|
||||
err = s.Call(ctx, MethodAccountRateLimitsRead, GetAccountRateLimitsParams{ExcludeResetCreditDetails: true}, &resp)
|
||||
if err != nil {
|
||||
return RateLimitSnapshot{}, fmt.Errorf("read rate limits: %w", err)
|
||||
}
|
||||
return resp.RateLimits, nil
|
||||
}
|
||||
|
||||
func spawn(opts Options) (*Session, error) {
|
||||
cmd := exec.Command("codex", "app-server", "-c", "check_for_update_on_startup=false")
|
||||
cmd.Dir = opts.Cwd
|
||||
cmd.Stderr = os.Stderr
|
||||
@@ -138,12 +177,6 @@ func Start(ctx context.Context, opts Options) (*Session, error) {
|
||||
|
||||
s := newSession(opts, stdin, stdout)
|
||||
s.cmd = cmd
|
||||
|
||||
err = s.open(ctx, opts)
|
||||
if err != nil {
|
||||
s.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -167,20 +200,7 @@ func newSession(opts Options, stdin io.WriteCloser, stdout io.Reader) *Session {
|
||||
}
|
||||
|
||||
func (s *Session) open(ctx context.Context, opts Options) error {
|
||||
init := InitializeResponse{}
|
||||
err := s.Call(ctx, MethodInitialize, InitializeParams{
|
||||
ClientInfo: ClientInfo{Name: "byre", Title: "byre", Version: "0.0.0"},
|
||||
Capabilities: &InitializeCapabilities{ExperimentalApi: true},
|
||||
}, &init)
|
||||
if err != nil {
|
||||
return fmt.Errorf("initialize: %w", err)
|
||||
}
|
||||
|
||||
s.stateMu.Lock()
|
||||
s.userAgent = init.UserAgent
|
||||
s.stateMu.Unlock()
|
||||
|
||||
err = s.notify("initialized", nil)
|
||||
err := s.initialize(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -239,6 +259,23 @@ func (s *Session) open(ctx context.Context, opts Options) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Session) initialize(ctx context.Context) error {
|
||||
init := InitializeResponse{}
|
||||
err := s.Call(ctx, MethodInitialize, InitializeParams{
|
||||
ClientInfo: ClientInfo{Name: "byre", Title: "byre", Version: "0.0.0"},
|
||||
Capabilities: &InitializeCapabilities{ExperimentalApi: true},
|
||||
}, &init)
|
||||
if err != nil {
|
||||
return fmt.Errorf("initialize: %w", err)
|
||||
}
|
||||
|
||||
s.stateMu.Lock()
|
||||
s.userAgent = init.UserAgent
|
||||
s.stateMu.Unlock()
|
||||
|
||||
return s.notify("initialized", nil)
|
||||
}
|
||||
|
||||
func stdioConfig(servers []mcp.Stdio) map[string]any {
|
||||
if len(servers) == 0 {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user