mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-20 00:47:15 +08:00
- Add optional `scheduler.continuity` plugin ABI/RPC hook with bounded `pre_admission` and `pre_output_429` wait gates, persisted cooldown provenance separation, and SSE/WebSocket keepalive handoff. - Enforce per-request continuity budgets, `disable_cooling` retry limits, and stable turn `RequestID` across SSE bootstrap retries and WebSocket in-place failover. - Add race-verified unit and handler integration tests covering continuity gates, cooldown provenance, keepalive suppression, and session isolation. Closes: #7589
208 lines
6.4 KiB
Go
208 lines
6.4 KiB
Go
package pluginhost
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func (h *Host) PickAuth(ctx context.Context, req pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, error) {
|
|
record := h.schedulerRecord()
|
|
if record == nil {
|
|
return pluginapi.SchedulerPickResponse{}, false, nil
|
|
}
|
|
|
|
resp, handled, errPick := h.callScheduler(ctx, *record, req)
|
|
if errPick != nil || !handled {
|
|
return resp, handled, errPick
|
|
}
|
|
if !resp.Handled {
|
|
if excluded := normalizeExcludedCandidates(resp.ExcludeCandidates); len(excluded) > 0 {
|
|
return pluginapi.SchedulerPickResponse{ExcludeCandidates: excluded}, false, nil
|
|
}
|
|
return pluginapi.SchedulerPickResponse{}, false, nil
|
|
}
|
|
|
|
resp, valid, reason := normalizeSchedulerResponse(resp, req)
|
|
if !valid {
|
|
log.WithField("plugin_id", record.id).Warnf("pluginhost: scheduler returned invalid response: %s", reason)
|
|
return pluginapi.SchedulerPickResponse{}, false, nil
|
|
}
|
|
return resp, true, nil
|
|
}
|
|
|
|
func (h *Host) HasScheduler() bool {
|
|
return h.schedulerRecord() != nil
|
|
}
|
|
|
|
func (h *Host) SchedulerWantsAcrossPriorities() bool {
|
|
record := h.schedulerRecord()
|
|
if record == nil {
|
|
return false
|
|
}
|
|
return schedulerWantsAcrossPriorities(record.plugin.Capabilities)
|
|
}
|
|
|
|
// HasSchedulerContinuity reports whether the active scheduler plugin provides SchedulerContinuity.
|
|
func (h *Host) HasSchedulerContinuity() bool {
|
|
return h.schedulerContinuityRecord() != nil
|
|
}
|
|
|
|
// EvaluateSchedulerContinuity invokes the active scheduler plugin's Continuity callback.
|
|
func (h *Host) EvaluateSchedulerContinuity(ctx context.Context, req pluginapi.SchedulerContinuityRequest) (pluginapi.SchedulerContinuityResponse, bool, error) {
|
|
record := h.schedulerContinuityRecord()
|
|
if record == nil {
|
|
return pluginapi.SchedulerContinuityResponse{}, false, nil
|
|
}
|
|
return h.callSchedulerContinuity(ctx, *record, req)
|
|
}
|
|
|
|
func (h *Host) schedulerContinuityRecord() *capabilityRecord {
|
|
record := h.schedulerRecord()
|
|
if record == nil || record.plugin.Capabilities.SchedulerContinuity == nil {
|
|
return nil
|
|
}
|
|
return record
|
|
}
|
|
|
|
func (h *Host) callSchedulerContinuity(ctx context.Context, record capabilityRecord, req pluginapi.SchedulerContinuityRequest) (resp pluginapi.SchedulerContinuityResponse, handled bool, err error) {
|
|
continuity := record.plugin.Capabilities.SchedulerContinuity
|
|
if h == nil || continuity == nil || h.isPluginFused(record.id) || !h.recordCurrent(record) {
|
|
return pluginapi.SchedulerContinuityResponse{}, false, nil
|
|
}
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
h.fusePlugin(record.id, "SchedulerContinuity.Continuity", recovered)
|
|
resp = pluginapi.SchedulerContinuityResponse{}
|
|
handled = false
|
|
err = nil
|
|
}
|
|
}()
|
|
|
|
req.Plugin = record.meta
|
|
resp, errEval := continuity.Continuity(ctx, req)
|
|
if errEval != nil {
|
|
log.WithField("plugin_id", record.id).WithError(errEval).Warn("pluginhost: scheduler continuity evaluation failed")
|
|
return pluginapi.SchedulerContinuityResponse{}, true, errEval
|
|
}
|
|
resp.Decision = pluginapi.SchedulerContinuityDecision(strings.ToLower(strings.TrimSpace(string(resp.Decision))))
|
|
resp.Reason = strings.TrimSpace(resp.Reason)
|
|
return resp, true, nil
|
|
}
|
|
|
|
func (h *Host) schedulerRecord() *capabilityRecord {
|
|
if h == nil {
|
|
return nil
|
|
}
|
|
for _, record := range h.activeRecords() {
|
|
if h.isPluginFused(record.id) || record.plugin.Capabilities.Scheduler == nil {
|
|
continue
|
|
}
|
|
copyRecord := record
|
|
return ©Record
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *Host) callScheduler(ctx context.Context, record capabilityRecord, req pluginapi.SchedulerPickRequest) (resp pluginapi.SchedulerPickResponse, handled bool, err error) {
|
|
scheduler := record.plugin.Capabilities.Scheduler
|
|
if h == nil || scheduler == nil || h.isPluginFused(record.id) || !h.recordCurrent(record) {
|
|
return pluginapi.SchedulerPickResponse{}, false, nil
|
|
}
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
h.fusePlugin(record.id, "Scheduler.Pick", recovered)
|
|
resp = pluginapi.SchedulerPickResponse{}
|
|
handled = false
|
|
err = nil
|
|
}
|
|
}()
|
|
|
|
req.Plugin = record.meta
|
|
resp, errPick := scheduler.Pick(ctx, req)
|
|
if errPick != nil {
|
|
log.WithField("plugin_id", record.id).WithError(errPick).Warn("pluginhost: scheduler rejected auth pick")
|
|
return pluginapi.SchedulerPickResponse{}, true, errPick
|
|
}
|
|
return resp, true, nil
|
|
}
|
|
|
|
func normalizeSchedulerResponse(resp pluginapi.SchedulerPickResponse, req pluginapi.SchedulerPickRequest) (pluginapi.SchedulerPickResponse, bool, string) {
|
|
resp.AuthID = strings.TrimSpace(resp.AuthID)
|
|
resp.DelegateBuiltin = strings.TrimSpace(resp.DelegateBuiltin)
|
|
resp.RejectCode = strings.TrimSpace(resp.RejectCode)
|
|
resp.RejectReason = strings.TrimSpace(resp.RejectReason)
|
|
resp.ExcludeCandidates = normalizeExcludedCandidates(resp.ExcludeCandidates)
|
|
|
|
if resp.Reject {
|
|
if resp.RejectCode == "" {
|
|
resp.RejectCode = "auth_unavailable"
|
|
}
|
|
if resp.RejectReason == "" {
|
|
resp.RejectReason = "scheduler rejected candidate selection"
|
|
}
|
|
resp.ExcludeCandidates = nil
|
|
return resp, true, ""
|
|
}
|
|
|
|
hasAuthID := resp.AuthID != ""
|
|
hasDelegate := resp.DelegateBuiltin != ""
|
|
if !hasAuthID && !hasDelegate {
|
|
return pluginapi.SchedulerPickResponse{}, false, "missing auth id or delegate"
|
|
}
|
|
if hasAuthID {
|
|
if !schedulerCandidateExists(req.Candidates, resp.AuthID) {
|
|
return pluginapi.SchedulerPickResponse{}, false, "unknown auth id"
|
|
}
|
|
resp.ExcludeCandidates = nil
|
|
return resp, true, ""
|
|
}
|
|
if !validSchedulerBuiltin(resp.DelegateBuiltin) {
|
|
return pluginapi.SchedulerPickResponse{}, false, "unknown delegate"
|
|
}
|
|
return resp, true, ""
|
|
}
|
|
|
|
func normalizeExcludedCandidates(candidates []string) []string {
|
|
if len(candidates) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]string, 0, len(candidates))
|
|
seen := make(map[string]struct{}, len(candidates))
|
|
for _, candidate := range candidates {
|
|
trimmed := strings.TrimSpace(candidate)
|
|
if trimmed == "" {
|
|
continue
|
|
}
|
|
if _, exists := seen[trimmed]; exists {
|
|
continue
|
|
}
|
|
seen[trimmed] = struct{}{}
|
|
out = append(out, trimmed)
|
|
}
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
return out
|
|
}
|
|
|
|
func schedulerCandidateExists(candidates []pluginapi.SchedulerAuthCandidate, authID string) bool {
|
|
for _, candidate := range candidates {
|
|
if strings.TrimSpace(candidate.ID) == authID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func validSchedulerBuiltin(delegate string) bool {
|
|
switch delegate {
|
|
case pluginapi.SchedulerBuiltinRoundRobin, pluginapi.SchedulerBuiltinFillFirst:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|