返回 CodeWhale
portable_projection.rs
根目录 / crates / engine / src / tool_inspection / portable_projection.rs
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 fn bounded(value: &crate::tool_inspection::BoundedString) -> DebugBoundedString {
7 DebugBoundedString {
8 value: value.value.clone(),
9 truncated: value.truncated,
10 }
11 }
12
13 fn evidence<T, U>(
14 value: &crate::tool_inspection::Evidence<T>,
15 convert: impl Fn(&T) -> U,
16 ) -> DebugEvidence<U> {
17 match value {
18 crate::tool_inspection::Evidence::Known { value } => DebugEvidence::Known {
19 value: convert(value),
20 },
21 crate::tool_inspection::Evidence::Unknown { reason } => DebugEvidence::Unknown {
22 reason: reason.clone(),
23 },
24 }
25 }
26
27 fn bounded_list(value: &crate::tool_inspection::BoundedList) -> DebugBoundedList {
28 DebugBoundedList {
29 count: value.count,
30 rendered: value.rendered.iter().map(bounded).collect(),
31 omitted: value.omitted,
32 }
33 }
34
35 pub fn tool_snapshot(value: &crate::tool_inspection::ToolInspectionSnapshot) -> DebugToolSnapshot {
36 use crate::tool_inspection::{
37 ProviderAvailability as P, ToolProvenance as R, ToolVisibility as V, TurnStopReason as S,
38 };
39 DebugToolSnapshot {
40 schema_version: value.schema_version,
41 capture_source: value.capture_source.to_string(),
42 delivery_status: value.delivery_status.to_string(),
43 turn_id: bounded(&value.turn_id),
44 step: value.step,
45 terminal: value
46 .terminal
47 .as_ref()
48 .map(|terminal| DebugTurnStopDiagnostics {
49 status: terminal.status.map(|status| match status {
50 crate::core::events::TurnOutcomeStatus::Completed => {
51 DebugTurnOutcomeStatus::Completed
52 }
53 crate::core::events::TurnOutcomeStatus::Interrupted => {
54 DebugTurnOutcomeStatus::Interrupted
55 }
56 crate::core::events::TurnOutcomeStatus::Failed => {
57 DebugTurnOutcomeStatus::Failed
58 }
59 }),
60 reason: terminal.reason.map(|reason| match reason {
61 S::ProviderNoToolCall => DebugTurnStopReason::ProviderNoToolCall,
62 S::ProviderToolCallMissing => DebugTurnStopReason::ProviderToolCallMissing,
63 S::StepBudgetExhausted => DebugTurnStopReason::StepBudgetExhausted,
64 S::NoProgress => DebugTurnStopReason::NoProgress,
65 S::Interrupted => DebugTurnStopReason::Interrupted,
66 S::Failed => DebugTurnStopReason::Failed,
67 }),
68 effective_max_steps: terminal.effective_max_steps,
69 step_budget_source: terminal.step_budget_source.to_string(),
70 model_step_index: terminal.model_step_index,
71 model_requests_started: terminal.model_requests_started,
72 transport_retries: terminal.transport_retries,
73 transparent_stream_retries: terminal.transparent_stream_retries,
74 stream_resumes: terminal.stream_resumes,
75 reasoning_only_reprompts: terminal.reasoning_only_reprompts,
76 empty_stop_retries: terminal.empty_stop_retries,
77 final_report_requested: terminal.final_report_requested,
78 permission_strategy_switches: terminal.permission_strategy_switches,
79 permission_denial_rounds_without_progress: terminal
80 .permission_denial_rounds_without_progress,
81 last_provider_finish_reason: terminal
82 .last_provider_finish_reason
83 .as_ref()
84 .map(bounded),
85 last_response_tool_calls: terminal.last_response_tool_calls,
86 last_response_tool_calls_suppressed: terminal.last_response_tool_calls_suppressed,
87 last_reported_input_tokens: terminal.last_reported_input_tokens,
88 route_context_window_tokens: terminal.route_context_window_tokens,
89 last_prepared_output_limit_tokens: terminal.last_prepared_output_limit_tokens,
90 automatic_compaction_attempts: terminal.automatic_compaction_attempts,
91 emergency_compaction_attempts: terminal.emergency_compaction_attempts,
92 }),
93 tools_field_present: value.tools_field_present,
94 tool_count: value.tool_count,
95 rendered_tool_count: value.rendered_tool_count,
96 omitted_tool_count: value.omitted_tool_count,
97 payload_json_bytes: value.payload_json_bytes,
98 payload_measurement_status: value.payload_measurement_status.clone(),
99 active_tool_catalog_sha256: value.active_tool_catalog_sha256.clone(),
100 unavailable_for_this_request: value
101 .unavailable_for_this_request
102 .iter()
103 .map(|item| (*item).to_string())
104 .collect(),
105 provider: match &value.provider {
106 P::Unknown => DebugProviderAvailability::Unknown,
107 P::Available { provider, model } => DebugProviderAvailability::Available {
108 provider: provider.clone(),
109 model: model.clone(),
110 },
111 P::Unavailable { reason } => DebugProviderAvailability::Unavailable {
112 reason: reason.clone(),
113 },
114 },
115 registry_facts_present: value.registry_facts_present,
116 registry_tool_count: evidence(&value.registry_tool_count, |count| *count),
117 registry_only_tools: evidence(&value.registry_only_tools, bounded_list),
118 tools: value
119 .tools
120 .iter()
121 .map(|tool| DebugToolProjection {
122 ordinal: tool.ordinal,
123 name: bounded(&tool.name),
124 tool_type: evidence(&tool.tool_type, bounded),
125 description: bounded(&tool.description),
126 input_schema_json: bounded(&tool.input_schema_json),
127 allowed_callers: evidence(&tool.allowed_callers, bounded_list),
128 defer_loading: evidence(&tool.defer_loading, |flag| *flag),
129 input_examples: evidence(&tool.input_examples, |count| DebugCountOnly {
130 count: count.count,
131 values: count.values.to_string(),
132 }),
133 strict: evidence(&tool.strict, |flag| *flag),
134 cache_control_type: evidence(&tool.cache_control_type, bounded),
135 provenance: evidence(&tool.provenance, |origin| match origin {
136 R::Builtin => DebugToolProvenance::Builtin,
137 R::Plugin => DebugToolProvenance::Plugin,
138 R::Mcp => DebugToolProvenance::Mcp,
139 R::Synthetic => DebugToolProvenance::Synthetic,
140 R::Unknown => DebugToolProvenance::Unknown,
141 }),
142 mcp_server: evidence(&tool.mcp_server, bounded),
143 capabilities: evidence(&tool.capabilities, bounded_list),
144 approval: evidence(&tool.approval, bounded),
145 model_visible: evidence(&tool.model_visible, |flag| *flag),
146 visibility: match tool.visibility {
147 V::Active => DebugToolVisibility::Active,
148 V::Deferred => DebugToolVisibility::Deferred,
149 V::InRequest => DebugToolVisibility::InRequest,
150 },
151 })
152 .collect(),
153 }
154 }
155
155 lines RUST