every-other-token 4.3.2

Watch an LLM answer token by token: a CLI and web UI that intercepts the OpenAI or Anthropic stream, shows per-token confidence and perplexity from logprobs, and rewrites every other token live. Mock provider needs no API key.
Documentation
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
//! Multi-turn conversation state tracking.
//!
//! Tracks conversation turns across multiple interactions, with topic analysis,
//! context windowing, and conversation summarization.

use std::collections::HashMap;

// ---------------------------------------------------------------------------
// TurnRole
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum TurnRole {
    User,
    Assistant,
    System,
    Tool,
}

// ---------------------------------------------------------------------------
// Turn
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct Turn {
    pub id: u64,
    pub role: TurnRole,
    pub content: String,
    pub timestamp_ms: u64,
    pub metadata: HashMap<String, String>,
    pub token_count: usize,
}

impl Turn {
    fn estimate_tokens(content: &str) -> usize {
        // Rough approximation: ~4 chars per token
        (content.len() / 4).max(1)
    }
}

// ---------------------------------------------------------------------------
// ConversationState
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConversationState {
    Active,
    Paused,
    Completed,
    Abandoned,
}

// ---------------------------------------------------------------------------
// TurnError
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TurnError {
    MaxTurnsExceeded,
    InvalidStateTransition,
    EmptyContent,
}

impl std::fmt::Display for TurnError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TurnError::MaxTurnsExceeded => write!(f, "Maximum number of turns exceeded"),
            TurnError::InvalidStateTransition => write!(f, "Invalid state transition"),
            TurnError::EmptyContent => write!(f, "Turn content must not be empty"),
        }
    }
}

impl std::error::Error for TurnError {}

// ---------------------------------------------------------------------------
// TopicTracker
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct TopicTracker {
    pub topics: Vec<String>,
    pub weights: Vec<f64>,
}

impl TopicTracker {
    pub fn new() -> Self {
        Self {
            topics: Vec::new(),
            weights: Vec::new(),
        }
    }

    /// Extract key terms from `text` and update topic weights with exponential decay.
    pub fn update(&mut self, text: &str) {
        // Apply decay to existing weights
        for w in self.weights.iter_mut() {
            *w *= 0.85;
        }

        // Extract candidate terms: capitalized words or words longer than 6 chars
        let candidates: Vec<String> = text
            .split_whitespace()
            .filter_map(|word| {
                let clean: String = word
                    .chars()
                    .filter(|c| c.is_alphabetic())
                    .collect();
                if clean.len() < 2 {
                    return None;
                }
                let is_capitalized = clean.chars().next().map(|c| c.is_uppercase()).unwrap_or(false);
                let is_long = clean.len() > 6;
                if is_capitalized || is_long {
                    Some(clean.to_lowercase())
                } else {
                    None
                }
            })
            .collect();

        for term in candidates {
            if let Some(pos) = self.topics.iter().position(|t| t == &term) {
                self.weights[pos] += 1.0;
            } else {
                self.topics.push(term);
                self.weights.push(1.0);
            }
        }

        // Remove very-low-weight topics
        let threshold = 0.01;
        let mut i = 0;
        while i < self.topics.len() {
            if self.weights[i] < threshold {
                self.topics.remove(i);
                self.weights.remove(i);
            } else {
                i += 1;
            }
        }
    }

    /// Return the topic with the highest weight.
    pub fn primary_topic(&self) -> Option<&str> {
        self.weights
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(i, _)| self.topics[i].as_str())
    }
}

impl Default for TopicTracker {
    fn default() -> Self {
        Self::new()
    }
}

// ---------------------------------------------------------------------------
// ConversationSummary
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct ConversationSummary {
    pub total_turns: usize,
    pub user_turns: usize,
    pub assistant_turns: usize,
    pub total_tokens: usize,
    pub primary_topic: Option<String>,
    pub duration_ms: u64,
}

