codetether_rlm/context_trace/
token_estimate.rs1use super::{ContextEvent, ContextTrace};
2
3impl ContextTrace {
4 pub fn estimate_tokens(text: &str) -> usize {
6 (text.chars().count() / 4).max(1)
7 }
8
9 pub fn event_from_text(event: ContextEvent, text: &str) -> ContextEvent {
11 let tokens = Self::estimate_tokens(text);
12 match event {
13 ContextEvent::SystemPrompt { content, .. } => Self::system_prompt(content, tokens),
14 ContextEvent::GrepResult {
15 pattern, matches, ..
16 } => Self::grep_result(pattern, matches, tokens),
17 ContextEvent::LlmQueryResult {
18 query,
19 response_preview,
20 ..
21 } => Self::llm_result(query, response_preview, tokens),
22 ContextEvent::AssistantCode { code, .. } => {
23 ContextEvent::AssistantCode { code, tokens }
24 }
25 ContextEvent::ExecutionOutput { output, .. } => {
26 ContextEvent::ExecutionOutput { output, tokens }
27 }
28 ContextEvent::Final { answer, .. } => ContextEvent::Final { answer, tokens },
29 ContextEvent::ToolCall {
30 name,
31 arguments_preview,
32 ..
33 } => Self::tool_call(name, arguments_preview, tokens),
34 ContextEvent::ToolResult {
35 tool_call_id,
36 result_preview,
37 ..
38 } => Self::tool_result(tool_call_id, result_preview, tokens),
39 }
40 }
41
42 fn system_prompt(content: String, tokens: usize) -> ContextEvent {
43 ContextEvent::SystemPrompt { content, tokens }
44 }
45}