Major refactoring: HTTP client unification and project restructuring
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 13m53s
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:
117
docs/README.md
Normal file
117
docs/README.md
Normal file
@ -0,0 +1,117 @@
|
||||
# ai.bot ドキュメント
|
||||
|
||||
ai.botプロジェクトの包括的なドキュメント集です。
|
||||
|
||||
## ドキュメント一覧
|
||||
|
||||
### 開発者向け
|
||||
|
||||
1. **[開発ガイド](./development-guide.md)**
|
||||
- プロジェクト概要とアーキテクチャ
|
||||
- 開発環境のセットアップ
|
||||
- 開発ワークフローとベストプラクティス
|
||||
- 新機能追加の手順
|
||||
|
||||
2. **[HTTPクライアントAPI](./http-client-api.md)**
|
||||
- HttpClientモジュールの完全なAPIリファレンス
|
||||
- 使用例とサンプルコード
|
||||
- エラーハンドリングのベストプラクティス
|
||||
|
||||
### 保守・運用向け
|
||||
|
||||
3. **[移行ガイド](./migration-guide.md)**
|
||||
- パッケージ名・CLI名の変更詳細
|
||||
- 段階的移行手順
|
||||
- 後方互換性の説明
|
||||
- トラブルシューティング
|
||||
|
||||
4. **[リファクタリングサマリー](./refactoring-summary.md)**
|
||||
- HTTPクライアント共通化の詳細
|
||||
- エラーハンドリング改善の記録
|
||||
- 対象ファイル一覧とBefore/After
|
||||
|
||||
## クイックスタート
|
||||
|
||||
### 1. 基本セットアップ
|
||||
```bash
|
||||
# 依存関係インストール
|
||||
cargo install cargo-make
|
||||
|
||||
# プロジェクトビルド
|
||||
cargo build
|
||||
|
||||
# テスト実行
|
||||
cargo test
|
||||
```
|
||||
|
||||
### 2. CLI使用方法
|
||||
```bash
|
||||
# 新しいコマンド(推奨)
|
||||
./target/debug/aibot --help
|
||||
|
||||
# 旧コマンド(互換性)
|
||||
./target/debug/ai --help
|
||||
```
|
||||
|
||||
### 3. 設定ディレクトリ
|
||||
- **新**: `~/.config/syui/ai/bot/`
|
||||
- **旧**: `~/.config/ai/` (自動移行対応)
|
||||
|
||||
## 主要な変更履歴
|
||||
|
||||
### 2025年6月6日 - 大規模リファクタリング
|
||||
|
||||
#### HTTPクライアント共通化
|
||||
- 24個のファイルをHttpClientモジュールで統合
|
||||
- コード重複を大幅削減
|
||||
- エラーハンドリングを改善(`.unwrap()` → `match`)
|
||||
|
||||
#### 命名規則統一
|
||||
- パッケージ名: `ai` → `aibot`
|
||||
- CLI名: `ai` → `aibot`(`ai`は互換性維持)
|
||||
- 設定ディレクトリ: `~/.config/ai/` → `~/.config/syui/ai/bot/`
|
||||
|
||||
#### テストインフラ構築
|
||||
- ユニットテストの追加
|
||||
- cargo-makeによるタスク管理
|
||||
- CI/CD対応の準備
|
||||
|
||||
## アーキテクチャ概要
|
||||
|
||||
```
|
||||
ai.bot/
|
||||
├── src/
|
||||
│ ├── main.rs # メインCLI (aibot)
|
||||
│ ├── alias.rs # 互換性CLI (ai)
|
||||
│ ├── http_client.rs # 統合HTTPクライアント
|
||||
│ ├── data.rs # 設定・データ管理
|
||||
│ ├── game/ # ゲーム機能
|
||||
│ └── tests/ # テストスイート
|
||||
├── docs/ # ドキュメント
|
||||
├── scripts/ # セットアップスクリプト
|
||||
└── ~/.config/syui/ai/bot/ # 設定ディレクトリ
|
||||
├── scpt/ # コマンドスクリプト
|
||||
└── txt/ # ログファイル
|
||||
```
|
||||
|
||||
## サポート・問い合わせ
|
||||
|
||||
### 開発関連
|
||||
- 新機能追加: [開発ガイド](./development-guide.md)を参照
|
||||
- API使用方法: [HTTPクライアントAPI](./http-client-api.md)を参照
|
||||
|
||||
### 移行・運用関連
|
||||
- 移行作業: [移行ガイド](./migration-guide.md)を参照
|
||||
- トラブル: 各ドキュメントのトラブルシューティング章を参照
|
||||
|
||||
### 履歴・詳細
|
||||
- 実装詳細: [リファクタリングサマリー](./refactoring-summary.md)を参照
|
||||
|
||||
## 貢献ガイドライン
|
||||
|
||||
1. **コードスタイル**: `cargo fmt`でフォーマット必須
|
||||
2. **テスト**: 新機能には対応するテストを追加
|
||||
3. **エラーハンドリング**: `.unwrap()`の使用禁止
|
||||
4. **ドキュメント**: 重要な変更は対応ドキュメントも更新
|
||||
|
||||
詳細は[開発ガイド](./development-guide.md)の「コントリビューション」章を参照してください。
|
332
docs/development-guide.md
Normal file
332
docs/development-guide.md
Normal file
@ -0,0 +1,332 @@
|
||||
# ai.bot 開発ガイド
|
||||
|
||||
## プロジェクト概要
|
||||
|
||||
ai.botは、Rust製のBluesky(AT Protocol)ボットです。メンション応答、コマンド実行、OpenAI統合などの機能を提供します。
|
||||
|
||||
**重要**: 2025年6月6日より、パッケージ名とCLI名が統一されました:
|
||||
- **新CLI名**: `aibot` (推奨)
|
||||
- **旧CLI名**: `ai` (互換性維持)
|
||||
- **設定ディレクトリ**: `~/.config/syui/ai/bot/` (旧: `~/.config/ai/`)
|
||||
|
||||
詳細は[移行ガイド](./migration-guide.md)を参照してください。
|
||||
|
||||
## アーキテクチャ
|
||||
|
||||
### 主要コンポーネント
|
||||
|
||||
1. **HTTPクライアント** (`src/http_client.rs`)
|
||||
- AT Protocol API呼び出しの統一インターフェース
|
||||
- 認証処理の自動化
|
||||
- エラーハンドリングの標準化
|
||||
|
||||
2. **コマンドシステム** (`src/main.rs`)
|
||||
- Seahorseを使用したCLIインターフェース
|
||||
- 各機能への振り分け
|
||||
|
||||
3. **AT Protocolモジュール**
|
||||
- 投稿、フォロー、いいね等の基本機能
|
||||
- 認証・セッション管理
|
||||
- フィード・通知処理
|
||||
|
||||
4. **ゲームシステム** (`src/game/`)
|
||||
- カードゲーム機能
|
||||
- ユーザー管理
|
||||
- ゲームデータ処理
|
||||
|
||||
5. **外部連携**
|
||||
- OpenAI API統合 (`src/openai.rs`)
|
||||
- 画像処理機能
|
||||
|
||||
## 開発環境セットアップ
|
||||
|
||||
### 必要なツール
|
||||
```bash
|
||||
# Rust(最新安定版)
|
||||
rustup update stable
|
||||
|
||||
# Cargo make(タスクランナー)
|
||||
cargo install cargo-make
|
||||
|
||||
# 開発用依存関係は自動インストール
|
||||
```
|
||||
|
||||
**注意**: 初回は`cargo install cargo-make`でcargo-makeのインストールが必要です。
|
||||
|
||||
### 設定ファイル
|
||||
```
|
||||
~/.config/syui/ai/bot/config.toml # 基本設定
|
||||
~/.config/syui/ai/bot/refresh # リフレッシュトークン
|
||||
~/.config/syui/ai/bot/access # アクセストークン
|
||||
```
|
||||
|
||||
**注意**: 旧設定ディレクトリ(`~/.config/ai/`)も自動的に参照・移行されます。
|
||||
|
||||
## 開発ワークフロー
|
||||
|
||||
### 1. コード変更
|
||||
```bash
|
||||
# フォーマット
|
||||
cargo fmt
|
||||
|
||||
# コンパイル確認
|
||||
cargo check
|
||||
|
||||
# テスト実行
|
||||
cargo test
|
||||
```
|
||||
|
||||
### 2. 統合ワークフロー
|
||||
```bash
|
||||
# 全体フロー(フォーマット→ビルド→テスト)
|
||||
cargo make my-flow
|
||||
```
|
||||
|
||||
### 3. 個別テスト
|
||||
```bash
|
||||
cargo make test-quick # 素早いテスト
|
||||
cargo make test-verbose # 詳細出力テスト
|
||||
```
|
||||
|
||||
## API呼び出しパターン
|
||||
|
||||
### HttpClientの使用方法
|
||||
|
||||
#### 基本的なGETリクエスト
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
|
||||
pub async fn get_user_profile(handle: String) -> String {
|
||||
let client = HttpClient::new();
|
||||
let url = format!("https://bsky.social/xrpc/app.bsky.actor.getProfile?actor={}", handle);
|
||||
|
||||
match client.get_with_auth(&url).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error getting profile: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### JSONを送信するPOSTリクエスト
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
use serde_json::json;
|
||||
|
||||
pub async fn create_post(text: String) -> String {
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/com.atproto.repo.createRecord";
|
||||
|
||||
let payload = json!({
|
||||
"repo": "user.handle",
|
||||
"collection": "app.bsky.feed.post",
|
||||
"record": {
|
||||
"text": text,
|
||||
"createdAt": chrono::Utc::now().to_rfc3339()
|
||||
}
|
||||
});
|
||||
|
||||
match client.post_json_with_auth(&url, &payload).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error creating post: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## テストの書き方
|
||||
|
||||
### ユニットテスト
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_http_client_creation() {
|
||||
let client = HttpClient::new();
|
||||
// テストロジック
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_data_parsing() {
|
||||
// 同期テスト
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 統合テスト
|
||||
```rust
|
||||
#[tokio::test]
|
||||
#[ignore] // 通常は無視、環境変数設定時のみ実行
|
||||
async fn test_real_api() {
|
||||
if std::env::var("RUN_INTEGRATION_TESTS").is_ok() {
|
||||
// 実際のAPI呼び出しテスト
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## エラーハンドリングガイドライン
|
||||
|
||||
### 推奨パターン
|
||||
```rust
|
||||
// Good: 適切なエラーハンドリング
|
||||
match api_call().await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error in operation: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
// Bad: unwrapの使用
|
||||
api_call().await.unwrap()
|
||||
```
|
||||
|
||||
### エラーレスポンス
|
||||
- API呼び出し失敗時は`"err"`文字列を返す
|
||||
- ログにエラー詳細を出力
|
||||
- 上位レイヤーでエラー処理を継続
|
||||
|
||||
## 新機能追加の手順
|
||||
|
||||
### 1. AT Protocol関連機能
|
||||
```bash
|
||||
# 1. モジュールファイルを作成
|
||||
touch src/new_feature.rs
|
||||
|
||||
# 2. main.rsに追加
|
||||
# pub mod new_feature;
|
||||
|
||||
# 3. HttpClientを使用して実装
|
||||
# 4. テストを追加
|
||||
# 5. main.rsのコマンドに追加
|
||||
```
|
||||
|
||||
### 2. 基本的なテンプレート
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
use crate::{data_toml, url};
|
||||
use serde_json::json;
|
||||
|
||||
pub async fn new_feature_request(param: String) -> String {
|
||||
let client = HttpClient::new();
|
||||
let endpoint_url = url(&"endpoint_name");
|
||||
|
||||
let payload = json!({
|
||||
"param": param
|
||||
});
|
||||
|
||||
match client.post_json_with_auth(&endpoint_url, &payload).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error in new_feature: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_new_feature() {
|
||||
// テスト実装
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## デバッグ方法
|
||||
|
||||
### 1. ログ出力
|
||||
```rust
|
||||
eprintln!("Debug: {}", variable);
|
||||
println!("Info: {}", message);
|
||||
```
|
||||
|
||||
### 2. テスト実行
|
||||
```bash
|
||||
# 特定のテストのみ
|
||||
cargo test test_name
|
||||
|
||||
# 詳細出力
|
||||
cargo test -- --nocapture
|
||||
|
||||
# 特定モジュール
|
||||
cargo test module_name
|
||||
```
|
||||
|
||||
### 3. HTTPリクエストのデバッグ
|
||||
HttpClientモジュール内でリクエスト/レスポンスをログ出力することで、API呼び出しの詳細を確認できます。
|
||||
|
||||
## パフォーマンス考慮事項
|
||||
|
||||
### HttpClientの再利用
|
||||
```rust
|
||||
// Good: 一度作成して再利用
|
||||
let client = HttpClient::new();
|
||||
for item in items {
|
||||
client.get_with_auth(&url).await;
|
||||
}
|
||||
|
||||
// Bad: 毎回新規作成
|
||||
for item in items {
|
||||
let client = HttpClient::new();
|
||||
client.get_with_auth(&url).await;
|
||||
}
|
||||
```
|
||||
|
||||
### 非同期処理
|
||||
- 可能な限り並列処理を活用
|
||||
- await呼び出しを最小限に
|
||||
- tokio::joinやfutures::join_allの活用
|
||||
|
||||
## トラブルシューティング
|
||||
|
||||
### よくある問題
|
||||
|
||||
1. **認証エラー**
|
||||
- トークンの有効期限切れ
|
||||
- 設定ファイルの不備
|
||||
|
||||
2. **コンパイルエラー**
|
||||
- 型不整合(特にライフタイム)
|
||||
- 未使用のimport
|
||||
|
||||
3. **実行時エラー**
|
||||
- ネットワーク接続問題
|
||||
- API仕様変更
|
||||
|
||||
### 解決方法
|
||||
```bash
|
||||
# 設定確認(新ディレクトリ)
|
||||
ls -la ~/.config/syui/ai/bot/
|
||||
# 旧ディレクトリも確認
|
||||
ls -la ~/.config/ai/
|
||||
|
||||
# トークンリフレッシュ
|
||||
./target/debug/aibot refresh
|
||||
# または互換性コマンド
|
||||
./target/debug/ai refresh
|
||||
|
||||
# 詳細ログ
|
||||
RUST_LOG=debug ./target/debug/aibot [command]
|
||||
```
|
||||
|
||||
## コントリビューション
|
||||
|
||||
1. コードフォーマット必須: `cargo fmt`
|
||||
2. テスト追加必須: 新機能には対応テスト
|
||||
3. エラーハンドリング必須: `.unwrap()`の使用禁止
|
||||
4. ドキュメント更新: 重要な変更は本ドキュメントも更新
|
||||
|
||||
## 関連ドキュメント
|
||||
|
||||
- [リファクタリングサマリー](./refactoring-summary.md)
|
||||
- [AT Protocol仕様](https://atproto.com/)
|
||||
- [Rust公式ドキュメント](https://doc.rust-lang.org/)
|
334
docs/http-client-api.md
Normal file
334
docs/http-client-api.md
Normal file
@ -0,0 +1,334 @@
|
||||
# HttpClient API リファレンス
|
||||
|
||||
## 概要
|
||||
|
||||
`HttpClient`は、AT Protocol APIへの統一されたHTTPクライアントインターフェースです。認証、エラーハンドリング、リクエスト管理を自動化します。
|
||||
|
||||
## 基本的な使用方法
|
||||
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
|
||||
let client = HttpClient::new();
|
||||
// または
|
||||
let client = HttpClient::default();
|
||||
```
|
||||
|
||||
## APIメソッド
|
||||
|
||||
### 認証付きリクエスト
|
||||
|
||||
#### get_with_auth
|
||||
AT Protocolの認証が必要なGETリクエストを実行します。
|
||||
|
||||
```rust
|
||||
pub async fn get_with_auth(&self, url: &str) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `url`: リクエスト先のURL
|
||||
|
||||
**戻り値:**
|
||||
- `Ok(String)`: レスポンスボディ(文字列)
|
||||
- `Err(Error)`: リクエストエラー
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/app.bsky.actor.getProfile?actor=user.bsky.social";
|
||||
|
||||
match client.get_with_auth(&url).await {
|
||||
Ok(response) => println!("Response: {}", response),
|
||||
Err(e) => eprintln!("Error: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
#### post_json_with_auth
|
||||
AT Protocolの認証が必要なPOSTリクエスト(JSON)を実行します。
|
||||
|
||||
```rust
|
||||
pub async fn post_json_with_auth<T: Serialize>(&self, url: &str, json: &T) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `url`: リクエスト先のURL
|
||||
- `json`: シリアライズ可能なJSONデータ
|
||||
|
||||
**戻り値:**
|
||||
- `Ok(String)`: レスポンスボディ(文字列)
|
||||
- `Err(Error)`: リクエストエラー
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
use serde_json::json;
|
||||
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/com.atproto.repo.createRecord";
|
||||
let payload = json!({
|
||||
"repo": "user.bsky.social",
|
||||
"collection": "app.bsky.feed.post",
|
||||
"record": {
|
||||
"text": "Hello, World!",
|
||||
"createdAt": "2025-01-01T00:00:00Z"
|
||||
}
|
||||
});
|
||||
|
||||
match client.post_json_with_auth(&url, &payload).await {
|
||||
Ok(response) => println!("Post created: {}", response),
|
||||
Err(e) => eprintln!("Error: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
#### delete_with_auth
|
||||
AT Protocolの認証が必要なDELETEリクエストを実行します。
|
||||
|
||||
```rust
|
||||
pub async fn delete_with_auth(&self, url: &str) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `url`: リクエスト先のURL
|
||||
|
||||
**戻り値:**
|
||||
- `Ok(String)`: レスポンスボディ(文字列)
|
||||
- `Err(Error)`: リクエストエラー
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/com.atproto.repo.deleteRecord";
|
||||
|
||||
match client.delete_with_auth(&url).await {
|
||||
Ok(response) => println!("Deleted: {}", response),
|
||||
Err(e) => eprintln!("Error: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
### 認証なしリクエスト
|
||||
|
||||
#### get
|
||||
認証なしのGETリクエストを実行します。
|
||||
|
||||
```rust
|
||||
pub async fn get(&self, url: &str) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
let client = HttpClient::new();
|
||||
let url = "https://public-api.example.com/data";
|
||||
|
||||
match client.get(&url).await {
|
||||
Ok(response) => println!("Response: {}", response),
|
||||
Err(e) => eprintln!("Error: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
#### post_json
|
||||
認証なしのPOSTリクエスト(JSON)を実行します。ログイン処理などで使用。
|
||||
|
||||
```rust
|
||||
pub async fn post_json<T: Serialize>(&self, url: &str, json: &T) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
use serde_json::json;
|
||||
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/com.atproto.session.create";
|
||||
let credentials = json!({
|
||||
"identifier": "user.bsky.social",
|
||||
"password": "password"
|
||||
});
|
||||
|
||||
match client.post_json(&url, &credentials).await {
|
||||
Ok(response) => println!("Login successful: {}", response),
|
||||
Err(e) => eprintln!("Login failed: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
### カスタムヘッダー付きリクエスト
|
||||
|
||||
#### post_with_headers
|
||||
カスタムヘッダーを指定したPOSTリクエストを実行します。
|
||||
|
||||
```rust
|
||||
pub async fn post_with_headers<T: Serialize>(
|
||||
&self,
|
||||
url: &str,
|
||||
json: &T,
|
||||
headers: Vec<(&str, &str)>
|
||||
) -> Result<String, Error>
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `url`: リクエスト先のURL
|
||||
- `json`: シリアライズ可能なJSONデータ
|
||||
- `headers`: ヘッダーのキーと値のペアのベクター
|
||||
|
||||
**使用例:**
|
||||
```rust
|
||||
use serde_json::json;
|
||||
|
||||
let client = HttpClient::new();
|
||||
let url = "https://bsky.social/xrpc/com.atproto.session.refresh";
|
||||
let refresh_token = "refresh_token_value";
|
||||
let auth_header = format!("Bearer {}", refresh_token);
|
||||
let headers = vec![("Authorization", auth_header.as_str())];
|
||||
let empty_json = json!({});
|
||||
|
||||
match client.post_with_headers(&url, &empty_json, headers).await {
|
||||
Ok(response) => println!("Token refreshed: {}", response),
|
||||
Err(e) => eprintln!("Refresh failed: {}", e),
|
||||
}
|
||||
```
|
||||
|
||||
## 内部実装詳細
|
||||
|
||||
### 認証処理
|
||||
認証付きメソッドは自動的に以下を実行します:
|
||||
|
||||
1. `data_refresh(&"access")`でアクセストークンを取得
|
||||
2. `Authorization: Bearer {token}`ヘッダーを自動追加
|
||||
3. リクエストを実行
|
||||
|
||||
### エラーハンドリング
|
||||
- ネットワークエラー
|
||||
- HTTPステータスエラー
|
||||
- JSON解析エラー
|
||||
- タイムアウトエラー
|
||||
|
||||
すべて`reqwest::Error`として統一されて返されます。
|
||||
|
||||
## 使用上の注意
|
||||
|
||||
### 1. トークン管理
|
||||
- アクセストークンは自動的に取得されます
|
||||
- トークンの有効期限切れは呼び出し元で処理する必要があります
|
||||
- リフレッシュトークンは`post_with_headers`で手動設定
|
||||
|
||||
### 2. エラー処理パターン
|
||||
```rust
|
||||
// 推奨パターン
|
||||
match client.get_with_auth(&url).await {
|
||||
Ok(response) => {
|
||||
// 成功処理
|
||||
response
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("API call failed: {}", e);
|
||||
"err".to_string() // 既存コードとの互換性
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. パフォーマンス
|
||||
- `HttpClient`の作成は軽量な操作です
|
||||
- 内部でrequwestクライアントを再利用しています
|
||||
- 複数のリクエストで同じインスタンスを使い回すことも可能
|
||||
|
||||
### 4. デバッグ
|
||||
リクエスト/レスポンスの詳細をログ出力したい場合:
|
||||
```rust
|
||||
// HttpClientモジュール内で適宜printlnを追加
|
||||
println!("Request URL: {}", url);
|
||||
println!("Response: {}", response);
|
||||
```
|
||||
|
||||
## 実装例:新しいAPI呼び出しモジュール
|
||||
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
use crate::{data_toml, url};
|
||||
use serde_json::json;
|
||||
use iso8601_timestamp::Timestamp;
|
||||
|
||||
pub async fn create_custom_record(data: String) -> String {
|
||||
let did = data_toml(&"did");
|
||||
let handle = data_toml(&"handle");
|
||||
let url = url(&"record_create");
|
||||
|
||||
let timestamp = Timestamp::now_utc().to_string();
|
||||
|
||||
let payload = json!({
|
||||
"repo": handle,
|
||||
"did": did,
|
||||
"collection": "app.bsky.custom.record",
|
||||
"record": {
|
||||
"data": data,
|
||||
"createdAt": timestamp
|
||||
}
|
||||
});
|
||||
|
||||
let client = HttpClient::new();
|
||||
match client.post_json_with_auth(&url, &payload).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error creating custom record: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_custom_record() {
|
||||
let result = create_custom_record("test data".to_string()).await;
|
||||
assert_ne!(result, "err");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 移行ガイド
|
||||
|
||||
### 既存コードからの移行
|
||||
|
||||
#### Before (旧実装)
|
||||
```rust
|
||||
extern crate reqwest;
|
||||
use crate::data_refresh;
|
||||
|
||||
pub async fn old_request() -> String {
|
||||
let token = data_refresh(&"access");
|
||||
let client = reqwest::Client::new();
|
||||
let res = client
|
||||
.post(url)
|
||||
.json(&data)
|
||||
.header("Authorization", "Bearer ".to_owned() + &token)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
res
|
||||
}
|
||||
```
|
||||
|
||||
#### After (新実装)
|
||||
```rust
|
||||
use crate::http_client::HttpClient;
|
||||
|
||||
pub async fn new_request() -> String {
|
||||
let client = HttpClient::new();
|
||||
match client.post_json_with_auth(&url, &data).await {
|
||||
Ok(response) => response,
|
||||
Err(e) => {
|
||||
eprintln!("Error in request: {}", e);
|
||||
"err".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### チェックリスト
|
||||
- [ ] `extern crate reqwest;`を削除
|
||||
- [ ] `use crate::http_client::HttpClient;`を追加
|
||||
- [ ] `data_refresh(&"access")`の手動呼び出しを削除
|
||||
- [ ] `reqwest::Client::new()`を`HttpClient::new()`に置換
|
||||
- [ ] `.unwrap()`を適切な`match`文に置換
|
||||
- [ ] エラーメッセージの追加
|
271
docs/migration-guide.md
Normal file
271
docs/migration-guide.md
Normal file
@ -0,0 +1,271 @@
|
||||
# ai.bot 段階的移行ガイド
|
||||
|
||||
## 概要
|
||||
|
||||
ai.botプロジェクトの命名規則とディレクトリ構造を統一するための段階的移行作業の記録です。
|
||||
|
||||
## 移行内容
|
||||
|
||||
### 1. パッケージ・CLI名の変更
|
||||
|
||||
| 項目 | 変更前 | 変更後 |
|
||||
|------|--------|--------|
|
||||
| パッケージ名 | `ai` | `aibot` |
|
||||
| メインCLI | `ai` | `aibot` |
|
||||
| 互換性CLI | - | `ai` (aibotへのエイリアス) |
|
||||
|
||||
### 2. ディレクトリ構造の変更
|
||||
|
||||
| 用途 | 変更前 | 変更後 |
|
||||
|------|--------|--------|
|
||||
| 設定ディレクトリ | `~/.config/ai/` | `~/.config/syui/ai/bot/` |
|
||||
| ログディレクトリ | `~/.config/ai/txt/` | `~/.config/syui/ai/bot/txt/` |
|
||||
| スクリプトディレクトリ | `~/.config/ai/scpt/` | `~/.config/syui/ai/bot/scpt/` |
|
||||
|
||||
## 実装した機能
|
||||
|
||||
### 1. デュアルバイナリシステム
|
||||
|
||||
#### Cargo.toml設定
|
||||
```toml
|
||||
[package]
|
||||
name = "aibot"
|
||||
description = "ai.bot - Bluesky AT Protocol Bot"
|
||||
|
||||
[[bin]]
|
||||
name = "aibot"
|
||||
path = "src/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "ai"
|
||||
path = "src/alias.rs"
|
||||
```
|
||||
|
||||
#### エイリアスバイナリ (src/alias.rs)
|
||||
- `ai`コマンドが`aibot`を自動的に呼び出し
|
||||
- 同一ディレクトリ内のaibotバイナリを優先検索
|
||||
- PATH内のaibotもフォールバック対応
|
||||
|
||||
### 2. 設定ディレクトリの自動移行
|
||||
|
||||
#### data_file関数の改良
|
||||
```rust
|
||||
pub fn data_file(s: &str) -> String {
|
||||
// 新しい設定ディレクトリ(優先)
|
||||
let new_config_dir = "/.config/syui/ai/bot/";
|
||||
// 旧設定ディレクトリ(互換性のため)
|
||||
let old_config_dir = "/.config/ai/";
|
||||
|
||||
// 自動移行ロジック
|
||||
// 1. 新しいパスにファイルが存在 → 新しいパスを使用
|
||||
// 2. 旧パスのみに存在 → 新しいパスにコピーして使用
|
||||
// 3. どちらにも存在しない → 新しいパスを使用
|
||||
}
|
||||
```
|
||||
|
||||
#### log_file関数も同様の移行対応
|
||||
|
||||
### 3. 移行セットアップスクリプト
|
||||
|
||||
#### scripts/setup-migration.sh
|
||||
```bash
|
||||
#!/bin/bash
|
||||
# 新しい設定ディレクトリの作成
|
||||
mkdir -p ~/.config/syui/ai/bot/
|
||||
|
||||
# スクリプトディレクトリのコピー
|
||||
cp -r ~/.config/ai/scpt ~/.config/syui/ai/bot/
|
||||
|
||||
# エイリアス設定の提案
|
||||
echo "alias ai='aibot'"
|
||||
```
|
||||
|
||||
## 後方互換性
|
||||
|
||||
### 1. 設定ファイル
|
||||
- **自動移行**: 旧パスから新パスへ自動コピー
|
||||
- **継続読み込み**: 移行後も旧パスは参照可能
|
||||
- **透過的**: ユーザーは変更を意識する必要なし
|
||||
|
||||
### 2. コマンドライン
|
||||
- **aiコマンド**: 既存スクリプトで引き続き使用可能
|
||||
- **aibotコマンド**: 新しい正式名称
|
||||
- **完全互換**: 引数、オプション、出力すべて同一
|
||||
|
||||
### 3. スクリプト
|
||||
- **shellscript**: `ai`コマンドをそのまま使用可能
|
||||
- **エイリアス推奨**: `alias ai='aibot'`で統一
|
||||
|
||||
## 移行手順
|
||||
|
||||
### 1. 自動移行(推奨)
|
||||
|
||||
```bash
|
||||
# プロジェクトをビルド
|
||||
cargo build
|
||||
|
||||
# 移行スクリプトを実行
|
||||
./scripts/setup-migration.sh
|
||||
|
||||
# 新しいバイナリを使用
|
||||
./target/debug/aibot --help
|
||||
./target/debug/ai --help # 互換性確認
|
||||
```
|
||||
|
||||
### 2. 手動移行
|
||||
|
||||
```bash
|
||||
# 1. 新しい設定ディレクトリ作成
|
||||
mkdir -p ~/.config/syui/ai/bot/
|
||||
|
||||
# 2. スクリプトディレクトリのコピー
|
||||
cp -r ~/.config/ai/scpt ~/.config/syui/ai/bot/
|
||||
|
||||
# 3. バイナリのインストール
|
||||
cargo install --path .
|
||||
|
||||
# 4. エイリアス設定(オプション)
|
||||
echo "alias ai='aibot'" >> ~/.zshrc
|
||||
```
|
||||
|
||||
### 3. 段階的移行(企業環境等)
|
||||
|
||||
```bash
|
||||
# Phase 1: 新しいバイナリの導入
|
||||
cargo install --path . --bin aibot
|
||||
|
||||
# Phase 2: エイリアス設定
|
||||
echo "alias ai='aibot'" >> ~/.profile
|
||||
|
||||
# Phase 3: スクリプトの段階的更新
|
||||
# (既存スクリプトは変更不要)
|
||||
|
||||
# Phase 4: 旧設定の完全移行(任意のタイミング)
|
||||
rm -rf ~/.config/ai/ # 十分な検証後
|
||||
```
|
||||
|
||||
## 影響範囲
|
||||
|
||||
### 1. 変更が必要な箇所
|
||||
- ❌ **なし** (完全後方互換)
|
||||
|
||||
### 2. 変更が推奨される箇所
|
||||
- 📝 shellrcでのエイリアス設定
|
||||
- 📝 ドキュメント内のコマンド例
|
||||
- 📝 CI/CDスクリプト(新しい名前使用)
|
||||
|
||||
### 3. 変更が不要な箇所
|
||||
- ✅ 既存のshellscript
|
||||
- ✅ 設定ファイル
|
||||
- ✅ ログファイル
|
||||
- ✅ git submodule
|
||||
|
||||
## トラブルシューティング
|
||||
|
||||
### 1. 設定ファイルが見つからない
|
||||
|
||||
```bash
|
||||
# 現在の設定ディレクトリを確認
|
||||
ls -la ~/.config/syui/ai/bot/
|
||||
ls -la ~/.config/ai/ # 旧ディレクトリ
|
||||
|
||||
# 手動でコピー
|
||||
cp ~/.config/ai/token.json ~/.config/syui/ai/bot/
|
||||
```
|
||||
|
||||
### 2. aiコマンドが動作しない
|
||||
|
||||
```bash
|
||||
# aibotバイナリの存在確認
|
||||
which aibot
|
||||
./target/debug/aibot --help
|
||||
|
||||
# エイリアスの設定
|
||||
alias ai='aibot'
|
||||
# または
|
||||
export PATH="$(pwd)/target/debug:$PATH"
|
||||
```
|
||||
|
||||
### 3. スクリプトディレクトリが見つからない
|
||||
|
||||
```bash
|
||||
# 手動でコピー
|
||||
cp -r ~/.config/ai/scpt ~/.config/syui/ai/bot/
|
||||
|
||||
# gitサブモジュールの場合
|
||||
cd ~/.config/syui/ai/bot/scpt
|
||||
git remote -v # リモートURLを確認
|
||||
```
|
||||
|
||||
## 検証方法
|
||||
|
||||
### 1. 基本動作確認
|
||||
|
||||
```bash
|
||||
# 新しいコマンド
|
||||
aibot --help
|
||||
aibot post "Test from aibot"
|
||||
|
||||
# 互換性確認
|
||||
ai --help
|
||||
ai post "Test from ai alias"
|
||||
|
||||
# 設定ディレクトリ確認
|
||||
ls -la ~/.config/syui/ai/bot/
|
||||
```
|
||||
|
||||
### 2. 自動移行確認
|
||||
|
||||
```bash
|
||||
# 旧設定で動作確認
|
||||
touch ~/.config/ai/test_file
|
||||
aibot some_command # 新ディレクトリにコピーされることを確認
|
||||
ls -la ~/.config/syui/ai/bot/test_file
|
||||
```
|
||||
|
||||
### 3. スクリプト互換性確認
|
||||
|
||||
```bash
|
||||
# 既存スクリプトの動作確認
|
||||
cd ~/.config/syui/ai/bot/scpt
|
||||
./ai.zsh # aiコマンドが正常動作することを確認
|
||||
```
|
||||
|
||||
## 今後の予定
|
||||
|
||||
### Phase 1 (完了)
|
||||
- ✅ パッケージ名の変更
|
||||
- ✅ デュアルバイナリシステム
|
||||
- ✅ 設定ディレクトリの自動移行
|
||||
- ✅ 後方互換性の確保
|
||||
|
||||
### Phase 2 (将来)
|
||||
- 📅 ドキュメントの更新
|
||||
- 📅 CI/CDの新コマンド対応
|
||||
- 📅 パフォーマンス最適化
|
||||
|
||||
### Phase 3 (任意)
|
||||
- 📅 旧設定ディレクトリの削除
|
||||
- 📅 エイリアスバイナリの削除
|
||||
- 📅 完全な新体系への移行
|
||||
|
||||
## 関連ファイル
|
||||
|
||||
- `Cargo.toml` - パッケージ設定
|
||||
- `src/alias.rs` - エイリアスバイナリ
|
||||
- `src/data.rs` - 設定ディレクトリ管理
|
||||
- `scripts/setup-migration.sh` - 移行スクリプト
|
||||
- `docs/migration-guide.md` - 本ドキュメント
|
||||
|
||||
## 注意事項
|
||||
|
||||
1. **gitサブモジュール**: scptディレクトリがgitサブモジュールの場合、リモートURLの確認が必要
|
||||
2. **権限**: 設定ディレクトリの権限は適切に設定すること
|
||||
3. **バックアップ**: 重要な設定は移行前にバックアップを推奨
|
||||
4. **テスト**: 本番環境での使用前に十分なテストを実施
|
||||
|
||||
## 参考情報
|
||||
|
||||
- [開発ガイド](./development-guide.md)
|
||||
- [リファクタリングサマリー](./refactoring-summary.md)
|
||||
- [HTTPクライアントAPI](./http-client-api.md)
|
208
docs/refactoring-summary.md
Normal file
208
docs/refactoring-summary.md
Normal 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通信部分が大幅に改善され、今後の開発・保守が容易になりました。
|
Reference in New Issue
Block a user