rho-coding-agent 0.15.0

A lightweight agent harness inspired by Pi
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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use std::{fs, path::PathBuf};

#[cfg(test)]
use std::{
    cell::RefCell,
    time::{SystemTime, UNIX_EPOCH},
};

use reqwest::{StatusCode, Url};
use rusqlite::{params, Connection};
use serde::Deserialize;
use serde_json::Value;

use crate::{
    auth::github_copilot_token::{
        auth_material_with_store, force_refresh_auth_material_with_store,
        GitHubCopilotAuthMaterial, GitHubCopilotAuthSource,
    },
    credentials::{load_provider_api_key, CredentialStore},
    model::{
        registry::{self, missing_credential_error, ProviderAuthKind, ProviderModelRefreshKind},
        ModelError,
    },
};

#[cfg(not(test))]
use crate::paths;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProviderModel {
    pub provider: String,
    pub model: String,
    pub display_name: String,
    pub max_output_tokens: Option<u64>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProviderModelRefresh {
    pub provider: String,
    pub models: Vec<ProviderModel>,
}

pub fn cached_provider_model(provider: &str, model: &str) -> Option<ProviderModel> {
    cached_provider_models(provider)
        .into_iter()
        .find(|entry| entry.model == model)
}

pub fn cached_provider_models(provider: &str) -> Vec<ProviderModel> {
    let Ok(connection) = open_provider_models_cache() else {
        return Vec::new();
    };
    let Ok(mut statement) = connection.prepare(
        "select model, display_name, max_output_tokens from provider_models where provider = ?1 order by model",
    ) else {
        return Vec::new();
    };
    let Ok(rows) = statement.query_map(params![provider], |row| {
        let model: String = row.get(0)?;
        let display_name: String = row.get(1)?;
        let max_output_tokens: Option<u64> = row.get(2)?;
        Ok(ProviderModel {
            provider: provider.to_string(),
            model,
            display_name,
            max_output_tokens,
        })
    }) else {
        return Vec::new();
    };
    rows.filter_map(Result::ok).collect()
}

pub async fn refresh_provider_models_with_store(
    provider: &str,
    store: &dyn CredentialStore,
) -> Result<ProviderModelRefresh, ModelError> {
    let descriptor = registry::provider_descriptor(provider)
        .ok_or_else(|| ModelError::UnsupportedProvider(provider.to_string()))?;
    let models = match descriptor.model_refresh {
        Some(ProviderModelRefreshKind::OpenAi) => fetch_openai_models(provider, store).await?,
        Some(ProviderModelRefreshKind::Anthropic) => {
            fetch_anthropic_models(provider, store).await?
        }
        Some(ProviderModelRefreshKind::GithubCopilot) => {
            fetch_github_copilot_models(provider, store).await?
        }
        None => return Err(ModelError::UnsupportedProvider(provider.to_string())),
    };
    replace_cached_provider_models(provider, &models)?;
    Ok(ProviderModelRefresh {
        provider: provider.to_string(),
        models,
    })
}

fn replace_cached_provider_models(
    provider: &str,
    models: &[ProviderModel],
) -> Result<(), ModelError> {
    let mut connection = open_provider_models_cache().map_err(model_cache_error)?;
    let tx = connection.transaction().map_err(model_cache_error)?;
    tx.execute(
        "delete from provider_models where provider = ?1",
        params![provider],
    )
    .map_err(model_cache_error)?;
    for model in models {
        tx.execute(
            "insert into provider_models (provider, model, display_name, max_output_tokens, raw_json, updated_at)
             values (?1, ?2, ?3, ?4, ?5, strftime('%s', 'now'))",
            params![
                provider,
                model.model,
                model.display_name,
                model.max_output_tokens,
                Value::Null.to_string()
            ],
        )
        .map_err(model_cache_error)?;
    }
    tx.execute(
        "insert into provider_model_refresh (provider, updated_at, error)
         values (?1, strftime('%s', 'now'), null)
         on conflict(provider) do update set updated_at = excluded.updated_at, error = null",
        params![provider],
    )
    .map_err(model_cache_error)?;
    tx.commit().map_err(model_cache_error)?;
    Ok(())
}

async fn fetch_openai_models(
    provider: &str,
    store: &dyn CredentialStore,
) -> Result<Vec<ProviderModel>, ModelError> {
    let key = load_api_key_auth(provider, store)?;
    let response: OpenAiModelsResponse = reqwest::Client::new()
        .get("https://api.openai.com/v1/models")
        .bearer_auth(key)
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    let mut models = response
        .data
        .into_iter()
        .filter(|model| is_supported_openai_model(&model.id))
        .map(|model| ProviderModel {
            provider: provider.to_string(),
            display_name: model.id.clone(),
            model: model.id,
            max_output_tokens: None,
        })
        .collect::<Vec<_>>();
    models.sort_by(|left, right| left.model.cmp(&right.model));
    models.dedup_by(|left, right| left.model == right.model);
    Ok(models)
}

async fn fetch_anthropic_models(
    provider: &str,
    store: &dyn CredentialStore,
) -> Result<Vec<ProviderModel>, ModelError> {
    let key = load_api_key_auth(provider, store)?;
    let client = reqwest::Client::new();
    let mut models = Vec::new();
    let mut after_id = None::<String>;
    loop {
        let mut url = Url::parse("https://api.anthropic.com/v1/models").map_err(|err| {
            ModelError::InvalidResponse(format!("invalid Anthropic models URL: {err}"))
        })?;
        if let Some(after_id) = &after_id {
            url.query_pairs_mut().append_pair("after_id", after_id);
        }
        let response: AnthropicModelsResponse = client
            .get(url)
            .header("x-api-key", &key)
            .header("anthropic-version", "2023-06-01")
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;
        let last_id = response.last_id.clone();
        models.extend(
            response
                .data
                .into_iter()
                .filter(|model| model.id.starts_with("claude-"))
                .map(|model| ProviderModel {
                    provider: provider.to_string(),
                    display_name: model.display_name.unwrap_or_else(|| model.id.clone()),
                    model: model.id,
                    max_output_tokens: model.max_tokens,
                }),
        );
        if !response.has_more {
            break;
        }
        let Some(next_after_id) = last_id else {
            break;
        };
        after_id = Some(next_after_id);
    }
    models.sort_by(|left, right| left.model.cmp(&right.model));
    models.dedup_by(|left, right| left.model == right.model);
    Ok(models)
}

async fn fetch_github_copilot_models(
    provider: &str,
    store: &dyn CredentialStore,
) -> Result<Vec<ProviderModel>, ModelError> {
    let client = reqwest::Client::new();
    let auth = auth_material_with_store(&client, store).await?;
    let response = send_github_copilot_models_request(&client, &auth).await?;
    let response = if response.status() == StatusCode::UNAUTHORIZED
        && auth.source == GitHubCopilotAuthSource::Store
    {
        if let Some(refreshed) = force_refresh_auth_material_with_store(&client, store).await? {
            send_github_copilot_models_request(&client, &refreshed).await?
        } else {
            response
        }
    } else {
        response
    };
    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return if status == StatusCode::UNAUTHORIZED {
            Err(ModelError::MissingGithubCopilotAuth)
        } else {
            Err(ModelError::HttpStatus { status, body })
        };
    }
    let value = response.json::<Value>().await?;
    parse_github_copilot_models(provider, &value)
}

