tokmesh-core 0.1.5

Core library for tokmesh — session parsing, scanning, aggregation, pricing
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
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
//! Cline task parser
//!
//! Cline ships in two flavours that persist sessions in unrelated layouts, so
//! `parse_cline_file` inspects the path and dispatches to the right handler:
//!
//! - **VS Code extension** (`saoudrizwan.claude-dev`): one
//!   `ui_messages.json` per task under VS Code globalStorage. Cline is the
//!   upstream Roo Code / Kilo forked from, so this layout is shared and
//!   handled by `roocode::parse_roo_kilo_file`.
//! - **Cline CLI / desktop** (`~/.cline/data/sessions/<id>/`): a
//!   `<id>.messages.json` transcript plus a sibling `<id>.json` manifest. This
//!   is the newer standalone runtime and is handled locally below.

use super::roocode::parse_roo_kilo_file;
use super::utils::{extract_i64, file_modified_timestamp_ms, parse_timestamp_value};
use super::{normalize_workspace_key, workspace_label_from_key, UnifiedMessage};
use crate::TokenBreakdown;
use serde::Deserialize;
use serde_json::Value;
use std::path::{Path, PathBuf};

/// Entry point shared by the aggregator. Routes VS Code task logs to the
/// shared Roo/Kilo parser and the CLI transcript format to the local handler.
pub fn parse_cline_file(path: &Path) -> Vec<UnifiedMessage> {
    if is_cline_cli_messages_path(path) {
        return parse_cline_cli_file(path);
    }
    parse_roo_kilo_file(path, "cline")
}

// ---------------------------------------------------------------------------
// Cline CLI / desktop transcript format
// ---------------------------------------------------------------------------

/// Top-level shape of `<id>.messages.json`.
#[derive(Debug, Deserialize)]
struct ClineCliMessagesFile {
    #[serde(rename = "sessionId")]
    session_id: Option<String>,
    agent: Option<String>,
    messages: Option<Vec<ClineCliMessage>>,
}

/// A single message in the transcript. Only `assistant` entries carry metrics;
/// `user` entries are inspected solely to detect human-vs-tool-result turns.
#[derive(Debug, Deserialize)]
struct ClineCliMessage {
    id: Option<String>,
    role: Option<String>,
    ts: Option<Value>,
    content: Option<Vec<Value>>,
    #[serde(rename = "modelInfo")]
    model_info: Option<ClineCliModelInfo>,
    metrics: Option<ClineCliMetrics>,
}

#[derive(Debug, Deserialize)]
struct ClineCliModelInfo {
    id: Option<String>,
    provider: Option<String>,
}

/// Token + cost metrics attached to assistant messages. Cline records
/// `inputTokens` as the **total** prompt size for the call (cache hits
/// included), so the parser must subtract `cacheReadTokens`/`cacheWriteTokens`
/// before storing the net input — otherwise `TokenBreakdown::total()` would
/// double-count the cached portion.
///
/// All fields arrive as JSON values rather than typed numbers because some
/// providers emit them as strings.
#[derive(Debug, Deserialize)]
struct ClineCliMetrics {
    #[serde(rename = "inputTokens")]
    input_tokens: Option<Value>,
    #[serde(rename = "outputTokens")]
    output_tokens: Option<Value>,
    #[serde(rename = "cacheReadTokens")]
    cache_read_tokens: Option<Value>,
    #[serde(rename = "cacheWriteTokens")]
    cache_write_tokens: Option<Value>,
    cost: Option<Value>,
}

/// Sibling `<id>.json` manifest. Carries provider/model/workspace/title when
/// the transcript itself omits `modelInfo` (e.g. a resumed session whose first
/// calls predate that field).
#[derive(Debug, Default, Deserialize)]
struct ClineCliManifest {
    session_id: Option<String>,
    provider: Option<String>,
    model: Option<String>,
    cwd: Option<String>,
    #[serde(rename = "workspace_root")]
    workspace_root: Option<String>,
    metadata: Option<Value>,
}

