Major refactoring: HTTP client unification and project restructuring
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 13m53s

## HTTP Client Refactoring
- Create unified HttpClient module (src/http_client.rs)
- Refactor 24 files to use shared HTTP client
- Replace .unwrap() with proper error handling
- Eliminate code duplication in HTTP requests

## Project Restructuring
- Rename package: ai → aibot
- Add dual binary support: aibot (main) + ai (compatibility alias)
- Migrate config directory: ~/.config/ai/ → ~/.config/syui/ai/bot/
- Implement backward compatibility with automatic migration

## Testing Infrastructure
- Add unit tests for HttpClient
- Create test infrastructure with cargo-make
- Add test commands: test, test-quick, test-verbose

## Documentation
- Complete migration guide with step-by-step instructions
- Updated development guide with new structure
- HTTP client API reference documentation
- Comprehensive refactoring summary

## Files Changed
- Modified: 24 source files (HTTP client integration)
- Added: src/http_client.rs, src/alias.rs, src/tests/
- Added: 5 documentation files in docs/
- Added: migration setup script

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-06-06 23:47:12 +09:00
parent 998777d46a
commit a17d2c9d66
45 changed files with 1871 additions and 593 deletions

208
docs/refactoring-summary.md Normal file
View File

@ -0,0 +1,208 @@
# ai.bot リファクタリング作業サマリー
## 概要
2025年6月6日に実施されたai.botプロジェクトのリファクタリング作業の完全な記録です。
## 実施した作業
### 1. HTTPクライアントの共通化
- **作成したファイル**: `src/http_client.rs`
- **対象**: 24個のファイルを統合
- **削減**: 重複するHTTPリクエストコードを一箇所に集約
#### HttpClientモジュールの機能
```rust
// 認証付きリクエスト
client.get_with_auth(&url).await
client.post_json_with_auth(&url, &json_data).await
client.delete_with_auth(&url).await
// 認証なしリクエスト
client.get(&url).await
client.post_json(&url, &json_data).await
// カスタムヘッダー付きリクエスト
client.post_with_headers(&url, &json_data, headers).await
```
### 2. リファクタリング対象ファイル一覧
#### コアAPIファイル (8個)
- `src/post.rs` - 投稿作成
- `src/like.rs` - いいね機能
- `src/repost.rs` - リポスト機能
- `src/follow.rs` - フォロー/アンフォロー
- `src/reply.rs` - 返信機能
- `src/profile.rs` - プロフィール取得
- `src/delete_record.rs` - レコード削除
- `src/describe.rs` - ユーザー説明取得
#### 認証・セッション管理 (3個)
- `src/session.rs` - セッション管理
- `src/refresh.rs` - トークンリフレッシュ
- `src/token.rs` - ログイン認証
#### 通知・メンション (3個)
- `src/mention.rs` - メンション投稿
- `src/notify.rs` - 通知取得
- `src/notify_read.rs` - 通知既読
#### フィード・タイムライン (4個)
- `src/feed_get.rs` - フィード取得
- `src/timeline_author.rs` - 作者タイムライン
- `src/followers.rs` - フォロワー取得
- `src/follows.rs` - フォロー取得
#### 画像関連 (3個)
- `src/img.rs` - 画像投稿
- `src/img_reply.rs` - 画像付き返信
- `src/img_upload.rs` - 画像アップロード
#### リンク・リッチテキスト (3個)
- `src/post_link.rs` - リンク付き投稿
- `src/reply_link.rs` - リンク付き返信
- `src/reply_og.rs` - OGデータ付き返信
#### ゲームモジュール (5個)
- `src/game/post_card.rs` - ゲームカード投稿
- `src/game/post_card_verify.rs` - カード検証
- `src/game/post_game.rs` - ゲームデータ投稿
- `src/game/post_game_login.rs` - ゲームログイン
- `src/game/post_game_user.rs` - ゲームユーザーデータ
### 3. 除外したファイル
- `src/openai.rs` - OpenAI API用異なる認証方式のため
- `src/feed_watch.rs` - reqwest使用していないため
### 4. 共通の変更パターン
#### Before (変更前)
```rust
extern crate reqwest;
use crate::data_refresh;
pub async fn some_request() -> String {
let token = data_refresh(&"access");
let client = reqwest::Client::new();
let res = client
.post(url)
.json(&post)
.header("Authorization", "Bearer ".to_owned() + &token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
res
}
```
#### After (変更後)
```rust
use crate::http_client::HttpClient;
pub async fn some_request() -> String {
let client = HttpClient::new();
match client.post_json_with_auth(&url, &post).await {
Ok(response) => response,
Err(e) => {
eprintln!("Error: {}", e);
"err".to_string()
}
}
}
```
### 5. テストインフラの追加
#### 作成したテストファイル
- `src/tests/mod.rs` - テストモジュール宣言
- `src/tests/http_client_tests.rs` - HttpClientのテスト
#### テストコマンド
```bash
# 基本テスト
cargo test
# Makefileを使用したテスト
cargo make test # cleanしてからテスト
cargo make test-quick # 素早いテスト
cargo make test-verbose # 詳細出力
```
#### 追加した依存関係 (Cargo.toml)
```toml
[dev-dependencies]
mockito = "1.2"
tokio-test = "0.4"
```
## 改善された点
### コード品質
- **コード重複の削除**: 同じHTTPリクエストパターンの重複を排除
- **エラーハンドリング**: `.unwrap()`を適切な`match`文に置換
- **保守性**: HTTP関連のロジックが一箇所に集約
### 開発効率
- **変更容易性**: HTTPクライアントの変更が1ファイルで完結
- **テスト可能性**: ユニットテストの追加でバグ検出が容易
- **デバッグ性**: エラーメッセージの改善
### 安定性
- **パニック回避**: `.unwrap()`によるパニックを防止
- **エラー処理**: 適切なエラーレスポンスの返却
## 次のステップ(推奨)
### 1. bot.rsのリファクタリング (高優先度)
- 500行以上の巨大な関数を分割
- コマンド処理の構造化
- 深いif-else文の改善
### 2. 設定管理の統一 (中優先度)
- 複数の設定構造体の統合
- 設定ファイルパスの一元管理
### 3. main.rsの整理 (中優先度)
- コマンド定義の外部ファイル化
- モジュール構造の改善
## ファイル構造
```
src/
├── http_client.rs # 新規作成共通HTTPクライアント
├── tests/ # 新規作成:テストディレクトリ
│ ├── mod.rs
│ └── http_client_tests.rs
├── main.rs # 更新http_clientモジュール追加
├── Cargo.toml # 更新:テスト依存関係追加
├── Makefile.toml # 更新:テストコマンド追加
└── [24個のリファクタリング済みファイル]
```
## 注意事項
1. **OpenAI API**: `src/openai.rs`は意図的に除外(異なる認証方式)
2. **後方互換性**: 既存のAPI呼び出しは同じ形式を維持
3. **エラー処理**: "err"文字列を返すパターンは既存仕様に合わせて維持
## 検証方法
```bash
# コンパイル確認
cargo check
# テスト実行
cargo test
# フォーマット確認
cargo fmt --check
# 全体ビルド
cargo build
```
この作業により、ai.botプロジェクトのHTTP通信部分が大幅に改善され、今後の開発・保守が容易になりました。