| 1 | //! Value-only projection owned by the authoritative source module. |
| 2 | //! No command, App or rendering operation is reachable from these conversions. |
| 3 | |
| 4 | use codewhale_command_contract::facets::*; |
| 5 | |
| 6 | pub(crate) fn source_map(report: crate::context_report::PromptSourceMap) -> DebugPromptSourceMap { |
| 7 | use crate::context_budget::PressureLevel; |
| 8 | use crate::context_report::{ActivationReason as A, CountingConfidence as C, SourceKind as S}; |
| 9 | use crate::route_runtime::ContextWindowSource; |
| 10 | let pressure_label = report |
| 11 | .budget_used_percent |
| 12 | .map(|percent| PressureLevel::from_usage_percent(percent).label()) |
| 13 | .unwrap_or("unknown") |
| 14 | .to_string(); |
| 15 | let source_label = report |
| 16 | .context_window_source |
| 17 | .as_deref() |
| 18 | .unwrap_or_else(|| ContextWindowSource::Fallback.label()); |
| 19 | let context_window_verified = |
| 20 | ContextWindowSource::from_label(source_label).is_some_and(ContextWindowSource::is_verified); |
| 21 | let kind = |kind| match kind { |
| 22 | S::Constitution => DebugSourceKind::Constitution, |
| 23 | S::UserConstitution => DebugSourceKind::UserConstitution, |
| 24 | S::RepoConstitution => DebugSourceKind::RepoConstitution, |
| 25 | S::ProjectContext => DebugSourceKind::ProjectContext, |
| 26 | S::ProjectContextWarning => DebugSourceKind::ProjectContextWarning, |
| 27 | S::ProjectContextPack => DebugSourceKind::ProjectContextPack, |
| 28 | S::SkillsBlock => DebugSourceKind::SkillsBlock, |
| 29 | S::ContextManagement => DebugSourceKind::ContextManagement, |
| 30 | S::CompactionRelayTemplate => DebugSourceKind::CompactionRelayTemplate, |
| 31 | S::RuntimePolicy => DebugSourceKind::RuntimePolicy, |
| 32 | S::AuthorityRecap => DebugSourceKind::AuthorityRecap, |
| 33 | S::EnvironmentBlock => DebugSourceKind::EnvironmentBlock, |
| 34 | S::UserMemory => DebugSourceKind::UserMemory, |
| 35 | S::SessionGoal => DebugSourceKind::SessionGoal, |
| 36 | S::HandoffRelay => DebugSourceKind::HandoffRelay, |
| 37 | S::ToolSchemas => DebugSourceKind::ToolSchemas, |
| 38 | S::UserRequest => DebugSourceKind::UserRequest, |
| 39 | S::ConversationHistory => DebugSourceKind::ConversationHistory, |
| 40 | S::ToolResult => DebugSourceKind::ToolResult, |
| 41 | S::ModelProviderFact => DebugSourceKind::ModelProviderFact, |
| 42 | }; |
| 43 | DebugPromptSourceMap { |
| 44 | entries: report |
| 45 | .entries |
| 46 | .into_iter() |
| 47 | .map(|entry| DebugSourceEntry { |
| 48 | source_kind: kind(entry.source_kind), |
| 49 | label: entry.label, |
| 50 | source_path: entry.source_path, |
| 51 | activation_reason: match entry.activation_reason { |
| 52 | A::AlwaysOn => DebugActivationReason::AlwaysOn, |
| 53 | A::FilePresent => DebugActivationReason::FilePresent, |
| 54 | A::ConfigEnabled => DebugActivationReason::ConfigEnabled, |
| 55 | A::RuntimeState => DebugActivationReason::RuntimeState, |
| 56 | A::PerRequest => DebugActivationReason::PerRequest, |
| 57 | A::Omitted => DebugActivationReason::Omitted, |
| 58 | }, |
| 59 | estimated_tokens: entry.estimated_tokens, |
| 60 | counting_confidence: match entry.counting_confidence { |
| 61 | C::High => DebugCountingConfidence::High, |
| 62 | C::Approximate => DebugCountingConfidence::Approximate, |
| 63 | }, |
| 64 | authority_tier: entry.authority_tier, |
| 65 | truncation_reason: entry.truncation_reason, |
| 66 | }) |
| 67 | .collect(), |
| 68 | total_estimated_tokens: report.total_estimated_tokens, |
| 69 | active_context_estimated_tokens: report.active_context_estimated_tokens, |
| 70 | overflow_guard_estimated_tokens: report.overflow_guard_estimated_tokens, |
| 71 | context_window_tokens: report.context_window_tokens, |
| 72 | context_window_source: report.context_window_source, |
| 73 | budget_used_percent: report.budget_used_percent, |
| 74 | pressure_label, |
| 75 | context_window_verified, |
| 76 | generated_at: report.generated_at, |
| 77 | note: report.note, |
| 78 | } |
| 79 | } |
| 80 |