difflore-core 0.2.0

Core library for the difflore CLI — rule store, retrieval, MCP server, hooks, cloud sync. Not intended for direct use; depend on `difflore-cli` instead.
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
//! Provider persistence and local provider auth probes.

use uuid::Uuid;

use crate::domain::models::{
    ProviderAddInput, ProviderRecord, ProviderRemoveInput, ProviderSetActiveInput,
    ProviderUpdateInput,
};
use crate::error::CoreError;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CheckAuthInput {
    pub engine: String,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CheckAuthResult {
    pub credential_detected: bool,
    pub verified: bool,
    pub method: String,
    pub detail: String,
}

#[derive(sqlx::FromRow)]
struct ProviderRow {
    id: String,
    name: String,
    base_url: String,
    api_key: String,
    model_mapping: String,
    is_active: i64,
    created_at: String,
    updated_at: String,
}

impl ProviderRow {
    fn decrypt_api_key(&self) -> String {
        match crate::infra::crypto::decrypt_secret(&self.api_key) {
            Ok(plaintext) => plaintext,
            Err(e) => {
                if crate::infra::env::debug_providers() {
                    eprintln!(
                        "[providers] failed to decrypt API key for provider {}: {e}",
                        self.id
                    );
                }
                String::new()
            }
        }
    }

    fn parse_model_mapping(&self) -> std::collections::HashMap<String, String> {
        match serde_json::from_str(&self.model_mapping) {
            Ok(mapping) => mapping,
            Err(e) => {
                if crate::infra::env::debug_providers() {
                    eprintln!(
                        "[providers] failed to parse model_mapping for provider {}: {e}",
                        self.id
                    );
                }
                std::collections::HashMap::new()
            }
        }
    }

    fn into_masked(self) -> ProviderRecord {
        let decrypted = self.decrypt_api_key();
        let model_mapping = self.parse_model_mapping();
        ProviderRecord {
            id: self.id,
            name: self.name,
            base_url: self.base_url,
            api_key: Some(mask_api_key(&decrypted)),
            model_mapping,
            is_active: self.is_active != 0,
            created_at: self.created_at,
            updated_at: self.updated_at,
        }
    }

    fn into_internal(self) -> ProviderRecord {
        let decrypted = self.decrypt_api_key();
        let model_mapping = self.parse_model_mapping();
        ProviderRecord {
            id: self.id,
            name: self.name,
            base_url: self.base_url,
            api_key: Some(decrypted),
            model_mapping,
            is_active: self.is_active != 0,
            created_at: self.created_at,
            updated_at: self.updated_at,
        }
    }
}

fn mask_api_key(key: &str) -> String {
    // Count chars, not bytes. Real API keys are ASCII so byte vs char
    // length usually agrees, but a user-pasted key with stray emoji
    // or a non-ASCII paste artefact would otherwise panic on the
    // `&key[..3]` byte slice when 3 falls inside a multi-byte rune.
    let n = key.chars().count();
    if n <= 8 {
        return "****".to_owned();
    }
    let head: String = key.chars().take(3).collect();
    let tail: String = key.chars().skip(n - 3).collect();
    format!("{head}***{tail}")
}

pub async fn list(db: &sqlx::SqlitePool) -> crate::Result<Vec<ProviderRecord>> {
    let rows = sqlx::query_as!(
        ProviderRow,
        "SELECT id, name, base_url, api_key, model_mapping, is_active, created_at, updated_at FROM providers ORDER BY created_at DESC"
    )
    .fetch_all(db)
    .await?;
    Ok(rows.into_iter().map(ProviderRow::into_masked).collect())
}

pub async fn get(
    db: &sqlx::SqlitePool,
    input: ProviderRemoveInput,
) -> crate::Result<Option<ProviderRecord>> {
    let row = sqlx::query_as!(
        ProviderRow,
        "SELECT id, name, base_url, api_key, model_mapping, is_active, created_at, updated_at FROM providers WHERE id = ?1",
        input.id
    )
    .fetch_optional(db)
    .await?;
    Ok(row.map(ProviderRow::into_masked))
}

pub async fn add(db: &sqlx::SqlitePool, input: ProviderAddInput) -> crate::Result<ProviderRecord> {
    let id = format!("provider-{}", Uuid::new_v4());
    let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();
    let mapping_json = serde_json::to_string(&input.model_mapping)?;
    // BYOK has been removed from the local CLI. Provider rows now only
    // describe an agent-cli sentinel (`agent-cli://<tool>`); the column
    // stays for back-compat with older DBs but is always written empty.
    let encrypted_key = crate::infra::crypto::encrypt_secret("")?;

    sqlx::query!(
        "INSERT INTO providers (id, name, base_url, api_key, model_mapping, is_active, created_at, updated_at)
         VALUES (?1, ?2, ?3, ?4, ?5, 0, ?6, ?6)",
        id,
        input.name,
        input.base_url,
        encrypted_key,
        mapping_json,
        now
    )
    .execute(db)
    .await?;

    Ok(ProviderRecord {
        id,
        name: input.name,
        base_url: input.base_url,
        api_key: None,
        model_mapping: input.model_mapping,
        is_active: false,
        created_at: now.clone(),
        updated_at: now,
    })
}

pub async fn update(
    db: &sqlx::SqlitePool,
    input: ProviderUpdateInput,
) -> crate::Result<ProviderRecord> {
    let now = chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string();

    let row = sqlx::query_as!(
        ProviderRow,
        "SELECT id, name, base_url, api_key, model_mapping, is_active, created_at, updated_at FROM providers WHERE id = ?1",
        input.id
    )
    .fetch_optional(db)
    .await?
    .ok_or_else(|| CoreError::NotFound(format!(
        "provider '{}' not found. List current providers with `difflore providers list`.",
        input.id
    )))?;

    let mut provider = row.into_internal();

    if let Some(name) = input.name {
        provider.name = name;
    }
    if let Some(base_url) = input.base_url {
        provider.base_url = base_url;
    }
    if let Some(mm) = input.model_mapping {
        provider.model_mapping = mm;
    }
    provider.updated_at = now;

    let mapping_json = serde_json::to_string(&provider.model_mapping)?;
    // BYOK has been removed; the api_key column is left in place for
    // older schemas but always overwritten with an encrypted empty string.
    let encrypted_secret = crate::infra::crypto::encrypt_secret("")?;

    let result = sqlx::query!(
        "UPDATE providers SET name=?1, base_url=?2, api_key=?3, model_mapping=?4, updated_at=?5 WHERE id=?6",
        provider.name,
        provider.base_url,
        encrypted_secret,
        mapping_json,
        provider.updated_at,
        provider.id
    )
    .execute(db)
    .await?;
    if result.rows_affected() == 0 {
        return Err(CoreError::NotFound(format!(
            "provider '{}' not found; cannot update. List current providers with `difflore providers list`.",
            provider.id
        )));
    }

    Ok(ProviderRecord {
        id: provider.id,
        name: provider.name,
        base_url: provider.base_url,
        api_key: None,
        model_mapping: provider.model_mapping,
        is_active: provider.is_active,
        created_at: provider.created_at,
        updated_at: provider.updated_at,
    })
}

pub async fn remove(db: &sqlx::SqlitePool, input: ProviderRemoveInput) -> crate::Result<()> {
    let result = sqlx::query!("DELETE FROM providers WHERE id = ?1", input.id)
        .execute(db)
        .await?;
    if result.rows_affected() == 0 {
        return Err(CoreError::NotFound(format!(
            "provider '{}' not found. List current providers with `difflore providers list`.",
            input.id
        )));
    }
    Ok(())
}

pub async fn set_active(db: &sqlx::SqlitePool, input: ProviderSetActiveInput) -> crate::Result<()> {
    let mut tx = db.begin().await?;
    if !input.is_active {
        let result = sqlx::query("UPDATE providers SET is_active = 0 WHERE id = ?1")
            .bind(&input.id)
            .execute(&mut *tx)
            .await?;
        if result.rows_affected() == 0 {
            return Err(CoreError::NotFound(format!(
                "provider '{}' not found. List current providers with `difflore providers list`.",
                input.id
            )));
        }
        tx.commit().await?;
        return Ok(());
    }

    sqlx::query!("UPDATE providers SET is_active = 0")
        .execute(&mut *tx)
        .await?;
    let result = sqlx::query!("UPDATE providers SET is_active = 1 WHERE id = ?1", input.id)
        .execute(&mut *tx)
        .await?;
    if result.rows_affected() == 0 {
        // Match the actionable message style used by `remove`: name
        // the bad id and tell the user where to look up the right
        // ones, instead of bare "provider".
        return Err(CoreError::NotFound(format!(
            "provider '{}' not found. List current providers with `difflore providers list`.",
            input.id
        )));
    }
    tx.commit().await?;
    Ok(())
}

pub async fn check_auth(input: CheckAuthInput) -> crate::Result<CheckAuthResult> {
    let home = dirs::home_dir()
        .ok_or_else(|| CoreError::Internal("cannot resolve home directory".into()))?;

    let (detected, method, detail) = match input.engine.as_str() {
        "claude" => {
            let path = home.join(".claude").join(".credentials.json");
            let found = path.exists();
            (
                found,
                "config_file".to_owned(),
                if found {
                    "Credentials file detected".to_owned()
                } else {
                    "No credentials file detected".to_owned()
                },
            )
        }
        "codex" => {
            let found = crate::infra::env::var(crate::infra::env::OPENAI_API_KEY).is_some();
            (
                found,
                "env_var".to_owned(),
                if found {
                    "OPENAI_API_KEY environment variable detected".to_owned()
                } else {
                    "OPENAI_API_KEY not found in environment".to_owned()
                },
            )
        }
        "gemini" => {
            let path = home.join(".gemini").join("credentials.json");
            let found = path.exists();
            (
                found,
                "config_file".to_owned(),
                if found {
                    "Credentials file detected".to_owned()
                } else {
                    "No credentials file detected".to_owned()
                },
            )
        }
        "cursor" => {
            let path = home.join(".cursor");
            let found = path.exists();
            (
                found,
                "cli_config".to_owned(),
                if found {
                    "Config directory detected".to_owned()
                } else {
                    "Config directory not found".to_owned()
                },
            )
        }
        other => (
            false,
            "unknown".to_owned(),
            format!("unsupported engine: {other}"),
        ),
    };

    Ok(CheckAuthResult {
        credential_detected: detected,
        verified: false,
        method,
        detail,
    })
}

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

    #[test]
    fn mask_api_key_table() {
        let cases: &[(&str, &str)] = &[
            ("", "****"),
            ("short", "****"),
            ("12345678", "****"),
            ("sk-ant-1234567890abcd", "sk-***bcd"),
            ("abcdefghijk", "abc***ijk"),
        ];
        for (input, expected) in cases {
            assert_eq!(mask_api_key(input), *expected, "input: {input}");
        }
    }

    #[tokio::test]
    async fn check_auth_unknown_engine_reports_unsupported() {
        let res = check_auth(CheckAuthInput {
            engine: "bogus-engine".into(),
        })
        .await
        .expect("check_auth should not error for unknown engine");
        assert!(!res.credential_detected);
        assert!(!res.verified);
        assert_eq!(res.method, "unknown");
        assert!(res.detail.contains("unsupported"));
    }

    #[tokio::test]
    async fn check_auth_codex_method_is_env_var() {
        // Just validate the method/shape — don't mutate env to avoid races with
        // other parallel tests.
        let res = check_auth(CheckAuthInput {
            engine: "codex".into(),
        })
        .await
        .unwrap();
        assert_eq!(res.method, "env_var");
        assert!(!res.verified);
    }

    #[tokio::test]
    async fn check_auth_claude_uses_config_file_method() {
        let res = check_auth(CheckAuthInput {
            engine: "claude".into(),
        })
        .await
        .unwrap();
        assert_eq!(res.method, "config_file");
        assert!(!res.verified);
    }
}