rho-coding-agent 0.25.0

A lightweight agent harness inspired by Pi
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
use std::{
    fs,
    path::{Path, PathBuf},
};

use base64::{engine::general_purpose::STANDARD as BASE64_STANDARD, Engine as _};
use serde_json::{json, Value};
use tokio::process::Command;
use url::Url;

use crate::tool::{truncate, ToolError};

use super::{FetchedTarget, PREVIEW_BYTES};
use crate::tools::web::{
    process,
    storage::{create_private_dir_all, web_access_cache_root},
    util::{safe_path_component, to_pretty_json},
};

const LARGE_REPO_THRESHOLD_KB: u64 = 350 * 1024;

pub(super) async fn fetch(
    client: &reqwest::Client,
    github: &GitHubTarget,
    force_clone: bool,
) -> Result<FetchedTarget, ToolError> {
    if github.kind == GitHubKind::Commit {
        return github_api_fallback(client, github, None).await;
    }

    let repo_api = format!(
        "https://api.github.com/repos/{}/{}",
        github.owner, github.repo
    );
    let repo_size_kb = github_api_json(client, &repo_api)
        .await
        .ok()
        .and_then(|value| value.get("size").and_then(Value::as_u64));
    let oversized = repo_size_kb.is_some_and(|size| size > LARGE_REPO_THRESHOLD_KB);
    if oversized && !force_clone {
        return github_api_fallback(client, github, repo_size_kb).await;
    }

    match ensure_github_clone(github).await {
        Ok(local_path) => read_github_clone(github, &local_path).await,
        Err(_) => github_api_fallback(client, github, repo_size_kb).await,
    }
}

async fn github_api_fallback(
    client: &reqwest::Client,
    github: &GitHubTarget,
    repo_size_kb: Option<u64>,
) -> Result<FetchedTarget, ToolError> {
    let api_url = github_api_content_url(github);
    let content = match github.kind {
        GitHubKind::Blob => github_api_file_content(client, &api_url).await?,
        GitHubKind::Root | GitHubKind::Tree | GitHubKind::Commit => {
            to_pretty_json(&github_api_json(client, &api_url).await?)
        }
    };
    Ok(FetchedTarget {
        title: Some(format!("{}/{}", github.owner, github.repo)),
        preview: json!({
            "type": "github_api_fallback",
            "repo": format!("{}/{}", github.owner, github.repo),
            "reason": repo_size_kb.map(|size| format!("repo size {size}KB exceeds 350MB threshold")).unwrap_or_else(|| "clone unavailable".into()),
            "canForceClone": true,
            "preview": truncate(content.clone(), PREVIEW_BYTES)
        }),
        content,
        metadata: json!({"mode": "api_fallback", "repoSizeKb": repo_size_kb}),
    })
}

fn github_api_content_url(github: &GitHubTarget) -> String {
    match github.kind {
        GitHubKind::Root | GitHubKind::Tree | GitHubKind::Blob => format!(
            "https://api.github.com/repos/{}/{}/contents/{}{}",
            github.owner,
            github.repo,
            github.path,
            github
                .ref_name
                .as_ref()
                .map(|ref_name| format!("?ref={ref_name}"))
                .unwrap_or_default()
        ),
        GitHubKind::Commit => format!(
            "https://api.github.com/repos/{}/{}/commits/{}",
            github.owner,
            github.repo,
            github.ref_name.as_deref().unwrap_or("HEAD")
        ),
    }
}

async fn github_api_file_content(client: &reqwest::Client, url: &str) -> Result<String, ToolError> {
    let value = github_api_json(client, url).await?;
    let encoding = value.get("encoding").and_then(Value::as_str);
    let content = value
        .get("content")
        .and_then(Value::as_str)
        .ok_or_else(|| {
            ToolError::Message("GitHub API response did not include file content".into())
        })?;
    if encoding == Some("base64") {
        let compact = content.lines().collect::<String>();
        let bytes = BASE64_STANDARD.decode(compact).map_err(|err| {
            ToolError::Message(format!("GitHub file content was not base64: {err}"))
        })?;
        return String::from_utf8(bytes).map_err(ToolError::Utf8);
    }
    Ok(content.to_string())
}

async fn github_api_json(client: &reqwest::Client, url: &str) -> Result<Value, ToolError> {
    let mut request = client.get(url).header("User-Agent", "rho-coding-agent");
    if let Ok(token) = github_token() {
        request = request.bearer_auth(token);
    }
    request
        .send()
        .await
        .map_err(|err| ToolError::Message(format!("GitHub API request failed: {err}")))?
        .error_for_status()
        .map_err(|err| ToolError::Message(format!("GitHub API request failed: {err}")))?
        .json()
        .await
        .map_err(|err| ToolError::Message(format!("GitHub API response was not JSON: {err}")))
}