async fn send_github_copilot_models_request(
    client: &reqwest::Client,
    auth: &GitHubCopilotAuthMaterial,
) -> Result<reqwest::Response, ModelError> {
    Ok(client
        .get(&auth.models_endpoint)
        .bearer_auth(&auth.token)
        .header("Accept", "application/json")
        .header("User-Agent", concat!("rho/", env!("CARGO_PKG_VERSION")))
        .header("Editor-Version", concat!("rho/", env!("CARGO_PKG_VERSION")))
        .header(
            "Editor-Plugin-Version",
            concat!("rho/", env!("CARGO_PKG_VERSION")),
        )
        .header("Copilot-Integration-Id", "vscode-chat")
        .send()
        .await?)
}

fn parse_github_copilot_models(
    provider: &str,
    value: &Value,
) -> Result<Vec<ProviderModel>, ModelError> {
    let raw_models = value
        .get("data")
        .or_else(|| value.get("models"))
        .unwrap_or(value)
        .as_array()
        .ok_or_else(|| {
            ModelError::InvalidResponse("GitHub Copilot models response was not an array".into())
        })?;
    let mut models = raw_models
        .iter()
        .filter_map(|value| {
            value.as_str().map(ToOwned::to_owned).or_else(|| {
                value
                    .get("id")
                    .or_else(|| value.get("name"))
                    .and_then(|id| id.as_str())
                    .map(ToOwned::to_owned)
            })
        })
        .filter(|model| !model.trim().is_empty())
        .map(|model| ProviderModel {
            provider: provider.to_string(),
            display_name: model.clone(),
            model,
            max_output_tokens: None,
        })
        .collect::<Vec<_>>();
    models.sort_by(|left, right| left.model.cmp(&right.model));
    models.dedup_by(|left, right| left.model == right.model);
    Ok(models)
}

