( ctx context.Context, req types.AIRequest, primary types.RouteDecision, fallback []types.RouteDecision, opts types.ReliabilityExecuteOptions, )
| 38 | } |
| 39 | |
| 40 | func (m *Manager) ExecuteWithFallbackOpts( |
| 41 | ctx context.Context, |
| 42 | req types.AIRequest, |
| 43 | primary types.RouteDecision, |
| 44 | fallback []types.RouteDecision, |
| 45 | opts types.ReliabilityExecuteOptions, |
| 46 | ) (types.ExecutionResult, error) { |
| 47 | if name := strings.TrimSpace(opts.ForcePrimaryBackend); name != "" { |
| 48 | primary = types.RouteDecision{ |
| 49 | BackendName: name, |
| 50 | Reason: "forced_primary", |
| 51 | EstimatedCost: primary.EstimatedCost, |
| 52 | FallbackChain: nil, |
| 53 | } |
| 54 | if opts.NoFallback { |
| 55 | fallback = nil |
| 56 | } |
| 57 | } |
| 58 | candidates := make([]types.RouteDecision, 0, 1+len(fallback)) |
| 59 | candidates = append(candidates, primary) |
| 60 | candidates = append(candidates, fallback...) |
| 61 | |
| 62 | var lastErr error |
| 63 | |
| 64 | for i, decision := range candidates { |
| 65 | adapter, ok := m.adapters[decision.BackendName] |
| 66 | if !ok { |
| 67 | lastErr = fmt.Errorf("adapter %q not found", decision.BackendName) |
| 68 | continue |
| 69 | } |
| 70 | |
| 71 | backendCfg, ok := m.cfg.BackendByName(decision.BackendName) |
| 72 | if !ok { |
| 73 | lastErr = fmt.Errorf("backend config %q not found", decision.BackendName) |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | invokeReq := req |
| 78 | if i > 0 && !m.cfg.Reliability.StreamFallbackEnabled && req.Options.Stream { |
| 79 | invokeReq = cloneAINonStream(req) |
| 80 | } |
| 81 | |
| 82 | invokeCtx, cancel := context.WithTimeout(ctx, time.Duration(backendCfg.TimeoutMS)*time.Millisecond) |
| 83 | resp, err := adapter.Invoke(invokeCtx, types.BackendRequest{AIRequest: invokeReq}) |
| 84 | cancel() |
| 85 | if err == nil { |
| 86 | return types.ExecutionResult{ |
| 87 | Status: "success", |
| 88 | BackendName: decision.BackendName, |
| 89 | Output: resp.Output, |
| 90 | UsedFallback: i > 0, |
| 91 | Error: nil, |
| 92 | Timing: resp.Timing, |
| 93 | }, nil |
| 94 | } |
| 95 | |
| 96 | lastErr = err |
| 97 | if !m.shouldFallback(decision.BackendName, err) { |
no test coverage detected