seher-sdk 0.0.32

Seher SDK: agent resolution, rate-limit checks, and provider clients
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
use super::auth::OpencodeGoAuth;
use super::types::{OpencodeGoUsageSnapshot, OpencodeGoUsageSource, OpencodeGoUsageWindow};
use chrono::{DateTime, Duration, Utc};
use rusqlite::Connection;
use serde::Deserialize;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use thiserror::Error;

// Documented OpenCode Go plan caps: $12 / 5h, $30 / 7d, $60 / 30d (rolling).
const FIVE_HOUR_LIMIT_USD: f64 = 12.0;
const WEEKLY_LIMIT_USD: f64 = 30.0;
const MONTHLY_LIMIT_USD: f64 = 60.0;

// Env overrides for the per-window USD caps. OpenCode Go is tracked by summing
// the per-message `cost` recorded in the local OpenCode DB over rolling windows
// and comparing against the plan caps above — local-device tracking, not the
// hosted console. These overrides let the user adjust the caps if the plan
// changes, or set a window to `0` to disable it entirely.
const ENV_FIVE_HOUR_LIMIT: &str = "SEHER_OPENCODE_5H_LIMIT_USD";
const ENV_WEEKLY_LIMIT: &str = "SEHER_OPENCODE_WEEKLY_LIMIT_USD";
const ENV_MONTHLY_LIMIT: &str = "SEHER_OPENCODE_MONTHLY_LIMIT_USD";

struct WindowDef {
    entry_type: &'static str,
    window_seconds: i64,
    default_limit_usd: f64,
    env_var: &'static str,
}

const WINDOW_DEFS: [WindowDef; 3] = [
    WindowDef {
        entry_type: "five_hour_spend",
        window_seconds: 5 * 60 * 60,
        default_limit_usd: FIVE_HOUR_LIMIT_USD,
        env_var: ENV_FIVE_HOUR_LIMIT,
    },
    WindowDef {
        entry_type: "weekly_spend",
        window_seconds: 7 * 24 * 60 * 60,
        default_limit_usd: WEEKLY_LIMIT_USD,
        env_var: ENV_WEEKLY_LIMIT,
    },
    WindowDef {
        entry_type: "monthly_spend",
        window_seconds: 30 * 24 * 60 * 60,
        default_limit_usd: MONTHLY_LIMIT_USD,
        env_var: ENV_MONTHLY_LIMIT,
    },
];

/// Resolve the effective USD limit for a window: the value of `env_var` if it
/// parses as a finite `f64`, otherwise `default`. A parsed value of `0` (or
/// negative) disables the window (never limited).
fn resolve_limit(env_var: &str, default: f64) -> f64 {
    std::env::var(env_var)
        .ok()
        .and_then(|v| v.trim().parse::<f64>().ok())
        .filter(|v| v.is_finite())
        .unwrap_or(default)
}

fn window_specs() -> Vec<WindowSpec> {
    WINDOW_DEFS
        .iter()
        .map(|d| WindowSpec {
            entry_type: d.entry_type,
            window_seconds: d.window_seconds,
            limit_usd: resolve_limit(d.env_var, d.default_limit_usd),
        })
        .collect()
}

const LIMIT_EPSILON: f64 = 1e-9;

#[derive(Debug, Error)]
pub enum OpencodeGoUsageError {
    #[error("could not determine home directory for opencode.db")]
    HomeDirNotFound,