/// Filename sentinel identifying the CLI transcript layout: VS Code task logs
/// are named `ui_messages.json` (exact), CLI transcripts are `<id>.messages.json`,
/// so a suffix check cannot collide with the older format.
pub(crate) fn is_cline_cli_messages_path(path: &Path) -> bool {
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.ends_with(".messages.json"))
}

/// Path of the sibling manifest for a CLI transcript: drop the `.messages`
/// infix from `<id>.messages.json` to get `<id>.json`.
pub(crate) fn cline_cli_manifest_path(path: &Path) -> PathBuf {
    let stem = path
        .file_stem()
        .and_then(|name| name.to_str())
        .unwrap_or_default();
    let session_stem = stem.strip_suffix(".messages").unwrap_or(stem);
    path.parent()
        .unwrap_or_else(|| Path::new("."))
        .join(format!("{session_stem}.json"))
}

/// Parse Cline CLI's persisted assistant messages from
/// `~/.cline/data/sessions/<session>/<session>.messages.json`.
fn parse_cline_cli_file(path: &Path) -> Vec<UnifiedMessage> {
    let Some(data) = super::utils::read_file_or_none(path) else {
        return Vec::new();
    };

    let mut bytes = data;
    let Ok(file) = simd_json::from_slice::<ClineCliMessagesFile>(&mut bytes) else {
        return Vec::new();
    };
    let manifest = read_cline_cli_manifest(path);

    let session_id = file
        .session_id
        .or(manifest.session_id)
        .or_else(|| {
            path.file_stem()
                .and_then(|name| name.to_str())
                .map(|name| name.trim_end_matches(".messages").to_string())
        })
        .unwrap_or_else(|| "unknown".to_string());
    let fallback_timestamp = file_modified_timestamp_ms(path);
    let workspace_key = manifest
        .workspace_root
        .as_deref()
        .or(manifest.cwd.as_deref())
        .and_then(normalize_workspace_key);
    let workspace_label = workspace_key.as_deref().and_then(workspace_label_from_key);
    let session_title = manifest
        .metadata
        .as_ref()
        .and_then(|metadata| metadata.get("title"))
        .and_then(Value::as_str)
        .map(ToOwned::to_owned);

    let mut current_model =
        non_empty_string(manifest.model.as_deref()).unwrap_or_else(|| "unknown".to_string());
    let mut current_provider =
        non_empty_string(manifest.provider.as_deref()).unwrap_or_else(|| "unknown".to_string());
    let mut pending_turn_start = false;
    let mut assistant_index = 0usize;
    let mut messages = Vec::new();

    for entry in file.messages.unwrap_or_default() {
        // User entries never carry tokens but flag whether the *next* assistant
        // reply opens a fresh human turn (vs. continuing after a tool_result).
        if entry.role.as_deref() == Some("user") {
            if is_human_user_prompt(entry.content.as_deref()) {
                pending_turn_start = true;
            }
            continue;
        }
        if entry.role.as_deref() != Some("assistant") {
            continue;
        }

        // modelInfo may evolve mid-session (e.g. a `/model` switch); remember
        // the latest sighting so later messages without one still resolve.
        if let Some(model_info) = entry.model_info.as_ref() {
            if let Some(model) = non_empty_string(model_info.id.as_deref()) {
                current_model = model;
            }
            if let Some(provider) = non_empty_string(model_info.provider.as_deref()) {
                current_provider = provider;
            }
        }

        let Some(metrics) = entry.metrics else {
            continue;
        };
        let cache_read = extract_i64(metrics.cache_read_tokens.as_ref())
            .unwrap_or(0)
            .max(0);
        let cache_write = extract_i64(metrics.cache_write_tokens.as_ref())
            .unwrap_or(0)
            .max(0);
        // `inputTokens` is inclusive of cached tokens (see ClineCliMetrics); pull
        // them back out so the breakdown sums without double counting.
        let input = extract_i64(metrics.input_tokens.as_ref())
            .unwrap_or(0)
            .max(0)
            .saturating_sub(cache_read)
            .saturating_sub(cache_write);
        let output = extract_i64(metrics.output_tokens.as_ref())
            .unwrap_or(0)
            .max(0);
        let reported_cost = extract_non_negative_finite_f64(metrics.cost.as_ref());

        // Skip vacuous entries: no tokens and no provider-reported cost means
        // this assistant message produced nothing billable.
        if input == 0
            && output == 0
            && cache_read == 0
            && cache_write == 0
            && reported_cost.is_none()
        {
            continue;
        }

        let timestamp = entry
            .ts
            .as_ref()
            .and_then(parse_timestamp_value)
            .unwrap_or(fallback_timestamp);
        let dedup_key = entry
            .id
            .as_deref()
            .filter(|id| !id.trim().is_empty())
            .map(|id| format!("cline-cli:{session_id}:{id}"))
            .unwrap_or_else(|| format!("cline-cli:{session_id}:{assistant_index}"));
        let cost = reported_cost.unwrap_or(0.0);

        let mut message = UnifiedMessage::new_with_agent(
            "cline",
            current_model.clone(),
            current_provider.clone(),
            session_id.clone(),
            timestamp,
            TokenBreakdown {
                input,
                output,
                cache_read,
                cache_write,
                reasoning: 0,
            },
            cost,
            file.agent.clone(),
        );
        message.dedup_key = Some(dedup_key);
        message.is_turn_start = pending_turn_start;
        message.session_title = session_title.clone();
        message.set_workspace(workspace_key.clone(), workspace_label.clone());
        if reported_cost.is_some() {
            message.mark_provider_reported_cost();
        }
        messages.push(message);

        assistant_index += 1;
        pending_turn_start = false;
    }

    messages
}