// ---------------------------------------------------------------------------
// MultiTurnConversation
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct MultiTurnConversation {
    pub id: String,
    pub turns: Vec<Turn>,
    pub state: ConversationState,
    pub topic_tracker: TopicTracker,
    pub created_at_ms: u64,
    pub max_turns: usize,
}

impl MultiTurnConversation {
    pub fn new(id: String, max_turns: usize) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);
        Self {
            id,
            turns: Vec::new(),
            state: ConversationState::Active,
            topic_tracker: TopicTracker::new(),
            created_at_ms: now,
            max_turns,
        }
    }

    /// Add a turn to the conversation. Returns the new turn ID on success.
    pub fn add_turn(
        &mut self,
        role: TurnRole,
        content: String,
        timestamp_ms: u64,
    ) -> Result<u64, TurnError> {
        if content.trim().is_empty() {
            return Err(TurnError::EmptyContent);
        }
        if self.state != ConversationState::Active {
            return Err(TurnError::InvalidStateTransition);
        }
        if self.turns.len() >= self.max_turns {
            return Err(TurnError::MaxTurnsExceeded);
        }

        let token_count = Turn::estimate_tokens(&content);
        self.topic_tracker.update(&content);

        let id = self.turns.len() as u64;
        self.turns.push(Turn {
            id,
            role,
            content,
            timestamp_ms,
            metadata: HashMap::new(),
            token_count,
        });
        Ok(id)
    }

    /// Return the last `n` turns.
    pub fn last_n_turns(&self, n: usize) -> Vec<&Turn> {
        let start = self.turns.len().saturating_sub(n);
        self.turns[start..].iter().collect()
    }

    /// Return all turns with the given role.
    pub fn turns_by_role(&self, role: &TurnRole) -> Vec<&Turn> {
        self.turns.iter().filter(|t| &t.role == role).collect()
    }

    /// Return the most recent turns that fit within `max_tokens`.
    pub fn context_window(&self, max_tokens: usize) -> Vec<&Turn> {
        let mut budget = max_tokens;
        let mut result: Vec<&Turn> = Vec::new();
        for turn in self.turns.iter().rev() {
            if turn.token_count > budget {
                break;
            }
            budget -= turn.token_count;
            result.push(turn);
        }
        result.reverse();
        result
    }

    /// Produce a summary of this conversation.
    pub fn conversation_summary(&self) -> ConversationSummary {
        let total_turns = self.turns.len();
        let user_turns = self.turns.iter().filter(|t| t.role == TurnRole::User).count();
        let assistant_turns = self.turns.iter().filter(|t| t.role == TurnRole::Assistant).count();
        let total_tokens = self.turns.iter().map(|t| t.token_count).sum();
        let primary_topic = self.topic_tracker.primary_topic().map(String::from);
        let duration_ms = if let (Some(first), Some(last)) = (self.turns.first(), self.turns.last()) {
            last.timestamp_ms.saturating_sub(first.timestamp_ms)
        } else {
            0
        };
        ConversationSummary {
            total_turns,
            user_turns,
            assistant_turns,
            total_tokens,
            primary_topic,
            duration_ms,
        }
    }
}

// ---------------------------------------------------------------------------
// MultiTurnManager
// ---------------------------------------------------------------------------

#[derive(Debug, Default)]
pub struct MultiTurnManager {
    pub conversations: HashMap<String, MultiTurnConversation>,
    next_id: u64,
}

impl MultiTurnManager {
    pub fn new() -> Self {
        Self {
            conversations: HashMap::new(),
            next_id: 0,
        }
    }

    /// Create a new conversation and return its ID.
    pub fn create(&mut self, max_turns: usize) -> String {
        let id = format!("conv-{}", self.next_id);
        self.next_id += 1;
        self.conversations
            .insert(id.clone(), MultiTurnConversation::new(id.clone(), max_turns));
        id
    }

    pub fn get(&self, id: &str) -> Option<&MultiTurnConversation> {
        self.conversations.get(id)
    }