fn github_token() -> Result<String, std::env::VarError> {
    std::env::var("GITHUB_TOKEN").or_else(|_| std::env::var("GH_TOKEN"))
}

async fn ensure_github_clone(github: &GitHubTarget) -> Result<PathBuf, ToolError> {
    let cache_root = web_access_cache_root()
        .join(std::process::id().to_string())
        .join("github")
        .join(safe_path_component(&github.owner))
        .join(safe_path_component(&github.repo));
    let ref_key = github.ref_name.as_deref().unwrap_or("HEAD");
    let local_path = cache_root.join(safe_path_component(ref_key));
    create_private_dir_all(&cache_root)?;
    if local_path.join(".git").is_dir() {
        checkout_github_ref(github, &local_path).await?;
        return Ok(local_path);
    }

    let repo_slug = format!("{}/{}", github.owner, github.repo);
    let clone_url = format!("https://github.com/{repo_slug}.git");
    let mut command = if let Ok(token) = github_token() {
        let mut command = Command::new("gh");
        command
            .arg("repo")
            .arg("clone")
            .arg(&repo_slug)
            .arg(&local_path)
            .arg("--")
            .arg("--depth")
            .arg("1");
        if std::env::var_os("GH_TOKEN").is_none() {
            command.env("GH_TOKEN", token);
        }
        command
    } else {
        let mut command = Command::new("git");
        command
            .arg("clone")
            .arg("--depth")
            .arg("1")
            .arg(clone_url)
            .arg(&local_path);
        command
    };
    process::run(
        &mut command,
        &format!("git clone for {}/{}", github.owner, github.repo),
    )
    .await?;
    checkout_github_ref(github, &local_path).await?;
    Ok(local_path)
}

async fn checkout_github_ref(github: &GitHubTarget, local_path: &Path) -> Result<(), ToolError> {
    let Some(ref_name) = github.ref_name.as_deref() else {
        return Ok(());
    };
    let mut fetch = Command::new("git");
    fetch
        .arg("-C")
        .arg(local_path)
        .arg("fetch")
        .arg("--depth")
        .arg("1")
        .arg("origin")
        .arg(ref_name);
    process::run(
        &mut fetch,
        &format!(
            "git fetch for {}/{} ref {ref_name}",
            github.owner, github.repo
        ),
    )
    .await?;
    let mut checkout = Command::new("git");
    checkout
        .arg("-C")
        .arg(local_path)
        .arg("checkout")
        .arg("--detach")
        .arg("FETCH_HEAD");
    process::run(
        &mut checkout,
        &format!(
            "git checkout for {}/{} ref {ref_name}",
            github.owner, github.repo
        ),
    )
    .await?;
    Ok(())
}

async fn read_github_clone(
    github: &GitHubTarget,
    local_path: &Path,
) -> Result<FetchedTarget, ToolError> {
    let target_path = local_path.join(&github.path);
    let commit = process::output(
        Command::new("git")
            .arg("-C")
            .arg(local_path)
            .arg("rev-parse")
            .arg("HEAD"),
        "git rev-parse HEAD",
    )
    .await
    .ok()
    .filter(|output| output.status.success())
    .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_string());

    match github.kind {
        GitHubKind::Root | GitHubKind::Tree => {
            let dir = if github.kind == GitHubKind::Root {
                local_path
            } else {
                &target_path
            };
            let tree = directory_listing(dir)?;
            let readme = find_readme(dir).and_then(|path| fs::read_to_string(path).ok());
            let content = format!(
                "localPath: {}\ncommit: {}\n\n{}{}",
                local_path.display(),
                commit.clone().unwrap_or_else(|| "unknown".into()),
                tree,
                readme
                    .as_ref()
                    .map(|readme| format!("\n\nREADME:\n{readme}"))
                    .unwrap_or_default()
            );
            Ok(FetchedTarget {
                title: Some(format!("{}/{}", github.owner, github.repo)),
                preview: json!({
                    "type": "github_repo",
                    "localPath": local_path,
                    "commit": commit,
                    "tree": tree,
                    "readmePreview": readme.map(|readme| truncate(readme, PREVIEW_BYTES))
                }),
                content,
                metadata: json!({"mode": "clone", "localPath": local_path, "commit": commit}),
            })
        }
        GitHubKind::Blob => {
            let content = fs::read_to_string(&target_path)?;
            Ok(FetchedTarget {
                title: Some(github.path.clone()),
                preview: json!({
                    "type": "github_file",
                    "localPath": target_path,
                    "commit": commit,
                    "preview": truncate(content.clone(), PREVIEW_BYTES)
                }),
                content,
                metadata: json!({"mode": "clone", "localPath": local_path, "commit": commit}),
            })
        }
        GitHubKind::Commit => github_api_fallback_sync_notice(github, local_path, commit),
    }
}