/// Detect a genuine human prompt: content has at least one `text` block and no
/// `tool_result` block. Tool-result echoes are role `"user"` but must not start
/// a new turn.
fn is_human_user_prompt(content: Option<&[Value]>) -> bool {
    let Some(content) = content else {
        return false;
    };
    let mut has_text = false;
    for block in content {
        match block.get("type").and_then(Value::as_str) {
            Some("tool_result") => return false,
            Some("text") => has_text = true,
            _ => {}
        }
    }
    has_text
}

fn read_cline_cli_manifest(path: &Path) -> ClineCliManifest {
    let manifest_path = cline_cli_manifest_path(path);
    let Ok(mut bytes) = std::fs::read(manifest_path) else {
        return ClineCliManifest::default();
    };
    simd_json::from_slice(&mut bytes).unwrap_or_default()
}

fn non_empty_string(value: Option<&str>) -> Option<String> {
    value
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

fn extract_f64(value: Option<&Value>) -> Option<f64> {
    value.and_then(|value| {
        value
            .as_f64()
            .or_else(|| value.as_i64().map(|value| value as f64))
            .or_else(|| value.as_u64().map(|value| value as f64))
            .or_else(|| value.as_str().and_then(|value| value.parse::<f64>().ok()))
    })
}

fn extract_non_negative_finite_f64(value: Option<&Value>) -> Option<f64> {
    extract_f64(value).filter(|value| value.is_finite() && *value >= 0.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sessions::CostSource;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_parse_cline_valid_api_req_started() {
        let dir = TempDir::new().unwrap();
        let task_dir = dir.path().join("tasks").join("cline-task-1");
        fs::create_dir_all(&task_dir).unwrap();
        fs::write(
            task_dir.join("ui_messages.json"),
            r#"[
  {
    "type": "say",
    "say": "api_req_started",
    "ts": "2026-02-18T12:00:00Z",
    "text": "{\"cost\":0.05,\"tokensIn\":40,\"tokensOut\":15,\"cacheReads\":7,\"cacheWrites\":3,\"apiProtocol\":\"anthropic\"}"
  }
]"#,
        )
        .unwrap();
        fs::write(
            task_dir.join("api_conversation_history.json"),
            r#"
<environment_details>
<model>claude-sonnet-4</model>
<name>ClineAgent</name>
</environment_details>
"#,
        )
        .unwrap();

        let messages = parse_cline_file(&task_dir.join("ui_messages.json"));
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].client, "cline");
        assert_eq!(messages[0].provider_id, "anthropic");
        assert_eq!(messages[0].model_id, "claude-sonnet-4");
        assert_eq!(messages[0].session_id, "cline-task-1");
        assert_eq!(messages[0].agent.as_deref(), Some("ClineAgent"));
        assert_eq!(messages[0].tokens.input, 40);
        assert_eq!(messages[0].tokens.output, 15);
        assert_eq!(messages[0].tokens.cache_read, 7);
        assert_eq!(messages[0].tokens.cache_write, 3);
        assert_eq!(messages[0].cost, 0.05);
    }

    #[test]
    fn test_parse_cline_ignores_non_api_req_started_events() {
        let dir = TempDir::new().unwrap();
        let task_dir = dir.path().join("tasks").join("cline-task-2");
        fs::create_dir_all(&task_dir).unwrap();
        fs::write(
            task_dir.join("ui_messages.json"),
            r#"[
  {
    "type": "say",
    "say": "assistant_message",
    "ts": "2026-02-18T12:00:00Z",
    "text": "{\"cost\":0.2,\"tokensIn\":10,\"tokensOut\":1,\"cacheReads\":0,\"cacheWrites\":0,\"apiProtocol\":\"anthropic\"}"
  }
]"#,
        )
        .unwrap();

        let messages = parse_cline_file(&task_dir.join("ui_messages.json"));
        assert!(messages.is_empty());
    }

    #[test]
    fn test_parse_cline_cli_messages() {
        let dir = TempDir::new().unwrap();
        let session_dir = dir.path().join("sessions").join("cline-cli-session");
        fs::create_dir_all(&session_dir).unwrap();
        fs::write(
            session_dir.join("cline-cli-session.json"),
            r#"{
  "session_id": "cline-cli-session",
  "provider": "cline-pass",
  "model": "cline-pass/glm-5.2",
  "workspace_root": "/home/example/project",
  "metadata": {"title": "CLI task"}
}"#,
        )
        .unwrap();
        fs::write(
            session_dir.join("cline-cli-session.messages.json"),
            r#"{
  "sessionId": "cline-cli-session",
  "agent": "lead",
  "messages": [
    {
      "role": "user",
      "ts": 1785320464923,
      "content": [{"type": "text", "text": "Inspect this project."}]
    },
    {
      "id": "msg-1",
      "role": "assistant",
      "ts": 1785320475705,
      "modelInfo": {"id": "cline-free/glm-5.2", "provider": "cline-pass"},
      "metrics": {
        "inputTokens": 7507,
        "outputTokens": 131,
        "cacheReadTokens": 50,
        "cacheWriteTokens": 0,
        "cost": 0.0110232
      }
    },
    {"role": "assistant", "metrics": {"inputTokens": 0, "outputTokens": 0}}
  ]
}"#,
        )
        .unwrap();

        let messages = parse_cline_file(&session_dir.join("cline-cli-session.messages.json"));
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].client, "cline");
        assert_eq!(messages[0].provider_id, "cline-pass");
        assert_eq!(messages[0].model_id, "cline-free/glm-5.2");
        assert_eq!(messages[0].session_id, "cline-cli-session");
        assert_eq!(messages[0].agent.as_deref(), Some("lead"));
        // 7507 total input minus 50 cache read = 7457 net input.
        assert_eq!(messages[0].tokens.input, 7457);
        assert_eq!(messages[0].tokens.output, 131);
        assert_eq!(messages[0].tokens.cache_read, 50);
        assert_eq!(messages[0].tokens.cache_write, 0);
        assert_eq!(messages[0].cost, 0.0110232);
        assert_eq!(messages[0].cost_source, CostSource::ProviderReported);
        assert_eq!(messages[0].workspace_label.as_deref(), Some("project"));
        assert_eq!(messages[0].session_title.as_deref(), Some("CLI task"));
        assert!(messages[0].is_turn_start);
        assert_eq!(
            messages[0].dedup_key.as_deref(),
            Some("cline-cli:cline-cli-session:msg-1")
        );
    }

    #[test]
    fn test_parse_cline_cli_turn_starts_ignore_tool_results() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("turns.messages.json");
        fs::write(
            &path,
            r#"{
  "sessionId": "turns",
  "messages": [
    {
      "id": "prompt-1",
      "role": "user",
      "content": [
        {"type": "text", "text": "Please inspect the repository."}
      ]
    },
    {
      "id": "assistant-tool-use",
      "role": "assistant",
      "content": [
        {"type": "text", "text": "I will inspect the repository."},
        {
          "type": "tool_use",
          "id": "tool-1",
          "name": "read_file",
          "input": {"path": "README.md"}
        },
        {"type": "future_block", "payload": {"priority": "low"}}
      ],
      "modelInfo": {"id": "provider/model", "provider": "provider"},
      "metrics": {
        "inputTokens": 100,
        "outputTokens": 20,
        "cacheReadTokens": 10,
        "cacheWriteTokens": 5,
        "cost": 0.02
      }
    },
    {
      "id": "tool-result",
      "role": "user",
      "content": [
        {
          "type": "tool_result",
          "tool_use_id": "tool-1",
          "content": [{"type": "text", "text": "README contents"}]
        }
      ]
    },
    {
      "id": "assistant-final",
      "role": "assistant",
      "content": [
        {"type": "text", "text": "The repository is ready."}
      ],
      "metrics": {
        "inputTokens": 15,
        "outputTokens": 6,
        "cacheReadTokens": 0,
        "cacheWriteTokens": 0
      }
    }
  ]
}"#,
        )
        .unwrap();

        let messages = parse_cline_cli_file(&path);

        assert_eq!(messages.len(), 2);
        assert_eq!(
            messages[0].dedup_key.as_deref(),
            Some("cline-cli:turns:assistant-tool-use")
        );
        assert_eq!(
            messages[1].dedup_key.as_deref(),
            Some("cline-cli:turns:assistant-final")
        );
        assert!(messages[0].is_turn_start);
        assert!(!messages[1].is_turn_start);
        assert_eq!(
            messages
                .iter()
                .filter(|message| message.is_turn_start)
                .count(),
            1
        );
    }

    #[test]
    fn test_parse_cline_cli_normalizes_cache_tokens_and_preserves_zero_cost() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("session.messages.json");
        fs::write(
            &path,
            r#"{
  "sessionId": "session-1",
  "messages": [
    {
      "id": "zero-cost",
      "role": "assistant",
      "metrics": {
        "inputTokens": 12,
        "outputTokens": 0,
        "cacheReadTokens": 5,
        "cacheWriteTokens": 2,
        "cost": 0
      }
    },
    {
      "id": "invalid-cost",
      "role": "assistant",
      "metrics": {
        "inputTokens": 1,
        "outputTokens": 2,
        "cost": "NaN"
      }
    }
  ]
}"#,
        )
        .unwrap();

        let messages = parse_cline_cli_file(&path);

        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].tokens.input, 5);
        assert_eq!(messages[0].tokens.cache_read, 5);
        assert_eq!(messages[0].tokens.cache_write, 2);
        assert_eq!(messages[0].cost, 0.0);
        assert_eq!(messages[0].cost_source, CostSource::ProviderReported);
        assert_eq!(messages[1].cost, 0.0);
        assert_eq!(messages[1].cost_source, CostSource::Unknown);
    }
}