    pub fn get_mut(&mut self, id: &str) -> Option<&mut MultiTurnConversation> {
        self.conversations.get_mut(id)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn ts() -> u64 {
        1_000_000
    }

    #[test]
    fn test_add_turns() {
        let mut conv = MultiTurnConversation::new("test".into(), 10);
        let id = conv.add_turn(TurnRole::User, "Hello world".into(), ts()).unwrap();
        assert_eq!(id, 0);
        let id2 = conv.add_turn(TurnRole::Assistant, "Hi there!".into(), ts() + 100).unwrap();
        assert_eq!(id2, 1);
        assert_eq!(conv.turns.len(), 2);
    }

    #[test]
    fn test_max_turns_exceeded() {
        let mut conv = MultiTurnConversation::new("test".into(), 2);
        conv.add_turn(TurnRole::User, "turn one".into(), ts()).unwrap();
        conv.add_turn(TurnRole::Assistant, "turn two".into(), ts()).unwrap();
        let err = conv.add_turn(TurnRole::User, "turn three".into(), ts()).unwrap_err();
        assert_eq!(err, TurnError::MaxTurnsExceeded);
    }

    #[test]
    fn test_empty_content_error() {
        let mut conv = MultiTurnConversation::new("test".into(), 10);
        let err = conv.add_turn(TurnRole::User, "   ".into(), ts()).unwrap_err();
        assert_eq!(err, TurnError::EmptyContent);
    }

    #[test]
    fn test_last_n_returns_correct() {
        let mut conv = MultiTurnConversation::new("test".into(), 20);
        for i in 0..10u64 {
            conv.add_turn(TurnRole::User, format!("message {}", i), ts() + i * 100).unwrap();
        }
        let last3 = conv.last_n_turns(3);
        assert_eq!(last3.len(), 3);
        assert!(last3[2].content.contains("9"));
    }

    #[test]
    fn test_context_window_respects_budget() {
        let mut conv = MultiTurnConversation::new("test".into(), 20);
        // each short message ~1 token, longer messages more
        conv.add_turn(TurnRole::User, "a".repeat(40), ts()).unwrap(); // ~10 tokens
        conv.add_turn(TurnRole::User, "b".repeat(40), ts() + 1).unwrap(); // ~10 tokens
        conv.add_turn(TurnRole::User, "c".repeat(40), ts() + 2).unwrap(); // ~10 tokens
        // Budget of 15 tokens should get at most 1-2 turns from the end
        let window = conv.context_window(15);
        let total: usize = window.iter().map(|t| t.token_count).sum();
        assert!(total <= 15);
    }

    #[test]
    fn test_summary_counts() {
        let mut conv = MultiTurnConversation::new("test".into(), 20);
        conv.add_turn(TurnRole::User, "Hello assistant".into(), ts()).unwrap();
        conv.add_turn(TurnRole::Assistant, "Hello user, how can I help?".into(), ts() + 500).unwrap();
        conv.add_turn(TurnRole::User, "Tell me about Rust programming".into(), ts() + 1000).unwrap();
        let summary = conv.conversation_summary();
        assert_eq!(summary.total_turns, 3);
        assert_eq!(summary.user_turns, 2);
        assert_eq!(summary.assistant_turns, 1);
        assert!(summary.total_tokens > 0);
        assert_eq!(summary.duration_ms, 1000);
    }

    #[test]
    fn test_topic_tracker_updates() {
        let mut tracker = TopicTracker::new();
        tracker.update("Discussing Rust programming language features");
        tracker.update("Rust ownership system is interesting");
        assert!(!tracker.topics.is_empty());
        let primary = tracker.primary_topic();
        assert!(primary.is_some());
    }

    #[test]
    fn test_multi_turn_manager_create_get() {
        let mut mgr = MultiTurnManager::new();
        let id = mgr.create(10);
        assert!(mgr.get(&id).is_some());
        let conv = mgr.get_mut(&id).unwrap();
        conv.add_turn(TurnRole::User, "test message".into(), ts()).unwrap();
        assert_eq!(mgr.get(&id).unwrap().turns.len(), 1);
    }
}