    #[error("failed to read OpenCode usage database: {0}")]
    Io(#[from] std::io::Error),

    #[error("failed to query OpenCode usage database: {0}")]
    Sql(#[from] rusqlite::Error),

    #[error("failed to parse OpenCode message row: {0}")]
    Parse(#[from] serde_json::Error),
}

#[derive(Debug, Clone, PartialEq)]
struct UsageRecord {
    completed_at: DateTime<Utc>,
    cost_usd: f64,
}

#[derive(Debug, Clone, Copy)]
struct WindowSpec {
    entry_type: &'static str,
    window_seconds: i64,
    limit_usd: f64,
}

#[derive(Debug, Deserialize)]
struct MessageRow {
    role: String,
    #[serde(rename = "providerID")]
    provider_id: Option<String>,
    cost: Option<f64>,
    time: Option<MessageTime>,
}

#[derive(Debug, Deserialize)]
struct MessageTime {
    completed: Option<i64>,
}

pub struct OpencodeGoUsageStore;

impl OpencodeGoUsageStore {
    /// # Errors
    ///
    /// Returns an error when the local `SQLite` history cannot be copied, read,
    /// or parsed.
    pub fn fetch_usage() -> Result<OpencodeGoUsageSnapshot, OpencodeGoUsageError> {
        Self::fetch_usage_with_paths_at(None, None, Utc::now())
    }

    /// # Errors
    ///
    /// Returns an error when the local `SQLite` history cannot be copied, read,
    /// or parsed.
    pub fn fetch_usage_from_path_at(
        db_path: &Path,
        now: DateTime<Utc>,
    ) -> Result<OpencodeGoUsageSnapshot, OpencodeGoUsageError> {
        Self::fetch_usage_with_paths_at(Some(db_path), None, now)
    }

    /// # Errors
    ///
    /// Returns an error when the local `SQLite` history cannot be copied, read,
    /// or parsed.
    pub fn fetch_usage_with_paths_at(
        db_path: Option<&Path>,
        auth_path: Option<&Path>,
        now: DateTime<Utc>,
    ) -> Result<OpencodeGoUsageSnapshot, OpencodeGoUsageError> {
        let credentials_available = auth_path
            .map_or_else(
                OpencodeGoAuth::read_api_key,
                OpencodeGoAuth::read_api_key_from,
            )
            .is_ok();
        let db_path = match db_path {
            Some(path) => path.to_path_buf(),
            None => Self::default_db_path()?,
        };
        let records = if db_path.exists() {
            Self::load_records(&db_path)?
        } else {
            Vec::new()
        };

        Ok(Self::snapshot_from_records(
            now,
            &records,
            credentials_available,
        ))
    }

    fn default_db_path() -> Result<PathBuf, OpencodeGoUsageError> {
        let home = dirs::home_dir().ok_or(OpencodeGoUsageError::HomeDirNotFound)?;
        Ok(home.join(".local/share/opencode/opencode.db"))
    }

    fn load_records(db_path: &Path) -> Result<Vec<UsageRecord>, OpencodeGoUsageError> {
        let (temp_dir, temp_db_path) = Self::copy_sqlite_database(db_path)?;
        let conn = Connection::open(&temp_db_path)?;
        let records = Self::query_records(&conn)?;
        drop(conn);
        drop(temp_dir);
        Ok(records)
    }

    fn copy_sqlite_database(db_path: &Path) -> Result<(TempDir, PathBuf), OpencodeGoUsageError> {
        let temp_dir = tempfile::tempdir()?;
        let file_name = db_path
            .file_name()
            .ok_or_else(|| std::io::Error::other("opencode.db path has no file name"))?;
        let temp_db_path = temp_dir.path().join(file_name);
        std::fs::copy(db_path, &temp_db_path)?;

        for suffix in ["-wal", "-shm"] {
            let sidecar_name = format!("{}{}", file_name.to_string_lossy(), suffix);
            let src = db_path.with_file_name(&sidecar_name);
            if src.exists() {
                let dst = temp_dir.path().join(sidecar_name);
                std::fs::copy(src, dst)?;
            }
        }

        Ok((temp_dir, temp_db_path))
    }

    fn query_records(conn: &Connection) -> Result<Vec<UsageRecord>, OpencodeGoUsageError> {
        let mut stmt = match conn.prepare("SELECT data FROM message") {
            Ok(stmt) => stmt,
            Err(rusqlite::Error::SqliteFailure(_, Some(message)))
                if message.contains("no such table: message") =>
            {
                return Ok(Vec::new());
            }
            Err(err) => return Err(err.into()),
        };
        let rows = stmt.query_map([], |row| row.get::<_, String>(0))?;
        let mut records = Vec::new();

        for row in rows {
            let row = serde_json::from_str::<MessageRow>(&row?)?;
            if row.role != "assistant" || row.provider_id.as_deref() != Some("opencode-go") {
                continue;
            }

            let Some(cost_usd) = row.cost else {
                continue;
            };
            if cost_usd <= 0.0 {
                continue;
            }

            let completed = row.time.and_then(|time| time.completed);
            let Some(completed_at) = completed.and_then(DateTime::from_timestamp_millis) else {
                continue;
            };

            records.push(UsageRecord {
                completed_at,
                cost_usd,
            });
        }

        records.sort_by_key(|record| record.completed_at);
        Ok(records)
    }

    fn snapshot_from_records(
        now: DateTime<Utc>,
        records: &[UsageRecord],
        credentials_available: bool,
    ) -> OpencodeGoUsageSnapshot {
        let windows = window_specs()
            .into_iter()
            .map(|spec| Self::window_from_records(now, records, spec))
            .collect();

        OpencodeGoUsageSnapshot {
            source: OpencodeGoUsageSource::LocalDatabase,
            credentials_available,
            total_messages: records.len(),
            windows,
        }
    }

    fn window_from_records(
        now: DateTime<Utc>,
        records: &[UsageRecord],
        spec: WindowSpec,
    ) -> OpencodeGoUsageWindow {
        let duration = Duration::seconds(spec.window_seconds);
        let window_start = now - duration;
        let active_records: Vec<&UsageRecord> = records
            .iter()
            .filter(|record| record.completed_at >= window_start)
            .collect();
        let spent_usd = active_records
            .iter()
            .map(|record| record.cost_usd)
            .sum::<f64>();

        // A non-positive limit means the window is disabled (never limited).
        let limited = spec.limit_usd > 0.0 && spent_usd + LIMIT_EPSILON >= spec.limit_usd;
        let resets_at = if limited {
            let mut remaining = spent_usd;
            active_records.iter().find_map(|record| {
                remaining -= record.cost_usd;
                if remaining + LIMIT_EPSILON < spec.limit_usd {
                    Some(record.completed_at + duration)
                } else {
                    None
                }
            })
        } else {
            None
        };

        OpencodeGoUsageWindow {
            entry_type: spec.entry_type,
            spent_usd,
            limit_usd: spec.limit_usd,
            resets_at,
        }
    }
}

#[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    type TestResult = Result<(), Box<dyn std::error::Error>>;

    fn usage_record(ts: i64, cost_usd: f64) -> UsageRecord {
        UsageRecord {
            completed_at: Utc.timestamp_millis_opt(ts).single().unwrap(),
            cost_usd,
        }
    }

    #[test]
    fn snapshot_is_empty_when_no_messages_exist() {
        let now = Utc.timestamp_millis_opt(1_000_000).single().unwrap();
        let snapshot = OpencodeGoUsageStore::snapshot_from_records(now, &[], false);

        assert_eq!(snapshot.total_messages, 0);
        assert_eq!(snapshot.windows.len(), 3);
        assert!(snapshot.windows.iter().all(|window| !window.is_limited()));
        assert!(
            snapshot
                .windows
                .iter()
                .all(|window| window.spent_usd == 0.0)
        );
    }

    #[test]
    fn computes_five_hour_reset_from_oldest_blocking_message() {
        let now = Utc
            .timestamp_millis_opt(20 * 60 * 60 * 1000)
            .single()
            .unwrap();
        let records = vec![
            usage_record(15 * 60 * 60 * 1000, 4.0),
            usage_record(16 * 60 * 60 * 1000, 5.0),
            usage_record(19 * 60 * 60 * 1000, 4.0),
        ];

        let snapshot = OpencodeGoUsageStore::snapshot_from_records(now, &records, true);
        let five_hour = snapshot
            .windows
            .iter()
            .find(|window| window.entry_type == "five_hour_spend")
            .unwrap();

        assert!(five_hour.is_limited());
        assert_eq!(
            five_hour.resets_at,
            Some(records[0].completed_at + Duration::hours(5))
        );
        assert!((five_hour.spent_usd - 13.0).abs() < LIMIT_EPSILON);
    }

    #[test]
    fn computes_longer_windows_independently() {
        let now = Utc
            .timestamp_millis_opt(40 * 24 * 60 * 60 * 1000)
            .single()
            .unwrap();
        let records = vec![
            usage_record(10 * 24 * 60 * 60 * 1000, 31.0),
            usage_record(34 * 24 * 60 * 60 * 1000, 11.0),
            usage_record(35 * 24 * 60 * 60 * 1000, 10.0),
            usage_record(39 * 24 * 60 * 60 * 1000, 10.0),
        ];

        let snapshot = OpencodeGoUsageStore::snapshot_from_records(now, &records, true);
        let weekly = snapshot
            .windows
            .iter()
            .find(|window| window.entry_type == "weekly_spend")
            .unwrap();
        let monthly = snapshot
            .windows
            .iter()
            .find(|window| window.entry_type == "monthly_spend")
            .unwrap();

        assert!(weekly.is_limited());
        assert_eq!(
            weekly.resets_at,
            Some(records[1].completed_at + Duration::days(7))
        );
        assert!(monthly.is_limited());
        assert_eq!(
            monthly.resets_at,
            Some(records[0].completed_at + Duration::days(30))
        );
    }

    #[test]
    fn disabled_window_is_never_limited() {
        // limit_usd <= 0 means the window is disabled, even if spend is huge.
        let now = Utc
            .timestamp_millis_opt(10 * 60 * 60 * 1000)
            .single()
            .unwrap();
        let records = vec![usage_record(9 * 60 * 60 * 1000, 999.0)];
        let spec = WindowSpec {
            entry_type: "five_hour_spend",
            window_seconds: 5 * 60 * 60,
            limit_usd: 0.0,
        };
        let w = OpencodeGoUsageStore::window_from_records(now, &records, spec);
        assert!(!w.is_limited());
        assert_eq!(w.resets_at, None);
        assert!(w.utilization().abs() < f64::EPSILON);
    }

    #[test]
    fn resolve_limit_uses_default_when_env_absent() {
        // Use a unique var name unlikely to be set in the environment.
        assert!(
            (resolve_limit("SEHER_OPENCODE_TEST_UNSET_LIMIT_XYZ", 42.0) - 42.0).abs()
                < f64::EPSILON
        );
    }

    #[test]
    fn limited_window_above_threshold() {
        let now = Utc
            .timestamp_millis_opt(10 * 60 * 60 * 1000)
            .single()
            .unwrap();
        let records = vec![usage_record(9 * 60 * 60 * 1000, 13.0)];
        let spec = WindowSpec {
            entry_type: "five_hour_spend",
            window_seconds: 5 * 60 * 60,
            limit_usd: 12.0,
        };
        let w = OpencodeGoUsageStore::window_from_records(now, &records, spec);
        assert!(w.is_limited());
        assert!(w.resets_at.is_some());
    }

    #[test]
    fn reads_only_opencode_go_assistant_messages_from_sqlite() -> TestResult {
        let tmp = tempfile::NamedTempFile::new()?;
        let conn = Connection::open(tmp.path())?;
        conn.execute("CREATE TABLE message (data TEXT NOT NULL)", [])?;
        conn.execute(
            "INSERT INTO message (data) VALUES (?1)",
            [r#"{"role":"assistant","providerID":"opencode-go","cost":1.5,"time":{"completed":3600000}}"#],
        )?;
        conn.execute(
            "INSERT INTO message (data) VALUES (?1)",
            [r#"{"role":"assistant","providerID":"opencode","cost":9.0,"time":{"completed":3600000}}"#],
        )?;
        conn.execute(
            "INSERT INTO message (data) VALUES (?1)",
            [r#"{"role":"user","providerID":"opencode-go","cost":9.0,"time":{"completed":3600000}}"#],
        )?;
        drop(conn);

        let snapshot = OpencodeGoUsageStore::fetch_usage_from_path_at(
            tmp.path(),
            Utc.timestamp_millis_opt(10 * 60 * 60 * 1000)
                .single()
                .unwrap(),
        )?;

        assert_eq!(snapshot.total_messages, 1);
        let five_hour = snapshot
            .windows
            .iter()
            .find(|window| window.entry_type == "five_hour_spend")
            .ok_or("missing five_hour window")?;
        assert!((five_hour.spent_usd - 0.0).abs() < LIMIT_EPSILON);
        let weekly = snapshot
            .windows
            .iter()
            .find(|window| window.entry_type == "weekly_spend")
            .ok_or("missing weekly window")?;
        assert!((weekly.spent_usd - 1.5).abs() < LIMIT_EPSILON);
        Ok(())
    }
}