fn load_api_key_auth(provider: &str, store: &dyn CredentialStore) -> Result<String, ModelError> {
    let descriptor = registry::provider_descriptor(provider)
        .ok_or_else(|| ModelError::UnsupportedProvider(provider.to_string()))?;
    let ProviderAuthKind::ApiKey {
        env_var, missing, ..
    } = descriptor.auth_kind
    else {
        return Err(ModelError::UnsupportedProvider(provider.to_string()));
    };
    if let Ok(key) = std::env::var(env_var) {
        return Ok(key);
    }
    load_provider_api_key(store, provider)?.ok_or_else(|| missing_credential_error(missing))
}

fn is_supported_openai_model(model: &str) -> bool {
    let is_reasoning =
        model.starts_with('o') && model.chars().nth(1).is_some_and(|c| c.is_ascii_digit());
    let is_gpt = model.starts_with("gpt-")
        && !model.contains("realtime")
        && !model.contains("audio")
        && !model.contains("image");
    is_reasoning || is_gpt
}

#[derive(Deserialize)]
struct OpenAiModelsResponse {
    data: Vec<OpenAiModel>,
}

#[derive(Deserialize)]
struct OpenAiModel {
    id: String,
}

#[derive(Deserialize)]
struct AnthropicModelsResponse {
    data: Vec<AnthropicModel>,
    #[serde(default)]
    has_more: bool,
    last_id: Option<String>,
}

#[derive(Deserialize)]
struct AnthropicModel {
    id: String,
    display_name: Option<String>,
    max_tokens: Option<u64>,
}

fn open_provider_models_cache() -> rusqlite::Result<Connection> {
    let path = provider_models_sqlite_path();
    if let Some(parent) = path.parent() {
        let _ = fs::create_dir_all(parent);
    }
    let connection = Connection::open(path)?;
    connection.execute_batch(
        "create table if not exists provider_models (
            provider text not null,
            model text not null,
            display_name text not null,
            max_output_tokens integer,
            raw_json text,
            updated_at integer not null,
            primary key(provider, model)
        );
        create table if not exists provider_model_refresh (
            provider text primary key,
            updated_at integer not null,
            error text
        );",
    )?;
    let _ = connection.execute(
        "alter table provider_models add column max_output_tokens integer",
        [],
    );
    Ok(connection)
}

fn model_cache_error(error: rusqlite::Error) -> ModelError {
    ModelError::InvalidResponse(format!("provider model cache error: {error}"))
}

fn provider_models_sqlite_path() -> PathBuf {
    cache_dir().join("provider-models.sqlite3")
}

fn cache_dir() -> PathBuf {
    #[cfg(test)]
    {
        if let Some(path) = test_cache_dir() {
            return path;
        }
        default_test_cache_dir()
    }
    #[cfg(not(test))]
    if let Some(path) = std::env::var_os("XDG_CACHE_HOME") {
        return PathBuf::from(path).join("rho");
    }
    #[cfg(not(test))]
    {
        #[cfg(target_os = "windows")]
        {
            if let Some(path) = std::env::var_os("LOCALAPPDATA") {
                return PathBuf::from(path).join("rho").join("cache");
            }
        }
        #[cfg(target_os = "macos")]
        {
            if let Some(path) = paths::home_dir() {
                return path.join("Library").join("Caches").join("rho");
            }
        }
        if let Some(path) = paths::home_dir() {
            return path.join(".cache").join("rho");
        }
        std::env::temp_dir().join("rho-cache")
    }
}

