1
0

add git-repo

This commit is contained in:
syui 2025-05-21 22:33:11 +09:00
parent 1122538c73
commit 6fdc573358
Signed by: syui
GPG Key ID: 5417CFEBAD92DF56
5 changed files with 77 additions and 5 deletions

View File

@ -35,5 +35,13 @@ $ ./aigpt mcp chat "hello world!" --host http://localhost:11434 --model syui/ai
# openai api
$ ./aigpt mcp set-api -api sk-abc123
$ ./aigpt mcp chat "こんにちは" -p openai -m gpt-4o-mini
---
# git管理されているファイルをAIに読ませる
./aigpt mcp chat --host http://localhost:11434 --repo git@git.syui.ai:ai/gpt
**改善案と次のステップ:**
1. **README.md の大幅な改善:**
**次のステップ:**
1. **README.md の作成:** 1. の指示に従って、README.md ファイルを作成します。
```

View File

@ -43,7 +43,7 @@ fn load_openai_api_key() -> Option<String> {
Some(parsed.token)
}
pub fn ask_chat(c: &Context, question: &str) {
pub fn ask_chat(c: &Context, question: &str) -> String {
let config = ConfigPaths::new();
let base_dir = config.base_dir.join("mcp");
let script_path = base_dir.join("scripts/ask.py");
@ -83,18 +83,18 @@ pub fn ask_chat(c: &Context, question: &str) {
command.env("OPENAI_API_KEY", api_key);
}
// 🔁 実行
let output = command
.output()
.expect("❌ MCPチャットスクリプトの実行に失敗しました");
if output.status.success() {
println!("💬 {}", String::from_utf8_lossy(&output.stdout));
String::from_utf8_lossy(&output.stdout).to_string()
} else {
eprintln!(
"❌ 実行エラー: {}\n{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout),
);
String::from("エラーが発生しました。")
}
}

17
src/commands/git_repo.rs Normal file
View File

@ -0,0 +1,17 @@
// src/commands/git_repo.rs
use std::fs;
// Gitリポジトリ内の全てのファイルを取得し、内容を読み取る
pub fn read_all_git_files(repo_path: &str) -> String {
let mut content = String::new();
for entry in fs::read_dir(repo_path).expect("ディレクトリ読み込み失敗") {
let entry = entry.expect("エントリ読み込み失敗");
let path = entry.path();
if path.is_file() {
if let Ok(file_content) = fs::read_to_string(&path) {
content.push_str(&format!("\n\n# File: {}\n{}", path.display(), file_content));
}
}
}
content
}

View File

@ -5,10 +5,10 @@ use std::path::{PathBuf};
use std::process::Command as OtherCommand;
use serde_json::json;
use seahorse::{Command, Context, Flag, FlagType};
use crate::chat::ask_chat;
use crate::git::{git_init, git_status};
use crate::config::ConfigPaths;
use crate::commands::git_repo::read_all_git_files;
pub fn mcp_setup() {
let config = ConfigPaths::new();
@ -153,13 +153,54 @@ fn chat_cmd() -> Command {
.description("OpenAI APIキー")
.alias("k"),
)
.flag(
Flag::new("repo", FlagType::String)
.description("Gitリポジトリのパスを指定 (すべてのコードを読み込む)")
.alias("r"),
)
.action(|c: &Context| {
if let Some(question) = c.args.get(0) {
ask_chat(c, question);
let response = ask_chat(c, question);
println!("💬 応答:\n{}", response);
} else {
eprintln!("❗ 質問が必要です: mcp chat 'こんにちは'");
}
})
.action(|c: &Context| {
let config = ConfigPaths::new();
if let Ok(repo_url) = c.string_flag("repo") {
let repo_base = config.base_dir.join("repos");
let repo_dir = repo_base.join(sanitize_repo_name(&repo_url));
if !repo_dir.exists() {
println!("📥 Gitリポジトリをクローン中: {}", repo_url);
let status = OtherCommand::new("git")
.args(&["clone", &repo_url, repo_dir.to_str().unwrap()])
.status()
.expect("❌ Gitのクローンに失敗しました");
assert!(status.success(), "Git clone エラー");
} else {
println!("✔ リポジトリはすでに存在します: {}", repo_dir.display());
}
//let files = read_all_git_files(&repo_dir);
let files = read_all_git_files(repo_dir.to_str().unwrap());
let prompt = format!(
"以下のコードベースを読み込んで、改善案や次のステップを提案してください:\n{}",
files
);
let response = ask_chat(c, &prompt);
println!("💡 提案:\n{}", response);
} else {
if let Some(question) = c.args.get(0) {
let response = ask_chat(c, question);
println!("💬 {}", response);
} else {
eprintln!("❗ 質問が必要です: mcp chat 'こんにちは'");
}
}
})
}
fn init_cmd() -> Command {
@ -200,3 +241,8 @@ pub fn mcp_cmd() -> Command {
.command(setup_cmd())
.command(set_api_key_cmd())
}
// ファイル名として安全な形に変換
fn sanitize_repo_name(repo_url: &str) -> String {
repo_url.replace("://", "_").replace("/", "_").replace("@", "_")
}

View File

@ -1,3 +1,4 @@
pub mod db;
pub mod scheduler;
pub mod mcp;
pub mod git_repo;