(w http.ResponseWriter, p aiSuccessParams)
| 535 | } |
| 536 | |
| 537 | func (s *Server) writeAIResponse(w http.ResponseWriter, p aiSuccessParams) { |
| 538 | chosenBackendCfg, _ := s.cfg.BackendByName(p.execution.BackendName) |
| 539 | endWall := time.Now() |
| 540 | latency := endWall.Sub(p.executionStart).Milliseconds() |
| 541 | |
| 542 | firstTok := endWall |
| 543 | compAt := endWall |
| 544 | if p.execution.Timing != nil { |
| 545 | if p.execution.Timing.TTFTMs > 0 { |
| 546 | firstTok = p.executionStart.Add(time.Duration(p.execution.Timing.TTFTMs) * time.Millisecond) |
| 547 | } |
| 548 | if p.execution.Timing.CompletionLatencyMs > 0 { |
| 549 | compAt = p.executionStart.Add(time.Duration(p.execution.Timing.CompletionLatencyMs) * time.Millisecond) |
| 550 | } |
| 551 | } |
| 552 | s.sloEngine.RecordFirstToken(p.requestID, firstTok) |
| 553 | s.sloEngine.RecordCompletion(p.requestID, compAt) |
| 554 | |
| 555 | resp := types.AIResponse{ |
| 556 | RequestID: p.requestID, |
| 557 | TraceID: p.traceID, |
| 558 | RequestType: p.req.RequestType, |
| 559 | PipelineRef: p.req.PipelineRef, |
| 560 | SelectedBackend: p.execution.BackendName, |
| 561 | RouteReason: p.primary.Reason, |
| 562 | PolicyReason: p.policyDecision.Reason, |
| 563 | EffectivePriority: p.req.Priority, |
| 564 | Status: p.execution.Status, |
| 565 | Result: p.execution.Output, |
| 566 | Metrics: types.AIMetrics{ |
| 567 | TTFTMs: 0, |
| 568 | TPOTMs: 0, |
| 569 | CompletionLatencyMs: latency, |
| 570 | EstimatedCost: chosenBackendCfg.Cost.Unit, |
| 571 | QueueWaitTimeMs: 0, |
| 572 | ServerReceivedAtUnix: p.nowUnixMs, |
| 573 | }, |
| 574 | Fallback: types.FallbackState{ |
| 575 | Triggered: p.execution.UsedFallback, |
| 576 | Reason: fallbackReason(p.execution.UsedFallback), |
| 577 | }, |
| 578 | Degrade: types.DegradeState{ |
| 579 | Triggered: false, |
| 580 | }, |
| 581 | } |
| 582 | if p.execution.UsedFallback { |
| 583 | s.sloEngine.RecordFallback(p.requestID, "execution_fallback") |
| 584 | log.Printf("event=fallback_triggered request_id=%s tenant_id=%s primary=%s selected=%s", p.requestID, p.req.TenantID, p.primary.BackendName, p.execution.BackendName) |
| 585 | } |
| 586 | |
| 587 | snapshot := s.sloEngine.Snapshot(p.requestID) |
| 588 | resp.Metrics.TTFTMs = snapshot.TTFTMs |
| 589 | resp.Metrics.TPOTMs = snapshot.TPOTMs |
| 590 | resp.Metrics.CompletionLatencyMs = snapshot.CompletionLatencyMs |
| 591 | if p.execution.Timing != nil && p.execution.Timing.TPOTMs > 0 { |
| 592 | resp.Metrics.TPOTMs = p.execution.Timing.TPOTMs |
| 593 | } |
| 594 | resp.Fallback.Triggered = snapshot.FallbackTriggered |
no test coverage detected