#[cfg(test)]
thread_local! {
    static TEST_CACHE_DIR: RefCell<Option<PathBuf>> = const { RefCell::new(None) };
}

#[cfg(test)]
fn test_cache_dir() -> Option<PathBuf> {
    TEST_CACHE_DIR.with(|path| path.borrow().clone())
}

#[cfg(test)]
fn default_test_cache_dir() -> PathBuf {
    std::env::temp_dir().join(format!(
        "rho-provider-models-default-test-cache-{}",
        std::process::id()
    ))
}

#[cfg(test)]
pub fn with_provider_models_cache_dir_for_tests<T>(path: PathBuf, f: impl FnOnce() -> T) -> T {
    TEST_CACHE_DIR.with(|cache_dir| {
        let previous = cache_dir.replace(Some(path));
        let result = f();
        cache_dir.replace(previous);
        result
    })
}

#[cfg(test)]
pub fn replace_cached_provider_models_for_tests(
    provider: &str,
    models: &[ProviderModel],
) -> Result<(), ModelError> {
    replace_cached_provider_models(provider, models)
}

#[cfg(test)]
fn unique_test_cache_dir(name: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("test clock should be after Unix epoch")
        .as_nanos();
    std::env::temp_dir().join(format!(
        "rho-provider-models-{name}-{}-{nanos}",
        std::process::id()
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::credentials::{
        save_github_copilot_tokens, save_provider_api_key, GitHubCopilotTokens,
        MemoryCredentialStore,
    };
    use tokio::{
        io::{AsyncReadExt, AsyncWriteExt},
        net::TcpListener,
    };

    #[test]
    fn openai_model_filter_keeps_chat_families() {
        assert!(is_supported_openai_model("gpt-5.5"));
        assert!(is_supported_openai_model("o3"));
        assert!(!is_supported_openai_model("text-embedding-3-large"));
        assert!(!is_supported_openai_model("whisper-1"));
    }

    #[test]
    fn load_api_key_auth_reads_the_supplied_store() {
        let store = MemoryCredentialStore::default();
        save_provider_api_key(&store, "anthropic", "sk-ant-test").unwrap();

        assert_eq!(
            load_api_key_auth("anthropic", &store).unwrap(),
            "sk-ant-test"
        );
    }

    #[test]
    fn parses_github_copilot_models_from_data_objects_and_deduplicates() {
        let value = serde_json::json!({
            "data": [
                {"id": "gpt-4.1"},
                {"name": "claude-sonnet-4"},
                {"id": "gpt-4.1"}
            ]
        });

        assert_eq!(
            parse_github_copilot_models("github-copilot", &value).unwrap(),
            vec![
                ProviderModel {
                    provider: "github-copilot".into(),
                    model: "claude-sonnet-4".into(),
                    display_name: "claude-sonnet-4".into(),
                    max_output_tokens: None,
                },
                ProviderModel {
                    provider: "github-copilot".into(),
                    model: "gpt-4.1".into(),
                    display_name: "gpt-4.1".into(),
                    max_output_tokens: None,
                },
            ]
        );
    }

    #[tokio::test]
    async fn github_copilot_models_retry_once_after_unauthorized() {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let base_url = format!("http://{}", listener.local_addr().unwrap());
        let base_url_for_server = base_url.clone();
        tokio::spawn(async move {
            for index in 0..3 {
                let (mut stream, _) = listener.accept().await.unwrap();
                let mut buffer = [0; 1024];
                let bytes = stream.read(&mut buffer).await.unwrap();
                let request = String::from_utf8_lossy(&buffer[..bytes]);
                let is_model_request = request.contains("GET /models");
                let (status, body) = match (index, is_model_request) {
                    (0, true) => ("401 Unauthorized", String::new()),
                    (1, false) => (
                        "200 OK",
                        format!(
                            "{{\"token\":\"second\",\"endpoints\":{{\"api\":\"{base_url_for_server}\"}}}}"
                        ),
                    ),
                    (2, true) => (
                        "200 OK",
                        r#"{"data":[{"id":"gpt-4.1"}]}"#.to_string(),
                    ),
                    _ => ("500 Internal Server Error", String::new()),
                };
                let reply = format!(
                    "HTTP/1.1 {status}\r\ncontent-type: application/json\r\ncontent-length: {}\r\n\r\n{}",
                    body.len(), body
                );
                stream.write_all(reply.as_bytes()).await.unwrap();
                stream.shutdown().await.unwrap();
            }
        });
        let store = MemoryCredentialStore::default();
        save_github_copilot_tokens(
            &store,
            &GitHubCopilotTokens {
                github_access_token: "github".into(),
                github_refresh_token: None,
                github_expires_at_unix: None,
                copilot_token: Some("first".into()),
                copilot_expires_at_unix: Some(i64::MAX),
                copilot_refresh_after_unix: None,
                copilot_token_endpoint: Some(base_url.clone()),
                copilot_chat_endpoint: None,
                copilot_models_endpoint: Some(format!("{base_url}/models")),
            },
        )
        .unwrap();

        assert_eq!(
            fetch_github_copilot_models("github-copilot", &store)
                .await
                .unwrap(),
            vec![ProviderModel {
                provider: "github-copilot".into(),
                model: "gpt-4.1".into(),
                display_name: "gpt-4.1".into(),
                max_output_tokens: None,
            }]
        );
    }

    #[test]
    fn provider_model_cache_replaces_one_provider_and_preserves_max_tokens() {
        let cache_dir = unique_test_cache_dir("replace");
        with_provider_models_cache_dir_for_tests(cache_dir.clone(), || {
            replace_cached_provider_models(
                "openai",
                &[ProviderModel {
                    provider: "openai".into(),
                    model: "gpt-5.5".into(),
                    display_name: "gpt-5.5".into(),
                    max_output_tokens: None,
                }],
            )
            .unwrap();
            replace_cached_provider_models(
                "anthropic",
                &[
                    ProviderModel {
                        provider: "anthropic".into(),
                        model: "claude-b".into(),
                        display_name: "Claude B".into(),
                        max_output_tokens: Some(64_000),
                    },
                    ProviderModel {
                        provider: "anthropic".into(),
                        model: "claude-a".into(),
                        display_name: "Claude A".into(),
                        max_output_tokens: Some(32_000),
                    },
                ],
            )
            .unwrap();
            replace_cached_provider_models(
                "anthropic",
                &[ProviderModel {
                    provider: "anthropic".into(),
                    model: "claude-c".into(),
                    display_name: "Claude C".into(),
                    max_output_tokens: Some(16_000),
                }],
            )
            .unwrap();

            assert_eq!(
                cached_provider_models("openai"),
                vec![ProviderModel {
                    provider: "openai".into(),
                    model: "gpt-5.5".into(),
                    display_name: "gpt-5.5".into(),
                    max_output_tokens: None,
                }]
            );
            assert_eq!(
                cached_provider_models("anthropic"),
                vec![ProviderModel {
                    provider: "anthropic".into(),
                    model: "claude-c".into(),
                    display_name: "Claude C".into(),
                    max_output_tokens: Some(16_000),
                }]
            );
        });
        let _ = fs::remove_dir_all(cache_dir);
    }

    #[test]
    fn provider_model_cache_migrates_old_schema() {
        let cache_dir = unique_test_cache_dir("migration");
        fs::create_dir_all(&cache_dir).unwrap();
        let connection = Connection::open(cache_dir.join("provider-models.sqlite3")).unwrap();
        connection
            .execute_batch(
                "create table provider_models (
                    provider text not null,
                    model text not null,
                    display_name text not null,
                    raw_json text,
                    updated_at integer not null,
                    primary key(provider, model)
                );
                create table provider_model_refresh (
                    provider text primary key,
                    updated_at integer not null,
                    error text
                );",
            )
            .unwrap();
        drop(connection);

        with_provider_models_cache_dir_for_tests(cache_dir.clone(), || {
            replace_cached_provider_models(
                "anthropic",
                &[ProviderModel {
                    provider: "anthropic".into(),
                    model: "claude-sonnet".into(),
                    display_name: "Claude Sonnet".into(),
                    max_output_tokens: Some(64_000),
                }],
            )
            .unwrap();

            assert_eq!(
                cached_provider_model("anthropic", "claude-sonnet")
                    .and_then(|model| model.max_output_tokens),
                Some(64_000)
            );
        });
        let _ = fs::remove_dir_all(cache_dir);
    }
}