Files
CLIProxyAPI/internal/pluginhost/routing_callbacks.go
Luis Pater 8348923a17 feat(pluginhost): add host routing reset cooldown callback
- Add `MethodHostRoutingResetCooldown` RPC callback to plugin ABI and host dispatcher
- Introduce `HostRoutingResetCooldownRequest` and `HostRoutingResetCooldownResponse` in plugin API
- Reset credential quota and cooldown routing state in memory without persisting to token files

Closes: #6274
2026-10-02 23:32:32 +08:00

49 lines
1.6 KiB
Go

package pluginhost
import (
"context"
"encoding/json"
"fmt"
"strings"
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
log "github.com/sirupsen/logrus"
)
// callHostRoutingResetCooldown clears quota and cooldown routing state for one credential, the
// same reset POST /v8/management/routing/cooldown/reset performs, while ensuring token files are left alone.
func (h *Host) callHostRoutingResetCooldown(ctx context.Context, request []byte) ([]byte, error) {
var req pluginapi.HostRoutingResetCooldownRequest
if errUnmarshal := json.Unmarshal(request, &req); errUnmarshal != nil {
return nil, fmt.Errorf("decode host routing reset cooldown request: %w", errUnmarshal)
}
authIndex := strings.TrimSpace(req.AuthIndex)
auth, errGet := h.authByIndex(authIndex)
if errGet != nil {
return nil, errGet
}
manager := h.currentAuthManager()
if manager == nil {
return nil, fmt.Errorf("core auth manager unavailable")
}
resetCtx := coreauth.WithSkipPersist(ctx)
updated, models, errReset := manager.ResetQuota(resetCtx, auth.ID)
if errReset != nil {
return nil, fmt.Errorf("reset cooldown: %w", errReset)
}
if updated == nil {
return nil, fmt.Errorf("auth not found for auth_index %s", authIndex)
}
updated.EnsureIndex()
log.WithFields(log.Fields{
"plugin_id": hostCallbackPluginIDFromContext(ctx),
"auth_index": updated.Index,
"models": len(models),
}).Info("pluginhost: plugin reset credential cooldown")
return marshalRPCResult(pluginapi.HostRoutingResetCooldownResponse{
AuthIndex: updated.Index,
Models: models,
})
}