acp-ws-bridge 0.3.3

WebSocket bridge between GitHub Copilot CLI (ACP) and remote clients
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
//! Read-only access to Copilot CLI session history from the configured Copilot data directory.

use std::collections::HashMap;
use std::path::Path;

use rusqlite::Connection;
use serde::Serialize;

fn open_session_store(copilot_dir: &Path) -> anyhow::Result<Option<Connection>> {
    let db_path = crate::paths::session_store_path(copilot_dir);
    if !db_path.exists() {
        return Ok(None);
    }

    Ok(Some(Connection::open_with_flags(
        db_path,
        rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
    )?))
}

#[derive(Debug, Serialize)]
pub struct HistorySession {
    pub id: String,
    pub cwd: Option<String>,
    pub repository: Option<String>,
    pub branch: Option<String>,
    pub summary: Option<String>,
    pub preview: Option<String>,
    pub created_at: Option<String>,
    pub updated_at: Option<String>,
    pub turn_count: i64,
}

#[derive(Debug, Serialize)]
pub struct HistoryTurn {
    pub turn_index: i64,
    pub user_message: Option<String>,
    pub assistant_response: Option<String>,
    pub timestamp: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct DateCount {
    pub date: String,
    pub count: i64,
}

#[derive(Debug, Serialize)]
pub struct MonthCount {
    pub month: String,
    pub count: i64,
}

#[derive(Debug, Serialize)]
pub struct TopRepo {
    pub name: String,
    pub session_count: i64,
    pub turn_count: i64,
}

#[derive(Debug, Serialize)]
pub struct NameCount {
    pub name: String,
    pub count: i64,
}

#[derive(Debug, Serialize)]
pub struct HourCount {
    pub hour: i64,
    pub count: i64,
}

#[derive(Debug, Serialize)]
pub struct RecentSession {
    pub id: String,
    pub summary: Option<String>,
    pub created_at: Option<String>,
    pub repository: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct HistoryStats {
    pub total_sessions: i64,
    pub total_turns: i64,
    pub total_repositories: i64,
    pub total_files_edited: i64,
    pub sessions_today: i64,
    pub sessions_this_week: i64,
    pub sessions_this_month: i64,
    pub turns_today: i64,
    pub turns_this_week: i64,
    pub turns_this_month: i64,
    pub sessions_by_day: Vec<DateCount>,
    pub sessions_by_month: Vec<MonthCount>,
    pub turns_by_day: Vec<DateCount>,
    pub top_repositories: Vec<TopRepo>,
    pub top_branches: Vec<NameCount>,
    pub recent_sessions: Vec<RecentSession>,
    pub average_turns_per_session: f64,
    pub average_session_duration: String,
    pub tools_used: Vec<NameCount>,
    pub active_hours: Vec<HourCount>,
    pub earliest_session: Option<String>,
    pub latest_session: Option<String>,
}

/// List all sessions with turn counts, ordered by most recent first.
#[allow(dead_code)]
pub fn list_sessions() -> anyhow::Result<Vec<HistorySession>> {
    let copilot_dir = crate::paths::default_copilot_dir()?;
    list_sessions_from(&copilot_dir)
}

/// List all sessions with turn counts from the configured Copilot data directory.
pub fn list_sessions_from(copilot_dir: &Path) -> anyhow::Result<Vec<HistorySession>> {
    let Some(conn) = open_session_store(copilot_dir)? else {
        return Ok(vec![]);
    };
    let mut stmt = conn.prepare(
        "SELECT s.id, s.cwd, s.repository, s.branch, s.summary, s.created_at, s.updated_at,
                COALESCE((SELECT COUNT(*) FROM turns t WHERE t.session_id = s.id), 0) as turn_count,
                (SELECT substr(t2.user_message, 1, 100) FROM turns t2 WHERE t2.session_id = s.id AND t2.user_message IS NOT NULL AND t2.user_message != '' ORDER BY t2.turn_index LIMIT 1) as preview
         FROM sessions s
         ORDER BY s.created_at DESC",
    )?;
    let sessions = stmt
        .query_map([], |row| {
            Ok(HistorySession {
                id: row.get(0)?,
                cwd: row.get(1)?,
                repository: row.get(2)?,
                branch: row.get(3)?,
                summary: row.get(4)?,
                preview: row.get(8)?,
                created_at: row.get(5)?,
                updated_at: row.get(6)?,
                turn_count: row.get(7)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(sessions)
}

/// Get turns for a specific session, ordered by turn index.
#[allow(dead_code)]
pub fn get_session_turns(session_id: &str) -> anyhow::Result<Vec<HistoryTurn>> {
    let copilot_dir = crate::paths::default_copilot_dir()?;
    get_session_turns_from(&copilot_dir, session_id)
}

/// Get turns for a specific session from the configured Copilot data directory.
pub fn get_session_turns_from(
    copilot_dir: &Path,
    session_id: &str,
) -> anyhow::Result<Vec<HistoryTurn>> {
    let Some(conn) = open_session_store(copilot_dir)? else {
        return Ok(vec![]);
    };
    let mut stmt = conn.prepare(
        "SELECT turn_index, user_message, assistant_response, timestamp
         FROM turns WHERE session_id = ? ORDER BY turn_index",
    )?;
    let turns = stmt
        .query_map([session_id], |row| {
            Ok(HistoryTurn {
                turn_index: row.get(0)?,
                user_message: row.get(1)?,
                assistant_response: row.get(2)?,
                timestamp: row.get(3)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
    Ok(turns)
}

/// Get aggregate statistics across all sessions.
#[allow(dead_code)]
pub fn get_history_stats() -> anyhow::Result<HistoryStats> {
    let copilot_dir = crate::paths::default_copilot_dir()?;
    get_history_stats_from(&copilot_dir)
}

/// Get aggregate statistics across all sessions from the configured Copilot data directory.
pub fn get_history_stats_from(copilot_dir: &Path) -> anyhow::Result<HistoryStats> {
    let Some(conn) = open_session_store(copilot_dir)? else {
        anyhow::bail!("history database unavailable");
    };

    let total_sessions: i64 = conn.query_row("SELECT COUNT(*) FROM sessions", [], |r| r.get(0))?;
    let total_turns: i64 = conn.query_row("SELECT COUNT(*) FROM turns", [], |r| r.get(0))?;
    let total_repositories: i64 = conn.query_row(
        "SELECT COUNT(DISTINCT repository) FROM sessions \
         WHERE repository IS NOT NULL AND repository != ''",
        [],
        |r| r.get(0),
    )?;
    let total_files_edited: i64 = conn
        .query_row(
            "SELECT COUNT(DISTINCT file_path) FROM session_files",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);

    // Time-period counts
    let sessions_today: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sessions WHERE date(created_at) = date('now')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let sessions_this_week: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sessions WHERE created_at >= date('now', '-7 days')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let sessions_this_month: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM sessions WHERE created_at >= date('now', '-30 days')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let turns_today: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM turns WHERE date(timestamp) = date('now')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let turns_this_week: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM turns WHERE timestamp >= date('now', '-7 days')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);
    let turns_this_month: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM turns WHERE timestamp >= date('now', '-30 days')",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0);

    let sessions_by_day = query_vec(
        &conn,
        "SELECT date(created_at) as d, COUNT(*) FROM sessions \
         WHERE created_at >= date('now', '-30 days') AND created_at IS NOT NULL \
         GROUP BY d ORDER BY d",
        |row| {
            Ok(DateCount {
                date: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );
    let sessions_by_month = query_vec(
        &conn,
        "SELECT strftime('%Y-%m', created_at) as m, COUNT(*) FROM sessions \
         WHERE created_at IS NOT NULL GROUP BY m ORDER BY m",
        |row| {
            Ok(MonthCount {
                month: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );
    let turns_by_day = query_vec(
        &conn,
        "SELECT date(timestamp) as d, COUNT(*) FROM turns \
         WHERE timestamp >= date('now', '-30 days') AND timestamp IS NOT NULL \
         GROUP BY d ORDER BY d",
        |row| {
            Ok(DateCount {
                date: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );
    let top_repositories = query_vec(
        &conn,
        "SELECT s.repository, COUNT(DISTINCT s.id), \
                COALESCE((SELECT COUNT(*) FROM turns t WHERE t.session_id IN \
                    (SELECT id FROM sessions WHERE repository = s.repository)), 0) \
         FROM sessions s WHERE s.repository IS NOT NULL AND s.repository != '' \
         GROUP BY s.repository ORDER BY COUNT(DISTINCT s.id) DESC LIMIT 10",
        |row| {
            Ok(TopRepo {
                name: row.get(0)?,
                session_count: row.get(1)?,
                turn_count: row.get(2)?,
            })
        },
    );
    let top_branches = query_vec(
        &conn,
        "SELECT branch, COUNT(*) as cnt FROM sessions \
         WHERE branch IS NOT NULL AND branch != '' \
         GROUP BY branch ORDER BY cnt DESC LIMIT 10",
        |row| {
            Ok(NameCount {
                name: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );
    let recent_sessions = query_vec(
        &conn,
        "SELECT id, summary, created_at, repository FROM sessions \
         ORDER BY created_at DESC LIMIT 10",
        |row| {
            Ok(RecentSession {
                id: row.get(0)?,
                summary: row.get(1)?,
                created_at: row.get(2)?,
                repository: row.get(3)?,
            })
        },
    );

    let average_turns_per_session = if total_sessions > 0 {
        (total_turns as f64) / (total_sessions as f64)
    } else {
        0.0
    };
    let avg_minutes: f64 = conn
        .query_row(
            "SELECT COALESCE(AVG((julianday(updated_at) - julianday(created_at)) * 24.0 * 60.0), 0) \
             FROM sessions WHERE updated_at IS NOT NULL AND created_at IS NOT NULL \
             AND updated_at != created_at",
            [],
            |r| r.get(0),
        )
        .unwrap_or(0.0);
    let average_session_duration = format_duration_minutes(avg_minutes);

    let tools_used = query_vec(
        &conn,
        "SELECT tool_name, COUNT(*) as cnt FROM session_files \
         WHERE tool_name IS NOT NULL AND tool_name != '' \
         GROUP BY tool_name ORDER BY cnt DESC",
        |row| {
            Ok(NameCount {
                name: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );
    let active_hours = query_vec(
        &conn,
        "SELECT CAST(strftime('%H', created_at) AS INTEGER) as h, COUNT(*) \
         FROM sessions WHERE created_at IS NOT NULL GROUP BY h ORDER BY h",
        |row| {
            Ok(HourCount {
                hour: row.get(0)?,
                count: row.get(1)?,
            })
        },
    );

    let earliest: Option<String> = conn
        .query_row("SELECT MIN(created_at) FROM sessions", [], |r| r.get(0))
        .ok();
    let latest: Option<String> = conn
        .query_row("SELECT MAX(created_at) FROM sessions", [], |r| r.get(0))
        .ok();

    Ok(HistoryStats {
        total_sessions,
        total_turns,
        total_repositories,
        total_files_edited,
        sessions_today,
        sessions_this_week,
        sessions_this_month,
        turns_today,
        turns_this_week,
        turns_this_month,
        sessions_by_day,
        sessions_by_month,
        turns_by_day,
        top_repositories,
        top_branches,
        recent_sessions,
        average_turns_per_session,
        average_session_duration,
        tools_used,
        active_hours,
        earliest_session: earliest,
        latest_session: latest,
    })
}

/// Run a query and collect results into a Vec, returning empty on error.
fn query_vec<T>(
    conn: &Connection,
    sql: &str,
    mapper: impl FnMut(&rusqlite::Row<'_>) -> rusqlite::Result<T>,
) -> Vec<T> {
    conn.prepare(sql)
        .and_then(|mut stmt| stmt.query_map([], mapper)?.collect::<Result<Vec<_>, _>>())
        .unwrap_or_default()
}

// ---- Copilot usage stats (events.jsonl + session-store.db) ----

#[derive(Debug, Serialize)]
pub struct ModelUsage {
    pub model: String,
    pub count: u64,
}

#[derive(Debug, Serialize)]
pub struct MonthUsage {
    pub month: String,
    pub sessions: i64,
    pub turns: i64,
}

#[derive(Debug, Serialize)]
pub struct ModelChange {
    pub model: String,
    pub timestamp: String,
}

#[derive(Debug, Serialize)]
pub struct CopilotUsageStats {
    #[serde(rename = "totalSessions")]
    pub total_sessions: i64,
    #[serde(rename = "totalTurns")]
    pub total_turns: i64,
    #[serde(rename = "totalFilesEdited")]
    pub total_files_edited: i64,
    #[serde(rename = "modelUsage")]
    pub model_usage: Vec<ModelUsage>,
    #[serde(rename = "sessionsByMonth")]
    pub sessions_by_month: Vec<MonthUsage>,
    #[serde(rename = "recentModelChanges")]
    pub recent_model_changes: Vec<ModelChange>,
    #[serde(rename = "totalToolExecutions")]
    pub total_tool_executions: u64,
    #[serde(rename = "eventTypeCounts")]
    pub event_type_counts: HashMap<String, u64>,
}

/// Aggregate usage statistics from the pre-built stats cache only.
/// session-store.db data is merged into the cache during refresh().
pub fn get_copilot_usage(
    cache: &crate::stats_cache::StatsCache,
) -> anyhow::Result<CopilotUsageStats> {
    let cached = cache.get_stats();

    Ok(CopilotUsageStats {
        total_sessions: cached.total_sessions,
        total_turns: cached.total_turns,
        total_files_edited: cached.total_files_edited,
        model_usage: cached
            .model_usage
            .into_iter()
            .map(|(m, c)| ModelUsage { model: m, count: c })
            .collect(),
        sessions_by_month: cached
            .sessions_by_month
            .into_iter()
            .map(|(m, s, t)| MonthUsage {
                month: m,
                sessions: s,
                turns: t,
            })
            .collect(),
        recent_model_changes: cached
            .recent_model_changes
            .into_iter()
            .map(|(m, t)| ModelChange {
                model: m,
                timestamp: t,
            })
            .collect(),
        total_tool_executions: cached.total_tool_executions,
        event_type_counts: HashMap::new(),
    })
}

/// Format minutes as a human-readable duration string.
fn format_duration_minutes(minutes: f64) -> String {
    if minutes < 1.0 {
        return "< 1m".to_string();
    }
    let total = minutes as u64;
    let h = total / 60;
    let m = total % 60;
    if h > 0 {
        format!("{}h {}m", h, m)
    } else {
        format!("{}m", m)
    }
}