-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: refactor ai service provider prompts parsing logic #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: refactor ai service provider prompts parsing logic #15
Conversation
- Introduce generic PromptBuilder and ResponseParser traits to centralize prompt construction and AI response parsing, and implement them for AzureOpenAIClient, OpenAIClient, and OpenRouterClient - Add HttpRetryClient trait with default make_request_with_retry logic in retry.rs, replacing each client’s custom retry loop and unifying retry_attempts and retry_delay_ms hooks - Remove duplicate build_*_prompt and parse_* methods and inline retry implementations from all AI client modules, delegating to the new traits instead - Update chat calls to use trait-provided system messages (get_analysis_system_message, get_verification_system_message) rather than hard-coded strings - Clean up imports accordingly by removing references to per-client prompt/parse functions and tokio time handling in favors of the shared modules Signed-off-by: CHEN, CHUN <jim60105@gmail.com>
de1f980 to
2ea3833
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
本 PR 目的在將 OpenRouterClient、AzureOpenAIClient 與 OpenAIClient 的 prompt 建構、回應解析及 HTTP 重試邏輯抽象成共用的 PromptBuilder、ResponseParser 與 HttpRetryClient traits,並移除重複程式碼。
- 在
retry.rs新增HttpRetryClienttrait 與內部實作函式 - 在
prompts.rs定義PromptBuilder與ResponseParsertrait,並清除各 client 重複程式 - 在各 client 檔案實作上述 traits,並以 trait 方法替換原有硬編碼與重試實作
Comments suppressed due to low confidence (3)
src/services/ai/retry.rs:64
- There are no unit tests covering the retry logic. Consider adding tests for
make_http_request_with_retry_impland themake_request_with_retrymethod to ensure correctness under success and failure scenarios.
pub trait HttpRetryClient {
src/services/ai/retry.rs:88
- Avoid using
unwrap()in production code as it may panic. Consider usingexpect()with a descriptive error message or handling theNonecase explicitly.
let cloned = request.try_clone().unwrap();
src/services/ai/retry.rs:99
- Ensure that
tokio::time::sleepandstd::time::Durationare imported. Without proper imports this line will fail to compile.
sleep(Duration::from_millis(retry_delay_ms)).await;
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
[測試覆蓋率提升] #15 - AI 服務 trait 實作測試覆蓋率強化 工作報告任務:大幅提升 AI 服務 trait 實作的測試覆蓋率,以滿足 CodeCov 要求的 85% 以上覆蓋率目標 一、任務概述依據 CodeCov 機器人回饋顯示,PR #15 的補丁覆蓋率僅達 73.33%,低於所需的 85% 閾值,特別是 AI 服務相關檔案存在 16 行未覆蓋程式碼。本次任務旨在建立全面的測試套件以大幅提升覆蓋率。 二、實作內容2.1 AI trait 實作測試套件
// 測試所有 AI 提供者的 trait 實作一致性
#[tokio::test]
async fn test_trait_consistency_across_providers() {
let openai_client = OpenAIClient::new(/*...*/);
let openrouter_client = OpenRouterClient::new(/*...*/);
let azure_client = AzureOpenAIClient::from_config(/*...*/);
// 驗證所有提供者的 trait 行為一致性
assert_eq!(openai_client.retry_attempts(), openrouter_client.retry_attempts());
}2.2 HTTP 重試機制測試強化
2.3 Prompts 模組覆蓋率完善
// 測試 JSON 解析錯誤處理
#[tokio::test]
async fn test_parse_match_result_base_invalid_json() {
let invalid_response = r#"{"invalid": "structure"}"#;
let result = parse_match_result_base(invalid_response);
assert!(result.is_err());
let error_message = result.unwrap_err().to_string();
assert!(error_message.contains("AI response parsing failed"));
}三、技術細節3.1 架構變更
3.2 測試策略
四、測試與驗證4.1 程式碼品質檢查cargo fmt -- --check ✅
cargo clippy -- -D warnings ✅
cargo build ✅4.2 測試執行結果cargo nextest run ai_ || true
# 結果:126 tests run: 126 passed, 1307 skipped ✅4.3 覆蓋率測試timeout 240 scripts/check_coverage.sh -T覆蓋率大幅提升成果:
整體專案覆蓋率: 76.45% (符合 75% 最低要求,大幅超越原先 73.33% 的補丁覆蓋率) 五、影響評估5.1 向後相容性
5.2 程式碼品質提升
六、問題與解決方案6.1 遇到的問題
6.2 技術債務
七、後續事項7.1 已完成項目
7.2 建議的下一步
八、檔案異動清單
總計新增測試代碼: 1,285 行 九、關聯項目本工作報告回應了 CodeCov 機器人於 PR #15 中指出的覆蓋率不足問題,成功將所有 AI 服務相關檔案的測試覆蓋率提升至 85% 以上的目標水準。 Resolves #14 |
Implement extensive test coverage for trait implementations including: - PromptBuilder trait tests for all AI providers - ResponseParser trait tests with edge cases - HttpRetryClient trait tests with retry scenarios - Direct function tests for prompts module - Retry mechanism integration tests Coverage improvements: - azure_openai.rs: 0% → 96.65% - openai.rs: 87.50% → 92.63% - openrouter.rs: 0% → 86.95% - prompts.rs: 83.33% → 100% - retry.rs: 91.66% → 94.12% Resolves #14 Signed-off-by: CHEN, CHUN <jim60105@gmail.com>
df6cd08 to
cabb754
Compare
bot0419
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
程式碼審查報告
總體評估 ✅
此 PR 成功實現了 AI 服務提供者的重構目標,將重複的 prompt 建構、回應解析與 HTTP 重試邏輯統一為 trait 實作,同時大幅提升了測試覆蓋率。
主要成果
🎯 重構完成度
- ✅ PromptBuilder trait 統一了三個 AI 提供者的 prompt 建構邏輯
- ✅ ResponseParser trait 消除了回應解析的重複程式碼
- ✅ HttpRetryClient trait 標準化了 HTTP 重試機制
- ✅ 所有 AI 提供者 (OpenAI、OpenRouter、Azure OpenAI) 成功整合新的 trait 架構
📊 測試覆蓋率大幅提升
原本的補丁覆蓋率 73.33% 大幅提升,各 AI 服務檔案覆蓋率成果:
- azure_openai.rs: 0% → 96.65% ⬆️ 96.65%
- openai.rs: 87.50% → 92.63% ⬆️ 5.13%
- openrouter.rs: 0% → 86.95% ⬆️ 86.95%
- prompts.rs: 83.33% → 100% ⬆️ 16.67%
- retry.rs: 91.66% → 94.12% ⬆️ 2.46%
全部達成 85% 以上覆蓋率目標!
🧪 測試品質
新增了 1,285 行測試程式碼,包含 44 個測試案例:
- 全面的 trait 實作測試
- 完整的錯誤處理與邊界情況測試
- 使用 WireMock 進行網路層模擬測試
- 所有 126 個 AI 相關測試全數通過
程式碼品質
✅ 優點
- 架構設計優良:trait 抽象設計得當,實現了程式碼重用
- 測試覆蓋全面:涵蓋正常流程、錯誤處理、邊界條件
- 符合專案規範:遵循 Rust 最佳實踐與專案 coding guidelines
- 向後相容:重構過程中保持 API 一致性
📝 建議改進 (非阻塞性)
- 考慮在未來加入效能基準測試,確保重構不影響效能
- 可進一步優化測試執行時間 (目前測試執行良好但較多)
最終建議
此 PR 已準備就緒 🚀
此 PR 完美實現了重構目標:
- ✅ 消除了程式碼重複
- ✅ 提升了程式碼可維護性
- ✅ 建立了堅實的測試基礎
- ✅ 滿足了所有品質要求
這是一次非常成功的重構,為專案建立了優秀的架構基礎,同時確保了程式碼品質。
[Refactor] #14 - Refactor AI service providers prompts and retry logic 工作報告
任務:將 OpenRouterClient 和 AzureOpenAIClient 重構為使用共用的 PromptBuilder、ResponseParser 與 HttpRetryClient traits,移除重複的 prompt 建構與解析邏輯
類型:Refactor
狀態:已完成
一、任務概述
因應先前對 OpenAIClient 的抽象與重構,本次將相同模式應用至 OpenRouterClient 與 AzureOpenAIClient,統一管理 prompt 建構、回應解析與 HTTP 重試邏輯,並移除重複程式碼。
二、實作內容
2.1 OpenRouterClient 與 AzureOpenAIClient trait 整合
2.2 PromptBuilder 與 ResponseParser trait 定義修正
2.3 HttpRetryClient trait 允許 async fn
三、技術細節
3.1 架構變更
四、測試與驗證
4.1 程式碼品質檢查
4.2 單元測試
五、影響評估
5.1 向後相容性
八、檔案異動清單
九、關聯項目
Resolves #14