Format massive JSON arrays into Markdown natively to avoid context blowouts
This commit is contained in:
1 parent
760ebfcd40
commit
3ee95f5c39
4 files changed
+157
-42
No files matched your search
+49
-14
@@ -27,7 +27,7 @@ impl McpTool for GetActiveWorktreeContextHandler {
|
||||
_state: Arc<MemoryState>,
|
||||
) -> crate::error::Result<String> {
|
||||
let result =
|
||||
tokio::task::spawn_blocking(move || -> crate::error::Result<serde_json::Value> {
|
||||
tokio::task::spawn_blocking(move || -> crate::error::Result<String> {
|
||||
let cwd = env::current_dir().map_err(|e| crate::error::AppError::Internal(format!("Failed to get current directory: {}", e)))?;
|
||||
let repo = git2::Repository::discover(&cwd).map_err(|e| {
|
||||
crate::error::AppError::Internal(format!("Not in a git repository: {}", e))
|
||||
@@ -79,16 +79,34 @@ impl McpTool for GetActiveWorktreeContextHandler {
|
||||
diff_output.push_str("\n... [Diff truncated due to size]");
|
||||
}
|
||||
|
||||
Ok(json!({
|
||||
"branch": branch_name,
|
||||
"modified_files": files,
|
||||
"diff": diff_output
|
||||
}))
|
||||
let mut out = String::new();
|
||||
out.push_str("## Active Worktree Context\n\n");
|
||||
out.push_str(&format!("**Branch:** `{}`\n\n", branch_name));
|
||||
|
||||
out.push_str("### Modified Files\n");
|
||||
if files.is_empty() {
|
||||
out.push_str("No modified files.\n");
|
||||
} else {
|
||||
for f in files {
|
||||
out.push_str(&format!("- `{}`\n", f));
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("\n### Diff\n");
|
||||
if diff_output.is_empty() {
|
||||
out.push_str("No changes.\n");
|
||||
} else {
|
||||
out.push_str("```diff\n");
|
||||
out.push_str(&diff_output);
|
||||
out.push_str("\n```\n");
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| crate::error::AppError::Internal(format!("Task panic: {}", e)))??;
|
||||
|
||||
Ok::<String, crate::error::AppError>(serde_yaml::to_string(&result)?)
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +188,25 @@ impl McpTool for QueryGitDiffsHandler {
|
||||
matches.extend(git_matches);
|
||||
}
|
||||
|
||||
Ok(serde_yaml::to_string(&matches)?)
|
||||
let mut out = String::new();
|
||||
out.push_str(&format!("## Git Diffs for `{}`\n\n", req.query));
|
||||
for m in &matches {
|
||||
if let Some(file) = m.get("file_path").and_then(|f| f.as_str()) {
|
||||
let desc = m.get("description").and_then(|d| d.as_str()).unwrap_or("");
|
||||
let commit = m.get("commit").and_then(|c| c.as_str()).unwrap_or("");
|
||||
out.push_str(&format!("- **File:** `{}` | **Commit:** `{}`\n - {}\n", file, commit, desc));
|
||||
} else {
|
||||
let commit = m.get("commit_id").and_then(|c| c.as_str()).unwrap_or("");
|
||||
let author = m.get("author").and_then(|a| a.as_str()).unwrap_or("");
|
||||
let msg = m.get("message").and_then(|m| m.as_str()).unwrap_or("");
|
||||
out.push_str(&format!("- **Commit:** `{}` | **Author:** {}\n - {}\n", commit, author, msg));
|
||||
}
|
||||
}
|
||||
if matches.is_empty() {
|
||||
out.push_str("No git diffs found.\n");
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,10 +229,10 @@ mod tests {
|
||||
.map_err(|e| format!("Failed to get worktree context: {}", e))
|
||||
.unwrap();
|
||||
|
||||
let parsed: serde_json::Value = serde_yaml::from_str(&result).unwrap();
|
||||
assert!(parsed.get("branch").is_some());
|
||||
assert!(parsed.get("modified_files").is_some());
|
||||
assert!(parsed.get("diff").is_some());
|
||||
assert!(result.contains("## Active Worktree Context"));
|
||||
assert!(result.contains("**Branch:**"));
|
||||
assert!(result.contains("### Modified Files"));
|
||||
assert!(result.contains("### Diff"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -212,7 +248,6 @@ mod tests {
|
||||
.map_err(|e| format!("Failed to get worktree context: {}", e))
|
||||
.unwrap();
|
||||
|
||||
let parsed: serde_json::Value = serde_yaml::from_str(&result).unwrap();
|
||||
assert!(parsed.get("branch").is_some() || parsed.is_object());
|
||||
assert!(result.contains("## Active Worktree Context"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user