Read Codex rate limits at startup so Status always shows them
This commit is contained in:
@@ -4,8 +4,11 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
"g.fc.run/misc/byre/internal/codex"
|
||||
)
|
||||
|
||||
@@ -66,9 +69,7 @@ func (m *Manager) handleCodex(ctx context.Context, l *live, s *codex.Session, ev
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.rateLimits[l.vendor] = codexRateLimit(n.RateLimits)
|
||||
m.mu.Unlock()
|
||||
m.setCodexRateLimit(n.RateLimits)
|
||||
return nil
|
||||
|
||||
case codex.MethodTurnCompleted:
|
||||
@@ -194,6 +195,29 @@ func codexToolResult(item codex.ThreadItem) (string, bool) {
|
||||
return status, status != "completed"
|
||||
}
|
||||
|
||||
func (m *Manager) readCodexRateLimit() {
|
||||
err := os.MkdirAll(m.codexHome, 0o700)
|
||||
if err != nil {
|
||||
slog.Error("read codex rate limits", "err", err)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
r, err := codex.ReadRateLimits(ctx, m.codexHome, []string{"CODEX_HOME=" + m.codexHome})
|
||||
if err != nil {
|
||||
slog.Error("read codex rate limits", "err", err)
|
||||
return
|
||||
}
|
||||
m.setCodexRateLimit(r)
|
||||
}
|
||||
|
||||
func (m *Manager) setCodexRateLimit(r codex.RateLimitSnapshot) {
|
||||
m.mu.Lock()
|
||||
m.rateLimits[api.Codex] = codexRateLimit(r)
|
||||
m.mu.Unlock()
|
||||
m.store.Broadcast(api.Event{Kind: "admin.health", Actor: "byre", Payload: map[string]any{}})
|
||||
}
|
||||
|
||||
func codexRateLimit(r codex.RateLimitSnapshot) map[string]any {
|
||||
status := "allowed"
|
||||
if r.RateLimitReachedType != "" {
|
||||
|
||||
Reference in New Issue
Block a user