File size: 33,919 Bytes
f3bcb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
"""Evaluate LLM Request Use Case.

Architecture Hexagonale: Use Case qui orchestre les appels domain et ports
pour évaluer et exécuter une requête LLM.

Fonctionnalités:
- Évaluation des policies
- Vérification des budgets
- Appel LLM (sync et streaming)
- Audit logging
- Métriques (optionnel)
"""

from dataclasses import dataclass, field
from enum import Enum
from typing import Any, AsyncIterator
import time

from backend.domain.policy import PolicyEvaluator, PolicyDecision
from backend.domain.budget import BudgetChecker, BudgetStatus
from backend.domain.models import (
    PolicyAction,
    ChatMessage,
    ChatRequest as DomainChatRequest,
    ChatResponse as DomainChatResponse,
)
from backend.ports.llm_provider import LLMProviderPort
from backend.ports.policy_repository import PolicyRepositoryPort
from backend.ports.budget_repository import BudgetRepositoryPort
from backend.ports.audit_log import AuditLogPort, AuditEntry
from backend.ports.metrics import MetricsPort, RequestMetrics, DecisionMetrics
from backend.ports.abuse_detector import AbuseDetectorPort
from backend.ports.feature_registry import FeatureRegistryPort, FeatureAction
from backend.ports.request_tracing import RequestTracingPort, TraceStep
from backend.ports.encryption import EncryptionPort
from backend.ports.observability import ObservabilityPort


class RequestOutcome(str, Enum):
    """Résultat possible d'une requête."""

    ALLOWED = "allowed"
    DENIED_POLICY = "denied_policy"
    DENIED_BUDGET = "denied_budget"
    DENIED_ABUSE = "denied_abuse"
    DENIED_FEATURE = "denied_feature"
    ERROR = "error"
    DRY_RUN = "dry_run"


@dataclass
class LLMRequestCommand:
    """Command pour évaluer une requête LLM."""

    request_id: str
    app_id: str
    org_id: str | None
    model: str
    messages: list[dict[str, str]]
    environment: str = "development"
    feature: str | None = None
    max_tokens: int | None = None
    temperature: float | None = None
    stream: bool = False
    dry_run: bool = False
    api_key: str | None = None


@dataclass
class LLMRequestResult:
    """Résultat de l'évaluation d'une requête LLM."""

    request_id: str
    outcome: RequestOutcome
    response: DomainChatResponse | None = None
    policy_decision: PolicyDecision | None = None
    budget_status: BudgetStatus | None = None
    error_message: str | None = None
    dry_run_result: dict[str, Any] | None = None
    metadata: dict[str, Any] = field(default_factory=dict)