fn github_api_fallback_sync_notice(
    github: &GitHubTarget,
    local_path: &Path,
    commit: Option<String>,
) -> Result<FetchedTarget, ToolError> {
    let content = format!(
        "Commit URLs are handled via GitHub API in fetch_content. Clone is available at {} with HEAD {}.",
        local_path.display(),
        commit.as_deref().unwrap_or("unknown")
    );
    Ok(FetchedTarget {
        title: Some(format!("{}/{} commit", github.owner, github.repo)),
        preview: json!({"type": "github_commit", "warning": content}),
        content,
        metadata: json!({"mode": "commit_notice", "localPath": local_path, "commit": commit}),
    })
}

fn directory_listing(path: &Path) -> Result<String, ToolError> {
    let mut entries = fs::read_dir(path)?
        .map(|entry| {
            let entry = entry?;
            let file_type = entry.file_type()?;
            let suffix = if file_type.is_dir() { "/" } else { "" };
            Ok(format!("{}{}", entry.file_name().to_string_lossy(), suffix))
        })
        .collect::<Result<Vec<_>, std::io::Error>>()?;
    entries.sort();
    Ok(entries.join("\n"))
}

fn find_readme(path: &Path) -> Option<PathBuf> {
    ["README.md", "README.txt", "README"]
        .into_iter()
        .map(|name| path.join(name))
        .find(|path| path.is_file())
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::tools::web) enum GitHubKind {
    Root,
    Tree,
    Blob,
    Commit,
}

#[derive(Debug)]
pub(in crate::tools::web) struct GitHubTarget {
    pub(in crate::tools::web) owner: String,
    pub(in crate::tools::web) repo: String,
    pub(in crate::tools::web) kind: GitHubKind,
    pub(in crate::tools::web) ref_name: Option<String>,
    pub(in crate::tools::web) path: String,
}

pub(in crate::tools::web) fn parse_url(input: &str) -> Option<GitHubTarget> {
    let url = Url::parse(input).ok()?;
    if url.host_str()? != "github.com" {
        return None;
    }
    let segments = url.path_segments()?.collect::<Vec<_>>();
    if segments.len() < 2 {
        return None;
    }
    let owner = segments[0].to_string();
    let repo = segments[1].trim_end_matches(".git").to_string();
    match segments.get(2).copied() {
        None | Some("") => Some(GitHubTarget {
            owner,
            repo,
            kind: GitHubKind::Root,
            ref_name: None,
            path: String::new(),
        }),
        Some("tree") | Some("blob") => {
            let kind = if segments[2] == "tree" {
                GitHubKind::Tree
            } else {
                GitHubKind::Blob
            };
            let (ref_name, path) = split_github_ref_and_path(kind, &segments[3..]);
            Some(GitHubTarget {
                owner,
                repo,
                kind,
                ref_name,
                path,
            })
        }
        Some("commit") => Some(GitHubTarget {
            owner,
            repo,
            kind: GitHubKind::Commit,
            ref_name: segments.get(3).map(|value| (*value).to_string()),
            path: String::new(),
        }),
        _ => None,
    }
}

fn split_github_ref_and_path(_kind: GitHubKind, segments: &[&str]) -> (Option<String>, String) {
    if segments.is_empty() {
        return (None, String::new());
    }
    if segments.len() == 1 {
        return (Some(segments[0].to_string()), String::new());
    }

    let split_at = find_github_path_start(segments).unwrap_or(1);
    (
        Some(segments[..split_at].join("/")),
        segments[split_at..].join("/"),
    )
}

fn find_github_path_start(segments: &[&str]) -> Option<usize> {
    (1..segments.len()).find(|index| is_common_github_path_start(segments[*index]))
}

fn is_common_github_path_start(segment: &str) -> bool {
    matches!(
        segment,
        "src"
            | "docs"
            | "doc"
            | "test"
            | "tests"
            | "crates"
            | "packages"
            | "package"
            | "examples"
            | "example"
            | "scripts"
            | "script"
            | "tools"
            | "tool"
            | "app"
            | "apps"
            | "lib"
            | "libs"
            | "cmd"
            | "components"
            | "component"
            | "internal"
            | "pkg"
            | ".github"
    )
}