class EvaluateLLMRequestUseCase:
    """Use Case: Évaluer et exécuter une requête LLM.

    Ce use case orchestre:
    1. L'évaluation des policies (via PolicyEvaluator)
    2. La vérification des budgets (via BudgetChecker)
    3. L'appel au LLM (via LLMProviderPort)
    4. L'enregistrement de l'audit (via AuditLogPort)
    5. L'enregistrement des métriques (via MetricsPort)

    Architecture:
    - Dépend uniquement des Ports (interfaces), pas des implémentations
    - Utilise les classes Domain pour la logique métier pure
    - Orchestre les différentes étapes sans contenir de logique métier

    Modes supportés:
    - Requête synchrone (execute)
    - Requête streaming (execute_stream)
    - Dry-run (simulation sans appel LLM)
    """

    def __init__(
        self,
        policy_evaluator: PolicyEvaluator,
        budget_checker: BudgetChecker,
        policy_repository: PolicyRepositoryPort,
        budget_repository: BudgetRepositoryPort,
        llm_provider: LLMProviderPort,
        audit_log: AuditLogPort | None = None,
        metrics: MetricsPort | None = None,
        abuse_detector: AbuseDetectorPort | None = None,
        feature_registry: FeatureRegistryPort | None = None,
        request_tracer: RequestTracingPort | None = None,
        encryption: EncryptionPort | None = None,
        observability: ObservabilityPort | None = None,
    ):
        self.policy_evaluator = policy_evaluator
        self.budget_checker = budget_checker
        self.policy_repository = policy_repository
        self.budget_repository = budget_repository
        self.llm_provider = llm_provider
        self.audit_log = audit_log
        self.metrics = metrics
        self.abuse_detector = abuse_detector
        self.feature_registry = feature_registry
        self.request_tracer = request_tracer
        self.encryption = encryption
        self.observability = observability

    async def execute(self, command: LLMRequestCommand) -> LLMRequestResult:
        """Exécute le use case.

        Flow:
        1. Récupérer les règles de policy depuis le repository
        2. Évaluer les policies avec le contexte de la requête
        3. Si policy.action == DENY -> retourner DENIED_POLICY
        4. Récupérer les budgets depuis le repository
        5. Estimer le coût de la requête
        6. Vérifier le budget
        7. Si budget.allowed == False -> retourner DENIED_BUDGET
        8. Si dry_run -> retourner le résultat sans appeler le LLM
        9. Appeler le LLM via le provider
        10. Enregistrer l'audit
        11. Retourner le résultat
        """
        trace = None
        try:
            # 0. Create trace (si configuré)
            if self.request_tracer:
                trace = await self.request_tracer.create_trace(
                    request_id=command.request_id,
                    app_id=command.app_id,
                    org_id=command.org_id,
                    model=command.model,
                    context={
                        "environment": command.environment,
                        "feature": command.feature,
                        "dry_run": command.dry_run,
                    },
                )

            # 0a. Abuse detection (si configuré)
            if self.abuse_detector:
                await self._start_span(trace, TraceStep.ABUSE_CHECK.value)
                abuse_result = await self.abuse_detector.check_request(
                    app_id=command.app_id,
                    feature=command.feature or "default",
                    model=command.model,
                    messages=command.messages,
                    request_id=command.request_id,
                )
                await self._end_span(
                    trace,
                    TraceStep.ABUSE_CHECK.value,
                    "blocked" if abuse_result.blocked else "ok",
                )
                if abuse_result.blocked:
                    self._record_decision_metrics(command, "deny", "abuse")
                    error_msg = (
                        abuse_result.reason
                        or f"Blocked for abuse: {abuse_result.abuse_type.value if abuse_result.abuse_type else 'unknown'}"
                    )
                    await self._handle_blocked_request(
                        command, trace, RequestOutcome.DENIED_ABUSE, "abuse", error_msg
                    )
                    return LLMRequestResult(
                        request_id=command.request_id,
                        outcome=RequestOutcome.DENIED_ABUSE,
                        error_message=error_msg,
                        metadata={
                            "abuse_type": (
                                abuse_result.abuse_type.value
                                if abuse_result.abuse_type
                                else None
                            ),
                            "cooldown_seconds": abuse_result.cooldown_seconds,
                        },
                    )

            # 0b. Feature allowlist (si configuré)
            if self.feature_registry:
                await self._start_span(trace, TraceStep.FEATURE_CHECK.value)
                feature_result = await self.feature_registry.check_feature(
                    app_id=command.app_id,
                    feature_id=command.feature,
                    action=FeatureAction.CHAT,  # Default action for chat requests
                    model=command.model,
                    environment=command.environment,
                )
                await self._end_span(
                    trace,
                    TraceStep.FEATURE_CHECK.value,
                    "denied" if not feature_result.allowed else "ok",
                )
                if not feature_result.allowed:
                    self._record_decision_metrics(command, "deny", "feature")
                    await self._handle_blocked_request(
                        command,
                        trace,
                        RequestOutcome.DENIED_FEATURE,
                        "feature",
                        feature_result.reason,
                    )
                    return LLMRequestResult(
                        request_id=command.request_id,
                        outcome=RequestOutcome.DENIED_FEATURE,
                        error_message=feature_result.reason,
                        metadata={
                            "feature_decision": feature_result.decision.value,
                            "feature_id": feature_result.feature_id,
                        },
                    )

            # 1. Récupérer les règles de policy
            await self._start_span(trace, TraceStep.POLICY_EVALUATION.value)
            rules = await self.policy_repository.get_active_rules(
                org_id=command.org_id,
                app_id=command.app_id,
                environment=command.environment,
            )

            # 2. Évaluer les policies
            policy_context = {
                "model": command.model,
                "environment": command.environment,
                "feature": command.feature,
                "app_id": command.app_id,
            }
            policy_decision = self.policy_evaluator.evaluate(rules, policy_context)
            await self._end_span(
                trace,
                TraceStep.POLICY_EVALUATION.value,
                "denied" if policy_decision.action == PolicyAction.DENY else "ok",
            )

            # 3. Vérifier si la policy refuse la requête
            if policy_decision.action == PolicyAction.DENY:
                await self._log_decision(
                    command, RequestOutcome.DENIED_POLICY, policy_decision
                )
                self._record_decision_metrics(command, "deny", "policy")
                error_msg = "; ".join(policy_decision.reasons) or "Denied by policy"
                await self._handle_blocked_request(
                    command, trace, RequestOutcome.DENIED_POLICY, "policy", error_msg
                )
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.DENIED_POLICY,
                    policy_decision=policy_decision,
                    error_message=error_msg,
                )

            # 4. Récupérer les budgets
            await self._start_span(trace, TraceStep.BUDGET_CHECK.value)
            budgets = await self.budget_repository.get_budgets_for_app(
                app_id=command.app_id,
                org_id=command.org_id,
            )

            # 5. Estimer le coût (approximatif avant l'appel)
            estimated_input_tokens = self._estimate_input_tokens(command.messages)
            estimated_output_tokens = command.max_tokens or 1000
            estimated_cost = self.budget_checker.estimate_cost(
                model=command.model,
                input_tokens=estimated_input_tokens,
                output_tokens=estimated_output_tokens,
            )

            # 6. Vérifier le budget
            budget_status = self.budget_checker.check(budgets, estimated_cost)
            await self._end_span(
                trace,
                TraceStep.BUDGET_CHECK.value,
                "denied" if not budget_status.allowed else "ok",
            )

            # 7. Vérifier si le budget refuse la requête
            if not budget_status.allowed:
                await self._log_decision(
                    command,
                    RequestOutcome.DENIED_BUDGET,
                    policy_decision,
                    budget_status,
                )
                self._record_decision_metrics(command, "deny", "budget")
                error_msg = "; ".join(budget_status.reasons) or "Budget exceeded"
                await self._handle_blocked_request(
                    command, trace, RequestOutcome.DENIED_BUDGET, "budget", error_msg
                )
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.DENIED_BUDGET,
                    policy_decision=policy_decision,
                    budget_status=budget_status,
                    error_message=error_msg,
                )

            # 8. Mode dry run - retourner sans appeler le LLM
            if command.dry_run:
                # Complete trace for dry run
                if trace and self.request_tracer:
                    await self.request_tracer.complete_trace(
                        trace_id=trace.trace_id,
                        outcome="dry_run",
                        final_data={"estimated_cost_usd": estimated_cost},
                    )
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.DRY_RUN,
                    policy_decision=policy_decision,
                    budget_status=budget_status,
                    dry_run_result={
                        "would_be_allowed": True,
                        "estimated_cost_usd": estimated_cost,
                        "policy_action": policy_decision.action.value,
                        "budget_remaining_usd": budget_status.remaining_usd,
                        "budget_usage_percent": budget_status.usage_percent,
                    },
                )

            # 9. Appeler le LLM
            # API key not required for mock or local providers (ollama, lmstudio)
            local_providers = ("mock", "ollama", "lmstudio")
            is_local_provider = self.llm_provider.name in local_providers
            if not command.api_key and not is_local_provider:
                await self._handle_blocked_request(
                    command,
                    trace,
                    RequestOutcome.ERROR,
                    "validation",
                    "API key required",
                )
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.ERROR,
                    error_message="API key required for LLM call",
                )

            # 9a. Déchiffrer l'API key si nécessaire
            api_key = command.api_key
            if self.encryption and api_key.startswith("enc:"):
                await self._start_span(trace, "decrypt_api_key")
                try:
                    api_key = await self.encryption.decrypt_api_key(
                        api_key[4:]
                    )  # Remove "enc:" prefix
                    await self._end_span(trace, "decrypt_api_key", "ok")
                except Exception as e:
                    await self._end_span(
                        trace, "decrypt_api_key", "error", error=str(e)
                    )
                    await self._handle_blocked_request(
                        command,
                        trace,
                        RequestOutcome.ERROR,
                        "encryption",
                        f"Failed to decrypt API key: {e}",
                    )
                    return LLMRequestResult(
                        request_id=command.request_id,
                        outcome=RequestOutcome.ERROR,
                        error_message=f"Failed to decrypt API key: {e}",
                    )

            domain_request = DomainChatRequest(
                model=command.model,
                messages=[
                    ChatMessage(role=m["role"], content=m["content"])
                    for m in command.messages
                ],
                max_tokens=command.max_tokens,
                temperature=command.temperature,
                stream=command.stream,
            )

            start_time = time.time()
            if self.metrics:
                self.metrics.request_started(command.app_id)

            await self._start_span(trace, TraceStep.LLM_REQUEST.value)
            response = await self.llm_provider.chat(domain_request, api_key)
            await self._end_span(
                trace,
                TraceStep.LLM_REQUEST.value,
                "ok",
                {
                    "input_tokens": response.input_tokens,
                    "output_tokens": response.output_tokens,
                },
            )

            latency_seconds = time.time() - start_time

            # 10. Enregistrer l'audit
            await self._log_decision(
                command,
                RequestOutcome.ALLOWED,
                policy_decision,
                budget_status,
                response,
            )

            # 11. Enregistrer les métriques
            self._record_request_metrics(
                command=command,
                status="success",
                latency_seconds=latency_seconds,
                input_tokens=response.input_tokens,
                output_tokens=response.output_tokens,
            )
            self._record_decision_metrics(command, "allow", "policy")
            if self.metrics:
                self.metrics.request_finished(command.app_id)

            # 12. Complete trace
            # Use "warned" outcome if policy decision is WARN
            trace_outcome = (
                "warned" if policy_decision.action == PolicyAction.WARN else "allowed"
            )
            await self._handle_successful_request(
                command, trace, response, trace_outcome
            )

            # 12a. Enregistrer les métriques d'observabilité
            actual_cost = self.budget_checker.estimate_cost(
                model=command.model,
                input_tokens=response.input_tokens,
                output_tokens=response.output_tokens,
            )

            if self.observability:
                await self.observability.record_request(
                    app_id=command.app_id,
                    org_id=command.org_id or "default",
                    model=command.model,
                    environment=command.environment,
                    input_tokens=response.input_tokens,
                    output_tokens=response.output_tokens,
                    cost_usd=actual_cost,
                    latency_ms=latency_seconds * 1000,
                    outcome="allowed",
                    feature=command.feature,
                )

            # 12b. Mettre à jour le budget avec le coût réel
            if budgets and actual_cost > 0:
                for budget in budgets:
                    await self.budget_repository.record_usage(
                        budget_id=budget.id,
                        amount_usd=actual_cost,
                    )

            # 13. Retourner le résultat
            return LLMRequestResult(
                request_id=command.request_id,
                outcome=RequestOutcome.ALLOWED,
                response=response,
                policy_decision=policy_decision,
                budget_status=budget_status,
                metadata={
                    "input_tokens": response.input_tokens,
                    "output_tokens": response.output_tokens,
                    "model": response.model,
                    "latency_seconds": latency_seconds,
                },
            )

        except Exception as e:
            self._record_request_metrics(command, status="error", latency_seconds=0)
            if self.metrics:
                self.metrics.record_error(command.app_id, type(e).__name__)
            # Fail trace for error
            if trace and self.request_tracer:
                await self.request_tracer.fail_trace(
                    trace_id=trace.trace_id,
                    error=str(e),
                    step="execute",
                )
            return LLMRequestResult(
                request_id=command.request_id,
                outcome=RequestOutcome.ERROR,
                error_message=str(e),
            )

    def _estimate_input_tokens(self, messages: list[dict[str, str]]) -> int:
        """Estime le nombre de tokens en entrée (approximation simple)."""
        total_chars = sum(len(m.get("content", "")) for m in messages)
        # Approximation: 4 caractères par token
        return max(10, total_chars // 4)

    async def _log_decision(
        self,
        command: LLMRequestCommand,
        outcome: RequestOutcome,
        policy_decision: PolicyDecision | None = None,
        budget_status: BudgetStatus | None = None,
        response: DomainChatResponse | None = None,
    ) -> None:
        """Enregistre la décision dans l'audit log."""
        if not self.audit_log:
            return

        entry = AuditEntry(
            event_type="llm_request",
            request_id=command.request_id,
            app_id=command.app_id,
            org_id=command.org_id,
            model=command.model,
            action=policy_decision.action.value if policy_decision else None,
            outcome=outcome.value,
            details={
                "environment": command.environment,
                "feature": command.feature,
                "policy_reasons": policy_decision.reasons if policy_decision else [],
                "budget_usage_percent": (
                    budget_status.usage_percent if budget_status else None
                ),
            },
            input_tokens=response.input_tokens if response else None,
            output_tokens=response.output_tokens if response else None,
        )

        await self.audit_log.log(entry)

    def _record_request_metrics(
        self,
        command: LLMRequestCommand,
        status: str,
        latency_seconds: float,
        input_tokens: int = 0,
        output_tokens: int = 0,
        cost_usd: float = 0.0,
    ) -> None:
        """Enregistre les métriques d'une requête."""
        if not self.metrics:
            return

        metrics = RequestMetrics(
            app_id=command.app_id,
            model=command.model,
            status=status,
            latency_seconds=latency_seconds,
            feature=command.feature or "default",
            environment=command.environment,
            input_tokens=input_tokens,
            output_tokens=output_tokens,
            cost_usd=cost_usd,
        )
        self.metrics.record_request(metrics)

    def _record_decision_metrics(
        self,
        command: LLMRequestCommand,
        decision: str,
        source: str,
    ) -> None:
        """Enregistre les métriques d'une décision."""
        if not self.metrics:
            return

        metrics = DecisionMetrics(
            app_id=command.app_id,
            decision=decision,
            source=source,
        )
        self.metrics.record_decision(metrics)

    async def execute_stream(
        self,
        command: LLMRequestCommand,
    ) -> AsyncIterator[str] | LLMRequestResult:
        """Exécute le use case en mode streaming.

        Effectue les mêmes validations que execute() (policies, budget),
        mais retourne un AsyncIterator pour le streaming au lieu d'une réponse complète.

        Args:
            command: Commande de requête LLM

        Returns:
            AsyncIterator[str] si les validations passent et streaming commence
            LLMRequestResult si la requête est refusée ou erreur

        Usage:
            result = await use_case.execute_stream(command)
            if isinstance(result, LLMRequestResult):
                # Handle denial or error
            else:
                async for chunk in result:
                    # Handle streaming chunk
        """
        try:
            # 0a. Abuse detection (si configuré)
            if self.abuse_detector:
                abuse_result = await self.abuse_detector.check_request(
                    app_id=command.app_id,
                    feature=command.feature or "default",
                    model=command.model,
                    messages=command.messages,
                    request_id=command.request_id,
                )
                if abuse_result.blocked:
                    self._record_decision_metrics(command, "deny", "abuse")
                    return LLMRequestResult(
                        request_id=command.request_id,
                        outcome=RequestOutcome.DENIED_ABUSE,
                        error_message=abuse_result.reason
                        or f"Blocked for abuse: {abuse_result.abuse_type.value if abuse_result.abuse_type else 'unknown'}",
                        metadata={
                            "abuse_type": (
                                abuse_result.abuse_type.value
                                if abuse_result.abuse_type
                                else None
                            ),
                            "cooldown_seconds": abuse_result.cooldown_seconds,
                        },
                    )

            # 0b. Feature allowlist (si configuré)
            if self.feature_registry:
                feature_result = await self.feature_registry.check_feature(
                    app_id=command.app_id,
                    feature_id=command.feature,
                    action=FeatureAction.CHAT,
                    model=command.model,
                    environment=command.environment,
                )
                if not feature_result.allowed:
                    self._record_decision_metrics(command, "deny", "feature")
                    return LLMRequestResult(
                        request_id=command.request_id,
                        outcome=RequestOutcome.DENIED_FEATURE,
                        error_message=feature_result.reason,
                        metadata={
                            "feature_decision": feature_result.decision.value,
                            "feature_id": feature_result.feature_id,
                        },
                    )

            # 1-7: Mêmes validations que execute()
            rules = await self.policy_repository.get_active_rules(
                org_id=command.org_id,
                app_id=command.app_id,
                environment=command.environment,
            )

            policy_context = {
                "model": command.model,
                "environment": command.environment,
                "feature": command.feature,
                "app_id": command.app_id,
            }
            policy_decision = self.policy_evaluator.evaluate(rules, policy_context)

            if policy_decision.action == PolicyAction.DENY:
                await self._log_decision(
                    command, RequestOutcome.DENIED_POLICY, policy_decision
                )
                self._record_decision_metrics(command, "deny", "policy")
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.DENIED_POLICY,
                    policy_decision=policy_decision,
                    error_message="; ".join(policy_decision.reasons)
                    or "Denied by policy",
                )

            budgets = await self.budget_repository.get_budgets_for_app(
                app_id=command.app_id,
                org_id=command.org_id,
            )

            estimated_input_tokens = self._estimate_input_tokens(command.messages)
            estimated_output_tokens = command.max_tokens or 1000
            estimated_cost = self.budget_checker.estimate_cost(
                model=command.model,
                input_tokens=estimated_input_tokens,
                output_tokens=estimated_output_tokens,
            )

            budget_status = self.budget_checker.check(budgets, estimated_cost)

            if not budget_status.allowed:
                await self._log_decision(
                    command,
                    RequestOutcome.DENIED_BUDGET,
                    policy_decision,
                    budget_status,
                )
                self._record_decision_metrics(command, "deny", "budget")
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.DENIED_BUDGET,
                    policy_decision=policy_decision,
                    budget_status=budget_status,
                    error_message="; ".join(budget_status.reasons) or "Budget exceeded",
                )

            # API key not required for mock or local providers (ollama, lmstudio)
            local_providers = ("mock", "ollama", "lmstudio")
            is_local_provider = self.llm_provider.name in local_providers
            if not command.api_key and not is_local_provider:
                return LLMRequestResult(
                    request_id=command.request_id,
                    outcome=RequestOutcome.ERROR,
                    error_message="API key required for LLM call",
                )

            # Créer la requête domain
            domain_request = DomainChatRequest(
                model=command.model,
                messages=[
                    ChatMessage(role=m["role"], content=m["content"])
                    for m in command.messages
                ],
                max_tokens=command.max_tokens,
                temperature=command.temperature,
                stream=True,
            )

            # Enregistrer métriques de début
            if self.metrics:
                self.metrics.request_started(command.app_id)
            self._record_decision_metrics(command, "allow", "policy")

            # Retourner le stream wrapper avec cleanup
            return self._stream_with_metrics(
                command=command,
                domain_request=domain_request,
                policy_decision=policy_decision,
                budget_status=budget_status,
            )

        except Exception as e:
            self._record_request_metrics(command, status="error", latency_seconds=0)
            if self.metrics:
                self.metrics.record_error(command.app_id, type(e).__name__)
            return LLMRequestResult(
                request_id=command.request_id,
                outcome=RequestOutcome.ERROR,
                error_message=str(e),
            )

    async def _stream_with_metrics(
        self,
        command: LLMRequestCommand,
        domain_request: DomainChatRequest,
        policy_decision: PolicyDecision,
        budget_status: BudgetStatus,
    ) -> AsyncIterator[str]:
        """Wrapper de streaming avec enregistrement des métriques à la fin."""
        start_time = time.time()
        try:
            async for chunk in self.llm_provider.chat_stream(
                domain_request, command.api_key
            ):
                yield chunk

            # Enregistrer les métriques après streaming
            latency_seconds = time.time() - start_time
            self._record_request_metrics(
                command=command,
                status="success",
                latency_seconds=latency_seconds,
            )
        except Exception as e:
            latency_seconds = time.time() - start_time
            self._record_request_metrics(
                command=command,
                status="error",
                latency_seconds=latency_seconds,
            )
            if self.metrics:
                self.metrics.record_error(command.app_id, type(e).__name__)
            raise
        finally:
            if self.metrics:
                self.metrics.request_finished(command.app_id)

    async def _handle_blocked_request(
        self,
        command: LLMRequestCommand,
        trace,
        outcome: RequestOutcome,
        blocked_by: str,
        reason: str,
    ) -> None:
        """Handle a blocked request: fail trace."""
        # Fail the trace with correct outcome
        if trace and self.request_tracer:
            await self.request_tracer.fail_trace(
                trace_id=trace.trace_id,
                error=reason,
                step=blocked_by,
                outcome=outcome.value if outcome else "error",
            )

    async def _handle_successful_request(
        self,
        command: LLMRequestCommand,
        trace,
        response,
        outcome: str = "allowed",
    ) -> None:
        """Handle a successful request: complete trace."""
        # Complete the trace
        if trace and self.request_tracer:
            await self.request_tracer.complete_trace(
                trace_id=trace.trace_id,
                outcome=outcome,
                final_data={
                    "input_tokens": response.input_tokens if response else 0,
                    "output_tokens": response.output_tokens if response else 0,
                    "model": response.model if response else command.model,
                },
            )

    async def _start_span(self, trace, step: str, data: dict | None = None) -> None:
        """Start a trace span if tracer is configured."""
        if trace and self.request_tracer:
            await self.request_tracer.start_span(trace.trace_id, step, data)

    async def _end_span(
        self,
        trace,
        step: str,
        status: str = "ok",
        data: dict | None = None,
        error: str | None = None,
    ) -> None:
        """End a trace span if tracer is configured."""
        if trace and self.request_tracer:
            await self.request_tracer.end_span(
                trace.trace_id, step, status, data, error
            )